1 package SL::Controller::MassDeliveryOrderPrint;
5 use parent qw(SL::Controller::Base);
9 use List::MoreUtils qw(uniq);
10 use List::Util qw(first);
12 use SL::Controller::Helper::GetModels;
13 use SL::BackgroundJob::MassDeliveryOrderPrinting;
15 use SL::DB::DeliveryOrder;
19 use SL::Helper::MassPrintCreatePDF qw(:all);
20 use SL::Helper::CreatePDF qw(:all);
21 use SL::Helper::File qw(store_pdf append_general_pdf_attachments);
22 use SL::Helper::PrintOptions;
23 use SL::Helper::Flash;
24 use SL::Locale::String;
26 use SL::System::TaskServer;
28 use Rose::Object::MakeMethods::Generic
30 'scalar --get_set_init' => [ qw(delivery_order_models delivery_order_ids printers filter_summary temp_files) ],
33 __PACKAGE__->run_before('setup');
38 sub action_list_delivery_orders {
41 my $show = ($::form->{noshow}?0:1);
42 delete $::form->{noshow};
45 my $key = 'MassDeliveryOrderPrint::ids-' . $::form->{ids};
46 $self->delivery_order_ids($::auth->get_session_value($key) || []);
47 $self->delivery_order_models->add_additional_url_params(ids => $::form->{ids});
50 my %selected_ids = map { +($_ => 1) } @{ $self->delivery_order_ids };
52 my $pr = SL::DB::Manager::Printer->find_by(
53 printer_description => $::locale->text("sales_delivery_order_printer"));
55 $::form->{printer_id} = $pr->id;
57 $self->render('mass_delivery_order_print/list_delivery_orders',
58 title => $::locale->text('Print delivery orders'),
60 print_opt => $self->print_options(hide_language_id => 1),
61 selected_ids => \%selected_ids);
64 sub action_mass_mdo_download {
66 my $job = SL::DB::BackgroundJob->new(id => $::form->{job_id})->load;
68 my $sfile = SL::SessionFile->new($job->data_as_hash->{pdf_file_name}, mode => 'r');
69 die $! if !$sfile->fh;
71 my $merged_pdf = do { local $/; my $fh = $sfile->fh; <$fh> };
74 my $file_name = t8('Sales Delivery Orders') . '-' . DateTime->now_local->strftime('%Y%m%d%H%M%S') . '.pdf';
75 $file_name =~ s{[^\w\.]+}{_}g;
77 return $self->send_file(
79 type => 'application/pdf',
84 sub action_mass_mdo_status {
86 $::lxdebug->enter_sub();
88 my $job = SL::DB::BackgroundJob->new(id => $::form->{job_id})->load;
89 my $html = $self->render('mass_delivery_order_print/_print_status', { output => 0 }, job => $job);
91 $self->js->html('#mass_print_dialog', $html);
92 if ( $job->data_as_hash->{status} == SL::BackgroundJob::MassDeliveryOrderPrinting->DONE() ) {
93 foreach my $dorder_id (@{$job->data_as_hash->{record_ids}}) {
94 $self->js->prop('#multi_id_id_'.$dorder_id,'checked',0);
96 $self->js->prop('#multi_all','checked',0);
97 $self->js->run('kivi.MassDeliveryOrderPrint.massConversionFinished');
101 $self->js->run('kivi.MassDeliveryOrderPrint.massConversionFinished')
102 ->run('kivi.MassDeliveryOrderPrint.massConversionFinishProcess')
103 ->flash('error', t8('No such job #1 in the database.',$::form->{job_id}));
107 $::lxdebug->leave_sub();
110 sub action_mass_mdo_print {
112 $::lxdebug->enter_sub();
115 my @do_ids = @{ $::form->{id} || [] };
116 push @do_ids, map { $::form->{"trans_id_$_"} } grep { $::form->{"multi_id_$_"} } (1..$::form->{rowcount});
118 my @delivery_orders = map { SL::DB::DeliveryOrder->new(id => $_)->load } @do_ids;
120 if (!@delivery_orders) {
121 $self->js->flash('error', t8('No delivery orders have been selected.'));
123 my $job = SL::DB::BackgroundJob->new(
126 package_name => 'MassDeliveryOrderPrinting',
129 record_ids => [ @do_ids ],
130 printer_id => $::form->{printer_id},
131 formname => $::form->{formname},
132 format => $::form->{format},
133 media => $::form->{media},
134 bothsided => ($::form->{bothsided}?1:0),
135 copies => $::form->{copies},
136 status => SL::BackgroundJob::MassDeliveryOrderPrinting->WAITING_FOR_EXECUTION(),
140 conversion_errors => [ ],
142 session_id => $::auth->get_session_id,
144 )->update_next_run_at;
146 SL::System::TaskServer->new->wake_up;
147 my $html = $self->render('mass_delivery_order_print/_print_status', { output => 0 }, job => $job);
150 ->html('#mass_print_dialog', $html)
151 ->run('kivi.MassDeliveryOrderPrint.massConversionPopup')
152 ->run('kivi.MassDeliveryOrderPrint.massConversionStarted');
157 my $htmlstr = $errstr;
158 $htmlstr =~ s/\n/<br>/g;
159 $self->js->flash('error', t8('Document generating failed. Please check Templates an LateX !'));
160 $self->js->flash_detail('error', $htmlstr);
163 $::lxdebug->leave_sub();
166 sub action_downloadpdf {
168 $::lxdebug->enter_sub();
169 if ( $::form->{filename} ) {
170 my $content = scalar File::Slurp::read_file($::form->{filename});
171 my $file_name = $::form->get_formname_translation($::form->{formname}) .
172 '-' . DateTime->now_local->strftime('%Y%m%d%H%M%S') . '.pdf';
173 $file_name =~ s{[^\w\.]+}{_}g;
175 unlink($::form->{filename});
177 return $self->send_file(
179 type => 'application/pdf',
183 flash('error', t8('No filename exists!'));
185 $::lxdebug->leave_sub();
192 sub init_printers { SL::DB::Manager::Printer->get_all_sorted }
193 sub init_delivery_order_ids { [] }
194 sub init_temp_files { [] }
196 sub init_delivery_order_models {
198 my @delivery_order_ids = @{ $self->delivery_order_ids };
200 SL::Controller::Helper::GetModels->new(
202 model => 'DeliveryOrder',
203 (paginated => 0,) x !!@delivery_order_ids,
209 customer => t8('Customer'),
210 donumber => t8('Delivery Order Number'),
211 employee => t8('Employee'),
212 ordnumber => t8('Order Number'),
213 reqdate => t8('Delivery Date'),
214 transdate => t8('Date'),
216 with_objects => [ qw(customer employee) ],
218 '!customer_id' => undef,
219 or => [ closed => undef, closed => 0 ],
220 (id => \@delivery_order_ids) x !!@delivery_order_ids,
225 sub init_filter_summary {
227 my $filter = $::form->{filter} || { customer => {}, shipto => {}, };
230 push @filters, t8('Customer') . ' ' . $filter->{customer}->{'name:substr::ilike'} if $filter->{customer}->{'name:substr::ilike'};
231 push @filters, t8('Shipping address (name)') . ' ' . $filter->{shipto}->{'shiptoname:substr::ilike'} if $filter->{shipto}->{'shiptoname:substr::ilike'};
232 push @filters, t8('Delivery Date') . ' ' . t8('From Date') . ' ' . $filter->{'reqdate:date::ge'} if $filter->{'reqdate:date::ge'};
233 push @filters, t8('Delivery Date') . ' ' . t8('To Date') . ' ' . $filter->{'reqdate:date::le'} if $filter->{'reqdate:date::le'};
235 return join ', ', @filters;
240 $::auth->assert('sales_delivery_order_edit');
241 $::request->layout->use_javascript("${_}.js") for qw(kivi.MassDeliveryOrderPrint);
245 sub generate_documents {
246 my ($self, @delivery_orders) = @_;
249 'documents' => \@delivery_orders ,
251 'type' => $::form->{type},
252 'formname' => $::form->{formname},
256 'printer_id' => $::form->{printer_id},
259 my ($temp_fh, $outname) = File::Temp::tempfile(
260 'kivitendo-outfileXXXXXX',
262 DIR => $::lx_office_conf{paths}->{userspath},
267 my @pdf_file_names = $self->create_pdfs(%pdf_params);
268 my $fcount = scalar(@pdf_file_names);
270 copy($pdf_file_names[0],$outname);
272 if ( !$self->merge_pdfs(file_names => \@pdf_file_names, out_path => $outname, bothsided => $::form->{bothsided} )) {
273 $::lxdebug->leave_sub();
277 foreach my $dorder (@delivery_orders) {
278 $self->js->prop('#multi_id_id_'.$dorder->id,'checked',0);
280 $self->js->prop('#multi_all','checked',0);