"alle" E-Mail-Adressen per Anhaken als Empfänger hinzufügen können
[kivitendo-erp.git] / SL / DB / BankAccount.pm
1 package SL::DB::BankAccount;
2
3 use strict;
4
5 use SL::DB::MetaSetup::BankAccount;
6 use SL::DB::Manager::BankAccount;
7 use SL::DB::Helper::ActsAsList;
8 use SL::DB::Helper::IBANValidation;
9
10 __PACKAGE__->meta->initialize;
11
12 sub validate {
13   my ($self) = @_;
14
15   my @errors;
16
17   if ( not $self->{chart_id} ) {
18     push @errors, $::locale->text('There is no connected chart.');
19   } else {
20     # check whether the assigned chart is valid or is already being used by
21     # another bank account (there is also a UNIQUE database constraint on
22     # chart_id)
23
24     my $chart_id = $self->chart_id;
25     require SL::DB::Chart;
26     my $chart = SL::DB::Manager::Chart->find_by( id => $chart_id );
27     if ( $chart ) {
28       my $linked_bank = SL::DB::Manager::BankAccount->find_by( chart_id => $chart_id );
29       if ( $linked_bank ) {
30         if ( not $self->{id} or ( $self->{id} && $linked_bank->id != $self->{id} )) {
31           push @errors, $::locale->text('The account #1 is already being used by bank account #2.', $chart->displayable_name, $linked_bank->{name});
32         };
33       };
34     } else {
35       push @errors, $::locale->text('The chart is not valid.');
36     };
37   };
38
39   push @errors, $::locale->text('The IBAN is missing.') unless $self->{iban};
40   push @errors, $self->validate_ibans;
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;