plugin-financials/routes/reports.js
2025-11-03 13:51:33 +02:00

174 lines
No EOL
4.5 KiB
JavaScript

const express = require('express');
const router = express.Router();
const { reportRepository } = require('../repositories');
/**
* GET /api/plugins/financials/reports
* Get all generated reports for site
*/
router.get('/', async (req, res) => {
try {
const { site_id } = req.query;
if (!site_id) {
return res.status(400).json({
success: false,
error: 'Missing required parameter: site_id'
});
}
const reports = await reportRepository.findBySiteId(site_id);
res.json({
success: true,
data: reports,
message: 'Reports retrieved successfully'
});
} catch (error) {
console.error('Failed to get reports:', error);
res.status(500).json({
success: false,
error: 'Failed to retrieve reports',
message: error.message
});
}
});
/**
* GET /api/plugins/financials/reports/trial-balance
* Generate trial balance report
*/
router.get('/trial-balance', async (req, res) => {
try {
const { site_id, start_date, end_date } = req.query;
if (!site_id) {
return res.status(400).json({
success: false,
error: 'Missing required parameter: site_id'
});
}
const options = {};
if (start_date) options.startDate = start_date;
if (end_date) options.endDate = end_date;
const trialBalance = await reportRepository.generateTrialBalance(site_id, options);
// Store report in database
await reportRepository.createReport({
site_id,
report_type: 'trial_balance',
report_name: `Trial Balance - ${options.endDate || 'All Time'}`,
report_date: options.endDate || new Date().toISOString().split('T')[0],
parameters: { ...options },
generated_by: 'system'
});
res.json({
success: true,
data: trialBalance,
message: 'Trial balance generated successfully'
});
} catch (error) {
console.error('Failed to generate trial balance:', error);
res.status(500).json({
success: false,
error: 'Failed to generate trial balance',
message: error.message
});
}
});
/**
* GET /api/plugins/financials/reports/balance-sheet
* Generate balance sheet
*/
router.get('/balance-sheet', async (req, res) => {
try {
const { site_id, end_date } = req.query;
if (!site_id) {
return res.status(400).json({
success: false,
error: 'Missing required parameter: site_id'
});
}
const options = {};
if (end_date) options.endDate = end_date;
const balanceSheet = await reportRepository.generateBalanceSheet(site_id, options);
// Store report in database
await reportRepository.createReport({
site_id,
report_type: 'balance_sheet',
report_name: `Balance Sheet - ${options.endDate || 'As of Today'}`,
report_date: options.endDate || new Date().toISOString().split('T')[0],
parameters: { ...options },
generated_by: 'system'
});
res.json({
success: true,
data: balanceSheet,
message: 'Balance sheet generated successfully'
});
} catch (error) {
console.error('Failed to generate balance sheet:', error);
res.status(500).json({
success: false,
error: 'Failed to generate balance sheet',
message: error.message
});
}
});
/**
* GET /api/plugins/financials/reports/income-statement
* Generate income statement
*/
router.get('/income-statement', async (req, res) => {
try {
const { site_id, start_date, end_date } = req.query;
if (!site_id) {
return res.status(400).json({
success: false,
error: 'Missing required parameter: site_id'
});
}
const options = {};
if (start_date) options.startDate = start_date;
if (end_date) options.endDate = end_date;
const incomeStatement = await reportRepository.generateIncomeStatement(site_id, options);
// Store report in database
await reportRepository.createReport({
site_id,
report_type: 'income_statement',
report_name: `Income Statement - ${options.startDate} to ${options.endDate}`,
report_date: options.end_date || new Date().toISOString().split('T')[0],
parameters: { ...options },
generated_by: 'system'
});
res.json({
success: true,
data: incomeStatement,
message: 'Income statement generated successfully'
});
} catch (error) {
console.error('Failed to generate income statement:', error);
res.status(500).json({
success: false,
error: 'Failed to generate income statement',
message: error.message
});
}
});
module.exports = router;