From 53ef8ff1759787a035e0de9dbb99c9e95992082a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20=22flowdy=22=20He=C3=9F?= Date: Sun, 15 Jan 2017 22:03:06 +0100 Subject: [PATCH] Various fixes and completing the HTTP interface --- TrsrDB/HTTP.pm | 24 +++++++++++++++++------- TrsrDB/HTTP/Account.pm | 30 +++++++++++++++++++++++------- site/style.css | 1 + t/schema.sh | 5 ++++- templates/account/history.html.ep | 8 ++++---- templates/account/list.html.ep | 9 +++++---- templates/account/report.html.ep | 8 ++++++++ templates/account/transfer.html.ep | 19 +++++++++---------- templates/bankStatement.html.ep | 4 ++-- templates/credit/list.html.ep | 2 +- templates/credit/upsert.html.ep | 8 ++++---- templates/debit/list.html.ep | 2 +- templates/debit/upsert.html.ep | 8 ++++---- templates/layouts/default.html.ep | 1 + 14 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 templates/account/report.html.ep diff --git a/TrsrDB/HTTP.pm b/TrsrDB/HTTP.pm index 155ce5b..556f871 100644 --- a/TrsrDB/HTTP.pm +++ b/TrsrDB/HTTP.pm @@ -32,6 +32,16 @@ sub startup { user => undef, ); + $self->helper(money => sub { + my (undef, $cent) = @_; + return if !defined $cent; + my $c100 = $cent =~ s{ (\d{0,2}) \z }{}xms && sprintf("%02d", $1); + $cent ||= 0; + return qq{$cent.$c100}; + }); + $self->helper(nl2br => sub { + pop =~ s{\n}{
}grms; + }); if ( my $l = $ENV{LOG} ) { use Mojo::Log; @@ -53,6 +63,13 @@ sub startup { $auth->get( '/logout' )->to("user#logout"); + my $check = $auth->under(sub { shift->stash('grade') })->get('/'); + $check->get('/bankStatement' => sub { + my $c = shift; + $c->stash( records => $c->app->db->resultset("ReconstructedBankStatement") ); + $c->render('bankStatement'); + }); + my $admin = $auth->under(sub { shift->stash('grade') > 1 }); $admin->any('/admin')->to('admin#dash'); $admin->post('/:account/in')->to('credit#upsert'); @@ -68,13 +85,6 @@ sub startup { $auth->get('/')->to('account#list')->name('home'); - my $check = $auth->under(sub { shift->stash('grade') })->get('/'); - $check->get('/bankStatement')->to(sub { - my $c = shift; - $c->stash( records => $c->app->db->resultset("ReconstructedBankStatement") ); - $c->render('bankStatement'); - }); - my $account = $auth->get('/:account')->under(sub { my $c = shift; diff --git a/TrsrDB/HTTP/Account.pm b/TrsrDB/HTTP/Account.pm index 5811f60..21b2f24 100644 --- a/TrsrDB/HTTP/Account.pm +++ b/TrsrDB/HTTP/Account.pm @@ -31,13 +31,15 @@ sub transfer { my $db = $self->app->db; my $account = $db->resultset("Account")->find( $self->stash("account") ); - if ( $self->req->method eq 'GET' ) { - $self->stash( - credits => $account->available_credits_rs, - arrears => $account->current_arrears_rs, - ); - return; - } + my $credits = $account->available_credits; + my $arrears = $account->current_arrears; + + return $self->redirect_to('home') + if !( $credits->count() && $arrears->count() ); + + $self->stash( credits => $credits, arrears => $arrears ); + + return if $self->req->method ne 'POST'; $db->make_transfers( $self->every_param('credits') @@ -47,5 +49,19 @@ sub transfer { return; } + +sub report { + my $self = shift; + my $account = $self->app->db + ->resultset("Account") + ->find( $self->stash("account") ) + ; + + $self->stash( report => $account->report->search_rs( + {}, { order_by => { -asc => [qw/date/] } } + ) ); + +} + 1; diff --git a/site/style.css b/site/style.css index b393a67..51d020c 100644 --- a/site/style.css +++ b/site/style.css @@ -42,6 +42,7 @@ a.transfer-btn { font-weight: bold; border: 2px outset rgb(224,32,0); text-decoration: none; + border-radius: 3px; } table td.even_until { diff --git a/t/schema.sh b/t/schema.sh index 87397d9..e967d83 100644 --- a/t/schema.sh +++ b/t/schema.sh @@ -1,4 +1,7 @@ echo "My database file: $1" if sqlite3 $1 < schema.sql; then - diff t/schema.out <(sqlite3 $1 < t/schema.sql 2>&1) + diff t/schema.out <(sqlite3 $1 < t/schema.sql 2>&1) \ + && rm $1 && sqlite3 $1 < t/schema.sql \ + && TRSRDB_SQLITE_FILE=$1 perl t/schema.t \ + && rm -i $1 fi diff --git a/templates/account/history.html.ep b/templates/account/history.html.ep index e686e65..15aeeee 100644 --- a/templates/account/history.html.ep +++ b/templates/account/history.html.ep @@ -7,10 +7,10 @@ % while ( my $hr = $history->next ) { <%= $hr->date %> % my $this = $hr->this_credit; -On <%= <%= $this->credId %>: <%= $this->purpose %>
+On <%= <%= $this->credId %>: <%== nl2br $this->purpose %>
% my $that = $hr->that_credit; -With <%= $that->account->ID %>/<%= <%= $that->credId %>: <%= $that->purpose %>
-Pay <%= $hr->billId %>: <%= $hr->purpose %> -<%= $hr->debit %><%= $hr->credit %><%= $hr->note %> +With <%= $that->account->ID %>/<%= <%= $that->credId %>: <%== nl2br $that->purpose %>
+Pay <%= $hr->billId %>: <%== nl2br $hr->purpose %> +<%== money $hr->debit %><%== money $hr->credit %><%= $hr->note %> % } diff --git a/templates/account/list.html.ep b/templates/account/list.html.ep index b87b66a..b93fd9d 100644 --- a/templates/account/list.html.ep +++ b/templates/account/list.html.ep @@ -16,22 +16,23 @@ % } % my $u = $account->ID; <%= $u %> - <%= $bal->even_until // "never" %><%= $bal->arrears %>listadd + <%= $bal->even_until // "never" %><%== money $bal->arrears %>listadd % my $which = ($bal->arrears && 1) + ($bal->available && 1); % if ( $which == 2 ) { C⇔D! % } % elsif ( $which ) { - N/A +  N/A  % } % else { - N/A +  N/A  % } - <%= $bal->available %>listadd<%= $bal->earned %><%= $bal->promised %>HistoryReport + <%== money $bal->available %>listadd<%== money $bal->earned %><%== money $bal->promised %>HistoryReport % } # while +

Reconstructed bank statement

Column explanation

diff --git a/templates/account/report.html.ep b/templates/account/report.html.ep new file mode 100644 index 0000000..b6f87a3 --- /dev/null +++ b/templates/account/report.html.ep @@ -0,0 +1,8 @@ +% title 'Account report'; + + + +% while ( my $record = $report->next ) { + +% } +
DateValuePurpose
<%= $record->date %><%== money $record->value %><%== nl2br $record->purpose %>
diff --git a/templates/account/transfer.html.ep b/templates/account/transfer.html.ep index ff2e3dd..6eb4c56 100644 --- a/templates/account/transfer.html.ep +++ b/templates/account/transfer.html.ep @@ -1,4 +1,4 @@ -% title 'Make transfers'; +% title 'Make transfers for ' . $account;

Check at least one item of each table that belong together.

@@ -6,34 +6,33 @@

Available credits

-

Check credits you want to pay above debit(s) with.

+

Check credits you want to spend for the arrear(s) below with.

- + % while ( my $d = $credits->next ) { - - + + % }
Sdatepurposeto spend
?datepurposeto spend
<%= $d->date %><%= $d->purpose %><%= $d->difference %><%== nl2br $d->purpose %><%== money $d->difference %>

Current arrears

-

Check arrears you want to use this credit for.

+

Check arrears you want to settle with credit(s) above.

- + % while ( my $d = $arrears->next ) { - - + + % }
Sdatepurposeto pay
?datepurposeto pay
<%= $d->date %><%= $d->purpose %><%= $d->difference %><%== nl2br $d->purpose %><%== money $d->difference %>

-Return to account list

diff --git a/templates/bankStatement.html.ep b/templates/bankStatement.html.ep index 8451adf..6a113d0 100644 --- a/templates/bankStatement.html.ep +++ b/templates/bankStatement.html.ep @@ -1,8 +1,8 @@ % title 'Bank statement'; - + % while ( my $record = $records->next ) { - + % }
DateAccountPurposeDebitCredit
DatePurposeAccountDebitCredit
<%= $record->date %><%= $record->account %><%= $record->purpose %><%= $record->debit %><%= $record->credit %>
<%= $record->date %><%= $record->purpose %><%= $record->account %><%= $record->debit %><%= $record->credit %>
diff --git a/templates/credit/list.html.ep b/templates/credit/list.html.ep index 59e93b9..dcbda59 100644 --- a/templates/credit/list.html.ep +++ b/templates/credit/list.html.ep @@ -3,6 +3,6 @@ % while ( my $credit = $credits->next ) { - + % }
IdDatePurposevaluespent
<%= $credit->credId %><%= $credit->date %><%= $credit->purpose %><%= $credit->value %><%= $credit->spent %>
<%= $credit->credId %><%= $credit->date %><%== nl2br $credit->purpose %><%== money $credit->value %><%== money $credit->spent %>
diff --git a/templates/credit/upsert.html.ep b/templates/credit/upsert.html.ep index e98eb91..570e86b 100644 --- a/templates/credit/upsert.html.ep +++ b/templates/credit/upsert.html.ep @@ -43,8 +43,8 @@ % my $d = $t->debit; <%= $t->timestamp %> - <%= $d->purpose %> - <%= $t->amount %> + <%== nl2br $d->purpose %> + <%== money $t->amount %> <%= $t->note %> % } @@ -66,8 +66,8 @@ % while ( my $d = $arrears->next ) { <%= $d->date %> - <%= $d->purpose %> - <%= $d->difference %> + <%== nl2br $d->purpose %> + <%== money $d->difference %> % } diff --git a/templates/debit/list.html.ep b/templates/debit/list.html.ep index 0482faa..dcd3313 100644 --- a/templates/debit/list.html.ep +++ b/templates/debit/list.html.ep @@ -4,6 +4,6 @@ IdDatePurposevaluepaidto % while ( my $debit = $debits->next ) { % my $tgt = $debit->target; -<%= $debit->billId %><%= $debit->date %><%= $debit->purpose %><%= $debit->value %><%= $debit->paid %><%= $tgt->account->ID %> +<%= $debit->billId %><%= $debit->date %><%== nl2br $debit->purpose %><%== money $debit->value %><%== money $debit->paid %><%= $tgt->account->ID %> % } diff --git a/templates/debit/upsert.html.ep b/templates/debit/upsert.html.ep index df2de78..4746874 100644 --- a/templates/debit/upsert.html.ep +++ b/templates/debit/upsert.html.ep @@ -70,8 +70,8 @@ oops % my $c = $t->credit; <%= $t->timestamp %> - <%= $c->purpose %> - <%= $t->amount %> + <%== nl2br $c->purpose %> + <%== money $t->amount %> <%= $t->note %> % } @@ -93,8 +93,8 @@ oops % while ( my $d = $credits->next ) { <%= $d->date %> - <%= $d->purpose %> - <%= $d->difference %> + <%== nl2br $d->purpose %> + <%== money $d->difference %> % } diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 6e37179..0f133c9 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -17,5 +17,6 @@ % } <%= content %> +

">Back home – Account List