コード例 #1
0
        public virtual IList <DRScheduleTran> GenerateTransactions(
            DRSchedule deferralSchedule,
            DRScheduleDetail scheduleDetail)
        {
            if (deferralSchedule == null)
            {
                throw new ArgumentNullException(nameof(deferralSchedule));
            }
            if (scheduleDetail == null)
            {
                throw new ArgumentNullException(nameof(scheduleDetail));
            }

            if (PXAccess.FeatureInstalled <CS.FeaturesSet.aSC606>())
            {
                ValidateTerms(scheduleDetail);
            }
            else
            {
                ValidateTerms(deferralSchedule);
            }


            int?    organizationID = PXAccess.GetParentOrganizationID(scheduleDetail.BranchID);
            decimal defAmount      = scheduleDetail.TotalAmt.Value;

            List <DRScheduleTran> list = new List <DRScheduleTran>();

            short lineCounter = 0;

            if (_code.ReconNowPct.Value > 0)
            {
                decimal recNowRaw = defAmount * _code.ReconNowPct.Value * 0.01m;
                decimal recNow    = _roundingFunction(recNowRaw);
                defAmount -= recNow;

                lineCounter++;

                DRScheduleTran nowTran = new DRScheduleTran
                {
                    BranchID     = scheduleDetail.BranchID,
                    AccountID    = scheduleDetail.AccountID,
                    SubID        = scheduleDetail.SubID,
                    Amount       = recNow,
                    RecDate      = deferralSchedule.DocDate,
                    FinPeriodID  = scheduleDetail.FinPeriodID,
                    TranPeriodID = scheduleDetail.TranPeriodID,

                    LineNbr       = lineCounter,
                    DetailLineNbr = scheduleDetail.DetailLineNbr,
                    ScheduleID    = scheduleDetail.ScheduleID,
                    ComponentID   = scheduleDetail.ComponentID,
                    Status        = GetStatus(),
                };

                list.Add(nowTran);
            }

            bool isFlexibleDeferralCode = DeferredMethodType.RequiresTerms(_code.Method);

            DateTime?termStartDate = null;
            DateTime?termEndDate   = null;

            if (isFlexibleDeferralCode)
            {
                bool isASC606 = PXAccess.FeatureInstalled <CS.FeaturesSet.aSC606>();
                termStartDate = isASC606 ? scheduleDetail.TermStartDate.Value : deferralSchedule.TermStartDate.Value;
                termEndDate   = isASC606 ? scheduleDetail.TermEndDate.Value : deferralSchedule.TermEndDate.Value;
            }

            DateTime  documentDate      = deferralSchedule.DocDate.Value;
            FinPeriod documentFinPeriod = _finPeriodRepository.FindFinPeriodByDate(documentDate, organizationID);
            int       occurrences       = isFlexibleDeferralCode ? CalcOccurrences(termStartDate.Value, termEndDate.Value, organizationID) : _code.Occurrences.Value;

            List <DRScheduleTran> deferredList   = new List <DRScheduleTran>(_code.Occurrences.Value);
            FinPeriod             deferredPeriod = null;

            for (int i = 0; i < occurrences; i++)
            {
                try
                {
                    if (deferredPeriod == null)
                    {
                        deferredPeriod = isFlexibleDeferralCode ?
                                         _finPeriodRepository.FindFinPeriodByDate(termStartDate, organizationID):
                                         _finPeriodRepository.GetOffsetPeriod(scheduleDetail.FinPeriodID, _code.StartOffset.Value, organizationID);
                    }
                    else
                    {
                        deferredPeriod = _finPeriodRepository.GetOffsetPeriod(deferredPeriod.FinPeriodID, _code.Frequency.Value, organizationID);
                    }
                }
                catch (PXFinPeriodException ex)
                {
                    throw new PXException(ex, Messages.NoFinPeriod, _code.DeferredCodeID);
                }

                lineCounter++;

                DateTime recognitionDate = GetRecognitionDate(
                    deferredPeriod.FinPeriodID,
                    minimumDate: isFlexibleDeferralCode ? termStartDate.Value : documentDate,
                    maximumDate: isFlexibleDeferralCode ? termEndDate : null,
                    organizationID: organizationID);

                FinPeriod finPeriod = _finPeriodRepository.FindFinPeriodByDate(recognitionDate, organizationID);

                DRScheduleTran deferralTransaction = new DRScheduleTran
                {
                    BranchID      = scheduleDetail.BranchID,
                    AccountID     = scheduleDetail.AccountID,
                    SubID         = scheduleDetail.SubID,
                    RecDate       = recognitionDate,
                    FinPeriodID   = finPeriod?.FinPeriodID,
                    TranPeriodID  = finPeriod?.MasterFinPeriodID,
                    LineNbr       = lineCounter,
                    DetailLineNbr = scheduleDetail.DetailLineNbr,
                    ScheduleID    = scheduleDetail.ScheduleID,
                    ComponentID   = scheduleDetail.ComponentID,
                    Status        = GetStatus(),
                };

                deferredList.Add(deferralTransaction);
            }

            SetAmounts(deferredList, defAmount, deferralSchedule.DocDate, termStartDate, termEndDate, organizationID);

            if (DeferredMethodType.RequiresTerms(_code) &&
                _code.RecognizeInPastPeriods != true)
            {
                // Adjust recognition dates and financial periods
                // that are in the past relative to the document date.
                // -
                foreach (DRScheduleTran transaction in deferredList
                         .Where(transaction => transaction.RecDate < documentDate))
                {
                    transaction.RecDate      = documentDate;
                    transaction.FinPeriodID  = documentFinPeriod?.FinPeriodID;
                    transaction.TranPeriodID = documentFinPeriod?.MasterFinPeriodID;
                }
            }

            list.AddRange(deferredList);

            return(list);
        }