1 package SL::DB::Customer;
5 use Rose::DB::Object::Helpers qw(as_tree);
7 use SL::Locale::String qw(t8);
9 use SL::DB::MetaSetup::Customer;
10 use SL::DB::Manager::Customer;
11 use SL::DB::Helper::IBANValidation;
12 use SL::DB::Helper::TransNumberGenerator;
13 use SL::DB::Helper::VATIDNrValidation;
14 use SL::DB::Helper::CustomVariables (
18 use SL::DB::Helper::DisplayableNamePreferences (
19 title => t8('Customer'),
20 options => [ {name => 'customernumber', title => t8('Customer Number') },
21 {name => 'name', title => t8('Name') },
22 {name => 'street', title => t8('Street') },
23 {name => 'city', title => t8('City') },
24 {name => 'zipcode', title => t8('Zipcode')},
25 {name => 'email', title => t8('E-Mail') },
26 {name => 'phone', title => t8('Phone') }, ]
31 __PACKAGE__->meta->add_relationship(
33 type => 'one to many',
34 class => 'SL::DB::Shipto',
35 column_map => { id => 'trans_id' },
36 manager_args => { sort_by => 'lower(shipto.shiptoname)' },
37 query_args => [ module => 'CT' ],
40 type => 'one to many',
41 class => 'SL::DB::Contact',
42 column_map => { id => 'cp_cv_id' },
43 manager_args => { sort_by => 'lower(contacts.cp_name)' },
47 __PACKAGE__->meta->initialize;
49 __PACKAGE__->before_save('_before_save_set_customernumber');
52 sub _before_save_set_customernumber {
55 $self->create_trans_number if !defined $self->customernumber || $self->customernumber eq '';
63 push @errors, $::locale->text('The customer name is missing.') if !$self->name;
64 push @errors, $self->validate_ibans;
65 push @errors, $self->validate_vat_id_numbers;
73 return join ', ', grep { $_ } $self->street, $self->zipcode, $self->city;
76 sub last_used_ar_chart {
82 JOIN acc_trans ac ON (ac.chart_id = c.id)
83 JOIN ar a ON (a.id = ac.trans_id)
84 WHERE (a.customer_id = ?)
85 AND (c.category = 'I')
86 AND (c.link !~ '_(paid|tax)')
87 AND (a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?))
88 ORDER BY ac.acc_trans_id ASC
92 my ($chart_id) = SL::DBUtils::selectfirst_array_query($::form, $self->db->dbh, $query, ($self->id) x 2);
95 return SL::DB::Chart->load_cached($chart_id);
98 sub is_customer { 1 };
100 sub payment_terms { goto &payment }
101 sub number { goto &customernumber }
103 sub create_zugferd_invoices_for_this_customer {
107 return $::instance_conf->get_create_zugferd_invoices if $self->create_zugferd_invoices == -1;
108 return $self->create_zugferd_invoices;