From 6c43accd5f8038cf587875b3a5a32c1498c54b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20=22flowdy=22=20He=C3=9F?= Date: Tue, 31 Jan 2017 22:18:16 +0100 Subject: [PATCH] Fixed Transfer note handling in debit and credit upsert forms. Plus, explanation in the case that debit is settled / credit is spent so that checking further items is pointless. --- TrsrDB/HTTP/Credit.pm | 8 ++++++++ TrsrDB/HTTP/Debit.pm | 15 ++++++++------- templates/credit/upsert.html.ep | 9 +++++++-- templates/debit/upsert.html.ep | 23 +++++++++++++---------- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/TrsrDB/HTTP/Credit.pm b/TrsrDB/HTTP/Credit.pm index a2144bd..6c43b49 100644 --- a/TrsrDB/HTTP/Credit.pm +++ b/TrsrDB/HTTP/Credit.pm @@ -71,6 +71,14 @@ sub upsert { $db->make_transfers( $self->param("billId") => $to_spend_for); } + for my $param ( grep { /^note\[/ } @{ $self->req->params->names } ) { + my $note = $self->param($param) || next; + s{^note\[}{} && s{\]$}{} for $param; + $credit->search_related( + outgoings => { billId => $param } + )->update({ note => $note }); + } + $self->redirect_to('home'); diff --git a/TrsrDB/HTTP/Debit.pm b/TrsrDB/HTTP/Debit.pm index 4b26e4a..156e73c 100644 --- a/TrsrDB/HTTP/Debit.pm +++ b/TrsrDB/HTTP/Debit.pm @@ -105,15 +105,16 @@ sub upsert { if ( @$to_pay_with ) { my $billId = $self->param("billId"); $db->make_transfers( $to_pay_with => $billId ); - for my $param ( grep { /^note\[/ } @{ $self->req->params->names } ) { - my $note = $self->param($param) || next; - s{^note\[}{} && s{\]$}{} for $param; - $db->resultset("Transfer")->find({ - billId => $self->param("billId"), credId => $param - })->update({ note => $note }); - } } + for my $param ( grep { /^note\[/ } @{ $self->req->params->names } ) { + my $note = $self->param($param) || next; + s{^note\[}{} && s{\]$}{} for $param; + $debit->search_related( + incomings => { credId => $param } + )->update({ note => $note }); + } + $self->redirect_to('home'); } diff --git a/templates/credit/upsert.html.ep b/templates/credit/upsert.html.ep index 23caa30..e12d894 100644 --- a/templates/credit/upsert.html.ep +++ b/templates/credit/upsert.html.ep @@ -43,6 +43,7 @@ +% my %paid_debits; % if ( $credit->in_storage ) { % my $o = $credit->outgoings; % if ( $o->count() ) { @@ -52,11 +53,12 @@ Rtimestamppaidvaluenote % while ( my $t = $o->next ) { % my $d = $t->debit; +% $paid_debits{ $t->billId } = 1; <%= $t->timestamp %> <%== nl2br $d->purpose %> <%== money $t->amount %> - <%= $t->note %> + % } @@ -70,15 +72,18 @@ % if ( $account //= $credit->account ) {

Spend for arrears

% my $arrears = ( ref $account ? $account : app->db->resultset("Account")->find($account) )->current_arrears; -% if ( $arrears->count() ) { +% if ( $arrears->first() ) { +

<%= $credit->value == $credit->spent ? "The credit is already spent. Unless you revoke any outgoings above, there is no point in checking" : "Check" %> <%= %paid_debits ? "other" : "" %> debits you want to spend this credit for:

Check arrears you want to use this credit for.

% while ( my $d = $arrears->next ) { +% next if $paid_debits{ $d->billId }; + % }
Sdatepurposeto pay
<%= $d->date %> <%== nl2br $d->purpose %> <%== money $d->difference %>
diff --git a/templates/debit/upsert.html.ep b/templates/debit/upsert.html.ep index 9d1072e..dd56102 100644 --- a/templates/debit/upsert.html.ep +++ b/templates/debit/upsert.html.ep @@ -73,6 +73,7 @@ oops % } +% my %used_credits; % if ( $debit->in_storage ) { % my $i = $debit->incomings; % if ( $i->count() ) { @@ -82,11 +83,12 @@ oops Rtimestamppaidvaluenote % while ( my $t = $i->next ) { % my $c = $t->credit; +% $used_credits{ $t->credId } = 1; <%= $t->timestamp %> <%== nl2br $c->purpose %> - <%== money $t->amount %> - <%= $t->note %> + <%== money $t->amount %> + % } @@ -100,16 +102,17 @@ oops % if ( $account ||= $debit->debtor ) {

Pay with credits

% my $credits = app->db->resultset("Account")->find($account)->available_credits; -% if ( $credits->count() ) { -

Check credits you want to pay this debit with.

+% if ( $credits->first() ) { +

<%= $debit->value == $debit->value ? "The debt is already settled. Unless you revoke any incomings above, there is no point in checking" : "Check" %> <%= %used_credits ? "other" : "" %> credits you want to pay this debit with:

-% while ( my $d = $credits->next ) { - - - - - +% while ( my $c = $credits->next ) { +% next if $used_credits{ $c->credId }; + + + + + % }
Sdatepurposeto spendnote
<%= $d->date %><%== nl2br $d->purpose %><%== money $d->difference %>
<%= $c->date %><%== nl2br $c->purpose %><%== money $c->difference %>