Invoices: issued in Billingo by the worker, downloadable on Számláim
A paid checkout queues an invoice row with every invoice detail (buyer, tax type and number, item, net, VAT, gross). The worker's new 'invoices' task creates the Billingo partner and the paid card invoice and e-mails it (API v3), saving each step so a retry never issues a second invoice, and keeps Billingo's whole answer; the PDF is fetched from Billingo when the user downloads it from the new Számláim page (in the menu once there is an invoice). Payments and invoices outlive a deleted account. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -19,17 +19,55 @@ CREATE TABLE stripe_checkouts (
|
||||
payment_intent text,
|
||||
-- What Stripe charged, in forints.
|
||||
amount_huf integer,
|
||||
-- Stripe's customer_details: name, e-mail, address, and for a company
|
||||
-- its name and tax ids.
|
||||
customer jsonb,
|
||||
-- The invoice (a background job issues it): pending → issued, or failed
|
||||
-- after retries (an admin looks at it).
|
||||
invoice_status text CHECK (invoice_status IN ('pending', 'issued', 'failed')),
|
||||
invoice_attempts integer NOT NULL DEFAULT 0,
|
||||
invoice_error text,
|
||||
invoice_number text,
|
||||
invoiced_at timestamptz
|
||||
-- Stripe's customer_details as it came: name, e-mail, address, and for a
|
||||
-- company its name and tax ids.
|
||||
customer jsonb
|
||||
);
|
||||
|
||||
CREATE INDEX stripe_checkouts_user_idx ON stripe_checkouts (user_id, created_at DESC);
|
||||
CREATE INDEX stripe_checkouts_invoice_idx ON stripe_checkouts (completed_at) WHERE invoice_status = 'pending';
|
||||
|
||||
-- One invoice per paid checkout, issued in Billingo by the worker. Every
|
||||
-- invoice detail is kept here (the buyer and the item as invoiced, Billingo's
|
||||
-- whole answer); the PDF itself is fetched from Billingo when downloaded.
|
||||
CREATE TABLE invoices (
|
||||
id uuid PRIMARY KEY,
|
||||
checkout_id text NOT NULL UNIQUE REFERENCES stripe_checkouts ON DELETE RESTRICT,
|
||||
user_id uuid REFERENCES users ON DELETE SET NULL,
|
||||
-- pending → issued; failed after the last retry (an admin looks at it).
|
||||
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'issued', 'failed')),
|
||||
attempts integer NOT NULL DEFAULT 0,
|
||||
next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_error text,
|
||||
-- The buyer as invoiced.
|
||||
buyer_name text NOT NULL,
|
||||
buyer_email text,
|
||||
buyer_country text NOT NULL,
|
||||
buyer_post_code text NOT NULL,
|
||||
buyer_city text NOT NULL,
|
||||
buyer_address text NOT NULL,
|
||||
-- Billingo's partner tax type: HAS_TAX_NUMBER, NO_TAX_NUMBER or FOREIGN.
|
||||
buyer_tax_type text NOT NULL CHECK (buyer_tax_type IN ('HAS_TAX_NUMBER', 'NO_TAX_NUMBER', 'FOREIGN')),
|
||||
buyer_tax_number text,
|
||||
-- The single item, gross price with 27% VAT.
|
||||
item_name text NOT NULL,
|
||||
gross_huf integer NOT NULL CHECK (gross_huf > 0),
|
||||
net_huf integer NOT NULL,
|
||||
vat_huf integer NOT NULL,
|
||||
vat_rate text NOT NULL DEFAULT '27%',
|
||||
payment_method text NOT NULL DEFAULT 'online_bankcard',
|
||||
-- From Billingo.
|
||||
billingo_partner_id bigint,
|
||||
billingo_document_id bigint UNIQUE,
|
||||
invoice_number text,
|
||||
invoice_date date,
|
||||
fulfillment_date date,
|
||||
due_date date,
|
||||
document jsonb,
|
||||
-- When Billingo e-mailed it to the buyer.
|
||||
sent_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
issued_at timestamptz
|
||||
);
|
||||
|
||||
CREATE INDEX invoices_user_idx ON invoices (user_id, created_at DESC);
|
||||
CREATE INDEX invoices_due_idx ON invoices (next_attempt_at) WHERE status = 'pending';
|
||||
|
||||
Reference in New Issue
Block a user