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
+5 -2
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 remaining = getPayableAmount(payment) - sumPaidTransactions(transactions);
const pending = await Transaction.find({ payment: payment._id, status: 'pending' }).sort({ dueDate: 1 });
@@ -85,11 +85,13 @@ const reconcilePendingTransactions = async (payment) => {
}
if (pending.length) {
if (adjustAmount) {
const pendingTotal = pending.reduce((sum, row) => sum + row.amount, 0);
if (pendingTotal !== remaining) {
pending[0].amount = remaining;
await pending[0].save();
}
}
if (pending.length > 1) {
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;
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);
emitPaymentStatusChangedIfNeeded(payment, previousStatus, actorId);