/ Cases / Restaurants / System

A custom ERP: the central warehouse moved off Loyverse

Purchasing, receiving matched against the invoice, location orders with approval, blind counts, waste, production, QR-tagged assets and costing from electronic invoices.

/ Restaurantes Atitlán/ 2026.10/ ≈108 server functions · immutable inventory ledger

≈108

server functions

Oct 2026

warehouse off Loyverse

0

ledger edits: only new entries

/ The problem

The central warehouse supplied every location using a point of sale built for coffee shops. There was no receiving against invoices, no reliable counts and no real cost of what went out.

/ What we built

  • Its own catalog and an inventory ledger that can't be edited: every movement is a new entry.
  • Purchasing with receiving matched against the supplier's invoice; cost comes from the electronic invoice received.
  • Location orders with approval and dispatch; blind counts and recorded waste.
  • Production and pastry, QR-tagged assets and a SHA-256 verification chain.
  • Per-person PIN access and authorized devices; alerts on Telegram.

/ How it works

  1. 01Supplier invoices (FEL)
  2. 02Receiving against the invoice
  3. 03Inventory ledger
  4. 04Approved location order
  5. 05Dispatch and real cost

/ By the numbers

Inventory movements per week

■ Real production data · counts per week, no amounts or personal data · Since launch (Oct 12); current week excluded.

/ Real code

supabase/migrations/20261016012354_bodega_mayor_inmutable.sqlsql
-- ── A. Guardas del mayor ───────────────────────────────────────────────────
-- M2. Las guardas son security INVOKER a propósito: así current_user es quien escribe de verdad
-- (dentro de la RPC, su dueño; por PostgREST, service_role). Una marca falsificada desde la API
-- no alcanza. El chequeo va escrito en cada trigger (no en una función aparte) para que el rol de
-- la API no necesite EXECUTE de nada para recibir el mensaje claro.
create or replace function private.bodega_mayor_solo_por_rpc()
returns trigger language plpgsql security invoker set search_path = '' as $$
declare
  v_rpc boolean := coalesce(current_setting('bodega.mayor_rpc', true), '') = '1'
                   and current_user not in ('service_role', 'anon', 'authenticated', 'authenticator');
begin
  if tg_op = 'DELETE' then
    raise exception 'mayor_inmutable: un movimiento no se borra; se revierte con otro movimiento' using errcode = 'P0001';
  end if;
  if not v_rpc then
    raise exception 'mayor_inmutable: el mayor sólo cambia con bodega_asentar_movimiento' using errcode = 'P0001';
  end if;
  if tg_op = 'UPDATE' and (to_jsonb(new) - 'saldo_despues') is distinct from (to_jsonb(old) - 'saldo_despues') then
    raise exception 'mayor_inmutable: de un movimiento sólo se completa el saldo después' using errcode = 'P0001';
  end if;
  return new;
end $$;
-- …
create trigger bodega_movimientos_solo_por_rpc before insert or update or delete on public.bodega_movimientos
  for each row execute function private.bodega_mayor_solo_por_rpc();

■ The inventory ledger is immutable in the database itself: a movement is never deleted or edited, only reversed by another, and not even the API service key can bypass it.

/ Stack

AstroReactCloudflare WorkersWorkers AIPostgresRPC security definerTelegram