revbank/plugins/revspace_mqtt
Juerd Waalboer 9797e3c1c8 Add revspace specific plugins
Written by several authors. There is no log...
2019-09-23 21:12:03 +02:00

36 lines
972 B
Perl

#!perl
use Net::MQTT::Simple "mosquitto.space.revspace.nl";
sub command { NEXT }
sub hook_checkout {
my ($class, $cart, $user, $transaction_id) = @_;
my $filename = "revbank.sales";
my @items = $cart->select_items('product_id') or return;
my %already_retained;
my %stats = do {
my $in;
open($in, '<', $filename)
? map { split " ", $_, 2 } readline $in
: ()
};
$stats{ $_->{product_id} }++ for @items;
for (@items) {
my $product = $_->{product_id};
publish "revspace/bank/sale" => $product;
next if $already_retained{ $product };
retain "revspace/bank/$product" => $stats{$_->{product_id}};
$already_retained{ $product } = 1;
}
open my $out, '>', "$filename.$$" or warn "$filename.$$: $!";
printf {$out} "%-16s %9d\n", $_, $stats{$_} for sort keys %stats;
close $out or die "$filename.$$: $!";
rename "$filename.$$", $filename or die $!;
}