protected override void ValidatePeriodAndSourcesImpl(PXCache cache, object oldRow, object newRow,
                                                             bool externalCall)
        {
            int?newMainCalendarOrgID = CalendarOrganizationIDProvider.GetCalendarOrganizationID(cache.Graph, cache, newRow);
            int?oldMainCalendarOrgID = CalendarOrganizationIDProvider.GetCalendarOrganizationID(cache.Graph, cache, oldRow);

            if (HeaderMasterFinPeriodIDType != null && CalculatePeriodByHeader)
            {
                if (newMainCalendarOrgID != oldMainCalendarOrgID)
                {
                    string headerMasterPeriodID = GetHeaderMasterFinPeriodID(cache, newRow);

                    if (headerMasterPeriodID != null)
                    {
                        ProcessingResult <FinPeriod> procResult = FinPeriodRepository.GetFinPeriodByMasterPeriodID(newMainCalendarOrgID, headerMasterPeriodID);

                        if (!procResult.IsSuccess)
                        {
                            SetErrorAndResetToOldForField(
                                cache,
                                oldRow,
                                newRow,
                                CalendarOrganizationIDProvider.GetSourcesSpecification(cache, newRow).MainSpecificationItem.BranchSourceType.Name,
                                new PXSetPropertyException(procResult.GetGeneralMessage()),
                                externalCall);
                        }
                    }
                }
            }
            else
            {
                FinPeriod newMainOrgFinPeriod = GetAndValidateMainFinPeriod(cache, oldRow, newRow, externalCall);

                if (newMainOrgFinPeriod == null)
                {
                    return;
                }

                ValidateRelatedToMainFinPeriods(cache, oldRow, newRow, externalCall, newMainCalendarOrgID, oldMainCalendarOrgID, newMainOrgFinPeriod);

                ValidateNotMainRowLevelSources(cache, oldRow, newRow, externalCall, newMainOrgFinPeriod);
            }
        }
		public virtual ProcessingResult CheckFinPeriod(string finPeriodID, int? branchID)
		{
			ProcessingResult result = new ProcessingResult();
			int? organizationID = PXAccess.GetParentOrganizationID(branchID);
			FinPeriod period = FinPeriodRepository.FindByID(organizationID, finPeriodID);

			if (period == null)
			{
				result.AddErrorMessage(GL.Messages.FinPeriodDoesNotExistForCompany,
						FinPeriodIDFormattingAttribute.FormatForError(finPeriodID),
						PXAccess.GetOrganizationCD(PXAccess.GetParentOrganizationID(branchID)));
			}
			else
			{
				result = FinPeriodUtils.CanPostToPeriod(period);
			}

			if (!result.IsSuccess)
			{
				PXProcessing<THistory>.SetError(new PXException(result.GetGeneralMessage()));
			}

			return result;
		}
Exemple #3
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);
            }
        }