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 $self->controller->track_progress(phase => 'building data', progress => 0);
52 my $numbercolumn = $self->controller->profile->get('table') . "number";
53 my %vcs_by_number = map { ( $_->$numbercolumn => 1 ) } @{ $self->existing_objects };
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};
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);
67 next if @{ $entry->{errors} };
69 if ($vcs_by_number{ $object->$numbercolumn }) {
70 $entry->{object}->$numbercolumn('####');
72 $vcs_by_number{ $object->$numbercolumn } = $object;
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;
82 sub get_duplicate_check_fields {
85 label => $::locale->text('Customer Name'),
88 my $name = shift->name;
89 $name =~ s/[\s,\.\-]//g;
97 my ($self, $entry) = @_;
99 my $name = $entry->{object}->name;
105 push @{ $entry->{errors} }, $::locale->text('Error: Name missing');
110 my ($self, $entry) = @_;
112 my $object = $entry->{object};
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');
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} };
126 push @{ $entry->{errors} }, $::locale->text('Error: Invalid language');
130 $object->language_id($language->id);
137 my ($self, $entry) = @_;
139 my $object = $entry->{object};
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');
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} };
152 push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
156 $object->business_id($business->id);
163 my ($self, %params) = @_;
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 } ];
169 foreach my $entry (@{$with_number}, @{$without_number}) {
170 my $object = $entry->{object};
172 my $number = SL::TransNumber->new(type => $self->table(),
173 number => $object->$numbercolumn(),
174 business_id => $object->business_id(),
177 if ( $object->$numbercolumn eq '####' || !$number->is_unique() ) {
178 $object->$numbercolumn($number->create_unique());
182 $self->SUPER::save_objects(data => $with_number);
183 $self->SUPER::save_objects(data => $without_number);
196 account_number => 15,
209 my $profile = $self->SUPER::init_profile;
210 delete @{$profile}{qw(business datevexport language payment salesman salesman_id taxincluded terms)};
215 sub setup_displayable_columns {
218 $self->SUPER::setup_displayable_columns;
219 $self->add_cvar_columns_to_displayable_columns;
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') },
264 # salesman_id -- Kunden mit Typ 'Verkäufer', falls Vertreter-Modus an ist, ansonsten Employees