/// <summary>
        /// Generates Appointments (Routes Contract) or Service Orders (Service Contract) for each TimeSlot in the [scheduleRules] List.
        /// </summary>
        public void GenerateAPPSOUpdateContracts(List <Schedule> scheduleRules, string recordType, DateTime?fromDate, DateTime?toDate, FSSchedule fsScheduleRow)
        {
            var      generator      = new TimeSlotGenerator();
            DateTime processEndDate = (DateTime)GetProcessEndDate(scheduleRules.ElementAt(0), toDate);

            var period = new Period((DateTime)fromDate, processEndDate);

            // Determines the next generationID number
            if (nextGenerationID == null)
            {
                FSProcessIdentity fsProcessIdentityRow = new FSProcessIdentity();
                fsProcessIdentityRow.ProcessType  = recordType;
                fsProcessIdentityRow.FilterFromTo = fromDate;
                fsProcessIdentityRow.FilterUpTo   = toDate;

                ProcessIdentityMaint graphProcessIdentityMaint = PXGraph.CreateInstance <ProcessIdentityMaint>();
                graphProcessIdentityMaint.processIdentityRecords.Insert(fsProcessIdentityRow);
                graphProcessIdentityMaint.Save.Press();

                nextGenerationID = graphProcessIdentityMaint.processIdentityRecords.Current.ProcessID;
            }

            List <TimeSlot> timeSlots   = generator.GenerateCalendar(period, scheduleRules, nextGenerationID);
            DateTime?       failsOnDate = null;

            // Transaction to create the appointments (Routes Contract) or Service Orders (Service Contract)
            using (PXTransactionScope ts = new PXTransactionScope())
            {
                Customer customerRow = PXSelect <Customer,
                                                 Where <
                                                     Customer.bAccountID, Equal <Required <Customer.bAccountID> > > >
                                       .Select(this, fsScheduleRow.CustomerID)
                                       .FirstOrDefault();

                if (customerRow.Status != BAccount.status.Active && customerRow.Status != BAccount.status.OneTime)
                {
                    throw new PXException(PXMessages.LocalizeFormat(AR.Messages.CustomerIsInStatus, new Customer.status.ListAttribute().ValueLabelDic[customerRow.Status]));
                }

                try
                {
                    foreach (var timeSlot in timeSlots)
                    {
                        failsOnDate = timeSlot.DateTimeBegin;

                        if (recordType == ID.RecordType_ServiceContract.SERVICE_CONTRACT)
                        {
                            bool createAppointmnet = fsScheduleRow.ScheduleGenType == ID.ScheduleGenType_ServiceContract.APPOINTMENT;

                            CreateServiceOrder(timeSlot, createAppointmnet);
                        }
                        else if (recordType == ID.RecordType_ServiceContract.ROUTE_SERVICE_CONTRACT)
                        {
                            CreateServiceOrder(timeSlot, true, true);
                        }
                    }

                    DateTime?lastGenerationDate = null;

                    if (timeSlots.Count > 0)
                    {
                        lastGenerationDate = timeSlots.Max(a => a.DateTimeBegin);
                    }

                    CreateContractGenerationHistory((int)nextGenerationID,
                                                    scheduleRules.ElementAt(0).ScheduleID,
                                                    processEndDate,
                                                    lastGenerationDate,
                                                    recordType);

                    UpdateGeneratedSchedule(scheduleRules.ElementAt(0).ScheduleID, processEndDate, lastGenerationDate, fsScheduleRow);
                }
                catch (Exception e)
                {
                    var exceptionWithContextMessage = ExceptionHelper.GetExceptionWithContextMessage(PXMessages.Localize(TX.Messages.COULD_NOT_PROCESS_RECORD), e);

                    FSGenerationLogError fsGenerationLogErrorRow = new FSGenerationLogError();
                    fsGenerationLogErrorRow.ProcessType  = recordType;
                    fsGenerationLogErrorRow.ErrorMessage = exceptionWithContextMessage.Message;
                    fsGenerationLogErrorRow.ScheduleID   = scheduleRules.ElementAt(0).ScheduleID;
                    fsGenerationLogErrorRow.GenerationID = nextGenerationID;
                    fsGenerationLogErrorRow.ErrorDate    = failsOnDate;

                    ts.Dispose();

                    var grapGenerationLogErrorMaint = PXGraph.CreateInstance <GenerationLogErrorMaint>();

                    grapGenerationLogErrorMaint.LogErrorMessageRecords.Insert(fsGenerationLogErrorRow);
                    grapGenerationLogErrorMaint.Save.Press();
                    throw exceptionWithContextMessage;
                }

                ts.Complete(this);
            }
        }
        public virtual IEnumerable GenerateProjection(PXAdapter adapter)
        {
            if (RouteAppointmentForecastingFilter.Current != null &&
                RouteAppointmentForecastingFilter.Current.DateBegin != null &&
                RouteAppointmentForecastingFilter.Current.DateEnd != null &&
                RouteAppointmentForecastingFilter.Current.DateBegin < RouteAppointmentForecastingFilter.Current.DateEnd)
            {
                DateTime beginFromFilter = RouteAppointmentForecastingFilter.Current.DateBegin.Value;
                DateTime endToFilter     = RouteAppointmentForecastingFilter.Current.DateEnd.Value;
                string   recordType      = ID.RecordType_ServiceContract.ROUTE_SERVICE_CONTRACT;

                int?serviceID          = RouteAppointmentForecastingFilter.Current.ServiceID;
                int?customerID         = RouteAppointmentForecastingFilter.Current.CustomerID;
                int?customerLocationID = RouteAppointmentForecastingFilter.Current.CustomerLocationID;
                int?routeID            = RouteAppointmentForecastingFilter.Current.RouteID;

                PXLongOperation.StartOperation(
                    this,
                    delegate
                {
                    using (PXTransactionScope ts = new PXTransactionScope())
                    {
                        if (beginFromFilter != null && endToFilter != null)
                        {
                            DateHandler requestDate = new DateHandler(endToFilter);

                            var period = new Period((DateTime)beginFromFilter, (DateTime)endToFilter);

                            List <Scheduler.Schedule> scheduleListToProcess = new List <Scheduler.Schedule>();
                            var generator = new TimeSlotGenerator();

                            var fsRouteContractScheduleRecords =
                                PXSelectJoinGroupBy <FSRouteContractSchedule,
                                                     InnerJoin <FSServiceContract,
                                                                On <FSServiceContract.serviceContractID, Equal <FSRouteContractSchedule.entityID> >,
                                                                InnerJoin <FSScheduleRoute,
                                                                           On <FSScheduleRoute.scheduleID, Equal <FSRouteContractSchedule.scheduleID> >,
                                                                           InnerJoin <FSScheduleDet,
                                                                                      On <FSScheduleDet.scheduleID, Equal <FSRouteContractSchedule.scheduleID> > > > >,
                                                     Where <
                                                         FSRouteContractSchedule.entityType, Equal <FSSchedule.entityType.Contract>,
                                                         And <FSServiceContract.recordType, Equal <FSServiceContract.recordType.RouteServiceContract>,
                                                              And <FSRouteContractSchedule.active, Equal <True>,
                                                                   And <FSServiceContract.status, Equal <FSServiceContract.status.Active>,
                                                                        And <FSRouteContractSchedule.startDate, LessEqual <Required <FSRouteContractSchedule.startDate> >,
                                                                             And2 <
                                                                                 Where <
                                                                                     Required <FSScheduleDet.inventoryID>, IsNull,
                                                                                     Or <FSScheduleDet.inventoryID, Equal <Required <FSScheduleDet.inventoryID> > > >,
                                                                                 And2 <
                                                                                     Where <
                                                                                         Required <FSRouteContractSchedule.customerID>, IsNull,
                                                                                         Or <FSRouteContractSchedule.customerID, Equal <Required <FSRouteContractSchedule.customerID> > > >,
                                                                                     And2 <
                                                                                         Where <
                                                                                             Required <FSRouteContractSchedule.customerLocationID>, IsNull,
                                                                                             Or <FSRouteContractSchedule.customerLocationID, Equal <Required <FSRouteContractSchedule.customerLocationID> > > >,
                                                                                         And2 <
                                                                                             Where <
                                                                                                 Required <FSScheduleRoute.dfltRouteID>, IsNull,
                                                                                                 Or <FSScheduleRoute.dfltRouteID, Equal <Required <FSScheduleRoute.dfltRouteID> > > >,
                                                                                             And <
                                                                                                 Where <FSRouteContractSchedule.endDate, IsNull,
                                                                                                        Or <FSRouteContractSchedule.endDate, GreaterEqual <Required <FSRouteContractSchedule.startDate> > > > > > > > > > > > > >,
                                                     Aggregate <
                                                         GroupBy <FSRouteContractSchedule.scheduleID> > >
                                .Select(this, beginFromFilter, serviceID, serviceID, customerID, customerID, customerLocationID, customerLocationID, routeID, routeID, endToFilter);

                            foreach (FSRouteContractSchedule fsRouteContractScheduleRecord in fsRouteContractScheduleRecords)
                            {
                                List <Scheduler.Schedule> mapScheduleResults = MapFSScheduleToSchedule.convertFSScheduleToSchedule(RouteAppointmentForecastingFilter.Cache, fsRouteContractScheduleRecord, endToFilter, recordType);

                                foreach (Scheduler.Schedule mapSchedule in mapScheduleResults)
                                {
                                    scheduleListToProcess.Add(mapSchedule);
                                }
                            }

                            if (recordType == ID.RecordType_ServiceContract.ROUTE_SERVICE_CONTRACT)
                            {
                                foreach (Scheduler.Schedule schedule in scheduleListToProcess)
                                {
                                    schedule.Priority      = (int?)RouteScheduleProcess.SetSchedulePriority(schedule, this);
                                    schedule.RouteInfoList = RouteScheduleProcess.getRouteListFromSchedule(schedule, this);
                                }
                            }

                            List <TimeSlot> timeSlots = generator.GenerateCalendar(period, scheduleListToProcess);

                            var fsRouteAppointmentForecastingRecords =
                                PXSelect <FSRouteAppointmentForecasting,
                                          Where <
                                              FSRouteAppointmentForecasting.startDate, GreaterEqual <Required <FSRouteAppointmentForecasting.startDate> >,
                                              And <
                                                  FSRouteAppointmentForecasting.startDate, LessEqual <Required <FSRouteAppointmentForecasting.startDate> > > > >
                                .Select(this, beginFromFilter, endToFilter);

                            PXCache <FSRouteAppointmentForecasting> cacheRouteForecasting = new PXCache <FSRouteAppointmentForecasting>(this);

                            foreach (FSRouteAppointmentForecasting fsRouteAppointmentDetailProjectionRecord in fsRouteAppointmentForecastingRecords)
                            {
                                cacheRouteForecasting.PersistDeleted(fsRouteAppointmentDetailProjectionRecord);
                            }

                            foreach (TimeSlot timeSlot in timeSlots)
                            {
                                requestDate.setDate(timeSlot.DateTimeBegin);

                                FSSchedule fsScheduleRow = ScheduleSelected.Select(timeSlot.ScheduleID);

                                FSScheduleRoute fsScheduleRouteRow = ScheduleRouteSelected.Select(fsScheduleRow.ScheduleID);

                                FSServiceContract fsServiceContractRow = ServiceContractSelected.Select(fsScheduleRow.EntityID);

                                FSRouteAppointmentForecasting fsRouteAppointmentForecastingRow = new FSRouteAppointmentForecasting();

                                fsRouteAppointmentForecastingRow.RouteID            = fsScheduleRouteRow.DfltRouteID;
                                fsRouteAppointmentForecastingRow.StartDate          = requestDate.StartOfDay();
                                fsRouteAppointmentForecastingRow.ScheduleID         = fsScheduleRow.ScheduleID;
                                fsRouteAppointmentForecastingRow.ServiceContractID  = fsServiceContractRow.ServiceContractID;
                                fsRouteAppointmentForecastingRow.CustomerLocationID = fsServiceContractRow.CustomerLocationID;
                                fsRouteAppointmentForecastingRow.CustomerID         = fsServiceContractRow.CustomerID;
                                fsRouteAppointmentForecastingRow.SequenceOrder      = int.Parse(fsScheduleRouteRow.GlobalSequence);

                                cacheRouteForecasting.PersistInserted(fsRouteAppointmentForecastingRow);
                            }
                        }

                        ts.Complete();
                    }
                });
            }

            return(adapter.Get());
        }
Пример #3
0
        /// <summary>
        /// Generates an FSTimeSlot for each TimeSlot in the [scheduleRules] List.
        /// </summary>
        protected void generateTimeSlotAndUpdateStaffSchedule(List <Schedule> scheduleRules, string recordType, DateTime?fromDate, DateTime?toDate, FSSchedule fsScheduleRow)
        {
            var      generator      = new TimeSlotGenerator();
            DateTime processEndDate = (DateTime)getProcessEndDate(scheduleRules.ElementAt(0), toDate);

            var period = new Period((DateTime)fromDate, (DateTime)toDate);

            // Determines the next generationID number
            if (nextGenerationID == null)
            {
                FSProcessIdentity fsProcessIdentityRow = new FSProcessIdentity()
                {
                    ProcessType  = recordType,
                    FilterFromTo = fromDate,
                    FilterUpTo   = toDate
                };
                var graphProcessIdentityMaint = PXGraph.CreateInstance <ProcessIdentityMaint>();

                graphProcessIdentityMaint.processIdentityRecords.Insert(fsProcessIdentityRow);

                graphProcessIdentityMaint.Save.Press();

                nextGenerationID = graphProcessIdentityMaint.processIdentityRecords.Current.ProcessID;
            }

            List <TimeSlot> timeSlots   = generator.GenerateCalendar(period, scheduleRules, nextGenerationID);
            DateTime?       failsOnDate = null;

            // Transaction to create the Time Slots
            using (PXTransactionScope ts = new PXTransactionScope())
            {
                try
                {
                    //Create all time slots
                    foreach (var timeSlot in timeSlots)
                    {
                        failsOnDate = timeSlot.DateTimeBegin;
                        createTimeSlot(timeSlot);
                    }

                    DateTime?lastGeneratedTimeSlotBySchedules = null;

                    if (timeSlots.Count > 0)
                    {
                        lastGeneratedTimeSlotBySchedules = timeSlots.Max(a => a.DateTimeBegin);
                    }

                    //Update and create a Contract Generation History
                    createContractGenerationHistory(
                        (int)nextGenerationID,
                        scheduleRules.ElementAt(0).ScheduleID,
                        processEndDate,
                        lastGeneratedTimeSlotBySchedules,
                        recordType);

                    //Update Last Generated Time Slot Date
                    updateGeneratedSchedule(scheduleRules.ElementAt(0).ScheduleID, processEndDate, lastGeneratedTimeSlotBySchedules, fsScheduleRow);
                }
                catch (PXException e)
                {
                    FSGenerationLogError fsGenerationLogError = new FSGenerationLogError()
                    {
                        ProcessType  = recordType,
                        ErrorMessage = e.Message,
                        ScheduleID   = scheduleRules.ElementAt(0).ScheduleID,
                        GenerationID = nextGenerationID,
                        ErrorDate    = failsOnDate
                    };
                    ts.Dispose();

                    var grapGenerationLogErrorMaint = PXGraph.CreateInstance <GenerationLogErrorMaint>();

                    grapGenerationLogErrorMaint.LogErrorMessageRecords.Insert(fsGenerationLogError);

                    grapGenerationLogErrorMaint.Save.Press();

                    throw new PXException(e.Message);
                }

                ts.Complete(this);
            }
        }