]> wagnertech.de Git - mfinanz.git/blob - bin/mozilla/dn.pl
restart apache2 in postinst
[mfinanz.git] / bin / mozilla / dn.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2006
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # Dunning process module
32 #
33 #======================================================================
34
35 use POSIX qw(strftime);
36
37 use List::Util qw(notall);
38 use List::MoreUtils qw(none);
39
40 use SL::IS;
41 use SL::DN;
42 use SL::DB::Department;
43 use SL::DB::Dunning;
44 use SL::DB::Manager::PaymentTerm;
45 use SL::File;
46 use SL::Helper::Flash qw(flash);
47 use SL::Locale::String qw(t8);
48 use SL::Presenter::EmailJournal;
49 use SL::Presenter::FileObject;
50 use SL::Presenter::WebdavObject;
51 use SL::ReportGenerator;
52
53 require "bin/mozilla/common.pl";
54 require "bin/mozilla/reportgenerator.pl";
55 require "bin/mozilla/io.pl";
56
57 use strict;
58
59 1;
60
61 sub edit_config {
62   $main::lxdebug->enter_sub();
63
64   my $form     = $main::form;
65   my %myconfig = %main::myconfig;
66   my $locale   = $main::locale;
67
68   $main::auth->assert('config');
69
70   DN->get_config(\%myconfig, \%$form);
71   $form->get_lists('charts' => { 'key'       => 'ALL_CHARTS',
72                                  'transdate' => 'current_date' });
73
74   $form->{SELECT_AR_AMOUNT} = [];
75   $form->{SELECT_AR}        = [];
76
77   foreach my $chart (@{ $form->{ALL_CHARTS} }) {
78     $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
79
80     if ($chart->{LINKS}->{AR}) {
81       $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
82       push @{ $form->{SELECT_AR} }, $chart;
83     }
84
85     if ($chart->{LINKS}->{AR_amount}) {
86       $chart->{AR_amount_fee_selected}      = "selected" if $chart->{id} == $form->{AR_amount_fee};
87       $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
88       push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
89     }
90   }
91
92   $form->{title}      = $locale->text('Edit Dunning Process Config');
93   $form->{callback} ||= build_std_url("action=edit_config");
94
95   setup_dn_edit_config_action_bar();
96
97   $form->header();
98   print $form->parse_html_template("dunning/edit_config");
99
100   $main::lxdebug->leave_sub();
101 }
102
103 sub add {
104   $main::lxdebug->enter_sub();
105
106   my $form     = $main::form;
107   my %myconfig = %main::myconfig;
108   my $locale   = $main::locale;
109
110   $main::auth->assert('dunning_edit');
111
112   DN->get_config(\%myconfig, \%$form);
113
114   $form->get_lists("departments" => "ALL_DEPARTMENTS");
115   $::form->{ALL_PAYMENT_TERMS} = SL::DB::Manager::PaymentTerm->get_all_sorted;
116
117   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
118   $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
119
120   $form->{title}    = $locale->text('Start Dunning Process');
121
122   setup_dn_add_action_bar();
123   $form->header();
124
125   print $form->parse_html_template("dunning/add");
126
127   $main::lxdebug->leave_sub();
128 }
129
130 sub show_invoices {
131   $main::lxdebug->enter_sub();
132
133   my $form     = $main::form;
134   my %myconfig = %main::myconfig;
135   my $locale   = $main::locale;
136
137   $main::auth->assert('dunning_edit');
138
139   DN->get_invoices(\%myconfig, \%$form);
140   $form->{title} = $locale->text('Start Dunning Process');
141
142   foreach my $row (@{ $form->{DUNNINGS} }) {
143     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
144
145     if ($row->{next_dunning_config_id}) {
146       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
147     }
148     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
149
150     if ($row->{'language_id'}) {
151       $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
152     }
153   }
154
155   $form->get_lists('printers'  => 'printers',
156                    'languages' => 'languages');
157
158   $form->{type}           = 'dunning';
159   $form->{rowcount}       = scalar @{ $form->{DUNNINGS} };
160   $form->{callback}     ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
161
162   $form->{PRINT_OPTIONS}  = print_options('inline'          => 1,
163                                           'no_queue'        => 1,
164                                           'no_postscript'   => 1,
165                                           'no_html'         => 1,
166                                           'no_opendocument' => 1,);
167
168   setup_dn_show_invoices_action_bar();
169   $form->header();
170   print $form->parse_html_template("dunning/show_invoices");
171
172   $main::lxdebug->leave_sub();
173 }
174
175 sub save {
176   $main::lxdebug->enter_sub();
177
178   my $form     = $main::form;
179   my %myconfig = %main::myconfig;
180   my $locale   = $main::locale;
181
182   $main::auth->assert('config');
183
184   for my $i (1 .. $form->{rowcount}) {
185     if ($form->{"dunning_description_$i"} ne "") {
186       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
187       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
188       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
189       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
190     }
191   }
192
193   DN->save_config(\%myconfig, \%$form);
194   # saving the history
195   if(!exists $form->{addition} && $form->{id} ne "") {
196     $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
197     $form->{addition} = "SAVED FOR DUNNING";
198     $form->save_history;
199   }
200   # /saving the history
201   $form->redirect($locale->text('Dunning Process Config saved!'));
202
203   $main::lxdebug->leave_sub();
204 }
205
206 sub save_dunning {
207   $main::lxdebug->enter_sub();
208
209   my $form     = $main::form;
210   my %myconfig = %main::myconfig;
211   my $locale   = $main::locale;
212
213   $main::auth->assert('dunning_edit');
214
215   my $active=1;
216   my @rows = ();
217   my @status;
218   undef($form->{DUNNING_PDFS});
219
220   my $saved_language_id = $form->{language_id};
221
222   if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
223     my %dunnings_for;
224
225     for my $i (1 .. $form->{rowcount}) {
226       next unless ($form->{"active_$i"});
227
228       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
229       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
230
231       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
232       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
233
234       push @{ $level }, { "row"                    => $i,
235                           "invoice_id"             => $form->{"inv_id_$i"},
236                           "credit_note"            => $form->{"credit_note_$i"},
237                           "customer_id"            => $form->{"customer_id_$i"},
238                           "language_id"            => $form->{"language_id_$i"},
239                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
240                           "print_invoice"          => $form->{"include_invoice_$i"},
241                           "email"                  => $form->{"email_$i"}, };
242     }
243
244     foreach my $levels (values %dunnings_for) {
245       foreach my $level (values %{ $levels }) {
246         next unless scalar @{ $level };
247         if (!$form->{force_lang}) {
248           $form->{language_id} = @{$level}[0]->{language_id};
249         }
250         my $rc =  DN->save_dunning(\%myconfig, $form, $level);
251         $rc->{error} =~ s{\n}{<br />}g if $rc->{error};
252         push @status, { invnumbers => [map { $form->{'invnumber_' . $_->{row}} } @$level],
253                         map { ( $_ => $rc->{$_} ) } qw(error dunning_id print_original_invoice send_email), };
254       }
255     }
256
257   } else {
258     for my $i (1 .. $form->{rowcount}) {
259       next unless $form->{"active_$i"};
260
261       my $level = [ { "row"                    => $i,
262                       "invoice_id"             => $form->{"inv_id_$i"},
263                       "customer_id"            => $form->{"customer_id_$i"},
264                       "language_id"            => $form->{"language_id_$i"},
265                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
266                       "print_invoice"          => $form->{"include_invoice_$i"},
267                       "email"                  => $form->{"email_$i"}, } ];
268       if (!$form->{force_lang}) {
269         $form->{language_id} = @{$level}[0]->{language_id};
270       }
271       my $rc = DN->save_dunning(\%myconfig, $form, $level);
272       $rc->{error} =~ s{\n}{<br />}g if $rc->{error};
273       push @status, { invnumbers => [map { $form->{'invnumber_' . $_->{row}} } @$level],
274                       map { ( $_ => $rc->{$_} ) } qw(error dunning_id print_original_invoice send_email), };
275     }
276   }
277
278   $form->{language_id} = $saved_language_id;
279
280   my $pdf_filename;
281   my $pdf_content;
282   if ($form->{DUNNING_PDFS} && scalar @{ $form->{DUNNING_PDFS} }) {
283     $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
284     ($pdf_filename, $pdf_content) = DN->melt_pdfs(\%myconfig, $form, $form->{copies}, return_content => $form->{media} ne 'printer');
285
286     flash('info', t8('Dunning Process started for selected invoices!'));
287     if ($form->{media} eq 'printer') {
288       flash('info', t8('The PDF has been printed'));
289     } else {
290       flash('info', t8('The PDF has been created'));
291     }
292   }
293
294   # saving the history
295   if(!exists $form->{addition} && $form->{id} ne "") {
296     $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
297     $form->{addition} = "DUNNING STARTED";
298     $form->save_history;
299   }
300   # /saving the history
301
302   setup_dn_status_action_bar();
303   $form->{"title"} = $locale->text("Dunning status");
304   $form->header();
305   print $form->parse_html_template('dunning/status', {
306     pdf_filename => $pdf_filename,
307     pdf_content  => $pdf_content,
308     status       => \@status, });
309
310   $main::lxdebug->leave_sub();
311 }
312
313 sub set_email {
314   $main::lxdebug->enter_sub();
315
316   my $form     = $main::form;
317   my $locale   = $main::locale;
318
319   $main::auth->assert('dunning_edit');
320
321   $form->{"title"} = $locale->text("Set eMail text");
322   $form->header(
323     no_layout       => 1,
324     use_javascripts => [ qw(ckeditor5/ckeditor ckeditor5/translations/de) ],
325   );
326   print($form->parse_html_template("dunning/set_email"));
327
328   $main::lxdebug->leave_sub();
329 }
330
331 sub search {
332   $main::lxdebug->enter_sub();
333
334   my $form     = $main::form;
335   my %myconfig = %main::myconfig;
336   my $locale   = $main::locale;
337
338   $main::auth->assert('dunning_edit');
339
340   $form->get_lists("customers"   => "ALL_CUSTOMERS",
341                    "departments" => "ALL_DEPARTMENTS");
342   $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
343
344   DN->get_config(\%myconfig, \%$form);
345
346   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
347
348   $form->{title}    = $locale->text('Dunnings');
349
350   setup_dn_search_action_bar();
351   $form->header();
352
353   print $form->parse_html_template("dunning/search");
354
355   $main::lxdebug->leave_sub();
356
357 }
358
359 sub show_dunning {
360   $main::lxdebug->enter_sub();
361
362   my $form     = $main::form;
363   my %myconfig = %main::myconfig;
364   my $locale   = $main::locale;
365   my $cgi      = $::request->{cgi};
366
367   $main::auth->assert('dunning_edit');
368
369   my @filter_field_list = qw(customer_id customer dunning_id dunning_level department_id invnumber ordnumber
370                              transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id
371                              l_mails l_webdav l_documents);
372
373   report_generator_set_default_sort('customername', 1);
374
375   DN->get_dunning(\%myconfig, \%$form);
376
377   if (!$form->{callback}) {
378     $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
379   }
380
381   $form->get_lists('printers'  => 'printers',
382                    'languages' => 'languages');
383
384   $form->{type}          = 'dunning';
385   $form->{PRINT_OPTIONS} = print_options('inline'          => 1,
386                                          'no_queue'        => 1,
387                                          'no_postscript'   => 1,
388                                          'no_html'         => 1,
389                                          'no_opendocument' => 1,);
390   $form->{title}         = $locale->text('Dunning overview');
391
392   my $report = SL::ReportGenerator->new(\%myconfig, $form);
393
394   $report->set_options('std_column_visibility' => 1,
395                        'title'                 => $form->{title});
396   $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
397
398   my %column_defs         =  (
399     'checkbox'            => { 'text' => '', 'visible' => 'HTML' },
400     'dunning_description' => { 'text' => $locale->text('Dunning Level') },
401     'customername'        => { 'text' => $locale->text('Customername') },
402     'departmentname'      => { 'text' => $locale->text('Department') },
403     'language'            => { 'text' => $locale->text('Language') },
404     'invnumber'           => { 'text' => $locale->text('Invnumber') },
405     'transdate'           => { 'text' => $locale->text('Invdate') },
406     'duedate'             => { 'text' => $locale->text('Invoice Duedate') },
407     'amount'              => { 'text' => $locale->text('Amount') },
408     'dunning_id'          => { 'text' => $locale->text('Dunning number') },
409     'dunning_date'        => { 'text' => $locale->text('Dunning Date') },
410     'dunning_duedate'     => { 'text' => $locale->text('Dunning Duedate') },
411     'fee'                 => { 'text' => $locale->text('Total Fees') },
412     'interest'            => { 'text' => $locale->text('Interest') },
413     'salesman'            => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
414     'documents'           => { 'text' => $locale->text('Documents'),   'visible' => $form->{l_documents}? 1 : 0 },
415     'webdav'              => { 'text' => $locale->text('WebDAV'),      'visible' => $form->{l_webdav}   ? 1 : 0 },
416     'mails'               => { 'text' => $locale->text('Mails'),       'visible' => $form->{l_mails}    ? 1 : 0 },
417   );
418
419   $report->set_columns(%column_defs);
420   $report->set_column_order(qw(checkbox dunning_description dunning_id customername language invnumber transdate
421                                duedate amount dunning_date dunning_duedate fee interest salesman departmentname mails webdav documents));
422   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
423
424   my $edit_url  = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
425   my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
426   my $sort_url  = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
427
428   foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman dunning_id)) {
429     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
430     $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
431   }
432
433   my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman dunning_id);
434
435   my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
436
437   $current_dunning_rows  = [];
438   $first_row_for_dunning = 1;
439   $form->{rowcount}      = scalar @{ $form->{DUNNINGS} };
440
441   my $i = 0;
442
443   foreach my $ref (@{ $form->{DUNNINGS} }) {
444     $i++;
445
446     if ($previous_dunning_id != $ref->{dunning_id}) {
447       $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
448       $current_dunning_rows  = [];
449       $first_row_for_dunning = 1;
450     }
451
452     if ($ref->{'language_id'}) {
453       $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
454     }
455
456     my $row = { };
457     foreach my $column (keys %{ $ref }) {
458       $row->{$column} = {
459         'data'  => $first_row_for_dunning || (none { $_ eq $column } qw(dunning_description customername dunning_id)) ? $ref->{$column} : '',
460
461         'align' => $alignment{$column},
462
463         'link'  => (  $column eq 'invnumber'           ? $edit_url->($ref)
464                     : $column eq 'dunning_description' ? $print_url->($ref)
465                     : $column eq 'dunning_id'          ? $print_url->($ref)
466                     :                                    ''),
467       };
468     }
469
470     $row->{checkbox} = !$first_row_for_dunning ? { } : {
471       'raw_data' =>   $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
472                     . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
473       'valign'   => 'center',
474       'align'    => 'center',
475     };
476
477     if ($first_row_for_dunning) {
478       $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
479                                         . " $ref->{language}" };
480     } else {
481       $row->{language} = { };
482     }
483
484     if ($form->{l_documents} && $first_row_for_dunning) {
485       my @files  = SL::File->get_all_versions(object_id   => $ref->{dunning_id},
486                                               object_type => 'dunning',
487                                               file_type   => 'document',);
488       if (scalar @files) {
489         my $html          = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
490         my $text          = join "\n",   map { $_->file_name                              } @files;
491         $row->{documents} = { 'raw_data' => $html, data => $text };
492       } else {
493         $row->{documents} = { };
494       }
495     }
496     if ($form->{l_webdav} && $first_row_for_dunning) {
497       my $webdav = SL::Webdav->new(
498         type     => 'dunning',
499         number   => $ref->{dunning_id},
500       );
501       my @all_objects = $webdav->get_all_objects;
502       if (scalar @all_objects) {
503         my $html          = join '<br>', map { SL::Presenter::WebdavObject::webdav_object($_) } @all_objects;
504         my $text          = join "\n",   map { $_->filename                                   } @all_objects;
505         $row->{webdav}    = { 'raw_data' => $html, data => $text };
506       } else {
507         $row->{webdav}    = { };
508       }
509     }
510
511     if ($form->{l_mails}) {
512       my @mail_links = RecordLinks->get_links(from_table => 'dunning', to_table => 'email_journal', from_id => $ref->{dunning_table_id});
513       if (scalar @mail_links) {
514         my $email_journals = SL::DB::Manager::EmailJournal->get_all(where => [id => [ map { $_->{to_id} } @mail_links ]]);
515         my $html          = join '<br>', map { SL::Presenter::EmailJournal::email_journal($_) } @$email_journals;
516         my $text          = join "\n",   map { $_->subject                                    } @$email_journals;
517         $row->{mails}     = { 'raw_data' => $html, data => $text };
518       } else {
519         $row->{mails}     = { };
520       }
521     }
522
523     push @{ $current_dunning_rows }, $row;
524
525     $previous_dunning_id   = $ref->{dunning_id};
526     $first_row_for_dunning = 0;
527   }
528
529   $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
530
531   $report->set_options('raw_top_info_text'    => $form->parse_html_template('dunning/show_dunning_top'),
532                        'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
533                        'output_format'        => 'HTML',
534                        'attachment_basename'  => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
535     );
536
537   $report->set_options_from_form();
538
539   setup_dn_show_dunning_action_bar();
540   $report->generate_with_headers();
541
542   $main::lxdebug->leave_sub();
543
544 }
545
546 sub print_dunning {
547   $main::lxdebug->enter_sub();
548
549   my $form     = $main::form;
550
551   $main::auth->assert('dunning_edit');
552
553   $form->{rowcount}     = 1;
554   $form->{selected_1}   = 1;
555   $form->{dunning_id_1} = $form->{dunning_id};
556   $form->{language_id_1} = $form->{language_id};
557
558   print_multiple();
559
560   $main::lxdebug->leave_sub();
561 }
562
563 sub delete {
564   $main::auth->assert('dunning_edit');
565
566   my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
567
568   if (!scalar @dunning_ids) {
569     $::form->error($::locale->text('No dunnings have been selected for printing.'));
570   }
571
572   my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
573
574   SL::DB::Dunning->new->db->with_transaction(sub {
575     for my $dunning (@$dunnings) {
576       SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
577       $dunning->delete;
578     }
579   });
580
581   flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
582
583   search();
584 }
585
586 sub print_multiple {
587   $main::lxdebug->enter_sub();
588
589   my $form     = $main::form;
590   my %myconfig = %main::myconfig;
591   my $locale   = $main::locale;
592
593   $main::auth->assert('dunning_edit');
594
595   $form->{title} = $locale->text('Print dunnings');
596
597   my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
598   my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
599
600   if (!scalar @dunning_ids) {
601     $form->error($locale->text('No dunnings have been selected for printing.'));
602   }
603
604   $form->{DUNNING_PDFS} = [];
605
606   my $saved_language_id = $form->{language_id};
607   my $i = 0;
608   foreach my $dunning_id (@dunning_ids) {
609     if (!$form->{force_lang}) {
610       $form->{language_id} = $language_ids[$i];
611     }
612     $form->{dunning_id} = $dunning_id;
613     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
614     DN->print_dunning(\%myconfig, $form, $dunning_id);
615
616     # print original dunned invoices, if they where printed on dunning run
617     my $dunnings = SL::DB::Manager::Dunning->get_all(where => [dunning_id => $dunning_id, original_invoice_printed => 1]);
618     DN->print_original_invoice(\%myconfig, $form, $dunning_id, $_->trans_id) for @$dunnings;
619
620     $i++;
621   }
622   $form->{language_id} = $saved_language_id;
623
624   if (scalar @{ $form->{DUNNING_PDFS} }) {
625     $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
626     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
627
628     if ($form->{media} eq 'printer') {
629       $form->header();
630       $form->info($locale->text('The dunnings have been printed.'));
631     }
632
633   } else {
634     $form->redirect($locale->text('Could not print dunning.'));
635   }
636
637   $main::lxdebug->leave_sub();
638 }
639
640 sub continue {
641   call_sub($main::form->{nextsub});
642 }
643
644 sub dispatcher {
645   foreach my $action (qw(delete print_multiple)) {
646     if ($::form->{"action_${action}"}) {
647       call_sub($action);
648       return;
649     }
650   }
651
652   $::form->error($::locale->text('No action defined.'));
653 }
654
655 sub setup_dn_add_action_bar {
656   my %params = @_;
657
658   for my $bar ($::request->layout->get('actionbar')) {
659     $bar->add(
660       action => [
661         t8('Search'),
662         submit    => [ '#form', { action => "show_invoices" } ],
663         accesskey => 'enter',
664       ],
665     );
666   }
667 }
668
669 sub setup_dn_show_invoices_action_bar {
670   my %params = @_;
671
672   for my $bar ($::request->layout->get('actionbar')) {
673     $bar->add(
674       action => [
675         t8('Create'),
676         submit    => [ '#form', { action => "save_dunning" } ],
677         checks    => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
678         accesskey => 'enter',
679         only_once => 1,
680       ],
681     );
682   }
683 }
684
685 sub setup_dn_search_action_bar {
686   my %params = @_;
687
688   for my $bar ($::request->layout->get('actionbar')) {
689     $bar->add(
690       action => [
691         t8('Search'),
692         submit    => [ '#form', { action => "show_dunning" } ],
693         accesskey => 'enter',
694       ],
695     );
696   }
697 }
698
699 sub setup_dn_show_dunning_action_bar {
700   my %params = @_;
701
702   for my $bar ($::request->layout->get('actionbar')) {
703     $bar->add(
704       action => [
705         t8('Print'),
706         submit    => [ '#form', { action => "print_multiple" } ],
707         checks    => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
708         accesskey => 'enter',
709       ],
710
711       action => [
712         t8('Delete'),
713         submit  => [ '#form', { action => "delete" } ],
714         checks  => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
715         confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
716       ],
717     );
718   }
719 }
720
721 sub setup_dn_edit_config_action_bar {
722   my %params = @_;
723
724   for my $bar ($::request->layout->get('actionbar')) {
725     $bar->add(
726       action => [
727         t8('Save'),
728         submit    => [ '#form', { action => "save" } ],
729         accesskey => 'enter',
730       ],
731     );
732   }
733 }
734
735 sub setup_dn_status_action_bar {
736   for my $bar ($::request->layout->get('actionbar')) {
737     $bar->add(
738       action => [
739         t8('Back'),
740         link      => $::form->{callback},
741         accesskey => 'enter',
742       ],
743     );
744   }
745
746 }
747
748 # end of main