public List <SchedulerEvent> GetSchedulerEvents(DateTime?from, DateTime?to, int unitId)
        {
            var result       = new List <SchedulerEvent>();
            var now          = DateTime.Now;
            var dataService  = new QNomyDataService();
            var appointments = dataService.GetAppointments(from, to);

            CustomizeData customizeData  = GetCustomizeData(unitId);
            var           calendarStages = dataService.GetClendarStages(customizeData.ConfigId);
            var           statusMapping  = GetMappingCalendarStageTypeToEntityStatus();

            foreach (var app in appointments)
            {
                app.CalendarStageType = statusMapping[app.CurrentEntityStatus];
                int?stageId = GetStageByServiceId(app.ServiceId, app.CalendarStageType, calendarStages, customizeData);
                if (!stageId.HasValue)
                {
                    continue;
                }

                app.StageId = stageId.Value;
                SchedulerEvent se = ToScheduleEvent(app);
                result.Add(se);
            }
            return(result);
        }
        internal AppointmentChangedResult AppointmentCancel(int userId, int unitId, SchedulerEvent schedulerEvent)
        {
            var dataService = new QNomyDataService();
            AppointmentChangedResult result = new AppointmentChangedResult();
            int processId = schedulerEvent.ProcessId;
            ProcessCancelCaseResults cancelResult = Process.CancelCase(processId, userId, 0);


            if (cancelResult.CurrentProcessId > 0)
            {
                schedulerEvent.ProcessId = cancelResult.CurrentProcessId;


                CustomizeData customizeData = GetCustomizeData(unitId);

                var calendarStages = dataService.GetClendarStages(customizeData.ConfigId);
                AdjustStageType(schedulerEvent, calendarStages, customizeData);

                result.EventData = schedulerEvent;
            }

            List <AppUser.UserStatus> okStatus = new List <AppUser.UserStatus>()
            {
                AppUser.UserStatus.Idle, AppUser.UserStatus.Unknown, AppUser.UserStatus.SignedOut
            };

            if (!okStatus.Contains(cancelResult.UserStatus))
            {
                throw new DataException($"Not Canceled, user status {cancelResult.UserStatus.ToString()}");
            }
            return(result);
        }
        internal bool SaveCustomizeData(CustomizeData data)
        {
            if (data.UnitId <= 0)
            {
                throw new ArgumentException("unit is not defined");
            }

            int sortOrder   = 0;
            var stageIds    = new List <int>();
            var dataService = new QNomyDataService();

            if (data.ConfigId <= 0)
            {
                data.ConfigId = dataService.InsertConfigId(data.UnitId);
            }

            dataService.DeleteCalendarStageServices(data.ConfigId);
            var dataNotShownStage = dataService.GetClendarStageByType(data.ConfigId, CalendarStageType.None);

            if (dataNotShownStage == null)
            {
                dataNotShownStage = new CalendarStage()
                {
                    Name = "None", StageType = CalendarStageType.None, CalendarStageConfigId = data.ConfigId
                };
                dataNotShownStage.Id = dataService.InserCalendarStage(dataNotShownStage);
            }

            stageIds.Add(dataNotShownStage.Id);
            dataService.InsertCalendarStageServices(dataNotShownStage.Id, data.NotShownServices);

            foreach (var stage in data.Stages)
            {
                sortOrder++;
                stage.SortOrder = sortOrder;
                dataService.UpdateOrInsertStage(data.ConfigId, stage);
                stageIds.Add(stage.Id);
            }


            dataService.DeleteCalendarStagesExcept(data.ConfigId, stageIds);

            return(true);
        }
        internal List <StageInfo> GetStages(int rootUnitId)
        {
            var dataService = new QNomyDataService();
            var configId    = dataService.GetConfigId(rootUnitId);
            List <CalendarStage> calendarStages = dataService.GetClendarStages(configId);


            var result = new List <StageInfo>();

            foreach (var stage in calendarStages)
            {
                result.Add(new StageInfo {
                    Key = stage.Id, Label = stage.Name
                });
            }


            return(result);
        }
        // do not use it, this method reschedules time
        internal SchedulerEvent SaveAppointment(SchedulerEvent appointment)
        {
            var dataService      = new QNomyDataService();
            var newAppointmentId = dataService.UpdateAppointmentTime(appointment.AppointmentId, appointment.Start_date, appointment.End_date);
            var qnomyApp         = Appointment.Get(newAppointmentId);
            var app = new AppointmentInfo();

            app.AppointmentDate     = qnomyApp.AppointmentDate;
            app.AppointmentDuration = qnomyApp.AppointmentDuration;
            app.AppointmentId       = qnomyApp.Id;
            app.AppointmentTypeName = qnomyApp.AppointmentTypeName;
            app.CurrentEntityStatus = qnomyApp.CurrentEntityStatus;

            app.StageId           = appointment.StageId;
            app.CalendarStageType = appointment.StageType;

            SchedulerEvent se = ToScheduleEvent(app);

            return(se);
        }
        private RouteResultData RouteUser(int currentUserId, SchedulerEvent theEvent, QNomyDataService dataService, int delegateId, List <int> servicesInStage, bool callAfterRoute, int?routeId)
        {
            RouteResultData      result          = new RouteResultData();
            List <RouteListItem> availableRoutes = Service.GetAvailableRoutes(theEvent.ServiceId);
            int?transferedFromServiceId          = dataService.GetTranseredFromServiceId(theEvent.ProcessId);

            var  routesToStage  = new List <RouteListItem>();
            var  returnToSender = new List <RouteListItem>();
            bool skipAddRoute   = false;

            foreach (var rout in availableRoutes)
            {
                skipAddRoute = routeId.HasValue && routeId.Value > 0 && routeId.Value != rout.Id;
                if (!skipAddRoute && transferedFromServiceId.HasValue && rout.TargetType == Route.RouteTargetType.ReturnToSender && servicesInStage.Contains(transferedFromServiceId.Value))
                {
                    routesToStage.Add(rout);
                    continue;
                }

                if (!skipAddRoute && servicesInStage.Contains(rout.TargetServiceId))
                {
                    routesToStage.Add(rout);
                    continue;
                }
            }


            if (routesToStage.Count == 0)
            {
                throw new DataException("Route to this stage does not exists.");
            }

            if (routesToStage.Count > 1)
            {
                result.Selection         = new SelectOptionData();
                result.Selection.Options = new List <KeyValuePair <int, string> >();
                foreach (var item in routesToStage)
                {
                    result.Selection.Options.Add(new KeyValuePair <int, string>(item.Id, item.Name));
                }

                return(result);
            }

            var route = routesToStage[0];

            // Route operation.
            ProcessRouteResults routeResult = Process.Route(theEvent.ProcessId, currentUserId, delegateId, route.Id, route.TargetServiceId, 0, 0, "", false, 0);

            if (routeResult.Status != ProcessRouteResults.ProcessRouteResultsStatus.Success)
            {
                throw new DataException(routeResult.Status.ToString());
            }

            if (routeResult.NewServiceId > 0)
            {
                theEvent.ServiceId = routeResult.NewServiceId;
            }

            if (routeResult.NewProcessId > 0)
            {
                theEvent.ProcessId = routeResult.NewProcessId;
            }

            if (callAfterRoute && routeResult.NewEntityStatus == (int)EntityType.EntityStatus.Waiting)
            {
                try
                {
                    CallUser(currentUserId, theEvent, delegateId);
                }
                catch (DataException)
                {
                }
            }
            result.IsRouted = true;
            return(result);
        }
        internal AppointmentChangedResult AppointmentChanged(int currentUserId, int currentUnitId, int previousStageId, int nextStageId, SchedulerEvent theEvent, int?routeId)
        {
            AppointmentChangedResult result = new AppointmentChangedResult();
            var dataService = new QNomyDataService();


            CustomizeData customizeData = GetCustomizeData(currentUnitId);
            // qnomy
            var calendarStages = customizeData.Stages.ToList <CalendarStage>();

            //var allCalendarStageServices = customizeData.GetClendarStageServices();
            var             prevCalendarStageType = GetStageTypeById(previousStageId, calendarStages);
            var             nextCalendarStageType = GetStageTypeById(nextStageId, calendarStages);
            int             previousServiceId     = theEvent.ServiceId;
            RouteResultData routeResult           = null;

            int delegateId = 0;

            if ((nextCalendarStageType == CalendarStageType.InService) || (prevCalendarStageType != nextCalendarStageType))
            {
                // calendar stage type - is old stageType.
                if (prevCalendarStageType == CalendarStageType.Completed)
                {
                    throw new DataException("Already Completed, cannot change");
                }

                switch (nextCalendarStageType)
                {
                case CalendarStageType.Completed:
                    ProcessPromoteResults processPromoteResults = Process.Promote(theEvent.ProcessId, currentUserId, delegateId, Process.ProcessPromoteAction.Complete, false);
                    break;

                case CalendarStageType.Expected:
                    throw new NotSupportedException("Cannot back to expected status");

                case CalendarStageType.InService:
                    var nextStage = customizeData.Stages.Find(x => x.Id == nextStageId);
                    if (nextStage == null)
                    {
                        throw new DataException("stage is not valid");
                    }
                    var servicesInStage = GetServicesIds(nextStage.Services);

                    if (servicesInStage.Count == 0)
                    {
                        throw new DataException("Cannot find any service for this stage");
                    }

                    if (prevCalendarStageType != CalendarStageType.InService)
                    {
                        if (servicesInStage.Contains(theEvent.ServiceId))
                        {
                            CallUser(currentUserId, theEvent, delegateId);
                        }
                        else
                        {
                            routeResult = RouteUser(currentUserId, theEvent, dataService, delegateId, servicesInStage, true, routeId);
                        }
                    }
                    else
                    {
                        routeResult = RouteUser(currentUserId, theEvent, dataService, delegateId, servicesInStage, true, routeId);
                    }

                    break;

                case CalendarStageType.None:
                    throw new InvalidOperationException("Invalid stage is not defined");

                case CalendarStageType.Waiting:
                    if (prevCalendarStageType == CalendarStageType.Expected)
                    {
                        ProcessEnqueueAppointmentResults enqueResult = Process.EnqueueAppointment(theEvent.ProcessId, forceEarly: true, forceLate: true);
                        var enqueValidStatuses = new List <ProcessEnqueueAppointmentResults.ProcessEnqueueAppointmentResultsStatus>();
                        enqueValidStatuses.Add(ProcessEnqueueAppointmentResults.ProcessEnqueueAppointmentResultsStatus.Success);
                        enqueValidStatuses.Add(ProcessEnqueueAppointmentResults.ProcessEnqueueAppointmentResultsStatus.Late);
                        if (!enqueValidStatuses.Contains(enqueResult.Status))
                        {
                            throw DataException.GetDataException(enqueResult.Status);
                        }
                        break;
                    }

                    if (prevCalendarStageType == CalendarStageType.WaitingCustomerAction)
                    {
                        ProcessStopWaitingForCustomerActionResults stopWaitResult = Process.StopWaitingForCustomerAction(theEvent.ProcessId, currentUserId, delegateId);

                        break;
                    }
                    if (prevCalendarStageType == CalendarStageType.Waiting)
                    {
                        break;
                    }
                    if (prevCalendarStageType == CalendarStageType.InService)
                    {
                        ProcessRequeueInServiceResults requeResult = Process.RequeueInService(theEvent.ProcessId, currentUnitId, delegateId);
                        break;
                    }
                    throw new DataException("Waiting stage does not accessible from this stage");

                case CalendarStageType.WaitingCustomerAction:
                    ProcessWaitForCustomerActionResults waitResult = Process.WaitForCustomerAction(theEvent.ProcessId, currentUserId, 0);
                    break;

                default:
                    throw new InvalidOperationException("Stage is not supported");
                }
            }

            if (routeResult != null && !routeResult.IsRouted)
            {
                result.RouteData = routeResult;
                return(result);
            }


            AdjustStageType(theEvent, calendarStages, customizeData);

            result.EventData = theEvent;
            return(result);
        }
        internal CustomizeData GetCustomizeData(int currentUnitId)
        {
            var result      = new CustomizeData();
            var dataService = new QNomyDataService();

            result.UnitId           = currentUnitId;
            result.ConfigId         = dataService.GetConfigId(currentUnitId);
            result.NotShownServices = new List <CustomizeStageService>();
            result.Stages           = new List <CustomizeCalendarStage>();
            List <CalendarStage> calendarStages = dataService.GetClendarStages(result.ConfigId);

            var services      = dataService.GetServicesForUnit(currentUnitId);
            var stageServices = dataService.GetClendarStageServices(result.ConfigId);


            foreach (var stage in calendarStages)
            {
                if (stage.StageType == CalendarStageType.None)
                {
                    continue;
                }

                var calendarStage = new CustomizeCalendarStage {
                    Id = stage.Id, Name = stage.Name, StageType = stage.StageType, SortOrder = stage.SortOrder, IsServiceDefault = stage.IsServiceDefault, CalendarStageConfigId = stage.CalendarStageConfigId
                };
                calendarStage.Services = new List <CustomizeStageService>();
                result.Stages.Add(calendarStage);
            }

            CustomizeCalendarStage defaultInServiceStageForNotMappedServices = result.Stages.Find(x => x.IsServiceDefault); //can be null;

            foreach (var service in services)
            {
                CustomizeCalendarStage stage           = null;
                CalendarStageService   stageForService = stageServices.Find(x => x.ServiceId == service.Id);
                if (stageForService == null)
                {
                    stage = defaultInServiceStageForNotMappedServices;
                }
                else
                {
                    stage = result.Stages.Find(x => x.Id == stageForService.CalendarStageId);
                }

                if (stage != null && stage.StageType == CalendarStageType.None)
                {
                    stage = null;
                }

                if (stage == null)
                {
                    result.NotShownServices.Add(new CustomizeStageService()
                    {
                        ServiceId = service.Id, Name = service.Name, CalendarStageId = -1
                    });
                }
                else
                {
                    stage.Services.Add(new CustomizeStageService()
                    {
                        ServiceId = service.Id, Name = service.Name, CalendarStageId = stage.Id
                    });
                }
            }

            return(result);
        }
Example #9
0
        public void ProcessRequest(HttpContext context)
        {
            string userName = HttpContext.Current?.User?.Identity?.Name;

            if (string.IsNullOrEmpty(userName))
            {
                userName = "******";
            }

            UserInfo userInfo = new UserInfo()
            {
                UserName = "******"
            };

            if (!string.IsNullOrEmpty(userName))
            {
                var qnomy = new QNomyDataService();
                userInfo = qnomy.GetUserInfo(userName);
            }

            var query = new QueryStringParams(context.Request.Params);

            var dataService = new SchedulerDataService();
            var response    = WrapResponse(() =>
            {
                object data = null;

                switch (query.Action)
                {
                case QueryStringParams.GET_STAGES:
                    data = dataService.GetStages(userInfo.UnitId);
                    break;

                case QueryStringParams.GET_CUSTOMIZEDATA:
                    data = dataService.GetCustomizeData(userInfo.UnitId);
                    break;

                case QueryStringParams.SAVE_CUSTOMIZEDATA:
                    string objJsonCustomize = GetData(context.Request);
                    var dataObjCustomize    = JsonConvert.DeserializeObject <CustomizeData>(objJsonCustomize);
                    data = dataService.SaveCustomizeData(dataObjCustomize);
                    break;

                case QueryStringParams.GET_APPOINTMENTS:
                    //string filterData = GetData(context.Request);
                    //DateFromTo filter = JsonConvert.DeserializeObject<DateFromTo>(filterData);
                    //data = dataService.GetSchedulerEvents(filter.From, filter.To, userInfo.UnitId);
                    data = dataService.GetAppointmentsData(userInfo.UnitId);
                    break;

                case QueryStringParams.SAVE_APPOINTMENT:
                    string objJson = GetData(context.Request);
                    var dataObj    = JsonConvert.DeserializeObject <SchedulerEvent>(objJson);
                    data           = dataService.SaveAppointment(dataObj);
                    break;

                case QueryStringParams.APPOINTMENT_CHANGED:
                    string objJsonApp = GetData(context.Request);
                    var appData       = JsonConvert.DeserializeObject <AppointmentChangedData>(objJsonApp);
                    data = dataService.AppointmentChanged(userInfo.UserId, userInfo.UnitId, appData.PreviousStageId, appData.NextStageId, appData.SchedulerEvent, appData.RouteId);
                    break;

                case QueryStringParams.APPOINTMENT_CANCEL:
                    var eventToCancel = JsonConvert.DeserializeObject <SchedulerEvent>(GetData(context.Request));
                    data = dataService.AppointmentCancel(userInfo.UserId, userInfo.UnitId, eventToCancel);
                    break;

                default:
                    throw new InvalidOperationException($"Action {query.Action} not supported");
                }

                return(data);
            });

            ResponseSetData(context.Response, response);
        }