Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision daa95513

Von Tamino Steinert vor 6 Tagen hinzugefügt

  • ID daa95513162cbde748d7d29fbe5d0f8ef1cc7e8f
  • Vorgänger 1861cb74
  • Nachfolger 88ad13f9

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

  
......
295 296

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

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

  
319 322

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

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

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

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

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

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

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

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

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

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

  
343 394
  $self->redirect_to(
......
1782 1833
  Carp::confess "invalid part_type" unless $_[0] =~ /^(part|service|assembly|assortment)$/;
1783 1834
}
1784 1835

  
1836
sub _check_variant_property_values_not_taken {
1837
  my ($parent_variant, $variant_property_value_lists) = @_;
1838

  
1839
  my @double_lists;
1840
  my $variants = $parent_variant->variants;
1841
  foreach my $variant (@$variants) {
1842
    my @property_value_ids = sort map {$_->id} $variant->variant_property_values;
1843
    foreach my $test_property_values (@$variant_property_value_lists) {
1844
      my @test_property_value_ids = sort map {$_->id} @$test_property_values;
1845
      if (@test_property_value_ids == @property_value_ids
1846
        && not any {$_} pairwise {$a != $b} @test_property_value_ids, @property_value_ids
1847
      ) {
1848
        push @double_lists, join(', ', map {$_->displayable_name} @$test_property_values);
1849
      }
1850
    }
1851
  }
1852

  
1853
  if (@double_lists) {
1854
    die t8("There is already a variant with the property values: #1.", join("; ", @double_lists));
1855
  }
1856
}
1785 1857

  
1786 1858
sub normalize_text_blocks {
1787 1859
  my ($self) = @_;

Auch abrufbar als: Unified diff