Große Teile des Mahncodes neu geschrieben bzw. umgeschrieben. Mehrere Fehler behoben:
[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 SL::IS;
35 use SL::PE;
36 use SL::DN;
37 use Data::Dumper;
38
39 require "bin/mozilla/io.pl";
40 require "bin/mozilla/arap.pl";
41
42 1;
43
44 sub edit_config {
45   $lxdebug->enter_sub();
46
47   # edit all dunning config data
48
49   $form->header;
50   DN->get_config(\%myconfig, \%$form);
51   $form->{title} = $locale->text('Edit Dunning Process Config');
52   
53   $form->{callback} =
54     "$form->{script}?action=edit_config&login=$form->{login}&password=$form->{password}"
55     unless $form->{callback};
56
57   @column_index = qw(dunning_level dunning_description active auto email payment_terms terms fee interest_rate template);
58
59   $column_header{dunning_level} =
60       qq|<th class=listheading>|
61     . $locale->text('Dunning Level')
62     . qq|</th>|;
63   $column_header{dunning_description} =
64       qq|<th class=listheading>|
65     . $locale->text('Dunning Description')
66     . qq|</th>|;
67   $column_header{active} =
68       qq|<th class=listheading>|
69     . $locale->text('Active?')
70     . qq|</th>|;
71   $column_header{auto} =
72       qq|<th class=listheading>|
73     . $locale->text('Auto Send?')
74     . qq|</th>|;
75   $column_header{email} =
76       qq|<th class=listheading>|
77     . $locale->text('eMail Send?')
78     . qq|</th>|;
79   $column_header{payment_terms} =
80       qq|<th class=listheading>|
81     . $locale->text('Fristsetzung')
82     . qq|</th>|;
83   $column_header{terms} =
84       qq|<th class=listheading>|
85     . $locale->text('Duedate +Days')
86     . qq|</th>|;
87   $column_header{fee} =
88       qq|<th class=listheading>|
89     . $locale->text('Fee')
90     . qq|</th>|;
91   $column_header{interest_rate} =
92       qq|<th class=listheading>|
93     . $locale->text('Interest Rate')
94     . qq|</th>|;
95   $column_header{template} =
96       qq|<th class=listheading>|
97     . $locale->text('Template')
98     . qq|</th>|;
99   print qq|
100 <body>
101 <script type="text/javascript" src="js/common.js"></script>
102 <script type="text/javascript" src="js/dunning.js"></script>
103 <form method=post action=$form->{script}>
104
105
106 <table width=100%>
107   <tr>
108     <th class=listtop colspan=10>$form->{title}</th>
109   </tr>
110   <tr height="5"></tr>
111   <tr>|;
112   map { print "$column_header{$_}\n" } @column_index;
113
114   print qq|
115         </tr>
116 |;
117   my $i = 0;
118   foreach $ref (@{ $form->{DUNNING} }) {
119
120     $i++;
121     my $j = $i % 2;
122
123     print qq|
124         <tr valign=top class=listrow$j>
125 |;
126
127
128     $column_data{dunning_level} =
129       qq|<td><input type=hidden name=dunning_level_$i size=2 value="$i"><input type=hidden name=id_$i value="$ref->{id}">$i</td>|;
130     $column_data{dunning_description}           = qq|<td><input name=dunning_description_$i value="$ref->{dunning_description}"></td>|;
131     my $active = ($ref->{active}) ? "checked" : "";
132     $column_data{active} =
133       qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
134     my $email = ($ref->{email}) ? "checked" : "";
135   $column_data{email} =
136     qq|<td><input type=checkbox name=email_$i value=1 $email><button type="button" onclick="set_email_window('email_subject_$i', 'email_body_$i', 'email_attachment_$i')">| . $locale->text('L') . qq|</button><input type=hidden name=email_body_$i value="$ref->{email_body}"><input type=hidden name=email_subject_$i value="$ref->{email_subject}"><input type=hidden name=email_attachment_$i value="$ref->{email_attachment}"></td>|;
137
138     my $auto = ($ref->{auto}) ? "checked" : "";
139     $column_data{auto} =
140       qq|<td><input type=checkbox name=auto_$i value=1 $auto></td>|;
141     $column_data{payment_terms}           = qq|<td><input name=payment_terms_$i size=3 value="$ref->{payment_terms}"></td>|;
142     $column_data{terms}           = qq|<td><input name=terms_$i size=3 value="$ref->{terms}"></td>|;
143     $column_data{fee}           = qq|<td><input name=fee_$i size=5 value="$ref->{fee}"></td>|;
144     $column_data{interest_rate}           = qq|<td><input name=interest_rate_$i size=4 value="$ref->{interest}">%</td>|;
145     $column_data{template}           = qq|<td><input name=template_$i value="$ref->{template}"></td>|;
146
147
148
149     map { print "$column_data{$_}\n" } @column_index;
150
151     print qq|
152         </tr>
153 |;
154   }
155   $i++;
156   my $j = $i % 2;
157
158   print qq|
159         <tr valign=top class=listrow$j>
160 |;
161
162
163   $column_data{dunning_level} =
164     qq|<td><input type=hidden size=2 name=dunning_level_$i value=$i>$i</td>|;
165   $column_data{dunning_description}           = qq|<td><input name=dunning_description_$i ></td>|;
166   $column_data{active} =
167     qq|<td><input type=checkbox name=active_$i value=1></td>|;
168   my $email = "";
169   $column_data{email} =
170     qq|<td><input type=checkbox name=email_$i value=1 $email><button type="button" onclick="set_email_window('email_subject_$i', 'email_body_$i', 'email_attachment_$i')">| . $locale->text('L') . qq|</button><input type=hidden name=email_body_$i><input type=hidden name=email_subject_$i><input type=hidden name=email_attachment_$i></td>|;
171   my $auto = "";
172   $column_data{auto} =
173     qq|<td><input type=checkbox name=auto_$i value=1 $auto></td>|;
174   $column_data{payment_terms}           = qq|<td><input  size=3 name=payment_terms_$i></td>|;
175   $column_data{terms}           = qq|<td><input  size=3 name=terms_$i></td>|;
176   $column_data{fee}           = qq|<td><input  size=5 name=fee_$i></td>|;
177   $column_data{interest_rate}           = qq|<td><input  size=4 name=interest_rate_$i>%</td>|;
178   $column_data{template}           = qq|<td><input name=template_$i></td>|;
179
180
181   $form->{rowcount} = $i;
182   map { print "$column_data{$_}\n" } @column_index;
183
184   print qq|
185       </tr>
186 |;
187
188
189   print qq|
190       </table>
191     </td>
192   </tr>
193   <tr>
194   <td><hr size=3 noshade></td>
195   </tr>
196 </table>
197
198 <br>
199 <form method=post action=$form->{script}>
200
201 <input name=callback type=hidden value="$form->{callback}">
202 <input name=rowcount type=hidden value="$form->{rowcount}">
203
204 <input type=hidden name=login value=$form->{login}>
205 <input type=hidden name=password value=$form->{password}>
206
207 <input class=submit type=submit name=action value="|
208     . $locale->text('Save') . qq|">
209
210   </form>
211
212   </body>
213   </html>
214 |;
215
216   $lxdebug->leave_sub();
217 }
218
219 sub add {
220   $lxdebug->enter_sub();
221   # setup customer selection
222   $form->all_vc(\%myconfig, "customer", "AR");
223
224   DN->get_config(\%myconfig, \%$form);
225
226   if (@{ $form->{all_customer} }) {
227     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
228       @{ $form->{all_customer} };
229     $customer = qq|<select name=customer><option>\n$customer</select>|;
230   } else {
231     $customer = qq|<input name=customer size=35>|;
232   }
233
234   # dunning levels
235   if (@{ $form->{DUNNING} }) {
236     $form->{selectdunning_level} = "<option></option\n";
237     map {
238       $form->{selectdunning_level} .=
239         "<option value=$_->{id}>$_->{dunning_description}</option>\n"
240     } (@{ $form->{DUNNING} });
241   }
242   $dunning_level = qq| 
243     <tr> 
244     <th align=right nowrap>| . $locale->text('Next Dunning Level') . qq|</th>
245     <td colspan=3><select name=dunning_level>$form->{selectdunning_level}</select></td>
246     </tr>
247     | if $form->{selectdunning_level};
248
249   # departments
250   if (@{ $form->{all_departments} }) {
251     $form->{selectdepartment} = "<option>\n";
252     map {
253       $form->{selectdepartment} .=
254         "<option>$_->{description}--$_->{id}\n"
255     } (@{ $form->{all_departments} });
256   }
257   $department = qq| 
258     <tr> 
259     <th align=right nowrap>| . $locale->text('Department') . qq|</th>
260     <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
261     </tr>
262     | if $form->{selectdepartment};
263
264   $form->{title}       = $locale->text('Start Dunning Process');
265   $form->{nextsub}     = "show_invoices";
266
267   $form->{jsscript}    = 1;
268   $form->{fokus}       = "search.customer";
269   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
270   $form->header();
271
272   $onload = qq|focus()|;
273   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
274   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
275
276   print qq|
277 <body onLoad="$onload">
278
279 <form method=post name="search" action=$form->{script}>
280
281 <table width=100%>
282   <tr><th class=listtop>$form->{title}</th></tr>
283   <tr height="5"></tr>
284   <tr>
285     <td>
286       <table>
287         <tr>
288           <th align=right>| . $locale->text('Customer') . qq|</th>
289           <td colspan=3>$customer</td>
290         </tr>
291         $dunning_level
292         $department
293         <tr>
294           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
295           <td colspan=3><input name=invnumber size=20></td>
296         </tr>
297         <tr>
298           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
299           <td colspan=3><input name=ordnumber size=20></td>
300         </tr>
301         <tr>
302           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
303           <td colspan=3><input name=notes size=40></td>
304         </tr>
305       </table>
306     </td>
307   </tr>
308   <tr>
309     <td><hr size=3 noshade></td>
310   </tr>
311   <tr>
312     <td>
313       <table>
314         <tr>
315           <th align=right nowrap>| . $locale->text('Minimum Amount') . qq|</th>
316           <td><input name=minamount size=6></td>
317         </tr>
318         <tr>
319           <th align=right nowrap>| . $locale->text('Group Invoices') . qq|</th>
320           <td><input type=checkbox value=1 name=groupinvoices checked></td>
321         </tr>
322       </table>
323     </td>
324   </tr>
325 </table>
326
327 <input type=hidden name=nextsub value=$form->{nextsub}>
328
329 <input type=hidden name=login value=$form->{login}>
330 <input type=hidden name=password value=$form->{password}>
331
332 <br>
333 <input class=submit type=submit name=action value="|
334     . $locale->text('Continue') . qq|">
335
336 </form>
337
338 </body>
339
340 $jsscript
341
342 </html>
343 |;
344
345   $lxdebug->leave_sub();
346
347 }
348
349 sub show_invoices {
350   $lxdebug->enter_sub();
351
352   DN->get_invoices(\%myconfig, \%$form);
353   $form->{title} = $locale->text('Start Dunning Process');
354
355   if (@{ $form->{DUNNING_CONFIG} }) {
356     foreach $item (@{ $form->{DUNNING_CONFIG} }) {
357         $form->{selectdunning} .=
358           "<option value=$item->{id}>$item->{dunning_description}</option>";
359     }
360   }
361
362
363   $form->{nextsub} = "save_dunning";
364
365   $form->{jsscript} = 1;
366   $form->{javascript} .= qq|<script type="text/javascript" src="js/checkbox_utils.js"></script>|;
367
368   $form->{callback} =
369     "$form->{script}?action=show_invoices&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&paymentuntil=$form->{paymentuntil}&groupinvoices=$form->{groupinvoices}&minamount=$form->{minamount}&dunning_level=$form->{dunning_level}&notes=$form->{notes}"
370     unless $form->{callback};
371
372   @column_index = qw(dunning_description dunning_description_next active email customername invnumber invdate inv_duedate amount next_duedate fee interest );
373
374   $column_header{dunning_description} =
375       qq|<th class="listheading" colspan="2">|
376     . $locale->text('Current / Next Level')
377     . qq|</th>|;
378   $column_header{active} =
379       qq|<th class=listheading>|
380     . NTI($cgi->checkbox('-name' => 'selectall_active',
381                          '-label' => $locale->text('Active?'),
382                          '-checked' => 0,
383                          '-onclick' => "checkbox_check_all('selectall_active', 'active_', 1, " . scalar(@{ $form->{DUNNINGS} }) . ")"))
384     . qq|</th>|;
385   $column_header{email} =
386       qq|<th class=listheading>|
387     . NTI($cgi->checkbox('-name' => 'selectall_email',
388                          '-label' => $locale->text('eMail?'),
389                          '-checked' => 0,
390                          '-onclick' => "checkbox_check_all('selectall_email', 'email_', 1, " . scalar(@{ $form->{DUNNINGS} }) . ")"))
391     . qq|</th>|;
392   $column_header{customername} =
393       qq|<th class=listheading>|
394     . $locale->text('Customername')
395     . qq|</th>|;
396   $column_header{invnumber} =
397       qq|<th class=listheading>|
398     . $locale->text('Invno.')
399     . qq|</th>|;
400   $column_header{inv_duedate} =
401       qq|<th class=listheading>|
402     . $locale->text('Inv. Duedate')
403     . qq|</th>|;
404   $column_header{next_duedate} =
405       qq|<th class=listheading>|
406     . $locale->text('Dunning Duedate')
407     . qq|</th>|;
408   $column_header{invdate} =
409       qq|<th class=listheading>|
410     . $locale->text('Invdate')
411     . qq|</th>|;
412   $column_header{amount} =
413       qq|<th class=listheading>|
414     . $locale->text('Amount')
415     . qq|</th>|;
416   $column_header{fee} =
417       qq|<th class=listheading>|
418     . $locale->text('Total Fees')
419     . qq|</th>|;
420   $column_header{interest} =
421       qq|<th class=listheading>|
422     . $locale->text('Interest')
423     . qq|</th>|;
424
425   $form->header;
426
427
428   print qq|
429 <body>
430 <script type="text/javascript" src="js/common.js"></script>
431 <script type="text/javascript" src="js/dunning.js"></script>
432 <form name=Form method=post action=$form->{script}>
433
434
435 <table width=100%>
436   <tr>
437     <th class=listtop colspan=12>$form->{title}</th>
438   </tr>
439   <tr height="5"></tr>
440   <tr>|;
441   map { print "$column_header{$_}\n" if $column_header{$_}; } @column_index;
442
443   print qq|
444         </tr>
445 |;
446   my $i = 0;
447   foreach $ref (@{ $form->{DUNNINGS} }) {
448
449     $i++;
450     my $j = $i % 2;
451
452     print qq|
453         <tr valign=top class=listrow$j>
454 |;
455
456     $form->{selectdunning} =~ s/ selected//g;
457     if ($ref->{next_dunning_config_id} ne "") {
458       $form->{selectdunning} =~ s/value=$ref->{next_dunning_config_id}/value=$ref->{next_dunning_config_id} selected/;
459     }
460
461     $column_data{dunning_description} =
462       qq|<td>|
463       . qq|<input type=hidden name=inv_id_$i size=2 value="$ref->{id}">|
464       . qq|<input type=hidden name=customer_id_$i size=2 value="$ref->{customer_id}">|
465       . ($ref->{dunning_level} ? $ref->{dunning_level} : "&nbsp;")
466       . qq|</td>|;
467     $column_data{dunning_description_next} =
468       qq|<td>|
469       . qq|<select name=next_dunning_config_id_$i>$form->{selectdunning}</select>|
470       . qq|</td>|;
471     my $active = ($ref->{active}) ? "checked" : "";
472     $column_data{active} =
473       qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
474     my $email = ($ref->{email}) ? "checked" : "";
475     $column_data{email} =
476       qq|<td><input type=checkbox name=email_$i value=1 $email></td>|;
477     $column_data{next_duedate}           = qq|<td>$ref->{next_duedate}</td>|;
478
479     $column_data{inv_duedate}           = qq|<td><input type=hidden name=inv_duedate_$i size=6 value="$ref->{duedate}">$ref->{duedate}</td>|;
480     $column_data{invdate}           = qq|<td><input type=hidden name=invdate_$i size=6 value="$ref->{transdate}">$ref->{transdate}</td>|;
481     $column_data{invnumber}           = qq|<td><input type=hidden name=invnumber_$i size=6 value="$ref->{invnumber}">$ref->{invnumber}</td>|;
482     $column_data{customername}           = qq|<td><input type=hidden name=customername_$i size=6 value="$ref->{customername}">$ref->{customername}</td>|;
483
484     map { $column_data{$_} =
485             qq|<td align="right">|
486             . H($form->format_amount(\%myconfig, $ref->{$_} * 1, -2))
487             . qq|</td>|
488     } qw(amount fee interest);
489
490     map { print "$column_data{$_}\n" } @column_index;
491
492     print qq|
493         </tr>
494 |;
495   }
496
497   $form->{rowcount} = $i;
498
499   print qq|
500       </table>
501     </td>
502   </tr>
503   <tr>
504   <td><hr size=3 noshade></td>
505   </tr>
506 </table>|;
507   print_options();
508   print qq|
509 <br>
510 <form method=post action=$form->{script}>
511
512 <input name=callback type=hidden value="$form->{callback}">
513 <input name=rowcount type=hidden value="$form->{rowcount}">
514 <input name=nextsub type=hidden value="$form->{nextsub}">
515 <input name=groupinvoices type=hidden value="$form->{groupinvoices}">
516
517
518 <input type=hidden name=login value=$form->{login}>
519 <input type=hidden name=password value=$form->{password}>
520 <input type="hidden" name="action">
521 <input type="submit" name="dummy" value="|
522     . $locale->text('Continue') . qq|" onclick="this.disabled=true; this.value='| . $locale->text("The dunning process started") . qq|'; document.Form.action.value='| . $locale->text('Continue') . qq|'; document.Form.submit()">
523
524   </form>
525
526   </body>
527   </html>
528 |;
529
530
531   $lxdebug->leave_sub();
532
533 }
534
535 sub save {
536   $lxdebug->enter_sub();
537
538   for my $i (1 .. $form->{rowcount}) {
539     if ($form->{"dunning_description_$i"} ne "") {
540       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
541       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
542       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
543       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
544     }
545   }
546
547   DN->save_config(\%myconfig, \%$form);
548   # saving the history
549   if(!exists $form->{addition} && $form->{id} ne "") {
550         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
551     $form->{addition} = "SAVED FOR DUNNING";
552         $form->save_history($form->dbconnect(\%myconfig));
553   }
554   # /saving the history 
555   $form->redirect($locale->text('Dunning Process Config saved!'));
556
557   $lxdebug->leave_sub();
558 }
559
560 sub save_dunning {
561   $lxdebug->enter_sub();
562
563   my $active=1;
564   my @rows = ();
565   undef($form->{DUNNING_PDFS});
566
567   if ($form->{groupinvoices}) {
568     my %dunnings_for;
569
570     for my $i (1 .. $form->{rowcount}) {
571       next unless ($form->{"active_$i"});
572
573       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
574       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
575
576       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
577       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
578
579       push @{ $level }, { "row"                    => $i,
580                           "invoice_id"             => $form->{"inv_id_$i"},
581                           "customer_id"            => $form->{"customer_id_$i"},
582                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
583                           "email"                  => $form->{"email_$i"}, };
584     }
585
586     foreach my $levels (values %dunnings_for) {
587       foreach my $level (values %{ $levels }) {
588         next unless scalar @{ $level };
589
590         DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
591       }
592     }
593
594   } else {
595     for my $i (1 .. $form->{rowcount}) {
596       next unless $form->{"active_$i"};
597
598       my $level = [ { "row"                    => $i,
599                       "invoice_id"             => $form->{"inv_id_$i"},
600                       "customer_id"            => $form->{"customer_id_$i"},
601                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
602                       "email"                  => $form->{"email_$i"}, } ];
603       DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
604     }
605   }
606
607   if($form->{DUNNING_PDFS}) {
608     DN->melt_pdfs(\%myconfig, \%$form,$spool);
609   }
610
611   # saving the history
612   if(!exists $form->{addition} && $form->{id} ne "") {
613         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
614     $form->{addition} = "DUNNING STARTED";
615         $form->save_history($form->dbconnect(\%myconfig));
616   }
617   # /saving the history
618
619   $form->redirect($locale->text('Dunning Process started for selected invoices!'));
620
621   $lxdebug->leave_sub();
622 }
623
624 sub set_email {
625   $lxdebug->enter_sub();
626
627
628   my $callback = "$form->{script}?action=set_email&";
629   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
630       (qw(login password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
631
632   if ($form->{email_attachment}) {
633     $form->{email_attachment} = "checked";
634   }
635   $form->{"title"} = $locale->text("Set eMail text");
636   $form->header();
637   print($form->parse_html_template("dunning/set_email"));
638
639   $lxdebug->leave_sub();
640 }
641
642 sub search {
643   $lxdebug->enter_sub();
644   # setup customer selection
645   $form->all_vc(\%myconfig, "customer", "AR");
646
647   DN->get_config(\%myconfig, \%$form);
648
649   if (@{ $form->{all_customer} }) {
650     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
651       @{ $form->{all_customer} };
652     $customer = qq|<select name=customer><option>\n$customer</select>|;
653   } else {
654     $customer = qq|<input name=customer size=35>|;
655   }
656
657   # dunning levels
658   if (@{ $form->{DUNNING} }) {
659     $form->{selectdunning_level} = "<option></option\n";
660     map {
661       $form->{selectdunning_level} .=
662         "<option value=$_->{id}>$_->{dunning_description}</option>\n"
663     } (@{ $form->{DUNNING} });
664   }
665   $dunning_level = qq| 
666     <tr> 
667     <th align=right nowrap>| . $locale->text('Next Dunning Level') . qq|</th>
668     <td colspan=3><select name=dunning_level>$form->{selectdunning_level}</select></td>
669     </tr>
670     | if $form->{selectdunning_level};
671
672   # departments
673   if (@{ $form->{all_departments} }) {
674     $form->{selectdepartment} = "<option>\n";
675     map {
676       $form->{selectdepartment} .=
677         "<option>$_->{description}--$_->{id}\n"
678     } (@{ $form->{all_departments} });
679   }
680   $department = qq| 
681     <tr> 
682     <th align=right nowrap>| . $locale->text('Department') . qq|</th>
683     <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
684     </tr>
685     | if $form->{selectdepartment};
686   $form->{title}   = $locale->text('Search Dunning');
687   $form->{nextsub} = "show_dunning";
688
689   # use JavaScript Calendar or not
690   $form->{jsscript} = $jscalendar;
691   $jsscript = "";
692   if ($form->{jsscript}) {
693
694     # with JavaScript Calendar
695     $button1 = qq|
696       <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
697       <input type=button name=transdatefrom id="trigger1" value=|
698       . $locale->text('button') . qq|></td>
699       |;
700     $button2 = qq|
701       <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
702       <input type=button name=transdateto id="trigger2" value=|
703       . $locale->text('button') . qq|></td>
704       |;
705     $button3 = qq|
706       <td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
707       <input type=button name=dunningfrom id="trigger3" value=|
708       . $locale->text('button') . qq|></td>
709       |;
710     $button4 = qq|
711       <td><input name=dunningto id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
712       <input type=button name=dunningto id="trigger4" value=|
713       . $locale->text('button') . qq|></td>
714       |;
715
716     #write Trigger
717     $jsscript =
718       Form->write_trigger(\%myconfig, "4", "transdatefrom", "BR", "trigger1", "transdateto", "BR", "trigger2", "dunningfrom", "BR", "trigger3", "dunningto", "BR", "trigger4");
719   } else {
720
721     # without JavaScript Calendar
722     $button1 =
723       qq|<td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
724     $button2 =
725       qq|<td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
726     $button3 =
727       qq|<td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
728     $button4 =
729       qq|<td><input name=dunningfrom id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
730   }
731
732   $form->{fokus} = "search.customer";
733   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
734   $form->header;
735   $onload = qq|focus()|;
736   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
737   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
738
739   print qq|
740 <body onLoad="$onload">
741
742 <form method=post name="search" action=$form->{script}>
743
744 <table width=100%>
745   <tr><th class=listtop>$form->{title}</th></tr>
746   <tr height="5"></tr>
747   <tr>
748     <td>
749       <table>
750         <tr>
751           <th align=right>| . $locale->text('Customer') . qq|</th>
752           <td colspan=3>$customer</td>
753         </tr>
754         $dunning_level
755         $department
756         <tr>
757           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
758           <td colspan=3><input name=invnumber size=20></td>
759         </tr>
760         <tr>
761           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
762           <td colspan=3><input name=ordnumber size=20></td>
763         </tr>
764         <tr>
765           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
766           <td colspan=3><input name=notes size=40></td>
767         </tr>
768         <tr>
769           <th align=right nowrap>| . $locale->text('Invdate from') . qq|</th>
770           $button1
771           <th align=right nowrap>| . $locale->text('To') . qq|</th>
772           $button2
773         </tr>
774         <tr>
775           <th align=right nowrap>| . $locale->text('Dunning Date from') . qq|</th>
776           $button3
777           <th align=right nowrap>| . $locale->text('To') . qq|</th>
778           $button4
779         </tr>
780
781       </table>
782     </td>
783   </tr>
784   <tr>
785     <td><hr size=3 noshade></td>
786   </tr>
787   <tr>
788     <td>
789       <table>
790         <tr>
791           <th align=right nowrap>| . $locale->text('Show old dunnings') . qq|</th>
792           <td><input type=checkbox value=1 name=showold></td>
793         </tr>
794       </table>
795     </td>
796   </tr>
797 </table>
798
799 <input type=hidden name=nextsub value=$form->{nextsub}>
800
801 <input type=hidden name=login value=$form->{login}>
802 <input type=hidden name=password value=$form->{password}>
803
804 <br>
805 <input class=submit type=submit name=action value="|
806     . $locale->text('Continue') . qq|">
807
808 </form>
809
810 </body>
811
812 $jsscript
813
814 </html>
815 |;
816
817   $lxdebug->leave_sub();
818
819 }
820
821 sub show_dunning {
822   $lxdebug->enter_sub();
823
824   DN->get_dunning(\%myconfig, \%$form);
825   $form->{title} = $locale->text('Dunning overview');
826
827
828
829
830   
831   $form->{callback} =
832     "$form->{script}?action=show_dunning&login=$form->{login}&password=$form->{password}&customer=$form->{customer}&invnumber=$form->{invnumber}&ordnumber=$form->{ordnumber}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&dunningfrom=$form->{dunningfrom}&dunningto=$form->{dunningto}&notes=$form->{notes}&showold=$form->{showold}&dunning_level=$form->{dunning_level}"
833     unless $form->{callback};
834
835   @column_index = qw(dunning_description customername invnumber invdate inv_duedate invamount dunning_date next_duedate fee interest );
836
837   $column_header{dunning_description} =
838       qq|<th class=listheading>|
839     . $locale->text('Dunning Level')
840     . qq|</th>|;
841   $column_header{customername} =
842       qq|<th class=listheading>|
843     . $locale->text('Customername')
844     . qq|</th>|;
845   $column_header{invnumber} =
846       qq|<th class=listheading>|
847     . $locale->text('Invnumber')
848     . qq|</th>|;
849   $column_header{inv_duedate} =
850       qq|<th class=listheading>|
851     . $locale->text('Invoice Duedate')
852     . qq|</th>|;
853   $column_header{dunning_date} =
854       qq|<th class=listheading>|
855     . $locale->text('Dunning Date')
856     . qq|</th>|;
857   $column_header{next_duedate} =
858       qq|<th class=listheading>|
859     . $locale->text('Dunning Duedate')
860     . qq|</th>|;
861   $column_header{invdate} =
862       qq|<th class=listheading>|
863     . $locale->text('Invdate')
864     . qq|</th>|;
865   $column_header{invamount} =
866       qq|<th class=listheading>|
867     . $locale->text('Amount')
868     . qq|</th>|;
869   $column_header{fee} =
870       qq|<th class=listheading>|
871     . $locale->text('Total Fees')
872     . qq|</th>|;
873   $column_header{interest} =
874       qq|<th class=listheading>|
875     . $locale->text('Interest')
876     . qq|</th>|;
877
878   $form->header;
879
880
881   print qq|
882 <body>
883 <script type="text/javascript" src="js/common.js"></script>
884 <script type="text/javascript" src="js/dunning.js"></script>
885 <form method=post action=$form->{script}>
886
887
888 <table width=100%>
889   <tr>
890     <th class=listtop colspan=10>$form->{title}</th>
891   </tr>
892   <tr height="5"></tr>
893   <tr>|;
894   map { print "$column_header{$_}\n" } @column_index;
895
896   print qq|
897         </tr>
898 |;
899
900   my %columns = (
901     "dunning_duedate" => "next_duedate",
902     "duedate" => "inv_duedate",
903     "transdate" => "invdate",
904     "amount" => "invamount",
905     );
906
907   my $i = 0;
908   my $j = 0;
909   my ($previous_dunning_id, $first_row_for_dunning);
910   foreach $ref (@{ $form->{DUNNINGS} }) {
911     $i++;
912
913     if ($previous_dunning_id != $ref->{dunning_id}) {
914       $j++;
915       $j = $j % 2;
916       $first_row_for_dunning = 1;
917     } else {
918       $first_row_for_dunning = 0;
919     }
920     $previous_dunning_id = $ref->{dunning_id};
921
922     print qq|
923         <tr valign=top class=listrow$j>
924 |;
925
926   
927
928     foreach (qw(dunning_date dunning_duedate duedate transdate customername amount fee interest)) {
929       my $col = $columns{$_} ? $columns{$_} : $_;
930       $column_data{$col} = "<td>" . H($ref->{$_}) . "</td>";
931     }
932
933     if ($first_row_for_dunning) {
934       $column_data{dunning_description} =
935         qq|<td><a href="dn.pl?action=print_dunning&format=pdf&media=screen&| .
936         qq|dunning_id=| . E($ref->{dunning_id}) .
937         join("", map({ "&${_}=" . E($form->{$_}) } qw(login password callback))) .
938         qq|">| . H($ref->{dunning_description}) . qq|</a></td>|;
939     } else {
940       $column_data{dunning_description} = qq|<td>&nbsp;</td>|;
941       $column_data{customername} = qq|<td>&nbsp;</td>|;
942     }
943
944     $column_data{invnumber} =
945       qq|<td><a href="| . ($ref->{invoice} ? "is.pl" : "ar.pl" ) .
946       qq|?action=edit&id=| . H($ref->{id}) .
947       join("", map({ "&${_}=" . E($form->{$_}) } qw(login password callback))) .
948       qq|">| . H($ref->{invnumber}) . qq|</a></td>|;
949
950     map { print "$column_data{$_}\n" } @column_index;
951
952     print qq|
953         </tr>
954 |;
955   }
956
957   $form->{rowcount} = $i;
958
959   print qq|
960       </table>
961     </td>
962   </tr>
963   <tr>
964   <td><hr size=3 noshade></td>
965   </tr>
966 </table>
967
968 <br>
969 <form method=post action=$form->{script}>
970
971 <input name=callback type=hidden value="$form->{callback}">
972 <input name=rowcount type=hidden value="$form->{rowcount}">
973 <input name=nextsub type=hidden value="$form->{nextsub}">
974
975
976 <input type=hidden name=login value=$form->{login}>
977 <input type=hidden name=password value=$form->{password}>
978
979   </form>
980
981   </body>
982   </html>
983 |;
984
985
986   $lxdebug->leave_sub();
987
988 }
989
990 sub print_dunning {
991   $lxdebug->enter_sub();
992
993   DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
994
995   if($form->{DUNNING_PDFS}) {
996     DN->melt_pdfs(\%myconfig, \%$form,$spool);
997   } else {
998     $form->redirect($locale->text('Could not create dunning copy!'));
999   }
1000
1001   $lxdebug->leave_sub();
1002
1003 }
1004
1005 # end of main
1006