コード例 #1
0
        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"));
        }
コード例 #2
0
        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"));
        }
コード例 #3
0
        public ActionResult CreateDetails()
        {
            int agencyID, invoiceID;

            if (Session["agencyID"] != null && Session["invoiceID"] != null)
            {
                agencyID  = Convert.ToInt32(Session["agencyID"]);
                invoiceID = Convert.ToInt32(Session["invoiceID"]);

                ViewBag.invoiceInfo = db.Invoices.Include(s => s.Agency)
                                      .SingleOrDefault(x => x.ID == invoiceID);


                // show books from chosen publisher
                var books = db.Books.ToList();

                ViewBag.books = new SelectList(books, "ID", "Name");

                List <InvoiceDetail> invoiceDetails = new List <InvoiceDetail>();
                int total = 0;

                AgencyDebt agencyDebt = db.AgencyDebts.Find(agencyID);
                invoiceDetails = db.InvoiceDetails.Where(s => s.InvoiceID == invoiceID).ToList();
                foreach (var item in invoiceDetails)
                {
                    total += item.Quantity * item.UnitPrice;
                }

                ViewBag.agencyDebt = agencyDebt.Amount;

                // 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 invoiceDetail = db.InvoiceDetails
                                        .FirstOrDefault(s => s.InvoiceID == invoiceID &&
                                                        s.BookID == bookID);

                    Book  book  = db.Books.Find(bookID);
                    Stock stock = db.Stocks.Where(s => s.BookID == bookID)
                                  .OrderByDescending(s => s.Date)
                                  .FirstOrDefault();

                    if ((invoiceDetail == null && stock.Quantity > 0) ||
                        (invoiceDetail != null && stock.Quantity >= invoiceDetail.Quantity)
                        )
                    {
                        // check if agency debt is greater than total of creating invoice
                        if (agencyDebt.Amount > total)
                        {
                            if (invoiceDetail != null)
                            {
                                // if chosen book already exists, increase its quantity by one
                                invoiceDetail.Quantity++;
                                db.SaveChanges();
                            }
                            else
                            {
                                // if chosen book not exists, add new
                                InvoiceDetail a = new InvoiceDetail
                                {
                                    InvoiceID = invoiceID,
                                    BookID    = bookID,
                                    Quantity  = 1,
                                    UnitPrice = book.SellingPrice
                                };
                                db.InvoiceDetails.Add(a);

                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            ViewBag.error     = "Tổng tiền vượt quá số tiền còn nợ! ";
                            ViewBag.errorInfo = agencyDebt.Amount.ToString();
                        }
                    }
                    else
                    {
                        ViewBag.error     = "Không đủ sách! ";
                        ViewBag.errorInfo = stock.Quantity.ToString();
                    }
                }
                // 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
                    {
                        invoiceDetails = db.InvoiceDetails
                                         .Where(s => s.InvoiceID == invoiceID)
                                         .ToList();

                        for (int i = 0; i < invoiceDetails.Count; i++)
                        {
                            int bookID   = Convert.ToInt32(Request.Form["invoiceDetail_" + i]);
                            int quantity = Convert.ToInt32(Request.Form["quantity_" + i]);

                            Book book = db.Books.Find(bookID);

                            if (quantity > 0)
                            {
                                Stock stock = db.Stocks.Where(s => s.BookID == bookID)
                                              .OrderByDescending(s => s.Date).FirstOrDefault();
                                if (stock.Quantity >= quantity)
                                {
                                    if (agencyDebt.Amount > (total + quantity * book.SellingPrice))
                                    {
                                        InvoiceDetail a = invoiceDetails.Where(s => s.BookID == bookID).FirstOrDefault();

                                        a.Quantity = quantity;

                                        db.SaveChanges();
                                    }
                                    else
                                    {
                                        ViewBag.error     = "Tổng tiền vượt quá số tiền còn nợ! ";
                                        ViewBag.errorInfo = agencyDebt.Amount.ToString();
                                    }
                                }
                                else
                                {
                                    ViewBag.error     = "Không đủ sách ";
                                    ViewBag.errorInfo = stock.Quantity.ToString();
                                }
                            }
                        }
                    }
                }

                invoiceDetails = db.InvoiceDetails
                                 .Where(s => s.InvoiceID == invoiceID)
                                 .Include(s => s.Book)
                                 .ToList();

                // re-count total
                total = 0;

                foreach (var item in invoiceDetails)
                {
                    total += item.Quantity * item.UnitPrice;
                }

                Invoice invoice = db.Invoices.Find(invoiceID);
                invoice.Total = total;

                db.SaveChanges();

                return(View(invoiceDetails));
            }

            return(RedirectToAction("Index"));
        }
コード例 #4
0
        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));
        }