public ActionResult Edit(int id) { ImportMasterModel model = _context.ImportMasterModel .Include(p => p.ImportDetailModel) .Where(p => p.ImportMasterId == id).FirstOrDefault(); model.CreatedAccount = currentAccount.UserName; AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault(); ViewBag.SalemanName = Account.EmployeeModel.FullName; ViewBag.ImportDetailList = model.ImportDetailModel.Select(p => new ImportDetailViewModel() { //ImportDetailId = p.ImportDetailId, ImportMasterId = p.ImportMasterId, ProductId = p.ProductId, Qty = p.Qty, Price = p.Price, UnitShippingWeight = p.UnitShippingWeight, UnitPrice = p.UnitPrice, ProductName = p.ProductModel.ProductName, }).ToList(); if (model == null) { return(HttpNotFound()); } CreateViewBag(); return(View(model)); }
public ActionResult Save(ImportMasterModel model, List <ImportDetailViewModel> detail, decimal TotalShippingWeight, decimal?GuestAmountPaid, DateTime ExchangeDate, int CreateReceipt = 1) { string resuilt = ""; ImportMasterRepository ImportMasterRepo = new ImportMasterRepository(_context); resuilt = ImportMasterRepo.Save(model, detail, TotalShippingWeight, GuestAmountPaid, ExchangeDate, CreateReceipt, currentAccount.UserName, currentEmployee.EmployeeId); return(Json(resuilt, JsonRequestBehavior.AllowGet)); }
public ActionResult Details(int id) { ImportMasterModel importmastermodel = _context.ImportMasterModel.Find(id); if (importmastermodel == null) { return(HttpNotFound()); } ViewBag.StoreName = _context.StoreModel.Where(p => p.StoreId == importmastermodel.StoreId).Select(p => p.StoreName).FirstOrDefault(); ViewBag.WarhouseName = _context.WarehouseModel.Where(p => p.WarehouseId == importmastermodel.WarehouseId).Select(p => p.WarehouseName).FirstOrDefault(); return(View(importmastermodel)); }
public ActionResult Cancel(int id) { //checkSession(); try { //using(TransactionScope ts = new TransactionScope()) //{ ImportMasterModel model = _context.ImportMasterModel .Where(p => p.ImportMasterId == id) .FirstOrDefault(); var Resuilt = ""; if (model == null) { Resuilt = "Không tìm thấy đơn hàng yêu cầu !"; } else { using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) { cmd.CommandText = "usp_ImportCanceled"; cmd.Parameters.AddWithValue("@ImportMasterId", model.ImportMasterId); cmd.Parameters.AddWithValue("@DeletedDate", DateTime.Now); AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault(); model.DeletedEmployeeId = Account.EmployeeId; cmd.Parameters.AddWithValue("@DeletedAccount", currentAccount.UserName); cmd.Parameters.AddWithValue("@DeletedEmployeeId", model.DeletedEmployeeId); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } //_context.Entry(model).State = System.Data.Entity.EntityState.Modified; //_context.SaveChanges(); Resuilt = "success"; //ts.Complete(); return(Json(Resuilt, JsonRequestBehavior.AllowGet)); } catch { return(Json(string.Format(Resources.LanguageResource.CancelErrorMessenge, "nhập hàng nhà cung cấp"), JsonRequestBehavior.AllowGet)); } }
public ActionResult Create() { ImportMasterModel model = new ImportMasterModel() { Actived = true }; //model.SalemanName = model.CreatedAccount = currentAccount.UserName; AccountModel Account = _context.AccountModel.Where(p => p.UserName == model.CreatedAccount).FirstOrDefault(); model.SalemanName = Account.EmployeeModel.FullName; ViewBag.SalemanName = model.SalemanName; CreateViewBag(null, null, null, null, 1); ViewBag.ImportCode = GetImportMasterCode(); return(View(model)); }
public ActionResult SaveConfirm(ImportMasterModel model, List <PreImportDetailViewModel> detail, decimal TotalShippingWeight, decimal?GuestAmountPaid, DateTime ExchangeDate, int PreImportMasterId, string StatusCode, int CreateReceipt = 1) { if (StatusCode.Equals("DADUYET") || StatusCode.Equals("HUY")) { return(Json("Yêu cầu đơn đặt hàng này đã được duyệt hoặc bị huỷ", JsonRequestBehavior.AllowGet)); } #region // Lấy danh sách sản phẩm có ComfirmQty > 0 var Lstdetail = (from p in detail where p.ConfirmQty > 0 select new ImportDetailViewModel() { ProductId = p.ProductId, Qty = p.ConfirmQty, Price = p.Price, UnitShippingWeight = p.UnitShippingWeight, UnitPrice = p.UnitPrice, Note = p.Note, ShippingFee = p.ShippingFee, UnitCOGS = p.UnitCOGS }).ToList(); model.TotalQty = Lstdetail.Count; #endregion // Bước 1 : Tính toán tương tự ImportMaster ImportMasterRepository ImportRepo = new ImportMasterRepository(_context); string Resuilt = ImportRepo.Save(model, Lstdetail, TotalShippingWeight, GuestAmountPaid, ExchangeDate, CreateReceipt, currentAccount.UserName, currentEmployee.EmployeeId); // Bước 2 : Cập nhật lại các field bên PreImportMaster : StatusCode , ImportMasterId if (Resuilt.Equals("success")) { var PreImportModel = _context.PreImportMasterModel.Where(p => p.PreImportMasterId == PreImportMasterId).FirstOrDefault(); PreImportModel.ImportMasterId = model.ImportMasterId; PreImportModel.StatusCode = "DADUYET"; _context.Entry(PreImportModel).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); } return(Json(Resuilt, 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 Update(ImportMasterModel model, List <ImportDetailViewModel> detail, decimal TotalShippingWeight) { try { //_context.Entry(model).State = System.Data.Entity.EntityState.Modified; Chỉ Modified những filed cần thiết ImportMasterModel ImportMastermodel = _context.ImportMasterModel .Include(p => p.ImportDetailModel) .Where(p => p.ImportMasterId == model.ImportMasterId).FirstOrDefault(); // Xoá những Detail cũ if (ImportMastermodel.ImportDetailModel != null && ImportMastermodel.ImportDetailModel.Count > 0) { while (ImportMastermodel.ImportDetailModel.Count > 0) { _context.Entry(ImportMastermodel.ImportDetailModel.First()).State = System.Data.Entity.EntityState.Deleted; } } #region // Update lại những filed cần Modified cho Model Master ImportMastermodel.ImportMasterId = model.ImportMasterId; ImportMastermodel.SupplierId = model.SupplierId; ImportMastermodel.SalemanName = model.SalemanName; ImportMastermodel.SenderName = model.SenderName; ImportMastermodel.ReceiverName = model.ReceiverName; ImportMastermodel.Note = model.Note; ImportMastermodel.CurrencyId = model.CurrencyId; ImportMastermodel.ExchangeRate = model.ExchangeRate; ImportMastermodel.WarehouseId = model.WarehouseId; ImportMastermodel.VATType = model.VATType; ImportMastermodel.VATValue = model.VATValue; ImportMastermodel.TAXBillCode = model.TAXBillCode; ImportMastermodel.TAXBillDate = model.TAXBillDate; ImportMastermodel.ManualDiscountType = model.ManualDiscountType; ImportMastermodel.ManualDiscount = model.ManualDiscount; ImportMastermodel.Paid = model.Paid; ImportMastermodel.MoneyTransfer = model.MoneyTransfer; ImportMastermodel.DebtDueDate = model.DebtDueDate; ImportMastermodel.LastModifiedDate = DateTime.Now; ImportMastermodel.TotalQty = model.TotalQty; ImportMastermodel.TotalPrice = model.TotalPrice; #endregion // đánh dấu sửa ImportMasterModel _context.Entry(ImportMastermodel).State = System.Data.Entity.EntityState.Modified; if (detail != null) { foreach (var item in detail) { ImportDetailModel detailmodel = new ImportDetailModel() { ImportMasterId = model.ImportMasterId, ProductId = item.ProductId, Qty = item.Qty, Price = item.Price, UnitShippingWeight = item.UnitShippingWeight, UnitPrice = item.UnitPrice }; _context.Entry(detailmodel).State = System.Data.Entity.EntityState.Added; } } _context.SaveChanges(); return(Content("success")); } catch { return(Content("Xảy ra lỗi trong quá trình thêm mới nhà cung cấp")); } }