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