public ActionResult Create(PhysicalInventoryViewModel model) { if (ModelState.IsValid) { PhysicalInventory PhysicalInventory = new Domain.Sale.Entities.PhysicalInventory(); AutoMapper.Mapper.Map(model, PhysicalInventory); PhysicalInventory.CreatedUserId = WebSecurity.CurrentUserId; PhysicalInventory.CreatedDate = DateTime.Now; PhysicalInventory.ModifiedUserId = WebSecurity.CurrentUserId; PhysicalInventory.ModifiedDate = DateTime.Now; PhysicalInventory.IsDeleted = false; PhysicalInventory.IsExchange = false; PhysicalInventory.BranchId = Helpers.Common.CurrentUser.BranchId; List <PhysicalInventoryDetail> PhysicalInventoryDetailList = new List <PhysicalInventoryDetail>(); if (model.DetailList != null) { foreach (var item in model.DetailList) { Inventory inventoryProduct = InventoryRepository.GetInventoryByWarehouseIdAndProductId(model.WarehouseId.Value, item.ProductId); int QuantityInInventory = 0; if (inventoryProduct != null) { QuantityInInventory = inventoryProduct.Quantity.Value; inventoryProduct.Quantity = item.QuantityRemaining; InventoryRepository.UpdateInventory(inventoryProduct); } PhysicalInventoryDetailList.Add(new PhysicalInventoryDetail { CreatedDate = DateTime.Now, CreatedUserId = WebSecurity.CurrentUserId, IsDeleted = false, Note = item.Note, ProductId = item.ProductId, QuantityInInventory = QuantityInInventory, QuantityRemaining = item.QuantityRemaining, QuantityDiff = item.QuantityRemaining - QuantityInInventory }); } } PhysicalInventoryRepository.InsertPhysicalInventory(PhysicalInventory, PhysicalInventoryDetailList); //cập nhật lại mã kiểm kho PhysicalInventory.Code = Erp.BackOffice.Helpers.Common.GetOrderNo("InventoryCheck"); PhysicalInventoryRepository.UpdatePhysicalInventory(PhysicalInventory); Erp.BackOffice.Helpers.Common.SetOrderNo("InventoryCheck"); TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess; if (model.Type == PhysicalInventoryType.Product) { return(RedirectToAction("Index")); } else { return(RedirectToAction("ListForCard")); } } var warehouseList = WarehouseRepository.GetAllWarehouse().Where(x => x.BranchId == Helpers.Common.CurrentUser.BranchId).AsEnumerable(); if (model.Type == PhysicalInventoryType.Card) { warehouseList = warehouseList.Where(x => x.Categories.Contains("CARD")); } else if (model.Type == PhysicalInventoryType.Gift) { warehouseList = warehouseList.Where(x => x.Categories.Contains("GIFT")); } var warehouseSelectList = warehouseList .Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString() }); ViewBag.warehouseList = warehouseSelectList; var manufacturer = categoryRepository.GetCategoryByCode("manufacturerList").AsEnumerable() .Select(item => new SelectListItem { Text = item.Name, Value = item.Value }); ViewBag.manufacturer = manufacturer; return(View(model)); }
public ActionResult Create(PhysicalInventoryViewModel model) { if (ModelState.IsValid) { using (var scope = new TransactionScope(TransactionScopeOption.Required)) { try { PhysicalInventory PhysicalInventory = new Domain.Sale.Entities.PhysicalInventory(); AutoMapper.Mapper.Map(model, PhysicalInventory); PhysicalInventory.CreatedUserId = WebSecurity.CurrentUserId; PhysicalInventory.CreatedDate = DateTime.Now; PhysicalInventory.ModifiedUserId = WebSecurity.CurrentUserId; PhysicalInventory.ModifiedDate = DateTime.Now; PhysicalInventory.IsDeleted = false; PhysicalInventory.IsExchange = false; var warehouse = WarehouseRepository.GetWarehouseById(model.WarehouseId.Value); PhysicalInventory.BranchId = warehouse.BranchId; List <PhysicalInventoryDetail> PhysicalInventoryDetailList = new List <PhysicalInventoryDetail>(); if (model.DetailList != null) { var list_inventoryProduct = InventoryRepository.GetAllInventory(); foreach (var item in model.DetailList) { var inventoryProduct = list_inventoryProduct.Where(x => x.WarehouseId == PhysicalInventory.WarehouseId && x.ProductId == item.ProductId).ToList(); if (!string.IsNullOrEmpty(item.LoCode)) { inventoryProduct = inventoryProduct.Where(x => x.LoCode == item.LoCode).ToList(); } else { inventoryProduct = inventoryProduct.Where(x => x.LoCode == null).ToList(); } if (item.ExpiryDate == null) { inventoryProduct = inventoryProduct.Where(x => x.ExpiryDate == null).ToList(); } else { inventoryProduct = inventoryProduct.Where(x => x.ExpiryDate == item.ExpiryDate).ToList(); } int QuantityInInventory = 0; if (inventoryProduct.Count() > 0) { var ii = inventoryProduct.FirstOrDefault(); QuantityInInventory = ii.Quantity.Value; ii.Quantity = item.QuantityRemaining; InventoryRepository.UpdateInventory(ii); } PhysicalInventoryDetailList.Add(new PhysicalInventoryDetail { CreatedDate = DateTime.Now, CreatedUserId = WebSecurity.CurrentUserId, IsDeleted = false, Note = item.Note, ProductId = item.ProductId, QuantityInInventory = QuantityInInventory, QuantityRemaining = item.QuantityRemaining, QuantityDiff = item.QuantityRemaining - QuantityInInventory, LoCode = item.LoCode, ExpiryDate = item.ExpiryDate }); } } PhysicalInventoryRepository.InsertPhysicalInventory(PhysicalInventory, PhysicalInventoryDetailList); //cập nhật lại mã kiểm kho string prefix = Erp.BackOffice.Helpers.Common.GetSetting("prefixOrderNo_InventoryCheck"); PhysicalInventory.Code = Erp.BackOffice.Helpers.Common.GetCode(prefix, PhysicalInventory.Id); PhysicalInventoryRepository.UpdatePhysicalInventory(PhysicalInventory); scope.Complete(); } catch (DbUpdateException) { return(Content("Fail")); } } TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess; return(RedirectToAction("Index")); } var warehouseList = WarehouseRepository.GetAllWarehouse().AsEnumerable() .Select(item => new SelectListItem { Text = item.Name, Value = item.Id.ToString() }); ViewBag.warehouseList = warehouseList; var manufacturer = categoryRepository.GetCategoryByCode("manufacturerList").AsEnumerable() .Select(item => new SelectListItem { Text = item.Name, Value = item.Value }); ViewBag.manufacturer = manufacturer; return(View(model)); }