コード例 #1
0
        public void CreateBatch(JournalEntry je, string periodId, int?ledgerID, int?branchID, GLConsolSetup item)
        {
            je.Clear();

            je.glsetup.Current.RequireControlTotal = false;

            Ledger ledger = PXSelect <Ledger,
                                      Where <Ledger.ledgerID, Equal <Required <Ledger.ledgerID> > > >
                            .Select(this, ledgerID);

            FinPeriod finPeriod = FinPeriodRepository.FindByID(PXAccess.GetParentOrganizationID(branchID), periodId);

            if (finPeriod == null)
            {
                throw new FiscalPeriodInvalidException(periodId);
            }

            je.Accessinfo.BusinessDate = finPeriod.EndDate.Value.AddDays(-1);

            CurrencyInfo info = new CurrencyInfo();

            info.CuryID      = ledger.BaseCuryID;
            info.CuryEffDate = finPeriod.EndDate.Value.AddDays(-1);
            info.CuryRate    = (decimal)1.0;
            info             = je.currencyinfo.Insert(info);

            Batch batch = new Batch();

            batch.BranchID    = branchID;
            batch.LedgerID    = ledgerID;
            batch.Module      = BatchModule.GL;
            batch.Hold        = false;
            batch.Released    = false;
            batch.CuryID      = ledger.BaseCuryID;
            batch.CuryInfoID  = info.CuryInfoID;
            batch.FinPeriodID = periodId;
            batch.CuryID      = ledger.BaseCuryID;
            batch.BatchType   = BatchTypeCode.Consolidation;
            batch.Description = PXMessages.LocalizeFormatNoPrefix(Messages.ConsolidationBatch, item.Description);
            batch             = je.BatchModule.Insert(batch);

            foreach (GLConsolRead read in listConsolRead)
            {
                Action <decimal?, decimal?> insertTransaction = (debitAmt, creditAmt) =>
                {
                    GLTran tran = new GLTran();

                    tran.AccountID     = read.AccountID;
                    tran.SubID         = read.SubID;
                    tran.CuryInfoID    = info.CuryInfoID;
                    tran.CuryCreditAmt = creditAmt;
                    tran.CuryDebitAmt  = debitAmt;
                    tran.CreditAmt     = creditAmt;
                    tran.DebitAmt      = debitAmt;
                    tran.TranType      = GLTran.tranType.Consolidation;
                    tran.TranClass     = GLTran.tranClass.Consolidation;
                    tran.TranDate      = finPeriod.EndDate.Value.AddDays(-1);
                    tran.TranDesc      = Messages.ConsolidationDetail;
                    tran.FinPeriodID   = periodId;
                    tran.RefNbr        = "";
                    tran.ProjectID     = PM.ProjectDefaultAttribute.NonProject();
                    tran = je.GLTranModuleBatNbr.Insert(tran);

                    if (tran != null && tran.SubID == null && read.MappedValue != null)
                    {
                        je.GLTranModuleBatNbr.SetValueExt <GLTran.subID>(tran, read.MappedValue);
                    }

                    if (tran == null || tran.AccountID == null || tran.SubID == null)
                    {
                        throw new PXException(Messages.AccountOrSubNotFound, read.AccountCD, read.MappedValue);
                    }
                };

                if (Math.Abs((decimal)read.ConsolAmtDebit) > 0)
                {
                    insertTransaction(read.ConsolAmtDebit, 0m);
                }

                if (Math.Abs((decimal)read.ConsolAmtCredit) > 0)
                {
                    insertTransaction(0m, read.ConsolAmtCredit);
                }
            }

            item.LastPostPeriod = finPeriod.FinPeriodID;
            item.LastConsDate   = DateTime.Now;
            je.Caches[typeof(GLConsolSetup)].Update(item);
            if (!je.Views.Caches.Contains(typeof(GLConsolSetup)))
            {
                je.Views.Caches.Add(typeof(GLConsolSetup));
            }
            GLConsolBatch cb = new GLConsolBatch();

            cb.SetupID = item.SetupID;
            je.Caches[typeof(GLConsolBatch)].Insert(cb);
            if (!je.Views.Caches.Contains(typeof(GLConsolBatch)))
            {
                je.Views.Caches.Add(typeof(GLConsolBatch));
            }

            try
            {
                je.Save.Press();
            }
            catch (PXException e)
            {
                try
                {
                    if (!String.IsNullOrEmpty(PXUIFieldAttribute.GetError <Batch.curyCreditTotal>(je.BatchModule.Cache, je.BatchModule.Current)) ||
                        !String.IsNullOrEmpty(PXUIFieldAttribute.GetError <Batch.curyDebitTotal>(je.BatchModule.Cache, je.BatchModule.Current)))
                    {
                        je.BatchModule.Current.Hold = true;
                        je.BatchModule.Update(je.BatchModule.Current);
                    }
                    je.Save.Press();
                    if (exception == null)
                    {
                        exception = new PXException(Messages.ConsolidationBatchOutOfBalance, je.BatchModule.Current.BatchNbr);
                    }
                    else
                    {
                        exception = new PXException(exception.Message + Messages.ConsolidationBatchOutOfBalance, je.BatchModule.Current.BatchNbr);
                    }
                }
                catch
                {
                    throw e;
                }
            }
        }