Internal metadata in cart items + smart matching for items.
This commit is contained in:
parent
c1db0fc1e9
commit
788dc3dc12
2 changed files with 12 additions and 7 deletions
|
@ -14,9 +14,11 @@ sub new {
|
|||
}
|
||||
|
||||
sub add {
|
||||
my ($self, $user, $amount, $description) = @_;
|
||||
my ($self, $user, $amount, $description, $data) = @_;
|
||||
$user ||= '$you';
|
||||
$data ||= {};
|
||||
push @{ $self->{ $user } }, {
|
||||
%$data, # Internal stuff, not logged or printed.
|
||||
amount => $amount,
|
||||
description => $description,
|
||||
};
|
||||
|
@ -104,14 +106,17 @@ sub checkout {
|
|||
}
|
||||
|
||||
sub select_items {
|
||||
my ($self, $regex) = @_;
|
||||
$regex ||= qr/(?:)/; # Match everything if no regex is given
|
||||
my ($self, $key, $smartmatch) = @_;
|
||||
|
||||
use v5.12; # New smartmatch semantics
|
||||
|
||||
my $match_all = (@_ == 1); # Match everything if no key/match is given.
|
||||
|
||||
my @matches;
|
||||
for my $user (keys %$self) {
|
||||
for my $item (@{ $self->{$user} }) {
|
||||
push @matches, { user => $user, %$item }
|
||||
if $item->{description} =~ /$regex/;
|
||||
if $match_all or $item->{ $key } ~~ $smartmatch;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ sub command :Tab(deposit) {
|
|||
return "Amount to deposit into your account", \&amount;
|
||||
}
|
||||
|
||||
if ($cart->select_items(qr/^Deposit$/)) {
|
||||
if ($cart->select_items(is_deposit => 1)) {
|
||||
# No other plugin recognised the input, so it must be a new user.
|
||||
$self->{new_user} = $command;
|
||||
return "Add new account for user '$command'?", \&create;
|
||||
|
@ -26,7 +26,7 @@ sub amount {
|
|||
$amount = parse_amount($amount)
|
||||
or return REJECT, "Invalid amount";
|
||||
|
||||
$cart->add(undef, +$amount, "Deposit");
|
||||
$cart->add(undef, +$amount, "Deposit", { is_deposit => 1 });
|
||||
|
||||
return ACCEPT;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ sub create {
|
|||
sub hook_checkout {
|
||||
my ($class, $cart, $user, $transaction_id) = @_;
|
||||
my $sum;
|
||||
$sum += $_->{amount} for $cart->select_items(qr/^Deposit$/);
|
||||
$sum += $_->{amount} for $cart->select_items(is_deposit => 1);
|
||||
|
||||
say sprintf "Don't forget to add EUR %.2f to the cash box!", $sum if $sum;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue