/// <summary> /// Update Open Balance of BP's /// </summary> /// <param name="bps">list of business partners</param> private void UpdateBP(HashSet <int> bps) { log.Info("#" + bps.Count); IEnumerator <int> it = bps.GetEnumerator(); it.Reset(); while (it.MoveNext()) { int C_BPartner_ID = it.Current; MBPartner bp = new MBPartner(GetCtx(), C_BPartner_ID, Get_TrxName()); bp.SetTotalOpenBalance(); // recalculates from scratch // bp.setSOCreditStatus(); // called automatically if (bp.Save()) { log.Fine(bp.ToString()); } else { log.Log(Level.SEVERE, "BP not updated - " + bp); } } }
/** * Complete Document * @return new status (Complete, In Progress, Invalid, Waiting ..) */ public String CompleteIt() { // Re-Check if (!_justPrepared) { String status = PrepareIt(); if (!DocActionVariables.STATUS_INPROGRESS.Equals(status)) { return(status); } } // Implicit Approval if (!IsApproved()) { ApproveIt(); } // log.Info(ToString()); // Allocation Header MAllocationHdr alloc = new MAllocationHdr(GetCtx(), false, GetDateAcct(), GetC_Currency_ID(), Msg.Translate(GetCtx(), "C_Cash_ID") + ": " + GetName(), Get_TrxName()); alloc.SetAD_Org_ID(GetAD_Org_ID()); if (!alloc.Save()) { _processMsg = "Could not create Allocation Hdr"; return(DocActionVariables.STATUS_INVALID); } // MCashLine[] lines = GetLines(false); for (int i = 0; i < lines.Length; i++) { MCashLine line = lines[i]; if (Util.GetValueOfInt(line.GetC_InvoicePaySchedule_ID()) != 0) { MInvoicePaySchedule paySch = new MInvoicePaySchedule(GetCtx(), Util.GetValueOfInt(line.GetC_InvoicePaySchedule_ID()), Get_TrxName()); paySch.SetC_CashLine_ID(line.GetC_CashLine_ID()); paySch.Save(); } else { int[] InvoicePaySchedule_ID = MInvoicePaySchedule.GetAllIDs("C_InvoicePaySchedule", "C_Invoice_ID = " + line.GetC_Invoice_ID() + @" AND C_InvoicePaySchedule_ID NOT IN (SELECT NVL(C_InvoicePaySchedule_ID,0) FROM C_InvoicePaySchedule WHERE C_Payment_ID IN (SELECT NVL(C_Payment_ID,0) FROM C_InvoicePaySchedule) UNION SELECT NVL(C_InvoicePaySchedule_ID,0) FROM C_InvoicePaySchedule WHERE C_Cashline_ID IN (SELECT NVL(C_Cashline_ID,0) FROM C_InvoicePaySchedule))", Get_TrxName()); foreach (int invocePay in InvoicePaySchedule_ID) { MInvoicePaySchedule paySch = new MInvoicePaySchedule(GetCtx(), invocePay, Get_TrxName()); paySch.SetC_CashLine_ID(line.GetC_CashLine_ID()); paySch.Save(); } } if (MCashLine.CASHTYPE_Invoice.Equals(line.GetCashType())) { bool differentCurrency = GetC_Currency_ID() != line.GetC_Currency_ID(); MAllocationHdr hdr = alloc; if (differentCurrency) { hdr = new MAllocationHdr(GetCtx(), false, GetDateAcct(), line.GetC_Currency_ID(), Msg.Translate(GetCtx(), "C_Cash_ID") + ": " + GetName(), Get_TrxName()); hdr.SetAD_Org_ID(GetAD_Org_ID()); if (!hdr.Save()) { _processMsg = "Could not create Allocation Hdr"; return(DocActionVariables.STATUS_INVALID); } } // Allocation Line MAllocationLine aLine = new MAllocationLine(hdr, line.GetAmount(), line.GetDiscountAmt(), line.GetWriteOffAmt(), line.GetOverUnderAmt()); aLine.SetC_Invoice_ID(line.GetC_Invoice_ID()); aLine.SetC_CashLine_ID(line.GetC_CashLine_ID()); if (!aLine.Save()) { _processMsg = "Could not create Allocation Line"; return(DocActionVariables.STATUS_INVALID); } if (differentCurrency) { // Should start WF hdr.ProcessIt(DocActionVariables.ACTION_COMPLETE); hdr.Save(); } } else if (MCashLine.CASHTYPE_BankAccountTransfer.Equals(line.GetCashType())) { // Payment just as intermediate info MPayment pay = new MPayment(GetCtx(), 0, Get_TrxName()); pay.SetAD_Org_ID(GetAD_Org_ID()); String documentNo = GetName(); pay.SetDocumentNo(documentNo); pay.SetR_PnRef(documentNo); pay.Set_Value("TrxType", "X"); // Transfer pay.Set_Value("TenderType", "X"); // pay.SetC_BankAccount_ID(line.GetC_BankAccount_ID()); pay.SetC_DocType_ID(true); // Receipt pay.SetDateTrx(GetStatementDate()); pay.SetDateAcct(GetDateAcct()); pay.SetAmount(line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount())); // Transfer pay.SetDescription(line.GetDescription()); pay.SetDocStatus(MPayment.DOCSTATUS_Closed); pay.SetDocAction(MPayment.DOCACTION_None); pay.SetPosted(true); pay.SetIsAllocated(true); // Has No Allocation! pay.SetProcessed(true); if (!pay.Save()) { _processMsg = "Could not create Payment"; return(DocActionVariables.STATUS_INVALID); } } // Added to Update Open Balance of Business Partner else if (MCashLine.CASHTYPE_BusinessPartner.Equals(line.GetCashType())) { if (line.GetC_BPartner_ID() != 0) { Decimal? UpdatedBal = 0; MBPartner bp = new MBPartner(GetCtx(), line.GetC_BPartner_ID(), Get_TrxName()); Decimal?cashAmt = VAdvantage.Model.MConversionRate.ConvertBase(GetCtx(), Decimal.Add(Decimal.Add(line.GetAmount(), line.GetDiscountAmt()), line.GetWriteOffAmt()), GetC_Currency_ID(), GetDateAcct(), 0, GetAD_Client_ID(), GetAD_Org_ID()); if (cashAmt > 0) { UpdatedBal = Decimal.Subtract((Decimal)bp.GetTotalOpenBalance(), (Decimal)cashAmt); Decimal?newCreditAmt = bp.GetSO_CreditUsed(); if (newCreditAmt == null) { newCreditAmt = Decimal.Negate((Decimal)cashAmt); } else { newCreditAmt = Decimal.Subtract((Decimal)newCreditAmt, (Decimal)cashAmt); } // log.Fine("TotalOpenBalance=" + bp.GetTotalOpenBalance(false) + "(" + cashAmt + ", Credit=" + bp.GetSO_CreditUsed() + "->" + newCreditAmt + ", Balance=" + bp.GetTotalOpenBalance(false) + " -> " + UpdatedBal); bp.SetSO_CreditUsed((Decimal)newCreditAmt); } else { UpdatedBal = Decimal.Subtract((Decimal)bp.GetTotalOpenBalance(), (Decimal)cashAmt); log.Fine("Payment Amount =" + line.GetAmount() + "(" + cashAmt + ") Balance=" + bp.GetTotalOpenBalance(false) + " -> " + UpdatedBal); } bp.SetTotalOpenBalance(Convert.ToDecimal(UpdatedBal)); bp.SetSOCreditStatus(); if (!bp.Save(Get_TrxName())) { _processMsg = "Could not update Business Partner"; return(DocActionVariables.STATUS_INVALID); } } } } // Should start WF alloc.ProcessIt(DocActionVariables.ACTION_COMPLETE); alloc.Save(); // User Validation String valid = ModelValidationEngine.Get().FireDocValidate(this, ModalValidatorVariables.DOCTIMING_AFTER_COMPLETE); if (valid != null) { _processMsg = valid; return(DocActionVariables.STATUS_INVALID); } // SetProcessed(true); SetDocAction(DOCACTION_Close); if (!UpdateCompletedBalance()) { _processMsg = "Could not update Header"; return(VAdvantage.Process.DocActionVariables.STATUS_INVALID); } return(VAdvantage.Process.DocActionVariables.STATUS_COMPLETED); }
/** * Create BP, Contact, Location from Lead * @return error message */ public String CreateBP() { if (GetC_BPartner_ID() != 0 && GetAD_User_ID() != 0 && GetC_BPartner_Location_ID() == 0) { return("@AlreadyExists@: @C_BPartner_ID@ (ID=" + GetC_BPartner_ID() + ")"); } // BPartner if (GetC_BPartner_ID() == 0 && (GetBPName() != null && GetBPName().Length > 0)) { // Existing User _user = GetUser(); if (_user != null) { if (_user.GetC_BPartner_ID() != 0) { SetRef_BPartner_ID(_user.GetC_BPartner_ID()); log.Info("Set to BPartner of User - " + _user); return(CreateBPLocation()); } } // _bp = new MBPartner(GetCtx(), Get_TrxName()); // Template _bp.SetAD_Org_ID(GetAD_Org_ID()); //_bp.SetValue(GetBPName()); _bp.SetName(GetBPName()); _bp.SetIsCustomer(false); _bp.SetIsProspect(true); _bp.SetSOCreditStatus("O"); // Check Removed as per Surya Sir's Decision BY Lokesh //_bp.SetC_Campaign_ID(GetC_Campaign_ID()); // if (GetC_BP_Group_ID() == 0) { // ShowMessage.Info("First Select Customer Group Then process again", true, null, null); return(null); } else { _bp.SetC_BP_Group_ID(GetC_BP_Group_ID()); } MBPGroup gp = new MBPGroup(GetCtx(), GetC_BP_Group_ID(), Get_TrxName()); _bp.SetM_PriceList_ID(gp.GetM_PriceList_ID()); if (GetC_BP_Size_ID() != 0) { _bp.SetC_BP_Size_ID(GetC_BP_Size_ID()); } if (GetURL() != null) { _bp.SetURL(GetURL()); } if (GetC_BP_Status_ID() != 0) { _bp.SetC_BP_Status_ID(GetC_BP_Status_ID()); } if (GetC_IndustryCode_ID() != 0) { _bp.SetC_IndustryCode_ID(GetC_IndustryCode_ID()); } if (GetNAICS() != null) { _bp.SetNAICS(GetNAICS()); } if (GetDUNS() != null) { _bp.SetDUNS(GetDUNS()); } if (GetNumberEmployees() != 0) { _bp.SetNumberEmployees(GetNumberEmployees()); } if (GetSalesVolume() != 0) { _bp.SetSalesVolume(GetSalesVolume()); } if (GetSalesRep_ID() != 0) { _bp.SetSalesRep_ID(GetSalesRep_ID()); } if (GetC_Campaign_ID() != 0) { _bp.SetC_Campaign_ID(GetC_Campaign_ID()); } if (!_bp.Save()) { return("@SaveError@"); } // Update User if (_user != null && _user.GetC_BPartner_ID() == 0) { _user.SetC_BPartner_ID(_bp.GetC_BPartner_ID()); _user.Save(); } // Save BP SetRef_BPartner_ID(_bp.GetC_BPartner_ID()); } String error = CreateBPContact(); if (error != null && error.Length > 0) { return(error); } CreateBPLocation(); try { int id = _bp.GetC_BPartner_ID(); string qry = "Update c_bpartner set Description='' where c_bpartner_id=" + id; int check = DB.ExecuteQuery(qry, null, Get_TrxName()); string val = _bp.GetValue(); qry = "Update c_bpartner set Value='" + val + GetBPName() + "' where c_bpartner_id=" + id; check = DB.ExecuteQuery(qry, null, Get_TrxName()); if (GetR_InterestArea_ID() != 0) { string sql = "Select R_InterestArea_ID from vss_lead_interestarea where C_Lead_ID=" + GetC_Lead_ID(); IDataReader dr = DB.ExecuteReader(sql, null, Get_TrxName()); while (dr.Read()) { X_R_ContactInterest Prospect = new X_R_ContactInterest(GetCtx(), 0, Get_TrxName()); Prospect.SetR_InterestArea_ID(Util.GetValueOfInt(dr[0])); Prospect.SetC_BPartner_ID(GetRef_BPartner_ID()); String query = "Select ad_user_id from ad_user where c_bpartner_id= " + GetRef_BPartner_ID(); int UserId = Util.GetValueOfInt(DB.ExecuteScalar(query, null, Get_TrxName())); Prospect.SetAD_User_ID(UserId); query = "Select C_BPartner_Location_id from C_BPartner_Location where c_bpartner_id= " + GetRef_BPartner_ID(); int Id = Util.GetValueOfInt(DB.ExecuteScalar(query, null, Get_TrxName())); X_C_BPartner_Location loc = new X_C_BPartner_Location(GetCtx(), Id, Get_TrxName()); Prospect.SetC_BPartner_Location_ID(Id); Prospect.SetPhone(loc.GetPhone()); Prospect.SetFax(loc.GetFax()); X_AD_User us = new X_AD_User(GetCtx(), UserId, Get_TrxName()); Prospect.SetC_Job_ID(us.GetC_Job_ID()); Prospect.SetSubscribeDate(DateTime.Today); query = "Select Email from ad_user where ad_user_id= " + UserId; String mail = Util.GetValueOfString(DB.ExecuteScalar(query, null, Get_TrxName())); Prospect.SetEMail(mail); if (Prospect.Save()) { } } dr.Close(); } } catch { } return(null); }