/** * 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()); }
/// <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"); } }
/// <summary> /// Prepare Calculations /// </summary> /// <returns>error message</returns> private String CheckPrerequisites() { String clientWhere = " AND AD_Client_ID=" + _plv.GetAD_Client_ID(); // PO Prices must exists int no = DataBase.DB.ExecuteQuery("UPDATE M_Product_PO SET PriceList = 0 WHERE PriceList IS NULL" + clientWhere, null, Get_TrxName()); no = DataBase.DB.ExecuteQuery("UPDATE M_Product_PO SET PriceLastPO = 0 WHERE PriceLastPO IS NULL" + clientWhere, null, Get_TrxName()); no = DataBase.DB.ExecuteQuery("UPDATE M_Product_PO SET PricePO = PriceLastPO " + "WHERE (PricePO IS NULL OR PricePO = 0) AND PriceLastPO <> 0" + clientWhere, null, Get_TrxName()); no = DataBase.DB.ExecuteQuery( "UPDATE M_Product_PO SET PricePO = 0 WHERE PricePO IS NULL" + clientWhere, null, Get_TrxName()); // Set default current vendor no = DataBase.DB.ExecuteQuery( "UPDATE M_Product_PO p SET IsCurrentVendor = 'Y' " + "WHERE IsCurrentVendor = 'N'" + " AND NOT EXISTS " + "(SELECT pp.M_Product_ID FROM M_Product_PO pp " + "WHERE pp.M_Product_ID=p.M_Product_ID " + "GROUP BY pp.M_Product_ID HAVING COUNT(*) > 1)" + clientWhere, null, Get_TrxName()); /** * Make sure that we have only one active product vendor */ String sql = "SELECT * FROM M_Product_PO po " + "WHERE IsCurrentVendor='Y' AND IsActive='Y'" + clientWhere + " AND EXISTS (SELECT M_Product_ID FROM M_Product_PO x " + "WHERE x.M_Product_ID=po.M_Product_ID" + " AND IsCurrentVendor='Y' AND IsActive='Y' " + "GROUP BY M_Product_ID HAVING COUNT(*) > 1) " + "ORDER BY M_Product_ID, Created"; int success = 0; int errors = 0; DataTable dt = null; IDataReader idr = null; try { idr = DataBase.DB.ExecuteReader(sql, null, Get_TrxName()); dt = new DataTable(); dt.Load(idr); idr.Close(); int M_Product_ID = 0; foreach (DataRow dr in dt.Rows) { MProductPO po = new MProductPO(GetCtx(), dr, Get_TrxName()); if (M_Product_ID != po.GetM_Product_ID()) { M_Product_ID = po.GetM_Product_ID(); continue; } po.SetIsCurrentVendor(false); if (po.Save()) { success++; } else { errors++; log.Warning("Not updated " + po); } } } catch (Exception e) { if (idr != null) { idr.Close(); } log.Log(Level.SEVERE, sql, e); } finally { dt = null; if (idr != null) { idr.Close(); } } log.Info("Current Vendor - Changes=" + success + ", Errors=" + errors); return(null); }