5f4a3e4765899c9155b20228939491a9cb6bf5e0
[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     require SL::DB::Chart;
28     my $chart = SL::DB::Manager::Chart->find_by( id => $chart_id );
29     if ( $chart ) {
30       my $linked_bank = SL::DB::Manager::BankAccount->find_by( chart_id => $chart_id );
31       if ( $linked_bank ) {
32         if ( not $self->{id} or ( $self->{id} && $linked_bank->id != $self->{id} )) {
33           push @errors, $::locale->text('The account #1 is already being used by bank account #2.', $chart->displayable_name, $linked_bank->{name});
34         };
35       };
36     } else {
37       push @errors, $::locale->text('The chart is not valid.');
38     };
39   };
40
41   push @errors, $::locale->text('The IBAN is missing.') unless $self->{iban};
42
43   return @errors;
44 }
45
46 sub displayable_name {
47   my ($self) = @_;
48
49   return join ' ', grep $_, $self->name, $self->bank, $self->iban;
50 }
51
52 1;