feat(accounting): re-bill proof attachment, CRM panel & hours↔re-bills cross-add (#979)
Closes #866. Three features, all behind the `incomingInvoices` feature flag: 1. Attach the stored supplier proof PDF to the client-invoice email when a captured invoice is re-billed/passed through, as a SEPARATE attachment so invoice immutability holds. Global default (off), per-customer tri-state override, and per-file selection in a new Send dialog. A missing proof at issue time stamps inbound_documents.proof_attach_error rather than silently dropping, and never blocks the send. Proof filename is a configurable template with {INVOICE} {SUPPLIER} {YEAR} {MONTH} {SEQ}/{SEQ:0Nd} tokens. 2. Re-bills & passthrough panel under CRM → Customer, grouped Open/Sent/Paid with status derived from the linked invoice lifecycle rather than a duplicated column. 3. Cross-add dialog rolling open hours and open re-bills into one invoice, symmetric from both entry points. The two stay distinct, contiguous line groups — never merged into shared line items. Migration 169 is additive, hasColumn-guarded and idempotent. Review (two rounds) closed two concerns: - Storno stranding: nothing cleared inbound_documents.billed_invoice_id when a covering invoice was cancelled, so a Storno'd re-bill showed as Open in the new panel while every billing path filters on that column being NULL — the supplier cost could never be re-billed. releaseRebillsForCancelledInvoice now detaches the linkage on both invoice-cancel paths, with a regression test on the issued-cancel path. - Permission gating: the new controls rendered on data presence alone while their endpoints require accounting.view / accounting.manage / customers.edit. Now gated at both the query and render layers. Known follow-up: two cross-add counter queries are gated on a permission their endpoint does not check (HoursSection.tsx:174, CustomerCrmPanels.tsx:270) — degrades safely, one line each.
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
/**
|
||||
* Issue #866 — the createInvoice-free halves of the re-bill proof + CRM panel
|
||||
* feature, against a real SQLite schema:
|
||||
*
|
||||
* • listCustomerRebills — status DERIVED from the linked invoice lifecycle
|
||||
* (open / sent / paid; a cancelled/Storno'd cover drops back to open) plus
|
||||
* cost-vs-rebilled math and mode.
|
||||
* • collectRebillProofAttachments — the Send-dialog per-file selection, the
|
||||
* all-or-none default resolution (per-customer override else global), the
|
||||
* Beleg-<inv#> filename (suffix only when >1), and the missing-file marker.
|
||||
*
|
||||
* The invoice-MINTING paths (billCombinedForCustomer / billPendingRebills) call
|
||||
* createInvoice inside a db.transaction, which deadlocks on the SQLite harness
|
||||
* (global-db sequence write vs. held write lock) — same limitation the sibling
|
||||
* incomingInvoiceRebill.test.js documents. They're covered by the existing
|
||||
* billPendingRebills / billUnbilledEntries suites; here we hand-craft billed
|
||||
* state instead.
|
||||
*/
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { bootCrmDb, seedMinimal } = require('./helpers/crmDb');
|
||||
|
||||
jest.setTimeout(120000);
|
||||
|
||||
describe('#866 re-bill proof attachment + CRM panel', () => {
|
||||
let db;
|
||||
let cleanup;
|
||||
let adminId;
|
||||
let expenseService;
|
||||
let rebillProofs;
|
||||
let flagCache;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ db, cleanup } = await bootCrmDb());
|
||||
const dbModule = require('../../src/database/db');
|
||||
dbModule.logActivity = async () => {};
|
||||
({ adminId } = await seedMinimal(db));
|
||||
expenseService = require('../../src/services/expenseService');
|
||||
rebillProofs = require('../../src/services/invoice/rebillProofs');
|
||||
flagCache = require('../../src/middleware/requireFeatureFlag');
|
||||
}, 120000);
|
||||
|
||||
afterAll(async () => {
|
||||
if (cleanup) await cleanup();
|
||||
});
|
||||
|
||||
const unwrapId = (ins) => (typeof ins[0] === 'object' ? ins[0].id : ins[0]);
|
||||
let seq = 0;
|
||||
|
||||
async function makeCustomer(overrides = {}) {
|
||||
seq += 1;
|
||||
const ins = await db('customer_accounts').insert({
|
||||
email: `c866-${seq}@example.com`,
|
||||
display_name: `C866 ${seq}`,
|
||||
password_hash: 'x',
|
||||
preferred_language: 'de',
|
||||
is_active: 1,
|
||||
billing_cadence: 'per_event',
|
||||
created_at: new Date(),
|
||||
...overrides,
|
||||
}).returning('id');
|
||||
return unwrapId(ins);
|
||||
}
|
||||
|
||||
async function makeDoc(customerId, overrides = {}) {
|
||||
const ins = await db('inbound_documents').insert({
|
||||
source: 'upload', status: 'categorized', parse_status: 'parsed', parse_method: 'none',
|
||||
supplier_name: 'ACME AG', currency: 'CHF', total_amount_minor: 10000,
|
||||
invoice_date: '2026-06-01', disposition: 'rebill', customer_account_id: customerId,
|
||||
created_at: new Date(), updated_at: new Date(),
|
||||
...overrides,
|
||||
}).returning('id');
|
||||
return unwrapId(ins);
|
||||
}
|
||||
|
||||
async function makeInvoice(customerId, status, number) {
|
||||
const ins = await db('invoices').insert({
|
||||
invoice_number: number,
|
||||
customer_account_id: customerId,
|
||||
status,
|
||||
currency: 'CHF',
|
||||
issue_date: '2026-06-01', due_date: '2026-07-01',
|
||||
vat_rate: 0, net_amount_minor: 10000, vat_amount_minor: 0, total_amount_minor: 10000,
|
||||
created_at: new Date(), updated_at: new Date(),
|
||||
}).returning('id');
|
||||
return unwrapId(ins);
|
||||
}
|
||||
|
||||
describe('listCustomerRebills', () => {
|
||||
it('derives open / sent / paid and open→cost==rebilled for passthrough, +markup for rebill', async () => {
|
||||
const customerId = await makeCustomer();
|
||||
|
||||
// Open re-bill (10% markup): rebilled = 11000.
|
||||
await makeDoc(customerId, { total_amount_minor: 10000, markup_type: 'percent', markup_percent: 10 });
|
||||
// Open passthrough: no markup, rebilled == cost.
|
||||
await makeDoc(customerId, { disposition: 'durchlaufend', total_amount_minor: 5000, markup_type: 'none' });
|
||||
// Sent (on a 'sent' invoice).
|
||||
const sentInv = await makeInvoice(customerId, 'sent', 'R-2026-0001');
|
||||
await makeDoc(customerId, { total_amount_minor: 8000, markup_type: 'none', billed_invoice_id: sentInv });
|
||||
// Paid.
|
||||
const paidInv = await makeInvoice(customerId, 'paid', 'R-2026-0002');
|
||||
await makeDoc(customerId, { total_amount_minor: 8000, markup_type: 'none', billed_invoice_id: paidInv });
|
||||
// Cancelled cover → drops back to 'open', no invoice link surfaced.
|
||||
const cancInv = await makeInvoice(customerId, 'cancelled', 'R-2026-0003');
|
||||
await makeDoc(customerId, { total_amount_minor: 8000, markup_type: 'none', billed_invoice_id: cancInv });
|
||||
|
||||
const items = await expenseService.listCustomerRebills(customerId);
|
||||
const byStatus = (s) => items.filter((r) => r.status === s);
|
||||
|
||||
expect(items).toHaveLength(5);
|
||||
expect(byStatus('open')).toHaveLength(3); // 2 genuinely-open + 1 cancelled-cover
|
||||
expect(byStatus('sent')).toHaveLength(1);
|
||||
expect(byStatus('paid')).toHaveLength(1);
|
||||
|
||||
const rebill = items.find((r) => r.mode === 'rebill' && r.costMinor === 10000);
|
||||
expect(rebill.rebilledMinor).toBe(11000);
|
||||
const passthrough = items.find((r) => r.mode === 'passthrough');
|
||||
expect(passthrough.rebilledMinor).toBe(passthrough.costMinor);
|
||||
|
||||
const sent = byStatus('sent')[0];
|
||||
expect(sent.invoiceNumber).toBe('R-2026-0001');
|
||||
expect(sent.invoiceId).toBe(sentInv);
|
||||
|
||||
const cancelledCover = items.find((r) => r.status === 'open' && r.invoiceNumber === null && r.costMinor === 8000);
|
||||
expect(cancelledCover).toBeDefined(); // cancelled cover isn't shown as a live invoice link
|
||||
});
|
||||
});
|
||||
|
||||
describe('storno releases the re-bill linkage (#866 review)', () => {
|
||||
it("clears billed_invoice_id so a Storno'd cover returns to the billable pool", async () => {
|
||||
const invoiceService = require('../../src/services/invoiceService');
|
||||
const customerId = await makeCustomer();
|
||||
const invId = await makeInvoice(customerId, 'sent', 'R-2026-9000');
|
||||
const lineIns = await db('invoice_line_items').insert({
|
||||
invoice_id: invId, position: 1, quantity: 1, description: 'Rebill',
|
||||
unit_price_minor: 8000, discount_percent: 0, line_total_minor: 8000,
|
||||
}).returning('id');
|
||||
const lineId = unwrapId(lineIns);
|
||||
const docId = await makeDoc(customerId, {
|
||||
total_amount_minor: 8000, markup_type: 'none', billed_invoice_id: invId, billed_invoice_line_item_id: lineId,
|
||||
});
|
||||
|
||||
// Storno claims a fresh number from document_sequences; the other tests
|
||||
// seed explicit R-2026-000x numbers without advancing it, so push the
|
||||
// counter past them to avoid a number collision (a test artifact — real
|
||||
// invoices always claim through the sequence).
|
||||
await db('document_sequences').insert({ kind: 'invoice', year: 2026, current_value: 9000, created_at: new Date(), updated_at: new Date() })
|
||||
.onConflict(['kind', 'year']).ignore();
|
||||
await db('document_sequences').where({ kind: 'invoice', year: 2026 }).update({ current_value: 9000 });
|
||||
|
||||
// Storno the covering invoice (the issued-cancel path).
|
||||
await db.transaction(async (trx) => invoiceService.createStorno(invId, adminId, trx));
|
||||
|
||||
const doc = await db('inbound_documents').where({ id: docId }).first();
|
||||
expect(doc.billed_invoice_id).toBeNull();
|
||||
expect(doc.billed_invoice_line_item_id).toBeNull();
|
||||
|
||||
// It now surfaces as a genuinely-open item AND the pending pool picks it up.
|
||||
const items = await expenseService.listCustomerRebills(customerId);
|
||||
const row = items.find((r) => r.id === docId);
|
||||
expect(row.status).toBe('open');
|
||||
expect(row.invoiceId).toBeNull();
|
||||
const pending = await db('inbound_documents')
|
||||
.where({ customer_account_id: customerId }).whereNull('billed_invoice_id')
|
||||
.whereIn('disposition', ['rebill', 'durchlaufend']).where('status', 'categorized');
|
||||
expect(pending.map((p) => p.id)).toContain(docId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('collectRebillProofAttachments', () => {
|
||||
const businessDocs = () => path.join(process.env.STORAGE_PATH, 'business-docs', 'inbound', '2026');
|
||||
|
||||
async function enableIncoming() {
|
||||
const existing = await db('feature_flags').where({ key: 'incomingInvoices' }).first();
|
||||
if (existing) await db('feature_flags').where({ key: 'incomingInvoices' }).update({ value: 1 });
|
||||
else await db('feature_flags').insert({ key: 'incomingInvoices', value: 1 });
|
||||
flagCache.invalidateFeatureFlagCache();
|
||||
}
|
||||
|
||||
function writeProof(name) {
|
||||
fs.mkdirSync(businessDocs(), { recursive: true });
|
||||
const p = path.join(businessDocs(), name);
|
||||
fs.writeFileSync(p, '%PDF-1.4\n% test proof\n');
|
||||
return p;
|
||||
}
|
||||
|
||||
it('honours explicit selection, names Beleg-<inv#>, and marks a missing file', async () => {
|
||||
await enableIncoming();
|
||||
const customerId = await makeCustomer();
|
||||
const invId = await makeInvoice(customerId, 'scheduled', 'R-2026-1000');
|
||||
const invoice = await db('invoices').where({ id: invId }).first();
|
||||
|
||||
const good1 = await makeDoc(customerId, { billed_invoice_id: invId, file_path: writeProof('p1.pdf') });
|
||||
const good2 = await makeDoc(customerId, { billed_invoice_id: invId, file_path: writeProof('p2.pdf') });
|
||||
const missing = await makeDoc(customerId, { billed_invoice_id: invId, file_path: path.join(businessDocs(), 'nope.pdf') });
|
||||
|
||||
// Select the two good proofs → two attachments, suffixed because >1.
|
||||
const both = await rebillProofs.collectRebillProofAttachments(invoice, null, [good1, good2]);
|
||||
expect(both.map((a) => a.filename).sort()).toEqual(['Beleg-R-2026-1000-1.pdf', 'Beleg-R-2026-1000-2.pdf']);
|
||||
|
||||
// Select exactly one → single, unsuffixed.
|
||||
const one = await rebillProofs.collectRebillProofAttachments(invoice, null, [good1]);
|
||||
expect(one).toHaveLength(1);
|
||||
expect(one[0].filename).toBe('Beleg-R-2026-1000.pdf');
|
||||
|
||||
// Select the missing-file doc → no attachment, but a marker is persisted.
|
||||
const none = await rebillProofs.collectRebillProofAttachments(invoice, null, [missing]);
|
||||
expect(none).toHaveLength(0);
|
||||
const markerRow = await db('inbound_documents').where({ id: missing }).first('proof_attach_error');
|
||||
expect(markerRow.proof_attach_error).toBeTruthy();
|
||||
// A successful attach clears any prior marker.
|
||||
await rebillProofs.collectRebillProofAttachments(invoice, null, [good1]);
|
||||
const cleared = await db('inbound_documents').where({ id: good1 }).first('proof_attach_error');
|
||||
expect(cleared.proof_attach_error).toBeNull();
|
||||
});
|
||||
|
||||
it('resolves the all-or-none default from the per-customer override then global', async () => {
|
||||
await enableIncoming();
|
||||
const customerId = await makeCustomer();
|
||||
const invId = await makeInvoice(customerId, 'scheduled', 'R-2026-2000');
|
||||
const invoice = await db('invoices').where({ id: invId }).first();
|
||||
await makeDoc(customerId, { billed_invoice_id: invId, file_path: writeProof('d1.pdf') });
|
||||
|
||||
// Global default off, no override → none.
|
||||
const off = await rebillProofs.collectRebillProofAttachments(invoice, { rebill_attach_proof: null }, undefined);
|
||||
expect(off).toHaveLength(0);
|
||||
|
||||
// Per-customer override ON → all, regardless of the (off) global.
|
||||
const on = await rebillProofs.collectRebillProofAttachments(invoice, { rebill_attach_proof: true }, undefined);
|
||||
expect(on).toHaveLength(1);
|
||||
|
||||
// Global ON (no override) → all.
|
||||
await db('app_settings').insert({ setting_key: 'accounting_rebill_attach_proof', setting_value: JSON.stringify(true), setting_type: 'accounting' });
|
||||
const globalOn = await rebillProofs.collectRebillProofAttachments(invoice, { rebill_attach_proof: null }, undefined);
|
||||
expect(globalOn).toHaveLength(1);
|
||||
// Override OFF beats global ON.
|
||||
const overrideOff = await rebillProofs.collectRebillProofAttachments(invoice, { rebill_attach_proof: false }, undefined);
|
||||
expect(overrideOff).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('attaches nothing when the incoming-invoices flag is off', async () => {
|
||||
const existing = await db('feature_flags').where({ key: 'incomingInvoices' }).first();
|
||||
if (existing) await db('feature_flags').where({ key: 'incomingInvoices' }).update({ value: 0 });
|
||||
else await db('feature_flags').insert({ key: 'incomingInvoices', value: 0 });
|
||||
flagCache.invalidateFeatureFlagCache();
|
||||
|
||||
const customerId = await makeCustomer();
|
||||
const invId = await makeInvoice(customerId, 'scheduled', 'R-2026-3000');
|
||||
const invoice = await db('invoices').where({ id: invId }).first();
|
||||
const doc = await makeDoc(customerId, { billed_invoice_id: invId, file_path: writeProof('f1.pdf') });
|
||||
|
||||
const res = await rebillProofs.collectRebillProofAttachments(invoice, { rebill_attach_proof: true }, [doc]);
|
||||
expect(res).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* renderProofName — the configurable Beleg proof-attachment filename template.
|
||||
* Pure function; no DB. Covers token substitution, the multi-proof index
|
||||
* fallback, padding, and filesystem-safe sanitisation.
|
||||
*/
|
||||
const { renderProofName } = require('../../src/services/invoice/rebillProofs');
|
||||
|
||||
describe('renderProofName', () => {
|
||||
const base = { invoiceNumber: 'R-2026-0042', supplierName: 'ACME AG', seq: 1, hasMulti: false, issueDate: '2026-08-03' };
|
||||
|
||||
it('defaults to Beleg-<invoice>.pdf', () => {
|
||||
expect(renderProofName('Beleg-{INVOICE}', base)).toBe('Beleg-R-2026-0042.pdf');
|
||||
expect(renderProofName('', base)).toBe('Beleg-R-2026-0042.pdf');
|
||||
expect(renderProofName(null, base)).toBe('Beleg-R-2026-0042.pdf');
|
||||
});
|
||||
|
||||
it('substitutes every token incl. padded SEQ and date parts', () => {
|
||||
expect(renderProofName('{SUPPLIER}-{INVOICE}-{YEAR}{MONTH}-{SEQ:03d}', { ...base, seq: 7 }))
|
||||
.toBe('ACME-AG-R-2026-0042-202608-007.pdf');
|
||||
});
|
||||
|
||||
it('appends an index for multiple proofs only when the template has no {SEQ}', () => {
|
||||
// No {SEQ} + multi → auto-suffixed with the index.
|
||||
expect(renderProofName('Beleg-{INVOICE}', { ...base, seq: 2, hasMulti: true })).toBe('Beleg-R-2026-0042-2.pdf');
|
||||
// Single proof → no suffix.
|
||||
expect(renderProofName('Beleg-{INVOICE}', { ...base, seq: 1, hasMulti: false })).toBe('Beleg-R-2026-0042.pdf');
|
||||
// Explicit {SEQ} → no double index even when multi.
|
||||
expect(renderProofName('Beleg-{INVOICE}-{SEQ}', { ...base, seq: 2, hasMulti: true })).toBe('Beleg-R-2026-0042-2.pdf');
|
||||
});
|
||||
|
||||
it('sanitises unsafe characters and slashes, and always ends in a single .pdf', () => {
|
||||
expect(renderProofName('Beleg {INVOICE}', { ...base, invoiceNumber: '2026/0042' })).toBe('Beleg-2026-0042.pdf');
|
||||
// Author-supplied extension is stripped and re-added (no double .pdf).
|
||||
expect(renderProofName('{INVOICE}.pdf', base)).toBe('R-2026-0042.pdf');
|
||||
// Falls back to 'Beleg' if the template renders empty after sanitising.
|
||||
expect(renderProofName('{SUPPLIER}', { ...base, supplierName: '///' })).toBe('Beleg.pdf');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Migration 169: re-bill proof-attachment support (issue #866).
|
||||
*
|
||||
* - inbound_documents.proof_attach_error : best-effort failure marker. When a
|
||||
* re-billed supplier invoice's stored
|
||||
* proof PDF is missing/unreadable at
|
||||
* the moment the client invoice is
|
||||
* issued, we DON'T silently drop it —
|
||||
* we stamp the reason here so the
|
||||
* re-bill row in CRM → Customer shows
|
||||
* a recovery banner.
|
||||
* - customer_accounts.rebill_attach_proof: per-customer tri-state override for
|
||||
* "attach the supplier proof to the
|
||||
* client-invoice email".
|
||||
* NULL = inherit the global default
|
||||
* true = always attach
|
||||
* false = never attach
|
||||
* The global default itself lives in
|
||||
* app_settings (accounting_rebill_
|
||||
* attach_proof, default off) and needs
|
||||
* no seed row — an absent key coerces
|
||||
* to false, exactly like
|
||||
* accounting_require_proof.
|
||||
*
|
||||
* Additive + hasColumn-guarded so re-runs are safe.
|
||||
*/
|
||||
async function addColumn(knex, table, column, builder) {
|
||||
if (!(await knex.schema.hasColumn(table, column))) {
|
||||
await knex.schema.alterTable(table, builder);
|
||||
}
|
||||
}
|
||||
|
||||
exports.up = async function (knex) {
|
||||
if (await knex.schema.hasTable('inbound_documents')) {
|
||||
await addColumn(knex, 'inbound_documents', 'proof_attach_error', (t) => t.text('proof_attach_error'));
|
||||
}
|
||||
if (await knex.schema.hasTable('customer_accounts')) {
|
||||
// Nullable boolean = tri-state (NULL inherit / true on / false off).
|
||||
await addColumn(knex, 'customer_accounts', 'rebill_attach_proof', (t) => t.boolean('rebill_attach_proof').nullable());
|
||||
}
|
||||
};
|
||||
|
||||
exports.down = async function (knex) {
|
||||
if (await knex.schema.hasTable('inbound_documents') && await knex.schema.hasColumn('inbound_documents', 'proof_attach_error')) {
|
||||
await knex.schema.alterTable('inbound_documents', (t) => t.dropColumn('proof_attach_error'));
|
||||
}
|
||||
if (await knex.schema.hasTable('customer_accounts') && await knex.schema.hasColumn('customer_accounts', 'rebill_attach_proof')) {
|
||||
await knex.schema.alterTable('customer_accounts', (t) => t.dropColumn('rebill_attach_proof'));
|
||||
}
|
||||
};
|
||||
@@ -19,9 +19,13 @@ const { db } = require('../database/db');
|
||||
// frontend already hides the surface). Per-customer enforcement stays in
|
||||
// customerHoursService.createEntry.
|
||||
const requireHoursLogging = requireFeatureFlag('hoursLogging', 'HOURS_LOGGING_DISABLED');
|
||||
// Combined hours+re-bills billing (#866) is introduced by the re-bill feature;
|
||||
// gate it behind incoming-invoices (no re-bills to combine when it's off).
|
||||
const requireIncoming = requireFeatureFlag('incomingInvoices', 'INCOMING_INVOICES_DISABLED');
|
||||
const { handleAsync, validateRequest, successResponse } = require('../utils/routeHelpers');
|
||||
const customerAccountsService = require('../services/customerAccountsService');
|
||||
const customerHoursService = require('../services/customerHoursService');
|
||||
const combinedBillingService = require('../services/combinedBillingService');
|
||||
const invoiceService = require('../services/invoiceService');
|
||||
const { IDENTITY_PRESERVING_NORMALIZE_EMAIL } = require('../utils/emailNormalization');
|
||||
|
||||
@@ -83,6 +87,10 @@ function transformCustomer(c) {
|
||||
// this customer's invoices qualify for an early-payment discount,
|
||||
// regardless of template / global defaults.
|
||||
skontoDisabled: c.skonto_disabled === true || c.skonto_disabled === 1,
|
||||
// Per-customer re-bill proof-attachment override (migration 169, #866).
|
||||
// Tri-state: null = inherit the global default, true = always attach,
|
||||
// false = never attach the supplier proof to the client-invoice email.
|
||||
rebillAttachProof: c.rebill_attach_proof == null ? null : (c.rebill_attach_proof === true || c.rebill_attach_proof === 1),
|
||||
lastLogin: c.last_login,
|
||||
createdAt: c.created_at,
|
||||
updatedAt: c.updated_at,
|
||||
@@ -413,6 +421,9 @@ router.put('/:id', [
|
||||
.withMessage('billing_cycle_day must be -15..-1 (days before month end) or 1..28 (day of month)'),
|
||||
// Per-customer Skonto opt-out (migration 112).
|
||||
body('skonto_disabled').optional().isBoolean(),
|
||||
// Per-customer re-bill proof-attachment override (migration 169, #866).
|
||||
// Nullable tri-state: null clears the override (inherit global default).
|
||||
body('rebill_attach_proof').optional({ nullable: true }).isBoolean(),
|
||||
], handleAsync(async (req, res) => {
|
||||
validateRequest(req);
|
||||
const customer = await customerAccountsService.updateCustomer(
|
||||
@@ -685,6 +696,25 @@ router.post('/:id/hour-entries/bill', [
|
||||
successResponse(res, result, 201);
|
||||
}));
|
||||
|
||||
// Combined hours + re-bills → one invoice (#866, Feature 3). Used by the
|
||||
// cross-add dialog when a per-event customer has open items in both categories.
|
||||
router.post('/:id/bill-combined', [
|
||||
adminAuth,
|
||||
requireIncoming,
|
||||
requirePermission('customers.edit'),
|
||||
param('id').isInt({ min: 1 }),
|
||||
body('includeHours').optional().isBoolean(),
|
||||
body('includeRebills').optional().isBoolean(),
|
||||
], handleAsync(async (req, res) => {
|
||||
validateRequest(req);
|
||||
const result = await combinedBillingService.billCombinedForCustomer(
|
||||
parseInt(req.params.id, 10),
|
||||
{ includeHours: req.body.includeHours !== false, includeRebills: req.body.includeRebills !== false },
|
||||
req.admin.id,
|
||||
);
|
||||
successResponse(res, result, 201);
|
||||
}));
|
||||
|
||||
function transformHourEntry(h) {
|
||||
return {
|
||||
id: h.id,
|
||||
|
||||
@@ -112,6 +112,13 @@ router.post('/inbound/bill-pending', requireIncoming, requirePermission('account
|
||||
[body('customerAccountId').isInt({ min: 1 })],
|
||||
handleAsync(async (req, res) => { validateRequest(req); return successResponse(res, await expenseService.billPendingRebills(toInt(req.body.customerAccountId), req.admin.id), 201, 'Re-billed'); }));
|
||||
|
||||
// Re-bill / passthrough items for one customer, with derived status (open /
|
||||
// sent / paid) — feeds the CRM → Customer panel (#866, Feature 2). Registered
|
||||
// BEFORE /inbound/:id so the literal path wins.
|
||||
router.get('/inbound/by-customer/:customerAccountId', requireIncoming, requirePermission('accounting.view'),
|
||||
[param('customerAccountId').isInt({ min: 1 })],
|
||||
handleAsync(async (req, res) => { validateRequest(req); return successResponse(res, { items: await expenseService.listCustomerRebills(toInt(req.params.customerAccountId)) }); }));
|
||||
|
||||
router.get('/inbound/:id/file', requireIncoming, requirePermission('accounting.view'),
|
||||
[param('id').isInt({ min: 1 })],
|
||||
handleAsync(async (req, res) => {
|
||||
|
||||
@@ -28,9 +28,13 @@ const { requirePermission } = require('../middleware/permissions');
|
||||
const { handleAsync, validateRequest, successResponse } = require('../utils/routeHelpers');
|
||||
const { getStoragePath } = require('../config/storage');
|
||||
const invoiceService = require('../services/invoiceService');
|
||||
const expenseService = require('../services/expenseService');
|
||||
const { requireFeatureFlag } = require('../middleware/requireFeatureFlag');
|
||||
const { db } = require('../database/db');
|
||||
|
||||
const router = express.Router();
|
||||
// Re-bill proof endpoints (#866) are behind the incoming-invoices flag.
|
||||
const requireIncoming = requireFeatureFlag('incomingInvoices', 'INCOMING_INVOICES_DISABLED');
|
||||
|
||||
// PR #603 review follow-up #2 — bound payment dates. `isISO8601()` alone
|
||||
// accepts year 1900/9999; cash-basis revenue keys on paid_at, so a typo
|
||||
@@ -753,13 +757,36 @@ router.put(
|
||||
|
||||
// ---- send / pay / remind / cancel ------------------------------------
|
||||
|
||||
router.post(
|
||||
'/:id/send',
|
||||
requirePermission('bills.manage'),
|
||||
// Re-bill proofs attached to this (not-yet-sent) invoice + the resolved attach
|
||||
// default — powers the Send dialog's per-file proof selection (#866). Behind the
|
||||
// incoming-invoices flag; bills.view since it's part of the invoice send flow.
|
||||
router.get(
|
||||
'/:id/rebill-proofs',
|
||||
requireIncoming,
|
||||
requirePermission('bills.view'),
|
||||
[param('id').isInt({ min: 1 })],
|
||||
handleAsync(async (req, res) => {
|
||||
validateRequest(req);
|
||||
await invoiceService.sendInvoice(parseInt(req.params.id, 10), req.admin.id);
|
||||
return successResponse(res, await expenseService.listInvoiceRebillProofs(parseInt(req.params.id, 10)));
|
||||
})
|
||||
);
|
||||
|
||||
router.post(
|
||||
'/:id/send',
|
||||
requirePermission('bills.manage'),
|
||||
[
|
||||
param('id').isInt({ min: 1 }),
|
||||
// Optional per-file re-bill proof selection (#866). Array of inbound
|
||||
// document ids the admin chose to attach; omitted → resolved default.
|
||||
body('proofInboundIds').optional({ nullable: true }).isArray(),
|
||||
body('proofInboundIds.*').isInt({ min: 1 }),
|
||||
],
|
||||
handleAsync(async (req, res) => {
|
||||
validateRequest(req);
|
||||
const proofInboundIds = Array.isArray(req.body.proofInboundIds)
|
||||
? req.body.proofInboundIds.map((n) => parseInt(n, 10)).filter(Number.isInteger)
|
||||
: undefined;
|
||||
await invoiceService.sendInvoice(parseInt(req.params.id, 10), req.admin.id, { proofInboundIds });
|
||||
return successResponse(res, { sent: true });
|
||||
})
|
||||
);
|
||||
|
||||
@@ -300,6 +300,28 @@ router.put('/accounting', adminAuth, requirePermission('settings.edit'), async (
|
||||
setting_type: 'accounting',
|
||||
});
|
||||
}
|
||||
// Global default for "attach the supplier proof PDF to the client-invoice
|
||||
// email when a re-bill/passthrough is issued" (issue #866). Off by default;
|
||||
// a per-customer override (customer_accounts.rebill_attach_proof) and the
|
||||
// per-file selection in the Send dialog both build on top of this default.
|
||||
if (Object.prototype.hasOwnProperty.call(req.body, 'accounting_rebill_attach_proof')) {
|
||||
updates.push({
|
||||
setting_key: 'accounting_rebill_attach_proof',
|
||||
setting_value: JSON.stringify(!!req.body.accounting_rebill_attach_proof),
|
||||
setting_type: 'accounting',
|
||||
});
|
||||
}
|
||||
// Filename template for the attached supplier proof (like the invoice/quote
|
||||
// number formats). Tokens: {INVOICE} {SUPPLIER} {YEAR} {MONTH} {SEQ}/{SEQ:0Nd}.
|
||||
// Empty falls back to the default at render time.
|
||||
if (Object.prototype.hasOwnProperty.call(req.body, 'crm_rebill_proof_filename_format')) {
|
||||
const fmt = String(req.body.crm_rebill_proof_filename_format || '').trim().slice(0, 120);
|
||||
updates.push({
|
||||
setting_key: 'crm_rebill_proof_filename_format',
|
||||
setting_value: JSON.stringify(fmt),
|
||||
setting_type: 'accounting',
|
||||
});
|
||||
}
|
||||
// VAT registration + reclaim. `registered` drives whether output VAT applies
|
||||
// + whether input VAT is deductible; `reclaim_countries` = the ISO-2 list of
|
||||
// countries whose input VAT can be reclaimed (drives cost tax-treatment +
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
// Combined hours + re-bills billing (issue #866, Feature 3).
|
||||
//
|
||||
// When a per-event customer has BOTH open hour entries AND open re-bills, the
|
||||
// admin can roll them into ONE client invoice. Hours and re-bills are NEVER
|
||||
// merged into shared line items — they stay as distinct lines, grouped
|
||||
// contiguously (hours first, then re-bills) with no section headers (product
|
||||
// decision). Each source row is stamped with its own invoice line so the CRM
|
||||
// panels keep deriving status correctly.
|
||||
//
|
||||
// Reuses the extracted build/stamp helpers from customerHoursService and
|
||||
// expenseService so there is one code path for line-item construction and
|
||||
// stamping. Lives in its own module to avoid a require cycle between those two
|
||||
// services.
|
||||
|
||||
const { db, logActivity } = require('../database/db');
|
||||
const { AppError } = require('../utils/errors');
|
||||
const invoiceService = require('./invoiceService');
|
||||
const customerHoursService = require('./customerHoursService');
|
||||
const expenseService = require('./expenseService');
|
||||
|
||||
/**
|
||||
* Bundle open hours and/or open re-bills for a per-event customer into one
|
||||
* invoice. At least one side must be requested AND non-empty.
|
||||
*
|
||||
* @param customerId
|
||||
* @param opts.includeHours include unbilled hour entries
|
||||
* @param opts.includeRebills include pending rebill/passthrough documents
|
||||
* @returns { invoiceId, entriesBilled, rebillsBilled }
|
||||
*/
|
||||
async function billCombinedForCustomer(customerId, { includeHours, includeRebills }, adminId) {
|
||||
const customer = await db('customer_accounts').where({ id: customerId }).first();
|
||||
if (!customer) throw new AppError('Customer not found', 404);
|
||||
// Both underlying flows are per-event only (accumulator cadences auto-bill on
|
||||
// save/categorise); keep the combined path consistent.
|
||||
if (customer.billing_cadence === 'monthly' || customer.billing_cadence === 'manual') {
|
||||
throw new AppError(
|
||||
'Accumulator-mode customers (monthly / manual) bill automatically; combining is for per-event customers.',
|
||||
409, 'CADENCE_MISMATCH',
|
||||
);
|
||||
}
|
||||
if (!includeHours && !includeRebills) {
|
||||
throw new AppError('Nothing selected to bill', 400, 'NOTHING_SELECTED');
|
||||
}
|
||||
|
||||
let logInfo = null;
|
||||
const result = await db.transaction(async (trx) => {
|
||||
const hours = includeHours
|
||||
? await customerHoursService.buildUnbilledHourLineItems(trx, customer)
|
||||
: { entries: [], lineItems: [] };
|
||||
const rebills = includeRebills
|
||||
? await expenseService.buildPendingRebillLineItems(trx, customer)
|
||||
: { docs: [], lineItems: [] };
|
||||
|
||||
if (hours.entries.length === 0 && rebills.docs.length === 0) {
|
||||
throw new AppError('No open hours or re-bills to bill', 409, 'NO_OPEN_ITEMS');
|
||||
}
|
||||
|
||||
// Contiguous, hours first then re-bills. Positions run 1..N across both
|
||||
// groups so each source row maps to exactly one invoice line.
|
||||
const lineItems = [];
|
||||
let pos = 0;
|
||||
for (const li of hours.lineItems) { pos += 1; lineItems.push({ ...li, position: pos }); }
|
||||
const hoursCount = pos;
|
||||
for (const li of rebills.lineItems) { pos += 1; lineItems.push({ ...li, position: pos }); }
|
||||
|
||||
const { invoiceIds } = await invoiceService.createInvoice({
|
||||
customerAccountId: customer.id,
|
||||
lineItems,
|
||||
}, adminId, trx);
|
||||
const invoiceId = invoiceIds[0];
|
||||
|
||||
const insertedLines = await trx('invoice_line_items').where({ invoice_id: invoiceId }).orderBy('position', 'asc');
|
||||
const lineByPos = new Map(insertedLines.map((li) => [li.position, li.id]));
|
||||
|
||||
if (hours.entries.length) {
|
||||
const hourLineIds = hours.entries.map((_, i) => lineByPos.get(i + 1) || null);
|
||||
await customerHoursService.stampBilledEntries(trx, hours.entries, invoiceId, hourLineIds);
|
||||
}
|
||||
if (rebills.docs.length) {
|
||||
const rebillLineIds = rebills.docs.map((_, i) => lineByPos.get(hoursCount + i + 1) || null);
|
||||
await expenseService.stampBilledRebills(trx, rebills.docs, invoiceId, rebillLineIds);
|
||||
}
|
||||
|
||||
logInfo = {
|
||||
meta: { customerId: customer.id, invoiceId, entriesBilled: hours.entries.length, rebillsBilled: rebills.docs.length },
|
||||
};
|
||||
return { invoiceId, entriesBilled: hours.entries.length, rebillsBilled: rebills.docs.length };
|
||||
});
|
||||
// Audit log after commit (global-db write deadlocks inside the SQLite trx).
|
||||
if (logInfo) {
|
||||
try {
|
||||
await logActivity('combined_hours_rebills_billed', logInfo.meta, null, `admin:${adminId}`);
|
||||
} catch (_) { /* audit log is best-effort */ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = { billCombinedForCustomer };
|
||||
@@ -581,6 +581,11 @@ async function updateCustomer(id, updates, updatedByAdminId) {
|
||||
// Per-customer Skonto opt-out (migration 112). Boolean, coerced
|
||||
// via formatBoolean below for SQLite compatibility.
|
||||
'skonto_disabled',
|
||||
// Per-customer re-bill proof-attachment override (migration 169, #866).
|
||||
// Tri-state: null = inherit global default, true/false = force. Handled
|
||||
// in its own branch below so null survives (formatBoolean would coerce
|
||||
// it to false and silently lose the "inherit" state).
|
||||
'rebill_attach_proof',
|
||||
];
|
||||
for (const f of fields) {
|
||||
if (updates[f] !== undefined) {
|
||||
@@ -597,6 +602,10 @@ async function updateCustomer(id, updates, updatedByAdminId) {
|
||||
|| f === 'skonto_disabled'
|
||||
) {
|
||||
allowed[f] = formatBoolean(updates[f]);
|
||||
} else if (f === 'rebill_attach_proof') {
|
||||
// Tri-state override. null/'' → NULL (inherit global default);
|
||||
// otherwise a real boolean (coerced for SQLite).
|
||||
allowed[f] = (updates[f] === null || updates[f] === '') ? null : formatBoolean(updates[f]);
|
||||
} else if (f === 'hourly_rate_minor') {
|
||||
// Default hourly rate. Null clears it (forces per-entry
|
||||
// overrides); otherwise coerce to a non-negative bigint-safe
|
||||
|
||||
@@ -447,6 +447,38 @@ async function deleteEntry(entryId, adminId) {
|
||||
* onto the running draft on save, so there should be no unbilled rows.
|
||||
* Returns the new invoice id.
|
||||
*/
|
||||
// Load this customer's unbilled hour entries and build their invoice line items
|
||||
// (no `position` yet — the caller assigns it, so hours can be combined
|
||||
// contiguously with re-bills in one invoice; #866). Shared by the hours-only
|
||||
// path and the combined orchestrator.
|
||||
async function buildUnbilledHourLineItems(trx, customer) {
|
||||
const entries = await trx('customer_hour_entries')
|
||||
.where({ customer_account_id: customer.id, status: 'unbilled' })
|
||||
.orderBy('entry_date', 'asc').orderBy('start_time', 'asc');
|
||||
const installDefaultMinor = await getInstallDefaultRateMinor(trx);
|
||||
const lineItems = entries.map((entry) => {
|
||||
const rate = resolveEffectiveRate(entry, customer, installDefaultMinor);
|
||||
return buildLineItemFromEntry(entry, rate);
|
||||
});
|
||||
return { entries, lineItems };
|
||||
}
|
||||
|
||||
// Stamp each hour entry with the invoice + its specific line-item id. `lineIds`
|
||||
// is aligned to `entries` order.
|
||||
async function stampBilledEntries(trx, entries, invoiceId, lineIds) {
|
||||
const now = new Date();
|
||||
for (let i = 0; i < entries.length; i += 1) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await trx('customer_hour_entries').where({ id: entries[i].id }).update({
|
||||
status: 'billed',
|
||||
invoice_id: invoiceId,
|
||||
invoice_line_item_id: lineIds[i] || null,
|
||||
billed_at: now,
|
||||
updated_at: now,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function billUnbilledEntries(customerId, adminId) {
|
||||
const customer = await db('customer_accounts').where({ id: customerId }).first();
|
||||
if (!customer) throw new AppError('Customer not found', 404);
|
||||
@@ -460,19 +492,11 @@ async function billUnbilledEntries(customerId, adminId) {
|
||||
|
||||
let logInfo = null; // logged after commit — see createEntry note.
|
||||
const result = await db.transaction(async (trx) => {
|
||||
const unbilled = await trx('customer_hour_entries')
|
||||
.where({ customer_account_id: customer.id, status: 'unbilled' })
|
||||
.orderBy('entry_date', 'asc').orderBy('start_time', 'asc');
|
||||
const { entries: unbilled, lineItems: rawLines } = await buildUnbilledHourLineItems(trx, customer);
|
||||
if (unbilled.length === 0) {
|
||||
throw new AppError('No unbilled entries to bill', 409, 'NO_UNBILLED');
|
||||
}
|
||||
|
||||
const installDefaultMinor = await getInstallDefaultRateMinor(trx);
|
||||
const lineItems = unbilled.map((entry, idx) => {
|
||||
const rate = resolveEffectiveRate(entry, customer, installDefaultMinor);
|
||||
const li = buildLineItemFromEntry(entry, rate);
|
||||
return { ...li, position: idx + 1 };
|
||||
});
|
||||
const lineItems = rawLines.map((li, idx) => ({ ...li, position: idx + 1 }));
|
||||
|
||||
// No installment metadata — hour-billing always mints a single
|
||||
// standalone invoice. createInvoice returns `{ invoiceIds: [N] }`
|
||||
@@ -491,19 +515,8 @@ async function billUnbilledEntries(customerId, adminId) {
|
||||
.where({ invoice_id: invoiceId })
|
||||
.orderBy('position', 'asc');
|
||||
const lineByPos = new Map(insertedLines.map((li) => [li.position, li.id]));
|
||||
|
||||
const now = new Date();
|
||||
for (let i = 0; i < unbilled.length; i += 1) {
|
||||
const entry = unbilled[i];
|
||||
const lineItemId = lineByPos.get(i + 1) || null;
|
||||
await trx('customer_hour_entries').where({ id: entry.id }).update({
|
||||
status: 'billed',
|
||||
invoice_id: invoiceId,
|
||||
invoice_line_item_id: lineItemId,
|
||||
billed_at: now,
|
||||
updated_at: now,
|
||||
});
|
||||
}
|
||||
const lineIds = unbilled.map((_, i) => lineByPos.get(i + 1) || null);
|
||||
await stampBilledEntries(trx, unbilled, invoiceId, lineIds);
|
||||
|
||||
logInfo = { type: 'hour_entries_billed', meta: { customerId: customer.id, invoiceId, entryCount: unbilled.length } };
|
||||
return { invoiceId, entriesBilled: unbilled.length };
|
||||
@@ -590,6 +603,9 @@ module.exports = {
|
||||
updateEntry,
|
||||
deleteEntry,
|
||||
billUnbilledEntries,
|
||||
// Shared with the combined hours+re-bills orchestrator (#866).
|
||||
buildUnbilledHourLineItems,
|
||||
stampBilledEntries,
|
||||
getInstallDefaultRateMinor,
|
||||
_internal: {
|
||||
computeDurationMinutes,
|
||||
|
||||
@@ -21,6 +21,9 @@ const { db, logActivity } = require('../database/db');
|
||||
const { AppError } = require('../utils/errors');
|
||||
const logger = require('../utils/logger');
|
||||
const invoiceService = require('./invoiceService');
|
||||
// Tri-state proof-attach resolver — single source of truth lives with the
|
||||
// send-time attachment logic (no require cycle: rebillProofs never imports us).
|
||||
const { resolveDefaultAttach } = require('./invoice/rebillProofs');
|
||||
|
||||
/**
|
||||
* Actor for logActivity. `adminId` is legitimately absent on automated paths —
|
||||
@@ -54,7 +57,7 @@ function toIsoDate(v) {
|
||||
// ── Accounting settings (app_settings, type 'accounting') ───────────────────
|
||||
async function getAccountingSettings() {
|
||||
const keys = ['accounting_km_rate_minor', 'accounting_per_diem_rate_minor', 'accounting_require_proof',
|
||||
'accounting_vat_reclaim_countries'];
|
||||
'accounting_vat_reclaim_countries', 'accounting_rebill_attach_proof'];
|
||||
let rows = [];
|
||||
try {
|
||||
rows = await db('app_settings').whereIn('setting_key', keys).select('setting_key', 'setting_value');
|
||||
@@ -69,6 +72,8 @@ async function getAccountingSettings() {
|
||||
kmRateMinor: Number.isFinite(Number(map.accounting_km_rate_minor)) ? Number(map.accounting_km_rate_minor) : 0,
|
||||
perDiemRateMinor: Number.isFinite(Number(map.accounting_per_diem_rate_minor)) ? Number(map.accounting_per_diem_rate_minor) : 0,
|
||||
requireProof: map.accounting_require_proof === true || map.accounting_require_proof === 1 || map.accounting_require_proof === '1',
|
||||
// Global default for attaching supplier proof PDFs on re-bill invoices (#866).
|
||||
rebillAttachProof: map.accounting_rebill_attach_proof === true || map.accounting_rebill_attach_proof === 1 || map.accounting_rebill_attach_proof === '1',
|
||||
vatReclaimCountries: Array.isArray(map.accounting_vat_reclaim_countries)
|
||||
? map.accounting_vat_reclaim_countries.map((c) => String(c || '').toUpperCase()) : [],
|
||||
};
|
||||
@@ -423,6 +428,14 @@ async function categorizeInbound(id, payload, adminId) {
|
||||
if (!row) throw new AppError('Incoming invoice not found', 404, 'INBOUND_NOT_FOUND');
|
||||
const doc = transformInbound(row);
|
||||
|
||||
// A doc attached to a customer will become a client invoice line, which
|
||||
// needs an amount. Require one NOW (0 is fine — a legitimately zero-value
|
||||
// pass-through — but null is not) rather than letting a value-less item sit
|
||||
// PENDING and blow up the whole bundle later at bill time.
|
||||
if (customerAccountId && doc.totalAmountMinor == null && doc.netAmountMinor == null) {
|
||||
throw new AppError('Set the invoice amount before re-billing (0 is allowed).', 400, 'AMOUNT_REQUIRED');
|
||||
}
|
||||
|
||||
// #1: unwind any prior re-bill so the disposition can change.
|
||||
if (doc.billedInvoiceId) await unwindBilledLine(trx, doc);
|
||||
|
||||
@@ -483,6 +496,11 @@ async function rebillInbound(id, payload, adminId, trx0) {
|
||||
const row = await trx('inbound_documents').where({ id }).first();
|
||||
if (!row) throw new AppError('Incoming invoice not found', 404, 'INBOUND_NOT_FOUND');
|
||||
const doc = transformInbound(row);
|
||||
// A client invoice line needs an amount (0 allowed, null not) — fail here
|
||||
// rather than deep inside buildInboundLineItem.
|
||||
if (doc.totalAmountMinor == null && doc.netAmountMinor == null) {
|
||||
throw new AppError('Set the invoice amount before re-billing (0 is allowed).', 400, 'AMOUNT_REQUIRED');
|
||||
}
|
||||
if (doc.billedInvoiceId) await unwindBilledLine(trx, doc);
|
||||
const markup = await resolveMarkup(
|
||||
{ markupType: doc.markupType, markupPercent: doc.markupPercent, markupFlatMinor: doc.markupFlatMinor },
|
||||
@@ -559,6 +577,44 @@ async function listPendingRebillSummary() {
|
||||
return Array.from(byCustomer.values()).sort((a, b) => b.openAmountMinor - a.openAmountMinor);
|
||||
}
|
||||
|
||||
// Load this customer's pending (categorised-but-unbilled) rebill/passthrough
|
||||
// documents and build their invoice line items (no `position` yet — the caller
|
||||
// assigns it, so re-bills can be combined contiguously with hours in one
|
||||
// invoice; #866). Shared by the re-bills-only path and the combined orchestrator.
|
||||
async function buildPendingRebillLineItems(trx, customer) {
|
||||
const docs = await trx('inbound_documents')
|
||||
.where({ customer_account_id: customer.id })
|
||||
.whereNull('billed_invoice_id')
|
||||
.whereIn('disposition', CUSTOMER_DISPOSITIONS)
|
||||
.where('status', 'categorized')
|
||||
.orderBy('invoice_date', 'asc').orderBy('id', 'asc');
|
||||
const lineItems = [];
|
||||
for (let i = 0; i < docs.length; i += 1) {
|
||||
const doc = transformInbound(docs[i]);
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const markup = await resolveMarkup(
|
||||
{ markupType: doc.markupType, markupPercent: doc.markupPercent, markupFlatMinor: doc.markupFlatMinor },
|
||||
null, null, trx,
|
||||
);
|
||||
lineItems.push(buildInboundLineItem(doc, doc.disposition, markup));
|
||||
}
|
||||
return { docs, lineItems };
|
||||
}
|
||||
|
||||
// Stamp each inbound document with the invoice + its specific line-item id.
|
||||
// `lineIds` is aligned to `docs` order.
|
||||
async function stampBilledRebills(trx, docs, invoiceId, lineIds) {
|
||||
const now = new Date();
|
||||
for (let i = 0; i < docs.length; i += 1) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await trx('inbound_documents').where({ id: docs[i].id }).update({
|
||||
billed_invoice_id: invoiceId,
|
||||
billed_invoice_line_item_id: lineIds[i] || null,
|
||||
updated_at: now,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-event flow: bundle all pending rebill/passthrough documents for a
|
||||
* customer into ONE invoice, one line per document. Refuses for monthly/manual
|
||||
@@ -576,24 +632,9 @@ async function billPendingRebills(customerId, adminId) {
|
||||
}
|
||||
|
||||
const result = await db.transaction(async (trx) => {
|
||||
const pending = await trx('inbound_documents')
|
||||
.where({ customer_account_id: customer.id })
|
||||
.whereNull('billed_invoice_id')
|
||||
.whereIn('disposition', CUSTOMER_DISPOSITIONS)
|
||||
.where('status', 'categorized')
|
||||
.orderBy('invoice_date', 'asc').orderBy('id', 'asc');
|
||||
const { docs: pending, lineItems: rawLines } = await buildPendingRebillLineItems(trx, customer);
|
||||
if (pending.length === 0) throw new AppError('No pending re-bills to bill', 409, 'NO_PENDING');
|
||||
|
||||
const lineItems = [];
|
||||
for (let i = 0; i < pending.length; i += 1) {
|
||||
const doc = transformInbound(pending[i]);
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const markup = await resolveMarkup(
|
||||
{ markupType: doc.markupType, markupPercent: doc.markupPercent, markupFlatMinor: doc.markupFlatMinor },
|
||||
null, null, trx,
|
||||
);
|
||||
lineItems.push({ ...buildInboundLineItem(doc, doc.disposition, markup), position: i + 1 });
|
||||
}
|
||||
const lineItems = rawLines.map((li, idx) => ({ ...li, position: idx + 1 }));
|
||||
|
||||
const { invoiceIds } = await invoiceService.createInvoice({
|
||||
customerAccountId: customer.id,
|
||||
@@ -603,15 +644,8 @@ async function billPendingRebills(customerId, adminId) {
|
||||
|
||||
const insertedLines = await trx('invoice_line_items').where({ invoice_id: invoiceId }).orderBy('position', 'asc');
|
||||
const lineByPos = new Map(insertedLines.map((li) => [li.position, li.id]));
|
||||
const now = new Date();
|
||||
for (let i = 0; i < pending.length; i += 1) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await trx('inbound_documents').where({ id: pending[i].id }).update({
|
||||
billed_invoice_id: invoiceId,
|
||||
billed_invoice_line_item_id: lineByPos.get(i + 1) || null,
|
||||
updated_at: now,
|
||||
});
|
||||
}
|
||||
const lineIds = pending.map((_, i) => lineByPos.get(i + 1) || null);
|
||||
await stampBilledRebills(trx, pending, invoiceId, lineIds);
|
||||
|
||||
return { invoiceId, count: pending.length };
|
||||
});
|
||||
@@ -620,6 +654,106 @@ async function billPendingRebills(customerId, adminId) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Derive a re-bill row's status from its linked client-invoice lifecycle — no
|
||||
// duplicated status column (avoids drift, #866). A covering invoice that was
|
||||
// cancelled (Storno) drops back to 'open' so storno'd items never inflate the
|
||||
// sent/paid aggregates.
|
||||
function deriveRebillStatus(billedInvoiceId, invoiceStatus) {
|
||||
if (!billedInvoiceId) return 'open';
|
||||
if (invoiceStatus === 'paid') return 'paid';
|
||||
if (invoiceStatus === 'cancelled') return 'open';
|
||||
return 'sent'; // scheduled / sent / overdue
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-bill / passthrough items for one customer (issue #866, Feature 2). One row
|
||||
* per captured supplier invoice attached to this customer as a rebill/
|
||||
* passthrough, with cost vs re-billed amount (incl. markup), mode, linked event
|
||||
* + client invoice, and a status DERIVED from the invoice lifecycle. History-
|
||||
* only — feeds the CRM → Customer panel.
|
||||
*/
|
||||
async function listCustomerRebills(customerId) {
|
||||
const rows = await db('inbound_documents as d')
|
||||
.leftJoin('invoices as inv', 'd.billed_invoice_id', 'inv.id')
|
||||
.leftJoin('events as e', 'd.event_id', 'e.id')
|
||||
.where('d.customer_account_id', customerId)
|
||||
.whereIn('d.disposition', CUSTOMER_DISPOSITIONS)
|
||||
.where('d.status', 'categorized')
|
||||
.orderBy('d.invoice_date', 'desc').orderBy('d.id', 'desc')
|
||||
.select(
|
||||
'd.id', 'd.supplier_name', 'd.invoice_date', 'd.currency',
|
||||
'd.net_amount_minor', 'd.total_amount_minor', 'd.disposition',
|
||||
'd.markup_type', 'd.markup_percent', 'd.markup_flat_minor',
|
||||
'd.billed_invoice_id', 'd.proof_attach_error', 'd.file_path', 'd.event_id',
|
||||
'e.event_name as event_name',
|
||||
'inv.invoice_number as invoice_number', 'inv.status as invoice_status',
|
||||
);
|
||||
|
||||
return rows.map((r) => {
|
||||
const base = r.total_amount_minor != null ? Number(r.total_amount_minor)
|
||||
: (r.net_amount_minor != null ? Number(r.net_amount_minor) : 0);
|
||||
const isPassthrough = r.disposition === 'durchlaufend';
|
||||
// Passthrough is invoiced at cost (VAT-neutral, no markup); re-bill carries
|
||||
// the stored markup snapshot.
|
||||
const markup = isPassthrough ? { type: 'none', percent: null, flatMinor: null } : {
|
||||
type: MARKUP_TYPES.includes(r.markup_type) ? r.markup_type : 'none',
|
||||
percent: r.markup_percent != null ? Number(r.markup_percent) : null,
|
||||
flatMinor: Number.isInteger(r.markup_flat_minor) ? r.markup_flat_minor : null,
|
||||
};
|
||||
const status = deriveRebillStatus(r.billed_invoice_id, r.invoice_status);
|
||||
const billed = status !== 'open';
|
||||
return {
|
||||
id: r.id,
|
||||
supplierName: r.supplier_name || null,
|
||||
date: toIsoDate(r.invoice_date),
|
||||
currency: r.currency || null,
|
||||
costMinor: base,
|
||||
rebilledMinor: base + computeMarkupMinor(base, markup),
|
||||
mode: isPassthrough ? 'passthrough' : 'rebill',
|
||||
eventId: r.event_id || null,
|
||||
eventName: r.event_name || null,
|
||||
hasProof: !!r.file_path,
|
||||
proofAttachError: r.proof_attach_error || null,
|
||||
status,
|
||||
invoiceId: billed ? r.billed_invoice_id : null,
|
||||
invoiceNumber: billed ? (r.invoice_number || null) : null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-bill proofs attached to ONE (not-yet-sent) client invoice, for the Send
|
||||
* dialog's per-file selection (#866, Feature 1). Also returns the resolved
|
||||
* attach default (per-customer override else global) so the dialog can
|
||||
* pre-check the boxes.
|
||||
*/
|
||||
async function listInvoiceRebillProofs(invoiceId) {
|
||||
const rows = await db('inbound_documents')
|
||||
.where({ billed_invoice_id: invoiceId })
|
||||
.whereIn('disposition', CUSTOMER_DISPOSITIONS)
|
||||
.orderBy('id', 'asc')
|
||||
.select('id', 'supplier_name', 'original_filename', 'file_path', 'currency',
|
||||
'net_amount_minor', 'total_amount_minor', 'disposition', 'proof_attach_error');
|
||||
const proofs = rows.map((r) => ({
|
||||
id: r.id,
|
||||
supplierName: r.supplier_name || null,
|
||||
filename: r.original_filename || null,
|
||||
hasProof: !!r.file_path,
|
||||
currency: r.currency || null,
|
||||
amountMinor: r.total_amount_minor != null ? Number(r.total_amount_minor)
|
||||
: (r.net_amount_minor != null ? Number(r.net_amount_minor) : 0),
|
||||
mode: r.disposition === 'durchlaufend' ? 'passthrough' : 'rebill',
|
||||
proofAttachError: r.proof_attach_error || null,
|
||||
}));
|
||||
|
||||
const inv = await db('invoices').where({ id: invoiceId }).first('customer_account_id');
|
||||
const customer = inv && inv.customer_account_id
|
||||
? await db('customer_accounts').where({ id: inv.customer_account_id }).first('rebill_attach_proof')
|
||||
: null;
|
||||
const { rebillAttachProof } = await getAccountingSettings();
|
||||
return { proofs, attachDefault: resolveDefaultAttach(customer, rebillAttachProof) };
|
||||
}
|
||||
|
||||
/** Mark the supplier paid on the incoming invoice (the payable lives here). */
|
||||
async function markInboundSupplierPayment(id, { paid, paidAt, paymentMethod, paymentReference }, adminId) {
|
||||
await getInbound(id);
|
||||
@@ -833,6 +967,11 @@ module.exports = {
|
||||
rebillInbound,
|
||||
listPendingRebillSummary,
|
||||
billPendingRebills,
|
||||
listCustomerRebills,
|
||||
listInvoiceRebillProofs,
|
||||
// Shared with the combined hours+re-bills orchestrator (#866).
|
||||
buildPendingRebillLineItems,
|
||||
stampBilledRebills,
|
||||
markInboundSupplierPayment,
|
||||
// expenses
|
||||
createExpense,
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
// Re-bill / passthrough proof attachments (issue #866).
|
||||
//
|
||||
// When a client invoice re-bills one or more captured supplier invoices
|
||||
// (inbound_documents.billed_invoice_id → this invoice), the original stored
|
||||
// supplier PDF can ride along on the invoice email as a SEPARATE attachment
|
||||
// (the invoice PDF itself is never touched — invoice immutability). Whether a
|
||||
// given proof attaches is decided at ISSUE time:
|
||||
//
|
||||
// • Manual send → the admin's per-file selection from the Send dialog
|
||||
// (proofInboundIds), which defaults to the resolved toggle.
|
||||
// • Auto send → (scheduler / monthly flush, no admin present) the resolved
|
||||
// default: per-customer override (customer_accounts
|
||||
// .rebill_attach_proof, tri-state) else the global
|
||||
// accounting_rebill_attach_proof (default off).
|
||||
//
|
||||
// A selected proof whose file is missing/unreadable does NOT silently drop and
|
||||
// does NOT block the send — we stamp inbound_documents.proof_attach_error so the
|
||||
// re-bill row surfaces a recovery banner in CRM → Customer.
|
||||
//
|
||||
// Kept in its own module (not expenseService) to avoid the invoiceService ↔
|
||||
// expenseService require cycle.
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { db } = require('../../database/db');
|
||||
const logger = require('../../utils/logger');
|
||||
const { isFeatureEnabled } = require('../../middleware/requireFeatureFlag');
|
||||
const { assertPathInside } = require('../../utils/safePath');
|
||||
const { getStoragePath } = require('../../config/storage');
|
||||
|
||||
// Dispositions that re-bill/pass a supplier invoice to a client (mirrors
|
||||
// expenseService.CUSTOMER_DISPOSITIONS — duplicated as a 2-item constant rather
|
||||
// than imported, to keep this module free of the require cycle).
|
||||
const CUSTOMER_DISPOSITIONS = ['rebill', 'durchlaufend'];
|
||||
|
||||
const DEFAULT_PROOF_FILENAME_FORMAT = 'Beleg-{INVOICE}';
|
||||
|
||||
// Render a proof attachment filename from the admin-configurable template.
|
||||
// Tokens: {INVOICE} (client invoice number), {SUPPLIER}, {YEAR}, {MONTH},
|
||||
// {SEQ} / {SEQ:0Nd} (per-invoice proof index). Always yields a filesystem-safe
|
||||
// name ending in .pdf. When one invoice carries several proofs but the template
|
||||
// has no {SEQ}, an index is appended so the filenames stay unique.
|
||||
function renderProofName(format, { invoiceNumber, supplierName, seq, hasMulti, issueDate }) {
|
||||
const d = issueDate ? new Date(issueDate) : new Date();
|
||||
const year = Number.isNaN(d.getTime()) ? '' : String(d.getFullYear());
|
||||
const month = Number.isNaN(d.getTime()) ? '' : String(d.getMonth() + 1).padStart(2, '0');
|
||||
let hadSeq = false;
|
||||
let name = String(format || DEFAULT_PROOF_FILENAME_FORMAT)
|
||||
.replace(/\{INVOICE\}/g, invoiceNumber || 'invoice')
|
||||
.replace(/\{SUPPLIER\}/g, supplierName || '')
|
||||
.replace(/\{YEAR\}/g, year)
|
||||
.replace(/\{MONTH\}/g, month)
|
||||
.replace(/\{SEQ:(\d+)d\}/g, (_, p) => { hadSeq = true; return String(seq).padStart(parseInt(p, 10), '0'); })
|
||||
.replace(/\{SEQ\}/g, () => { hadSeq = true; return String(seq); });
|
||||
if (hasMulti && !hadSeq) name += `-${seq}`;
|
||||
// Filesystem-safe: drop any author-supplied extension, collapse whitespace +
|
||||
// unsafe chars to '-', trim stray separators. Some German schemes use '/'.
|
||||
name = name.replace(/\.pdf$/i, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^A-Za-z0-9._-]+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^[-.]+|[-.]+$/g, '');
|
||||
if (!name) name = 'Beleg';
|
||||
return `${name}.pdf`;
|
||||
}
|
||||
|
||||
async function readFilenameFormat() {
|
||||
try {
|
||||
const row = await db('app_settings').where({ setting_key: 'crm_rebill_proof_filename_format' }).first('setting_value');
|
||||
if (!row) return DEFAULT_PROOF_FILENAME_FORMAT;
|
||||
let v = row.setting_value;
|
||||
if (typeof v === 'string') { try { v = JSON.parse(v); } catch (_e) { /* keep raw */ } }
|
||||
return (typeof v === 'string' && v.trim()) ? v.trim() : DEFAULT_PROOF_FILENAME_FORMAT;
|
||||
} catch (_e) {
|
||||
return DEFAULT_PROOF_FILENAME_FORMAT;
|
||||
}
|
||||
}
|
||||
|
||||
async function readGlobalDefault() {
|
||||
try {
|
||||
const row = await db('app_settings').where({ setting_key: 'accounting_rebill_attach_proof' }).first('setting_value');
|
||||
if (!row) return false;
|
||||
let v = row.setting_value;
|
||||
if (typeof v === 'string') { try { v = JSON.parse(v); } catch (_e) { /* keep raw */ } }
|
||||
return v === true || v === 1 || v === '1';
|
||||
} catch (_e) {
|
||||
return false; // app_settings absent in some test harnesses → off
|
||||
}
|
||||
}
|
||||
|
||||
// Tri-state resolution: per-customer override wins; NULL/undefined inherits the
|
||||
// global default.
|
||||
function resolveDefaultAttach(customer, globalOn) {
|
||||
const ov = customer ? customer.rebill_attach_proof : null;
|
||||
if (ov === null || ov === undefined) return !!globalOn;
|
||||
return ov === true || ov === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the proof attachments for an invoice being issued, and persist per-row
|
||||
* failure markers. Returns an array of nodemailer-style attachment descriptors
|
||||
* ({ filename, contentPath, contentType }) — possibly empty. Never throws into
|
||||
* the send path.
|
||||
*
|
||||
* @param invoice the invoice row (needs id, invoice_number)
|
||||
* @param customer the customer_accounts row (for the tri-state override)
|
||||
* @param proofInboundIds optional explicit selection (manual send). When
|
||||
* omitted, the resolved default decides all-or-none.
|
||||
*/
|
||||
async function collectRebillProofAttachments(invoice, customer, proofInboundIds) {
|
||||
// Backend flag gate — no proof handling at all when incoming-invoices is off.
|
||||
if (!(await isFeatureEnabled('incomingInvoices'))) return [];
|
||||
|
||||
let rebillRows;
|
||||
try {
|
||||
rebillRows = await db('inbound_documents')
|
||||
.where({ billed_invoice_id: invoice.id })
|
||||
.whereIn('disposition', CUSTOMER_DISPOSITIONS)
|
||||
.select('id', 'file_path', 'supplier_name', 'original_filename');
|
||||
} catch (_e) {
|
||||
return []; // table absent (older install / test harness)
|
||||
}
|
||||
if (!rebillRows || rebillRows.length === 0) return [];
|
||||
|
||||
// Decide the set to attach.
|
||||
let selected;
|
||||
if (Array.isArray(proofInboundIds)) {
|
||||
// Manual send: attach exactly the admin's picks that actually belong to
|
||||
// this invoice's re-bill set (ignore anything foreign).
|
||||
const wanted = new Set(proofInboundIds.map((n) => parseInt(n, 10)).filter(Number.isInteger));
|
||||
selected = rebillRows.filter((r) => wanted.has(r.id));
|
||||
} else {
|
||||
// Auto send: all-or-none per the resolved default.
|
||||
const globalOn = await readGlobalDefault();
|
||||
selected = resolveDefaultAttach(customer, globalOn) ? rebillRows : [];
|
||||
}
|
||||
if (selected.length === 0) return [];
|
||||
|
||||
const businessDocs = path.join(getStoragePath(), 'business-docs');
|
||||
const format = await readFilenameFormat();
|
||||
const multi = selected.length > 1;
|
||||
const attachments = [];
|
||||
|
||||
for (let i = 0; i < selected.length; i += 1) {
|
||||
const row = selected[i];
|
||||
let markerErr = null;
|
||||
if (!row.file_path) {
|
||||
markerErr = 'proof file missing (no stored PDF on the supplier invoice)';
|
||||
} else {
|
||||
try {
|
||||
const safe = assertPathInside(row.file_path, [businessDocs]);
|
||||
if (!fs.existsSync(safe)) {
|
||||
markerErr = 'proof file not found on disk at issue time';
|
||||
} else {
|
||||
attachments.push({
|
||||
filename: renderProofName(format, {
|
||||
invoiceNumber: invoice.invoice_number,
|
||||
supplierName: row.supplier_name,
|
||||
seq: i + 1,
|
||||
hasMulti: multi,
|
||||
issueDate: invoice.issue_date,
|
||||
}),
|
||||
contentPath: safe,
|
||||
contentType: 'application/pdf',
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
markerErr = `proof path rejected: ${e.message}`;
|
||||
}
|
||||
}
|
||||
// Persist / clear the failure marker (best-effort; never blocks the send).
|
||||
try {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await db('inbound_documents').where({ id: row.id })
|
||||
.update({ proof_attach_error: markerErr, updated_at: new Date() });
|
||||
} catch (_e) { /* marker column may be absent pre-migration — ignore */ }
|
||||
if (markerErr) logger.warn?.(`rebillProofs: invoice ${invoice.invoice_number} inbound ${row.id}: ${markerErr}`);
|
||||
}
|
||||
|
||||
return attachments;
|
||||
}
|
||||
|
||||
module.exports = { collectRebillProofAttachments, resolveDefaultAttach, renderProofName };
|
||||
@@ -14,12 +14,19 @@ const { computeDueDate, ensureCustomerCanBill, formatMajor, getHierarchyHelpers,
|
||||
const { getInvoiceById } = require('./queries');
|
||||
const { createInvoice } = require('./create');
|
||||
const { buildInvoiceRenderContext } = require('./render');
|
||||
const { collectRebillProofAttachments } = require('./rebillProofs');
|
||||
|
||||
|
||||
/**
|
||||
* Send an invoice email + PDF. Flips status scheduled → sent.
|
||||
*
|
||||
* @param options.proofInboundIds optional explicit re-bill proof selection
|
||||
* (issue #866). Set by the manual Send dialog so the admin picks which
|
||||
* supplier proofs ride the email — all, some, or none. When omitted
|
||||
* (auto-send / scheduler) the resolved per-customer/global default
|
||||
* decides all-or-none.
|
||||
*/
|
||||
async function sendInvoice(id, adminId) {
|
||||
async function sendInvoice(id, adminId, options = {}) {
|
||||
const data = await getInvoiceById(id);
|
||||
if (!data) throw new AppError('Invoice not found', 404);
|
||||
const { invoice, lineItems } = data;
|
||||
@@ -118,6 +125,23 @@ async function sendInvoice(id, adminId) {
|
||||
});
|
||||
|
||||
const { to: invoiceTo, cc: invoiceCc } = resolveBillingRecipients(customer, invoice.cc_pdf_email);
|
||||
|
||||
// Re-bill/passthrough proof attachments (#866). Separate attachments — the
|
||||
// invoice PDF above is never touched. Selection comes from the Send dialog on
|
||||
// a manual send; auto-sends fall back to the resolved default. Best-effort:
|
||||
// a missing proof marks the re-bill row but never blocks the send.
|
||||
const invoiceAttachments = [{
|
||||
filename: `${invoice.invoice_number}.pdf`,
|
||||
contentPath: pdfPath,
|
||||
contentType: 'application/pdf',
|
||||
}];
|
||||
try {
|
||||
const proofs = await collectRebillProofAttachments(invoice, customer, options.proofInboundIds);
|
||||
if (proofs.length) invoiceAttachments.push(...proofs);
|
||||
} catch (e) {
|
||||
logger.warn?.(`sendInvoice: proof attachment collection failed for ${invoice.invoice_number}: ${e.message}`);
|
||||
}
|
||||
|
||||
await emailProcessor.queueEmail(invoice.event_id || null, invoiceTo, 'invoice_sent', {
|
||||
invoice_number: invoice.invoice_number,
|
||||
customer_name: customer.display_name || customer.first_name || customer.email.split('@')[0],
|
||||
@@ -131,11 +155,7 @@ async function sendInvoice(id, adminId) {
|
||||
// above) rather than the event-first default resolution.
|
||||
__language: ctx.locale,
|
||||
cc: invoiceCc,
|
||||
attachments: [{
|
||||
filename: `${invoice.invoice_number}.pdf`,
|
||||
contentPath: pdfPath,
|
||||
contentType: 'application/pdf',
|
||||
}],
|
||||
attachments: invoiceAttachments,
|
||||
});
|
||||
|
||||
try { await logActivity('invoice_sent', { invoiceId: id }, invoice.event_id || null, `admin:${adminId}`); } catch (_) {}
|
||||
@@ -185,6 +205,26 @@ async function sendInvoice(id, adminId) {
|
||||
* cancellation itself; the storno sits in `status='scheduled'`
|
||||
* and the cron picks it up.
|
||||
*/
|
||||
/**
|
||||
* When an invoice is cancelled, detach any re-billed/passed-through supplier
|
||||
* invoices linked to it (#866 review). Nothing else clears
|
||||
* inbound_documents.billed_invoice_id, so without this a Storno'd cover would
|
||||
* strand the supplier cost: the CRM panel shows it as Open but every billing
|
||||
* path filters on billed_invoice_id IS NULL, so it could never be re-billed.
|
||||
* Mirrors the categorise-time reset; returns the item to the billable pool.
|
||||
* Best-effort + schema-guarded (no-op on non-accounting installs).
|
||||
*/
|
||||
async function releaseRebillsForCancelledInvoice(conn, invoiceId) {
|
||||
try {
|
||||
if (!(await conn.schema.hasTable('inbound_documents'))) return;
|
||||
await conn('inbound_documents')
|
||||
.where({ billed_invoice_id: invoiceId })
|
||||
.update({ billed_invoice_id: null, billed_invoice_line_item_id: null, updated_at: new Date() });
|
||||
} catch (e) {
|
||||
logger.warn?.(`releaseRebillsForCancelledInvoice failed for invoice ${invoiceId}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function createStorno(originalId, adminId, trx = db) {
|
||||
const original = await trx('invoices').where({ id: originalId }).first();
|
||||
if (!original) throw new AppError('Invoice not found', 404);
|
||||
@@ -305,6 +345,8 @@ async function createStorno(originalId, adminId, trx = db) {
|
||||
cancellation_storno_id: stornoId,
|
||||
updated_at: now,
|
||||
});
|
||||
// Free any re-billed supplier invoices so the cost isn't stranded (#866 review).
|
||||
await releaseRebillsForCancelledInvoice(trx, originalId);
|
||||
|
||||
try {
|
||||
// Pass `trx` so the audit insert rides the transaction's connection;
|
||||
@@ -597,6 +639,8 @@ async function cancelInvoice(id, adminId) {
|
||||
await db('invoices').where({ id }).update({
|
||||
status: 'cancelled', updated_at: new Date(),
|
||||
});
|
||||
// Free any re-billed supplier invoices so the cost isn't stranded (#866 review).
|
||||
await releaseRebillsForCancelledInvoice(db, id);
|
||||
try {
|
||||
await logActivity('invoice_cancelled',
|
||||
{ invoiceId: id, viaStorno: false },
|
||||
|
||||
Reference in New Issue
Block a user