Exemple #1
0
        public override Movement Add(Movement entity)
        {
            var stockRepo    = new StockRepository(Db);
            var locationRepo = new LocationRepository(Db);

            if (stockRepo.Find(entity.StockId) == null)
            {
                throw new EntityNotFoundException("Cannot find Stock with given ID");
            }

            if (locationRepo.Find(entity.ToLocationId) == null)
            {
                throw new EntityNotFoundException("Cannot find Location with given ID");
            }

            return(base.Add(entity));
        }
Exemple #2
0
        public Stock AddStock(int itemId, Stock stock)
        {
            var item = Find(itemId);

            if (item == null)
            {
                throw new EntityNotFoundException("Cannot find Item with given ID");
            }

            var locationRepo  = new LocationRepository(Db);
            var storeLocation = locationRepo.GetOrCreateStoreLocation();

            stock.LocationId = storeLocation.Id;

            var stockRepo = new StockRepository(Db);

            return(stockRepo.Add(stock));
        }
Exemple #3
0
        public Movement MoveStock(Movement movement)
        {
            var locationRepo = new LocationRepository(Db);
            var movementRepo = new MovementRepository(Db);
            var stockRepo    = new StockRepository(Db);

            var stock = stockRepo.Find(movement.StockId);

            if (stock == null)
            {
                throw new EntityNotFoundException("Cannot find Stock with given ID");
            }

            var location = locationRepo.Find(movement.ToLocationId);

            if (location == null)
            {
                throw new EntityNotFoundException("Cannot find Location with given ID");
            }

            stock.LocationId = location.Id;
            stockRepo.Update(stock);

            // TODO: Optimize!
            var lastKnownMovt = movementRepo.All.Where(x => x.StockId == movement.StockId).ToList().LastOrDefault();

            if (lastKnownMovt != null)
            {
                movement.FromLocationId = lastKnownMovt.ToLocationId;
            }

            movement.DateCreated = DateTime.UtcNow;

            movementRepo.Add(movement);
            Db.SaveChanges();

            return(movement);
        }