/// <summary>
        /// New Order Line (different Product)
        /// </summary>
        /// <param name="rLine">request line</param>
        private void NewLine(MRequisitionLine rLine)
        {
            if (_orderLine != null)
            {
                if (!_orderLine.Save())
                {
                    throw new Exception("Cannot update Order Line");
                }
            }
            _orderLine = null;
            MProduct product = null;

            //	Get Business Partner
            int C_BPartner_ID = rLine.GetC_BPartner_ID();

            if (C_BPartner_ID != 0)
            {
                ;
            }
            else if (rLine.GetC_Charge_ID() != 0)
            {
                MCharge charge = MCharge.Get(GetCtx(), rLine.GetC_Charge_ID());
                C_BPartner_ID = charge.GetC_BPartner_ID();
                if (C_BPartner_ID == 0)
                {
                    throw new Exception("No Vendor for Charge " + charge.GetName());
                }
            }
            else
            {
                //	Find Vendor from Produt
                product = MProduct.Get(GetCtx(), rLine.GetM_Product_ID());
                MProductPO[] ppos = MProductPO.GetOfProduct(GetCtx(), product.GetM_Product_ID(), null);
                for (int i = 0; i < ppos.Length; i++)
                {
                    if (ppos[i].IsCurrentVendor() && ppos[i].GetC_BPartner_ID() != 0)
                    {
                        C_BPartner_ID = ppos[i].GetC_BPartner_ID();
                        break;
                    }
                }
                if (C_BPartner_ID == 0 && ppos.Length > 0)
                {
                    C_BPartner_ID = ppos[0].GetC_BPartner_ID();
                }
                if (C_BPartner_ID == 0)
                {
                    throw new Exception("No Vendor for " + product.GetName());
                }
            }

            //	New Order - Different Vendor
            if (_order == null ||
                _order.GetC_BPartner_ID() != C_BPartner_ID)
            {
                NewOrder(rLine, C_BPartner_ID);
            }

            //	No Order Line
            _orderLine = new MOrderLine(_order);
            if (product != null)
            {
                _orderLine.SetProduct(product);
                _orderLine.SetM_AttributeSetInstance_ID(rLine.GetM_AttributeSetInstance_ID());
            }
            else
            {
                _orderLine.SetC_Charge_ID(rLine.GetC_Charge_ID());
                _orderLine.SetPriceActual(rLine.GetPriceActual());
            }
            _orderLine.SetAD_Org_ID(rLine.GetAD_Org_ID());


            //	Prepare Save
            _m_M_Product_ID = rLine.GetM_Product_ID();
            _m_M_AttributeSetInstance_ID = rLine.GetM_AttributeSetInstance_ID();
            if (!_orderLine.Save())
            {
                throw new Exception("Cannot save Order Line");
            }
        }
Beispiel #2
0
        /**
         *  Create PO from Planned Amt/Qty
         *  @param projectLine project line
         */
        private void CreatePO(MProject project, MProjectLine projectLine)
        {
            if (projectLine.GetM_Product_ID() == 0)
            {
                AddLog(projectLine.GetLine(), null, null, "Line has no Product");
                return;
            }
            if (projectLine.GetC_OrderPO_ID() != 0)
            {
                AddLog(projectLine.GetLine(), null, null, "Line was ordered previously");
                return;
            }

            //	PO Record
            MProductPO[] pos = MProductPO.GetOfProduct(GetCtx(), projectLine.GetM_Product_ID(), Get_TrxName());
            if (pos == null || pos.Length == 0)
            {
                AddLog(projectLine.GetLine(), null, null, "Product has no PO record");
                return;
            }

            //	Create to Order
            MOrder order = null;

            //	try to find PO to C_BPartner
            for (int i = 0; i < m_pos.Count; i++)
            {
                MOrder test = m_pos[i];
                if (test.GetC_BPartner_ID() == pos[0].GetC_BPartner_ID())
                {
                    order = test;
                    break;
                }
            }
            if (order == null)  //	create new Order
            {
                //	Vendor
                MBPartner bp = new MBPartner(GetCtx(), pos[0].GetC_BPartner_ID(), Get_TrxName());
                //	New Order
                order = new MOrder(project, false, null);
                int AD_Org_ID = projectLine.GetAD_Org_ID();
                if (AD_Org_ID == 0)
                {
                    log.Warning("createPOfromProjectLine - AD_Org_ID=0");
                    AD_Org_ID = GetCtx().GetAD_Org_ID();
                    if (AD_Org_ID != 0)
                    {
                        projectLine.SetAD_Org_ID(AD_Org_ID);
                    }
                }
                order.SetClientOrg(projectLine.GetAD_Client_ID(), AD_Org_ID);
                order.SetBPartner(bp);
                order.SetC_Project_ID(project.Get_ID());
                order.Save();
                //	optionally save for consolidation
                if (m_ConsolidateDocument)
                {
                    m_pos.Add(order);
                }
            }

            //	Create Line
            MOrderLine orderLine = new MOrderLine(order);

            orderLine.SetM_Product_ID(projectLine.GetM_Product_ID(), true);
            orderLine.SetQty(projectLine.GetPlannedQty());
            orderLine.SetDescription(projectLine.GetDescription());

            //	(Vendor) PriceList Price
            orderLine.SetPrice();
            if (Env.Signum(orderLine.GetPriceActual()) == 0)
            {
                //	Try to find purchase price
                Decimal poPrice       = pos[0].GetPricePO();
                int     C_Currency_ID = pos[0].GetC_Currency_ID();
                //
                if (Env.Signum(poPrice) == 0)
                {
                    poPrice = pos[0].GetPriceLastPO();
                }
                if (Env.Signum(poPrice) == 0)
                {
                    poPrice = pos[0].GetPriceList();
                }
                //	We have a price
                if (Env.Signum(poPrice) != 0)
                {
                    if (order.GetC_Currency_ID() != C_Currency_ID)
                    {
                        poPrice = VAdvantage.Model.MConversionRate.Convert(GetCtx(), poPrice,
                                                                           C_Currency_ID, order.GetC_Currency_ID(),
                                                                           order.GetDateAcct(), order.GetC_ConversionType_ID(),
                                                                           order.GetAD_Client_ID(), order.GetAD_Org_ID());
                    }
                    orderLine.SetPrice(poPrice);
                }
            }

            orderLine.SetTax();
            orderLine.Save();

            // touch order to recalculate tax and totals
            order.SetIsActive(order.IsActive());
            order.Save();

            //	update ProjectLine
            projectLine.SetC_OrderPO_ID(order.GetC_Order_ID());
            projectLine.Save();
            AddLog(projectLine.GetLine(), null, projectLine.GetPlannedQty(), order.GetDocumentNo());
        }