725aed997c78b2139c99d8bc72309a720e8c8924
[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 "$form->{path}/io.pl";
40 require "$form->{path}/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&path=$form->{path}&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 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} =
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}           = qq|<td><input name=interest_$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}           = qq|<td><input  size=4 name=interest_$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=path value=$form->{path}>
205 <input type=hidden name=login value=$form->{login}>
206 <input type=hidden name=password value=$form->{password}>
207
208 <input class=submit type=submit name=action value="|
209     . $locale->text('Save') . qq|">
210
211   </form>
212
213   </body>
214   </html>
215 |;
216
217   $lxdebug->leave_sub();
218 }
219
220 sub add {
221   $lxdebug->enter_sub();
222   # setup customer selection
223   $form->all_vc(\%myconfig, "customer", "AR");
224
225   DN->get_config(\%myconfig, \%$form);
226
227   if (@{ $form->{all_customer} }) {
228     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
229       @{ $form->{all_customer} };
230     $customer = qq|<select name=customer><option>\n$customer</select>|;
231   } else {
232     $customer = qq|<input name=customer size=35>|;
233   }
234
235   # dunning levels
236   if (@{ $form->{DUNNING} }) {
237     $form->{selectdunning_level} = "<option></option\n";
238     map {
239       $form->{selectdunning_level} .=
240         "<option value=$_->{id}>$_->{dunning_description}</option>\n"
241     } (@{ $form->{DUNNING} });
242   }
243   $dunning_level = qq| 
244     <tr> 
245     <th align=right nowrap>| . $locale->text('Next Dunning Level') . qq|</th>
246     <td colspan=3><select name=dunning_level>$form->{selectdunning_level}</select></td>
247     </tr>
248     | if $form->{selectdunning_level};
249
250   # departments
251   if (@{ $form->{all_departments} }) {
252     $form->{selectdepartment} = "<option>\n";
253     map {
254       $form->{selectdepartment} .=
255         "<option>$_->{description}--$_->{id}\n"
256     } (@{ $form->{all_departments} });
257   }
258   $department = qq| 
259     <tr> 
260     <th align=right nowrap>| . $locale->text('Department') . qq|</th>
261     <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
262     </tr>
263     | if $form->{selectdepartment};
264   $form->{title}   = $locale->text('Start Dunning Process');
265   $form->{nextsub} = "show_invoices";
266
267   # use JavaScript Calendar or not
268   $form->{jsscript} = $jscalendar;
269   $jsscript = "";
270   if ($form->{jsscript}) {
271
272     # with JavaScript Calendar
273     $button1 = qq|
274       <td><input name=paymentuntil id=paymentuntil size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
275       <input type=button name=paymentuntil id="trigger1" value=|
276       . $locale->text('button') . qq|></td>
277       |;
278
279     #write Trigger
280     $jsscript =
281       Form->write_trigger(\%myconfig, "1", "paymentuntil", "BR", "trigger1");
282   } else {
283
284     # without JavaScript Calendar
285     $button1 =
286       qq|<td><input name=paymentuntil id=paymentuntil size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
287   }
288   $form->{fokus} = "search.customer";
289   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
290   $form->header;
291   $onload = qq|focus()|;
292   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
293   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
294   print qq|
295 <body onLoad="$onload">
296
297 <form method=post name="search" action=$form->{script}>
298
299 <table width=100%>
300   <tr><th class=listtop>$form->{title}</th></tr>
301   <tr height="5"></tr>
302   <tr>
303     <td>
304       <table>
305         <tr>
306           <th align=right>| . $locale->text('Customer') . qq|</th>
307           <td colspan=3>$customer</td>
308         </tr>
309         $dunning_level
310         $department
311         <tr>
312           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
313           <td colspan=3><input name=invnumber size=20></td>
314         </tr>
315         <tr>
316           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
317           <td colspan=3><input name=ordnumber size=20></td>
318         </tr>
319         <tr>
320           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
321           <td colspan=3><input name=notes size=40></td>
322         </tr>
323         <tr>
324           <th align=right nowrap>| . $locale->text('Payment until') . qq|</th>
325           $button1
326         </tr>
327       </table>
328     </td>
329   </tr>
330   <tr>
331     <td><hr size=3 noshade></td>
332   </tr>
333   <tr>
334     <td>
335       <table>
336         <tr>
337           <th align=right nowrap>| . $locale->text('Minimum Amount') . qq|</th>
338           <td><input name=minamount size=6></td>
339         </tr>
340         <tr>
341           <th align=right nowrap>| . $locale->text('Group Invoices') . qq|</th>
342           <td><input type=checkbox value=1 name=groupinvoices checked></td>
343         </tr>
344       </table>
345     </td>
346   </tr>
347 </table>
348
349 <input type=hidden name=nextsub value=$form->{nextsub}>
350
351 <input type=hidden name=path value=$form->{path}>
352 <input type=hidden name=login value=$form->{login}>
353 <input type=hidden name=password value=$form->{password}>
354
355 <br>
356 <input class=submit type=submit name=action value="|
357     . $locale->text('Continue') . qq|">
358
359 </form>
360
361 </body>
362
363 $jsscript
364
365 </html>
366 |;
367
368   $lxdebug->leave_sub();
369
370 }
371
372 sub show_invoices {
373   $lxdebug->enter_sub();
374
375   DN->get_invoices(\%myconfig, \%$form);
376   $form->{title} = $locale->text('Start Dunning Process');
377
378   if (@{ $form->{DUNNING_CONFIG} }) {
379     foreach $item (@{ $form->{DUNNING_CONFIG} }) {
380         $form->{selectdunning} .=
381           "<option value=$item->{id}>$item->{dunning_description}</option>";
382     }
383   }
384
385
386   $form->{nextsub} = "save_dunning";
387   
388   $form->{callback} =
389     "$form->{script}?action=show_invoices&path=$form->{path}&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}"
390     unless $form->{callback};
391
392   @column_index = qw(dunning_description active email customername invnumber invdate inv_duedate invamount next_duedate fee interest );
393
394   $column_header{dunning_description} =
395       qq|<th class=listheading>|
396     . $locale->text('Current / Next Level')
397     . qq|</th>|;
398   $column_header{active} =
399       qq|<th class=listheading>|
400     . $locale->text('Active?')
401     . qq|</th>|;
402   $column_header{email} =
403       qq|<th class=listheading>|
404     . $locale->text('eMail?')
405     . qq|</th>|;
406   $column_header{customername} =
407       qq|<th class=listheading>|
408     . $locale->text('Customername')
409     . qq|</th>|;
410   $column_header{invnumber} =
411       qq|<th class=listheading>|
412     . $locale->text('Invno.')
413     . qq|</th>|;
414   $column_header{inv_duedate} =
415       qq|<th class=listheading>|
416     . $locale->text('Inv. Duedate')
417     . qq|</th>|;
418   $column_header{next_duedate} =
419       qq|<th class=listheading>|
420     . $locale->text('Dunning Duedate')
421     . qq|</th>|;
422   $column_header{invdate} =
423       qq|<th class=listheading>|
424     . $locale->text('Invdate')
425     . qq|</th>|;
426   $column_header{invamount} =
427       qq|<th class=listheading>|
428     . $locale->text('Amount')
429     . qq|</th>|;
430   $column_header{fee} =
431       qq|<th class=listheading>|
432     . $locale->text('Total Fees')
433     . qq|</th>|;
434   $column_header{interest} =
435       qq|<th class=listheading>|
436     . $locale->text('Total Interest')
437     . qq|</th>|;
438
439   $form->header;
440
441
442   print qq|
443 <body>
444 <script type="text/javascript" src="js/common.js"></script>
445 <script type="text/javascript" src="js/dunning.js"></script>
446 <form name=Form method=post action=$form->{script}>
447
448
449 <table width=100%>
450   <tr>
451     <th class=listtop colspan=11>$form->{title}</th>
452   </tr>
453   <tr height="5"></tr>
454   <tr>|;
455   map { print "$column_header{$_}\n" } @column_index;
456
457   print qq|
458         </tr>
459 |;
460   my $i = 0;
461   foreach $ref (@{ $form->{DUNNINGS} }) {
462
463     $i++;
464     my $j = $i % 2;
465
466     print qq|
467         <tr valign=top class=listrow$j>
468 |;
469
470   $form->{selectdunning} =~ s/ selected//g;
471   if ($ref->{next_dunning_config_id} ne "") {
472      $form->{selectdunning} =~ s/value=$ref->{next_dunning_config_id}/value=$ref->{next_dunning_config_id} selected/;
473   }
474   
475
476   $dunning = qq|<select name=next_dunning_config_id_$i>$form->{selectdunning}</select>|;
477
478
479     $column_data{dunning_description}           = qq|<td><input type=hidden name=inv_id_$i size=2 value="$ref->{id}"><input type=hidden name=customer_id_$i size=2 value="$ref->{customer_id}">$ref->{dunning_level}:&nbsp;$dunning</td>|;
480     my $active = ($ref->{active}) ? "checked" : "";
481     $column_data{active} =
482       qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
483     my $email = ($ref->{email}) ? "checked" : "";
484   $column_data{email} =
485     qq|<td><input type=checkbox name=email_$i value=1 $email></td>|;
486     $column_data{next_duedate}           = qq|<td><input type=hidden name=next_duedate_$i size=6 value="$ref->{next_duedate}">$ref->{next_duedate}</td>|;
487
488     $column_data{inv_duedate}           = qq|<td><input type=hidden name=inv_duedate_$i size=6 value="$ref->{duedate}">$ref->{duedate}</td>|;
489     $column_data{invdate}           = qq|<td><input type=hidden name=invdate_$i size=6 value="$ref->{transdate}">$ref->{transdate}</td>|;
490     $column_data{invnumber}           = qq|<td><input type=hidden name=invnumber_$i size=6 value="$ref->{invnumber}">$ref->{invnumber}</td>|;
491     $column_data{customername}           = qq|<td><input type=hidden name=customername_$i size=6 value="$ref->{customername}">$ref->{customername}</td>|;
492     $column_data{invamount}           = qq|<td><input type=hidden name=invamount_$i size=6 value="$ref->{amount}">$ref->{amount}</td>|;
493     $column_data{fee}           = qq|<td><input type=hidden name=fee_$i size=5 value="$ref->{fee}">$ref->{fee}</td>|;
494     $column_data{interest}           = qq|<td><input type=hidden name=interest_$i size=4 value="$ref->{interest}">$ref->{interest}</td>|;
495
496
497
498     map { print "$column_data{$_}\n" } @column_index;
499
500     print qq|
501         </tr>
502 |;
503   }
504
505   $form->{rowcount} = $i;
506
507   print qq|
508       </table>
509     </td>
510   </tr>
511   <tr>
512   <td><hr size=3 noshade></td>
513   </tr>
514 </table>|;
515   print_options();
516   print qq|
517 <br>
518 <form method=post action=$form->{script}>
519
520 <input name=callback type=hidden value="$form->{callback}">
521 <input name=rowcount type=hidden value="$form->{rowcount}">
522 <input name=nextsub type=hidden value="$form->{nextsub}">
523 <input name=groupinvoices type=hidden value="$form->{groupinvoices}">
524
525
526 <input type=hidden name=path value=$form->{path}>
527 <input type=hidden name=login value=$form->{login}>
528 <input type=hidden name=password value=$form->{password}>
529 <input type="hidden" name="action">
530 <input type="submit" name="dummy" value="|
531     . $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()">
532
533   </form>
534
535   </body>
536   </html>
537 |;
538
539
540   $lxdebug->leave_sub();
541
542 }
543
544 sub save {
545   $lxdebug->enter_sub();
546
547   for my $i (1 .. $form->{rowcount}) {
548     if ($form->{"dunning_description_$i"} ne "") {
549       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
550       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
551       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
552       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
553     }
554   }
555
556   DN->save_config(\%myconfig, \%$form);
557   # saving the history
558   if(!exists $form->{addition} && $form->{id} ne "") {
559         $form->{addition} = "SAVED FOR DUNNING";
560         $form->save_history($form->dbconnect(\%myconfig));
561   }
562   # /saving the history 
563   $form->redirect($locale->text('Dunning Process Config saved!'));
564
565   $lxdebug->leave_sub();
566 }
567
568 sub save_dunning {
569   $lxdebug->enter_sub();
570
571   my $active=1;
572   my @rows = ();
573   undef($form->{DUNNING_PDFS});
574   if ($form->{groupinvoices}) {
575     while ($active) {
576       $lastcustomer = 0;
577       $form->{inv_ids} = [];
578       $active = 0;
579       @rows = ();
580       for my $i (1 .. $form->{rowcount}) {
581         $form->{"active_$i"} *= 1;
582         $lastcustomer = $form->{"customer_id_$i"} unless ($lastcustomer);
583         if ($form->{"active_$i"} && ($form->{"customer_id_$i"} == $lastcustomer)) {
584           push(@{ $form->{inv_ids} }, $form->{"inv_id_$i"});
585           $form->{"active_$i"} = 0;
586           $form->{"customer_id_$i"} = 0;
587           push(@rows, $i);
588         } elsif ($form->{"active_$i"}) {
589           $active = 1;
590         } else {
591           $form->{"customer_id_$i"} = 0;
592         }
593       }
594       if (scalar(@{ $form->{inv_ids} }) != 0) {
595         DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail);
596       }
597     }
598   } else {
599     for my $i (1 .. $form->{rowcount}) {
600       if ($form->{"active_$i"}) {
601         @rows = ();
602         $form->{inv_ids} = [ $form->{"inv_id_$i"} ];
603         push(@rows, $i);
604         DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail);
605       }
606     }
607   }
608   if($form->{DUNNING_PDFS}) {
609     DN->melt_pdfs(\%myconfig, \%$form,$spool);
610   }
611   # saving the history
612   if(!exists $form->{addition} && $form->{id} ne "") {
613         $form->{addition} = "DUNNING STARTED";
614         $form->save_history($form->dbconnect(\%myconfig));
615   }
616   # /saving the history 
617   $form->redirect($locale->text('Dunning Process started for selected invoices!'));
618
619   $lxdebug->leave_sub();
620 }
621   
622 sub set_email {
623   $lxdebug->enter_sub();
624
625
626   my $callback = "$form->{script}?action=set_email&";
627   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
628       (qw(login path password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
629
630   if ($form->{email_attachment}) {
631     $form->{email_attachment} = "checked";
632   }
633   $form->{"title"} = $locale->text("Set eMail text");
634   $form->header();
635   print($form->parse_html_template("dunning/set_email"));
636
637   $lxdebug->leave_sub();
638 }
639
640 sub search {
641   $lxdebug->enter_sub();
642   # setup customer selection
643   $form->all_vc(\%myconfig, "customer", "AR");
644
645   DN->get_config(\%myconfig, \%$form);
646
647   if (@{ $form->{all_customer} }) {
648     map { $customer .= "<option>$_->{name}--$_->{id}\n" }
649       @{ $form->{all_customer} };
650     $customer = qq|<select name=customer><option>\n$customer</select>|;
651   } else {
652     $customer = qq|<input name=customer size=35>|;
653   }
654
655   # dunning levels
656   if (@{ $form->{DUNNING} }) {
657     $form->{selectdunning_level} = "<option></option\n";
658     map {
659       $form->{selectdunning_level} .=
660         "<option value=$_->{id}>$_->{dunning_description}</option>\n"
661     } (@{ $form->{DUNNING} });
662   }
663   $dunning_level = qq| 
664     <tr> 
665     <th align=right nowrap>| . $locale->text('Next Dunning Level') . qq|</th>
666     <td colspan=3><select name=dunning_level>$form->{selectdunning_level}</select></td>
667     </tr>
668     | if $form->{selectdunning_level};
669
670   # departments
671   if (@{ $form->{all_departments} }) {
672     $form->{selectdepartment} = "<option>\n";
673     map {
674       $form->{selectdepartment} .=
675         "<option>$_->{description}--$_->{id}\n"
676     } (@{ $form->{all_departments} });
677   }
678   $department = qq| 
679     <tr> 
680     <th align=right nowrap>| . $locale->text('Department') . qq|</th>
681     <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
682     </tr>
683     | if $form->{selectdepartment};
684   $form->{title}   = $locale->text('Search Dunning');
685   $form->{nextsub} = "show_dunning";
686
687   # use JavaScript Calendar or not
688   $form->{jsscript} = $jscalendar;
689   $jsscript = "";
690   if ($form->{jsscript}) {
691
692     # with JavaScript Calendar
693     $button1 = qq|
694       <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
695       <input type=button name=transdatefrom id="trigger1" value=|
696       . $locale->text('button') . qq|></td>
697       |;
698     $button2 = qq|
699       <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
700       <input type=button name=transdateto id="trigger2" value=|
701       . $locale->text('button') . qq|></td>
702       |;
703     $button3 = qq|
704       <td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
705       <input type=button name=dunningfrom id="trigger3" value=|
706       . $locale->text('button') . qq|></td>
707       |;
708     $button4 = qq|
709       <td><input name=dunningto id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
710       <input type=button name=dunningto id="trigger4" value=|
711       . $locale->text('button') . qq|></td>
712       |;
713
714     #write Trigger
715     $jsscript =
716       Form->write_trigger(\%myconfig, "4", "transdatefrom", "BR", "trigger1", "transdateto", "BR", "trigger2", "dunningfrom", "BR", "trigger3", "dunningto", "BR", "trigger4");
717   } else {
718
719     # without JavaScript Calendar
720     $button1 =
721       qq|<td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
722     $button2 =
723       qq|<td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
724     $button3 =
725       qq|<td><input name=dunningfrom id=dunningfrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
726     $button4 =
727       qq|<td><input name=dunningfrom id=dunningto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
728   }
729
730   $form->{fokus} = "search.customer";
731   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
732   $form->header;
733   $onload = qq|focus()|;
734   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
735   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
736
737   print qq|
738 <body onLoad="$onload">
739
740 <form method=post name="search" action=$form->{script}>
741
742 <table width=100%>
743   <tr><th class=listtop>$form->{title}</th></tr>
744   <tr height="5"></tr>
745   <tr>
746     <td>
747       <table>
748         <tr>
749           <th align=right>| . $locale->text('Customer') . qq|</th>
750           <td colspan=3>$customer</td>
751         </tr>
752         $dunning_level
753         $department
754         <tr>
755           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
756           <td colspan=3><input name=invnumber size=20></td>
757         </tr>
758         <tr>
759           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
760           <td colspan=3><input name=ordnumber size=20></td>
761         </tr>
762         <tr>
763           <th align=right nowrap>| . $locale->text('Notes') . qq|</th>
764           <td colspan=3><input name=notes size=40></td>
765         </tr>
766         <tr>
767           <th align=right nowrap>| . $locale->text('Invdate from') . qq|</th>
768           $button1
769           <th align=right nowrap>| . $locale->text('To') . qq|</th>
770           $button2
771         </tr>
772         <tr>
773           <th align=right nowrap>| . $locale->text('Dunning Date from') . qq|</th>
774           $button3
775           <th align=right nowrap>| . $locale->text('To') . qq|</th>
776           $button4
777         </tr>
778
779       </table>
780     </td>
781   </tr>
782   <tr>
783     <td><hr size=3 noshade></td>
784   </tr>
785   <tr>
786     <td>
787       <table>
788         <tr>
789           <th align=right nowrap>| . $locale->text('Show old dunnings') . qq|</th>
790           <td><input type=checkbox value=1 name=showold></td>
791         </tr>
792       </table>
793     </td>
794   </tr>
795 </table>
796
797 <input type=hidden name=nextsub value=$form->{nextsub}>
798
799 <input type=hidden name=path value=$form->{path}>
800 <input type=hidden name=login value=$form->{login}>
801 <input type=hidden name=password value=$form->{password}>
802
803 <br>
804 <input class=submit type=submit name=action value="|
805     . $locale->text('Continue') . qq|">
806
807 </form>
808
809 </body>
810
811 $jsscript
812
813 </html>
814 |;
815
816   $lxdebug->leave_sub();
817
818 }
819
820 sub show_dunning {
821   $lxdebug->enter_sub();
822
823   DN->get_dunning(\%myconfig, \%$form);
824   $form->{title} = $locale->text('Dunning overview');
825
826
827
828
829   
830   $form->{callback} =
831     "$form->{script}?action=show_dunning&path=$form->{path}&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}"
832     unless $form->{callback};
833
834   @column_index = qw(dunning_description customername invnumber invdate inv_duedate invamount dunning_date next_duedate fee interest );
835
836   $column_header{dunning_description} =
837       qq|<th class=listheading>|
838     . $locale->text('Dunning Level')
839     . qq|</th>|;
840   $column_header{customername} =
841       qq|<th class=listheading>|
842     . $locale->text('Customername')
843     . qq|</th>|;
844   $column_header{invnumber} =
845       qq|<th class=listheading>|
846     . $locale->text('Invnumber')
847     . qq|</th>|;
848   $column_header{inv_duedate} =
849       qq|<th class=listheading>|
850     . $locale->text('Invoice Duedate')
851     . qq|</th>|;
852   $column_header{dunning_date} =
853       qq|<th class=listheading>|
854     . $locale->text('Dunning Date')
855     . qq|</th>|;
856   $column_header{next_duedate} =
857       qq|<th class=listheading>|
858     . $locale->text('Dunning Duedate')
859     . qq|</th>|;
860   $column_header{invdate} =
861       qq|<th class=listheading>|
862     . $locale->text('Invdate')
863     . qq|</th>|;
864   $column_header{invamount} =
865       qq|<th class=listheading>|
866     . $locale->text('Amount')
867     . qq|</th>|;
868   $column_header{fee} =
869       qq|<th class=listheading>|
870     . $locale->text('Total Fees')
871     . qq|</th>|;
872   $column_header{interest} =
873       qq|<th class=listheading>|
874     . $locale->text('Total Interest')
875     . qq|</th>|;
876
877   $form->header;
878
879
880   print qq|
881 <body>
882 <script type="text/javascript" src="js/common.js"></script>
883 <script type="text/javascript" src="js/dunning.js"></script>
884 <form method=post action=$form->{script}>
885
886
887 <table width=100%>
888   <tr>
889     <th class=listtop colspan=10>$form->{title}</th>
890   </tr>
891   <tr height="5"></tr>
892   <tr>|;
893   map { print "$column_header{$_}\n" } @column_index;
894
895   print qq|
896         </tr>
897 |;
898
899   my %columns = (
900     "dunning_duedate" => "next_duedate",
901     "duedate" => "inv_duedate",
902     "transdate" => "invdate",
903     "amount" => "invamount",
904     );
905
906   my $i = 0;
907   my $j = 0;
908   my ($previous_dunning_id, $first_row_for_dunning);
909   foreach $ref (@{ $form->{DUNNINGS} }) {
910     $i++;
911
912     if ($previous_dunning_id != $ref->{dunning_id}) {
913       $j++;
914       $j = $j % 2;
915       $first_row_for_dunning = 1;
916     } else {
917       $first_row_for_dunning = 0;
918     }
919     $previous_dunning_id = $ref->{dunning_id};
920
921     print qq|
922         <tr valign=top class=listrow$j>
923 |;
924
925   
926
927     foreach (qw(dunning_date dunning_duedate duedate transdate customername
928                 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 path 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 path 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=path value=$form->{path}>
977 <input type=hidden name=login value=$form->{login}>
978 <input type=hidden name=password value=$form->{password}>
979
980   </form>
981
982   </body>
983   </html>
984 |;
985
986
987   $lxdebug->leave_sub();
988
989 }
990
991 sub print_dunning {
992   $lxdebug->enter_sub();
993
994   DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
995
996   if($form->{DUNNING_PDFS}) {
997     DN->melt_pdfs(\%myconfig, \%$form,$spool);
998   } else {
999     $form->redirect($locale->text('Could not create dunning copy!'));
1000   }
1001
1002   $lxdebug->leave_sub();
1003
1004 }
1005
1006 # end of main
1007