Ejemplo n.º 1
0
        private static void _createPayments(List <ARInvoice> list, PayBillsFilter filter, CurrencyInfo info)
        {
            bool           failed = false;
            ARPaymentEntry pe     = PXGraph.CreateInstance <ARPaymentEntry>();

            list.Sort((in1, in2) =>
            {
                if (in1.CustomerID != in2.CustomerID)
                {
                    return(((IComparable)in1.CustomerID).CompareTo(in2.CustomerID));
                }
                return(((IComparable)in1.PMInstanceID).CompareTo(in2.PMInstanceID));
            }
                      );
            for (int i = 0; i < list.Count; i++)
            {
                ARInvoice doc       = list[i];
                ARPayment pmt       = null;
                bool      docFailed = false;
                try
                {
                    pe.CreatePayment(doc, info, filter.PayDate, filter.PayFinPeriodID, false);
                    pmt = pe.Document.Current;
                    if (pmt != null)
                    {
                        pmt.Hold = false;

                        FinPeriodIDAttribute.SetPeriodsByMaster <ARPayment.finPeriodID>(pe.Document.Cache, pmt, filter.PayFinPeriodID);
                    }
                    pe.Save.Press();
                }
                catch (Exception e)
                {
                    PXFilteredProcessing <ARInvoice, PayBillsFilter> .SetError(i, e.Message);

                    docFailed = failed = true;
                }

                if (!docFailed)
                {
                    PXFilteredProcessing <ARInvoice, PayBillsFilter> .SetInfo(i, PXMessages.LocalizeFormatNoPrefixNLA(Messages.ARPaymentIsCreatedProcessingINProgress, pmt.RefNbr));
                }
            }
            if (failed)
            {
                throw new PXException(Messages.CreationOfARPaymentFailedForSomeInvoices);
            }
        }
Ejemplo n.º 2
0
 public virtual void CopyPeriods <TDAC, TFinPeriodID, TMasterFinPeriodID>(PXCache cache, TDAC src, TDAC dest)
     where TDAC : class, IBqlTable, new()
     where TFinPeriodID : class, IBqlField
     where TMasterFinPeriodID : class, IBqlField
 {
     if (!cache.ObjectsEqual <TFinPeriodID, TMasterFinPeriodID>(src, dest))
     {
         object origFinPeriodID = cache.GetValue <TFinPeriodID>(src);
         string tranPeriodID    = (string)cache.GetValue <TMasterFinPeriodID>(src);
         try
         {
             FinPeriodIDAttribute.SetPeriodsByMaster <TFinPeriodID>(cache, dest, tranPeriodID);
             origFinPeriodID = cache.GetValue <TFinPeriodID>(dest);
             cache.RaiseFieldVerifying <TFinPeriodID>(dest, ref origFinPeriodID);
         }
         catch (PXException e)
         {
             cache.RaiseExceptionHandling <TFinPeriodID>(dest, origFinPeriodID, e);
         }
     }
 }
Ejemplo n.º 3
0
        public void Revalue(RevalueFilter filter, List <RevaluedGLHistory> list)
        {
            JournalEntry je        = PXGraph.CreateInstance <JournalEntry>();
            PostGraph    pg        = PXGraph.CreateInstance <PostGraph>();
            PXCache      basecache = je.Caches[typeof(AcctHist)];

            je.Views.Caches.Add(typeof(AcctHist));

            string extRefNbrNumbering = je.CMSetup.Current.ExtRefNbrNumberingID;

            if (string.IsNullOrEmpty(extRefNbrNumbering) == false)
            {
                RevaluationRefNbrHelper helper = new RevaluationRefNbrHelper(extRefNbrNumbering);
                helper.Subscribe(je);
            }

            DocumentList <Batch> created = new DocumentList <Batch>(je);

            Currency currency = PXSelect <Currency, Where <Currency.curyID, Equal <Required <Currency.curyID> > > > .Select(je, filter.CuryID);

            bool hasErrors = false;

            using (PXTransactionScope ts = new PXTransactionScope())
            {
                foreach (RevaluedGLHistory hist in list)
                {
                    PXProcessing <RevaluedGLHistory> .SetCurrentItem(hist);

                    if (hist.FinPtdRevalued == 0m)
                    {
                        PXProcessing <RevaluedGLHistory> .SetProcessed();

                        continue;
                    }

                    string FinPeriod =
                        FinPeriodRepository.GetFinPeriodByMasterPeriodID(PXAccess.GetParentOrganizationID(hist.BranchID), filter.FinPeriodID)
                        .Result
                        .FinPeriodID;

                    ProcessingResult result = CheckFinPeriod(FinPeriod, hist.BranchID);
                    if (!result.IsSuccess)
                    {
                        hasErrors = true;
                        continue;
                    }

                    if (je.GLTranModuleBatNbr.Cache.IsInsertedUpdatedDeleted)
                    {
                        je.Save.Press();

                        if (created.Find(je.BatchModule.Current) == null)
                        {
                            created.Add(je.BatchModule.Current);
                        }
                    }

                    Batch cmbatch = created.Find <Batch.branchID>(hist.BranchID) ?? new Batch();
                    if (cmbatch.BatchNbr == null)
                    {
                        je.Clear();

                        CurrencyInfo info = new CurrencyInfo();
                        info.CuryID      = hist.CuryID;
                        info.CuryEffDate = hist.CuryEffDate;
                        info.BaseCalc    = false;
                        info             = je.currencyinfo.Insert(info) ?? info;

                        cmbatch             = new Batch();
                        cmbatch.BranchID    = hist.BranchID;
                        cmbatch.Module      = "CM";
                        cmbatch.Status      = "U";
                        cmbatch.AutoReverse = false;
                        cmbatch.Released    = true;
                        cmbatch.Hold        = false;
                        cmbatch.DateEntered = filter.CuryEffDate;
                        FinPeriodIDAttribute.SetPeriodsByMaster <Batch.finPeriodID>(je.BatchModule.Cache, cmbatch, filter.FinPeriodID);

                        cmbatch.CuryID      = hist.CuryID;
                        cmbatch.CuryInfoID  = info.CuryInfoID;
                        cmbatch.DebitTotal  = 0m;
                        cmbatch.CreditTotal = 0m;
                        cmbatch.Description = filter.Description;
                        je.BatchModule.Insert(cmbatch);

                        CurrencyInfo b_info = je.currencyinfo.Select();
                        if (b_info != null)
                        {
                            b_info.CuryID         = hist.CuryID;
                            b_info.CuryEffDate    = hist.CuryEffDate;
                            b_info.CuryRateTypeID = hist.CuryRateTypeID;
                            b_info.CuryRate       = hist.CuryRate;
                            b_info.RecipRate      = hist.RateReciprocal;
                            b_info.CuryMultDiv    = hist.CuryMultDiv;
                            je.currencyinfo.Update(b_info);
                        }
                    }
                    else
                    {
                        if (!je.BatchModule.Cache.ObjectsEqual(je.BatchModule.Current, cmbatch))
                        {
                            je.Clear();
                        }

                        je.BatchModule.Current = je.BatchModule.Search <Batch.batchNbr>(cmbatch.BatchNbr, cmbatch.Module);
                    }

                    {
                        GLTran tran = new GLTran();
                        tran.SummPost      = false;
                        tran.AccountID     = hist.AccountID;
                        tran.SubID         = hist.SubID;
                        tran.CuryDebitAmt  = 0m;
                        tran.CuryCreditAmt = 0m;

                        if (hist.AccountType == AccountType.Asset || hist.AccountType == AccountType.Expense)
                        {
                            tran.DebitAmt  = (hist.FinPtdRevalued < 0m) ? 0m : hist.FinPtdRevalued;
                            tran.CreditAmt = (hist.FinPtdRevalued < 0m) ? -1m * hist.FinPtdRevalued : 0m;
                        }
                        else
                        {
                            tran.DebitAmt  = (hist.FinPtdRevalued < 0m) ? -1m * hist.FinPtdRevalued : 0m;
                            tran.CreditAmt = (hist.FinPtdRevalued < 0m) ? 0m : hist.FinPtdRevalued;
                        }

                        tran.TranType  = "REV";
                        tran.TranClass = hist.AccountType;
                        tran.RefNbr    = string.Empty;
                        tran.TranDesc  = filter.Description;
                        FinPeriodIDAttribute.SetPeriodsByMaster <GLTran.finPeriodID>(je.GLTranModuleBatNbr.Cache, tran, filter.FinPeriodID);
                        tran.TranDate    = filter.CuryEffDate;
                        tran.CuryInfoID  = null;
                        tran.Released    = true;
                        tran.ReferenceID = null;
                        tran.ProjectID   = PM.ProjectDefaultAttribute.NonProject();

                        je.GLTranModuleBatNbr.Insert(tran);
                    }

                    foreach (GLTran tran in je.GLTranModuleBatNbr.SearchAll <Asc <GLTran.tranClass> >(new object[] { "G" }))
                    {
                        je.GLTranModuleBatNbr.Delete(tran);
                    }

                    {
                        GLTran tran = new GLTran();
                        tran.SummPost      = true;
                        tran.ZeroPost      = false;
                        tran.CuryDebitAmt  = 0m;
                        tran.CuryCreditAmt = 0m;

                        if (je.BatchModule.Current.DebitTotal > je.BatchModule.Current.CreditTotal)
                        {
                            tran.AccountID = currency.RevalGainAcctID;
                            tran.SubID     = GainLossSubAccountMaskAttribute.GetSubID <Currency.revalGainSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = 0m;
                            tran.CreditAmt = (je.BatchModule.Current.DebitTotal - je.BatchModule.Current.CreditTotal);
                        }
                        else
                        {
                            tran.AccountID = currency.RevalLossAcctID;
                            tran.SubID     = GainLossSubAccountMaskAttribute.GetSubID <Currency.revalLossSubID>(je, hist.BranchID, currency);
                            tran.DebitAmt  = (je.BatchModule.Current.CreditTotal - je.BatchModule.Current.DebitTotal);
                            tran.CreditAmt = 0m;
                        }

                        tran.TranType    = "REV";
                        tran.TranClass   = GLTran.tranClass.UnrealizedAndRevaluationGOL;
                        tran.RefNbr      = string.Empty;
                        tran.TranDesc    = filter.Description;
                        tran.Released    = true;
                        tran.ReferenceID = null;

                        je.GLTranModuleBatNbr.Insert(tran);
                    }

                    {
                        AcctHist accthist = new AcctHist();
                        accthist.BranchID    = hist.BranchID;
                        accthist.LedgerID    = hist.LedgerID;
                        accthist.AccountID   = hist.AccountID;
                        accthist.SubID       = hist.SubID;
                        accthist.FinPeriodID = filter.FinPeriodID;
                        accthist.CuryID      = hist.CuryID;
                        accthist.BalanceType = "A";

                        accthist = (AcctHist)basecache.Insert(accthist);
                        accthist.FinPtdRevalued += hist.FinPtdRevalued;
                    }

                    PXProcessing <RevaluedGLHistory> .SetProcessed();
                }

                if (je.GLTranModuleBatNbr.Cache.IsInsertedUpdatedDeleted)
                {
                    je.Save.Press();

                    if (created.Find(je.BatchModule.Current) == null)
                    {
                        created.Add(je.BatchModule.Current);
                    }
                }

                ts.Complete();
            }

            CMSetup cmsetup = PXSelect <CMSetup> .Select(je);

            for (int i = 0; i < created.Count; i++)
            {
                if (cmsetup.AutoPostOption == true)
                {
                    pg.Clear();
                    pg.PostBatchProc(created[i]);
                }
            }

            if (hasErrors)
            {
                //Clean current to prevent set exception to the last item
                PXProcessing <RevaluedGLHistory> .SetCurrentItem(null);

                throw new PXException(ErrorMessages.SeveralItemsFailed);
            }

            if (created.Count > 0)
            {
                je.BatchModule.Current = created[created.Count - 1];
                throw new PXRedirectRequiredException(je, "Preview");
            }

            decimal val = 0m;

            foreach (RevaluedGLHistory res in GLAccountList.Cache.Updated)
            {
                if ((bool)res.Selected)
                {
                    decimal sign = AccountRules.IsDEALAccount(res.AccountType) ? 1.0m : -1.0m;
                    val += sign * (decimal)res.FinPtdRevalued;
                }
            }

            if (val == 0)
            {
                throw new PXOperationCompletedWithWarningException(Messages.NoRevaluationEntryWasMade);
            }
        }
Ejemplo n.º 4
0
        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);
                }
            }
        }
Ejemplo n.º 5
0
 public static void SetPeriodsByMaster(PXCache docCache, CATran caTran, string masterPeriodID)
 {
     FinPeriodIDAttribute.SetPeriodsByMaster <CATran.finPeriodID>(docCache.Graph.Caches[typeof(CATran)], caTran, masterPeriodID);
 }
        private Batch BuildReclassificationBatch(IReadOnlyCollection <ReclassificationItem> transForReclassItems,
                                                 List <GLTran> transMovedFromExistingBatch,
                                                 Batch batchForEditing = null)
        {
            DateTime earliestNewTranDate = DateTime.MaxValue;
            Batch    batch;

            var representativeTranForReclass = transForReclassItems.First().HeadTranForReclass;

            JournalEntryInstance.BatchModule.Current = null;

            if (batchForEditing == null)
            {
                batch = JournalEntryInstance.BatchModule.Insert(new Batch()
                {
                    BranchID    = representativeTranForReclass.NewBranchID,
                    FinPeriodID = representativeTranForReclass.NewFinPeriodID
                });
            }
            else
            {
                batch = batchForEditing;
                JournalEntryInstance.BatchModule.Current = batch;
            }

            //adding or editing of transactions
            foreach (var reclassItem in transForReclassItems)
            {
                IEnumerable <GLTranForReclassification> headTranForReclass = IsReclassAttrChanged(reclassItem.HeadTranForReclass)
                                                                                                                                                                ? reclassItem.HeadTranForReclass.SingleToArray().Union(reclassItem.SplittingTransForReclass)
                                                                                                                                                                : new GLTranForReclassification[0];

                IEnumerable <GLTranForReclassification> tranForReclassToProcess = headTranForReclass.Union(reclassItem.SplittingTransForReclass);

                foreach (var tranForReclass in tranForReclassToProcess)
                {
                    GLTran reclassifyingTran = CreateOrEditReclassTranPair(tranForReclass, reclassItem, transMovedFromExistingBatch);

                    if (reclassifyingTran.TranDate < earliestNewTranDate)
                    {
                        earliestNewTranDate = reclassifyingTran.TranDate.Value;
                    }
                }
            }

            //remove deleted trans
            if (batchForEditing != null)
            {
                foreach (var tranForReclassToDel in State.GLTranForReclassToDelete)
                {
                    var reverseTran       = LocateReverseTran(JournalEntryInstance.GLTranModuleBatNbr.Cache, tranForReclassToDel);
                    var reclassifyingTran = LocateReclassifyingTran(JournalEntryInstance.GLTranModuleBatNbr.Cache, tranForReclassToDel);

                    JournalEntryInstance.GLTranModuleBatNbr.Delete(reclassifyingTran);
                    JournalEntryInstance.GLTranModuleBatNbr.Delete(reverseTran);
                }
            }

            //creating and editing of batch header
            if (batchForEditing == null)
            {
                var batchCuryInfo = PXCache <CurrencyInfo> .CreateCopy(transForReclassItems.First().CuryInfo);

                batchCuryInfo.CuryInfoID = null;
                batchCuryInfo            = JournalEntryInstance.currencyinfo.Insert(batchCuryInfo);

                batch.BatchType = BatchTypeCode.Reclassification;
                batch.BranchID  = representativeTranForReclass.NewBranchID;
                FinPeriodIDAttribute.SetPeriodsByMaster <Batch.finPeriodID>(JournalEntryInstance.BatchModule.Cache, batch,
                                                                            representativeTranForReclass.TranPeriodID);
                batch.LedgerID   = representativeTranForReclass.LedgerID;
                batch.Module     = BatchModule.GL;
                batch.CuryInfoID = batchCuryInfo.CuryInfoID;
            }
            else
            {
                CurrencyInfo firstCuryInfo = null;
                int?         minLineNbr    = int.MaxValue;

                foreach (var item in transForReclassItems)
                {
                    var reclassTran = item.ReclassifyingTrans.GetItemWithMin(m => m.LineNbr.Value);
                    if (minLineNbr > reclassTran.LineNbr)
                    {
                        minLineNbr    = reclassTran.LineNbr;
                        firstCuryInfo = item.CuryInfo;
                    }
                }

                var batchCuryInfo = PXCache <CurrencyInfo> .CreateCopy(firstCuryInfo);

                batchCuryInfo.CuryInfoID = JournalEntryInstance.currencyInfo.CuryInfoID;
                batchCuryInfo.tstamp     = JournalEntryInstance.currencyInfo.tstamp;

                JournalEntryInstance.currencyinfo.Update(batchCuryInfo);
            }

            batch.DateEntered = earliestNewTranDate;
            batch.CuryID      = representativeTranForReclass.CuryID;

            if (State.ReclassScreenMode == ReclassScreenMode.Reversing)
            {
                batch.OrigModule      = State.OrigBatchModuleToReverse;
                batch.OrigBatchNbr    = State.OrigBatchNbrToReverse;
                batch.AutoReverseCopy = true;
            }

            batch = JournalEntryInstance.BatchModule.Update(batch);

            return(batch);
        }