Revision 06e79bf0
Von Kivitendo Admin vor etwa 8 Jahren hinzugefügt
SL/DB/Part.pm | ||
---|---|---|
68 | 68 |
return 1; |
69 | 69 |
} |
70 | 70 |
|
71 |
sub items { |
|
72 |
my ($self) = @_; |
|
73 |
|
|
74 |
if ( $self->part_type eq 'assembly' ) { |
|
75 |
return $self->assemblies; |
|
76 |
} elsif ( $self->part_type eq 'assortment' ) { |
|
77 |
return $self->assortment_items; |
|
78 |
} else { |
|
79 |
return undef; |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
sub items_checksum { |
|
84 |
my ($self) = @_; |
|
85 |
|
|
86 |
# for detecting if the items of an (orphaned) assembly or assortment have |
|
87 |
# changed when saving |
|
88 |
|
|
89 |
return join(' ', sort map { $_->part->id } @{$self->items}); |
|
90 |
}; |
|
91 |
|
|
71 | 92 |
sub validate { |
72 | 93 |
my ($self) = @_; |
73 | 94 |
|
... | ... | |
80 | 101 |
push @errors, $::locale->text('The partnumber already exists.') if SL::DB::Manager::Part->get_all_count(where => [ partnumber => $self->partnumber ]); |
81 | 102 |
}; |
82 | 103 |
|
83 |
if ($self->is_assortment && scalar @{$self->assortment_items} == 0) { |
|
104 |
if ($self->is_assortment && $self->orphaned && scalar @{$self->assortment_items} == 0) { |
|
105 |
# when assortment isn't orphaned form doesn't contain any items |
|
84 | 106 |
push @errors, $::locale->text('The assortment doesn\'t have any items.'); |
85 | 107 |
} |
86 | 108 |
|
... | ... | |
147 | 169 |
my ($self) = @_; |
148 | 170 |
die 'not an accessor' if @_ > 1; |
149 | 171 |
|
172 |
return 1 unless $self->id; |
|
173 |
|
|
150 | 174 |
my @relations = qw( |
151 | 175 |
SL::DB::InvoiceItem |
152 | 176 |
SL::DB::OrderItem |
177 |
SL::DB::DeliveryOrderItem |
|
153 | 178 |
SL::DB::Inventory |
154 | 179 |
SL::DB::Assembly |
155 | 180 |
SL::DB::AssortmentItem |
... | ... | |
265 | 290 |
my ($self) = @_; |
266 | 291 |
|
267 | 292 |
my $clone = $self->clone_and_reset; # resets id and partnumber (primary key and unique constraint) |
268 |
$clone->makemodels( map { $_->clone_and_reset } @{$self->makemodels});
|
|
269 |
$clone->translations( map { $_->clone_and_reset } @{$self->translations});
|
|
293 |
$clone->makemodels( map { $_->clone_and_reset } @{$self->makemodels} ) if @{$self->makemodels};
|
|
294 |
$clone->translations( map { $_->clone_and_reset } @{$self->translations} ) if @{$self->translations};
|
|
270 | 295 |
|
271 | 296 |
if ( $self->is_assortment ) { |
297 |
# use clone rather than reset_and_clone because the unique constraint would also remove parts_id |
|
272 | 298 |
$clone->assortment_items( map { $_->clone } @{$self->assortment_items} ); |
273 |
foreach my $ai ( @{ $clone->assortment_items } ) { |
|
274 |
$ai->assortment_id(undef); |
|
275 |
}; |
|
299 |
$_->assortment_id(undef) foreach @{ $clone->assortment_items } |
|
276 | 300 |
}; |
277 | 301 |
|
278 | 302 |
if ( $self->is_assembly ) { |
... | ... | |
292 | 316 |
return $clone; |
293 | 317 |
} |
294 | 318 |
|
295 |
sub assembly_sellprice_sum {
|
|
296 |
my ($self) = @_; |
|
319 |
sub item_diffs {
|
|
320 |
my ($self, $comparison_part) = @_;
|
|
297 | 321 |
|
298 |
return unless $self->is_assembly; |
|
299 |
sum map { $_->linetotal_sellprice } @{$self->assemblies}; |
|
300 |
}; |
|
322 |
die "item_diffs needs a part object" unless ref($comparison_part) eq 'SL::DB::Part'; |
|
323 |
die "part and comparison_part need to be of the same part_type" unless |
|
324 |
( $self->part_type eq 'assembly' or $self->part_type eq 'assortment' ) |
|
325 |
and ( $comparison_part->part_type eq 'assembly' or $comparison_part->part_type eq 'assortment' ) |
|
326 |
and $self->part_type eq $comparison_part->part_type; |
|
301 | 327 |
|
302 |
sub assembly_lastcost_sum { |
|
303 |
my ($self) = @_; |
|
328 |
# return [], [] if $self->items_checksum eq $comparison_part->items_checksum; |
|
329 |
my @self_part_ids = map { $_->parts_id } $self->items; |
|
330 |
my @comparison_part_ids = map { $_->parts_id } $comparison_part->items; |
|
304 | 331 |
|
305 |
return unless $self->is_assembly; |
|
306 |
sum map { $_->linetotal_lastcost } @{$self->assemblies}; |
|
332 |
my %orig = map{ $_ => 1 } @self_part_ids; |
|
333 |
my %comparison = map{ $_ => 1 } @comparison_part_ids; |
|
334 |
my (@additions, @removals); |
|
335 |
@additions = grep { !exists( $orig{$_} ) } @comparison_part_ids if @comparison_part_ids; |
|
336 |
@removals = grep { !exists( $comparison{$_} ) } @self_part_ids if @self_part_ids; |
|
337 |
|
|
338 |
return \@additions, \@removals; |
|
307 | 339 |
}; |
308 | 340 |
|
309 |
sub assortment_sellprice_sum { |
|
341 |
sub items_sellprice_sum { |
|
342 |
my ($self, %params) = @_; |
|
343 |
|
|
344 |
return unless $self->is_assortment or $self->is_assembly; |
|
345 |
return unless $self->items; |
|
346 |
|
|
347 |
if ($self->is_assembly) { |
|
348 |
return sum map { $_->linetotal_sellprice } @{$self->items}; |
|
349 |
} else { |
|
350 |
return sum map { $_->linetotal_sellprice(%params) } grep { $_->charge } @{$self->items}; |
|
351 |
} |
|
352 |
} |
|
353 |
|
|
354 |
sub items_lastcost_sum { |
|
310 | 355 |
my ($self) = @_; |
311 | 356 |
|
312 |
return unless $self->is_assortment; |
|
313 |
sum map { $_->linetotal_sellprice } @{$self->assortment_items}; |
|
357 |
return unless $self->is_assortment or $self->is_assembly; |
|
358 |
return unless $self->items; |
|
359 |
sum map { $_->linetotal_lastcost } @{$self->items}; |
|
314 | 360 |
}; |
315 | 361 |
|
316 | 362 |
sub assortment_lastcost_sum { |
Auch abrufbar als: Unified diff
SL::DB::Part - Zugriff auf items und Summen überarbeitet