public JsonResult SaveCustomerInDatabase(int ID, string Name, string Address, string Phone)
        {
            var result = false;

            try
            {
                if (ID > 0)
                {
                    Customer Cus = db.Customers.SingleOrDefault(x => x.Id_Customer == ID);
                    Cus.FullName = Name;
                    Cus.Address  = Address;
                    Cus.Phone    = Phone;
                    db.SaveChanges();
                    result = true;
                }
                else
                {
                    Customer Cus = new Customer();
                    Cus.FullName = Name;
                    Cus.Address  = Address;
                    Cus.Phone    = Phone;
                    Cus.Fine     = 0;


                    db.Customers.Add(Cus);
                    db.SaveChanges();
                    result = true;
                }
            }
            catch
            {
                throw new HttpException(404, "Add false");
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult AddOrder(string lst, string date1, string date2, int CusID)
        {
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            List <int>           Lst             = json_serializer.Deserialize <List <int> >(lst);


            Order or = new Order();

            or.DateRent    = DateTime.Now;
            or.TotalRent   = 0;
            or.Id_Customer = CusID;
            db.Orders.Add(or);

            db.SaveChanges();

            foreach (int i in Lst)
            {
                Item x = db.Items.Where(t => t.Id_Item == i).FirstOrDefault();
                if (x != null)
                {
                    OrderDetail ord = new OrderDetail();
                    ord.Id_Item    = i;
                    ord.Id_Order   = or.Id_Order;
                    ord.DateDue    = DateTime.Now.AddDays(x.Titles.TypeDisk.MaxDate);
                    ord.DateReturn = DateTime.Now.AddDays(x.Titles.TypeDisk.MaxDate);
                    ord.Status     = "Chưa Trả";
                    ord.RentFee    = x.Titles.TypeDisk.RentPrice;
                    or.TotalRent  += ord.RentFee;
                    db.OrderDetails.Add(ord);

                    Item it = new Item();
                    x.Status  = "Rented";
                    it.Status = x.Status;
                }
                else
                {
                    break;
                }
            }

            db.SaveChanges();


            return(Json(true, JsonRequestBehavior.AllowGet));
        }
        public JsonResult UpdateItemInDatabase(int Id_Item, int Id_Title, string Status)
        {
            var result = false;

            try
            {
                Item item = db.Items.SingleOrDefault(x => x.Id_Item == Id_Item);
                item.Id_Title = Id_Title;
                item.Status   = Status;
                db.SaveChanges();
                result = true;
            }
            catch
            {
                throw new HttpException(404, "Add false");
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveTitleInDatabase(string Name_Title, int Type)
        {
            var result = false;

            try
            {
                Title t = new Title();
                t.Name        = Name_Title;
                t.Id_TypeDisk = Type;
                t.CountOfItem = 0;
                db.Titles.Add(t);
                db.SaveChanges();
                result = true;
            }
            catch
            {
                throw new HttpException(404, "Add false");
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveDiskReturn(string lst)
        {
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            List <int>           Lst             = json_serializer.Deserialize <List <int> >(lst);

            OrderDetail d = new OrderDetail();

            foreach (int i in Lst)
            {
                OrderDetail o    = db.OrderDetails.Where(a => a.Id_Item == i && a.Status == "Chưa Trả").FirstOrDefault();
                Item        item = db.Items.Where(t => t.Id_Item == i).FirstOrDefault();
                if (o != null)
                {
                    o.Status     = "Đã Trả";
                    o.DateReturn = DateTime.Now;
                    item.Status  = "On-Shelf";

                    DateTime ngaytra    = Convert.ToDateTime(o.DateReturn);
                    DateTime hantra     = Convert.ToDateTime(o.DateDue);
                    TimeSpan Time       = ngaytra - hantra;
                    int      TongSoNgay = Time.Days;
                    if (TongSoNgay > 0)
                    {
                        LateFee lt = new LateFee();
                        lt.Id_OrderDetail = o.Id_OrderDetail;
                        lt.Late_Fee       = o.Items.Titles.TypeDisk.LateFee;
                        lt.NumOfLateDate  = TongSoNgay;
                        lt.Total          = o.Items.Titles.TypeDisk.LateFee * TongSoNgay;
                        lt.Status         = "Chưa Trả";
                        db.LateFees.Add(lt);
                        o.Orders.Customers.Fine += lt.Total;
                        Customer cus = new Customer();
                        cus.Fine = o.Items.Titles.TypeDisk.LateFee * TongSoNgay;
                    }
                }
                else
                {
                    break;
                }
                //Kiểm tra Reservation nếu có thì thêm ReservDetail cho Reservation có id nhỏ nhất, đặt status của item = "On Hold"
                new ReservationsController().PlaceOnHold(item);
            }
            db.SaveChanges();

            string value = string.Empty;

            value = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
            return(Json(value, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveReservationsInDatabase(string Res_Id_Title, int Res_Cus_Id_Customer)
        {
            var result = false;

            try
            {
                Reservation Res = new Reservation();

                Res.Id_Title    = int.Parse(Res_Id_Title);
                Res.Id_Customer = Res_Cus_Id_Customer;
                Res.DateCreate  = DateTime.Now;
                Res.Status      = "Chưa có đĩa";
                db.Reservations.Add(Res);
                db.SaveChanges();
                result = true;
            }
            catch
            {
                throw new HttpException(404, "Add false");
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveLateFee(int Id_LateFee)
        {
            LateFee lt = db.LateFees.Where(x => x.Id_LateFee == Id_LateFee && x.Status == "Chưa Trả").FirstOrDefault();

            if (lt != null)
            {
                lt.Status = "Đã Trả";
                lt.OrderDetails.Orders.Customers.Fine -= lt.Total;
            }
            db.SaveChanges();

            return(Json(true, JsonRequestBehavior.AllowGet));
        }
示例#8
0
        public JsonResult UpdateTypeInDatabase(int Id_Type, double RentPrice, int MaxDate)
        {
            TypeDisk d = db.Types.Where(x => x.Id_TypeDisk == Id_Type).FirstOrDefault();

            if (d != null)
            {
                d.RentPrice = RentPrice;
                d.MaxDate   = MaxDate;
                db.SaveChanges();
                return(Json(true, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(false, JsonRequestBehavior.AllowGet));
            }
        }