private void edittratien(int iddmsdb) { decimal tongtien = 0; danhmucsachdaban dm = db.danhmucsachdabans.Find(iddmsdb); var ctdmsdb = dm.ctdmsdbs.Where(o => o.iddmsdb == dm.iddmsdb); dm.sotiendathanhtoan = db.ctdmsdbs.Where(ct => ct.iddmsdb == iddmsdb).Select(ct => ct.soluong * ct.sach.giaxuat).DefaultIfEmpty(0).Sum(); dm.sotienconno = 0; dm.tinhtrang = "Completed"; foreach (ctdmsdb ct in ctdmsdb) { tongtien += (decimal)(ct.soluong * ct.sach.giaxuat); } congnotheothoigian cn = new congnotheothoigian(); cn.thoidiem = (DateTime)dm.thoigian; congnotheothoigian cnht = db.congnotheothoigians.OrderByDescending(o => o.id).FirstOrDefault(o => o.iddl == dm.iddl); if (cnht.congno != null) { cn.iddl = dm.iddl; cn.congno = cnht.congno - tongtien; db.congnotheothoigians.Add(cn); } else { cn.iddl = dm.iddl; cn.congno = tongtien; db.congnotheothoigians.Add(cn); } db.SaveChanges(); }
public ActionResult Create([Bind(Include = "iddl,tendl,diachi,sodt")] daily daily) { if (ModelState.IsValid) { foreach (daily dl in db.dailies) { if (daily.tendl == dl.tendl) { ModelState.AddModelError("", "Đại lý đã tồn tại"); return(View()); } } congnotheothoigian cn = new congnotheothoigian(); cn.iddl = daily.iddl; cn.thoidiem = DateTime.Now; cn.congno = 0; db.congnotheothoigians.Add(cn); db.dailies.Add(daily); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(daily)); }
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)); }