Example #1
0
        public virtual void EnsureCanCancel(ARInvoice doc, bool isCorrection)
        {
            if (doc.DocType != ARDocType.Invoice)
            {
                throw new PXException(Messages.CantCancelDocType, doc.DocType);
            }

            if (doc.InstallmentCntr > 0)
            {
                throw new PXException(Messages.CantCancelMultipleInstallmentsInvoice);
            }

            var arAdjustGroups = PXSelectGroupBy <ARAdjust,
                                                  Where <ARAdjust.adjdDocType, Equal <Current <ARInvoice.docType> >, And <ARAdjust.adjdRefNbr, Equal <Current <ARInvoice.refNbr> > > >,
                                                  Aggregate <
                                                      GroupBy <ARAdjust.adjgDocType, GroupBy <ARAdjust.adjgRefNbr, GroupBy <ARAdjust.released,
                                                                                                                            Sum <ARAdjust.curyAdjdAmt> > > > > >
                                 .SelectMultiBound(Base, new[] { doc })
                                 .RowCast <ARAdjust>().ToList();

            if (arAdjustGroups.Any(a => a.Released == false))
            {
                throw new PXException(Messages.CantCancelInvoiceWithUnreleasedApplications);
            }

            var nonReversedCreditMemo = arAdjustGroups.FirstOrDefault(a => a.CuryAdjdAmt != 0m && a.AdjgDocType == ARDocType.CreditMemo);

            if (nonReversedCreditMemo != null)
            {
                throw new PXException(Messages.CantCancelInvoiceWithCM, nonReversedCreditMemo.AdjdRefNbr, nonReversedCreditMemo.AdjgRefNbr);
            }

            var nonReversedApplication = arAdjustGroups.FirstOrDefault(a => a.CuryAdjdAmt != 0m);

            if (nonReversedApplication != null)
            {
                throw new PXException(Messages.CantCancelInvoiceWithPayment, nonReversedApplication.AdjdRefNbr, nonReversedApplication.AdjgRefNbr);
            }

            ARTran directSale = PXSelectReadonly <ARTran,
                                                  Where <ARTran.tranType, Equal <Current <ARInvoice.docType> >, And <ARTran.refNbr, Equal <Current <ARInvoice.refNbr> >,
                                                                                                                     And <ARTran.invtMult, NotEqual <short0>, And <ARTran.lineType, Equal <SOLineType.inventory> > > > > >
                                .SelectSingleBound(Base, new[] { doc });

            if (directSale != null)
            {
                throw new PXException(Messages.CantCancelInvoiceWithDirectStockSales, doc.RefNbr);
            }

            SOOrderShipment notRequireShipment = PXSelectReadonly <SOOrderShipment,
                                                                   Where <SOOrderShipment.invoiceType, Equal <Current <ARInvoice.docType> >, And <SOOrderShipment.invoiceNbr, Equal <Current <ARInvoice.refNbr> >,
                                                                                                                                                  And <SOOrderShipment.shipmentNbr, Equal <Constants.noShipmentNbr> > > > >
                                                 .SelectSingleBound(Base, new[] { doc });

            if (notRequireShipment != null)
            {
                throw new PXException(Messages.CantCancelInvoiceWithOrdersNotRequiringShipments, doc.RefNbr);
            }
        }
        public virtual IEnumerable Action(
            PXAdapter adapter,
            [PXInt]
            [PXIntList(new int[] { 1, 2, 3, 4, 5 }, new string[] { "Create Shipment", "Apply Assignment Rules", "Create Invoice", "Post Invoice to IN", "Create Purchase Order" })]
            int?actionID,
            [PXDate]
            DateTime?shipDate,
            [PXSelector(typeof(INSite.siteCD))]
            string siteCD,
            [SOOperation.List]
            string operation,
            [PXString()]
            string ActionName,
            Func <PXAdapter, int?, DateTime?, string, string, string, IEnumerable> baseMtd)
        {
            switch (actionID)
            {
            case 1:
            {
                if (!string.IsNullOrEmpty(ActionName))
                {
                    PXAction action = Base.Actions[ActionName];

                    if (action != null)
                    {
                        Base.Save.Press();
                        List <object> result = new List <object>();
                        foreach (object data in action.Press(adapter))
                        {
                            result.Add(data);
                        }
                        return(result);
                    }
                }

                List <SOOrder> list = new List <SOOrder>();
                foreach (SOOrder order in adapter.Get <SOOrder>())
                {
                    list.Add(order);
                }

                if (shipDate != null)
                {
                    Base.soparamfilter.Current.ShipDate = shipDate;
                }

                if (Base.soparamfilter.Current.ShipDate == null)
                {
                    Base.soparamfilter.Current.ShipDate = Base.Accessinfo.BusinessDate;
                }

                if (siteCD != null)
                {
                    Base.soparamfilter.Cache.SetValueExt <SOParamFilter.siteID>(Base.soparamfilter.Current, siteCD);
                }

                if (!adapter.MassProcess)
                {
                    if (Base.soparamfilter.Current.SiteID == null)
                    {
                        Base.soparamfilter.Current.SiteID = GetPreferedSiteID();
                    }
                    if (adapter.ExternalCall)
                    {
                        Base.soparamfilter.AskExt(true);
                    }
                }
                if (Base.soparamfilter.Current.SiteID != null || adapter.MassProcess)
                {
                    try
                    {
                        Base.RecalculateExternalTaxesSync = true;
                        Base.Save.Press();
                    }
                    finally
                    {
                        Base.RecalculateExternalTaxesSync = false;
                    }
                    PXAutomation.RemovePersisted(Base, typeof(SOOrder), new List <object>(list));

                    SOParamFilter filter = Base.soparamfilter.Current;
                    PXLongOperation.StartOperation(Base, delegate()
                        {
                            bool anyfailed                    = false;
                            SOShipmentEntry docgraph          = PXGraph.CreateInstance <SOShipmentEntry>();
                            SOOrderEntry ordgraph             = PXGraph.CreateInstance <SOOrderEntry>();
                            DocumentList <SOShipment> created = new DocumentList <SOShipment>(docgraph);

                            //address AC-92776
                            for (int i = 0; i < list.Count; i++)
                            {
                                SOOrder order = list[i];
                                if (adapter.MassProcess)
                                {
                                    PXProcessing <SOOrder> .SetCurrentItem(order);
                                }

                                SOOrder ordercopy = (SOOrder)Base.Caches[typeof(SOOrder)].CreateCopy(order);
                                try
                                {
                                    if (operation == SOOperation.Issue)
                                    {
                                        ReviewWarehouseAvailability(ordgraph, order);
                                    }
                                }
                                catch (SOShipmentException ex)
                                {
                                    Base.Caches[typeof(SOOrder)].RestoreCopy(order, ordercopy);
                                    if (!adapter.MassProcess)
                                    {
                                        throw;
                                    }

                                    order.LastShipDate = filter.ShipDate;
                                    order.Status       = SOOrderStatus.Shipping; //Automation will set the order to back order since no shipments were created

                                    docgraph.Clear();

                                    var ordergraph = PXGraph.CreateInstance <SOOrderEntry>();
                                    ordergraph.Clear();

                                    ordergraph.Document.Cache.MarkUpdated(order);
                                    PXAutomation.CompleteSimple(ordergraph.Document.View);
                                    try
                                    {
                                        ordergraph.Save.Press();
                                        PXAutomation.RemovePersisted(ordergraph, order);

                                        PXTrace.WriteInformation(ex);
                                        PXProcessing <SOOrder> .SetWarning(ex);
                                    }
                                    catch (Exception inner)
                                    {
                                        Base.Caches[typeof(SOOrder)].RestoreCopy(order, ordercopy);
                                        PXProcessing <SOOrder> .SetError(inner);
                                        anyfailed = true;
                                    }
                                    continue;     //Stop there for this order
                                }
                                catch (Exception ex)
                                {
                                    if (!adapter.MassProcess)
                                    {
                                        throw;
                                    }
                                    PXProcessing <SOOrder> .SetError(ex);
                                    anyfailed = true;
                                    continue;
                                }

                                List <int?> sites = new List <int?>();

                                if (filter.SiteID != null)
                                {
                                    sites.Add(filter.SiteID);
                                }
                                else
                                {
                                    foreach (SOShipmentPlan plan in PXSelectGroupBy <SOShipmentPlan, Where <SOShipmentPlan.orderType, Equal <Current <SOOrder.orderType> >, And <SOShipmentPlan.orderNbr, Equal <Current <SOOrder.orderNbr> > > >, Aggregate <GroupBy <SOShipmentPlan.siteID> >, OrderBy <Asc <SOShipmentPlan.siteID> > > .SelectMultiBound(docgraph, new object[] { order }))
                                    {
                                        sites.Add(plan.SiteID);
                                    }
                                }

                                foreach (int?SiteID in sites)
                                {
                                    ordercopy = (SOOrder)Base.Caches[typeof(SOOrder)].CreateCopy(order);
                                    try
                                    {
                                        using (var ts = new PXTransactionScope())
                                        {
                                            PXTimeStampScope.SetRecordComesFirst(typeof(SOOrder), true);
                                            docgraph.CreateShipment(order, SiteID, filter.ShipDate, adapter.MassProcess, operation, created, adapter.QuickProcessFlow);
                                            ts.Complete();
                                        }

                                        if (adapter.MassProcess)
                                        {
                                            PXProcessing <SOOrder> .SetProcessed();
                                        }
                                    }
                                    catch (SOShipmentException ex)
                                    {
                                        Base.Caches[typeof(SOOrder)].RestoreCopy(order, ordercopy);
                                        if (!adapter.MassProcess)
                                        {
                                            throw;
                                        }
                                        order.LastSiteID   = SiteID;
                                        order.LastShipDate = filter.ShipDate;
                                        order.Status       = SOOrderStatus.Shipping;

                                        docgraph.Clear();

                                        var ordergraph = PXGraph.CreateInstance <SOOrderEntry>();
                                        ordergraph.Clear();

                                        ordergraph.Document.Cache.MarkUpdated(order);
                                        PXAutomation.CompleteSimple(ordergraph.Document.View);
                                        try
                                        {
                                            ordergraph.Save.Press();
                                            PXAutomation.RemovePersisted(ordergraph, order);

                                            PXTrace.WriteInformation(ex);
                                            PXProcessing <SOOrder> .SetWarning(ex);
                                        }
                                        catch (Exception inner)
                                        {
                                            Base.Caches[typeof(SOOrder)].RestoreCopy(order, ordercopy);
                                            PXProcessing <SOOrder> .SetError(inner);
                                            anyfailed = true;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Base.Caches[typeof(SOOrder)].RestoreCopy(order, ordercopy);
                                        docgraph.Clear();

                                        if (!adapter.MassProcess)
                                        {
                                            throw;
                                        }
                                        PXProcessing <SOOrder> .SetError(ex);
                                        anyfailed = true;
                                    }
                                }
                            }

                            if (adapter.AllowRedirect && !adapter.MassProcess && created.Count > 0)
                            {
                                using (new PXTimeStampScope(null))
                                {
                                    docgraph.Clear();
                                    docgraph.Document.Current = docgraph.Document.Search <SOShipment.shipmentNbr>(created[0].ShipmentNbr);
                                    throw new PXRedirectRequiredException(docgraph, "Shipment");
                                }
                            }

                            if (anyfailed)
                            {
                                throw new PXOperationCompletedWithErrorException(ErrorMessages.SeveralItemsFailed);
                            }
                        });
                }
                return(list);
            }

            default:
                return(baseMtd(adapter, actionID, shipDate, siteCD, operation, ActionName));
            }
        }