Exemplo n.º 1
0
        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 }));
        }
Exemplo n.º 2
0
        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 }));
        }