Exemple #1
0
        protected virtual void ARInvoice_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
        {
            ARInvoiceRUTROT row    = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>((ARInvoice)e.Row);
            ARInvoiceRUTROT oldRow = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>((ARInvoice)e.OldRow);

            RRManager.RUTROTDeductibleUpdated(row, oldRow, Rutrots.SelectSingle());
        }
        public virtual void InvoiceCreated(ARInvoice invoice, SOOrder source, SOInvoiceEntry.InvoiceCreatedDelegate baseMethod)
        {
            baseMethod(invoice, source);

            SOOrderRUTROT orderRR = PXCache <SOOrder> .GetExtension <SOOrderRUTROT>(source);

            if (orderRR?.IsRUTROTDeductible == true && invoice != null)
            {
                ARInvoiceRUTROT invoiceRR = PXCache <ARInvoice> .GetExtension <ARInvoiceRUTROT>(invoice);

                Base.Document.SetValueExt <ARInvoiceRUTROT.isRUTROTDeductible>(invoice, true);
                Base.Document.Update(invoice);

                RUTROT rutrot = PXSelect <RUTROT,
                                          Where <RUTROT.docType, Equal <Required <SOOrder.orderType> >,
                                                 And <RUTROT.refNbr, Equal <Required <SOOrder.orderNbr> > > > > .Select(Base, source.OrderType, source.OrderNbr);

                rutrot = RUTROTHelper.CreateCopy(Base.Rutrots.Cache, rutrot, invoice.DocType, invoice.RefNbr);
                rutrot = Base.Rutrots.Update(rutrot);

                foreach (RUTROTDistribution rutrotDetail in PXSelect <RUTROTDistribution,
                                                                      Where <RUTROTDistribution.docType, Equal <Required <SOOrder.orderType> >,
                                                                             And <RUTROTDistribution.refNbr, Equal <Required <SOOrder.orderNbr> > > > > .Select(Base, source.OrderType, source.OrderNbr))
                {
                    RUTROTDistribution new_detail = (RUTROTDistribution)Base.RRDistribution.Cache.CreateCopy(rutrotDetail);
                    new_detail.RefNbr  = null;
                    new_detail.DocType = null;
                    Base.RRDistribution.Insert(new_detail);
                }
            }
        }
Exemple #3
0
        protected virtual void ARInvoice_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
        {
            ARInvoiceRUTROT doc = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>(e.Row as ARInvoice);

            if (doc == null)
            {
                return;
            }

            RRManager.Update((ARInvoice)e.Row);
            UpdateLinesControls(doc.IsRUTROTDeductible == true);
        }
Exemple #4
0
        protected virtual void ARInvoice_CuryID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
        {
            var row = (ARInvoice)e.Row;

            if (row == null)
            {
                return;
            }

            ARInvoiceRUTROT doc = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>(row);

            if (!RUTROTHelper.CurrenciesMatch(RUTROTHelper.GetBranchRUTROT(Base), doc))
            {
                doc.IsRUTROTDeductible = false;
            }
        }
Exemple #5
0
 public void RecalcFormulas(RUTROT rowRR, ARInvoiceRUTROT row)
 {
     if (row.IsRUTROTDeductible == true)
     {
         foreach (ARTran tran in Base.Transactions.Select())
         {
             if (PXCache <ARTran> .GetExtension <ARTranRUTROT>(tran).IsRUTROTDeductible == true)
             {
                 Base.Transactions.Cache.RaiseFieldUpdated <ARTranRUTROT.isRUTROTDeductible>(tran, false);                       //this is required for updating formula on ARTranRUTROT.CuryRUTROTAvailableAmt
                 Base.Transactions.Update(tran);
             }
         }
         PXUnboundFormulaAttribute.CalcAggregate <ARTranRUTROT.curyRUTROTTotal>(Base.Transactions.Cache, rowRR);
         PXFormulaAttribute.CalcAggregate <ARTranRUTROT.curyRUTROTAvailableAmt>(Base.Transactions.Cache, rowRR);
     }
 }
Exemple #6
0
        public static bool IsNeedBalancing(ARInvoiceEntry graph, ARInvoice invoice, string balanceOn)
        {
            if (!PXAccess.FeatureInstalled <FeaturesSet.rutRotDeduction>())
            {
                return(false);
            }

            BranchRUTROT branchRUTROT = GetBranchRUTROT(graph);

            if (invoice == null)
            {
                return(branchRUTROT?.BalanceOnProcess == balanceOn);
            }

            ARInvoiceRUTROT invoiceRUTROT = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>(invoice);

            return(invoice.DocType == ARDocType.Invoice && invoiceRUTROT?.IsRUTROTDeductible == true &&
                   branchRUTROT?.BalanceOnProcess == balanceOn);
        }
        private static void CreateMemo(ARInvoiceEntry graph, ARRegister doc, RUTROT rutrot, string docType, bool OnRelease = false)
        {
            DuplicateFilter filter = PXCache <DuplicateFilter> .CreateCopy(graph.duplicatefilter.Current);

            foreach (PXResult <ARInvoice, CurrencyInfo, Terms, Customer> res in ARInvoice_CurrencyInfo_Terms_Customer.Select(graph, (object)doc.DocType, doc.RefNbr, doc.CustomerID))
            {
                CurrencyInfo info = PXCache <CurrencyInfo> .CreateCopy((CurrencyInfo)res);

                info.CuryInfoID = null;
                info.IsReadOnly = false;
                info            = PXCache <CurrencyInfo> .CreateCopy(graph.currencyinfo.Insert(info));

                ARInvoice invoice = (ARInvoice)graph.Document.Cache.CreateInstance();

                if (docType == ARDocType.CreditMemo)
                {
                    invoice.DueDate     = null;
                    invoice.DiscDate    = null;
                    invoice.CustomerID  = doc.CustomerID;
                    invoice.ARAccountID = doc.ARAccountID;
                    invoice.ARSubID     = doc.ARSubID;
                }

                if (docType == ARInvoiceType.DebitMemo)
                {
                    invoice.DueDate  = ((ARInvoice)res).DueDate;
                    invoice.DiscDate = ((ARInvoice)res).DiscDate;

                    BranchRUTROT branchRUTROT = GetBranchRUTROT(graph);

                    invoice.CustomerID  = branchRUTROT.TaxAgencyAccountID;
                    invoice.ARAccountID = null;
                    invoice.ARSubID     = null;
                }

                ARInvoiceRUTROT invoiceRUTROT = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>(invoice);
                invoiceRUTROT.IsRUTROTDeductible = false;

                invoice.CuryInfoID = info.CuryInfoID;
                invoice.DocType    = docType;
                invoice.OrigModule = GL.BatchModule.AR;
                invoice.RefNbr     = null;
                invoice.OrigModule = GL.BatchModule.AR;
                invoice.DocDesc    = PXLocalizer.LocalizeFormat(RUTROTMessages.MemoDescription, doc.RefNbr);

                invoice.OpenDoc    = true;
                invoice.Released   = false;
                invoice.Hold       = false;
                invoice.Printed    = false;
                invoice.Emailed    = false;
                invoice.BatchNbr   = null;
                invoice.ScheduleID = null;
                invoice.Scheduled  = false;
                invoice.NoteID     = null;
                invoice.RefNoteID  = null;

                invoice.TermsID             = null;
                invoice.InstallmentCntr     = null;
                invoice.InstallmentNbr      = null;
                invoice.CuryOrigDiscAmt     = 0m;
                invoice.FinPeriodID         = doc.FinPeriodID;
                invoice.OrigDocDate         = invoice.DocDate;
                invoice.CuryLineTotal       = 0m;
                invoice.IsTaxPosted         = false;
                invoice.IsTaxValid          = false;
                invoice.CuryVatTaxableTotal = 0m;
                invoice.CuryVatExemptTotal  = 0m;
                invoice.StatementDate       = null;
                invoice.PendingPPD          = false;
                invoice.CustomerLocationID  = null;

                if (!string.IsNullOrEmpty(invoice.PaymentMethodID))
                {
                    CA.PaymentMethod pm = null;

                    if (invoice.CashAccountID.HasValue)
                    {
                        CA.PaymentMethodAccount pmAccount = null;
                        PXResult <CA.PaymentMethod, CA.PaymentMethodAccount> pmResult = (PXResult <CA.PaymentMethod, CA.PaymentMethodAccount>)
                                                                                        PXSelectJoin <CA.PaymentMethod,
                                                                                                      LeftJoin <
                                                                                                          CA.PaymentMethodAccount, On <CA.PaymentMethod.paymentMethodID, Equal <CA.PaymentMethodAccount.paymentMethodID> > >,
                                                                                                      Where <
                                                                                                          CA.PaymentMethod.paymentMethodID, Equal <Required <CA.PaymentMethod.paymentMethodID> >,
                                                                                                          And <CA.PaymentMethodAccount.cashAccountID, Equal <Required <CA.PaymentMethodAccount.cashAccountID> > > > > .
                                                                                        Select(graph, invoice.PaymentMethodID, invoice.CashAccountID);

                        pm        = pmResult;
                        pmAccount = pmResult;

                        if (pm == null || pm.UseForAR == false || pm.IsActive == false)
                        {
                            invoice.PaymentMethodID = null;
                            invoice.CashAccountID   = null;
                        }
                        else if (pmAccount == null || pmAccount.CashAccountID == null || pmAccount.UseForAR != true)
                        {
                            invoice.CashAccountID = null;
                        }
                    }
                    else
                    {
                        pm = PXSelect <CA.PaymentMethod,
                                       Where <CA.PaymentMethod.paymentMethodID, Equal <Required <CA.PaymentMethod.paymentMethodID> > > >
                             .Select(graph, invoice.PaymentMethodID);

                        if (pm == null || pm.UseForAR == false || pm.IsActive == false)
                        {
                            invoice.PaymentMethodID = null;
                            invoice.CashAccountID   = null;
                            invoice.PMInstanceID    = null;
                        }
                    }

                    if (invoice.PMInstanceID.HasValue)
                    {
                        CustomerPaymentMethod cpm = PXSelect <CustomerPaymentMethod,
                                                              Where <CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> > > > .
                                                    Select(graph, invoice.PMInstanceID);

                        if (string.IsNullOrEmpty(invoice.PaymentMethodID) || cpm == null || cpm.IsActive == false || cpm.PaymentMethodID != invoice.PaymentMethodID)
                        {
                            invoice.PMInstanceID = null;
                        }
                    }
                }
                else
                {
                    invoice.CashAccountID = null;
                    invoice.PMInstanceID  = null;
                }

                SalesPerson sp = (SalesPerson)PXSelectorAttribute.Select <ARInvoice.salesPersonID>(graph.Document.Cache, invoice);

                if (sp == null || sp.IsActive == false)
                {
                    invoice.SalesPersonID = null;
                }

                invoice = graph.Document.Insert(invoice);
            }

            TX.TaxAttribute.SetTaxCalc <ARTran.taxCategoryID>(graph.Transactions.Cache, null, TX.TaxCalc.ManualCalc);

            graph.FieldDefaulting.AddHandler <ARTran.salesPersonID>((sender, e) =>
            {
                e.NewValue = null;
                e.Cancel   = true;
            });

            decimal roundedTotalDistributedLinesAmt = 0m;

            foreach (ARTran srcTran in PXSelect <ARTran, Where <ARTran.tranType, Equal <Required <ARTran.tranType> >,
                                                                And <ARTran.refNbr, Equal <Required <ARTran.refNbr> > > > > .Select(graph, doc.DocType, doc.RefNbr))
            {
                ARTran tran = PXCache <ARTran> .CreateCopy(srcTran);

                ARTranRUTROT tranRR = RUTROTHelper.GetExtensionNullable <ARTran, ARTranRUTROT>(tran);

                if (tranRR.IsRUTROTDeductible != true)
                {
                    continue;
                }

                tran.TranType = graph.Document.Current.DocType;
                tran.RefNbr   = graph.Document.Current.RefNbr;
                string origDrCr = tran.DrCr;
                tran.DrCr                        = null;
                tran.Released                    = null;
                tran.CuryInfoID                  = null;
                tran.SOOrderNbr                  = null;
                tran.SOShipmentNbr               = null;
                tran.OrigInvoiceDate             = tran.TranDate;
                tran.NoteID                      = null;
                tran.ManualPrice                 = true;
                tran.CuryTranAmt                 = Math.Floor(tranRR.CuryRUTROTAvailableAmt ?? 0m);
                roundedTotalDistributedLinesAmt += tran.CuryTranAmt ?? 0m;
                tranRR.IsRUTROTDeductible        = false;

                if (!string.IsNullOrEmpty(tran.DeferredCode))
                {
                    DRSchedule schedule = PXSelect <DRSchedule,
                                                    Where <DRSchedule.module, Equal <BQLConstants.moduleAR>,
                                                           And <DRSchedule.docType, Equal <Required <DRSchedule.docType> >,
                                                                And <DRSchedule.refNbr, Equal <Required <DRSchedule.refNbr> >,
                                                                     And <DRSchedule.lineNbr, Equal <Required <DRSchedule.lineNbr> > > > > > > .
                                          Select(graph, doc.DocType, doc.RefNbr, tran.LineNbr);

                    if (schedule != null)
                    {
                        tran.DefScheduleID = schedule.ScheduleID;
                    }
                }

                SalesPerson sp = (SalesPerson)PXSelectorAttribute.Select <ARTran.salesPersonID>(graph.Transactions.Cache, tran);

                if (sp == null || sp.IsActive == false)
                {
                    tran.SalesPersonID = null;
                }

                ARTran insertedTran = graph.Transactions.Insert(tran);
                PXNoteAttribute.CopyNoteAndFiles(graph.Transactions.Cache, srcTran, graph.Transactions.Cache, insertedTran);

                insertedTran.ManualDisc = true;

                insertedTran.TaxCategoryID = null;
                graph.Transactions.Update(insertedTran);
            }

            decimal distributedFee = (rutrot.CuryDistributedAmt ?? 0m) - roundedTotalDistributedLinesAmt;

            if (distributedFee != 0m)
            {
                foreach (ARTran artran in graph.Transactions.Cache.Inserted)
                {
                    if (Math.Round(distributedFee) == 0m)
                    {
                        break;
                    }
                    if (artran.CuryTranAmt != 0m)
                    {
                        artran.CuryTranAmt += Math.Sign(distributedFee);
                        distributedFee     -= Math.Sign(distributedFee);
                    }

                    graph.Transactions.Update(artran);
                }
            }

            graph.Document.Current.CuryOrigDocAmt = graph.Document.Current.CuryDocBal;
            graph.Document.Cache.Update(graph.Document.Current);

            graph.RowInserting.AddHandler <ARSalesPerTran>((sender, e) => { e.Cancel = true; });

            foreach (ARSalesPerTran salespertran in PXSelect <ARSalesPerTran, Where <ARSalesPerTran.docType, Equal <Required <ARSalesPerTran.docType> >,
                                                                                     And <ARSalesPerTran.refNbr, Equal <Required <ARSalesPerTran.refNbr> > > > > .Select(graph, doc.DocType, doc.RefNbr))
            {
                ARSalesPerTran newtran = PXCache <ARSalesPerTran> .CreateCopy(salespertran);

                newtran.DocType         = graph.Document.Current.DocType;
                newtran.RefNbr          = graph.Document.Current.RefNbr;
                newtran.Released        = false;
                newtran.CuryInfoID      = null;
                newtran.CuryCommnblAmt *= -1m;
                newtran.CuryCommnAmt   *= -1m;

                SalesPerson sp = (SalesPerson)PXSelectorAttribute.Select <ARSalesPerTran.salespersonID>(graph.salesPerTrans.Cache, newtran);

                if (!(sp == null || sp.IsActive == false))
                {
                    graph.salesPerTrans.Update(newtran);
                }
            }

            var discountDetailsSet = PXSelect <ARInvoiceDiscountDetail,
                                               Where <ARInvoiceDiscountDetail.docType, Equal <Required <ARInvoice.docType> >,
                                                      And <ARInvoiceDiscountDetail.refNbr, Equal <Required <ARInvoice.refNbr> > > >,
                                               OrderBy <Asc <ARInvoiceDiscountDetail.docType,
                                                             Asc <ARInvoiceDiscountDetail.refNbr> > > >
                                     .Select(graph, doc.DocType, doc.RefNbr);

            foreach (ARInvoiceDiscountDetail discountDetail in discountDetailsSet)
            {
                ARInvoiceDiscountDetail newDiscountDetail = PXCache <ARInvoiceDiscountDetail> .CreateCopy(discountDetail);

                newDiscountDetail.DocType  = graph.Document.Current.DocType;
                newDiscountDetail.RefNbr   = graph.Document.Current.RefNbr;
                newDiscountDetail.IsManual = true;
                DiscountEngineProvider.GetEngineFor <ARTran, ARInvoiceDiscountDetail>().UpdateDiscountDetail(graph.ARDiscountDetails.Cache, graph.ARDiscountDetails, newDiscountDetail);
            }

            graph.Save.Press();

            if (docType == ARDocType.CreditMemo && !OnRelease)
            {
                CreateAdjustment(graph, doc, graph.Document.Current);
            }
        }