コード例 #1
0
        /// <summary>
        /// Create Line
        /// </summary>
        /// <param name="order">order</param>
        /// <param name="orderLine">line</param>
        /// <param name="qty">Quantity</param>
        /// <param name="storages">storage info</param>
        /// <param name="force">force delivery</param>
        private void CreateLine(MOrder order, MOrderLine orderLine, Decimal qty,
                                MStorage[] storages, bool force)
        {
            //	Complete last Shipment - can have multiple shipments
            if (_lastC_BPartner_Location_ID != orderLine.GetC_BPartner_Location_ID())
            {
                CompleteShipment();
            }
            _lastC_BPartner_Location_ID = orderLine.GetC_BPartner_Location_ID();
            //	Create New Shipment
            if (_shipment == null)
            {
                _shipment = new MInOut(order, 0, _movementDate);
                _shipment.SetM_Warehouse_ID(orderLine.GetM_Warehouse_ID());     //	sets Org too
                if (order.GetC_BPartner_ID() != orderLine.GetC_BPartner_ID())
                {
                    _shipment.SetC_BPartner_ID(orderLine.GetC_BPartner_ID());
                }
                if (order.GetC_BPartner_Location_ID() != orderLine.GetC_BPartner_Location_ID())
                {
                    _shipment.SetC_BPartner_Location_ID(orderLine.GetC_BPartner_Location_ID());
                }
                if (!_shipment.Save())
                {
                    throw new Exception("Could not create Shipment");
                }
            }
            //	Non Inventory Lines
            if (storages == null)
            {
                MInOutLine line = new MInOutLine(_shipment);
                line.SetOrderLine(orderLine, 0, Env.ZERO);
                line.SetQty(qty);       //	Correct UOM
                if (orderLine.GetQtyEntered().CompareTo(orderLine.GetQtyOrdered()) != 0)
                {
                    line.SetQtyEntered(Decimal.Round(Decimal.Divide(Decimal.Multiply(qty,
                                                                                     orderLine.GetQtyEntered()), orderLine.GetQtyOrdered()),
                                                     12, MidpointRounding.AwayFromZero));
                }
                line.SetLine(_line + orderLine.GetLine());
                //Amit 27-jan-2014
                if (Util.GetValueOfInt(DB.ExecuteScalar("SELECT COUNT(AD_MODULEINFO_ID) FROM AD_MODULEINFO WHERE PREFIX='DTD001_'")) > 0)
                {
                    line.SetDTD001_IsAttributeNo(true);
                }
                if (!line.Save())
                {
                    throw new Exception("Could not create Shipment Line");
                }
                log.Fine(line.ToString());
                return;
            }

            //	Product
            MProduct product    = orderLine.GetProduct();
            bool     linePerASI = false;

            if (product.GetM_AttributeSet_ID() != 0)
            {
                MAttributeSet mas = MAttributeSet.Get(GetCtx(), product.GetM_AttributeSet_ID());
                linePerASI = mas.IsInstanceAttribute();
            }

            //	Inventory Lines
            List <MInOutLine> list      = new List <MInOutLine>();
            Decimal           toDeliver = qty;

            for (int i = 0; i < storages.Length; i++)
            {
                MStorage storage = storages[i];
                Decimal  deliver = toDeliver;
                //	Not enough On Hand
                if (deliver.CompareTo(storage.GetQtyOnHand()) > 0 &&
                    Env.Signum(storage.GetQtyOnHand()) >= 0)            //	positive storage
                {
                    if (!force || //	Adjust to OnHand Qty
                        (i + 1 != storages.Length))     //	if force don't adjust last location
                    {
                        deliver = storage.GetQtyOnHand();
                    }
                }
                if (Env.Signum(deliver) == 0)   //	zero deliver
                {
                    continue;
                }
                int M_Locator_ID = storage.GetM_Locator_ID();
                //
                MInOutLine line = null;
                if (!linePerASI)        //	find line with Locator
                {
                    for (int n = 0; n < list.Count; n++)
                    {
                        MInOutLine test = (MInOutLine)list[n];
                        if (test.GetM_Locator_ID() == M_Locator_ID)
                        {
                            line = test;
                            break;
                        }
                    }
                }
                if (line == null)       //	new line
                {
                    line = new MInOutLine(_shipment);
                    line.SetOrderLine(orderLine, M_Locator_ID, order.IsSOTrx() ? deliver : Env.ZERO);
                    line.SetQty(deliver);
                    list.Add(line);
                }
                else
                {
                    //	existing line
                    line.SetQty(Decimal.Add(line.GetMovementQty(), deliver));
                }
                if (orderLine.GetQtyEntered().CompareTo(orderLine.GetQtyOrdered()) != 0)
                {
                    line.SetQtyEntered(Decimal.Round(Decimal.Divide(Decimal.Multiply(line.GetMovementQty(), orderLine.GetQtyEntered()),
                                                                    orderLine.GetQtyOrdered()), 12, MidpointRounding.AwayFromZero));
                }

                line.SetLine(_line + orderLine.GetLine());
                if (linePerASI)
                {
                    line.SetM_AttributeSetInstance_ID(storage.GetM_AttributeSetInstance_ID());
                }

                //Amit 27-jan-2014
                if (Util.GetValueOfInt(DB.ExecuteScalar("SELECT COUNT(AD_MODULEINFO_ID) FROM AD_MODULEINFO WHERE PREFIX='DTD001_'")) > 0)
                {
                    line.SetDTD001_IsAttributeNo(true);
                }

                if (!line.Save())
                {
                    throw new Exception("Could not create Shipment Line");
                }
                log.Fine("ToDeliver=" + qty + "/" + deliver + " - " + line);
                toDeliver = Decimal.Subtract(toDeliver, deliver);
                //	Temp adjustment
                storage.SetQtyOnHand(Decimal.Subtract(storage.GetQtyOnHand(), deliver));
                //
                if (Env.Signum(toDeliver) == 0)
                {
                    break;
                }
            }
            if (Env.Signum(toDeliver) != 0)
            {
                throw new Exception("Not All Delivered - Remainder=" + toDeliver);
            }
        }