Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 8a0d2996

Von Tamino Steinert vor 9 Monaten hinzugefügt

  • ID 8a0d29968757bfba2f0a06dfa07b650eaffc4ef7
  • Vorgänger bc8ddcc1
  • Nachfolger e4982f5b

Varianten: einfachen Artikel in Variante umwandeln

Unterschiede anzeigen:

SL/Controller/Part.pm
10 10
use File::Temp;
11 11
use List::Util qw(sum);
12 12
use List::UtilsBy qw(extract_by);
13
use List::MoreUtils qw(any pairwise);
13 14
use POSIX qw(strftime);
14 15
use Text::CSV_XS;
15 16

  
......
294 295

  
295 296
  my $part = $self->part;
296 297
  my $variant_properties = $part->variant_properties();
297
  my @needed_variant_property_ids = sort map {$_->id} @$variant_properties;
298 298

  
299
  if (@variant_property_ids != @needed_variant_property_ids) {
299
  if (any {$_} grep {
300
      !defined $::form->{variant_properties}->{$_->id}->{selected_property_value_ids}
301
    } @$variant_properties
302
    ) {
300 303
    return $self->js->error(
301 304
      t8('No property value selected for variant properties: #1.',
302 305
        join(", ",
303 306
          map {$_->displayable_name}
304
          grep {!defined $::form->{variant_properties}->{$_->id}->{selected_property_values}}
307
          grep {!defined $::form->{variant_properties}->{$_->id}->{selected_property_value_ids}}
305 308
          @$variant_properties
306 309
        )
307 310
      )
......
311 314
  my @grouped_variant_property_values = ();
312 315
  push @grouped_variant_property_values,
313 316
    SL::DB::Manager::VariantPropertyValue->get_all(
314
      where => [ id => $::form->{variant_properties}->{$_}->{selected_property_values} ]
317
      where => [ id => $::form->{variant_properties}->{$_}->{selected_property_value_ids} ]
315 318
    )
316 319
    for @variant_property_ids;
317 320

  
318 321

  
319
  my @variant_property_values_lists = ();
322
  my @variant_property_value_lists = ();
320 323
  foreach my $variant_property_values (@grouped_variant_property_values) {
321 324
    my @new_lists = ();
322 325
    foreach my $variant_property_value (@$variant_property_values) {
323
      unless (scalar @variant_property_values_lists) {
326
      unless (scalar @variant_property_value_lists) {
324 327
        push @new_lists, [$variant_property_value];
325 328
      }
326
      foreach my $variant_property_values_list (@variant_property_values_lists) {
329
      foreach my $variant_property_values_list (@variant_property_value_lists) {
327 330
        push @new_lists, [@$variant_property_values_list, $variant_property_value];
328 331
      }
329 332
    }
330
    @variant_property_values_lists = @new_lists;
333
    @variant_property_value_lists = @new_lists;
331 334
  }
332 335

  
333
  my @new_variants = ();
336
  _check_variant_property_values_not_taken($part, \@variant_property_value_lists);
334 337
  SL::DB->client->with_transaction(sub {
335
    push @new_variants, $part->create_new_variant($_)
336
      for @variant_property_values_lists;
338
    $part->create_new_variant($_) for @variant_property_value_lists;
337 339
    1;
338 340
  }) or do {
339
    die t8('Error while creating variants: '), $@;
341
    die t8('Error while creating variants: '), SL::DB->client->error;
342
  };
343

  
344
  $self->redirect_to(
345
    controller => 'Part',
346
    action     => 'edit',
347
    'part.id'  => $self->part->id
348
  );
349
}
350

  
351
sub action_convert_part_to_variant {
352
  my ($self) = @_;
353

  
354
  my $convert_part_id = $::form->{convert_part}->{id};
355
  die t8("Please select a part to convert.") unless $convert_part_id;
356

  
357
  my @variant_property_ids = sort keys %{$::form->{convert_part}->{variant_properties}};
358

  
359
  my $part = $self->part;
360
  my $variant_properties = $part->variant_properties();
361
  my @needed_variant_property_ids = sort map {$_->id} @$variant_properties;
362

  
363
  if (@variant_property_ids != @needed_variant_property_ids) {
364
    return $self->js->error(
365
      t8('No property value selected for variant properties: #1.',
366
        join(", ",
367
          map {$_->displayable_name}
368
          grep {!defined $::form->{convert_part}->{variant_properties}->{$_->id}->{selected_property_value_id}}
369
          @$variant_properties
370
        )
371
      )
372
    )->render();
373
  }
374

  
375
  my @variant_property_values = map {
376
      SL::DB::VariantPropertyValue->new(
377
        id => $::form->{convert_part}->{variant_properties}->{$_}->{selected_property_value_id}
378
      )->load()
379
    } @variant_property_ids;
380

  
381
  _check_variant_property_values_not_taken($part, [\@variant_property_values]);
382
  SL::DB->client->with_transaction(sub {
383
    my $part_to_convert = SL::DB::Part->new(id => $convert_part_id)->load;
384
    $part_to_convert->variant_type('variant');
385
    $part_to_convert->variant_property_values(\@variant_property_values);
386
    $part_to_convert->parent_variant($part);
387
    $part_to_convert->save;
388
    1;
389
  }) or do {
390
    die t8('Error while converting part to variant: '), SL::DB->Client->error;
340 391
  };
341 392

  
342 393
  $self->redirect_to(
......
1748 1799
  Carp::confess "invalid part_type" unless $_[0] =~ /^(part|service|assembly|assortment)$/;
1749 1800
}
1750 1801

  
1802
sub _check_variant_property_values_not_taken {
1803
  my ($parent_variant, $variant_property_value_lists) = @_;
1804

  
1805
  my @double_lists;
1806
  my $variants = $parent_variant->variants;
1807
  foreach my $variant (@$variants) {
1808
    my @property_value_ids = sort map {$_->id} $variant->variant_property_values;
1809
    foreach my $test_property_values (@$variant_property_value_lists) {
1810
      my @test_property_value_ids = sort map {$_->id} @$test_property_values;
1811
      if (@test_property_value_ids == @property_value_ids
1812
        && not any {$_} pairwise {$a != $b} @test_property_value_ids, @property_value_ids
1813
      ) {
1814
        push @double_lists, join(', ', map {$_->displayable_name} @$test_property_values);
1815
      }
1816
    }
1817
  }
1818

  
1819
  if (@double_lists) {
1820
    die t8("There is already a variant with the property values: #1.", join("; ", @double_lists));
1821
  }
1822
}
1751 1823

  
1752 1824
sub normalize_text_blocks {
1753 1825
  my ($self) = @_;

Auch abrufbar als: Unified diff