//Lấy chi tiết gói cước của khách hàng
 public CustomerServicePlanDetailModel GetCustomerServicePlanDetail(int id)
 {
     try
     {
         CustomerServicePlanDetailModel data = new CustomerServicePlanDetailModel();
         CustomerServicePlan            c    = cnn.CustomerServicePlans.Find(id);
         data.ID          = c.ID;
         data.Code        = c.code;
         data.CusName     = c.Customer.Name;
         data.ServiceName = c.Order.ServicePlan.Name;
         data.LocaRequest = !String.IsNullOrEmpty(c.Address) ? c.Customer.Village.Name + " " + c.Customer.District.Name + " " + c.Customer.Province.Name : c.Address;
         data.ActiveDate  = c.ActiveDate;
         data.Status      = c.Status.Value;
         data.histories   = c.HistoryCustomerServicePlans.Where(h => h.IsActive.Equals(SystemParam.ACTIVE)).Select(h => new HistoryCustomerServicePlan
         {
             Note        = h.Note,
             Status      = h.Status,
             CreatedDate = h.CreatedDate
         }).OrderByDescending(h => h.ID).ToList();
         return(data);
     }
     catch
     {
         return(new CustomerServicePlanDetailModel());
     }
 }
 public JsonResultModel CusDelSercicePlan(int id)
 {
     try
     {
         CustomerServicePlan s = cnn.CustomerServicePlans.Find(id);
         s.Status = SystemParam.NO_ACTIVE_STATUS;
         cnn.SaveChanges();
         return(rp.response(SystemParam.SUCCESS, SystemParam.SUCCESS_CODE, SystemParam.SUCCESS_MESSAGE, ""));
     }
     catch
     {
         return(rp.serverError());
     }
 }
        //Cập nhật thông tin gói cước của khách hàng
        public JsonResultModel UpdateCustomerServicePlan(CustomerServicePlanDetailModel input)
        {
            try
            {
                CustomerServicePlan        c = cnn.CustomerServicePlans.Find(input.ID);
                HistoryCustomerServicePlan h = new HistoryCustomerServicePlan();
                string content = "";

                switch (input.Type)
                {
                //Cập nhật trạng thái của gói cước
                case 1:
                    if (!input.Status.Equals(c.Status.Value))
                    {
                        switch (input.Status)
                        {
                        case SystemParam.NO_ACTIVE_STATUS:

                            //Lưu lại lịch sử gói cước
                            h.UserID   = input.UserID;
                            h.Note     = !String.IsNullOrEmpty(input.Note) ? input.Note : "";
                            h.IsActive = SystemParam.ACTIVE;
                            h.CustomerServicePlanID = c.ID;
                            h.Status      = input.Status;
                            h.CreatedDate = DateTime.Now;
                            cnn.HistoryCustomerServicePlans.Add(h);
                            content = "Gói cước " + c.Order.ServicePlan.Name + " của bạn đã bị ngừng hoạt động";
                            break;

                        case SystemParam.ACTIVE_STATUS:

                            h.UserID = input.UserID;
                            h.Note   = !String.IsNullOrEmpty(input.Note) ? input.Note : "";
                            h.CustomerServicePlanID = c.ID;
                            h.IsActive    = SystemParam.ACTIVE;
                            h.Status      = input.Status;
                            h.CreatedDate = DateTime.Now;
                            cnn.HistoryCustomerServicePlans.Add(h);
                            content = "Gói cước " + c.Order.ServicePlan.Name + " của bạn đã được hoạt động trở lại";
                            break;

                        default: break;
                        }
                    }
                    c.Status = input.Status;
                    break;

                //Gia gạn thêm cho gói cước
                case 2:
                    c.Status = SystemParam.ACTIVE_STATUS;

                    h.UserID      = input.UserID;
                    h.Note        = !String.IsNullOrEmpty(input.Note) ? input.Note : "";
                    h.IsActive    = SystemParam.ACTIVE;
                    h.CreatedDate = DateTime.Now;
                    cnn.HistoryCustomerServicePlans.Add(h);
                    c.ExtendDate            = DateTime.Now;
                    h.CustomerServicePlanID = c.ID;
                    h.Status     = SystemParam.ACTIVE_STATUS;
                    c.ExpiryDate = DateTime.Now.AddMonths(c.Order.ServicePlan.Value);
                    h.Status     = SystemParam.ACTIVE_STATUS;
                    content      = "Gói cước " + c.Order.ServicePlan.Name + " của bạn đã được gia hạn thêm";

                    break;

                default:
                    break;
                }
                cnn.SaveChanges();
                if (!String.IsNullOrEmpty(content))
                {
                    email.configClient(c.Customer.Email, "[NEXUS SYSTEM THÔNG BÁO]", content);
                }
                return(rp.response(SystemParam.SUCCESS, SystemParam.SUCCESS_CODE, "Thành công", ""));
            }
            catch (Exception e)
            {
                e.ToString();
                return(rp.serverError());
            }
        }
        //Cập nhật đơn hàng
        public JsonResultModel UpdateBill(OrderDetailOuputModel input, int userID, int role)
        {
            try
            {
                EmailBusiness email = new EmailBusiness();
                if (role.Equals(SystemParam.ROLE_TECHNICAL_STAFF))
                {
                    return(rp.response(SystemParam.ERROR, SystemParam.FAIL, "Bạn không được phép sửa đơn hàng", null));
                }
                Order o = cnn.Orders.Find(input.ID);

                switch (input.Status)
                {
                case SystemParam.PENDING:

                    o.DiscountValue = input.DiscountValue;
                    o.Discount      = input.Discount;
                    o.TotalPrice    = input.TotalPrice;
                    o.AdminNote     = input.AdminNote;
                    if (o.Status != input.Status)
                    {
                        email.configClient(o.Customer.Email, "[NEXUS SYSTEM THÔNG BÁO]", "Đơn hàng " + o.Code + " của bạn đang chờ xác nhận");
                    }
                    o.Status       = input.Status;
                    o.UserHandleID = userID;
                    break;

                case SystemParam.ACCEPT:
                    o.DiscountValue = input.DiscountValue;
                    o.Discount      = input.Discount;
                    o.TotalPrice    = input.TotalPrice;
                    o.AdminNote     = input.AdminNote;
                    if (o.Status != input.Status)
                    {
                        email.configClient(o.Customer.Email, "[NEXUS SYSTEM THÔNG BÁO]", "Đơn hàng " + o.Code + " của bạn đã được xác nhận");
                    }
                    o.Status       = input.Status;
                    o.UserHandleID = userID;
                    break;

                case SystemParam.COMPLETE:

                    o.DiscountValue = input.DiscountValue;
                    o.Discount      = input.Discount;
                    o.TotalPrice    = input.TotalPrice;
                    o.AdminNote     = input.AdminNote;


                    if (o.Status != input.Status)
                    {
                        //Lưu lịch sử
                        List <HistoryCustomerServicePlan> history = new List <HistoryCustomerServicePlan>();
                        HistoryCustomerServicePlan        h       = new HistoryCustomerServicePlan();

                        h.Note        = "Dịch vụ dược kích hoạt";
                        h.UserID      = userID;
                        h.IsActive    = SystemParam.ACTIVE;
                        h.CreatedDate = DateTime.Now;
                        history.Add(h);

                        //Kích hoạt gói cước
                        CustomerServicePlan c = new CustomerServicePlan();
                        c.CustomerID  = o.CustomerID;
                        c.ActiveDate  = DateTime.Now;
                        c.ExtendDate  = DateTime.Now;
                        c.ExpiryDate  = DateTime.Now.AddMonths(o.ServicePlan.Value);
                        c.CreatedDate = DateTime.Now;
                        c.code        = o.Code;
                        c.OrderID     = o.ID;
                        c.Status      = SystemParam.ACTIVE_STATUS;
                        c.IsActive    = SystemParam.ACTIVE;
                        c.HistoryCustomerServicePlans = history;
                        c.Address = !String.IsNullOrEmpty(o.Address) ? o.Customer.Village.Name + " " + o.Customer.District.Name + " " + o.Customer.Province.Name : o.Address;
                        cnn.CustomerServicePlans.Add(c);


                        email.configClient(o.Customer.Email, "[NEXUS SYSTEM THÔNG BÁO]", "Đơn hàng " + o.Code + " của bạn đã được hoàn thành");
                    }

                    o.Status       = input.Status;
                    o.UserHandleID = userID;

                    break;

                case SystemParam.CANCEL:

                    o.DiscountValue = input.DiscountValue;
                    o.Discount      = input.Discount;
                    o.TotalPrice    = input.TotalPrice;
                    o.AdminNote     = input.AdminNote;

                    if (o.Status != input.Status)
                    {
                        email.configClient(o.Customer.Email, "[NEXUS SYSTEM THÔNG BÁO]", "Đơn hàng " + o.Code + " của bạn bị trừ chối vì lý do: " + input.AdminNote);
                    }
                    o.Status = input.Status;
                    break;

                default:
                    break;
                }
                cnn.SaveChanges();
                return(rp.response(SystemParam.SUCCESS, SystemParam.SUCCESS_CODE, SystemParam.SUCCESS_MESSAGE, ""));
            }
            catch (Exception e)
            {
                e.ToString();
                return(rp.serverError());
            }
        }