Add support for user-accessible accounts that are excluded from grandtotal

This commit is contained in:
Juerd Waalboer 2023-11-02 05:22:56 +01:00
parent df8c84672d
commit d54428b092
4 changed files with 30 additions and 6 deletions

View file

@ -33,8 +33,12 @@ sub Tab($self, $method) {
}
if (delete $completions{USERS}) {
$completions{$_}++ for grep !RevBank::Users::is_hidden($_),
RevBank::Users::names();
for my $name (RevBank::Users::names()) {
next if RevBank::Users::is_hidden($name);
$completions{ $name }++;
$completions{ $1 }++ if $name =~ /^\*(.*)/;
}
}
return keys %completions;

View file

@ -7,17 +7,33 @@ use experimental 'signatures'; # stable since v5.36
use RevBank::Global;
use RevBank::Plugins;
use Carp ();
use List::Util ();
my $filename = "revbank.accounts";
sub _read() {
my @users;
/\S/ and push @users, [split " "] for slurp $filename;
return { map { lc($_->[0]) => $_ } @users };
my %users;
for (@users) {
my $name = $_->[0];
if ($name =~ /^\*/) {
# user-accessible special account: support without * prefix
$users{ lc($name) =~ s/^\*//r } = $_;
# also support literal account name with * prefix
$users{ lc($name) } = $_;
} else {
# hidden or normal account
$users{ lc($name) } = $_;
}
}
return \%users;
}
sub names() {
return map $_->[0], values %{ _read() };
return List::Util::uniqstr map $_->[0], values %{ _read() };
}
sub balance($username) {
@ -73,6 +89,10 @@ sub is_hidden($username) {
return $username =~ /^[-+]/;
}
sub is_special($username) {
return $username =~ /^[-+*]/;
}
sub parse_user($username) {
return undef if is_hidden($username);

View file

@ -10,7 +10,7 @@ sub command :Tab(grandtotal) ($self, $cart, $command, @) {
for my $line (slurp 'revbank.accounts') {
my ($username, $balance) = split " ", $line;
next if RevBank::Users::is_hidden($username);
next if RevBank::Users::is_special($username);
my $credit = $balance;
$neg += $credit if $credit < 0;

View file

@ -17,7 +17,7 @@ use RevBank::Global;
use RevBank::Messages;
use RevBank::Cart;
our $VERSION = "4.1.1";
our $VERSION = "4.2.0";
our %HELP1 = (
"abort" => "Abort the current transaction",
);