/ Cases / Restaurants / Finance
The invoice is built in a Telegram mini-app from the POS receipt, and a bot issues it with the SAT (Guatemala's tax authority) as FEL, the country's electronic invoice. Another bot pulls supplier invoices every hour for costing.
1 h
between purchase downloads
10 min
queue monitoring
/ The problem
Every invoice was typed by hand into the SAT's online portal, and supplier invoices were downloaded one by one. The SAT blocks common cloud networks.
/ What we built
/ How it works
/ By the numbers
■ Real production data · counts per month, no amounts or personal data · November through the 27th.
■ Real production data · counts per month, no amounts or personal data · November through the 27th.
/ Real code
create table if not exists public.fel_documentos (
origen text not null check (origen in ('telegram','admin','pos','loyverse')),
-- …
estado text not null default 'borrador'
check (estado in ('borrador','pendiente','emitiendo','certificada','rechazada','error','incierta','cancelada')),
-- …
constraint fel_documentos_origen_uq unique (origen, origen_id),
constraint fel_documentos_cf_tope check (receptor_tipo <> 'CF' or total < 2500)
);
-- …
-- El emisor toma UNA pendiente, la pasa a «emitiendo» y la devuelve. Si dos
-- emisores preguntan a la vez, cada uno recibe una distinta (skip locked).
create or replace function public.fel_tomar_siguiente()
returns setof public.fel_documentos
language sql
security invoker
set search_path = ''
as $$
update public.fel_documentos d
set estado = 'emitiendo', tomado_en = now(), intentos = d.intentos + 1, actualizado_en = now()
where d.id = (
select p.id from public.fel_documentos p
where p.estado = 'pendiente'
order by p.creado_en
for update skip locked
limit 1)
returning d.*;
$$;
■ The same sale is never invoiced twice (unique per source) and two robots never pick the same invoice (for update skip locked).
/ Stack