Revision 58b77960
Von Tamino Steinert vor mehr als 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; |
... | ... | |
77 | 78 |
__PACKAGE__->before_save('_before_save_create_new_project'); |
78 | 79 |
__PACKAGE__->before_save('_before_save_remove_empty_custom_shipto'); |
79 | 80 |
__PACKAGE__->before_save('_before_save_set_custom_shipto_module'); |
81 |
__PACKAGE__->before_save('_before_save_delete_from_purchase_basket'); |
|
80 | 82 |
|
81 | 83 |
# hooks |
82 | 84 |
|
... | ... | |
134 | 136 |
return 1; |
135 | 137 |
} |
136 | 138 |
|
139 |
sub _before_save_delete_from_purchase_basket { |
|
140 |
my ($self) = @_; |
|
141 |
|
|
142 |
my @basket_item_ids = grep { $_ ne ''} map { $_->{basket_item_id} } $self->orderitems; |
|
143 |
|
|
144 |
if (scalar @basket_item_ids) { |
|
145 |
SL::DB::Manager::PurchaseBasketItem->delete_all( |
|
146 |
where => [ id => \@basket_item_ids] |
|
147 |
); |
|
148 |
} |
|
149 |
|
|
150 |
return 1; |
|
151 |
} |
|
152 |
|
|
137 | 153 |
# methods |
138 | 154 |
|
139 | 155 |
sub items { goto &orderitems; } |
... | ... | |
338 | 354 |
return $cloned; |
339 | 355 |
} |
340 | 356 |
|
357 |
sub create_from_purchase_basket { |
|
358 |
my ($class, $basket_item_ids, $vendor_item_ids, $vendor_id) = @_; |
|
359 |
|
|
360 |
my ($vendor, $employee); |
|
361 |
$vendor = SL::DB::Manager::Vendor->find_by(id => $vendor_id); |
|
362 |
$employee = SL::DB::Manager::Employee->current; |
|
363 |
|
|
364 |
my @orderitem_maps = (); # part, qty, orderer_id |
|
365 |
if ($basket_item_ids && scalar @{ $basket_item_ids}) { |
|
366 |
my $basket_items = SL::DB::Manager::PurchaseBasketItem->get_all( |
|
367 |
query => [ id => $basket_item_ids ], |
|
368 |
with_objects => ['part'], |
|
369 |
); |
|
370 |
push @orderitem_maps, map {{ |
|
371 |
basket_item_id => $_->id, |
|
372 |
part => $_->part, |
|
373 |
qty => $_->qty, |
|
374 |
orderer_id => $_->orderer_id, |
|
375 |
}} @{$basket_items}; |
|
376 |
} |
|
377 |
if ($vendor_item_ids && scalar @{ $vendor_item_ids}) { |
|
378 |
my $vendor_items = SL::DB::Manager::Part->get_all( |
|
379 |
query => [ id => $vendor_item_ids ] ); |
|
380 |
push @orderitem_maps, map {{ |
|
381 |
basket_item_id => undef, |
|
382 |
part => $_, |
|
383 |
qty => $_->order_qty || 1, |
|
384 |
orderer_id => $employee->id, |
|
385 |
}} @{$vendor_items}; |
|
386 |
} |
|
387 |
|
|
388 |
my $order = $class->new( |
|
389 |
vendor_id => $vendor->id, |
|
390 |
employee_id => $employee->id, |
|
391 |
intnotes => $vendor->notes, |
|
392 |
salesman_id => $employee->id, |
|
393 |
payment_id => $vendor->payment_id, |
|
394 |
delivery_term_id => $vendor->delivery_term_id, |
|
395 |
taxzone_id => $vendor->taxzone_id, |
|
396 |
currency_id => $vendor->currency_id, |
|
397 |
transdate => DateTime->today_local |
|
398 |
); |
|
399 |
|
|
400 |
my @order_items; |
|
401 |
my $i = 0; |
|
402 |
foreach my $orderitem_map (@orderitem_maps) { |
|
403 |
$i++; |
|
404 |
my $part = $orderitem_map->{part}; |
|
405 |
my $qty = $orderitem_map->{qty}; |
|
406 |
my $orderer_id = $orderitem_map->{orderer_id}; |
|
407 |
|
|
408 |
my $order_item = SL::DB::OrderItem->new( |
|
409 |
part => $part, |
|
410 |
qty => $qty, |
|
411 |
unit => $part->unit, |
|
412 |
description => $part->description, |
|
413 |
price_factor_id => $part->price_factor_id, |
|
414 |
price_factor => |
|
415 |
$part->price_factor_id ? $part->price_factor->factor |
|
416 |
: '', |
|
417 |
orderer_id => $orderer_id, |
|
418 |
position => $i, |
|
419 |
); |
|
420 |
$order_item->{basket_item_id} = $orderitem_map->{basket_item_id}; |
|
421 |
|
|
422 |
my $price_source = SL::PriceSource->new( |
|
423 |
record_item => $order_item, record => $order); |
|
424 |
$order_item->sellprice( |
|
425 |
$price_source->best_price ? $price_source->best_price->price |
|
426 |
: 0); |
|
427 |
$order_item->active_price_source( |
|
428 |
$price_source->best_price ? $price_source->best_price->source |
|
429 |
: ''); |
|
430 |
push @order_items, $order_item; |
|
431 |
} |
|
432 |
|
|
433 |
$order->assign_attributes(orderitems => \@order_items); |
|
434 |
|
|
435 |
$order->calculate_prices_and_taxes; |
|
436 |
|
|
437 |
foreach my $item(@{ $order->orderitems }){ |
|
438 |
$item->parse_custom_variable_values; |
|
439 |
$item->{custom_variables} = \@{ $item->cvars_by_config }; |
|
440 |
} |
|
441 |
|
|
442 |
return $order; |
|
443 |
} |
|
444 |
|
|
341 | 445 |
sub new_from { |
342 | 446 |
my ($class, $source, %params) = @_; |
343 | 447 |
|
Auch abrufbar als: Unified diff
DispositionManager: Lieferschein beim Erstellen nicht automatisch speichern