7bbd8976792d8192ca5faa513c2d2d5de75deb91
[kivitendo-erp.git] / SL / DB / BankAccount.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::BankAccount;
5
6 use strict;
7
8 use SL::DB::MetaSetup::BankAccount;
9 use SL::DB::Manager::BankAccount;
10 use SL::DB::Helper::ActsAsList;
11
12 __PACKAGE__->meta->initialize;
13
14 sub validate {
15   my ($self) = @_;
16
17   my @errors;
18
19   if ( not $self->{chart_id} ) {
20     push @errors, $::locale->text('There is no connected chart.');
21   } else {
22     # check whether the assigned chart is valid or is already being used by
23     # another bank account (there is also a UNIQUE database constraint on
24     # chart_id)
25
26     my $chart_id = $self->chart_id;
27     my $chart = SL::DB::Manager::Chart->find_by( id => $chart_id );
28     if ( $chart ) {
29       my $linked_bank = SL::DB::Manager::BankAccount->find_by( chart_id => $chart_id );
30       if ( $linked_bank ) {
31         if ( not $self->{id} or ( $self->{id} && $linked_bank->id != $self->{id} )) {
32           push @errors, $::locale->text('The account #1 is already being used by bank account #2.', $chart->displayable_name, $linked_bank->{name});
33         };
34       };
35     } else {
36       push @errors, $::locale->text('The chart is not valid.');
37     };
38   };
39
40   push @errors, $::locale->text('The IBAN is missing.') unless $self->{iban};
41
42   return @errors;
43 }
44
45 sub displayable_name {
46   my ($self) = @_;
47
48   return join ' ', grep $_, $self->name, $self->bank, $self->iban;
49 }
50
51 1;