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.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 = 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.TranPeriodID = 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 = 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; } } }
protected virtual void ReleaseBatchDetailsProc(GLVoucherBatch aBatch, List <Batch> toPost, Dictionary <Guid, CAMessage> errors) { int toProcessCount = 0; List <Guid> failed = new List <Guid>(); Dictionary <string, IList> processQueue = new Dictionary <string, IList>(); foreach (PXResult <GLVoucher, AP.APRegister, AR.ARRegister, CAAdj, Batch> it in this.Details.Select(aBatch.WorkBookID, aBatch.VoucherBatchNbr)) { GLVoucher iVoucher = it; Guid key = iVoucher.RefNoteID.Value; try { switch (iVoucher.Module) { case GL.BatchModule.AP: AP.APRegister iAPR = it; if (iAPR == null || String.IsNullOrEmpty(iAPR.RefNbr)) { throw new PXException(GL.Messages.DocumentIsInvalidForVoucher, iVoucher.RefNbr, iVoucher.VoucherBatchNbr); } if (iAPR.Released != true) { toProcessCount++; List <AP.APRegister> list = new List <AP.APRegister>(1); list.Add(iAPR); if (iAPR.Scheduled == true) { throw new PXException(Messages.CannotReleaseScheduled); } AP.APDocumentRelease.ReleaseDoc(list, false, false, toPost); } break; case GL.BatchModule.AR: AR.ARRegister iARR = it; if (iARR == null || String.IsNullOrEmpty(iARR.RefNbr)) { throw new PXException(GL.Messages.DocumentIsInvalidForVoucher, iVoucher.RefNbr, iVoucher.VoucherBatchNbr); } if (iARR.Released != true) { toProcessCount++; List <AR.ARRegister> list = new List <AR.ARRegister>(1); list.Add(iARR); if (iARR.Scheduled == true) { throw new PXException(Messages.CannotReleaseScheduled); } AR.ARDocumentRelease.ReleaseDoc(list, false, toPost, null); } break; case GL.BatchModule.CA: CAAdj iCAR = it; if (iCAR == null || String.IsNullOrEmpty(iCAR.AdjRefNbr)) { throw new PXException(GL.Messages.DocumentIsInvalidForVoucher, iVoucher.RefNbr, iVoucher.VoucherBatchNbr); } if (iCAR.Released != true) { toProcessCount++; CATrxRelease.ReleaseDoc <CAAdj>(iCAR, 0, toPost); } break; case GL.BatchModule.GL: Batch iGL = it; if (iGL == null || String.IsNullOrEmpty(iGL.BatchNbr)) { throw new PXException(GL.Messages.DocumentIsInvalidForVoucher, iVoucher.RefNbr, iVoucher.VoucherBatchNbr); } if (iGL.Released != true) { toProcessCount++; List <Batch> list = new List <Batch>(1); list.Add(iGL); if (iGL.Scheduled == true) { throw new PXException(Messages.CannotReleaseScheduled); } JournalEntry.ReleaseBatch(list, toPost); } break; default: throw new PXException(GL.Messages.ModuleIsNotSupported, iVoucher.Module); } } catch (Exception ex) { failed.Add(key); //prevent process from stoping on first error errors.Add(key, FormatError(ex)); } } if (failed.Count > 0) { throw new PXException(Messages.ReleasingOfSomeOfTheIncludedDocumentsFailed, failed.Count, toProcessCount); } }