c2c436dfd551cce708ecbd685c6c7724f888ee77
[kivitendo-erp.git] / SL / DB / Customer.pm
1 package SL::DB::Customer;
2
3 use strict;
4
5 use SL::DB::MetaSetup::Customer;
6 use SL::DB::Manager::Customer;
7 use SL::DB::Helper::TransNumberGenerator;
8
9 use SL::DB::VC;
10
11 __PACKAGE__->meta->add_relationship(
12   shipto => {
13     type         => 'one to many',
14     class        => 'SL::DB::Shipto',
15     column_map   => { id      => 'trans_id' },
16     manager_args => { sort_by => 'lower(shipto.shiptoname)' },
17     query_args   => [ module   => 'CT' ],
18   },
19   contacts => {
20     type         => 'one to many',
21     class        => 'SL::DB::Contact',
22     column_map   => { id      => 'cp_cv_id' },
23     manager_args => { sort_by => 'lower(contacts.cp_name)' },
24   },
25   business => {
26     type         => 'one to one',
27     class        => 'SL::DB::Business',
28     column_map   => { business_id => 'id' },
29   },
30   custom_variables => {
31     type           => 'one to many',
32     class          => 'SL::DB::CustomVariable',
33     column_map     => { id => 'trans_id' },
34     query_args     => [ config_id => [ \"(SELECT custom_variable_configs.id FROM custom_variable_configs WHERE custom_variable_configs.module = 'CT')" ] ],
35   },
36 );
37
38 __PACKAGE__->meta->make_manager_class;
39 __PACKAGE__->meta->initialize;
40
41 __PACKAGE__->before_save('_before_save_set_customernumber');
42
43 sub _before_save_set_customernumber {
44   my ($self) = @_;
45
46   $self->create_trans_number if $self->customernumber eq '';
47   return 1;
48 }
49
50 sub short_address {
51   my ($self) = @_;
52
53   return join ', ', grep { $_ } $self->street, $self->zipcode, $self->city;
54 }
55
56 1;