public ActionResult Create() { CreateViewSearchBag(); AM_TransactionModel model = new AM_TransactionModel(); return(View(model)); }
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 string Save(ImportMasterModel model, List <ImportDetailViewModel> detail, decimal TotalShippingWeight, decimal?GuestAmountPaid, DateTime ExchangeDate, int CreateReceipt, string UserName, int EmployeeId) { try { using (TransactionScope ts = new TransactionScope()) { var currentTime = DateTime.Now; #region ImportMaster model.CreatedDate = currentTime; model.CreatedAccount = UserName; model.CreatedEmployeeId = EmployeeId; model.InventoryTypeId = EnumInventoryType.NC; model.Paid = GuestAmountPaid.HasValue ? GuestAmountPaid : 0; model.ImportMasterCode = GetImportMasterCode(); //Thêm tổng công nợ cộng dồn = nợ cũ + nợ mới //decimal? SuplierOldDebt = _context.ImportMasterModel // .Where(p => p.SupplierId == model.SupplierId) // .OrderByDescending(p => p.ImportMasterId) // .Select(p => p.RemainingAmountAccrued) // .FirstOrDefault(); decimal?SuplierOldDebt = _context.AM_DebtModel .Where(p => p.SupplierId == model.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 IMPORTMASTERID (SẼ BỊ SCROLLBACK KHI XẢY RA LỖI) #endregion #region Kế toán if (CreateReceipt == 1) { #region Thêm vào giao dịch kế toán AM_TransactionModel AMmodel; #region TH1 : Trả đủ 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.NXNHAP, 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 = 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.PTNCC && p.AMAccountTypeCode == EnumAM_AccountType.CONGNO && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(), TransactionTypeCode = EnumTransactionType.NXNHAP, 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 = EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _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 = (_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.NXNHAP, 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 = 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.NXNHAP, 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 = EmployeeId, RemainingAmountAccrued = model.RemainingAmountAccrued }; _context.Entry(AMmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion } #endregion #endregion } #endregion #region Thêm AM_DebtModel (Số nợ còn lại) if (model.RemainingAmount > 0) { var AMDebModel = new AM_DebtModel() { SupplierId = model.SupplierId, TimeOfDebt = currentTime, RemainingAmountAccrued = model.RemainingAmountAccrued, ImportId = model.ImportMasterId, TransactionTypeCode = EnumTransactionType.NXNHAP }; _context.Entry(AMDebModel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion #region InventoryMaster InventoryMasterModel InvenMaster = new InventoryMasterModel(); InvenMaster.WarehouseModelId = model.WarehouseId; InvenMaster.InventoryCode = model.ImportMasterCode; InvenMaster.InventoryTypeId = EnumInventoryType.NC; InvenMaster.CreatedDate = model.CreatedDate; InvenMaster.CreatedAccount = model.CreatedAccount; InvenMaster.CreatedEmployeeId = model.CreatedEmployeeId; InvenMaster.Actived = true; InvenMaster.BusinessId = model.ImportMasterId; // Id nghiệp vụ InvenMaster.BusinessName = "ImportMasterModel"; // Tên bảng nghiệp vụ InvenMaster.ActionUrl = "/ImportMaster/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 ExchangeRate // Update ExchangeRate var Exchangerate = _context.ExchangeRateModel .OrderByDescending(p => p.ExchangeDate) .Where(p => p.CurrencyId == model.CurrencyId && p.ExchangeDate.Value.CompareTo(DateTime.Now) <= 0 ) .FirstOrDefault(); string DateDB = string.Format("{0}-{1}-{2}", Exchangerate.ExchangeDate.Value.Year, Exchangerate.ExchangeDate.Value.Month, Exchangerate.ExchangeDate.Value.Day); string DateNow = string.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); if (DateDB == DateNow) { // update Exchangerate.ExchangeRate = (float)model.ExchangeRate.Value; Exchangerate.ExchangeDate = DateTime.Now; _context.Entry(Exchangerate).State = System.Data.Entity.EntityState.Modified; } else { ExchangeRateModel Exchangeratemodel = new ExchangeRateModel() { CurrencyId = model.CurrencyId, ExchangeRate = (float)model.ExchangeRate.Value, ExchangeDate = DateTime.Now, }; // add _context.Entry(Exchangeratemodel).State = System.Data.Entity.EntityState.Added; } _context.SaveChanges(); #endregion #region Lst Product if (detail != null) { //if (detail.GroupBy(p => p.ProductId).ToList().Count < detail.Count) //{ // //khong duoc trung san pham // return "Vui lòng không chọn thông tin sản phẩm trùng nhau !"; //} foreach (var item in detail) { item.UnitCOGS = (item.Price * model.ExchangeRate) + item.ShippingFee; #region Import Detail ImportDetailModel detailmodel = new ImportDetailModel() { ImportMasterId = model.ImportMasterId, ProductId = item.ProductId, Qty = item.Qty, Price = item.Price, UnitShippingWeight = item.UnitShippingWeight, UnitPrice = item.UnitPrice, ShippingFee = item.ShippingFee, UnitCOGS = item.UnitCOGS, Note = item.Note }; _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion #region update bảng Product var productmodel = _context.ProductModel.Where(p => p.ProductId == item.ProductId).FirstOrDefault(); //productmodel.ImportPrice = item.Price; //productmodel.ShippingFee = item.ShippingFee; //productmodel.COGS = item.UnitCOGS; //productmodel.CurrencyId = model.CurrencyId; //productmodel.ExchangeRate = model.ExchangeRate; productmodel.ImportDate = DateTime.Now; _context.Entry(productmodel).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); #endregion } #region Insert InventoryDetail #region groupby Importdetail var detailgruoppd = (from c in detail group c by new { c.ProductId } into gcs select new ImportDetailViewModel() { ProductId = gcs.Key.ProductId, Qty = gcs.Sum(p => p.Qty), Price = gcs.Sum(p => p.Price), UnitShippingWeight = gcs.Sum(p => p.UnitShippingWeight), UnitPrice = gcs.Sum(p => p.UnitPrice), ShippingFee = gcs.Sum(p => p.ShippingFee), UnitCOGS = gcs.Sum(p => p.UnitCOGS) }).ToList(); #endregion foreach (var item in detailgruoppd) { item.UnitCOGS = (item.Price * model.ExchangeRate) + item.ShippingFee; #region Insert // Insert InventoryDetail 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 = item.Qty, //ExportQty = 0, UnitCOGS = GiaVon * item.Qty, //UnitPrice = 0, EndInventoryQty = tondau + item.Qty }; _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.Sum(p => p.UnitCOGS * p.Qty); _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); // đánh dấu Transaction hoàn tất ts.Complete(); #region Cập nhật Tỉ giá và giá nhập,phí vận chuyển,giá vốn trên StoreProcedure #endregion return("success"); } else { //chua nhap tt san pham return("Vui lòng chọn thông tin sản phẩm"); } #endregion } } catch { return("Xảy ra lỗi trong quá trình thêm mới sản phẩm từ nhà cung cấp"); } }
public ActionResult Save(AM_TransactionModel model) { if (ModelState.IsValid) { try { using (TransactionScope ts = new TransactionScope()) { var currentTime = DateTime.Now; decimal?DebtOld = null; // nợ cũ int? CustomerIdCreate = null, SupplierIdCreate = null; model.AMAccountId = (_context.AM_AccountModel.Where(p => p.AMAccountTypeCode == EnumAM_AccountType.NGANHANG && p.StoreId == model.StoreId).Select(p => p.AMAccountId)).FirstOrDefault(); model.CreateDate = currentTime; model.CreateEmpId = currentEmployee.EmployeeId; int dau = model.TransactionTypeCode.Equals(EnumTransactionType.NHRUT) ? 1 : -1; // Xét dấu (Thu hay chi tiền ) #region Thêm vào bảng AMDebModel if (model.ContactItemTypeCode.Equals("KH") || model.ContactItemTypeCode.Equals("NCC")) { #region Đối tượng là khách hàng //B1. Tính nợ cũ còn lại if (model.ContactItemTypeCode.Equals("KH") && model.CustomerId.HasValue) { DebtOld = _context.AM_DebtModel .Where(p => p.CustomerId == model.CustomerId) .OrderByDescending(p => p.TimeOfDebt) .Select(p => p.RemainingAmountAccrued) .FirstOrDefault(); CustomerIdCreate = model.CustomerId; } #endregion #region Đối tượng là NCC else if (model.ContactItemTypeCode.Equals("NCC") && model.SupplierId.HasValue) { DebtOld = _context.AM_DebtModel .Where(p => p.SupplierId == model.SupplierId) .OrderByDescending(p => p.TimeOfDebt) .Select(p => p.RemainingAmountAccrued) .FirstOrDefault(); SupplierIdCreate = model.SupplierId; } #endregion //B2 : Đưa nợ cũ còn lại vào AM_DebtModel DebtOld = (DebtOld == null) ? 0 : DebtOld.Value; model.RemainingAmountAccrued = DebtOld + (model.Amount * dau); _context.Entry(model).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); var AMDebModel = new AM_DebtModel() { SupplierId = SupplierIdCreate, CustomerId = CustomerIdCreate, TimeOfDebt = currentTime, RemainingAmountAccrued = model.RemainingAmountAccrued, TransactionId = model.TransactionId, TransactionTypeCode = model.TransactionTypeCode }; _context.Entry(AMDebModel).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } else { _context.Entry(model).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); } #endregion ts.Complete(); return(Json("success", JsonRequestBehavior.AllowGet)); } } catch { return(Json("Xảy ra lỗi trong quá trình thêm mới !", JsonRequestBehavior.AllowGet)); } } return(Json("Vui lòng kiểm tra các ô được điền đầy đủ thông tin !", JsonRequestBehavior.AllowGet)); }
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")); } }