/// <summary> /// Perrform Process. /// </summary> /// <returns>Message (clear text)</returns> protected override String DoIt() { log.Info("C_Order_ID=" + _C_Order_ID + ", C_Invoice_ID=" + _C_Invoice_ID); if (_C_Order_ID == 0 && _C_Invoice_ID == 0) { throw new Exception("Nothing to do"); } String retValue = ""; if (_C_Order_ID != 0) { MOrder order = new MOrder(GetCtx(), _C_Order_ID, Get_TrxName()); Decimal oldPrice = order.GetGrandTotal(); MOrderLine[] lines = order.GetLines(); for (int i = 0; i < lines.Length; i++) { lines[i].SetPrice(order.GetM_PriceList_ID()); lines[i].Save(); } order = new MOrder(GetCtx(), _C_Order_ID, Get_TrxName()); Decimal newPrice = order.GetGrandTotal(); retValue = order.GetDocumentNo() + ": " + oldPrice + " -> " + newPrice; } if (_C_Invoice_ID != 0) { MInvoice invoice = new MInvoice(GetCtx(), _C_Invoice_ID, null); Decimal oldPrice = invoice.GetGrandTotal(); MInvoiceLine[] lines = invoice.GetLines(false); for (int i = 0; i < lines.Length; i++) { lines[i].SetPrice(invoice.GetM_PriceList_ID(), invoice.GetC_BPartner_ID()); lines[i].Save(); } invoice = new MInvoice(GetCtx(), _C_Invoice_ID, null); Decimal newPrice = invoice.GetGrandTotal(); if (retValue.Length > 0) { retValue += Env.NL; } retValue += invoice.GetDocumentNo() + ": " + oldPrice + " -> " + newPrice; } // return(retValue); }
} // afterSave /// <summary> /// This function is used to set Values on Recognition Plan /// </summary> /// <param name="invoiceLine">invoice line object </param> /// <param name="invoice">invoice object</param> /// <param name="C_RevenueRecognition_ID">Recognition ID</param> /// <param name="ToCurrency">Currency</param> public void SetRecognitionPlan(MInvoiceLine invoiceLine, MInvoice invoice, int C_RevenueRecognition_ID, int ToCurrency) { SetAD_Client_ID(invoice.GetAD_Client_ID()); SetAD_Org_ID(invoice.GetAD_Org_ID()); SetC_Currency_ID(ToCurrency); SetC_InvoiceLine_ID(invoiceLine.GetC_InvoiceLine_ID()); SetC_RevenueRecognition_ID(C_RevenueRecognition_ID); // when tax include into price list, then reduce tax from Line Net Amount bool isTaxIncide = (new MPriceList(invoice.GetCtx(), invoice.GetM_PriceList_ID(), invoice.Get_Trx())).IsTaxIncluded(); Decimal Amount = invoiceLine.GetLineNetAmt() - (isTaxIncide ? (invoiceLine.GetTaxAmt() + invoiceLine.GetSurchargeAmt()) : 0); if (invoice.GetC_Currency_ID() != ToCurrency) { Amount = MConversionRate.Convert(GetCtx(), Amount, invoice.GetC_Currency_ID(), ToCurrency, invoice.GetDateInvoiced(), invoice.GetC_ConversionType_ID(), invoice.GetAD_Client_ID(), invoice.GetAD_Org_ID()); } SetTotalAmt(Amount); SetRecognizedAmt(Env.ZERO); }
/// <summary> /// Generate Invoices /// </summary> /// <param name="idr">pstmt order query</param> /// <returns>info</returns> //private String Generate(IDataReader idr) private String Generate(DataTable dt) { //JID_1139 Avoided the duplicate charge line created Invoice(customer) bool isAllownonItem = Util.GetValueOfString(GetCtx().GetContext("$AllowNonItem")).Equals("Y"); foreach (DataRow dr in dt.Rows) { MOrder order = new MOrder(GetCtx(), dr, Get_TrxName()); // Credit Limit check MBPartner bp = MBPartner.Get(GetCtx(), order.GetC_BPartner_ID()); if (bp.GetCreditStatusSettingOn() == "CH") { decimal creditLimit = bp.GetSO_CreditLimit(); string creditVal = bp.GetCreditValidation(); if (creditLimit != 0) { decimal creditAvlb = creditLimit - bp.GetSO_CreditUsed(); if (creditAvlb <= 0) { if (creditVal == "C" || creditVal == "D" || creditVal == "F") { AddLog(Msg.GetMsg(GetCtx(), "StopInvoice") + bp.GetName()); continue; } else if (creditVal == "I" || creditVal == "J" || creditVal == "L") { if (_msg != null) { _msg.Clear(); } _msg.Append(Msg.GetMsg(GetCtx(), "WarningInvoice") + bp.GetName()); //AddLog(Msg.GetMsg(GetCtx(), "WarningInvoice") + bp.GetName()); } } } } // JID_0161 // change here now will check credit settings on field only on Business Partner Header // Lokesh Chauhan 15 July 2019 else if (bp.GetCreditStatusSettingOn() == X_C_BPartner.CREDITSTATUSSETTINGON_CustomerLocation) { MBPartnerLocation bpl = new MBPartnerLocation(GetCtx(), order.GetC_BPartner_Location_ID(), null); //MBPartner bpartner = MBPartner.Get(GetCtx(), order.GetC_BPartner_ID()); //if (bpl.GetCreditStatusSettingOn() == "CL") //{ decimal creditLimit = bpl.GetSO_CreditLimit(); string creditVal = bpl.GetCreditValidation(); if (creditLimit != 0) { decimal creditAvlb = creditLimit - bpl.GetSO_CreditUsed(); if (creditAvlb <= 0) { if (creditVal == "C" || creditVal == "D" || creditVal == "F") { AddLog(Msg.GetMsg(GetCtx(), "StopInvoice") + bp.GetName() + " " + bpl.GetName()); continue; } else if (creditVal == "I" || creditVal == "J" || creditVal == "L") { if (_msg != null) { _msg.Clear(); } _msg.Append(Msg.GetMsg(GetCtx(), "WarningInvoice") + bp.GetName() + " " + bpl.GetName()); //AddLog(Msg.GetMsg(GetCtx(), "WarningInvoice") + bp.GetName() + " " + bpl.GetName()); } } } //} } // Credit Limit End // New Invoice Location // JID_1237 : While creating invoice need to consolidate order on the basis of Org, Payment Term, BP Location (Bill to Location) and Pricelist. if (!_ConsolidateDocument || (_invoice != null && (_invoice.GetC_BPartner_Location_ID() != order.GetBill_Location_ID() || _invoice.GetC_PaymentTerm_ID() != order.GetC_PaymentTerm_ID() || _invoice.GetM_PriceList_ID() != order.GetM_PriceList_ID() || _invoice.GetAD_Org_ID() != order.GetAD_Org_ID() || ((_invoice.GetC_ConversionType_ID() != 0 ? _invoice.GetC_ConversionType_ID() : defaultConversionType) != (order.GetC_ConversionType_ID() != 0 ? order.GetC_ConversionType_ID() : defaultConversionType)) ))) { CompleteInvoice(); } bool completeOrder = MOrder.INVOICERULE_AfterOrderDelivered.Equals(order.GetInvoiceRule()); // Schedule After Delivery bool doInvoice = false; if (MOrder.INVOICERULE_CustomerScheduleAfterDelivery.Equals(order.GetInvoiceRule())) { _bp = new MBPartner(GetCtx(), order.GetBill_BPartner_ID(), null); if (_bp.GetC_InvoiceSchedule_ID() == 0) { log.Warning("BPartner has no Schedule - set to After Delivery"); order.SetInvoiceRule(MOrder.INVOICERULE_AfterDelivery); order.Save(); } else { MInvoiceSchedule ins = MInvoiceSchedule.Get(GetCtx(), _bp.GetC_InvoiceSchedule_ID(), Get_TrxName()); if (ins.CanInvoice(order.GetDateOrdered(), order.GetGrandTotal())) { doInvoice = true; } else { continue; } } } // Schedule // After Delivery if (doInvoice || MOrder.INVOICERULE_AfterDelivery.Equals(order.GetInvoiceRule())) { MInOut shipment = null; MInOutLine[] shipmentLines = order.GetShipmentLines(); MOrderLine[] oLines = order.GetLines(true, null); for (int i = 0; i < shipmentLines.Length; i++) { MInOutLine shipLine = shipmentLines[i]; if (shipLine.IsInvoiced()) { continue; } if (shipment == null || shipment.GetM_InOut_ID() != shipLine.GetM_InOut_ID()) { shipment = new MInOut(GetCtx(), shipLine.GetM_InOut_ID(), Get_TrxName()); } if (!shipment.IsComplete() || // ignore incomplete or reversals shipment.GetDocStatus().Equals(MInOut.DOCSTATUS_Reversed)) { continue; } //JID_1139 Avoided the duplicate charge records if (shipLine.GetM_Product_ID() > 0 || isAllownonItem) { CreateLine(order, shipment, shipLine); } }// shipment lines //JID_1139 Avoided the duplicate charge records if (!isAllownonItem) { for (int i = 0; i < oLines.Length; i++) { MOrderLine oLine = oLines[i]; if (oLine.GetC_Charge_ID() > 0) { Decimal toInvoice = Decimal.Subtract(oLine.GetQtyOrdered(), oLine.GetQtyInvoiced()); log.Fine("Immediate - ToInvoice=" + toInvoice + " - " + oLine); Decimal qtyEntered = toInvoice; // Correct UOM for QtyEntered if (oLine.GetQtyEntered().CompareTo(oLine.GetQtyOrdered()) != 0) { qtyEntered = Decimal.Round(Decimal.Divide(Decimal.Multiply( toInvoice, oLine.GetQtyEntered()), oLine.GetQtyOrdered()), 12, MidpointRounding.AwayFromZero); } //JID_1139_1 avoided the charge line with 0 qty inserted if (oLine.IsContract() == false && oLine.GetQtyOrdered() > oLine.GetQtyInvoiced()) { CreateLine(order, oLine, toInvoice, qtyEntered); log.Info("ID " + oLine.Get_ID() + "Qty Ordered " + oLine.GetQtyOrdered() + " Qty Invoiced " + oLine.GetQtyInvoiced()); } } } } } // After Order Delivered, Immediate else { MOrderLine[] oLines = order.GetLines(true, null); for (int i = 0; i < oLines.Length; i++) { MOrderLine oLine = oLines[i]; Decimal toInvoice = Decimal.Subtract(oLine.GetQtyOrdered(), oLine.GetQtyInvoiced()); if (toInvoice.CompareTo(Env.ZERO) == 0 && oLine.GetM_Product_ID() != 0) { continue; } // bool fullyDelivered = oLine.GetQtyOrdered().CompareTo(oLine.GetQtyDelivered()) == 0; //JID_1136: While creating the Invoices against the charge system should not check the Ordered qty= Delivered qty. need to check this only in case of products if (completeOrder && oLine.GetC_Charge_ID() > 0) { fullyDelivered = true; if (oLine.GetC_Charge_ID() > 0) { log.Fine("After Order Delivery - ToInvoice=" + toInvoice + " - " + oLine); Decimal qtyEntered = toInvoice; // Correct UOM for QtyEntered if (oLine.GetQtyEntered().CompareTo(oLine.GetQtyOrdered()) != 0) { qtyEntered = Decimal.Round(Decimal.Divide(Decimal.Multiply( toInvoice, oLine.GetQtyEntered()), oLine.GetQtyOrdered()), 12, MidpointRounding.AwayFromZero); } // if (oLine.IsContract() == false && !isAllownonItem) { CreateLine(order, oLine, toInvoice, qtyEntered); log.Info("ID " + oLine.Get_ID() + "Qty Ordered " + oLine.GetQtyOrdered() + " Qty Invoiced " + oLine.GetQtyInvoiced()); } } } // Complete Order if (completeOrder && !fullyDelivered) { log.Fine("Failed CompleteOrder - " + oLine); completeOrder = false; break; } // Immediate else if (MOrder.INVOICERULE_Immediate.Equals(order.GetInvoiceRule())) { log.Fine("Immediate - ToInvoice=" + toInvoice + " - " + oLine); Decimal qtyEntered = toInvoice; // Correct UOM for QtyEntered if (oLine.GetQtyEntered().CompareTo(oLine.GetQtyOrdered()) != 0) { qtyEntered = Decimal.Round(Decimal.Divide(Decimal.Multiply( toInvoice, oLine.GetQtyEntered()), oLine.GetQtyOrdered()), 12, MidpointRounding.AwayFromZero); } // if (oLine.IsContract() == false) { CreateLine(order, oLine, toInvoice, qtyEntered); log.Info("ID " + oLine.Get_ID() + "Qty Ordered " + oLine.GetQtyOrdered() + " Qty Invoiced " + oLine.GetQtyInvoiced()); } } else { log.Fine("Failed: " + order.GetInvoiceRule() + " - ToInvoice=" + toInvoice + " - " + oLine); } } // for all order lines if (MOrder.INVOICERULE_Immediate.Equals(order.GetInvoiceRule())) { _line += 1000; } } // Complete Order successful if (completeOrder && MOrder.INVOICERULE_AfterOrderDelivered.Equals(order.GetInvoiceRule())) { MInOut[] shipments = order.GetShipments(true); for (int i = 0; i < shipments.Length; i++) { MInOut ship = shipments[i]; if (!ship.IsComplete() || // ignore incomplete or reversals ship.GetDocStatus().Equals(MInOut.DOCSTATUS_Reversed)) { continue; } MInOutLine[] shipLines = ship.GetLines(false); for (int j = 0; j < shipLines.Length; j++) { MInOutLine shipLine = shipLines[j]; if (!order.IsOrderLine(shipLine.GetC_OrderLine_ID())) { continue; } if (!shipLine.IsInvoiced()) { CreateLine(order, ship, shipLine); } } // lines _line += 1000; } // all shipments } // complete Order } // for all orders CompleteInvoice(); return("@Created@ = " + _created); }
/// <summary> /// Perform Process. /// </summary> /// <returns>Message (variables are parsed)</returns> protected override String DoIt() { log.Info("doIt - C_CommissionRun_ID=" + GetRecord_ID()); // Load Data MCommissionRun comRun = new MCommissionRun(GetCtx(), GetRecord_ID(), Get_Trx()); if (comRun.Get_ID() == 0) { throw new ArgumentException("CommissionAPInvoice - No Commission Run"); } if (Env.ZERO.CompareTo(comRun.GetGrandTotal()) == 0) { throw new ArgumentException("@GrandTotal@ = 0"); } MCommission com = new MCommission(GetCtx(), comRun.GetC_Commission_ID(), Get_Trx()); if (com.Get_ID() == 0) { throw new ArgumentException("CommissionAPInvoice - No Commission"); } if (com.GetC_Charge_ID() == 0) { throw new ArgumentException("CommissionAPInvoice - No Charge on Commission"); } MBPartner bp = new MBPartner(GetCtx(), com.GetC_BPartner_ID(), Get_Trx()); if (bp.Get_ID() == 0) { throw new ArgumentException("CommissionAPInvoice - No BPartner"); } // Create Invoice MInvoice invoice = new MInvoice(GetCtx(), 0, null); invoice.SetClientOrg(com.GetAD_Client_ID(), com.GetAD_Org_ID()); invoice.SetC_DocTypeTarget_ID(MDocBaseType.DOCBASETYPE_APINVOICE); // API invoice.SetBPartner(bp); // JID_0101: When we generate the AP invoice from Commission run window, its giving price list error. if (invoice.GetM_PriceList_ID() == 0) { string sql = "SELECT M_PriceList_ID FROM M_PriceList WHERE IsActive = 'Y' AND AD_Client_ID = " + com.GetAD_Client_ID() + " AND AD_Org_ID = " + com.GetAD_Org_ID() + " AND IsDefault='Y' AND IsSOPriceList='N'"; int pricelist = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, Get_Trx())); if (pricelist == 0) { pricelist = MPriceList.GetDefault(GetCtx(), false).Get_ID(); } if (pricelist > 0) { invoice.SetM_PriceList_ID(pricelist); } } // invoice.setDocumentNo (comRun.getDocumentNo()); // may cause unique constraint invoice.SetSalesRep_ID(GetAD_User_ID()); // caller // if (com.GetC_Currency_ID() != invoice.GetC_Currency_ID()) { throw new ArgumentException("CommissionAPInvoice - Currency of PO Price List not Commission Currency"); } // if (!invoice.Save()) { //return GetReterivedError(invoice, "CommissionAPInvoice - cannot save Invoice"); throw new Exception("CommissionAPInvoice - cannot save Invoice"); } // Create Invoice Line MInvoiceLine iLine = new MInvoiceLine(invoice); iLine.SetC_Charge_ID(com.GetC_Charge_ID()); iLine.SetQty(1); iLine.SetPrice(comRun.GetGrandTotal()); iLine.SetTax(); if (!iLine.Save()) { //return GetReterivedError(iLine, "CommissionAPInvoice - cannot save Invoice Line"); throw new Exception("CommissionAPInvoice - cannot save Invoice Line"); } // return("@C_Invoice_ID@ = " + invoice.GetDocumentNo()); }