Add revspace specific plugins

Written by several authors. There is no log...
This commit is contained in:
Juerd Waalboer 2019-09-23 21:12:03 +02:00
parent b6855df288
commit 9797e3c1c8
8 changed files with 210 additions and 1 deletions

View file

@ -31,10 +31,15 @@ line (space separated), but that's an unintended feature...
# PLUGINS # PLUGINS
Refer to [RevBank::Plugins](https://metacpan.org/pod/RevBank::Plugins) for documentation about writing plugins. Refer to [RevBank::Plugins](https://metacpan.org/pod/RevBank::Plugins) for
documentation about writing plugins.
Plugins themselves may have some documentation in the respective plugin files. Plugins themselves may have some documentation in the respective plugin files.
Note that plugins that begin with `revspace_` are revspace specific hacks, and
were not written with reusability in mind. They will probably not work for your
setup.
# AUTHOR # AUTHOR
Juerd Waalboer <#####@juerd.nl> Juerd Waalboer <#####@juerd.nl>

56
plugins/revspace_barcode Normal file
View file

@ -0,0 +1,56 @@
#!perl
# Ja, het is lelijk. Even snel in elkaar geklust en daarna niet meer naar
# gekeken. Pech :) -- Juerd
sub command :Tab(barcode) {
my ($self, $cart, $command) = @_;
return NEXT if $command ne "barcode";
return "Barcode data", \&data;
}
sub data {
my ($self, $cart, $input) = @_;
$cart->add(
undef,
-0.07,
"Barcode <$input>",
{ is_barcode => 1, barcode_data => $input }
);
return ACCEPT;
}
sub hook_checkout {
my ($class, $cart, $username, $transaction_id) = @_;
my @barcodes;
for my $item ($cart->select_items('is_barcode')) {
push @barcodes, $item->{barcode_data};
}
if (@barcodes) {
print "\nCheck the following:\n 1. label tape is 12 mm\n 2. printer is on\n 3. wifi is enabled and connected\n\nPress enter to continue.";
readline STDIN;
my $printjob = "";
open my $bcgen, "-|", "/home/bar/revlabel/barcode.pl", @barcodes
or warn "Could not open script 1";
local $/;
my $filenames = readline $bcgen;
close $bcgen;
open my $fh, "| /home/bar/revlabel/ptouch-770-write 12 $filenames | nc -N 10.42.42.222 9100"
or warn "Couldn't open script 2\n";
print $fh $printjob;
close $fh;
}
}

20
plugins/revspace_bounties Normal file
View file

@ -0,0 +1,20 @@
#!perl
my %bounties = (
1 => [ 10, "Bedankt voor het vegen/stofzuigen" ],
2 => [ 10, "Bedankt voor het afvoeren van het afval" ],
3 => [ 25, "Bedankt voor het dweilen" ],
4 => [ 15, "Bedankt voor 't poetsen van alle tafels" ],
);
sub command :Tab(BOUNTY1,BOUNTY2,BOUNTY3,BOUNTY4) {
my ($self, $cart, $command) = @_;
if ($command =~ /BOUNTY(\d+)/) {
$cart->add(undef, +$bounties{$1}[0], $bounties{$1}[1]);
return ACCEPT;
}
return NEXT;
}

15
plugins/revspace_git Normal file
View file

@ -0,0 +1,15 @@
#!perl
sub command { NEXT }
sub hook_user_balance {
my ($class, $username, $old, $delta, $new, $transaction_id) = @_;
my $msg = "$transaction_id ($username)";
$msg =~ s/[^\x20-\x7E]//g;
$msg =~ s/'//g;
system("(git commit -am '$msg') > /dev/null 2>&1")
== 0 or warn "Meh, gitfaal";
system("git gc --auto");
}

36
plugins/revspace_mqtt Normal file
View file

@ -0,0 +1,36 @@
#!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 $!;
}

46
plugins/revspace_saldo Normal file
View file

@ -0,0 +1,46 @@
#!perl
use POSIX qw(strftime);
sub command { NEXT }
sub _box {
print(
"#" x 79, "\n",
(map { sprintf("## %-73s ##\n", $_) } @_),
"#" x 79, "\n"
);
}
sub hook_checkout_done {
my ($class, $cart, $user, $transaction_id) = @_;
my $balance = RevBank::Users::balance($user) or return;
my $since = RevBank::Users::since($user);
if ($balance < -13.37) {
_box(
"Hoi $user,",
"",
"Je saldo is $balance en dus lager dan toegestaan. Graag meteen aanvullen!",
"Zodra je een positief saldo hebt, mag je weer producten kopen.",
"",
"Bedankt!",
"-- Het bestuur",
);
} elsif (
defined $since
and $since =~ /^-\@(.*)/
and $1 lt strftime('%Y-%m-%d_%H:%M:%S', localtime(time() - 14 * 86400))
) {
_box(
"Hoi $user,",
"",
"Je staat al sinds $1 negatief, dus meer dan 2 weken.",
"Je mag rood staan, maar niet langdurig. Wil je je saldo even aanvullen?",
"",
"Bedankt!",
"-- Het bestuur",
);
}
}

15
plugins/revspace_terminal Normal file
View file

@ -0,0 +1,15 @@
sub command { NEXT }
# Terminal hacks
# Reset terminal on startup
system 'reset';
# Select special characters in G1 on IBM 3151 for utf8 qr code, to counteract
# brokenness caused by squeezen's linedrawing.
sub hook_checkout {
my $select_charset = "\e>A"; # IBM 3151
my $device_control = "\eP" . $select_charset . "\e\\"; # ANSI
my $clear_line = "\r \r";
print $device_control, $clear_line;
}

16
plugins/revspace_window Normal file
View file

@ -0,0 +1,16 @@
sub command {
# Bij elke invoer (bijv. barcode-scan) terug naar window 0.
system qw(screen -S bar -X at % select 0);
# Iemand zet steeds vbell aan, en dus het piepje uit. Daardoor werkt de
# idle-plugin niet zinnig en dat kost geld.
system qw(screen -S bar -X vbell off);
return NEXT;
}
sub hook_abort {
command();
return undef;
}