Example #1
0
        public Movement MoveStock(int stockId, int locationId, string notes = "")
        {
            var locationRepo = new LocationRepository(Db);
            var movementRepo = new MovementRepository(Db);
            var stockRepo    = new StockRepository(Db);

            var stock = stockRepo.Find(stockId);

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

            var location = locationRepo.Find(locationId);

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

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

            var movement = new Movement {
                DateCreated  = DateTime.UtcNow,
                StockId      = stock.Id,
                ToLocationId = location.Id,
                Notes        = notes
            };

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

            return(movement);
        }
Example #2
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));
        }
Example #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);
        }