create or replace function stock_on_location(sku integer, loc integer)
  returns integer
language sql as
$$
  select
    -- Sum quantities from all the batches available in location
    sum(itemsonshelf.quantity)
  from
    itemsonshelf
  join
    "batches"
  on
    "batches".id = itemsonshelf.batch
  where
    "batches".sku = $1 and itemsonshelf.location = $2
$$
