create or replace function random_cart_on_location(loc integer, lim integer)
  returns table(sku integer, unit_price numeric(9,2), item_id integer, discount integer)
language sql as
$$
  with
    items
  as
    (
      select
        id,
        random_available_on_location($1) as sku
      from
        generate_series(0, $2) id
    ),
  stock as
    (
    select
      sku,
      price_on_location(sku, $1) as unit_price,
      fetch_from_shelf(sku, $1) as item_id
    from
      items
    limit
      $2
    )
  select
    sku,
    unit_price,
    item_id,
    discount
  from
    stock
  join
    itemsonshelf
  on
    stock.item_id = itemsonshelf.id
    ;
$$;
