public async Task <IActionResult> CreateInvoce(InvoceModel model) { // Tach tao hoa don ra rieng de nhin va de tranh loi null Hoadon HDtoADD = HoadonCreator(model); List <Chitiethd> chitiethds = new List <Chitiethd>(); // Lay danh sach san pham trong cart var cartLst = await _context.Carts.Include(sp => sp.Sp).ToListAsync(); foreach (Carts item in cartLst) { // Tao chi tiet hoa don vs san pham vaf thong tin trong cart Chitiethd chitiet = new Chitiethd() { IdHd = HDtoADD.Idhoadon, IdSp = item.SpId, GiaSp = item.Sp.GiaSp, Soluong = item.Quantity.Value.ToString(), }; chitiethds.Add(chitiet); } _context.Carts.RemoveRange(cartLst); _context.Hoadon.Add(HDtoADD); _context.Chitiethd.AddRange(chitiethds); await _context.SaveChangesAsync(); ViewData["Message"] = "Đã lưu thông tin Hóa Đơn, mã hóa đơn của bạn là " + HDtoADD.Idhoadon.ToString(); return(RedirectToAction("Index", "HoaDon")); }
private static InvoceModel MappingForm(RecognizedForm form) { var result = new InvoceModel() { CompanyPhone = form.Fields.Where(f => f.Key == "CompanyPhone").FirstOrDefault().Value?.ValueData?.Text, Date = form.Fields.Where(f => f.Key == "DatePurchase").FirstOrDefault().Value?.ValueData?.Text, Email = form.Fields.Where(f => f.Key == "Mail").FirstOrDefault().Value?.ValueData?.Text, PurchaseOrder = form.Fields.Where(f => f.Key == "PurchaseOrder").FirstOrDefault().Value?.ValueData?.Text, ShippedFromCompany = form.Fields.Where(f => f.Key == "ShippedFromCompany").FirstOrDefault().Value?.ValueData?.Text, ShippedtoCompany = form.Fields.Where(f => f.Key == "ShippedToCompany").FirstOrDefault().Value?.ValueData?.Text, ShippedToVendor = form.Fields.Where(f => f.Key == "ShippedToVendor").FirstOrDefault().Value?.ValueData?.Text, Subtotal = form.Fields.Where(f => f.Key == "Subtotal").FirstOrDefault().Value?.ValueData?.Text, Tax = form.Fields.Where(f => f.Key == "Tax").FirstOrDefault().Value?.ValueData?.Text, Total = form.Fields.Where(f => f.Key == "Total").FirstOrDefault().Value?.ValueData?.Text, WebSite = form.Fields.Where(f => f.Key == "WebSite").FirstOrDefault().Value?.ValueData?.Text }; return(result); }
//Method to create an Invoke Hoadon HoadonCreator(InvoceModel model) { Hoadon hoadon; // If this is the first one in db if (_context.Hoadon.Count() <= 0) { hoadon = new Hoadon() { Idhoadon = 1000, IdNguoimua = int.Parse(this.User.FindFirst(ClaimTypes.NameIdentifier).Value), Nguoinhan = model.tenNguoiNhan, TongTien = model.toltal, SoLuong = model.Quantity, Ghichu = model.GhiChu, NgayTao = DateTime.Now, Diachi = model.DiaChi, TinhTrang = "Đang Xử Lý", }; } else { // else just add up the value int idToAdd = _context.Hoadon.Last().Idhoadon + 1; hoadon = new Hoadon() { Idhoadon = idToAdd, IdNguoimua = int.Parse(this.User.FindFirst(ClaimTypes.NameIdentifier).Value), Nguoinhan = model.tenNguoiNhan, TongTien = model.toltal, SoLuong = model.Quantity, Ghichu = model.GhiChu, Diachi = model.DiaChi, NgayTao = DateTime.Now, TinhTrang = "Đang Xử Lý", }; } return(hoadon); }