// JID_0811 // Added by Bharat on 24 August 2018 to get Warehouse from Organization Info public JsonResult GetWarehouse(string fields) { string retJSON = ""; if (Session["ctx"] != null) { VAdvantage.Utility.Ctx ctx = Session["ctx"] as Ctx; int AD_Org_ID = Util.GetValueOfInt(fields); int warehouse_ID = 0; MOrgInfo oInfo = new MOrgInfo(ctx, AD_Org_ID, null); if (oInfo != null) { warehouse_ID = Util.GetValueOfInt(oInfo.Get_Value("M_Warehouse_ID")); } retJSON = JsonConvert.SerializeObject(warehouse_ID); } return(Json(retJSON, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Create Orders /// </summary> /// <returns>true if created</returns> private bool CreateOrders() { // Get Counter Org/BP int runAD_Org_ID = _run.GetAD_Org_ID(); if (runAD_Org_ID == 0) { runAD_Org_ID = GetCtx().GetAD_Org_ID(); } MOrg runOrg = MOrg.Get(GetCtx(), runAD_Org_ID); int runC_BPartner_ID = runOrg.GetLinkedC_BPartner_ID(); bool counter = !_run.IsCreateSingleOrder() && // no single Order runC_BPartner_ID > 0 && // Org linked to BP !_docType.IsSOTrx(); // PO MBPartner runBPartner = counter ? new MBPartner(GetCtx(), runC_BPartner_ID, Get_TrxName()) : null; if (!counter || runBPartner == null || runBPartner.Get_ID() != runC_BPartner_ID) { counter = false; } if (counter) { log.Info("RunBP=" + runBPartner + " - " + _docType); } log.Info("Single=" + _run.IsCreateSingleOrder() + " - " + _docType + ",SO=" + _docType.IsSOTrx()); log.Fine("Counter=" + counter + ",C_BPartner_ID=" + runC_BPartner_ID + "," + runBPartner); // MBPartner bp = null; MOrder singleOrder = null; MProduct product = null; // Consolidated Order if (_run.IsCreateSingleOrder()) { bp = new MBPartner(GetCtx(), _run.GetC_BPartner_ID(), Get_TrxName()); if (bp.Get_ID() == 0) { throw new ArgumentException("Business Partner not found - C_BPartner_ID=" + _run.GetC_BPartner_ID()); } // if (!_IsTest) { singleOrder = new MOrder(GetCtx(), 0, Get_TrxName()); singleOrder.SetC_DocTypeTarget_ID(_docType.GetC_DocType_ID()); singleOrder.SetC_DocType_ID(_docType.GetC_DocType_ID()); singleOrder.SetIsReturnTrx(_docType.IsReturnTrx()); singleOrder.SetIsSOTrx(_docType.IsSOTrx()); singleOrder.SetBPartner(bp); if (_run.GetC_BPartner_Location_ID() != 0) { singleOrder.SetC_BPartner_Location_ID(_run.GetC_BPartner_Location_ID()); } singleOrder.SetDateOrdered(_DateOrdered); singleOrder.SetDatePromised(_DatePromised); if (!singleOrder.Save()) { log.Log(Level.SEVERE, "Order not saved"); return(false); } _counter++; } } int lastC_BPartner_ID = 0; int lastC_BPartner_Location_ID = 0; MOrder order = null; // For all lines for (int i = 0; i < _details.Length; i++) { MDistributionRunDetail detail = _details[i]; // Create Order Header if (_run.IsCreateSingleOrder()) { order = singleOrder; } // New Business Partner else if (lastC_BPartner_ID != detail.GetC_BPartner_ID() || lastC_BPartner_Location_ID != detail.GetC_BPartner_Location_ID()) { // finish order order = null; } lastC_BPartner_ID = detail.GetC_BPartner_ID(); lastC_BPartner_Location_ID = detail.GetC_BPartner_Location_ID(); // New Order if (order == null) { bp = new MBPartner(GetCtx(), detail.GetC_BPartner_ID(), Get_TrxName()); if (!_IsTest) { order = new MOrder(GetCtx(), 0, Get_TrxName()); order.SetC_DocTypeTarget_ID(_docType.GetC_DocType_ID()); order.SetIsReturnTrx(_docType.IsReturnTrx()); order.SetC_DocType_ID(_docType.GetC_DocType_ID()); order.SetIsSOTrx(_docType.IsSOTrx()); // Counter Doc if (counter && bp.GetAD_OrgBP_ID_Int() > 0) { log.Fine("Counter - From_BPOrg=" + bp.GetAD_OrgBP_ID_Int() + "-" + bp + ", To_BP=" + runBPartner); order.SetAD_Org_ID(bp.GetAD_OrgBP_ID_Int()); MOrgInfo oi = MOrgInfo.Get(GetCtx(), bp.GetAD_OrgBP_ID_Int(), null); if (oi.GetM_Warehouse_ID() > 0) { order.SetM_Warehouse_ID(oi.GetM_Warehouse_ID()); } order.SetBPartner(runBPartner); } else // normal { log.Fine("From_Org=" + runAD_Org_ID + ", To_BP=" + bp); order.SetAD_Org_ID(runAD_Org_ID); order.SetBPartner(bp); if (detail.GetC_BPartner_Location_ID() != 0) { order.SetC_BPartner_Location_ID(detail.GetC_BPartner_Location_ID()); } } order.SetDateOrdered(_DateOrdered); order.SetDatePromised(_DatePromised); if (!order.Save()) { log.Log(Level.SEVERE, "Order not saved"); return(false); } } } // Line if (product == null || product.GetM_Product_ID() != detail.GetM_Product_ID()) { product = MProduct.Get(GetCtx(), detail.GetM_Product_ID()); } if (_IsTest) { AddLog(0, null, detail.GetActualAllocation(), bp.GetName() + " - " + product.GetName()); continue; } // Create Order Line MOrderLine line = new MOrderLine(order); if (counter && bp.GetAD_OrgBP_ID_Int() > 0) { ; // don't overwrite counter doc } else // normal - optionally overwrite { line.SetC_BPartner_ID(detail.GetC_BPartner_ID()); if (detail.GetC_BPartner_Location_ID() != 0) { line.SetC_BPartner_Location_ID(detail.GetC_BPartner_Location_ID()); } } // line.SetProduct(product); line.SetQty(detail.GetActualAllocation()); line.SetPrice(); if (!line.Save()) { log.Log(Level.SEVERE, "OrderLine not saved"); return(false); } AddLog(0, null, detail.GetActualAllocation(), order.GetDocumentNo() + ": " + bp.GetName() + " - " + product.GetName()); } // finish order order = null; return(true); }
} // sendAlert private int sendAlertToResponsible(MWFResponsible responsible, List <int> list, MWFProcess process, String subject, String message, FileInfo pdf) { int counter = 0; if (responsible.IsInvoker()) { ; } // Human else if (X_AD_WF_Responsible.RESPONSIBLETYPE_Human.Equals(responsible.GetResponsibleType()) && responsible.GetAD_User_ID() != 0 && !list.Contains(responsible.GetAD_User_ID())) { if (m_client.SendEMail(responsible.GetAD_User_ID(), subject, message, pdf)) { counter++; } list.Add(responsible.GetAD_User_ID()); } // Org of the Document else if (X_AD_WF_Responsible.RESPONSIBLETYPE_Organization.Equals(responsible.GetResponsibleType())) { PO document = process.GetPO(); if (document != null) { MOrgInfo org = MOrgInfo.Get(GetCtx(), document.GetAD_Org_ID(), null); if (org.GetSupervisor_ID() != 0 && !list.Contains(org.GetSupervisor_ID())) { if (m_client.SendEMail(org.GetSupervisor_ID(), subject, message, pdf)) { counter++; } list.Add(org.GetSupervisor_ID()); } } } // Role else if (X_AD_WF_Responsible.RESPONSIBLETYPE_Role.Equals(responsible.GetResponsibleType()) && responsible.GetAD_Role_ID() != 0) { MUserRoles[] userRoles = MUserRoles.GetOfRole(GetCtx(), responsible.GetAD_Role_ID()); for (int i = 0; i < userRoles.Length; i++) { MUserRoles roles = userRoles[i]; if (!roles.IsActive()) { continue; } int AD_User_ID = roles.GetAD_User_ID(); if (!list.Contains(AD_User_ID)) { if (m_client.SendEMail(AD_User_ID, subject, message, pdf)) { counter++; } list.Add(AD_User_ID); } } } return(counter); } // sendAlertToResponsible
} // prepare /// <summary> /// Perform Process. /// </summary> /// <returns>Message (text with variables)</returns> protected override String DoIt() { log.Info("C_BPartner_ID=" + _C_BPartner_ID + ", AD_Org_ID=" + _AD_Org_ID + ", AD_OrgType_ID=" + _AD_OrgType_ID + ", AD_Role_ID=" + _AD_Role_ID); if (_C_BPartner_ID == 0) { throw new Exception("No Business Partner ID"); } MBPartner bp = new MBPartner(GetCtx(), _C_BPartner_ID, Get_Trx()); if (bp.Get_ID() == 0) { throw new Exception("Business Partner not found - C_BPartner_ID=" + _C_BPartner_ID); } // BP Location MBPartnerLocation[] locs = bp.GetLocations(false); if (locs == null || locs.Length == 0) { throw new ArgumentException("Business Partner has no Location"); } // Location int C_Location_ID = locs[0].GetC_Location_ID(); if (C_Location_ID == 0) { throw new ArgumentException("Business Partner Location has no Address"); } // Create Org Boolean newOrg = _AD_Org_ID == 0; MOrg org = new MOrg(GetCtx(), _AD_Org_ID, Get_Trx()); if (newOrg) { org.SetValue(bp.GetValue()); org.SetName(bp.GetName()); org.SetDescription(bp.GetDescription()); if (!org.Save()) { return(GetRetrievedError(org, "Organization not saved")); //throw new Exception("Organization not saved"); } } else // check if linked to already { int C_BPartner_ID = org.GetLinkedC_BPartner_ID(); if (C_BPartner_ID > 0) { throw new ArgumentException("Organization '" + org.GetName() + "' already linked (to C_BPartner_ID=" + C_BPartner_ID + ")"); } } _AD_Org_ID = org.GetAD_Org_ID(); // Update Org Info MOrgInfo oInfo = org.GetInfo(); oInfo.SetAD_OrgType_ID(_AD_OrgType_ID); if (newOrg) { oInfo.SetC_Location_ID(C_Location_ID); } // Create Warehouse MWarehouse wh = null; if (!newOrg) { MWarehouse[] whs = MWarehouse.GetForOrg(GetCtx(), _AD_Org_ID); if (whs != null && whs.Length > 0) { wh = whs[0]; // pick first } } // New Warehouse if (wh == null) { wh = new MWarehouse(org); if (!wh.Save()) { return(GetRetrievedError(wh, "Warehouse not saved")); //throw new Exception("Warehouse not saved"); } } // Create Locator MLocator mLoc = wh.GetDefaultLocator(); if (mLoc == null) { mLoc = new MLocator(wh, "Standard"); mLoc.SetIsDefault(true); mLoc.Save(); } // Update/Save Org Info oInfo.SetM_Warehouse_ID(wh.GetM_Warehouse_ID()); if (!oInfo.Save(Get_Trx())) { return(GetRetrievedError(oInfo, "Organization Info not saved")); //throw new Exception("Organization Info not saved"); } // Update BPartner bp.SetAD_OrgBP_ID(_AD_Org_ID); if (bp.GetAD_Org_ID() != 0) { bp.SetClientOrg(bp.GetAD_Client_ID(), 0); // Shared BPartner } // Save BP if (!bp.Save()) { return(GetRetrievedError(bp, "Business Partner not updated")); //throw new Exception("Business Partner not updated"); } // Limit to specific Role if (_AD_Role_ID != 0) { Boolean found = false; MRoleOrgAccess[] orgAccesses = MRoleOrgAccess.GetOfOrg(GetCtx(), _AD_Org_ID); // delete all accesses except the specific for (int i = 0; i < orgAccesses.Length; i++) { if (orgAccesses[i].GetAD_Role_ID() == _AD_Role_ID) { found = true; } else { orgAccesses[i].Delete(true); } } // create access if (!found) { MRoleOrgAccess orgAccess = new MRoleOrgAccess(org, _AD_Role_ID); orgAccess.Save(); } } // Reset Client Role MRole.GetDefault(GetCtx(), true); return("Business Partner - Organization Link created"); } // doIt