Get 'cash' working again
Now implemented via a hidden user called '-cash'. This also introduces the concept of hidden accounts, that begin with '+' or '-', for result accounts and balance accounts. Future versions can further use this for more detailed bookkeeping. The idea behind the sign is that '-' accounts should be inverted to get the intuitive value. So if the account '-cash' has -13.37, that means there should be +13.37 in the cash box (or, well, once the rest of this is implemented and the initial values are then set correctly.)
This commit is contained in:
parent
f262bce57c
commit
ccae71021a
6 changed files with 27 additions and 4 deletions
|
@ -8,6 +8,7 @@ no warnings qw(experimental::signatures);
|
||||||
use Carp ();
|
use Carp ();
|
||||||
use List::Util ();
|
use List::Util ();
|
||||||
use RevBank::Global;
|
use RevBank::Global;
|
||||||
|
use RevBank::Users;
|
||||||
use RevBank::Cart::Entry;
|
use RevBank::Cart::Entry;
|
||||||
|
|
||||||
sub new($class) {
|
sub new($class) {
|
||||||
|
@ -65,6 +66,12 @@ sub checkout($self, $user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($user =~ /^[-+]/) {
|
||||||
|
# Hidden internal accounts
|
||||||
|
my $canonical = RevBank::Users::parse_user($user);
|
||||||
|
$user = $canonical // RevBank::Users::create($user);
|
||||||
|
}
|
||||||
|
|
||||||
my $entries = $self->{entries};
|
my $entries = $self->{entries};
|
||||||
|
|
||||||
my %deltas;
|
my %deltas;
|
||||||
|
|
|
@ -13,8 +13,10 @@ use base 'RevBank::Plugin';
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
RevBank::Plugins::register("RevBank::Messages");
|
RevBank::Plugins::register("RevBank::Messages");
|
||||||
|
*hidden = \&RevBank::Users::is_hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub command { return NEXT; }
|
sub command { return NEXT; }
|
||||||
sub id { 'built in messages' }
|
sub id { 'built in messages' }
|
||||||
|
|
||||||
|
@ -53,6 +55,8 @@ sub hook_reject($class, $plugin, $reason, $abort, @) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sub hook_user_balance($class, $username, $old, $delta, $new, @) {
|
sub hook_user_balance($class, $username, $old, $delta, $new, @) {
|
||||||
|
return if hidden $username;
|
||||||
|
|
||||||
my $sign = $delta->cents >= 0 ? '+' : '-';
|
my $sign = $delta->cents >= 0 ? '+' : '-';
|
||||||
my $rood = $new->cents < 0 ? '31;' : '';
|
my $rood = $new->cents < 0 ? '31;' : '';
|
||||||
my $abs = $delta->abs;
|
my $abs = $delta->abs;
|
||||||
|
@ -63,6 +67,8 @@ sub hook_user_balance($class, $username, $old, $delta, $new, @) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sub hook_user_created($class, $username, @) {
|
sub hook_user_created($class, $username, @) {
|
||||||
|
return if hidden $username;
|
||||||
|
|
||||||
say "New account '$username' created.";
|
say "New account '$username' created.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,13 +36,14 @@ sub create($username) {
|
||||||
print {$fh} "$username 0.00 $now\n" or die $!;
|
print {$fh} "$username 0.00 $now\n" or die $!;
|
||||||
close $fh or die $!;
|
close $fh or die $!;
|
||||||
RevBank::Plugins::call_hooks("user_created", $username);
|
RevBank::Plugins::call_hooks("user_created", $username);
|
||||||
|
return $username;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub update($username, $delta, $transaction_id) {
|
sub update($username, $delta, $transaction_id) {
|
||||||
open my $in, 'revbank.accounts' or die $!;
|
open my $in, 'revbank.accounts' or die $!;
|
||||||
open my $out, ">.revbank.$$" or die $!;
|
open my $out, ">.revbank.$$" or die $!;
|
||||||
my $old;
|
my $old = RevBank::Amount->new(0);
|
||||||
my $new;
|
my $new = RevBank::Amount->new(0);
|
||||||
while (defined (my $line = readline $in)) {
|
while (defined (my $line = readline $in)) {
|
||||||
my @a = split " ", $line;
|
my @a = split " ", $line;
|
||||||
if (lc $a[0] eq lc $username) {
|
if (lc $a[0] eq lc $username) {
|
||||||
|
@ -82,6 +83,10 @@ sub parse_user($username) {
|
||||||
return $users->{ lc $username }->[0];
|
return $users->{ lc $username }->[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub is_hidden($username) {
|
||||||
|
return $username =~ /^[-+]/;
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,9 @@ sub username($self, $cart, $name, @) {
|
||||||
return REJECT, "Sorry, whitespace is not allowed."
|
return REJECT, "Sorry, whitespace is not allowed."
|
||||||
if $name =~ /\s/;
|
if $name =~ /\s/;
|
||||||
|
|
||||||
|
return REJECT, "Sorry, invalid first character."
|
||||||
|
if $name =~ /^[-+*]/;
|
||||||
|
|
||||||
return REJECT, "That's too numeric to be a user name."
|
return REJECT, "That's too numeric to be a user name."
|
||||||
if defined parse_amount($name);
|
if defined parse_amount($name);
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,7 @@ HELP1 "cash" => "Checkout without a user account";
|
||||||
sub command :Tab(cash) ($self, $cart, $command, @) {
|
sub command :Tab(cash) ($self, $cart, $command, @) {
|
||||||
return NEXT if $command ne 'cash';
|
return NEXT if $command ne 'cash';
|
||||||
|
|
||||||
call_hooks("checkout", $cart, 'cash', 0); # Fake checkout
|
$cart->checkout('-cash');
|
||||||
$cart->empty;
|
|
||||||
|
|
||||||
return ACCEPT;
|
return ACCEPT;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,5 @@
|
||||||
juerd +163.48 2022-06-04_02:19:56 +@2021-12-03_18:27:54
|
juerd +163.48 2022-06-04_02:19:56 +@2021-12-03_18:27:54
|
||||||
bla -36.00 2022-01-19_17:11:25 -@2022-01-19_17:00:43
|
bla -36.00 2022-01-19_17:11:25 -@2022-01-19_17:00:43
|
||||||
|
0.00 2022-06-11_16:47:33
|
||||||
|
fooooo 0.00 2022-06-11_16:52:48
|
||||||
|
-cash -0.50 2022-06-11_16:56:36 -@2022-06-11_16:56:36
|
||||||
|
|
Loading…
Add table
Reference in a new issue