Revision 842d6c44
Von Bernd Bleßmann vor etwa 6 Jahren hinzugefügt
SL/DB/Order.pm | ||
---|---|---|
277 | 277 |
my $order = $class->new(%args); |
278 | 278 |
$order->assign_attributes(%{ $params{attributes} }) if $params{attributes}; |
279 | 279 |
my $items = delete($params{items}) || $source->items_sorted; |
280 |
|
|
280 | 281 |
my %item_parents; |
281 | 282 |
|
282 | 283 |
my @items = map { |
... | ... | |
316 | 317 |
return $order; |
317 | 318 |
} |
318 | 319 |
|
320 |
sub new_from_multi { |
|
321 |
my ($class, $sources, %params) = @_; |
|
322 |
|
|
323 |
croak("Unsupported object type in sources") if any { ref($_) !~ m{SL::DB::Order} } @$sources; |
|
324 |
croak("Cannot create order for purchase records") if any { !$_->is_sales } @$sources; |
|
325 |
croak("Cannot create order from source records of different customers") if any { $_->customer_id != $sources->[0]->customer_id } @$sources; |
|
326 |
|
|
327 |
# bb: todo: check shipto: is it enough to check the ids or do we have to compare the entries? |
|
328 |
if (delete $params{check_same_shipto}) { |
|
329 |
die "check same shipto address is not implemented yet"; |
|
330 |
die "Source records do not have the same shipto" if 1; |
|
331 |
} |
|
332 |
|
|
333 |
# sort sources |
|
334 |
if (defined $params{sort_sources_by}) { |
|
335 |
my $sort_by = delete $params{sort_sources_by}; |
|
336 |
if ($sources->[0]->can($sort_by)) { |
|
337 |
$sources = [ sort { $a->$sort_by cmp $b->$sort_by } @$sources ]; |
|
338 |
} else { |
|
339 |
die "Cannot sort source records by $sort_by"; |
|
340 |
} |
|
341 |
} |
|
342 |
|
|
343 |
# set this entries to undef that yield different information |
|
344 |
my %attributes; |
|
345 |
foreach my $attr (qw(ordnumber transdate reqdate taxincluded shippingpoint |
|
346 |
shipvia notes closed delivered reqdate quonumber |
|
347 |
cusordnumber proforma transaction_description |
|
348 |
order_probability expected_billing_date)) { |
|
349 |
$attributes{$attr} = undef if any { ($sources->[0]->$attr//'') ne ($_->$attr//'') } @$sources; |
|
350 |
} |
|
351 |
foreach my $attr (qw(cp_id currency_id employee_id salesman_id department_id |
|
352 |
delivery_customer_id delivery_vendor_id shipto_id |
|
353 |
globalproject_id)) { |
|
354 |
$attributes{$attr} = undef if any { ($sources->[0]->$attr||0) != ($_->$attr||0) } @$sources; |
|
355 |
} |
|
356 |
|
|
357 |
# set this entries from customer that yield different information |
|
358 |
foreach my $attr (qw(language_id taxzone_id payment_id delivery_term_id)) { |
|
359 |
$attributes{$attr} = $sources->[0]->customervendor->$attr if any { ($sources->[0]->$attr||0) != ($_->$attr||0) } @$sources; |
|
360 |
} |
|
361 |
$attributes{intnotes} = $sources->[0]->customervendor->notes if any { ($sources->[0]->intnotes//'') ne ($_->intnotes//'') } @$sources; |
|
362 |
|
|
363 |
# no periodic invoice config for new order |
|
364 |
$attributes{periodic_invoices_config} = undef; |
|
365 |
|
|
366 |
# copy global ordnumber, transdate, cusordnumber into item scope |
|
367 |
# unless already present there |
|
368 |
foreach my $attr (qw(ordnumber transdate cusordnumber)) { |
|
369 |
foreach my $src (@$sources) { |
|
370 |
foreach my $item (@{ $src->items_sorted }) { |
|
371 |
$item->$attr($src->$attr) if !$item->$attr; |
|
372 |
} |
|
373 |
} |
|
374 |
} |
|
375 |
|
|
376 |
# collect items |
|
377 |
my @items; |
|
378 |
push @items, @{$_->items_sorted} for @$sources; |
|
379 |
# make order from first source and all items |
|
380 |
my $order = $class->new_from($sources->[0], |
|
381 |
destination_type => 'sales_order', |
|
382 |
attributes => \%attributes, |
|
383 |
items => \@items, |
|
384 |
%params); |
|
385 |
|
|
386 |
return $order; |
|
387 |
} |
|
388 |
|
|
319 | 389 |
sub number { |
320 | 390 |
my $self = shift; |
321 | 391 |
|
... | ... | |
468 | 538 |
|
469 | 539 |
=back |
470 | 540 |
|
541 |
=head2 C<new_from_multi $sources, %params> |
|
542 |
|
|
543 |
Creates a new C<SL::DB::Order> instance from multiple sources and copies as |
|
544 |
much information from C<$sources> as possible. |
|
545 |
At the moment only sales orders can be combined and they must be of the same |
|
546 |
customer. |
|
547 |
|
|
548 |
The new order is created from the first one using C<new_from> and the positions |
|
549 |
of all orders are added to the new order. The orders can be sorted with the |
|
550 |
parameter C<sort_sources_by>. |
|
551 |
|
|
552 |
The orders attributes are kept if they contain the same information for all |
|
553 |
source orders an will be set to empty if they contain different information. |
|
554 |
|
|
555 |
Returns the new order instance. The object returned is not |
|
556 |
saved. |
|
557 |
|
|
558 |
C<params> other then C<sort_sources_by> are passed to C<new_from>. |
|
559 |
|
|
471 | 560 |
=head1 BUGS |
472 | 561 |
|
473 | 562 |
Nothing here yet. |
Auch abrufbar als: Unified diff
SL::DB::Order: new_from_multi
Neue Aufträge aus mehreren Belegen (im Moment nur Aufträge) erzeugen.