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); } } }
protected virtual void ARAutoApplyParameters_RowSelected(PXCache sender, PXRowSelectedEventArgs e) { if (e.Row == null) { return; } ARAutoApplyParameters filter = Filter.Current; ARStatementCycleList.SetProcessDelegate <ARPaymentEntry>( delegate(ARPaymentEntry graph, ARStatementCycle cycle) { ProcessDoc(graph, cycle, filter); } ); }
protected virtual void ARAutoApplyParameters_RowSelected(PXCache sender, PXRowSelectedEventArgs e) { if (e.Row == null) return; ARAutoApplyParameters filter = Filter.Current; ARStatementCycleList.SetProcessDelegate<ARPaymentEntry>( delegate(ARPaymentEntry graph, ARStatementCycle cycle) { ProcessDoc(graph, cycle, filter); } ); PXStringListAttribute.SetList<ARAutoApplyParameters.loadChildDocuments>(Filter.Cache, filter, filter.ApplyCreditMemos == true ? (PXStringListAttribute)new LoadChildDocumentsOptions.ListAttribute() : (PXStringListAttribute)new LoadChildDocumentsOptions.NoCRMListAttribute()); }
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); } }
private static IEnumerable<Customer> GetCustomersForAutoApplication(PXGraph graph, ARStatementCycle cycle, ARAutoApplyParameters filter) { var result = new List<Customer> { }; if (filter.LoadChildDocuments != LoadChildDocumentsOptions.None) { var childrenOfParentsWithCycle = PXSelectJoin<Customer, InnerJoin<CustomerMaster, On<Customer.parentBAccountID, Equal<CustomerMaster.bAccountID>, And<Customer.consolidateToParent, Equal<True>>>>, Where<CustomerMaster.statementCycleId, Equal<Required<Customer.statementCycleId>>, And<Match<Current<AccessInfo.userName>>>>> .Select(graph, cycle.StatementCycleId); result.AddRange(childrenOfParentsWithCycle.RowCast<Customer>()); } var nonChildrenWithCycle = PXSelectJoin<Customer, LeftJoin<CustomerMaster, On<Customer.parentBAccountID, Equal<CustomerMaster.bAccountID>, And<Customer.consolidateToParent, Equal<True>>>>, Where<Customer.statementCycleId, Equal<Required<Customer.statementCycleId>>, And2<Match<Current<AccessInfo.userName>>, And<Where<CustomerMaster.bAccountID, IsNull, Or<CustomerMaster.statementCycleId, NotEqual<Required<CustomerMaster.statementCycleId>>>>>>>> .Select(graph, cycle.StatementCycleId, cycle.StatementCycleId); var toAdd = nonChildrenWithCycle.RowCast<Customer>() .Where(c => result.FirstOrDefault(alreadyPresent => alreadyPresent.BAccountID == c.BAccountID) == null).ToList(); result.AddRange(toAdd); return result; }
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); } } }
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); } }