From 2fbc8330111a57aca3c325f92c787bf91a9cc44a Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Sun, 22 Jul 2018 22:39:26 +0200 Subject: [PATCH] Optional extra questions after 'deposit' Made for Bitlair but PAY ATTENTION... the IBAN in this repo is RevSpace's ;) --- plugins/deposit | 54 +++++++++++++++++++++++++++++++++-------- plugins/deposit_methods | 31 +++++++++++++++++++++++ 2 files changed, 75 insertions(+), 10 deletions(-) create mode 100755 plugins/deposit_methods diff --git a/plugins/deposit b/plugins/deposit index 878c7fd..993ee0d 100755 --- a/plugins/deposit +++ b/plugins/deposit @@ -23,11 +23,53 @@ sub command :Tab(deposit) { sub amount :Tab(13.37,42) { my ($self, $cart, $amount) = @_; - $amount = parse_amount($amount) + $self->{amount} = parse_amount($amount) or return REJECT, "Invalid amount"; - $cart->add(undef, +$amount, "Deposit", { is_deposit => 1 }); + call_hooks("deposit_methods", $self->{deposit_methods} = {}); + return "How are we receiving this EUR $amount?", \&how + if keys %{ $self->{deposit_methods} }; + + $cart->add(undef, +$self->{amount}, "Deposit", { is_deposit => 1 }); + return ACCEPT; +} + +sub how :Tab(&how_tab) { + my ($self, $cart, $input) = @_; + + my %methods = %{ $self->{deposit_methods} }; + + my $how = $self->{how} = $methods{$input} + or return REJECT, "'$input' is not a valid answer."; + + if (@{ $how->{prompts} // [] }) { + return shift @{ $how->{prompts} }, \&how_prompt; + } + + $cart->add(undef, +$self->{amount}, $how->{description}, { is_deposit => 1 }); + return ACCEPT; +} + +sub how_tab { + my ($self) = @_; + return keys %{ $self->{deposit_methods} }; +} + +sub how_prompt { + my ($self, $cart, $input) = @_; + + my $how = $self->{how}; + + push @{ $how->{answers} }, $input; + + if (@{ $how->{prompts} }) { + return shift @{ $how->{prompts} }, \&how_prompt; + } + + my $desc = sprintf $how->{description}, @{ $how->{answers} }; + + $cart->add(undef, +$self->{amount}, $desc, { is_deposit => 1 }); return ACCEPT; } @@ -43,11 +85,3 @@ sub create :Tab(yes,no) { return ABORT; } -sub hook_checkout { - my ($class, $cart, $user, $transaction_id) = @_; - my $sum; - $sum += $_->{amount} for $cart->select_items('is_deposit'); - - say sprintf "Don't forget to add EUR %.2f to the cash box!", $sum if $sum; -} - diff --git a/plugins/deposit_methods b/plugins/deposit_methods new file mode 100755 index 0000000..4eee95e --- /dev/null +++ b/plugins/deposit_methods @@ -0,0 +1,31 @@ +#!perl + +sub command { NEXT } + +sub hook_deposit_methods { + my ($class, $hash) = @_; + + print <<"END"; + +Please type one of the following: + +'iban': IBAN transfer (NL 69 ABNA 0431 1582 07) +'cash': Cash in the cash box +'reimburse': Reimbursement of expenses agreed upon in advance + Note: we require an invoice or receipt with this exact amount! +'other': Provide a manual description +END + + %$hash = ( + iban => { description => "Deposit (IBAN transfer)" }, + cash => { description => "Deposit (Cash)" }, + other => { description => "Deposit (%s)", prompts => [ "Description" ] }, + reimburse => { + description => "Reimbursement (%s, approval: %s)", + prompts => [ + "Please provide a short description", + "Which board member gave approval?", + ], + }, + ); +}