public ActionResult Create([Bind(Include = "IdPx,NgayXuat,NguoiNhan,IdDL")] Phieuxuat phieuxuat, [Bind(Prefix = "ct")] CTPX[] ctpx) { if (ModelState.IsValid) { if (ctpx == null) { ModelState.AddModelError("", "Chưa thêm chi tiết phiếu nhập"); ViewBag.IdDL = new SelectList(db.Dailis, "IdDL", "TenDL"); ViewBag.idsach = new SelectList(db.Saches, "IdSach", "TenSach"); return(View()); } if (!new XuatBUS().KTSL(ctpx)) { ModelState.AddModelError("", "Không đủ số lượng hoặc có sách chưa nhập về"); ViewBag.IdDL = new SelectList(db.Dailis, "IdDL", "TenDL"); ViewBag.idsach = new SelectList(db.Saches, "IdSach", "TenSach"); PXvaCTPXmodel view1 = new PXvaCTPXmodel(); phieuxuat.CTPXes = ctpx; view1.phieuxuat = phieuxuat; return(View(view1)); } else { foreach (var i in ctpx) { //cap nhat sl sách trong kho new XuatBUS().CapnhatSLtrongKho(phieuxuat.NgayXuat, i.SoLuong, i.IdSach); //cap nhat kho dai li KhoDL khodaily = new KhoDL(); khodaily.IdSach = i.IdSach; khodaily.IdDL = phieuxuat.IdDL; khodaily.SL = i.SoLuong; new XuatBUS().AddKhoDL(khodaily); phieuxuat.CTPXes.Add(i); } db.Phieuxuats.Add(phieuxuat); new XuatBUS().Congno(phieuxuat); db.SaveChanges(); return(RedirectToAction("Index")); } } ViewBag.IdDL = new SelectList(db.Dailis, "IdDL", "TenDL"); ViewBag.idsach = new SelectList(db.Saches, "IdSach", "TenSach"); PXvaCTPXmodel view2 = new PXvaCTPXmodel(); phieuxuat.CTPXes = ctpx; view2.phieuxuat = phieuxuat; return(View(view2)); }
public void AddKhoDL(KhoDL kho) { var a = (from c in db.KhoDLs where c.IdDL == kho.IdDL && c.IdSach == kho.IdSach select c ).FirstOrDefault(); if (a != null) { a.SL += kho.SL; db.Entry(a).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } else { db.KhoDLs.Add(kho); db.SaveChanges(); } }