Auftrags-Controller: Bemerkungen und interne Bemerkungen
[kivitendo-erp.git] / SL / Controller / CsvImport / CustomerVendor.pm
1 package SL::Controller::CsvImport::CustomerVendor;
2
3 use strict;
4
5 use SL::Helper::Csv;
6 use SL::Controller::CsvImport::Helper::Consistency;
7 use SL::DB::Business;
8 use SL::DB::CustomVariable;
9 use SL::DB::CustomVariableConfig;
10 use SL::DB::PaymentTerm;
11 use SL::TransNumber;
12
13 use parent qw(SL::Controller::CsvImport::Base);
14
15 use Rose::Object::MakeMethods::Generic
16 (
17  'scalar --get_set_init' => [ qw(table languages_by businesses_by salesmen_by) ],
18 );
19
20 sub set_profile_defaults {
21   my ($self) = @_;
22   $self->controller->profile->_set_defaults(table => 'customer');
23 };
24
25 sub init_table {
26   my ($self) = @_;
27   $self->table($self->controller->profile->get('table') eq 'customer' ? 'customer' : 'vendor');
28 }
29
30 sub init_class {
31   my ($self) = @_;
32   $self->class('SL::DB::' . ucfirst($self->table));
33 }
34
35 sub init_all_cvar_configs {
36   my ($self) = @_;
37
38   return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'CT' ]);
39 }
40
41 sub init_businesses_by {
42   my ($self) = @_;
43
44   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Business->get_all } } ) } qw(id description) };
45 }
46
47 sub init_languages_by {
48   my ($self) = @_;
49
50   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
51 }
52
53 sub init_salesmen_by {
54   my ($self) = @_;
55
56   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Employee->get_all } } ) } qw(id name) };
57 }
58
59 sub check_objects {
60   my ($self) = @_;
61
62   $self->controller->track_progress(phase => 'building data', progress => 0);
63
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};
69
70   my $i;
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};
75
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);
85
86     next if @{ $entry->{errors} };
87
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));
90
91     push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
92       if @cleaned_fields;
93
94     my $existing_vc = $vcs_by_number{ $object->$numbercolumn };
95     if (!$existing_vc) {
96       $vcs_by_number{ $object->$numbercolumn } = $object if $object->$numbercolumn;
97
98     } elsif ($update_policy eq 'skip') {
99       push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database'));
100
101     } elsif ($update_policy eq 'update_existing') {
102       # Update existing customer/vendor records.
103       $entry->{object_to_save} = $existing_vc;
104
105       $existing_vc->$_( $entry->{object}->$_ ) for @{ $methods }, keys %{ $self->clone_methods };
106
107       push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
108
109     } else {
110       $object->$numbercolumn('####');
111     }
112   } continue {
113     $i++;
114   }
115
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;
118 }
119
120 sub get_duplicate_check_fields {
121   return {
122     name => {
123       label     => $::locale->text('Customer Name'),
124       default   => 1,
125       maker     => sub {
126         my $name = shift->name;
127         $name =~ s/[\s,\.\-]//g;
128         return $name;
129       }
130     },
131   };
132 }
133
134 sub check_name {
135   my ($self, $entry) = @_;
136
137   my $name =  $entry->{object}->name;
138   $name    =~ s/^\s+//;
139   $name    =~ s/\s+$//;
140
141   return 1 if $name;
142
143   push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
144   return 0;
145 }
146
147 sub check_language {
148   my ($self, $entry) = @_;
149
150   my $object = $entry->{object};
151
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');
155     return 0;
156   }
157
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} };
162
163     if (!$language) {
164       push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
165       return 0;
166     }
167
168     $object->language_id($language->id);
169
170     # register language_id for method copying later
171     $self->clone_methods->{language_id} = 1;
172   }
173
174   return 1;
175 }
176
177 sub check_business {
178   my ($self, $entry) = @_;
179
180   my $object = $entry->{object};
181
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');
185     return 0;
186   }
187
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} };
191
192     if (!$business) {
193       push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
194       return 0;
195     }
196
197     $object->business_id($business->id);
198
199     # register business_id for method copying later
200     $self->clone_methods->{business_id} = 1;
201   }
202
203   return 1;
204 }
205
206 sub check_salesman {
207   my ($self, $entry) = @_;
208
209   my $object = $entry->{object};
210
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');
214     return 0;
215   }
216
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} };
220
221     if (!$salesman) {
222       push @{ $entry->{errors} }, $::locale->text('Error: Invalid salesman');
223       return 0;
224     }
225
226     $object->salesman_id($salesman->id);
227
228     # register salesman_id for method copying later
229     $self->clone_methods->{salesman_id} = 1;
230   }
231
232   return 1;
233 }
234
235 sub save_objects {
236   my ($self, %params) = @_;
237
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 } ];
241
242   foreach my $entry (@{$with_number}, @{$without_number}) {
243     my $object = $entry->{object};
244
245     my $number = SL::TransNumber->new(type        => $self->table(),
246                                       number      => $object->$numbercolumn(),
247                                       business_id => $object->business_id(),
248                                       save        => 1);
249
250     if ( $object->$numbercolumn eq '####' || !$number->is_unique() ) {
251       $object->$numbercolumn($number->create_unique());
252     }
253   }
254
255   $self->SUPER::save_objects(data => $with_number);
256   $self->SUPER::save_objects(data => $without_number);
257 }
258
259 sub init_profile {
260   my ($self) = @_;
261
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;
265
266   return $profile;
267 }
268
269 sub setup_displayable_columns {
270   my ($self) = @_;
271
272   $self->SUPER::setup_displayable_columns;
273   $self->add_cvar_columns_to_displayable_columns;
274
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')                         },
319                                 );
320
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') });
324   }
325
326 }
327
328 # TODO:
329 # salesman_id -- Kunden mit Typ 'Verkäufer', falls Vertreter-Modus an ist, ansonsten Employees
330
331 1;