BIR E-invoice
High-throughput financial compliance platform built on Laravel 11 that consumes marketplace orders via Shopee Open API v2, computes exact 12% Philippine VAT distributions, prevents invoice numbering collisions via pessimistic database locks, and queues thermal print jobs.
From Commerce Store to Compliance Engine
Originally initiated as an e-commerce platform (Haru-main), the codebase evolved significantly into BIR E-invoice. While the underlying e-commerce catalog modules were retained, the system was re-architected on Laravel 11.56 with specialized compliance domains: an automated Shopee Open API v2 adapter, a Philippine 12% VAT tax calculation engine, asynchronous PDF generator jobs, and raw ESC/POS thermal printer hardware streaming.
The Concurrency & Compliance Dilemma
Philippine enterprises selling on major marketplaces like Shopee must comply with Bureau of Internal Revenue (BIR) Electronic Invoicing System (EIS) regulations. Under high-volume promotional flash sales (such as 11.11 or payday sales), hundreds of orders settle simultaneously within seconds.
Standard auto-increment or naive optimistic locking fails under this concurrency: two workers attempting to generate the next invoice series simultaneously either crash with unique key constraint violations, issue duplicate invoice numbers, or create gaps in the government-audited series—all of which carry severe regulatory penalties.
Pessimistic Invoice Sequence Locking
To eliminate numbering race conditions and ensure 100% gapless sequential official invoice series, BIR E-invoice executes sequence allocation within an isolated database transaction using MySQL row-level exclusive locks:
// Strict pessimistic sequence locking in Laravel 11
return DB::transaction(function () use ($order, $taxSnapshot) {
// 1. Acquire exclusive lock on the specific active fiscal series
$sequence = InvoiceSequence::where('series_code', 'BIR-2026')
->where('is_active', true)
->lockForUpdate()
->firstOrFail();
// 2. Compute the deterministic next sequential number
$nextNumber = $sequence->current_number + 1;
$sequence->update(['current_number' => $nextNumber]);
// 3. Issue the immutable official invoice with cryptographic snapshot
return Invoice::create([
'invoice_number' => $sequence->prefix . str_pad($nextNumber, 8, '0', STR_PAD_LEFT),
'order_id' => $order->id,
'gross_amount' => $taxSnapshot['gross_total'],
'vatable_sales' => $taxSnapshot['vatable_sales'],
'vat_amount' => $taxSnapshot['vat_amount'],
'snapshot_hash' => hash('sha256', json_encode($taxSnapshot)),
'issued_at' => now(),
]);
});
Because tax rules or entity thresholds may adjust across fiscal years, past invoices cannot be retroactively recalculated. BIR E-invoice serializes the exact rule version, tax rates (12% VAT), and item classifications at the millisecond of issuance, sealing the record with an immutable SHA-256 fingerprint.
The Order-to-Invoice Lifecycle
End-to-end execution pipeline from marketplace ingress to physical ESC/POS thermal printing. Engineered with strict transactional boundary isolation, versioned tax formulas, and pessimistic MySQL sequence locks:
Marketplace Order-to-Compliance Execution Lifecycle
Shopee Open API v2 Ingress → Deterministic 12% VAT Engine → DB Pessimistic Sequence Lock → ESC/POS Thermal Print
Integrity & Compliance Outcomes
By enforcing database locking, decoupling marketplace webhooks into asynchronous background queues, and implementing direct ESC/POS hardware spoolers, BIR E-invoice delivers an enterprise-grade compliance foundation that eliminates human invoicing errors and withstands peak concurrency surges.