Revision 2bef4707
Von Tamino Steinert vor etwa 1 Jahr hinzugefügt
SL/DB/Order.pm | ||
---|---|---|
9 | 9 |
use List::MoreUtils qw(any); |
10 | 10 |
|
11 | 11 |
use SL::DBUtils (); |
12 |
use SL::DB::PurchaseBasketItem; |
|
12 | 13 |
use SL::DB::MetaSetup::Order; |
13 | 14 |
use SL::DB::Manager::Order; |
14 | 15 |
use SL::DB::Helper::Attr; |
... | ... | |
84 | 85 |
__PACKAGE__->before_save('_before_save_set_custom_shipto_module'); |
85 | 86 |
__PACKAGE__->after_save('_after_save_link_records'); |
86 | 87 |
__PACKAGE__->after_save('_after_save_close_reachable_intakes'); # uses linked records (order matters) |
88 |
__PACKAGE__->before_save('_before_save_delete_from_purchase_basket'); |
|
87 | 89 |
|
88 | 90 |
# hooks |
89 | 91 |
|
... | ... | |
171 | 173 |
return 1; |
172 | 174 |
} |
173 | 175 |
|
176 |
sub _before_save_delete_from_purchase_basket { |
|
177 |
my ($self) = @_; |
|
178 |
|
|
179 |
my @basket_item_ids = grep { $_ ne ''} map { $_->{basket_item_id} } $self->orderitems; |
|
180 |
|
|
181 |
if (scalar @basket_item_ids) { |
|
182 |
SL::DB::Manager::PurchaseBasketItem->delete_all( |
|
183 |
where => [ id => \@basket_item_ids] |
|
184 |
); |
|
185 |
} |
|
186 |
|
|
187 |
return 1; |
|
188 |
} |
|
189 |
|
|
174 | 190 |
# methods |
175 | 191 |
|
176 | 192 |
sub items { goto &orderitems; } |
... | ... | |
348 | 364 |
return $cloned; |
349 | 365 |
} |
350 | 366 |
|
367 |
sub create_from_purchase_basket { |
|
368 |
my ($class, $basket_item_ids, $vendor_item_ids, $vendor_id) = @_; |
|
369 |
|
|
370 |
my ($vendor, $employee); |
|
371 |
$vendor = SL::DB::Manager::Vendor->find_by(id => $vendor_id); |
|
372 |
$employee = SL::DB::Manager::Employee->current; |
|
373 |
|
|
374 |
my @orderitem_maps = (); # part, qty, orderer_id |
|
375 |
if ($basket_item_ids && scalar @{ $basket_item_ids}) { |
|
376 |
my $basket_items = SL::DB::Manager::PurchaseBasketItem->get_all( |
|
377 |
query => [ id => $basket_item_ids ], |
|
378 |
with_objects => ['part'], |
|
379 |
); |
|
380 |
push @orderitem_maps, map {{ |
|
381 |
basket_item_id => $_->id, |
|
382 |
part => $_->part, |
|
383 |
qty => $_->qty, |
|
384 |
orderer_id => $_->orderer_id, |
|
385 |
}} @{$basket_items}; |
|
386 |
} |
|
387 |
if ($vendor_item_ids && scalar @{ $vendor_item_ids}) { |
|
388 |
my $vendor_items = SL::DB::Manager::Part->get_all( |
|
389 |
query => [ id => $vendor_item_ids ] ); |
|
390 |
push @orderitem_maps, map {{ |
|
391 |
basket_item_id => undef, |
|
392 |
part => $_, |
|
393 |
qty => $_->order_qty || 1, |
|
394 |
orderer_id => $employee->id, |
|
395 |
}} @{$vendor_items}; |
|
396 |
} |
|
397 |
|
|
398 |
my $order = $class->new( |
|
399 |
vendor_id => $vendor->id, |
|
400 |
employee_id => $employee->id, |
|
401 |
intnotes => $vendor->notes, |
|
402 |
salesman_id => $employee->id, |
|
403 |
payment_id => $vendor->payment_id, |
|
404 |
delivery_term_id => $vendor->delivery_term_id, |
|
405 |
taxzone_id => $vendor->taxzone_id, |
|
406 |
currency_id => $vendor->currency_id, |
|
407 |
transdate => DateTime->today_local |
|
408 |
); |
|
409 |
|
|
410 |
my @order_items; |
|
411 |
my $i = 0; |
|
412 |
foreach my $orderitem_map (@orderitem_maps) { |
|
413 |
$i++; |
|
414 |
my $part = $orderitem_map->{part}; |
|
415 |
my $qty = $orderitem_map->{qty}; |
|
416 |
my $orderer_id = $orderitem_map->{orderer_id}; |
|
417 |
|
|
418 |
my $order_item = SL::DB::OrderItem->new( |
|
419 |
part => $part, |
|
420 |
qty => $qty, |
|
421 |
unit => $part->unit, |
|
422 |
description => $part->description, |
|
423 |
price_factor_id => $part->price_factor_id, |
|
424 |
price_factor => |
|
425 |
$part->price_factor_id ? $part->price_factor->factor |
|
426 |
: '', |
|
427 |
orderer_id => $orderer_id, |
|
428 |
position => $i, |
|
429 |
); |
|
430 |
$order_item->{basket_item_id} = $orderitem_map->{basket_item_id}; |
|
431 |
|
|
432 |
my $price_source = SL::PriceSource->new( |
|
433 |
record_item => $order_item, record => $order); |
|
434 |
$order_item->sellprice( |
|
435 |
$price_source->best_price ? $price_source->best_price->price |
|
436 |
: 0); |
|
437 |
$order_item->active_price_source( |
|
438 |
$price_source->best_price ? $price_source->best_price->source |
|
439 |
: ''); |
|
440 |
push @order_items, $order_item; |
|
441 |
} |
|
442 |
|
|
443 |
$order->assign_attributes(orderitems => \@order_items); |
|
444 |
|
|
445 |
$order->calculate_prices_and_taxes; |
|
446 |
|
|
447 |
foreach my $item(@{ $order->orderitems }){ |
|
448 |
$item->parse_custom_variable_values; |
|
449 |
$item->{custom_variables} = \@{ $item->cvars_by_config }; |
|
450 |
} |
|
451 |
|
|
452 |
return $order; |
|
453 |
} |
|
454 |
|
|
351 | 455 |
sub new_from { |
352 | 456 |
my ($class, $source, %params) = @_; |
353 | 457 |
|
Auch abrufbar als: Unified diff
DispositionManager: Lieferschein beim Erstellen nicht automatisch speichern