Esempio n. 1
0
        public void AddSalesToStoreAndDecrementStockProducts(SaleDto saleDto)
        {
            var newSale = new Sale()
            {
                SellingDate = saleDto.SellingDate,
                StoreId     = saleDto.StoreId
            };

            _shoesDataEntities.Sales.Add(newSale);
            foreach (var saleProductDto in saleDto.SalesProducts)
            {
                var currentProduct = _productServices.FindProduct(
                    saleProductDto.ModelId, saleProductDto.ColorId, saleProductDto.Size);
                var newSaleProduct = new SaleProduct
                {
                    ProductId    = currentProduct.Id,
                    SaleId       = newSale.Id,
                    Quantity     = saleProductDto.Quantity,
                    SellingPrice = saleProductDto.Price
                };
                _shoesDataEntities.SaleProducts.Add(newSaleProduct);
                var lastStockRoom     = _storeStockRoomDataServices.GetLastStoreStockRoomByProductId(saleDto.StoreId.Value, currentProduct.Id);
                var newStoreStockRoom = new StoreStockRoom()
                {
                    ProductId     = currentProduct.Id,
                    StockValue    = lastStockRoom.StockValue - saleProductDto.Quantity,
                    EntryValue    = -saleProductDto.Quantity,
                    EntryDate     = saleDto.SellingDate,
                    StoreId       = saleDto.StoreId.Value,
                    OperationType = OperationType.OUT.ToString()
                };
                _shoesDataEntities.StoreStockRooms.Add(newStoreStockRoom);
                SaveChanges();
            }
        }
        public void SupplyStoreStockRoom(StoreStockRoomDto storeStockRoomDto)
        {
            if (storeStockRoomDto.ColorId == null)
            {
                throw new InvalidOperationException("Error: No enter new Store StockRoom if Color is null");
            }
            if (storeStockRoomDto.Size == null)
            {
                throw new InvalidOperationException("Error: No enter new Store StockRoom if Shoes Size is null");
            }

            var modelId        = storeStockRoomDto.ModelId;
            var colorId        = storeStockRoomDto.ColorId.Value;
            var size           = storeStockRoomDto.Size.Value;
            var currentProduct = _productServices.FindProduct(modelId, colorId, size);

            if (currentProduct == null)
            {
                throw new StockRoomOperationException("No Found a product item to supply Store StockRoom");
            }

            var lastStockRoom = _stockRoomDataServices.GetLastStockRoomByProductId(currentProduct.Id);

            if (lastStockRoom == null)
            {
                throw new StockRoomOperationException("No Found any stock room to execute the operation");
            }
            if (lastStockRoom.StockValue < storeStockRoomDto.Quantity)
            {
                throw new StockRoomOperationException(
                          "It is not possible to move requested quantity to selected store");
            }

            var newStockRoom = new StockRoom()
            {
                ProductId     = lastStockRoom.ProductId,
                StockValue    = lastStockRoom.StockValue - storeStockRoomDto.Quantity,
                EntryDate     = storeStockRoomDto.DateOfSupplier,
                EntryValue    = -storeStockRoomDto.Quantity,
                StoreId       = storeStockRoomDto.StoreId,
                OperationType = OperationType.OUT.ToString()
            };

            _shoesDataEntities.StockRooms.Add(newStockRoom);
            var lastStoreStockRoom = GetLastStoreStockRoomByProductId(storeStockRoomDto.StoreId, currentProduct.Id);
            var storeStocks        = storeStockRoomDto.Quantity;

            if (lastStoreStockRoom != null)
            {
                storeStocks += lastStoreStockRoom.StockValue;
            }
            var newStoreStockRoom = new StoreStockRoom
            {
                ProductId     = currentProduct.Id,
                StockValue    = storeStocks,
                EntryDate     = storeStockRoomDto.DateOfSupplier,
                EntryValue    = storeStockRoomDto.Quantity,
                StoreId       = storeStockRoomDto.StoreId,
                OperationType = OperationType.IN.ToString()
            };

            _shoesDataEntities.StoreStockRooms.Add(newStoreStockRoom);
            SaveChanges();
        }