public override void Process(ActionData actionData)
        {
            var ticket = actionData.GetDataValue <Ticket>("Ticket");
            var pjName = actionData.GetAsString("PrintJobName");

            if (!string.IsNullOrEmpty(pjName))
            {
                var j = _cacheService.GetPrintJobByName(pjName);
                if (j != null)
                {
                    var copies      = actionData.GetAsInteger("Copies");
                    var printTicket = actionData.GetAsBoolean("PrintTicket", true);
                    var priority    = actionData.GetAsBoolean("HighPriority");
                    if (ticket != null && printTicket)
                    {
                        var orderTagName    = actionData.GetAsString("OrderTagName");
                        var orderTagValue   = actionData.GetAsString("OrderTagValue");
                        var orderStateName  = actionData.GetAsString("OrderStateName");
                        var orderState      = actionData.GetAsString("OrderState");
                        var orderStateValue = actionData.GetAsString("OrderStateValue");

                        Expression <Func <Order, bool> > expression = ex => true;
                        if (!string.IsNullOrWhiteSpace(orderTagName))
                        {
                            expression =
                                ex => ex.OrderTagExists(y => y.TagName == orderTagName && y.TagValue == orderTagValue);
                        }
                        if (!string.IsNullOrWhiteSpace(orderStateName))
                        {
                            expression = expression.And(ex => ex.IsInState(orderStateName, orderState));
                            if (!string.IsNullOrWhiteSpace(orderStateValue))
                            {
                                expression = expression.And(ex => ex.IsAnyStateValue(orderStateValue));
                            }
                        }
                        _ticketService.UpdateTicketNumber(ticket, _applicationState.CurrentTicketType.TicketNumerator);
                        ExecuteByCopies(copies, () => _printerService.PrintTicket(ticket, j, expression.Compile(), priority));
                    }
                    else
                    {
                        ExecuteByCopies(copies, () => _printerService.ExecutePrintJob(j, priority));
                    }
                }
            }
        }
Esempio n. 2
0
        private static void HandleEvents()
        {
            EventServiceFactory.EventService.GetEvent <GenericEvent <IActionData> >().Subscribe(x =>
            {
                if (x.Value.Action.ActionType == ActionNames.DisplayPaymentScreen)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.PublishEvent(EventTopicNames.MakePayment);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.LockTicket)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.LockTicket();
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.DisplayTicket)
                {
                    var ticketId = x.Value.GetAsInteger("TicketId");
                    if (ticketId > 0)
                    {
                        ExtensionMethods.PublishIdEvent(ticketId, EventTopicNames.DisplayTicket);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.CreateTicket)
                {
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.CreateTicket);
                }

                if (x.Value.Action.ActionType == ActionNames.UnlockTicket)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.Locked = false;
                    }
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.UnlockTicketRequested);
                }

                if (x.Value.Action.ActionType == ActionNames.CloseActiveTicket)
                {
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.CloseTicketRequested, true);
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateResourceState)
                {
                    var resourceId     = x.Value.GetDataValueAsInt("ResourceId");
                    var resourceTypeId = x.Value.GetDataValueAsInt("ResourceTypeId");
                    var stateName      = x.Value.GetAsString("ResourceState");
                    var state          = CacheService.GetResourceStateByName(stateName);
                    if (state != null)
                    {
                        if (resourceId > 0 && resourceTypeId > 0)
                        {
                            ResourceService.UpdateResourceState(resourceId, state.Id);
                        }
                        else
                        {
                            var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                            if (ticket != null)
                            {
                                var resourceTypeName = x.Value.GetDataValueAsString("ResourceTypeName");
                                foreach (var ticketResource in ticket.TicketResources)
                                {
                                    var resourceType = CacheService.GetResourceTypeById(ticketResource.ResourceTypeId);
                                    if (string.IsNullOrEmpty(resourceTypeName.Trim()) || resourceType.Name == resourceTypeName)
                                    {
                                        ResourceService.UpdateResourceState(ticketResource.ResourceId, state.Id);
                                    }
                                }
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketAccount)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        Expression <Func <Resource, bool> > qFilter = null;

                        var phoneNumber = x.Value.GetAsString("AccountPhone");
                        var accountName = x.Value.GetAsString("AccountName");
                        var note        = x.Value.GetAsString("Note");

                        if (!string.IsNullOrEmpty(phoneNumber))
                        {
                            qFilter = y => y.SearchString == phoneNumber;
                        }

                        if (!string.IsNullOrEmpty(accountName))
                        {
                            if (qFilter == null)
                            {
                                qFilter = y => y.Name == accountName;
                            }
                            else
                            {
                                qFilter = qFilter.And(y => y.Name == accountName);
                            }
                        }

                        if (qFilter != null)
                        {
                            var account = Dao.Query(qFilter).FirstOrDefault();
                            if (account != null)
                            {
                                TicketService.UpdateResource(ticket, account);
                            }
                        }
                        else
                        {
                            TicketService.UpdateResource(ticket, Resource.Null);
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateProgramSetting)
                {
                    var settingName = x.Value.GetAsString("SettingName");
                    var updateType  = x.Value.GetAsString("UpdateType");
                    if (!string.IsNullOrEmpty(settingName))
                    {
                        var isLocal = x.Value.GetAsBoolean("IsLocal");
                        var setting = isLocal
                            ? SettingService.ReadLocalSetting(settingName)
                            : SettingService.ReadGlobalSetting(settingName);

                        if (updateType == Resources.Increase)
                        {
                            var settingValue = x.Value.GetAsInteger("SettingValue");
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.IntegerValue = settingValue;
                            }
                            else
                            {
                                setting.IntegerValue = setting.IntegerValue + settingValue;
                            }
                        }
                        else if (updateType == Resources.Decrease)
                        {
                            var settingValue = x.Value.GetAsInteger("SettingValue");
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.IntegerValue = settingValue;
                            }
                            else
                            {
                                setting.IntegerValue = setting.IntegerValue - settingValue;
                            }
                        }
                        else if (updateType == "Toggle")
                        {
                            var settingValue = x.Value.GetAsString("SettingValue");
                            var parts        = settingValue.Split(',');
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.StringValue = parts[0];
                            }
                            else
                            {
                                for (var i = 0; i < parts.Length; i++)
                                {
                                    if (parts[i] == setting.StringValue)
                                    {
                                        setting.StringValue = (i + 1) < parts.Length ? parts[i + 1] : parts[0];
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            var settingValue    = x.Value.GetAsString("SettingValue");
                            setting.StringValue = settingValue;
                        }
                        if (!isLocal)
                        {
                            SettingService.SaveProgramSettings();
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.RefreshCache)
                {
                    MethodQueue.Queue("ResetCache", ResetCache);
                }

                if (x.Value.Action.ActionType == ActionNames.SendMessage)
                {
                    AppServices.MessagingService.SendMessage("ActionMessage", x.Value.GetAsString("Command"));
                }

                if (x.Value.Action.ActionType == ActionNames.SendEmail)
                {
                    EMailService.SendEMailAsync(x.Value.GetAsString("SMTPServer"),
                                                x.Value.GetAsString("SMTPUser"),
                                                x.Value.GetAsString("SMTPPassword"),
                                                x.Value.GetAsInteger("SMTPPort"),
                                                x.Value.GetAsString("ToEMailAddress"),
                                                x.Value.GetAsString("CCEmailAddresses"),
                                                x.Value.GetAsString("FromEMailAddress"),
                                                x.Value.GetAsString("Subject"),
                                                x.Value.GetAsString("EMailMessage"),
                                                x.Value.GetAsString("FileName"),
                                                x.Value.GetAsBoolean("DeleteFile"),
                                                x.Value.GetAsBoolean("BypassSslErrors"));
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketTax)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var taxTemplateName = x.Value.GetAsString("TaxTemplate");
                        var taxTemplate     = SettingService.GetTaxTemplateByName(taxTemplateName);
                        if (taxTemplate != null)
                        {
                            ticket.UpdateTax(taxTemplate);
                            TicketService.RecalculateTicket(ticket);
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketCalculation)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var calculationTypeName = x.Value.GetAsString("CalculationType");
                        var calculationType     = SettingService.GetCalculationTypeByName(calculationTypeName);
                        if (calculationType != null)
                        {
                            var amount = x.Value.GetAsDecimal("Amount");
                            ticket.AddCalculation(calculationType, amount);
                            TicketService.RecalculateTicket(ticket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.RegenerateTicketTax)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        TicketService.RegenerateTaxRates(ticket);
                        TicketService.RecalculateTicket(ticket);
                        EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.Addticketdiscount)
                {
                    //var ticket = x.Value.GetDataValue<Ticket>("Ticket");
                    //if (ticket != null)
                    //{
                    //    var percentValue = x.Value.GetAsDecimal("DiscountPercentage");
                    //    ticket.AddTicketDiscount(DiscountType.Percent, percentValue, ApplicationState.CurrentLoggedInUser.Id);
                    //    TicketService.RecalculateTicket(ticket);
                    //}
                }

                if (x.Value.Action.ActionType == ActionNames.Addorder)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");

                    if (ticket != null)
                    {
                        var menuItemName = x.Value.GetAsString("MenuItemName");
                        var menuItem     = CacheService.GetMenuItem(y => y.Name == menuItemName);
                        var portionName  = x.Value.GetAsString("PortionName");
                        var quantity     = x.Value.GetAsDecimal("Quantity");
                        var tag          = x.Value.GetAsString("Tag");
                        var order        = TicketService.AddOrder(ticket, menuItem.Id, quantity, portionName, null);
                        if (order != null)
                        {
                            order.Tag = tag;
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketTag)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var tagName  = x.Value.GetAsString("TagName");
                        var tagValue = x.Value.GetAsString("TagValue");
                        ticket.SetTagValue(tagName, tagValue);
                        var tagData = new TicketTagData {
                            TagName = tagName, TagValue = tagValue
                        };
                        tagData.PublishEvent(EventTopicNames.TicketTagSelected);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.TagOrder || x.Value.Action.ActionType == ActionNames.UntagOrder || x.Value.Action.ActionType == ActionNames.RemoveOrderTag)
                {
                    var order = x.Value.GetDataValue <Order>("Order");
                    if (order != null)
                    {
                        var tagName  = x.Value.GetAsString("OrderTagName");
                        var orderTag = CacheService.GetOrderTagGroupByName(tagName);

                        if (orderTag != null)
                        {
                            if (x.Value.Action.ActionType == ActionNames.RemoveOrderTag)
                            {
                                var tags = order.OrderTagValues.Where(y => y.OrderTagGroupId == orderTag.Id);
                                tags.ToList().ForEach(y => order.OrderTagValues.Remove(y));
                                return;
                            }
                            var tagValue      = x.Value.GetAsString("OrderTagValue");
                            var orderTagValue = orderTag.OrderTags.SingleOrDefault(y => y.Name == tagValue);
                            if (orderTagValue != null)
                            {
                                if (x.Value.Action.ActionType == ActionNames.TagOrder)
                                {
                                    order.TagIfNotTagged(orderTag, orderTagValue, ApplicationState.CurrentLoggedInUser.Id);
                                }
                                if (x.Value.Action.ActionType == ActionNames.UntagOrder)
                                {
                                    order.UntagIfTagged(orderTag, orderTagValue);
                                }
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.MoveTaggedOrders)
                {
                    var ticket       = x.Value.GetDataValue <Ticket>("Ticket");
                    var orderTagName = x.Value.GetAsString("OrderTagName");
                    if (ticket != null && !string.IsNullOrEmpty(orderTagName))
                    {
                        var orderTagValue = x.Value.GetAsString("OrderTagValue");
                        if (ticket.Orders.Any(y => y.OrderTagValues.Any(z => z.OrderTagGroupName == orderTagName && z.Name == orderTagValue)))
                        {
                            var tid = ticket.Id;
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.CloseTicketRequested, true);
                            ticket           = TicketService.OpenTicket(tid);
                            var orders       = ticket.Orders.Where(y => y.OrderTagValues.Any(z => z.OrderTagGroupName == orderTagName && z.Name == orderTagValue)).ToArray();
                            var commitResult = TicketService.MoveOrders(ticket, orders, 0);
                            if (string.IsNullOrEmpty(commitResult.ErrorMessage) && commitResult.TicketId > 0)
                            {
                                ExtensionMethods.PublishIdEvent(commitResult.TicketId, EventTopicNames.DisplayTicket);
                            }
                            else
                            {
                                ExtensionMethods.PublishIdEvent(tid, EventTopicNames.DisplayTicket);
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdatePriceTag)
                {
                    using (var workspace = WorkspaceFactory.Create())
                    {
                        var priceTag       = x.Value.GetAsString("PriceTag");
                        var departmentName = x.Value.GetAsString("DepartmentName");
                        var department     = workspace.Single <Department>(y => y.Name == departmentName);
                        if (department != null)
                        {
                            department.PriceTag = priceTag;
                            workspace.CommitChanges();
                            MethodQueue.Queue("ResetCache", ResetCache);
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.ExecutePrintJob)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    var pjName = x.Value.GetAsString("PrintJobName");
                    if (!string.IsNullOrEmpty(pjName))
                    {
                        TicketService.UpdateTicketNumber(ticket, ApplicationState.CurrentDepartment.TicketTemplate.TicketNumerator);
                        var j = CacheService.GetPrintJobByName(pjName);

                        if (j != null)
                        {
                            if (ticket != null)
                            {
                                PrinterService.PrintTicket(ticket, j);
                            }
                            else
                            {
                                PrinterService.ExecutePrintJob(j);
                            }
                        }
                    }
                }
            });
        }
Esempio n. 3
0
        private static void HandleEvents()
        {
            EventServiceFactory.EventService.GetEvent <GenericEvent <ActionData> >().Subscribe(x =>
            {
                if (x.Value.Action.ActionType == ActionNames.StartProcess)
                {
                    var fileName  = x.Value.GetAsString("FileName");
                    var arguments = x.Value.GetAsString("Arguments");
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        var psi      = new ProcessStartInfo(fileName, arguments);
                        var isHidden = x.Value.GetAsBoolean("IsHidden");
                        if (isHidden)
                        {
                            psi.WindowStyle = ProcessWindowStyle.Hidden;
                        }

                        var useShellExecute = x.Value.GetAsBoolean("UseShellExecute");
                        if (useShellExecute)
                        {
                            psi.UseShellExecute = true;
                        }

                        var workingDirectory = x.Value.GetAsString("WorkingDirectory");
                        if (!string.IsNullOrEmpty(workingDirectory))
                        {
                            psi.WorkingDirectory = workingDirectory;
                        }

                        Process.Start(psi);
                    }
                }
                if (x.Value.Action.ActionType == ActionNames.SetActiveTicketType)
                {
                    var ticketTypeName = x.Value.GetAsString("TicketTypeName");
                    var ticketType     = CacheService.GetTicketTypes().SingleOrDefault(y => y.Name == ticketTypeName);
                    if (ticketType != null)
                    {
                        ApplicationState.TempTicketType = ticketType;
                    }
                    else if (ApplicationState.SelectedEntityScreen != null && ApplicationState.SelectedEntityScreen.TicketTypeId != 0)
                    {
                        ApplicationState.TempTicketType = CacheService.GetTicketTypeById(ApplicationState.SelectedEntityScreen.TicketTypeId);
                    }
                    else
                    {
                        ApplicationState.TempTicketType = CacheService.GetTicketTypeById(ApplicationState.CurrentDepartment.TicketTypeId);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.ChangeTicketEntity)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var entityTypeName = x.Value.GetAsString("EntityTypeName");
                        var entityName     = x.Value.GetAsString("EntityName");
                        if (!string.IsNullOrEmpty(entityTypeName))
                        {
                            var entity = CacheService.GetEntityByName(entityTypeName, entityName);
                            if (entity != null)
                            {
                                TicketService.UpdateEntity(ticket, entity);
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateOrder)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    var orders = GetOrders(x.Value, ticket);
                    if (orders.Any())
                    {
                        foreach (var order in orders)
                        {
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("Quantity")))
                            {
                                order.Quantity = x.Value.GetAsDecimal("Quantity");
                            }
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("Price")))
                            {
                                order.UpdatePrice(x.Value.GetAsDecimal("Price"), "");
                            }
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("IncreaseInventory")))
                            {
                                order.IncreaseInventory = x.Value.GetAsBoolean("IncreaseInventory");
                            }
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("DecreaseInventory")))
                            {
                                order.DecreaseInventory = x.Value.GetAsBoolean("DecreaseInventory");
                            }
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("Locked")))
                            {
                                order.Locked = x.Value.GetAsBoolean("Locked");
                            }
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("CalculatePrice")))
                            {
                                order.CalculatePrice = x.Value.GetAsBoolean("CalculatePrice");
                            }
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("AccountTransactionType")))
                            {
                                TicketService.ChangeOrdersAccountTransactionTypeId(ticket, new List <Order> {
                                    order
                                },
                                                                                   CacheService.GetAccountTransactionTypeIdByName
                                                                                       (x.Value.GetAsString("AccountTransactionType")));
                            }

                            if (!string.IsNullOrEmpty(x.Value.GetAsString("PortionName")) || !string.IsNullOrEmpty(x.Value.GetAsString("PriceTag")))
                            {
                                var portionName = x.Value.GetAsString("PortionName");
                                var priceTag    = x.Value.GetAsString("PriceTag");
                                TicketService.UpdateOrderPrice(order, portionName, priceTag);
                            }
                        }
                    }
                }

                // Not supported on XP machines. We'll move it to a module later

                //if (x.Value.Action.ActionType == ActionNames.ExecutePowershellScript)
                //{
                //    var script = x.Value.GetAsString("Script");
                //    if (!string.IsNullOrEmpty(script))
                //    {
                //        if (Utility.IsValidFile(script)) script = File.ReadAllText(script);
                //        var runspace = RunspaceFactory.CreateRunspace();
                //        runspace.Open();
                //        runspace.SessionStateProxy.SetVariable("locator", ServiceLocator.Current);
                //        var pipeline = runspace.CreatePipeline(script);
                //        pipeline.Invoke();
                //        runspace.Close();
                //    }
                //}

                if (x.Value.Action.ActionType == ActionNames.DisplayPaymentScreen)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.PublishEvent(EventTopicNames.MakePayment);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.LockTicket)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.RequestLock();
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.LoadTicket)
                {
                    var ticketId       = x.Value.GetAsInteger("TicketId");
                    var ticket         = TicketService.OpenTicket(ticketId);
                    x.Value.DataObject = new { Ticket = ticket };
                    ticket.PublishEvent(EventTopicNames.SetSelectedTicket);
                }

                if (x.Value.Action.ActionType == ActionNames.DisplayTicket)
                {
                    var ticketId = x.Value.GetAsInteger("TicketId");
                    if (ticketId > 0)
                    {
                        ExtensionMethods.PublishIdEvent(ticketId, EventTopicNames.DisplayTicket);
                    }
                    else
                    {
                        EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.DisplayTicketList)
                {
                    var ticketTagName   = x.Value.GetAsString("TicketTagName");
                    var ticketStateName = x.Value.GetAsString("TicketStateName");

                    if (!string.IsNullOrEmpty(ticketStateName))
                    {
                        var dt = new TicketStateData {
                            StateName = ticketStateName
                        };
                        dt.PublishEvent(EventTopicNames.ActivateTicketList);
                    }
                    else if (!string.IsNullOrEmpty(ticketTagName))
                    {
                        var dt = new TicketTagGroup {
                            Name = ticketTagName
                        };
                        dt.PublishEvent(EventTopicNames.ActivateTicketList);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.CreateTicket)
                {
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.CreateTicket);
                }

                if (x.Value.Action.ActionType == ActionNames.UnlockTicket)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.UnLock();
                    }
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.UnlockTicketRequested);
                }

                if (x.Value.Action.ActionType == ActionNames.MarkTicketAsClosed)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        ticket.Close();
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.CloseActiveTicket)
                {
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.CloseTicketRequested, true);
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateEntityState)
                {
                    var entityId     = x.Value.GetDataValueAsInt("EntityId");
                    var entityTypeId = x.Value.GetDataValueAsInt("EntityTypeId");
                    var stateName    = x.Value.GetAsString("EntityStateName");
                    var state        = x.Value.GetAsString("EntityState");
                    if (state != null)
                    {
                        if (entityId > 0 && entityTypeId > 0)
                        {
                            EntityService.UpdateEntityState(entityId, entityTypeId, stateName, state);
                        }
                        else
                        {
                            var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                            if (ticket != null)
                            {
                                var entityTypeName = x.Value.GetDataValueAsString("EntityTypeName");
                                foreach (var ticketEntity in ticket.TicketEntities)
                                {
                                    var entityType = CacheService.GetEntityTypeById(ticketEntity.EntityTypeId);
                                    if (string.IsNullOrEmpty(entityTypeName.Trim()) || entityType.Name == entityTypeName)
                                    {
                                        EntityService.UpdateEntityState(ticketEntity.EntityId, ticketEntity.EntityTypeId, stateName, state);
                                    }
                                }
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateProgramSetting)
                {
                    var settingName = x.Value.GetAsString("SettingName");
                    var updateType  = x.Value.GetAsString("UpdateType");
                    if (!string.IsNullOrEmpty(settingName))
                    {
                        var isLocal = x.Value.GetAsBoolean("IsLocal");
                        var setting = isLocal
                            ? SettingService.ReadLocalSetting(settingName)
                            : SettingService.ReadGlobalSetting(settingName);

                        if (updateType == Resources.Increase)
                        {
                            var settingValue = x.Value.GetAsInteger("SettingValue");
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.IntegerValue = settingValue;
                            }
                            else
                            {
                                setting.IntegerValue = setting.IntegerValue + settingValue;
                            }
                        }
                        else if (updateType == Resources.Decrease)
                        {
                            var settingValue = x.Value.GetAsInteger("SettingValue");
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.IntegerValue = settingValue;
                            }
                            else
                            {
                                setting.IntegerValue = setting.IntegerValue - settingValue;
                            }
                        }
                        else if (updateType == Resources.Toggle)
                        {
                            var settingValue = x.Value.GetAsString("SettingValue");
                            var parts        = settingValue.Split(',');
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.StringValue = parts[0];
                            }
                            else
                            {
                                for (var i = 0; i < parts.Length; i++)
                                {
                                    if (parts[i] == setting.StringValue)
                                    {
                                        setting.StringValue = (i + 1) < parts.Length ? parts[i + 1] : parts[0];
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            var settingValue    = x.Value.GetAsString("SettingValue");
                            setting.StringValue = settingValue;
                        }
                        if (!isLocal)
                        {
                            SettingService.SaveProgramSettings();
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.RefreshCache)
                {
                    MethodQueue.Queue("ResetCache", ResetCache);
                }

                if (x.Value.Action.ActionType == ActionNames.SendMessage)
                {
                    AppServices.MessagingService.SendMessage("ActionMessage", x.Value.GetAsString("Command"));
                }

                if (x.Value.Action.ActionType == ActionNames.SendEmail)
                {
                    EmailService.SendEMailAsync(x.Value.GetAsString("SMTPServer"),
                                                x.Value.GetAsString("SMTPUser"),
                                                x.Value.GetAsString("SMTPPassword"),
                                                x.Value.GetAsInteger("SMTPPort"),
                                                x.Value.GetAsString("ToEMailAddress"),
                                                x.Value.GetAsString("CCEmailAddresses"),
                                                x.Value.GetAsString("FromEMailAddress"),
                                                x.Value.GetAsString("Subject"),
                                                x.Value.GetAsString("EMailMessage"),
                                                x.Value.GetAsString("FileName"),
                                                x.Value.GetAsBoolean("DeleteFile"),
                                                x.Value.GetAsBoolean("BypassSslErrors"));
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketCalculation)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var calculationTypeName = x.Value.GetAsString("CalculationType");
                        var calculationType     = CacheService.GetCalculationTypeByName(calculationTypeName);
                        if (calculationType != null)
                        {
                            var amount = x.Value.GetAsDecimal("Amount");
                            ticket.AddCalculation(calculationType, amount);
                            TicketService.RecalculateTicket(ticket);
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.RegenerateSelectedTicket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.AddOrder)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");

                    if (ticket != null)
                    {
                        var menuItemName = x.Value.GetAsString("MenuItemName");
                        var menuItem     = CacheService.GetMenuItem(y => y.Name == menuItemName);
                        var portionName  = x.Value.GetAsString("PortionName");
                        var quantity     = x.Value.GetAsDecimal("Quantity");
                        var tag          = x.Value.GetAsString("Tag");
                        var order        = TicketService.AddOrder(ticket, menuItem.Id, quantity, portionName, null);
                        if (order != null)
                        {
                            order.Tag = tag;
                        }
                        order.PublishEvent(EventTopicNames.OrderAdded);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketTag)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var tagName  = x.Value.GetAsString("TagName");
                        var tagValue = x.Value.GetAsString("TagValue");
                        ticket.SetTagValue(tagName, tagValue);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateTicketState)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var stateName    = x.Value.GetAsString("StateName");
                        var currentState = x.Value.GetAsString("CurrentState");
                        var state        = x.Value.GetAsString("State");
                        var stateValue   = x.Value.GetAsString("StateValue");
                        var quantity     = x.Value.GetAsInteger("Quantity");
                        TicketService.UpdateTicketState(ticket, stateName, currentState, state, stateValue, quantity);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdateOrderState)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    var orders = GetOrders(x.Value, ticket);
                    if (orders.Any())
                    {
                        var stateName    = x.Value.GetAsString("StateName");
                        var currentState = x.Value.GetAsString("CurrentState");
                        var groupOrder   = x.Value.GetAsInteger("GroupOrder");
                        var state        = x.Value.GetAsString("State");
                        var stateOrder   = x.Value.GetAsInteger("StateOrder");
                        var stateValue   = x.Value.GetAsString("StateValue");
                        TicketService.UpdateOrderStates(ticket, orders.ToList(), stateName, currentState, groupOrder, state, stateOrder, stateValue);
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.TagOrder || x.Value.Action.ActionType == ActionNames.UntagOrder || x.Value.Action.ActionType == ActionNames.RemoveOrderTag)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    var orders = GetOrders(x.Value, ticket);
                    if (orders.Any())
                    {
                        var tagName  = x.Value.GetAsString("OrderTagName");
                        var orderTag = CacheService.GetOrderTagGroupByName(tagName);

                        if (orderTag != null)
                        {
                            var tagValue      = x.Value.GetAsString("OrderTagValue");
                            var oldTagValue   = x.Value.GetAsString("OldOrderTagValue");
                            var tagNote       = x.Value.GetAsString("OrderTagNote");
                            var orderTagValue = orderTag.OrderTags.SingleOrDefault(y => y.Name == tagValue);
                            if (!string.IsNullOrEmpty(x.Value.GetAsString("OrderTagPrice")))
                            {
                                var price           = x.Value.GetAsDecimal("OrderTagPrice");
                                orderTagValue.Price = price;
                            }
                            if (orderTagValue != null)
                            {
                                if (!string.IsNullOrEmpty(oldTagValue))
                                {
                                    orders = orders.Where(o => o.OrderTagExists(y => y.OrderTagGroupId == orderTag.Id && y.TagValue == oldTagValue)).ToList();
                                }
                                if (x.Value.Action.ActionType == ActionNames.TagOrder)
                                {
                                    TicketService.TagOrders(ticket, orders, orderTag, orderTagValue, tagNote);
                                }
                                if (x.Value.Action.ActionType == ActionNames.UntagOrder)
                                {
                                    TicketService.UntagOrders(ticket, orders, orderTag, orderTagValue);
                                }
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.MoveTaggedOrders)
                {
                    var ticket       = x.Value.GetDataValue <Ticket>("Ticket");
                    var orderTagName = x.Value.GetAsString("OrderTagName");
                    if (ticket != null && !string.IsNullOrEmpty(orderTagName))
                    {
                        var orderTagValue = x.Value.GetAsString("OrderTagValue");
                        if (ticket.Orders.Any(y => y.OrderTagExists(z => z.TagName == orderTagName && z.TagValue == orderTagValue)))
                        {
                            var tid = ticket.Id;
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.CloseTicketRequested, true);
                            ticket           = TicketService.OpenTicket(tid);
                            var orders       = ticket.Orders.Where(y => y.OrderTagExists(z => z.TagName == orderTagName && z.TagValue == orderTagValue)).ToArray();
                            var commitResult = TicketService.MoveOrders(ticket, orders, 0);
                            if (string.IsNullOrEmpty(commitResult.ErrorMessage) && commitResult.TicketId > 0)
                            {
                                ExtensionMethods.PublishIdEvent(commitResult.TicketId, EventTopicNames.DisplayTicket);
                            }
                            else
                            {
                                ExtensionMethods.PublishIdEvent(tid, EventTopicNames.DisplayTicket);
                            }
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.UpdatePriceTag)
                {
                    using (var workspace = WorkspaceFactory.Create())
                    {
                        var priceTag       = x.Value.GetAsString("PriceTag");
                        var departmentName = x.Value.GetAsString("DepartmentName");
                        var department     = workspace.Single <Department>(y => y.Name == departmentName);
                        if (department != null)
                        {
                            department.PriceTag = priceTag;
                            workspace.CommitChanges();
                            MethodQueue.Queue("ResetCache", ResetCache);
                        }
                    }
                }

                if (x.Value.Action.ActionType == ActionNames.ExecutePrintJob)
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    var pjName = x.Value.GetAsString("PrintJobName");
                    if (!string.IsNullOrEmpty(pjName))
                    {
                        TicketService.UpdateTicketNumber(ticket, ApplicationState.CurrentTicketType.TicketNumerator);
                        var j = CacheService.GetPrintJobByName(pjName);

                        if (j != null)
                        {
                            if (ticket != null)
                            {
                                var orderTagName    = x.Value.GetAsString("OrderTagName");
                                var orderTagValue   = x.Value.GetAsString("OrderTagValue");
                                var orderStateName  = x.Value.GetAsString("OrderStateName");
                                var orderState      = x.Value.GetAsString("OrderState");
                                var orderStateValue = x.Value.GetAsString("OrderStateValue");
                                Expression <Func <Order, bool> > expression = ex => true;
                                if (!string.IsNullOrWhiteSpace(orderTagName))
                                {
                                    expression = ex => ex.OrderTagExists(y => y.TagName == orderTagName && y.TagValue == orderTagValue);
                                }
                                if (!string.IsNullOrWhiteSpace(orderStateName))
                                {
                                    expression = expression.And(ex => ex.IsInState(orderStateName, orderState));
                                    if (!string.IsNullOrWhiteSpace(orderStateValue))
                                    {
                                        expression = expression.And(ex => ex.IsInState(orderStateValue));
                                    }
                                }
                                PrinterService.PrintTicket(ticket, j, expression.Compile());
                            }
                            else
                            {
                                PrinterService.ExecutePrintJob(j);
                            }
                        }
                    }
                }
            });
        }