revbank/plugins/stock
2022-06-11 16:31:44 +02:00

35 lines
992 B
Perl

#!perl
HELP1 "cash" => "Checkout without a user account";
sub command :Tab(cash) ($self, $cart, $command, @) {
return NEXT if $command ne 'cash';
call_hooks("checkout", $cart, 'cash', 0); # Fake checkout
$cart->empty;
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 $!;
}