1 package SL::DB::Customer;
5 use List::Util qw(first);
6 use Rose::DB::Object::Helpers qw(as_tree);
8 use SL::Locale::String qw(t8);
10 use SL::DB::MetaSetup::Customer;
11 use SL::DB::Manager::Customer;
12 use SL::DB::Helper::IBANValidation;
13 use SL::DB::Helper::TransNumberGenerator;
14 use SL::DB::Helper::VATIDNrValidation;
15 use SL::DB::Helper::CustomVariables (
19 use SL::DB::Helper::DisplayableNamePreferences (
20 title => t8('Customer'),
21 options => [ {name => 'customernumber', title => t8('Customer Number') },
22 {name => 'name', title => t8('Name') },
23 {name => 'street', title => t8('Street') },
24 {name => 'city', title => t8('City') },
25 {name => 'zipcode', title => t8('Zipcode')},
26 {name => 'email', title => t8('E-Mail') },
27 {name => 'phone', title => t8('Phone') }, ]
32 __PACKAGE__->meta->add_relationship(
33 additional_billing_addresses => {
34 type => 'one to many',
35 class => 'SL::DB::AdditionalBillingAddress',
36 column_map => { id => 'customer_id' },
37 manager_args => { sort_by => 'lower(additional_billing_addresses.name)' },
40 type => 'one to many',
41 class => 'SL::DB::Shipto',
42 column_map => { id => 'trans_id' },
43 manager_args => { sort_by => 'lower(shipto.shiptoname)' },
44 query_args => [ module => 'CT' ],
47 type => 'one to many',
48 class => 'SL::DB::Contact',
49 column_map => { id => 'cp_cv_id' },
50 manager_args => { sort_by => 'lower(contacts.cp_name)' },
54 __PACKAGE__->meta->initialize;
56 __PACKAGE__->before_save('_before_save_set_customernumber');
59 sub _before_save_set_customernumber {
62 $self->create_trans_number if !defined $self->customernumber || $self->customernumber eq '';
70 push @errors, $::locale->text('The customer name is missing.') if !$self->name;
71 push @errors, $self->validate_ibans;
72 push @errors, $self->validate_vat_id_numbers;
80 return join ', ', grep { $_ } $self->street, $self->zipcode, $self->city;
83 sub last_used_ar_chart {
89 JOIN acc_trans ac ON (ac.chart_id = c.id)
90 JOIN ar a ON (a.id = ac.trans_id)
91 WHERE (a.customer_id = ?)
92 AND (c.category = 'I')
93 AND (c.link !~ '_(paid|tax)')
94 AND (a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?))
95 ORDER BY ac.acc_trans_id ASC
99 my ($chart_id) = SL::DBUtils::selectfirst_array_query($::form, $self->db->dbh, $query, ($self->id) x 2);
101 return if !$chart_id;
102 return SL::DB::Chart->load_cached($chart_id);
105 sub is_customer { 1 };
107 sub payment_terms { goto &payment }
108 sub number { goto &customernumber }
110 sub create_zugferd_invoices_for_this_customer {
114 return $::instance_conf->get_create_zugferd_invoices if $self->create_zugferd_invoices == -1;
115 return $self->create_zugferd_invoices;
118 sub default_billing_address {
121 die 'not an accessor' if @_ > 1;
122 return first { $_->default_address } @{ $self->additional_billing_addresses };