1 package SL::BackgroundJob::ShopOrderMassTransfer;
6 use parent qw(SL::BackgroundJob::Base);
10 use SL::DB::ShopOrderItem;
13 use SL::DB::DeliveryOrder;
14 use SL::DB::Inventory;
15 use Sort::Naturally ();
16 use SL::Locale::String qw(t8);
18 use constant WAITING_FOR_EXECUTION => 0;
19 use constant CONVERTING_TO_ORDER => 1;
20 use constant DONE => 2;
24 # shop_order_record_ids => [ 603, 604, 605],
25 # num_order_created => 0,
26 # orders_ids => [1,2,3]
27 # conversion_errors => [ { id => 603 , item => 2, message => "Out of stock"}, ],
33 my $job_obj = $self->{job_obj};
34 my $db = $job_obj->db;
35 $job_obj->set_data(CONVERTING_TO_ORDER())->save;
37 my $data = $job_obj->data_as_hash;
38 foreach my $shop_order_id (@{ $data->{shop_order_record_ids} }) {
39 my $shop_order = SL::DB::ShopOrder->new(id => $shop_order_id)->load;
41 push @{ $data->{conversion_errors} }, { id => $shop_order->id, number => $shop_order->shop_ordernumber, message => t8('Shoporder not found') };
42 $job_obj->update_attributes(data_as_hash => $data);
44 my $customer = SL::DB::Manager::Customer->find_by(id => $shop_order->{kivi_customer_id});
46 push @{ $data->{conversion_errors} }, { id => $shop_order->id, number => $shop_order->shop_ordernumber, message => t8('Customer not found') };
47 $job_obj->update_attributes(data_as_hash => $data);
49 my $employee = SL::DB::Manager::Employee->current;
50 my $items = SL::DB::Manager::ShopOrderItem->get_all( where => [shop_order_id => $shop_order_id], );
52 if ($customer->{order_lock} == 0) {
53 $shop_order->{shop_order_items} = $items;
55 $db->with_transaction( sub {
56 my $order = $shop_order->convert_to_sales_order(customer => $customer, employee => $employee);
59 push @{ $data->{conversion_errors} }, { id => $shop_order->id, number => $shop_order->shop_ordernumber, message => \@{$order->{errors}} };
60 $job_obj->update_attributes(data_as_hash => $data);
63 $order->calculate_prices_and_taxes;
64 my $snumbers = "ordernumber_" . $order->ordnumber;
66 trans_id => $order->id,
67 snumbers => $snumbers,
68 employee_id => SL::DB::Manager::Employee->current->id,
70 what_done => 'Shopimport->Order(MassTransfer)',
72 $shop_order->transferred(1);
73 $shop_order->transfer_date(DateTime->now_local);
75 $shop_order->link_to_record($order);
76 $data->{num_order_created} ++;
77 push @{ $data->{orders_ids} }, $order->id;
78 push @{ $data->{shop_orders_ids} }, $shop_order->id;
80 $job_obj->update_attributes(data_as_hash => $data);
84 push @{ $data->{conversion_errors} }, { id => $shop_order->id, number => $shop_order->shop_ordernumber, message => $@ };
85 $job_obj->update_attributes(data_as_hash => $data);
88 push @{ $data->{conversion_errors} }, { id => $shop_order->id, number => $shop_order->shop_ordernumber, message => t8('Customerorderlock') };
89 $job_obj->update_attributes(data_as_hash => $data);
95 my ($self, $job_obj) = @_;
97 $self->{job_obj} = $job_obj;
100 $job_obj->set_data(status => DONE())->save;