public async Task <ActionResult> Create(Order Order, OrderRecipientInfo shipmentAndRecipientInfo)
        {
            // get properties of tenant
            try
            {
                if (ModelState.IsValid)
                {
                    var tempList       = GaneOrderDetailsSessionHelper.GetOrderDetailSession(shipmentAndRecipientInfo.PageSessionToken);
                    var orderNotesList = GaneOrderNotesSessionHelper.GetOrderNotesSession(shipmentAndRecipientInfo.PageSessionToken);

                    OrderService.CreateWorksOrder(Order, shipmentAndRecipientInfo, CurrentTenantId, CurrentWarehouseId, CurrentUserId, Mapper.Map(tempList, new List <OrderDetail>()), orderNotesList);

                    var result = await _ganeConfigurationsHelper.CreateTenantEmailNotificationQueue($"#{Order.OrderNumber} - Works order logged", Mapper.Map(Order, new OrderViewModel()),
                                                                                                    sendImmediately : true, shipmentAndRecipientInfo : shipmentAndRecipientInfo, worksOrderNotificationType : WorksOrderNotificationTypeEnum.WorksOrderLogTemplate);

                    if (result != "Success")
                    {
                        TempData["Error"] = result;
                    }

                    return(AnchoredOrderIndex("WorksOrders", "Index", ViewBag.Fragment as string));
                }
                SetViewBagItems();
                ViewBag.OrderDetails              = Order.OrderDetails;
                ViewBag.OrderProcesses            = Order.OrderProcess;
                ViewBag.ForceRegeneratePageToken  = "True";
                ViewBag.ForceRegeneratedPageToken = shipmentAndRecipientInfo.PageSessionToken ?? Guid.NewGuid().ToString();
                ViewBag.SupplierID = new SelectList(AccountServices.GetAllValidAccounts(CurrentTenantId, EnumAccountType.Supplier), "AccountID", "CompanyName", Order.AccountID);
                return(View(Order));
            }
            catch (Exception Exp)
            {
                if (Exp.InnerException != null && Exp.InnerException.Message == "Duplicate Order Number")
                {
                    ModelState.AddModelError("OrderNumber", Exp.Message);
                }
                else if (Exp.InnerException != null && Exp.InnerException.Message.Contains("Template"))
                {
                    ModelState.AddModelError("PropertyTenantIds", Exp.Message);
                }
                else
                {
                    ModelState.AddModelError(string.Empty, Exp.Message);
                }

                SetViewBagItems();
                ViewBag.ForceRegeneratePageToken  = "True";
                ViewBag.ForceRegeneratedPageToken = shipmentAndRecipientInfo.PageSessionToken ?? Guid.NewGuid().ToString();
                ViewBag.OrderDetails   = Order.OrderDetails;
                ViewBag.OrderProcesses = Order.OrderProcess;
                ViewBag.SupplierID     = new SelectList(AccountServices.GetAllValidAccounts(CurrentTenantId, EnumAccountType.Supplier), "AccountID", "CompanyName", Order.AccountID);
                return(View(Order));
            }
        }
        public async Task <ActionResult> CreateAppointment(string start, string end, string subject, string resourceId, int orderId, int joblabel, int tenantId)
        {
            if (!caSession.AuthoriseSession())
            {
                return(Json(new { Message = "You are not authorised to perofrm this operation" }));
            }

            var appointment = _appointmentsService.CreateAppointment(start, end, subject, resourceId, orderId, joblabel, tenantId);

            if (appointment != null)
            {
                var order = OrderService.UpdateOrderStatus(orderId, 6, CurrentUserId);

                var result = await _emailNotificationsHelper.CreateTenantEmailNotificationQueue($"#{order.OrderNumber} - Works order scheduled", Mapper.Map(order, new OrderViewModel()), worksOrderNotificationType :
                                                                                                WorksOrderNotificationTypeEnum.WorksOrderScheduledTemplate, appointment : appointment, sendImmediately : false);

                if (result != "Success")
                {
                    ViewBag.Error = result;
                }
            }

            // send resources as per filter values
            var selectedJobType = int.Parse(!string.IsNullOrEmpty(Request.Params["SelectedJobType"]) ? Request.Params["SelectedJobType"] : "0");

            Session["selectedJobType"] = selectedJobType;
            return(PartialView("_SchedulerPartial", AppointmentsSchedulerSettings.DataObject));
        }
        // POST http://ganetest.qsrtime.net/api/sync/post-order-processes
        public async Task <IHttpActionResult> PostOrderProcesses(OrderProcessesSyncCollection data)
        {
            data.SerialNo = data.SerialNo.Trim().ToLower();

            var terminal = TerminalServices.GetTerminalBySerial(data.SerialNo);

            if (terminal == null)
            {
                return(Unauthorized());
            }

            var TransactionLog = TerminalServices.CheckTransactionLog(data.TransactionLogId, terminal.TerminalId);

            if (TransactionLog == true)
            {
                return(Conflict());
            }

            var mobileLocation = TerminalServices.GetMobileLocationByTerminalId(terminal.TerminalId);

            if (mobileLocation != null)
            {
                terminal.WarehouseId = mobileLocation.WarehouseId;
            }

            var results = new List <OrdersSync>();

            if (data.OrderProcesses != null && data.OrderProcesses.Any())
            {
                var groupedOrderProcesses = data.OrderProcesses.GroupBy(p => p.OrderToken, (key, g) => new { OrderToken = key, OrderProcesses = g.ToList() });

                foreach (var processGroup in groupedOrderProcesses)
                {
                    var orderProcesses = processGroup.OrderProcesses;
                    foreach (var item in orderProcesses)
                    {
                        var order = OrderService.SaveOrderProcessSync(item, terminal);

                        results.Add(order);

                        if (order.OrderStatusID == (int)OrderStatusEnum.AwaitingAuthorisation)
                        {
                            OrderViewModel orderViewModel = new OrderViewModel();
                            orderViewModel.OrderID   = order.OrderID;
                            orderViewModel.TenentId  = order.TenentId;
                            orderViewModel.AccountID = order.AccountID;
                            await _configHelper.CreateTenantEmailNotificationQueue($"#{order.OrderNumber} - Order Requires Authorisation", orderViewModel, null, shipmentAndRecipientInfo : null,
                                                                                   worksOrderNotificationType : WorksOrderNotificationTypeEnum.AwaitingOrderTemplate);
                        }
                    }
                }
            }

            TerminalServices.CreateTerminalLog(DateTime.UtcNow, terminal.TenantId, data.OrderProcesses.Count, terminal.TerminalId, TerminalLogTypeEnum.OrderProcessesPost);

            return(Ok(results));
        }