Allow user-configurable invoice numbering start
Add a per-user invoice_starting_number setting (default 1) instead of always starting invoice numbering at 001. Adds /api/settings endpoints, a user_settings table, and a Číslování faktur panel in the account menu where the user can pick the starting number for a year with no invoices yet. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+37
-2
@@ -227,6 +227,33 @@ app.put('/api/supplier', requireAuth, async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/settings', requireAuth, async (req, res, next) => {
|
||||
try {
|
||||
const result = await pool.query('SELECT invoice_starting_number FROM user_settings WHERE user_id = $1', [req.userId]);
|
||||
res.json({ settings: { invoiceStartingNumber: result.rowCount ? result.rows[0].invoice_starting_number : 1 } });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/api/settings', requireAuth, async (req, res, next) => {
|
||||
try {
|
||||
const invoiceStartingNumber = Number.parseInt(req.body.invoiceStartingNumber, 10);
|
||||
if (!Number.isInteger(invoiceStartingNumber) || invoiceStartingNumber < 1) {
|
||||
res.status(400).json({ error: 'Počáteční číslo faktury musí být kladné celé číslo.' });
|
||||
return;
|
||||
}
|
||||
const result = await pool.query(`
|
||||
INSERT INTO user_settings (user_id, invoice_starting_number) VALUES ($1, $2)
|
||||
ON CONFLICT (user_id) DO UPDATE SET invoice_starting_number = EXCLUDED.invoice_starting_number
|
||||
RETURNING invoice_starting_number
|
||||
`, [req.userId, invoiceStartingNumber]);
|
||||
res.json({ settings: { invoiceStartingNumber: result.rows[0].invoice_starting_number } });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/clients', requireAuth, async (req, res, next) => {
|
||||
try {
|
||||
const result = await pool.query('SELECT * FROM clients WHERE user_id = $1 ORDER BY created_at, id', [req.userId]);
|
||||
@@ -376,13 +403,21 @@ app.post('/api/invoices', requireAuth, async (req, res, next) => {
|
||||
|
||||
const invoiceYear = new Date().getFullYear();
|
||||
await client.query('BEGIN');
|
||||
const settingsResult = await client.query(
|
||||
'SELECT invoice_starting_number FROM user_settings WHERE user_id = $1',
|
||||
[req.userId]
|
||||
);
|
||||
const invoiceStartingNumber = settingsResult.rowCount ? settingsResult.rows[0].invoice_starting_number : 1;
|
||||
await client.query(`
|
||||
INSERT INTO invoice_counters (user_id, invoice_year, last_number)
|
||||
SELECT $1, $2, COALESCE(MAX((substring(invoice_number FROM '^F-[0-9]{4}-([0-9]+)$'))::integer), 0)
|
||||
SELECT $1, $2, GREATEST(
|
||||
COALESCE(MAX((substring(invoice_number FROM '^F-[0-9]{4}-([0-9]+)$'))::integer), 0),
|
||||
$4 - 1
|
||||
)
|
||||
FROM invoices
|
||||
WHERE user_id = $1 AND invoice_number LIKE $3
|
||||
ON CONFLICT (user_id, invoice_year) DO NOTHING
|
||||
`, [req.userId, invoiceYear, `F-${invoiceYear}-%`]);
|
||||
`, [req.userId, invoiceYear, `F-${invoiceYear}-%`, invoiceStartingNumber]);
|
||||
const counterResult = await client.query(`
|
||||
UPDATE invoice_counters
|
||||
SET last_number = last_number + 1
|
||||
|
||||
Reference in New Issue
Block a user