revbank/plugins/stock
Juerd Waalboer ccae71021a 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.)
2022-06-11 16:58:20 +02:00

34 lines
941 B
Perl

#!perl
HELP1 "cash" => "Checkout without a user account";
sub command :Tab(cash) ($self, $cart, $command, @) {
return NEXT if $command ne 'cash';
$cart->checkout('-cash');
return ACCEPT;
}
sub hook_checkout($class, $cart, $user, $transaction_id, @) {
# Hack42 for some reason used the dutch word in their revbank1 hack.
my $filename = -e("revbank.voorraad")
? "revbank.voorraad"
: "revbank.stock";
my @entries = $cart->entries('product_id') or return;
my %stock = do {
my $in;
open($in, '<', $filename)
? map { split " ", $_, 2 } readline $in
: ()
};
$stock{ $_->attribute('product_id') } -= $_->quantity for @entries;
open my $out, '>', "$filename.$$" or warn "$filename.$$: $!";
printf {$out} "%-16s %+9d\n", $_, $stock{$_} for sort keys %stock;
close $out or die "$filename.$$: $!";
rename "$filename.$$", $filename or die $!;
}