1 package SL::Controller::CsvImport::CustomerVendor;
6 use SL::Controller::CsvImport::Helper::Consistency;
8 use SL::DB::CustomVariable;
9 use SL::DB::CustomVariableConfig;
10 use SL::DB::PaymentTerm;
13 use parent qw(SL::Controller::CsvImport::Base);
15 use Rose::Object::MakeMethods::Generic
17 'scalar --get_set_init' => [ qw(table languages_by businesses_by salesmen_by) ],
20 sub set_profile_defaults {
22 $self->controller->profile->_set_defaults(table => 'customer');
27 $self->table($self->controller->profile->get('table') eq 'customer' ? 'customer' : 'vendor');
32 $self->class('SL::DB::' . ucfirst($self->table));
35 sub init_all_cvar_configs {
38 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'CT' ]);
41 sub init_businesses_by {
44 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Business->get_all } } ) } qw(id description) };
47 sub init_languages_by {
50 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
53 sub init_salesmen_by {
56 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Employee->get_all } } ) } qw(id name) };
62 $self->controller->track_progress(phase => 'building data', progress => 0);
64 my $vc = $self->controller->profile->get('table');
65 my $update_policy = $self->controller->profile->get('update_policy') || 'update_existing';
66 my $numbercolumn = "${vc}number";
67 my %vcs_by_number = map { ( $_->$numbercolumn => $_ ) } @{ $self->existing_objects };
68 my $methods = $self->controller->headers->{methods};
71 my $num_data = scalar @{ $self->controller->data };
72 foreach my $entry (@{ $self->controller->data }) {
73 $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
74 my $object = $entry->{object};
76 $self->check_name($entry);
77 $self->check_language($entry);
78 $self->check_business($entry);
79 $self->check_payment($entry);
80 $self->check_delivery_term($entry);
81 $self->check_taxzone($entry, take_default => 1);
82 $self->check_currency($entry, take_default => 1);
83 $self->check_salesman($entry);
84 $self->handle_cvars($entry);
86 next if @{ $entry->{errors} };
88 my @cleaned_fields = $self->clean_fields(qr{[\r\n]}, $object, qw(name department_1 department_2 street zipcode city country contact phone fax homepage email cc bcc
89 taxnumber account_number bank_code bank username greeting taxzone));
91 push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
94 my $existing_vc = $vcs_by_number{ $object->$numbercolumn };
96 $vcs_by_number{ $object->$numbercolumn } = $object if $object->$numbercolumn;
98 } elsif ($update_policy eq 'skip') {
99 push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database'));
101 } elsif ($update_policy eq 'update_existing') {
102 # Update existing customer/vendor records.
103 $entry->{object_to_save} = $existing_vc;
105 $existing_vc->$_( $entry->{object}->$_ ) for @{ $methods }, keys %{ $self->clone_methods };
107 push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
110 $object->$numbercolumn('####');
116 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(language business payment delivery_term taxzone));
117 $self->add_cvar_raw_data_columns;
120 sub get_duplicate_check_fields {
123 label => $::locale->text('Customer Name'),
126 my $name = shift->name;
127 $name =~ s/[\s,\.\-]//g;
135 my ($self, $entry) = @_;
137 my $name = $entry->{object}->name;
143 push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
148 my ($self, $entry) = @_;
150 my $object = $entry->{object};
152 # Check whether or not language ID is valid.
153 if ($object->language_id && !$self->languages_by->{id}->{ $object->language_id }) {
154 push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
158 # Map name to ID if given.
159 if (!$object->language_id && $entry->{raw_data}->{language}) {
160 my $language = $self->languages_by->{description}->{ $entry->{raw_data}->{language} }
161 || $self->languages_by->{article_code}->{ $entry->{raw_data}->{language} };
164 push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
168 $object->language_id($language->id);
170 # register language_id for method copying later
171 $self->clone_methods->{language_id} = 1;
178 my ($self, $entry) = @_;
180 my $object = $entry->{object};
182 # Check whether or not business ID is valid.
183 if ($object->business_id && !$self->businesses_by->{id}->{ $object->business_id }) {
184 push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
188 # Map name to ID if given.
189 if (!$object->business_id && $entry->{raw_data}->{business}) {
190 my $business = $self->businesses_by->{description}->{ $entry->{raw_data}->{business} };
193 push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
197 $object->business_id($business->id);
199 # register business_id for method copying later
200 $self->clone_methods->{business_id} = 1;
207 my ($self, $entry) = @_;
209 my $object = $entry->{object};
211 # Check whether or not salesman ID is valid.
212 if ($object->salesman_id && !$self->salesmen_by->{id}->{ $object->salesman_id }) {
213 push @{ $entry->{errors} }, $::locale->text('Error: Invalid salesman');
217 # Map name to ID if given.
218 if (!$object->salesman_id && $entry->{raw_data}->{salesman}) {
219 my $salesman = $self->salesmen_by->{name}->{ $entry->{raw_data}->{salesman} };
222 push @{ $entry->{errors} }, $::locale->text('Error: Invalid salesman');
226 $object->salesman_id($salesman->id);
228 # register salesman_id for method copying later
229 $self->clone_methods->{salesman_id} = 1;
236 my ($self, %params) = @_;
238 my $numbercolumn = $self->table . 'number';
239 my $with_number = [ grep { $_->{object}->$numbercolumn ne '####' } @{ $self->controller->data } ];
240 my $without_number = [ grep { $_->{object}->$numbercolumn eq '####' } @{ $self->controller->data } ];
242 foreach my $entry (@{$with_number}, @{$without_number}) {
243 my $object = $entry->{object};
245 my $number = SL::TransNumber->new(type => $self->table(),
246 number => $object->$numbercolumn(),
247 business_id => $object->business_id(),
250 if ( $object->$numbercolumn eq '####' || !$number->is_unique() ) {
251 $object->$numbercolumn($number->create_unique());
255 $self->SUPER::save_objects(data => $with_number);
256 $self->SUPER::save_objects(data => $without_number);
262 my $profile = $self->SUPER::init_profile;
263 delete @{$profile}{qw(business datevexport language payment delivery_term taxincluded terms)};
264 delete @{$profile}{qw(salesman salesman_id)} if $::instance_conf->get_vertreter;
269 sub setup_displayable_columns {
272 $self->SUPER::setup_displayable_columns;
273 $self->add_cvar_columns_to_displayable_columns;
275 $self->add_displayable_columns({ name => 'account_number', description => $::locale->text('Account Number') },
276 { name => 'bank', description => $::locale->text('Bank') },
277 { name => 'bank_code', description => $::locale->text('Bank Code') },
278 { name => 'bcc', description => $::locale->text('Bcc') },
279 { name => 'bic', description => $::locale->text('BIC') },
280 { name => 'business_id', description => $::locale->text('Business type (database ID)') },
281 { name => 'business', description => $::locale->text('Business type (name)') },
282 { name => 'c_vendor_id', description => $::locale->text('our vendor number at customer') },
283 { name => 'cc', description => $::locale->text('Cc') },
284 { name => 'city', description => $::locale->text('City') },
285 { name => 'contact', description => $::locale->text('Contact') },
286 { name => 'country', description => $::locale->text('Country') },
287 { name => 'creditlimit', description => $::locale->text('Credit Limit') },
288 { name => 'currency', description => $::locale->text('Currency') },
289 { name => 'currency_id', description => $::locale->text('Currency (database ID)') },
290 { name => 'customernumber', description => $::locale->text('Customer Number') },
291 { name => 'department_1', description => $::locale->text('Department 1') },
292 { name => 'department_2', description => $::locale->text('Department 2') },
293 { name => 'delivery_term_id', description => $::locale->text('Delivery terms (database ID)') },
294 { name => 'delivery_term', description => $::locale->text('Delivery terms (name)') },
295 { name => 'direct_debit', description => $::locale->text('direct debit') },
296 { name => 'discount', description => $::locale->text('Discount') },
297 { name => 'email', description => $::locale->text('E-mail') },
298 { name => 'fax', description => $::locale->text('Fax') },
299 { name => 'greeting', description => $::locale->text('Greeting') },
300 { name => 'homepage', description => $::locale->text('Homepage') },
301 { name => 'iban', description => $::locale->text('IBAN') },
302 { name => 'klass', description => $::locale->text('Preisklasse') },
303 { name => 'language_id', description => $::locale->text('Language (database ID)') },
304 { name => 'language', description => $::locale->text('Language (name)') },
305 { name => 'name', description => $::locale->text('Name') },
306 { name => 'notes', description => $::locale->text('Notes') },
307 { name => 'obsolete', description => $::locale->text('Obsolete') },
308 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
309 { name => 'payment', description => $::locale->text('Payment terms (name)') },
310 { name => 'phone', description => $::locale->text('Phone') },
311 { name => 'street', description => $::locale->text('Street') },
312 { name => 'taxnumber', description => $::locale->text('Tax Number / SSN') },
313 { name => 'taxzone', description => $::locale->text('Tax zone (description)') },
314 { name => 'taxzone_id', description => $::locale->text('Tax zone (database ID)') },
315 { name => 'user_password', description => $::locale->text('Password') },
316 { name => 'username', description => $::locale->text('Username') },
317 { name => 'ustid', description => $::locale->text('sales tax identification number') },
318 { name => 'zipcode', description => $::locale->text('Zipcode') },
321 if (!$::instance_conf->get_vertreter) {
322 $self->add_displayable_columns({ name => 'salesman_id', description => $::locale->text('Salesman (database ID)') });
323 $self->add_displayable_columns({ name => 'salesman', description => $::locale->text('Salesman') });
329 # salesman_id -- Kunden mit Typ 'Verkäufer', falls Vertreter-Modus an ist, ansonsten Employees