public ActionResult Detail(int?Id) { var model = new RequestInboundViewModel(); var modelproductInbound = new ProductInboundViewModel(); var modelproductOutbound = new ProductOutboundViewModel(); var requestInbound = new vwRequestInbound(); var productInbound = new vwProductInbound(); var productOutbound = new vwProductOutbound(); if (Id != null && Id.Value > 0) { requestInbound = RequestInboundRepository.GetvwRequestInboundById(Id.Value); if (requestInbound.InboundId != null && requestInbound.InboundId.Value > 0) { productInbound = productInboundRepository.GetvwProductInboundById(requestInbound.InboundId.Value); AutoMapper.Mapper.Map(productInbound, modelproductInbound); modelproductInbound.CreatedUserName = userRepository.GetUserById(modelproductInbound.CreatedUserId.Value).FullName; } if (requestInbound.OutboundId != null && requestInbound.OutboundId.Value > 0) { productOutbound = productOutboundRepository.GetvwProductOutboundById(requestInbound.OutboundId.Value); AutoMapper.Mapper.Map(productOutbound, modelproductOutbound); modelproductOutbound.CreatedUserName = userRepository.GetUserById(modelproductOutbound.CreatedUserId.Value).FullName; } } ViewBag.productInbound = modelproductInbound; ViewBag.productOutbound = modelproductOutbound; AutoMapper.Mapper.Map(requestInbound, model); model.CreatedUserName = userRepository.GetUserById(requestInbound.CreatedUserId.Value).FullName; model.DetailList = RequestInboundRepository.GetAllvwRequestInboundDetailsByInvoiceId(requestInbound.Id).AsEnumerable().Select(x => new RequestInboundDetailViewModel { Id = x.Id, Price = x.Price, ProductId = x.ProductId, Quantity = x.Quantity, Unit = x.Unit, CategoryCode = x.CategoryCode, ProductName = x.ProductName, ProductCode = x.ProductCode, ProductGroup = x.ProductGroup, Manufacturer = x.Manufacturer, ProductBarCode = x.ProductBarCode, RequestInboundId = x.RequestInboundId, QuantityRemaining = x.QuantityRemaining, Image_Name = Erp.BackOffice.Helpers.Common.KiemTraTonTaiHinhAnh(x.Image_Name, "product-image-folder", "product") }).ToList(); model.GroupProduct = model.DetailList.GroupBy(x => new { x.ProductGroup }, (key, group) => new RequestInboundDetailViewModel { ProductGroup = key.ProductGroup, ProductId = group.FirstOrDefault().ProductId, Id = group.FirstOrDefault().Id }).ToList(); foreach (var item in model.GroupProduct) { if (!string.IsNullOrEmpty(item.ProductGroup)) { var ProductGroupName = categoryRepository.GetCategoryByCode("Categories_product").Where(x => x.Value == item.ProductGroup).FirstOrDefault(); item.ProductGroupName = ProductGroupName.Name; } } var user = userRepository.GetUserById(WebSecurity.CurrentUserId); ViewBag.User = user; return(View(model)); }
public ActionResult Detail(int?Id) { var model = new RequestInboundViewModel(); var modelproductInbound = new ProductInboundViewModel(); var modelproductOutbound = new ProductOutboundViewModel(); var requestInbound = new vwRequestInbound(); var productInbound = new vwProductInbound(); var productOutbound = new vwProductOutbound(); if (Id != null && Id.Value > 0) { requestInbound = RequestInboundRepository.GetvwRequestInboundById(Id.Value); if (requestInbound.InboundId != null && requestInbound.InboundId.Value > 0) { productInbound = productInboundRepository.GetvwProductInboundFullById(requestInbound.InboundId.Value); AutoMapper.Mapper.Map(productInbound, modelproductInbound); modelproductInbound.CreatedUserName = userRepository.GetUserById(modelproductInbound.CreatedUserId.Value).FullName; } if (requestInbound.OutboundId != null && requestInbound.OutboundId.Value > 0) { productOutbound = productOutboundRepository.GetvwProductOutboundFullById(requestInbound.OutboundId.Value); AutoMapper.Mapper.Map(productOutbound, modelproductOutbound); modelproductOutbound.CreatedUserName = userRepository.GetUserById(modelproductOutbound.CreatedUserId.Value).FullName; } } ViewBag.productInbound = modelproductInbound; ViewBag.productOutbound = modelproductOutbound; AutoMapper.Mapper.Map(requestInbound, model); model.CreatedUserName = userRepository.GetUserById(requestInbound.CreatedUserId.Value).FullName; model.DetailList = RequestInboundRepository.GetAllvwRequestInboundDetailsByInvoiceId(requestInbound.Id).AsEnumerable().Select(x => new RequestInboundDetailViewModel { Id = x.Id, Price = x.Price, ProductId = x.ProductId, Quantity = x.Quantity, Unit = x.Unit, CategoryCode = x.CategoryCode, ProductName = x.ProductName, ProductCode = x.ProductCode, ProductGroup = x.ProductGroup, Manufacturer = x.Manufacturer, ProductBarCode = x.ProductBarCode, RequestInboundId = x.RequestInboundId, QuantityRemaining = x.QuantityRemaining, ProductGroupName = x.ProductGroupName, Image_Name = x.Image_Name }).OrderBy(x => x.Id).ToList(); var warehouse = WarehouseRepository.GetAllWarehouse().Where(x => x.IsSale == true && x.BranchId == null).ToList(); if (warehouse.Count() > 0) { // var wh = warehouse.FirstOrDefault().Id; var productList = Domain.Helper.SqlHelper.QuerySP <InventoryViewModel>("spSale_Get_Inventory", new { WarehouseId = "", HasQuantity = "1", ProductCode = "", ProductName = "", CategoryCode = "", ProductGroup = "", BranchId = "", LoCode = "", ProductId = "", ExpiryDate = "" }); productList = productList.Where(id1 => warehouse.Any(id2 => id2.Id == id1.WarehouseId)).ToList(); foreach (var item in model.DetailList) { var quantity = productList.Where(x => x.ProductId == item.ProductId).Sum(x => x.Quantity); item.QuantityInventoryKT = quantity == null ? 0 : quantity; item.QuantityNotCondition = item.QuantityInventoryKT <= 0 ? 1 : 0; } } var ProductInvoiceList = SqlHelper.QuerySP <ProductInvoiceViewModel>("spSale_LiabilitiesDrugStore", new { StartDate = "", EndDate = "", branchId = model.BranchId, CityId = "", DistrictId = "" }).ToList(); //var inbound = productInboundRepository.GetAllProductInbound().Where(x => x.BranchId == model.BranchId&&x.IsArchive==true).Sum(x => x.TotalAmount); model.Liabilities = 0; var invoice = ProductInvoiceList.Where(x => x.AccountancyUserId == null).Sum(x => x.TotalAmount); model.Liabilities = invoice; model.MaxDebitAmount = branchRepository.GetBranchById(model.BranchId.Value).MaxDebitAmount; var tonkho = Domain.Helper.SqlHelper.QuerySP <InventoryViewModel>("spSale_Get_Inventory", new { WarehouseId = model.WarehouseDestinationId, HasQuantity = "1", ProductCode = "", ProductName = "", CategoryCode = "", ProductGroup = "", BranchId = "", LoCode = "", ProductId = "", ExpiryDate = "" }).ToList(); model.DinhMucKho = tonkho.Sum(x => x.Quantity * x.ProductPriceOutbound) + invoice; //var user = userRepository.GetUserById(WebSecurity.CurrentUserId); //ViewBag.User = user; return(View(model)); }
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)); }