Exemple #1
0
        private void CalcBalances(object row, bool isCalcRGOL, bool DiscOnDiscDate)
        {
            ARAdjust adj = (ARAdjust)row;

            PXCache cache = Caches[typeof(ARAdjust)];

            ARPayment payment = (ARPayment)ARPayment_CustomerID_DocType_RefNbr.Select(adj.CustomerID, adj.AdjgDocType, adj.AdjgRefNbr);

            if (payment == null)
            {
                return;
            }

            PaymentEntry.CalcBalances <ARPayment, ARAdjust>(CurrencyInfo_CuryInfoID, adj.AdjgCuryInfoID, adj.AdjdCuryInfoID, payment, adj);

            if (DiscOnDiscDate)
            {
                PaymentEntry.CalcDiscount <ARPayment, ARAdjust>(adj.AdjgDocDate, payment, adj);
            }

            PaymentEntry.AdjustBalance <ARAdjust>(CurrencyInfo_CuryInfoID, adj);
            if (isCalcRGOL)
            {
                PaymentEntry.CalcRGOL <ARPayment, ARAdjust>(CurrencyInfo_CuryInfoID, payment, adj);
                adj.RGOLAmt = (bool)adj.ReverseGainLoss ? -1m * adj.RGOLAmt : adj.RGOLAmt;
            }
        }
Exemple #2
0
        private static void VoidProcess(ARDunningLetter doc)
        {
            ARDunningLetterMaint graph = PXGraph.CreateInstance <ARDunningLetterMaint>();

            graph.Document.Current    = doc;
            graph.Details.AllowUpdate = true;
            foreach (ARDunningLetterDetail detail in graph.Details.Select())
            {
                ARDunningLetterProcess.ARInvoiceWithDL invoice = PXSelect <ARDunningLetterProcess.ARInvoiceWithDL, Where <ARDunningLetterProcess.ARInvoiceWithDL.refNbr, Equal <Required <ARInvoice.refNbr> >, And <ARDunningLetterProcess.ARInvoiceWithDL.docType, Equal <Required <ARInvoice.docType> > > > > .Select(graph, detail.RefNbr, detail.DocType);

                if (invoice != null && invoice.DunningLetterLevel > detail.DunningLetterLevel)
                {
                    throw new PXException(Messages.DunningLetterHigherLevelExists);
                }
            }
            ARInvoice feeInvoice = PXSelect <ARInvoice, Where <ARInvoice.refNbr, Equal <Required <ARInvoice.refNbr> >, And <ARInvoice.docType, Equal <Required <ARInvoice.docType> > > > > .Select(graph, doc.FeeRefNbr, doc.FeeDocType);

            if (feeInvoice != null && feeInvoice.Voided == false)
            {
                ARInvoiceEntry invoiceGraph = PXGraph.CreateInstance <ARInvoiceEntry>();
                invoiceGraph.Document.Current = feeInvoice;
                if (feeInvoice.Released == false)
                {
                    invoiceGraph.Delete.Press();
                }
                else if (feeInvoice.Released == true && feeInvoice.OpenDoc == true)
                {
                    if (feeInvoice.CuryOrigDocAmt != feeInvoice.CuryDocBal)
                    {
                        throw new PXException(Messages.DunningLetterHavePaidFee);
                    }
                    invoiceGraph.reverseInvoice.Press();                     // replace by reverse & attach in 2017R2
                    invoiceGraph.Document.Current.Hold = false;
                    invoiceGraph.Document.Update(invoiceGraph.Document.Current);
                    invoiceGraph.release.Press();
                    ARPaymentEntry paymentGraph = PXGraph.CreateInstance <ARPaymentEntry>();
                    paymentGraph.Document.Current = PXSelect <ARPayment, Where <ARPayment.refNbr, Equal <Required <ARPayment.refNbr> >, And <ARPayment.docType, Equal <Required <ARPayment.docType> > > > > .Select(paymentGraph, invoiceGraph.Document.Current.RefNbr, invoiceGraph.Document.Current.DocType);

                    ARAdjust adj = new ARAdjust();
                    paymentGraph.Adjustments.Insert(adj);
                    adj.AdjdRefNbr  = feeInvoice.RefNbr;
                    adj.AdjdDocType = feeInvoice.DocType;
                    paymentGraph.Adjustments.Update(adj);
                    paymentGraph.release.Press();
                }
                else
                {
                    throw new PXException(Messages.DunningLetterHavePaidFee);
                }
            }
            foreach (ARDunningLetterDetail detail in graph.Details.Select())
            {
                detail.Voided = true;
                graph.Details.Update(detail);
            }

            doc.Voided = true;
            graph.Document.Update(doc);
            graph.Save.Press();
        }
        private static void ApplyPayment(ARPaymentEntry graph, ARAutoApplyParameters filter, ARPayment payment, List <ARInvoice> arInvoiceList, List <ARRegister> toRelease)
        {
            if (arInvoiceList.Any() == false)
            {
                return;
            }

            int invoiceIndex = 0;
            var paymentsViewIntoInvoiceList = new List <ARInvoice>(arInvoiceList);

            graph.Clear();
            graph.Document.Current = payment;

            bool adjustmentAdded = false;

            while (graph.Document.Current.CuryUnappliedBal > 0 && invoiceIndex < paymentsViewIntoInvoiceList.Count)
            {
                if (graph.Document.Current.CuryApplAmt == null)
                {
                    object curyapplamt = graph.Document.Cache.GetValueExt <ARPayment.curyApplAmt>(graph.Document.Current);
                    if (curyapplamt is PXFieldState)
                    {
                        curyapplamt = ((PXFieldState)curyapplamt).Value;
                    }
                    graph.Document.Current.CuryApplAmt = (decimal?)curyapplamt;
                }
                graph.Document.Current.AdjDate        = filter.ApplicationDate;
                graph.Document.Current.AdjFinPeriodID = filter.FinPeriodID;
                graph.Document.Cache.Update(graph.Document.Current);

                ARInvoice invoice = paymentsViewIntoInvoiceList[invoiceIndex];

                ARAdjust adj = new ARAdjust();
                adj.AdjdDocType = invoice.DocType;
                adj.AdjdRefNbr  = invoice.RefNbr;

                graph.AutoPaymentApp = true;
                adj = graph.Adjustments.Insert(adj);
                if (adj == null)
                {
                    invoiceIndex++;
                    continue;
                }
                adjustmentAdded = true;
                if (adj.CuryDocBal <= 0m)
                {
                    arInvoiceList.Remove(invoice);
                }

                invoiceIndex++;
            }
            if (adjustmentAdded)
            {
                graph.Save.Press();
                if (filter.ReleaseBatchWhenFinished == true)
                {
                    toRelease.Add(graph.Document.Current);
                }
            }
        }
Exemple #4
0
        public virtual void CreateWriteOff(ReasonCode reasonCode, string WOSubCD, DateTime?WODate, string WOFinPeriodID, ARRegister ardoc)
        {
            Int32?WOAccountID = reasonCode.AccountID;

            if (WOAccountID == null)
            {
                throw new ArgumentNullException("WOAccountID");
            }

            if (WOSubCD == null)
            {
                throw new ArgumentNullException("WOSubCD");
            }

            this.Clear();
            customer.Current = null;
            _CustomerID      = ardoc.CustomerID;

            ARPayment payment = new ARPayment();

            payment.BranchID = ardoc.BranchID;
            payment          = PXCache <ARPayment> .CreateCopy(this.Document.Insert(payment));

            payment.CustomerID         = ardoc.CustomerID;
            payment.CustomerLocationID = ardoc.CustomerLocationID;
            payment.AdjDate            = WODate;
            payment.AdjFinPeriodID     = WOFinPeriodID;
            payment.DocDate            = WODate;
            payment.FinPeriodID        = WOFinPeriodID;
            payment.CuryID             = ardoc.CuryID;
            payment.ARAccountID        = WOAccountID;
            payment.Hold    = false;
            payment.DocDesc = reasonCode.Descr;

            payment = this.Document.Update(payment);
            this.Document.Cache.SetValueExt <ARPayment.aRSubID>(payment, WOSubCD);

            ARAdjust adj = new ARAdjust();

            adj.AdjdDocType = ardoc.DocType;
            adj.AdjdRefNbr  = ardoc.RefNbr;

            adj = this.Adjustments.Insert(adj);

            Document.Current.CuryDocBal += (decimal)adj.CuryAdjgAmt;
            Document.Current.DocBal     += (decimal)adj.AdjAmt;

            Document.Current.CuryOrigDocAmt += (decimal)adj.CuryAdjgAmt;
            Document.Current.OrigDocAmt     += (decimal)adj.AdjAmt;

            ARReleaseProcess.UpdateARBalances(this, Document.Current, adj.AdjAmt);

            if (Document.Cache.GetStatus(Document.Current) == PXEntryStatus.Notchanged)
            {
                Document.Cache.SetStatus(Document.Current, PXEntryStatus.Updated);
            }

            this.Actions.PressSave();
        }
Exemple #5
0
        public virtual void CreateWriteOff(ReasonCode reasonCode, string WOSubCD, DateTime?WODate, string WOFinPeriodID, ARRegister ardoc)
        {
            Int32?WOAccountID = reasonCode.AccountID;

            if (WOAccountID == null)
            {
                throw new ArgumentNullException("WOAccountID");
            }

            if (WOSubCD == null)
            {
                throw new ArgumentNullException("WOSubCD");
            }


            this.Clear();
            customer.Current = null;
            _CustomerID      = ardoc.CustomerID;

            ARInvoice payment = new ARInvoice();

            payment.BranchID = ardoc.BranchID;
            payment          = PXCache <ARInvoice> .CreateCopy(this.Document.Insert(payment));

            payment.CustomerID         = ardoc.CustomerID;
            payment.CustomerLocationID = ardoc.CustomerLocationID;
            payment.DocDate            = WODate;
            payment.FinPeriodID        = WOFinPeriodID;
            payment.CuryID             = ardoc.CuryID;
            payment.ARAccountID        = WOAccountID;
            payment.Hold    = false;
            payment.AdjCntr = -1;
            payment.DocDesc = reasonCode.Descr;

            payment = this.Document.Update(payment);

            this.Document.Cache.SetValueExt <ARInvoice.aRSubID>(payment, WOSubCD);

            ARAddressAttribute.DefaultRecord <ARInvoice.billAddressID>(Document.Cache, payment);
            ARContactAttribute.DefaultRecord <ARInvoice.billContactID>(Document.Cache, payment);

            ARAdjust adj = new ARAdjust();

            adj.AdjgDocType        = ardoc.DocType;
            adj.AdjgRefNbr         = ardoc.RefNbr;
            adj.AdjNbr             = payment.AdjCntr;
            adj.AdjdCuryInfoID     = Document.Current.CuryInfoID;
            adj.AdjdOrigCuryInfoID = Document.Current.CuryInfoID;

            adj = this.Adjustments.Insert(adj);

            Document.Current.CuryDocBal += (decimal)adj.CuryAdjdAmt;
            Document.Current.DocBal     += (decimal)adj.AdjAmt;

            Document.Current.CuryOrigDocAmt += (decimal)adj.CuryAdjdAmt;
            Document.Current.OrigDocAmt     += (decimal)adj.AdjAmt;

            ARReleaseProcess.UpdateARBalances(this, Document.Current, adj.AdjAmt);

            Document.Cache.MarkUpdated(Document.Current);

            this.Actions.PressSave();
        }
        private static void ApplyPayment(ARPaymentEntry graph, ARAutoApplyParameters filter, ARPayment payment, List <ARInvoice> arInvoiceList, List <ARRegister> toRelease, out string warningRefNbr)
        {
            warningRefNbr = String.Empty;

            if (arInvoiceList.Any() == false)
            {
                return;
            }

            if (payment.DocDate > filter.ApplicationDate)
            {
                warningRefNbr = payment.RefNbr;
                PXProcessing <ARStatementCycle> .SetWarning(Messages.FuturePaymentWarning);

                return;
            }

            int invoiceIndex = 0;
            var paymentsViewIntoInvoiceList = new List <ARInvoice>(arInvoiceList);

            graph.Clear();
            graph.Document.Current = payment;

            bool adjustmentAdded = false;

            while (graph.Document.Current.CuryUnappliedBal > 0 && invoiceIndex < paymentsViewIntoInvoiceList.Count)
            {
                if (graph.Document.Current.CuryApplAmt == null)
                {
                    object curyapplamt = graph.Document.Cache.GetValueExt <ARPayment.curyApplAmt>(graph.Document.Current);
                    if (curyapplamt is PXFieldState)
                    {
                        curyapplamt = ((PXFieldState)curyapplamt).Value;
                    }
                    graph.Document.Current.CuryApplAmt = (decimal?)curyapplamt;
                }
                graph.Document.Current.AdjDate = filter.ApplicationDate;

                FinPeriodIDAttribute.SetPeriodsByMaster <ARPayment.adjFinPeriodID>(graph.Document.Cache, graph.Document.Current, filter.FinPeriodID);

                graph.Document.Cache.Adjust <PX.Objects.AR.AROpenPeriodAttribute>().For <ARPayment.adjFinPeriodID>(atr => atr.RedefaultOnDateChanged = false);
                graph.Document.Cache.Update(graph.Document.Current);

                ARInvoice invoice = paymentsViewIntoInvoiceList[invoiceIndex];

                var trans = invoice.PaymentsByLinesAllowed == true
                                        ? PXSelect <ARTran,
                                                    Where <ARTran.tranType, Equal <Required <ARTran.tranType> >,
                                                           And <ARTran.refNbr, Equal <Required <ARTran.refNbr> > > > > .Select(graph, invoice.DocType, invoice.RefNbr)
                                        : new PXResultset <ARTran>()
                {
                    null
                };

                foreach (ARTran tran in trans)
                {
                    ARAdjust adj = new ARAdjust();
                    adj.AdjdDocType = invoice.DocType;
                    adj.AdjdRefNbr  = invoice.RefNbr;
                    adj.AdjdLineNbr = tran?.LineNbr ?? 0;

                    graph.AutoPaymentApp = true;
                    adj = graph.Adjustments.Insert(adj);

                    if (adj != null)
                    {
                        adjustmentAdded = true;
                        if (adj.CuryDocBal <= 0m)
                        {
                            arInvoiceList.Remove(invoice);
                        }
                    }
                }

                invoiceIndex++;
            }
            if (adjustmentAdded)
            {
                graph.Save.Press();
                if (filter.ReleaseBatchWhenFinished == true)
                {
                    toRelease.Add(graph.Document.Current);
                }
            }
        }
Exemple #7
0
        private static void AddTaxesAndApplications(ARInvoiceEntry ie, PendingPPDCreditMemoApp doc, Customer customer, ARInvoice invoice)
        {
            ARTaxTran artaxMax               = null;
            decimal?  TaxTotal               = 0m;
            decimal?  InclusiveTotal         = 0m;
            decimal?  DiscountedTaxableTotal = 0m;
            decimal?  DiscountedPriceTotal   = 0m;
            decimal   CashDiscPercent        = (decimal)(doc.CuryAdjdPPDAmt / doc.InvCuryOrigDocAmt);

            PXResultset <ARTaxTran> taxes = PXSelectJoin <ARTaxTran,
                                                          InnerJoin <Tax, On <Tax.taxID, Equal <ARTaxTran.taxID> > >,
                                                          Where <ARTaxTran.module, Equal <BatchModule.moduleAR>,
                                                                 And <ARTaxTran.tranType, Equal <Required <ARTaxTran.tranType> >,
                                                                      And <ARTaxTran.refNbr, Equal <Required <ARTaxTran.refNbr> > > > > > .Select(ie, doc.AdjdDocType, doc.AdjdRefNbr);

            //add taxes
            foreach (PXResult <ARTaxTran, Tax> res in taxes)
            {
                Tax       tax   = res;
                ARTaxTran artax = PXCache <ARTaxTran> .CreateCopy(res);

                ARTaxTran artaxNew = ie.Taxes.Search <ARTaxTran.taxID>(artax.TaxID);

                if (artaxNew == null)
                {
                    artax.TranType    = null;
                    artax.RefNbr      = null;
                    artax.TaxPeriodID = null;
                    artax.Released    = false;
                    artax.Voided      = false;
                    artax.CuryInfoID  = invoice.CuryInfoID;

                    TaxBaseAttribute.SetTaxCalc <ARTran.taxCategoryID, ARTaxAttribute>(ie.Transactions.Cache, null, TaxCalc.NoCalc);
                    artaxNew = ie.Taxes.Insert(artax);

                    artaxNew.CuryTaxableAmt = 0m;
                    artaxNew.CuryTaxAmt     = 0m;
                    artaxNew.TaxRate        = artax.TaxRate;
                }

                bool isTaxable = CalculateDiscountedTaxes(ie.Taxes.Cache, artax, CashDiscPercent);
                DiscountedPriceTotal += artax.CuryDiscountedPrice;

                decimal?CuryTaxableAmt = artax.CuryTaxableAmt - artax.CuryDiscountedTaxableAmt;
                decimal?CuryTaxAmt     = artax.CuryTaxAmt - artax.CuryDiscountedPrice;

                artaxNew.CuryTaxableAmt += CuryTaxableAmt;
                artaxNew.CuryTaxAmt     += CuryTaxAmt;

                TaxBaseAttribute.SetTaxCalc <ARTran.taxCategoryID, ARTaxAttribute>(ie.Transactions.Cache, null, TaxCalc.ManualCalc);
                ie.Taxes.Update(artaxNew);

                if (isTaxable)
                {
                    DiscountedTaxableTotal += artax.CuryDiscountedTaxableAmt;
                    if (artaxMax == null || artaxNew.CuryTaxableAmt > artaxMax.CuryTaxableAmt)
                    {
                        artaxMax = artaxNew;
                    }
                }

                if (tax.TaxCalcLevel == CSTaxCalcLevel.Inclusive)
                {
                    InclusiveTotal += CuryTaxAmt;
                }
                else
                {
                    TaxTotal += CuryTaxAmt;
                }
            }

            //adjust taxes according to parent ARInvoice
            decimal?DiscountedInvTotal = doc.InvCuryOrigDocAmt - doc.InvCuryOrigDiscAmt;
            decimal?DiscountedDocTotal = DiscountedTaxableTotal + DiscountedPriceTotal;

            if (doc.InvCuryOrigDiscAmt == doc.CuryAdjdPPDAmt &&
                artaxMax != null &&
                doc.InvCuryVatTaxableTotal + doc.InvCuryTaxTotal == doc.InvCuryOrigDocAmt &&
                DiscountedDocTotal != DiscountedInvTotal)
            {
                artaxMax.CuryTaxableAmt += DiscountedDocTotal - DiscountedInvTotal;
                TaxBaseAttribute.SetTaxCalc <ARTran.taxCategoryID, ARTaxAttribute>(ie.Transactions.Cache, null, TaxCalc.ManualCalc);
                ie.Taxes.Update(artaxMax);
            }

            //add document details
            ARTran tranNew = ie.Transactions.Insert();

            tranNew.BranchID = doc.AdjdBranchID;
            using (new PXLocaleScope(customer.LocaleName))
                tranNew.TranDesc = string.Format("{0} {1}, {2} {3}", PXMessages.LocalizeNoPrefix(DocTypes[doc.AdjdDocType]), doc.AdjdRefNbr, PXMessages.LocalizeNoPrefix(Messages.Payment), doc.AdjgRefNbr);
            tranNew.CuryExtPrice         = doc.CuryAdjdPPDAmt - TaxTotal;
            tranNew.CuryTaxableAmt       = tranNew.CuryExtPrice - InclusiveTotal;
            tranNew.CuryTaxAmt           = TaxTotal + InclusiveTotal;
            tranNew.AccountID            = customer.DiscTakenAcctID;
            tranNew.SubID                = customer.DiscTakenSubID;
            tranNew.TaxCategoryID        = null;
            tranNew.IsFree               = true;
            tranNew.ManualDisc           = true;
            tranNew.CuryDiscAmt          = 0m;
            tranNew.DiscPct              = 0m;
            tranNew.GroupDiscountRate    = 1m;
            tranNew.DocumentDiscountRate = 1m;

            if (taxes.Count == 1)
            {
                ARTaxTran artax  = taxes[0];
                ARTran    artran = PXSelectJoin <ARTran,
                                                 InnerJoin <ARTax, On <ARTax.tranType, Equal <ARTran.tranType>,
                                                                       And <ARTax.refNbr, Equal <ARTran.refNbr>,
                                                                            And <ARTax.lineNbr, Equal <ARTran.lineNbr> > > > >,
                                                 Where <ARTax.tranType, Equal <Required <ARTax.tranType> >,
                                                        And <ARTax.refNbr, Equal <Required <ARTax.refNbr> >,
                                                             And <ARTax.taxID, Equal <Required <ARTax.taxID> > > > >,
                                                 OrderBy <Asc <ARTran.lineNbr> > > .SelectSingleBound(ie, null, artax.TranType, artax.RefNbr, artax.TaxID);

                if (artran != null)
                {
                    tranNew.TaxCategoryID = artran.TaxCategoryID;
                }
            }

            ie.Transactions.Update(tranNew);

            //add applications
            ARAdjust adj = new ARAdjust();

            adj.AdjdDocType = doc.AdjdDocType;
            adj.AdjdRefNbr  = doc.AdjdRefNbr;
            adj             = ie.Adjustments_1.Insert(adj);

            adj.CuryAdjgAmt = doc.InvCuryDocBal;
            ie.Adjustments_1.Update(adj);
        }
Exemple #8
0
        public static void ProcessDoc(ARPaymentEntry graph, ARStatementCycle cycle, ARAutoApplyParameters filter)
        {
            List <ARRegister> toRelease = new List <ARRegister>();

            // Build Invoices List
            foreach (Customer customer in
                     PXSelect <Customer,
                               Where <Customer.statementCycleId, Equal <Required <Customer.statementCycleId> >,
                                      And <Match <Required <AccessInfo.userName> > > > >
                     .Select(graph, cycle.StatementCycleId, graph.Accessinfo.UserName))
            {
                List <ARInvoice> arInvoiceList = new List <ARInvoice>();

                foreach (ARInvoice invoice in
                         PXSelect <ARInvoice, Where <ARInvoice.customerID, Equal <Required <ARInvoice.customerID> >,
                                                     And <ARInvoice.released, Equal <boolTrue>,
                                                          And <ARInvoice.openDoc, Equal <boolTrue>,
                                                               And <Where <ARInvoice.docType, Equal <Required <ARInvoice.docType> >,
                                                                           Or <ARInvoice.docType, Equal <Required <ARInvoice.docType> >,
                                                                               Or <ARInvoice.docType, Equal <Required <ARInvoice.docType> > > > > > > > >,
                                   OrderBy <Asc <ARInvoice.dueDate> > >
                         .Select(graph,
                                 customer.BAccountID,
                                 ARDocType.Invoice,
                                 ARDocType.FinCharge,
                                 filter.ApplyCreditMemos == true ? ARDocType.DebitMemo : ARDocType.Invoice))
                {
                    arInvoiceList.Add(invoice);
                }

                arInvoiceList.Sort(new Comparison <ARInvoice>(delegate(ARInvoice a, ARInvoice b)
                {
                    if ((bool)graph.arsetup.Current.FinChargeFirst)
                    {
                        int aSortOrder = (a.DocType == ARDocType.FinCharge ? 0 : 1);
                        int bSortOrder = (b.DocType == ARDocType.FinCharge ? 0 : 1);
                        int ret        = ((IComparable)aSortOrder).CompareTo(bSortOrder);
                        if (ret != 0)
                        {
                            return(ret);
                        }
                    }

                    {
                        object aDueDate = a.DueDate;
                        object bDueDate = b.DueDate;
                        int ret         = ((IComparable)aDueDate).CompareTo(bDueDate);

                        return(ret);
                    }
                }
                                                              ));


                if (arInvoiceList.Count > 0)
                {
                    int invoiceIndex = 0;

                    // this foreach gets all payments [and CreditMemos if applyCreditMemos = true] sorted by docDate
                    foreach (ARPayment payment in
                             PXSelect <ARPayment,
                                       Where <ARPayment.customerID, Equal <Required <ARPayment.customerID> >,
                                              And <ARPayment.released, Equal <boolTrue>,
                                                   And <ARPayment.openDoc, Equal <boolTrue>,
                                                        And <Where <ARPayment.docType, Equal <Required <ARPayment.docType> >,
                                                                    Or <ARPayment.docType, Equal <Required <ARPayment.docType> > > > > > > >,
                                       OrderBy <Asc <ARPayment.docDate> > >
                             .Select(graph,
                                     customer.BAccountID,
                                     ARDocType.Payment,
                                     filter.ApplyCreditMemos == true ? ARDocType.CreditMemo : ARDocType.Payment))
                    {
                        graph.Clear();
                        graph.Document.Current = payment;

                        if (graph.Adjustments.Select().Count == 0)
                        {
                            bool adjustmentAdded = false;
                            while (graph.Document.Current.CuryUnappliedBal > 0)
                            {
                                if (graph.Document.Current.CuryApplAmt == null)
                                {
                                    object curyapplamt = graph.Document.Cache.GetValueExt <ARPayment.curyApplAmt>(graph.Document.Current);
                                    if (curyapplamt is PXFieldState)
                                    {
                                        curyapplamt = ((PXFieldState)curyapplamt).Value;
                                    }
                                    graph.Document.Current.CuryApplAmt = (decimal?)curyapplamt;
                                }
                                graph.Document.Current.AdjDate        = filter.ApplicationDate;
                                graph.Document.Current.AdjFinPeriodID = filter.FinPeriodID;
                                graph.Document.Cache.Update(graph.Document.Current);

                                ARInvoice invoice = arInvoiceList[invoiceIndex];

                                ARAdjust adj = new ARAdjust();
                                adj.AdjdDocType = invoice.DocType;
                                adj.AdjdRefNbr  = invoice.RefNbr;

                                graph.AutoPaymentApp = true;
                                adj = graph.Adjustments.Insert(adj);
                                if (adj == null)
                                {
                                    throw new PXException(PXMessages.LocalizeFormat(Messages.ErrorAutoApply, invoice.DocType, invoice.RefNbr, payment.DocType, payment.RefNbr));
                                }
                                adjustmentAdded = true;
                                if (adj.CuryDocBal <= 0m)
                                {
                                    invoiceIndex++;
                                }
                                if (invoiceIndex >= arInvoiceList.Count)
                                {
                                    break;
                                }
                            }
                            if (adjustmentAdded)
                            {
                                graph.Save.Press();
                                if (filter.ReleaseBatchWhenFinished == true)
                                {
                                    toRelease.Add(graph.Document.Current);
                                }
                            }
                        }
                        if (invoiceIndex >= arInvoiceList.Count)
                        {
                            break;
                        }
                    }
                }
            }

            if (toRelease.Count > 0)
            {
                ARDocumentRelease.ReleaseDoc(toRelease, false);
            }
        }
		public override IEnumerable adjustments()
		{
			CurrencyInfo inv_info = PXSelect<CurrencyInfo, Where<CurrencyInfo.curyInfoID, Equal<Current<ARInvoice.curyInfoID>>>>.Select(this);

			foreach (PXResult<ARAdjust, ARPayment, CurrencyInfo> res in Adjustments_Raw.Select())
			{
				ARPayment payment = PXCache<ARPayment>.CreateCopy(res);
				ARAdjust adj = (ARAdjust)res;
				CurrencyInfo pay_info = (CurrencyInfo)res;

				ARAdjust other = PXSelectGroupBy<ARAdjust, Where<ARAdjust.adjgDocType, Equal<Required<ARAdjust.adjgDocType>>, And<ARAdjust.adjgRefNbr, Equal<Required<ARAdjust.adjgRefNbr>>, And<ARAdjust.released, Equal<False>, And<Where<ARAdjust.adjdDocType, NotEqual<Required<ARAdjust.adjdDocType>>, Or<ARAdjust.adjdRefNbr, NotEqual<Required<ARAdjust.adjdRefNbr>>>>>>>>, Aggregate<GroupBy<ARAdjust.adjgDocType, GroupBy<ARAdjust.adjgRefNbr, Sum<ARAdjust.curyAdjgAmt, Sum<ARAdjust.adjAmt>>>>>>.Select(this, adj.AdjgDocType, adj.AdjgRefNbr, adj.AdjdDocType, adj.AdjdRefNbr);
				if (other != null && other.AdjdRefNbr != null)
				{
					payment.CuryDocBal -= other.CuryAdjgAmt;
					payment.DocBal -= other.AdjAmt;
				}

				decimal CuryDocBal;
				if (string.Equals(pay_info.CuryID, inv_info.CuryID))
				{
					CuryDocBal = (decimal)payment.CuryDocBal;
				}
				else
				{
					PXDBCurrencyAttribute.CuryConvCury(Adjustments.Cache, inv_info, (decimal)payment.DocBal, out CuryDocBal);
				}

				if (adj.Released == false)
				{
					if (adj.CuryAdjdAmt > CuryDocBal)
					{
						//if reconsidered need to calc RGOL
						adj.CuryDocBal = CuryDocBal;
						adj.CuryAdjdAmt = 0m;
					}
					else
					{
						adj.CuryDocBal = CuryDocBal - adj.CuryAdjdAmt;
					}
				}
				yield return res;
			}

			if (Document.Current != null && (Document.Current.DocType == ARDocType.Invoice || Document.Current.DocType == ARDocType.DebitMemo) && Document.Current.Released == false)
			{
				using (ReadOnlyScope rs = new ReadOnlyScope(Adjustments.Cache, Document.Cache, arbalances.Cache))
				{
					//same as ARInvoiceEntry but without released constraint and with hold constraint
					foreach (PXResult<AR.ARPayment, CurrencyInfo, ARAdjust> res in PXSelectReadonly2<ARPayment, 
						InnerJoin<CurrencyInfo, On<CurrencyInfo.curyInfoID, Equal<ARPayment.curyInfoID>>, 
						LeftJoin<ARAdjust, 
							On<ARAdjust.adjgDocType, Equal<ARPayment.docType>, 
							And<ARAdjust.adjgRefNbr, Equal<ARPayment.refNbr>, 
							And<ARAdjust.adjNbr, Equal<ARPayment.lineCntr>, 
							And<ARAdjust.released, Equal<False>, 
							And<ARAdjust.hold, Equal<False>, 
							And<ARAdjust.voided, Equal<False>, 
							And<Where<ARAdjust.adjdDocType, NotEqual<Current<ARInvoice.docType>>, 
								Or<ARAdjust.adjdRefNbr, NotEqual<Current<ARInvoice.refNbr>>>>>>>>>>>>>, 
						Where<ARPayment.customerID, Equal<Current<ARInvoice.customerID>>, 
							And2<Where<ARPayment.docType, Equal<ARDocType.payment>, 
								Or<ARPayment.docType, Equal<ARDocType.prepayment>, 
								Or<ARPayment.docType, Equal<ARDocType.creditMemo>>>>, 
							And<ARPayment.docDate, LessEqual<Current<ARInvoice.docDate>>, 
							And<ARPayment.finPeriodID, LessEqual<Current<ARInvoice.finPeriodID>>, 
							And<ARPayment.openDoc, Equal<boolTrue>, 
							And<ARAdjust.adjdRefNbr, IsNull>>>>>>>.Select(this))
					{
						ARPayment payment = PXCache<ARPayment>.CreateCopy(res);
						ARAdjust adj = new ARAdjust();
						CurrencyInfo pay_info = (CurrencyInfo)res;

						adj.CustomerID = Document.Current.CustomerID;
						adj.AdjdDocType = Document.Current.DocType;
						adj.AdjdRefNbr = Document.Current.RefNbr;
						adj.AdjdBranchID = Document.Current.BranchID;
						adj.AdjgDocType = payment.DocType;
						adj.AdjgRefNbr = payment.RefNbr;
						adj.AdjgBranchID = payment.BranchID;
						adj.AdjNbr = payment.LineCntr;

						ARAdjust other = PXSelectGroupBy<ARAdjust, Where<ARAdjust.adjgDocType, Equal<Required<ARAdjust.adjgDocType>>, And<ARAdjust.adjgRefNbr, Equal<Required<ARAdjust.adjgRefNbr>>, And<ARAdjust.released, Equal<False>, And<Where<ARAdjust.adjdDocType, NotEqual<Required<ARAdjust.adjdDocType>>, Or<ARAdjust.adjdRefNbr, NotEqual<Required<ARAdjust.adjdRefNbr>>>>>>>>, Aggregate<GroupBy<ARAdjust.adjgDocType, GroupBy<ARAdjust.adjgRefNbr, Sum<ARAdjust.curyAdjgAmt, Sum<ARAdjust.adjAmt>>>>>>.Select(this, adj.AdjgDocType, adj.AdjgRefNbr, adj.AdjdDocType, adj.AdjdRefNbr);
						if (other != null && other.AdjdRefNbr != null)
						{
							payment.CuryDocBal -= other.CuryAdjgAmt;
							payment.DocBal -= other.AdjAmt;
						}

						if (Adjustments.Cache.Locate(adj) == null)
						{
							adj.AdjgCuryInfoID = payment.CuryInfoID;
							adj.AdjdOrigCuryInfoID = Document.Current.CuryInfoID;
							//if LE constraint is removed from payment selection this must be reconsidered
							adj.AdjdCuryInfoID = Document.Current.CuryInfoID;

							decimal CuryDocBal;
							if (string.Equals(pay_info.CuryID, inv_info.CuryID))
							{
								CuryDocBal = (decimal)payment.CuryDocBal;
							}
							else
							{
								PXDBCurrencyAttribute.CuryConvCury(Adjustments.Cache, inv_info, (decimal)payment.DocBal, out CuryDocBal);
							}
							adj.CuryDocBal = CuryDocBal;

							yield return new PXResult<ARAdjust, AR.ARPayment>(Adjustments.Insert(adj), payment);
						}
					}
				}
			}
		}