// GET: PhieuNhap/Edit/5
 public ActionResult Edit(int?id)
 {
     if (id == null)
     {
         putErrorMessage("Đường dẫn không chính xác");
         return(RedirectToAction("All"));
     }
     if (_currentPhieu == null || _currentPhieu != id)
     {
         _currentPhieu = id;
         _phieu        = PhieuXuatManager.find((int)id);
         if (_phieu == null)
         {
             putErrorMessage("Không tìm thấy");
             return(RedirectToAction("All"));
         }
         if (_phieu.TrangThai == 1)
         {
             //Nếu đã duyệt thì không cho sửa, chuyển sang trang chi tiết
             _currentPhieu = null;
             putErrorMessage("Phiếu đã duyệt");
             return(RedirectToAction("Details", new { id = id }));
         }
     }
     ViewBag.cultureInfo = CultureInfo;
     ViewBag.DMSach      = new SelectList(SachManager.getAllAlive()
                                          .Where(s => s.Soluong > 0).ToList(),
                                          nameof(SachManager.Properties.MaSoSach),
                                          nameof(SachManager.Properties.TenSach), "");
     ViewBag.DMDaiLy = new SelectList(DaiLyManager.getAllAlive(),
                                      nameof(DaiLyManager.Properties.MaSoDaiLy),
                                      nameof(DaiLyManager.Properties.TenDaiLy), "");
     setAlertMessage();
     return(View(_phieu));
 }
        private void btXoa_Click(object sender, EventArgs e)
        {
            if (!txbMaPhieuXuat.Text.Equals(""))
            {
                PhieuXuat pn    = new PhieuXuat();
                int       x     = int.Parse(txbMaPhieuXuat.Text.ToString());
                var       phieu = PhieuXuatManager.find(x);

                if (phieu.TrangThai != 1)
                {
                    if (PhieuXuatManager.delete(phieu.MaSoPhieuXuat))
                    {
                        MessageBox.Show("Đã xóa thành công");
                        LoadPX();
                    }
                    else
                    {
                        MessageBox.Show("Không xóa được");
                    }
                }
                else
                {
                    MessageBox.Show("...");
                }
            }
            else
            {
                MessageBox.Show("Chọn phiếu xuất cần xóa");
            }
        }
        // GET: PhieuNhap/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                putErrorMessage("Đường dẫn không chính xác");
                return(RedirectToAction("All"));
            }
            var model = PhieuXuatManager.find((int)id);

            if (model == null)
            {
                putErrorMessage("Không tìm thấy");
                return(RedirectToAction("All"));
            }
            setAlertMessage();
            return(View(model));
        }
        private void btDuyet_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Bạn có muốn duyệt phiếu này", "Thông báo", MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)
            {
                if (!txbMaPhieuXuat.Text.Equals(""))
                {
                    int maso  = int.Parse(txbMaPhieuXuat.Text.ToString());
                    var phieu = PhieuXuatManager.find(maso);
                    if (phieu != null)
                    {
                        var result = phieu.accept();
                        switch (result)
                        {
                        case PhieuXuat.AcceptStatus.Success:
                            MessageBox.Show("Đã duyệt thành công");
                            LoadPX();
                            return;

                        case PhieuXuat.AcceptStatus.Error:
                            MessageBox.Show("Sách tồn không đủ để duyệt! Phiếu xuất yêu cầu được hủy!");
                            return;

                        case PhieuXuat.AcceptStatus.Limited:
                            MessageBox.Show("Tiền nợ đã vượt quá mức cho phép, vui lòng thanh toán trước khi đặt tiếp");
                            return;

                        default:
                            MessageBox.Show("Duyệt không thành công");
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Chưa chọn phiếu xuất cần duyệt");
                }
            }
            else if (dialogResult == DialogResult.No)
            {
                return;
            }
        }
        //Duyệt phiếu
        public ActionResult Accept(int?id)
        {
            if (id == null)
            {
                putErrorMessage("Đường dẫn không chính xác");
                return(RedirectToAction("All"));
            }
            var model = PhieuXuatManager.find((int)id);

            if (model == null)
            {
                putErrorMessage("Không tìm thấy");
                return(RedirectToAction("All"));
            }
            if (model.TrangThai == 1)
            {
                putErrorMessage("Phiếu đã duyệt");
                return(RedirectToAction("Details", new { id = id }));
            }
            var result = model.accept();

            switch (result)
            {
            case PhieuXuat.AcceptStatus.Success:
                putSuccessMessage("Đã duyệt thành công");
                return(RedirectToAction("Details", new { id = id }));

            case PhieuXuat.AcceptStatus.Error:
                putErrorMessage("Sách tồn không đủ để duyệt! Phiếu xuất yêu cầu được hủy!");
                return(RedirectToAction("Edit", new { id }));

            case PhieuXuat.AcceptStatus.Limited:
                putErrorMessage("Tiền nợ đã vượt quá mức cho phép, vui lòng thanh toán trước khi đặt tiếp");
                return(RedirectToAction("Edit", new { id }));

            default:
                putErrorMessage("Duyệt không thành công");
                return(RedirectToAction("Edit", new { id }));
            }
        }
 public ActionResult DeleteOrder(int id)
 {
     if (isUserSessionExisted())
     {
         var user = Session[Core.Constants.SESSION.USERNAME] as NguoiDung;
         if (user.DaiLy == null)
         {
             putErrorMessage("Chưa đăng ký thông tin đại lý");
             return(RedirectToAction("Agency"));
         }
         var model = PhieuXuatManager.find(id);
         if (model == null)
         {
             putErrorMessage("Không tìm thấy đơn");
             return(RedirectToAction("Order"));
         }
         if (user.DaiLy.getAllPhieuXuat().Contains(model))
         {
             if (PhieuXuatManager.delete((int)id))
             {
                 putSuccessMessage("Xóa thành công");
                 return(RedirectToAction("Order"));
             }
             else
             {
                 putErrorMessage("Xóa không thành công");
                 return(RedirectToAction("Order"));
             }
         }
         else
         {
             putErrorMessage("Không tìm thấy đơn");
             return(RedirectToAction("Order"));
         }
     }
     else
     {
         return(RedirectToAction("Index", "Home", null));
     }
 }
        // GET: PhieuNhap/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                putErrorMessage("Đường dẫn không chính xác");
                return(RedirectToAction("All"));
            }
            var model = PhieuXuatManager.find((int)id);

            if (model == null)
            {
                putErrorMessage("Không tìm thấy");
                return(RedirectToAction("All"));
            }
            if (model.TrangThai == 1)
            {
                putErrorMessage("Phiếu đã duyệt");
                return(RedirectToAction("Details", new { id = model.MaSoPhieuXuat }));
            }
            setAlertMessage();
            return(View(model));
        }
 public ActionResult OrderDetail(int?id)
 {
     if (id == null)
     {
         putErrorMessage("Đường dẫn không đúng");
         return(RedirectToAction("Order"));
     }
     if (isUserSessionExisted())
     {
         var user = Session[Core.Constants.SESSION.USERNAME] as NguoiDung;
         if (user.DaiLy == null)
         {
             putErrorMessage("Chưa đăng ký thông tin đại lý");
             return(RedirectToAction("Agency"));
         }
         var model = PhieuXuatManager.find((int)id);
         if (model == null)
         {
             putErrorMessage("Không tìm thấy đơn");
             return(RedirectToAction("Order"));
         }
         if (user.DaiLy.getAllPhieuXuat().Contains(model))
         {
             setAlertMessage();
             return(View(model));
         }
         else
         {
             putErrorMessage("Không tìm thấy đơn");
             return(RedirectToAction("Order"));
         }
     }
     else
     {
         return(RedirectToAction("Index", "Home", null));
     }
 }