public ActionResult SaveTicket(Models.Ticket.ViewModelTicket ticket, string description, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                ticket.TicketId        = Convert.ToInt32(UnitOfTicket.Next());
                ticket.OpenDateAndTime = DateTime.Now.ToLocalTime();
                ticket.UserId          = User.Identity.GetUserName();
                var user = (System.Security.Claims.ClaimsIdentity)User.Identity;
                ticket.CompanyId = Convert.ToInt32(user.FindFirstValue("CompanyId"));
                ticket.StatusId  = 1;


                var ret = Repository._priorityTime.Where(x => x.PriorityId == ticket.PriorityId)
                          .Select(x => x.SlaTime).FirstOrDefault();


                ticket.SlaExpiration = CalculateSla.AddWithinWorkingHours(ticket.OpenDateAndTime,
                                                                          TimeSpan.FromHours(ret), 9, 8);


                this.UnitOfTicket.Save(ticket);

                // Chech if exists files
                var path = string.Empty;
                if (file != null)
                {
                    path = UploadAndDownload.Upload(file, ticket.TicketId);
                }


                var actions = new Models.Ticket.Action
                {
                    TicketId          = ticket.TicketId,
                    ActionDescription = description,
                    StatusId          = ticket.StatusId,
                    Date       = DateTime.Now.ToLocalTime(),
                    AlteredBy  = User.Identity.Name,
                    SendToUser = true,
                    SlaRest    = TimeSpan.FromHours(
                        Repository._priorityTime
                        .Where(x => x.PriorityId == ticket.PriorityId)
                        .Select(x => x.SlaTime).FirstOrDefault()),
                    Files       = path,
                    IterationId = 1
                };

                this.UnitOfTicketAction.Save(actions);

                //return RedirectToAction(nameof(NewTicket));
                if (User.IsInRole("User") || User.IsInRole("SuperUser"))
                {
                    return(RedirectToAction(nameof(MyTickets)));
                }
                else
                {
                    return(RedirectToAction(nameof(TicketList)));
                }
            }
            else
            {
                return(View(nameof(NewTicket), ticket));
            }
        }
        public JsonResult SaveTicketAjaxUpdate(ViewModelTicket ticket, string description, bool SendToUser, HttpPostedFileBase file)
        {
            var diff       = TimeSpan.Zero;
            var diffTime   = 0.0;
            var lastStatus = UnitOfTicketAction.Where(x => x.TicketId == ticket.TicketId)
                             .OrderByDescending(x => x.IterationId)
                             .Select(x => x.StatusId).FirstOrDefault();


            if (ticket.StatusId == 6 || ticket.StatusId == 7 &&
                lastStatus != 6 && lastStatus != 7)
            {
                ticket.ClosedDateTime = DateTime.Now.ToLocalTime();
            }

            else if (ticket.StatusId == 3 || ticket.StatusId == 4 || ticket.StatusId == 5 &&
                     lastStatus != 3 && lastStatus != 4 && lastStatus != 5)
            {
                var sla       = new CalculateSla();
                var TotalTime = sla.SlaRestTime(ticket.TicketId);

                var time = CalculateSla.SubtractWithinWorkingHours(DateTime.Now.ToLocalTime(), (int)TotalTime);

                ticket.SlaExpiration = CalculateSla.AddWithinWorkingHours(DateTime.Now.ToLocalTime(), TimeSpan.FromHours(time), 9, 8);


                /*
                 * var dateTicket = UnitOfTicket.Where(x => x.TicketId == ticket.TicketId)
                 * .Select(x => x.SlaExpiration).FirstOrDefault();
                 *  diffTime = dateTicket.Subtract(DateTime.Now.ToLocalTime()).TotalHours;
                 * CalculateSla.SubtractWithinWorkingHours(DateTime.Now.ToLocalTime(), (int)diffTime);
                 */
            }

            else if (ticket.StatusId == 2 && lastStatus == 3 ||
                     lastStatus == 4 || lastStatus == 5)
            {
                var slaRest = UnitOfTicketAction.Where(x => x.TicketId == ticket.TicketId).OrderByDescending(x => x.IterationId).Select(x => x.SlaRest).FirstOrDefault();
                //diff = slaRest;
                diffTime = slaRest.TotalHours;
                ticket.ClosedDateTime = DateTime.Now.ToLocalTime().Add(slaRest);
            }

            var path = string.Empty;

            if (file != null)
            {
                path = UploadAndDownload.Upload(file, ticket.TicketId);
            }



            //If Ticket exist then Update Ticket
            this.UnitOfTicket.Update(ticket, ticket.TicketId);

            var actions = new Models.Ticket.Action
            {
                TicketId          = ticket.TicketId,
                ActionDescription = description,
                StatusId          = ticket.StatusId,
                Date       = DateTime.Now.ToLocalTime(),
                PriorityId = ticket.PriorityId,
                AlteredBy  = User.Identity.Name,
                SendToUser = SendToUser,
                //SlaRest = TimeSpan.FromHours(diffTime),
                IterationId = UnitOfTicketAction.Where(x => x.TicketId == ticket.TicketId).Max(x => x.IterationId) + 1
            };


            if (!string.IsNullOrEmpty(description))
            {
                UnitOfTicketAction.Save(actions);
            }


            return(Json(new { actions, ticket }, JsonRequestBehavior.AllowGet));
        }