2017-01-15 16:27:22 +01:00
|
|
|
use strict;
|
|
|
|
|
|
|
|
package TrsrDB::HTTP::Account;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
use Carp qw(croak);
|
|
|
|
|
|
|
|
sub list {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $accounts = $self->app->db->resultset("Account");
|
2017-01-28 14:00:20 +01:00
|
|
|
my $user = $self->stash("user");
|
|
|
|
my %args = $user->grade ? ()
|
|
|
|
: $accounts->find( $user->user_id ) ? ( 'me.ID' => $user->user_id )
|
|
|
|
: ( type => q{} );
|
|
|
|
$accounts = $accounts->search(\%args, {
|
|
|
|
order_by => { -asc => [qw/type balance.even_until me.ID/] },
|
|
|
|
prefetch => 'balance'
|
|
|
|
});
|
2017-01-15 16:27:22 +01:00
|
|
|
|
|
|
|
$self->stash( accounts => $accounts );
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-25 22:38:39 +01:00
|
|
|
sub upsert {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $db = $self->app->db;
|
|
|
|
my $account_rs = $db->resultset("Account");
|
|
|
|
my $account;
|
|
|
|
if ( my $name = $self->stash("account") ) {
|
2017-02-02 22:23:23 +01:00
|
|
|
$self->stash( id => $name );
|
2017-01-25 22:38:39 +01:00
|
|
|
$account = $account_rs->find($name);
|
|
|
|
}
|
|
|
|
else {
|
2017-02-02 22:23:23 +01:00
|
|
|
$self->stash( id => undef );
|
2017-01-25 22:38:39 +01:00
|
|
|
$account = $account_rs->new({});
|
|
|
|
}
|
|
|
|
$self->stash( account => $account );
|
|
|
|
|
2017-02-02 22:23:23 +01:00
|
|
|
my $iban = $self->param("IBAN") // $account->IBAN;
|
|
|
|
|
2017-01-25 22:38:39 +01:00
|
|
|
if ( $self->req->method eq 'POST' ) {
|
2017-02-02 22:23:23 +01:00
|
|
|
my $p = $self->req->params->to_hash;
|
|
|
|
if ( $p->{IBAN} eq q{*} ) {
|
|
|
|
$p->{IBAN} = q{};
|
|
|
|
}
|
|
|
|
elsif ( !$iban ) {
|
|
|
|
delete $p->{IBAN};
|
|
|
|
}
|
2017-01-25 22:38:39 +01:00
|
|
|
for my $field ($account->result_source->columns) {
|
2017-02-02 22:23:23 +01:00
|
|
|
my $value = $p->{ $field };
|
2017-01-25 22:38:39 +01:00
|
|
|
$account->$field($value);
|
|
|
|
}
|
|
|
|
$account->update_or_insert();
|
|
|
|
$self->redirect_to("home");
|
|
|
|
}
|
|
|
|
else {
|
2017-02-02 22:23:23 +01:00
|
|
|
if ( defined( $iban ) ) {
|
|
|
|
$iban = q{*} if !$iban;
|
|
|
|
$account->IBAN($iban);
|
|
|
|
}
|
2017-01-25 22:38:39 +01:00
|
|
|
my @types = $account_rs->search({ type => { '!=' => q// } }, {
|
|
|
|
columns => ['type'], distinct => 1
|
|
|
|
})->get_column("type")->all;
|
|
|
|
$self->stash( types => \@types );
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-15 16:27:22 +01:00
|
|
|
sub history {
|
|
|
|
my $self = shift;
|
2017-01-29 15:57:34 +01:00
|
|
|
my %query = ( account => $self->stash("account") );
|
|
|
|
if ( my $p = $self->param("purpose") ) {
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
my $history = $self->app->db->resultset("History")->search(
|
|
|
|
TrsrDB::HTTP::process_table_filter_widget(
|
|
|
|
$self, \%query,
|
|
|
|
{ order_by => { -desc => [qw/date/] } }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-01-15 16:27:22 +01:00
|
|
|
$self->stash( history => $history );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub transfer {
|
|
|
|
my $self = shift;
|
|
|
|
my $db = $self->app->db;
|
|
|
|
my $account = $db->resultset("Account")->find( $self->stash("account") );
|
|
|
|
|
2017-02-02 22:23:23 +01:00
|
|
|
my $credits_rs = $account->available_credits;
|
|
|
|
my $arrears_rs = $account->current_arrears;
|
2017-01-15 22:03:06 +01:00
|
|
|
|
2017-02-02 22:23:23 +01:00
|
|
|
my $credit_ids = $self->every_param('credits');
|
|
|
|
my $arrear_ids = $self->every_param('debits');
|
|
|
|
|
|
|
|
if ( $self->req->method eq 'POST' && @$credit_ids && @$arrear_ids ) {
|
|
|
|
$db->make_transfers( $credit_ids => $arrear_ids );
|
2017-01-25 22:38:39 +01:00
|
|
|
}
|
2017-01-15 22:03:06 +01:00
|
|
|
|
2017-02-02 22:23:23 +01:00
|
|
|
if ( !( $credits_rs->count && $arrears_rs->count ) ) {
|
2017-01-25 22:38:39 +01:00
|
|
|
$self->redirect_to('home');
|
|
|
|
return;
|
|
|
|
}
|
2017-01-15 22:03:06 +01:00
|
|
|
|
2017-02-02 22:23:23 +01:00
|
|
|
$self->stash( credits => $credits_rs, arrears => $arrears_rs );
|
2017-01-15 16:27:22 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
2017-01-15 22:03:06 +01:00
|
|
|
|
|
|
|
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/] } }
|
|
|
|
) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-15 16:27:22 +01:00
|
|
|
1;
|
|
|
|
|