# Code structure and implementation analysis

The original project contained reference UI and empty controller/service shells. The canonical application now implements authentication, master CRUD, POS, purchasing/partial receipts, supplier payments, actual-input production, counts, waste, unpaid losses, public QR ordering and reports.

The first installation found no application tables. Reproducible migrations install 35 ERP tables plus CI4's migration table. No original business data was replaced; verification uses separate databases.

## Layers and persistence

Controllers handle HTTP and presentation. WorkspaceController provides user/settings and derives the actor from authentication. Config/Management allowlists shared CRUD definitions; domain services retain their own validation.

Services calculate values, acquire locks, own transactions and audit changes. Models use `$db->query($sql, $bindings)`, never Query Builder or CI Model. CafeModel/CompositeModel enforce explicit writable fields; read-only reporting aggregates each ledger independently.

StockService requires a transaction, locks stock, posts a unique event and updates the cached balance. Requirements are processed by inventory ID. Nested transactions use savepoints so later shortages roll back earlier deductions.

Production stores actual inputs with movement IDs and recipe version. Receipt lines preserve purchase units, conversion, actual accepted cost/quantity and received value. Changes to recipes cannot rewrite past consumption. Supplier debt comes only from accepted goods.

Money is stored in integer minor units; quantities use DECIMAL(16,3) and BCMath. Names, prices, stock modes and currency are snapshotted on bills. Physical counts, food waste and unpaid losses remain distinct histories.

## Access, UI and QR

Individual hashed passwords, session regeneration, fresh permissions and CSRF protect mutations. One-time setup is serialized and local/development-only. Manager/Staff roles have explicit route permissions.

Table QR hashes validate public capabilities; encrypted values support reprinting. A different status capability exposes one order only. Customers cannot select an actor, price, table or approval outcome. Staff acceptance rechecks availability and keeps each request as a separate bill.

Photos accept bounded JPEG/PNG/WebP input, decode/re-encode JPEG, and use random filenames in private storage.

Shared Bootstrap 5.3.8 preserves the coral/peach/white palette, labels, focused errors and responsive layouts. CSS/JS are separate files. Development previews cannot submit business operations.

Reports distinguish billed value, served value, receipts, refunds, losses, stock waste and accepted supplier debt. Nairobi date ranges map to UTC query boundaries. Inventory balances reconcile with all-time ledger sums.

## Naming and boundaries

Active code, packages and Herd URL use cafe-erp. This chat still holds the original cafe-erp-ci4 working directory; that old copy remains until Codex is reopened in the canonical folder.

The single-café first-release workflows are implemented. Deployment, gateways, discounts/tax, posted-document corrections and custom role editors are future work. See IMPLEMENTATION-STATUS.md for verification evidence.
