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