From 23e08fa977b4da9f2a48d28e31bccdacfde57ddb Mon Sep 17 00:00:00 2001
From: Juerd Waalboer <juerd@tnx.nl>
Date: Sat, 18 Jun 2022 21:54:23 +0200
Subject: [PATCH] New plugin: undeposit

---
 plugins/undeposit | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 plugins/undeposit

diff --git a/plugins/undeposit b/plugins/undeposit
new file mode 100644
index 0000000..977779c
--- /dev/null
+++ b/plugins/undeposit
@@ -0,0 +1,41 @@
+#!perl
+
+# This is basically like 'withdraw', but for non-cash (e.g. iban)
+
+my @TAB;
+
+sub command :Tab(undeposit) ($self, $cart, $command, @) {
+    $command eq 'undeposit' or return NEXT;
+
+    warn "\n\n\n";
+    warn "\e[1mNote: this function is for internal use by board members ONLY.\e[0m\n";
+    warn "\n\n\n";
+    warn "Enter 'abort' to abort.\n";
+
+    @TAB = grep /^[-+]deposit/, RevBank::Users::names
+        or return REJECT, "No contras available.";
+    print "Available contras:\n", map "  $_\n", sort(@TAB);
+
+    return "Contra", \&contra;
+}
+
+sub tab { @TAB }
+
+sub contra :Tab(&tab) ($self, $cart, $arg, @) { 
+    return REJECT, "Invalid contra." unless grep $_ eq $arg, @TAB;
+
+    $self->{contra} = $arg;
+
+    return "Amount to withdraw", \&amount;
+}
+
+sub amount($self, $cart, $arg, @) { 
+    my $amount = parse_amount($arg);
+    defined $amount or return REJECT, "Invalid amount";
+
+    $cart
+        ->add(-$amount, "Undeposit", { is_withdrawal => 1 })
+        ->add_contra($self->{contra}, +$amount, "Undeposited by \$you");
+
+    return ACCEPT;
+}