Switch syncActuals to timesheets endpoint — PBI-matched method
Uses /api/v2/timesheets/on/{date} per day (no location param), expands
nested shifts[], skips leave (leave_request_id!=null), and post-filters
to this hotel's departments via location_id match on the departments
list. This mirrors exactly what the FD's Power BI query does and should
give the correct £51k timesheet figure for June.
Also simplifies compareEndpoints to two variants: A (old shifts method)
vs B (new timesheets method) so the discrepancy is immediately visible.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
74024f554f
commit
2690ee149f
1 changed files with 160 additions and 164 deletions
|
|
@ -69,11 +69,6 @@ async function getEnabledDeptIds() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDeptNameMap() {
|
|
||||||
const depts = await fetchAllDepartments()
|
|
||||||
return Object.fromEntries(depts.map(d => [d.id, d.name]))
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getUserNameMap() {
|
async function getUserNameMap() {
|
||||||
const all = await wfFetchPaged('/api/v2/users')
|
const all = await wfFetchPaged('/api/v2/users')
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
|
|
@ -81,59 +76,91 @@ async function getUserNameMap() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build list of YYYY-MM-DD strings between from and to (inclusive)
|
||||||
|
function dateRange(from, to) {
|
||||||
|
const dates = []
|
||||||
|
const cur = new Date(from + 'T00:00:00')
|
||||||
|
const end = new Date(to + 'T00:00:00')
|
||||||
|
while (cur <= end) { dates.push(cur.toISOString().slice(0, 10)); cur.setDate(cur.getDate() + 1) }
|
||||||
|
return dates
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch timesheets for a single date — no location param (mirrors PBI pattern).
|
||||||
|
// Each record is { user_id, shifts: [{ department_id, date, cost, leave_request_id, ... }] }
|
||||||
|
async function fetchTimesheetsForDate(dateStr) {
|
||||||
|
try { return await wfFetchPaged(`/api/v2/timesheets/on/${dateStr}?show_costs=true`) }
|
||||||
|
catch { return [] }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expand an array of timesheet objects into flat shift rows, filtering out leave and
|
||||||
|
// optionally filtering by location department IDs and enabled department IDs.
|
||||||
|
function expandShifts(timesheets, locationDeptIds, enabledDeptIds) {
|
||||||
|
const rows = []
|
||||||
|
for (const t of timesheets) {
|
||||||
|
if (!Array.isArray(t.shifts)) continue
|
||||||
|
const userId = String(t.user_id)
|
||||||
|
for (const sh of t.shifts) {
|
||||||
|
if (sh.leave_request_id != null) continue // exclude leave accrual shifts
|
||||||
|
const deptId = String(sh.department_id ?? 'unknown')
|
||||||
|
if (locationDeptIds && !locationDeptIds.has(deptId)) continue // filter to this hotel
|
||||||
|
if (enabledDeptIds && !enabledDeptIds.includes(deptId)) continue
|
||||||
|
rows.push({ date: sh.date, userId, deptId, cost: parseFloat(sh.cost ?? 0) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
export async function syncActuals(from, to) {
|
export async function syncActuals(from, to) {
|
||||||
const creds = await getWorkforceCreds()
|
const creds = await getWorkforceCreds()
|
||||||
const locationId = creds.location_id
|
const locationId = creds.location_id ? String(creds.location_id) : null
|
||||||
const enabledDeptIds = await getEnabledDeptIds()
|
const enabledDeptIds = await getEnabledDeptIds()
|
||||||
|
|
||||||
let path = `/api/v2/shifts?from=${from}&to=${to}&show_costs=true&include_oncosts=true`
|
// Fetch departments + users in parallel — departments used to filter to this hotel's depts
|
||||||
if (locationId) path += `&report_location_id=${locationId}`
|
const [allDepts, userNameMap] = await Promise.all([
|
||||||
|
wfFetchPaged('/api/v2/departments'),
|
||||||
|
getUserNameMap(),
|
||||||
|
])
|
||||||
|
const deptNameMap = Object.fromEntries(allDepts.map(d => [String(d.id), d.name]))
|
||||||
|
const locationDeptIds = locationId
|
||||||
|
? new Set(allDepts.filter(d => String(d.location_id) === locationId).map(d => String(d.id)))
|
||||||
|
: null
|
||||||
|
|
||||||
const shifts = await wfFetchPaged(path)
|
// Fetch all dates in parallel — same pattern as PBI (no location param on API)
|
||||||
const [deptNameMap, userNameMap] = await Promise.all([getDeptNameMap(), getUserNameMap()])
|
const dates = dateRange(from, to)
|
||||||
|
const perDay = await Promise.all(dates.map(fetchTimesheetsForDate))
|
||||||
|
|
||||||
const byDateDept = {}
|
const byDateDept = {}
|
||||||
const byDateDeptEmp = {}
|
const byDateDeptEmp = {}
|
||||||
for (const s of shifts) {
|
|
||||||
const deptId = String(s.department_id)
|
|
||||||
if (enabledDeptIds && !enabledDeptIds.includes(deptId)) continue
|
|
||||||
|
|
||||||
const date = s.date
|
for (const timesheets of perDay) {
|
||||||
const baseCost = parseFloat(s.cost ?? 0)
|
for (const { date, userId, deptId, cost } of expandShifts(timesheets, locationDeptIds, enabledDeptIds)) {
|
||||||
const totalCost = parseFloat(s.cost_with_oncosts ?? s.cost ?? 0)
|
|
||||||
|
|
||||||
// aggregate by date+dept
|
|
||||||
const key = `${date}:${deptId}`
|
const key = `${date}:${deptId}`
|
||||||
if (!byDateDept[key]) {
|
if (!byDateDept[key]) {
|
||||||
byDateDept[key] = {
|
byDateDept[key] = {
|
||||||
date,
|
date, department_id: deptId,
|
||||||
department_id: deptId,
|
department_name: deptNameMap[deptId] || deptId,
|
||||||
department_name: deptNameMap[deptId] || s.department_name || deptId,
|
|
||||||
base_cost: 0,
|
|
||||||
total_cost: 0,
|
|
||||||
shift_count: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
byDateDept[key].base_cost += baseCost
|
|
||||||
byDateDept[key].total_cost += totalCost
|
|
||||||
byDateDept[key].shift_count += 1
|
|
||||||
|
|
||||||
// detail by date+dept+employee — include any shift that contributed cost to the aggregate
|
|
||||||
if (totalCost > 0 || baseCost > 0) {
|
|
||||||
const empId = String(s.user_id)
|
|
||||||
const empName = userNameMap[empId] || `Employee ${empId}`
|
|
||||||
const empKey = `${date}:${deptId}:${empId}`
|
|
||||||
if (!byDateDeptEmp[empKey]) {
|
|
||||||
byDateDeptEmp[empKey] = {
|
|
||||||
date, department_id: deptId, employee_id: empId, employee_name: empName,
|
|
||||||
base_cost: 0, total_cost: 0, shift_count: 0,
|
base_cost: 0, total_cost: 0, shift_count: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
byDateDeptEmp[empKey].base_cost += baseCost
|
byDateDept[key].base_cost += cost
|
||||||
byDateDeptEmp[empKey].total_cost += totalCost
|
byDateDept[key].total_cost += cost
|
||||||
|
byDateDept[key].shift_count += 1
|
||||||
|
|
||||||
|
if (cost > 0) {
|
||||||
|
const empKey = `${date}:${deptId}:${userId}`
|
||||||
|
const empName = userNameMap[userId] || `Employee ${userId}`
|
||||||
|
if (!byDateDeptEmp[empKey]) {
|
||||||
|
byDateDeptEmp[empKey] = {
|
||||||
|
date, department_id: deptId, employee_id: userId, employee_name: empName,
|
||||||
|
base_cost: 0, total_cost: 0, shift_count: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
byDateDeptEmp[empKey].base_cost += cost
|
||||||
|
byDateDeptEmp[empKey].total_cost += cost
|
||||||
byDateDeptEmp[empKey].shift_count += 1
|
byDateDeptEmp[empKey].shift_count += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const row of Object.values(byDateDept)) {
|
for (const row of Object.values(byDateDept)) {
|
||||||
await pool.query(
|
await pool.query(
|
||||||
|
|
@ -258,155 +285,124 @@ export async function runRollingSync() {
|
||||||
return { actualRows, scheduledRows }
|
return { actualRows, scheduledRows }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch a raw item list from a path and return a cost summary — used for endpoint comparison
|
// Fetch a raw shifts list from a path and return a cost summary — used for endpoint comparison
|
||||||
async function fetchAndSummarise(path, includeSample = false) {
|
async function fetchShiftsAndSummarise(path, locationDeptIds) {
|
||||||
try {
|
try {
|
||||||
const items = await wfFetchPaged(path)
|
const shifts = await wfFetchPaged(path)
|
||||||
if (!Array.isArray(items) || items.length === 0) return { count: 0, baseCost: 0, totalCost: 0, topDepts: [] }
|
if (!Array.isArray(shifts) || shifts.length === 0) return { count: 0, baseCost: 0, topDepts: [] }
|
||||||
let baseCost = 0, totalCost = 0
|
let baseCost = 0
|
||||||
const byDept = {}
|
const byDept = {}
|
||||||
let sampleRecord = null
|
let sampleShift = null
|
||||||
for (const s of items) {
|
for (const s of shifts) {
|
||||||
const base = parseFloat(s.cost ?? 0)
|
|
||||||
const total = parseFloat(s.cost_with_oncosts ?? s.cost ?? 0)
|
|
||||||
baseCost += base
|
|
||||||
totalCost += total
|
|
||||||
if (includeSample && !sampleRecord && base > 0) sampleRecord = s
|
|
||||||
const deptId = String(s.department_id ?? 'unknown')
|
const deptId = String(s.department_id ?? 'unknown')
|
||||||
const deptName = s.department_name || deptId
|
if (locationDeptIds && !locationDeptIds.has(deptId)) continue
|
||||||
if (!byDept[deptId]) byDept[deptId] = { name: deptName, baseCost: 0, totalCost: 0, count: 0 }
|
const cost = parseFloat(s.cost ?? 0)
|
||||||
byDept[deptId].baseCost += base
|
baseCost += cost
|
||||||
byDept[deptId].totalCost += total
|
if (!sampleShift && cost > 0) sampleShift = s
|
||||||
|
if (!byDept[deptId]) byDept[deptId] = { name: s.department_name || deptId, cost: 0, count: 0 }
|
||||||
|
byDept[deptId].cost += cost
|
||||||
byDept[deptId].count++
|
byDept[deptId].count++
|
||||||
}
|
}
|
||||||
const deptList = Object.entries(byDept)
|
const topDepts = Object.entries(byDept)
|
||||||
.map(([id, v]) => ({ id, name: v.name, baseCost: +v.baseCost.toFixed(2), totalCost: +v.totalCost.toFixed(2), count: v.count }))
|
.map(([id, v]) => ({ id, name: v.name, cost: +v.cost.toFixed(2), count: v.count }))
|
||||||
.sort((a, b) => b.totalCost - a.totalCost)
|
.sort((a, b) => b.cost - a.cost)
|
||||||
const result = { count: items.length, baseCost: +baseCost.toFixed(2), totalCost: +totalCost.toFixed(2), topDepts: deptList.slice(0, 15) }
|
const result = { rawCount: shifts.length, filteredCount: Object.values(byDept).reduce((s, v) => s + v.count, 0), baseCost: +baseCost.toFixed(2), topDepts: topDepts.slice(0, 15) }
|
||||||
if (includeSample && sampleRecord) {
|
if (sampleShift) result.sampleShiftFields = Object.fromEntries(Object.entries(sampleShift).filter(([, v]) => v !== null && v !== undefined && v !== ''))
|
||||||
result.sampleFields = Object.fromEntries(Object.entries(sampleRecord).filter(([, v]) => v !== null && v !== undefined && v !== ''))
|
|
||||||
}
|
|
||||||
return result
|
return result
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return { error: err.message }
|
return { error: err.message }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract cost from a timesheet record — tries multiple field name conventions
|
// Fetch per-date timesheets for every day in [from, to] in parallel.
|
||||||
function tsBaseCost(s) {
|
// Expands nested shifts[], filters leave, optionally filters by location dept IDs.
|
||||||
// Try all known field names in order of likelihood
|
// This is the method used by PBI — no location param on the API call.
|
||||||
for (const k of ['cost', 'timesheet_cost', 'base_cost', 'employee_cost', 'wage_cost', 'total_cost']) {
|
async function fetchTimesheetsByDay(from, to, locationDeptIds) {
|
||||||
const v = parseFloat(s[k])
|
const dates = dateRange(from, to)
|
||||||
if (!isNaN(v) && v > 0) return v
|
const perDay = await Promise.all(dates.map(fetchTimesheetsForDate))
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
function tsTotalCost(s, base) {
|
|
||||||
for (const k of ['cost_with_oncosts', 'total_cost_with_oncosts', 'oncost', 'cost_with_on_costs', 'total_oncost']) {
|
|
||||||
const v = parseFloat(s[k])
|
|
||||||
if (!isNaN(v) && v > 0) return v
|
|
||||||
}
|
|
||||||
return base // fallback: same as base
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch per-date timesheets for every day in [from, to] in parallel and aggregate.
|
let totalShifts = 0, filteredShifts = 0, cost = 0
|
||||||
// Uses /api/v2/timesheets/on/{date} when the range endpoint returns 404.
|
|
||||||
async function fetchTimesheetsByDay(from, to, locationId) {
|
|
||||||
const start = new Date(from + 'T00:00:00')
|
|
||||||
const end = new Date(to + 'T00:00:00')
|
|
||||||
|
|
||||||
const dates = []
|
|
||||||
let cur = new Date(start)
|
|
||||||
while (cur <= end) {
|
|
||||||
dates.push(cur.toISOString().slice(0, 10))
|
|
||||||
cur.setDate(cur.getDate() + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const perDay = await Promise.all(dates.map(async dateStr => {
|
|
||||||
let path = `/api/v2/timesheets/on/${dateStr}?show_costs=true&include_oncosts=true`
|
|
||||||
if (locationId) path += `&location_id=${locationId}`
|
|
||||||
try { return await wfFetchPaged(path) } catch { return [] }
|
|
||||||
}))
|
|
||||||
|
|
||||||
let baseCost = 0, totalCost = 0, count = 0
|
|
||||||
const byDept = {}
|
const byDept = {}
|
||||||
let sampleRecord = null // first record with cost > 0 for field inspection
|
let sampleShift = null
|
||||||
|
|
||||||
for (const items of perDay) {
|
for (const timesheets of perDay) {
|
||||||
if (!Array.isArray(items)) continue
|
if (!Array.isArray(timesheets)) continue
|
||||||
for (const s of items) {
|
for (const t of timesheets) {
|
||||||
const base = tsBaseCost(s)
|
if (!Array.isArray(t.shifts)) continue
|
||||||
const total = tsTotalCost(s, base)
|
for (const sh of t.shifts) {
|
||||||
baseCost += base; totalCost += total; count++
|
totalShifts++
|
||||||
if (!sampleRecord && base > 0) sampleRecord = s
|
const isLeave = sh.leave_request_id != null
|
||||||
const deptId = String(s.department_id ?? 'unknown')
|
const deptId = String(sh.department_id ?? 'unknown')
|
||||||
if (!byDept[deptId]) byDept[deptId] = { name: s.department_name || deptId, baseCost: 0, totalCost: 0, count: 0 }
|
const inLoc = !locationDeptIds || locationDeptIds.has(deptId)
|
||||||
byDept[deptId].baseCost += base
|
if (isLeave || !inLoc) continue
|
||||||
byDept[deptId].totalCost += total
|
filteredShifts++
|
||||||
|
const shCost = parseFloat(sh.cost ?? 0)
|
||||||
|
cost += shCost
|
||||||
|
if (!sampleShift && shCost > 0) sampleShift = sh
|
||||||
|
if (!byDept[deptId]) byDept[deptId] = { cost: 0, count: 0 }
|
||||||
|
byDept[deptId].cost += shCost
|
||||||
byDept[deptId].count++
|
byDept[deptId].count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const topDepts = Object.entries(byDept)
|
const topDepts = Object.entries(byDept)
|
||||||
.map(([id, v]) => ({ id, name: v.name, baseCost: +v.baseCost.toFixed(2), totalCost: +v.totalCost.toFixed(2), count: v.count }))
|
.map(([id, v]) => ({ id, cost: +v.cost.toFixed(2), count: v.count }))
|
||||||
.sort((a, b) => b.totalCost - a.totalCost)
|
.sort((a, b) => b.cost - a.cost)
|
||||||
|
|
||||||
// Expose all keys+values from a sample record so we can identify the correct cost fields
|
return {
|
||||||
const sampleFields = sampleRecord
|
timesheetRecords: perDay.reduce((s, d) => s + (Array.isArray(d) ? d.length : 0), 0),
|
||||||
? Object.fromEntries(Object.entries(sampleRecord).filter(([, v]) => v !== null && v !== undefined && v !== ''))
|
totalShifts,
|
||||||
: null
|
filteredShifts,
|
||||||
|
cost: +cost.toFixed(2),
|
||||||
return { count, baseCost: +baseCost.toFixed(2), totalCost: +totalCost.toFixed(2), topDepts: topDepts.slice(0, 15), sampleFields }
|
topDepts: topDepts.slice(0, 15),
|
||||||
|
sampleShiftFields: sampleShift
|
||||||
|
? Object.fromEntries(Object.entries(sampleShift).filter(([, v]) => v !== null && v !== undefined && v !== ''))
|
||||||
|
: null,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare cost totals from all available endpoints for a given date range.
|
// Compare cost totals from available endpoints for a given date range.
|
||||||
// Call GET /api/sync/compare?from=YYYY-MM-DD&to=YYYY-MM-DD to diagnose figure discrepancies.
|
// GET /api/sync/compare?from=YYYY-MM-DD&to=YYYY-MM-DD
|
||||||
export async function compareEndpoints(from, to) {
|
export async function compareEndpoints(from, to) {
|
||||||
const creds = await getWorkforceCreds()
|
const creds = await getWorkforceCreds()
|
||||||
const locationId = creds.location_id
|
const locationId = creds.location_id ? String(creds.location_id) : null
|
||||||
|
|
||||||
// A: Shifts with report_location_id (current sync method)
|
// Build location dept IDs set for post-fetch filtering (PBI pattern)
|
||||||
let shiftsReportLoc = `/api/v2/shifts?from=${from}&to=${to}&show_costs=true&include_oncosts=true`
|
const allDepts = await wfFetchPaged('/api/v2/departments')
|
||||||
if (locationId) shiftsReportLoc += `&report_location_id=${locationId}`
|
const locationDeptIds = locationId
|
||||||
|
? new Set(allDepts.filter(d => String(d.location_id) === locationId).map(d => String(d.id)))
|
||||||
|
: null
|
||||||
|
|
||||||
// B: Shifts with location_id (alternative location param)
|
// A: Shifts with report_location_id (old sync method)
|
||||||
let shiftsLocId = `/api/v2/shifts?from=${from}&to=${to}&show_costs=true&include_oncosts=true`
|
const shiftsReportLoc = `/api/v2/shifts?from=${from}&to=${to}&show_costs=true&include_oncosts=true`
|
||||||
if (locationId) shiftsLocId += `&location_id=${locationId}`
|
+ (locationId ? `&report_location_id=${locationId}` : '')
|
||||||
|
|
||||||
// C: Shifts with NO location filter — catches all shifts regardless of location assignment
|
// B: Timesheets per-day, no API location filter, post-filtered to this hotel's depts
|
||||||
const shiftsNoFilter = `/api/v2/shifts?from=${from}&to=${to}&show_costs=true&include_oncosts=true`
|
// This is the PBI method and should match WF "Timesheet exc. leave inc. allowances"
|
||||||
|
const [a, b] = await Promise.all([
|
||||||
// D: Timesheets range endpoint (may 404 on some WF versions)
|
fetchShiftsAndSummarise(shiftsReportLoc, locationDeptIds),
|
||||||
let tsRange = `/api/v2/timesheets?from=${from}&to=${to}&show_costs=true&include_oncosts=true`
|
fetchTimesheetsByDay(from, to, locationDeptIds),
|
||||||
if (locationId) tsRange += `&location_id=${locationId}`
|
|
||||||
|
|
||||||
const [a, b, c, d] = await Promise.all([
|
|
||||||
fetchAndSummarise(shiftsReportLoc, true), // sample fields from shift object
|
|
||||||
fetchAndSummarise(shiftsLocId),
|
|
||||||
fetchAndSummarise(shiftsNoFilter),
|
|
||||||
fetchAndSummarise(tsRange),
|
|
||||||
])
|
])
|
||||||
|
|
||||||
// E: Per-day timesheets (slow — one request per day — only run if range endpoint 404'd)
|
|
||||||
let e = null
|
|
||||||
if (d.error) {
|
|
||||||
e = await fetchTimesheetsByDay(from, to, locationId).catch(err => ({ error: err.message }))
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from, to,
|
from, to,
|
||||||
current_method: 'A: shifts with report_location_id',
|
location_id: locationId,
|
||||||
A_shifts_report_location_id: a,
|
location_dept_count: locationDeptIds ? locationDeptIds.size : 'all',
|
||||||
B_shifts_location_id: b,
|
A_shifts_report_location_id: {
|
||||||
C_shifts_no_location_filter: c,
|
...a,
|
||||||
D_timesheets_range: d,
|
note: 'Old sync method — shifts endpoint with report_location_id filter',
|
||||||
E_timesheets_per_day: e ?? { skipped: 'D succeeded — per-day fetch not needed' },
|
},
|
||||||
wf_report_reference: {
|
B_timesheets_per_day_no_leave: {
|
||||||
note: 'Paste WF Cost by Location and Team report totals here for comparison',
|
...b,
|
||||||
scheduled_exc_leave: null,
|
note: 'PBI method — timesheets/on/{date}, shifts expanded, leave excluded (leave_request_id!=null), filtered to this hotel\'s dept IDs. Target: WF Timesheet exc.leave inc.allowances',
|
||||||
timesheet_exc_leave: null,
|
},
|
||||||
leave_cost: null,
|
wf_report_reference: {
|
||||||
|
note: 'Compare B.cost against WF "Timesheet exc. leave inc. allowances" figure',
|
||||||
|
scheduled_exc_leave: null,
|
||||||
|
timesheet_exc_leave_inc_all: null,
|
||||||
|
leave_accrual: null,
|
||||||
},
|
},
|
||||||
note: 'baseCost = wage only; totalCost = with estimated employer on-costs. Match totalCost against the WF Timesheet Cost column to find the right endpoint.',
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue