revbank/plugins/stock
2022-08-29 17:50:12 +02:00

39 lines
1.2 KiB
Perl

#!perl
{
# If you want to keep track of stock, you need a way for people to
# register cash payments. The 'cash' plugin takes care of that, but
# that also assumes deposit_methods. So here's a minimal fallback
# implementation for the 'cash' command.
# If you use the 'cash' plugin, make sure it is loaded *before*
# the 'stock' plugin in 'revbank.plugins'.
HELP1 "cash" => "Checkout without a user account";
sub command :Tab(cash) ($self, $cart, $command, @) {
return NEXT if $command ne 'cash';
return NEXT if not $cart->size;
$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 = eval { map { split " ", $_, 2 } slurp $filename };
$stock{ $_->attribute('product_id') } -= $_->quantity for @entries;
spurt $filename, map {
sprintf "%-16s %+9d\n", $_, $stock{$_}
} sort keys %stock;
}