feat: store payment installments in a transactions collection
Move embedded payment rows into Transaction documents with due dates so remaining tuition can be tracked separately from paid amounts.
This commit is contained in:
+26
-1
@@ -22,9 +22,34 @@ const sanitizeNotes = (notes) => {
|
||||
return String(notes).trim().slice(0, NOTES_MAX_LENGTH);
|
||||
};
|
||||
|
||||
const rialsToToman = (value) => Math.floor(toNonNegativeNumber(value) / 10);
|
||||
|
||||
const isPaidTransaction = (trx = {}) => {
|
||||
const status = String(trx.status || '').toLowerCase();
|
||||
if (status === 'pending') return false;
|
||||
if (status === 'paid') return true;
|
||||
return Boolean(trx.date);
|
||||
};
|
||||
|
||||
const sumPaidTransactions = (transactions = []) => {
|
||||
if (!Array.isArray(transactions)) return 0;
|
||||
return transactions.reduce((sum, trx) => {
|
||||
if (!isPaidTransaction(trx)) return sum;
|
||||
return sum + toNonNegativeNumber(trx.amount);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const remainingPayable = (payment = {}, transactions = []) => {
|
||||
return Math.max(0, getPayableAmount(payment) - sumPaidTransactions(transactions));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
NOTES_MAX_LENGTH,
|
||||
getPayableAmount,
|
||||
normalizeDiscount,
|
||||
sanitizeNotes
|
||||
sanitizeNotes,
|
||||
rialsToToman,
|
||||
isPaidTransaction,
|
||||
sumPaidTransactions,
|
||||
remainingPayable
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user