Changes and fixes in HTTP (e.g. + account upsertion)
==================================================== * Account upsertion * Empty credit and debit forms, the latter with the ability to charge groups * Fixed redirection on posting transfers * Reverse order of credits and debits * Style: prevent monospace size reduction in some browsers; td.mark class * Totals in report and and bankStatement
This commit is contained in:
@@ -72,15 +72,17 @@ sub startup {
|
||||
|
||||
my $admin = $auth->under(sub { shift->stash('grade') > 1 });
|
||||
$admin->any('/admin')->to('admin#dash');
|
||||
$admin->any( [qw/GET POST/] => '/account/:account' => { account => undef })
|
||||
->to('account#upsert');
|
||||
$admin->post('/:account/in')->to('credit#upsert');
|
||||
$admin->post('/:account/out')->to('debit#upsert');
|
||||
$admin->get('/:account/credits')->to('credit#list');
|
||||
$admin->get('/:account/debits')->to('debit#list');
|
||||
$admin->post('/:account/transfer')->to('account#transfer');
|
||||
$admin->any( [qw/GET POST PATCH/] => '/credit/:id' )->to('credit#upsert');
|
||||
$admin->post('/credit')->to('credit#upsert');
|
||||
$admin->any( [qw/GET POST/] => '/credit')->to('credit#upsert');
|
||||
$admin->any( [qw/GET POST PATCH/] => '/debit/*id' )->to('debit#upsert');
|
||||
$admin->post('/debit')->to('debit#upsert');
|
||||
$admin->any( [qw/GET POST/] => '/debit')->to('debit#upsert');
|
||||
$admin->get('/:action')->to(controller => 'admin');
|
||||
|
||||
$auth->get('/')->to('account#list')->name('home');
|
||||
|
||||
@@ -18,6 +18,40 @@ sub list {
|
||||
|
||||
}
|
||||
|
||||
sub upsert {
|
||||
my $self = shift;
|
||||
|
||||
my $db = $self->app->db;
|
||||
my $account_rs = $db->resultset("Account");
|
||||
my $account;
|
||||
if ( my $name = $self->stash("account") ) {
|
||||
$self->stash( name => $name );
|
||||
$account = $account_rs->find($name);
|
||||
}
|
||||
else {
|
||||
$self->stash( name => undef );
|
||||
$account = $account_rs->new({});
|
||||
}
|
||||
$self->stash( account => $account );
|
||||
|
||||
if ( $self->req->method eq 'POST' ) {
|
||||
for my $field ($account->result_source->columns) {
|
||||
my $value = $self->param($field);
|
||||
$account->$field($value);
|
||||
}
|
||||
$account->update_or_insert();
|
||||
$self->redirect_to("home");
|
||||
}
|
||||
else {
|
||||
my @types = $account_rs->search({ type => { '!=' => q// } }, {
|
||||
columns => ['type'], distinct => 1
|
||||
})->get_column("type")->all;
|
||||
$self->stash( types => \@types );
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub history {
|
||||
my $self = shift;
|
||||
my $history = $self->app->db->resultset("History")->search({
|
||||
@@ -34,18 +68,20 @@ sub transfer {
|
||||
my $credits = $account->available_credits;
|
||||
my $arrears = $account->current_arrears;
|
||||
|
||||
return $self->redirect_to('home')
|
||||
if !( $credits->count() && $arrears->count() );
|
||||
if ( $self->req->method eq 'POST' ) {
|
||||
$db->make_transfers(
|
||||
$self->every_param('credits')
|
||||
=> $self->every_param('debits')
|
||||
);
|
||||
}
|
||||
|
||||
if ( !( $credits->count && $arrears->count ) ) {
|
||||
$self->redirect_to('home');
|
||||
return;
|
||||
}
|
||||
|
||||
$self->stash( credits => $credits, arrears => $arrears );
|
||||
|
||||
return if $self->req->method ne 'POST';
|
||||
|
||||
$db->make_transfers(
|
||||
$self->every_param('credits')
|
||||
=> $self->every_param('debits')
|
||||
);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ sub list {
|
||||
return;
|
||||
}
|
||||
|
||||
$self->stash( credits => $account->credits_rs );
|
||||
my $rs = $account->search_related(
|
||||
credits => {}, { order_by => { -desc => [qw/date/] } }
|
||||
);
|
||||
$self->stash( credits => $rs );
|
||||
|
||||
return;
|
||||
|
||||
|
||||
@@ -18,7 +18,10 @@ sub list {
|
||||
return;
|
||||
}
|
||||
|
||||
$self->stash( debits => $account->debits_rs );
|
||||
my $rs = $account->search_related(
|
||||
debits => {}, { order_by => { -desc => [qw/date/] }}
|
||||
);
|
||||
$self->stash( debits => $rs );
|
||||
|
||||
return;
|
||||
|
||||
@@ -29,17 +32,36 @@ sub upsert {
|
||||
|
||||
my $db = $self->app->db;
|
||||
my $id = $self->stash("id");
|
||||
my $account = $self->stash("account");
|
||||
my @FIELDS = qw/billId date purpose value targetCredit/;
|
||||
my $debtor = $self->stash("account") // $self->param("debtor");
|
||||
if ( $self->req->method eq 'POST' && $debtor =~ s{^@}{} ) {
|
||||
|
||||
my $group_members = $db->resultset('Account')->search({
|
||||
type => $debtor
|
||||
});
|
||||
|
||||
while ( my $m = $group_members->next ) {
|
||||
my %props = map { $_ => $self->param($_) } @FIELDS;
|
||||
for ( $props{billId} ) {
|
||||
s{\%u}{ $m->ID }e or $_ .= "-" . $m->ID;
|
||||
}
|
||||
$m->debits->create( \%props );
|
||||
}
|
||||
|
||||
$self->redirect_to('home');
|
||||
return;
|
||||
}
|
||||
|
||||
my $method = $id ? 'find_or_new' : 'new';
|
||||
my $debit = $db->resultset("Debit")->$method(
|
||||
{ $id ? (billId => $id) : (), debtor => $account }
|
||||
{ $id ? (billId => $id) : (), debtor => $debtor }
|
||||
);
|
||||
|
||||
$self->stash( debit => $debit );
|
||||
|
||||
if ( $self->req->method eq 'GET' ) {
|
||||
my $targets
|
||||
= $db->resultset("Credit")->search({ account => { '!=' => $account } },
|
||||
= $db->resultset("Credit")->search({ account => { '!=' => $debtor } },
|
||||
{ join => 'income',
|
||||
'+select' => [ { count => 'income.targetCredit', -as => 'targetted_by' } ],
|
||||
group_by => ['income.targetCredit'],
|
||||
@@ -52,7 +74,7 @@ sub upsert {
|
||||
return;
|
||||
}
|
||||
|
||||
for my $field ( qw/billId debtor date purpose value targetCredit/ ) {
|
||||
for my $field ( @FIELDS ) {
|
||||
my $value = $self->param($field);
|
||||
$debit->$field($value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user