Ejemplo n.º 1
0
            public override void FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
            {
                SOLineSplit row = e.Row as SOLineSplit;

                if (row != null && !string.IsNullOrEmpty(row.PONbr))
                {
                    e.ReturnValue = GetEntityRowID(sender.Graph.Caches[typeof(POOrder)], new object[] { row.POType, row.PONbr });
                    e.ReturnState = PXLinkState.CreateInstance(e.ReturnState, typeof(POOrder), new object[] { row.POType, row.PONbr });
                }
                else if (row != null && !string.IsNullOrEmpty(row.ShipmentNbr))
                {
                    e.ReturnValue = GetEntityRowID(sender.Graph.Caches[typeof(SOShipment)], new object[] { row.ShipmentNbr });
                    e.ReturnState = PXLinkState.CreateInstance(e.ReturnState, typeof(SOShipment), new object[] { row.ShipmentNbr });
                }
                else if (row != null && !string.IsNullOrEmpty(row.SOOrderNbr))
                {
                    e.ReturnValue = GetEntityRowID(sender.Graph.Caches[typeof(SOOrder)], new object[] { row.SOOrderType, row.SOOrderNbr });
                    e.ReturnState = PXLinkState.CreateInstance(e.ReturnState, typeof(SOOrder), new object[] { row.SOOrderType, row.SOOrderNbr });
                }
                else if (row != null && !string.IsNullOrEmpty(row.POReceiptNbr))
                {
                    e.ReturnValue = GetEntityRowID(sender.Graph.Caches[typeof(POReceipt)], new object[] { row.POReceiptType, row.POReceiptNbr });
                    e.ReturnState = PXLinkState.CreateInstance(e.ReturnState, typeof(POReceipt), new object[] { row.POReceiptType, row.POReceiptNbr });
                }
                else
                {
                    base.FieldSelecting(sender, e);
                }
            }
        //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);
                }
            }
        }