Esempio n. 1
0
        public static void Approve(List <GLBudgetLine> list)
        {
            bool          anyFailed = false;
            GLBudgetEntry graph     = PXGraph.CreateInstance <GLBudgetEntry>();

            graph.Views.Caches.Add(typeof(AccountHistory));

            for (int i = 0; i < list.Count; i++)
            {
                try
                {
                    GLBudgetLine article = list[i];
                    Ledger       ledger  = PXSelect <Ledger, Where <Ledger.ledgerID, Equal <Required <Ledger.ledgerID> > > > .Select(graph, article.LedgerID);

                    if (article.AllocatedAmount != article.Amount)
                    {
                        PXProcessing <GLBudgetLine> .SetError(i, Messages.BudgetArticleIsNotAllocatedProperly);

                        anyFailed = true;
                        continue;
                    }
                    Account acct = PXSelect <Account,
                                             Where <Account.accountID, Equal <Required <Account.accountID> > > >
                                   .Select(graph, article.AccountID);

                    foreach (GLBudgetLineDetail alloc in PXSelect <GLBudgetLineDetail,
                                                                   Where <GLBudgetLineDetail.ledgerID, Equal <Required <GLBudgetLineDetail.ledgerID> >,
                                                                          And <GLBudgetLineDetail.branchID, Equal <Required <GLBudgetLineDetail.branchID> >,
                                                                               And <GLBudgetLineDetail.finYear, Equal <Required <GLBudgetLineDetail.finYear> >,
                                                                                    And <GLBudgetLineDetail.accountID, Equal <Required <GLBudgetLineDetail.accountID> >,
                                                                                         And <GLBudgetLineDetail.subID, Equal <Required <GLBudgetLineDetail.subID> > > > > > > >
                             .Select(graph, article.LedgerID, article.BranchID, article.FinYear, article.AccountID, article.SubID))
                    {
                        decimal delta = (decimal)((alloc.Amount ?? 0m) - (alloc.ReleasedAmount ?? 0m));
                        if (delta != 0m)
                        {
                            AccountHistory accthist = new AccountHistory
                            {
                                BranchID    = alloc.BranchID,
                                AccountID   = alloc.AccountID,
                                FinPeriodID = alloc.FinPeriodID,
                                LedgerID    = alloc.LedgerID,
                                SubID       = alloc.SubID,
                                CuryID      = null,
                                BalanceType = ledger.BalanceType
                            };
                            accthist = (AccountHistory)graph.Caches[typeof(AccountHistory)].Insert(accthist);
                            if (accthist == null)
                            {
                                PXProcessing <GLBudgetLine> .SetError(i, Messages.BudgetApproveUnexpectedError);

                                anyFailed = true;
                            }
                            else
                            {
                                accthist.YtdBalance        += delta;
                                accthist.CuryFinYtdBalance  = accthist.YtdBalance;
                                accthist.TranYtdBalance    += delta;
                                accthist.CuryTranYtdBalance = accthist.TranYtdBalance;
                                if (acct.Type == AccountType.Asset || acct.Type == AccountType.Expense)
                                {
                                    accthist.FinPtdDebit     += delta;
                                    accthist.CuryFinPtdDebit  = accthist.FinPtdDebit;
                                    accthist.TranPtdDebit    += delta;
                                    accthist.CuryTranPtdDebit = accthist.TranPtdDebit;
                                    //accthist.FinPtdCredit += 0;
                                    accthist.CuryFinPtdCredit = accthist.FinPtdCredit;
                                    //accthist.TranPtdCredit += 0;
                                    accthist.CuryTranPtdCredit = accthist.TranPtdCredit;
                                }
                                else
                                {
                                    accthist.FinPtdCredit     += delta;
                                    accthist.CuryFinPtdCredit  = accthist.FinPtdCredit;
                                    accthist.TranPtdCredit    += delta;
                                    accthist.CuryTranPtdCredit = accthist.TranPtdCredit;
                                    //accthist.FinPtdDebit += 0;
                                    accthist.CuryFinPtdDebit = accthist.FinPtdDebit;
                                    //accthist.TranPtdDebit += 0;
                                    accthist.CuryTranPtdDebit = accthist.TranPtdDebit;
                                }
                            }
                        }
                        alloc.ReleasedAmount = alloc.Amount;
                        graph.Caches[typeof(GLBudgetLineDetail)].Update(alloc);
                    }
                    article.ReleasedAmount = article.Amount;
                    article.Released       = true;
                    graph.Caches[typeof(GLBudgetLine)].Update(article);
                    PXProcessing <GLBudgetLine> .SetInfo(i, ActionsMessages.RecordProcessed);
                }
                catch (Exception e)
                {
                    PXProcessing <GLBudgetLine> .SetError(i, e.Message);

                    throw;
                }
            }
            graph.Save.Press();
            if (anyFailed)
            {
                throw new PXException(Messages.BudgetItemsApprovalFailure);
            }
        }
		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 IEnumerable consolRecords(
			[PXDBString]
			string ledgerCD,
			[PXDBString]
			string branchCD
			)
		{
			Ledger ledger = PXSelect<Ledger,
				Where<Ledger.consolAllowed, Equal<True>,
				And<Ledger.ledgerCD, Equal<Required<Ledger.ledgerCD>>>>>.Select(this, ledgerCD);

			if (ledger == null)
			{
				throw new PXException(Messages.CantFindConsolidationLedger, ledgerCD);
			}

			Branch branch = PXSelect<Branch,
				Where<Branch.branchCD, Equal<Required<Branch.branchCD>>>>.Select(this, branchCD);

			if (!string.IsNullOrEmpty(branchCD) && branch == null)
			{
				throw new PXException(Messages.CantFindConsolidationBranch, branchCD);
			}

			if (branch != null && ledger.BalanceType != LedgerBalanceType.Report)
			{
				Organization organization = OrganizationMaint.FindOrganizationByID(this, branch.OrganizationID);

				if (organization.OrganizationType == OrganizationTypes.WithBranchesNotBalancing)
				{
					throw new PXException(Messages.BranchCannotBeConsolidated, branchCD);
				}
			}

			var exportSubaccountMapper = CreateExportSubaccountMapper();

			var noSegmentsToExport = false;
			if (PXAccess.FeatureInstalled<FeaturesSet.subAccount>())
			{
				noSegmentsToExport = SubaccountSegmentsView.Select()
					.RowCast<Segment>()
					.All(segment => segment.ConsolNumChar <= 0);
			}

			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 = exportSubaccountMapper.GetMappedSubaccountCD(sub);

				if (accountCD != null && accountCD.TrimEnd() != ""
					&& (subCD != null && subCD.TrimEnd() != "" || noSegmentsToExport))
				{
					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.MappedValueLength = subCD.Length;
						consolData.AccountCD = accountCD;
						consolData.FinPeriodID = history.FinPeriodID;
						consolData.ConsolAmtDebit = history.FinPtdDebit;
						consolData.ConsolAmtCredit = history.FinPtdCredit;
						ConsolRecords.Insert(consolData);
					}
				}
			}

			return ConsolRecords.Cache.Inserted;
		}
Esempio n. 4
0
        public static void Approve(List <GLBudgetLine> list)
        {
            bool          anyFailed  = false;
            bool          lineFailed = false;
            GLBudgetEntry graph      = PXGraph.CreateInstance <GLBudgetEntry>();

            graph.Views.Caches.Add(typeof(AccountHistory));
            PXCache <AccountHistory>     cacheAccountHistory     = graph.Caches <AccountHistory>();
            PXCache <GLBudgetLine>       cacheGLBudgetLine       = graph.Caches <GLBudgetLine>();
            PXCache <GLBudgetLineDetail> cacheGLBudgetLineDetail = graph.Caches <GLBudgetLineDetail>();

            for (int i = 0; i < list.Count; i++)
            {
                try
                {
                    lineFailed = false;
                    GLBudgetLine article = list[i];
                    Ledger       ledger  = PXSelect <Ledger, Where <Ledger.ledgerID, Equal <Required <Ledger.ledgerID> > > > .Select(graph, article.LedgerID);

                    if (article.AllocatedAmount != article.Amount)
                    {
                        PXProcessing <GLBudgetLine> .SetError(i, Messages.BudgetArticleIsNotAllocatedProperly);

                        anyFailed = true;
                        continue;
                    }
                    Account acct = PXSelect <Account,
                                             Where <Account.accountID, Equal <Required <Account.accountID> > > >
                                   .Select(graph, article.AccountID);

                    PXResultset <GLBudgetLineDetail> allocations = PXSelect <
                        GLBudgetLineDetail,
                        Where <GLBudgetLineDetail.ledgerID, Equal <Required <GLBudgetLineDetail.ledgerID> >,
                               And <GLBudgetLineDetail.branchID, Equal <Required <GLBudgetLineDetail.branchID> >,
                                    And <GLBudgetLineDetail.finYear, Equal <Required <GLBudgetLineDetail.finYear> >,
                                         And <GLBudgetLineDetail.groupID, Equal <Required <GLBudgetLineDetail.groupID> > > > > > >
                                                                   .Select(graph, article.LedgerID, article.BranchID, article.FinYear, article.GroupID);

                    ProcessingResult validateFinPeriodsResult = ValidateFinPeriods(graph, allocations.Select(record => (GLBudgetLineDetail)record));

                    if (!validateFinPeriodsResult.IsSuccess)
                    {
                        PXProcessing <GLBudgetLine> .SetError(i, validateFinPeriodsResult.GetGeneralMessage());

                        anyFailed = true;
                        continue;
                    }
                    List <AccountHistory> listHistory = new List <AccountHistory>();

                    foreach (GLBudgetLineDetail alloc in allocations)
                    {
                        decimal delta = (decimal)((alloc.Amount ?? 0m) - (alloc.ReleasedAmount ?? 0m));
                        if (delta != 0m)
                        {
                            AccountHistory accthist = new AccountHistory
                            {
                                BranchID    = alloc.BranchID,
                                AccountID   = alloc.AccountID,
                                FinPeriodID = alloc.FinPeriodID,
                                LedgerID    = alloc.LedgerID,
                                SubID       = alloc.SubID,
                                CuryID      = null,
                                BalanceType = ledger.BalanceType
                            };
                            accthist = (AccountHistory)cacheAccountHistory.Insert(accthist);
                            if (accthist == null)
                            {
                                PXProcessing <GLBudgetLine> .SetError(i, Messages.BudgetApproveUnexpectedError);

                                lineFailed = true;
                                break;
                            }
                            else
                            {
                                accthist.YtdBalance        += delta;
                                accthist.CuryFinYtdBalance  = accthist.YtdBalance;
                                accthist.TranYtdBalance    += delta;
                                accthist.CuryTranYtdBalance = accthist.TranYtdBalance;
                                if (acct.Type == AccountType.Asset || acct.Type == AccountType.Expense)
                                {
                                    accthist.FinPtdDebit     += delta;
                                    accthist.CuryFinPtdDebit  = accthist.FinPtdDebit;
                                    accthist.TranPtdDebit    += delta;
                                    accthist.CuryTranPtdDebit = accthist.TranPtdDebit;
                                    //accthist.FinPtdCredit += 0;
                                    accthist.CuryFinPtdCredit = accthist.FinPtdCredit;
                                    //accthist.TranPtdCredit += 0;
                                    accthist.CuryTranPtdCredit = accthist.TranPtdCredit;
                                }
                                else
                                {
                                    accthist.FinPtdCredit     += delta;
                                    accthist.CuryFinPtdCredit  = accthist.FinPtdCredit;
                                    accthist.TranPtdCredit    += delta;
                                    accthist.CuryTranPtdCredit = accthist.TranPtdCredit;
                                    //accthist.FinPtdDebit += 0;
                                    accthist.CuryFinPtdDebit = accthist.FinPtdDebit;
                                    //accthist.TranPtdDebit += 0;
                                    accthist.CuryTranPtdDebit = accthist.TranPtdDebit;
                                }
                                listHistory.Add(accthist);
                            }
                        }
                    }

                    anyFailed = (anyFailed || lineFailed);
                    if (lineFailed)
                    {
                        foreach (AccountHistory accthist in listHistory)
                        {
                            cacheAccountHistory.Remove(accthist);
                        }
                        continue;
                    }

                    foreach (GLBudgetLineDetail alloc in allocations)
                    {
                        alloc.ReleasedAmount = alloc.Amount;
                        cacheGLBudgetLineDetail.Update(alloc);
                    }

                    article.ReleasedAmount = article.Amount;
                    article.Released       = true;
                    article.WasReleased    = true;
                    cacheGLBudgetLine.Update(article);
                    PXProcessing <GLBudgetLine> .SetInfo(i, ActionsMessages.RecordProcessed);
                }
                catch (Exception e)
                {
                    PXProcessing <GLBudgetLine> .SetError(i, e.Message);

                    throw;
                }
            }
            graph.Save.Press();
            if (anyFailed)
            {
                throw new PXException(Messages.BudgetItemsApprovalFailure);
            }
        }
Esempio n. 5
0
        protected virtual IEnumerable enqResult()
        {
            GLHistoryEnqFilter filter = (GLHistoryEnqFilter)this.Filter.Current;
            bool showCurrency         = filter.ShowCuryDetail.HasValue && filter.ShowCuryDetail.Value;

            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.curyID>(EnqResult.Cache, null, showCurrency);
            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.curyPtdCreditTotal>(EnqResult.Cache, null, showCurrency);
            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.curyPtdDebitTotal>(EnqResult.Cache, null, showCurrency);
            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.curyBegBalance>(EnqResult.Cache, null, showCurrency);
            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.curyEndBalance>(EnqResult.Cache, null, showCurrency);
            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.signCuryBegBalance>(EnqResult.Cache, null, showCurrency);
            PXUIFieldAttribute.SetVisible <GLHistoryEnquiryResult.signCuryEndBalance>(EnqResult.Cache, null, showCurrency);
            if (filter.LedgerID == null || filter.FinPeriodID == null)
            {
                yield break;                 //Prevent code from accessing database;
            }
            #region cmd
            PXSelectBase <GLHistoryByPeriod> cmd = new PXSelectJoinGroupBy <GLHistoryByPeriod,
                                                                            InnerJoin <Account,
                                                                                       On <GLHistoryByPeriod.accountID, Equal <Account.accountID>, And <Match <Account, Current <AccessInfo.userName> > > >,
                                                                                       InnerJoin <Sub,
                                                                                                  On <GLHistoryByPeriod.subID, Equal <Sub.subID>, And <Match <Sub, Current <AccessInfo.userName> > > >,
                                                                                                  LeftJoin <GLHistory, On <GLHistoryByPeriod.accountID, Equal <GLHistory.accountID>,
                                                                                                                           And <GLHistoryByPeriod.ledgerID, Equal <GLHistory.ledgerID>,
                                                                                                                                And <GLHistoryByPeriod.branchID, Equal <GLHistory.branchID>,
                                                                                                                                     And <GLHistoryByPeriod.subID, Equal <GLHistory.subID>,
                                                                                                                                          And <GLHistoryByPeriod.finPeriodID, Equal <GLHistory.finPeriodID> > > > > >,
                                                                                                            LeftJoin <AH, On <GLHistoryByPeriod.ledgerID, Equal <AH.ledgerID>,
                                                                                                                              And <GLHistoryByPeriod.branchID, Equal <AH.branchID>,
                                                                                                                                   And <GLHistoryByPeriod.accountID, Equal <AH.accountID>,
                                                                                                                                        And <GLHistoryByPeriod.subID, Equal <AH.subID>,
                                                                                                                                             And <GLHistoryByPeriod.lastActivityPeriod, Equal <AH.finPeriodID> > > > > > > > > >,
                                                                            Where <GLHistoryByPeriod.ledgerID, Equal <Current <GLHistoryEnqFilter.ledgerID> >,
                                                                                   And <GLHistoryByPeriod.finPeriodID, Equal <Current <GLHistoryEnqFilter.finPeriodID> >,
                                                                                        And <
                                                                                            Where2 <
                                                                                                Where <Account.accountID, NotEqual <Current <GLSetup.ytdNetIncAccountID> >, And <Where <Account.type, Equal <AccountType.asset>,
                                                                                                                                                                                        Or <Account.type, Equal <AccountType.liability> > > > >,
                                                                                                Or <Where <GLHistoryByPeriod.lastActivityPeriod, GreaterEqual <Required <GLHistoryByPeriod.lastActivityPeriod> >,
                                                                                                           And <Where <Account.type, Equal <AccountType.expense>,
                                                                                                                       Or <Account.type, Equal <AccountType.income>,
                                                                                                                           Or <Account.accountID, Equal <Current <GLSetup.ytdNetIncAccountID> > > > > > > > > > > >,
                                                                            Aggregate <
                                                                                Sum <AH.finYtdBalance,
                                                                                     Sum <AH.tranYtdBalance,
                                                                                          Sum <AH.curyFinYtdBalance,
                                                                                               Sum <AH.curyTranYtdBalance,
                                                                                                    Sum <GLHistory.finPtdDebit,
                                                                                                         Sum <GLHistory.tranPtdDebit,
                                                                                                              Sum <GLHistory.finPtdCredit,
                                                                                                                   Sum <GLHistory.tranPtdCredit,
                                                                                                                        Sum <GLHistory.finBegBalance,
                                                                                                                             Sum <GLHistory.tranBegBalance,
                                                                                                                                  Sum <GLHistory.finYtdBalance,
                                                                                                                                       Sum <GLHistory.tranYtdBalance,
                                                                                                                                            Sum <GLHistory.curyFinBegBalance,
                                                                                                                                                 Sum <GLHistory.curyTranBegBalance,
                                                                                                                                                      Sum <GLHistory.curyFinYtdBalance,
                                                                                                                                                           Sum <GLHistory.curyTranYtdBalance,
                                                                                                                                                                Sum <GLHistory.curyFinPtdCredit,
                                                                                                                                                                     Sum <GLHistory.curyTranPtdCredit,
                                                                                                                                                                          Sum <GLHistory.curyFinPtdDebit,
                                                                                                                                                                               Sum <GLHistory.curyTranPtdDebit,
                                                                                                                                                                                    GroupBy <GLHistoryByPeriod.branchID,
                                                                                                                                                                                             GroupBy <GLHistoryByPeriod.ledgerID,
                                                                                                                                                                                                      GroupBy <GLHistoryByPeriod.accountID,
                                                                                                                                                                                                               GroupBy <GLHistoryByPeriod.finPeriodID
                                                                                                                                                                                                                        > > > > > > > > > > > > > > > > > > > > > > > > > >(this);

            if (filter.LedgerID != null)
            {
                Ledger ledger = (Ledger)PXSelectorAttribute.Select <GLHistoryEnqFilter.ledgerID>(Filter.Cache, filter);
                if (ledger?.BalanceType == LedgerBalanceType.Budget)
                {
                    // we shouldn't select history from the previous years for the budget ledgers
                    cmd.WhereAnd <Where <Substring <GLHistoryByPeriod.finPeriodID, int1, int4>, Equal <Substring <GLHistoryByPeriod.lastActivityPeriod, int1, int4> > > >();
                }
            }
            if (filter.AccountID != null)
            {
                cmd.WhereAnd <Where <GLHistoryByPeriod.accountID, Equal <Current <GLHistoryEnqFilter.accountID> > > >();
            }
            if (filter.AccountClassID != null)
            {
                cmd.WhereAnd <Where <Account.accountClassID, Equal <Current <GLHistoryEnqFilter.accountClassID> > > >();
            }

            if (filter.SubID != null)
            {
                cmd.WhereAnd <Where <GLHistoryByPeriod.subID, Equal <Current <GLHistoryEnqFilter.subID> > > >();
            }

            int[] branchIDs = null;

            if (filter.BranchID != null)
            {
                cmd.WhereAnd <Where <GLHistoryByPeriod.branchID, Equal <Current <GLHistoryEnqFilter.branchID> > > >();
            }
            else if (filter.OrganizationID != null)
            {
                branchIDs = PXAccess.GetChildBranchIDs(filter.OrganizationID, false);

                cmd.WhereAnd <Where <GLHistoryByPeriod.branchID, In <Required <GLHistoryEnqFilter.branchID> >,
                                     And <MatchWithBranch <GLHistoryByPeriod.branchID> > > >();
            }

            if (!SubCDUtils.IsSubCDEmpty(filter.SubCD))
            {
                cmd.WhereAnd <Where <Sub.subCD, Like <Current <GLHistoryEnqFilter.subCDWildcard> > > >();
            }
            //cmd.WhereAnd<Where<Match<Current<AccessInfo.userName>>>>();
            #endregion
            string  yearBegPeriod = filter.BegFinPeriod;
            GLSetup glSetup       = glsetup.Current;
            bool    reverseSign   = (glSetup != null) && (glSetup.TrialBalanceSign == GLSetup.trialBalanceSign.Reversed);
            foreach (PXResult <GLHistoryByPeriod, Account, Sub, GLHistory, AH> it in cmd.Select(yearBegPeriod, branchIDs))
            {
                GLHistoryByPeriod baseview = (GLHistoryByPeriod)it;
                Account           acct     = (Account)it;
                GLHistory         ah       = (GLHistory)it;
                AH ah1 = (AH)it;
                ah.FinFlag  = filter.UseMasterCalendar != true;
                ah1.FinFlag = filter.UseMasterCalendar != true;

                if (reverseSign && acct.AccountID == glSetup.YtdNetIncAccountID)
                {
                    continue;
                }

                GLHistoryEnquiryResult item = new GLHistoryEnquiryResult();
                item.BranchID           = baseview.BranchID;
                item.LedgerID           = baseview.LedgerID;
                item.AccountID          = baseview.AccountID;
                item.AccountCD          = acct.AccountCD;
                item.Type               = acct.Type;
                item.Description        = acct.Description;
                item.LastActivityPeriod = baseview.LastActivityPeriod;
                item.PtdCreditTotal     = ah.PtdCredit ?? 0m;
                item.PtdDebitTotal      = ah.PtdDebit ?? 0m;
                item.EndBalance         = ah1.YtdBalance ?? 0m;
                item.ConsolAccountCD    = acct.GLConsolAccountCD;
                item.AccountClassID     = acct.AccountClassID;
                if (!(string.IsNullOrEmpty(ah.CuryID) && string.IsNullOrEmpty(ah1.CuryID)))
                {
                    item.CuryEndBalance     = ah1.CuryYtdBalance ?? 0m;
                    item.CuryPtdCreditTotal = ah.CuryPtdCredit ?? 0m;
                    item.CuryPtdDebitTotal  = ah.CuryPtdDebit ?? 0m;
                    item.CuryID             = string.IsNullOrEmpty(ah.CuryID) ? ah1.CuryID : ah.CuryID;
                }
                else
                {
                    item.CuryEndBalance     = null;
                    item.CuryPtdCreditTotal = null;
                    item.CuryPtdDebitTotal  = null;
                    item.CuryID             = null;
                }

                item.recalculate(true);                 // End balance is considered as correct digit - so we need to calculate begBalance base on ending one
                item.recalculateSignAmount(reverseSign);
                yield return(item);
            }
        }
Esempio n. 6
0
        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);
        }