From dbef4b9f7aa7cdd86ce8c75fef01a722578eb791 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Mon, 20 Jul 2026 08:23:54 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20sales=20breakdown=20GL=20matching=20?= =?UTF-8?q?=E2=80=94=20exact=20group=20ID=20instead=20of=20fuzzy=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switch sales breakdown column matching from fuzzy name matching to exact gl_group_id comparison. Add gl_account_groups_list endpoint for settings sync so columns store real Newbook group IDs. Removes the name-based fallback that was causing sundries VAT to bleed into sundries no-VAT. Co-Authored-By: Claude Sonnet 4.6 --- backend/src/index.js | 3 +- backend/src/lib/newbook.js | 9 +++-- backend/src/routes/reports.js | 24 +++---------- frontend/src/App.tsx | 16 ++++++--- frontend/src/components/UpdateBanner.tsx | 44 ++++++++++++++++++++++++ frontend/src/hooks/useVersionCheck.ts | 43 +++++++++++++++++++++++ 6 files changed, 111 insertions(+), 28 deletions(-) create mode 100644 frontend/src/components/UpdateBanner.tsx create mode 100644 frontend/src/hooks/useVersionCheck.ts diff --git a/backend/src/index.js b/backend/src/index.js index aa2df17..2f139f6 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -16,6 +16,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url)) const UPLOADS_DIR = join(__dirname, '..', 'uploads') const app = Fastify({ logger: true, trustProxy: true }) +const startedAt = Date.now() await app.register(cookie) await app.register(cors, { @@ -29,7 +30,7 @@ await app.register(staticFiles, { decorateReply: false, }) -app.get('/health', async () => ({ status: 'healthy' })) +app.get('/health', async () => ({ status: 'healthy', version: process.env.BUILD_VERSION || String(startedAt) })) await app.register(cashupRoutes) await app.register(newbookRoutes) diff --git a/backend/src/lib/newbook.js b/backend/src/lib/newbook.js index 89dd9cd..e667ebb 100644 --- a/backend/src/lib/newbook.js +++ b/backend/src/lib/newbook.js @@ -175,14 +175,19 @@ export async function fetchGlAccountList() { return response?.data ?? [] } +export async function fetchGlAccountGroupsList() { + const response = await callApi('gl_account_groups_list', {}) + return response?.data ?? [] +} + export async function fetchGlAccountsGrouped() { - const data = await fetchGlAccountList() + const data = await fetchGlAccountGroupsList() const groups = {} for (const item of data) { if (item.gl_group_id && item.gl_group_name && !groups[item.gl_group_id]) { let displayName = item.gl_group_name if (displayName.includes(' - ')) displayName = displayName.split(' - ').slice(1).join(' - ').trim() - groups[item.gl_group_id] = displayName + groups[String(item.gl_group_id)] = displayName } } return groups diff --git a/backend/src/routes/reports.js b/backend/src/routes/reports.js index 4511be0..30b9fe7 100644 --- a/backend/src/routes/reports.js +++ b/backend/src/routes/reports.js @@ -131,27 +131,11 @@ export async function reportRoutes(app) { { category: 'bacs', banked_amount: pt.bacs, reported_amount: pt.bacs }, ] - // Build a lookup from gl_group_id → name using the accounts list - // gl_group_id from Newbook is often numeric; normalise to string for comparison - const glGroupById = {} - for (const a of (glAccountList || [])) { - const gid = String(a.gl_group_id ?? '') - if (gid && !glGroupById[gid]) glGroupById[gid] = (a.gl_group_name ?? '').toLowerCase().replace(/[^a-z0-9]/g, '') - } - - // Sales breakdown — match earned revenue to configured columns - // Try: (1) exact code match, (2) normalised name contains code / vice-versa + // Sales breakdown — match earned revenue to configured columns by exact gl_group_id const salesBreakdown = enabledColumns.map(col => { - const code = col.gl_code.toLowerCase().replace(/[^a-z0-9]/g, '') - const item = earnedRevenue.find(r => { - if (r.period !== date) return false - const gid = String(r.gl_group_id ?? '') - if (gid === col.gl_code || gid.toUpperCase() === col.gl_code.toUpperCase()) return true - const gName = glGroupById[gid] ?? '' - // Only fuzzy-match when the column has a non-empty gl_code — prevents - // placeholder columns (empty code) from matching any GL group via gName.includes('') - return code.length > 0 && (gName.includes(code) || code.includes(gName)) - }) + const item = col.gl_code + ? earnedRevenue.find(r => r.period === date && String(r.gl_group_id ?? '') === String(col.gl_code)) + : undefined const net = parseFloat(item?.earned_revenue_ex || 0) const vat = parseFloat(item?.earned_revenue_tax || 0) const gross = parseFloat(item?.earned_revenue || 0) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index df59e08..5667afe 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,5 +1,7 @@ import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom' import { AuthGate } from './components/AuthGate' +import { UpdateBanner } from './components/UpdateBanner' +import { useVersionCheck } from './hooks/useVersionCheck' import { Layout } from './components/Layout' import { DailyCashUp } from './pages/DailyCashUp' import { History } from './pages/History' @@ -42,11 +44,15 @@ function AppRoutes({ user }: { user: User }) { } export default function App() { + const updateAvailable = useVersionCheck('/cashup/health') return ( - - - {user => } - - + <> + + + {user => } + + + + ) } diff --git a/frontend/src/components/UpdateBanner.tsx b/frontend/src/components/UpdateBanner.tsx new file mode 100644 index 0000000..b492c67 --- /dev/null +++ b/frontend/src/components/UpdateBanner.tsx @@ -0,0 +1,44 @@ +import { RefreshCw } from 'lucide-react' + +export function UpdateBanner({ visible }: { visible: boolean }) { + if (!visible) return null + return ( +
+ A new version is available. + +
+ ) +} diff --git a/frontend/src/hooks/useVersionCheck.ts b/frontend/src/hooks/useVersionCheck.ts new file mode 100644 index 0000000..e4fed28 --- /dev/null +++ b/frontend/src/hooks/useVersionCheck.ts @@ -0,0 +1,43 @@ +import { useEffect, useState } from 'react' + +const POLL_MS = 2 * 60 * 1000 + +export function useVersionCheck(healthUrl: string) { + const [updateAvailable, setUpdateAvailable] = useState(false) + + useEffect(() => { + let seenVersion: string | null = null + + async function check() { + try { + const res = await fetch(healthUrl, { cache: 'no-store' }) + if (!res.ok) return + const data = await res.json() + const v: string | undefined = data.version + if (!v) return + if (seenVersion === null) { + seenVersion = v + } else if (v !== seenVersion) { + setUpdateAvailable(true) + } + } catch { + // network error — skip silently + } + } + + check() + const interval = setInterval(check, POLL_MS) + + function onVisible() { + if (document.visibilityState === 'visible') check() + } + document.addEventListener('visibilitychange', onVisible) + + return () => { + clearInterval(interval) + document.removeEventListener('visibilitychange', onVisible) + } + }, [healthUrl]) + + return updateAvailable +}