public static void ReleasePayments(List <APPayment> list, string Action)
        {
            APReleaseChecks releaseChecksGraph = CreateInstance <APReleaseChecks>();
            APPaymentEntry  pe        = CreateInstance <APPaymentEntry>();
            bool            failed    = false;
            bool            successed = false;

            List <APRegister> docs = new List <APRegister>(list.Count);

            foreach (APPayment payment in list)
            {
                if (payment.Passed == true)
                {
                    releaseChecksGraph.TimeStamp = pe.TimeStamp = payment.tstamp;
                }

                switch (Action)
                {
                case ReleaseChecksFilter.action.ReleaseChecks:
                    payment.Printed = true;
                    break;

                case ReleaseChecksFilter.action.ReprintChecks:
                case ReleaseChecksFilter.action.VoidAndReprintChecks:
                    payment.ExtRefNbr = null;
                    payment.Printed   = false;
                    break;

                default:
                    continue;
                }

                PXProcessing <APPayment> .SetCurrentItem(payment);

                if (Action == ReleaseChecksFilter.action.ReleaseChecks)
                {
                    try
                    {
                        pe.Document.Current = pe.Document.Search <APPayment.refNbr>(payment.RefNbr, payment.DocType);
                        if (PaymentRefAttribute.PaymentRefMustBeUnique(pe.paymenttype.Current) && string.IsNullOrEmpty(payment.ExtRefNbr))
                        {
                            throw new PXException(ErrorMessages.FieldIsEmpty, typeof(APPayment.extRefNbr).Name);
                        }

                        payment.IsReleaseCheckProcess = true;

                        APPrintChecks.AssignNumbersWithNoAdditionalProcessing(pe, payment);
                        pe.Save.Press();

                        object[] persisted = PXTimeStampScope.GetPersisted(pe.Document.Cache, pe.Document.Current);
                        if (persisted == null || persisted.Length == 0)
                        {
                            //preserve timestamp which will be @@dbts after last record committed to db on previous Persist().
                            //otherwise another process updated APAdjust.
                            docs.Add(payment);
                        }
                        else
                        {
                            if (payment.Passed == true)
                            {
                                pe.Document.Current.Passed = true;
                            }
                            docs.Add(pe.Document.Current);
                        }
                        successed = true;
                    }
                    catch (PXException e)
                    {
                        PXProcessing <APPayment> .SetError(e);

                        docs.Add(null);
                        failed = true;
                    }
                }

                if (Action == ReleaseChecksFilter.action.ReprintChecks || Action == ReleaseChecksFilter.action.VoidAndReprintChecks)
                {
                    try
                    {
                        payment.IsPrintingProcess = true;

                        releaseChecksGraph.APPaymentList.Cache.SetValueExt <APPayment.printed>(payment, false);
                        releaseChecksGraph.APPaymentList.Cache.SetValueExt <APPayment.hold>(payment, false);
                        releaseChecksGraph.APPaymentList.Cache.SetValueExt <APPayment.extRefNbr>(payment, null);

                        releaseChecksGraph.APPaymentList.Cache.RaiseRowSelected(payment);                         //We need payment's status to be set correctly before persisting
                        // TODO: Need to rework. Use legal CRUD methods of caches!
                        releaseChecksGraph.APPaymentList.Cache.PersistUpdated(payment);
                        releaseChecksGraph.APPaymentList.Cache.Persisted(false);

                        releaseChecksGraph.TimeStamp = PXDatabase.SelectTimeStamp();

                        // delete check numbers only if Reprint (not Void and Reprint)
                        PaymentMethod pm = pe.paymenttype.Select(payment.PaymentMethodID);
                        if (pm.APPrintChecks == true && Action == ReleaseChecksFilter.action.ReprintChecks)
                        {
                            APPayment doc = payment;
                            new HashSet <string>(pe.Adjustments_Raw.Select(doc.DocType, doc.RefNbr)
                                                 .RowCast <APAdjust>()
                                                 .Select(adj => adj.StubNbr))
                            .ForEach(nbr => PaymentRefAttribute.DeleteCheck((int)doc.CashAccountID, doc.PaymentMethodID, nbr));

                            // sync PaymentMethodAccount.APLastRefNbr with actual last CashAccountCheck number
                            PaymentMethodAccount det = releaseChecksGraph.cashaccountdetail.SelectSingle(payment.CashAccountID, payment.PaymentMethodID);
                            PaymentRefAttribute.LastCashAccountCheckSelect.Clear(releaseChecksGraph);
                            CashAccountCheck cacheck = PaymentRefAttribute.LastCashAccountCheckSelect.SelectSingleBound(releaseChecksGraph, new object[] { det });
                            det.APLastRefNbr = cacheck?.CheckNbr;
                            releaseChecksGraph.cashaccountdetail.Cache.PersistUpdated(det);
                            releaseChecksGraph.cashaccountdetail.Cache.Persisted(false);
                        }
                        // END TODO
                        if (string.IsNullOrEmpty(payment.ExtRefNbr))
                        {
                            //try to get next number
                            releaseChecksGraph.APPaymentList.Cache.SetDefaultExt <APPayment.extRefNbr>(payment);
                        }
                    }
                    catch (PXException e)
                    {
                        PXProcessing <APPayment> .SetError(e);
                    }
                    docs.Add(null);
                }
            }
            if (successed)
            {
                APDocumentRelease.ReleaseDoc(docs, true);
            }

            if (failed)
            {
                throw new PXOperationCompletedWithErrorException(GL.Messages.DocumentsNotReleased);
            }
        }
        public static void ReleasePayments(List <APPayment> list, string Action)
        {
            APReleaseChecks releaseChecksGraph = CreateInstance <APReleaseChecks>();
            APPaymentEntry  pe        = CreateInstance <APPaymentEntry>();
            CABatchEntry    be        = CreateInstance <CABatchEntry>();
            bool            failed    = false;
            bool            successed = false;

            List <APRegister> docs = new List <APRegister>(list.Count);

            foreach (APPayment payment in list)
            {
                if (payment.Passed == true)
                {
                    releaseChecksGraph.TimeStamp = pe.TimeStamp = payment.tstamp;
                }

                switch (Action)
                {
                case ReleaseChecksFilter.action.ReleaseChecks:
                    payment.Printed = true;
                    break;

                case ReleaseChecksFilter.action.ReprintChecks:
                case ReleaseChecksFilter.action.VoidAndReprintChecks:
                    payment.ExtRefNbr = null;
                    payment.Printed   = false;
                    break;

                default:
                    continue;
                }

                PXProcessing <APPayment> .SetCurrentItem(payment);

                if (Action == ReleaseChecksFilter.action.ReleaseChecks)
                {
                    try
                    {
                        pe.Document.Current = pe.Document.Search <APPayment.refNbr>(payment.RefNbr, payment.DocType);
                        if (pe.Document.Current?.ExtRefNbr != payment.ExtRefNbr)
                        {
                            APPayment payment_copy = PXCache <APPayment> .CreateCopy(pe.Document.Current);

                            payment_copy.ExtRefNbr = payment.ExtRefNbr;
                            pe.Document.Update(payment_copy);
                        }

                        if (PaymentRefAttribute.PaymentRefMustBeUnique(pe.paymenttype.Current) && string.IsNullOrEmpty(payment.ExtRefNbr))
                        {
                            throw new PXException(ErrorMessages.FieldIsEmpty, typeof(APPayment.extRefNbr).Name);
                        }

                        payment.IsReleaseCheckProcess = true;

                        APPrintChecks.AssignNumbersWithNoAdditionalProcessing(pe, payment);
                        pe.Save.Press();

                        object[] persisted = PXTimeStampScope.GetPersisted(pe.Document.Cache, pe.Document.Current);
                        if (persisted == null || persisted.Length == 0)
                        {
                            //preserve timestamp which will be @@dbts after last record committed to db on previous Persist().
                            //otherwise another process updated APAdjust.
                            docs.Add(payment);
                        }
                        else
                        {
                            if (payment.Passed == true)
                            {
                                pe.Document.Current.Passed = true;
                            }
                            docs.Add(pe.Document.Current);
                        }
                        successed = true;
                    }
                    catch (PXException e)
                    {
                        PXProcessing <APPayment> .SetError(e);

                        docs.Add(null);
                        failed = true;
                    }
                }

                if (Action == ReleaseChecksFilter.action.ReprintChecks || Action == ReleaseChecksFilter.action.VoidAndReprintChecks)
                {
                    try
                    {
                        payment.IsPrintingProcess = true;
                        using (PXTransactionScope transactionScope = new PXTransactionScope())
                        {
                            #region Update CABatch if ReprintChecks
                            CABatch caBatch = PXSelectJoin <CABatch,
                                                            InnerJoin <CABatchDetail, On <CABatch.batchNbr, Equal <CABatchDetail.batchNbr> > >,
                                                            Where <CABatchDetail.origModule, Equal <Required <APPayment.origModule> >,
                                                                   And <CABatchDetail.origDocType, Equal <Required <APPayment.docType> >,
                                                                        And <CABatchDetail.origRefNbr, Equal <Required <APPayment.refNbr> > > > > > .
                                              Select(be, payment.OrigModule, payment.DocType, payment.RefNbr);

                            if (caBatch != null)
                            {
                                be.Document.Current = caBatch;
                                int           DetailCount = be.Details.Select().Count;                       // load
                                CABatchDetail detail      = be.Details.Locate(new CABatchDetail()
                                {
                                    BatchNbr    = be.Document.Current.BatchNbr,
                                    OrigModule  = payment.OrigModule,
                                    OrigDocType = payment.DocType,
                                    OrigRefNbr  = payment.RefNbr,
                                    OrigLineNbr = CABatchDetail.origLineNbr.DefaultValue,
                                });
                                if (detail != null)
                                {
                                    // payment.Status is recalculated in CABatchEntry.Delete
                                    if (DetailCount == 1)
                                    {
                                        be.Document.Delete(be.Document.Current);                                         // Details will delete by PXParent
                                    }
                                    else
                                    {
                                        be.Details.Delete(detail);                                          // recalculated batch totals
                                    }
                                }
                                be.Save.Press();
                            }
                            else
                            {
                                PXCache cacheAPPayment = releaseChecksGraph.APPaymentList.Cache;

                                cacheAPPayment.SetValueExt <APPayment.printed>(payment, false);
                                cacheAPPayment.SetValueExt <APPayment.hold>(payment, false);
                                cacheAPPayment.SetValueExt <APPayment.extRefNbr>(payment, null);
                                // APPayment.Status is recalculated by SetStatusCheckAttribute
                                cacheAPPayment.MarkUpdated(payment);

                                cacheAPPayment.PersistUpdated(payment);
                                cacheAPPayment.Persisted(false);
                            }
                            #endregion
                            // TODO: Need to rework. Use legal CRUD methods of caches!
                            releaseChecksGraph.TimeStamp = PXDatabase.SelectTimeStamp();

                            // delete check numbers only if Reprint (not Void and Reprint)
                            PaymentMethod pm = pe.paymenttype.Select(payment.PaymentMethodID);
                            if (pm.APPrintChecks == true && Action == ReleaseChecksFilter.action.ReprintChecks)
                            {
                                APPayment doc = payment;
                                new HashSet <string>(pe.Adjustments_Raw.Select(doc.DocType, doc.RefNbr)
                                                     .RowCast <APAdjust>()
                                                     .Select(adj => adj.StubNbr))
                                .ForEach(nbr => PaymentRefAttribute.DeleteCheck((int)doc.CashAccountID, doc.PaymentMethodID, nbr));

                                // sync PaymentMethodAccount.APLastRefNbr with actual last CashAccountCheck number
                                PaymentMethodAccount det = releaseChecksGraph.cashaccountdetail.SelectSingle(payment.CashAccountID, payment.PaymentMethodID);
                                PaymentRefAttribute.LastCashAccountCheckSelect.Clear(releaseChecksGraph);
                                CashAccountCheck cacheck = PaymentRefAttribute.LastCashAccountCheckSelect.SelectSingleBound(releaseChecksGraph, new object[] { det });
                                det.APLastRefNbr = cacheck?.CheckNbr;
                                releaseChecksGraph.cashaccountdetail.Cache.PersistUpdated(det);
                                releaseChecksGraph.cashaccountdetail.Cache.Persisted(false);
                            }
                            // END TODO
                            if (string.IsNullOrEmpty(payment.ExtRefNbr))
                            {
                                //try to get next number
                                releaseChecksGraph.APPaymentList.Cache.SetDefaultExt <APPayment.extRefNbr>(payment);
                            }
                            transactionScope.Complete();
                        }
                    }
                    catch (PXException e)
                    {
                        PXProcessing <APPayment> .SetError(e);
                    }
                    docs.Add(null);
                }
            }
            if (successed)
            {
                APDocumentRelease.ReleaseDoc(docs, true);
            }

            if (failed)
            {
                throw new PXOperationCompletedWithErrorException(GL.Messages.DocumentsNotReleased);
            }
        }
        public static void ReleasePayments(List <APPayment> list, string Action)
        {
            APReleaseChecks graph     = CreateInstance <APReleaseChecks>();
            APPaymentEntry  pe        = CreateInstance <APPaymentEntry>();
            bool            failed    = false;
            bool            successed = false;

            List <APRegister> docs = new List <APRegister>(list.Count);

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Passed == true)
                {
                    graph.TimeStamp = pe.TimeStamp = list[i].tstamp;
                }

                switch (Action)
                {
                case "R":
                    list[i].Printed = true;
                    break;

                case "D":
                case "V":
                    list[i].ExtRefNbr = null;
                    list[i].Printed   = false;
                    break;

                default:
                    continue;
                }

                if ((bool)list[i].Printed)
                {
                    try
                    {
                        APPrintChecks.AssignNumbersWithNoAdditionalProcessing(pe, list[i]);
                        pe.Save.Press();

                        object[] persisted = PXTimeStampScope.GetPersisted(pe.Document.Cache, pe.Document.Current);
                        if (persisted == null || persisted.Length == 0)
                        {
                            //preserve timestamp which will be @@dbts after last record committed to db on previous Persist().
                            //otherwise another process updated APAdjust.
                            docs.Add(list[i]);
                        }
                        else
                        {
                            if (list[i].Passed == true)
                            {
                                pe.Document.Current.Passed = true;
                            }
                            docs.Add(pe.Document.Current);
                        }
                        successed = true;
                    }
                    catch (Exception e)
                    {
                        PXProcessing <APPayment> .SetError(i, e);

                        docs.Add(null);
                        failed = true;
                    }
                }
                else
                {
                    try
                    {
                        list[i].Hold = true;
                        // TODO: Need to rework. Use legal CRUD methods of caches!
                        graph.APPaymentList.Cache.PersistUpdated(list[i]);
                        graph.APPaymentList.Cache.Persisted(false);

                        graph.TimeStamp = PXDatabase.SelectTimeStamp();

                        // delete check numbers only if Reprint (not Void and Reprint)
                        PaymentMethod pm = pe.paymenttype.Select(list[i].PaymentMethodID);
                        if (pm.APPrintChecks == true && Action == "D")
                        {
                            APPayment doc = list[i];
                            new HashSet <string>(pe.Adjustments_Raw.Select(doc.DocType, doc.RefNbr, doc.LineCntr)
                                                 .RowCast <APAdjust>()
                                                 .Select(adj => adj.StubNbr))
                            .ForEach(nbr => PaymentRefAttribute.DeleteCheck((int)doc.CashAccountID, doc.PaymentMethodID, nbr));

                            // sync PaymentMethodAccount.APLastRefNbr with actual last CashAccountCheck number
                            PaymentMethodAccount det = graph.cashaccountdetail.SelectSingle(list[i].CashAccountID, list[i].PaymentMethodID);
                            PaymentRefAttribute.LastCashAccountCheckSelect.Clear(graph);
                            CashAccountCheck cacheck = PaymentRefAttribute.LastCashAccountCheckSelect.SelectSingleBound(graph, new object[] { det });
                            det.APLastRefNbr = cacheck == null ? null : cacheck.CheckNbr;
                            graph.cashaccountdetail.Cache.PersistUpdated(det);
                            graph.cashaccountdetail.Cache.Persisted(false);
                        }
                        // END TODO
                        if (string.IsNullOrEmpty(list[i].ExtRefNbr))
                        {
                            //try to get next number
                            graph.APPaymentList.Cache.SetDefaultExt <APPayment.extRefNbr>(list[i]);
                        }
                    }
                    catch (Exception e)
                    {
                        PXProcessing <APPayment> .SetError(i, e);
                    }
                    docs.Add(null);
                }
            }
            if (successed)
            {
                APDocumentRelease.ReleaseDoc(docs, true);
            }

            if (failed)
            {
                throw new PXException(GL.Messages.DocumentsNotReleased);
            }
        }