How billing works
Invoices are generated in two contexts:- Order billing — triggered automatically after a ticket sale completes. The
process_order_billingfunction enriches the order with purchaser and client data, then emits an invoice. The resultingbilling_infoobject is written back to theordersdocument. - Expense billing — triggered manually via
generateBillingFromExpenses. Used to issue invoices for operational expenses (e.g. production costs, logistics) tied to an event. The result is stored independently in theevent_invoicescollection.
What data goes into an invoice
Each invoice is built from three sources:| Source | Data used |
|---|---|
| Order / expense record | Ticket quantities, zones, amounts, currency, payment methods |
u_clients document | Buyer identity (TipoIdentificacion, NumeroIdentificacion, RazonSocial, address, email) |
data/exchange_rates | USD → BSD conversion rate applied to all monetary fields |
data/invoice_counter document. This guarantees no two invoices share the same number, even under concurrent load.
Tax calculations
Every invoice includes Venezuelan taxes computed from the order totals:- IVA (16%) — applied to the pre-tax subtotal on all invoices.
- IGTF (3%) — applied when payment was made in foreign currency (Zelle, Efectivo Dólares, or Efectivo Divisa). The IGTF base is the lesser of the total-with-IVA or the amount paid in foreign currency.
TotalesOtraMoneda block carrying the equivalent USD amounts at the recorded exchange rate.
What is stored after a successful invoice
For expense invoices, a document is created inevent_invoices/{numeroDocumento} with:
| Field | Description |
|---|---|
invoice_id | The numeroDocumento returned by TFHKA |
event_id | The event the invoice belongs to |
client_id | The client derived from the event |
expense_type | The expense category passed in the request |
currency | The currency of the expense (USD or BSD) |
billed_items | Array of expense line items |
created_at | Server timestamp |
api_response | Full raw response from the TFHKA API |
status | Always "EMITTED" on successful creation |
orders/{order_id} under the billing_info field.
The TFHKA integration currently points to the demo environment (
demoemisionv2.thefactoryhka.com.ve). Update BASE_URL, USER, and PASSWORD in billing.js before going to production.Subsections
Digital Invoicing
Complete reference for
billing_emision and generateBillingFromExpenses, the internal invoice generation flow, the full DocumentoElectronico structure, and Firestore storage details.