From 0e1aa77fe5d03f909a2fb0b1d81c779ed8d7315b Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Mon, 13 Feb 2023 02:44:02 +0100 Subject: [PATCH] statiegeld_tokens: simplify void - No more red messages - Accept "yes" case insensitively - Change entry description and amount so the voiding is logged, which is more code but less complex than passing an attribute to be used during checkout. --- lib/RevBank/Cart/Entry.pm | 4 ++++ plugins/statiegeld_tokens | 16 +++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/RevBank/Cart/Entry.pm b/lib/RevBank/Cart/Entry.pm index a6e2e0e..5233339 100644 --- a/lib/RevBank/Cart/Entry.pm +++ b/lib/RevBank/Cart/Entry.pm @@ -95,6 +95,10 @@ sub contras($self) { return map +{ %$_ }, @{ $self->{contras} }; } +sub delete_contras($self) { + $self->{contras} = []; +} + my $HI = "\e[37;1m"; my $LO = "\e[2m"; my $END = "\e[0m"; diff --git a/plugins/statiegeld_tokens b/plugins/statiegeld_tokens index a31bc0f..666fb31 100644 --- a/plugins/statiegeld_tokens +++ b/plugins/statiegeld_tokens @@ -212,9 +212,6 @@ sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) { $entry->quantity($available); $warnings_by_type{$type}++; } - if ($entry->attribute('statiegeld_VOID_TOKENS')) { - $cart->delete($entry); - } push @used, splice @{ $tokens_by_type->{$type} }, 0, $entry->quantity; $tokens_changed++; @@ -275,25 +272,26 @@ sub command($self, $cart, $command, @) { } $found or return REJECT, "Add deposit returns first."; - return "\e[31;1mDeposit token destruction mode.\e[0m\n\n" - . "The tokens will be deleted irrevokably and you will NOT RECEIVE THE MONEY.\n" + return "The tokens will be deleted irrevokably and you will NOT RECEIVE THE MONEY.\n" . "Type 'yes' if you are sure", \&void; } sub void :Tab(yes,no) ($self, $cart, $input, @) { - if ($input eq 'y') { + if (lc $input eq 'y') { return REJECT, "y is not yes..."; } - if ($input ne 'yes') { + if (lc $input ne 'yes') { print "Destruction cancelled.\n"; return ACCEPT; } for my $entry ($cart->entries('plugin')) { next if $entry->attribute('plugin') ne 'statiegeld'; - $entry->attribute('statiegeld_VOID_TOKENS', 1); + $entry->{description} = "Void: $entry->{description}"; + $entry->amount(0); + $entry->delete_contras; + $entry->attribute(deduplicate => join("/", $self->id, $entry->attribute('addon_id'))); } - print "\e[31;1mDeposit token destruction mode activated.\e[0m\n"; return ACCEPT; }