Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 81379539

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 81379539e7f75fc6f886057176f8a51ebb8f9cf4
  • Vorgänger 07ad0fee
  • Nachfolger 8d87ea57

Artikelimport bisher

Unterschiede anzeigen:

SL/Controller/CsvImport.pm
8 8
use SL::SessionFile;
9 9
use SL::Controller::CsvImport::Contact;
10 10
use SL::Controller::CsvImport::CustomerVendor;
11
use SL::Controller::CsvImport::Part;
11 12
use SL::Controller::CsvImport::Shipto;
12 13

  
13 14
use List::MoreUtils qw(none);
......
143 144
    return $self->action_new;
144 145
  }
145 146

  
146
  my $worker = $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(controller => $self, file => $file)
147
             : $self->{type} eq 'contacts'          ? SL::Controller::CsvImport::Contact->new(       controller => $self, file => $file)
148
             : $self->{type} eq 'addresses'         ? SL::Controller::CsvImport::Shipto->new(        controller => $self, file => $file)
149
             :                                        die "Program logic error";
150

  
147
  my $worker = $self->create_worker($file);
151 148
  $worker->run;
152 149
  $worker->save_objects if !$params{test};
153 150

  
......
194 191
    push @settings, { key => "${type}_char", value => $char };
195 192
  }
196 193

  
194
  if ($self->type eq 'parts') {
195
    $::form->{settings}->{sellprice_adjustment} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{sellprice_adjustment});
196
  }
197

  
197 198
  delete $::form->{profile}->{id};
198 199
  $self->profile($existing_profile || SL::DB::CsvImportProfile->new);
199 200
  $self->profile->assign_attributes(%{ $::form->{profile} });
......
222 223
  return "csv-import-" . $self->type . ".csv";
223 224
}
224 225

  
226
sub create_worker {
227
  my ($self, $file) = @_;
228

  
229
  return $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(controller => $self, file => $file)
230
       : $self->{type} eq 'contacts'          ? SL::Controller::CsvImport::Contact->new(       controller => $self, file => $file)
231
       : $self->{type} eq 'addresses'         ? SL::Controller::CsvImport::Shipto->new(        controller => $self, file => $file)
232
       : $self->{type} eq 'parts'             ? SL::Controller::CsvImport::Part->new(          controller => $self, file => $file)
233
       :                                        die "Program logic error";
234
}
235

  
225 236
1;
SL/Controller/CsvImport/Base.pm
33 33

  
34 34
  my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
35 35
  $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
36
  $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
36 37
  $self->controller->headers($headers);
37 38

  
38 39
  # my @data;
......
46 47
  $self->fix_field_lenghts;
47 48
}
48 49

  
50
sub add_columns {
51
  my ($self, @columns) = @_;
52

  
53
  my $h = $self->controller->headers;
54

  
55
  foreach my $column (grep { !$h->{used}->{$_} } @columns) {
56
    $h->{used}->{$column} = 1;
57
    push @{ $h->{methods} }, $column;
58
    push @{ $h->{headers} }, $column;
59
  }
60
}
61

  
49 62
sub init_profile {
50 63
  my ($self) = @_;
51 64

  
SL/Controller/CsvImport/Part.pm
1
package SL::Controller::CsvImport::Part;
2

  
3
use strict;
4

  
5
use SL::Helper::Csv;
6

  
7
use SL::DB::Buchungsgruppe;
8
use SL::DB::PartsGroup;
9
use SL::DB::PaymentTerm;
10
use SL::DB::PriceFactor;
11
use SL::DB::Unit;
12

  
13
use parent qw(SL::Controller::CsvImport::Base);
14

  
15
use Rose::Object::MakeMethods::Generic
16
(
17
 scalar                  => [ qw(table) ],
18
 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by payment_terms_by packing_types_by partsgroups_by) ],
19
);
20

  
21
sub init_class {
22
  my ($self) = @_;
23
  $self->class('SL::DB::Part');
24
}
25

  
26
sub init_bg_by {
27
  my ($self) = @_;
28

  
29
  my $all_bg = SL::DB::Manager::Buchungsgruppe->get_all;
30
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) };
31
}
32

  
33
sub init_price_factors_by {
34
  my ($self) = @_;
35

  
36
  my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
37
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
38
}
39

  
40
sub init_payment_terms_by {
41
  my ($self) = @_;
42

  
43
  my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
44
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
45
}
46

  
47
sub init_packing_types_by {
48
  my ($self) = @_;
49

  
50
  my $all_packing_types = SL::DB::Manager::PackingType->get_all;
51
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_packing_types } } ) } qw(id description) };
52
}
53

  
54
sub init_partsgroups_by {
55
  my ($self) = @_;
56

  
57
  my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
58
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
59
}
60

  
61
sub init_units_by {
62
  my ($self) = @_;
63

  
64
  my $all_units = SL::DB::Manager::Unit->get_all;
65
  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
66
}
67

  
68
sub init_parts_by {
69
  my ($self) = @_;
70

  
71
  my $parts_by = { id         => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
72
                   partnumber => { part    => { },
73
                                   service => { } } };
74

  
75
  foreach my $part (@{ $self->existing_objects }) {
76
    next if $part->assembly;
77
    $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
78
  }
79

  
80
  return $parts_by;
81
}
82

  
83
sub init_settings {
84
  my ($self) = @_;
85

  
86
  return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
87
                                                                    sellprice_places sellprice_adjustment sellprice_adjustment_type
88
                                                                    shoparticle_if_missing parts_type) };
89
}
90

  
91
sub check_objects {
92
  my ($self) = @_;
93

  
94
  return unless @{ $self->controller->data };
95

  
96
  $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
97
  $self->add_columns(qw(buchungsgruppen_id unit));
98
  $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{$_} } qw (price_factor payment packing_type partsgroup));
99

  
100
  foreach my $entry (@{ $self->controller->data }) {
101
    my $object   = $entry->{object};
102
    my $raw_data = $entry->{raw_data};
103

  
104
    next unless $self->check_buchungsgruppe($entry);
105
    next unless $self->check_type($entry);
106
    next unless $self->check_unit($entry);
107
    next unless $self->check_price_factor($entry);
108
    next unless $self->check_payment($entry);
109
    next unless $self->check_packing_type($entry);
110
    next unless $self->check_partsgroup($entry);
111
    $self->check_existing($entry);
112
    $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
113
    $self->handle_shoparticle($entry);
114
    $self->set_various_fields($entry);
115
  }
116
}
117

  
118
sub check_duplicates {
119
  my ($self, %params) = @_;
120

  
121
  die "IMPLEMENT ME";
122

  
123
  my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
124
  my $name_maker = sub { return $normalizer->($_[0]->shiptoname) . '--' . $normalizer->($_[0]->shiptostreet) };
125

  
126
  my %by_id_and_name;
127
  if ('check_db' eq $self->controller->profile->get('duplicates')) {
128
    foreach my $type (qw(customers vendors)) {
129
      foreach my $vc (@{ $self->all_vc->{$type} }) {
130
        $by_id_and_name{ $vc->id } = { map { ( $name_maker->($_) => 'db' ) } @{ $vc->shipto } };
131
      }
132
    }
133
  }
134

  
135
  foreach my $entry (@{ $self->controller->data }) {
136
    next if @{ $entry->{errors} };
137

  
138
    my $name = $name_maker->($entry->{object});
139

  
140
    $by_id_and_name{ $entry->{vc}->id } ||= { };
141
    if (!$by_id_and_name{ $entry->{vc}->id }->{ $name }) {
142
      $by_id_and_name{ $entry->{vc}->id }->{ $name } = 'csv';
143

  
144
    } else {
145
      push @{ $entry->{errors} }, $by_id_and_name{ $entry->{vc}->id }->{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
146
    }
147
  }
148
}
149

  
150
sub check_buchungsgruppe {
151
  my ($self, $entry) = @_;
152

  
153
  my $object = $entry->{object};
154

  
155
  # Check Buchungsgruppe
156

  
157
  # Store and verify default ID.
158
  my $default_id = $self->settings->{default_buchungsgruppe};
159
  $default_id    = undef unless $self->bg_by->{id}->{ $default_id };
160

  
161
  # 1. Use default ID if enforced.
162
  $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
163

  
164
  # 2. Use supplied ID if valid
165
  $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
166

  
167
  # 3. Look up name if supplied.
168
  if (!$object->buchungsgruppen_id) {
169
    my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
170
    $object->buchungsgruppen_id($bg->id) if $bg;
171
  }
172

  
173
  # 4. Use default ID if not valid.
174
  $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
175

  
176
  return 1 if $object->buchungsgruppen_id;
177

  
178
  push @{ $entry->{errors} }, $::locale->text('Error: Buchungsgruppe missing or invalid');
179
  return 0;
180
}
181

  
182
sub check_existing {
183
  my ($self, $entry) = @_;
184

  
185
  my $object = $entry->{object};
186

  
187
  my $entry->{part} = $self->parts_by->{partnumber}->{ $object->type }->{ $object->partnumber };
188

  
189
  if ($self->settings->{article_number_policy} eq 'update_prices') {
190
    if ($entry->{part}) {
191
      map { $object->$_( $entry->{part}->$_ ) } qw(sellprice listprice lastcost);
192
      $entry->{priceupdate} = 1;
193
    }
194

  
195
  } else {
196
    $object->partnumber('####') if $entry->{part};
197
  }
198
}
199

  
200
sub handle_prices {
201
  my ($self, $entry) = @_;
202

  
203
  foreach my $column (qw(sellprice listprice lastcost)) {
204
    next unless $self->controller->headers->{used}->{ $column };
205

  
206
    my $adjustment = $self->settings->{sellprice_adjustment};
207
    my $value      = $entry->{object}->$column;
208

  
209
    $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
210
    $entry->{object}->$column($::form->round_amount($self->settings->{sellprice_places}, $value));
211
  }
212
}
213

  
214
sub handle_shoparticle {
215
  my ($self, $entry) = @_;
216

  
217
  $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
218
}
219

  
220
sub check_type {
221
  my ($self, $entry) = @_;
222

  
223
  my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
224
  die "Program logic error" if !$bg;
225

  
226
  my $type = $self->settings->{parts_type};
227
  if ($type eq 'mixed') {
228
    $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
229
          : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
230
          :                                        undef;
231
  }
232

  
233
  $entry->{object}->income_accno_id(  $bg->income_accno_id_0 );
234
  $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
235

  
236
  if ($type eq 'part') {
237
    $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
238

  
239
  } elsif ($type ne 'service') {
240
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
241
    return 0;
242
  }
243

  
244
  return 1;
245
}
246

  
247
sub check_price_factor {
248
  my ($self, $entry) = @_;
249

  
250
  my $object = $entry->{object};
251

  
252
  # Check whether or not price factor ID is valid.
253
  if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
254
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
255
    return 0;
256
  }
257

  
258
  # Map name to ID if given.
259
  if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
260
    my $id = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
261

  
262
    if (!$id) {
263
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
264
      return 0;
265
    }
266

  
267
    $object->price_factor_id($id);
268
  }
269

  
270
  return 1;
271
}
272

  
273
sub check_payment {
274
  my ($self, $entry) = @_;
275

  
276
  my $object = $entry->{object};
277

  
278
  # Check whether or not payment ID is valid.
279
  if ($object->payment_id && !$self->payment_terms_by->{id}->{ $object->payment_id }) {
280
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
281
    return 0;
282
  }
283

  
284
  # Map name to ID if given.
285
  if (!$object->payment_id && $entry->{raw_data}->{payment}) {
286
    my $id = $self->payment_terms_by->{description}->{ $entry->{raw_data}->{payment} };
287

  
288
    if (!$id) {
289
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
290
      return 0;
291
    }
292

  
293
    $object->payment_id($id);
294
  }
295

  
296
  return 1;
297
}
298

  
299
sub check_packing_type {
300
  my ($self, $entry) = @_;
301

  
302
  my $object = $entry->{object};
303

  
304
  # Check whether or not packing type ID is valid.
305
  if ($object->packing_type_id && !$self->packing_types_by->{id}->{ $object->packing_type_id }) {
306
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
307
    return 0;
308
  }
309

  
310
  # Map name to ID if given.
311
  if (!$object->packing_type_id && $entry->{raw_data}->{packing_type}) {
312
    my $id = $self->packing_type_by->{description}->{ $entry->{raw_data}->{packing_type} };
313

  
314
    if (!$id) {
315
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
316
      return 0;
317
    }
318

  
319
    $object->packing_type_id($id);
320
  }
321

  
322
  return 1;
323
}
324

  
325
sub check_partsgroup {
326
  my ($self, $entry) = @_;
327

  
328
  my $object = $entry->{object};
329

  
330
  # Check whether or not part group ID is valid.
331
  if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
332
    push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
333
    return 0;
334
  }
335

  
336
  # Map name to ID if given.
337
  if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
338
    my $id = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
339

  
340
    if (!$id) {
341
      push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
342
      return 0;
343
    }
344

  
345
    $object->partsgroup_id($id);
346
  }
347

  
348
  return 1;
349
}
350

  
351
sub check_unit {
352
  my ($self, $entry) = @_;
353

  
354
  my $object = $entry->{object};
355

  
356
  # Check whether or unit is valid.
357
  if (!$self->units_by->{name}->{ $object->unit }) {
358
    push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
359
    return 0;
360
  }
361

  
362
  return 1;
363
}
364

  
365
sub set_various_fields {
366
  my ($self, $entry) = @_;
367

  
368
  $entry->{object}->priceupdate(DateTime->now_local);
369
}
370

  
371
sub init_profile {
372
  my ($self) = @_;
373

  
374
  my $profile = $self->SUPER::init_profile;
375
  delete $profile->{type};
376

  
377
  $::lxdebug->dump(0, "prof", $profile);
378

  
379
  return $profile;
380
}
381

  
382

  
383
# TODO:
384
#  priceupdate aus Profil raus
385
#  CVARs ins Profil rein
386

  
387
1;

Auch abrufbar als: Unified diff