public string PeriodPlusPeriodsCount(string finPeriodID, int counter, int?bookID, int?assetID)
        {
            FABookPeriodRepository.CheckNotNullOrEmptyStringContract(finPeriodID, nameof(finPeriodID));
            FABookPeriodRepository.CheckNotNullIDContract(bookID, nameof(bookID));
            FABookPeriodRepository.CheckNotNullIDContract(assetID, nameof(assetID));

            IYearSetup   setup         = FABookPeriodRepositoryHelper.FindFABookYearSetup(bookID);
            IPeriodSetup periodsInYear = FABookPeriodRepositoryHelper.FindFABookPeriodSetup(bookID).LastOrDefault();

            int organizationID = FABookPeriodRepositoryHelper.GetFABookPeriodOrganizationID(bookID, assetID);

            if (setup != null && FiscalPeriodSetupCreator.IsFixedLengthPeriod(setup.FPType) &&
                periodsInYear != null && periodsInYear.PeriodNbr != null)
            {
                return(FinPeriodUtils.OffsetPeriod(finPeriodID, counter, Convert.ToInt32(periodsInYear.PeriodNbr)));
            }
            else if (counter > 0)
            {
                PXResultset <FABookPeriod> res = PXSelect <
                    FABookPeriod,
                    Where <FABookPeriod.finPeriodID, Greater <Required <FABookPeriod.finPeriodID> >,
                           And <FABookPeriod.startDate, NotEqual <FABookPeriod.endDate>,
                                And <FABookPeriod.bookID, Equal <Required <FABookPeriod.bookID> >,
                                     And <FABookPeriod.organizationID, Equal <Required <FABookPeriod.organizationID> > > > > >,
                    OrderBy <
                        Asc <FABookPeriod.finPeriodID> > >
                                                 .SelectWindowed(Graph, 0, counter, finPeriodID, bookID, organizationID);

                if (res.Count < counter)
                {
                    throw new PXFABookPeriodException();
                }

                return(((FABookPeriod)res[res.Count - 1]).FinPeriodID);
            }
            else if (counter < 0)
            {
                PXResultset <FABookPeriod> res = PXSelect <
                    FABookPeriod,
                    Where <FABookPeriod.finPeriodID, Less <Required <FABookPeriod.finPeriodID> >,
                           And <FABookPeriod.startDate, NotEqual <FABookPeriod.endDate>,
                                And <FABookPeriod.bookID, Equal <Required <FABookPeriod.bookID> >,
                                     And <FABookPeriod.organizationID, Equal <Required <FABookPeriod.organizationID> > > > > >,
                    OrderBy <
                        Desc <FABookPeriod.finPeriodID> > >
                                                 .SelectWindowed(Graph, 0, -counter, finPeriodID, bookID, organizationID);

                if (res.Count < -counter)
                {
                    throw new PXFABookPeriodException();
                }

                return(((FABookPeriod)res[res.Count - 1]).FinPeriodID);
            }
            else
            {
                return(finPeriodID);
            }
        }
Beispiel #2
0
        public static IEnumerable <string> GetPeriodAgingBucketDescriptions(
            IFinPeriodRepository finPeriodRepository,
            DateTime currentDate,
            AgingDirection agingDirection,
            int numberOfBuckets,
            int calendarOrganizationID,
            bool usePeriodDescription)
        {
            if (finPeriodRepository == null)
            {
                throw new ArgumentNullException(nameof(finPeriodRepository));
            }
            if (numberOfBuckets <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfBuckets));
            }

            short periodStep = (short)(agingDirection == AgingDirection.Backwards ? -1 : 1);

            FinPeriod currentPeriod = finPeriodRepository.GetByID(
                finPeriodRepository.GetPeriodIDFromDate(currentDate, calendarOrganizationID),
                calendarOrganizationID);

            yield return(usePeriodDescription
                ? currentPeriod.Descr
                : FinPeriodUtils.FormatForError(currentPeriod.FinPeriodID));

            --numberOfBuckets;

            while (numberOfBuckets > 1)
            {
                currentPeriod = finPeriodRepository.GetByID(
                    finPeriodRepository.GetOffsetPeriodId(currentPeriod.FinPeriodID, periodStep, calendarOrganizationID),
                    calendarOrganizationID);

                yield return(usePeriodDescription
                                ? currentPeriod.Descr
                                : FinPeriodUtils.FormatForError(currentPeriod.FinPeriodID));

                --numberOfBuckets;
            }

            if (numberOfBuckets > 0)
            {
                yield return(PXMessages.LocalizeFormatNoPrefix(
                                 agingDirection == AgingDirection.Backwards
                                                ? Messages.BeforeMonth
                                                : Messages.AfterMonth,
                                 usePeriodDescription
                                        ? currentPeriod.Descr
                                        : FinPeriodUtils.FormatForError(currentPeriod.FinPeriodID)));
            }
        }
        protected virtual void AccountByYearFilter_RowInserted(PXCache cache, PXRowInsertedEventArgs e)
        {
            AccountByYearFilter filter = e.Row as AccountByYearFilter;

            if (filter != null)
            {
                if (!string.IsNullOrEmpty(filter.FinPeriodID))
                {
                    filter.FinYear = FinPeriodUtils.FiscalYear(filter.FinPeriodID);                     //Fill year from finPeriodID
                }

                if (string.IsNullOrEmpty(filter.FinYear))
                {
                    DateTime businessDate = Accessinfo.BusinessDate.Value;
                    filter.FinYear = businessDate.Year.ToString("0000");
                }
            }
        }
        /// <summary>
        /// Retrieves the first financial period of the year corresponding
        /// to the financial period specified.
        /// If no such period exists, returns <c>null</c>.
        /// </summary>
        public string GetFirstPeriodOfYear(string financialPeriodID)
        {
            if (financialPeriodID == null)
            {
                return(null);
            }

            PXGraph graph = PXGraph.CreateInstance <MasterFinPeriodMaint>();

            string firstPeriodOfYear = FinPeriodUtils.GetFirstFinPeriodIDOfYear(financialPeriodID);

            IFinPeriodRepository finPeriodRepository = graph.GetService <IFinPeriodRepository>();

            if (!finPeriodRepository.PeriodExists(financialPeriodID, FinPeriod.organizationID.MasterValue))
            {
                return(null);
            }

            return(FinPeriodIDFormattingAttribute.FormatForDisplay(firstPeriodOfYear));
        }
Beispiel #5
0
        public virtual IEnumerable amts(PXAdapter adapter)
        {
            AccBalanceByAssetFilter filter = Filter.Current;

            if (filter == null)
            {
                yield break;
            }

            PXSelectBase <FATran> select = new PXSelectJoin <FATran,
                                                             InnerJoin <FixedAsset, On <FixedAsset.assetID, Equal <FATran.assetID>, And <FixedAsset.recordType, Equal <FARecordType.assetType> > >,
                                                                        InnerJoin <FADetails, On <FADetails.assetID, Equal <FixedAsset.assetID> >,
                                                                                   InnerJoin <Branch, On <FixedAsset.branchID, Equal <Branch.branchID> >,
                                                                                              InnerJoin <FALocationHistoryCurrent, On <FALocationHistoryCurrent.assetID, Equal <FixedAsset.assetID> >,
                                                                                                         InnerJoin <FALocationHistory, On <FALocationHistory.assetID, Equal <FixedAsset.assetID>,
                                                                                                                                           And <FALocationHistory.periodID, Equal <FALocationHistoryCurrent.lastPeriodID>,
                                                                                                                                                And <FALocationHistory.revisionID, Equal <FALocationHistoryCurrent.lastRevisionID> > > > > > > > >,
                                                             Where <FATran.released, Equal <True>,
                                                                    And <FATran.finPeriodID, LessEqual <Current <AccBalanceByAssetFilter.periodID> >,
                                                                         And <FATran.bookID, Equal <Current <AccBalanceByAssetFilter.bookID> >,
                                                                              And2 <Where <FALocationHistory.fAAccountID, Equal <Current <AccBalanceByAssetFilter.accountID> >,
                                                                                           And <FALocationHistory.fASubID, Equal <Current <AccBalanceByAssetFilter.subID> >,
                                                                                                Or <FALocationHistory.accumulatedDepreciationAccountID, Equal <Current <AccBalanceByAssetFilter.accountID> >,
                                                                                                    And <FALocationHistory.accumulatedDepreciationSubID, Equal <Current <AccBalanceByAssetFilter.subID> > > > > >,
                                                                                    And <Where <FATran.debitAccountID, Equal <Current <AccBalanceByAssetFilter.accountID> >,
                                                                                                And <FATran.debitSubID, Equal <Current <AccBalanceByAssetFilter.subID> >,
                                                                                                     Or <FATran.creditAccountID, Equal <Current <AccBalanceByAssetFilter.accountID> >,
                                                                                                         And <FATran.creditSubID, Equal <Current <AccBalanceByAssetFilter.subID> > > > > > > > > > > >(this);

            if (PXAccess.FeatureInstalled <FeaturesSet.multipleCalendarsSupport>() || filter.OrganizationID != null)
            {
                select.WhereAnd <Where <Branch.organizationID, Equal <Current <AccBalanceByAssetFilter.organizationID> >, And <MatchWithBranch <Branch.branchID> > > >();
            }
            if (filter.BranchID != null)
            {
                select.WhereAnd <Where <Branch.branchID, Equal <Current <AccBalanceByAssetFilter.branchID> > > >();
            }

            Dictionary <int?, Amounts> dict = new Dictionary <int?, Amounts>();

            foreach (PXResult <FATran, FixedAsset, FADetails, Branch, FALocationHistoryCurrent, FALocationHistory> res in select.Select())
            {
                FATran            tran     = (FATran)res;
                FixedAsset        asset    = (FixedAsset)res;
                FADetails         details  = (FADetails)res;
                FALocationHistory location = (FALocationHistory)res;

                Amounts record = null;
                if (!dict.TryGetValue(asset.AssetID, out record))
                {
                    record = new Amounts
                    {
                        AssetID            = asset.AssetID,
                        Description        = asset.Description,
                        Status             = details.Status,
                        ClassID            = asset.ClassID,
                        DepreciateFromDate = details.DepreciateFromDate,
                        BranchID           = location.BranchID,
                        Department         = location.Department,
                        ItdAmt             = decimal.Zero,
                        YtdAmt             = decimal.Zero,
                        PtdAmt             = decimal.Zero
                    };
                }

                decimal tranAmt = tran.TranAmt ?? decimal.Zero;
                decimal amount  = tran.DebitAccountID == tran.CreditAccountID && tran.DebitSubID == tran.CreditSubID
                                        ? decimal.Zero
                                        : tran.DebitAccountID == filter.AccountID && tran.DebitSubID == filter.SubID ? tranAmt : -tranAmt;

                record.ItdAmt += amount;
                record.YtdAmt += (FinPeriodUtils.FinPeriodEqual(filter.PeriodID, tran.FinPeriodID, FinPeriodUtils.FinPeriodComparison.Year) ? amount : decimal.Zero);
                record.PtdAmt += (filter.PeriodID == tran.FinPeriodID ? amount : decimal.Zero);

                dict[asset.AssetID] = record;
            }

            foreach (Amounts amt in dict.Values)
            {
                if (amt.ItdAmt != decimal.Zero || amt.YtdAmt != decimal.Zero || amt.PtdAmt != decimal.Zero)
                {
                    yield return(amt);
                }
            }
        }
        public static string PeriodPlusPeriod(PXGraph graph, string FiscalPeriodID, int counter, int?BookID)
        {
            FABook book = PXSelect <FABook, Where <FABook.bookID, Equal <Required <FABook.bookID> > > > .Select(graph, BookID);

            IYearSetup setup;

            if (book.UpdateGL == true)
            {
                setup = (FinYearSetup)PXSelect <FinYearSetup> .Select(graph);
            }
            else
            {
                setup = (FABookYearSetup)PXSelect <FABookYearSetup, Where <FABookYearSetup.bookID, Equal <Required <FABookYearSetup.bookID> > > > .Select(graph, BookID);
            }

            IPeriodSetup periodsInYear;

            if (book.UpdateGL == true)
            {
                periodsInYear = (FinPeriodSetup)PXSelectGroupBy <FinPeriodSetup, Where <FinPeriodSetup.endDate, Greater <FinPeriodSetup.startDate> >,
                                                                 Aggregate <Max <FinPeriodSetup.periodNbr> > > .Select(graph);
            }
            else
            {
                periodsInYear = (FABookPeriodSetup)PXSelectGroupBy <FABookPeriodSetup, Where <FABookPeriodSetup.endDate, Greater <FABookPeriodSetup.startDate>, And <FABookPeriodSetup.bookID, Equal <Required <FABookPeriodSetup.bookID> > > >,
                                                                    Aggregate <Max <FABookPeriodSetup.periodNbr> > > .Select(graph, BookID);
            }

            if (setup != null && FiscalPeriodSetupCreator.IsFixedLengthPeriod(setup.FPType) &&
                periodsInYear != null && periodsInYear.PeriodNbr != null)
            {
                return(FinPeriodUtils.OffsetPeriod(FiscalPeriodID, counter, Convert.ToInt32(periodsInYear.PeriodNbr)));
            }
            else if (counter > 0)
            {
                // TODO: AC-106141
                // Use BranchID (OrganizationID) passed as parameter
                PXResultset <FABookPeriod> res = PXSelect <
                    FABookPeriod,
                    Where <FABookPeriod.finPeriodID, Greater <Required <FABookPeriod.finPeriodID> >,
                           And <FABookPeriod.startDate, NotEqual <FABookPeriod.endDate>,
                                And <FABookPeriod.bookID, Equal <Required <FABookPeriod.bookID> > > > >,
                    OrderBy <
                        Asc <FABookPeriod.finPeriodID> > >
                                                 .SelectWindowed(graph, 0, counter, FiscalPeriodID, BookID);

                if (res.Count < counter)
                {
                    throw new PXFABookPeriodException();
                }

                return(((FABookPeriod)res[res.Count - 1]).FinPeriodID);
            }
            else if (counter < 0)
            {
                // TODO: AC-106141
                // Use BranchID (OrganizationID) passed as parameter
                PXResultset <FABookPeriod> res = PXSelect <
                    FABookPeriod,
                    Where <FABookPeriod.finPeriodID, Less <Required <FABookPeriod.finPeriodID> >,
                           And <FABookPeriod.startDate, NotEqual <FABookPeriod.endDate>,
                                And <FABookPeriod.bookID, Equal <Required <FABookPeriod.bookID> > > > >,
                    OrderBy <
                        Desc <FABookPeriod.finPeriodID> > >
                                                 .SelectWindowed(graph, 0, -counter, FiscalPeriodID, BookID);

                if (res.Count < -counter)
                {
                    throw new PXFABookPeriodException();
                }

                return(((FABookPeriod)res[res.Count - 1]).FinPeriodID);
            }
            else
            {
                return(FiscalPeriodID);
            }
        }
Beispiel #7
0
        protected virtual void GLAllocation_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
        {
            GLAllocation row = (GLAllocation)e.Row;

            if ((e.Operation & PXDBOperation.Command) != PXDBOperation.Delete)
            {
                if (row.Active == true && (this.Source.Select().Count == 0))
                {
                    throw new PXException(Messages.SourceAccountNotSpecified);
                }

                //In the case of external rule Distribution is defined dynamically in external table
                if (row.Active == true &&
                    row.AllocMethod != Constants.AllocationMethod.ByExternalRule &&
                    this.Destination.Select().Count == 0)
                {
                    throw new PXException(Messages.DestAccountNotSpecified);
                }

                if (row.StartFinPeriodID != null && row.EndFinPeriodID != null)
                {
                    int startYr, endYr;
                    int startNbr, endNbr;
                    if (!FinPeriodUtils.TryParse(row.StartFinPeriodID, out startYr, out startNbr))
                    {
                        cache.RaiseExceptionHandling <GLAllocation.startFinPeriodID>(e.Row, row.StartFinPeriodID, new PXSetPropertyException(Messages.AllocationStartPeriodHasIncorrectFormat));
                    }

                    if (!FinPeriodUtils.TryParse(row.EndFinPeriodID, out endYr, out endNbr))
                    {
                        cache.RaiseExceptionHandling <GLAllocation.endFinPeriodID>(e.Row, row.EndFinPeriodID, new PXSetPropertyException(Messages.AllocationEndPeriodHasIncorrectFormat));
                    }
                    bool isReversed = (endYr < startYr) || (endYr == startYr && startNbr > endNbr);
                    if (isReversed)
                    {
                        cache.RaiseExceptionHandling <GLAllocation.endFinPeriodID>(e.Row, row.EndFinPeriodID, new PXSetPropertyException(Messages.AllocationEndPeriodIsBeforeStartPeriod));
                    }
                }
                if (!this.ValidateSrcAccountsForCurrency())
                {
                    throw new PXException(Messages.AccountsNotInBaseCury);
                }

                GLAllocationSource src;
                if (!this.ValidateSrcAccountsForInterlacing(out src))
                {
                    this.Source.Cache.RaiseExceptionHandling <GLAllocationSource.accountCD>(src, src.AccountCD, new PXSetPropertyException(Messages.AllocationSourceAccountSubInterlacingDetected, PXErrorLevel.RowError));
                }

                if (row.Active == true &&
                    this.isWeigthRecalcRequired())
                {
                    decimal total = 0.00m;
                    GLAllocationDestination dest = null;
                    foreach (GLAllocationDestination iDest in this.Destination.Select())
                    {
                        if (iDest.Weight.HasValue)
                        {
                            total += iDest.Weight.Value;
                            decimal weight = (decimal)iDest.Weight.Value;
                            if (weight <= 0.00m || weight > 100.00m)
                            {
                                this.Destination.Cache.RaiseExceptionHandling <GLAllocationDestination.weight>(iDest, iDest.Weight,
                                                                                                               new PXSetPropertyException(PXMessages.LocalizeFormatNoPrefix(Messages.ValueForWeight, 0, 100)));
                                return;
                            }
                        }
                        if (dest == null)
                        {
                            dest = iDest;
                        }
                    }
                    if (Math.Abs(total - 100.0m) >= 0.000001m)
                    {
                        if (dest != null)
                        {
                            this.Destination.Cache.RaiseExceptionHandling <GLAllocationDestination.weight>(dest, dest.Weight,
                                                                                                           new PXSetPropertyException(Messages.SumOfDestsMustBe100));
                        }
                        else
                        {
                            throw new PXException(Messages.SumOfDestsMustBe100);
                        }
                    }
                }
            }
        }