Add Stock catalog (items/warehouses) and fix session persistence

Stock is scoped to a catalog + cost centers only, per direction that
physical GRV/stock-take/recipe transactions belong in an external
system (Matrix), not this accounting app. Stock Control/GRN/Variance/
PPV GL accounts are now plain postable accounts rather than locked
control accounts.

Also fixes session loss on page refresh: auth stores now persist to
localStorage, and ProtectedRoute waits for rehydration before deciding
to redirect to login.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JWqiUSojMLUoN7mrJqdEbN
This commit is contained in:
Joshua Oosthuizen
2026-09-04 10:35:04 +02:00
co-authored by Claude Sonnet 5
parent 4acbc03b35
commit b137176c40
35 changed files with 1199 additions and 46 deletions
+36 -3
View File
@@ -18,6 +18,8 @@ const PERMISSIONS = [
'ap.invoice.manage',
'ap.debitnote.manage',
'ap.payment.manage',
'stock.warehouse.manage',
'stock.item.manage',
] as const;
const ROLE_PERMISSIONS: Record<string, readonly string[]> = {
@@ -35,6 +37,8 @@ const ROLE_PERMISSIONS: Record<string, readonly string[]> = {
'ap.invoice.manage',
'ap.debitnote.manage',
'ap.payment.manage',
'stock.warehouse.manage',
'stock.item.manage',
],
AR_AP_CLERK: [
'ar.customer.manage',
@@ -46,6 +50,7 @@ const ROLE_PERMISSIONS: Record<string, readonly string[]> = {
'ap.debitnote.manage',
'ap.payment.manage',
],
STOCK_CLERK: ['stock.warehouse.manage', 'stock.item.manage'],
BRANCH_USER: [],
READ_ONLY: [],
};
@@ -98,14 +103,17 @@ async function main() {
{ code: '1100', description: 'Accounts Receivable Control' },
{ code: '1200', description: 'Inventory / Stock Control' },
{ code: '2000', description: 'Accounts Payable Control' },
{ code: '2050', description: 'Goods Received Not Invoiced' },
{ code: '2100', description: 'Output Tax Payable' },
{ code: '2200', description: 'Input Tax Receivable' },
{ code: '2300', description: 'Accrued Expenses' },
{ code: '3000', description: 'Retained Earnings' },
{ code: '3100', description: 'Share Capital' },
{ code: '4000', description: 'Sales Income' },
{ code: '5000', description: 'Cost of Goods Sold' },
{ code: '5100', description: 'Operating Expenses' },
{ code: '5200', description: 'Stock Variance' },
{ code: '5300', description: 'Purchase Price Variance' },
];
const masterByCode: Record<string, { id: number }> = {};
for (const value of masterValues) {
@@ -157,14 +165,19 @@ async function main() {
}> = [
{ masterCode: '1000', accountName: 'Bank / Cash', accountType: 'ASSET', normalBalance: 'DEBIT', controlType: 'BANK' },
{ masterCode: '1100', accountName: 'Accounts Receivable Control', accountType: 'ASSET', normalBalance: 'DEBIT', isControlAccount: true, controlType: 'AR' },
{ masterCode: '1200', accountName: 'Inventory / Stock Control', accountType: 'ASSET', normalBalance: 'DEBIT', isControlAccount: true, controlType: 'STOCK' },
// Not a control account: no subledger in this app owns stock value — see the Stock section
// of prisma/schema.prisma. Postable directly via manual journal like any other GL account.
{ masterCode: '1200', accountName: 'Inventory / Stock Control', accountType: 'ASSET', normalBalance: 'DEBIT' },
{ masterCode: '2000', accountName: 'Accounts Payable Control', accountType: 'LIABILITY', normalBalance: 'CREDIT', isControlAccount: true, controlType: 'AP' },
{ masterCode: '2050', accountName: 'Goods Received Not Invoiced', accountType: 'LIABILITY', normalBalance: 'CREDIT' },
{ masterCode: '2100', accountName: 'Output Tax Payable', accountType: 'LIABILITY', normalBalance: 'CREDIT', isControlAccount: true, controlType: 'TAX_OUTPUT' },
{ masterCode: '2200', accountName: 'Input Tax Receivable', accountType: 'ASSET', normalBalance: 'DEBIT', isControlAccount: true, controlType: 'TAX_INPUT' },
{ masterCode: '2300', accountName: 'Accrued Expenses', accountType: 'LIABILITY', normalBalance: 'CREDIT' },
{ masterCode: '4000', accountName: 'Sales Income', accountType: 'INCOME', normalBalance: 'CREDIT' },
{ masterCode: '5000', accountName: 'Cost of Goods Sold', accountType: 'EXPENSE', normalBalance: 'DEBIT' },
{ masterCode: '5100', accountName: 'Operating Expenses', accountType: 'EXPENSE', normalBalance: 'DEBIT' },
{ masterCode: '5200', accountName: 'Stock Variance', accountType: 'EXPENSE', normalBalance: 'DEBIT' },
{ masterCode: '5300', accountName: 'Purchase Price Variance', accountType: 'EXPENSE', normalBalance: 'DEBIT' },
];
for (const branchCode of Object.keys(branchByCode)) {
@@ -189,7 +202,13 @@ async function main() {
],
},
},
update: {},
update: {
accountName: `${account.accountName} — ${branchCode}`,
accountType: account.accountType,
normalBalance: account.normalBalance,
isControlAccount: account.isControlAccount ?? false,
controlType: account.controlType ?? null,
},
});
}
}
@@ -218,7 +237,7 @@ async function main() {
],
},
},
update: {},
update: { accountName: account.accountName, controlType: account.controlType ?? null },
});
}
@@ -258,6 +277,20 @@ async function main() {
create: { code: 'CASH_PAYMENT', name: 'Cash Payments Journal', journalType: JournalType.CASH_PAYMENT },
update: {},
});
await prisma.journal.upsert({
where: { code: 'STOCK' },
create: { code: 'STOCK', name: 'Stock Journal', journalType: JournalType.STOCK },
update: {},
});
console.log('Seeding warehouses...');
for (const branchCode of Object.keys(branchByCode)) {
await prisma.warehouse.upsert({
where: { code: `WH-${branchCode}` },
create: { code: `WH-${branchCode}`, name: `Main Warehouse — ${branchCode}`, branchSegmentValueId: branchByCode[branchCode].id },
update: {},
});
}
console.log('Seeding tax codes...');
const taxCodes = [