示例#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);
        }
示例#2
0
        public IEnumerable <Stock> AddStocks(int itemId, IEnumerable <Stock> stocks)
        {
            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();
            var stockList     = stocks as IList <Stock> ?? stocks.ToList();

            if (!stockList.Any(x => x.IsParent))
            {
                stockList.First().IsParent = true;
            }

            var stockRepo   = new StockRepository(Db);
            var addedStocks = stockRepo.AddMany(stockList).ToList();

            foreach (var stock in addedStocks)
            {
                MoveStock(new Movement {
                    StockId = stock.Id, ToLocationId = storeLocation.Id
                });
            }

            return(addedStocks);
        }
示例#3
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));
        }
示例#4
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));
        }
示例#5
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);
        }