diff --git a/CHANGELOG.md b/CHANGELOG.md index da74974..71310fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed - Allowing manually setting `created_at` when finalizing documents +### Fixed +- Auto mark invoices as paid only when transaction was newly finalized + ## [0.18.0] - 2024-07-27 ### Added diff --git a/src/bill/operations/mixins/auto_mark_invoices_as_paid.cr b/src/bill/operations/mixins/auto_mark_invoices_as_paid.cr index 19d6d21..2026bdb 100644 --- a/src/bill/operations/mixins/auto_mark_invoices_as_paid.cr +++ b/src/bill/operations/mixins/auto_mark_invoices_as_paid.cr @@ -3,9 +3,10 @@ module Bill::AutoMarkInvoicesAsPaid after_save mark_invoices_as_paid private def mark_invoices_as_paid(transaction : Bill::Transaction) + return unless TransactionStatus.now_finalized?(status) + balance = Ledger.balance!(user_id = transaction.user_id) - return mark_all(user_id) unless balance.debit? - mark_for_debit(user_id, balance) + balance.debit? ? mark_for_debit(user_id, balance) : mark_all(user_id) end # A credit or zero balance means all invoices have been paid.