1 package SL::BackgroundJob::MassRecordCreationAndPrinting;
6 use parent qw(SL::BackgroundJob::Base);
8 use SL::DB::DeliveryOrder;
9 use SL::DB::Order; # origin order to delivery_order
15 use constant WAITING_FOR_EXECUTION => 0;
16 use constant CONVERTING_DELIVERY_ORDERS => 1;
17 use constant PRINTING_INVOICES => 2;
18 use constant DONE => 3;
21 # record_ids => [ 123, 124, 127, ],
25 # invoice_ids => [ 234, 235, ],
26 # conversion_errors => [ { id => 124, number => 'A981723', message => "Stuff went boom" }, ],
27 # print_errors => [ { id => 234, number => 'L87123123', message => "Printer is out of coffee" }, ],
28 # pdf_file_name => 'qweqwe.pdf',
34 my $job_obj = $self->{job_obj};
35 my $db = $job_obj->db;
37 $job_obj->set_data(status => CONVERTING_DELIVERY_ORDERS())->save;
39 foreach my $delivery_order_id (@{ $job_obj->data_as_hash->{record_ids} }) {
40 my $number = $delivery_order_id;
41 my $data = $job_obj->data_as_hash;
45 my $sales_delivery_order = SL::DB::DeliveryOrder->new(id => $delivery_order_id)->load;
46 $number = $sales_delivery_order->donumber;
48 if (!$db->do_transaction(sub {
49 $invoice = $sales_delivery_order->convert_to_invoice(item_filter => \&delivery_order_item_filter, queue_sort => 1) || die $db->error;
50 # $delivery_order->post_save_sanity_check; # just a hint at e8521eee (#90 od)
56 $data->{num_created}++;
57 push @{ $data->{invoice_ids} }, $invoice->id;
58 push @{ $self->{invoices} }, $invoice;
62 push @{ $data->{conversion_errors} }, { id => $delivery_order_id, number => $number, message => $@ };
65 $job_obj->update_attributes(data_as_hash => $data);
69 sub convert_invoices_to_pdf {
72 return if !@{ $self->{invoices} };
74 my $job_obj = $self->{job_obj};
75 my $db = $job_obj->db;
77 $job_obj->set_data(status => PRINTING_INVOICES())->save;
79 require SL::Controller::MassInvoiceCreatePrint;
81 my $printer_id = $job_obj->data_as_hash->{printer_id};
82 my $ctrl = SL::Controller::MassInvoiceCreatePrint->new;
85 formname => 'invoice',
87 media => $printer_id ? 'printer' : 'file',
92 foreach my $invoice (@{ $self->{invoices} }) {
93 my $data = $job_obj->data_as_hash;
97 template => $ctrl->find_template(name => 'invoice', printer_id => $printer_id),
98 variables => Form->new(''),
99 return => 'file_name',
102 $create_params{variables}->{$_} = $variables{$_} for keys %variables;
104 $invoice->flatten_to_form($create_params{variables}, format_amounts => 1);
105 $create_params{variables}->prepare_for_printing;
107 push @pdf_file_names, $ctrl->create_pdf(%create_params);
109 $data->{num_printed}++;
114 push @{ $data->{print_errors} }, { id => $invoice->id, number => $invoice->invnumber, message => $@ };
117 $job_obj->update_attributes(data_as_hash => $data);
120 if (@pdf_file_names) {
121 my $data = $job_obj->data_as_hash;
124 $self->{merged_pdf} = $ctrl->merge_pdfs(file_names => \@pdf_file_names);
125 unlink @pdf_file_names;
128 my $file_name = 'mass_invoice' . $job_obj->id . '.pdf';
129 my $sfile = SL::SessionFile->new($file_name, mode => 'w');
130 $sfile->fh->print($self->{merged_pdf});
133 $data->{pdf_file_name} = $file_name;
139 push @{ $data->{print_errors} }, { message => $@ };
142 $job_obj->update_attributes(data_as_hash => $data);
149 my $job_obj = $self->{job_obj};
150 my $data = $job_obj->data_as_hash;
151 my $printer_id = $data->{printer_id};
153 return if !$printer_id;
155 my $printer = SL::DB::Printer->new(id => $printer_id)->load;
156 my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command);
159 if (!open $out, '|-', $command) {
160 push @{ $data->{print_errors} }, { message => $::locale->text('Could not execute printer command: #1', $!) };
161 $job_obj->update_attributes(data_as_hash => $data);
166 print $out $self->{merged_pdf};
171 my ($self, $job_obj) = @_;
173 $self->{job_obj} = $job_obj;
174 $self->{invoices} = [];
176 $self->create_invoices;
177 $self->convert_invoices_to_pdf;
180 $job_obj->set_data(status => DONE())->save;
195 SL::BackgroundJob::MassRecordCreationAndPrinting
201 use SL::BackgroundJob::MassRecordCreationAndPrinting
203 my $job = SL::DB::BackgroundJob->new(
206 package_name => 'MassRecordCreationAndPrinting',
209 record_ids => [ map { $_->id } @records[0..$num - 1] ],
210 printer_id => $::form->{printer_id},
211 status => SL::BackgroundJob::MassRecordCreationAndPrinting->WAITING_FOR_EXECUTION(),
215 conversion_errors => [ ],
218 )->update_next_run_at;
219 SL::System::TaskServer->new->wake_up;
223 This background job has 4 states which are described by the four constants above.
227 =item * WAITING_FOR_EXECUTION
228 Background has been initialised and needs to be picked up by the task_server
230 =item * CONVERTING_DELIVERY_ORDERS
233 =item * PRINTING_INVOICES
234 Printing, if done via print command
237 To release the process and for the user information
245 =item C<create_invoices>
247 Converts the source objects (DeliveryOrder) to destination objects (Invoice).
248 On success objects will be saved.
250 =item C<convert_invoices_to_pdf>
252 Takes the new destination objects and merges them via print template in one pdf.
256 Sent the pdf to the printer command (if checked).
262 Currently the calculation from the gui (form) differs from the calculation via convert (PTC).
263 Furthermore mass conversion with foreign currencies could lead to problems (daily rate check).
267 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
269 Jan Büren E<lt>jan@kivitendo-premium.deE<gt>