Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 8f374230

Von Tamino Steinert vor 10 Monaten hinzugefügt

  • ID 8f374230c3e80f08105b077c09c628aa25ef18b5
  • Vorgänger f43dbabd
  • Nachfolger 498f3ea9

kuw: CSV-Import-Script um Größenstaffeln erweitert

Unterschiede anzeigen:

scripts/import_variant_csv.pl
30 30
use SL::DB::Part;
31 31
use SL::DB::PartsGroup;
32 32
use SL::DB::VariantProperty;
33
use SL::DB::VariantPropertyValue;
33 34
use SL::DB::Warehouse;
34 35
use SL::DB::Inventory;
35 36

  
......
37 38

  
38 39
chdir($FindBin::Bin . '/..');
39 40

  
40
my ($opt_user, $opt_client, $opt_part_csv_file);
41
my ($opt_user, $opt_client, $opt_part_csv_file, $opt_groessen_staffeln_csv_file);
41 42
our (%myconfig, $form, $user, $employee, $auth, $locale);
42 43

  
43 44
$opt_client = "variant";
44 45
$opt_user   = "test_user";
45 46

  
46
# $opt_part_csv_file = "Artikel_Auswahl.csv";
47
$opt_part_csv_file = "Artikel_small.csv";
48
# $opt_part_csv_file = "Artikel_komplett.csv";
47
$opt_part_csv_file = "Artikel_komplett.csv";
48

  
49
$opt_groessen_staffeln_csv_file = "Größenstaffeln.csv";
49 50

  
50 51

  
51 52
$locale = Locale->new;
......
138 139
  file        => $opt_part_csv_file,
139 140
  encoding    => 'utf-8', # undef means utf8
140 141
  sep_char    => ';',     # default ';'
141
  quote_char  => '"',    # default '"'
142
  quote_char  => '"',     # default '"'
142 143
  escape_char => '"',     # default '"'
143 144
);
144

  
145 145
$part_csv->parse or die "Could not parse csv";
146
my $hrefs   = $part_csv->get_data;
146
my $part_hrefs = $part_csv->get_data;
147 147

  
148 148
my %parent_variants_to_variants;
149
foreach my $part_row (@$hrefs) {
149
foreach my $part_row (@$part_hrefs) {
150 150
  my %part =
151 151
    map {$parts_mapping{$_} => $part_row->{$_}}
152 152
    grep {$part_row->{$_}}
153 153
    keys %parts_mapping;
154 154

  
155 155
  if ($part{varianten_farbnummer} || $part{varianten_farbname}) {
156
    $part{varianten_farbnummer} ||= '';
157
    $part{varianten_farbname} ||= '';
156 158
    $part{varianten_farbe} = (delete $part{varianten_farbnummer}) . '-' . (delete $part{varianten_farbname});
157 159
  }
158 160

  
159 161
  push @{$parent_variants_to_variants{$part_row->{LIEFERANT}}->{$part_row->{ARTIKELNR}}}, \%part;
160 162
}
161 163

  
162
my %variant_property_groups;
163
my @parent_variants;
164
my @variants;
165

  
166
my %partsgroup_name_to_partsgroup = map {$_->partsgroup => $_} @{SL::DB::Manager::PartsGroup->get_all()};
167
my %vendor_name_to_vendor = map {$_->name => $_} @{SL::DB::Manager::Vendor->get_all()};
168
my %warehouse_description_to_warehouse = map {$_->description => $_} @{SL::DB::Manager::Warehouse->get_all()};
169

  
170
my @all_variant_properties = @{SL::DB::Manager::VariantProperty->get_all()};
164
my $groessen_staffel_csv = SL::Helper::Csv->new(
165
  file        => $opt_groessen_staffeln_csv_file,
166
  encoding    => 'utf-8', # undef means utf8
167
  sep_char    => ';',     # default ';'
168
  quote_char  => '"',     # default '"'
169
  escape_char => '"',     # default '"'
170
);
171
$groessen_staffel_csv->parse or die "Could not parse csv";
172
my $groessen_staffel_hrefs = $groessen_staffel_csv->get_data;
171 173

  
172 174
my $transfer_type = SL::DB::Manager::TransferType->find_by(
173 175
  direction => 'in',
......
175 177
) or die "Could no find transfer_type";
176 178

  
177 179
SL::DB->client->with_transaction(sub {
178
foreach my $vendor (keys %parent_variants_to_variants) {
179
  foreach my $partnumber (keys %{$parent_variants_to_variants{$vendor}}) {
180
    my $grouped_variant_values = $parent_variants_to_variants{$vendor}->{$partnumber};
181

  
182
    #get data for parent_variant
183
    my $first_part = $grouped_variant_values->[0];
184
    my $description = $first_part->{description};
185
    my $partsgroup_name = $first_part->{warengruppe};
186
    my $warehouse_description = $first_part->{warehouse_description};
187
    my $vendor_name = $first_part->{vendor_name};
188
    my $makemodel_model = $first_part->{makemodel_model};
189
    my $best_sellprice = first {$_} sort map {$_->{sellprice}} @$grouped_variant_values;
190
    my $partsgroup = $partsgroup_name_to_partsgroup{$partsgroup_name} or die
191
      die "Could not find partsgroup '$partsgroup_name' for part '$makemodel_model $description'";
192
    my $vendor = $vendor_name_to_vendor{$vendor_name} or
193
      die "Could not find vendor: '$vendor_name' for part '$makemodel_model $description'";
194
    my $warehouse = $warehouse_description_to_warehouse{$warehouse_description} or
195
      die "Could not find warehouse '$warehouse_description' for part '$makemodel_model $description'";
196
    my $parent_variant = SL::DB::Part->new_parent_variant(
197
      description => $description,
198
      sellprice   => $best_sellprice,
199
      partsgroup  => $partsgroup,
200
      warehouse   => $warehouse,
201
      bin         => $warehouse->bins->[0],
202
      part_type   => 'part',
203
      unit        => 'Stck',
204
    );
205

  
206
    # add makemodel
207
    my $makemodel = SL::DB::MakeModel->new(
208
      make             => $vendor->id,
209
      model            => $makemodel_model,
210
      part_description => $description,
211
    );
212
    $parent_variant->add_makemodels($makemodel);
213

  
214
    # get active variant_properties
215
    my %group_variant_property_vales;
216
    foreach my $variant_values (@$grouped_variant_values) {
217
      $group_variant_property_vales{$_}->{$variant_values->{$_}} = 1 for
218
        grep { $_ =~ m/^variant/ }
219
        keys %$variant_values;
220
    }
221
    foreach my $variant_property (keys %group_variant_property_vales) {
222
      $group_variant_property_vales{$variant_property} = [sort keys %{$group_variant_property_vales{$variant_property}}];
223
    }
224 180

  
225
    # find variant_properties
226
    my %property_name_to_variant_property;
227
    foreach my $property_name (keys %group_variant_property_vales) {
228
      # TODO find valid properties for partsgroup
229
      my $needed_property_values = $group_variant_property_vales{$property_name};
230
      my ($best_match) =
231
        sort {scalar @{$a->{missing}} <=> scalar @{$b->{missing}}}
232
        map {
233
          my @property_values = map {$_->value} @{$_->property_values};
234
          my @missing;
235
          foreach my $needed_property_value (@$needed_property_values) {
236
            push @missing, $needed_property_value unless scalar grep {$needed_property_value eq $_} @property_values;
237
          }
238
          {property => $_, missing => \@missing};
239
        }
240
        @all_variant_properties;
241

  
242
      if (scalar @{$best_match->{missing}}) {
243
        die "Could not find variant property with values '@{$needed_property_values}'.\n" .
244
        "Best match is ${\$best_match->{property}->name} with missing values '@{$best_match->{missing}}'";
245
      }
246
      $property_name_to_variant_property{$property_name} = $best_match->{property};
247
    }
248
    my @variant_properties = values %property_name_to_variant_property;
249
    $parent_variant->variant_properties(@variant_properties);
250

  
251
    $parent_variant->save();
252

  
253
    foreach my $variant_values (@$grouped_variant_values) {
254
      my @property_values =
255
        map {
256
          my $value = $variant_values->{$_};
257
          first {$_->value eq $value} @{$property_name_to_variant_property{$_}->property_values}}
258
        grep { $_ =~ m/^variant/ }
259
        keys %$variant_values;
181
  # create groessen staffeln
182
  foreach my $groessen_staffel_row (@$groessen_staffel_hrefs) {
183
    my $name = delete $groessen_staffel_row->{BEZEICHNUNG};
184
    my $variant_property = SL::DB::VariantProperty->new(
185
      name         => $name,
186
      unique_name  => $name,
187
      abbreviation => "gr",
188
    )->save;
189

  
190
    my $pos = 1;
191
    SL::DB::VariantPropertyValue->new(
192
      variant_property => $variant_property,
193
      value            => $_,
194
      abbreviation     => $_,
195
      sortkey => $pos++,
196
    )->save for
197
      map {$groessen_staffel_row->{$_}}
198
      sort
199
      grep {defined $groessen_staffel_row->{$_}}
200
      keys %$groessen_staffel_row;
201
  }
260 202

  
261
      my $variant = $parent_variant->create_new_variant(\@property_values);
203
  die "end";
204

  
205
  my %partsgroup_name_to_partsgroup = map {$_->partsgroup => $_} @{SL::DB::Manager::PartsGroup->get_all()};
206
  my %vendor_name_to_vendor = map {$_->name => $_} @{SL::DB::Manager::Vendor->get_all()};
207
  my %warehouse_description_to_warehouse = map {$_->description => $_} @{SL::DB::Manager::Warehouse->get_all()};
208

  
209
  my @all_variant_properties = @{SL::DB::Manager::VariantProperty->get_all()};
210
  # create parts
211
  foreach my $vendor (keys %parent_variants_to_variants) {
212
    foreach my $partnumber (keys %{$parent_variants_to_variants{$vendor}}) {
213
      my $grouped_variant_values = $parent_variants_to_variants{$vendor}->{$partnumber};
214

  
215
      #get data for parent_variant
216
      my $first_part = $grouped_variant_values->[0];
217
      my $description = $first_part->{description};
218
      my $partsgroup_name = $first_part->{warengruppe};
219
      my $warehouse_description = $first_part->{warehouse_description};
220
      my $vendor_name = $first_part->{vendor_name};
221
      my $makemodel_model = $first_part->{makemodel_model};
222
      my $best_sellprice = first {$_} sort map {$_->{sellprice}} @$grouped_variant_values;
223
      my $partsgroup = $partsgroup_name_to_partsgroup{$partsgroup_name} or die
224
        die "Could not find partsgroup '$partsgroup_name' for part '$makemodel_model $description'";
225
      my $vendor = $vendor_name_to_vendor{$vendor_name} or
226
        die "Could not find vendor: '$vendor_name' for part '$makemodel_model $description'";
227
      my $warehouse = $warehouse_description_to_warehouse{$warehouse_description} or
228
        die "Could not find warehouse '$warehouse_description' for part '$makemodel_model $description'";
229
      my $parent_variant = SL::DB::Part->new_parent_variant(
230
        description => $description,
231
        sellprice   => $best_sellprice,
232
        partsgroup  => $partsgroup,
233
        warehouse   => $warehouse,
234
        bin         => $warehouse->bins->[0],
235
        part_type   => 'part',
236
        unit        => 'Stck',
237
      );
262 238

  
263
      $variant->update_attributes(
264
        ean => $variant_values->{ean},
265
        description => $variant_values->{description},
266
        sellprice => $variant_values->{sellprice},
239
      # add makemodel
240
      my $makemodel = SL::DB::MakeModel->new(
241
        make             => $vendor->id,
242
        model            => $makemodel_model,
243
        part_description => $description,
267 244
      );
245
      $parent_variant->add_makemodels($makemodel);
246

  
247
      # get active variant_properties
248
      my %group_variant_property_vales;
249
      foreach my $variant_values (@$grouped_variant_values) {
250
        $group_variant_property_vales{$_}->{$variant_values->{$_}} = 1 for
251
          grep { $_ =~ m/^variant/ }
252
          keys %$variant_values;
253
      }
254
      foreach my $variant_property (keys %group_variant_property_vales) {
255
        $group_variant_property_vales{$variant_property} = [sort keys %{$group_variant_property_vales{$variant_property}}];
256
      }
268 257

  
269
      # set stock
270
      my ($trans_id) = selectrow_query($::form, $::form->get_standard_dbh, qq|SELECT nextval('id')|);
271
      SL::DB::Inventory->new(
272
        part         => $variant,
273
        trans_id     => $trans_id,
274
        trans_type   => $transfer_type,
275
        shippingdate => 'now()',
276
        comment      => 'initialer Bestand',
277
        warehouse    => $variant->warehouse,
278
        bin          => $variant->bin,
279
        qty          => $variant_values->{part_qty},
280
        employee     => $employee,
281
      )->save;
258
      # find variant_properties
259
      my %property_name_to_variant_property;
260
      foreach my $property_name (keys %group_variant_property_vales) {
261
        # TODO find valid properties for partsgroup
262
        my $needed_property_values = $group_variant_property_vales{$property_name};
263
        my ($best_match) =
264
          sort {scalar @{$a->{missing}} <=> scalar @{$b->{missing}}}
265
          map {
266
            my @property_values = map {$_->value} @{$_->property_values};
267
            my @missing;
268
            foreach my $needed_property_value (@$needed_property_values) {
269
              push @missing, $needed_property_value unless scalar grep {$needed_property_value eq $_} @property_values;
270
            }
271
            {property => $_, missing => \@missing};
272
          }
273
          @all_variant_properties;
274

  
275
        if (scalar @{$best_match->{missing}}) {
276
          die "Could not find variant property with values '@{$needed_property_values}'.\n" .
277
          "Best match is ${\$best_match->{property}->name} with missing values '@{$best_match->{missing}}'";
278
        }
279
        $property_name_to_variant_property{$property_name} = $best_match->{property};
280
      }
281
      my @variant_properties = values %property_name_to_variant_property;
282
      $parent_variant->variant_properties(@variant_properties);
283

  
284
      $parent_variant->save();
285

  
286
      foreach my $variant_values (@$grouped_variant_values) {
287
        my @property_values =
288
          map {
289
            my $value = $variant_values->{$_};
290
            first {$_->value eq $value} @{$property_name_to_variant_property{$_}->property_values}}
291
          grep { $_ =~ m/^variant/ }
292
          keys %$variant_values;
293

  
294
        my $variant = $parent_variant->create_new_variant(\@property_values);
295

  
296
        $variant->update_attributes(
297
          ean => $variant_values->{ean},
298
          description => $variant_values->{description},
299
          sellprice => $variant_values->{sellprice},
300
        );
301

  
302
        # set stock
303
        my ($trans_id) = selectrow_query($::form, $::form->get_standard_dbh, qq|SELECT nextval('id')|);
304
        SL::DB::Inventory->new(
305
          part         => $variant,
306
          trans_id     => $trans_id,
307
          trans_type   => $transfer_type,
308
          shippingdate => 'now()',
309
          comment      => 'initialer Bestand',
310
          warehouse    => $variant->warehouse,
311
          bin          => $variant->bin,
312
          qty          => $variant_values->{part_qty},
313
          employee     => $employee,
314
        )->save;
282 315

  
316
      }
283 317
    }
284 318
  }
285
}
286
1;
319
  1;
287 320
}) or do {
288 321
  die t8('Error while creating variants: '), SL::DB->client->error;
289 322
};

Auch abrufbar als: Unified diff