Skip to content
webdmamun
All work

Multi-Branch Retail Operations Platform

A multi-branch retail operations platform: storefront, point of sale, branch inventory, attendance, commission and payroll, on one codebase and one database.

Built for
US Arabian Perfume
Industry
Fragrance retail, e-commerce and physical branches
Status
live2025

Built with Next.js, React, TypeScript, Tailwind CSS, PostgreSQL, Prisma, Auth.js, Square, Shippo, Resend, Cloudflare R2, Upstash Redis, Sentry, Vitest, Vercel

Visit the live system

The system in numbers

API route handlers
210
Page routes
72
Database models
64
Automated test files
146
Scheduled jobs
6
Product surfaces
4

Source: Repository and deployment audit of the live system, September 2026.

In this case study

The problem

US Arabian Perfume sells perfume oils and sprays online and across retail locations in New Jersey and New York. Both halves of the business share the same products, the same stock, the same staff and the same money, so they cannot be run as two systems that are reconciled afterwards.

Four questions have to be answered correctly, all day, every day.

What can actually be sold, and where. A perfume oil listed at 30 ml is not an object on a shelf. It is raw oil, decanted into an empty 30 ml bottle, handed over in a carry bag. A branch can hold half a litre of oil and be unable to sell a single bottle because the bottles have run out. Treat stock as one number and the counts are wrong on the first day.

Who sold it, and what are they owed. Staff earn an hourly rate plus commission above a threshold that moves with the day of the week, and the person operating the register is frequently not the person who made the sale.

Whether the money and the stock both moved. A card payment can succeed while the order fails. A till can lose connectivity after charging a customer.

Whether head office can see everything without getting in the way. Administrators need every location. A branch needs its own shop floor and nothing else. An employee needs their shift and their pay, on a phone, between customers.

What was needed

One platform holding a single consistent picture of products, stock, sales, staff and pay — across the website, the shop floor and head office — with the permission model to let each of those see only its own part of it.

What I built

One Next.js application over one PostgreSQL database, carrying four separate product surfaces.

The storefront handles catalogue, multi-size variants, cart, checkout through Square, destination-based United States sales tax and international shipping with label generation. The admin portal covers orders and fulfilment, the product catalogue, central inventory with alerting and forecasting, every branch, the workforce, the commission ledger and payroll. The branch portal gives each shop a deliberately narrow view of its own trading day: register shifts, point of sale, its own stock and its own staff. The employee application is mobile-first and installable, reducing the whole platform to four things a person needs between customers.

The rules live in a service layer rather than in route handlers, so a screen cannot compute something different from the scheduled job that pays it.

Who uses it

StorefrontCustomers
Catalogue, multi-size variants, cart and Square checkout, with destination-based sales tax and international shipping.
AdminHead office
Orders, catalogue, central inventory, every branch, the workforce, the commission ledger and payroll.
BranchShop floor
Register shifts, the till, this branch’s own stock and staff — and nothing belonging to any other branch.
EmployeeStaff, on a phone
Four things: the shift clock, commission with its workings, outstanding pay, and attendance history.

What it does

  • Storefront and checkout

    Catalogue with multi-size variants and sale windows, cart, Square checkout, destination-based US sales tax and international shipping.

  • Point of sale

    A till that will not open without a register shift, prices and tax derived on the server, and a per-branch price adjustment that never alters the catalogue.

  • Composite inventory

    Raw oils, bottles, carry bags, testers and finished sprays as typed inventory, held and counted per branch.

  • Branch operations

    Register shifts with cash reconciliation, branch-scoped reporting, and stock requests raised against live central availability.

  • Attendance and shifts

    Session-based clock in and out with breaks, branch attribution, and a two-sided correction workflow.

  • Commission and payroll

    A daily commission ledger under per-branch, effective-dated rules, with approval, and payroll settled on demand in a single transaction.

  • Role separation

    Six roles gated at the network edge, again in the API layer, and again inside the database transaction.

  • Operational safety

    Audit logging, rate limiting, immediate session revocation and an administrator-controlled maintenance mode.

The storefront

Catalogue, multi-size variants, cart and Square checkout, with destination-based sales tax and international shipping.

The storefront — the part customers see, and the smallest part of the system.
One fragrance, four sizes, four stock-keeping units — the same variants the till and the warehouse are counting.

The admin portal

Orders, catalogue, central inventory, every branch, the workforce, the commission ledger and payroll.

Every commission figure shows its own arithmetic. Employee identities are redacted in this screenshot.
Every location is an addressable unit with its own stock, staff and rules.
Pay rules are configured per branch and dated — a change tomorrow cannot move what someone already earned today.
What each person is owed right now — settled when the business chooses to pay, not on a calendar it does not follow.
A branch can charge more at its own till without forking the catalogue, and without rewriting a sale already recorded.
Branches request, the warehouse approves — stock moves on a record, not a phone call.

The branch portal

Register shifts, the till, this branch’s own stock and staff — and nothing belonging to any other branch.

Having the oil is not having the product: 500 ml in stock still reads as not sellable when the matching bottles have run out.
The branch portal answers one question — what is happening in this shop today — and is not permitted to answer any other.
Every shift closes with a number that either matches or does not, and a name against both ends of it.
The till refuses to operate unsafely: no open register shift, no sale — and a sale can only be credited to staff clocked in at that branch.

The employee app

Four things: the shift clock, commission with its workings, outstanding pay, and attendance history.

One button, one number.
The same calculation the approver sees.
What I am owed, and what my last payment covered.

The hard parts

A till that loses the network mid-sale

The cashier taps charge, the connection drops, and nobody knows whether the sale landed. Tapping again risks charging the customer twice and deducting stock twice; not tapping risks a customer leaving unbilled.

The till generates an identifier for the checkout before sending it. Inside the sale transaction the server takes a PostgreSQL advisory lock on that identifier, so a retry arriving while the first request is still committing waits rather than races it. It then looks for sales already recorded under that identifier: the same items, salesperson and payment method mean this is a retry, and the original receipt is returned from the committed rows rather than recalculated — so it still works even if the salesperson has clocked out since. The same identifier with a different basket is a conflict, not a retry, and is refused.

Idempotency was not a header added at the end. It is a decision about what an operation is, taken by the client before the operation happens.

A perfume oil is three kinds of stock at once

Selling one 30 ml bottle of oil consumes raw oil, an empty 30 ml bottle and often a carry bag, all belonging to that specific branch.

Inventory items are typed — raw material oil, packaging bottle, packaging bag, tester, finished spray. An oil variant links to both a bulk oil item and a bottle size, and a product cannot be created at all without that link, because the link is what makes volume-level deduction possible. A sale aggregates requirements per inventory item first, so two 15 ml lines become one 30 g claim rather than two competing ones, then deducts them inside the sale transaction using conditional updates so the database itself refuses to go below zero.

Sellability is therefore derived, not stored: a branch screen can honestly show 500 ml of oil in stock and still mark the product as not sellable, because the bottles have run out.

Payment captured, order creation failed

Online, a card payment can be captured successfully and the order can still fail to be created. The customer has paid and has nothing.

Before anything is created the server verifies the payment independently — not only its status, but that the captured amount matches the platform’s own authoritative total in the expected currency. Order creation and stock deduction then run in one bounded transaction. If the amount disagrees, or that transaction fails after capture, the server refunds immediately with a fresh idempotency key, records the failure with a snapshot of the checkout and cart, raises an error-monitoring event, emails an administrator and tells the customer plainly what happened.

The decision that mattered was persisting the snapshot. A refund that fails leaves a capture with no context; a stored record leaves something a person can actually resolve.

Commission that can be argued with, and won

An employee’s threshold for a day is their wages for that day — hours worked times their hourly rate — multiplied again by a branch-configured factor, four on weekdays and seven at weekends. Commission is a percentage of sales above that threshold. Every input can change after the fact, and an administrator can edit the rules.

So the ledger stores one row per employee, per branch, per branch-local business date, holding not just the answer but the inputs and the rules that produced it: hours, the rate applied, the multiplier applied, the percentage applied, ex-tax sales, and the commissionable amount. Recalculation is idempotent and keyed on that identity. Only pending rows recalculate — approved, paid and rejected rows are returned untouched, because recalculating them would silently rewrite a decision. Rules resolve by date, so editing a multiplier applies from the next branch-local day and an earlier pending day still uses the rules in force then.

Commission is credited to the salesperson, not the register operator, and calculated on merchandise revenue excluding tax. The employee’s phone and the approver’s ledger read from the same calculator, so they cannot disagree.

Payroll that refuses to guess

Paying someone touches the settlement record, the wage information explaining it, the commission rows it settles and the adjustments it absorbs. If some of that lands and some does not, a person is simultaneously paid and owed.

Settlement runs as a single transaction covering all four. The client sends who is being paid, through which date, and when the money moved — never an amount, because a figure that travels through a browser is a figure that can be edited. A request key makes confirmation idempotent, so a replay returns the original settlement rather than paying twice.

Where a historic attendance session predates wage-rate snapshots its value is genuinely unknown. The system reports those hours, declines to value them, and declines to pay the employee at all until they are resolved — paying the rest would mark someone settled while hours nobody can price stay unpaid. The missing rate has to be stated by an administrator, bounded by the same limits as any forward-looking rate change, recorded against the person who stated it, and written once.

Architecture

One Next.js application serves all four surfaces over a single PostgreSQL database through Prisma. Authorisation is layered on purpose: route prefixes are gated by role at the network edge, where a deactivated session is also forced to log out on its next request; branch scope is then resolved from the authenticated session by pure, independently tested functions, so a branch requesting another branch’s data is refused rather than quietly narrowed; and the riskiest checks run inside the database transaction, because state changes between a pre-flight check and a commit.

Concurrency uses the right tool per case — version columns with bounded retries for stock, conditional updates so the database refuses to go negative, advisory locks to serialise retries of one checkout, a partial unique index for “one open attendance session”, and unique identity keys for idempotent payroll settlement. Six scheduled jobs handle daily and weekly commission calculation, payroll processing, inventory alerts, forecasts and reorder points; each takes a lease before running, so a retried invocation cannot run a money-touching job twice.

StorefrontAdmin portalBranch & POSEmployee appAuth & role permissionsAPI & server layerCommission & payrollInventory & POS rulesAttendance & shiftsIntegrationsPostgreSQL

Where it stands

The platform is live and trading, online and in store.

The website, the shop floor and head office work from one catalogue, one stock position and one set of staff records. Every in-store sale belongs to an open register shift with a named opener and closer, and each shift closes against a counted figure. Commission is derived from recorded attendance and attributed sales under dated, per-branch rules, and the employee sees the same derivation the approver does. Stock reaches a branch through a request and an approval, leaving a record. A payment that cannot be turned into an order is refunded automatically and queued for review rather than discovered by a customer.

No performance or commercial figures are published here: none were measured, and the client’s trading numbers are theirs.

Want something like this? Tell me how your business runs today.