report_generator.pl in reportgenerator.pl umbenannt.
[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 require "bin/mozilla/arap.pl";
45
46 1;
47
48 sub edit_config {
49   $lxdebug->enter_sub();
50
51   DN->get_config(\%myconfig, \%$form);
52   $form->get_lists('charts' => { 'key'       => 'ALL_CHARTS',
53                                  'transdate' => 'current_date' });
54
55   $form->{SELECT_AR_AMOUNT} = [];
56   $form->{SELECT_AR}        = [];
57
58   foreach my $chart (@{ $form->{ALL_CHARTS} }) {
59     $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
60
61     if ($chart->{LINKS}->{AR}) {
62       $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
63       push @{ $form->{SELECT_AR} }, $chart;
64     }
65
66     if ($chart->{LINKS}->{AR_amount}) {
67       $chart->{AR_amount_fee_selected}      = "selected" if $chart->{id} == $form->{AR_amount_fee};
68       $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
69       push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
70     }
71   }
72
73   $form->{title}          = $locale->text('Edit Dunning Process Config');
74   $form->{callback}     ||= build_std_url("action=edit_config");
75   $form->{rowcount}       = 1 + scalar @{ $form->{DUNNING} };
76   $form->{rowcount_odd}   = $form->{rowcount} % 2;
77
78   $form->header();
79   print $form->parse_html_template("dunning/edit_config");
80
81   $lxdebug->leave_sub();
82 }
83
84 sub add {
85   $lxdebug->enter_sub();
86
87   # setup customer selection
88   $form->all_vc(\%myconfig, "customer", "AR");
89
90   DN->get_config(\%myconfig, \%$form);
91
92   $form->{SHOW_CUSTOMER_SELECTION}      = $form->{all_customer}    && scalar @{ $form->{all_customer} };
93   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
94   $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{all_departments} && scalar @{ $form->{all_departments} };
95
96   $form->{title}    = $locale->text('Start Dunning Process');
97   $form->{jsscript} = 1;
98   $form->{fokus}    = "search.customer";
99   $form->header();
100
101   print $form->parse_html_template("dunning/add");
102
103   $lxdebug->leave_sub();
104 }
105
106 sub show_invoices {
107   $lxdebug->enter_sub();
108
109   DN->get_invoices(\%myconfig, \%$form);
110   $form->{title} = $locale->text('Start Dunning Process');
111
112   foreach my $row (@{ $form->{DUNNINGS} }) {
113     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
114
115     if ($row->{next_dunning_config_id}) {
116       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
117     }
118     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, -2) } qw(amount fee interest);
119   }
120
121   $form->get_lists('printers'  => 'printers',
122                    'languages' => 'languages');
123
124   $form->{type}           = 'dunning';
125   $form->{rowcount}       = scalar @{ $form->{DUNNINGS} };
126   $form->{jsscript}       = 1;
127   $form->{callback}     ||= build_std_url("action=show_invoices", qw(login password customer invnumber ordnumber groupinvoices minamount dunning_level notes));
128
129   $form->{PRINT_OPTIONS}  = print_options({ 'inline'          => 1,
130                                             'no_queue'        => 1,
131                                             'no_postscript'   => 1,
132                                             'no_html'         => 1,
133                                             'no_opendocument' => 1, });
134
135   $form->header();
136   print $form->parse_html_template("dunning/show_invoices");
137
138   $lxdebug->leave_sub();
139 }
140
141 sub save {
142   $lxdebug->enter_sub();
143
144   for my $i (1 .. $form->{rowcount}) {
145     if ($form->{"dunning_description_$i"} ne "") {
146       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
147       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
148       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
149       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
150     }
151   }
152
153   DN->save_config(\%myconfig, \%$form);
154   # saving the history
155   if(!exists $form->{addition} && $form->{id} ne "") {
156         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
157     $form->{addition} = "SAVED FOR DUNNING";
158         $form->save_history($form->dbconnect(\%myconfig));
159   }
160   # /saving the history
161   $form->redirect($locale->text('Dunning Process Config saved!'));
162
163   $lxdebug->leave_sub();
164 }
165
166 sub save_dunning {
167   $lxdebug->enter_sub();
168
169   my $active=1;
170   my @rows = ();
171   undef($form->{DUNNING_PDFS});
172
173   if ($form->{groupinvoices}) {
174     my %dunnings_for;
175
176     for my $i (1 .. $form->{rowcount}) {
177       next unless ($form->{"active_$i"});
178
179       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
180       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
181
182       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
183       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
184
185       push @{ $level }, { "row"                    => $i,
186                           "invoice_id"             => $form->{"inv_id_$i"},
187                           "customer_id"            => $form->{"customer_id_$i"},
188                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
189                           "email"                  => $form->{"email_$i"}, };
190     }
191
192     foreach my $levels (values %dunnings_for) {
193       foreach my $level (values %{ $levels }) {
194         next unless scalar @{ $level };
195
196         DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool, $sendmail);
197       }
198     }
199
200   } else {
201     for my $i (1 .. $form->{rowcount}) {
202       next unless $form->{"active_$i"};
203
204       my $level = [ { "row"                    => $i,
205                       "invoice_id"             => $form->{"inv_id_$i"},
206                       "customer_id"            => $form->{"customer_id_$i"},
207                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
208                       "email"                  => $form->{"email_$i"}, } ];
209       DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool, $sendmail);
210     }
211   }
212
213   if($form->{DUNNING_PDFS}) {
214     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
215   }
216
217   # saving the history
218   if(!exists $form->{addition} && $form->{id} ne "") {
219         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
220     $form->{addition} = "DUNNING STARTED";
221         $form->save_history($form->dbconnect(\%myconfig));
222   }
223   # /saving the history
224
225   if ($form->{media} eq 'printer') {
226     delete $form->{callback};
227     $form->redirect($locale->text('Dunning Process started for selected invoices!'));
228   }
229
230   $lxdebug->leave_sub();
231 }
232
233 sub set_email {
234   $lxdebug->enter_sub();
235
236   $form->{"title"} = $locale->text("Set eMail text");
237   $form->header();
238   print($form->parse_html_template("dunning/set_email"));
239
240   $lxdebug->leave_sub();
241 }
242
243 sub search {
244   $lxdebug->enter_sub();
245
246   $form->get_lists("customers"   => "ALL_CUSTOMERS",
247                    "departments" => "ALL_DEPARTMENTS");
248
249   DN->get_config(\%myconfig, \%$form);
250
251   $form->{SHOW_CUSTOMER_DDBOX}   = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
252   $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
253   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
254
255   $form->{jsscript} = 1;
256   $form->{title}    = $locale->text('Search Dunning');
257   $form->{fokus}    = "search.customer";
258
259   $form->header();
260
261   $form->{onload} = qq|focus()|
262     . qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|
263     . qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
264
265   print $form->parse_html_template("dunning/search");
266
267   $lxdebug->leave_sub();
268
269 }
270
271 sub show_dunning {
272   $lxdebug->enter_sub();
273
274   my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
275                              transdatefrom transdateto dunningfrom dunningto notes showold);
276
277   DN->get_dunning(\%myconfig, \%$form);
278
279   if (!$form->{callback}) {
280     $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
281   }
282
283   $form->get_lists('printers'  => 'printers',
284                    'languages' => 'languages');
285
286   $form->{type}          = 'dunning';
287   $form->{PRINT_OPTIONS} = print_options({ 'inline'          => 1,
288                                            'no_queue'        => 1,
289                                            'no_postscript'   => 1,
290                                            'no_html'         => 1,
291                                            'no_opendocument' => 1, });
292   $form->{title}         = $locale->text('Dunning overview');
293
294   my $report = SL::ReportGenerator->new(\%myconfig, $form);
295
296   $report->set_options('std_column_visibility' => 1,
297                        'title'                 => $form->{title});
298   $report->set_export_options('show_dunning', @filter_field_list);
299
300   $report->set_columns(
301     'checkbox'            => { 'text' => '', 'visible' => 'HTML' },
302     'dunning_description' => { 'text' => $locale->text('Dunning Level') },
303     'customername'        => { 'text' => $locale->text('Customername') },
304     'invnumber'           => { 'text' => $locale->text('Invnumber') },
305     'transdate'           => { 'text' => $locale->text('Invdate') },
306     'duedate'             => { 'text' => $locale->text('Invoice Duedate') },
307     'amount'              => { 'text' => $locale->text('Amount') },
308     'dunning_date'        => { 'text' => $locale->text('Dunning Date') },
309     'dunning_duedate'     => { 'text' => $locale->text('Dunning Duedate') },
310     'fee'                 => { 'text' => $locale->text('Total Fees') },
311     'interest'            => { 'text' => $locale->text('Interest') },
312   );
313
314   $report->set_column_order(qw(checkbox dunning_description customername invnumber transdate
315                                duedate amount dunning_date dunning_duedate fee interest));
316
317   my $edit_url  = build_std_url('script=is.pl', 'action=edit', 'callback') . '&id=';
318   my $print_url = build_std_url('action=print_dunning', 'format=pdf', 'media=screen') . '&dunning_id=';
319
320   my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest);
321
322   my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
323
324   $current_dunning_rows  = [];
325   $first_row_for_dunning = 1;
326   $form->{rowcount}      = scalar @{ $form->{DUNNINGS} };
327
328   my $i = 0;
329
330   foreach $ref (@{ $form->{DUNNINGS} }) {
331     $i++;
332
333     if ($previous_dunning_id != $ref->{dunning_id}) {
334       $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
335       $current_dunning_rows  = [];
336       $first_row_for_dunning = 1;
337     }
338
339     my $row = { };
340     foreach my $column (keys %{ $ref }) {
341       $row->{$column} = {
342         'data'  => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
343
344         'align' => $alignment{$column},
345
346         'link'  => ($column eq 'invnumber'           ? $edit_url  . E($ref->{id})         :
347                     $column eq 'dunning_description' ? $print_url . E($ref->{dunning_id}) : ''),
348       };
349     }
350
351     $row->{checkbox} = !$first_row_for_dunning ? { } : {
352       'raw_data' =>   $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
353                     . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
354       'valign'   => 'center',
355       'align'    => 'center',
356     };
357
358     push @{ $current_dunning_rows }, $row;
359
360     $previous_dunning_id   = $ref->{dunning_id};
361     $first_row_for_dunning = 0;
362   }
363
364   $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
365
366   $report->set_options('raw_top_info_text'    => $form->parse_html_template('dunning/show_dunning_top'),
367                        'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
368                        'output_format'        => 'HTML',
369                        'attachment_basename'  => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
370     );
371
372   $report->set_options_from_form();
373
374   $report->generate_with_headers();
375
376   $lxdebug->leave_sub();
377
378 }
379
380 sub print_dunning {
381   $lxdebug->enter_sub();
382
383   $form->{rowcount}     = 1;
384   $form->{selected_1}   = 1;
385   $form->{dunning_id_1} = $form->{dunning_id};
386
387   print_multiple();
388
389   $lxdebug->leave_sub();
390 }
391
392 sub print_multiple {
393   $lxdebug->enter_sub();
394
395   $form->{title} = $locale->text('Print dunnings');
396
397   my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
398
399   if (!scalar @dunning_ids) {
400     $form->error($locale->text('No dunnings have been selected for printing.'));
401   }
402
403   $form->{DUNNING_PDFS} = [];
404
405   foreach my $dunning_id (@dunning_ids) {
406     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
407     DN->print_dunning(\%myconfig, $form, $dunning_id);
408   }
409
410   if (scalar @{ $form->{DUNNING_PDFS} }) {
411     $form->{dunning_id} = strftime("%Y%m%d", localtime time);
412     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
413
414     if ($form->{media} eq 'printer') {
415       $form->header();
416       $form->info($locale->text('The dunnings have been printed.'));
417     }
418
419   } else {
420     $form->redirect($locale->text('Could not print dunning.'));
421   }
422
423   $lxdebug->leave_sub();
424 }
425
426 # end of main