1 package SL::Controller::BankAccount;
5 use parent qw(SL::Controller::Base);
8 use SL::Locale::String;
10 use SL::DB::Manager::BankAccount;
11 use SL::DB::Manager::BankTransaction;
13 use Rose::Object::MakeMethods::Generic (
14 scalar => [ qw(bank_account) ],
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_bank_account', only => [ qw(edit update delete) ]);
27 $self->render('bankaccounts/list',
28 title => t8('Bank accounts'),
29 BANKACCOUNTS => SL::DB::Manager::BankAccount->get_all_sorted,
36 $self->{bank_account} = SL::DB::BankAccount->new;
37 $self->render('bankaccounts/form',
38 title => t8('Add bank account'));
44 $self->render('bankaccounts/form', title => t8('Edit bank account'));
50 $self->{bank_account} = SL::DB::BankAccount->new;
51 $self->create_or_update;
56 $self->create_or_update;
62 if ( $self->{bank_account}->{number_of_bank_transactions} > 0 ) {
63 flash_later('error', $::locale->text('The bank account has been used and cannot be deleted.'));
64 } elsif ( eval { $self->{bank_account}->delete; 1; } ) {
65 flash_later('info', $::locale->text('The bank account has been deleted.'));
67 flash_later('error', $::locale->text('The bank account has been used and cannot be deleted.'));
69 $self->redirect_to(action => 'list');
76 SL::DB::BankAccount->reorder_list(@{ $::form->{account_id} || [] });
77 $self->render(\'', { type => 'json' });
85 $::auth->assert('config');
88 sub load_bank_account {
91 $self->{bank_account} = SL::DB::BankAccount->new(id => $::form->{id})->load;
92 $self->{bank_account}->{number_of_bank_transactions} = SL::DB::Manager::BankTransaction->get_all_count( query => [ local_bank_account_id => $self->{bank_account}->{id} ] );
99 sub create_or_update {
101 my $is_new = !$self->{bank_account}->id;
103 my $params = delete($::form->{bank_account}) || { };
105 $self->{bank_account}->assign_attributes(%{ $params });
107 my @errors = $self->{bank_account}->validate;
110 flash('error', @errors);
111 $self->render('bankaccounts/form',
112 title => $is_new ? t8('Add bank account') : t8('Edit bank account'));
116 $self->{bank_account}->save;
118 flash_later('info', $is_new ? t8('The bank account has been created.') : t8('The bank account has been saved.'));
119 $self->redirect_to(action => 'list');