Revision daa95513
Von Tamino Steinert vor 6 Tagen hinzugefügt
SL/DB/Part.pm | ||
---|---|---|
3 | 3 |
use strict; |
4 | 4 |
|
5 | 5 |
use Carp; |
6 |
use List::MoreUtils qw(any uniq); |
|
6 |
use List::MoreUtils qw(any uniq pairwise);
|
|
7 | 7 |
use List::Util qw(sum max); |
8 | 8 |
use Rose::DB::Object::Helpers qw(as_tree); |
9 | 9 |
|
... | ... | |
133 | 133 |
|
134 | 134 |
__PACKAGE__->before_save('_before_save_set_partnumber'); |
135 | 135 |
__PACKAGE__->before_save('_before_save_set_assembly_weight'); |
136 |
__PACKAGE__->before_save('_before_check_variant_property_values'); |
|
136 | 137 |
|
137 | 138 |
sub _before_save_set_partnumber { |
138 | 139 |
my ($self) = @_; |
... | ... | |
151 | 152 |
return 1; |
152 | 153 |
} |
153 | 154 |
|
155 |
sub _before_check_variant_property_values { |
|
156 |
my ($self) = @_; |
|
157 |
if ($self->is_variant) { |
|
158 |
my @property_value_ids = sort map {$_->id} $self->variant_property_values; |
|
159 |
my ($parent_variant) = $self->parent_variant; |
|
160 |
my $other_variants = $parent_variant->variants; |
|
161 |
foreach my $variant (@$other_variants) { |
|
162 |
next if $variant->id == $self->id; |
|
163 |
my @other_property_value_ids = sort map {$_->id} $variant->variant_property_values; |
|
164 |
if (@other_property_value_ids == @property_value_ids |
|
165 |
&& not any {$_} pairwise {$a != $b} @other_property_value_ids, @property_value_ids |
|
166 |
) { |
|
167 |
die t8("There is already a variant with the property values: "), |
|
168 |
join(' ,', map {$_->displayable_name} $self->variant_property_values); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
return 1; |
|
173 |
} |
|
174 |
|
|
154 | 175 |
sub items { |
155 | 176 |
my ($self) = @_; |
156 | 177 |
|
... | ... | |
514 | 535 |
die "Given variant_property_values dosn't match the variant_properties of parent_variant part"; |
515 | 536 |
} |
516 | 537 |
|
517 |
my $separator = '.'; # TODO: make configurable |
|
538 |
my $new_variant = $self->clone_and_reset; |
|
539 |
$new_variant->partnumber($self->_next_variant_partnumber); |
|
540 |
$new_variant->variant_type('variant'); |
|
541 |
$new_variant->add_assemblies(map {$_->clone_and_reset} $self->assemblies) if ($self->is_assembly); |
|
542 |
$new_variant->add_variant_property_values(@$variant_property_values); |
|
543 |
$new_variant->parent_variant($self); |
|
544 |
$new_variant->save; |
|
518 | 545 |
|
546 |
$self->add_variants($new_variant); |
|
547 |
return $new_variant; |
|
548 |
} |
|
549 |
|
|
550 |
sub _next_variant_partnumber { |
|
551 |
my ($self) = @_; |
|
552 |
die "only callable on parts of type parent_variant" unless $self->is_parent_variant; |
|
553 |
|
|
554 |
my $separator = '.'; # TODO: make configurable |
|
519 | 555 |
my $last_variant_number = |
520 | 556 |
max |
521 | 557 |
map { |
... | ... | |
525 | 561 |
} |
526 | 562 |
$self->variants; |
527 | 563 |
|
528 |
my $new_variant = $self->clone_and_reset; |
|
529 |
$new_variant->partnumber($self->partnumber . $separator . ($last_variant_number + 1)); |
|
530 |
$new_variant->variant_type('variant'); |
|
531 |
$new_variant->add_assemblies(map {$_->clone_and_reset} $self->assemblies) if ($self->is_assembly); |
|
532 |
$new_variant->add_variant_property_values(@$variant_property_values); |
|
533 |
|
|
534 |
$self->add_variants($new_variant); |
|
535 |
$self->save; |
|
536 |
return $new_variant; |
|
564 |
return $self->partnumber . $separator . ($last_variant_number + 1); |
|
537 | 565 |
} |
538 | 566 |
|
539 | 567 |
sub clone_and_reset_deep { |
Auch abrufbar als: Unified diff
Varianten: einfachen Artikel in Variante umwandeln