internal static void PersistTicket(AromaContext db, Guid systemEventId, Guid id,int clientId, int supportTicketType, string text, Guid userId)
        {
            var evnt = db.SystemEvents.Find(systemEventId);
                      
            var links = (from item in db.SystemLinks
                        where item.Parent.Equals(id)
                        select item.Child).ToArray();
            var ticket = (from item in db.SupportTickets
                          where links.Contains(item.SupportTicketId)
                          && item.SupportTicketTypeId == supportTicketType
                          select item).FirstOrDefault();
            if (ticket == null)
            {
                ticket = new SupportTicket()
                {
                    ClientID = clientId,
                    Description = text,
                    iDate = DateTime.Now,
                    SupportTicketId=Guid.NewGuid(),
                    SupportTicketStatusID = 1,
                    SupportTicketTypeId = supportTicketType,
                    UserID = evnt.UserId
                };
                db.SupportTickets.Add(ticket);

                var link = new SystemLink() {
                    Parent =id,
                    Child = ticket.SupportTicketId,
                    Created = DateTime.Now,
                    UserID = userId,
                    LinkId = Guid.NewGuid()
                };
                db.SystemLinks.Add(link);

                db.SaveChanges();
            }
        }
        public static void SendSMSEvent(AromaContext db, int systemSMSEventId, int clientId, Guid userId, Guid? source, params KeyValuePair<string, string>[] pars)
        {
            try
            {
                var systemSMSevent = db.SystemSMSEvents.Find(systemSMSEventId);
                var template = systemSMSevent.SystemSMSTemplate;
                var client = db.Clients.Find(clientId);
                if (!source.HasValue) source = userId;
                try
                {
                
                    if (systemSMSevent.Active)
                    {
                        

                        var contact = db.Contacts.Where(m => m.ContactTypeID == 3 && m.ClientID == clientId).FirstOrDefault();
                        var smsSub = new SystemSMSTemplateModel(template.Text, client, db);
                        smsSub.EventInfo = pars;
                        var textResult = smsSub.Generate();
                        if (contact != null)
                        {

                            var sms = (from item in db.SystemSMSes
                                       where item.ClientID == clientId
                                       && item.SMSDescription == textResult
                                       select item).FirstOrDefault();
                            if (sms == null)
                            {
                                sms = new SystemSMS()
                                {
                                    Active = true,
                                    ClientID = clientId,
                                    iDate = DateTime.Now,
                                    Number = contact.ContactName,
                                    SMSDescription = textResult,
                                    Source = source,
                                    SystemSMSStatusId = 1
                                };
                                db.SystemSMSes.Add(sms);
                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            var ticket = new SupportTicket()
                            {
                                ClientID = clientId,
                                Description = string.Format("An attemt was made to send a SMS to this client ({0}) but the cell number could not be found.\r\n{1}", clientId, textResult),
                                iDate = DateTime.Now,
                                SupportTicketStatusID = 1,
                                SupportTicketTypeId = 2,
                                UserID = userId
                            };
                            db.SupportTickets.Add(ticket);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        var ticket = new SupportTicket()
                        {
                            ClientID = client.ClientId,
                            Description = string.Format("Unable to create system sms:\"{0}\" Message:{1}", template.Description, ex.Message),
                            iDate = DateTime.Now,
                            SupportTicketStatusID = 1,
                            SupportTicketTypeId = 2,
                            UserID = userId
                        };
                        db.SupportTickets.Add(ticket);
                        db.SaveChanges();
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
            catch
            {
                throw;
            }
        }
 // GET: SupportTickets/Create
 public ActionResult Create(int clientId =0)
 {
     ViewBag.ClientID = new SelectList(db.Clients, "ClientId", "ClientClientId");
     ViewBag.SupportTicketStatusID = new SelectList(db.SupportTicketStatuses, "SupportTicketStatusId", "SupportTicketStatusName");
     ViewBag.SupportTicketTypeId = new SelectList(db.SupportTicketTypes, "SupportTicketTypeId", "SupportTicketTypeName");
     var newTicket = new SupportTicket() {iDate = DateTime.Now, SupportTicketStatus = db.SupportTicketStatuses.Where(m=>m.SupportTicketStatusName=="New").First() };
     newTicket.SupportTicketStatusID = newTicket.SupportTicketStatus.SupportTicketStatusId;
     if (clientId > 0)
         newTicket.ClientID = clientId;
     return View(newTicket);
 }