#!perl 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 :Tab(pfand) { my ($self, $cart, $command) = @_; return NEXT if $command ne 'pfand'; return "Pfand zurueck fuer", \&product; } sub product :Tab(&tab) { my ($self, $cart, $product) = @_; my $pfand = parse_amount(_read_pfand->{ $product }) or return REJECT, "Invalid pfand amount for $product"; if ($pfand) { $cart->add(+$pfand, "Pfand zurueck", { is_return => 1 }); } else { say "$product: Kein Pfand"; } return ACCEPT; } sub tab { return keys %{ _read_pfand() }; } sub hook_add_entry { my ($class, $cart, $entry) = @_; return if $entry->has_attribute('is_return'); return if not $entry->has_attribute('product_id'); my $pfand = _read_pfand->{ $entry->attribute('product_id') } or return; $cart->add(-$pfand, "Pfand", { is_pfand => 1 }); return; }