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