Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 28faaa2a

Von Tamino Steinert vor 10 Tagen hinzugefügt

  • ID 28faaa2a0f73da818bdebda3e95253ee6d48a7d0
  • Vorgänger e945cb1d
  • Nachfolger aa7ea0f0

Varianten: Varianten Ausprägungen bearbeiten

Unterschiede anzeigen:

SL/Controller/Part.pm
246 246
  }
247 247

  
248 248
  my $new_select_tag = select_tag(
249
    "variants[].add_variant_property_value", \@options,
249
    "variants_properties[].add_variant_property_value", \@options,
250
    with_empty => 1,
250 251
    %select_tag_options
251 252
  );
252
  $self->js->replaceWith('[name^="variants[].add_variant_property_value"]', $new_select_tag);
253
  $self->js->replaceWith('[name^="variants_properties[].add_variant_property_value"]', $new_select_tag);
253 254

  
254 255
  my $new_select_tag_multible = select_tag(
255
    "add_variant_property_value_for_selected_variants", \@options,
256
    "add_variant_property_value_for_selected_variants_properties", \@options,
256 257
    %select_tag_options
257 258
  );
258
  $self->js->replaceWith("#add_variant_property_value_for_selected_variants", $new_select_tag_multible);
259
  $self->js->replaceWith("#add_variant_property_value_for_selected_variants_properties", $new_select_tag_multible);
259 260

  
260 261
  $self->js->render();
261 262
}
262 263

  
263
sub action_update_variants {
264
sub action_update_variants_values {
264 265
  my ($self) = @_;
265 266

  
266
  my %variant_id_to_values = map {$_->{id} => $_} @{$::form->{variants}};
267
  my %variant_id_to_values = map {$_->{id} => $_} @{$::form->{variants_values}};
267 268

  
268
  my $variant_property_id = $::form->{add_variant_property};
269
  SL::DB->client->with_transaction(sub {
270
    my $new_variant_property;
271
    foreach my $variant (@{$self->part->variants}) {
272
      my $variant_attributes = $variant_id_to_values{$variant->id};
273
      delete $variant_attributes->{$_} for qw(id position);
274
      $variant->update_attributes(%$variant_attributes);
275
    }
276
    1;
277
  }) or do {
278
    return $self->js->error(t8('Error while updating values of variants: #1', SL::DB->client->error))->render();
279
  };
280

  
281
  $self->redirect_to(
282
    controller => 'Part',
283
    action     => 'edit',
284
    'part.id'  => $self->part->id
285
  );
286
}
287

  
288
sub action_update_variants_properties {
289
  my ($self) = @_;
290

  
291
  my %variant_id_to_values = map {$_->{id} => $_} @{$::form->{variants_properties}};
292

  
293
  my $variant_property_id = delete $::form->{add_variant_property};
269 294
  if ($variant_property_id) {
270 295
    foreach my $variant (@{$self->part->variants}) {
271
      die t8("Please select a new variant property value for all variants")
272
        unless $variant_id_to_values{$variant->id}->{"add_variant_property_value"};
296
      my $variant_values = $variant_id_to_values{$variant->id};
297
      $variant_values->{"variant_property_$variant_property_id"} =
298
        delete $variant_values->{"add_variant_property_value"}
299
        or die t8("Please select a new variant property value for all variants");
273 300
    }
274 301
  }
275 302

  
276
  SL::DB->client->with_transaction(sub {
277
    my $new_variant_property;
278
    if ($variant_property_id) {
279
      $new_variant_property = SL::DB::VariantPropertyPart->new(
280
        part_id             => $self->part->id,
281
        variant_property_id => $variant_property_id,
282
      )->save;
303
  my ($one_variant_id) = keys %variant_id_to_values;
304
  my %variant_property_ids =
305
    map { $_ => 1 }
306
    grep {$_ =~ m/^variant_property_/}
307
    keys %{$variant_id_to_values{$one_variant_id}};
308
  my $variant_property_id_string = join " ", sort keys %variant_property_ids;
309

  
310
  my %variant_property_values_to_variant;
311
  foreach my $variant (@{$self->part->variants}) {
312
    my %variant_values = %{$variant_id_to_values{$variant->id}};
313

  
314
    my $current_variant_property_id_string =
315
      join " ",
316
      sort
317
      grep {$_ =~ m/^variant_property_/}
318
      keys %variant_values;
319

  
320
    die "property ids doesn't match"
321
      . $current_variant_property_id_string . ";" .$variant_property_id_string
322
      if $current_variant_property_id_string ne $variant_property_id_string;
323

  
324
    my $variant_property_values =
325
      join " ",
326
      sort
327
      map {$variant_values{$_}}
328
      keys %variant_property_ids;
329

  
330
    if (defined $variant_property_values_to_variant{$variant_property_values}) {
331
      my $matching_variant = $variant_property_values_to_variant{$variant_property_values};
332
      die t8("The variants '#1' and '#2' would have the same variant property values.",
333
        $variant->displayable_name, $matching_variant->displayable_name
334
      );
335
    } else {
336
      $variant_property_values_to_variant{$variant_property_values} = $variant;
283 337
    }
338
  }
339

  
340
  SL::DB->client->with_transaction(sub {
341

  
342
    my @variant_properties =
343
      map {SL::DB::Manager::VariantProperty->find_by(id => $_)}
344
      map {$_ =~ s/^variant_property_//; $_}
345
      keys %variant_property_ids;
346

  
347
    $self->part->variant_properties(\@variant_properties);
348
    $self->part->save;
349

  
284 350
    foreach my $variant (@{$self->part->variants}) {
285
      my $variant_attributes = $variant_id_to_values{$variant->id};
286
      my $variant_property_value_id = delete $variant_attributes->{add_variant_property_value};
287
      delete $variant_attributes->{$_} for qw(id position);
288
      $variant->update_attributes(%$variant_attributes);
289
      if ($new_variant_property) {
290
        SL::DB::VariantPropertyValuePart->new(
291
          part_id                   => $variant->id,
292
          variant_property_value_id => $variant_property_value_id,
293
        )->save;
294
      }
351
      my %variant_values = %{$variant_id_to_values{$variant->id}};
352

  
353
      my @variant_property_values =
354
        map {
355
          SL::DB::Manager::VariantPropertyValue->find_by(
356
            id => $variant_values{"variant_property_" . $_->id}
357
          );
358
        }
359
        @variant_properties;
360

  
361
      $variant->variant_property_values(\@variant_property_values);
362
      $variant->save;
295 363
    }
296 364
    1;
297 365
  }) or do {
298
    return $self->js->error(t8('Error while adding variant property: #1', SL::DB->client->error))->render();
366
    return $self->js->error(t8('Error while updating variant properties: #1', SL::DB->client->error))->render();
299 367
  };
300 368

  
301 369
  $self->redirect_to(
......
932 1000
  $self->js->run('kivi.Part.redisplay_items', \@to_sort)->render;
933 1001
}
934 1002

  
935
sub action_reorder_variants {
1003
sub action_reorder_variants_values {
936 1004
  my ($self) = @_;
937 1005

  
938 1006
  my $part= $self->part;
......
961 1029

  
962 1030
  my %variant_id_to_position =
963 1031
    map {$_->{id} => $_->{position}}
964
    @{$::form->{variants}};
1032
    @{$::form->{variants_values}};
965 1033

  
966 1034
  my @to_sort = map { { old_pos => $variant_id_to_position{$_->id}, order_by => $method->($_) } } @items;
967 1035
  if ($::form->{order_by} =~ /^(listprice|sellprice|lastcost|onhand|rop)$/ ||
......
979 1047
    }
980 1048
  };
981 1049

  
982
  $self->js->run('kivi.Part.redisplay_variants', \@to_sort)->render;
1050
  $self->js->run('kivi.Part.redisplay_variants_values', \@to_sort)->render;
1051
}
1052

  
1053
sub action_reorder_variants_properties {
1054
  my ($self) = @_;
1055

  
1056
  my $part= $self->part;
1057

  
1058
  my %sort_keys = (
1059
    partnumber       => sub { $_[0]->partnumber },
1060
    description      => sub { $_[0]->description },
1061
    ean              => sub { $_[0]->ean },
1062
    variant_values   => sub { $_[0]->variant_values },
1063
  );
1064
  foreach my $variant_property (@{$part->variant_properties}) {
1065
    my $key = 'variant_property_' . $variant_property->id;
1066
    $sort_keys{$key} = sub {
1067
      $_[0]->get_variant_property_value_by_unique_name($variant_property->unique_name)->sortkey;
1068
    }
1069
  }
1070

  
1071
  my $method = $sort_keys{$::form->{order_by}};
1072

  
1073
  my @items = $part->variants;
1074

  
1075
  my %variant_id_to_position =
1076
    map {$_->{id} => $_->{position}}
1077
    @{$::form->{variants_properties}};
1078

  
1079
  my @to_sort = map { { old_pos => $variant_id_to_position{$_->id}, order_by => $method->($_) } } @items;
1080
  if ($::form->{order_by} =~ /^variant_property_/) {
1081
    if ($::form->{sort_dir}) {
1082
      @to_sort = sort { $a->{order_by} <=> $b->{order_by} } @to_sort;
1083
    } else {
1084
      @to_sort = sort { $b->{order_by} <=> $a->{order_by} } @to_sort;
1085
    }
1086
  } else {
1087
    if ($::form->{sort_dir}) {
1088
      @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort;
1089
    } else {
1090
      @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort;
1091
    }
1092
  };
1093

  
1094
  $self->js->run('kivi.Part.redisplay_variants_properties', \@to_sort)->render;
983 1095
}
984 1096

  
985 1097
sub action_warehouse_changed {

Auch abrufbar als: Unified diff