diff --git a/plugins/plus b/plugins/plus new file mode 100644 index 0000000..b85b3e8 --- /dev/null +++ b/plugins/plus @@ -0,0 +1,45 @@ +#!perl + +HELP "+" => "Add N more items of the previous thing"; + +my $limit = 200; +my $err_limit = "Repetition is limited at $limit items."; +my $err_pfand = "Plugins 'pfand' and 'repeat' cannot be combined."; + +sub command { + my ($self, $cart, $command) = @_; + + return ABORT, $err_pfand if $cart->entries('is_pfand'); + + my ($post) = $command =~ /^\+(\d+)?$/ + or return NEXT; + + return ABORT, "Can't modify an empty transaction." if not $cart->size; + + my $last = ($cart->entries)[-1]; + + return REJECT, "Addition only works on products." if not $last->has_attribute('product_id'); + + if ($post) { + return REJECT, $err_limit if $last->quantity + $post > $limit; + + $last->quantity($last->quantity + $post); + return ACCEPT; + } + + return "Add to previous product", \&add; +} + +sub add { + my ($self, $cart, $arg) = @_; + + $arg =~ /^\d+$/ and $arg > 0 + or return REJECT, "Invalid value."; + + my $last = ($cart->entries)[-1]; + return REJECT, $err_limit if $last->quantity + $arg > $limit; + + $last->quantity($last->quantity + $arg); + return ACCEPT; +} +