public bool Add(bill_payment_out model, List <details_bill_payment_out> detailsBill) { using (var dbContextTransaction = _db.Database.BeginTransaction()) { try { _db.bill_payment_out.Add(model); _db.SaveChanges(); foreach (var item in detailsBill) { item.bill_id = model.id; //_db.details_bill_payment_out.AddRange(detailsBill); } _db.details_bill_payment_out.AddRange(detailsBill); _db.SaveChanges(); dbContextTransaction.Commit(); return(true); } catch (Exception ex) { dbContextTransaction.Rollback(); return(false); } } }
public ActionResult AddBill(BillVM modelInput) { try { var model = Session["LstProduct"] as List <product>; if (model == null || model.Count == 0) { return(Json(new { status = false, msg = "Hóa đơn chưa tạo sản phẩm!" })); } var data = _customerRepository.GetCustomerById(modelInput.cutomer.user_cd); if (data == null) { return(Json(new { status = false, msg = "Chưa lựa chọn khách hàng" })); } var billPaymentOut = new bill_payment_out { customer_id = modelInput.cutomer.user_cd, customer_name = modelInput.cutomer.name, price = model.Sum(x => x.price * x.amount), create_date = DateTime.Now, create_user = "" }; var lstdetails = new List <details_bill_payment_out>(); foreach (var item in model) { var details = new details_bill_payment_out { product_id = item.id, amount = item.amount, price = item.price.ToString(), create_date = DateTime.Now, create_user = "" }; lstdetails.Add(details); } IBillPaymentOutRepository repository = new BillPaymentOutRopository(); var res = repository.Add(billPaymentOut, lstdetails); if (res) { return(Json(new { status = true })); } else { return(Json(new { status = false, msg = Constant.Msg007 })); } } catch (Exception ex) { return(Json(new { status = false, msg = ex.Message })); } }