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