c6ffad4da406347457a655faa88a4ff9c687dec5
[kivitendo-erp.git] / SL / DB / AdditionalBillingAddress.pm
1 package SL::DB::AdditionalBillingAddress;
2
3 use strict;
4
5 use SL::DB::MetaSetup::AdditionalBillingAddress;
6 use SL::DB::Manager::AdditionalBillingAddress;
7
8 __PACKAGE__->meta->initialize;
9
10 __PACKAGE__->after_save('_after_save_ensure_only_one_marked_as_default_per_customer');
11
12 sub _after_save_ensure_only_one_marked_as_default_per_customer {
13   my ($self) = @_;
14
15   if ($self->id && $self->customer_id && $self->default_address) {
16     SL::DB::Manager::AdditionalBillingAddress->update_all(
17       set   => { default_address => 0 },
18       where => [
19         customer_id => $self->customer_id,
20         '!id'       => $self->id,
21       ],
22     );
23   }
24
25   return 1;
26 }
27
28 sub displayable_id {
29   my $self = shift;
30   my $text = join('; ', grep { $_ } (map({ $self->$_ } qw(name street)),
31                                      join(' ', grep { $_ }
32                                                map  { $self->$_ }
33                                                qw(zipcode city))));
34
35   return $text;
36 }
37
38 sub used {
39   my ($self) = @_;
40
41   return unless $self->id;
42
43   require SL::DB::Order;
44   require SL::DB::Invoice;
45   require SL::DB::DeliveryOrder;
46
47   my %args = (query => [ billing_address_id => $self->id ]);
48
49   return SL::DB::Manager::Invoice->get_all_count(%args)
50       || SL::DB::Manager::Order->get_all_count(%args)
51       || SL::DB::Manager::DeliveryOrder->get_all_count(%args);
52 }
53
54 sub detach {
55   $_[0]->customer_id(undef);
56   return $_[0];
57 }
58
59 1;