/// <summary> /// Get Lines /// </summary> /// <param name="requery">if true requery</param> /// <returns>lines</returns> public MAllocationLine[] GetLines(bool requery) { if (_lines != null && _lines.Length != 0 && !requery) { return(_lines); } // String sql = "SELECT * FROM C_AllocationLine WHERE C_AllocationHdr_ID=" + GetC_AllocationHdr_ID(); List <MAllocationLine> list = new List <MAllocationLine>(); try { DataSet ds = DataBase.DB.ExecuteDataset(sql, null, Get_TrxName()); if (ds.Tables.Count > 0) { foreach (DataRow dr in ds.Tables[0].Rows) { MAllocationLine line = new MAllocationLine(GetCtx(), dr, Get_TrxName()); line.SetParent(this); list.Add(line); } } } catch (Exception e) { log.Log(Level.SEVERE, sql, e); } // _lines = new MAllocationLine[list.Count]; _lines = list.ToArray(); return(_lines); }
/// <summary> /// Prepare Document /// </summary> /// <returns>new status (In Progress or Invalid)</returns> public String PrepareIt() { log.Info(ToString()); _processMsg = ModelValidationEngine.Get().FireDocValidate (this, ModalValidatorVariables.DOCTIMING_BEFORE_PREPARE); if (_processMsg != null) { return(DocActionVariables.STATUS_INVALID); } // Std Period open? if (!MPeriod.IsOpen(GetCtx(), GetDateAcct(), MDocBaseType.DOCBASETYPE_PAYMENTALLOCATION)) { _processMsg = "@PeriodClosed@"; return(DocActionVariables.STATUS_INVALID); } // is Non Business Day? if (MNonBusinessDay.IsNonBusinessDay(GetCtx(), GetDateAcct())) { _processMsg = Common.Common.NONBUSINESSDAY; return(DocActionVariables.STATUS_INVALID); } GetLines(false); if (_lines.Length == 0) { _processMsg = "@NoLines@"; return(DocActionVariables.STATUS_INVALID); } // Add up Amounts & validate Decimal approval = Env.ZERO; for (int i = 0; i < _lines.Length; i++) { MAllocationLine line = _lines[i]; approval = Decimal.Add(Decimal.Add(approval, line.GetWriteOffAmt()), line.GetDiscountAmt()); // Make sure there is BP if (line.GetC_BPartner_ID() == 0) { _processMsg = "No Business Partner"; return(DocActionVariables.STATUS_INVALID); } } SetApprovalAmt(approval); // _justPrepared = true; if (!DOCACTION_Complete.Equals(GetDocAction())) { SetDocAction(DOCACTION_Complete); } return(DocActionVariables.STATUS_INPROGRESS); }
/// <summary> /// Before Delete /// </summary> /// <returns>true if acct was deleted</returns> protected override bool BeforeDelete() { Trx trxName = Get_Trx(); if (trxName == null) { log.Warning("No transaction"); } if (IsPosted()) { if (!MPeriod.IsOpen(GetCtx(), GetDateTrx(), MDocBaseType.DOCBASETYPE_PAYMENTALLOCATION)) { log.Warning("Period Closed"); return(false); } //// is Non Business Day? //if (MNonBusinessDay.IsNonBusinessDay(GetCtx(), GetDateTrx())) //{ // log.Warning("DateIsInNonBusinessDay"); // return false; //} SetPosted(false); if (MFactAcct.Delete(Table_ID, Get_ID(), trxName) < 0) { return(false); } } // Mark as Inactive SetIsActive(false); // updated DB for line delete/process String sql = "UPDATE C_AllocationHdr SET IsActive='N' WHERE C_AllocationHdr_ID=" + GetC_AllocationHdr_ID(); DataBase.DB.ExecuteQuery(sql, null, trxName); // Unlink GetLines(true); HashSet <int> bps = new HashSet <int>(); for (int i = 0; i < _lines.Length; i++) { MAllocationLine line = _lines[i]; bps.Add(line.GetC_BPartner_ID()); if (!line.Delete(true, trxName)) { return(false); } } UpdateBP(bps); return(true); }
/// <summary> /// Reverse Allocation. /// Period needs to be open /// </summary> /// <returns>true if reversed</returns> private bool ReverseIt() { if (!IsActive()) { throw new Exception("Allocation already reversed (not active)"); } // Can we delete posting if (!MPeriod.IsOpen(GetCtx(), GetDateTrx(), MDocBaseType.DOCBASETYPE_PAYMENTALLOCATION)) { throw new Exception("@PeriodClosed@"); } // is Non Business Day? if (MNonBusinessDay.IsNonBusinessDay(GetCtx(), GetDateTrx())) { throw new Exception(Common.Common.NONBUSINESSDAY); } // Set Inactive SetIsActive(false); SetDocumentNo(GetDocumentNo() + "^"); SetDocStatus(DOCSTATUS_Reversed); // for direct calls if (!Save() || IsActive()) { throw new Exception("Cannot de-activate allocation"); } // Delete Posting String sql = "DELETE FROM Fact_Acct WHERE AD_Table_ID=" + MAllocationHdr.Table_ID + " AND Record_ID=" + GetC_AllocationHdr_ID(); int no = DataBase.DB.ExecuteQuery(sql, null, Get_TrxName()); log.Fine("Fact_Acct deleted #" + no); // Unlink Invoices GetLines(true); HashSet <int> bps = new HashSet <int>(); for (int i = 0; i < _lines.Length; i++) { MAllocationLine line = _lines[i]; line.SetIsActive(false); line.Save(); bps.Add(line.ProcessIt(true)); // reverse } UpdateBP(bps); return(true); }
/// <summary> /// Complete Document /// </summary> /// <returns>new status (Complete, In Progress, Invalid, Waiting ..)</returns> 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()); // Link GetLines(false); HashSet <int> bps = new HashSet <int>(); for (int i = 0; i < _lines.Length; i++) { MAllocationLine line = _lines[i]; bps.Add(line.ProcessIt(false)); // not reverse } UpdateBP(bps); // 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); return(DocActionVariables.STATUS_COMPLETED); }
/** * 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); }