1 package SL::Controller::CsvImport::CustomerVendor;
7 use SL::DB::CustomVariable;
8 use SL::DB::CustomVariableConfig;
9 use SL::DB::PaymentTerm;
12 use parent qw(SL::Controller::CsvImport::Base);
14 use Rose::Object::MakeMethods::Generic
16 'scalar --get_set_init' => [ qw(table languages_by businesses_by) ],
21 $self->table($self->controller->profile->get('table') eq 'customer' ? 'customer' : 'vendor');
26 $self->class('SL::DB::' . ucfirst($self->table));
29 sub init_all_cvar_configs {
32 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'CT' ]);
35 sub init_businesses_by {
38 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Business->get_all } } ) } qw(id description) };
41 sub init_languages_by {
44 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
50 my $numbercolumn = $self->controller->profile->get('table') . "number";
51 my %vcs_by_number = map { ( $_->$numbercolumn => 1 ) } @{ $self->existing_objects };
53 foreach my $entry (@{ $self->controller->data }) {
54 my $object = $entry->{object};
56 $self->check_name($entry);
57 $self->check_language($entry);
58 $self->check_business($entry);
59 $self->check_payment($entry);
60 $self->handle_cvars($entry);
62 next if @{ $entry->{errors} };
64 if ($vcs_by_number{ $object->$numbercolumn }) {
65 $entry->{object}->$numbercolumn('####');
67 $vcs_by_number{ $object->$numbercolumn } = $object;
71 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(language business payment));
72 $self->add_cvar_raw_data_columns;
75 sub get_duplicate_check_fields {
78 label => $::locale->text('Customer Name'),
81 my $name = shift->name;
82 $name =~ s/[\s,\.\-]//g;
90 my ($self, $entry) = @_;
92 my $name = $entry->{object}->name;
98 push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
103 my ($self, $entry) = @_;
105 my $object = $entry->{object};
107 # Check whether or not language ID is valid.
108 if ($object->language_id && !$self->languages_by->{id}->{ $object->language_id }) {
109 push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
113 # Map name to ID if given.
114 if (!$object->language_id && $entry->{raw_data}->{language}) {
115 my $language = $self->languages_by->{description}->{ $entry->{raw_data}->{language} }
116 || $self->languages_by->{article_code}->{ $entry->{raw_data}->{language} };
119 push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
123 $object->language_id($language->id);
130 my ($self, $entry) = @_;
132 my $object = $entry->{object};
134 # Check whether or not business ID is valid.
135 if ($object->business_id && !$self->businesss_by->{id}->{ $object->business_id }) {
136 push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
140 # Map name to ID if given.
141 if (!$object->business_id && $entry->{raw_data}->{business}) {
142 my $business = $self->businesses_by->{description}->{ $entry->{raw_data}->{business} };
145 push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
149 $object->business_id($business->id);
156 my ($self, %params) = @_;
158 my $numbercolumn = $self->table . 'number';
159 my $with_number = [ grep { $_->{object}->$numbercolumn ne '####' } @{ $self->controller->data } ];
160 my $without_number = [ grep { $_->{object}->$numbercolumn eq '####' } @{ $self->controller->data } ];
162 foreach my $entry (@{$with_number}, @{$without_number}) {
163 my $object = $entry->{object};
165 my $number = SL::TransNumber->new(type => $self->table(),
166 number => $object->$numbercolumn(),
167 business_id => $object->business_id(),
170 if ( $object->$numbercolumn eq '####' || !$number->is_unique() ) {
171 $object->$numbercolumn($number->create_unique());
175 $self->SUPER::save_objects(data => $with_number);
176 $self->SUPER::save_objects(data => $without_number);
189 account_number => 15,
202 my $profile = $self->SUPER::init_profile;
203 delete @{$profile}{qw(business datevexport language payment salesman salesman_id taxincluded terms)};
208 sub setup_displayable_columns {
211 $self->SUPER::setup_displayable_columns;
212 $self->add_cvar_columns_to_displayable_columns;
214 $self->add_displayable_columns({ name => 'account_number', description => $::locale->text('Account Number') },
215 { name => 'bank', description => $::locale->text('Bank') },
216 { name => 'bank_code', description => $::locale->text('Bank Code') },
217 { name => 'bcc', description => $::locale->text('Bcc') },
218 { name => 'bic', description => $::locale->text('BIC') },
219 { name => 'business_id', description => $::locale->text('Business type (database ID)') },
220 { name => 'business', description => $::locale->text('Business type (name)') },
221 { name => 'c_vendor_id', description => $::locale->text('our vendor number at customer') },
222 { name => 'cc', description => $::locale->text('Cc') },
223 { name => 'city', description => $::locale->text('City') },
224 { name => 'contact', description => $::locale->text('Contact') },
225 { name => 'country', description => $::locale->text('Country') },
226 { name => 'creditlimit', description => $::locale->text('Credit Limit') },
227 { name => 'customernumber', description => $::locale->text('Customer Number') },
228 { name => 'department_1', description => $::locale->text('Department 1') },
229 { name => 'department_2', description => $::locale->text('Department 2') },
230 { name => 'direct_debit', description => $::locale->text('direct debit') },
231 { name => 'discount', description => $::locale->text('Discount') },
232 { name => 'email', description => $::locale->text('E-mail') },
233 { name => 'fax', description => $::locale->text('Fax') },
234 { name => 'greeting', description => $::locale->text('Greeting') },
235 { name => 'homepage', description => $::locale->text('Homepage') },
236 { name => 'iban', description => $::locale->text('IBAN') },
237 { name => 'klass', description => $::locale->text('Preisklasse') },
238 { name => 'language_id', description => $::locale->text('Language (database ID)') },
239 { name => 'language', description => $::locale->text('Language (name)') },
240 { name => 'name', description => $::locale->text('Name') },
241 { name => 'notes', description => $::locale->text('Notes') },
242 { name => 'obsolete', description => $::locale->text('Obsolete') },
243 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
244 { name => 'payment', description => $::locale->text('Payment terms (name)') },
245 { name => 'phone', description => $::locale->text('Phone') },
246 { name => 'street', description => $::locale->text('Street') },
247 { name => 'taxnumber', description => $::locale->text('Tax Number / SSN') },
248 { name => 'taxzone_id', description => $::locale->text('Steuersatz') },
249 { name => 'user_password', description => $::locale->text('Password') },
250 { name => 'username', description => $::locale->text('Username') },
251 { name => 'ustid', description => $::locale->text('sales tax identification number') },
252 { name => 'zipcode', description => $::locale->text('Zipcode') },
257 # salesman_id -- Kunden mit Typ 'Verkäufer', falls Vertreter-Modus an ist, ansonsten Employees