예제 #1
0
        public async Task <IActionResult> AddInvoiceDetail(InvoiceDetailViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (!(await productService.ReadAsync(model.Product.Value) != null) || model.Amount.Value < 1 || model.Amount.Value > 50)
                {
                    return(RedirectToAction("InvoiceDetail", new { id = model.InvoiceId.Value }).WithDanger("Lỗi", "Thêm chi tiết thất bại"));
                }
                model.Id = Guid.NewGuid();

                model.CreatedDate  = DateTime.Now;
                model.ModifiedDate = DateTime.Now;

                model.UnitPrice  = (await productService.ReadAsync(model.Product.Value)).UnitPrice;
                model.TotalPrice = model.UnitPrice * model.Amount;

                var currentInvoice = await invoiceService.ReadAsync(model.InvoiceId.Value);

                currentInvoice.TotalPrice += model.UnitPrice;
                await invoiceDetailService.CreateAsync(model);



                return(RedirectToAction("InvoiceDetail", new { id = currentInvoice.Id }).WithSuccess("Thành công", "Thêm chi tiết hóa đơn thành công"));
            }

            ViewBag.Product   = productService.GetAll();
            ViewBag.InvoiceId = model.InvoiceId.Value;
            return(RedirectToAction("AddInvoiceDetail", new { id = model.InvoiceId.Value }).WithDanger("Lỗi", "Thêm chi tiết hóa đơn thất bại"));
        }