public ActionResult Edit(int?id) { Report_NXB report = db.Report_NXB.Find(id); report.status = 1; db.Entry(report).State = EntityState.Modified; //test gap Debt_NXB debt = new Debt_NXB(); Debt_NXB debt1 = db.Debt_NXB.OrderByDescending(m => m.id).FirstOrDefault(m => m.NXB_id == (int)report.NXB_id); debt.update_date = DateTime.Now; if (debt1 != null) { debt.NXB_id = (int)report.NXB_id; debt.debt = (double)(debt1.debt - report.total); debt.repay = Double.Parse(report.total.ToString()); } db.Debt_NXB.Add(debt); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult ThemChiTietPhieuBaoCao(FormCollection chitiet) { // Report_Agency report_Agency = new Report_Agency(); report_Agency = (Report_Agency)Session["PhieuBaoCao"]; var lstBookid = (from b in db.Inventory_Agency where b.Agency_id == report_Agency.Agency_id select b.Book_id).Distinct().ToList(); List <Book> lstBooks = new List <Book>(); foreach (var item in lstBookid) { Book book = db.Books.SingleOrDefault(n => n.Book_id == item); lstBooks.Add(book); } ViewBag.sach = new SelectList(lstBooks, "Book_id", "Book_name"); // ViewBag.loi = null; if (Request.Form["add"] != null) { if (ModelState.IsValid) { bool check = true; foreach (var ctpbc in (List <Detail_Report_Agency>)Session["ctphieubaocao"]) { if (ctpbc.Book_id == Int32.Parse(chitiet["sach"].ToString())) { check = false; ViewBag.loi = "Đã tồn tại sách trong phiếu"; break; } } if (check) { if (chitiet["sach"] == null) { ViewBag.loi = "Không tồn tại sách để báo cáo"; goto baoloi; } var sach = db.Books.Find(Int32.Parse(chitiet["sach"].ToString())); int soluong = Int32.Parse(chitiet["soluong"].ToString()); Report_Agency idagency = (Report_Agency)Session["PhieuBaoCao"]; Inventory_Agency tonkhodaily = db.Inventory_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => (m.Agency_id == idagency.Agency_id && m.Book_id == (int)sach.Book_id)); if (sach == null) { ViewBag.loi = "Mã sách không tồn tại"; goto baoloi; } else { if (tonkhodaily == null) { ViewBag.loi = "Không có tồn kho"; goto baoloi; } else { if (soluong > 0) { if (soluong <= (tonkhodaily.deliver_quantity - tonkhodaily.repay_quantity)) { Detail_Report_Agency ctpbc = new Detail_Report_Agency(); ctpbc.Report_id = (db.Report_Agency.Max(u => (int?)u.Report_id) != null ? db.Report_Agency.Max(u => u.Report_id) : 0) + 1; ctpbc.Book_id = Int32.Parse(chitiet["sach"].ToString()); ctpbc.quantity = Int32.Parse(chitiet["soluong"].ToString()); //((List<Int32>)Session["BookID"]).Add(ctpbc.Book_id); ((List <Detail_Report_Agency>)Session["ctphieubaocao"]).Add(ctpbc); } else { ViewBag.loi = "Vượt quá số lượng báo cáo"; goto baoloi; } } else { ViewBag.loi = "Nhập số lượng lớn hơn 0"; goto baoloi; } } } return(RedirectToAction("ThemChiTietPhieuBaoCao")); } else { } } } else if (Request.Form["create"] != null) { if (ModelState.IsValid) { if (((List <Detail_Report_Agency>)Session["ctphieubaocao"]).Count == 0) { ViewBag.loi = "Không được để phiếu trống"; goto baoloi; } double tongTien = 0; double? temptongtien = 0; Report_Agency test = new Report_Agency(); test = (Report_Agency)Session["PhieuBaoCao"]; var luu = db.Report_Agency.Add(test); db.SaveChanges(); foreach (var ctpbc in (List <Detail_Report_Agency>)Session["ctphieubaocao"]) { temptongtien += ctpbc.quantity * double.Parse(db.Books.Find(ctpbc.Book_id).Cost_Export.ToString());; tongTien = double.Parse(temptongtien.ToString()); //tongSoLuong += ctpn.SoLuongNhap; Detail_Report_Agency ctPhieuBaoCao = new Detail_Report_Agency(); ctPhieuBaoCao = ctpbc; db.Detail_Report_Agency.Add(ctPhieuBaoCao); //db.SaveChanges(); //cập nhật tồn kho đại lý Inventory_Agency tonkhodaily = new Inventory_Agency(); Inventory_Agency tonkhodaily1 = db.Inventory_Agency.OrderByDescending(n => n.id).FirstOrDefault(n => n.Book_id == (int)ctpbc.Book_id && n.Agency_id == (int)ctpbc.Report_Agency.Agency_id); tonkhodaily.UpdatedDate = DateTime.Now; if (tonkhodaily1 != null) { if (tonkhodaily1.repay_quantity == null) { tonkhodaily.Book_id = ctpbc.Book_id; tonkhodaily.Agency_id = (int)luu.Agency_id; tonkhodaily.repay_quantity = ctpbc.quantity; tonkhodaily.deliver_quantity = tonkhodaily1.deliver_quantity; } else { tonkhodaily.Book_id = ctpbc.Book_id; tonkhodaily.Agency_id = (int)luu.Agency_id; tonkhodaily.repay_quantity = tonkhodaily1.repay_quantity + ctpbc.quantity; tonkhodaily.deliver_quantity = tonkhodaily1.deliver_quantity; } } //else //{ // tonkhodaily.Book_id = ctpbc.Book_id; // tonkhodaily.Agency_id = (int)luu.Agency_id; // tonkhodaily.repay_quantity = ctpbc.quantity; // tonkhodaily.deliver_quantity = ctpbc.quantity; //} db.Inventory_Agency.Add(tonkhodaily); //db.SaveChanges(); //cập nhật báo cáo đến NXB int idNXB = int.Parse(db.Books.Find(ctpbc.Book_id).NXB_id.ToString()); Report_NXB a = db.Report_NXB.FirstOrDefault(m => m.status == 0 && m.NXB_id == idNXB); if (a == null) { Report_NXB reportNXB = new Report_NXB(); reportNXB.NXB_id = idNXB; reportNXB.update_date = DateTime.Now; reportNXB.status = 0; reportNXB.total = ctpbc.quantity * double.Parse(db.Books.Find(ctpbc.Book_id).Cost_Import.ToString()); db.Report_NXB.Add(reportNXB); db.SaveChanges(); } else { a.total += ctpbc.quantity * double.Parse(db.Books.Find(ctpbc.Book_id).Cost_Import.ToString()); a.update_date = DateTime.Now; db.Report_NXB.Attach(a); db.Entry(a).State = EntityState.Modified; db.SaveChanges(); } } //Lưu phiếu - tính tổng tiền phiếu //addedPhieuNhap.TongSoLuong = tongSoLuong; luu.Total = tongTien; db.Report_Agency.Attach(luu); db.Entry(luu).State = EntityState.Modified; //cập nhật công nợ đại lý Debt_Agency debt = new Debt_Agency(); Debt_Agency debt1 = db.Debt_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => m.Agency_id == (int)luu.Agency_id); debt.update_date = DateTime.Now; if (debt1 != null) { debt.Agency_id = (int)luu.Agency_id; debt.debt = (double)(debt1.debt - luu.Total); debt.repay = Double.Parse(luu.Total.ToString()); } //else //{ // debt.Agency_id = (int)luu.Agency_id; // debt.debt = (double)luu.Total; //} db.Debt_Agency.Add(debt); db.SaveChanges(); Session["ctphieubaocao"] = null; Session["PhieuBaoCao"] = null; return(RedirectToAction("Create")); } } baoloi: return(View()); }