예제 #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);
            }
        }
예제 #2
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);
            }
        }