90 lines
2.2 KiB
Perl
90 lines
2.2 KiB
Perl
#!perl
|
|
|
|
HELP1 "<account>" => "[Pay with your account and] show balance";
|
|
HELP "list" => "List accounts and balances";
|
|
HELP "shame" => "Display Hall of Shame (negative balances)";
|
|
|
|
sub command :Tab(list,ls,shame,USERS) ($self, $cart, $command, @) {
|
|
return $self->list if $command eq 'list';
|
|
return $self->list if $command eq 'ls';
|
|
return $self->shame if $command eq 'shame';
|
|
|
|
return "Username", \&log_for if $command eq 'log';
|
|
|
|
my $user = parse_user($command)
|
|
or return NEXT;
|
|
|
|
return $self->balance($user) if not $cart->size;
|
|
|
|
$cart->checkout($user) or return REJECT, "Checkout failed.";
|
|
|
|
return ACCEPT;
|
|
}
|
|
|
|
sub hook_checkout($class, $cart, $user, $transaction_id, @) {
|
|
if ($cart->changed) {
|
|
say "Done:";
|
|
$cart->display;
|
|
}
|
|
say "Transaction ID: $transaction_id";
|
|
}
|
|
|
|
sub list($self) {
|
|
require RevBank::TextEditor;
|
|
|
|
my $list = join "", sort {
|
|
lc($a) cmp lc($b)
|
|
} grep {
|
|
!/^[-+]/
|
|
} slurp("revbank.accounts");
|
|
|
|
RevBank::TextEditor::pager("RevBank account list", $list);
|
|
return ACCEPT;
|
|
}
|
|
|
|
sub shame($self) {
|
|
my $list = join "", sort {
|
|
(split " ", $a)[1] <=> (split " ", $b)[1]
|
|
} grep {
|
|
/ -/ && !/^[-+]/
|
|
} slurp("revbank.accounts");
|
|
|
|
$list =~ s/( -[\d.]+)/\e[31;1m$1\e[0m/g;
|
|
print $list;
|
|
return ACCEPT;
|
|
}
|
|
|
|
sub _grep($u) {
|
|
my @lines;
|
|
open my $fh, "<", ".revbank.log" or die $!;
|
|
while (defined($_ = readline $fh)) {
|
|
s/CHECKOUT\s+\S+\s+(\S+)\s+// or next;
|
|
lc($1) eq lc($u) or next;
|
|
s/ #// or next;
|
|
s/_/ /;
|
|
push @lines, $_;
|
|
}
|
|
return @lines;
|
|
}
|
|
|
|
sub log_for :Tab(USERS) ($self, $cart, $input, @) {
|
|
my $user = parse_user($input) or return REJECT, "Unknown user";
|
|
my @lines = _grep($user);
|
|
|
|
require RevBank::TextEditor;
|
|
RevBank::TextEditor::logpager("RevBank log for $user", join("", @lines, "(end)"));
|
|
return ACCEPT;
|
|
}
|
|
|
|
sub _recent($n, $u) {
|
|
$n += 0;
|
|
print "Last $n transactions for $u:\n";
|
|
print +(_grep($u))[-$n .. -1];
|
|
}
|
|
|
|
sub balance($self, $u) {
|
|
_recent(10, $u);
|
|
printf "Balance for $u is \e[1m%s\e[0m\n", RevBank::Users::balance($u)->string("+");
|
|
say "NB: Products/amounts/commands FIRST, username LAST.";
|
|
return ABORT;
|
|
}
|