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