Auftrags-Controller: date_tags mit richtiger id ansprechen
[kivitendo-erp.git] / SL / BackgroundJob / ShopOrderMassTransfer.pm
1 package SL::BackgroundJob::ShopOrderMassTransfer;
2
3 use strict;
4 use warnings;
5
6 use parent qw(SL::BackgroundJob::Base);
7
8 use SL::DBUtils;
9 use SL::DB::ShopOrder;
10 use SL::DB::ShopOrderItem;
11 use SL::DB::Order;
12 use SL::DB::History;
13 use SL::DB::DeliveryOrder;
14 use SL::DB::Inventory;
15 use Sort::Naturally ();
16 use SL::Locale::String qw(t8);
17
18 use constant WAITING_FOR_EXECUTION        => 0;
19 use constant CONVERTING_TO_ORDER          => 1;
20 use constant DONE                         => 2;
21
22 # Data format:
23 # my $data                  = {
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"}, ],
28 # };
29 #
30
31 sub create_order {
32   my ( $self ) = @_;
33   my $job_obj = $self->{job_obj};
34   my $db      = $job_obj->db;
35   $job_obj->set_data(CONVERTING_TO_ORDER())->save;
36
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;
40     unless($shop_order){
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);
43     }
44     my $customer = SL::DB::Manager::Customer->find_by(id => $shop_order->{kivi_customer_id});
45     unless($customer){
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);
48     }
49     my $employee = SL::DB::Manager::Employee->current;
50     my $items = SL::DB::Manager::ShopOrderItem->get_all( where => [shop_order_id => $shop_order_id], );
51
52     if ($customer->{order_lock} == 0) {
53       $shop_order->{shop_order_items} = $items;
54
55       $db->with_transaction( sub {
56         my $order = $shop_order->convert_to_sales_order(customer => $customer, employee => $employee);
57
58         if ($order->{error}){
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);
61         }else{
62           $order->save;
63           $order->calculate_prices_and_taxes;
64           my $snumbers = "ordernumber_" . $order->ordnumber;
65           SL::DB::History->new(
66                             trans_id    => $order->id,
67                             snumbers    => $snumbers,
68                             employee_id => SL::DB::Manager::Employee->current->id,
69                             addition    => 'SAVED',
70                             what_done   => 'Shopimport->Order(MassTransfer)',
71                           )->save();
72           $shop_order->transferred(1);
73           $shop_order->transfer_date(DateTime->now_local);
74           $shop_order->save;
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;
79
80           $job_obj->update_attributes(data_as_hash => $data);
81         }
82         1;
83       })or do {
84         push @{ $data->{conversion_errors} }, { id => $shop_order->id, number => $shop_order->shop_ordernumber, message => $@ };
85         $job_obj->update_attributes(data_as_hash => $data);
86       }
87     }else{
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);
90     }
91   }
92 }
93
94 sub run {
95   my ($self, $job_obj) = @_;
96
97   $self->{job_obj}         = $job_obj;
98   $self->create_order;
99
100   $job_obj->set_data(status => DONE())->save;
101
102   return 1;
103 }
104 1;