} // allocateOldestFirst /// <summary> /// Create Allocation allocation /// </summary> /// <param name="_C_Currency_ID"></param> /// <param name="description"></param> /// <param name="_dateAcct"></param> /// <param name="Amount"></param> /// <param name="DiscountAmt"></param> /// <param name="WriteOffAmt"></param> /// <param name="OverUnderAmt"></param> /// <param name="_C_BPartner_ID"></param> /// <param name="C_Payment_ID"></param> /// <param name="C_Invoice_ID"></param> /// <param name="AD_Org_ID"></param> /// <returns>true if created</returns> private Boolean CreateAllocation(int _C_Currency_ID, String description, DateTime?_dateAcct, Decimal Amount, Decimal?DiscountAmt, Decimal?WriteOffAmt, Decimal?OverUnderAmt, int _C_BPartner_ID, int C_Payment_ID, int C_Invoice_ID, int AD_Org_ID) { // Process old Allocation if (_allocation != null && _allocation.GetC_Currency_ID() != _C_Currency_ID) { ProcessAllocation(); } // New Allocation if (_allocation == null) { _allocation = new MAllocationHdr(GetCtx(), false, _dateAcct, // automatic _C_Currency_ID, "Auto " + description, Get_Trx()); _allocation.SetAD_Org_ID(AD_Org_ID); if (!_allocation.Save()) { return(false); } } // New Allocation Line MAllocationLine aLine = new MAllocationLine(_allocation, Amount, DiscountAmt, WriteOffAmt, OverUnderAmt); aLine.SetC_BPartner_ID(_C_BPartner_ID); aLine.SetC_Payment_ID(C_Payment_ID); aLine.SetC_Invoice_ID(C_Invoice_ID); return(aLine.Save()); } // createAllocation
} // doIt /// <summary> /// Write Off /// </summary> /// <param name="C_Invoice_ID"></param> /// <param name="DocumentNo"></param> /// <param name="DateInvoiced"></param> /// <param name="C_Currency_ID"></param> /// <param name="OpenAmt"></param> /// <returns>true if written off</returns> private Boolean writeOff(int C_Invoice_ID, String DocumentNo, DateTime?DateInvoiced, int C_Currency_ID, Decimal OpenAmt) { // Nothing to do if (Env.Signum(OpenAmt) == 0) { return(false); } if (Math.Abs(OpenAmt).CompareTo(_MaxInvWriteOffAmt) >= 0) { return(false); } // if (_IsSimulation) { AddLog(C_Invoice_ID, DateInvoiced, OpenAmt, DocumentNo); return(true); } // Invoice MInvoice invoice = new MInvoice(GetCtx(), C_Invoice_ID, Get_TrxName()); if (!invoice.IsSOTrx()) { OpenAmt = Decimal.Negate(OpenAmt); } // Allocation if (_m_alloc == null || C_Currency_ID != _m_alloc.GetC_Currency_ID()) { ProcessAllocation(); _m_alloc = new MAllocationHdr(GetCtx(), true, _DateAcct, C_Currency_ID, GetProcessInfo().GetTitle() + " #" + GetAD_PInstance_ID(), Get_TrxName()); _m_alloc.SetAD_Org_ID(invoice.GetAD_Org_ID()); if (!_m_alloc.Save()) { log.Log(Level.SEVERE, "Cannot create allocation header"); return(false); } } // Payment if (_CreatePayment && (_m_payment == null || invoice.GetC_BPartner_ID() != _m_payment.GetC_BPartner_ID() || C_Currency_ID != _m_payment.GetC_Currency_ID())) { ProcessPayment(); _m_payment = new MPayment(GetCtx(), 0, Get_TrxName()); _m_payment.SetAD_Org_ID(invoice.GetAD_Org_ID()); _m_payment.SetC_BankAccount_ID(_C_BankAccount_ID); _m_payment.SetTenderType(MPayment.TENDERTYPE_Check); _m_payment.SetDateTrx(_DateAcct); _m_payment.SetDateAcct(_DateAcct); _m_payment.SetDescription(GetProcessInfo().GetTitle() + " #" + GetAD_PInstance_ID()); _m_payment.SetC_BPartner_ID(invoice.GetC_BPartner_ID()); _m_payment.SetIsReceipt(true); // payments are negative _m_payment.SetC_Currency_ID(C_Currency_ID); if (!_m_payment.Save()) { log.Log(Level.SEVERE, "Cannot create payment"); return(false); } } // Line MAllocationLine aLine = null; if (_CreatePayment) { aLine = new MAllocationLine(_m_alloc, OpenAmt, Env.ZERO, Env.ZERO, Env.ZERO); _m_payment.SetPayAmt(Decimal.Add(_m_payment.GetPayAmt(), OpenAmt)); aLine.SetC_Payment_ID(_m_payment.GetC_Payment_ID()); } else { aLine = new MAllocationLine(_m_alloc, Env.ZERO, Env.ZERO, OpenAmt, Env.ZERO); } aLine.SetC_Invoice_ID(C_Invoice_ID); if (aLine.Save()) { AddLog(C_Invoice_ID, DateInvoiced, OpenAmt, DocumentNo); return(true); } // Error log.Log(Level.SEVERE, "Cannot create allocation line for C_Invoice_ID=" + C_Invoice_ID); return(false); } // writeOff