public ActionResult Create([Bind(Include = "idsach,tensach,tacgia,idnxb,idlv,giaxuat,gianhap")] sach sach) { if (ModelState.IsValid) { foreach (sach s in db.saches) { if (sach.tensach == s.tensach) { ModelState.AddModelError("", "Trùng tên sách hoặc sách đã tồn tại"); ViewBag.idlv = new SelectList(db.linhvucs, "idlv", "tenlv", sach.idlv); ViewBag.idnxb = new SelectList(db.nxbs, "idnxb", "tennxb", sach.idnxb); return(View()); } } tonkho tk = new tonkho(); tk.idsach = sach.idsach; tk.thoidiem = DateTime.Now; tk.soluongton = 0; db.tonkhoes.Add(tk); db.saches.Add(sach); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idlv = new SelectList(db.linhvucs, "idlv", "tenlv", sach.idlv); ViewBag.idnxb = new SelectList(db.nxbs, "idnxb", "tennxb", sach.idnxb); return(View(sach)); }
public ActionResult Edit([Bind(Prefix = "phieunhap")] phieunhap phieunhap, [Bind(Prefix = "ct")] ctpn[] ctpn) { if (ModelState.IsValid) { int idpn = phieunhap.idpn; int idct = 1; //thêm chi tiết sửa vào database foreach (ctpn ct in ctpn) { ct.idpn = idpn; ct.idctpn = idct; idct++; tonkho tkht = db.tonkhoes.OrderByDescending(o => o.idtk).FirstOrDefault(o => o.idsach == (int)ct.idsach); if (tkht.soluongton != null) { tkht.soluongton = tkht.soluongton + ct.soluong; } else { tkht.soluongton = ct.soluong; } } //xóa chi tiết cũ trong database var ctpncu = db.ctpns.Where(ct => ct.idpn == phieunhap.idpn).ToList(); foreach (var ct in ctpncu) { tonkho tkht = db.tonkhoes.OrderByDescending(o => o.idtk).FirstOrDefault(o => o.idsach == (int)ct.idsach); int soluonghientai = (int)(tkht.soluongton - ct.soluong); if (soluonghientai < 0) { ViewBag.idsach = new SelectList(db.saches, "idsach", "tensach"); ViewBag.idnxb = new SelectList(db.nxbs, "idnxb", "tennxb", phieunhap.idnxb); phieunhapviewmodel pnvm = new phieunhapviewmodel(); phieunhap.ctpns = ctpn; pnvm.phieunhap = phieunhap; return(View(pnvm)); } tkht.soluongton = soluonghientai; db.ctpns.Remove(ct); } foreach (ctpn ct in ctpn) { db.ctpns.Add(ct); } db.Entry(phieunhap).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idsach = new SelectList(db.saches, "idsach", "tensach"); ViewBag.idnxb = new SelectList(db.nxbs, "idnxb", "tennxb", phieunhap.idnxb); phieunhapviewmodel pnvm1 = new phieunhapviewmodel(); phieunhap.ctpns = ctpn; pnvm1.phieunhap = phieunhap; return(View(pnvm1)); }
public bool deleteTonKho(string msp) { try { tonkho tk = db.tonkhos.Single(m => m.masp == msp); db.tonkhos.DeleteOnSubmit(tk); db.SubmitChanges(); return(true); } catch { return(false); } }
public bool updateTonKho(int id, string msp, DateTime ngay, int sl) { try { tonkho tk = db.tonkhos.Single(m => m.id == id); tk.masp = msp; tk.ngaycapnhat = ngay; tk.soluongton = sl; db.SubmitChanges(); return(true); } catch { return(false); } }
public bool insertTonKho(string msp, DateTime ngay, int sl) { try { tonkho tk = new tonkho(); tk.masp = msp; tk.ngaycapnhat = ngay; tk.soluongton = sl; db.tonkhos.InsertOnSubmit(tk); db.SubmitChanges(); return(true); } catch { return(false); } }
public ActionResult Create([Bind(Prefix = "phieunhap")] phieunhap phieunhap, [Bind(Prefix = "ct")] ctpn[] ctpn) { if (ModelState.IsValid) { int idpn = 1; if (db.phieunhaps.Any()) { idpn = db.phieunhaps.Max(o => o.idpn) + 1; } int idct = 1; foreach (ctpn ct in ctpn) { ct.idpn = idpn; ct.idctpn = idct; idct++; tonkho tk = new tonkho(); tk.thoidiem = DateTime.Now; //lấy tồn kho mới nhất của sách này tonkho tkht = db.tonkhoes.OrderByDescending(o => o.idtk).FirstOrDefault(o => o.idsach == (int)ct.idsach); if (tkht != null) { tk.idsach = (int)ct.idsach; tk.soluongton = tkht.soluongton + ct.soluong; } else { tk.idsach = (int)ct.idsach; tk.soluongton = ct.soluong; } db.tonkhoes.Add(tk); } phieunhap.ctpns = ctpn; db.phieunhaps.Add(phieunhap); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.idnxb = new SelectList(db.nxbs, "idnxb", "tennxb", phieunhap.idnxb); ViewBag.idsach = new SelectList(db.saches, "idsach", "tensach"); return(View()); }
public ActionResult Create([Bind(Prefix = "phieuxuat")] phieuxuat phieuxuat, [Bind(Prefix = "ct")] ctpx[] ctpx) { if (ModelState.IsValid) { decimal tongtien = 0; int idpx = 1; if (db.phieuxuats.Any()) { idpx = db.phieuxuats.Max(o => o.idpx) + 1; } int idct = 1; foreach (ctpx ct in ctpx) { ct.idpx = idpx; ct.idctpx = idct; idct++; //cap nhat ton kho hien tai tonkho tk = new tonkho(); tk.thoidiem = DateTime.Now; tonkho tkht = db.tonkhoes.OrderByDescending(o => o.idtk).FirstOrDefault(o => o.idsach == (int)ct.idsach); //kiem tra xem cuon sach du so luong de xuat ko if (tkht != null && tkht.soluongton >= ct.soluong) { tk.idsach = (int)ct.idsach; tk.soluongton = tkht.soluongton - ct.soluong; db.tonkhoes.Add(tk); } else { ModelState.AddModelError("", "Không đủ số lượng hoặc chưa nhập sách về"); ViewBag.iddl = new SelectList(db.dailies, "iddl", "tendl"); ViewBag.idsach = new SelectList(db.saches, "idsach", "tensach"); return(View()); } //cap nhat so sach da gui cho dai ly hangtoncuadaily htdl = db.hangtoncuadailies.FirstOrDefault(o => o.iddl == phieuxuat.iddl && o.idsach == ct.idsach); if (htdl != null) { htdl.soluongchuaban = htdl.soluongchuaban + ct.soluong; db.Entry(htdl).State = EntityState.Modified; } else { htdl = new hangtoncuadaily(); htdl.iddl = phieuxuat.iddl; htdl.idsach = ct.idsach; htdl.soluongchuaban = ct.soluong; db.hangtoncuadailies.Add(htdl); } // tạo mới công nợ tongtien += (decimal)(ct.soluong * db.saches.Find(ct.idsach).giaxuat); } congnotheothoigian cn = new congnotheothoigian(); cn.thoidiem = (DateTime)phieuxuat.ngayxuat; congnotheothoigian cnht = db.congnotheothoigians.OrderByDescending(o => o.id).FirstOrDefault(o => o.iddl == phieuxuat.iddl); if (cnht.congno > 0 && tongtien > cnht.congno) { ModelState.AddModelError("", "Tổng tiền lớn hơn công nợ"); ViewBag.iddl = new SelectList(db.dailies, "iddl", "tendl"); ViewBag.idsach = new SelectList(db.saches, "idsach", "tensach"); return(View()); } if (cnht.congno != null) { cn.iddl = phieuxuat.iddl; System.Diagnostics.Debug.WriteLine("Tổng tiền là : " + tongtien); cn.congno = cnht.congno + tongtien; System.Diagnostics.Debug.WriteLine("Công nợ là : " + cn.congno); db.congnotheothoigians.Add(cn); } else { cn.iddl = phieuxuat.iddl; cn.congno = tongtien; db.congnotheothoigians.Add(cn); } phieuxuat.ctpxes = ctpx; db.phieuxuats.Add(phieuxuat); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.iddl = new SelectList(db.dailies, "iddl", "tendl"); ViewBag.idsach = new SelectList(db.saches, "idsach", "tensach"); return(View()); }
public ActionResult Edit([Bind(Prefix = "phieuxuat")] phieuxuat phieuxuat, [Bind(Prefix = "ct")] ctpx[] ctpx) { if (ModelState.IsValid) { int idpx = phieuxuat.idpx; int idct = 1; //xóa chi tiết cũ trong database table sach //tinh tong tien cu int tongtiencu = 0; var ctpxcu = db.ctpxes.Where(ct => ct.idpx == phieuxuat.idpx).ToList(); foreach (var ct in ctpxcu) { sach s = db.saches.Find(ct.idsach); tonkho tkht = db.tonkhoes.OrderByDescending(o => o.idtk).FirstOrDefault(o => o.idsach == (int)ct.idsach); int soluonghientai = (int)(tkht.soluongton + ct.soluong); tkht.soluongton = soluonghientai; tongtiencu += (int)(ct.soluong * s.giaxuat); } //thêm chi tiết sửa vào database table hangtoncuadaily foreach (ctpx ct in ctpx) { ct.idpx = idpx; ct.idctpx = idct; idct++; hangtoncuadaily ht = db.hangtoncuadailies.FirstOrDefault(o => o.iddl == phieuxuat.iddl && o.idsach == ct.idsach); ht.soluongchuaban = (int)(ht.soluongchuaban + ct.soluong); } //xoa chi tiet cu trong database table hangtoncuadaily foreach (ctpx ct in ctpxcu) { hangtoncuadaily ht = db.hangtoncuadailies.FirstOrDefault(o => o.iddl == phieuxuat.iddl && o.idsach == ct.idsach); int hangtondaily = (int)(ht.soluongchuaban - ct.soluong); if (hangtondaily < 0) { ModelState.AddModelError("", "so sach nay da duoc ban" + hangtondaily); phieuxuat.ctpxes = ctpx; return(re(phieuxuat)); } db.ctpxes.Remove(ct); } //tinh tong tien moi int tongtien = 0; //thêm chi tiết sửa vào database table sach foreach (ctpx ct in ctpx) { sach s = db.saches.Find(ct.idsach); tonkho tkht = db.tonkhoes.OrderByDescending(o => o.idtk).FirstOrDefault(o => o.idsach == (int)ct.idsach); if (tkht.soluongton != null) { tkht.soluongton = tkht.soluongton - ct.soluong; } if (tkht.soluongton < 0) { ModelState.AddModelError("", "so luong hien tai khong du de xuat " + tkht.soluongton + " vui long kiem tra lai"); phieuxuat.ctpxes = ctpx; return(re(phieuxuat)); } tongtien += (int)(ct.soluong * s.giaxuat); } //cap nhat cong no daily dl = db.dailies.Find(phieuxuat.iddl); congnotheothoigian cnht = db.congnotheothoigians.OrderByDescending(o => o.id).FirstOrDefault(o => o.iddl == phieuxuat.iddl); if (cnht.congno > 0 && tongtien > cnht.congno) { ModelState.AddModelError("", "tong tien phieu xuat lon hon cong no hien tai cua dai ly"); phieuxuat.ctpxes = ctpx; return(re(phieuxuat)); } if (cnht.congno != null) { cnht.congno = cnht.congno + tongtien - tongtiencu; } if (cnht.congno < 0) { ModelState.AddModelError("", "cong no dai ly la so am => co sach da ban trong phieu chua sua"); phieuxuat.ctpxes = ctpx; return(re(phieuxuat)); } foreach (ctpx ct in ctpx) { db.ctpxes.Add(ct); } db.Entry(phieuxuat).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index"));; } phieuxuat.ctpxes = ctpx; return(re(phieuxuat)); }