public static void ReleaseRetainage(ARInvoiceEntry graph, ARInvoiceExt invoice, ARRetainageFilter filter, bool isAutoRelease)
        {
            graph.Clear(PXClearOption.ClearAll);
            PXUIFieldAttribute.SetError(graph.Document.Cache, null, null, null);

            RetainageOptions retainageOptions = new RetainageOptions();

            retainageOptions.DocDate          = filter.DocDate;
            retainageOptions.FinPeriodID      = filter.FinPeriodID;
            retainageOptions.CuryRetainageAmt = invoice.CuryRetainageReleasedAmt;

            ARInvoiceEntryRetainage retainageExt = graph.GetExtension <ARInvoiceEntryRetainage>();

            ARInvoice retainageInvoice = retainageExt.ReleaseRetainageProc(invoice, retainageOptions, isAutoRelease);

            graph.Save.Press();

            if (isAutoRelease)
            {
                List <ARRegister> toRelease = new List <ARRegister>()
                {
                    retainageInvoice
                };
                using (new PXTimeStampScope(null))
                {
                    ARDocumentRelease.ReleaseDoc(toRelease, true);
                }
            }
        }
Ejemplo n.º 2
0
        public static void CreatePPDCreditMemos(PXCache cache, ARPPDCreditMemoParameters filter, ARSetup setup, List <PendingPPDCreditMemoApp> docs)
        {
            int  i      = 0;
            bool failed = false;

            List <ARRegister> toRelease = new List <ARRegister>();
            ARInvoiceEntry    ie        = PXGraph.CreateInstance <ARInvoiceEntry>();

            ie.ARSetup.Current = setup;

            if (filter.GenerateOnePerCustomer == true)
            {
                if (filter.CreditMemoDate == null)
                {
                    throw new PXSetPropertyException(CR.Messages.EmptyValueErrorFormat,
                                                     PXUIFieldAttribute.GetDisplayName <ARPPDCreditMemoParameters.creditMemoDate>(cache));
                }

                if (filter.FinPeriodID == null)
                {
                    throw new PXSetPropertyException(CR.Messages.EmptyValueErrorFormat,
                                                     PXUIFieldAttribute.GetDisplayName <ARPPDCreditMemoParameters.finPeriodID>(cache));
                }

                Dictionary <PPDCreditMemoKey, List <PendingPPDCreditMemoApp> > dict = new Dictionary <PPDCreditMemoKey, List <PendingPPDCreditMemoApp> >();
                foreach (PendingPPDCreditMemoApp doc in docs)
                {
                    CurrencyInfo info = PXSelect <CurrencyInfo, Where <CurrencyInfo.curyInfoID, Equal <Required <CurrencyInfo.curyInfoID> > > > .Select(ie, doc.InvCuryInfoID);

                    PPDCreditMemoKey key = new PPDCreditMemoKey();
                    doc.Index              = i++;
                    key.BranchID           = doc.AdjdBranchID;
                    key.CustomerID         = doc.AdjdCustomerID;
                    key.CustomerLocationID = doc.InvCustomerLocationID;
                    key.CuryID             = info.CuryID;
                    key.CuryRate           = info.CuryRate;
                    key.ARAccountID        = doc.AdjdARAcct;
                    key.ARSubID            = doc.AdjdARSub;
                    key.TaxZoneID          = doc.InvTaxZoneID;

                    List <PendingPPDCreditMemoApp> list;
                    if (!dict.TryGetValue(key, out list))
                    {
                        dict[key] = list = new List <PendingPPDCreditMemoApp>();
                    }

                    list.Add(doc);
                }

                foreach (List <PendingPPDCreditMemoApp> list in dict.Values)
                {
                    ARInvoice invoice = CreatePPDCreditMemo(ie, filter, list);
                    if (invoice != null)
                    {
                        toRelease.Add(invoice);
                    }
                    else
                    {
                        failed = true;
                    }
                }
            }
            else
            {
                foreach (PendingPPDCreditMemoApp doc in docs)
                {
                    List <PendingPPDCreditMemoApp> list = new List <PendingPPDCreditMemoApp>(1);
                    doc.Index = i++;
                    list.Add(doc);

                    ARInvoice invoice = CreatePPDCreditMemo(ie, filter, list);
                    if (invoice != null)
                    {
                        toRelease.Add(invoice);
                    }
                    else
                    {
                        failed = true;
                    }
                }
            }

            if (setup.AutoReleasePPDCreditMemo == true && toRelease.Count > 0)
            {
                using (new PXTimeStampScope(null))
                {
                    ARDocumentRelease.ReleaseDoc(toRelease, true);
                }
            }

            if (failed)
            {
                throw new PXException(GL.Messages.DocumentsNotReleased);
            }
        }
Ejemplo n.º 3
0
        public static void CreatePayments(List <ARRegister> list, ARWriteOffFilter filter)
        {
            if (string.IsNullOrEmpty(filter.ReasonCode))
            {
                throw new PXException(Messages.ReasonCodeIsRequired);
            }

            bool failed = false;

            IARWriteOffEntry pe = null;

            if (filter.WOType == ARDocType.SmallBalanceWO)
            {
                pe = PXGraph.CreateInstance <ARSmallBalanceWriteOffEntry>();
            }
            else
            {
                pe = PXGraph.CreateInstance <ARSmallCreditWriteOffEntry>();
            }

            List <ARRegister> orig = list;

            list = new List <ARRegister>(orig);

            List <ARRegister> paylist = new List <ARRegister>();
            List <int>        paybind = new List <int>();

            var cache = (pe as PXGraph).Caches[typeof(ARRegisterEx)];

            list = list.OrderBy(doc => new Tuple <string, string, string, string, string>(
                                    (string)(cache.GetValueExt <ARRegisterEx.branchID>(doc) as PXFieldState).Value,
                                    doc.CuryID,
                                    (string)(cache.GetValueExt <ARRegisterEx.customerID>(doc) as PXFieldState).Value,
                                    doc.DocType,
                                    doc.RefNbr
                                    )).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ARRegisterEx doc = (ARRegisterEx)list[i];
                int          idx = orig.IndexOf(doc);
                try
                {
                    ReasonCode reasonCode = PXSelect <ReasonCode, Where <ReasonCode.reasonCodeID, Equal <Required <ReasonCode.reasonCodeID> > > > .Select((PXGraph)pe, doc.ReasonCode ?? filter.ReasonCode);

                    if (reasonCode == null)
                    {
                        throw new PXException(PXMessages.LocalizeFormatNoPrefixNLA(Messages.ReasonCodeNotFound, filter.ReasonCode));
                    }

                    Location customerLocation = PXSelect <Location, Where <Location.bAccountID, Equal <Required <Location.bAccountID> >,
                                                                           And <Location.locationID, Equal <Required <Location.locationID> > > > > .Select((PXGraph)pe, doc.CustomerID, doc.CustomerLocationID);

                    CRLocation companyLocation = PXSelectJoin <CRLocation,
                                                               InnerJoin <BAccountR, On <CRLocation.bAccountID, Equal <BAccountR.bAccountID>, And <CRLocation.locationID, Equal <BAccountR.defLocationID> > >,
                                                                          InnerJoin <GL.Branch, On <BAccountR.bAccountID, Equal <GL.Branch.bAccountID> > > >, Where <Branch.branchID, Equal <Required <Branch.branchID> > > > .Select((PXGraph)pe, doc.BranchID);

                    object value = null;
                    if (reasonCode.Usage == ReasonCodeUsages.BalanceWriteOff || reasonCode.Usage == ReasonCodeUsages.CreditWriteOff)
                    {
                        value = ReasonCodeSubAccountMaskAttribute.MakeSub <ReasonCode.subMask>((PXGraph)pe, reasonCode.SubMask,
                                                                                               new object[] { reasonCode.SubID, customerLocation.CSalesSubID, companyLocation.CMPSalesSubID },
                                                                                               new Type[] { typeof(ReasonCode.subID), typeof(Location.cSalesSubID), typeof(CRLocation.cMPSalesSubID) });
                    }
                    else
                    {
                        throw new PXException(Messages.InvalidReasonCode);
                    }

                    ARReleaseProcess.EnsureNoUnreleasedVoidPaymentExists((pe as PXGraph), doc, Common.Messages.ActionWrittenOff);
                    pe.CreateWriteOff(reasonCode, value.ToString(), filter.WODate, filter.WOFinPeriodID, doc);

                    if (pe.ARDocument != null && !paylist.Contains(pe.ARDocument))
                    {
                        paylist.Add(pe.ARDocument);
                        paybind.Add(idx);
                    }
                }
                catch (Exception e)
                {
                    PXProcessing <ARRegister> .SetError(idx, e);

                    failed = true;
                }
            }

            if (paylist.Count > 0)
            {
                try
                {
                    ARDocumentRelease.ReleaseDoc(paylist, false);
                }
                catch (PXMassProcessException e)
                {
                    PXProcessing <ARRegister> .SetError(paybind[e.ListIndex], e.InnerException);

                    failed = true;
                }
            }

            if (failed)
            {
                throw new PXException(GL.Messages.DocumentsNotReleased);
            }
        }
Ejemplo n.º 4
0
		public static void ProcessDoc(ARPaymentEntry graph, ARStatementCycle cycle, ARAutoApplyParameters filter)
		{
			List<ARRegister> toRelease = new List<ARRegister>();

			HashSet<string> trace = new HashSet<string>();
			string warningRefNbr = String.Empty;

			int?[] branches = PXAccess.GetBranchIDs().Select(i => (int?)i).ToArray<int?>();

			foreach (Customer customer in GetCustomersForAutoApplication(graph, cycle, filter))
			{
				List<ARInvoice> arInvoiceList = new List<ARInvoice>();

				var invoiceQuery = new PXSelectJoin<ARInvoice,
					InnerJoin<Customer, On<ARInvoice.customerID, Equal<Customer.bAccountID>>>,
					Where<ARInvoice.released, Equal<True>,
						And<ARInvoice.openDoc, Equal<True>,
						And<ARInvoice.pendingPPD, NotEqual<True>, 
						And<Where<ARInvoice.docType, Equal<ARDocType.invoice>,
							Or<ARInvoice.docType, Equal<ARDocType.finCharge>,
							Or<ARInvoice.docType, Equal<ARDocType.debitMemo>>>>>>>>,
					OrderBy<Asc<ARInvoice.dueDate>>>(graph);

				if(filter.LoadChildDocuments == LoadChildDocumentsOptions.None || customer.ParentBAccountID != null)
				{
					invoiceQuery.WhereAnd<Where<Customer.bAccountID, Equal<Required<ARInvoice.customerID>>>>();
				}
				else
				{
					invoiceQuery.WhereAnd<Where<Customer.consolidatingBAccountID, Equal<Required<ARInvoice.customerID>>>>();
				}

				PXResultset<ARInvoice> invoices;
				using (new PXReadBranchRestrictedScope(null, branches, true, false))
				{
					invoices = invoiceQuery.Select(customer.BAccountID);
				}

				foreach (ARInvoice invoice in invoices)
				{
					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)
				{
					PXSelectBase<ARPayment> getPaymentsQuery =
						new PXSelectJoin<ARPayment,
								InnerJoin<Branch,
									On<ARPayment.branchID, Equal<Branch.branchID>>,
								InnerJoin<OrganizationFinPeriod,
									On<Branch.organizationID, Equal<OrganizationFinPeriod.organizationID>,
										And<OrganizationFinPeriod.finPeriodID, Equal<Required<OrganizationFinPeriod.finPeriodID>>>>>>,
								Where<
									OrganizationFinPeriod.status, NotEqual<FinPeriod.status.locked>,
									And<OrganizationFinPeriod.status, NotEqual<FinPeriod.status.inactive>,
									And<ARPayment.released, Equal<True>,
									And<ARPayment.openDoc, Equal<True>,
									And<ARPayment.customerID, Equal<Required<ARPayment.customerID>>,
									And2<Not<HasUnreleasedVoidPayment<ARPayment.docType, ARPayment.refNbr>>,
									And<Where<
											ARPayment.docType, Equal<ARDocType.payment>,
											Or<ARPayment.docType, Equal<ARDocType.prepayment>,
											Or<ARPayment.docType, Equal<Required<ARPayment.docType>>>>>>>>>>>>,
								OrderBy<
									Asc<ARPayment.docDate>>>(graph);

					if (!graph.FinPeriodUtils.CanPostToClosedPeriod())
					{
						getPaymentsQuery.WhereAnd<Where<OrganizationFinPeriod.status, NotEqual<FinPeriod.status.closed>>>();
					}

					foreach (ARPayment payment in 
						getPaymentsQuery.Select(filter.FinPeriodID,
												customer.BAccountID,
												filter.ApplyCreditMemos == true ? ARDocType.CreditMemo : ARDocType.Payment))
					{
						ApplyPayment(graph, filter, payment, arInvoiceList, toRelease, out warningRefNbr);
						trace.Add(warningRefNbr);
					}
				}

				var remainingParentInvoices = arInvoiceList.Where(inv => inv.CustomerID == customer.BAccountID).ToList();

				if (remainingParentInvoices.Count > 0
					&& filter.ApplyCreditMemos == true 
					&& filter.LoadChildDocuments == LoadChildDocumentsOptions.IncludeCRM)
				{
					PXSelectBase<ARPayment> getCreditMemosQuery =
						new PXSelectJoin<ARPayment,
							InnerJoin<Customer, 
								On<ARPayment.customerID, Equal<Customer.bAccountID>>,
							InnerJoin<Branch,
								On<ARPayment.branchID, Equal<Branch.branchID>>,
							InnerJoin<OrganizationFinPeriod,
								On<Branch.organizationID, Equal<OrganizationFinPeriod.organizationID>,
									And<OrganizationFinPeriod.finPeriodID, Equal<Required<OrganizationFinPeriod.finPeriodID>>>>>>>,
							Where<
								OrganizationFinPeriod.status, NotEqual<FinPeriod.status.locked>,
								And<OrganizationFinPeriod.status, NotEqual<FinPeriod.status.inactive>,
								And<ARPayment.released, Equal<True>,
								And<ARPayment.openDoc, Equal<True>,
								And<Customer.consolidatingBAccountID, Equal<Required<Customer.consolidatingBAccountID>>,
								And<ARPayment.docType, Equal<ARDocType.creditMemo>,
								And<Not<HasUnreleasedVoidPayment<ARPayment.docType, ARPayment.refNbr>>>>>>>>>,
							OrderBy<
								Asc<ARPayment.docDate>>>(graph);

					if (!graph.FinPeriodUtils.CanPostToClosedPeriod())
					{
						getCreditMemosQuery.WhereAnd<Where<OrganizationFinPeriod.status, NotEqual<FinPeriod.status.closed>>>();
					}

					foreach (ARPayment payment in 
						getCreditMemosQuery.Select(filter.FinPeriodID, customer.BAccountID))
					{
						ApplyPayment(graph, filter, payment, remainingParentInvoices, toRelease, out warningRefNbr);
						trace.Add(warningRefNbr);
					}
				}
			}

			if (trace != null && trace.Count != 0)
			{
				PXTrace.WriteWarning(String.Format(Messages.FuturePayments, trace.Where(x => !x.IsNullOrEmpty()).JoinToString("; ")));
				trace.Clear();
			}

			if (toRelease.Count > 0)
			{
				ARDocumentRelease.ReleaseDoc(toRelease, false);
			}
		}
Ejemplo n.º 5
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 static void ProcessDoc(ARPaymentEntry graph, ARStatementCycle cycle, ARAutoApplyParameters filter)
        {
            List <ARRegister> toRelease = new List <ARRegister>();

            foreach (Customer customer in GetCustomersForAutoApplication(graph, cycle, filter))
            {
                List <ARInvoice> arInvoiceList = new List <ARInvoice>();

                var invoiceQuery = new PXSelectJoin <ARInvoice,
                                                     InnerJoin <Customer, On <ARInvoice.customerID, Equal <Customer.bAccountID> > >,
                                                     Where <ARInvoice.released, Equal <True>,
                                                            And <ARInvoice.openDoc, Equal <True>,
                                                                 And <ARInvoice.pendingPPD, NotEqual <True>,
                                                                      And <Where <ARInvoice.docType, Equal <ARDocType.invoice>,
                                                                                  Or <ARInvoice.docType, Equal <ARDocType.finCharge>,
                                                                                      Or <ARInvoice.docType, Equal <ARDocType.debitMemo> > > > > > > >,
                                                     OrderBy <Asc <ARInvoice.dueDate> > >(graph);

                if (filter.LoadChildDocuments == LoadChildDocumentsOptions.None || customer.ParentBAccountID != null)
                {
                    invoiceQuery.WhereAnd <Where <Customer.bAccountID, Equal <Required <ARInvoice.customerID> > > >();
                }
                else
                {
                    invoiceQuery.WhereAnd <Where <Customer.consolidatingBAccountID, Equal <Required <ARInvoice.customerID> > > >();
                }

                foreach (ARInvoice invoice in invoiceQuery.Select(
                             customer.BAccountID))
                {
                    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)
                {
                    foreach (ARPayment payment in PXSelect <ARPayment,
                                                            Where <ARPayment.released, Equal <True>,
                                                                   And <ARPayment.openDoc, Equal <True>,
                                                                        And <ARPayment.customerID, Equal <Required <ARPayment.customerID> >,
                                                                             And <Where <ARPayment.docType, Equal <ARDocType.payment>,
                                                                                         Or <ARPayment.docType, Equal <ARDocType.prepayment>,
                                                                                             Or <ARPayment.docType, Equal <Required <ARPayment.docType> > > > > > > > >,
                                                            OrderBy <Asc <ARPayment.docDate> > > .Select(graph,
                                                                                                         customer.BAccountID,
                                                                                                         filter.ApplyCreditMemos == true ? ARDocType.CreditMemo : ARDocType.Payment))
                    {
                        ApplyPayment(graph, filter, payment, arInvoiceList, toRelease);
                    }
                }

                var remainingParentInvoices = arInvoiceList.Where(inv => inv.CustomerID == customer.BAccountID).ToList();

                if (remainingParentInvoices.Count > 0 &&
                    filter.ApplyCreditMemos == true && filter.LoadChildDocuments == LoadChildDocumentsOptions.IncludeCRM)
                {
                    foreach (ARPayment payment in PXSelectJoin <ARPayment,
                                                                InnerJoin <Customer, On <ARPayment.customerID, Equal <Customer.bAccountID> > >,
                                                                Where <ARPayment.released, Equal <True>,
                                                                       And <ARPayment.openDoc, Equal <True>,
                                                                            And <Customer.consolidatingBAccountID, Equal <Required <Customer.consolidatingBAccountID> >,
                                                                                 And <ARPayment.docType, Equal <ARDocType.creditMemo> > > > >,
                                                                OrderBy <Asc <ARPayment.docDate> > > .Select(graph, customer.BAccountID))
                    {
                        ApplyPayment(graph, filter, payment, remainingParentInvoices, toRelease);
                    }
                }
            }

            if (toRelease.Count > 0)
            {
                ARDocumentRelease.ReleaseDoc(toRelease, false);
            }
        }