protected virtual IEnumerable consolRecords( [PXDBString] string ledgerCD, [PXDBString] string branchCD ) { Ledger ledger = PXSelect <Ledger, Where <Ledger.consolAllowed, Equal <boolTrue>, And <Ledger.ledgerCD, Equal <Required <Ledger.ledgerCD> > > > > .Select(this, ledgerCD); Branch branch = PXSelect <Branch, Where <Branch.branchCD, Equal <Required <Branch.branchCD> > > > .Select(this, branchCD); if (ledger == null) { throw new PXException(Messages.CantFindConsolidationLedger, ledgerCD); } if (!string.IsNullOrEmpty(branchCD) && branch == null) { throw new PXException(Messages.CantFindConsolidationBranch, branchCD); } if (true) { InitSegmentData(null); PXSelectBase <GLHistory> cmd = new PXSelectJoin <GLHistory, InnerJoin <Account, On <Account.accountID, Equal <GLHistory.accountID> >, InnerJoin <Sub, On <Sub.subID, Equal <GLHistory.subID> >, InnerJoin <Ledger, On <Ledger.ledgerID, Equal <GLHistory.ledgerID> >, InnerJoin <Branch, On <Branch.branchID, Equal <GLHistory.branchID> > > > > >, Where <Ledger.ledgerCD, Equal <Required <Ledger.ledgerCD> >, And <GLHistory.accountID, NotEqual <Current <GLSetup.ytdNetIncAccountID> > > >, OrderBy <Asc <GLHistory.finPeriodID, Asc <Account.accountCD, Asc <Sub.subCD> > > > >(this); if (!string.IsNullOrEmpty(branchCD)) { cmd.WhereAnd <Where <Branch.branchCD, Equal <Required <Branch.branchCD> > > >(); } foreach (PXResult <GLHistory, Account, Sub> result in cmd.Select(ledgerCD, branchCD)) { GLHistory history = result; Account account = result; Sub sub = result; string accountCD = account.GLConsolAccountCD; string subCD = GetMappedValue(sub.SubCD); if (accountCD != null && accountCD.TrimEnd() != "" && subCD != null && subCD.TrimEnd() != "") { GLConsolData consolData = new GLConsolData(); consolData.MappedValue = subCD; consolData.AccountCD = accountCD; consolData.FinPeriodID = history.FinPeriodID; consolData = ConsolRecords.Locate(consolData); if (consolData != null) { consolData.ConsolAmtDebit += history.FinPtdDebit; consolData.ConsolAmtCredit += history.FinPtdCredit; } else { consolData = new GLConsolData(); consolData.MappedValue = subCD; consolData.AccountCD = accountCD; consolData.FinPeriodID = history.FinPeriodID; consolData.ConsolAmtDebit = history.FinPtdDebit; consolData.ConsolAmtCredit = history.FinPtdCredit; ConsolRecords.Insert(consolData); } } } } return(ConsolRecords.Cache.Inserted); }
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); Branch branch = PXSelect <Branch, Where <Branch.branchID, Equal <Required <Branch.branchID> > > > .Select(this, branchID); FinPeriod fiscalPeriod = PXSelect <FinPeriod, Where <FinPeriod.finPeriodID, Equal <Required <FinPeriod.finPeriodID> > > > . Select(this, periodId); if (fiscalPeriod == null) { throw new FiscalPeriodInvalidException(periodId); } je.Accessinfo.BusinessDate = fiscalPeriod.EndDate.Value.AddDays(-1); CurrencyInfo info = new CurrencyInfo(); info.CuryID = ledger.BaseCuryID; info.CuryEffDate = fiscalPeriod.EndDate.Value.AddDays(-1); info.CuryRate = (decimal)1.0; info = je.currencyinfo.Insert(info); Batch batch = new Batch(); batch.TranPeriodID = periodId; 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 = "C"; batch.Description = PXMessages.LocalizeFormatNoPrefix(Messages.ConsolidationBatch, item.Description); batch = je.BatchModule.Insert(batch); foreach (GLConsolRead read in listConsolRead) { if (Math.Abs((decimal)read.ConsolAmtDebit) > 0) { GLTran tran = new GLTran(); tran.AccountID = read.AccountID; tran.SubID = read.SubID; tran.CuryInfoID = info.CuryInfoID; tran.CuryCreditAmt = (decimal)0.0; tran.CuryDebitAmt = read.ConsolAmtDebit; tran.CreditAmt = (decimal)0.0; tran.DebitAmt = read.ConsolAmtDebit; tran.TranType = "CON"; tran.TranClass = "C"; tran.TranDate = fiscalPeriod.EndDate.Value.AddDays(-1); tran.TranDesc = Messages.ConsolidationDetail; tran.TranPeriodID = periodId; tran.RefNbr = ""; tran.ProjectID = PM.ProjectDefaultAttribute.NonProject(this); tran = je.GLTranModuleBatNbr.Insert(tran); if (tran.SubID == null && read.MappedValue != null) { je.GLTranModuleBatNbr.SetValueExt <GLTran.subID>(tran, read.MappedValue); } if (tran.AccountID == null || tran.SubID == null) { throw new PXException(Messages.AccountOrSubNotFound, read.AccountCD, read.MappedValue); } } if (Math.Abs((decimal)read.ConsolAmtCredit) > 0) { GLTran tran = new GLTran(); tran.AccountID = read.AccountID; tran.SubID = read.SubID; tran.CuryInfoID = info.CuryInfoID; tran.CuryCreditAmt = read.ConsolAmtCredit; tran.CuryDebitAmt = (decimal)0.0; tran.CreditAmt = read.ConsolAmtCredit; tran.DebitAmt = (decimal)0.0; tran.TranType = "CON"; tran.TranClass = "C"; tran.TranDate = fiscalPeriod.EndDate.Value.AddDays(-1); tran.TranDesc = Messages.ConsolidationDetail; tran.TranPeriodID = periodId; tran.RefNbr = ""; tran.ProjectID = PM.ProjectDefaultAttribute.NonProject(this); tran = je.GLTranModuleBatNbr.Insert(tran); if (tran.SubID == null && read.MappedValue != null) { je.GLTranModuleBatNbr.SetValueExt <GLTran.subID>(tran, read.MappedValue); } if (tran.AccountID == null || tran.SubID == null) { throw new PXException(Messages.AccountOrSubNotFound, read.AccountCD, read.MappedValue); } } } item.LastPostPeriod = periodId; 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; } } }