Stop reconcile from overwriting edited transaction amounts.

When updating a transaction, skip automatic pending-amount adjustment so
admin edits to amount, due date, and other fields are preserved.
This commit is contained in:
2026-08-16 22:36:12 +03:30
parent 8cd355c24b
commit a436cfeb47
+9 -6
View File
@@ -74,7 +74,7 @@ const emitPaymentStatusChangedIfNeeded = (payment, previousStatus, actorId = nul
}); });
}; };
const reconcilePendingTransactions = async (payment) => { const reconcilePendingTransactions = async (payment, { adjustAmount = true } = {}) => {
const transactions = await Transaction.find({ payment: payment._id }).lean(); const transactions = await Transaction.find({ payment: payment._id }).lean();
const remaining = getPayableAmount(payment) - sumPaidTransactions(transactions); const remaining = getPayableAmount(payment) - sumPaidTransactions(transactions);
const pending = await Transaction.find({ payment: payment._id, status: 'pending' }).sort({ dueDate: 1 }); const pending = await Transaction.find({ payment: payment._id, status: 'pending' }).sort({ dueDate: 1 });
@@ -85,10 +85,12 @@ const reconcilePendingTransactions = async (payment) => {
} }
if (pending.length) { if (pending.length) {
const pendingTotal = pending.reduce((sum, row) => sum + row.amount, 0); if (adjustAmount) {
if (pendingTotal !== remaining) { const pendingTotal = pending.reduce((sum, row) => sum + row.amount, 0);
pending[0].amount = remaining; if (pendingTotal !== remaining) {
await pending[0].save(); pending[0].amount = remaining;
await pending[0].save();
}
} }
if (pending.length > 1) { if (pending.length > 1) {
await Transaction.deleteMany({ _id: { $in: pending.slice(1).map((row) => row._id) } }); await Transaction.deleteMany({ _id: { $in: pending.slice(1).map((row) => row._id) } });
@@ -363,7 +365,8 @@ const updateTransaction = async (transactionId, body, actorId = null) => {
if (actorId) trx.recordedBy = actorId; if (actorId) trx.recordedBy = actorId;
await trx.save(); await trx.save();
await reconcilePendingTransactions(payment); // Preserve admin-edited amounts; only reconcile balance when recording new payments.
await reconcilePendingTransactions(payment, { adjustAmount: false });
await refreshPaymentTotals(payment); await refreshPaymentTotals(payment);
emitPaymentStatusChangedIfNeeded(payment, previousStatus, actorId); emitPaymentStatusChangedIfNeeded(payment, previousStatus, actorId);