Пример #1
0
        /// <summary>
        /// Returns the appropriate recognition date in a given financial period,
        /// taking into account the <see cref="DRScheduleOption"/> settings of the
        /// deferral code, as well as the absolute recognition date boundaries provided
        /// as arguments to this method.
        /// </summary>
        /// <param name="finPeriod">The financial period in which recognition must happen.</param>
        /// <param name="minimumDate">The earliest date where recognition is allowed.</param>
        /// <param name="maximumDate">The latest date where recognition is allowed.</param>
        /// <returns></returns>
        protected DateTime GetRecognitionDate(string finPeriod, DateTime minimumDate, DateTime?maximumDate, int?organizationID)
        {
            DateTime date = minimumDate;

            switch (_code.ScheduleOption)
            {
            case DRScheduleOption.ScheduleOptionStart:
                date = _finPeriodRepository.PeriodStartDate(finPeriod, organizationID);
                break;

            case DRScheduleOption.ScheduleOptionEnd:
                date = _finPeriodRepository.PeriodEndDate(finPeriod, organizationID);
                break;

            case DRScheduleOption.ScheduleOptionFixedDate:
                DateTime startDate = _finPeriodRepository.PeriodStartDate(finPeriod, organizationID);
                DateTime endDate   = _finPeriodRepository.PeriodEndDate(finPeriod, organizationID);

                if (_code.FixedDay.Value <= startDate.Day)
                {
                    date = startDate;
                }
                else if (_code.FixedDay.Value >= endDate.Day)
                {
                    date = endDate;
                }
                else
                {
                    date = new DateTime(startDate.Year, startDate.Month, _code.FixedDay.Value);
                }
                break;
            }

            if (date < minimumDate && _code.StartOffset >= 0)
            {
                return(minimumDate);
            }
            else if (date > maximumDate)
            {
                return(maximumDate.Value);
            }

            return(date);
        }
        protected virtual IEnumerable internalResultRecords()
        {
            InventoryTranHistEnqFilter filter = Filter.Current;

            bool summaryByDay      = filter.SummaryByDay ?? false;
            bool includeUnreleased = filter.IncludeUnreleased ?? false;

            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.inventoryID>(ResultRecords.Cache, null, false);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.finPerNbr>(ResultRecords.Cache, null, false);  //???
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.tranPerNbr>(ResultRecords.Cache, null, false); //???


            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.tranType>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.docRefNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.subItemID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.siteID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.locationID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.lotSerialNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible(Tran.Cache, null, !summaryByDay);

            var resultList = new List <PXResult <InventoryTranHistEnqResult, INTran> >();

            if (filter.InventoryID == null)
            {
                return(resultList);  //empty
            }

            PXSelectBase <INTranSplit> cmd = new PXSelectReadonly2 <INTranSplit,
                                                                    InnerJoin <INTran,
                                                                               On <INTran.docType, Equal <INTranSplit.docType>,
                                                                                   And <INTran.refNbr, Equal <INTranSplit.refNbr>,
                                                                                        And <INTran.lineNbr, Equal <INTranSplit.lineNbr> > > >,
                                                                               InnerJoin <INSubItem,
                                                                                          On <INSubItem.subItemID, Equal <INTranSplit.subItemID> >,
                                                                                          InnerJoin <INSite, On <INSite.siteID, Equal <INTran.siteID> > > > >,
                                                                    Where <INTranSplit.inventoryID, Equal <Current <InventoryTranHistEnqFilter.inventoryID> >, And <Match <INSite, Current <AccessInfo.userName> > > >,
                                                                    OrderBy <Asc <INTranSplit.docType, Asc <INTranSplit.refNbr, Asc <INTranSplit.lineNbr, Asc <INTranSplit.splitLineNbr> > > > > >(this);

            PXSelectBase <INItemSiteHistByPeriod> cmdBegBalance = new PXSelectReadonly2 <INItemSiteHistByPeriod,
                                                                                         InnerJoin <INItemSiteHist,
                                                                                                    On <INItemSiteHist.inventoryID, Equal <INItemSiteHistByPeriod.inventoryID>,
                                                                                                        And <INItemSiteHist.siteID, Equal <INItemSiteHistByPeriod.siteID>,
                                                                                                             And <INItemSiteHist.subItemID, Equal <INItemSiteHistByPeriod.subItemID>,
                                                                                                                  And <INItemSiteHist.locationID, Equal <INItemSiteHistByPeriod.locationID>,
                                                                                                                       And <INItemSiteHist.finPeriodID, Equal <INItemSiteHistByPeriod.lastActivityPeriod> > > > > >,
                                                                                                    InnerJoin <INSubItem,
                                                                                                               On <INSubItem.subItemID, Equal <INItemSiteHistByPeriod.subItemID> >,
                                                                                                               InnerJoin <INSite, On <INSite.siteID, Equal <INItemSiteHistByPeriod.siteID> > > > >,
                                                                                         Where <INItemSiteHistByPeriod.inventoryID, Equal <Current <InventoryTranHistEnqFilter.inventoryID> >,
                                                                                                And <INItemSiteHistByPeriod.finPeriodID, Equal <Required <INItemSiteHistByPeriod.finPeriodID> >,
                                                                                                     And <Match <INSite, Current <AccessInfo.userName> > > > > >(this);

            if (!SubCDUtils.IsSubCDEmpty(filter.SubItemCD) && PXAccess.FeatureInstalled <FeaturesSet.subItem>())
            {
                cmdBegBalance.WhereAnd <Where <INSubItem.subItemCD, Like <Current <InventoryTranHistEnqFilter.subItemCDWildcard> > > >();
                cmd.WhereAnd <Where <INSubItem.subItemCD, Like <Current <InventoryTranHistEnqFilter.subItemCDWildcard> > > >();
            }

            if (filter.SiteID != null && PXAccess.FeatureInstalled <FeaturesSet.warehouse>())
            {
                cmdBegBalance.WhereAnd <Where <INItemSiteHistByPeriod.siteID, Equal <Current <InventoryTranHistEnqFilter.siteID> > > >();
                cmd.WhereAnd <Where <INTranSplit.siteID, Equal <Current <InventoryTranHistEnqFilter.siteID> > > >();
            }

            if ((filter.LocationID ?? -1) != -1 && PXAccess.FeatureInstalled <FeaturesSet.warehouseLocation>()) // there are cases when filter.LocationID = -1
            {
                cmdBegBalance.WhereAnd <Where <INItemSiteHistByPeriod.locationID, Equal <Current <InventoryTranHistEnqFilter.locationID> > > >();
                cmd.WhereAnd <Where <INTranSplit.locationID, Equal <Current <InventoryTranHistEnqFilter.locationID> > > >();
            }

            if ((filter.LotSerialNbr ?? "") != "" && PXAccess.FeatureInstalled <FeaturesSet.lotSerialTracking>())
            {
                cmd.WhereAnd <Where <INTranSplit.lotSerialNbr, Like <Current <InventoryTranHistEnqFilter.lotSerialNbrWildcard> > > >();
            }

            if (!includeUnreleased)
            {
                cmd.WhereAnd <Where <INTranSplit.released, Equal <True> > >();
            }

            decimal  cumulativeQty = 0m;
            string   TranPeriodID;
            DateTime?PeriodStartDate;
            bool     AnyPeriod = false;

            INSite site = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(this, filter.SiteID);

            int calendarOrganizationID = PXAccess.GetParentOrganizationID(site?.BranchID) ?? FinPeriod.organizationID.MasterValue;

            try
            {
                TranPeriodID    = FinPeriodRepository.GetPeriodIDFromDate(filter.StartDate, calendarOrganizationID);
                PeriodStartDate = FinPeriodRepository.PeriodStartDate(TranPeriodID, calendarOrganizationID);
            }
            catch (PXFinPeriodException)
            {
                TranPeriodID    = null;
                PeriodStartDate = filter.StartDate;
            }

            int startRow    = 0;
            int totalRows   = 0;
            int maximumRows = 0;

            if (includeUnreleased)
            {
                PXSelectBase <OrganizationFinPeriod> periodCmd =
                    new PXSelectGroupBy <OrganizationFinPeriod,
                                         Where <OrganizationFinPeriod.finPeriodID, LessEqual <Required <OrganizationFinPeriod.finPeriodID> >,
                                                And <OrganizationFinPeriod.iNClosed, Equal <False>,
                                                     Or <OrganizationFinPeriod.finPeriodID, Equal <Required <OrganizationFinPeriod.finPeriodID> > > > >,
                                         Aggregate <GroupBy <OrganizationFinPeriod.finPeriodID> >,
                                         OrderBy <Asc <OrganizationFinPeriod.finPeriodID> > >(this);

                List <object> periodCmdParams = new List <object>()
                {
                    TranPeriodID, TranPeriodID
                };


                if (calendarOrganizationID != FinPeriod.organizationID.MasterValue)
                {
                    periodCmd.WhereAnd <Where <OrganizationFinPeriod.organizationID, Equal <Required <OrganizationFinPeriod.organizationID> > > >();

                    periodCmdParams.Add(calendarOrganizationID);
                }

                OrganizationFinPeriod firstOpenOrCurrentClosedPeriod = periodCmd.SelectWindowed(0, 1, periodCmdParams.ToArray());

                if (firstOpenOrCurrentClosedPeriod != null)
                {
                    TranPeriodID    = firstOpenOrCurrentClosedPeriod.FinPeriodID;
                    PeriodStartDate = FinPeriodRepository.PeriodStartDate(firstOpenOrCurrentClosedPeriod.FinPeriodID, calendarOrganizationID);
                }

                foreach (PXResult <INItemSiteHistByPeriod, INItemSiteHist> res in cmdBegBalance.Select(TranPeriodID))
                {
                    INItemSiteHistByPeriod byperiod = res;
                    INItemSiteHist         hist     = res;

                    cumulativeQty += string.Equals(byperiod.FinPeriodID, byperiod.LastActivityPeriod) ? (decimal)hist.TranBegQty : (decimal)hist.TranYtdQty;
                    AnyPeriod      = true;
                }

                if (AnyPeriod)
                {
                    //if StartDate is not on the Period border, make additional select with grouping
                    if (PeriodStartDate != filter.StartDate)
                    {
                        PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                               .WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Required <INTranSplit.tranDate> > > >()
                                               .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                               .AggregateNew <Aggregate <GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                        foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { PeriodStartDate, filter.StartDate }, new object[0], new string[0], new bool[0], new PXFilterRow[0], ref startRow, 0, ref totalRows))
                        {
                            INTranSplit ts_rec = res;
                            cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                        }
                    }
                }
                else
                {
                    PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                           .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                           .AggregateNew <Aggregate <GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                    foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { filter.StartDate }, new object[0], new string[0], new bool[0], new PXFilterRow[0], ref startRow, 0, ref totalRows))
                    {
                        INTranSplit ts_rec = res;
                        cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                    }
                }
            }
            else
            {
                foreach (PXResult <INItemSiteHistByPeriod, INItemSiteHist> res in cmdBegBalance.Select(TranPeriodID))
                {
                    INItemSiteHistByPeriod byperiod = res;
                    INItemSiteHist         hist     = res;

                    cumulativeQty += string.Equals(byperiod.FinPeriodID, byperiod.LastActivityPeriod) ? (decimal)hist.TranBegQty : (decimal)hist.TranYtdQty;
                    AnyPeriod      = true;
                }

                if (AnyPeriod)
                {
                    //if StartDate is not on the Period border, make additional select with grouping
                    if (PeriodStartDate != filter.StartDate)
                    {
                        PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                               .WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Required <INTranSplit.tranDate> > > >()
                                               .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                               .AggregateNew <Aggregate <GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                        foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { PeriodStartDate, filter.StartDate }, new object[0], new string[0], new bool[0], new PXFilterRow[0], ref startRow, 0, ref totalRows))
                        {
                            INTranSplit ts_rec = res;
                            cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                        }
                    }
                }
            }

            if (filter.StartDate != null)
            {
                cmd.WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Current <InventoryTranHistEnqFilter.startDate> > > >();
            }

            if (filter.EndDate != null)
            {
                cmd.WhereAnd <Where <INTranSplit.tranDate, LessEqual <Current <InventoryTranHistEnqFilter.endDate> > > >();
            }

            string[] newSortColumns;
            bool[]   newDescendings;
            AlterSorts(out newSortColumns, out newDescendings);
            PXFilterRow[] newFilters = AlterFilters();

            //if user clicks last, sorts will be inverted
            //as it is not possible to calculate beginning balance from the end
            //we will select without top from the start and then apply reverse order and select top n records
            //for next page we will ommit startrow to set beginning balance correctly
            //top (n, m) will be set in the outer search results since we do not reset PXView.StartRow to 0
            startRow    = 0;
            maximumRows = !PXView.ReverseOrder ? PXView.StartRow + PXView.MaximumRows : 0;
            totalRows   = 0;

            PXView selectView = !summaryByDay ? cmd.View
                                : new PXView(this, true, cmd.View.BqlSelect.AggregateNew <Aggregate <GroupBy <INTranSplit.tranDate, Sum <INTranSplit.qtyIn, Sum <INTranSplit.qtyOut> > > > >());

            List <object> intermediateResult = selectView.Select(PXView.Currents, new object[] { filter.StartDate }, new string[newSortColumns.Length], newSortColumns, PXView.Descendings, newFilters, ref startRow, maximumRows, ref totalRows);

            int gridLineNbr = 0;

            foreach (PXResult <INTranSplit, INTran, INSubItem> it in intermediateResult)
            {
                INTranSplit ts_rec = (INTranSplit)it;
                INTran      t_rec  = (INTran)it;

                if (ts_rec.TranDate < filter.StartDate)
                {
                    cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                }
                else
                {
                    if (summaryByDay)
                    {
                        InventoryTranHistEnqResult item = new InventoryTranHistEnqResult();
                        item.BegQty      = cumulativeQty;
                        item.TranDate    = ts_rec.TranDate;
                        item.QtyIn       = ts_rec.QtyIn;
                        item.QtyOut      = ts_rec.QtyOut;
                        item.EndQty      = item.BegQty + ts_rec.QtyIn - ts_rec.QtyOut;
                        item.GridLineNbr = ++gridLineNbr;
                        resultList.Add(new PXResult <InventoryTranHistEnqResult, INTran>(item, null));
                        cumulativeQty += (ts_rec.QtyIn - ts_rec.QtyOut) ?? 0m;
                    }
                    else
                    {
                        InventoryTranHistEnqResult item = new InventoryTranHistEnqResult();
                        item.BegQty   = cumulativeQty;
                        item.TranDate = ts_rec.TranDate;
                        item.QtyIn    = ts_rec.QtyIn;
                        item.QtyOut   = ts_rec.QtyOut;
                        item.EndQty   = item.BegQty + ts_rec.QtyIn - ts_rec.QtyOut;

                        item.InventoryID  = ts_rec.InventoryID;
                        item.TranType     = ts_rec.TranType;
                        item.DocType      = ts_rec.DocType;
                        item.DocRefNbr    = ts_rec.RefNbr;
                        item.SubItemID    = ts_rec.SubItemID;
                        item.SiteID       = ts_rec.SiteID;
                        item.LocationID   = ts_rec.LocationID;
                        item.LotSerialNbr = ts_rec.LotSerialNbr;
                        item.FinPerNbr    = t_rec.FinPeriodID;
                        item.TranPerNbr   = t_rec.TranPeriodID;
                        item.Released     = t_rec.Released;
                        item.GridLineNbr  = ++gridLineNbr;

                        decimal?unitcost;
                        if (filter.ShowAdjUnitCost ?? false)
                        {
                            unitcost = ts_rec.TotalQty != null && ts_rec.TotalQty != 0m ? (ts_rec.TotalCost + ts_rec.AdditionalCost) / ts_rec.TotalQty : 0m;
                        }
                        else
                        {
                            unitcost = ts_rec.TotalQty != null && ts_rec.TotalQty != 0m ? ts_rec.TotalCost / ts_rec.TotalQty : 0m;
                        }

                        item.UnitCost = unitcost;
                        resultList.Add(new PXResult <InventoryTranHistEnqResult, INTran>(item, t_rec));
                        cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                    }
                }
            }
            if (!PXView.ReverseOrder)
            {
                return(resultList);
            }
            return(PXView.Sort(resultList));
        }
Пример #3
0
        public virtual IEnumerable <ScheduleDet> MakeSchedule(
            Schedule scheduleParameters,
            short numberOccurrences,
            DateTime?runDate = null)
        {
            runDate = runDate ?? _graph.Accessinfo.BusinessDate;

            List <ScheduleDet> scheduleOccurrences = new List <ScheduleDet>();

            if (scheduleParameters.NextRunDate == null && scheduleParameters.LastRunDate == null)
            {
                scheduleParameters.NextRunDate = scheduleParameters.StartDate;
            }
            else if (scheduleParameters.NextRunDate == null)
            {
                scheduleParameters.NextRunDate = scheduleParameters.LastRunDate;
            }

            int   occurrencesCreated = 0;
            short oldRunCounter      = scheduleParameters.RunCntr ?? 0;

            do
            {
                ScheduleDet occurrence = new ScheduleDet();
                do
                {
                    switch (scheduleParameters.ScheduleType)
                    {
                    case GLScheduleType.Daily:
                        occurrence.ScheduledDate       = scheduleParameters.NextRunDate;
                        scheduleParameters.NextRunDate = ((DateTime)scheduleParameters.NextRunDate).AddDays((short)scheduleParameters.DailyFrequency);
                        break;

                    case GLScheduleType.Weekly:
                        CalcRunDatesWeekly(scheduleParameters, occurrence);
                        break;

                    case GLScheduleType.Periodically:
                        try
                        {
                            switch (scheduleParameters.PeriodDateSel)
                            {
                            case PeriodDateSelOption.PeriodStart:
                                occurrence.ScheduledDate = _finPeriodRepository.PeriodStartDate(
                                    _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                    FinPeriod.organizationID.MasterValue);

                                scheduleParameters.NextRunDate = _finPeriodRepository.PeriodStartDate(
                                    _finPeriodRepository.GetOffsetPeriodId(
                                        _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                        (short)scheduleParameters.PeriodFrequency,
                                        FinPeriod.organizationID.MasterValue),
                                    FinPeriod.organizationID.MasterValue);
                                break;

                            case PeriodDateSelOption.PeriodEnd:
                                occurrence.ScheduledDate = _finPeriodRepository.PeriodEndDate(
                                    _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                    FinPeriod.organizationID.MasterValue);

                                scheduleParameters.NextRunDate = _finPeriodRepository.PeriodEndDate(
                                    _finPeriodRepository.GetOffsetPeriodId(
                                        _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                        (short)scheduleParameters.PeriodFrequency,
                                        FinPeriod.organizationID.MasterValue),
                                    FinPeriod.organizationID.MasterValue);
                                break;

                            case PeriodDateSelOption.PeriodFixedDate:
                                occurrence.ScheduledDate = _finPeriodRepository.PeriodStartDate(
                                    _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                    FinPeriod.organizationID.MasterValue)
                                                           .AddDays((short)scheduleParameters.PeriodFixedDay - 1);

                                if (((DateTime)occurrence.ScheduledDate).CompareTo(
                                        _finPeriodRepository.PeriodEndDate(
                                            _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                            FinPeriod.organizationID.MasterValue))
                                    > 0)
                                {
                                    occurrence.ScheduledDate = _finPeriodRepository.PeriodEndDate(
                                        _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                        FinPeriod.organizationID.MasterValue);
                                }

                                DateTime?OldNextRunDate = scheduleParameters.NextRunDate;
                                scheduleParameters.NextRunDate = _finPeriodRepository.PeriodStartDate(
                                    _finPeriodRepository.GetOffsetPeriodId(
                                        _finPeriodRepository.GetPeriodIDFromDate(scheduleParameters.NextRunDate, FinPeriod.organizationID.MasterValue),
                                        (short)scheduleParameters.PeriodFrequency,
                                        FinPeriod.organizationID.MasterValue),
                                    FinPeriod.organizationID.MasterValue)
                                                                 .AddDays((short)scheduleParameters.PeriodFixedDay - 1);

                                if (((DateTime)scheduleParameters.NextRunDate).CompareTo(
                                        _finPeriodRepository.PeriodEndDate(
                                            _finPeriodRepository.GetOffsetPeriodId(
                                                _finPeriodRepository.GetPeriodIDFromDate(OldNextRunDate, FinPeriod.organizationID.MasterValue),
                                                (short)scheduleParameters.PeriodFrequency,
                                                FinPeriod.organizationID.MasterValue),
                                            FinPeriod.organizationID.MasterValue))
                                    > 0)
                                {
                                    scheduleParameters.NextRunDate = _finPeriodRepository.PeriodEndDate(
                                        _finPeriodRepository.GetOffsetPeriodId(
                                            _finPeriodRepository.GetPeriodIDFromDate(OldNextRunDate, FinPeriod.organizationID.MasterValue),
                                            (short)scheduleParameters.PeriodFrequency,
                                            FinPeriod.organizationID.MasterValue),
                                        FinPeriod.organizationID.MasterValue);
                                }
                                break;
                            }
                        }
                        catch (PXFinPeriodException)
                        {
                            if (occurrence.ScheduledDate != null &&
                                (bool)scheduleParameters.NoRunLimit == false &&
                                scheduleParameters.RunCntr + 1 == scheduleParameters.RunLimit &&
                                scheduleParameters.NextRunDate != null)
                            {
                                scheduleParameters.NextRunDate = null;
                            }
                            else
                            {
                                scheduleParameters.RunCntr = oldRunCounter;
                                throw new PXFinPeriodException(Messages.NoFinancialPeriodForNextExecutionDate);
                            }
                        }
                        break;

                    case GLScheduleType.Monthly:
                        switch (scheduleParameters.MonthlyDaySel)
                        {
                        case "D":
                            occurrence.ScheduledDate       = new PXDateTime((short)((DateTime)scheduleParameters.NextRunDate).Year, (short)((DateTime)scheduleParameters.NextRunDate).Month, (short)scheduleParameters.MonthlyOnDay);
                            scheduleParameters.NextRunDate = PXDateTime.DatePlusMonthSetDay((DateTime)occurrence.ScheduledDate, (short)scheduleParameters.MonthlyFrequency, (short)scheduleParameters.MonthlyOnDay);
                            break;

                        case "W":
                            occurrence.ScheduledDate = PXDateTime.MakeDayOfWeek((short)((DateTime)scheduleParameters.NextRunDate).Year, (short)((DateTime)scheduleParameters.NextRunDate).Month, (short)scheduleParameters.MonthlyOnWeek, (short)scheduleParameters.MonthlyOnDayOfWeek);

                            scheduleParameters.NextRunDate = new PXDateTime(((DateTime)occurrence.ScheduledDate).Year, ((DateTime)occurrence.ScheduledDate).Month, 1).AddMonths((short)scheduleParameters.MonthlyFrequency);
                            scheduleParameters.NextRunDate = PXDateTime.MakeDayOfWeek((short)((DateTime)scheduleParameters.NextRunDate).Year, (short)((DateTime)scheduleParameters.NextRunDate).Month, (short)scheduleParameters.MonthlyOnWeek, (short)scheduleParameters.MonthlyOnDayOfWeek);
                            break;
                        }
                        break;

                    default:
                        throw new PXException();
                    }
                }while (occurrence.ScheduledDate == scheduleParameters.LastRunDate);

                if (occurrence.ScheduledDate != null &&
                    ((DateTime)occurrence.ScheduledDate).CompareTo(scheduleParameters.StartDate) >= 0 &&
                    ((bool)scheduleParameters.NoEndDate || ((DateTime)occurrence.ScheduledDate).CompareTo(scheduleParameters.EndDate) <= 0) &&
                    (((DateTime)occurrence.ScheduledDate).CompareTo(runDate) <= 0) &&
                    ((bool)scheduleParameters.NoRunLimit || scheduleParameters.RunCntr < scheduleParameters.RunLimit))
                {
                    try
                    {
                        occurrence.ScheduledPeriod = _finPeriodRepository.GetPeriodIDFromDate(occurrence.ScheduledDate, FinPeriod.organizationID.MasterValue);
                    }
                    catch (PXFinPeriodException)
                    {
                        scheduleParameters.RunCntr = oldRunCounter;
                        throw new PXFinPeriodException(Messages.NoFinancialPeriodForNextExecutionDate);
                    }

                    scheduleOccurrences.Add(occurrence);
                    scheduleParameters.RunCntr++;

                    if ((bool)scheduleParameters.NoRunLimit == false && scheduleParameters.RunCntr >= scheduleParameters.RunLimit ||
                        (scheduleParameters.NextRunDate != null) && (bool)scheduleParameters.NoEndDate == false && ((DateTime)scheduleParameters.NextRunDate).CompareTo(scheduleParameters.EndDate) > 0)
                    {
                        scheduleParameters.Active      = false;
                        scheduleParameters.NextRunDate = null;
                    }

                    if (scheduleParameters.NextRunDate == null)
                    {
                        break;
                    }

                    if (++occurrencesCreated >= numberOccurrences)
                    {
                        return(scheduleOccurrences);
                    }
                }
                else
                {
                    if (((DateTime)occurrence.ScheduledDate).CompareTo(scheduleParameters.StartDate) < 0)
                    {
                        continue;
                    }
                    if (occurrence.ScheduledDate != null)
                    {
                        scheduleParameters.NextRunDate = occurrence.ScheduledDate;
                    }
                    break;
                }
            }while (true);

            return(scheduleOccurrences);
        }