department filter and excel support
This commit is contained in:
@@ -394,6 +394,7 @@ export default function () {
|
||||
const wantXlsx = String(req.query.format || 'csv').toLowerCase() === 'xlsx'
|
||||
|
||||
let workerIdClause = ''
|
||||
let departmentClause = ''
|
||||
const params = [`${startDate} 00:00:00`, `${endDate} 23:59:59`]
|
||||
|
||||
if (workerIds) {
|
||||
@@ -406,6 +407,12 @@ export default function () {
|
||||
}
|
||||
}
|
||||
|
||||
const { department } = req.query
|
||||
if (department) {
|
||||
departmentClause = ` AND LOWER(w.department) = LOWER(?)`
|
||||
params.push(department)
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT
|
||||
cr.worker_id,
|
||||
@@ -418,7 +425,7 @@ export default function () {
|
||||
FROM clock_records cr
|
||||
JOIN workers w ON cr.worker_id = w.id
|
||||
LEFT JOIN qr_codes qc ON cr.qr_code_id = qc.id
|
||||
WHERE cr.timestamp BETWEEN ? AND ? ${workerIdClause}
|
||||
WHERE cr.timestamp BETWEEN ? AND ? ${workerIdClause}${departmentClause}
|
||||
AND cr.event_type IN ('clock_in','clock_out')
|
||||
ORDER BY cr.worker_id, cr.timestamp ASC
|
||||
`
|
||||
@@ -850,9 +857,16 @@ export default function () {
|
||||
let whereClauses = ["w.role = 'worker'", "w.status != 'deleted'"] // Filter out soft-deleted workers
|
||||
|
||||
if (search) {
|
||||
whereClauses.push(`(w.full_name LIKE ? OR w.department LIKE ?)`)
|
||||
params.push(searchTerm, searchTerm)
|
||||
countParams.push(searchTerm, searchTerm)
|
||||
whereClauses.push(`w.full_name LIKE ?`)
|
||||
params.push(searchTerm)
|
||||
countParams.push(searchTerm)
|
||||
}
|
||||
|
||||
const { department } = req.query
|
||||
if (department) {
|
||||
whereClauses.push(`LOWER(w.department) = LOWER(?)`)
|
||||
params.push(department)
|
||||
countParams.push(department)
|
||||
}
|
||||
|
||||
if (whereClauses.length > 0) {
|
||||
@@ -1479,6 +1493,28 @@ export default function () {
|
||||
db.release()
|
||||
}
|
||||
})
|
||||
// GET distinct departments for filter tabs
|
||||
router.get('/departments', checkPermission('view_all'), async (req, res) => {
|
||||
const db = await getConnection()
|
||||
try {
|
||||
const [rows] = await db.execute(`
|
||||
SELECT DISTINCT department
|
||||
FROM workers
|
||||
WHERE role = 'worker'
|
||||
AND status != 'deleted'
|
||||
AND department IS NOT NULL
|
||||
AND department != ''
|
||||
ORDER BY department ASC
|
||||
`)
|
||||
const departments = rows.map((r) => r.department)
|
||||
res.json(departments)
|
||||
} catch (error) {
|
||||
console.error('Get departments error:', error)
|
||||
res.status(500).json({ message: 'Database error fetching departments.', details: error.message })
|
||||
} finally {
|
||||
db.release()
|
||||
}
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user