Ejemplo n.º 1
0
        public async Task <IActionResult> PutDattruoc(string mathe, string masach, Dattruoc dattruoc)
        {
            if (mathe != dattruoc.Mathe)
            {
                return(BadRequest());
            }
            if (masach != dattruoc.Masach)
            {
                return(BadRequest());
            }
            var _dattruocCu = await _context.Dattruoc.FindAsync(mathe, masach);

            dattruoc.Ngaydattruoc = _dattruocCu.Ngaydattruoc;
            //neu da nhan thi tao ra phieu muon
            if (dattruoc.Danhan == true)
            {
                var _sach = await _context.Sach.FindAsync(dattruoc.Masach);

                var _nhomsach = await _context.Nhomsach.FindAsync(_sach.Manhomsach);

                var _theloai = await _context.Theloai.FindAsync(_nhomsach.Matheloai);

                _sach.Damuon = true;

                Phieumuon _phieumuon = new Phieumuon();
                _phieumuon.Mathe      = dattruoc.Mathe;
                _phieumuon.Masach     = dattruoc.Masach;
                _phieumuon.Ngaymuon   = dattruoc.Ngaydattruoc;
                _phieumuon.Ngayhethan = _phieumuon.Ngaymuon.AddDays(_theloai.Songaymuontoida);
                _phieumuon.Giahan     = false;
                _phieumuon.Datra      = false;
                _context.Phieumuon.Add(_phieumuon);
            }
            _context.Entry(_dattruocCu).State = EntityState.Detached;
            _context.Entry(dattruoc).State    = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DattruocExists(mathe, masach))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
        //Cập nhật giỏ hàng
        public ActionResult CapnhatPhieumuon(int iMaSP, FormCollection f)
        {
            //Lay gio hang
            List <Phieumuon> lstPhieumuon = LayPhieumuon();
            //kiểm tra sách có trong giỏ hàng
            Phieumuon sanpham = lstPhieumuon.SingleOrDefault(n => n.iMasach == iMaSP);

            //nếu tồn tại thì cho sữa số lượng
            if (sanpham != null)
            {
                sanpham.iSoluong = int.Parse(f["txtSoluong"].ToString());
            }
            return(RedirectToAction("Phieumuon"));
        }
Ejemplo n.º 3
0
        //Thêm vào giở hàng
        public ActionResult ThemPhieumuon(int iMasach, string strURL)
        {
            List <Phieumuon> lstPhieumuon = LayPhieumuon();
            Phieumuon        sanpham      = lstPhieumuon.Find(n => n.iMasach == iMasach);

            if (sanpham == null)
            {
                sanpham = new Phieumuon(iMasach);
                lstPhieumuon.Add(sanpham);
                return(Redirect(strURL));
            }
            else
            {
                sanpham.iSoluong++;
                return(Redirect(strURL));
            }
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <Phieumuon> > PostPhieumuon(Phieumuon phieumuon)
        {
            //
            phieumuon.Ngaymuon = DateTime.Now;
            var _user = await _context.User.FindAsync(phieumuon.Mathe);

            var _sach = await _context.Sach.FindAsync(phieumuon.Masach);

            var _nhomsach = await _context.Nhomsach.FindAsync(_sach.Manhomsach);

            var _theloai = await _context.Theloai.FindAsync(_nhomsach.Matheloai);

            phieumuon.Ngayhethan = phieumuon.Ngaymuon.AddDays(_theloai.Songaymuontoida);
            phieumuon.Giahan     = false;
            phieumuon.Datra      = false;
            //
            if (_sach.Damuon == true || _sach.Tinhtrangsach == false || _user.Sosachdamuon > 6 || DateTime.Compare(_user.Ngayhethan, phieumuon.Ngaymuon) < 0)
            {
                return(BadRequest());
            }
            //chuyen sach.Damuon = true  va tru so luong con cua nhomsach -1
            _sach.Damuon = true;
            _nhomsach.Soluongcon--;
            _user.Sosachdamuon++;

            _context.Phieumuon.Add(phieumuon);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (PhieumuonExists(phieumuon.Mathe, phieumuon.Masach))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            //return CreatedAtAction("GetPhieumuon", new { id = phieumuon.Mathe }, phieumuon);
            return(Ok());
        }
Ejemplo n.º 5
0
        //Xóa 1 sanpham Giỏ hàng
        public ActionResult XoaPhieumuon(int iMaSP)
        {
            //Lay gio hang
            List <Phieumuon> lstPhieumuon = LayPhieumuon();
            //kiểm tra sách có trong giỏ hàng
            Phieumuon sanpham = lstPhieumuon.SingleOrDefault(n => n.iMasach == iMaSP);

            if (sanpham != null)
            {
                lstPhieumuon.RemoveAll(n => n.iMasach == iMaSP);
                return(RedirectToAction("Phieumuon"));
            }
            if (lstPhieumuon.Count == 0)
            {
                return(RedirectToAction("Index", "ThuVien"));
            }
            return(RedirectToAction("Phieumuon"));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> PutPhieumuon(string mathe, string masach, Phieumuon phieumuon)
        {
            if (mathe != phieumuon.Mathe)
            {
                return(BadRequest());
            }
            if (masach != phieumuon.Masach)
            {
                return(BadRequest());
            }

            var currentUserID = User.Identity.Name;

            if (phieumuon.Mathe != currentUserID && !User.IsInRole(Role.Admin))
            {
                return(Forbid());
            }

            var _phieumuonCu = await _context.Phieumuon.FindAsync(mathe, masach);

            phieumuon.Ngayhethan = _phieumuonCu.Ngayhethan;
            phieumuon.Ngaymuon   = _phieumuonCu.Ngaymuon;
            //neu gia han = true => tang ngay het han len
            if (_phieumuonCu.Giahan == false)
            {
                if (phieumuon.Giahan == true)
                {
                    var _sach = await _context.Sach.FindAsync(phieumuon.Masach);

                    var _nhomsach = await _context.Nhomsach.FindAsync(_sach.Manhomsach);

                    var _theloai = await _context.Theloai.FindAsync(_nhomsach.Matheloai);

                    phieumuon.Ngayhethan = _phieumuonCu.Ngayhethan.AddDays(_theloai.Songaymuontoida);
                }
                else
                {
                    phieumuon.Ngayhethan = _phieumuonCu.Ngayhethan;
                }
            }
            else
            {
                phieumuon.Giahan = true;
            }

            //neu tra sach = true thi damuon=true, soluongcon++ va sinh ra phieu muon(hoa don) va xoa di phieu muon
            if (User.IsInRole(Role.Admin))
            {
                if (_phieumuonCu.Datra == false)
                {
                    if (phieumuon.Datra == true)
                    {
                        var _user = await _context.User.FindAsync(phieumuon.Mathe);

                        var _sach = await _context.Sach.FindAsync(phieumuon.Masach);

                        var _nhomsach = await _context.Nhomsach.FindAsync(_sach.Manhomsach);

                        var _theloai = await _context.Theloai.FindAsync(_nhomsach.Matheloai);

                        //
                        _nhomsach.Soluongcon++;
                        _sach.Damuon = false;
                        _user.Sosachdamuon--;
                        //nen remove di ko?
                    }
                }
            }
            _context.Entry(_phieumuonCu).State = EntityState.Detached;
            _context.Entry(phieumuon).State    = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PhieumuonExists(mathe, masach))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }