Projekt

Allgemein

Profil

Herunterladen (3,47 KB) Statistiken
| Zweig: | Markierung: | Revision:
82515b2d Sven Schöling
package SL::DB::Customer;

use strict;

844a541e Moritz Bunkus
use List::Util qw(first);
f16c5520 Sven Schöling
use Rose::DB::Object::Helpers qw(as_tree);

9e24ed15 Bernd Bleßmann
use SL::Locale::String qw(t8);
41cceb1e Moritz Bunkus
use SL::DBUtils ();
82515b2d Sven Schöling
use SL::DB::MetaSetup::Customer;
930e5ecb Moritz Bunkus
use SL::DB::Manager::Customer;
07a768c3 Moritz Bunkus
use SL::DB::Helper::IBANValidation;
930e5ecb Moritz Bunkus
use SL::DB::Helper::TransNumberGenerator;
29a13714 Moritz Bunkus
use SL::DB::Helper::VATIDNrValidation;
e1bf173b Sven Schöling
use SL::DB::Helper::CustomVariables (
module => 'CT',
cvars_alias => 1,
);
9e24ed15 Bernd Bleßmann
use SL::DB::Helper::DisplayableNamePreferences (
title => t8('Customer'),
options => [ {name => 'customernumber', title => t8('Customer Number') },
{name => 'name', title => t8('Name') },
{name => 'street', title => t8('Street') },
{name => 'city', title => t8('City') },
{name => 'zipcode', title => t8('Zipcode')},
{name => 'email', title => t8('E-Mail') },
9adbb6d6 Geoffrey Richardson
{name => 'phone', title => t8('Phone') },
{name => 'country', title => t8('Country') }, ]
9e24ed15 Bernd Bleßmann
);
82515b2d Sven Schöling
use SL::DB::VC;

__PACKAGE__->meta->add_relationship(
b5b366c9 Moritz Bunkus
additional_billing_addresses => {
type => 'one to many',
class => 'SL::DB::AdditionalBillingAddress',
column_map => { id => 'customer_id' },
manager_args => { sort_by => 'lower(additional_billing_addresses.name)' },
},
82515b2d Sven Schöling
shipto => {
type => 'one to many',
class => 'SL::DB::Shipto',
column_map => { id => 'trans_id' },
manager_args => { sort_by => 'lower(shipto.shiptoname)' },
9e152755 Moritz Bunkus
query_args => [ module => 'CT' ],
},
contacts => {
type => 'one to many',
class => 'SL::DB::Contact',
column_map => { id => 'cp_cv_id' },
manager_args => { sort_by => 'lower(contacts.cp_name)' },
82515b2d Sven Schöling
},
);

__PACKAGE__->meta->initialize;

930e5ecb Moritz Bunkus
__PACKAGE__->before_save('_before_save_set_customernumber');

9e24ed15 Bernd Bleßmann
930e5ecb Moritz Bunkus
sub _before_save_set_customernumber {
my ($self) = @_;

7c667c90 Sven Schöling
$self->create_trans_number if !defined $self->customernumber || $self->customernumber eq '';
f87763cd Moritz Bunkus
return 1;
930e5ecb Moritz Bunkus
}

454d4b8f Bernd Bleßmann
sub validate {
my ($self) = @_;

my @errors;
push @errors, $::locale->text('The customer name is missing.') if !$self->name;
07a768c3 Moritz Bunkus
push @errors, $self->validate_ibans;
29a13714 Moritz Bunkus
push @errors, $self->validate_vat_id_numbers;
454d4b8f Bernd Bleßmann
return @errors;
}

2a052485 Sven Schöling
sub short_address {
my ($self) = @_;

return join ', ', grep { $_ } $self->street, $self->zipcode, $self->city;
}

41cceb1e Moritz Bunkus
sub last_used_ar_chart {
my ($self) = @_;

my $query = <<EOSQL;
SELECT c.id
FROM chart c
JOIN acc_trans ac ON (ac.chart_id = c.id)
JOIN ar a ON (a.id = ac.trans_id)
WHERE (a.customer_id = ?)
AND (c.category = 'I')
AND (c.link !~ '_(paid|tax)')
AND (a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?))
ORDER BY ac.acc_trans_id ASC
LIMIT 1
EOSQL

my ($chart_id) = SL::DBUtils::selectfirst_array_query($::form, $self->db->dbh, $query, ($self->id) x 2);

return if !$chart_id;
return SL::DB::Chart->load_cached($chart_id);
}

5b192e71 Moritz Bunkus
sub is_customer { 1 };
sub is_vendor { 0 };
38b898b0 Moritz Bunkus
sub payment_terms { goto &payment }
f2372ded Sven Schöling
sub number { goto &customernumber }
5b192e71 Moritz Bunkus
5da10e01 Moritz Bunkus
sub create_zugferd_invoices_for_this_customer {
my ($self) = @_;

no warnings 'once';
return $::instance_conf->get_create_zugferd_invoices if $self->create_zugferd_invoices == -1;
return $self->create_zugferd_invoices;
}

844a541e Moritz Bunkus
sub default_billing_address {
my $self = shift;

die 'not an accessor' if @_ > 1;
return first { $_->default_address } @{ $self->additional_billing_addresses };
}

82515b2d Sven Schöling
1;