/** * After Save * @param newRecord * @param success * @return success */ protected override bool AfterSave(bool newRecord, bool success) { if (!success) { return(success); } if (!UpdateCbAndLine()) { return(false); } if (GetVSS_PAYMENTTYPE() == X_C_CashLine.VSS_PAYMENTTYPE_Payment && GetC_BPartner_ID() > 0) { MCash csh = new MCash(GetCtx(), GetC_Cash_ID(), Get_TrxName()); Decimal amt = MConversionRate.ConvertBase(GetCtx(), GetAmount(), // CM adjusted GetC_Currency_ID(), csh.GetDateAcct(), 0, GetAD_Client_ID(), GetAD_Org_ID()); MBPartner bp = new MBPartner(GetCtx(), GetC_BPartner_ID(), Get_Trx()); string retMsg = ""; bool crdAll = bp.IsCreditAllowed(GetC_BPartner_Location_ID(), Decimal.Subtract(0, amt), out retMsg); if (!crdAll) { log.SaveWarning("Warning", retMsg); } else if (bp.IsCreditWatch(GetC_BPartner_Location_ID())) { log.SaveWarning("Warning", Msg.GetMsg(GetCtx(), "VIS_BPCreditWatch")); } } return(true); }
/// <summary> /// Set Converted Amt /// </summary> /// <param name="date">date for conversion</param> public void SetConvertedAmt(DateTime?date) { Decimal amt = MConversionRate.ConvertBase(GetCtx(), GetActualAmt(), GetC_Currency_ID(), date, 0, // type GetAD_Client_ID(), GetAD_Org_ID()); //if (amt != null) SetConvertedAmt(amt); }
/// <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()); }