Beispiel #1
0
        public ActionResult New()
        {
            var items     = _context.Items.ToList();
            var viewModel = new SaleTransactionFormViewModel
            {
                SalesTransaction = new SalesTransaction(),
                Items            = items
            };

            return(View("SaleTransactionForm", viewModel));
        }
Beispiel #2
0
        public ActionResult Edit(int id)
        {
            var salesTransaction = _context.SalesTransactions.SingleOrDefault(s => s.Id == id);

            if (salesTransaction == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new SaleTransactionFormViewModel
            {
                SalesTransaction = salesTransaction,
                Items            = _context.Items.ToList()
            };

            return(View("SaleTransactionForm", viewModel));
        }
Beispiel #3
0
        public ActionResult Save(SaleTransactionFormViewModel sale)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new SaleTransactionFormViewModel
                {
                    Items            = _context.Items.ToList(),
                    SalesTransaction = sale.SalesTransaction
                };

                return(View("New", viewModel));
            }

            try
            {
                if (sale.SalesTransaction.Id == 0)
                {
                    sale.SalesTransaction.Date = DateTime.Now;
                    _context.SalesTransactions.Add(sale.SalesTransaction);

                    var itemInTx = _context.Items.Single(i => i.ItemId == sale.SalesTransaction.ItemId);

                    itemInTx.QuantityOnHand -= sale.SalesTransaction.Quantity;
                }
                else
                {
                    var saleInDb = _context.SalesTransactions.Single(s => s.Id == sale.SalesTransaction.Id);

                    saleInDb.Item       = sale.SalesTransaction.Item;
                    saleInDb.ItemId     = sale.SalesTransaction.ItemId;
                    saleInDb.Notes      = sale.SalesTransaction.Notes;
                    saleInDb.Quantity   = sale.SalesTransaction.Quantity;
                    saleInDb.TotalPrice = sale.SalesTransaction.TotalPrice;
                }
            }
            catch
            {
                return(View("SalesHistory", "Sales"));
            }

            _context.SaveChanges();

            return(RedirectToAction("SalesHistory", "Sales"));
        }