示例#1
0
        public ActionResult Create(FormViewModel formViewModel)
        {
            var itemLines  = formViewModel.ItemLines;
            var totalPrice = itemLines.Sum(x => Convert.ToDecimal(x.SinglePrice) * Convert.ToInt32(x.Amount));

            // Invoice to Db
            var invoice = new Invoice()
            {
                ApplicationUser    = GetCurrentUser(),
                BuyerName          = formViewModel.Invoice.BuyerName,
                DateCreated        = DateTime.Today,
                DateDue            = formViewModel.Invoice.DateDue,
                TotalPrice         = itemLines.Sum(x => Convert.ToDecimal(x.SinglePrice) * Convert.ToInt32(x.Amount)),
                TotalPriceAfterTax = Core.CalculateTax(totalPrice, formViewModel.TaxCountries[formViewModel.TaxCountriesValue].Text)
            };

            _invoiceRepository.InsertInvoice(invoice);
            _invoiceRepository.Save();


            foreach (var current in itemLines)
            {
                var itemLine = new ItemLine()
                {
                    Description = current.Description,
                    Amount      = current.Amount,
                    SinglePrice = current.SinglePrice,
                    TotalPrice  = current.SinglePrice * current.Amount,
                    Invoice     = invoice
                };

                _itemLineRepository.InsertItemLine(itemLine);
            }

            _itemLineRepository.Save();

            return(RedirectToAction("Index"));
        }