public ActionResult _CreateList(List <OrderDetailViewModel> detail = null) { if (detail == null) { detail = new List <OrderDetailViewModel>(); } EndInventoryRepository EndRepo = new EndInventoryRepository(_context); foreach (var item in detail) { item.EndInventoryQty = Convert.ToInt32(EndRepo.GetQty(item.ProductId.Value)); } return(PartialView(detail)); }
public ActionResult GetValueSpecifications(int SelectedProductid) { EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context); decimal tempt = EndInventoryRepo.GetQty(SelectedProductid); var Specifications = _context.ProductModel .Where(p => p.ProductId == SelectedProductid) .Select(p => p.Specifications) .FirstOrDefault(); var result = new { EndInventoryQty = tempt, Specifications = Specifications }; return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult _CreateDetailListInnerCategory(int id) { EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context); var detail = (from p in _context.ProductModel where p.CategoryId == id select new WarehouseInventoryDetailViewModel() { ProductId = p.ProductId, ProductName = p.ProductCode + " | " + p.ProductName + " | " + p.Specifications, ProductCode = p.ProductCode, Specifications = p.Specifications, AmountDifference = 0 }).ToList(); //Cập nhật tồn hệ thống foreach (var item in detail) { int ProductId = item.ProductId ?? default(int); item.EndInventoryQty = EndInventoryRepo.GetQty(ProductId); item.ActualInventory = item.EndInventoryQty; } return(PartialView(detail)); }
public ActionResult Save(ReturnMasterModel model, List <ReturnDetailViewModel> detail, decimal?GuestAmountPaid, int CreateReceipt = 1) { if (ModelState.IsValid) { try { using (TransactionScope ts = new TransactionScope()) { var currentTime = DateTime.Now; #region Lưu ReturnMaster model.CreatedDate = currentTime; model.CreatedAccount = currentAccount.UserName; AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault(); model.CreatedEmployeeId = Account.EmployeeId; model.InventoryTypeId = EnumInventoryType.XC; //Xuất - Trả hàng cho nhà cung cấp model.Paid = GuestAmountPaid.HasValue ? GuestAmountPaid : 0; model.ReturnMasterCode = GetReturnCode(); model.Actived = true; #endregion var importModel = _context.ImportMasterModel.Where(p => p.ImportMasterId == model.ImportMasterId).FirstOrDefault(); #region Tính số dư còn lại decimal?SuplierOldDebt = _context.AM_DebtModel .Where(p => p.SupplierId == importModel.SupplierId) .OrderByDescending(p => p.TimeOfDebt) .Select(p => p.RemainingAmountAccrued) .FirstOrDefault(); SuplierOldDebt = (SuplierOldDebt == null) ? 0 : SuplierOldDebt.Value; model.RemainingAmount = (model.RemainingAmount == null) ? 0 : model.RemainingAmount.Value; model.RemainingAmountAccrued = SuplierOldDebt.Value - model.RemainingAmount.Value; _context.Entry(model).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); // LƯU TẠM ĐỂ LẤY ReturnMASTERID (SẼ BỊ SCROLLBACK KHI XẢY RA LỖI) #endregion if (CreateReceipt == 1) { #region Thêm vào giao dịch kế toán AM_TransactionModel AMmodel; #region TH1 : nhận đủ if (model.TotalPrice == GuestAmountPaid) { AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.TM && p.AMAccountTypeCode == EnumAM_AccountType.TIENMAT && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.NXXUAT, ContactItemTypeCode = EnumContactType.NCC, CustomerId = null, SupplierId = model.SupplierId, EmployeeId = null, OtherId = null, Amount = GuestAmountPaid, OrderId = null, ImportMasterId = model.ImportMasterId, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion #region TH2 : Không nhận lưu vào công nợ else if (GuestAmountPaid == 0 || GuestAmountPaid == null) { AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.PTNCC && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.NXXUAT, ContactItemTypeCode = EnumContactType.NCC, CustomerId = null, SupplierId = model.SupplierId, EmployeeId = null, OtherId = null, Amount = model.TotalPrice, OrderId = null, ImportMasterId = model.ImportMasterId, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion #region TH3 : nhận 1 phần else { #region 1 phần (Tiền mặt hoặc chuyển khoản) AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.TM && p.AMAccountTypeCode == EnumAM_AccountType.TIENMAT && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.NXXUAT, ContactItemTypeCode = EnumContactType.NCC, CustomerId = null, SupplierId = model.SupplierId, EmployeeId = null, OtherId = null, Amount = GuestAmountPaid, OrderId = null, ImportMasterId = model.ImportMasterId, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion #region 1 phần đưa vào công nợ AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.PTNCC && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.NXXUAT, ContactItemTypeCode = EnumContactType.NCC, CustomerId = null, SupplierId = model.SupplierId, EmployeeId = null, OtherId = null, Amount = model.TotalPrice - GuestAmountPaid, OrderId = null, ImportMasterId = model.ImportMasterId, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion } #endregion #endregion } #region Thêm AM_DebtModel (Số nợ còn lại) if (model.RemainingAmount > 0) { var AMDebModel = new AM_DebtModel() { SupplierId = importModel.SupplierId, TimeOfDebt = currentTime, RemainingAmountAccrued = model.RemainingAmountAccrued, ReturnMasterId = model.ReturnMasterId, TransactionTypeCode = EnumTransactionType.NXXUAT }; _context.Entry(AMDebModel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion #region Lưu InventoryMaster InventoryMasterModel InvenMaster = new InventoryMasterModel(); InvenMaster.WarehouseModelId = model.WarehouseId; InvenMaster.InventoryCode = model.ReturnMasterCode; InvenMaster.InventoryTypeId = EnumInventoryType.XC; //Xuất - Trả hàng cho nhà cung cấp InvenMaster.CreatedDate = model.CreatedDate; InvenMaster.CreatedAccount = model.CreatedAccount; InvenMaster.CreatedEmployeeId = model.CreatedEmployeeId; InvenMaster.Actived = true; InvenMaster.BusinessId = model.ReturnMasterId; // Id nghiệp vụ InvenMaster.BusinessName = "ReturnMasterModel"; // Tên bảng nghiệp vụ InvenMaster.ActionUrl = "/ReturnMaster/Details/"; // Đường dẫn ( cộng ID cho truy xuất) InvenMaster.StoreId = model.StoreId; _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); // insert tạm để lấy InvenMasterID #endregion #region duyệt list lưu ReturnDetail và InvenrotyDetail decimal TotalQty = 0; foreach (var item in detail) { if (item.ReturnQty > 0) // Chỉ tính Số lượng trả > 0 { TotalQty += item.ReturnQty.Value; #region Lưu ReturnDetailModel //// Lấy ReturnedQty mới nhất //var tempLanDaTraCuoi = (from detal in _context.ReturnDetailModel // join master in _context.ReturnMasterModel on detal.ReturnMasterId equals master.ReturnMasterId // orderby detal.ReturnDetailId descending // where master.Actived == true && detal.ProductId == item.ProductId // select new // { // LanDaTraCuoi = detal.ReturnedQty.Value // }).FirstOrDefault(); decimal CogsInOd = _context.ImportDetailModel.Where(p => p.ImportMasterId == model.ImportMasterId && p.ProductId == item.ProductId).Select(p => p.UnitCOGS.Value).FirstOrDefault(); item.UnitCOGS = CogsInOd; ReturnDetailModel detailmodel = new ReturnDetailModel() { ReturnMasterId = model.ReturnMasterId, ProductId = item.ProductId, ImportQty = item.ImportQty, //ReturnedQty = (tempLanDaTraCuoi == null ? 0 + (item.ReturnQty.HasValue ? item.ReturnQty : 0) : tempLanDaTraCuoi.LanDaTraCuoi + (item.ReturnQty.HasValue ? item.ReturnQty : 0)), ReturnedQty = item.ReturnedQty, InventoryQty = item.InventoryQty, ReturnQty = item.ReturnQty.HasValue ? item.ReturnQty : 0, Price = item.Price, UnitShippingWeight = item.UnitShippingWeight, UnitPrice = item.UnitPrice, Note = item.Note, ShippingFee = item.ShippingFee, UnitCOGS = item.UnitCOGS }; //_context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added; model.ReturnDetailModel.Add(detailmodel); _context.SaveChanges(); #endregion #region Lưu InventoryDetail //var temp = _context.InventoryDetailModel.OrderByDescending(p => p.InventoryDetailId).Where(p => p.ProductId == item.ProductId).Select(p => p.EndInventoryQty).FirstOrDefault(); //var temp = (from detal in _context.InventoryDetailModel // join master in _context.InventoryMasterModel on detal.InventoryMasterId equals master.InventoryMasterId // orderby detal.InventoryDetailId descending // where master.Actived == true && detal.ProductId == item.ProductId // select new // { // TonCuoi = detal.EndInventoryQty.Value // }).FirstOrDefault(); //decimal tondau; //if (temp != null) //{ // tondau = Convert.ToInt32(temp.TonCuoi); //} //else //{ // tondau = 0; //} EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context); decimal tondau = EndInventoryRepo.GetQty(item.ProductId.Value); var tempt2 = _context.ProductModel.Where(p => p.ProductId == item.ProductId).FirstOrDefault(); decimal GiaVon = tempt2.COGS.HasValue ? tempt2.COGS.Value : 0; InventoryDetailModel InvenDetail = new InventoryDetailModel() { InventoryMasterId = InvenMaster.InventoryMasterId, ProductId = item.ProductId, BeginInventoryQty = tondau, //COGS = GiaVon, Price = item.Price, //ImportQty = 0, ExportQty = item.ReturnQty.HasValue ? item.ReturnQty : 0,// Xuất //UnitCOGS = GiaVon * (item.ReturnQty.HasValue ? item.ReturnQty : 0), UnitPrice = item.UnitPrice.HasValue ? item.UnitPrice : 0, EndInventoryQty = tondau - (item.ReturnQty.HasValue ? item.ReturnQty : 0) }; _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added; // _context.SaveChanges(); } #endregion } #endregion // Cập nhật lại Tổng giá vốn model.SumCOGSOfOrderDetail = detail.Where(p => p.ReturnQty > 0).Sum(p => p.UnitCOGS * p.ReturnQty); model.TotalQty = TotalQty; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); ts.Complete(); // hoàn tất và thực sự lưu vào db return(Json("success", JsonRequestBehavior.AllowGet)); } } catch { return(Json(Resources.LanguageResource.AddErrorMessage, JsonRequestBehavior.AllowGet)); } } else { return(Json(Resources.LanguageResource.AddErrorMessage, JsonRequestBehavior.AllowGet)); } }
public ActionResult UpdateOrderPay(int OrderId) { try { var model = _context.Daily_ChicCut_OrderModel.Where(p => p.OrderId == OrderId).FirstOrDefault(); if (model == null) { return(Content("Không tìm thấy đơn hàng yêu cầu!")); } else { using (TransactionScope ts = new TransactionScope()) { var currentTime = DateTime.Now; List <Daily_ChicCut_OrderDetailModel> detail = model.Daily_ChicCut_OrderDetailModel.ToList(); #region // Insert InventoryMaster InventoryMasterModel InvenMaster = new InventoryMasterModel(); InvenMaster.WarehouseModelId = 1; //Kho 489 Hồng Bàng InvenMaster.InventoryTypeId = EnumInventoryType.XB; // xuất bán hàng InvenMaster.InventoryCode = string.Format("{0}-{1}", ConstantInventoryCode.MaDonHangTaoMoi, model.OrderId); InvenMaster.CreatedDate = currentTime; InvenMaster.CreatedAccount = currentAccount.UserName; InvenMaster.CreatedEmployeeId = currentEmployee.EmployeeId; InvenMaster.StoreId = 1000; //Chic Cut Salon InvenMaster.Actived = true; InvenMaster.BusinessId = model.OrderId; // Id nghiệp vụ InvenMaster.BusinessName = "Daily_ChicCut_OrderModel"; // Tên bảng nghiệp vụ InvenMaster.ActionUrl = "/Daily_ChicCut_OrderModel/Details/"; // Đường dẫn ( cộng ID cho truy xuất) _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); // insert tạm để lấy InvenMasterID #endregion #region /* Chi tiết định lượng : Qty = QDetail.Qty * Od.Qty, Price = pdprice.Price * Od.Qty*/ var QDetailList = ( from Od in detail join QMaster in _context.Master_ChicCut_QuantificationMasterModel on Od.ServiceId equals QMaster.ServiceId join QDetail in _context.Master_ChicCut_QuantificationDetailModel on QMaster.QuantificationMasterId equals QDetail.QuantificationMasterId join pd in _context.ProductModel on QDetail.ProductId equals pd.ProductId //join pdprice in _context.ProductPriceModel on pd.ProductId equals pdprice.ProductId where detail.Select(p => p.ServiceId).ToList().Contains(QMaster.ServiceId) select new Master_ChicCut_QuantificationDetailViewModel() { ProductId = QDetail.ProductId, Qty = QDetail.Qty * Od.Qty, Price = 0 //pdprice.Price * Od.Qty, }).ToList(); if (QDetailList != null && QDetailList.Count > 0) { foreach (var item in QDetailList) { EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context); decimal TonCuoiTrongHeThong = EndInventoryRepo.GetQty(item.ProductId.Value); InventoryDetailModel InvenDetail = new InventoryDetailModel() { InventoryMasterId = InvenMaster.InventoryMasterId, ProductId = item.ProductId, BeginInventoryQty = TonCuoiTrongHeThong, //COGS = 0, Price = item.Price, //ImportQty = 0, // số lượng nhập = 0 ExportQty = item.Qty, //UnitCOGS = 0, //Tổng giá vốn = [COGS] * [ImportQty] = 0 UnitPrice = item.Qty * item.Price, //[ExportQty] *[Price] EndInventoryQty = TonCuoiTrongHeThong + 0 - item.Qty //Tồn cuối = [BeginInventoryQty] + [ImportQty] - [ExportQty] }; _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added; } } #endregion model.OrderStatusId = EnumDaily_ChicCut_OrderStatus.DaTinhTien; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); ts.Complete(); return(Content("success")); } } } catch { return(Content("Xảy ra lỗi trong quá trình cập nhật trạng thái")); } }
public ActionResult Save(PreOrderMasterModel model, List <OrderDetailViewModel> detail, decimal?GuestAmountPaid, decimal?TotalBillDiscount, decimal?TotalVAT, int CreateReceipt = 1) { try { if (ModelState.IsValid) { using (TransactionScope ts = new TransactionScope()) { model.CreatedDate = DateTime.Now; model.CreatedAccount = currentAccount.UserName; model.OrderStatusId = EnumOrderStatus.KhoiTao; AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault(); model.CreatedEmployeeId = Account.EmployeeId; model.SaleName = Account.EmployeeModel.FullName; model.StatusCode = "TAOMOI"; //// Tìm giá trị STT order code model.PreOrderCode = GetOrderCode(); if (model.PaymentMethodId == EnumPamentMethod.TienMat) { model.Paid = GuestAmountPaid; model.MoneyTransfer = 0; } else if (model.PaymentMethodId == EnumPamentMethod.ChuyenKhoan) { model.Paid = 0; model.MoneyTransfer = GuestAmountPaid; } else if (model.PaymentMethodId == EnumPamentMethod.CongNo) { model.Paid = 0; model.MoneyTransfer = 0; } else { return(Content("Phương thức thanh toán không hợp lệ !")); } _context.Entry(model).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); //_context.SaveChanges(); if (detail != null) { if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count) { //khong duoc trung san pham return(Content("Vui lòng không chọn thông tin sản phẩm trùng nhau")); } foreach (var item in detail) { #region Thêm giá vốn decimal?COGS = _context.ProductModel.Where(p => p.ProductId == item.ProductId).Select(p => p.COGS).FirstOrDefault(); item.COGS = COGS ?? 0; #endregion EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context); decimal TonCuoiTrongHeThong = EndInventoryRepo.GetQty(item.ProductId.Value); if (item.Quantity > TonCuoiTrongHeThong) { return(Content(string.Format("Vui lòng chọn số lượng sản phẩm ' {0} ' nhỏ hơn hoặc bằng trong kho", item.ProductName))); } PreOrderDetailModel detailmodel = new PreOrderDetailModel() { PreOrderId = model.PreOrderId, ProductId = item.ProductId, Quantity = item.Quantity, Price = item.Price, DiscountTypeId = item.DiscountTypeId, Discount = item.Discount, UnitDiscount = item.UnitDiscount, UnitPrice = item.UnitPrice, Note = item.Note, COGS = item.COGS }; _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added; } // Cập nhật lại Tổng giá vốn model.SumCOGSOfOrderDetail = detail.Sum(p => p.COGS); model.TotalDiscount = TotalBillDiscount; model.TotalVAT = TotalVAT; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); ts.Complete(); return(Content("success")); } else { //chua nhap tt san pham return(Content("Vui lòng chọn thông tin sản phẩm")); } } } else { return(Content("Vui lòng kiểm tra lại thông tin không hợp lệ")); } } catch { return(Content("Xảy ra lỗi trong quá trình thêm mới đơn hàng")); } }
public ActionResult Save(OrderReturnModel model, List <OrderReturnDetailViewModel> detail, decimal?GuestAmountPaid, int CreateReceipt) { try { if (ModelState.IsValid) { using (TransactionScope ts = new TransactionScope()) { var currentTime = DateTime.Now; #region Thêm vào OrderReturnModel model.CreatedDate = currentTime; model.CreatedAccount = currentAccount.UserName; AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault(); model.CreatedEmployeeId = Account.EmployeeId; //// Tìm giá trị STT order code model.OrderReturnMasterCode = GetReturnCode(); if (model.PaymentMethodId == EnumPaymentMethod.TienMat) { model.Paid = GuestAmountPaid; model.MoneyTransfer = 0; } else if (model.PaymentMethodId == EnumPaymentMethod.ChuyenKhoan) { model.Paid = 0; model.MoneyTransfer = GuestAmountPaid; } else if (model.PaymentMethodId == EnumPaymentMethod.CongNo) { model.Paid = 0; model.MoneyTransfer = 0; } else { return(Content("Phương thức thanh toán không hợp lệ !")); } #endregion var orderModel = _context.OrderMasterModel.Where(p => p.OrderId == model.OrderId).FirstOrDefault(); #region Tính số dư còn lại decimal?CustomerOldDebt = _context.AM_DebtModel .Where(p => p.CustomerId == orderModel.CustomerId) .OrderByDescending(p => p.TimeOfDebt) .Select(p => p.RemainingAmountAccrued) .FirstOrDefault(); CustomerOldDebt = (CustomerOldDebt == null) ? 0 : CustomerOldDebt.Value; model.RemainingAmount = (model.RemainingAmount == null) ? 0 : model.RemainingAmount.Value; model.RemainingAmountAccrued = CustomerOldDebt.Value - model.RemainingAmount.Value; _context.Entry(model).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); // LƯU TẠM ĐỂ LẤY OrderId (SẼ BỊ SCROLLBACK KHI XẢY RA LỖI) #endregion if (CreateReceipt == 1) { #region Thêm vào giao dịch kế toán AM_TransactionModel AMmodel; int MaKH = (_context.OrderMasterModel.Where(p => p.OrderId == model.OrderId).Select(p => p.CustomerId).FirstOrDefault()); #region TH1 : Trả đủ if (model.TotalPrice == GuestAmountPaid) { AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = model.PaymentMethodId == EnumPaymentMethod.TienMat ? (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.TM && p.AMAccountTypeCode == EnumAM_AccountType.TIENMAT && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault() : (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.NH && p.AMAccountTypeCode == EnumAM_AccountType.NGANHANG && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.BHTRA, ContactItemTypeCode = EnumContactType.KH, CustomerId = MaKH, SupplierId = null, EmployeeId = null, OtherId = null, Amount = GuestAmountPaid, OrderId = model.OrderId, ImportMasterId = null, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion #region TH2 : Không trả lưu vào công nợ else if (GuestAmountPaid == 0 || GuestAmountPaid == null) { AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.PTKH && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.BHTRA, ContactItemTypeCode = EnumContactType.KH, CustomerId = MaKH, SupplierId = null, EmployeeId = null, OtherId = null, Amount = model.TotalPrice, OrderId = model.OrderId, ImportMasterId = null, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; // model.PaymentMethodId == EnumPamentMethod.CongNo model.PaymentMethodId = EnumPaymentMethod.CongNo; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); } #endregion #region TH3 : Trả 1 phần else { #region 1 phần (Tiền mặt hoặc chuyển khoản) AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = model.PaymentMethodId == EnumPaymentMethod.TienMat ? (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.TM && p.AMAccountTypeCode == EnumAM_AccountType.TIENMAT && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault() : (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.NH && p.AMAccountTypeCode == EnumAM_AccountType.NGANHANG && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.BHTRA, ContactItemTypeCode = EnumContactType.KH, CustomerId = MaKH, SupplierId = null, EmployeeId = currentEmployee.EmployeeId, OtherId = null, Amount = GuestAmountPaid, OrderId = model.OrderId, ImportMasterId = null, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion #region 1 phần đưa vào công nợ AMmodel = new AM_TransactionModel() { StoreId = model.StoreId, AMAccountId = (_context.AM_AccountModel.Where(p => p.Code == EnumAccountCode.PTKH && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.BHTRA, ContactItemTypeCode = EnumContactType.KH, CustomerId = MaKH, SupplierId = null, EmployeeId = null, OtherId = null, Amount = model.TotalPrice - GuestAmountPaid, OrderId = model.OrderId, ImportMasterId = null, IEOtherMasterId = null, Note = model.Note, CreateDate = currentTime, CreateEmpId = currentEmployee.EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion } #endregion #endregion } #region Thêm AM_DebtModel (Số nợ còn lại) if (model.RemainingAmount > 0) { var AMDebModel = new AM_DebtModel() { CustomerId = orderModel.CustomerId, TimeOfDebt = currentTime, RemainingAmountAccrued = model.RemainingAmountAccrued, OrderReturnId = model.OrderReturnMasterId, TransactionTypeCode = EnumTransactionType.BHTRA }; _context.Entry(AMDebModel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion #region Thêm vào InventoryMaster // Insert InventoryMaster InventoryMasterModel InvenMaster = new InventoryMasterModel(); InvenMaster.WarehouseModelId = model.WarehouseId; InvenMaster.InventoryTypeId = EnumInventoryType.NB;// Nhập - Khách trả hàng InvenMaster.InventoryCode = model.OrderReturnMasterCode; InvenMaster.CreatedDate = model.CreatedDate; InvenMaster.CreatedAccount = model.CreatedAccount; InvenMaster.CreatedEmployeeId = model.CreatedEmployeeId; InvenMaster.StoreId = model.StoreId; InvenMaster.Actived = true; InvenMaster.BusinessId = model.OrderId; // Id nghiệp vụ InvenMaster.BusinessName = "OrderReturnMaster"; // Tên bảng nghiệp vụ InvenMaster.ActionUrl = "/OrderReturnMaster/Details/"; // Đường dẫn ( cộng ID cho truy xuất) _context.Entry(InvenMaster).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); // insert tạm để lấy InvenMasterID #endregion #region duyệt list lưu OrderReturnDetailModel và InvenrotyDetail decimal TotalQty = 0; foreach (var item in detail) { if (item.ReturnQuantity > 0) // Chỉ tính Số lượng trả > 0 { TotalQty += item.ReturnQuantity.Value; #region Lưu OrderReturnDetailModel decimal CogsInOd = _context.OrderDetailModel.Where(p => p.OrderId == model.OrderId && p.ProductId == item.ProductId).Select(p => p.COGS.Value).FirstOrDefault(); item.COGS = CogsInOd; OrderReturnDetailModel detailmodel = new OrderReturnDetailModel() { OrderReturnId = model.OrderReturnMasterId, ProductId = item.ProductId, SellQuantity = item.SellQuantity, ReturnedQuantity = item.ReturnQuantity, // cần xem lại ReturnQuantity = item.ReturnQuantity, Price = item.Price, UnitPrice = item.UnitPrice, Note = item.Note, ReturnReason = item.ReturnReason, COGS = item.COGS }; _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion #region Lưu InventoryDetail // tính tồn đầu EndInventoryRepository EndInventoryRepo = new EndInventoryRepository(_context); decimal tondau = EndInventoryRepo.GetQty(item.ProductId.Value); InventoryDetailModel InvenDetail = new InventoryDetailModel() { InventoryMasterId = InvenMaster.InventoryMasterId, ProductId = item.ProductId, BeginInventoryQty = tondau, COGS = CogsInOd, //Price = item.Price, ImportQty = item.ReturnQuantity, //ExportQty = 0, UnitCOGS = item.ReturnQuantity * CogsInOd, //UnitPrice = item.ReturnQuantity * item.Price, //[ImportQty] *[Price] EndInventoryQty = tondau + item.ReturnQuantity//Tồn cuối = [BeginInventoryQty] + [ImportQty] - [ExportQty] }; _context.Entry(InvenDetail).State = System.Data.Entity.EntityState.Added; // _context.SaveChanges(); #endregion } } #endregion // Cập nhật lại Tổng giá vốn model.SumCOGSOfOrderDetail = detail.Where(p => p.ReturnQuantity > 0).Sum(p => p.COGS * p.ReturnQuantity); model.TotalQty = TotalQty; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); ts.Complete(); return(Content("success")); } } else { return(Content("Vui lòng kiểm tra lại thông tin không hợp lệ")); } } catch { return(Content("Xảy ra lỗi trong quá trình thêm mới đơn hàng")); } }