public ActionResult Exchange(int Id) { var PhysicalInventory = PhysicalInventoryRepository.GetvwPhysicalInventoryById(Id); var model = new PhysicalInventory(); if (PhysicalInventory == null) { TempData[Globals.FailedMessageKey] = App_GlobalResources.Wording.NotfoundObject; return(RedirectToAction("Index")); } using (var scope = new TransactionScope(TransactionScopeOption.Required)) { try { AutoMapper.Mapper.Map(PhysicalInventory, model); List <ProductOutboundDetail> outboundDetails = new List <ProductOutboundDetail>(); List <ProductInboundDetail> inboundDetails = new List <ProductInboundDetail>(); var outbounds = productOutboundRepository.GetAllProductOutbound().Where(item => item.Code == PhysicalInventory.ProductOutboundCode); if (outbounds.Count() > 0) { //Xóa chi tiết xuất cũ var outboundDetails_old = productOutboundRepository.GetAllProductOutboundDetailByOutboundId(outbounds.FirstOrDefault().Id).Select(item => item.Id).ToList(); foreach (var item in outboundDetails_old) { productOutboundRepository.DeleteProductOutboundDetail(item); } productOutboundRepository.DeleteProductOutbound(outbounds.FirstOrDefault().Id); } var inbounds = productInboundRepository.GetAllProductInbound().Where(item => item.Code == PhysicalInventory.ProductInboundCode); if (inbounds.Count() > 0) { //Xóa chi tiết nhập cũ var inboundDetails_old = productInboundRepository.GetAllProductInboundDetailByInboundId(inbounds.FirstOrDefault().Id).Select(item => item.Id).ToList(); foreach (var item in inboundDetails_old) { productInboundRepository.DeleteProductInboundDetail(item); } productInboundRepository.DeleteProductInbound(inbounds.FirstOrDefault().Id); } var listDetail = PhysicalInventoryRepository.GetAllPhysicalInventoryDetail(Id).Where(x => x.QuantityInInventory != x.QuantityRemaining).ToList(); foreach (var item in listDetail) { var product = ProductRepository.GetProductById(item.ProductId); if (item.QuantityDiff < 0) //Chênh lệch dương thì thuộc về xuất { outboundDetails.Add( new ProductOutboundDetail { IsDeleted = false, CreatedDate = DateTime.Now, CreatedUserId = Helpers.Common.CurrentUser.Id, ModifiedDate = DateTime.Now, ModifiedUserId = Helpers.Common.CurrentUser.Id, Price = product.PriceOutbound, ProductId = product.Id, Quantity = Math.Abs(item.QuantityDiff), ExpiryDate = item.ExpiryDate, LoCode = item.LoCode } ); } else if (item.QuantityDiff > 0) //Chênh lệch âm thì thuộc về nhập { inboundDetails.Add( new ProductInboundDetail { IsDeleted = false, CreatedDate = DateTime.Now, CreatedUserId = Helpers.Common.CurrentUser.Id, ModifiedDate = DateTime.Now, ModifiedUserId = Helpers.Common.CurrentUser.Id, Price = product.PriceInbound, ProductId = product.Id, Quantity = Math.Abs(item.QuantityDiff), LoCode = item.LoCode, ExpiryDate = item.ExpiryDate } ); } } if (outboundDetails.Count != 0) { var outbound = new ProductOutbound { IsDeleted = false, CreatedDate = DateTime.Now, CreatedUserId = Helpers.Common.CurrentUser.Id, ModifiedDate = DateTime.Now, ModifiedUserId = Helpers.Common.CurrentUser.Id, BranchId = model.BranchId, IsDone = true, Type = "PhysicalInventory", TotalAmount = outboundDetails.Sum(x => x.Quantity * x.Price), WarehouseSourceId = PhysicalInventory.WarehouseId, PhysicalInventoryId = PhysicalInventory.Id, Note = "Xuất kho kiểm kê" }; productOutboundRepository.InsertProductOutbound(outbound); foreach (var item in outboundDetails) { item.ProductOutboundId = outbound.Id; item.IsDeleted = false; item.CreatedUserId = WebSecurity.CurrentUserId; item.ModifiedUserId = WebSecurity.CurrentUserId; item.CreatedDate = DateTime.Now; item.ModifiedDate = DateTime.Now; productOutboundRepository.InsertProductOutboundDetail(item); } //cập nhật lại mã xuất kho string prefix = Erp.BackOffice.Helpers.Common.GetSetting("prefixOrderNo_Outbound"); outbound.Code = Erp.BackOffice.Helpers.Common.GetCode(prefix, outbound.Id); productOutboundRepository.UpdateProductOutbound(outbound); foreach (var item in listDetail.Where(x => x.QuantityDiff < 0)) { item.ModifiedDate = DateTime.Now; item.ModifiedUserId = Helpers.Common.CurrentUser.Id; item.ReferenceVoucher = outbound.Code; PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item); } } if (inboundDetails.Count != 0) { var inbound = new ProductInbound { IsDeleted = false, CreatedDate = DateTime.Now, CreatedUserId = Helpers.Common.CurrentUser.Id, ModifiedDate = DateTime.Now, ModifiedUserId = Helpers.Common.CurrentUser.Id, BranchId = model.BranchId, IsDone = true, Type = "PhysicalInventory", TotalAmount = inboundDetails.Sum(x => x.Quantity * x.Price), WarehouseDestinationId = PhysicalInventory.WarehouseId, PhysicalInventoryId = PhysicalInventory.Id, Note = "Nhập kho kiểm kê" }; productInboundRepository.InsertProductInbound(inbound); //Thêm chi tiết phiếu nhập foreach (var item in inboundDetails) { item.ProductInboundId = inbound.Id; item.IsDeleted = false; item.CreatedUserId = WebSecurity.CurrentUserId; item.ModifiedUserId = WebSecurity.CurrentUserId; item.CreatedDate = DateTime.Now; item.ModifiedDate = DateTime.Now; productInboundRepository.InsertProductInboundDetail(item); } //cập nhật lại mã xuất kho string prefix = Erp.BackOffice.Helpers.Common.GetSetting("prefixOrderNo_Inbound"); inbound.Code = Erp.BackOffice.Helpers.Common.GetCode(prefix, inbound.Id); productInboundRepository.UpdateProductInbound(inbound); foreach (var item in listDetail.Where(x => x.QuantityDiff > 0)) { item.ModifiedDate = DateTime.Now; item.ModifiedUserId = Helpers.Common.CurrentUser.Id; item.ReferenceVoucher = inbound.Code; PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item); } } model.IsExchange = true; model.ModifiedDate = DateTime.Now; model.ModifiedUserId = Helpers.Common.CurrentUser.Id; PhysicalInventoryRepository.UpdatePhysicalInventory(model); scope.Complete(); } catch (DbUpdateException) { return(Content("Fail")); } } TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.Success; return(RedirectToAction("Detail", new { Id = PhysicalInventory.Id })); }
public ActionResult CheckData(int Id) { var physicalInventory = PhysicalInventoryRepository.GetAllvwPhysicalInventory().Where(item => item.Id == Id).FirstOrDefault(); if (physicalInventory == null) { TempData[Globals.FailedMessageKey] = App_GlobalResources.Wording.NotfoundObject; return(RedirectToAction("Index")); } //Cập nhật lại tồn kho hệ thống đến thời điểm kiểm kê var listDetail = PhysicalInventoryRepository.GetAllPhysicalInventoryDetail(Id).ToList(); foreach (var item in listDetail) { //Số lượng nhập var soLuongNhap = productInboundRepository.GetAllvwProductInboundDetailByProductId(item.ProductId) .Where(x => x.IsArchive == true && x.WarehouseDestinationId == physicalInventory.WarehouseId).Sum(x => x.Quantity); //Số lượng xuất var soLuongXuat = productOutboundRepository.GetAllvwProductOutboundDetailByProductId(item.ProductId) .Where(x => x.IsArchive == true && x.WarehouseSourceId == physicalInventory.WarehouseId).Sum(x => x.Quantity); //Cập nhật lại item.QuantityInInventory = (soLuongNhap - soLuongXuat); item.QuantityDiff = item.QuantityRemaining - item.QuantityInInventory; PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item); } //Tách dữ liệu cần nhập/xuất kiểm kê List <ProductOutboundDetail> outboundDetails = new List <ProductOutboundDetail>(); List <ProductInboundDetail> inboundDetails = new List <ProductInboundDetail>(); listDetail = PhysicalInventoryRepository.GetAllPhysicalInventoryDetail(Id).Where(x => x.QuantityInInventory != x.QuantityRemaining).ToList(); foreach (var item in listDetail) { var product = ProductRepository.GetProductById(item.ProductId); if (item.QuantityDiff < 0) //Chênh lệch dương thì thuộc về xuất { outboundDetails.Add( new ProductOutboundDetail { IsDeleted = false, CreatedDate = DateTime.Now, CreatedUserId = Helpers.Common.CurrentUser.Id, ModifiedDate = DateTime.Now, ModifiedUserId = Helpers.Common.CurrentUser.Id, Price = product.PriceOutbound, ProductId = product.Id, Quantity = Math.Abs(item.QuantityDiff), } ); } else if (item.QuantityDiff > 0) //Chênh lệch âm thì thuộc về nhập { inboundDetails.Add( new ProductInboundDetail { IsDeleted = false, CreatedDate = DateTime.Now, CreatedUserId = Helpers.Common.CurrentUser.Id, ModifiedDate = DateTime.Now, ModifiedUserId = Helpers.Common.CurrentUser.Id, Price = product.PriceInbound.Value, ProductId = product.Id, Quantity = Math.Abs(item.QuantityDiff), } ); } } //Cập nhật phiếu xuất if (outboundDetails.Count != 0) { var outbound = productOutboundRepository.GetAllProductOutbound().Where(item => item.Code == physicalInventory.ProductOutboundCode).FirstOrDefault(); if (outbound != null) { //Xóa chi tiết xuất cũ var outboundDetails_old = productOutboundRepository.GetAllProductOutboundDetailByOutboundId(outbound.Id).Select(item => item.Id).ToList(); foreach (var item in outboundDetails_old) { productOutboundRepository.DeleteProductOutboundDetail(item); } //Thêm chi tiết xuất mới foreach (var item in outboundDetails) { item.ProductOutboundId = outbound.Id; item.IsDeleted = false; item.CreatedUserId = WebSecurity.CurrentUserId; item.ModifiedUserId = WebSecurity.CurrentUserId; item.CreatedDate = DateTime.Now; item.ModifiedDate = DateTime.Now; productOutboundRepository.InsertProductOutboundDetail(item); } //Cập nhật tham chiếu trong chi tiết kiểm kê foreach (var item in listDetail.Where(x => x.QuantityDiff < 0)) { item.ModifiedDate = DateTime.Now; item.ModifiedUserId = Helpers.Common.CurrentUser.Id; item.ReferenceVoucher = outbound.Code; PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item); } } } //Cập nhật phiếu nhập if (inboundDetails.Count != 0) { var inbound = productInboundRepository.GetAllProductInbound().Where(item => item.Code == physicalInventory.ProductInboundCode).FirstOrDefault(); //Xóa chi tiết nhập cũ var inboundDetails_old = productInboundRepository.GetAllProductInboundDetailByInboundId(inbound.Id).Select(item => item.Id).ToList(); foreach (var item in inboundDetails_old) { productInboundRepository.DeleteProductInboundDetail(item); } //Thêm chi tiết phiếu nhập foreach (var item in inboundDetails) { item.ProductInboundId = inbound.Id; item.IsDeleted = false; item.CreatedUserId = WebSecurity.CurrentUserId; item.ModifiedUserId = WebSecurity.CurrentUserId; item.CreatedDate = DateTime.Now; item.ModifiedDate = DateTime.Now; productInboundRepository.InsertProductInboundDetail(item); } //Cập nhật tham chiếu trong chi tiết kiểm kê foreach (var item in listDetail.Where(x => x.QuantityDiff > 0)) { item.ModifiedDate = DateTime.Now; item.ModifiedUserId = Helpers.Common.CurrentUser.Id; item.ReferenceVoucher = inbound.Code; PhysicalInventoryRepository.UpdatePhysicalInventoryDetail(item); } } TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.Success; return(RedirectToAction("Detail", new { Id = physicalInventory.Id })); }
public ActionResult Create(RePayPointsViewModel model) { if (ModelState.IsValid && model.DetailList.Count != 0) { RePayPoints rePayPoints = null; if (model.Id > 0) { rePayPoints = RePayPointsService.Get(model.Id); } if (rePayPoints != null) { #region Cập nhật //Nếu đã ghi sổ rồi thì không được sửa if (rePayPoints.IsArchive == true) { TempData[Globals.FailedMessageKey] = "Đã ghi sổ. không được chỉnh sửa"; return(RedirectToAction("Detail", new { Id = rePayPoints.Id })); } //Kiểm tra xem nếu có xuất kho rồi thì return var checkProductOutbound = ProductOutboundRepository.GetAllProductOutbound() .Where(item => item.Type == ProductOutboundType.Gift && item.PayPointId == rePayPoints.Id).FirstOrDefault(); if (checkProductOutbound != null) { TempData[Globals.FailedMessageKey] = "Phiếu xuất kho đã có!"; return(RedirectToAction("Detail", new { Id = rePayPoints.Id })); } AutoMapper.Mapper.Map(model, rePayPoints); rePayPoints.ModifiedUserId = WebSecurity.CurrentUserId; rePayPoints.ModifiedDate = DateTime.Now; rePayPoints.Status = RePayPointsStatus.Pending; rePayPoints.BranchId = Helpers.Common.CurrentUser.BranchId; rePayPoints.IsArchive = false; RePayPointsService.Update(rePayPoints); //Xóa chi tiết cũ và thêm chi tiết mới var listDetail_old = RePayPointsDetailService.GetRePayPointsDetailByPayPointId(rePayPoints.Id).ToList(); foreach (var item in listDetail_old) { RePayPointsDetailService.Delete(item); } foreach (var item in model.DetailList) { RePayPointsDetail rePayPointsDetail = new RePayPointsDetail(); AutoMapper.Mapper.Map(item, rePayPointsDetail); rePayPointsDetail.RePayPointId = rePayPoints.Id; rePayPointsDetail.IsDeleted = false; rePayPointsDetail.CreatedUserId = WebSecurity.CurrentUserId; rePayPointsDetail.ModifiedUserId = WebSecurity.CurrentUserId; rePayPointsDetail.CreatedDate = DateTime.Now; rePayPointsDetail.ModifiedDate = DateTime.Now; RePayPointsDetailService.Create(rePayPointsDetail); } #endregion } else// Thêm mới { #region MyRegion rePayPoints = new RePayPoints(); AutoMapper.Mapper.Map(model, rePayPoints); rePayPoints.IsDeleted = false; rePayPoints.CreatedUserId = WebSecurity.CurrentUserId; rePayPoints.ModifiedUserId = WebSecurity.CurrentUserId; rePayPoints.CreatedDate = DateTime.Now; rePayPoints.ModifiedDate = DateTime.Now; rePayPoints.Status = RePayPointsStatus.Pending; rePayPoints.BranchId = Helpers.Common.CurrentUser.BranchId; rePayPoints.IsArchive = false; RePayPointsService.Create(rePayPoints); //Cập nhật lại mã xuất kho rePayPoints.Code = Erp.BackOffice.Helpers.Common.GetOrderNo("RepayPoints", model.Code); RePayPointsService.Update(rePayPoints); Erp.BackOffice.Helpers.Common.SetOrderNo("RepayPoints"); foreach (var item in model.DetailList) { RePayPointsDetail rePayPointsDetail = new RePayPointsDetail(); AutoMapper.Mapper.Map(item, rePayPointsDetail); rePayPointsDetail.IsDeleted = false; rePayPointsDetail.CreatedUserId = WebSecurity.CurrentUserId; rePayPointsDetail.ModifiedUserId = WebSecurity.CurrentUserId; rePayPointsDetail.CreatedDate = DateTime.Now; rePayPointsDetail.ModifiedDate = DateTime.Now; rePayPointsDetail.RePayPointId = rePayPoints.Id; RePayPointsDetailService.Create(rePayPointsDetail); } //Thêm vào quản lý chứng từ TransactionController.Create(new TransactionViewModel { TransactionModule = "RePayPoints", TransactionCode = rePayPoints.Code, TransactionName = "Trả điểm" }); #endregion } //Tạo phiếu nhập, nếu là tự động string sale_tu_dong_tao_chung_tu = Erp.BackOffice.Helpers.Common.GetSetting("sale_auto_outbound"); if (sale_tu_dong_tao_chung_tu == "true") { ProductOutboundViewModel productOutboundViewModel = new ProductOutboundViewModel(); // var warehouseDefault = WarehouseRepository.GetAllWarehouse().Where(x => x.BranchId == Helpers.Common.CurrentUser.BranchId && x.IsSale == true).FirstOrDefault(); //Nếu trong đơn hàng có sản phẩm thì xuất kho if (model.WarehouseSourceId != null) { productOutboundViewModel.PayPointId = rePayPoints.Id; productOutboundViewModel.PayPointCode = rePayPoints.Code; productOutboundViewModel.WarehouseSourceId = model.WarehouseSourceId; productOutboundViewModel.Note = "Xuất kho cho đơn trả điểm " + rePayPoints.Code; // lấy các danh sách không Xuất kho var productNoInbound = ProductRepository.GetAllProduct().Where(x => x.Type == ProductType.Gift && x.NoInbound == true).ToList(); //Lấy dữ liệu cho chi tiết productOutboundViewModel.DetailList = model.DetailList.Where(x => !productNoInbound.Any(y => y.Id == x.GiftId)).Select(x => new ProductOutboundDetailViewModel { ProductId = x.GiftId, Quantity = x.Quantity, Price = 0, Unit = x.Unit, }).ToList(); var productOutbound = ProductOutboundController.CreateFromPayPoint(ProductOutboundRepository, productOutboundViewModel, rePayPoints.Code, TempData); PostController.SavePost(rePayPoints.Id, "RePayPoints", "Xuất kho trả điểm (" + productOutbound.Code + ")"); } //Ghi sổ chứng từ bán hàng model.Id = rePayPoints.Id; Archive(model); } return(RedirectToAction("Detail", new { Id = rePayPoints.Id })); } return(View(model)); }