Billing: refund a payment; void invoices that aren't paid (none, or all refunded)
Void was refused while any payment was recorded, so a paid-then-refunded invoice could only be voided by deleting the payment and losing the record. A payment can now be marked refunded: kept with its receipt, no longer counted as paid, shown on the statement as money going back out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -411,7 +411,8 @@ async function recalculateCustomerInvoice(invoice, settings) {
|
||||
async function voidInvoice(id, reason) {
|
||||
const invoice = await loadInvoice(id);
|
||||
if (invoice.status === 'void') throw bad('It\'s already void.', 409);
|
||||
if (invoice.payments.length) throw bad('It has payments recorded against it. Remove them (or move them) first.', 409);
|
||||
// Refunded payments don't count: an invoice that was paid and refunded can be voided.
|
||||
if (invoice.payments.some(p => !p.refundedAt)) throw bad('It has payments that weren\'t refunded. Refund them (or remove a payment recorded by mistake) first.', 409);
|
||||
await prisma.invoice.update({ where: { id }, data: { status: 'void', voidedAt: new Date(), voidReason: reason ? String(reason).slice(0, 500) : null } });
|
||||
// A tier change that hasn't happened yet goes with it.
|
||||
await prisma.licenceChange.deleteMany({ where: { invoiceId: id, appliedAt: null, sentAt: null } });
|
||||
@@ -508,6 +509,20 @@ async function deletePayment(id) {
|
||||
return payment;
|
||||
}
|
||||
|
||||
/**
|
||||
* The money went back to the church: the payment stays on record (with its receipt) but no longer
|
||||
* counts as paid, so its invoice is owing again and can be voided. The statement shows the refund.
|
||||
*/
|
||||
async function refundPayment(id, { on, note } = {}) {
|
||||
const payment = await prisma.payment.findUnique({ where: { id } });
|
||||
if (!payment) throw bad('Payment not found', 404);
|
||||
if (payment.refundedAt) throw bad('It was already refunded.', 409);
|
||||
const refundedAt = on ? day(on) : today();
|
||||
if (Number.isNaN(refundedAt.getTime())) throw bad('Not a valid date.');
|
||||
if (ymd(refundedAt) < ymd(payment.paidOn)) throw bad('A refund can\'t be before the payment.');
|
||||
return prisma.payment.update({ where: { id }, data: { refundedAt, refundNote: note ? String(note).trim().slice(0, 300) || null : null }, include: { invoice: true } });
|
||||
}
|
||||
|
||||
async function resendReceipt(id) {
|
||||
const res = await sendReceipt(id, await getSettings());
|
||||
if (!res.ok) throw bad(`It couldn't be emailed: ${res.error}`, 502);
|
||||
@@ -650,7 +665,7 @@ async function licenceChangePdfFor(id) {
|
||||
|
||||
module.exports = {
|
||||
nextNumber, loadInvoice, invoicePdfFor, draftForCustomer, createDueDrafts, sendInvoice, sendDueInvoices, remindUnapproved,
|
||||
applyDueLicenceChanges, runBilling, remindDomainRenewals, approveInvoice, unapproveInvoice, updateInvoice, recalculateInvoice, voidInvoice,
|
||||
applyDueLicenceChanges, runBilling, remindDomainRenewals, approveInvoice, unapproveInvoice, updateInvoice, recalculateInvoice, voidInvoice, refundPayment,
|
||||
resendInvoice, createManualInvoice, recordPayment, deletePayment, resendReceipt, receiptPdfFor, customerStatement,
|
||||
statementPdfFor, emailStatement, createLicenceChange, resendLicenceChange, licenceChangePdfFor, emailApproval,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user