Various fixes and completing the HTTP interface

This commit is contained in:
Florian "flowdy" Heß 2017-01-15 22:03:06 +01:00
parent f4bcbcad6f
commit 53ef8ff175
14 changed files with 84 additions and 45 deletions

View File

@ -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{<strong>$cent</strong>.$c100};
});
$self->helper(nl2br => sub {
pop =~ s{\n}{<br>}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;

View File

@ -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;

View File

@ -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 {

View File

@ -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

View File

@ -7,10 +7,10 @@
% while ( my $hr = $history->next ) {
<tr><td><%= $hr->date %></td><td>
% my $this = $hr->this_credit;
<strong>On <a href="/credit/<%= $hr->credId %>"><%= <%= $this->credId %></a>:</strong> <%= $this->purpose %><br>
<strong>On <a href="/credit/<%= $hr->credId %>"><%= <%= $this->credId %></a>:</strong> <%== nl2br $this->purpose %><br>
% my $that = $hr->that_credit;
<strong>With <%= $that->account->ID %>/<a href="/credit/<%= $that->credId %>"><%= <%= $that->credId %></a>:</strong> <%= $that->purpose %><br>
<strong>Pay <%= $hr->billId %>:</strong> <%= $hr->purpose %>
</td><td class="number"><%= $hr->debit %></td><td class="number"><%= $hr->credit %></td><td><%= $hr->note %></td></tr>
<strong>With <%= $that->account->ID %>/<a href="/credit/<%= $that->credId %>"><%= <%= $that->credId %></a>:</strong> <%== nl2br $that->purpose %><br>
<strong>Pay <%= $hr->billId %>:</strong> <%== nl2br $hr->purpose %>
</td><td class="number"><%== money $hr->debit %></td><td class="number"><%== money $hr->credit %></td><td><%= $hr->note %></td></tr>
% }
</table>

View File

@ -16,22 +16,23 @@
% }
% my $u = $account->ID;
<tr><th><%= $u %></th>
<td class="even_until"><%= $bal->even_until // "never" %></td><td class="number"><%= $bal->arrears %></td><td><a href="<%= $u %>/debits">list</td><td><a href="<%= $u %>/out">add</a></td>
<td class="even_until"><%= $bal->even_until // "never" %></td><td class="number"><%== money $bal->arrears %></td><td><a href="<%= $u %>/debits">list</td><td><a href="<%= $u %>/out">add</a></td>
<td class="centered">
% my $which = ($bal->arrears && 1) + ($bal->available && 1);
% if ( $which == 2 ) {
<a class="transfer-btn" href="<%= $u %>/transfer">C&hArr;D!</a>
% }
% elsif ( $which ) {
<span style="background-color:rgba(224,224,0,0.4);">N/A</span>
<span style="background-color:rgba(224,224,0,0.4);">&nbsp;N/A&nbsp;</span>
% }
% else {
<span style="background-color:rgba(0,224,0,0.4);">N/A</span>
<span style="background-color:rgba(0,224,0,0.4);">&nbsp;N/A&nbsp;</span>
% }
</td><td class="number"><%= $bal->available %></td><td><a href="<%= $u %>/credits">list</a></td><td><a href="<%= $u %>/in">add</a></td><td class="number"><%= $bal->earned %></td><td class="number"><%= $bal->promised %></td><td><a href="<%= $u %>/history">History</a></td><td><a href="<%= $u %>/report">Report</a></td></tr>
</td><td class="number"><%== money $bal->available %></td><td><a href="<%= $u %>/credits">list</a></td><td><a href="<%= $u %>/in">add</a></td><td class="number"><%== money $bal->earned %></td><td class="number"><%== money $bal->promised %></td><td><a href="<%= $u %>/history">History</a></td><td><a href="<%= $u %>/report">Report</a></td></tr>
% } # while
</table>
<p style="text-align:center;"><a href="/bankStatement">Reconstructed bank statement</a></p>
<div class="help">
<h2>Column explanation</h2>
<dl>

View File

@ -0,0 +1,8 @@
% title 'Account report';
<table>
<tr><th>Date</th><th>Value</th><th>Purpose</th></tr>
% while ( my $record = $report->next ) {
<tr><td><%= $record->date %></td><td class="number"><%== money $record->value %></td><td><%== nl2br $record->purpose %></td></tr>
% }
</table>

View File

@ -1,4 +1,4 @@
% title 'Make transfers';
% title 'Make transfers for ' . $account;
<p>Check at least one item of each table that belong together.</p>
@ -6,34 +6,33 @@
<h2>Available credits</h2>
<p>Check credits you want to <strong>pay above debit(s)</strong> with.</p>
<p>Check credits you want to <strong>spend for the arrear(s) below</strong> with.</p>
<table>
<tr><th>S</th><th>date</th><th>purpose</th><th>to spend</th></tr>
<tr><th>?</th><th>date</th><th>purpose</th><th>to spend</th></tr>
% while ( my $d = $credits->next ) {
<tr><td><input type="checkbox" name="credits" value="<%= $d->credId %>"></td>
<td><%= $d->date %></td>
<td><%= $d->purpose %></td>
<td><%= $d->difference %></td>
<td><%== nl2br $d->purpose %></td>
<td class="number"><%== money $d->difference %></td>
</tr>
% }
</table>
<h2>Current arrears</h2>
<p>Check arrears you want to <strong>use this credit</strong> for.</p>
<p>Check arrears you want to <strong>settle with credit(s) above</strong>.</p>
<table>
<tr><th>S</th><th>date</th><th>purpose</th><th>to pay</th></tr>
<tr><th>?</th><th>date</th><th>purpose</th><th>to pay</th></tr>
% while ( my $d = $arrears->next ) {
<tr><td><input type="checkbox" name="debits" value="<%= $d->billId %>"></td>
<td><%= $d->date %></td>
<td><%= $d->purpose %></td>
<td><%= $d->difference %></td>
<td><%== nl2br $d->purpose %></td>
<td class="number"><%== money $d->difference %></td>
</tr>
% }
</table>
<p><button type="submit">Make transfers</button>
<a href="<%= url_for('home') %>">Return to account list</a></p>
</form>

View File

@ -1,8 +1,8 @@
% title 'Bank statement';
<table>
<tr><th>Date</th><th>Account</th><th>Purpose</th><th>Debit</th><th>Credit</th></tr>
<tr><th>Date</th><th>Purpose</th><th>Account</th><th>Debit</th><th>Credit</th></tr>
% while ( my $record = $records->next ) {
<tr><td><%= $record->date %></td><td><%= $record->account %></td><td><%= $record->purpose %></td><td><%= $record->debit %></td><td><%= $record->credit %></td></tr>
<tr><td><%= $record->date %></td><td><%= $record->purpose %></td><td><%= $record->account %></td><td><%= $record->debit %></td><td><%= $record->credit %></td></tr>
% }
</table>

View File

@ -3,6 +3,6 @@
<table class="income">
<tr><th>Id</th><th>Date</th><th>Purpose</th><th>value</th><th>spent</th></tr>
% while ( my $credit = $credits->next ) {
<tr><td class="number"><a href="/credit/<%= $credit->credId %>"><%= $credit->credId %></a></td><td><%= $credit->date %></td><td><%= $credit->purpose %></td><td class="number"><%= $credit->value %></td><td class="number"><%= $credit->spent %></td></tr>
<tr><td class="number"><a href="/credit/<%= $credit->credId %>"><%= $credit->credId %></a></td><td><%= $credit->date %></td><td><%== nl2br $credit->purpose %></td><td class="number"><%== money $credit->value %></td><td class="number"><%== money $credit->spent %></td></tr>
% }
</table>

View File

@ -43,8 +43,8 @@
% my $d = $t->debit;
<tr><td><input type="checkbox" name="revoke" value="<%= $t->billId %>"></td>
<td><%= $t->timestamp %></td>
<td><%= $d->purpose %></td>
<td><%= $t->amount %></td>
<td><%== nl2br $d->purpose %></td>
<td class="number"><%== money $t->amount %></td>
<td><%= $t->note %></td>
</tr>
% }
@ -66,8 +66,8 @@
% while ( my $d = $arrears->next ) {
<tr><td><input type="checkbox" name="spendFor" value="<%= $d->billId %>"></td>
<td><%= $d->date %></td>
<td><%= $d->purpose %></td>
<td><%= $d->difference %></td>
<td><%== nl2br $d->purpose %></td>
<td class="number"><%== money $d->difference %></td>
</tr>
% }
</table>

View File

@ -4,6 +4,6 @@
<tr><th>Id</th><th>Date</th><th>Purpose</th><th>value</th><th>paid</th><th>to</th></tr>
% while ( my $debit = $debits->next ) {
% my $tgt = $debit->target;
<tr><td><a href="/debit/<%= $debit->billId %>"><%= $debit->billId %></a></td><td><%= $debit->date %></td><td><%= $debit->purpose %></td><td class="number"><%= $debit->value %></td><td class="number"><%= $debit->paid %><td><a href="/credit/<%= $tgt->credId %>"><%= $tgt->account->ID %></a></tr>
<tr><td><a href="/debit/<%= $debit->billId %>"><%= $debit->billId %></a></td><td><%= $debit->date %></td><td><%== nl2br $debit->purpose %></td><td class="number"><%== money $debit->value %></td><td class="number"><%== money $debit->paid %><td><a href="/credit/<%= $tgt->credId %>"><%= $tgt->account->ID %></a></tr>
% }
</table>

View File

@ -70,8 +70,8 @@ oops
% my $c = $t->credit;
<tr><td><input type="checkbox" name="revoke" value="<%= $t->credId %>"></td>
<td><%= $t->timestamp %></td>
<td><%= $c->purpose %></td>
<td><%= $t->amount %></td>
<td><%== nl2br $c->purpose %></td>
<td><%== money $t->amount %></td>
<td><%= $t->note %></td>
</tr>
% }
@ -93,8 +93,8 @@ oops
% while ( my $d = $credits->next ) {
<tr><td><input type="checkbox" name="payWith" value="<%= $d->credId %>"></td>
<td><%= $d->date %></td>
<td><%= $d->purpose %></td>
<td><%= $d->difference %></td>
<td><%== nl2br $d->purpose %></td>
<td><%== money $d->difference %></td>
</tr>
% }
</table>

View File

@ -17,5 +17,6 @@
% }
<%= content %>
<p style="text-align:center;"><a href="<%= url_for "home" %>">Back home Account List</p>
</body>
</html>