1 package SL::BackgroundJob::ConvertTimeRecordings;
5 use parent qw(SL::BackgroundJob::Base);
7 use SL::DB::DeliveryOrder;
8 use SL::DB::TimeRecording;
10 use SL::Locale::String qw(t8);
14 $_[0]->create_standard_job('7 3 1 * *'); # every first day of month at 03:07
19 # If job does not throw an error,
20 # success in background_job_histories is 'success'.
21 # It is 'failure' otherwise.
23 # Return value goes to result in background_job_histories.
26 my ($self, $db_obj) = @_;
29 $data = $db_obj->data_as_hash if $db_obj;
31 # from/to date from data. Defaults to begining and end of last month.
34 $from_date = DateTime->from_kivitendo($data->{from_date}) if $data->{from_date};
35 $to_date = DateTime->from_kivitendo($data->{to_date}) if $data->{to_date};
36 $from_date ||= DateTime->new( day => 1, month => DateTime->today_local->month, year => DateTime->today_local->year)->subtract(months => 1);
37 $to_date ||= DateTime->last_day_of_month(month => DateTime->today_local->month, year => DateTime->today_local->year)->subtract(months => 1);
39 $to_date->add(days => 1); # to get all from the to_date, because of the time part (15.12.2020 23.59 > 15.12.2020)
42 %customer_where = ('customer.customernumber' => $data->{customernumbers}) if 'ARRAY' eq ref $data->{customernumbers};
44 my $time_recordings = SL::DB::Manager::TimeRecording->get_all(where => [end_time => { ge_lt => [ $from_date, $to_date ]},
45 or => [booked => 0, booked => undef],
47 with_objects => ['customer']);
48 my %time_recordings_by_customer_id;
49 push @{ $time_recordings_by_customer_id{$_->customer_id} }, $_ for @$time_recordings;
53 foreach my $customer_id (keys %time_recordings_by_customer_id) {
56 $do = SL::DB::DeliveryOrder->new_from_time_recordings($time_recordings_by_customer_id{$customer_id});
59 $::lxdebug->message(LXDebug->WARN(),
60 "ConvertTimeRecordings: creating delivery order failed ($@) for time recording ids " . join ', ', map { $_->id } @{$time_recordings_by_customer_id{$customer_id}});
65 if (!SL::DB->client->with_transaction(sub {
67 $_->update_attributes(booked => 1) for @{$time_recordings_by_customer_id{$customer_id}};
70 $::lxdebug->message(LXDebug->WARN(),
71 "ConvertTimeRecordings: saving delivery order failed for time recording ids " . join ', ', map { $_->id } @{$time_recordings_by_customer_id{$customer_id}});
74 push @donumbers, $do->donumber;
79 my $msg = t8('Number of deliveryorders created:');
81 $msg .= scalar @donumbers;
83 $msg .= join ', ', @donumbers;
85 $msg .= ' ' . t8('There are Warnings.') if $has_warnings;
92 # from_date: 01.12.2020
94 # customernumbers: [1,2,3]
103 SL::BackgroundJob::ConvertTimeRecordings - Convert time recording
104 entries into delivery orders
108 Get all time recording entries for the given period and customer numbers
109 and create delivery ordes out of that (using
110 C<SL::DB::DeliveryOrder-E<gt>new_from_time_recordings>).
114 Some data can be provided to configure this backgroung job.
120 The date from which on time recordings should be collected. It defaults
121 to the first day of the previous month.
123 Example (format depends on your settings):
125 from_date: 01.12.2020
129 The date till which time recordings should be collected. It defaults
130 to the last day of the previous month.
132 Example (format depends on your settings):
136 =item C<customernumbers>
138 An array with the customer numbers for which time recordings should
139 be collected. If not given, time recordings for customers are
140 collected. This is the default.
142 Example (format depends on your settings):
144 customernumbers: [c1,22332,334343]
150 Bernd Bleßmann E<lt>bernd@kivitendo-premium.deE<gt>