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