From 972df6150de086b500c61560e5581e704042c4f9 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 28 Feb 2013 13:16:48 +0100 Subject: [PATCH] New plugin: pfand (to demonstrate the new "add" hook) --- plugins/pfand | 47 +++++++++++++++++++++++++++++++++++++++++++++++ revbank.plugins | 1 + 2 files changed, 48 insertions(+) create mode 100644 plugins/pfand diff --git a/plugins/pfand b/plugins/pfand new file mode 100644 index 0000000..2af4c62 --- /dev/null +++ b/plugins/pfand @@ -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"); +} + diff --git a/revbank.plugins b/revbank.plugins index 813a137..8e21528 100644 --- a/revbank.plugins +++ b/revbank.plugins @@ -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