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