Example #1
0
        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"));
        }