$::request->layout->focus() größtenteils durch class="initial_focus" ersetzen
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Dunning process module
31 #
32 #======================================================================
33
34 use POSIX;
35
36 use SL::IS;
37 use SL::PE;
38 use SL::DN;
39 use SL::ReportGenerator;
40
41 require "bin/mozilla/common.pl";
42 require "bin/mozilla/reportgenerator.pl";
43 require "bin/mozilla/io.pl";
44
45 use strict;
46
47 1;
48
49 sub edit_config {
50   $main::lxdebug->enter_sub();
51
52   my $form     = $main::form;
53   my %myconfig = %main::myconfig;
54   my $locale   = $main::locale;
55
56   $main::auth->assert('config');
57
58   DN->get_config(\%myconfig, \%$form);
59   $form->get_lists('charts' => { 'key'       => 'ALL_CHARTS',
60                                  'transdate' => 'current_date' });
61
62   $form->{SELECT_AR_AMOUNT} = [];
63   $form->{SELECT_AR}        = [];
64
65   foreach my $chart (@{ $form->{ALL_CHARTS} }) {
66     $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
67
68     if ($chart->{LINKS}->{AR}) {
69       $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
70       push @{ $form->{SELECT_AR} }, $chart;
71     }
72
73     if ($chart->{LINKS}->{AR_amount}) {
74       $chart->{AR_amount_fee_selected}      = "selected" if $chart->{id} == $form->{AR_amount_fee};
75       $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
76       push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
77     }
78   }
79
80   $form->{title}      = $locale->text('Edit Dunning Process Config');
81   $form->{callback} ||= build_std_url("action=edit_config");
82
83   $form->header();
84   print $form->parse_html_template("dunning/edit_config");
85
86   $main::lxdebug->leave_sub();
87 }
88
89 sub add {
90   $main::lxdebug->enter_sub();
91
92   my $form     = $main::form;
93   my %myconfig = %main::myconfig;
94   my $locale   = $main::locale;
95
96   $main::auth->assert('dunning_edit');
97
98   # setup customer selection
99   $form->all_vc(\%myconfig, "customer", "AR");
100
101   DN->get_config(\%myconfig, \%$form);
102
103   $form->{SHOW_CUSTOMER_SELECTION}      = $form->{all_customer}    && scalar @{ $form->{all_customer} };
104   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
105   $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{all_departments} && scalar @{ $form->{all_departments} || [] };
106
107   $form->{title}    = $locale->text('Start Dunning Process');
108   $form->{jsscript} = 1;
109   $form->header();
110
111   print $form->parse_html_template("dunning/add");
112
113   $main::lxdebug->leave_sub();
114 }
115
116 sub show_invoices {
117   $main::lxdebug->enter_sub();
118
119   my $form     = $main::form;
120   my %myconfig = %main::myconfig;
121   my $locale   = $main::locale;
122
123   $main::auth->assert('dunning_edit');
124
125   DN->get_invoices(\%myconfig, \%$form);
126   $form->{title} = $locale->text('Start Dunning Process');
127
128   foreach my $row (@{ $form->{DUNNINGS} }) {
129     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
130
131     if ($row->{next_dunning_config_id}) {
132       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
133     }
134     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, -2) } qw(amount open_amount fee interest);
135
136     if ($row->{'language_id'}) {
137       $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
138     }
139   }
140
141   $form->get_lists('printers'  => 'printers',
142                    'languages' => 'languages');
143
144   $form->{type}           = 'dunning';
145   $form->{rowcount}       = scalar @{ $form->{DUNNINGS} };
146   $form->{jsscript}       = 1;
147   $form->{callback}     ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
148
149   $form->{PRINT_OPTIONS}  = print_options('inline'          => 1,
150                                           'no_queue'        => 1,
151                                           'no_postscript'   => 1,
152                                           'no_html'         => 1,
153                                           'no_opendocument' => 1,);
154
155   $form->header();
156   print $form->parse_html_template("dunning/show_invoices");
157
158   $main::lxdebug->leave_sub();
159 }
160
161 sub save {
162   $main::lxdebug->enter_sub();
163
164   my $form     = $main::form;
165   my %myconfig = %main::myconfig;
166   my $locale   = $main::locale;
167
168   $main::auth->assert('config');
169
170   for my $i (1 .. $form->{rowcount}) {
171     if ($form->{"dunning_description_$i"} ne "") {
172       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
173       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
174       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
175       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
176     }
177   }
178
179   DN->save_config(\%myconfig, \%$form);
180   # saving the history
181   if(!exists $form->{addition} && $form->{id} ne "") {
182     $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
183     $form->{addition} = "SAVED FOR DUNNING";
184     $form->save_history;
185   }
186   # /saving the history
187   $form->redirect($locale->text('Dunning Process Config saved!'));
188
189   $main::lxdebug->leave_sub();
190 }
191
192 sub save_dunning {
193   $main::lxdebug->enter_sub();
194
195   my $form     = $main::form;
196   my %myconfig = %main::myconfig;
197   my $locale   = $main::locale;
198
199   $main::auth->assert('dunning_edit');
200
201   my $active=1;
202   my @rows = ();
203   undef($form->{DUNNING_PDFS});
204
205   my $saved_language_id = $form->{language_id};
206
207   if ($form->{groupinvoices}) {
208     my %dunnings_for;
209
210     for my $i (1 .. $form->{rowcount}) {
211       next unless ($form->{"active_$i"});
212
213       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
214       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
215
216       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
217       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
218
219       push @{ $level }, { "row"                    => $i,
220                           "invoice_id"             => $form->{"inv_id_$i"},
221                           "customer_id"            => $form->{"customer_id_$i"},
222                           "language_id"            => $form->{"language_id_$i"},
223                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
224                           "email"                  => $form->{"email_$i"}, };
225     }
226
227     foreach my $levels (values %dunnings_for) {
228       foreach my $level (values %{ $levels }) {
229         next unless scalar @{ $level };
230         if (!$form->{force_lang}) {
231           $form->{language_id} = @{$level}[0]->{language_id};
232         }
233         DN->save_dunning(\%myconfig, $form, $level);
234       }
235     }
236
237   } else {
238     for my $i (1 .. $form->{rowcount}) {
239       next unless $form->{"active_$i"};
240
241       my $level = [ { "row"                    => $i,
242                       "invoice_id"             => $form->{"inv_id_$i"},
243                       "customer_id"            => $form->{"customer_id_$i"},
244                       "language_id"            => $form->{"language_id_$i"},
245                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
246                       "email"                  => $form->{"email_$i"}, } ];
247       if (!$form->{force_lang}) {
248         $form->{language_id} = @{$level}[0]->{language_id};
249       }
250       DN->save_dunning(\%myconfig, $form, $level);
251     }
252   }
253
254   $form->{language_id} = $saved_language_id;
255
256   if($form->{DUNNING_PDFS}) {
257     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
258   }
259
260   # saving the history
261   if(!exists $form->{addition} && $form->{id} ne "") {
262     $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
263     $form->{addition} = "DUNNING STARTED";
264     $form->save_history;
265   }
266   # /saving the history
267
268   if ($form->{media} eq 'printer') {
269     delete $form->{callback};
270     $form->redirect($locale->text('Dunning Process started for selected invoices!'));
271   }
272
273   $main::lxdebug->leave_sub();
274 }
275
276 sub set_email {
277   $main::lxdebug->enter_sub();
278
279   my $form     = $main::form;
280   my $locale   = $main::locale;
281
282   $main::auth->assert('dunning_edit');
283
284   $form->{"title"} = $locale->text("Set eMail text");
285   $form->header(no_layout => 1);
286   print($form->parse_html_template("dunning/set_email"));
287
288   $main::lxdebug->leave_sub();
289 }
290
291 sub search {
292   $main::lxdebug->enter_sub();
293
294   my $form     = $main::form;
295   my %myconfig = %main::myconfig;
296   my $locale   = $main::locale;
297
298   $main::auth->assert('dunning_edit');
299
300   $form->get_lists("customers"   => "ALL_CUSTOMERS",
301                    "departments" => "ALL_DEPARTMENTS");
302   $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
303
304   DN->get_config(\%myconfig, \%$form);
305
306   $form->{SHOW_CUSTOMER_DDBOX}   = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
307   $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
308   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
309
310   $form->{jsscript} = 1;
311   $form->{title}    = $locale->text('Dunnings');
312
313   $form->header();
314
315   print $form->parse_html_template("dunning/search");
316
317   $main::lxdebug->leave_sub();
318
319 }
320
321 sub show_dunning {
322   $main::lxdebug->enter_sub();
323
324   my $form     = $main::form;
325   my %myconfig = %main::myconfig;
326   my $locale   = $main::locale;
327   my $cgi      = $::request->{cgi};
328
329   $main::auth->assert('dunning_edit');
330
331   my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
332                              transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id);
333
334   report_generator_set_default_sort('customername', 1);
335
336   DN->get_dunning(\%myconfig, \%$form);
337
338   if (!$form->{callback}) {
339     $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
340   }
341
342   $form->get_lists('printers'  => 'printers',
343                    'languages' => 'languages');
344
345   $form->{type}          = 'dunning';
346   $form->{PRINT_OPTIONS} = print_options('inline'          => 1,
347                                          'no_queue'        => 1,
348                                          'no_postscript'   => 1,
349                                          'no_html'         => 1,
350                                          'no_opendocument' => 1,);
351   $form->{title}         = $locale->text('Dunning overview');
352
353   my $report = SL::ReportGenerator->new(\%myconfig, $form);
354
355   $report->set_options('std_column_visibility' => 1,
356                        'title'                 => $form->{title});
357   $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
358
359   my %column_defs         =  (
360     'checkbox'            => { 'text' => '', 'visible' => 'HTML' },
361     'dunning_description' => { 'text' => $locale->text('Dunning Level') },
362     'customername'        => { 'text' => $locale->text('Customername') },
363     'language'            => { 'text' => $locale->text('Language') },
364     'invnumber'           => { 'text' => $locale->text('Invnumber') },
365     'transdate'           => { 'text' => $locale->text('Invdate') },
366     'duedate'             => { 'text' => $locale->text('Invoice Duedate') },
367     'amount'              => { 'text' => $locale->text('Amount') },
368     'dunning_date'        => { 'text' => $locale->text('Dunning Date') },
369     'dunning_duedate'     => { 'text' => $locale->text('Dunning Duedate') },
370     'fee'                 => { 'text' => $locale->text('Total Fees') },
371     'interest'            => { 'text' => $locale->text('Interest') },
372     'salesman'            => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
373   );
374
375   $report->set_columns(%column_defs);
376   $report->set_column_order(qw(checkbox dunning_description customername language invnumber transdate
377                                duedate amount dunning_date dunning_duedate fee interest salesman));
378   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
379
380   my $edit_url  = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
381   my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
382   my $sort_url  = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
383
384   foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman)) {
385     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
386     $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
387   }
388
389   my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman);
390
391   my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
392
393   $current_dunning_rows  = [];
394   $first_row_for_dunning = 1;
395   $form->{rowcount}      = scalar @{ $form->{DUNNINGS} };
396
397   my $i = 0;
398
399   foreach my $ref (@{ $form->{DUNNINGS} }) {
400     $i++;
401
402     if ($previous_dunning_id != $ref->{dunning_id}) {
403       $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
404       $current_dunning_rows  = [];
405       $first_row_for_dunning = 1;
406     }
407
408     if ($ref->{'language_id'}) {
409       $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
410     }
411
412     my $row = { };
413     foreach my $column (keys %{ $ref }) {
414       $row->{$column} = {
415         'data'  => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
416
417         'align' => $alignment{$column},
418
419         'link'  => (  $column eq 'invnumber'           ? $edit_url->($ref)
420                     : $column eq 'dunning_description' ? $print_url->($ref)
421                     :                                    ''),
422       };
423     }
424
425     $row->{checkbox} = !$first_row_for_dunning ? { } : {
426       'raw_data' =>   $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
427                     . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
428       'valign'   => 'center',
429       'align'    => 'center',
430     };
431
432     if ($first_row_for_dunning) {
433       $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
434                                         . " $ref->{language}" };
435     } else {
436       $row->{language} = { };
437     }
438
439     push @{ $current_dunning_rows }, $row;
440
441     $previous_dunning_id   = $ref->{dunning_id};
442     $first_row_for_dunning = 0;
443   }
444
445   $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
446
447   $report->set_options('raw_top_info_text'    => $form->parse_html_template('dunning/show_dunning_top'),
448                        'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
449                        'output_format'        => 'HTML',
450                        'attachment_basename'  => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
451     );
452
453   $report->set_options_from_form();
454
455   $report->generate_with_headers();
456
457   $main::lxdebug->leave_sub();
458
459 }
460
461 sub print_dunning {
462   $main::lxdebug->enter_sub();
463
464   my $form     = $main::form;
465
466   $main::auth->assert('dunning_edit');
467
468   $form->{rowcount}     = 1;
469   $form->{selected_1}   = 1;
470   $form->{dunning_id_1} = $form->{dunning_id};
471   $form->{language_id_1} = $form->{language_id};
472
473   print_multiple();
474
475   $main::lxdebug->leave_sub();
476 }
477
478 sub print_multiple {
479   $main::lxdebug->enter_sub();
480
481   my $form     = $main::form;
482   my %myconfig = %main::myconfig;
483   my $locale   = $main::locale;
484
485   $main::auth->assert('dunning_edit');
486
487   $form->{title} = $locale->text('Print dunnings');
488
489   my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
490   my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
491
492   if (!scalar @dunning_ids) {
493     $form->error($locale->text('No dunnings have been selected for printing.'));
494   }
495
496   $form->{DUNNING_PDFS} = [];
497
498   my $saved_language_id = $form->{language_id};
499   my $i = 0;
500   foreach my $dunning_id (@dunning_ids) {
501     if (!$form->{force_lang}) {
502       $form->{language_id} = $language_ids[$i];
503     }
504     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
505     DN->print_dunning(\%myconfig, $form, $dunning_id);
506     $i++;
507   }
508   $form->{language_id} = $saved_language_id;
509
510   if (scalar @{ $form->{DUNNING_PDFS} }) {
511     $form->{dunning_id} = strftime("%Y%m%d", localtime time);
512     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
513
514     if ($form->{media} eq 'printer') {
515       $form->header();
516       $form->info($locale->text('The dunnings have been printed.'));
517     }
518
519   } else {
520     $form->redirect($locale->text('Could not print dunning.'));
521   }
522
523   $main::lxdebug->leave_sub();
524 }
525
526 sub continue {
527   call_sub($main::form->{nextsub});
528 }
529
530 # end of main