/// <summary> ///Convert an amount /// </summary> /// <param name="ctx">context</param> /// <param name="Amt">amount to be converted</param> /// <param name="CurFrom_ID">The C_Currency_ID FROM</param> /// <param name="CurTo_ID">The C_Currency_ID TO</param> /// <param name="ConvDate">conversion date - if null - use current date</param> /// <param name="C_ConversionType_ID">C_ConversionType_ID conversion rate type - if 0 - use Default</param> /// <param name="AD_Client_ID">client</param> /// <param name="AD_Org_ID">organization</param> /// <returns>converted amount or null if no rate</returns> public static Decimal Convert(Ctx ctx, Decimal amt, int CurFrom_ID, int CurTo_ID, DateTime?convDate, int C_ConversionType_ID, int AD_Client_ID, int AD_Org_ID) { //if (amt == null) //{ // throw new ArgumentException("Required parameter missing - Amt"); //} if (CurFrom_ID == CurTo_ID || amt.Equals(Env.ZERO)) { return(amt); } // Get Rate Decimal retValue = GetRate(CurFrom_ID, CurTo_ID, convDate, C_ConversionType_ID, AD_Client_ID, AD_Org_ID); //if (retValue == null) //{ // //return null; // return retValue; //} // Get Amount in Currency Precision retValue = Decimal.Multiply(retValue, amt); int stdPrecision = MCurrency.GetStdPrecision(ctx, CurTo_ID); if (Env.Scale(retValue) > stdPrecision) { retValue = Decimal.Round(retValue, stdPrecision, MidpointRounding.AwayFromZero); } return(retValue); }
} // setCurrency /// <summary> /// Set C_Currency_ID and precision /// </summary> /// <param name="C_Currency_ID">currency</param> public new void SetC_Currency_ID(int C_Currency_ID) { if (C_Currency_ID == 0) { return; } base.SetC_Currency_ID(C_Currency_ID); m_precision = MCurrency.GetStdPrecision(GetCtx(), C_Currency_ID); } // setC_Currency_ID
/// <summary> /// Get Standard Currency Precision /// </summary> /// <returns>precision</returns> public int GetStandardPrecision() { if (_precision == null) { MCurrency c = MCurrency.Get(GetCtx(), GetC_Currency_ID()); _precision = c.GetStdPrecision(); } return((int)_precision); }
/** * Get Std Precision of accounting Currency * @return precision */ public int GetStdPrecision() { if (_stdPrecision < 0) { MCurrency cur = MCurrency.Get(GetCtx(), GetC_Currency_ID()); _stdPrecision = cur.GetStdPrecision(); _costPrecision = cur.GetCostingPrecision(); } return(_stdPrecision); }
/// <summary> /// Get Currency Iso Code. /// </summary> /// <param name="ctx">Ctx</param> /// <param name="C_Currency_ID">currency</param> /// <returns>ISO Code</returns> public static string GetISO_Code(Ctx ctx, int C_Currency_ID) { String contextKey = "C_Currency_" + C_Currency_ID; String retValue = ctx.GetContext(contextKey); if (retValue != null && retValue.Length > 0) { return(retValue); } // Create it MCurrency c = Get(ctx, C_Currency_ID); retValue = c.GetISO_Code(); ctx.SetContext(contextKey, retValue); return(retValue); }
/// <summary> /// Get Currency /// </summary> /// <param name="ctx">Ctx</param> /// <param name="C_Currency_ID">currency</param> /// <returns>ISO Code</returns> public static MCurrency Get(Ctx ctx, int C_Currency_ID) { // Try Cache int key = C_Currency_ID; MCurrency retValue = currencies[key]; if (retValue != null) { return(retValue); } // Create it retValue = new MCurrency(ctx, C_Currency_ID, null); // Save in System if (retValue.GetAD_Client_ID() == 0) { currencies.Add(key, retValue); } return(retValue); }
/** * Parent Constructor * @param invoice invoice * @param paySchedule payment schedule */ public MInvoicePaySchedule(MInvoice invoice, MPaySchedule paySchedule) : base(invoice.GetCtx(), 0, invoice.Get_TrxName()) { _parent = invoice; SetClientOrg(invoice); SetC_Invoice_ID(invoice.GetC_Invoice_ID()); SetC_PaySchedule_ID(paySchedule.GetC_PaySchedule_ID()); // Amounts int scale = MCurrency.GetStdPrecision(GetCtx(), invoice.GetC_Currency_ID()); // distribute schedule based on GrandTotalAfterWithholding which is (GrandTotal – WithholdingAmount) Decimal due = (invoice.Get_ColumnIndex("GrandTotalAfterWithholding") > 0 && invoice.GetGrandTotalAfterWithholding() != 0 ? invoice.GetGrandTotalAfterWithholding() : invoice.GetGrandTotal()); if (due.CompareTo(Env.ZERO) == 0) { SetDueAmt(Env.ZERO); SetDiscountAmt(Env.ZERO); SetIsValid(false); } else { //due = due.multiply(paySchedule.getPercentage()).divide(HUNDRED, scale, Decimal.ROUND_HALF_UP); due = Decimal.Multiply(due, Decimal.Divide(paySchedule.GetPercentage(), Decimal.Round(HUNDRED, scale, MidpointRounding.AwayFromZero))); SetDueAmt(due); Decimal discount = Decimal.Multiply(due, Decimal.Divide(paySchedule.GetDiscount(), Decimal.Round(HUNDRED, scale, MidpointRounding.AwayFromZero))); SetDiscountAmt(discount); SetIsValid(true); } /** Adhoc Payment - Setting DueDate for an InvoicePaySchedule ** Dt: 18/01/2021 ** Modified By: Kumar **/ // Dates DateTime?dueDate = (invoice.Get_ColumnIndex("DueDate") >= 0 && invoice.GetDueDate() >= invoice.GetDateInvoiced()) ? invoice.GetDueDate() : TimeUtil.AddDays(invoice.GetDateInvoiced(), paySchedule.GetNetDays()); SetDueDate(dueDate); DateTime discountDate = TimeUtil.AddDays(invoice.GetDateInvoiced(), paySchedule.GetDiscountDays()); SetDiscountDate(discountDate); }
/** * Parent Constructor * @param invoice invoice * @param paySchedule payment schedule */ public MInvoicePaySchedule(MInvoice invoice, MPaySchedule paySchedule) : base(invoice.GetCtx(), 0, invoice.Get_TrxName()) { _parent = invoice; SetClientOrg(invoice); SetC_Invoice_ID(invoice.GetC_Invoice_ID()); SetC_PaySchedule_ID(paySchedule.GetC_PaySchedule_ID()); // Amounts int scale = MCurrency.GetStdPrecision(GetCtx(), invoice.GetC_Currency_ID()); Decimal due = invoice.GetGrandTotal(); if (due.CompareTo(Env.ZERO) == 0) { SetDueAmt(Env.ZERO); SetDiscountAmt(Env.ZERO); SetIsValid(false); } else { //due = due.multiply(paySchedule.getPercentage()).divide(HUNDRED, scale, Decimal.ROUND_HALF_UP); due = Decimal.Multiply(due, Decimal.Divide(paySchedule.GetPercentage(), Decimal.Round(HUNDRED, scale, MidpointRounding.AwayFromZero))); SetDueAmt(due); Decimal discount = Decimal.Multiply(due, Decimal.Divide(paySchedule.GetDiscount(), Decimal.Round(HUNDRED, scale, MidpointRounding.AwayFromZero))); SetDiscountAmt(discount); SetIsValid(true); } // Dates DateTime dueDate = TimeUtil.AddDays(invoice.GetDateInvoiced(), paySchedule.GetNetDays()); SetDueDate(dueDate); DateTime discountDate = TimeUtil.AddDays(invoice.GetDateInvoiced(), paySchedule.GetDiscountDays()); SetDiscountDate(discountDate); }
/** * Before Save * @param newRecord new * @return true */ protected override bool BeforeSave(bool newRecord) { if (Is_ValueChanged("DueAmt")) { log.Fine("beforeSave"); SetIsValid(false); } oldDueAmt = Util.GetValueOfDecimal(Get_ValueOld("DueAmt")); if (Env.IsModuleInstalled("VA009_")) { // get invoice currency for rounding MCurrency currency = MCurrency.Get(GetCtx(), GetC_Currency_ID()); SetDueAmt(Decimal.Round(GetDueAmt(), currency.GetStdPrecision())); SetVA009_PaidAmntInvce(Decimal.Round(GetVA009_PaidAmntInvce(), currency.GetStdPrecision())); // when invoice schedule have payment reference then need to check payment mode on payment window & update here if (GetC_Payment_ID() > 0) { #region for payment MPayment payment = new MPayment(GetCtx(), GetC_Payment_ID(), Get_Trx()); SetVA009_PaymentMethod_ID(payment.GetVA009_PaymentMethod_ID()); // get payment method detail -- update to here DataSet dsPaymentMethod = DB.ExecuteDataset(@"SELECT VA009_PaymentMode, VA009_PaymentType, VA009_PaymentTrigger FROM VA009_PaymentMethod WHERE VA009_PaymentMethod_ID = " + payment.GetVA009_PaymentMethod_ID(), null, Get_Trx()); if (dsPaymentMethod != null && dsPaymentMethod.Tables.Count > 0 && dsPaymentMethod.Tables[0].Rows.Count > 0) { if (!String.IsNullOrEmpty(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMode"]))) { SetVA009_PaymentMode(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMode"])); } if (!String.IsNullOrEmpty(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentType"]))) { SetVA009_PaymentType(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentType"])); } SetVA009_PaymentTrigger(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentTrigger"])); } #endregion } else if (GetC_CashLine_ID() > 0) { #region For Cash // when invoice schedule have cashline reference then need to check // payment mode of "Cash" type having currency is null on "Payment Method" window & update here DataSet dsPaymentMethod = (DB.ExecuteDataset(@"SELECT VA009_PaymentMethod_ID, VA009_PaymentMode, VA009_PaymentType, VA009_PaymentTrigger FROM VA009_PaymentMethod WHERE IsActive = 'Y' AND AD_Client_ID = " + GetAD_Client_ID() + @" AND VA009_PaymentBaseType = 'B' AND NVL(C_Currency_ID , 0) = 0", null, Get_Trx())); if (dsPaymentMethod != null && dsPaymentMethod.Tables.Count > 0 && dsPaymentMethod.Tables[0].Rows.Count > 0) { SetVA009_PaymentMethod_ID(Util.GetValueOfInt(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMethod_ID"])); if (!String.IsNullOrEmpty(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMode"]))) { SetVA009_PaymentMode(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMode"])); } if (!String.IsNullOrEmpty(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentType"]))) { SetVA009_PaymentType(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentType"])); } SetVA009_PaymentTrigger(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentTrigger"])); } else { #region when we not found record of "Cash" then we will create a new rcord on Payment Method for Cash string sql = @"SELECT AD_TABLE_ID FROM AD_TABLE WHERE tablename LIKE 'VA009_PaymentMethod' AND IsActive = 'Y'"; int tableId = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null)); MTable tbl = new MTable(GetCtx(), tableId, Get_Trx()); PO po = tbl.GetPO(GetCtx(), 0, Get_Trx()); po.SetAD_Client_ID(GetAD_Client_ID()); po.SetAD_Org_ID(0); // Recod will be created in (*) Organization po.Set_Value("Value", "By Cash"); po.Set_Value("VA009_Name", "By Cash"); po.Set_Value("IsActive", true); po.Set_Value("VA009_PaymentBaseType", "B"); po.Set_Value("VA009_PaymentRule", "M"); po.Set_Value("VA009_PaymentMode", "C"); po.Set_Value("VA009_PaymentType", "S"); po.Set_Value("VA009_PaymentTrigger", "S"); po.Set_Value("C_Currency_ID", null); po.Set_Value("VA009_InitiatePay", false); if (!po.Save(Get_Trx())) { ValueNamePair pp = VLogger.RetrieveError(); log.Info("Error Occured when try to save record on Payment Method for Cash. Error Type : " + pp.GetValue()); } else { SetVA009_PaymentMethod_ID(Util.GetValueOfInt(po.Get_Value("VA009_PaymentMethod_ID"))); SetVA009_PaymentMode("C"); SetVA009_PaymentType("S"); SetVA009_PaymentTrigger("S"); } #endregion } #endregion } } // to set processing false because in case of new schedule processing is set to be false if (newRecord) { SetProcessing(false); // when schedile is not paid and invoice hedaer having "Hold Payment", then set "Hold payment" on schedule also if (Get_ColumnIndex("IsHoldPayment") > 0 && (GetC_Payment_ID() == 0 && GetC_CashLine_ID() == 0)) { String sql = "SELECT IsHoldPayment FROM C_Invoice WHERE C_Invoice_ID = " + GetC_Invoice_ID(); String IsHoldPayment = Util.GetValueOfString(DB.ExecuteScalar(sql, null, Get_Trx())); SetIsHoldPayment(IsHoldPayment.Equals("Y")); } } // if payment refrence not found on schedule the set withholdimh amount as ZERO if (GetC_Payment_ID() <= 0) { SetBackupWithholdingAmount(0); SetWithholdingAmt(0); } return(true); }
/// <summary> /// Process Allocation (does not update line). /// - Update and Link Invoice/Payment/Cash /// </summary> /// <param name="reverse">reverse if true allocation is reversed</param> /// <returns>C_BPartner_ID</returns> public int ProcessIt(bool reverse) { log.Fine("Reverse=" + reverse + " - " + ToString()); int C_Invoice_ID = GetC_Invoice_ID(); MInvoicePaySchedule invoiceSchedule = null; MPayment payment = null; MCashLine cashLine = null; MInvoice invoice = GetInvoice(); if (invoice != null && GetC_BPartner_ID() != invoice.GetC_BPartner_ID()) { SetC_BPartner_ID(invoice.GetC_BPartner_ID()); } // int C_Payment_ID = GetC_Payment_ID(); int C_CashLine_ID = GetC_CashLine_ID(); // Update Payment if (C_Payment_ID != 0) { payment = new MPayment(GetCtx(), C_Payment_ID, Get_TrxName()); if (GetC_BPartner_ID() != payment.GetC_BPartner_ID()) { log.Warning("C_BPartner_ID different - Invoice=" + GetC_BPartner_ID() + " - Payment=" + payment.GetC_BPartner_ID()); } if (reverse) { if (!payment.IsCashTrx()) { payment.SetIsAllocated(false); payment.Save(); } } else { if (payment.TestAllocation()) { payment.Save(); } } } // Payment - Invoice if (C_Payment_ID != 0 && invoice != null) { // Link to Invoice if (reverse) { invoice.SetC_Payment_ID(0); log.Fine("C_Payment_ID=" + C_Payment_ID + " Unlinked from C_Invoice_ID=" + C_Invoice_ID); } else if (invoice.IsPaid()) { invoice.SetC_Payment_ID(C_Payment_ID); log.Fine("C_Payment_ID=" + C_Payment_ID + " Linked to C_Invoice_ID=" + C_Invoice_ID); } // Link to Order String update = "UPDATE C_Order o " + "SET C_Payment_ID=" + (reverse ? "NULL " : "(SELECT C_Payment_ID FROM C_Invoice WHERE C_Invoice_ID=" + C_Invoice_ID + ") ") + "WHERE EXISTS (SELECT * FROM C_Invoice i " + "WHERE o.C_Order_ID=i.C_Order_ID AND i.C_Invoice_ID=" + C_Invoice_ID + ")"; if (DataBase.DB.ExecuteQuery(update, null, Get_TrxName()) > 0) { log.Fine("C_Payment_ID=" + C_Payment_ID + (reverse ? " UnLinked from" : " Linked to") + " order of C_Invoice_ID=" + C_Invoice_ID); } } // Cash - Invoice if (C_CashLine_ID != 0 && invoice != null) { // Link to Invoice if (reverse) { invoice.SetC_CashLine_ID(0); log.Fine("C_CashLine_ID=" + C_CashLine_ID + " Unlinked from C_Invoice_ID=" + C_Invoice_ID); // Set isallocated false on cashline while allocation gets deallocated assigned by Mukesh sir on 27/12/2017 MCashLine cashline = new MCashLine(GetCtx(), GetC_CashLine_ID(), Get_TrxName()); cashline.SetIsAllocated(false); cashline.Save(); } else { invoice.SetC_CashLine_ID(C_CashLine_ID); log.Fine("C_CashLine_ID=" + C_CashLine_ID + " Linked to C_Invoice_ID=" + C_Invoice_ID); } // Link to Order String update = "UPDATE C_Order o " + "SET C_CashLine_ID=" + (reverse ? "NULL " : "(SELECT C_CashLine_ID FROM C_Invoice WHERE C_Invoice_ID=" + C_Invoice_ID + ") ") + "WHERE EXISTS (SELECT * FROM C_Invoice i " + "WHERE o.C_Order_ID=i.C_Order_ID AND i.C_Invoice_ID=" + C_Invoice_ID + ")"; if (DataBase.DB.ExecuteQuery(update, null, Get_TrxName()) > 0) { log.Fine("C_CashLine_ID=" + C_CashLine_ID + (reverse ? " UnLinked from" : " Linked to") + " order of C_Invoice_ID=" + C_Invoice_ID); } } // Added by Bharat- Update Discrepancy amount on Invoice. if (C_Payment_ID == 0 && C_CashLine_ID == 0 && invoice != null) { if (invoice.Get_ColumnIndex("DiscrepancyAmt") >= 0) { decimal desAmt = invoice.GetDiscrepancyAmt(); decimal desAjusted = invoice.GetDiscrepancyAdjusted(); decimal allocAmt = Math.Abs(GetAmount()); // absolute if (reverse) { if (allocAmt > desAjusted) { desAmt = Decimal.Add(desAjusted, desAmt); desAjusted = 0; } else { desAmt = Decimal.Add(desAmt, allocAmt); desAjusted = Decimal.Subtract(desAjusted, allocAmt); } invoice.SetDiscrepancyAmt(desAmt); invoice.SetDiscrepancyAdjusted(desAjusted); if (desAmt > 0) { invoice.SetIsInDispute(true); } } else { if (allocAmt > desAmt) { desAjusted = Decimal.Add(desAjusted, desAmt); desAmt = 0; } else { desAjusted = Decimal.Add(desAjusted, allocAmt); desAmt = Decimal.Subtract(desAmt, allocAmt); } invoice.SetDiscrepancyAmt(desAmt); invoice.SetDiscrepancyAdjusted(desAjusted); if (desAmt == 0) { invoice.SetIsInDispute(false); } } if (!invoice.Save()) { log.Log(Level.SEVERE, "Invoice not updated - " + invoice); } } } // Update Balance / Credit used - Counterpart of MInvoice.completeIt if (invoice != null) { if (invoice.TestAllocation() && !invoice.Save()) { log.Log(Level.SEVERE, "Invoice not updated - " + invoice); } else if (reverse) { // added by Amit // if payment Management module downloaded and Invoice Schedule id available on Allocation then mark ispaid on schedule as false if (Env.IsModuleInstalled("VA009_")) { MAllocationHdr allocHdr = new MAllocationHdr(GetCtx(), GetC_AllocationHdr_ID(), Get_Trx()); decimal payAmt = 0; MDocType doctype = null; MCurrency currency = new MCurrency(GetCtx(), invoice.GetC_Currency_ID(), null); if (GetC_InvoicePaySchedule_ID() != 0 && !invoice.IsPaid()) { invoiceSchedule = new MInvoicePaySchedule(GetCtx(), GetC_InvoicePaySchedule_ID(), Get_TrxName()); invoiceSchedule.SetVA009_IsPaid(false); // when we update schedule paid as False, then update payment method and related fields on schedule as on Invoice Header if (reverse) { invoiceSchedule.SetVA009_PaymentMethod_ID(invoice.GetVA009_PaymentMethod_ID()); DataSet dsPaymentMethod = DB.ExecuteDataset(@"SELECT VA009_PaymentMode, VA009_PaymentType, VA009_PaymentTrigger FROM VA009_PaymentMethod WHERE IsActive = 'Y' AND VA009_PaymentMethod_ID = " + invoice.GetVA009_PaymentMethod_ID(), null, Get_Trx()); if (dsPaymentMethod != null && dsPaymentMethod.Tables.Count > 0 && dsPaymentMethod.Tables[0].Rows.Count > 0) { if (!String.IsNullOrEmpty(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMode"]))) { invoiceSchedule.SetVA009_PaymentMode(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentMode"])); } if (!String.IsNullOrEmpty(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentType"]))) { invoiceSchedule.SetVA009_PaymentType(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentType"])); } invoiceSchedule.SetVA009_PaymentTrigger(Util.GetValueOfString(dsPaymentMethod.Tables[0].Rows[0]["VA009_PaymentTrigger"])); } dsPaymentMethod = null; } if (reverse && payment != null) { #region Handle for Payment & Invoice Allocation doctype = new MDocType(GetCtx(), invoice.GetC_DocType_ID(), null); // convert (payment amount / Amount from View Allocation) to invoice currency amount then subtract Paid invoice amount to calculated amount if (doctype.GetDocBaseType() == "ARC" || doctype.GetDocBaseType() == "APC") { if (payment.GetC_Invoice_ID() != 0) { // when payment created with invoice refernce direct // convert payment amount in invoice amt with payment date and payment conversion type payAmt = MConversionRate.Convert(GetCtx(), Decimal.Negate(Decimal.Add(Decimal.Add(payment.GetPayAmt(), payment.GetDiscountAmt()), payment.GetWriteOffAmt())), payment.GetC_Currency_ID(), invoice.GetC_Currency_ID(), payment.GetDateAcct(), payment.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); } else { // when payment created with Payment Allocate entry OR // when we match payment with invoice through Payment Allocation form // convert payment amount in invoice amt with view allocation date payAmt = MConversionRate.Convert(GetCtx(), Decimal.Negate(Decimal.Add(Decimal.Add(GetAmount(), GetDiscountAmt()), GetWriteOffAmt())), allocHdr.GetC_Currency_ID(), invoice.GetC_Currency_ID(), allocHdr.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); if (doctype.GetDocBaseType() == "APC") { payAmt = Decimal.Negate(payAmt); } } } else { if (payment.GetC_Invoice_ID() != 0) { // when we create payment with invoice reference direct // convert payment amount in invoice amt with payment date and payment conversion type payAmt = MConversionRate.Convert(GetCtx(), Decimal.Add(Decimal.Add(payment.GetPayAmt(), payment.GetDiscountAmt()), payment.GetWriteOffAmt()), payment.GetC_Currency_ID(), invoice.GetC_Currency_ID(), payment.GetDateAcct(), payment.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); } else { // when payment created with Payment Allocate entry Or // when we match payment with invoice through Payment Allocation form // convert payment amount in invoice amt with view allocation date payAmt = MConversionRate.Convert(GetCtx(), Decimal.Add(Decimal.Add(GetAmount(), GetDiscountAmt()), GetWriteOffAmt()), allocHdr.GetC_Currency_ID(), invoice.GetC_Currency_ID(), allocHdr.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); if (doctype.GetDocBaseType() == "API") { payAmt = Decimal.Negate(payAmt); } } } invoiceSchedule.SetVA009_PaidAmntInvce(Decimal.Round(Decimal.Subtract(invoiceSchedule.GetVA009_PaidAmntInvce(), payAmt), currency.GetStdPrecision())); // during reversal, if Invoice paid amount <> 0 then reduce that amount from next schedule try { if (invoiceSchedule.GetVA009_PaidAmntInvce() != 0) { int no = Util.GetValueOfInt(DB.ExecuteQuery(@"UPDATE C_InvoicePaySchedule SET VA009_PaidAmntInvce = NVL(VA009_PaidAmntInvce , 0) + " + Decimal.Round(invoiceSchedule.GetVA009_PaidAmntInvce(), currency.GetStdPrecision()) + @" , VA009_PaidAmnt = NVL(VA009_PaidAmnt , 0) + " + Decimal.Round(MConversionRate.ConvertBase(GetCtx(), invoiceSchedule.GetVA009_PaidAmntInvce(), invoice.GetC_Currency_ID(), invoice.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()), currency.GetStdPrecision()) + @" WHERE C_InvoicePaySchedule_ID = ( SELECT MIN(C_InvoicePaySchedule_ID) FROM C_InvoicePaySchedule WHERE IsActive = 'Y' AND VA009_IsPaid = 'N' AND C_Invoice_ID = " + invoice.GetC_Invoice_ID() + @" AND C_InvoicePaySchedule_ID <> " + GetC_InvoicePaySchedule_ID() + " ) ", null, Get_Trx())); // set paid invoice amount = 0, no > 0 bcz this is not last schedule if (no > 0) { invoiceSchedule.SetVA009_PaidAmntInvce(0); } } } catch { } // convert invoice paid amount to base currency amount invoiceSchedule.SetVA009_PaidAmnt(Decimal.Round(MConversionRate.ConvertBase(GetCtx(), invoiceSchedule.GetVA009_PaidAmntInvce(), invoice.GetC_Currency_ID(), invoice.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()), currency.GetStdPrecision())); // set Currency Variance amount as 0, when we reverse paymnet/ cash journalor allocation against this schedule invoiceSchedule.SetVA009_Variance(0); // remove linking of Payment from schedule invoiceSchedule.SetC_Payment_ID(0); #endregion } else if (reverse && C_CashLine_ID > 0) { #region Handle fo Cash Journal & Invoice Allocation doctype = new MDocType(GetCtx(), invoice.GetC_DocType_ID(), null); cashLine = new MCashLine(GetCtx(), C_CashLine_ID, Get_Trx()); // convert cash amount to invoice currency amount with allocation date then subtract Paid invoice amount to calculated amount if (doctype.GetDocBaseType() == "ARC" || doctype.GetDocBaseType() == "API") { payAmt = Decimal.Negate(Decimal.Add(Decimal.Add(GetAmount(), GetDiscountAmt()), GetWriteOffAmt())); payAmt = MConversionRate.Convert(GetCtx(), payAmt, allocHdr.GetC_Currency_ID(), invoice.GetC_Currency_ID(), allocHdr.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); invoiceSchedule.SetVA009_PaidAmntInvce(Decimal.Round(Decimal.Subtract(invoiceSchedule.GetVA009_PaidAmntInvce(), payAmt), currency.GetStdPrecision())); } else { payAmt = Decimal.Add(Decimal.Add(GetAmount(), GetDiscountAmt()), GetWriteOffAmt()); payAmt = MConversionRate.Convert(GetCtx(), payAmt, allocHdr.GetC_Currency_ID(), invoice.GetC_Currency_ID(), allocHdr.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); invoiceSchedule.SetVA009_PaidAmntInvce(Decimal.Round(Decimal.Subtract(invoiceSchedule.GetVA009_PaidAmntInvce(), payAmt), currency.GetStdPrecision())); } // during reversal, if Invoice paid amount <> 0 then reduce that amount from next schedule try { if (invoiceSchedule.GetVA009_PaidAmntInvce() != 0) { int no = Util.GetValueOfInt(DB.ExecuteQuery(@"UPDATE C_InvoicePaySchedule SET VA009_PaidAmntInvce = NVL(VA009_PaidAmntInvce , 0) + " + Decimal.Round(invoiceSchedule.GetVA009_PaidAmntInvce(), currency.GetStdPrecision()) + @" , VA009_PaidAmnt = NVL(VA009_PaidAmnt , 0) + " + Decimal.Round(MConversionRate.ConvertBase(GetCtx(), invoiceSchedule.GetVA009_PaidAmntInvce(), invoice.GetC_Currency_ID(), invoice.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()), currency.GetStdPrecision()) + @" WHERE C_InvoicePaySchedule_ID = ( SELECT MIN(C_InvoicePaySchedule_ID) FROM C_InvoicePaySchedule WHERE IsActive = 'Y' AND VA009_IsPaid = 'N' AND C_Invoice_ID = " + invoice.GetC_Invoice_ID() + @" AND C_InvoicePaySchedule_ID <> " + GetC_InvoicePaySchedule_ID() + " ) ", null, Get_Trx())); // set paid invoice amount = 0, no > 0 bcz this is not last schedule if (no > 0) { invoiceSchedule.SetVA009_PaidAmntInvce(0); } } } catch { } // convert invoice paid amount to base currency amount invoiceSchedule.SetVA009_PaidAmnt(Decimal.Round(MConversionRate.ConvertBase(GetCtx(), invoiceSchedule.GetVA009_PaidAmntInvce(), invoice.GetC_Currency_ID(), invoice.GetDateAcct(), invoice.GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()), currency.GetStdPrecision())); // set Currency Variance amount as 0, when we reverse paymnet/ cash journalor allocation against this schedule invoiceSchedule.SetVA009_Variance(0); // remove linking of cash line from schedule invoiceSchedule.SetC_CashLine_ID(0); #endregion } else { invoiceSchedule.SetVA009_PaidAmntInvce(0); invoiceSchedule.SetVA009_PaidAmnt(0); // set Currency Variance amount as 0, when we reverse paymnet/ cash journalor allocation against this schedule invoiceSchedule.SetVA009_Variance(0); } if (!invoiceSchedule.Save(Get_TrxName())) { log.Log(Level.SEVERE, "Invoice Pay Schedule not updated - " + invoice); } } } } } return(GetC_BPartner_ID()); }
/** * Export to File * @param checks array of checks * @param file file to export checks * @return number of lines */ public static int ExportToFile(MPaySelectionCheck[] checks, File file) { if (checks == null || checks.Length == 0) { return(0); } // Must be a file if (file.isDirectory()) { _log.Log(Level.WARNING, "File is directory - " + file.getAbsolutePath()); return(0); } // delete if exists try { if (file.exists()) { file.delete(); } } catch (Exception e) { _log.Log(Level.WARNING, "Could not delete - " + file.getAbsolutePath(), e); } char x = '"'; // ease int noLines = 0; StringBuilder line = null; try { FileWriter fw = new FileWriter(file); // write header line = new StringBuilder(); line.Append(x).Append("Value").Append(x).Append(",") .Append(x).Append("Name").Append(x).Append(",") .Append(x).Append("Contact").Append(x).Append(",") .Append(x).Append("Addr1").Append(x).Append(",") .Append(x).Append("Addr2").Append(x).Append(",") .Append(x).Append("City").Append(x).Append(",") .Append(x).Append("State").Append(x).Append(",") .Append(x).Append("ZIP").Append(x).Append(",") .Append(x).Append("Country").Append(x).Append(",") .Append(x).Append("ReferenceNo").Append(x).Append(",") .Append(x).Append("BPRoutingNo").Append(x).Append(",") .Append(x).Append("BPAccountNo").Append(x).Append(",") .Append(x).Append("BPAName").Append(x).Append(",") .Append(x).Append("BPACity").Append(x).Append(",") .Append(x).Append("BPBBAN").Append(x).Append(",") .Append(x).Append("BPIBAN").Append(x).Append(",") .Append(x).Append("BAName").Append(x).Append(",") .Append(x).Append("BARoutingNo").Append(x).Append(",") .Append(x).Append("BASwiftCode").Append(x).Append(",") .Append(x).Append("DocumentNo").Append(x).Append(",") .Append(x).Append("PayDate").Append(x).Append(",") .Append(x).Append("Currency").Append(x).Append(",") .Append(x).Append("PayAmount").Append(x).Append(",") .Append(x).Append("Comment").Append(x) .Append(Env.NL); fw.write(line.ToString()); noLines++; // write lines for (int i = 0; i < checks.Length; i++) { MPaySelectionCheck mpp = checks[i]; if (mpp == null) { continue; } // BPartner Info String[] bp = GetBPartnerInfo(mpp.GetC_BPartner_ID()); // TarGet BankAccount Info String[] bpba = GetBPBankAccountInfo(mpp.GetC_BP_BankAccount_ID()); // Comment - list of invoice document no StringBuilder comment = new StringBuilder(); MPaySelectionLine[] psls = mpp.GetPaySelectionLines(false); for (int l = 0; l < psls.Length; l++) { if (l > 0) { comment.Append(", "); } comment.Append(psls[l].GetInvoice().GetDocumentNo()); } line = new StringBuilder(); line.Append(x).Append(bp[BP_VALUE]).Append(x).Append(",") // Value .Append(x).Append(bp[BP_NAME]).Append(x).Append(",") // Name .Append(x).Append(bp[BP_CONTACT]).Append(x).Append(",") // Contact .Append(x).Append(bp[BP_ADDR1]).Append(x).Append(",") // Addr1 .Append(x).Append(bp[BP_ADDR2]).Append(x).Append(",") // Addr2 .Append(x).Append(bp[BP_CITY]).Append(x).Append(",") // City .Append(x).Append(bp[BP_REGION]).Append(x).Append(",") // State .Append(x).Append(bp[BP_POSTAL]).Append(x).Append(",") // ZIP .Append(x).Append(bp[BP_COUNTRY]).Append(x).Append(",") // Country .Append(x).Append(bp[BP_REFNO]).Append(x).Append(",") // ReferenceNo .Append(x).Append(bpba[BPBA_RoutingNo]).Append(x).Append(",") // Routing No (as of BPBankAccount .Append(x).Append(bpba[BPBA_AccountNo]).Append(x).Append(",") // AccountNo .Append(x).Append(bpba[BPBA_AName]).Append(x).Append(",") // Account Name .Append(x).Append(bpba[BPBA_ACity]).Append(x).Append(",") // Account City .Append(x).Append(bpba[BPBA_BBAN]).Append(x).Append(",") // BBAN .Append(x).Append(bpba[BPBA_IBAN]).Append(x).Append(",") // IBAN .Append(x).Append(bpba[BA_Name]).Append(x).Append(",") // Bank Name .Append(x).Append(bpba[BA_RoutingNo]).Append(x).Append(",") // Bank RoutingNo .Append(x).Append(bpba[BA_SwitftCode]).Append(x).Append(",") // SwiftCode // Payment Info .Append(x).Append(mpp.GetDocumentNo()).Append(x).Append(",") // DocumentNo .Append(mpp.GetParent().GetPayDate()).Append(",") // PayDate .Append(x).Append(MCurrency.GetISO_Code(Env.GetContext(), mpp.GetParent().GetC_Currency_ID())).Append(x).Append(",") // Currency .Append(mpp.GetPayAmt()).Append(",") // PayAmount .Append(x).Append(comment.ToString()).Append(x) // Comment .Append(Env.NL); fw.write(line.ToString()); noLines++; } // write line fw.flush(); fw.close(); } catch (Exception e) { _log.Log(Level.SEVERE, "", e); } return(noLines); }
/// <summary> /// Get Standard Precision. /// </summary> /// <param name="ctx">Ctx</param> /// <param name="C_Currency_ID">currency</param> /// <returns>Standard Precision</returns> public static int GetStdPrecision(Ctx ctx, int C_Currency_ID) { MCurrency c = Get(ctx, C_Currency_ID); return(c.GetStdPrecision()); }
} // validate /// <summary> /// Distribute Amount to Lines /// </summary> /// <param name="acct">account</param> /// <param name="Amt">amount</param> /// <param name="C_Currency_ID">currency</param> public void Distribute(MAccount acct, Decimal Amt, int C_Currency_ID) { log.Info("Amt=" + Amt + " - " + acct); GetLines(false); int precision = MCurrency.GetStdPrecision(GetCtx(), C_Currency_ID); // First Round Decimal total = Env.ZERO; int indexBiggest = -1; int indexZeroPercent = -1; for (int i = 0; i < _lines.Length; i++) { MDistributionLine dl = _lines[i]; if (!dl.IsActive()) { continue; } dl.SetAccount(acct); // Calculate Amount dl.CalculateAmt(Amt, precision); //total = total.add(dl.GetAmt()); total = Decimal.Add(total, dl.GetAmt()); // log.fine("distribute - Line=" + dl.getLine() + " - " + dl.getPercent() + "% " + dl.getAmt() + " - Total=" + total); // Remainder if (Env.Signum(dl.GetPercentDistribution()) == 0) { indexZeroPercent = i; } if (indexZeroPercent == -1) { if (indexBiggest == -1) { indexBiggest = i; } else if (dl.GetAmt().CompareTo(_lines[indexBiggest].GetAmt()) > 0) { indexBiggest = i; } } } // Adjust Remainder //Decimal difference = Amt.subtract(total); Decimal difference = Decimal.Subtract(Amt, total); if (difference.CompareTo(Env.ZERO) != 0) { if (indexZeroPercent != -1) { // log.fine("distribute - Difference=" + difference + " - 0%Line=" + _lines[indexZeroPercent]); _lines[indexZeroPercent].SetAmt(difference); } else if (indexBiggest != -1) { // log.fine("distribute - Difference=" + difference + " - MaxLine=" + _lines[indexBiggest] + " - " + _lines[indexBiggest].getAmt()); //_lines[indexBiggest].SetAmt (_lines[indexBiggest].GetAmt().add(difference)); _lines[indexBiggest].SetAmt(Decimal.Add(_lines[indexBiggest].GetAmt(), difference)); } else { log.Warning("Remaining Difference=" + difference); } } // if (VLogMgt.IsLevelFinest()) //if (CLogMgt.isLevelFinest()) { for (int i = 0; i < _lines.Length; i++) { if (_lines[i].IsActive()) { log.Fine("Amt=" + _lines[i].GetAmt() + " - " + _lines[i].GetAccount()); } } } } // distribute