public static bool edit(PhieuXuat phieu)
        {
            try
            {
                using(EntitiesDataContext db = new EntitiesDataContext())
                {
                    PHIEUXUAT px;
                    px = (from p in db.PHIEUXUATs
                          where p.masophieuxuat.Equals(phieu.MaSoPhieuXuat)
                          select p).SingleOrDefault();
                    if (px == null) return false;
                    px.masodaily = phieu.MaSoDaiLy;
                    px.ngaylap = phieu.NgayLap;
                    px.nguoinhasach = phieu.NguoiNhan;
                    px.trangthai = phieu.TrangThai;
                    px.tongtien = phieu.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia); // tính tổng tiền các chi tiết
                    db.CHITIETPHIEUXUATs.DeleteAllOnSubmit(px.CHITIETPHIEUXUATs);
                    db.SubmitChanges();
                    foreach (ChiTietPhieuXuat ct in phieu.ChiTiet)
                    {
                        ct.MaSoPhieuXuat = phieu.MaSoPhieuXuat;
                        Chitiet.add(ct);
                    }

                    return true;
                }
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
 public static int add(PhieuXuat phieuxuat)
 {
     try
     {
         using (EntitiesDataContext db = new EntitiesDataContext())
         {
             var phieu = new PHIEUXUAT
             {
                 masodaily = phieuxuat.MaSoDaiLy,
                 ngaylap = phieuxuat.NgayLap,
                 nguoinhasach = phieuxuat.NguoiNhan,
                 tongtien = phieuxuat.ChiTiet.Sum(ct => ct.SoLuong * ct.DonGia),
                 trangthai = 0
             };
             db.PHIEUXUATs.InsertOnSubmit(phieu);
             db.SubmitChanges();
             foreach(ChiTietPhieuXuat ct in phieuxuat.ChiTiet)
             {
                 Chitiet.add(ct, phieu.masophieuxuat);
             }
             return phieu.masophieuxuat;
         }
     }catch(Exception ex)
     {
         Console.WriteLine(ex.Message);
         return 0;
     }
 }
 public ActionResult Edit(PhieuXuat model, FormCollection collection)
 {
     try
     {
         if (ModelState.IsValid)
         {
             if (PhieuXuatManager.edit(model))
             {
                 _currentPhieu = null;
                 putSuccessMessage("Cập nhật thành công");
                 return RedirectToAction("Details", new { id = model.MaSoPhieuXuat });
             }
             else
             {
                 putErrorMessage("Cập nhật không thành công");
             }
         }
         else
         {
             putModelStateFailErrors(ModelState);
             return RedirectToAction("Edit", new { id = model.MaSoPhieuXuat});
         }
         //// TODO: Add update logic here
         //_phieu = model;
         //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), "");
         //return View(_phieu);
         return RedirectToAction("Edit", new { id = model.MaSoPhieuXuat });
     }
     catch(Exception ex)
     {
         putErrorMessage(ex.Message);
         return RedirectToAction("Edit", new { id = model.MaSoPhieuXuat });
     }
 }
 // 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);
 }
        public ActionResult Create(PhieuXuat model, FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    var result = PhieuXuatManager.add(model);
                    if (result != 0)
                    {
                        _phieu = null;
                        putSuccessMessage("Thêm thành công");
                        return RedirectToAction("Details", new { id = result });
                    }
                    else
                    {
                        putErrorMessage("Thêm không thành công");
                    }
                }
                else
                {
                    putModelStateFailErrors(ModelState);
                }
                //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), "");
                //_phieu.NgayLap = DateTime.Now;

                //return View(_phieu);
                return RedirectToAction("Create");
            }
            catch(Exception ex)
            {
                putErrorMessage(ex.Message);
                return View();
            }
        }
 // GET: PhieuNhap/Create
 public ActionResult Create()
 {
     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), "");
     if (_phieu == null)
     {
         _phieu = new PhieuXuat();
     }
     _phieu.NgayLap = DateTime.Now;
     setAlertMessage();
     return View(_phieu);
 }
 public ActionResult ShoppingCart()
 {
     currentPhieu = null;
     var model = currentPhieu;
     if (TempData[Core.Constants.TEMPDATA.ERRORS] != null)
     {
         ViewBag.Errors = TempData[Core.Constants.TEMPDATA.ERRORS];
         TempData[Core.Constants.TEMPDATA.ERRORS] = null;
     }
     return View(model);
 }
        public ActionResult CheckOut(PhieuXuat model)
        {
            if (isUserSessionExisted())
            {

                currentPhieu.Daily = currentNguoiDung.DaiLy;
                currentPhieu.NgayLap = DateTime.Now;
                currentPhieu.NguoiNhan = currentNguoiDung.TenDayDu;
                currentPhieu.MaSoDaiLy = currentNguoiDung.DaiLy.MaSoDaiLy;
                var result = PhieuXuatManager.add(currentPhieu);
                if(result != 0)
                {
                    return RedirectToAction("OrderDetail","Manager",new { id = result });
                }
                else
                {
                    return RedirectToAction("CheckOut");
                }
            }
            else
            {
                return RedirectToAction("Login", "Account", null);
            }
        }
 public ActionResult CheckOut()
 {
     currentPhieu = null;
     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", "Manager",null);
         }
         var model = currentPhieu;
         if (TempData[Core.Constants.TEMPDATA.ERRORS] != null)
         {
             ViewBag.Errors = TempData[Core.Constants.TEMPDATA.ERRORS];
             TempData[Core.Constants.TEMPDATA.ERRORS] = null;
         }
         return View(model);
     }
     else
     {
         return RedirectToAction("Login", "Account", null);
     }
 }
        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");
        }