protected virtual OrganizationFinYear GenerateSingleOrganizationFinYear(int organizationID, string startYearNumber, string startMasterFinPeriodID)
        {
            MasterFinYear   startYear            = FinPeriodRepository.FindMasterFinYearByID(startYearNumber);
            MasterFinPeriod startMasterFinPeriod = FinPeriodRepository.FindMasterFinPeriodByID(startMasterFinPeriodID);

            return(GenerateSingleOrganizationFinYear(organizationID, startYear, startMasterFinPeriod));
        }
        protected virtual OrganizationFinYear GenerateNextOrganizationFinYear(OrganizationFinYear year)
        {
            string        generatedYearNumber = $"{int.Parse(year.Year) + 1:0000}";
            MasterFinYear masterFinYear;

            while ((masterFinYear = FinPeriodRepository.FindMasterFinYearByID(generatedYearNumber, clearQueryCache: true)) == null)
            {
                MasterCalendarGraph.Clear();
                MasterCalendarGraph.GenerateCalendar(
                    FinPeriod.organizationID.MasterValue,
                    int.Parse(FinPeriodRepository.FindLastYear(FinPeriod.organizationID.MasterValue, clearQueryCache: true).Year),
                    int.Parse(generatedYearNumber));
            }

            short generatedFinPeriodsCount = (short)masterFinYear.FinPeriods;

            if (YearSetup.Current.HasAdjustmentPeriod == true)
            {
                generatedFinPeriodsCount--;
            }

            OrganizationFinPeriod lastNonAdjustmentOrgFinPeriod = this.FinPeriodRepository.FindLastNonAdjustmentOrganizationFinPeriodOfYear(year.OrganizationID, year.Year, clearQueryCache: true);
            int generatedMasterYearNumber = int.Parse(lastNonAdjustmentOrgFinPeriod.FinYear);

            List <MasterFinPeriod> masterFinPeriods;

            PXSelectBase <MasterFinPeriod> select = new PXSelectReadonly <
                MasterFinPeriod,
                Where <MasterFinPeriod.finPeriodID, Greater <Required <MasterFinPeriod.finPeriodID> >,
                       And <MasterFinPeriod.startDate, NotEqual <MasterFinPeriod.endDate> > >,
                OrderBy <
                    Asc <MasterFinPeriod.finPeriodID> > >(this);

            select.View.Clear();

            while ((masterFinPeriods = select
                                       .SelectWindowed(0, generatedFinPeriodsCount, lastNonAdjustmentOrgFinPeriod.MasterFinPeriodID)
                                       .RowCast <MasterFinPeriod>()
                                       .ToList()).Count < generatedFinPeriodsCount)
            {
                generatedMasterYearNumber++;
                MasterCalendarGraph.Clear();
                MasterCalendarGraph.GenerateCalendar(
                    FinPeriod.organizationID.MasterValue,
                    int.Parse(FinPeriodRepository.FindLastYear(FinPeriod.organizationID.MasterValue, clearQueryCache: true).Year),
                    generatedMasterYearNumber);
                select.View.Clear();
            }

            MasterFinPeriod startMasterFinPeriod = masterFinPeriods.First();

            return(GenerateSingleOrganizationFinYear((int)year.OrganizationID, masterFinYear, startMasterFinPeriod));
        }
        protected virtual OrganizationFinPeriod GenerateAdjustmentOrganizationFinPeriod(int organizationID, OrganizationFinPeriod prevFinPeriod)
        {
            (string masterYearNumber, string masterPeriodNumber) = FinPeriodUtils.ParseFinPeriodID(prevFinPeriod.FinPeriodID);
            MasterFinYear masterFinYear = FinPeriodRepository.FindMasterFinYearByID(masterYearNumber, clearQueryCache: true);
            string        adjustmentMasterFinPeriodID = $"{masterYearNumber:0000}{masterFinYear.FinPeriods:00}";

            (string yearNumber, string periodNumber) = FinPeriodUtils.ParseFinPeriodID(prevFinPeriod.FinPeriodID);
            periodNumber = $"{int.Parse(periodNumber) + 1:00}";
            OrganizationFinPeriod orgFinPeriod = new OrganizationFinPeriod
            {
                OrganizationID    = organizationID,
                FinPeriodID       = FinPeriodUtils.ComposeFinPeriodID(yearNumber, periodNumber),
                MasterFinPeriodID = adjustmentMasterFinPeriodID,
                FinYear           = yearNumber,
                PeriodNbr         = periodNumber,
                Custom            = prevFinPeriod.Custom,
                DateLocked        = prevFinPeriod.DateLocked,
                StartDate         = prevFinPeriod.EndDate,
                EndDate           = prevFinPeriod.EndDate,

                Status   = prevFinPeriod.Status,
                ARClosed = prevFinPeriod.ARClosed,
                APClosed = prevFinPeriod.APClosed,
                FAClosed = prevFinPeriod.FAClosed,
                CAClosed = prevFinPeriod.CAClosed,
                INClosed = prevFinPeriod.INClosed,

                Descr = Messages.AdjustmentPeriod,
            };

            MasterFinPeriod masterFinPeriod = FinPeriodRepository.FindMasterFinPeriodByID(adjustmentMasterFinPeriodID);

            PXDBLocalizableStringAttribute.CopyTranslations <MasterFinPeriod.descr, OrganizationFinPeriod.descr>(this, masterFinPeriod, orgFinPeriod);

            return(orgFinPeriod);
        }