Example #1
0
        public static PXRedirectRequiredException CreatePOOrders2(
            System.Collections.Generic.List <POFixedDemand> list,
            DateTime?PurchDate,
            bool extSort)
        {
            POOrderEntry docgraph = PXGraph.CreateInstance <POOrderEntry>();

            docgraph.Views.Caches.Add(typeof(POOrderEntry.SOLineSplit3));
            POSetup current = docgraph.POSetup.Current;
            DocumentList <POOrder> documentList1 = new DocumentList <POOrder>((PXGraph)docgraph);
            Dictionary <string, DocumentList <POLine> > dictionary = new Dictionary <string, DocumentList <POLine> >();

            list.Sort((Comparison <POFixedDemand>)((a, b) =>
            {
                string empty1 = string.Empty;
                string empty2 = string.Empty;
                string str;
                if (a.PlanType == "90")
                {
                    PX.Objects.IN.InventoryItem inventoryItem = PX.Objects.IN.InventoryItem.PK.Find((PXGraph)docgraph, a.InventoryID);
                    str = string.Format("ZZ.{0}", inventoryItem == null ? (object)string.Empty : (object)inventoryItem.InventoryCD);
                }
                else
                {
                    POOrderEntry.SOLineSplit3 soLineSplit3 = (POOrderEntry.SOLineSplit3)PXSelectBase <POOrderEntry.SOLineSplit3, PXSelect <POOrderEntry.SOLineSplit3, Where <POOrderEntry.SOLineSplit3.planID, Equal <Required <POOrderEntry.SOLineSplit3.planID> > > > .Config> .Select((PXGraph)docgraph, (object)a.PlanID);
                    str = soLineSplit3 == null ? string.Empty : string.Format("{0}.{1}.{2:D7}", (object)soLineSplit3.OrderType, (object)soLineSplit3.OrderNbr, (object)soLineSplit3.SortOrder.GetValueOrDefault());
                }
                string strB;
                if (b.PlanType == "90")
                {
                    PX.Objects.IN.InventoryItem inventoryItem = PX.Objects.IN.InventoryItem.PK.Find((PXGraph)docgraph, b.InventoryID);
                    strB = string.Format("ZZ.{0}", inventoryItem == null ? (object)string.Empty : (object)inventoryItem.InventoryCD);
                }
                else
                {
                    POOrderEntry.SOLineSplit3 soLineSplit3 = (POOrderEntry.SOLineSplit3)PXSelectBase <POOrderEntry.SOLineSplit3, PXSelect <POOrderEntry.SOLineSplit3, Where <POOrderEntry.SOLineSplit3.planID, Equal <Required <POOrderEntry.SOLineSplit3.planID> > > > .Config> .Select((PXGraph)docgraph, (object)b.PlanID);
                    strB = soLineSplit3 == null ? string.Empty : string.Format("{0}.{1}.{2:D7}", (object)soLineSplit3.OrderType, (object)soLineSplit3.OrderNbr, (object)soLineSplit3.SortOrder.GetValueOrDefault());
                }
                return(str.CompareTo(strB));
            }));
            POOrder poOrder1 = (POOrder)null;
            bool    flag1    = false;

            foreach (POFixedDemand demand in list)
            {
                if (!(demand.FixedSource != "P"))
                {
                    string OrderType = demand.PlanType == "6D" ? "DP" : (demand.PlanType == "6E" ? "DP" : "RO");
                    string str1      = (string)null;
                    int?   nullable1 = demand.VendorID;
                    int    num1;
                    if (nullable1.HasValue)
                    {
                        nullable1 = demand.VendorLocationID;
                        num1      = !nullable1.HasValue ? 1 : 0;
                    }
                    else
                    {
                        num1 = 1;
                    }
                    if (num1 != 0)
                    {
                        PXProcessing <POFixedDemand> .SetWarning(list.IndexOf(demand), "Vendor and vendor location should be defined.");
                    }
                    else
                    {
                        PXErrorLevel pxErrorLevel = PXErrorLevel.RowInfo;
                        string       empty        = string.Empty;
                        try
                        {
                            PX.Objects.SO.SOOrder soOrder = (PX.Objects.SO.SOOrder) PXSelectBase <PX.Objects.SO.SOOrder, PXSelect <PX.Objects.SO.SOOrder, Where <PX.Objects.SO.SOOrder.noteID, Equal <Required <PX.Objects.SO.SOOrder.noteID> > > > .Config> .Select((PXGraph)docgraph, (object)demand.RefNoteID);

                            POOrderEntry.SOLineSplit3 soLineSplit3 = (POOrderEntry.SOLineSplit3) PXSelectBase <POOrderEntry.SOLineSplit3, PXSelect <POOrderEntry.SOLineSplit3, Where <POOrderEntry.SOLineSplit3.planID, Equal <Required <POOrderEntry.SOLineSplit3.planID> > > > .Config> .Select((PXGraph)docgraph, (object)demand.PlanID);

                            string str2 = (string)null;
                            string str3 = (string)null;
                            if (demand.PlanType == "6B" || demand.PlanType == "6E")
                            {
                                str2 = soLineSplit3.POType;
                                str3 = soLineSplit3.PONbr;
                            }
                            System.Collections.Generic.List <FieldLookup> fieldLookupList1 = new System.Collections.Generic.List <FieldLookup>()
                            {
                                (FieldLookup) new FieldLookup <POOrder.orderType>((object)OrderType),
                                (FieldLookup) new FieldLookup <POOrder.vendorID>((object)demand.VendorID),
                                (FieldLookup) new FieldLookup <POOrder.vendorLocationID>((object)demand.VendorLocationID),
                                (FieldLookup) new FieldLookup <POOrder.bLOrderNbr>((object)str3)
                            };
                            if (OrderType == "RO")
                            {
                                bool?projectPerDocument = docgraph.apsetup.Current.RequireSingleProjectPerDocument;
                                bool flag2 = true;
                                if (projectPerDocument.GetValueOrDefault() == flag2 & projectPerDocument.HasValue)
                                {
                                    nullable1 = demand.ProjectID;
                                    int?nullable2 = nullable1.HasValue ? nullable1 : ProjectDefaultAttribute.NonProject();
                                    fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.projectID>((object)nullable2));
                                }
                                int num2;
                                if (poOrder1 != null && poOrder1.ShipDestType == "L")
                                {
                                    nullable1 = poOrder1.SiteID;
                                    num2      = !nullable1.HasValue ? 1 : 0;
                                }
                                else
                                {
                                    num2 = 0;
                                }
                                if (num2 == 0)
                                {
                                    fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.siteID>((object)demand.POSiteID));
                                }
                            }
                            else if (OrderType == "DP")
                            {
                                fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.sOOrderType>((object)soLineSplit3.OrderType));
                                fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.sOOrderNbr>((object)soLineSplit3.OrderNbr));
                            }
                            else
                            {
                                fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.shipToBAccountID>((object)soOrder.CustomerID));
                                fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.shipToLocationID>((object)soOrder.CustomerLocationID));
                                fieldLookupList1.Add((FieldLookup) new FieldLookup <POOrder.siteID>((object)demand.POSiteID));
                            }
                            poOrder1 = documentList1.Find(fieldLookupList1.ToArray()) ?? new POOrder();
                            if (poOrder1.OrderNbr == null)
                            {
                                docgraph.Clear();
                                poOrder1.OrderType = OrderType;
                                poOrder1           = PXCache <POOrder> .CreateCopy(docgraph.Document.Insert(poOrder1));

                                poOrder1.VendorID         = demand.VendorID;
                                poOrder1.VendorLocationID = demand.VendorLocationID;
                                poOrder1.SiteID           = demand.POSiteID;
                                nullable1 = demand.ProjectID;
                                if (nullable1.HasValue)
                                {
                                    poOrder1.ProjectID = demand.ProjectID;
                                }
                                poOrder1.OrderDate  = PurchDate;
                                poOrder1.BLType     = str2;
                                poOrder1.BLOrderNbr = str3;
                                if (OrderType == "DP" | extSort)
                                {
                                    poOrder1.SOOrderType = soLineSplit3.OrderType;
                                    poOrder1.SOOrderNbr  = soLineSplit3.OrderNbr;
                                }
                                if (!string.IsNullOrEmpty(poOrder1.BLOrderNbr))
                                {
                                    POOrder poOrder2 = (POOrder)PXSelectBase <POOrder, PXSelect <POOrder, Where <POOrder.orderType, Equal <Current <POOrder.bLType> >, And <POOrder.orderNbr, Equal <Current <POOrder.bLOrderNbr> > > > > .Config> .SelectSingleBound((PXGraph)docgraph, new object[1]
                                    {
                                        (object)poOrder1
                                    });

                                    if (poOrder2 != null)
                                    {
                                        poOrder1.VendorRefNbr = poOrder2.VendorRefNbr;
                                    }
                                }
                                if (OrderType == "DP")
                                {
                                    poOrder1.ShipDestType     = "C";
                                    poOrder1.ShipToBAccountID = soOrder.CustomerID;
                                    poOrder1.ShipToLocationID = soOrder.CustomerLocationID;
                                }
                                else if (current.ShipDestType == "S")
                                {
                                    poOrder1.ShipDestType = "S";
                                    poOrder1.SiteID       = demand.POSiteID;
                                }
                                if (PXAccess.FeatureInstalled <PX.Objects.CS.FeaturesSet.multicurrency>())
                                {
                                    docgraph.currencyinfo.Current.CuryID = (string)null;
                                }
                                poOrder1 = docgraph.Document.Update(poOrder1);
                                if (OrderType == "DP")
                                {
                                    SOAddress soAddress = (SOAddress)PXSelectBase <SOAddress, PXSelect <SOAddress, Where <SOAddress.addressID, Equal <Required <PX.Objects.SO.SOOrder.shipAddressID> > > > .Config> .Select((PXGraph)docgraph, (object)soOrder.ShipAddressID);

                                    bool?isDefaultAddress = soAddress.IsDefaultAddress;
                                    bool flag2            = false;
                                    if (isDefaultAddress.GetValueOrDefault() == flag2 & isDefaultAddress.HasValue)
                                    {
                                        SharedRecordAttribute.CopyRecord <POOrder.shipAddressID>(docgraph.Document.Cache, (object)poOrder1, (object)soAddress, true);
                                    }
                                    SOContact soContact = (SOContact)PXSelectBase <SOContact, PXSelect <SOContact, Where <SOContact.contactID, Equal <Required <PX.Objects.SO.SOOrder.shipContactID> > > > .Config> .Select((PXGraph)docgraph, (object)soOrder.ShipContactID);

                                    bool?isDefaultContact = soContact.IsDefaultContact;
                                    bool flag3            = false;
                                    if (isDefaultContact.GetValueOrDefault() == flag3 & isDefaultContact.HasValue)
                                    {
                                        SharedRecordAttribute.CopyRecord <POOrder.shipContactID>(docgraph.Document.Cache, (object)poOrder1, (object)soContact, true);
                                    }
                                    DateTime?expectedDate = poOrder1.ExpectedDate;
                                    DateTime?requestDate  = soOrder.RequestDate;
                                    if (expectedDate.HasValue & requestDate.HasValue && expectedDate.GetValueOrDefault() < requestDate.GetValueOrDefault())
                                    {
                                        poOrder1 = PXCache <POOrder> .CreateCopy(poOrder1);

                                        poOrder1.ExpectedDate = soOrder.RequestDate;
                                        poOrder1 = docgraph.Document.Update(poOrder1);
                                    }
                                }
                            }
                            else if (!docgraph.Document.Cache.ObjectsEqual((object)docgraph.Document.Current, (object)poOrder1))
                            {
                                docgraph.Document.Current = (POOrder)docgraph.Document.Search <POOrder.orderNbr>((object)poOrder1.OrderNbr, (object)poOrder1.OrderType);
                            }
                            poOrder1.UpdateVendorCost = new bool?(false);
                            POLine poLine1 = (POLine)null;
                            DocumentList <POLine> documentList2;
                            if (!dictionary.TryGetValue(demand.PlanType, out documentList2))
                            {
                                documentList2 = dictionary[demand.PlanType] = new DocumentList <POLine>((PXGraph)docgraph);
                            }
                            if (OrderType == "RO" && demand.PlanType != "6B")
                            {
                                System.Collections.Generic.List <FieldLookup> fieldLookupList2 = new System.Collections.Generic.List <FieldLookup>();
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.vendorID>((object)demand.VendorID));
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.vendorLocationID>((object)demand.VendorLocationID));
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.siteID>((object)demand.POSiteID));
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.inventoryID>((object)demand.InventoryID));
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.subItemID>((object)demand.SubItemID));
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.requestedDate>((object)(DateTime?)soLineSplit3?.ShipDate));
                                int?nullable2;
                                if (soLineSplit3 == null)
                                {
                                    nullable1 = new int?();
                                    nullable2 = nullable1;
                                }
                                else
                                {
                                    nullable2 = soLineSplit3.ProjectID;
                                }
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.projectID>((object)nullable2));
                                int?nullable3;
                                if (soLineSplit3 == null)
                                {
                                    nullable1 = new int?();
                                    nullable3 = nullable1;
                                }
                                else
                                {
                                    nullable3 = soLineSplit3.TaskID;
                                }
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.taskID>((object)nullable3));
                                int?nullable4;
                                if (soLineSplit3 == null)
                                {
                                    nullable1 = new int?();
                                    nullable4 = nullable1;
                                }
                                else
                                {
                                    nullable4 = soLineSplit3.CostCodeID;
                                }
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.costCodeID>((object)nullable4));
                                int?nullable5;
                                if (soLineSplit3 == null)
                                {
                                    nullable1 = new int?();
                                    nullable5 = nullable1;
                                }
                                else
                                {
                                    nullable5 = soLineSplit3.LineNbr;
                                }
                                fieldLookupList2.Add((FieldLookup) new FieldLookup <POLine.lineNbr>((object)nullable5));
                                System.Collections.Generic.List <FieldLookup> fieldLookupList3 = fieldLookupList2;
                                bool?nullable6 = current.CopyLineDescrSO;
                                bool flag2     = true;
                                if (nullable6.GetValueOrDefault() == flag2 & nullable6.HasValue && soLineSplit3 != null)
                                {
                                    fieldLookupList3.Add((FieldLookup) new FieldLookup <POLine.tranDesc>((object)soLineSplit3.TranDesc));
                                    poLine1 = documentList2.Find(fieldLookupList3.ToArray());
                                    int num2;
                                    if (poLine1 != null)
                                    {
                                        nullable6 = current.CopyLineNoteSO;
                                        bool flag3 = true;
                                        if (nullable6.GetValueOrDefault() == flag3 & nullable6.HasValue)
                                        {
                                            num2 = PXNoteAttribute.GetNote(docgraph.Caches[typeof(POLine)], (object)poLine1) != null ? 1 : (PXNoteAttribute.GetNote(docgraph.Caches[typeof(POOrderEntry.SOLineSplit3)], (object)soLineSplit3) != null ? 1 : 0);
                                            goto label_67;
                                        }
                                    }
                                    num2 = 0;
label_67:
                                    if (num2 != 0)
                                    {
                                        poLine1 = (POLine)null;
                                    }
                                }
                                else
                                {
                                    poLine1 = documentList2.Find(fieldLookupList3.ToArray());
                                }
                            }
                            POLine dest = poLine1 ?? new POLine();
                            POLine copy1;
                            if (dest.OrderNbr == null)
                            {
                                docgraph.FillPOLineFromDemand(dest, demand, OrderType, soLineSplit3);
                                POLine line           = docgraph.Transactions.Insert(dest);
                                bool?  copyLineNoteSo = current.CopyLineNoteSO;
                                bool   flag2          = true;
                                if (copyLineNoteSo.GetValueOrDefault() == flag2 & copyLineNoteSo.HasValue && soLineSplit3 != null)
                                {
                                    PXNoteAttribute.SetNote(docgraph.Transactions.Cache, (object)line, PXNoteAttribute.GetNote(docgraph.Caches[typeof(POOrderEntry.SOLineSplit3)], (object)soLineSplit3));
                                }
                                if (docgraph.onCopyPOLineFields != null)
                                {
                                    docgraph.onCopyPOLineFields(demand, line);
                                }
                                copy1 = PXCache <POLine> .CreateCopy(line);

                                documentList2.Add(copy1);
                            }
                            else
                            {
                                copy1 = PXCache <POLine> .CreateCopy((POLine)PXSelectBase <POLine, PXSelect <POLine, Where <POLine.orderType, Equal <Current <POOrder.orderType> >, And <POLine.orderNbr, Equal <Current <POOrder.orderNbr> >, And <POLine.lineNbr, Equal <Current <POLine.lineNbr> > > > > > .Config> .SelectSingleBound((PXGraph)docgraph, new object[1]
                                {
                                    (object)dest
                                }));

                                POLine  poLine2   = copy1;
                                Decimal?orderQty1 = poLine2.OrderQty;
                                Decimal?orderQty2 = demand.OrderQty;
                                poLine2.OrderQty = orderQty1.HasValue & orderQty2.HasValue ? new Decimal?(orderQty1.GetValueOrDefault() + orderQty2.GetValueOrDefault()) : new Decimal?();
                            }
                            if (demand.PlanType == "6B" || demand.PlanType == "6E")
                            {
                                str1 = demand.PlanType == "6B" ? "66" : "6D";
                                demand.FixedSource = "P";
                                copy1.POType       = soLineSplit3.POType;
                                copy1.PONbr        = soLineSplit3.PONbr;
                                copy1.POLineNbr    = soLineSplit3.POLineNbr;
                                POLine poLine2 = (POLine)PXSelectBase <POLine, PXSelect <POLine, Where <POLine.orderType, Equal <Current <POLine.pOType> >, And <POLine.orderNbr, Equal <Current <POLine.pONbr> >, And <POLine.lineNbr, Equal <Current <POLine.pOLineNbr> > > > > > .Config> .SelectSingleBound((PXGraph)docgraph, new object[1]
                                {
                                    (object)copy1
                                });

                                if (poLine2 != null)
                                {
                                    Decimal?nullable2   = demand.PlanQty;
                                    Decimal?baseOpenQty = poLine2.BaseOpenQty;
                                    if (nullable2.GetValueOrDefault() > baseOpenQty.GetValueOrDefault() & (nullable2.HasValue & baseOpenQty.HasValue))
                                    {
                                        POLine  poLine3  = copy1;
                                        Decimal?orderQty = poLine3.OrderQty;
                                        nullable2        = demand.OrderQty;
                                        poLine3.OrderQty = orderQty.HasValue & nullable2.HasValue ? new Decimal?(orderQty.GetValueOrDefault() - nullable2.GetValueOrDefault()) : new Decimal?();
                                        if (string.Equals(copy1.UOM, poLine2.UOM))
                                        {
                                            POLine poLine4 = copy1;
                                            nullable2 = poLine4.OrderQty;
                                            Decimal?openQty = poLine2.OpenQty;
                                            poLine4.OrderQty = nullable2.HasValue & openQty.HasValue ? new Decimal?(nullable2.GetValueOrDefault() + openQty.GetValueOrDefault()) : new Decimal?();
                                        }
                                        else
                                        {
                                            PXDBQuantityAttribute.CalcBaseQty <POLine.orderQty>(docgraph.Transactions.Cache, (object)copy1);
                                            POLine  poLine4      = copy1;
                                            Decimal?baseOrderQty = poLine4.BaseOrderQty;
                                            nullable2            = poLine2.BaseOpenQty;
                                            poLine4.BaseOrderQty = baseOrderQty.HasValue & nullable2.HasValue ? new Decimal?(baseOrderQty.GetValueOrDefault() + nullable2.GetValueOrDefault()) : new Decimal?();
                                            PXDBQuantityAttribute.CalcTranQty <POLine.orderQty>(docgraph.Transactions.Cache, (object)copy1);
                                        }
                                        pxErrorLevel = PXErrorLevel.RowWarning;
                                        empty       += PXMessages.LocalizeFormatNoPrefixNLA("Order Quantity reduced to Blanket Order: '{0}' Open Qty. for this item", (object)copy1.PONbr);
                                    }
                                    copy1.CuryUnitCost = poLine2.CuryUnitCost;
                                    copy1.UnitCost     = poLine2.UnitCost;
                                }
                            }
                            copy1.SiteID = POCreate_Extension.GetWasrehouseByBranch((PXGraph)docgraph, copy1.BranchID);
                            short?       vleadTime = docgraph.location.Current.VLeadTime;
                            POLine       poLine5   = copy1;
                            DateTime     dateTime  = soLineSplit3.ShipDate.Value;
                            ref DateTime local     = ref dateTime;
                            short?       nullable7 = vleadTime;
                            nullable1 = nullable7.HasValue ? new int?((int)nullable7.GetValueOrDefault()) : new int?();
                            int      num3      = 0;
                            double   num4      = nullable1.GetValueOrDefault() > num3 & nullable1.HasValue ? (double)-vleadTime.Value : -14.0;
                            DateTime?nullable8 = new DateTime?(local.AddDays(num4));
                            poLine5.RequestedDate = nullable8;
                            POLine  poLine6 = docgraph.Transactions.Update(copy1);
                            PXCache cach    = docgraph.Caches[typeof(INItemPlan)];
                            POCreate_Extension.CreateSplitDemand2(cach, demand);
                            cach.SetStatus((object)demand, PXEntryStatus.Updated);
                            demand.SupplyPlanID = poLine6.PlanID;
                            if (str1 != null)
                            {
                                cach.RaiseRowDeleted((object)demand);
                                demand.PlanType = str1;
                                cach.RaiseRowInserted((object)demand);
                            }
                            if (soLineSplit3 != null)
                            {
                                int num2;
                                if (demand.AlternateID != null)
                                {
                                    nullable1 = demand.InventoryID;
                                    num2      = nullable1.HasValue ? 1 : 0;
                                }
                                else
                                {
                                    num2 = 0;
                                }
                                if (num2 != 0)
                                {
                                    PXSelectBase <INItemXRef> pxSelectBase = (PXSelectBase <INItemXRef>) new PXSelect <INItemXRef, Where <INItemXRef.inventoryID, Equal <Required <INItemXRef.inventoryID> >, And <INItemXRef.alternateID, Equal <Required <INItemXRef.alternateID> > > > >((PXGraph)docgraph);
                                    INItemXRef inItemXref1 = (INItemXRef)pxSelectBase.Select((object)demand.InventoryID, (object)demand.AlternateID);
                                    if (inItemXref1 != null && inItemXref1.AlternateType == "GLBL")
                                    {
                                        int num5;
                                        if (poLine6.AlternateID != null)
                                        {
                                            nullable1 = poLine6.InventoryID;
                                            num5      = nullable1.HasValue ? 1 : 0;
                                        }
                                        else
                                        {
                                            num5 = 0;
                                        }
                                        if (num5 != 0)
                                        {
                                            INItemXRef inItemXref2 = (INItemXRef)pxSelectBase.Select((object)poLine6.InventoryID, (object)poLine6.AlternateID);
                                            if (inItemXref2 != null && inItemXref2.AlternateType == "GLBL")
                                            {
                                                poLine6.AlternateID = demand.AlternateID;
                                            }
                                        }
                                        else
                                        {
                                            poLine6.AlternateID = demand.AlternateID;
                                        }
                                    }
                                }
                                soLineSplit3.POType    = poLine6.OrderType;
                                soLineSplit3.PONbr     = poLine6.OrderNbr;
                                soLineSplit3.POLineNbr = poLine6.LineNbr;
                                soLineSplit3.RefNoteID = docgraph.Document.Current.NoteID;
                                PX.Objects.SO.SOLine soLine = SelectFrom <PX.Objects.SO.SOLine> .Where <PX.Objects.SO.SOLine.orderType.IsEqual <P.AsString>
                                                                                                        .And <PX.Objects.SO.SOLine.orderNbr.IsEqual <P.AsString>
                                                                                                              .And <PX.Objects.SO.SOLine.lineNbr.IsEqual <P.AsInt> > > > .View.ReadOnly.Select((PXGraph)docgraph, (object)soLineSplit3.OrderType, (object)soLineSplit3.OrderNbr, (object)soLineSplit3.LineNbr);

                                POLineExt extension1 = poLine6.GetExtension <POLineExt>();
                                SOLineExt extension2 = soLine.GetExtension <SOLineExt>();
                                extension1.UsrEndCustomerID = extension2.UsrEndCustomerID;
                                extension1.UsrNonStockItem  = extension2.UsrNonStockItem;
                                extension1.UsrProjectNbr    = extension2.UsrProjectNbr;
                                if (!string.IsNullOrEmpty(extension2.UsrProjectNbr))
                                {
                                    FLXProject flxProject = SelectFrom <FLXProject> .Where <FLXProject.projectNbr.IsEqual <P.AsString> > .View.ReadOnly.Select((PXGraph)docgraph, (object)extension2.UsrProjectNbr);

                                    extension1.UsrCust2Factory = flxProject.Cust2Factory;
                                    extension1.UsrFactoryPN    = flxProject.FactoryPN;
                                }
                                docgraph.GetExtension <POOrderEntry_Extension>().UpdateSOLine(soLineSplit3, docgraph.Document.Current.VendorID, true);
                                docgraph.FixedDemand.Cache.SetStatus((object)soLineSplit3, PXEntryStatus.Updated);
                            }
                            if (docgraph.Transactions.Cache.IsInsertedUpdatedDeleted)
                            {
                                using (PXTransactionScope transactionScope = new PXTransactionScope())
                                {
                                    docgraph.Save.Press();
                                    if (demand.PlanType == "90")
                                    {
                                        docgraph.Replenihment.Current = (INReplenishmentOrder)docgraph.Replenihment.Search <INReplenishmentOrder.noteID>((object)demand.RefNoteID);
                                        if (docgraph.Replenihment.Current != null)
                                        {
                                            INReplenishmentLine copy2 = PXCache <INReplenishmentLine> .CreateCopy(docgraph.ReplenishmentLines.Insert(new INReplenishmentLine()));

                                            copy2.InventoryID      = poLine6.InventoryID;
                                            copy2.SubItemID        = poLine6.SubItemID;
                                            copy2.UOM              = poLine6.UOM;
                                            copy2.VendorID         = poLine6.VendorID;
                                            copy2.VendorLocationID = poLine6.VendorLocationID;
                                            copy2.Qty              = poLine6.OrderQty;
                                            copy2.POType           = poLine6.OrderType;
                                            copy2.PONbr            = docgraph.Document.Current.OrderNbr;
                                            copy2.POLineNbr        = poLine6.LineNbr;
                                            copy2.SiteID           = demand.POSiteID;
                                            copy2.PlanID           = demand.PlanID;
                                            docgraph.ReplenishmentLines.Update(copy2);
                                            docgraph.Caches[typeof(INItemPlan)].Delete((object)demand);
                                            docgraph.Save.Press();
                                        }
                                    }
                                    transactionScope.Complete();
                                }
                                if (pxErrorLevel == PXErrorLevel.RowInfo)
                                {
                                    PXProcessing <POFixedDemand> .SetInfo(list.IndexOf(demand), PXMessages.LocalizeFormatNoPrefixNLA("Purchase Order '{0}' created.", (object)docgraph.Document.Current.OrderNbr) + "\r\n" + empty);
                                }
                                else
                                {
                                    PXProcessing <POFixedDemand> .SetWarning(list.IndexOf(demand), PXMessages.LocalizeFormatNoPrefixNLA("Purchase Order '{0}' created.", (object)docgraph.Document.Current.OrderNbr) + "\r\n" + empty);
                                }
                                if (documentList1.Find((object)docgraph.Document.Current) == null)
                                {
                                    documentList1.Add(docgraph.Document.Current);
                                }
                            }
                        }
        //TODO:refactor
        public static void SOCreateProc(List <SOFixedDemand> list, DateTime?PurchDate)
        {
            SOOrderEntry           docgraph = PXGraph.CreateInstance <SOOrderEntry>();
            SOSetup                sosetup  = docgraph.sosetup.Current;
            DocumentList <SOOrder> created  = new DocumentList <SOOrder>(docgraph);

            docgraph.ExceptionHandling.AddHandler <SOLineSplit.qty>((cache, e) => { e.Cancel = true; });
            docgraph.ExceptionHandling.AddHandler <SOLineSplit.isAllocated>((cache, e) => { ((SOLineSplit)e.Row).IsAllocated = true; e.Cancel = true; });

            foreach (SOFixedDemand demand in list)
            {
                string OrderType =
                    sosetup.TransferOrderType ?? SOOrderTypeConstants.TransferOrder;

                string demandPlanType = demand.PlanType;

                try
                {
                    if (demand.SourceSiteID == null)
                    {
                        PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.MissingSourceSite);

                        continue;
                    }

                    SOOrder      order;
                    SOLineSplit2 sosplit = PXSelect <SOLineSplit2, Where <SOLineSplit2.planID, Equal <Required <SOLineSplit2.planID> > > > .Select(docgraph, demand.PlanID);

                    if (sosplit != null)
                    {
                        order = created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID>(OrderType, sosplit.ToSiteID, sosplit.SiteID);
                    }
                    else
                    {
                        if (demand.SourceSiteID == demand.SiteID)
                        {
                            PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.EqualSourceDestinationSite);

                            continue;
                        }

                        order = created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID>(OrderType, demand.SiteID, demand.SourceSiteID);
                    }

                    if (order == null)
                    {
                        order = new SOOrder();
                    }

                    if (order.OrderNbr == null)
                    {
                        docgraph.Clear();

                        if (sosplit != null)
                        {
                            INSite sourceSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(docgraph, sosplit.SiteID);

                            order.BranchID  = sourceSite.BranchID;
                            order.OrderType = OrderType;
                            order           = PXCache <SOOrder> .CreateCopy(docgraph.Document.Insert(order));

                            order.DefaultSiteID     = sosplit.SiteID;
                            order.DestinationSiteID = sosplit.ToSiteID;
                            order.OrderDate         = PurchDate;

                            docgraph.Document.Update(order);
                        }
                        else
                        {
                            INSite sourceSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(docgraph, demand.SourceSiteID);

                            order.BranchID  = sourceSite.BranchID;
                            order.OrderType = OrderType;
                            order           = PXCache <SOOrder> .CreateCopy(docgraph.Document.Insert(order));

                            order.DefaultSiteID     = demand.SourceSiteID;
                            order.DestinationSiteID = demand.SiteID;
                            order.OrderDate         = PurchDate;

                            docgraph.Document.Update(order);
                        }
                    }
                    else if (docgraph.Document.Cache.ObjectsEqual(docgraph.Document.Current, order) == false)
                    {
                        docgraph.Document.Current = docgraph.Document.Search <SOOrder.orderNbr>(order.OrderNbr, order.OrderType);
                    }

                    SOLine      newline;
                    SOLineSplit newsplit;
                    PXCache     cache = docgraph.Caches[typeof(INItemPlan)];
                    INItemPlan  rp;

                    if (sosplit != null)
                    {
                        docgraph.Transactions.Current = newline = docgraph.Transactions.Search <SOLine.inventoryID, SOLine.subItemID, SOLine.siteID, SOLine.pOCreate>(demand.InventoryID, demand.SubItemID, demand.SiteID, false);
                        if (newline == null)
                        {
                            newline = PXCache <SOLine> .CreateCopy(docgraph.Transactions.Insert());

                            newline.InventoryID = demand.InventoryID;
                            newline.SubItemID   = demand.SubItemID;
                            newline.SiteID      = demand.SiteID;
                            newline.UOM         = demand.UOM;
                            newline.OrderQty    = 0m;

                            newline = docgraph.Transactions.Update(newline);
                        }

                        newsplit = new SOLineSplit();
                        newsplit.LotSerialNbr = sosplit.LotSerialNbr;
                        newsplit.IsAllocated  = true;
                        newsplit.IsMergeable  = false;
                        newsplit.SiteID       = demand.SiteID; //SiteID should be explicitly set because of PXFormula
                        newsplit.Qty          = demand.OrderQty;
                        newsplit.RefNoteID    = demand.RefNoteID;

                        //we have to delete previous allocation and reinsert it after the newsplit for transfer-order allocation to work properly
                        rp = PXCache <INItemPlan> .CreateCopy(demand);

                        cache.RaiseRowDeleted(demand);

                        newsplit = docgraph.splits.Insert(newsplit);

                        sosplit.SOOrderType    = newsplit.OrderType;
                        sosplit.SOOrderNbr     = newsplit.OrderNbr;
                        sosplit.SOLineNbr      = newsplit.LineNbr;
                        sosplit.SOSplitLineNbr = newsplit.SplitLineNbr;
                        docgraph.sodemand.Update(sosplit);

                        rp.SiteID       = sosplit.ToSiteID;
                        rp.PlanType     = INPlanConstants.Plan93;
                        rp.FixedSource  = null;
                        rp.SupplyPlanID = newsplit.PlanID;
                        cache.RaiseRowInserted(rp);
                        cache.SetStatus(rp, PXEntryStatus.Updated);
                    }
                    else
                    {
                        docgraph.Transactions.Current = newline = docgraph.Transactions.Search <SOLine.inventoryID, SOLine.subItemID, SOLine.siteID, SOLine.pOCreate>(demand.InventoryID, demand.SubItemID, demand.SourceSiteID, demand.VendorID != null);
                        if (newline == null)
                        {
                            newline = PXCache <SOLine> .CreateCopy(docgraph.Transactions.Insert());

                            newline.InventoryID = demand.InventoryID;
                            newline.SubItemID   = demand.SubItemID;
                            newline.SiteID      = demand.SourceSiteID;
                            newline.UOM         = demand.UOM;
                            newline.OrderQty    = 0m;
                            //newline.POCreate = (demand.VendorID != null);
                            newline.POCreate = (demand.FixedSource == INReplenishmentSource.TransferToPurchase);
                            newline.VendorID = demand.VendorID;
                            //newline.VendorLocationID = demand.VendorLocationID;

                            newline = docgraph.Transactions.Update(newline);
                        }

                        newsplit             = new SOLineSplit();
                        newsplit.IsAllocated = false;
                        newsplit.Qty         = demand.OrderQty;
                        //newsplit.POCreate = (demand.VendorID != null);
                        newsplit.POCreate = (demand.FixedSource == INReplenishmentSource.TransferToPurchase);
                        newsplit.VendorID = demand.VendorID;
                        //newsplit.VendorLocationID = demand.VendorLocationID;
                        newsplit = docgraph.splits.Insert(newsplit) ?? docgraph.splits.Current;

                        rp = PXCache <INItemPlan> .CreateCopy(demand);

                        cache.RaiseRowDeleted(demand);
                        rp.SiteID       = demand.SiteID;
                        rp.PlanType     = INPlanConstants.Plan94;
                        rp.FixedSource  = null;
                        rp.SupplyPlanID = newsplit.PlanID;
                        cache.RaiseRowInserted(rp);
                        cache.SetStatus(rp, PXEntryStatus.Updated);
                    }

                    if (newsplit.PlanID == null)
                    {
                        throw new PXRowPersistedException(typeof(SOLine).Name, newline, RQ.Messages.UnableToCreateSOOrders);
                    }

                    if (docgraph.Transactions.Cache.IsInsertedUpdatedDeleted)
                    {
                        using (PXTransactionScope scope = new PXTransactionScope())
                        {
                            docgraph.Save.Press();
                            if (demandPlanType == INPlanConstants.Plan90)
                            {
                                docgraph.Replenihment.Current = docgraph.Replenihment.Search <INReplenishmentOrder.noteID>(demand.RefNoteID);
                                if (docgraph.Replenihment.Current != null)
                                {
                                    INReplenishmentLine rLine =
                                        PXCache <INReplenishmentLine> .CreateCopy(docgraph.ReplenishmentLines.Insert(new INReplenishmentLine()));

                                    rLine.InventoryID       = newsplit.InventoryID;
                                    rLine.SubItemID         = newsplit.SubItemID;
                                    rLine.UOM               = newsplit.UOM;
                                    rLine.Qty               = newsplit.Qty;
                                    rLine.SOType            = newsplit.OrderType;
                                    rLine.SONbr             = docgraph.Document.Current.OrderNbr;
                                    rLine.SOLineNbr         = newsplit.LineNbr;
                                    rLine.SOSplitLineNbr    = newsplit.SplitLineNbr;
                                    rLine.SiteID            = demand.SourceSiteID;
                                    rLine.DestinationSiteID = demand.SiteID;
                                    rLine.PlanID            = demand.PlanID;
                                    docgraph.ReplenishmentLines.Update(rLine);
                                    INItemPlan plan = PXSelect <INItemPlan,
                                                                Where <INItemPlan.planID, Equal <Required <INItemPlan.planID> > > > .SelectWindowed(docgraph, 0, 1,
                                                                                                                                                    demand.SupplyPlanID);

                                    if (plan != null)
                                    {
                                        //plan.SupplyPlanID = rp.PlanID;
                                        rp.SupplyPlanID = plan.PlanID;
                                        cache.SetStatus(rp, PXEntryStatus.Updated);
                                    }

                                    docgraph.Save.Press();
                                }
                            }
                            scope.Complete();
                        }

                        PXProcessing <SOFixedDemand> .SetInfo(list.IndexOf(demand), PXMessages.LocalizeFormatNoPrefixNLA(Messages.TransferOrderCreated, docgraph.Document.Current.OrderNbr));

                        if (created.Find(docgraph.Document.Current) == null)
                        {
                            created.Add(docgraph.Document.Current);
                        }
                    }
                }
                catch (Exception e)
                {
                    PXProcessing <SOFixedDemand> .SetError(list.IndexOf(demand), e);
                }
            }
            if (created.Count == 1)
            {
                using (new PXTimeStampScope(null))
                {
                    docgraph.Clear();
                    docgraph.Document.Current = docgraph.Document.Search <POOrder.orderNbr>(created[0].OrderNbr, created[0].OrderType);
                    throw new PXRedirectRequiredException(docgraph, Messages.SOOrder);
                }
            }
        }
Example #3
0
        public static void SOCreateProc(List <SOFixedDemand> list, DateTime?PurchDate)
        {
            SOOrderEntry           docgraph = PXGraph.CreateInstance <SOOrderEntry>();
            SOSetup                sosetup  = docgraph.sosetup.Current;
            DocumentList <SOOrder> created  = new DocumentList <SOOrder>(docgraph);

            foreach (SOFixedDemand demand in list)
            {
                string OrderType =
                    sosetup.TransferOrderType ?? SOOrderTypeConstants.TransferOrder;

                string ErrorText = string.Empty;

                try
                {
                    if (demand.ReplenishmentSourceSiteID == null)
                    {
                        PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.MissingSourceSite);

                        continue;
                    }

                    if (demand.ReplenishmentSourceSiteID == demand.SiteID)
                    {
                        PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.EqualSourceDestinationSite);

                        continue;
                    }
                    SOOrder order;
                    POLine  poline = PXSelect <POLine, Where <POLine.planID, Equal <Required <POLine.planID> > > > .Select(docgraph, demand.PlanID);

                    if (poline != null)
                    {
                        order =
                            created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID, SOOrder.origPOType, SOOrder.origPONbr>(
                                OrderType, demand.SiteID, demand.ReplenishmentSourceSiteID, poline.OrderType, poline.OrderNbr);
                    }
                    else
                    {
                        order = created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID>(OrderType, demand.SiteID, demand.ReplenishmentSourceSiteID);
                    }

                    if (order == null)
                    {
                        order = new SOOrder();
                    }

                    if (order.OrderNbr == null)
                    {
                        docgraph.Clear();

                        INSite sourceSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(docgraph, demand.ReplenishmentSourceSiteID);

                        order.BranchID  = sourceSite.BranchID;
                        order.OrderType = OrderType;
                        order           = PXCache <SOOrder> .CreateCopy(docgraph.Document.Insert(order));

                        order.DefaultSiteID     = demand.ReplenishmentSourceSiteID;
                        order.DestinationSiteID = demand.SiteID;
                        order.Status            = SOOrderStatus.Open;
                        order.OrderDate         = PurchDate;
                        if (poline != null)
                        {
                            order.OrigPOType = poline.OrderType;
                            order.OrigPONbr  = poline.OrderNbr;
                        }
                        docgraph.Document.Update(order);
                    }
                    else if (docgraph.Document.Cache.ObjectsEqual(docgraph.Document.Current, order) == false)
                    {
                        docgraph.Document.Current = docgraph.Document.Search <SOOrder.orderNbr>(order.OrderNbr, order.OrderType);
                    }
                    SOLine line = new SOLine();
                    line = PXCache <SOLine> .CreateCopy(docgraph.Transactions.Insert(line));

                    line.InventoryID = demand.InventoryID;
                    line.SubItemID   = demand.SubItemID;
                    line.SiteID      = demand.ReplenishmentSourceSiteID;
                    line.UOM         = demand.UOM;
                    line.OrderQty    = demand.OrderQty;
                    if (poline != null)
                    {
                        line.OrigPOType    = poline.OrderType;
                        line.OrigPONbr     = poline.OrderNbr;
                        line.OrigPOLineNbr = poline.LineNbr;
                    }

                    line = docgraph.Transactions.Update(line);

                    if (line.PlanID == null)
                    {
                        throw new PXRowPersistedException(typeof(SOLine).Name, line, RQ.Messages.UnableToCreateSOOrders);
                    }

                    PXCache cache          = docgraph.Caches[typeof(INItemPlan)];
                    string  demandPlanType = demand.PlanType;
                    //cache.SetStatus(demand, PXEntryStatus.Updated);
                    //demand.SupplyPlanID = line.PlanID;
                    INItemPlan rp = PXCache <INItemPlan> .CreateCopy(demand);

                    cache.RaiseRowDeleted(demand);
                    rp.PlanType     = INPlanConstants.Plan94;
                    rp.FixedSource  = null;
                    rp.SupplyPlanID = line.PlanID;
                    cache.RaiseRowInserted(rp);
                    cache.SetStatus(rp, PXEntryStatus.Updated);

                    if (docgraph.Transactions.Cache.IsInsertedUpdatedDeleted)
                    {
                        using (PXTransactionScope scope = new PXTransactionScope())
                        {
                            docgraph.Save.Press();
                            if (demandPlanType == INPlanConstants.Plan90)
                            {
                                docgraph.Replenihment.Current = docgraph.Replenihment.Search <INReplenishmentOrder.noteID>(demand.RefNoteID);
                                if (docgraph.Replenihment.Current != null)
                                {
                                    INReplenishmentLine rLine =
                                        PXCache <INReplenishmentLine> .CreateCopy(docgraph.ReplenishmentLines.Insert(new INReplenishmentLine()));

                                    rLine.InventoryID       = line.InventoryID;
                                    rLine.SubItemID         = line.SubItemID;
                                    rLine.UOM               = line.UOM;
                                    rLine.Qty               = line.OrderQty;
                                    rLine.SOType            = line.OrderType;
                                    rLine.SONbr             = docgraph.Document.Current.OrderNbr;
                                    rLine.SOLineNbr         = line.LineNbr;
                                    rLine.SiteID            = demand.ReplenishmentSourceSiteID;
                                    rLine.DestinationSiteID = demand.SiteID;
                                    rLine.PlanID            = demand.PlanID;
                                    docgraph.ReplenishmentLines.Update(rLine);
                                    INItemPlan plan = PXSelect <INItemPlan,
                                                                Where <INItemPlan.planID, Equal <Required <INItemPlan.planID> > > > .SelectWindowed(docgraph, 0, 1,
                                                                                                                                                    demand.SupplyPlanID);

                                    if (plan != null)
                                    {
                                        //plan.SupplyPlanID = rp.PlanID;
                                        rp.SupplyPlanID = plan.PlanID;
                                        cache.SetStatus(rp, PXEntryStatus.Updated);
                                    }

                                    docgraph.Save.Press();
                                }
                            }
                            scope.Complete();
                        }

                        PXProcessing <SOFixedDemand> .SetInfo(list.IndexOf(demand), string.Format(Messages.TransferOrderCreated, docgraph.Document.Current.OrderNbr) + "\r\n" + ErrorText);


                        if (created.Find(docgraph.Document.Current) == null)
                        {
                            created.Add(docgraph.Document.Current);
                        }
                    }
                }
                catch (Exception e)
                {
                    PXProcessing <SOFixedDemand> .SetError(list.IndexOf(demand), e);
                }
            }
        }