public ActionResult UpdateAll(string url)
        {
            string rs            = "";
            var    inventoryList = InventoryRepository.GetAllInventory().ToList();

            foreach (var item in inventoryList)
            {
                var warehouseId = item.WarehouseId.Value;
                var productId   = item.ProductId.Value;
                var inbound     = ProductInboundRepository.GetAllvwProductInboundDetail()
                                  .Where(x => x.IsArchive && x.ProductId == productId && x.WarehouseDestinationId == warehouseId)
                                  .Sum(x => x.Quantity);

                var outbound = ProductOutboundRepository.GetAllvwProductOutboundDetail()
                               .Where(x => x.IsArchive && x.ProductId == productId && x.WarehouseSourceId == warehouseId)
                               .Sum(x => x.Quantity);

                var inventory = (inbound == null ? 0 : inbound) - (outbound == null ? 0 : outbound);

                if (item.Quantity != inventory)
                {
                    rs           += "<br/>" + item.ProductId + " | " + item.Quantity + " => " + inventory;
                    item.Quantity = inventory;
                    InventoryRepository.UpdateInventory(item);
                }
            }

            TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.UpdateSuccess + rs;
            return(Redirect(url));
        }
        public static string Update(string productName, int productId, int warehouseId, int quantityIn, int quantityOut, bool isArchive = true)
        {
            string error = "";
            IProductInboundRepository  productInboundRepository  = DependencyResolver.Current.GetService <IProductInboundRepository>();
            IProductOutboundRepository productOutboundRepository = DependencyResolver.Current.GetService <IProductOutboundRepository>();
            IInventoryRepository       inventoryRepository       = DependencyResolver.Current.GetService <IInventoryRepository>();

            var inbound = productInboundRepository.GetAllvwProductInboundDetail()
                          .Where(x => x.IsArchive && x.ProductId == productId && x.WarehouseDestinationId == warehouseId);

            var outbound = productOutboundRepository.GetAllvwProductOutboundDetail()
                           .Where(x => x.IsArchive && x.ProductId == productId && x.WarehouseSourceId == warehouseId);

            var inventory = (inbound.Count() == 0 ? 0 : inbound.Sum(x => x.Quantity)) - (outbound.Count() == 0 ? 0 : outbound.Sum(x => x.Quantity)) + quantityIn - quantityOut;

            var inventoryCurrent = inventoryRepository.GetInventoryByWarehouseIdAndProductId(warehouseId, productId);

            //Sau khi thay đổi dữ liệu phải đảm bảo tồn kho >= 0
            if (inventory >= 0)
            {
                if (isArchive)
                {
                    if (inventoryCurrent != null)
                    {
                        if (inventoryCurrent.Quantity != inventory)
                        {
                            inventoryCurrent.Quantity = inventory;
                            inventoryRepository.UpdateInventory(inventoryCurrent);
                        }
                    }
                    else
                    {
                        inventoryCurrent                = new Inventory();
                        inventoryCurrent.IsDeleted      = false;
                        inventoryCurrent.CreatedUserId  = WebSecurity.CurrentUserId;
                        inventoryCurrent.ModifiedUserId = WebSecurity.CurrentUserId;
                        inventoryCurrent.CreatedDate    = DateTime.Now;
                        inventoryCurrent.ModifiedDate   = DateTime.Now;
                        inventoryCurrent.WarehouseId    = warehouseId;
                        inventoryCurrent.ProductId      = productId;
                        inventoryCurrent.Quantity       = inventory;
                        inventoryRepository.InsertInventory(inventoryCurrent);
                    }
                }
            }
            else
            {
                error += string.Format("<br/>Dữ liệu tồn kho của sản phẩm \"{0}\" là {1}: không hợp lệ!", productName, inventory);
            }

            return(error);
        }
Beispiel #3
0
        public ActionResult UpdateAll(string url)
        {
            string rs = "";

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    var inventoryList = InventoryRepository.GetAllInventory().ToList();
                    var inbound       = ProductInboundRepository.GetAllvwProductInboundDetail().ToList();
                    var outbound      = ProductOutboundRepository.GetAllvwProductOutboundDetail().ToList();
                    foreach (var item in inventoryList)
                    {
                        var warehouseId = item.WarehouseId.Value;
                        var productId   = item.ProductId.Value;

                        var _inbound = inbound.Where(x => x.IsArchive &&
                                                     x.ProductId == productId &&
                                                     x.WarehouseDestinationId == warehouseId &&
                                                     x.LoCode == item.LoCode &&
                                                     x.ExpiryDate.Value.ToShortDateString() == item.ExpiryDate.Value.ToShortDateString()
                                                     ).ToList().Sum(x => x.Quantity);

                        var _outbound = outbound.Where(x => x.IsArchive &&
                                                       x.ProductId == productId &&
                                                       x.WarehouseSourceId == warehouseId &&
                                                       x.LoCode == item.LoCode &&
                                                       x.ExpiryDate.Value.ToShortDateString() == item.ExpiryDate.Value.ToShortDateString()
                                                       ).ToList().Sum(x => x.Quantity);

                        var inventory = (_inbound == null ? 0 : _inbound) - (_outbound == null ? 0 : _outbound);

                        if (item.Quantity != inventory)
                        {
                            rs           += "<br/>" + item.ProductId + " | " + item.Quantity + " => " + inventory;
                            item.Quantity = inventory;
                            InventoryRepository.UpdateInventory(item);
                        }
                    }
                    scope.Complete();
                }
                catch (DbUpdateException)
                {
                    TempData[Globals.FailedMessageKey] = App_GlobalResources.Wording.Error;
                    return(Redirect(url));
                }
            }
            TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.UpdateSuccess + rs;
            return(Redirect(url));
        }