757c52c5d64dbe468bb8f574e9ba5cdba4f219ad
[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::DB::Business;
7 use SL::DB::CustomVariable;
8 use SL::DB::CustomVariableConfig;
9 use SL::DB::PaymentTerm;
10 use SL::TransNumber;
11
12 use parent qw(SL::Controller::CsvImport::Base);
13
14 use Rose::Object::MakeMethods::Generic
15 (
16  'scalar --get_set_init' => [ qw(table languages_by businesses_by) ],
17 );
18
19 sub init_table {
20   my ($self) = @_;
21   $self->table($self->controller->profile->get('table') eq 'customer' ? 'customer' : 'vendor');
22 }
23
24 sub init_class {
25   my ($self) = @_;
26   $self->class('SL::DB::' . ucfirst($self->table));
27 }
28
29 sub init_all_cvar_configs {
30   my ($self) = @_;
31
32   return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'CT' ]);
33 }
34
35 sub init_businesses_by {
36   my ($self) = @_;
37
38   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Business->get_all } } ) } qw(id description) };
39 }
40
41 sub init_languages_by {
42   my ($self) = @_;
43
44   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
45 }
46
47 sub check_objects {
48   my ($self) = @_;
49
50   $self->controller->track_progress(phase => 'building data', progress => 0);
51
52   my $numbercolumn  = $self->controller->profile->get('table') . "number";
53   my %vcs_by_number = map { ( $_->$numbercolumn => 1 ) } @{ $self->existing_objects };
54
55   my $i;
56   my $num_data = scalar @{ $self->controller->data };
57   foreach my $entry (@{ $self->controller->data }) {
58     $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
59     my $object = $entry->{object};
60
61     $self->check_name($entry);
62     $self->check_language($entry);
63     $self->check_business($entry);
64     $self->check_payment($entry);
65     $self->handle_cvars($entry);
66
67     next if @{ $entry->{errors} };
68
69     if ($vcs_by_number{ $object->$numbercolumn }) {
70       $entry->{object}->$numbercolumn('####');
71     } else {
72       $vcs_by_number{ $object->$numbercolumn } = $object;
73     }
74   } continue {
75     $i++;
76   }
77
78   $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(language business payment));
79   $self->add_cvar_raw_data_columns;
80 }
81
82 sub get_duplicate_check_fields {
83   return {
84     name => {
85       label     => $::locale->text('Customer Name'),
86       default   => 1,
87       maker     => sub {
88         my $name = shift->name;
89         $name =~ s/[\s,\.\-]//g;
90         return $name;
91       }
92     },
93   };
94 }
95
96 sub check_name {
97   my ($self, $entry) = @_;
98
99   my $name =  $entry->{object}->name;
100   $name    =~ s/^\s+//;
101   $name    =~ s/\s+$//;
102
103   return 1 if $name;
104
105   push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
106   return 0;
107 }
108
109 sub check_language {
110   my ($self, $entry) = @_;
111
112   my $object = $entry->{object};
113
114   # Check whether or not language ID is valid.
115   if ($object->language_id && !$self->languages_by->{id}->{ $object->language_id }) {
116     push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
117     return 0;
118   }
119
120   # Map name to ID if given.
121   if (!$object->language_id && $entry->{raw_data}->{language}) {
122     my $language = $self->languages_by->{description}->{  $entry->{raw_data}->{language} }
123                 || $self->languages_by->{article_code}->{ $entry->{raw_data}->{language} };
124
125     if (!$language) {
126       push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
127       return 0;
128     }
129
130     $object->language_id($language->id);
131   }
132
133   return 1;
134 }
135
136 sub check_business {
137   my ($self, $entry) = @_;
138
139   my $object = $entry->{object};
140
141   # Check whether or not business ID is valid.
142   if ($object->business_id && !$self->businesss_by->{id}->{ $object->business_id }) {
143     push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
144     return 0;
145   }
146
147   # Map name to ID if given.
148   if (!$object->business_id && $entry->{raw_data}->{business}) {
149     my $business = $self->businesses_by->{description}->{ $entry->{raw_data}->{business} };
150
151     if (!$business) {
152       push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
153       return 0;
154     }
155
156     $object->business_id($business->id);
157   }
158
159   return 1;
160 }
161
162 sub save_objects {
163   my ($self, %params) = @_;
164
165   my $numbercolumn   = $self->table . 'number';
166   my $with_number    = [ grep { $_->{object}->$numbercolumn ne '####' } @{ $self->controller->data } ];
167   my $without_number = [ grep { $_->{object}->$numbercolumn eq '####' } @{ $self->controller->data } ];
168
169   foreach my $entry (@{$with_number}, @{$without_number}) {
170     my $object = $entry->{object};
171
172     my $number = SL::TransNumber->new(type        => $self->table(),
173                                       number      => $object->$numbercolumn(),
174                                       business_id => $object->business_id(),
175                                       save        => 1);
176
177     if ( $object->$numbercolumn eq '####' || !$number->is_unique() ) {
178       $object->$numbercolumn($number->create_unique());
179     }
180   }
181
182   $self->SUPER::save_objects(data => $with_number);
183   $self->SUPER::save_objects(data => $without_number);
184 }
185
186 sub field_lengths {
187   return ( name           => 75,
188            department_1   => 75,
189            department_2   => 75,
190            street         => 75,
191            zipcode        => 10,
192            city           => 75,
193            country        => 75,
194            contact        => 75,
195            fax            => 30,
196            account_number => 15,
197            bank_code      => 10,
198            language       => 5,
199            username       => 50,
200            ustid          => 14,
201            iban           => 100,
202            bic            => 100,
203          );
204 }
205
206 sub init_profile {
207   my ($self) = @_;
208
209   my $profile = $self->SUPER::init_profile;
210   delete @{$profile}{qw(business datevexport language payment salesman salesman_id taxincluded terms)};
211
212   return $profile;
213 }
214
215 sub setup_displayable_columns {
216   my ($self) = @_;
217
218   $self->SUPER::setup_displayable_columns;
219   $self->add_cvar_columns_to_displayable_columns;
220
221   $self->add_displayable_columns({ name => 'account_number',    description => $::locale->text('Account Number')                  },
222                                  { name => 'bank',              description => $::locale->text('Bank')                            },
223                                  { name => 'bank_code',         description => $::locale->text('Bank Code')                       },
224                                  { name => 'bcc',               description => $::locale->text('Bcc')                             },
225                                  { name => 'bic',               description => $::locale->text('BIC')                             },
226                                  { name => 'business_id',       description => $::locale->text('Business type (database ID)')     },
227                                  { name => 'business',          description => $::locale->text('Business type (name)')            },
228                                  { name => 'c_vendor_id',       description => $::locale->text('our vendor number at customer')   },
229                                  { name => 'cc',                description => $::locale->text('Cc')                              },
230                                  { name => 'city',              description => $::locale->text('City')                            },
231                                  { name => 'contact',           description => $::locale->text('Contact')                         },
232                                  { name => 'country',           description => $::locale->text('Country')                         },
233                                  { name => 'creditlimit',       description => $::locale->text('Credit Limit')                    },
234                                  { name => 'customernumber',    description => $::locale->text('Customer Number')                 },
235                                  { name => 'department_1',      description => $::locale->text('Department 1')                    },
236                                  { name => 'department_2',      description => $::locale->text('Department 2')                    },
237                                  { name => 'direct_debit',      description => $::locale->text('direct debit')                    },
238                                  { name => 'discount',          description => $::locale->text('Discount')                        },
239                                  { name => 'email',             description => $::locale->text('E-mail')                          },
240                                  { name => 'fax',               description => $::locale->text('Fax')                             },
241                                  { name => 'greeting',          description => $::locale->text('Greeting')                        },
242                                  { name => 'homepage',          description => $::locale->text('Homepage')                        },
243                                  { name => 'iban',              description => $::locale->text('IBAN')                            },
244                                  { name => 'klass',             description => $::locale->text('Preisklasse')                     },
245                                  { name => 'language_id',       description => $::locale->text('Language (database ID)')          },
246                                  { name => 'language',          description => $::locale->text('Language (name)')                 },
247                                  { name => 'name',              description => $::locale->text('Name')                            },
248                                  { name => 'notes',             description => $::locale->text('Notes')                           },
249                                  { name => 'obsolete',          description => $::locale->text('Obsolete')                        },
250                                  { name => 'payment_id',        description => $::locale->text('Payment terms (database ID)')     },
251                                  { name => 'payment',           description => $::locale->text('Payment terms (name)')            },
252                                  { name => 'phone',             description => $::locale->text('Phone')                           },
253                                  { name => 'street',            description => $::locale->text('Street')                          },
254                                  { name => 'taxnumber',         description => $::locale->text('Tax Number / SSN')                },
255                                  { name => 'taxzone_id',        description => $::locale->text('Steuersatz')                      },
256                                  { name => 'user_password',     description => $::locale->text('Password')                        },
257                                  { name => 'username',          description => $::locale->text('Username')                        },
258                                  { name => 'ustid',             description => $::locale->text('sales tax identification number') },
259                                  { name => 'zipcode',           description => $::locale->text('Zipcode')                         },
260                                 );
261 }
262
263 # TODO:
264 # salesman_id -- Kunden mit Typ 'Verkäufer', falls Vertreter-Modus an ist, ansonsten Employees
265
266 1;