Skip to content

Commit ae9b30a

Browse files
committed
Wrap balance calculations in a database transaction
1 parent 422b4cd commit ae9b30a

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111
- Make `Currency` JSON-serializable
1212

13+
### Fixed
14+
- Wrap balance calculations in a database transaction
15+
1316
## [0.21.0] - 2025-06-05
1417

1518
### Added

src/bill/models/mixins/parent_net_amount.cr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ module Bill::ParentNetAmount
99
end
1010

1111
def net_amount! : Amount
12-
sum = amount!
13-
sum -= self.credit_notes_amount! if responds_to?(:credit_notes_amount!)
12+
sum = Amount.new(0)
13+
14+
self.class.database.transaction do
15+
sum += amount!
16+
sum -= self.credit_notes_amount! if responds_to?(:credit_notes_amount!)
17+
end
18+
1419
sum
1520
end
1621

src/bill/utilities/mixins/invoices_ledger.cr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ module Bill::InvoicesLedger
5656

5757
# :ditto:
5858
def over_owing!(user)
59-
balance = @ledger.balance!(user) - amount_not_overdue!(user)
59+
balance = Amount.new(0)
60+
61+
user.class.database.transaction do
62+
balance += @ledger.balance!(user) - amount_not_overdue!(user)
63+
end
64+
6065
balance if balance.debit?
6166
end
6267

0 commit comments

Comments
 (0)