Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 09c207eb

Von Tamino Steinert vor 6 Tagen hinzugefügt

  • ID 09c207eb7155ccaf69deda884b555f9b35d72ed0
  • Vorgänger 1c22ac7b
  • Nachfolger 75687e03

Varianten: Varianten Ausprägungen bearbeiten

Unterschiede anzeigen:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  
302 370
  $self->redirect_to(
......
939 1007
  $self->js->run('kivi.Part.redisplay_items', \@to_sort)->render;
940 1008
}
941 1009

  
942
sub action_reorder_variants {
1010
sub action_reorder_variants_values {
943 1011
  my ($self) = @_;
944 1012

  
945 1013
  my $part= $self->part;
......
968 1036

  
969 1037
  my %variant_id_to_position =
970 1038
    map {$_->{id} => $_->{position}}
971
    @{$::form->{variants}};
1039
    @{$::form->{variants_values}};
972 1040

  
973 1041
  my @to_sort = map { { old_pos => $variant_id_to_position{$_->id}, order_by => $method->($_) } } @items;
974 1042
  if ($::form->{order_by} =~ /^(listprice|sellprice|lastcost|onhand|rop)$/ ||
......
986 1054
    }
987 1055
  };
988 1056

  
989
  $self->js->run('kivi.Part.redisplay_variants', \@to_sort)->render;
1057
  $self->js->run('kivi.Part.redisplay_variants_values', \@to_sort)->render;
1058
}
1059

  
1060
sub action_reorder_variants_properties {
1061
  my ($self) = @_;
1062

  
1063
  my $part= $self->part;
1064

  
1065
  my %sort_keys = (
1066
    partnumber       => sub { $_[0]->partnumber },
1067
    description      => sub { $_[0]->description },
1068
    ean              => sub { $_[0]->ean },
1069
    variant_values   => sub { $_[0]->variant_values },
1070
  );
1071
  foreach my $variant_property (@{$part->variant_properties}) {
1072
    my $key = 'variant_property_' . $variant_property->id;
1073
    $sort_keys{$key} = sub {
1074
      $_[0]->get_variant_property_value_by_unique_name($variant_property->unique_name)->sortkey;
1075
    }
1076
  }
1077

  
1078
  my $method = $sort_keys{$::form->{order_by}};
1079

  
1080
  my @items = $part->variants;
1081

  
1082
  my %variant_id_to_position =
1083
    map {$_->{id} => $_->{position}}
1084
    @{$::form->{variants_properties}};
1085

  
1086
  my @to_sort = map { { old_pos => $variant_id_to_position{$_->id}, order_by => $method->($_) } } @items;
1087
  if ($::form->{order_by} =~ /^variant_property_/) {
1088
    if ($::form->{sort_dir}) {
1089
      @to_sort = sort { $a->{order_by} <=> $b->{order_by} } @to_sort;
1090
    } else {
1091
      @to_sort = sort { $b->{order_by} <=> $a->{order_by} } @to_sort;
1092
    }
1093
  } else {
1094
    if ($::form->{sort_dir}) {
1095
      @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort;
1096
    } else {
1097
      @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort;
1098
    }
1099
  };
1100

  
1101
  $self->js->run('kivi.Part.redisplay_variants_properties', \@to_sort)->render;
990 1102
}
991 1103

  
992 1104
sub action_warehouse_changed {

Auch abrufbar als: Unified diff