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