public static void ReleaseDoc(CABatch aDoc)
        {
            if ((bool)aDoc.Released || (bool)aDoc.Hold)
            {
                throw new PXException(CA.Messages.CABatchStatusIsNotValidForProcessing);
            }
            ARBatchUpdate be  = PXGraph.CreateInstance <ARBatchUpdate>();
            CABatch       doc = be.Document.Select(aDoc.BatchNbr);

            be.Document.Current = doc;
            if ((bool)doc.Released || (bool)doc.Hold)
            {
                throw new PXException(CA.Messages.CABatchStatusIsNotValidForProcessing);
            }

            ARPayment voided = PXSelectReadonly2 <ARPayment,
                                                  InnerJoin <CABatchDetail, On <CABatchDetail.origDocType, Equal <ARPayment.docType>,
                                                                                And <CABatchDetail.origRefNbr, Equal <ARPayment.refNbr>,
                                                                                     And <CABatchDetail.origModule, Equal <GL.BatchModule.moduleAR> > > > >,
                                                  Where <CABatchDetail.batchNbr, Equal <Required <CABatch.batchNbr> >,
                                                         And <ARPayment.voided, Equal <True> > > > .Select(be, doc.BatchNbr);

            if (voided != null && String.IsNullOrEmpty(voided.RefNbr) == false)
            {
                throw new PXException(CA.Messages.CABatchContainsVoidedPaymentsAndConnotBeReleased);
            }

            List <ARRegister>        unreleasedList   = new List <ARRegister>();
            PXSelectBase <ARPayment> selectUnreleased = new PXSelectReadonly2 <ARPayment,
                                                                               InnerJoin <CABatchDetail, On <CABatchDetail.origDocType, Equal <ARPayment.docType>,
                                                                                                             And <CABatchDetail.origRefNbr, Equal <ARPayment.refNbr>,
                                                                                                                  And <CABatchDetail.origModule, Equal <GL.BatchModule.moduleAR> > > > >,
                                                                               Where <CABatchDetail.batchNbr, Equal <Optional <CABatch.batchNbr> >,
                                                                                      And <ARPayment.released, Equal <boolFalse> > > >(be);

            foreach (ARPayment iPmt in selectUnreleased.Select(doc.BatchNbr))
            {
                if (iPmt.Released != true)
                {
                    unreleasedList.Add(iPmt);
                }
            }
            if (unreleasedList.Count > 0)
            {
                ARDocumentRelease.ReleaseDoc(unreleasedList, true);
            }

            selectUnreleased.View.Clear();
            ARPayment pmt = selectUnreleased.Select(doc.BatchNbr);

            if (pmt != null)
            {
                throw new PXException(CA.Messages.CABatchContainsUnreleasedPaymentsAndCannotBeReleased);
            }
            doc.Released   = true;
            doc.DateSeqNbr = CABatchEntry.GetNextDateSeqNbr(be, aDoc); //Nothing AP specific in this static function
            be.RecalcTotals();
            doc = be.Document.Update(doc);
            be.Actions.PressSave();
        }
Exemple #2
0
        public static CABatch CreateBatchPayment(List <APPayment> list, PrintChecksFilter filter)
        {
            CABatchEntry be     = CreateInstance <CABatchEntry>();
            CABatch      result = be.Document.Insert(new CABatch());

            be.Document.Current = result;

            CABatch copy = (CABatch)be.Document.Cache.CreateCopy(result);

            copy.CashAccountID   = filter.PayAccountID;
            copy.PaymentMethodID = filter.PayTypeID;
            result = be.Document.Update(copy);

            foreach (APPayment iPmt in list)
            {
                if (iPmt.CashAccountID != result.CashAccountID || iPmt.PaymentMethodID != result.PaymentMethodID)
                {
                    throw new PXException(Messages.APPaymentDoesNotMatchCABatchByAccountOrPaymentType);
                }

                if (string.IsNullOrEmpty(iPmt.ExtRefNbr) && string.IsNullOrEmpty(filter.NextCheckNbr))
                {
                    throw new PXException(Messages.NextCheckNumberIsRequiredForProcessing);
                }

                be.AddPayment(iPmt, true);
            }

            be.Save.Press();
            result = be.Document.Current;
            return(result);
        }
        private static PXGraph PrepareDestinationGraphForAP(string origDocType, string origRefNbr, bool preferPrimaryDocForm)
        {
            if (origDocType == APDocType.Invoice ||
                origDocType == APDocType.CreditAdj ||
                origDocType == APDocType.DebitAdj && preferPrimaryDocForm)
            {
                return(MakeDestinationGraph <APInvoiceEntry, APInvoice, APInvoice.docType, APInvoice.refNbr>(origDocType, origRefNbr));
            }

            switch (origDocType)
            {
            case APDocType.QuickCheck:
            case APDocType.VoidQuickCheck:
                return(MakeDestinationGraph <APQuickCheckEntry, APQuickCheck, APQuickCheck.docType, APQuickCheck.refNbr>(origDocType, origRefNbr));

            case CATranType.CABatch:
                CABatchEntry caBatchGraph = PXGraph.CreateInstance <CABatchEntry>();
                caBatchGraph.Document.Current =
                    PXSelect <CABatch,
                              Where <CABatch.batchNbr, Equal <Required <CATran.origRefNbr> > > >
                    .Select(caBatchGraph, origRefNbr);

                return(caBatchGraph);

            case APDocType.Check:
            case APDocType.DebitAdj:
            case APDocType.Prepayment:
            case APDocType.Refund:
            case APDocType.VoidCheck:
                return(MakeDestinationGraph <APPaymentEntry, APPayment, APPayment.docType, APPayment.refNbr>(origDocType, origRefNbr));

            default:
                return(null);
            }
        }
Exemple #4
0
        protected virtual void RedirectToResultWithCreateBatch(CABatch batch)
        {
            CABatchEntry be = CreateInstance <CABatchEntry>();

            be.Document.Current = be.Document.Search <CABatch.batchNbr>(batch.BatchNbr);
            be.SelectTimeStamp();
            throw new PXRedirectRequiredException(be, "Redirect");
        }
        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);
            }
        }
Exemple #6
0
        protected virtual void PrintPayments(List <APPayment> list, PrintChecksFilter filter, PaymentMethod paymentMethod)
        {
            if (list.Count == 0)
            {
                return;
            }

            if (paymentMethod.UseForAP == true)
            {
                if (paymentMethod.APPrintChecks == true && string.IsNullOrEmpty(paymentMethod.APCheckReportID))
                {
                    throw new PXException(Messages.FieldNotSetInPaymentMethod, PXUIFieldAttribute.GetDisplayName <PaymentMethod.aPCheckReportID>(paymenttype.Cache), paymentMethod.PaymentMethodID);
                }

                if (paymentMethod.APPrintChecks == true && paymentMethod.APPrintRemittance == true && string.IsNullOrEmpty(paymentMethod.APRemittanceReportID))
                {
                    throw new PXException(Messages.FieldNotSetInPaymentMethod, PXUIFieldAttribute.GetDisplayName <PaymentMethod.aPRemittanceReportID>(paymenttype.Cache), paymentMethod.PaymentMethodID);
                }
            }

            bool printAdditionRemit = false;

            if (paymentMethod.APCreateBatchPayment == true)
            {
                CABatchEntry be     = CreateInstance <CABatchEntry>();
                CABatch      result = be.Document.Insert(new CABatch());
                be.Document.Current = result;

                CABatch copy = (CABatch)be.Document.Cache.CreateCopy(result);
                copy.CashAccountID   = filter.PayAccountID;
                copy.PaymentMethodID = filter.PayTypeID;
                result = be.Document.Update(copy);

                CABatch batch = be.Document.Current;

                if (batch != null)
                {
                    bool           failed = false;
                    APPaymentEntry pe     = CreateInstance <APPaymentEntry>();

                    string NextCheckNbr = filter.NextCheckNbr;
                    foreach (APPayment pmt in list)
                    {
                        APPayment payment = pmt;

                        if (pmt.CashAccountID != result.CashAccountID || pmt.PaymentMethodID != result.PaymentMethodID)
                        {
                            throw new PXException(Messages.APPaymentDoesNotMatchCABatchByAccountOrPaymentType);
                        }

                        if (string.IsNullOrEmpty(pmt.ExtRefNbr) && string.IsNullOrEmpty(filter.NextCheckNbr))
                        {
                            throw new PXException(Messages.NextCheckNumberIsRequiredForProcessing);
                        }

                        PXProcessing <APPayment> .SetCurrentItem(payment);

                        try
                        {
                            payment = pe.Document.Search <APPayment.refNbr>(payment.RefNbr, payment.DocType);
                            if (payment.PrintCheck != true)
                            {
                                throw new PXException(Messages.CantPrintNonprintableCheck);
                            }
                            if (payment.DocType.IsIn(APDocType.Check, APDocType.QuickCheck, APDocType.Prepayment) &&
                                payment.Status != APDocStatus.PendingPrint)
                            {
                                throw new PXException(Messages.ChecksMayBePrintedInPendingPrintStatus);
                            }

                            AssignNumbers(pe, payment, ref NextCheckNbr, true);

                            if (payment.Passed == true)
                            {
                                pe.TimeStamp = payment.tstamp;
                            }
                            pe.Save.Press();
                            payment.tstamp = pe.TimeStamp;
                            pe.Clear();

                            be.AddPayment(payment, true);
                        }
                        catch (PXException e)
                        {
                            PXProcessing <APPayment> .SetError(e);

                            failed = true;
                        }
                    }

                    bool batchHasPayments = be.BatchPayments.Select().Count > 0;

                    if (failed && batchHasPayments)
                    {
                        be.Save.Press();
                        batch = be.Document.Current;
                        throw new PXOperationCompletedWithErrorException(Messages.APPaymentsAreAddedToTheBatchFailedPaymentsExcluded, batch.BatchNbr);
                    }

                    if (failed && !batchHasPayments)
                    {
                        throw new PXOperationCompletedWithErrorException();
                    }

                    be.Save.Press();
                    batch = be.Document.Current;

                    RedirectToResultWithCreateBatch(batch);
                }
            }
            else
            {
                APReleaseChecks     pp          = CreateInstance <APReleaseChecks>();
                ReleaseChecksFilter filter_copy = PXCache <ReleaseChecksFilter> .CreateCopy(pp.Filter.Current);

                filter_copy.PayAccountID = filter.PayAccountID;
                filter_copy.PayTypeID    = filter.PayTypeID;
                filter_copy.CuryID       = filter.CuryID;
                pp.Filter.Cache.Update(filter_copy);

                APPaymentEntry pe             = CreateInstance <APPaymentEntry>();
                bool           failed         = false;
                Dictionary <string, string> d = new Dictionary <string, string>();

                string nextCheckNbr = filter.NextCheckNbr;
                string prevCheckNbr = nextCheckNbr;

                int idxReportFilter = 0;
                foreach (APPayment pmt in list)
                {
                    APPayment payment = pmt;
                    PXProcessing <APPayment> .SetCurrentItem(payment);

                    try
                    {
                        prevCheckNbr = nextCheckNbr;
                        if (filter.IsNextNumberDuplicated(this, nextCheckNbr))
                        {
                            string duplicate = nextCheckNbr;
                            nextCheckNbr = AutoNumberAttribute.NextNumber(nextCheckNbr);
                            throw new PXException(Messages.ConflictWithExistingCheckNumber, duplicate);
                        }

                        payment = pe.Document.Search <APPayment.refNbr>(payment.RefNbr, payment.DocType);
                        if (payment.PrintCheck != true)
                        {
                            throw new PXException(Messages.CantPrintNonprintableCheck);
                        }
                        if (payment.DocType.IsIn(APDocType.Check, APDocType.QuickCheck, APDocType.Prepayment) &&
                            payment.Status != APDocStatus.PendingPrint)
                        {
                            throw new PXException(Messages.ChecksMayBePrintedInPendingPrintStatus);
                        }
                        AssignNumbers(pe, payment, ref nextCheckNbr);

                        if (payment.Passed == true)
                        {
                            pe.TimeStamp = payment.tstamp;
                        }
                        pe.Save.Press();
                        payment.tstamp = pe.TimeStamp;
                        pe.Clear();

                        APPayment seldoc = pe.Document.Search <APPayment.refNbr>(payment.RefNbr, payment.DocType);
                        seldoc.Selected = true;
                        seldoc.Passed   = true;
                        seldoc.tstamp   = payment.tstamp;
                        pp.APPaymentList.Cache.Update(seldoc);
                        pp.APPaymentList.Cache.SetStatus(seldoc, PXEntryStatus.Updated);

                        printAdditionRemit |= seldoc.BillCntr > paymentMethod.APStubLines;

                        StringBuilder sbDocType = new StringBuilder($"{nameof(APPayment)}.{nameof(APPayment.DocType)}");
                        sbDocType.Append(Convert.ToString(idxReportFilter));
                        StringBuilder sbRefNbr = new StringBuilder($"{nameof(APPayment)}.{nameof(APPayment.RefNbr)}");
                        sbRefNbr.Append(Convert.ToString(idxReportFilter));

                        idxReportFilter++;

                        d[sbDocType.ToString()] = payment.DocType.IsIn(APDocType.Prepayment, APDocType.QuickCheck) ? payment.DocType : APDocType.Check;
                        d[sbRefNbr.ToString()]  = payment.RefNbr;
                        PXProcessing <APPayment> .SetProcessed();
                    }
                    catch (PXException e)
                    {
                        PXProcessing <APPayment> .SetError(e);

                        failed       = true;
                        nextCheckNbr = prevCheckNbr;
                    }
                }

                if (failed)
                {
                    PXReportRequiredException report = null;
                    if (d.Count > 0)
                    {
                        d[ReportMessages.CheckReportFlag] = ReportMessages.CheckReportFlagValue;
                        report = new PXReportRequiredException(d, paymentMethod.APCheckReportID, PXBaseRedirectException.WindowMode.New, "Check");
                    }
                    throw new PXOperationCompletedWithErrorException(GL.Messages.DocumentsNotReleased, report);
                }
                else
                {
                    if (d.Count > 0)
                    {
                        RedirectToResultNoBatch(pp, d, paymentMethod, printAdditionRemit, nextCheckNbr);
                    }
                }
            }
        }