WORK / SYS-02: BIR E-INVOICE
ACTIVE DEVELOPMENT FOUNDATION FINANCIAL COMPLIANCE & COMMERCE 2025 — 2026

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.

ROLE
Lead Full-Stack Systems Architect & Developer
FRAMEWORK
Laravel 11.56 · PHP 8.2+
INTEGRATIONS
Shopee API v2 · BIR EIS · ESC/POS
CONCURRENCY GUARD
Pessimistic Sequence Locking
01 / ARCHITECTURAL EVOLUTION

From Commerce Store to Compliance Engine

VERIFIED REPOSITORY CONTEXT

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.

02 / REGULATORY & TRANSACTIONAL FRICTION

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.

03 / ENGINEERING DEEP-DIVE

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:

CONCURRENT SEQUENCE ALLOCATION (MySQL lockForUpdate)
// 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(),
    ]);
});
CRYPTOGRAPHIC TAX SNAPSHOT INTEGRITY

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.

SPECIFICATION SUMMARY
Framework: Laravel 11.56
Language: PHP 8.2+ Strict
Marketplace: Shopee Open API v2
Tax Engine: Philippine VAT 12%
Concurrency: lockForUpdate()
Hardware: ESC/POS 80mm/58mm
NEXT SYSTEM BRIEF

Flexzone

Gym management platform, Xendit payment billing, and hardware-bridged QR scanning kiosk.

VIEW FLEXZONE →
04 / PIPELINE TOPOLOGY

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:

04 / PIPELINE TOPOLOGY // EXECUTION SPECIFICATION

Marketplace Order-to-Compliance Execution Lifecycle

Shopee Open API v2 Ingress → Deterministic 12% VAT Engine → DB Pessimistic Sequence Lock → ESC/POS Thermal Print

01 / SHOPEE INGRESS
INTEGRATION
Open API v2
HMAC-SHA256 Sig
IDEMPOTENCY GUARD
Payload Deduping
Prevents duplicate order processing
INGEST WORKER
Laravel Queue
ProcessOrderJob
02 / TAX CALCULATION
PHILIPPINE TAX RULES
12% VAT Engine
Net of VAT Breakdown
RULE VERSIONING
Tax Rule Schema
Versioned tax formulas for immutable audit trail
CRYPTOGRAPHIC AUDIT
SHA-256 Snapshot
Non-repudiation seal
03 / CONCURRENCY LOCK ● CRITICAL
EXCLUSIVE ROW LOCK
lockForUpdate()
Locks series counter row during concurrent save
ZERO GAPS & COLLISIONS
Atomic Series Increment
Guaranteed 100% continuous BIR invoice series
TRANSACTION ISOLATION
DB::transaction(fn)
04 / EIS SUBMISSION
BIR EIS STANDARDS
Official Invoice
Compliant JSON schema payload generation
ADAPTER PATTERN
Mock / Sandbox / Live
Decoupled adapters with automatic exponential backoff
● READY FOR TRANSMIT
05 / PRINT
ESC/POS PROTOCOL
Raw Bytes
80mm & 58mm Thermal Command Stream
HARDWARE TRANSPORT
TCP Socket
Warehouse print spooling daemon
● FAST PRINT
PHASE 01 // INGRESS
1. Shopee Order Ingest
Consumes marketplace webhook payloads, verifies HMAC-SHA256 headers, and deduplicates order IDs.
PHASE 02 // COMPLIANCE
2. Tax Engine & Classification
Parses individual line items into Vatable Sales, VAT-Exempt, and Zero-Rated Sales, calculating the precise 12% output VAT.
PHASE 03 // INTEGRITY
3. Pessimistic Sequence Lock
Exclusive database row lock reserves official invoice number and commits the signed fiscal snapshot.
PHASE 04 // DISPATCH
4. ESC/POS Print Dispatch
Generates customer-facing PDF invoices via DomPDF while streaming binary ESC/POS commands directly to warehouse 80mm/58mm thermal printers.
05 / ENGINEERING IMPACT

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.

NEXT SYSTEM BRIEF

Flexzone

Gym management platform, Xendit payment billing, and hardware-bridged QR scanning kiosk.