Ejemplo n.º 1
0
        private static void HandleEvents()
        {
            EventServiceFactory.EventService.GetEvent <GenericEvent <ActionData> >().Subscribe(x =>
            {
                if (x.Value.Action.ActionType == "UpdateApplicationSubTitle")
                {
                    var title = x.Value.GetAsString("Title");
                    PresentationServices.SubTitle.ApplicationTitle = title;
                    var fontSize = x.Value.GetAsInteger("FontSize");
                    if (fontSize > 0)
                    {
                        PresentationServices.SubTitle.ApplicationTitleFontSize = fontSize;
                    }
                    var fontColor = x.Value.GetAsString("Color");
                    if (!string.IsNullOrEmpty(fontColor))
                    {
                        PresentationServices.SubTitle.ApplicationTitleColor = fontColor;
                    }
                }
                if (x.Value.Action.ActionType == "RemoveLastModifier")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket == null)
                    {
                        return;
                    }
                    var ti = x.Value.GetDataValue <TicketItem>("TicketItem");
                    if (ti == null)
                    {
                        return;
                    }
                    if (ti.Properties.Count > 0)
                    {
                        var prop = ti.LastSelectedProperty ?? ti.Properties.Last();
                        prop.Quantity--;
                        if (prop.Quantity < 1)
                        {
                            ti.Properties.Remove(prop);
                        }
                    }
                    TicketViewModel.RecalculateTicket(ticket);
                }

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

                    var ti = x.Value.GetDataValue <TicketItem>("TicketItem");
                    if (ti == null)
                    {
                        return;
                    }

                    var priceTag = x.Value.GetAsString("PriceTag");
                    var mi       = AppServices.DataAccessService.GetMenuItem(ti.MenuItemId);
                    if (mi == null)
                    {
                        return;
                    }

                    var portion = mi.Portions.SingleOrDefault(y => y.Name == ti.PortionName);
                    if (portion == null)
                    {
                        return;
                    }

                    ti.UpdatePortion(portion, priceTag, null);

                    TicketViewModel.RecalculateTicket(ticket);
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                }

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

                    var ti = x.Value.GetDataValue <TicketItem>("TicketItem");
                    if (ti == null)
                    {
                        return;
                    }

                    var tag = x.Value.GetAsString("Tag");
                    ti.Tag  = tag;
                    decimal val;
                    decimal.TryParse(tag, out val);
                }

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

                    var ti = ticket.TicketItems.LastOrDefault();
                    if (ti == null)
                    {
                        return;
                    }

                    var priceTag = x.Value.GetAsString("PriceTag");
                    var mi       = AppServices.DataAccessService.GetMenuItem(ti.MenuItemId);
                    if (mi == null)
                    {
                        return;
                    }

                    var portion = mi.Portions.SingleOrDefault(y => y.Name == ti.PortionName);
                    if (portion == null)
                    {
                        return;
                    }

                    ti.UpdatePortion(portion, priceTag, null);

                    TicketViewModel.RecalculateTicket(ticket);
                    EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                }

                if (x.Value.Action.ActionType == "GiftLastTicketItem")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var ti = ticket.TicketItems.LastOrDefault();
                        if (ti != null)
                        {
                            decimal quantity;
                            decimal.TryParse(x.Value.Action.GetParameter("Quantity"), out quantity);
                            if (quantity > 0 && ti.Quantity > quantity)
                            {
                                ti.UpdateSelectedQuantity(quantity);
                                ti = ticket.ExtractSelectedTicketItems(new List <TicketItem> {
                                    ti
                                }).FirstOrDefault();
                                if (ti == null)
                                {
                                    return;
                                }
                                AppServices.MainDataContext.AddItemToSelectedTicket(ti);
                            }
                            var reasonId   = 0;
                            var giftReason = x.Value.Action.GetParameter("GiftReason");
                            if (!string.IsNullOrEmpty(giftReason))
                            {
                                var reason = Dao.SingleWithCache <Reason>(u => u.Name == giftReason);
                                if (reason != null)
                                {
                                    reasonId = reason.Id;
                                }
                            }
                            ticket.GiftItem(ti, reasonId, AppServices.CurrentLoggedInUser.Id);
                            TicketViewModel.RecalculateTicket(ticket);
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == "UpdateTicketAccount")
                {
                    Expression <Func <Customer, 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.PhoneNumber == phoneNumber;
                    }

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

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

                    if (qFilter != null)
                    {
                        var customer = Dao.Query(qFilter).FirstOrDefault();
                        if (customer != null)
                        {
                            AppServices.MainDataContext.AssignCustomerToSelectedTicket(customer);
                        }
                    }
                    else
                    {
                        AppServices.MainDataContext.AssignCustomerToSelectedTicket(Customer.Null);
                    }
                }

                if (x.Value.Action.ActionType == "UpdateProgramSetting")
                {
                    SettingAccessor.ResetCache();

                    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
                            ? AppServices.SettingService.ReadLocalSetting(settingName)
                            : AppServices.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 == "Multiply")
                        {
                            if (string.IsNullOrEmpty(setting.StringValue))
                            {
                                setting.DecimalValue = 0;
                            }
                            else
                            {
                                setting.DecimalValue = setting.DecimalValue * x.Value.GetAsDecimal("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)
                        {
                            AppServices.SettingService.SaveChanges();
                        }
                    }
                }

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

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

                if (x.Value.Action.ActionType == "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("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 == "ExecuteTicketEvent")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var operationName = x.Value.GetAsString("TicketOperation");
                        if (operationName == Resources.Refresh)
                        {
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.DisplayTicketView);
                        }
                        if (operationName == Resources.Close)
                        {
                            ticket.PublishEvent(EventTopicNames.PaymentSubmitted);
                        }
                    }
                }


                if (x.Value.Action.ActionType == "UpdateTicketVat")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var vatTemplateName = x.Value.GetAsString("VatTemplate");
                        var vatTemplate     = AppServices.MainDataContext.VatTemplates.FirstOrDefault(y => y.Name == vatTemplateName);
                        if (vatTemplate != null)
                        {
                            ticket.UpdateVat(vatTemplate);
                            TicketViewModel.RecalculateTicket(ticket);
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == "UpdateTicketTaxService")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var taxServiceTemplateName = x.Value.GetAsString("TaxServiceTemplate");
                        var taxServiceTemplate     =
                            AppServices.MainDataContext.TaxServiceTemplates.FirstOrDefault(
                                y => y.Name == taxServiceTemplateName);
                        if (taxServiceTemplate != null)
                        {
                            var amount = x.Value.GetAsDecimal("Amount");
                            ticket.AddTaxService(taxServiceTemplate.Id, taxServiceTemplate.CalculationMethod, amount);
                            TicketViewModel.RecalculateTicket(ticket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == "RegenerateTicketVat")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        TicketViewModel.RegenerateVatRates(ticket);
                        TicketViewModel.RecalculateTicket(ticket);
                        EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                    }
                }

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

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

                    if (ticket != null)
                    {
                        var menuItemName = x.Value.GetAsString("MenuItemName");
                        var menuItem     = AppServices.DataAccessService.GetMenuItemByName(menuItemName);
                        var portionName  = x.Value.GetAsString("PortionName");
                        var quantity     = x.Value.GetAsDecimal("Quantity");
                        var gifted       = x.Value.GetAsBoolean("Gift");
                        var giftReason   = x.Value.GetAsString("GiftReason");
                        var tag          = x.Value.GetAsString("Tag");

                        var departmentId = AppServices.CurrentTerminal.DepartmentId > 0
                               ? AppServices.MainDataContext.SelectedDepartment.Id
                               : ticket.DepartmentId;

                        var ti = ticket.AddTicketItem(AppServices.CurrentLoggedInUser.Id,
                                                      departmentId, menuItem, portionName,
                                                      AppServices.MainDataContext.SelectedDepartment.PriceTag, "");

                        ti.Quantity = quantity;
                        ti.Gifted   = gifted;
                        if (gifted && !string.IsNullOrEmpty(giftReason))
                        {
                            var reason = Dao.SingleWithCache <Reason>(u => u.Name == giftReason);
                            if (reason != null)
                            {
                                ti.ReasonId = reason.Id;
                            }
                        }
                        else
                        {
                            ti.ReasonId = 0;
                        }
                        ti.Tag = tag;

                        TicketViewModel.RecalculateTicket(ticket);
                        EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                    }
                }

                if (x.Value.Action.ActionType == "VoidTicketItems")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var menuItemName = x.Value.GetAsString("MenuItemName");
                        var tag          = x.Value.GetAsString("Tag");
                        if (!string.IsNullOrEmpty(menuItemName) && !string.IsNullOrEmpty(tag))
                        {
                            var lines = ticket.TicketItems.Where(y => !y.Voided &&
                                                                 (string.IsNullOrEmpty(menuItemName) || y.MenuItemName.Contains(menuItemName)) &&
                                                                 (y.Tag.Contains(tag) || string.IsNullOrEmpty(tag))).ToList();
                            lines.ForEach(y => ticket.VoidItem(y, 0, AppServices.CurrentLoggedInUser.Id));
                            TicketViewModel.RecalculateTicket(ticket);
                            EventServiceFactory.EventService.PublishEvent(EventTopicNames.RefreshSelectedTicket);
                        }
                    }
                }

                if (x.Value.Action.ActionType == "UpdateTicketTag")
                {
                    var ticket = x.Value.GetDataValue <Ticket>("Ticket");
                    if (ticket != null)
                    {
                        var tagName  = x.Value.GetAsString("TagName");
                        var tagValue = x.Value.GetAsString("TagValue");
                        if (tagValue.Contains(","))
                        {
                            var ctag = ticket.GetTagValue(tagName);
                            if (!string.IsNullOrEmpty(ctag))
                            {
                                var nextTag = tagValue.Split(',').SkipWhile(y => y != ctag).Skip(1).FirstOrDefault();
                                if (string.IsNullOrEmpty(nextTag))
                                {
                                    nextTag = tagValue.Split(',')[0];
                                }
                                tagValue = nextTag;
                            }
                        }
                        ticket.SetTagValue(tagName, tagValue);
                        var tagData = new TicketTagData {
                            TagName = tagName, TagValue = tagValue
                        };
                        tagData.PublishEvent(EventTopicNames.TagSelectedForSelectedTicket);
                    }
                }

                if (x.Value.Action.ActionType == "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 == "ExecutePrintJob")
                {
                    var ticket        = x.Value.GetDataValue <Ticket>("Ticket");
                    var pjName        = x.Value.GetAsString("PrintJobName");
                    var ticketItemTag = x.Value.GetAsString("TicketItemTag");

                    if (!string.IsNullOrEmpty(pjName))
                    {
                        var j = AppServices.CurrentTerminal.PrintJobs.SingleOrDefault(y => y.Name == pjName);

                        if (j != null)
                        {
                            if (ticket != null)
                            {
                                AppServices.MainDataContext.UpdateTicketNumber(ticket);
                                if (j.LocksTicket)
                                {
                                    ticket.RequestLock();
                                }

                                var clonedTicket = ObjectCloner.Clone(ticket);
                                clonedTicket.CopyPaidItemsCache(ticket);
                                if (!string.IsNullOrEmpty(ticketItemTag))
                                {
                                    clonedTicket.TicketItems =
                                        clonedTicket.TicketItems.Where(y => !string.IsNullOrEmpty(y.Tag) &&
                                                                       y.Tag.ToLower().Contains(ticketItemTag.Trim().ToLower())).ToList();
                                }
                                AppServices.PrintService.ManualPrintTicket(clonedTicket, j);
                            }
                            else
                            {
                                AppServices.PrintService.ExecutePrintJob(j);
                            }
                        }
                    }
                }
            });
        }
Ejemplo 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);
                            }
                        }
                    }
                }
            });
        }