public ActionResult Save(int invoiceID, int agencyID) { int totalAgencyDebt = 0; List <InvoiceDetail> invoiceDetails = db.InvoiceDetails .Where(s => s.InvoiceID == invoiceID).ToList(); // update stock foreach (InvoiceDetail i in invoiceDetails) { Stock stock = db.Stocks .Where(s => s.BookID == i.BookID) .OrderByDescending(s => s.Date) .FirstOrDefault(); stock.Quantity -= i.Quantity; stock.Date = DateTime.Now; db.Stocks.Add(stock); db.SaveChanges(); totalAgencyDebt += (i.Quantity * i.UnitPrice); } // update agency book debt foreach (InvoiceDetail i in invoiceDetails) { AgencyBookDebt agencyBookDebt = db.AgencyBookDebts .Where(s => s.BookID == i.BookID && s.AgencyID == agencyID) .FirstOrDefault(); agencyBookDebt.Quantity += i.Quantity; db.SaveChanges(); } // update debt AgencyDebt agencyDebt = db.AgencyDebts .Where(s => s.AgencyID == agencyID) .OrderByDescending(s => s.Date) .First(); agencyDebt.Amount += totalAgencyDebt; agencyDebt.Date = DateTime.Now; db.AgencyDebts.Add(agencyDebt); db.SaveChanges(); Session.Clear(); return(RedirectToAction("Index")); }
public ActionResult Save(int reportID, int agencyID) { int total = 0; List <AgencyReportDetail> agencyReportDetails = db.AgencyReportDetails .Where(s => s.AgencyReportID == reportID).ToList(); // update angency book debt foreach (AgencyReportDetail i in agencyReportDetails) { AgencyBookDebt agencyBookDebt = db.AgencyBookDebts .Where(s => s.BookID == i.BookID && s.AgencyID == agencyID) .FirstOrDefault(); agencyBookDebt.Quantity -= i.Quantity; db.SaveChanges(); total += (i.Quantity * i.UnitPrice); } // update angency debt AgencyDebt agencyDebt = db.AgencyDebts .Where(s => s.AgencyID == agencyID) .OrderByDescending(s => s.Date) .First(); agencyDebt.Amount -= total; agencyDebt.Date = DateTime.Now; db.AgencyDebts.Add(agencyDebt); db.SaveChanges(); Session.Clear(); return(RedirectToAction("Index")); }
public ActionResult CreateReportDetails() { int agencyID, agencyReportID; if (Session["agencyID"] != null && Session["agencyReportID"] != null) { agencyID = Convert.ToInt32(Session["agencyID"]); agencyReportID = Convert.ToInt32(Session["agencyReportID"]); ViewBag.agencyReportInfo = db.AgencyReports.Include(s => s.Agency) .SingleOrDefault(x => x.ID == agencyReportID); // show books agency has received var agencyBookDebt = db.AgencyBookDebts.Include(s => s.Book) .Where(s => s.AgencyID == agencyID) .ToList(); List <Book> resultBooks = new List <Book>(); foreach (var item in agencyBookDebt) { resultBooks.Add(item.Book); } ViewBag.agencyBooks = new SelectList(resultBooks, "ID", "Name"); List <AgencyReportDetail> agencyReportDetails = new List <AgencyReportDetail>(); // if user has chosen a book from books list if (!string.IsNullOrWhiteSpace(Request.Form["bookID"])) { int bookID; Int32.TryParse(Request.Form["bookID"].ToString(), out bookID); var agencyReportDetail = db.AgencyReportDetails .FirstOrDefault(s => s.AgencyReportID == agencyReportID && s.BookID == bookID); AgencyBookDebt bookDebt = db.AgencyBookDebts .Where(s => s.BookID == bookID && s.AgencyID == agencyID) .FirstOrDefault(); int debtQuantity = bookDebt.Quantity; if (agencyReportDetail != null) { // if chosen book already exists, increase its quantity by one if ((agencyReportDetail.Quantity + 1) <= debtQuantity) { agencyReportDetail.Quantity++; db.SaveChanges(); } else { ViewBag.quantityError = "Số lượng báo cáo vượt quá số lượng còn nợ!" + " Số nợ: " + debtQuantity + " cuốn"; } } else { if (debtQuantity != 0) { // if chosen book not exists, add new Book book = db.Books.Find(bookID); AgencyReportDetail a = new AgencyReportDetail { AgencyReportID = agencyReportID, BookID = bookID, Quantity = 1, UnitPrice = book.SellingPrice }; db.AgencyReportDetails.Add(a); db.SaveChanges(); } else { ViewBag.quantityError = "Số lượng báo cáo vượt quá số lượng còn nợ." + " Số nợ: " + 0 + " cuốn)"; } } } // if user doesn't choose a book else { if (Request.Form["justCreated"] != null && Request.Form["justCreated"] == "0") // if this is not a newly created report with no details { agencyReportDetails = db.AgencyReportDetails .Where(s => s.AgencyReportID == agencyReportID) .ToList(); for (int i = 0; i < agencyReportDetails.Count; i++) { int bookID = Convert.ToInt32(Request.Form["agencyReportDetail_" + i]); int quantity = Convert.ToInt32(Request.Form["quantity_" + i]); if (quantity > 0) { AgencyBookDebt bookDebt = db.AgencyBookDebts .Where(s => s.BookID == bookID && s.AgencyID == agencyID) .FirstOrDefault(); int debtQuantity = bookDebt.Quantity; if (quantity <= debtQuantity) { AgencyReportDetail a = agencyReportDetails.Where(s => s.BookID == bookID).FirstOrDefault(); a.Quantity = quantity; db.SaveChanges(); } else { ViewBag.quantityError = "Số lượng báo cáo vượt quá số lượng còn nợ" + " Số nợ: " + debtQuantity + " cuốn"; } } } } } agencyReportDetails = db.AgencyReportDetails .Where(s => s.AgencyReportID == agencyReportID) .Include(s => s.Book) .ToList(); int total = 0; foreach (var item in agencyReportDetails) { total += item.Quantity * item.UnitPrice; } AgencyReport agencyReport = db.AgencyReports.Find(agencyReportID); agencyReport.Total = total; db.SaveChanges(); return(View(agencyReportDetails)); } return(RedirectToAction("Index")); }
public ActionResult Index(int publisherID) { ViewBag.publishers = new SelectList(db.Publishers, "ID", "Name"); // borrow AgencyBookDebt model to store books debt List <AgencyBookDebt> bookDebts = new List <AgencyBookDebt>(); if (!string.IsNullOrWhiteSpace(Request.Form["publisherID"])) { publisherID = Convert.ToInt32(Request.Form["publisherID"]); DateTime filterDate = DateTime.Now; if (!string.IsNullOrWhiteSpace(Request.Form["date"])) { var tempDate = Request.Form["date"].ToString(); TimeSpan time = new TimeSpan(23, 59, 59); filterDate = DateTime.Parse(tempDate).Add(time); } int totalDebt = 0; int totalAgencyPay = 0; List <Receipt> receipts = db.Receipts .Where(s => DbFunctions.TruncateTime(s.Date) <= filterDate && s.PublisherID == publisherID ) .Include(s => s.ReceiptDetails) .ToList(); List <AgencyReport> agencyReports = db.AgencyReports .Where(s => DbFunctions.TruncateTime(s.Date) <= filterDate) .Include(s => s.AgencyReportDetails) .ToList(); foreach (var x in receipts) { totalDebt += x.Total; List <ReceiptDetail> receiptDetails = db.ReceiptDetails .Where(s => s.ReceiptID == x.ID) .Include(s => s.Book) .ToList(); //return Json(receiptDetails, JsonRequestBehavior.AllowGet); foreach (ReceiptDetail y in receiptDetails) { var bookDebt = bookDebts.Find(s => s.BookID == y.Book.ID); if (bookDebt != null) { // if book exists, increase quantity bookDebts.Find(s => s.BookID == y.Book.ID).Quantity += y.Quantity; } else { AgencyBookDebt a = new AgencyBookDebt() { BookID = y.BookID, Quantity = y.Quantity, Book = y.Book }; bookDebts.Add(a); } } } foreach (AgencyReport x in agencyReports) { foreach (AgencyReportDetail y in x.AgencyReportDetails) { if (y.Book.PublisherID == publisherID) { totalAgencyPay += (y.Quantity * y.UnitPrice); } } } ViewBag.totalDebt = totalDebt; ViewBag.totalAgencyPay = totalAgencyPay; ViewBag.date = filterDate; } return(View(bookDebts)); }
public ActionResult Index(int agencyID) { ViewBag.agencies = new SelectList(db.Agencies, "ID", "Name"); List <AgencyBookDebt> agencyBookDebts = new List <AgencyBookDebt>(); if (!string.IsNullOrWhiteSpace(Request.Form["agencyID"])) { agencyID = Convert.ToInt32(Request.Form["agencyID"]); if (!string.IsNullOrWhiteSpace(Request.Form["date"])) { var tempDate = Request.Form["date"].ToString(); TimeSpan time = new TimeSpan(23, 59, 59); DateTime date = DateTime.Parse(tempDate).Add(time); // get agency's total debt on specific date AgencyDebt agencyDebt = db.AgencyDebts .Where(s => s.AgencyID == agencyID && DbFunctions.TruncateTime(s.Date) <= date) .OrderByDescending(s => s.Date) .FirstOrDefault(); ViewBag.total = agencyDebt.Amount; // get all agency's invoices from start to specific date List <Invoice> invoices = db.Invoices .Where(s => s.AgencyID == agencyID && DbFunctions.TruncateTime(s.Date) <= date) .Include(s => s.InvoiceDetails) .ToList(); foreach (var x in invoices) { List <InvoiceDetail> invoiceDetails = db.InvoiceDetails .Where(s => s.InvoiceID == x.ID) .Include(s => s.Book) .ToList(); foreach (var y in invoiceDetails) { var agencyBookDebt = agencyBookDebts.Find(s => s.BookID == y.BookID); if (agencyBookDebt != null) { // if book exists, increase quantity agencyBookDebts.Find(s => s.BookID == y.BookID).Quantity += y.Quantity; } else { AgencyBookDebt a = new AgencyBookDebt() { BookID = y.BookID, Quantity = y.Quantity, Book = y.Book }; agencyBookDebts.Add(a); } } } // get all agency's reports from start to specific date List <AgencyReport> agencyReports = db.AgencyReports .Where(s => DbFunctions.TruncateTime(s.Date) <= date) .ToList(); foreach (var x in agencyReports) { List <AgencyReportDetail> agencyReportDetails = db.AgencyReportDetails .Where(s => s.AgencyReportID == x.ID) .Include(s => s.Book) .ToList(); foreach (var y in agencyReportDetails) { foreach (var z in agencyBookDebts) { if (z.BookID == y.BookID) { z.Quantity -= y.Quantity; } } } } ViewBag.date = date; } else { agencyBookDebts = db.AgencyBookDebts .Where(s => s.AgencyID == agencyID) .Include(s => s.Book) .ToList(); int total = 0; foreach (var i in agencyBookDebts) { total += (i.Quantity * i.Book.SellingPrice); } ViewBag.total = total; ViewBag.date = "Hôm nay"; } } return(View(agencyBookDebts)); }