Add new hook_checkout_prepare, rollback earlier change of hook_checkout

In hindsight, it was a bad idea to allow manipulating the cart (entries)
in hook_checkout, because that hook is used by the `log` plugin. You now
get unused entries in the log.

Although that plugin should maybe have used hook_checkout_done, existing
log file readers (including scripts) and custom plugins may depend on
the CHECKOUT items in the log being before the BALANCE items.
This commit is contained in:
Juerd Waalboer 2023-01-16 03:52:00 +01:00
parent 50d93b3f6e
commit 6180bf6ea5
2 changed files with 8 additions and 5 deletions

View file

@ -79,15 +79,18 @@ sub checkout($self, $user) {
RevBank::FileIO::with_lock { RevBank::FileIO::with_lock {
my $transaction_id = time() - 1300000000; my $transaction_id = time() - 1300000000;
RevBank::Plugins::call_hooks("checkout_prepare", $self, $user, $transaction_id);
for my $entry (@$entries) {
$entry->sanity_check;
$entry->user($user) if not $entry->user;
}
RevBank::Plugins::call_hooks("checkout", $self, $user, $transaction_id); RevBank::Plugins::call_hooks("checkout", $self, $user, $transaction_id);
my %deltas = ($user => RevBank::Amount->new(0)); my %deltas = ($user => RevBank::Amount->new(0));
for my $entry (@$entries) { for my $entry (@$entries) {
# In case entries were added by the hook
$entry->sanity_check;
$entry->user($user) if not $entry->user;
$deltas{$_->{user}} += $_->{amount} * $entry->quantity $deltas{$_->{user}} += $_->{amount} * $entry->quantity
for $entry, $entry->contras; for $entry, $entry->contras;
} }

View file

@ -89,7 +89,7 @@ sub _handle_undo($cart) {
} }
} }
sub hook_checkout($class, $cart, $username, $transaction_id, @) { sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) {
if ($username eq '-undo') { if ($username eq '-undo') {
_handle_undo($cart); _handle_undo($cart);
return; return;