示例#1
0
        /**
         *  Generate Shipments
         *  @param pstmt order query
         *	@return info
         */
        private String Generate(IDataReader idr)
        {
            DataTable dt = new DataTable();

            try
            {
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)//  while (dr.next ())
                {
                    MOrder order = new MOrder(GetCtx(), dr, Get_TrxName());

                    // Credit Limit check added by Vivek on 24/08/2016
                    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());
                                }
                            }
                        }
                    }
                    else
                    {
                        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
                    if (!_ConsolidateDocument ||
                        (_invoice != null &&
                         (_invoice.GetC_BPartner_Location_ID() != order.GetBill_Location_ID() ||
                          _invoice.GetC_PaymentTerm_ID() != order.GetC_PaymentTerm_ID())))
                    {
                        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;
                            }
                            //
                            CreateLine(order, shipment, shipLine);
                        }       //	shipment lines
                        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);
                                }
                                //
                                if (oLine.IsContract() == false)
                                {
                                    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;

                            //	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
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "", e);
            }
            finally
            {
                if (idr != null)
                {
                    idr.Close();
                }

                dt = null;
            }

            CompleteInvoice();
            return("@Created@ = " + _created);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <summary>
        /// Generate Shipments
        /// </summary>
        /// <param name="idr">Order Query</param>
        /// <returns>info</returns>
        private String Generate(IDataReader idr)
        {
            DataTable dt     = new DataTable();
            MClient   client = MClient.Get(GetCtx());

            try
            {
                dt.Load(idr);
                idr.Close();
                //ResultSet dr = pstmt.executeQuery();
                foreach (DataRow dr in dt.Rows)//  while (dr.next ())		//	Order
                {
                    order = new MOrder(GetCtx(), dr, Get_TrxName());

                    // Credit Limit check added by Vivek on 24/08/2016
                    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 == "B" || creditVal == "D" || creditVal == "E" || creditVal == "F")
                                {
                                    AddLog(Msg.GetMsg(GetCtx(), "StopShipment") + bp.GetName());
                                    continue;
                                }
                                else if (creditVal == "H" || creditVal == "J" || creditVal == "K" || creditVal == "L")
                                {
                                    if (_msg != null)
                                    {
                                        _msg.Clear();
                                    }
                                    _msg.Append(Msg.GetMsg(GetCtx(), "WarningShipment") + bp.GetName());
                                    //AddLog(Msg.GetMsg(GetCtx(), "WarningShipment") + bp.GetName());
                                }
                            }
                        }
                    }
                    else
                    {
                        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 == "B" || creditVal == "D" || creditVal == "E" || creditVal == "F")
                                    {
                                        AddLog(Msg.GetMsg(GetCtx(), "StopShipment") + bp.GetName() + " " + bpl.GetName());
                                        continue;
                                    }
                                    else if (creditVal == "H" || creditVal == "J" || creditVal == "K" || creditVal == "L")
                                    {
                                        if (_msg != null)
                                        {
                                            _msg.Clear();
                                        }
                                        _msg.Append(Msg.GetMsg(GetCtx(), "WarningShipment") + bp.GetName() + " " + bpl.GetName());
                                        //AddLog(Msg.GetMsg(GetCtx(), "WarningShipment") + bp.GetName() + " " + bpl.GetName());
                                    }
                                }
                            }
                        }
                    }
                    // Credit Limit End

                    //	New Header different Shipper, Shipment Location
                    if (!_consolidateDocument ||
                        (_shipment != null &&
                         (_shipment.GetC_BPartner_Location_ID() != order.GetC_BPartner_Location_ID() ||
                          _shipment.GetM_Shipper_ID() != order.GetM_Shipper_ID())))
                    {
                        CompleteShipment();
                    }
                    log.Fine("check: " + order + " - DeliveryRule=" + order.GetDeliveryRule());
                    //
                    DateTime?minGuaranteeDate = _movementDate;
                    bool     completeOrder    = MOrder.DELIVERYRULE_CompleteOrder.Equals(order.GetDeliveryRule());
                    //	OrderLine WHERE
                    String where = " AND M_Warehouse_ID=" + _M_Warehouse_ID;
                    if (_datePromised != null)
                    {
                        where += " AND (TRUNC(DatePromised,'DD')<=" + DB.TO_DATE((DateTime?)_datePromised, true)
                                 + " OR DatePromised IS NULL)";
                        //where += " AND (TRUNC(DatePromised,'DD')<='" + String.Format("{0:dd-MMM-yy}", _datePromised)
                        //    + "' OR DatePromised IS NULL)";
                    }
                    //	Exclude Auto Delivery if not Force
                    if (!MOrder.DELIVERYRULE_Force.Equals(order.GetDeliveryRule()))
                    {
                        where += " AND (C_OrderLine.M_Product_ID IS NULL"
                                 + " OR EXISTS (SELECT * FROM M_Product p "
                                 + "WHERE C_OrderLine.M_Product_ID=p.M_Product_ID"
                                 + " AND IsExcludeAutoDelivery='N'))";
                    }
                    //	Exclude Unconfirmed
                    if (!_isUnconfirmedInOut)
                    {
                        where += " AND NOT EXISTS (SELECT * FROM M_InOutLine iol"
                                 + " INNER JOIN M_InOut io ON (iol.M_InOut_ID=io.M_InOut_ID) "
                                 + "WHERE iol.C_OrderLine_ID=C_OrderLine.C_OrderLine_ID AND io.DocStatus IN ('IP','WC'))";
                    }
                    //	Deadlock Prevention - Order by M_Product_ID
                    MOrderLine[] lines = order.GetLines(where, "ORDER BY C_BPartner_Location_ID, M_Product_ID");
                    for (int i = 0; i < lines.Length; i++)
                    {
                        MOrderLine line = lines[i];
                        // if order line is not drop ship type
                        if (!line.IsDropShip())
                        {
                            if (line.GetM_Warehouse_ID() != _M_Warehouse_ID)
                            {
                                continue;
                            }
                            log.Fine("check: " + line);
                            Decimal onHand    = Env.ZERO;
                            Decimal toDeliver = Decimal.Subtract(line.GetQtyOrdered(),
                                                                 line.GetQtyDelivered());
                            Decimal QtyNotDelivered = Util.GetValueOfDecimal(DB.ExecuteScalar(@"SELECT SUM(MovementQty) FROM M_Inout i INNER JOIN M_InoutLine il ON i.M_Inout_ID = il.M_Inout_ID
                            WHERE il.C_OrderLine_ID = " + line.GetC_OrderLine_ID() + @" AND il.Isactive = 'Y' AND i.docstatus NOT IN ('RE' , 'VO' , 'CL' , 'CO')", null, Get_Trx()));
                            toDeliver -= QtyNotDelivered;
                            MProduct product = line.GetProduct();
                            //	Nothing to Deliver
                            if (product != null && Env.Signum(toDeliver) == 0)
                            {
                                continue;
                            }

                            // Added by Bharat on 07 April 2017 as code already on Admpiere but deleted here.
                            if (line.GetC_Charge_ID() != 0)
                            {
                                continue;
                            }

                            //	Check / adjust for confirmations
                            Decimal unconfirmedShippedQty = Env.ZERO;
                            if (_isUnconfirmedInOut && product != null && Env.Signum(toDeliver) != 0)
                            {
                                String       where2 = "EXISTS (SELECT * FROM M_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('IP','WC'))";
                                MInOutLine[] iols   = MInOutLine.GetOfOrderLine(GetCtx(),
                                                                                line.GetC_OrderLine_ID(), where2, null);
                                for (int j = 0; j < iols.Length; j++)
                                {
                                    unconfirmedShippedQty = Decimal.Add(unconfirmedShippedQty, iols[j].GetMovementQty());
                                }
                                String logInfo = "Unconfirmed Qty=" + unconfirmedShippedQty
                                                 + " - ToDeliver=" + toDeliver + "->";
                                toDeliver = Decimal.Subtract(toDeliver, unconfirmedShippedQty);
                                logInfo  += toDeliver;
                                if (Env.Signum(toDeliver) < 0)
                                {
                                    toDeliver = Env.ZERO;
                                    logInfo  += " (set to 0)";
                                }
                                //	Adjust On Hand
                                onHand = Decimal.Subtract(onHand, unconfirmedShippedQty);
                                log.Fine(logInfo);
                            }

                            //	Comments & lines w/o product & services
                            if ((product == null || !product.IsStocked()) &&
                                (Env.Signum(line.GetQtyOrdered()) == 0 ||                               //	comments
                                 Env.Signum(toDeliver) != 0))                                           //	lines w/o product
                            {
                                if (!MOrder.DELIVERYRULE_CompleteOrder.Equals(order.GetDeliveryRule())) //	printed later
                                {
                                    CreateLine(order, line, toDeliver, null, false);
                                }
                                continue;
                            }

                            //	Stored Product
                            MProductCategory pc       = MProductCategory.Get(order.GetCtx(), product.GetM_Product_Category_ID());
                            String           MMPolicy = pc.GetMMPolicy();
                            if (MMPolicy == null || MMPolicy.Length == 0)
                            {
                                MMPolicy = client.GetMMPolicy();
                            }
                            //
                            MStorage[] storages = GetStorages(line.GetM_Warehouse_ID(),
                                                              line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(),
                                                              product.GetM_AttributeSet_ID(),
                                                              line.GetM_AttributeSetInstance_ID() == 0,
                                                              (DateTime?)minGuaranteeDate,
                                                              MClient.MMPOLICY_FiFo.Equals(MMPolicy));

                            for (int j = 0; j < storages.Length; j++)
                            {
                                MStorage storage = storages[j];
                                onHand = Decimal.Add(onHand, storage.GetQtyOnHand());
                            }
                            bool fullLine = onHand.CompareTo(toDeliver) >= 0 ||
                                            Env.Signum(toDeliver) < 0;

                            //	Complete Order
                            if (completeOrder && !fullLine)
                            {
                                log.Fine("Failed CompleteOrder - OnHand=" + onHand
                                         + " (Unconfirmed=" + unconfirmedShippedQty
                                         + "), ToDeliver=" + toDeliver + " - " + line);
                                completeOrder = false;
                                break;
                            }
                            //	Complete Line
                            else if (fullLine && MOrder.DELIVERYRULE_CompleteLine.Equals(order.GetDeliveryRule()))
                            {
                                log.Fine("CompleteLine - OnHand=" + onHand
                                         + " (Unconfirmed=" + unconfirmedShippedQty
                                         + ", ToDeliver=" + toDeliver + " - " + line);
                                //
                                CreateLine(order, line, toDeliver, storages, false);
                            }
                            //	Availability
                            else if (MOrder.DELIVERYRULE_Availability.Equals(order.GetDeliveryRule()) &&
                                     (Env.Signum(onHand) > 0 ||
                                      Env.Signum(toDeliver) < 0))
                            {
                                Decimal deliver = toDeliver;
                                if (deliver.CompareTo(onHand) > 0)
                                {
                                    deliver = onHand;
                                }
                                log.Fine("Available - OnHand=" + onHand
                                         + " (Unconfirmed=" + unconfirmedShippedQty
                                         + "), ToDeliver=" + toDeliver
                                         + ", Delivering=" + deliver + " - " + line);
                                //
                                CreateLine(order, line, deliver, storages, false);
                            }
                            //	Force
                            else if (MOrder.DELIVERYRULE_Force.Equals(order.GetDeliveryRule()))
                            {
                                Decimal deliver = toDeliver;
                                log.Fine("Force - OnHand=" + onHand
                                         + " (Unconfirmed=" + unconfirmedShippedQty
                                         + "), ToDeliver=" + toDeliver
                                         + ", Delivering=" + deliver + " - " + line);
                                //
                                CreateLine(order, line, deliver, storages, true);
                            }
                            //	Manual
                            else if (MOrder.DELIVERYRULE_Manual.Equals(order.GetDeliveryRule()))
                            {
                                log.Fine("Manual - OnHand=" + onHand
                                         + " (Unconfirmed=" + unconfirmedShippedQty
                                         + ") - " + line);
                            }
                            else
                            {
                                log.Fine("Failed: " + order.GetDeliveryRule() + " - OnHand=" + onHand
                                         + " (Unconfirmed=" + unconfirmedShippedQty
                                         + "), ToDeliver=" + toDeliver + " - " + line);
                            }
                        }
                    }//	for all order lines

                    //	Complete Order successful
                    if (completeOrder && MOrder.DELIVERYRULE_CompleteOrder.Equals(order.GetDeliveryRule()))
                    {
                        for (int i = 0; i < lines.Length; i++)
                        {
                            MOrderLine line = lines[i];
                            // if order line is not drop ship type
                            if (!line.IsDropShip())
                            {
                                if (line.GetM_Warehouse_ID() != _M_Warehouse_ID)
                                {
                                    continue;
                                }
                                MProduct product   = line.GetProduct();
                                Decimal  toDeliver = Decimal.Subtract(line.GetQtyOrdered(), line.GetQtyDelivered());
                                //
                                MStorage[] storages = null;
                                if (product != null && product.IsStocked())
                                {
                                    MProductCategory pc       = MProductCategory.Get(order.GetCtx(), product.GetM_Product_Category_ID());
                                    String           MMPolicy = pc.GetMMPolicy();
                                    if (MMPolicy == null || MMPolicy.Length == 0)
                                    {
                                        MMPolicy = client.GetMMPolicy();
                                    }
                                    //
                                    storages = GetStorages(line.GetM_Warehouse_ID(),
                                                           line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(),
                                                           product.GetM_AttributeSet_ID(),
                                                           line.GetM_AttributeSetInstance_ID() == 0, (DateTime?)minGuaranteeDate,
                                                           MClient.MMPOLICY_FiFo.Equals(MMPolicy));
                                }
                                //
                                CreateLine(order, line, toDeliver, storages, false);
                            }
                        }
                    }
                    _line += 1000;
                }       //	while order
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, _sql, e);
            }
            finally
            {
                if (idr != null)
                {
                    idr.Close();
                }
                dt = null;
            }
            CompleteShipment();
            return("@Created@ = " + _created);
        }