Revert "Banktransactions - beim Speichern der Rechnung Zahlungsart prüfen"
[kivitendo-erp.git] / SL / Controller / BankAccount.pm
1 package SL::Controller::BankAccount;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Helper::Flash;
8 use SL::Locale::String;
9 use SL::DB::Default;
10 use SL::DB::Manager::BankAccount;
11 use SL::DB::Manager::BankTransaction;
12
13 use Rose::Object::MakeMethods::Generic (
14   scalar                  => [ qw(bank_account) ],
15 );
16
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_bank_account', only => [ qw(edit update delete) ]);
19
20 #
21 # actions
22 #
23
24 sub action_list {
25   my ($self) = @_;
26
27   $self->render('bankaccounts/list',
28                 title           => t8('Bank accounts'),
29                 BANKACCOUNTS    => SL::DB::Manager::BankAccount->get_all_sorted,
30                );
31 }
32
33 sub action_new {
34   my ($self) = @_;
35
36   $self->{bank_account} = SL::DB::BankAccount->new;
37   $self->render('bankaccounts/form',
38                  title => t8('Add bank account'));
39 }
40
41 sub action_edit {
42   my ($self) = @_;
43
44   $self->render('bankaccounts/form', title => t8('Edit bank account'));
45 }
46
47 sub action_create {
48   my ($self) = @_;
49
50   $self->{bank_account} = SL::DB::BankAccount->new;
51   $self->create_or_update;
52 }
53
54 sub action_update {
55   my ($self) = @_;
56   $self->create_or_update;
57 }
58
59 sub action_delete {
60   my ($self) = @_;
61
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.'));
66   } else {
67     flash_later('error', $::locale->text('The bank account has been used and cannot be deleted.'));
68   };
69   $self->redirect_to(action => 'list');
70
71 }
72
73 sub action_reorder {
74   my ($self) = @_;
75
76   SL::DB::BankAccount->reorder_list(@{ $::form->{account_id} || [] });
77   $self->render(\'', { type => 'json' });
78 }
79
80 #
81 # filters
82 #
83
84 sub check_auth {
85   $::auth->assert('config');
86 }
87
88 sub load_bank_account {
89   my ($self) = @_;
90
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} ] );
93 }
94
95 #
96 # helpers
97 #
98
99 sub create_or_update {
100   my ($self) = @_;
101   my $is_new = !$self->{bank_account}->id;
102
103   my $params = delete($::form->{bank_account}) || { };
104
105   $self->{bank_account}->assign_attributes(%{ $params });
106
107   my @errors = $self->{bank_account}->validate;
108
109   if (@errors) {
110     flash('error', @errors);
111     $self->render('bankaccounts/form',
112                    title => $is_new ? t8('Add bank account') : t8('Edit bank account'));
113     return;
114   }
115
116   $self->{bank_account}->save;
117
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');
120 }
121
122 1;