New plugin: pfand (to demonstrate the new "add" hook)

This commit is contained in:
Juerd Waalboer 2013-02-28 13:16:48 +01:00
parent 7c3f43f18a
commit 972df6150d
2 changed files with 48 additions and 0 deletions

47
plugins/pfand Normal file
View file

@ -0,0 +1,47 @@
HELP "pfand" => "Pfand zurueck";
# This is a demo plugin. It's called "pfand" because "deposit" would be
# confusing and only the Germans are crazy enough to have deposits on small
# bottles anyway ;)
# The file format for 'revbank.pfand' is simply two whitespace separated
# columns: product id and pfand amount.
sub _read_pfand {
open my $fh, 'revbank.pfand' or die $!;
return {
map { split " " } grep /\S/, grep !/^\s*#/, readline $fh
};
}
sub command {
my ($self, $cart, $command) = @_;
return NEXT if $command ne 'pfand';
return "Pfand zurueck fuer", \&product;
}
sub product {
my ($self, $cart, $product) = @_;
my $pfand = _read_pfand->{ $product };
if ($pfand) {
$cart->add(undef, $pfand, "Pfand zurueck");
} else {
say "$product: Kein Pfand";
}
return ACCEPT;
}
sub hook_add {
my ($class, $cart, $user, $item) = @_;
return if defined $user;
return if exists $item->{is_pfand};
return if not exists $item->{product_id};
my $pfand = _read_pfand->{ $item->{product_id} };
$cart->add(undef, -$pfand, "Pfand");
}

View file

@ -10,6 +10,7 @@ undo
give
take
#nyan # fun, but not compatible with dumb terminals
#pfand # makes little sense in a self service environment
#stock
# Then, plugins that apply heuristics