#!perl # Use this plugin for cashbox contents tracking. For it to make sense, # you will also need the "deposit_methods" plugin to let users distinguish # between cash deposits and other deposit methods. # This plugin should be loaded *before* the 'stock' plugin in # the 'revbank.plugins' configuration file. HELP1 "cash" => "Checkout without a user account"; sub command :Tab(cash) ($self, $cart, $command, @) { return NEXT if $command ne 'cash'; if ($cart->size) { return REJECT, "Can't use cash checkout on a deposit transaction." if $cart->entries('is_deposit'); return REJECT, "Can't use cash checkout on a withdraw transaction." if $cart->entries('is_withdrawal'); $cart->checkout('-cash'); } else { call_hooks 'cash'; return "Please count the money to verify. How much is there, exactly?", \✓ } return ACCEPT; } sub hook_cash($class, @) { printf "There should currently be (at least) %s in the cash box.\n", -RevBank::Users::balance("-cash") || "0.00"; } our $suppress = 0; sub hook_user_balance($class, $username, $old, $delta, $new, @) { return if $username ne '-cash' or $delta->cents == 0; return if $suppress; # "-" accounts need to be inverted to display the intuitive value. my $actual_delta = -$delta; my $actual_new = -$new; printf "\nProceed to %s %s %s the cash box;\n it should then have (at least) %s%s.\n", ($actual_delta->cents < 0 ? "remove" : "put"), abs($delta), ($actual_delta->cents < 0 ? "from" : "into"), $actual_new, ($actual_delta->cents < 0 ? " remaining" : " in it"); } my $confirm_prompt = "Type 'fix pls' to apply a permanent correction, or 'abort' to abort"; sub check($self, $cart, $arg, @) { my $should = -RevBank::Users::balance("-cash") || parse_amount(0); my $have = parse_amount($arg); return REJECT, "Invalid amount" if not defined $have; if ($have == $should) { print "Thank you for checking!\n"; return ACCEPT; } my $surplus = $have - $should; $self->{surplus} = $surplus; my $what = $surplus < 0 ? "shortage" : "overage"; $self->{what} = $what; my $abs = abs $surplus; my $suffix = $surplus <= -100 ? "??!! WTF?! Really?!" : $surplus <= -20 ? "! :(" : $surplus <= -10 ? "!" : $surplus >= +20 ? "?!" : "."; my $an = $what =~ /^o/ ? "an" : "a"; print "\nThank you for checking! That's $an $what of $abs$suffix\n"; return $confirm_prompt, \&confirm; } sub confirm($self, $cart, $arg, @) { return $confirm_prompt, \&confirm unless $arg eq "fix pls"; $cart ->add($self->{surplus}, "Cash $self->{what}") ->add_contra("-cash", -$self->{surplus}, "Cash $self->{what}"); local $suppress = 1; $cart->checkout('-expenses/discrepancies'); printf "\nDiscrepancy recorded; corrected cash box amount is %s.\n", -RevBank::Users::balance("-cash") || "0.00"; return ACCEPT; }