revbank/plugins/undo

92 lines
2.5 KiB
Perl

#!perl
HELP1 "undo <transactionID>" => "Undo a transaction";
my $filename = ".revbank.undo";
my @TAB;
sub command :Tab(undo) ($self, $cart, $command, @) {
$command eq 'undo' or return NEXT;
$cart->size and return REJECT, "Undo is not available mid-transaction.";
my @log;
for my $line (slurp $filename) {
my ($tid, $user, $delta, $dt) = split " ", $line;
if (@log and $log[-1]{tid} eq $tid) {
push @{ $log[-1]{deltas} }, [ $user, $delta ];
} else {
push @log, { tid => $tid, dt => $dt, deltas => [ [ $user, $delta ] ] };
}
}
@TAB = ();
my $menu = "";
my $max = @log < 15 ? @log : 15;
for my $txn (@log[-$max .. -1]) {
$menu .= "ID: $txn->{tid} $txn->{dt} " . join(", ",
map { sprintf "%s:%+.2f", @$_ } @{ $txn->{deltas} }
) . "\n";
push @TAB, $txn->{tid};
}
return $menu . "Transaction ID", \&undo;
}
sub tab { @TAB }
our $doing_undo = 0; # Ugly but works, just like the rest of this plugin
sub undo :Tab(&tab) ($self, $cart, $tid, @) {
my $description = "Undo $tid";
my $entry;
my $found = 0;
my $aborted = 0;
return with_lock {
for my $line (slurp $filename) {
if ($line =~ /^\Q$tid\E\s/) {
my (undef, $user, $delta) = split " ", $line;
$entry ||= $cart->add(0, $description, { undo_transaction_id => $tid });
$entry->add_contra($user, $delta, "Undo $tid");
}
}
$cart->size or return ABORT, "Transaction ID '$tid' not found in undo log.";
call_hooks("undo", $cart) or return ABORT;
local $doing_undo = 1; # don't allow undoing undos
$cart->checkout('-undo');
return ACCEPT;
};
}
sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) {
$username eq '-undo' or return;
for my $entry ($cart->entries) {
my $undo_tid = $entry->attribute('undo_transaction_id')
or die "Plugin error: broken '-undo' transaction";
rewrite $filename, sub($line) {
if ($line =~ /^\Q$undo_tid\E\s/) {
return undef; # remove line
} else {
return $line;
}
};
}
}
sub hook_user_balance($class, $username, $old, $delta, $new, $transaction_id, @) {
return if $doing_undo; # don't allow undoing undos
append $filename, join(" ", $transaction_id, $username, -$delta, now()), "\n";
}