/// <summary> /// Create PO From SO /// </summary> /// <param name="so">sales order</param> /// <returns>number of POs created</returns> private int CreatePOFromSO(MOrder so) { StringBuilder sql = new StringBuilder(); StringBuilder sqlErrorMessage = new StringBuilder(); sqlErrorMessage.Clear(); string _Dropship = ""; log.Info(so.ToString()); MOrderLine[] soLines = so.GetLines(true, null); if (soLines == null || soLines.Length == 0) { log.Warning("No Lines - " + so); return(0); } // int counter = 0; // Order Lines with a Product which has a current vendor sql.Append(@"SELECT DISTINCT po.C_BPartner_ID, po.M_Product_ID ,ol.Isdropship, po.PriceList , po.PricePO , po.C_Currency_ID FROM M_Product_PO po INNER JOIN M_Product prd ON po.M_Product_ID=prd.M_Product_ID INNER JOIN C_OrderLine ol ON (po.M_Product_ID=ol.M_Product_ID "); // changes done by bharat on 26 June 2018 If purchased Checkbox is false on Finished Good Product, System should not generate Purchase Order. sqlErrorMessage.Append(@"SELECT DISTINCT po.C_BPartner_ID, bp.name AS BPName, ol.M_Product_ID , p.Name, ol.Isdropship, po.C_Currency_ID, bp.PO_PaymentTerm_ID, bp.PO_PriceList_ID FROM C_OrderLine ol INNER JOIN m_product p ON (p.M_Product_ID =ol.M_Product_ID) LEFT JOIN M_Product_PO po ON (po.M_Product_ID=ol.M_Product_ID AND po.isactive = 'Y' AND po.IsCurrentVendor = 'Y' ) LEFT JOIN c_bpartner bp ON (bp.c_bpartner_id = po.c_bpartner_id "); // Added by Vivek on 20/09/2017 Assigned By Pradeep for drop shipment // if drop ship parameter is true then get all records true drop ship lines if (_IsDropShip == "Y") { sql.Append(@"AND Ol.Isdropship='Y' "); sqlErrorMessage.Append(@"AND Ol.Isdropship='Y' "); } // if drop ship parameter is false then get all records false drop ship lines else if (_IsDropShip == "N") { sql.Append(@"AND Ol.Isdropship='N' "); sqlErrorMessage.Append(@"AND Ol.Isdropship='N' "); } // changes don eby Bharat on 26 June 2018 to handle If purchased Checkbox is false on Finished Good Product, System should not generate Purchase Order. sql.Append(@") WHERE ol.C_Order_ID=" + so.GetC_Order_ID() + @" AND po.IsCurrentVendor='Y' AND prd.IsPurchased='Y'"); sqlErrorMessage.Append(@") WHERE ol.C_Order_ID=" + so.GetC_Order_ID()); if (_Vendor_ID > 0) { sql.Append(@" AND po.C_BPartner_ID = " + _Vendor_ID); sqlErrorMessage.Append(@" AND po.C_BPartner_ID = " + _Vendor_ID); } sql.Append(@" ORDER BY po.c_bpartner_id,ol.Isdropship "); sqlErrorMessage.Append(@" ORDER BY po.c_bpartner_id,ol.Isdropship "); // get error or setting message GetErrorOrSetting(sqlErrorMessage.ToString(), Get_TrxName()); IDataReader idr = null; MOrder po = null; ConsolidatePO consolidatePO = null; //ConsolidatePOLine consolidatePOLine = null; try { idr = DataBase.DB.ExecuteReader(sql.ToString(), null, Get_TrxName()); while (idr.Read()) { // New Order int C_BPartner_ID = Utility.Util.GetValueOfInt(idr[0]);//.getInt(1); // Code Commented by Vivek Kumar on 20/09/2017 Assigned By Pradeep for drop shipment //if (po == null || po.GetBill_BPartner_ID() != C_BPartner_ID) //{ // po = CreatePOForVendor(Utility.Util.GetValueOfInt(idr[0]), so); // AddLog(0, null, null, po.GetDocumentNo()); // counter++; // // check ANY PO created with same Business Partnet and Drop Shipment if (_IsConsolidatedPO && listConsolidatePO.Count > 0) { ConsolidatePO poRecord; if (listConsolidatePO.Exists(x => (x.C_BPartner_ID == C_BPartner_ID) && (x.IsDropShip == Utility.Util.GetValueOfString(idr[2])))) { poRecord = listConsolidatePO.Find(x => (x.C_BPartner_ID == C_BPartner_ID) && (x.IsDropShip == Utility.Util.GetValueOfString(idr[2]))); if (poRecord != null) { po = new MOrder(GetCtx(), poRecord.C_Order_ID, Get_Trx()); _Dropship = po.IsDropShip() ? "Y" : "N"; } } } // Drop Shipment fucntionality added by Vivek on 20/09/2017 Assigned By Pradeep if (po == null || po.GetBill_BPartner_ID() != C_BPartner_ID || _Dropship != Utility.Util.GetValueOfString(idr[2])) { po = CreatePOForVendor(Utility.Util.GetValueOfInt(idr[0]), so, Utility.Util.GetValueOfString(idr[2])); if (po == null) { return(counter); } // AddLog(0, null, null, po.GetDocumentNo()); counter++; // maintain list if (po != null && po.GetC_Order_ID() > 0) { consolidatePO = new ConsolidatePO(); consolidatePO.C_Order_ID = po.GetC_Order_ID(); consolidatePO.C_BPartner_ID = C_BPartner_ID; consolidatePO.IsDropShip = Utility.Util.GetValueOfString(idr[2]); listConsolidatePO.Add(consolidatePO); } } _Dropship = Utility.Util.GetValueOfString(idr[2]); // Line int M_Product_ID = Utility.Util.GetValueOfInt(idr[1]);//.getInt(2); for (int i = 0; i < soLines.Length; i++) { // When Drop ship parameter is yes but SO line does not contains any drop shipment product if (_IsDropShip == "Y" && Util.GetValueOfBool(soLines[i].IsDropShip()) == false) { continue; } // When Drop ship parameter is NO but SO line contains drop shipment product then it also does not generate any else if (_IsDropShip == "N" && Util.GetValueOfBool(soLines[i].IsDropShip()) == true) { continue; } //When Drop ship parameter is yes and SO line also contains drop shipment product else { String _Drop = "N"; if (Util.GetValueOfBool(soLines[i].IsDropShip())) { _Drop = "Y"; } if (soLines[i].GetM_Product_ID() == M_Product_ID && _Drop == _Dropship) { MOrderLine poLine = new MOrderLine(po); poLine.SetRef_OrderLine_ID(soLines[i].GetC_OrderLine_ID()); poLine.SetM_Product_ID(soLines[i].GetM_Product_ID()); poLine.SetM_AttributeSetInstance_ID(soLines[i].GetM_AttributeSetInstance_ID()); poLine.SetC_UOM_ID(soLines[i].GetC_UOM_ID()); poLine.SetQtyEntered(soLines[i].GetQtyEntered()); poLine.SetQtyOrdered(soLines[i].GetQtyOrdered()); poLine.SetDescription(soLines[i].GetDescription()); poLine.SetDatePromised(soLines[i].GetDatePromised()); poLine.SetIsDropShip(soLines[i].IsDropShip()); poLine.SetPrice(); // Set value in Property From Process to check on Before Save. poLine.SetFromProcess(true); if (!poLine.Save()) { ValueNamePair pp = VLogger.RetrieveError(); log.Info("CreatePOfromSO : Not Saved. Error Value : " + pp.GetValue() + " , Error Name : " + pp.GetName()); } //else //{ // if (poLine != null && poLine.GetC_OrderLine_ID() > 0) // { // consolidatePOLine = new ConsolidatePOLine(); // consolidatePOLine.C_Order_ID = poLine.GetC_Order_ID(); // consolidatePOLine.C_OrderLine_ID = poLine.GetC_OrderLine_ID(); // consolidatePOLine.M_Product_ID = poLine.GetM_Product_ID(); // consolidatePOLine.M_AttributeSetInstance_ID = poLine.GetM_AttributeSetInstance_ID(); // consolidatePOLine.C_UOM_ID = poLine.GetC_UOM_ID(); // consolidatePOLine.IsDropShip = soLines[i].IsDropShip() ? "Y" : "N"; // listConsolidatePOLine.Add(consolidatePOLine); // } //} } } } } idr.Close(); } catch (Exception e) { if (idr != null) { idr.Close(); } log.Log(Level.SEVERE, sql.ToString(), e); } // Set Reference to PO if (po != null) { so.SetRef_Order_ID(po.GetC_Order_ID()); so.Save(); } return(counter); }
/// <summary> /// Create PO From SO /// </summary> /// <param name="so">sales order</param> /// <returns>number of POs created basesd on vendor selected or current vendor under product(purchasing tab)</returns> private int CreatePOFromSO(MOrder so) { StringBuilder sql = new StringBuilder(); StringBuilder sqlErrorMessage = new StringBuilder(); sqlErrorMessage.Clear(); string _Dropship = ""; log.Info(so.ToString()); MOrderLine[] soLines = so.GetLines(true, null); if (soLines == null || soLines.Length == 0) { log.Warning("No Lines - " + so); return(0); } // int counter = 0; // Changes Done by Rakesh kumar 03/Mar/2021 to fetch record based on vendor selected instead of current vendor if (_C_RefVendor_ID > 0) { // Get Order Lines with a Product sql.Append(@"SELECT DISTINCT OL.M_PRODUCT_ID ,OL.ISDROPSHIP FROM C_ORDERLINE OL INNER JOIN M_PRODUCT PRD ON (PRD.M_PRODUCT_ID=OL.M_PRODUCT_ID "); // Added by Vivek on 20/09/2017 Assigned By Pradeep for drop shipment // if drop ship parameter is true then get all records true drop ship lines if (_IsDropShip == "Y") { sql.Append(@"AND Ol.Isdropship='Y' "); } // if drop ship parameter is false then get all records false drop ship lines else if (_IsDropShip == "N") { sql.Append(@"AND Ol.Isdropship='N' "); } // changes don eby Bharat on 26 June 2018 to handle If purchased Checkbox is false on Finished Good Product, System should not generate Purchase Order. sql.Append(@") WHERE ol.C_Order_ID=" + so.GetC_Order_ID() + @" AND prd.IsPurchased='Y'"); sql.Append(@" ORDER BY ol.Isdropship "); } else { // Order Lines with a Product which has a current vendor sql.Append(@"SELECT DISTINCT po.C_BPartner_ID, po.M_Product_ID ,ol.Isdropship, po.PriceList , po.PricePO , po.C_Currency_ID FROM M_Product_PO po INNER JOIN M_Product prd ON po.M_Product_ID=prd.M_Product_ID INNER JOIN C_OrderLine ol ON (po.M_Product_ID=ol.M_Product_ID "); // changes done by bharat on 26 June 2018 If purchased Checkbox is false on Finished Good Product, System should not generate Purchase Order. sqlErrorMessage.Append(@"SELECT DISTINCT po.C_BPartner_ID, bp.name AS BPName, ol.M_Product_ID , p.Name, ol.Isdropship, po.C_Currency_ID, bp.PO_PaymentTerm_ID, bp.PO_PriceList_ID FROM C_OrderLine ol INNER JOIN m_product p ON (p.M_Product_ID =ol.M_Product_ID) LEFT JOIN M_Product_PO po ON (po.M_Product_ID=ol.M_Product_ID AND po.isactive = 'Y' AND po.IsCurrentVendor = 'Y' ) LEFT JOIN c_bpartner bp ON (bp.c_bpartner_id = po.c_bpartner_id "); // Added by Vivek on 20/09/2017 Assigned By Pradeep for drop shipment // if drop ship parameter is true then get all records true drop ship lines if (_IsDropShip == "Y") { sql.Append(@"AND Ol.Isdropship='Y' "); sqlErrorMessage.Append(@"AND Ol.Isdropship='Y' "); } // if drop ship parameter is false then get all records false drop ship lines else if (_IsDropShip == "N") { sql.Append(@"AND Ol.Isdropship='N' "); sqlErrorMessage.Append(@"AND Ol.Isdropship='N' "); } // changes don eby Bharat on 26 June 2018 to handle If purchased Checkbox is false on Finished Good Product, System should not generate Purchase Order. sql.Append(@") WHERE ol.C_Order_ID=" + so.GetC_Order_ID() + @" AND po.IsCurrentVendor='Y' AND prd.IsPurchased='Y'"); sqlErrorMessage.Append(@") WHERE ol.C_Order_ID=" + so.GetC_Order_ID()); if (_Vendor_ID > 0) { sql.Append(@" AND po.C_BPartner_ID = " + _Vendor_ID); sqlErrorMessage.Append(@" AND po.C_BPartner_ID = " + _Vendor_ID); } sql.Append(@" ORDER BY po.c_bpartner_id,ol.Isdropship "); sqlErrorMessage.Append(@" ORDER BY po.c_bpartner_id,ol.Isdropship "); // get error or setting message GetErrorOrSetting(sqlErrorMessage.ToString(), Get_TrxName()); } IDataReader idr = null; DataTable dt = null; MOrder po = null; ConsolidatePO consolidatePO = null; //ConsolidatePOLine consolidatePOLine = null; try { idr = DataBase.DB.ExecuteReader(sql.ToString(), null, Get_TrxName()); dt = new DataTable(); dt.Load(idr); idr.Close(); int C_BPartner_ID = _C_RefVendor_ID; foreach (DataRow dr in dt.Rows) { // Changes Done by Rakesh kumar 03/Mar/2021 // Assign BPartnerId (VendorId) if only current vendor selected if (_C_RefVendor_ID == 0) { C_BPartner_ID = Utility.Util.GetValueOfInt(dr["C_BPartner_ID"]); } // check ANY PO created with same Business Partnet and Drop Shipment if (_IsConsolidatedPO && listConsolidatePO.Count > 0) { ConsolidatePO poRecord; if (listConsolidatePO.Exists(x => (x.C_BPartner_ID == C_BPartner_ID) && (x.IsDropShip == Utility.Util.GetValueOfString(dr["ISDROPSHIP"])))) { poRecord = listConsolidatePO.Find(x => (x.C_BPartner_ID == C_BPartner_ID) && (x.IsDropShip == Utility.Util.GetValueOfString(dr["ISDROPSHIP"]))); if (poRecord != null) { po = new MOrder(GetCtx(), poRecord.C_Order_ID, Get_Trx()); _Dropship = po.IsDropShip() ? "Y" : "N"; } } } // Drop Shipment fucntionality added by Vivek on 20/09/2017 Assigned By Pradeep if (po == null || po.GetBill_BPartner_ID() != C_BPartner_ID || _Dropship != Utility.Util.GetValueOfString(dr["ISDROPSHIP"])) { // Create PO Header po = CreatePOForVendor(C_BPartner_ID, so, Utility.Util.GetValueOfString(dr["ISDROPSHIP"])); if (po == null) { return(counter); } // AddLog(0, null, null, po.GetDocumentNo()); counter++; // maintain list if (po != null && po.GetC_Order_ID() > 0) { consolidatePO = new ConsolidatePO(); consolidatePO.C_Order_ID = po.GetC_Order_ID(); consolidatePO.C_BPartner_ID = C_BPartner_ID; consolidatePO.IsDropShip = Utility.Util.GetValueOfString(dr["ISDROPSHIP"]); listConsolidatePO.Add(consolidatePO); } } _Dropship = Utility.Util.GetValueOfString(dr["ISDROPSHIP"]); // int M_Product_ID = Utility.Util.GetValueOfInt(dr["M_PRODUCT_ID"]); // Create PO Line for (int i = 0; i < soLines.Length; i++) { // When Drop ship parameter is yes but SO line does not contains any drop shipment product if (_IsDropShip == "Y" && Util.GetValueOfBool(soLines[i].IsDropShip()) == false) { continue; } // When Drop ship parameter is NO but SO line contains drop shipment product then it also does not generate any else if (_IsDropShip == "N" && Util.GetValueOfBool(soLines[i].IsDropShip()) == true) { continue; } //When Drop ship parameter is yes and SO line also contains drop shipment product else { String _Drop = "N"; if (Util.GetValueOfBool(soLines[i].IsDropShip())) { _Drop = "Y"; } if (soLines[i].GetM_Product_ID() == Utility.Util.GetValueOfInt(dr["M_PRODUCT_ID"]) && _Drop == _Dropship) { MOrderLine poLine = new MOrderLine(po); poLine.SetRef_OrderLine_ID(soLines[i].GetC_OrderLine_ID()); poLine.SetM_Product_ID(soLines[i].GetM_Product_ID()); poLine.SetM_AttributeSetInstance_ID(soLines[i].GetM_AttributeSetInstance_ID()); poLine.SetC_UOM_ID(soLines[i].GetC_UOM_ID()); poLine.SetQtyEntered(soLines[i].GetQtyEntered()); poLine.SetQtyOrdered(soLines[i].GetQtyOrdered()); poLine.SetDescription(soLines[i].GetDescription()); //poLine.SetDatePromised(soLines[i].GetDatePromised()); // Set date promised current date by Rakesh Kumar on 19/Mar/2021 suggested by Mandeep Singh poLine.SetDatePromised(Convert.ToDateTime(DateTime.Now)); poLine.SetIsDropShip(soLines[i].IsDropShip()); poLine.SetPrice(); // Set value in Property From Process to check on Before Save. poLine.SetFromProcess(true); if (!poLine.Save()) { ValueNamePair pp = VLogger.RetrieveError(); string msg = string.Empty; if (pp != null) { msg = pp.GetName(); //if GetName is Empty then it will check GetValue if (string.IsNullOrEmpty(msg)) { msg = Msg.GetMsg("", pp.GetValue()); } } if (string.IsNullOrEmpty(msg)) { msg = Msg.GetMsg(GetCtx(), "RecordNotSaved"); } log.Info("CreatePOfromSO : Not Saved. Error Value : " + msg); AddLog(0, null, null, msg + " : @DocumentNo@ : " + so.GetDocumentNo()); } //else //{ // if (poLine != null && poLine.GetC_OrderLine_ID() > 0) // { // consolidatePOLine = new ConsolidatePOLine(); // consolidatePOLine.C_Order_ID = poLine.GetC_Order_ID(); // consolidatePOLine.C_OrderLine_ID = poLine.GetC_OrderLine_ID(); // consolidatePOLine.M_Product_ID = poLine.GetM_Product_ID(); // consolidatePOLine.M_AttributeSetInstance_ID = poLine.GetM_AttributeSetInstance_ID(); // consolidatePOLine.C_UOM_ID = poLine.GetC_UOM_ID(); // consolidatePOLine.IsDropShip = soLines[i].IsDropShip() ? "Y" : "N"; // listConsolidatePOLine.Add(consolidatePOLine); // } //} } } } } //idr.Close(); } catch (Exception e) { if (idr != null) { idr.Close(); } log.Log(Level.SEVERE, sql.ToString(), e); } // Set Reference to PO if (po != null) { // Commented by Rakesh Kumar on 03/Mar/2020 //so.SetRef_Order_ID(po.GetC_Order_ID()); //so.Save(); // Update reference of PO in SO By Rakesh Kumar on 03/Mar/2020 sql.Clear(); sql.Append("Update C_Order Set REF_ORDER_ID=" + po.GetC_Order_ID() + " Where C_Order_ID=" + so.GetC_Order_ID()); DataBase.DB.ExecuteQuery(sql.ToString(), null, Get_TrxName()); } return(counter); }