From e17c092efee509e68f22814496f1dcdc81cefc69 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Mon, 16 Jan 2023 04:38:10 +0100 Subject: [PATCH] Allow statiegeld return via deposit command May come in handy if there the bottle return revbank machine is dead. --- plugins/deposit | 16 ++++++++++++---- plugins/statiegeld | 48 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/plugins/deposit b/plugins/deposit index 6d62cbb..0bac34d 100644 --- a/plugins/deposit +++ b/plugins/deposit @@ -7,12 +7,20 @@ HELP1 "deposit " => "Deposit into an account"; sub command :Tab(deposit) ($self, $cart, $command, @) { $command eq 'deposit' or return NEXT; - return "Amount to deposit into your account", \&amount; + my $prompt = "Amount to deposit into your account"; + call_hooks("deposit_command", \$prompt, $self->{alternatives} = []); + + return $prompt, \&amount; } -sub amount :Tab(13.37,42) ($self, $cart, $amount, @) { - $self->{amount} = $amount = parse_amount($amount) - or return REJECT, "Invalid amount"; +sub amount :Tab(13.37,42) ($self, $cart, $input, @) { + for my $sub (@{ $self->{alternatives} }) { + my @rv = $sub->(undef, $cart, $input); + return @rv if $rv[0] != NEXT; + } + + $self->{amount} = my $amount = parse_amount($input) + or return REJECT, "Invalid input."; call_hooks("deposit_methods", \my $message, $self->{deposit_methods} = {}); diff --git a/plugins/statiegeld b/plugins/statiegeld index 2d16647..bb4c7e2 100644 --- a/plugins/statiegeld +++ b/plugins/statiegeld @@ -21,16 +21,11 @@ use List::Util; our @addon_accounts = ("+statiegeld"); my $nope = "Sorry, no deposit on that product.\n"; -my $S = ($ENV{REVBANK_STATIEGELD} // 0) == 1; - -sub command ($self, $cart, $command, @) { - $S or return NEXT; - - defined &RevBank::Plugin::products::_read_products - or die "statiegeld plugin requires products plugin"; +our $S = ($ENV{REVBANK_STATIEGELD} // 0) == 1; +sub statiegeld_product($product_id) { my $products = RevBank::Plugin::products::_read_products(); - my $product = $products->{$command} or return NEXT; + my $product = $products->{$product_id} or return; my @addons = @{ $product->{addons} }; my @relevant_addons; @@ -46,16 +41,47 @@ sub command ($self, $cart, $command, @) { push @addons, @{ $addon->{addons} }; }; - if (not @relevant_addons) { + return 0 if not @relevant_addons; + return { product => $product, statiegeld_addons => \@relevant_addons }; +} + +sub hook_deposit_command($class, $prompt, $array, @) { + $$prompt =~ s/$/, or scan empty container/; + + push @$array, sub($, $cart, $input, @) { + my $p = statiegeld_product($input) // return NEXT; + + if (not $p) { + print $nope; + return NEXT; + } + + local $S = 1; + return command($class, $cart, $input); + }; +} + +sub command ($invocant, $cart, $command, @) { + $S or return NEXT; + + defined &RevBank::Plugin::products::_read_products + or die "statiegeld plugin requires products plugin"; + + my $sg = statiegeld_product($command) // return NEXT; + + if (not $sg) { print $nope; return ACCEPT; } - for my $addon (@relevant_addons) { + my $product = $sg->{product}; + my $addons = $sg->{statiegeld_addons}; + + for my $addon (@$addons) { my $d = "$addon->{description} ($product->{description})"; $cart - ->add(+$addon->{price}, $d, { plugin => $self->id, addon_id => $addon->{id} }) + ->add(+$addon->{price}, $d, { plugin => $invocant->id, addon_id => $addon->{id} }) ->add_contra($addon->{contra}, -$addon->{price}, "$d for \$you"); }