Allow statiegeld return via deposit command

May come in handy if there the bottle return revbank machine is dead.
This commit is contained in:
Juerd Waalboer 2023-01-16 04:38:10 +01:00
parent dbb11b5898
commit e17c092efe
2 changed files with 49 additions and 15 deletions

View file

@ -7,12 +7,20 @@ HELP1 "deposit <amount>" => "Deposit into an account";
sub command :Tab(deposit) ($self, $cart, $command, @) { sub command :Tab(deposit) ($self, $cart, $command, @) {
$command eq 'deposit' or return NEXT; $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, @) { sub amount :Tab(13.37,42) ($self, $cart, $input, @) {
$self->{amount} = $amount = parse_amount($amount) for my $sub (@{ $self->{alternatives} }) {
or return REJECT, "Invalid amount"; 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} = {}); call_hooks("deposit_methods", \my $message, $self->{deposit_methods} = {});

View file

@ -21,16 +21,11 @@ use List::Util;
our @addon_accounts = ("+statiegeld"); our @addon_accounts = ("+statiegeld");
my $nope = "Sorry, no deposit on that product.\n"; my $nope = "Sorry, no deposit on that product.\n";
my $S = ($ENV{REVBANK_STATIEGELD} // 0) == 1; our $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";
sub statiegeld_product($product_id) {
my $products = RevBank::Plugin::products::_read_products(); 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 @addons = @{ $product->{addons} };
my @relevant_addons; my @relevant_addons;
@ -46,16 +41,47 @@ sub command ($self, $cart, $command, @) {
push @addons, @{ $addon->{addons} }; 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; print $nope;
return ACCEPT; 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})"; my $d = "$addon->{description} ($product->{description})";
$cart $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"); ->add_contra($addon->{contra}, -$addon->{price}, "$d for \$you");
} }