/* Get Locations for BPartner * @param ctx context * @param C_BPartner_ID bp * @return array of locations */ public MBPartnerLocation[] GetForBPartner(Ctx ctx, int C_BPartner_ID, Trx trxName) { List <MBPartnerLocation> list = new List <MBPartnerLocation>(); String sql = "SELECT * FROM C_BPartner_Location WHERE C_BPartner_ID=" + C_BPartner_ID; DataSet ds = new DataSet(); try { ds = DataBase.DB.ExecuteDataset(sql, null, trxName); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow dr = ds.Tables[0].Rows[i]; //jz list.add(new MBPartnerLocation(ctx, dr, null)); list.Add(new MBPartnerLocation(ctx, dr, trxName)); } ds = null; } catch (Exception e) { _log.Log(Level.SEVERE, sql, e); } MBPartnerLocation[] retValue = new MBPartnerLocation[list.Count]; retValue = list.ToArray(); return(retValue); }
/// <summary> /// Get All Locations of a business partner sorted by updated descending. /// </summary> /// <returns>locations</returns> /// Writer - Mohit, Date - * May 2019. public MBPartnerLocation[] GetLocations() { MBPartnerLocation[] _locations = null; List <MBPartnerLocation> list = new List <MBPartnerLocation>(); String sql = "SELECT * FROM C_BPartner_Location WHERE C_BPartner_ID=" + GetC_BPartner_ID() + " AND IsActive='Y' ORDER BY Updated DESC "; DataSet ds = null; try { ds = DataBase.DB.ExecuteDataset(sql, null, Get_TrxName()); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow dr = ds.Tables[0].Rows[i]; list.Add(new MBPartnerLocation(GetCtx(), dr, Get_TrxName())); } ds = null; } catch (Exception e) { log.Log(Level.SEVERE, sql, e); } _locations = new MBPartnerLocation[list.Count]; _locations = list.ToArray(); return(_locations); }
/// <summary> /// Get explicit or first bill Location /// </summary> /// <param name="C_BPartner_Location_ID">optional explicit location</param> /// <returns>location or null</returns> public MBPartnerLocation GetLocation(int C_BPartner_Location_ID) { MBPartnerLocation[] locations = GetLocations(false); if (locations.Length == 0) { return(null); } MBPartnerLocation retValue = null; for (int i = 0; i < locations.Length; i++) { if (locations[i].GetC_BPartner_Location_ID() == C_BPartner_Location_ID) { return(locations[i]); } if (retValue == null && locations[i].IsBillTo()) { retValue = locations[i]; } } if (retValue == null) { return(locations[0]); } return(retValue); }
/**** * Before Save. * - Set Name * @param newRecord new * @return save */ protected override bool BeforeSave(bool newRecord) { if (GetC_Location_ID() == 0) { return(false); } // Set New Name if (!newRecord) { return(true); } MLocation address = GetLocation(true); _uniqueName = GetName(); if (_uniqueName != null && _uniqueName.Equals(".")) // default { _uniqueName = null; } _unique = 0; MakeUnique(address); // Check uniqueness MBPartnerLocation[] locations = GetForBPartner(GetCtx(), GetC_BPartner_ID(), Get_TrxName()); bool unique = locations.Length == 0; while (!unique) { unique = true; for (int i = 0; i < locations.Length; i++) { MBPartnerLocation location = locations[i]; if (location.GetC_BPartner_Location_ID() == Get_ID()) { continue; } if (_uniqueName.Equals(location.GetName())) { MakeUnique(address); unique = false; break; } } } SetName(_uniqueName); return(true); }
/** * Create BP Location from Lead * @return error message */ private String CreateBPLocation() { if (GetC_BPartner_Location_ID() != 0 || GetC_Country_ID() == 0) // mandatory { return(null); } // Something to save if ((GetAddress1() != null && GetAddress1().Length > 0) || (GetPostal() != null && GetPostal().Length > 0) || (GetCity() != null && GetCity().Length > 0) || (GetRegionName() != null && GetRegionName().Length > 0) ) { ; } else { log.Fine("No BP Location Info to save"); return(null); } // Address MLocation location = new MLocation(GetCtx(), GetC_Country_ID(), GetC_Region_ID(), GetCity(), Get_TrxName()); location.SetAddress1(GetAddress1()); location.SetAddress2(GetAddress2()); location.SetPostal(GetPostal()); location.SetPostal_Add(GetPostal_Add()); location.SetRegionName(GetRegionName()); if (location.Save()) { MBPartnerLocation bpl = new MBPartnerLocation(_bp); bpl.SetC_Location_ID(location.GetC_Location_ID()); bpl.SetPhone(GetPhone()); bpl.SetPhone2(GetPhone2()); bpl.SetFax(GetFax()); bpl.SetC_SalesRegion_ID(GetC_SalesRegion_ID()); if (bpl.Save()) { SetC_BPartner_Location_ID(bpl.GetC_BPartner_Location_ID()); } } return(null); }
/**** * Before Save. * - Set Name * @param newRecord new * @return save */ protected override bool BeforeSave(bool newRecord) { if (GetC_Location_ID() == 0) { return(false); } if (VAdvantage.Utility.Env.IsModuleInstalled("VA077_")) { // Error if Customer Location No is not unique if (GetVA077_LocNo() != null) { string sql = @"SELECT C_BPartner_ID, VA077_IsMailAdd FROM C_BPartner_Location WHERE VA077_LocNo = '" + GetVA077_LocNo() + "' AND C_BPartner_Location_ID !=" + GetC_BPartner_Location_ID(); DataSet ds = DB.ExecuteDataset(sql, null, Get_Trx()); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows.Count == 1) { int PartnerID = Util.GetValueOfInt(ds.Tables[0].Rows[0]["C_BPartner_ID"]); bool value = Util.GetValueOfString(ds.Tables[0].Rows[0]["VA077_IsMailAdd"]).Equals("Y") ? true : false; if (value.Equals(IsVA077_IsMailAdd()) || PartnerID != GetC_BPartner_ID()) { log.SaveError("VA077_UniqueLocNo", ""); return(false); } } else { log.SaveError("VA077_UniqueLocNo", ""); return(false); } } } } // change by amit // Set New Name //if (!newRecord) // return true; if (Util.GetValueOfString(Get_ValueOld("Name")) == GetName() && Util.GetValueOfInt(Get_ValueOld("C_Location_ID")) == GetC_Location_ID()) { return(true); } MLocation address = GetLocation(true); _uniqueName.Append(GetName()); //if (_uniqueName != null && _uniqueName.Equals(".")) // default change by amit _uniqueName.Clear(); _unique = 0; // Changes Done By Vivek on 10/12/2015 //Set City Name at Name Field if (GetName() == ".") { MakeUnique(address); } //else Set Manually Edited Name by User at name field else { SetName(GetName()); return(true); } //if (Util.GetValueOfString(Get_ValueOld("Name")) != GetName()) //{ // _uniqueName = GetName(); // SetName(_uniqueName); // return true; //} //MakeUnique(address); // Check uniqueness MBPartnerLocation[] locations = GetForBPartner(GetCtx(), GetC_BPartner_ID(), Get_TrxName()); bool unique = locations.Length == 0; while (!unique) { unique = true; for (int i = 0; i < locations.Length; i++) { MBPartnerLocation location = locations[i]; if (location.GetC_BPartner_Location_ID() == Get_ID()) { continue; } if (_uniqueName.Equals(location.GetName())) { MakeUnique(address); unique = false; break; } } } SetName(_uniqueName.ToString()); return(true); }
/// <summary> /// Set BPartner /// </summary> /// <param name="bp">partner</param> /// <param name="isSOTrx">SO</param> public void SetBPartner(MBPartner bp, bool isSOTrx) { SetC_BPartner_ID(bp.GetC_BPartner_ID()); MBPartnerLocation[] locations = bp.GetLocations(false); // Location if (locations.Length == 1) { SetC_BPartner_Location_ID(locations[0].GetC_BPartner_Location_ID()); } else { for (int i = 0; i < locations.Length; i++) { MBPartnerLocation location = locations[i]; if (!location.IsActive()) { continue; } if ((location.IsPayFrom() && isSOTrx) || (location.IsRemitTo() && !isSOTrx)) { SetC_BPartner_Location_ID(location.GetC_BPartner_Location_ID()); break; } } } if (GetC_BPartner_Location_ID() == 0) { String msg = "@C_BPartner_ID@ " + bp.GetName(); if (isSOTrx) { msg += " @No@ @IsPayFrom@"; } else { msg += " @No@ @IsRemitTo@"; } throw new ArgumentException(msg); } // User with location MUser[] users = MUser.GetOfBPartner(GetCtx(), bp.GetC_BPartner_ID()); if (users.Length == 1) { SetAD_User_ID(users[0].GetAD_User_ID()); } else { for (int i = 0; i < users.Length; i++) { MUser user = users[i]; if (user.GetC_BPartner_Location_ID() == GetC_BPartner_Location_ID()) { SetAD_User_ID(users[i].GetAD_User_ID()); break; } } } // int SalesRep_ID = bp.GetSalesRep_ID(); if (SalesRep_ID != 0) { SetSalesRep_ID(SalesRep_ID); } }
/**** * Before Save. * - Set Name * @param newRecord new * @return save */ protected override bool BeforeSave(bool newRecord) { if (GetC_Location_ID() == 0) { return(false); } // change by amit // Set New Name //if (!newRecord) // return true; if (Util.GetValueOfString(Get_ValueOld("Name")) == GetName() && Util.GetValueOfInt(Get_ValueOld("C_Location_ID")) == GetC_Location_ID()) { return(true); } MLocation address = GetLocation(true); _uniqueName = GetName(); //if (_uniqueName != null && _uniqueName.Equals(".")) // default change by amit _uniqueName = null; _unique = 0; // Changes Done By Vivek on 10/12/2015 //Set City Name at Name Field if (GetName() == ".") { MakeUnique(address); } //else Set Manually Edited Name by User at name field else { SetName(GetName()); return(true); } //if (Util.GetValueOfString(Get_ValueOld("Name")) != GetName()) //{ // _uniqueName = GetName(); // SetName(_uniqueName); // return true; //} //MakeUnique(address); // Check uniqueness MBPartnerLocation[] locations = GetForBPartner(GetCtx(), GetC_BPartner_ID(), Get_TrxName()); bool unique = locations.Length == 0; while (!unique) { unique = true; for (int i = 0; i < locations.Length; i++) { MBPartnerLocation location = locations[i]; if (location.GetC_BPartner_Location_ID() == Get_ID()) { continue; } if (_uniqueName.Equals(location.GetName())) { MakeUnique(address); unique = false; break; } } } SetName(_uniqueName); return(true); }
/// <summary> /// Set BPartner /// </summary> /// <param name="bp">partner</param> /// <param name="isSOTrx">SO</param> public void SetBPartner(MBPartner bp, bool isSOTrx) { SetC_BPartner_ID(bp.GetC_BPartner_ID()); MBPartnerLocation[] locations = GetLocations(); // Location for (int i = 0; i < locations.Length; i++) { MBPartnerLocation location = locations[i]; if (!location.IsActive()) { continue; } if ((location.IsPayFrom() && isSOTrx) || (location.IsRemitTo() && !isSOTrx)) { SetC_BPartner_Location_ID(location.GetC_BPartner_Location_ID()); break; } } //} if (GetC_BPartner_Location_ID() == 0) { String msg = "@C_BPartner_ID@ " + bp.GetName(); if (isSOTrx) { msg += " @No@ @IsPayFrom@"; } else { msg += " @No@ @IsRemitTo@"; } //throw new ArgumentException(msg); log.SaveInfo("", msg); return; } // User with location // Change done by mohit to pick users sorted by date updated. 7 May 2019. MUser[] users = GetOfBPartner(GetCtx(), bp.GetC_BPartner_ID()); if (users.Length == 1) { if (users[0].IsEmail() || users[0].GetNotificationType() == MUser.NOTIFICATIONTYPE_EMail || users[0].GetNotificationType() == MUser.NOTIFICATIONTYPE_EMailPlusNotice || users[0].GetNotificationType() == MUser.NOTIFICATIONTYPE_EMailPlusFaxEMail) { SetAD_User_ID(users[0].GetAD_User_ID()); } } else { for (int i = 0; i < users.Length; i++) { MUser user = users[i]; if (user.GetC_BPartner_Location_ID() == GetC_BPartner_Location_ID() && (user.IsEmail() || user.GetNotificationType() == MUser.NOTIFICATIONTYPE_EMail || user.GetNotificationType() == MUser.NOTIFICATIONTYPE_EMailPlusNotice || user.GetNotificationType() == MUser.NOTIFICATIONTYPE_EMailPlusFaxEMail)) { SetAD_User_ID(users[i].GetAD_User_ID()); break; } } } // int SalesRep_ID = bp.GetSalesRep_ID(); if (SalesRep_ID != 0) { SetSalesRep_ID(SalesRep_ID); } }
} // prepare /// <summary> /// Perrform Process. /// </summary> /// <returns>Message (clear text)</returns> protected override String DoIt() { log.Info("C_Project_ID=" + _C_Project_ID); if (_C_Project_ID == 0) { throw new ArgumentException("C_Project_ID == 0"); } MProject fromProject = GetProject(GetCtx(), _C_Project_ID, Get_TrxName()); GetCtx().SetIsSOTrx(true); // Set SO context /** @todo duplicate invoice prevention */ //Added by Vivek for Credit Limit on 24/08/2016 if (fromProject.GetC_BPartner_ID() != 0) { VAdvantage.Model.MBPartner bp = new VAdvantage.Model.MBPartner(GetCtx(), fromProject.GetC_BPartner_ID(), Get_TrxName()); if (bp.GetCreditStatusSettingOn() == "CH") { if (bp.GetCreditValidation() == "A" || bp.GetCreditValidation() == "D" || bp.GetCreditValidation() == "E") { log.SaveError("StopOrder", ""); return(Msg.GetMsg(GetCtx(), "StopOrder")); } else if (bp.GetCreditValidation() == "G" || bp.GetCreditValidation() == "J" || bp.GetCreditValidation() == "K") { if (_msg != null) { _msg.Clear(); } log.SaveError("WarningOrder", ""); _msg.Append(Msg.GetMsg(GetCtx(), "WarningOrder")); } } else { VAdvantage.Model.MBPartnerLocation loc = new VAdvantage.Model.MBPartnerLocation(GetCtx(), fromProject.GetC_BPartner_Location_ID(), Get_TrxName()); if (loc.GetCreditValidation() == "A" || loc.GetCreditValidation() == "D" || loc.GetCreditValidation() == "E") { log.SaveError("StopOrder", ""); return(Msg.GetMsg(GetCtx(), "StopOrder")); } else if (loc.GetCreditValidation() == "G" || loc.GetCreditValidation() == "J" || loc.GetCreditValidation() == "K") { if (_msg != null) { _msg.Clear(); } log.SaveError("WarningOrder", ""); _msg.Append(Msg.GetMsg(GetCtx(), "WarningOrder")); } } } //Credit Limit MOrder order = new MOrder(fromProject, true, MOrder.DocSubTypeSO_OnCredit); if (!order.Save()) { throw new Exception("Could not create Order"); } // *** Lines *** int count = 0; // Service Project if (MProject.PROJECTCATEGORY_ServiceChargeProject.Equals(fromProject.GetProjectCategory())) { /** @todo service project invoicing */ throw new Exception("Service Charge Projects are on the TODO List"); } // Service Lines else // Order Lines { MProjectLine[] lines = fromProject.GetLines(); for (int i = 0; i < lines.Length; i++) { MOrderLine ol = new MOrderLine(order); ol.SetLine(lines[i].GetLine()); ol.SetDescription(lines[i].GetDescription()); // ol.SetM_Product_ID(lines[i].GetM_Product_ID(), true); //ol.SetQty(lines[i].GetPlannedQty().subtract(lines[i].GetInvoicedQty())); ol.SetQty(Decimal.Subtract(lines[i].GetPlannedQty(), lines[i].GetInvoicedQty())); ol.SetPrice(); if (lines[i].GetPlannedPrice().CompareTo(Env.ZERO) != 0) { ol.SetPrice(lines[i].GetPlannedPrice()); } ol.SetDiscount(); ol.SetTax(); if (ol.Save()) { count++; } } // for all lines if (lines.Length != count) { log.Log(Level.SEVERE, "Lines difference - ProjectLines=" + lines.Length + " <> Saved=" + count); } } // Order Lines if (_msg != null) { return("@C_Order_ID@ " + order.GetDocumentNo() + " (" + count + ")" + " " + _msg); } else { return("@C_Order_ID@ " + order.GetDocumentNo() + " (" + count + ")"); } } // doIt