kivitendo/SL/DB/Vendor.pm @ 4b1666b7
82515b2d | Sven Schöling | package SL::DB::Vendor;
|
||
use strict;
|
||||
f16c5520 | Sven Schöling | use Rose::DB::Object::Helpers qw(as_tree);
|
||
9e24ed15 | Bernd Bleßmann | use SL::Locale::String qw(t8);
|
||
0fa80981 | Moritz Bunkus | use SL::DBUtils ();
|
||
82515b2d | Sven Schöling | use SL::DB::MetaSetup::Vendor;
|
||
2b82180a | Moritz Bunkus | use SL::DB::Manager::Vendor;
|
||
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('Vendor'),
|
||||
options => [ {name => 'vendornumber', title => t8('Vendor 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') },
|
||||
{name => 'phone', title => t8('Phone') }, ]
|
||||
);
|
||||
82515b2d | Sven Schöling | |||
use SL::DB::VC;
|
||||
__PACKAGE__->meta->add_relationship(
|
||||
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' ],
|
||
82515b2d | Sven Schöling | },
|
||
efc63086 | Moritz Bunkus | 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_vendornumber');
|
||
sub _before_save_set_vendornumber {
|
||||
my ($self) = @_;
|
||||
7c667c90 | Sven Schöling | $self->create_trans_number if !defined $self->vendornumber || $self->vendornumber eq '';
|
||
f87763cd | Moritz Bunkus | return 1;
|
||
930e5ecb | Moritz Bunkus | }
|
||
454d4b8f | Bernd Bleßmann | sub validate {
|
||
my ($self) = @_;
|
||||
my @errors;
|
||||
push @errors, $::locale->text('The vendor 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;
|
||||
}
|
||||
5b192e71 | Moritz Bunkus | sub is_customer { 0 };
|
||
sub is_vendor { 1 };
|
||||
38b898b0 | Moritz Bunkus | sub payment_terms { goto &payment }
|
||
f2372ded | Sven Schöling | sub number { goto &vendornumber }
|
||
5b192e71 | Moritz Bunkus | |||
0fa80981 | Moritz Bunkus | sub last_used_ap_chart {
|
||
my ($self) = @_;
|
||||
my $query = <<EOSQL;
|
||||
SELECT c.id
|
||||
FROM chart c
|
||||
JOIN acc_trans ac ON (ac.chart_id = c.id)
|
||||
JOIN ap a ON (a.id = ac.trans_id)
|
||||
WHERE (a.vendor_id = ?)
|
||||
AND (c.category = 'E')
|
||||
AND (c.link !~ '_(paid|tax)')
|
||||
AND (a.id IN (SELECT max(a2.id) FROM ap a2 WHERE a2.vendor_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);
|
||||
}
|
||||
82515b2d | Sven Schöling | 1;
|