Allowances & deductions
What you’ll find here
Section titled “What you’ll find here”- What allowance categories and deduction categories are, and how they feed into the payroll batch
allowance_items/deduction_itemslines. - How to create / rename / delete a category on the mobile POS workspace.
- The full set of type values, the calculation method discriminator, and how a category’s numeric
valueis interpreted. - The API surface (
GET / POST / PUT / DELETE) and the database schema behind both catalogs. - Common pitfalls and the migration story.
Reading order: this page documents the catalog of categories. Once a batch is created, the engine copies the relevant rows into the payslip’s
allowance_items/deduction_itemsJSON. For the full payroll lifecycle, see Payrolls.
I. Two catalogs, two tables
Section titled “I. Two catalogs, two tables”Allowances and deductions live in separate tables, not a single kind-discriminated table. They never share a row id.
| Catalog | Table | Default type | Surfaces in mobile |
|---|---|---|---|
| Allowance categories | vendor_allowances | daily_fixed | More → Employees → Settings → Payroll → Allowances |
| Deduction categories | vendor_deductions | monthly_fixed | More → Employees → Settings → Payroll → Deductions |
Each row is a (vendor, name) unique pair — names are unique within the vendor scope so duplicate categories can never be created by mistake. A vendor in Cambodia operating in two branches still owns one catalog; both branches share the same rows.
II. Why two tables (not one)
Section titled “II. Why two tables (not one)”- The payroll engine queries each catalog independently. A future FK from
payslip_lines.allowance_id/payslip_lines.deduction_idto one or the other remains a single-column reference (nokinddiscriminator to remember in joins). - The value spaces differ: allowance
typeis{daily_fixed, monthly_fixed, monthly_standard_days, per_shift}; deductiontypeis{monthly_fixed, per_shift, social_insurance, tax, advance}. Splitting the tables keeps the DTO validation (@IsIn([...])) from needing akinddiscriminator on every endpoint. - Mobile UX mirrors the split — the list screen for each category is identical, and each lives on its own route under the “Payroll” sub-tree.
III. The type discriminator
Section titled “III. The type discriminator”type is a free-form varchar(64) column on the mobile side (the TypeScript union in allowancesDeductionsApi.ts is the source of truth). Adding a new type is a mobile-only release — no backend migration required.
Allowance type | Meaning
Section titled “Allowance type | Meaning”type value | UI label | When it triggers |
|---|---|---|
daily_fixed | Daily fixed allowance | Adds the allowance once per worked day in the pay period. |
monthly_fixed | Monthly fixed allowance | Adds the allowance once per month regardless of days worked. |
monthly_standard_days | Monthly allowance (prorated by standard working days) | Adds value × (actual_days / standard_days) so the employee is paid in proportion to days worked. |
per_shift | Per-shift allowance | Adds the allowance once per shift the employee opens. |
Deduction type | Meaning
Section titled “Deduction type | Meaning”type value | UI label | When it triggers |
|---|---|---|
monthly_fixed | Monthly fixed deduction | Deducts once per month regardless of days worked. |
per_shift | Per-shift deduction | Deducts once per shift. |
social_insurance | Social insurance contribution | Statutory social insurance (BHXH-style) — payroll engine computes the legal rate. |
tax | Personal income tax | Personal income tax (TNCN) — payroll engine applies the bracket table. |
advance | Salary advance | One-shot advance deduction; cleared when paid. |
The persisted
typeis the raw English enum value, not the localised UI label. Localised labels live in the mobile app’s i18n files (employeeSettings.payroll.allowanceForm.types.daily_fixed/…deductionForm.types.monthly_fixed…).
IV. The calculation_method discriminator
Section titled “IV. The calculation_method discriminator”A separate axis from type. A category is paid either as a flat currency amount or as a percentage of the employee’s base salary.
calculation_method | UI label | value interpretation |
|---|---|---|
AMOUNT | Amount (currency) | A currency amount. The mobile UI shows it with the active store’s currency suffix (e.g. VND). |
PERCENTAGE | % of base salary | A 0–100 percentage. The payroll line reads BASE × value / 100. Values > 100 are rejected with 400 BadRequest on both create and update. |
The currency suffix is store-aware — the same category created by a Vietnam store shows VND, but the same store-level data viewed from a Cambodia store shows KHR. This is driven by useStoreDisplayPreferences() on the mobile side and the active vendor_id + store_id on the backend side.
V. Mobile walkthrough (POS workspace)
Section titled “V. Mobile walkthrough (POS workspace)”V.1 List screen (empty)
Section titled “V.1 List screen (empty)”Open More → Employees → Settings → Payroll → Allowances (or Deductions). When the catalog is empty, the screen shows a centred hero with a file illustration, an empty-state message, and a primary action button.
┌──────────────────────────────────────────────┐│ < Allowance categories │├──────────────────────────────────────────────┤│ ││ ┌──────────┐ ││ │ 📄 │ ││ │ │ ││ └──────────┘ ││ ││ You don't have any ││ allowances yet ││ ││ Add an allowance to start ││ tracking per-employee perks ││ ││ ┌──────────────┐ ││ │ + Add │ ││ │ allowance… │ ││ └──────────────┘ ││ │└──────────────────────────────────────────────┘The list, when populated, shows one row per category:
┌──────────────────────────────────────────────┐│ < Allowance categories │├──────────────────────────────────────────────┤│ ┌──────────────────────────────────────┐ ││ │ Fuel › │ ││ │ Daily fixed │ ││ │ Amount · 50,000 VND │ ││ └──────────────────────────────────────┘ ││ ┌──────────────────────────────────────┐ ││ │ Lunch › │ ││ │ Monthly fixed │ ││ │ Amount · 1,000,000 VND │ ││ └──────────────────────────────────────┘ ││ ┌──────────────────────────────────────┐ ││ │ Seniority › │ ││ │ Monthly (standard days) │ ││ │ % of base · 12.5 % │ ││ └──────────────────────────────────────┘ ││ │└──────────────────────────────────────────────┘Each row carries:
| Field | Source |
|---|---|
| Name (bold) | name (the user-visible label, e.g. “Fuel”, “Lunch”). |
| Type | Resolved via typeLabel(row.type) — the localised UI label, not the raw enum. |
| Method · Value | e.g. “Amount · 50,000 VND” or ”% of base · 12.5 %”. |
| Chevron | Tap → opens the edit form. |
A long-press on a row triggers delete with a confirmation toast on success.
V.2 Add / edit form
Section titled “V.2 Add / edit form”Tap the + Add new button (empty state) or any existing row (edit). The form has four fields:
┌──────────────────────────────────────────────┐│ < Add allowance type [ Save ] │├──────────────────────────────────────────────┤│ ││ Allowance name ││ ┌────────────────────────────────────┐ ││ │ Fuel │ ││ └────────────────────────────────────┘ ││ ││ Allowance type ││ ┌────────────────────────────────────┐ ││ │ Daily fixed ▾ │ ││ └────────────────────────────────────┘ ││ ││ Calculation method ││ ◉ Amount ││ ○ % of base salary ││ ││ Value ││ ┌────────────────────────┐ VND ││ │ 50,000 │ ││ └────────────────────────┘ ││ │└──────────────────────────────────────────────┘| Field | What you enter |
|---|---|
Allowance name (name) | Free text, 1–191 chars. |
Allowance type (type) | Single-select bottom sheet — the canonical type value space for allowances (4 options). |
Calculation method (calculation_method) | Radio group: Amount vs % of base salary. Drives the suffix on the value input. |
Value (value) | Numeric input. Suffix switches to VND when AMOUNT, % when PERCENTAGE. 0 is allowed (renders as 0 VND / 0 %). |
The AppBar’s Save button (top right) is disabled until name is non-empty and value parses as a number. Save calls POST /api/v1/vendor/allowances (create) or PUT /api/v1/vendor/allowances/:id (update) and on success invalidates ["allowances"] so the list refetches.
V.3 Edit flow — instant hydrate
Section titled “V.3 Edit flow — instant hydrate”The edit form pre-fetches the row from the cached list (["allowances"]) before falling back to a single-row GET /api/v1/vendor/allowances/:id. If the row is in the list cache (the common case — the user just tapped the row), the form hydrates instantly without a network round-trip.
V.4 Loading, error, and empty states
Section titled “V.4 Loading, error, and empty states”| State | What you see |
|---|---|
| Loading (initial) | Static shell (AppBar + chrome) renders immediately. Three skeleton rows occupy the list area until the query resolves. |
| Error | Full-screen error message with a Retry button calling refetch(). |
| Empty | Centred hero + + Add button. |
| Success | Populated list with rows as in V.1. |
The static shell renders for the entire loading window per the targeted-skeleton rule — skeletons only appear where dynamic data is being fetched (the list rows and the form’s edit-mode values).
VI. Vendor-web (HR dashboard)
Section titled “VI. Vendor-web (HR dashboard)”The HR dashboard surfaces the same catalogs under HR → Payroll → Allowances (planned). The vendor-web route will reuse the same NestJS endpoints with the same DTOs; no backend work is pending. Until the vendor-web page ships, the mobile POS workspace is the canonical editor for both catalogs.
VII. Endpoints
Section titled “VII. Endpoints”| Method | Path | Purpose |
|---|---|---|
GET | /api/v1/vendor/allowances | List all allowance categories for the active vendor. No store_id — categories are vendor-scoped. |
GET | /api/v1/vendor/allowances/:id | One category. |
POST | /api/v1/vendor/allowances | Create. Body: { name, type, calculation_method, value }. Rejects duplicate (vendor_id, name) with 400. |
PUT | /api/v1/vendor/allowances/:id | Partial update. Only the keys you send are written. |
DELETE | /api/v1/vendor/allowances/:id | Remove. Cascades nothing — payslips that already reference the category keep their JSON snapshot. |
GET | /api/v1/vendor/deductions | Same shape for deductions. |
GET | /api/v1/vendor/deductions/:id | ” |
POST | /api/v1/vendor/deductions | ” |
PUT | /api/v1/vendor/deductions/:id | ” |
DELETE | /api/v1/vendor/deductions/:id | ” |
All routes require the vendor JWT (@UseGuards(VendorJwtAuthGuard)); vendor_id is read from the JWT and is not a query param. The service layer rejects unknown type values (checked against the canonical VENDOR_ALLOWANCE_TYPE_VALUES / VENDOR_DEDUCTION_TYPE_VALUES enums) and PERCENTAGE > 100 with 400 BadRequest. Cross-vendor id (e.g. another vendor’s category id) resolves to 404 NotFound to avoid leaking ids.
VIII. Schema
Section titled “VIII. Schema”DDL is written with the TypeORM object-form (new Table({...})) so the DDL stays in lockstep with the entity decorators.
vendor_allowances:
| Column | Type | Notes |
|---|---|---|
id | char(13) | Crockford Base32, minted by PublicIdSubscriber. |
vendor_id | char(13) | Owning vendor (FK to vendors.id). |
name | varchar(191) | Display label. Unique per vendor_id. |
type | varchar(64) | Free-form discriminator — see §III. |
calculation_method | varchar(16) | AMOUNT | PERCENTAGE. |
value | decimal(15,2) | Numeric value. AMOUNT rows store the currency amount; PERCENTAGE rows store the 0..100 percentage. |
sort_order | int unsigned | Stable sort order (lower first). Default 0. |
created_at, updated_at | datetime(6) | Timestamps. |
Indexes:
idx_vendor_allowances_vendoron(vendor_id)— list query path.uniq_vendor_allowances_vendor_nameon(vendor_id, name)— duplicate-name gate.
vendor_deductions mirrors vendor_allowances 1:1 — same column types, same indexes (with deduction in place of allowance), same fp(...) prefix rules. The default type is monthly_fixed instead of daily_fixed.
DDL lives in apps/backend/src/database/migrations/2000000000212-CreateVendorAllowanceAndDeductionTables.ts. Entities are apps/backend/src/database/entities/vendor-allowance.entity.ts and vendor-deduction.entity.ts.
IX. Common pitfalls
Section titled “IX. Common pitfalls”- Cross-store leakage never happens at this layer. Both tables are vendor-scoped, not store-scoped — every store in the same vendor sees the same catalog rows.
typeis free-form on the DB but validated at the service layer. If the mobile app extends the enum without a backend release, the new value passes the column constraint (varchar(64)) and is caught by the service’s@IsIn([...]). The wire payload always returns the canonical enum value.- Editing a category does not retroactively re-derive payslip
allowance_items/deduction_itemslines. The payslip stores its own JSON snapshot, so renaming or repricing a category only affects batches created after the edit. This matches the wage engine upstream — payslips are user-edited numbers, not derived values. - Deleting a category in use. Existing payslips that referenced it keep their JSON snapshot. The mobile form pre-populates from the list cache; if the row was deleted server-side before the user opened the form, the form falls back to a single-row
GET /:idand surfaces an error toast. - Percentage values must be 0–100.
value = 150withcalculation_method = PERCENTAGEis rejected with400. The mobile form does not enforce this client-side — it relies on the backend response. - Migration ordering. The new tables are introduced by migration
2000000000212-CreateVendorAllowanceAndDeductionTables.ts. If the deployment targets an environment where migration< 212has not yet run (e.g. a half-applied cutover), the tables are absent and the API returnsER_NO_SUCH_TABLEinstead of404— re-run pending migrations before opening the editor.
X. Where this feeds into the payroll engine
Section titled “X. Where this feeds into the payroll engine”When a payslip is generated for an employee during a pay period, the wage engine reads every category for the vendor (no per-store filtering) and emits one allowance_items / deduction_items row per category whose trigger fires for that employee. The line’s total is computed by the engine from category.value × the trigger’s multiplier (days, shifts, base-salary percentage). The mobile editor only defines the categories — it does not run the engine.
See Payrolls → §IV for the payslip layout, including the
allowance_items/deduction_itemsJSON shape.
Related
Section titled “Related”- Payrolls — the batch + payslip lifecycle that consumes the categories.
- Commission sheets — the rules engine that emits
commission_totalon the payslip; orthogonal to allowances and deductions. - API catalog → Vendor / Allowances, Vendor / Deductions.
- Migration DDL — CreateVendorAllowanceAndDeductionTables.ts.
- Entities — VendorAllowanceEntity.ts, VendorDeductionEntity.ts.
- Mobile app screens —
indochina-pos/src/modules/employees/screens/{PayrollConfigListScreen,PayrollConfigFormScreen,AllowanceListScreen,AllowanceFormScreen,DeductionListScreen,DeductionFormScreen}.tsx.