report_generator.pl in reportgenerator.pl umbenannt.
[kivitendo-erp.git] / bin / mozilla / rp.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
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 #  Contributors: Antonio Gallardo <agssa@ibw.com.ni>
16 #                Benjamin Lee <benjaminlee@consultant.com>
17 #                Philip Reetz <p.reetz@linet-services.de>
18 #                Udo Spallek
19 #
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #======================================================================
33 #
34 # module for preparing Income Statement and Balance Sheet
35 #
36 #======================================================================
37
38 use POSIX qw(strftime);
39
40 use SL::PE;
41 use SL::RP;
42 use SL::USTVA;
43 use SL::Iconv;
44 use SL::ReportGenerator;
45
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
49
50 1;
51
52 # end of main
53
54 # this is for our long dates
55 # $locale->text('January')
56 # $locale->text('February')
57 # $locale->text('March')
58 # $locale->text('April')
59 # $locale->text('May ')
60 # $locale->text('June')
61 # $locale->text('July')
62 # $locale->text('August')
63 # $locale->text('September')
64 # $locale->text('October')
65 # $locale->text('November')
66 # $locale->text('December')
67
68 # this is for our short month
69 # $locale->text('Jan')
70 # $locale->text('Feb')
71 # $locale->text('Mar')
72 # $locale->text('Apr')
73 # $locale->text('May')
74 # $locale->text('Jun')
75 # $locale->text('Jul')
76 # $locale->text('Aug')
77 # $locale->text('Sep')
78 # $locale->text('Oct')
79 # $locale->text('Nov')
80 # $locale->text('Dec')
81
82 # $locale->text('Balance Sheet')
83 # $locale->text('Income Statement')
84 # $locale->text('Trial Balance')
85 # $locale->text('AR Aging')
86 # $locale->text('AP Aging')
87 # $locale->text('Tax collected')
88 # $locale->text('Tax paid')
89 # $locale->text('Receipts')
90 # $locale->text('Payments')
91 # $locale->text('Project Transactions')
92 # $locale->text('Non-taxable Sales')
93 # $locale->text('Non-taxable Purchases')
94
95 sub report {
96   $lxdebug->enter_sub();
97
98   %title = ('balance_sheet'        => 'Balance Sheet',
99             'income_statement'     => 'Income Statement',
100             'trial_balance'        => 'Trial Balance',
101             'ar_aging'             => 'AR Aging',
102             'ap_aging'             => 'Offene Verbindlichkeiten',
103             'tax_collected'        => 'Tax collected',
104             'tax_paid'             => 'Tax paid',
105             'nontaxable_sales'     => 'Non-taxable Sales',
106             'nontaxable_purchases' => 'Non-taxable Purchases',
107             'receipts'             => 'Receipts',
108             'payments'             => 'Payments',
109             'projects'             => 'Project Transactions',
110             'bwa'                  => 'Betriebswirtschaftliche Auswertung',
111             'ustva'                => 'Umsatzsteuervoranmeldung',);
112
113   $form->{title} = $locale->text($title{ $form->{report} });
114
115   $accrual = ($eur) ? ""        : "checked";
116   $cash    = ($eur) ? "checked" : "";
117
118   $year = (localtime)[5] + 1900;
119
120   # get departments
121   $form->all_departments(\%myconfig);
122   if (@{ $form->{all_departments} }) {
123     $form->{selectdepartment} = "<option>\n";
124
125     map {
126       $form->{selectdepartment} .=
127         "<option>$_->{description}--$_->{id}\n"
128     } (@{ $form->{all_departments} });
129   }
130
131   $department = qq|
132         <tr>
133           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
134           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
135         </tr>
136 | if $form->{selectdepartment};
137
138   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
139                                    "all" => 1 });
140
141   my %project_labels = ();
142   my @project_values = ("");
143   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
144     push(@project_values, $item->{"id"});
145     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
146   }
147
148   my $projectnumber =
149     NTI($cgi->popup_menu('-name' => "project_id",
150                          '-values' => \@project_values,
151                          '-labels' => \%project_labels));
152
153   # use JavaScript Calendar or not
154   $form->{jsscript} = 1;
155   $jsscript = "";
156   if ($form->{report} eq "ustva") {
157     $department = "";
158   } else {
159     if ($form->{report} eq "balance_sheet") {
160       $name_1    = "asofdate";
161       $id_1      = "asofdate";
162       $value_1   = "$form->{asofdate}";
163       $trigger_1 = "trigger1";
164       $name_2    = "compareasofdate";
165       $id_2      = "compareasofdate";
166       $value_2   = "$form->{compareasofdate}";
167       $trigger_2 = "trigger2";
168     } elsif ($form->{report} =~ /(receipts|payments)$/) {
169       $name_1    = "fromdate";
170       $id_1      = "fromdate";
171       $value_1   = "$form->{fromdate}";
172       $trigger_1 = "trigger1";
173       $name_2    = "todate";
174       $id_2      = "todate";
175       $value_2   = "";
176       $trigger_2 = "trigger2";
177     } else {
178       if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
179         $name_1    = "";
180         $id_1      = "";
181         $value_1   = "";
182         $trigger_1 = "";
183         $name_2    = "todate";
184         $id_2      = "todate";
185         $value_2   = "";
186         $trigger_2 = "trigger2";
187
188       } else {
189         $name_1    = "fromdate";
190         $id_1      = "fromdate";
191         $value_1   = "$form->{fromdate}";
192         $trigger_1 = "trigger1";
193         $name_2    = "todate";
194         $id_2      = "todate";
195         $value_2   = "";
196         $trigger_2 = "trigger2";
197       }
198     }
199   }
200
201   # with JavaScript Calendar
202   if ($form->{jsscript}) {
203     if ($name_1 eq "") {
204
205       $button1 = qq|
206          <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
207       $button1_2 = qq|
208         <input type=button name=$name_2 id="$trigger_2" value=|
209         . $locale->text('button') . qq|>|;
210
211       #write Trigger
212       $jsscript =
213         Form->write_trigger(\%myconfig, "1", "$name_2", "BR", "$trigger_2");
214     } else {
215       $button1 = qq|
216          <input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value="$value_1" onBlur=\"check_right_date_format(this)\">|;
217       $button1_2 = qq|
218         <input type=button name=$name_1 id="$trigger_1" value=|
219         . $locale->text('button') . qq|>|;
220       $button2 = qq|
221          <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
222       $button2_2 = qq|
223          <input type=button name=$name_2 id="$trigger_2" value=|
224         . $locale->text('button') . qq|>
225        |;
226
227       #write Trigger
228       $jsscript =
229         Form->write_trigger(\%myconfig, "2", "$name_1", "BR", "$trigger_1",
230                             "$name_2", "BL", "$trigger_2");
231     }
232   } else {
233
234     # without JavaScript Calendar
235     if ($name_1 eq "") {
236       $button1 =
237         qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
238     } else {
239       $button1 =
240         qq|<input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value=$value_1 onBlur=\"check_right_date_format(this)\">|;
241       $button2 =
242         qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
243     }
244   }
245   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
246   $form->header;
247   $onload = qq|focus()|;
248   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
249   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
250   print qq|
251 <body onLoad="$onload">
252
253 <form method=post action=$form->{script}>
254
255 <input type=hidden name=title value="$form->{title}">
256
257 <table width=100%>
258   <tr>
259     <th class=listtop>$form->{title}</th>
260   </tr>
261   <tr height="5"></tr>
262   <tr>
263     <td>
264       <table>
265       $department
266 |;
267
268   if ($form->{report} eq "projects") {
269     print qq|
270         <tr>
271           <th align=right nowrap>| . $locale->text('Project') . qq|</th>
272           <td colspan=5><input name=projectnumber size=25</td>
273         </tr>
274         <input type=hidden name=nextsub value=generate_projects>
275         <tr>
276           <th align=right>| . $locale->text('From') . qq|</th>
277           <td>$button1</td>
278           <td>$button1_2</td>
279           <th align=right>| . $locale->text('Bis') . qq|</th>
280           <td>$button2</td>
281           <td>$button2_2</td>
282         </tr>
283       </table>
284     </td>
285   </tr>
286   <tr>
287     <td>
288       <table>
289         <tr>
290           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
291           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
292       . $locale->text('Heading') . qq|
293           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
294       . $locale->text('Subtotal') . qq|</td>
295         </tr>
296
297 $jsscript
298 |;
299   }
300
301   if ($form->{report} eq "income_statement") {
302     print qq|
303         <tr>
304           <th align=right nowrap>| . $locale->text('Project') . qq|</th>
305           <td colspan=3>$projectnumber</td>
306         </tr>
307         <input type=hidden name=nextsub value=generate_income_statement>
308 </table>
309 <table>
310         <tr>
311           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
312       . $locale->text('Customized Report') . qq|</th>
313         </tr>
314         <tr>
315           <th colspan=1>| . $locale->text('Year') . qq|</th>
316           <td><input name=year size=11 title="|
317       . $locale->text('YYYY') . qq|" value="$year"></td>
318         </tr>
319 |;
320
321     print qq|
322         <tr>
323                 <td align=right>
324 <b> | . $locale->text('Yearly') . qq|</b> </td>
325                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
326                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
327         </tr>
328         <tr>
329                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
330 "checked"></td>
331                 <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. |
332       . $locale->text('Quarter') . qq|</td>
333 |;
334     $checked = "";
335     print qq|
336                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;|
337       . $locale->text('January') . qq|</td>
338 |;
339     $checked = "";
340     print qq|
341                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;|
342       . $locale->text('May') . qq|</td>
343                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;|
344       . $locale->text('September') . qq|</td>
345
346         </tr>
347         <tr>
348                 <td align= right>&nbsp;</td>
349                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. |
350       . $locale->text('Quarter') . qq|</td>
351                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;|
352       . $locale->text('February') . qq|</td>
353                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;|
354       . $locale->text('June') . qq|</td>
355                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;|
356       . $locale->text('October') . qq|</td>
357         </tr>
358         <tr>
359                 <td> &nbsp;</td>
360                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. |
361       . $locale->text('Quarter') . qq|</td>
362                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;|
363       . $locale->text('March') . qq|</td>
364                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;|
365       . $locale->text('July') . qq|</td>
366                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;|
367       . $locale->text('November') . qq|</td>
368
369         </tr>
370         <tr>
371                 <td> &nbsp;</td>
372                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. |
373       . $locale->text('Quarter') . qq|&nbsp;</td>
374                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;|
375       . $locale->text('April') . qq|</td>
376                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;|
377       . $locale->text('August') . qq|</td>
378                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;|
379       . $locale->text('December') . qq|</td>
380
381         </tr>
382         <tr>
383                 <td colspan=5><hr size=3 noshade></td>
384         </tr>
385         <tr>
386           <th align=left><input name=reporttype class=radio type=radio value="free" $checked> |
387       . $locale->text('Free report period') . qq|</th>
388           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
389               $button1
390               $button1_2&nbsp;
391               | . $locale->text('Bis') . qq|
392               $button2
393               $button2_2&nbsp;
394           </td>
395         </tr>
396         <tr>
397                 <td colspan=5><hr size=3 noshade></td>
398         </tr>
399         <tr>
400           <th align=leftt>| . $locale->text('Method') . qq|</th>
401           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
402       . $locale->text('Accrual') . qq|
403           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
404       . $locale->text('EUR') . qq|</td>
405         </tr>
406
407 $jsscript
408 |;
409   }
410
411   if ($form->{report} eq "bwa") {
412     print qq|
413         <tr>
414           <th align=right nowrap>| . $locale->text('Project') . qq|</th>
415           <td colspan=3>$projectnumber</td>
416         </tr>
417         <input type=hidden name=nextsub value=generate_bwa>
418 </table>
419 <table>
420         <tr>
421           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
422       . $locale->text('Customized Report') . qq|</th>
423         </tr>
424         <tr>
425           <th colspan=1>| . $locale->text('Year') . qq|</th>
426           <td><input name=year size=11 title="|
427       . $locale->text('YYYY') . qq|" value="$year"></td>
428         </tr>
429 |;
430
431     print qq|
432         <tr>
433                 <td align=right>
434 <b> | . $locale->text('Yearly') . qq|</b> </td>
435                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
436                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
437         </tr>
438         <tr>
439                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
440 $checked></td>
441                 <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. |
442       . $locale->text('Quarter') . qq|</td>
443 |;
444     $checked = "checked";
445     print qq|
446                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;|
447       . $locale->text('January') . qq|</td>
448 |;
449     $checked = "";
450     print qq|
451                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;|
452       . $locale->text('May') . qq|</td>
453                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;|
454       . $locale->text('September') . qq|</td>
455
456         </tr>
457         <tr>
458                 <td align= right>&nbsp;</td>
459                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. |
460       . $locale->text('Quarter') . qq|</td>
461                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;|
462       . $locale->text('February') . qq|</td>
463                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;|
464       . $locale->text('June') . qq|</td>
465                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;|
466       . $locale->text('October') . qq|</td>
467         </tr>
468         <tr>
469                 <td> &nbsp;</td>
470                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. |
471       . $locale->text('Quarter') . qq|</td>
472                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;|
473       . $locale->text('March') . qq|</td>
474                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;|
475       . $locale->text('July') . qq|</td>
476                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;|
477       . $locale->text('November') . qq|</td>
478
479         </tr>
480         <tr>
481                 <td> &nbsp;</td>
482                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. |
483       . $locale->text('Quarter') . qq|&nbsp;</td>
484                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;|
485       . $locale->text('April') . qq|</td>
486                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;|
487       . $locale->text('August') . qq|</td>
488                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;|
489       . $locale->text('December') . qq|</td>
490
491         </tr>
492         <tr>
493                 <td colspan=5><hr size=3 noshade></td>
494         </tr>
495         <tr>
496           <th align=left><input name=reporttype class=radio type=radio value="free" $checked> |
497       . $locale->text('Free report period') . qq|</th>
498           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
499               $button1
500               $button1_2&nbsp;
501               | . $locale->text('Bis') . qq|&nbsp;
502               $button2
503               $button2_2
504           </td>
505         </tr>
506         <tr>
507                 <td colspan=5><hr size=3 noshade></td>
508         </tr>
509         <tr>
510           <th align=leftt>| . $locale->text('Method') . qq|</th>
511           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
512       . $locale->text('Accrual') . qq|
513           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
514       . $locale->text('EUR') . qq|</td>
515         </tr>
516         <tr>
517          <th align=right colspan=4>|
518       . $locale->text('Decimalplaces')
519       . qq|</th>
520              <td><input name=decimalplaces size=3 value="2"></td>
521          </tr>
522                                     
523 $jsscript
524 |;
525   }
526
527   if ($form->{report} eq "ustva") {
528
529     print qq|
530
531         <br>
532         <input type=hidden name=nextsub value=generate_ustva>
533 </table>
534 <table>
535         <tr>
536           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
537       . $locale->text('Zeitraum') . qq|</th>
538         </tr>
539         <tr>
540           <th colspan=1>| . $locale->text('Year') . qq|</th>
541           <td><input name=year size=11 title="|
542       . $locale->text('YYYY') . qq|" value="$year"></td>
543         </tr>
544 |;
545
546     print qq|
547         <tr>
548                 <td align=right>
549 <b> | . $locale->text('Yearly') . qq|</b> </td>
550                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
551                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
552         </tr>
553         <tr>
554                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
555 $checked></td>
556                 <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. |
557       . $locale->text('Quarter') . qq|</td>
558 |;
559     $checked = "checked";
560     print qq|
561                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;|
562       . $locale->text('January') . qq|</td>
563 |;
564     $checked = "";
565     print qq|
566                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;|
567       . $locale->text('May') . qq|</td>
568                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;|
569       . $locale->text('September') . qq|</td>
570
571         </tr>
572         <tr>
573                 <td align= right>&nbsp;</td>
574                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. |
575       . $locale->text('Quarter') . qq|</td>
576                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;|
577       . $locale->text('February') . qq|</td>
578                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;|
579       . $locale->text('June') . qq|</td>
580                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;|
581       . $locale->text('October') . qq|</td>
582         </tr>
583         <tr>
584                 <td> &nbsp;</td>
585                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. |
586       . $locale->text('Quarter') . qq|</td>
587                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;|
588       . $locale->text('March') . qq|</td>
589                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;|
590       . $locale->text('July') . qq|</td>
591                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;|
592       . $locale->text('November') . qq|</td>
593
594         </tr>
595         <tr>
596                 <td> &nbsp;</td>
597                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. |
598       . $locale->text('Quarter') . qq|&nbsp;</td>
599                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;|
600       . $locale->text('April') . qq|</td>
601                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;|
602       . $locale->text('August') . qq|</td>
603                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;|
604       . $locale->text('December') . qq|</td>
605
606         </tr>
607         <tr>
608                 <td colspan=5><hr size=3 noshade></td>
609         </tr>
610         <tr>
611           <th align=left>| . $locale->text('Method') . qq|</th>
612           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
613       . $locale->text('Accrual') . qq|
614           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
615       . $locale->text('EUR') . qq|</td>
616         </tr>
617         <tr>
618           <th colspan=4>|;
619 ##########
620
621     &print_options();
622     print qq|
623           </th>
624         </tr>
625 |;
626   }
627
628   if ($form->{report} eq "balance_sheet") {
629     print qq|
630         <input type=hidden name=nextsub value=generate_balance_sheet>
631         <tr>
632           <th align=right>| . $locale->text('as at') . qq|</th>
633           <td>
634             $button1
635             $button1_2
636           </td>
637           <th align=right nowrap>| . $locale->text('Compare to') . qq|</th>
638           <td>
639           $button2
640           $button2_2
641           </td>
642         </tr>
643         <tr>
644           <th align=right>| . $locale->text('Decimalplaces') . qq|</th>
645           <td><input name=decimalplaces size=3 value="2"></td>
646         </tr>
647       </table>
648     </td>
649   </tr>
650   <tr>
651     <td>
652       <table>
653         <tr>
654           <th align=right>| . $locale->text('Method') . qq|</th>
655           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
656       . $locale->text('Accrual') . qq|
657           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
658       . $locale->text('EUR') . qq|</td>
659         </tr>
660
661         <tr>
662           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
663           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
664       . $locale->text('Heading') . qq|
665           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
666       . $locale->text('Subtotal') . qq|
667           <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|
668       . $locale->text('Account Number') . qq|</td>
669         </tr>
670
671 $jsscript
672 |;
673   }
674
675   if ($form->{report} eq "trial_balance") {
676     print qq|
677         <input type=hidden name=nextsub value=generate_trial_balance>
678         <input type=hidden name=eur value=$eur>
679        <tr>
680           <th align=right>| . $locale->text('From') . qq|</th>
681           <td>
682             $button1
683             $button1_2
684           </td>
685           <th align=right>| . $locale->text('Bis') . qq|</th>
686           <td>
687             $button2
688             $button2_2
689           </td>
690         </tr>
691       </table>
692     </td>
693   </tr>
694   <tr>
695     <td>
696       <table>
697         <tr>
698           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
699           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
700       . $locale->text('Heading') . qq|
701           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
702       . $locale->text('Subtotal') . qq|
703           <input name=all_accounts class=checkbox type=checkbox value=Y>&nbsp;|
704       . $locale->text('All Accounts') . qq|</td>
705         </tr>
706
707 $jsscript
708 |;
709   }
710
711   if ($form->{report} =~ /^tax_/) {
712     $form->{db} = ($form->{report} =~ /_collected/) ? "ar" : "ap";
713
714     RP->get_taxaccounts(\%myconfig, \%$form);
715
716     print qq|
717         <input type=hidden name=nextsub value=generate_tax_report>
718         <tr>
719           <th align=right>| . $locale->text('From') . qq|</th>
720           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
721           <th align=right>| . $locale->text('Bis') . qq|</th>
722           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
723         </tr>
724         <tr>
725           <th align=right>| . $locale->text('Report for') . qq|</th>
726           <td colspan=3>
727 |;
728
729     $checked = "checked";
730     foreach $ref (@{ $form->{taxaccounts} }) {
731
732       print
733         qq|<input name=accno class=radio type=radio value=$ref->{accno} $checked>&nbsp;$ref->{description}
734
735     <input name="$ref->{accno}_description" type=hidden value="$ref->{description}">
736     <input name="$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
737
738       $checked = "";
739
740     }
741
742     print qq|
743   <input type=hidden name=db value=$form->{db}>
744   <input type=hidden name=sort value=transdate>
745
746           </td>
747         </tr>
748         <tr>
749           <th align=right>| . $locale->text('Method') . qq|</th>
750           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
751       . $locale->text('Accrual') . qq|
752           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
753       . $locale->text('EUR') . qq|</td>
754         </tr>
755       </table>
756     </td>
757   </tr>
758   <tr>
759     <td>
760       <table>
761         <tr>
762           <th align=right>| . $locale->text('Include in Report') . qq|</th>
763           <td>
764             <table>
765               <tr>
766                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
767                 <td>| . $locale->text('ID') . qq|</td>
768                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
769                 <td>| . $locale->text('Invoice') . qq|</td>
770                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
771                 <td>| . $locale->text('Date') . qq|</td>
772               </tr>
773               <tr>
774                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
775                 <td>|;
776
777     if ($form->{db} eq 'ar') {
778       print $locale->text('Customer');
779     }
780     if ($form->{db} eq 'ap') {
781       print $locale->text('Vendor');
782     }
783
784     print qq|</td>
785                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
786                 <td>| . $locale->text('Amount') . qq|</td>
787                 <td><input name="l_tax" class=checkbox type=checkbox value=Y checked></td>
788                 <td>| . $locale->text('Tax') . qq|</td>
789                 <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
790                 <td>| . $locale->text('Total') . qq|</td>
791               </tr>
792               <tr>
793                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
794                 <td>| . $locale->text('Subtotal') . qq|</td>
795               </tr>
796             </table>
797           </td>
798         </tr>
799 |;
800
801   }
802
803   if ($form->{report} =~ /^nontaxable_/) {
804     $form->{db} = ($form->{report} =~ /_sales/) ? "ar" : "ap";
805
806     print qq|
807         <input type=hidden name=nextsub value=generate_tax_report>
808
809         <input type=hidden name=db value=$form->{db}>
810         <input type=hidden name=sort value=transdate>
811         <input type=hidden name=report value=$form->{report}>
812
813         <tr>
814           <th align=right>| . $locale->text('From') . qq|</th>
815           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
816           <th align=right>| . $locale->text('Bis') . qq|</th>
817           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
818         </tr>
819         <tr>
820           <th align=right>| . $locale->text('Method') . qq|</th>
821           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
822       . $locale->text('Accrual') . qq|
823           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
824       . $locale->text('EUR') . qq|</td>
825         </tr>
826         <tr>
827           <th align=right>| . $locale->text('Include in Report') . qq|</th>
828           <td colspan=3>
829             <table>
830               <tr>
831                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
832                 <td>| . $locale->text('ID') . qq|</td>
833                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
834                 <td>| . $locale->text('Invoice') . qq|</td>
835                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
836                 <td>| . $locale->text('Date') . qq|</td>
837               </tr>
838               <tr>
839                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
840                 <td>|;
841
842     if ($form->{db} eq 'ar') {
843       print $locale->text('Customer');
844     }
845     if ($form->{db} eq 'ap') {
846       print $locale->text('Vendor');
847     }
848
849     print qq|</td>
850                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
851                 <td>| . $locale->text('Amount') . qq|</td>
852                 <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
853                 <td>| . $locale->text('Total') . qq|</td>
854               </tr>
855               <tr>
856                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
857                 <td>| . $locale->text('Subtotal') . qq|</td>
858               </tr>
859             </table>
860           </td>
861         </tr>
862 |;
863
864   }
865
866   if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
867     if ($form->{report} eq 'ar_aging') {
868       $label = $locale->text('Customer');
869       $form->{vc} = 'customer';
870     } else {
871       $label = $locale->text('Vendor');
872       $form->{vc} = 'vendor';
873     }
874
875     $nextsub = "generate_$form->{report}";
876
877     # setup vc selection
878     $form->all_vc(\%myconfig, $form->{vc},
879                   ($form->{vc} eq 'customer') ? "AR" : "AP");
880
881     map { $vc .= "<option>$_->{name}--$_->{id}\n" }
882       @{ $form->{"all_$form->{vc}"} };
883
884     $vc =
885       ($vc)
886       ? qq|<select name=$form->{vc}><option>\n$vc</select>|
887       : qq|<input name=$form->{vc} size=35>|;
888
889     print qq|
890         <tr>
891           <th align=right>| . $locale->text($label) . qq|</th>
892           <td>$vc</td>
893         </tr>
894         <tr>
895           <th align=right>| . $locale->text('Bis') . qq|</th>
896           <td>
897             $button1
898             $button1_2
899           </td>
900         </tr>
901         <input type=hidden name=type value=statement>
902         <input type=hidden name=format value=html>
903         <input type=hidden name=media value=screen>
904
905         <input type=hidden name=nextsub value=$nextsub>
906         <input type=hidden name=action value=$nextsub>
907
908 $jsscript
909 |;
910   }
911
912   # above action can be removed if there is more than one input field
913
914   if ($form->{report} =~ /(receipts|payments)$/) {
915     $form->{db} = ($form->{report} =~ /payments$/) ? "ap" : "ar";
916
917     RP->paymentaccounts(\%myconfig, \%$form);
918
919     $selection = "<option>\n";
920     foreach $ref (@{ $form->{PR} }) {
921       $paymentaccounts .= "$ref->{accno} ";
922       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
923     }
924
925     chop $paymentaccounts;
926
927     print qq|
928         <input type=hidden name=nextsub value=list_payments>
929         <tr>
930           <th align=right nowrap>| . $locale->text('Account') . qq|</th>
931           <td colspan=3><select name=account>$selection</select>
932             <input type=hidden name=paymentaccounts value="$paymentaccounts">
933           </td>
934         </tr>
935         <tr>
936           <th align=right>| . $locale->text('Reference') . qq|</th>
937           <td colspan=3><input name=reference></td>
938         </tr>
939         <tr>
940           <th align=right nowrap>| . $locale->text('Source') . qq|</th>
941           <td colspan=3><input name=source></td>
942         </tr>
943         <tr>
944           <th align=right nowrap>| . $locale->text('Memo') . qq|</th>
945           <td colspan=3><input name=memo size=30></td>
946         </tr>
947         <tr>
948           <th align=right>| . $locale->text('From') . qq|</th>
949           <td>
950             $button1
951             $button1_2
952           </td>
953           <th align=right>| . $locale->text('Bis') . qq|</th>
954           <td>
955             $button2
956             $button2_2
957           </td>
958         </tr>
959         <tr>
960           <td align=right><input type=checkbox style=checkbox name=fx_transaction value=1 checked></td>
961           <th align=left colspan=3>|
962       . $locale->text('Include Exchangerate Difference') . qq|</td>
963         </tr>
964
965 $jsscript
966
967           <input type=hidden name=db value=$form->{db}>
968           <input type=hidden name=sort value=transdate>
969 |;
970
971   }
972
973   print qq|
974
975       </table>
976     </td>
977   </tr>
978   <tr>
979     <td><hr size=3 noshade></td>
980   </tr>
981 </table>
982
983 <br>
984 <input type=hidden name=login value=$form->{login}>
985 <input type=hidden name=password value=$form->{password}>
986
987 <input type=submit class=submit name=action value="|
988     . $locale->text('Continue') . qq|">
989 |;
990
991   # Hier Aufruf von get_config zum Einlesen der Finanzamtdaten
992   USTVA->get_config($userspath, 'finanzamt.ini');
993
994   $disabled = qq|disabled="disabled"|;
995   $disabled = '' if ($form->{elster} eq '1');
996   if ($form->{report} eq 'ustva') {
997     print qq|
998   <input type=submit class=submit name=action value="|
999       . $locale->text('debug') . qq|">
1000   <input type=submit class=submit name=action $disabled
1001    value="| . $locale->text('winston_export') . qq|">
1002   |;
1003     print qq|
1004    <input type=submit class=submit name=action value="|
1005       . $locale->text('config') . qq|">
1006   |;
1007   }
1008
1009   print qq|
1010 </form>
1011
1012 </body>
1013 </html>
1014 |;
1015
1016   $lxdebug->leave_sub();
1017 }
1018
1019 sub continue { call_sub($form->{"nextsub"}); }
1020
1021 sub get_project {
1022   $lxdebug->enter_sub();
1023   my $nextsub = shift;
1024
1025   $form->{project_id} = $form->{project_id_1};
1026   if ($form->{projectnumber} && !$form->{project_id}) {
1027     $form->{rowcount} = 1;
1028
1029     # call this instead of update
1030     $form->{update}          = $nextsub;
1031     $form->{projectnumber_1} = $form->{projectnumber};
1032
1033     delete $form->{sort};
1034     &check_project;
1035
1036     # if there is one only, assign id
1037     $form->{project_id} = $form->{project_id_1};
1038   }
1039
1040   $lxdebug->leave_sub();
1041 }
1042
1043 sub generate_income_statement {
1044   $lxdebug->enter_sub();
1045
1046   $form->{padding} = "&nbsp;&nbsp;";
1047   $form->{bold}    = "<b>";
1048   $form->{endbold} = "</b>";
1049   $form->{br}      = "<br>";
1050
1051   if ($form->{reporttype} eq "custom") {
1052
1053     #forgotten the year --> thisyear
1054     if ($form->{year} !~ m/^\d\d\d\d$/) {
1055       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1056         /(\d\d\d\d)/;
1057       $form->{year} = $1;
1058     }
1059
1060     #yearly report
1061     if ($form->{duetyp} eq "13") {
1062       $form->{fromdate} = "1.1.$form->{year}";
1063       $form->{todate}   = "31.12.$form->{year}";
1064     }
1065
1066     #Quater reports
1067     if ($form->{duetyp} eq "A") {
1068       $form->{fromdate} = "1.1.$form->{year}";
1069       $form->{todate}   = "31.3.$form->{year}";
1070     }
1071     if ($form->{duetyp} eq "B") {
1072       $form->{fromdate} = "1.4.$form->{year}";
1073       $form->{todate}   = "30.6.$form->{year}";
1074     }
1075     if ($form->{duetyp} eq "C") {
1076       $form->{fromdate} = "1.7.$form->{year}";
1077       $form->{todate}   = "30.9.$form->{year}";
1078     }
1079     if ($form->{duetyp} eq "D") {
1080       $form->{fromdate} = "1.10.$form->{year}";
1081       $form->{todate}   = "31.12.$form->{year}";
1082     }
1083
1084     #Monthly reports
1085   SWITCH: {
1086       $form->{duetyp} eq "1" && do {
1087         $form->{fromdate} = "1.1.$form->{year}";
1088         $form->{todate}   = "31.1.$form->{year}";
1089         last SWITCH;
1090       };
1091       $form->{duetyp} eq "2" && do {
1092         $form->{fromdate} = "1.2.$form->{year}";
1093
1094         #this works from 1901 to 2099, 1900 and 2100 fail.
1095         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1096         $form->{todate} = "$leap.2.$form->{year}";
1097         last SWITCH;
1098       };
1099       $form->{duetyp} eq "3" && do {
1100         $form->{fromdate} = "1.3.$form->{year}";
1101         $form->{todate}   = "31.3.$form->{year}";
1102         last SWITCH;
1103       };
1104       $form->{duetyp} eq "4" && do {
1105         $form->{fromdate} = "1.4.$form->{year}";
1106         $form->{todate}   = "30.4.$form->{year}";
1107         last SWITCH;
1108       };
1109       $form->{duetyp} eq "5" && do {
1110         $form->{fromdate} = "1.5.$form->{year}";
1111         $form->{todate}   = "31.5.$form->{year}";
1112         last SWITCH;
1113       };
1114       $form->{duetyp} eq "6" && do {
1115         $form->{fromdate} = "1.6.$form->{year}";
1116         $form->{todate}   = "30.6.$form->{year}";
1117         last SWITCH;
1118       };
1119       $form->{duetyp} eq "7" && do {
1120         $form->{fromdate} = "1.7.$form->{year}";
1121         $form->{todate}   = "31.7.$form->{year}";
1122         last SWITCH;
1123       };
1124       $form->{duetyp} eq "8" && do {
1125         $form->{fromdate} = "1.8.$form->{year}";
1126         $form->{todate}   = "31.8.$form->{year}";
1127         last SWITCH;
1128       };
1129       $form->{duetyp} eq "9" && do {
1130         $form->{fromdate} = "1.9.$form->{year}";
1131         $form->{todate}   = "30.9.$form->{year}";
1132         last SWITCH;
1133       };
1134       $form->{duetyp} eq "10" && do {
1135         $form->{fromdate} = "1.10.$form->{year}";
1136         $form->{todate}   = "31.10.$form->{year}";
1137         last SWITCH;
1138       };
1139       $form->{duetyp} eq "11" && do {
1140         $form->{fromdate} = "1.11.$form->{year}";
1141         $form->{todate}   = "30.11.$form->{year}";
1142         last SWITCH;
1143       };
1144       $form->{duetyp} eq "12" && do {
1145         $form->{fromdate} = "1.12.$form->{year}";
1146         $form->{todate}   = "31.12.$form->{year}";
1147         last SWITCH;
1148       };
1149     }
1150   }
1151
1152   RP->income_statement(\%myconfig, \%$form);
1153
1154   ($form->{department}) = split /--/, $form->{department};
1155
1156   $form->{period} =
1157     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1158   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1159
1160   # if there are any dates construct a where
1161   if ($form->{fromdate} || $form->{todate}) {
1162
1163     unless ($form->{todate}) {
1164       $form->{todate} = $form->current_date(\%myconfig);
1165     }
1166
1167     $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
1168     $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
1169
1170     $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
1171     $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
1172
1173     $form->{this_period} = "$shortfromdate\n$shorttodate";
1174     $form->{period}      =
1175         $locale->text('for Period')
1176       . qq|\n$longfromdate |
1177       . $locale->text('Bis')
1178       . qq| $longtodate|;
1179   }
1180
1181   if ($form->{comparefromdate} || $form->{comparetodate}) {
1182     $longcomparefromdate =
1183       $locale->date(\%myconfig, $form->{comparefromdate}, 1);
1184     $shortcomparefromdate =
1185       $locale->date(\%myconfig, $form->{comparefromdate}, 0);
1186
1187     $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
1188     $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
1189
1190     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
1191     $form->{period} .=
1192         "\n$longcomparefromdate "
1193       . $locale->text('Bis')
1194       . qq| $longcomparetodate|;
1195   }
1196
1197   # setup variables for the form
1198   @a = qw(company address businessnumber);
1199   map { $form->{$_} = $myconfig{$_} } @a;
1200
1201   $form->{templates} = $myconfig{templates};
1202
1203   $form->{IN} = "income_statement.html";
1204
1205   $form->parse_template;
1206
1207   $lxdebug->leave_sub();
1208 }
1209
1210 sub generate_balance_sheet {
1211   $lxdebug->enter_sub();
1212
1213   $form->{padding} = "&nbsp;&nbsp;";
1214   $form->{bold}    = "<b>";
1215   $form->{endbold} = "</b>";
1216   $form->{br}      = "<br>";
1217
1218   RP->balance_sheet(\%myconfig, \%$form);
1219
1220   $form->{asofdate} = $form->current_date(\%myconfig) unless $form->{asofdate};
1221   $form->{period} =
1222     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1223
1224   ($form->{department}) = split /--/, $form->{department};
1225
1226   # define Current Earnings account
1227   $padding = ($form->{l_heading}) ? $form->{padding} : "";
1228   push(@{ $form->{equity_account} },
1229        $padding . $locale->text('Current Earnings'));
1230
1231   $form->{this_period} = $locale->date(\%myconfig, $form->{asofdate}, 0);
1232   $form->{last_period} =
1233     $locale->date(\%myconfig, $form->{compareasofdate}, 0);
1234
1235   $form->{IN} = "balance_sheet.html";
1236
1237   # setup company variables for the form
1238   map { $form->{$_} = $myconfig{$_};
1239         $form->{$_} =~ s/\\n/\n/g; }
1240     (qw(company address businessnumber nativecurr));
1241
1242   $form->{templates} = $myconfig{templates};
1243
1244   $form->parse_template;
1245
1246   $lxdebug->leave_sub();
1247 }
1248
1249 sub generate_projects {
1250   $lxdebug->enter_sub();
1251
1252   &get_project(generate_projects);
1253   $form->{projectnumber} = $form->{projectnumber_1};
1254
1255   $form->{nextsub} = "generate_projects";
1256   $form->{title}   = $locale->text('Project Transactions');
1257   RP->trial_balance(\%myconfig, \%$form);
1258
1259   &list_accounts;
1260
1261   $lxdebug->leave_sub();
1262 }
1263
1264 # Antonio Gallardo
1265 #
1266 # D.S. Feb 16, 2001
1267 # included links to display transactions for period entered
1268 # added headers and subtotals
1269 #
1270 sub generate_trial_balance {
1271   $lxdebug->enter_sub();
1272
1273   # get for each account initial balance, debits and credits
1274   RP->trial_balance(\%myconfig, \%$form);
1275
1276   $form->{nextsub} = "generate_trial_balance";
1277   $form->{title}   = $locale->text('Trial Balance');
1278   &list_accounts;
1279
1280   $lxdebug->leave_sub();
1281 }
1282
1283 sub list_accounts {
1284   $lxdebug->enter_sub();
1285
1286   $title = $form->escape($form->{title});
1287
1288   if ($form->{department}) {
1289     ($department) = split /--/, $form->{department};
1290     $options    = $locale->text('Department') . " : $department<br>";
1291     $department = $form->escape($form->{department});
1292   }
1293   if ($form->{projectnumber}) {
1294     $options .=
1295       $locale->text('Project Number') . " : $form->{projectnumber}<br>";
1296     $projectnumber = $form->escape($form->{projectnumber});
1297   }
1298
1299   # if there are any dates
1300   if ($form->{fromdate} || $form->{todate}) {
1301     if ($form->{fromdate}) {
1302       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1303     }
1304     if ($form->{todate}) {
1305       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1306     }
1307
1308     $form->{period} = "$fromdate - $todate";
1309   } else {
1310     $form->{period} =
1311       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1312
1313   }
1314   $options .= $form->{period};
1315
1316   @column_index = qw(accno description begbalance debit credit endbalance);
1317
1318   $column_header{accno} =
1319     qq|<th class=listheading>| . $locale->text('Account') . qq|</th>|;
1320   $column_header{description} =
1321     qq|<th class=listheading>| . $locale->text('Description') . qq|</th>|;
1322   $column_header{debit} =
1323     qq|<th class=listheading>| . $locale->text('Debit') . qq|</th>|;
1324   $column_header{credit} =
1325     qq|<th class=listheading>| . $locale->text('Credit') . qq|</th>|;
1326   $column_header{begbalance} =
1327     qq|<th class=listheading>| . $locale->text('Balance') . qq|</th>|;
1328   $column_header{endbalance} =
1329     qq|<th class=listheading>| . $locale->text('Balance') . qq|</th>|;
1330
1331   $form->header;
1332
1333   print qq|
1334 <body>
1335
1336 <table width=100%>
1337   <tr>
1338     <th class=listtop>$form->{title}</th>
1339   </tr>
1340   <tr height="5"></tr>
1341   <tr>
1342     <td>$options</td>
1343   </tr>
1344   <tr>
1345     <td>
1346       <table width=100%>
1347         <tr>|;
1348
1349   map { print "$column_header{$_}\n" } @column_index;
1350
1351   print qq|
1352         </tr>
1353 |;
1354
1355   # sort the whole thing by account numbers and display
1356   foreach $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} }) {
1357
1358     $description = $form->escape($ref->{description});
1359
1360     $href =
1361       qq|ca.pl?action=list_transactions&accounttype=$form->{accounttype}&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&sort=transdate&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&eur=$form->{eur}&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}&accno=$ref->{accno}&description=$description|;
1362
1363     $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
1364
1365     $debit  = ($ref->{debit} != 0) ? $form->format_amount(\%myconfig, $ref->{debit},  2, "&nbsp;") : "&nbsp;";
1366     $credit = ($ref->{credit} != 0) ? $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;") : "&nbsp;";
1367     $begbalance =
1368       $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2, "&nbsp;");
1369     $endbalance =
1370       $form->format_amount(\%myconfig,
1371                            ($ref->{balance} + $ref->{amount}) * $ml,
1372                            2, "&nbsp;");
1373
1374     #    next if ($ref->{debit} == 0 && $ref->{credit} == 0);
1375
1376     if ($ref->{charttype} eq "H" && $subtotal && $form->{l_subtotal}) {
1377       map { $column_data{$_} = "<th>&nbsp;</th>" }
1378         qw(accno begbalance endbalance);
1379
1380       $subtotalbegbalance =
1381         $form->format_amount(\%myconfig, $subtotalbegbalance, 2, "&nbsp;");
1382       $subtotalendbalance =
1383         $form->format_amount(\%myconfig, $subtotalendbalance, 2, "&nbsp;");
1384       $subtotaldebit =
1385         $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
1386       $subtotalcredit =
1387         $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
1388
1389       $column_data{description} = "<th>$subtotaldescription</th>";
1390       $column_data{begbalance}  = "<th align=right>$subtotalbegbalance</th>";
1391       $column_data{endbalance}  = "<th align=right>$subtotalendbalance</th>";
1392       $column_data{debit}       = "<th align=right>$subtotaldebit</th>";
1393       $column_data{credit}      = "<th align=right>$subtotalcredit</th>";
1394
1395       print qq|
1396         <tr class=listsubtotal>
1397 |;
1398       map { print "$column_data{$_}\n" } @column_index;
1399
1400       print qq|
1401         </tr>
1402 |;
1403     }
1404
1405     if ($ref->{charttype} eq "H") {
1406       $subtotal            = 1;
1407       $subtotaldescription = $ref->{description};
1408       $subtotaldebit       = $ref->{debit};
1409       $subtotalcredit      = $ref->{credit};
1410       $subtotalbegbalance  = 0;
1411       $subtotalendbalance  = 0;
1412
1413       next unless $form->{l_heading};
1414
1415       map { $column_data{$_} = "<th>&nbsp;</th>" }
1416         qw(accno debit credit begbalance endbalance);
1417       $column_data{description} =
1418         "<th class=listheading>$ref->{description}</th>";
1419     }
1420
1421     if ($ref->{charttype} eq "A") {
1422       $column_data{accno}       = "<td><a href=$href>$ref->{accno}</a></td>";
1423       $column_data{description} = "<td>$ref->{description}</td>";
1424       $column_data{debit}       = "<td align=right>$debit</td>";
1425       $column_data{credit}      = "<td align=right>$credit</td>";
1426       $column_data{begbalance}  = "<td align=right>$begbalance</td>";
1427       $column_data{endbalance}  = "<td align=right>$endbalance</td>";
1428
1429       $totaldebit  += $ref->{debit};
1430       $totalcredit += $ref->{credit};
1431
1432       $subtotalbegbalance += $ref->{balance} * $ml;
1433       $subtotalendbalance += ($ref->{balance} + $ref->{amount}) * $ml;
1434
1435     }
1436
1437     if ($ref->{charttype} eq "H") {
1438       print qq|
1439       <tr class=listheading>
1440 |;
1441     }
1442     if ($ref->{charttype} eq "A") {
1443       $i++;
1444       $i %= 2;
1445       print qq|
1446       <tr class=listrow$i>
1447 |;
1448     }
1449
1450     map { print "$column_data{$_}\n" } @column_index;
1451
1452     print qq|
1453       </tr>
1454 |;
1455   }
1456
1457   # print last subtotal
1458   if ($subtotal && $form->{l_subtotal}) {
1459     map { $column_data{$_} = "<th>&nbsp;</th>" }
1460       qw(accno begbalance endbalance);
1461     $subtotalbegbalance =
1462       $form->format_amount(\%myconfig, $subtotalbegbalance, 2, "&nbsp;");
1463     $subtotalendbalance =
1464       $form->format_amount(\%myconfig, $subtotalendbalance, 2, "&nbsp;");
1465     $subtotaldebit =
1466       $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
1467     $subtotalcredit =
1468       $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
1469     $column_data{description} = "<th>$subdescription</th>";
1470     $column_data{begbalance}  = "<th align=right>$subtotalbegbalance</th>";
1471     $column_data{endbalance}  = "<th align=right>$subtotalendbalance</th>";
1472     $column_data{debit}       = "<th align=right>$subtotaldebit</th>";
1473     $column_data{credit}      = "<th align=right>$subtotalcredit</th>";
1474
1475     print qq|
1476       <tr class=listsubtotal>
1477 |;
1478     map { print "$column_data{$_}\n" } @column_index;
1479
1480     print qq|
1481       </tr>
1482 |;
1483   }
1484
1485   $totaldebit  = $form->format_amount(\%myconfig, $totaldebit,  2, "&nbsp;");
1486   $totalcredit = $form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;");
1487
1488   map { $column_data{$_} = "<th>&nbsp;</th>" }
1489     qw(accno description begbalance endbalance);
1490
1491   $column_data{debit}  = qq|<th align=right class=listtotal>$totaldebit</th>|;
1492   $column_data{credit} = qq|<th align=right class=listtotal>$totalcredit</th>|;
1493
1494   print qq|
1495         <tr class=listtotal>
1496 |;
1497
1498   map { print "$column_data{$_}\n" } @column_index;
1499
1500   print qq|
1501         </tr>
1502       </table>
1503     </td>
1504   </tr>
1505   <tr>
1506     <td><hr size=3 noshade></td>
1507   </tr>
1508 </table>
1509
1510 </body>
1511 </html>
1512 |;
1513
1514   $lxdebug->leave_sub();
1515 }
1516
1517 sub generate_ar_aging {
1518   $lxdebug->enter_sub();
1519
1520   # split customer
1521   ($form->{customer}) = split(/--/, $form->{customer});
1522
1523   $form->{ct}   = "customer";
1524   $form->{arap} = "ar";
1525
1526   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
1527
1528   RP->aging(\%myconfig, \%$form);
1529   aging();
1530
1531   $lxdebug->leave_sub();
1532 }
1533
1534 sub generate_ap_aging {
1535   $lxdebug->enter_sub();
1536
1537   # split vendor
1538   ($form->{vendor}) = split(/--/, $form->{vendor});
1539
1540   $form->{ct}   = "vendor";
1541   $form->{arap} = "ap";
1542
1543   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
1544
1545   RP->aging(\%myconfig, \%$form);
1546   aging();
1547
1548   $lxdebug->leave_sub();
1549 }
1550
1551 sub create_aging_subtotal_row {
1552   $lxdebug->enter_sub();
1553
1554   my ($subtotals, $columns, $periods, $class) = @_;
1555
1556   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1557
1558   foreach (@{ $periods }) {
1559     $row->{"c$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
1560     $subtotals->{$_}      = 0;
1561   }
1562
1563   $lxdebug->leave_sub();
1564
1565   return $row;
1566 }
1567
1568 sub aging {
1569   $lxdebug->enter_sub();
1570
1571   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1572
1573   my @columns = qw(statement ct invnumber transdate duedate c0 c30 c60 c90);
1574
1575   my %column_defs = (
1576     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1577     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1578     'invnumber' => { 'text' => $locale->text('Invoice'), },
1579     'transdate' => { 'text' => $locale->text('Date'), },
1580     'duedate'   => { 'text' => $locale->text('Due'), },
1581     'c0'        => { 'text' => $locale->text('Current'), },
1582     'c30'       => { 'text' => '30', },
1583     'c60'       => { 'text' => '60', },
1584     'c90'       => { 'text' => '90', },
1585   );
1586
1587   my %column_alignment = ('statement' => 'center',
1588                           map { $_ => 'right' } qw(c0 c30 c60 c90));
1589
1590   $report->set_options('std_column_visibility' => 1);
1591   $report->set_columns(%column_defs);
1592   $report->set_column_order(@columns);
1593
1594   my @hidden_variables = qw(todate customer vendor arap title ct);
1595   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1596
1597   my @options;
1598
1599   if ($form->{department}) {
1600     my ($department) = split /--/, $form->{department};
1601     push @options, $locale->text('Department') . " : $department";
1602     $form->{callback} .= "&department=" . E($department);
1603   }
1604
1605   if (($form->{arap} eq 'ar') && $form->{customer}) {
1606     push @options, $form->{customer};
1607   }
1608
1609   if (($form->{arap} eq 'ap') && $form->{vendor}) {
1610     push @options, $form->{vendor};
1611   }
1612
1613   push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1614
1615   my $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1616
1617   $report->set_options('top_info_text'        => join("\n", @options),
1618                        'output_format'        => 'HTML',
1619                        'title'                => $form->{title},
1620                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1621     );
1622
1623   my $previous_ctid = 0;
1624   my $row_idx       = 0;
1625   my @periods       = qw(0 30 60 90);
1626   my %subtotals     = map { $_ => 0 } @periods;
1627   my %totals        = map { $_ => 0 } @periods;
1628
1629   foreach $ref (@{ $form->{AG} }) {
1630     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1631       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1632     }
1633
1634     foreach my $key (@periods) {
1635       $subtotals{$key}  += $ref->{"c${key}"};
1636       $totals{$key}     += $ref->{"c${key}"};
1637       $ref->{"c${key}"}  = $ref->{"c${key}"} != 0 ? $form->format_amount(\%myconfig, $ref->{"c${key}"}, 2) : '';
1638     }
1639
1640     my $row = { };
1641
1642     foreach my $column (@columns) {
1643       $row->{$column} = {
1644         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1645         'align'  => $column_alignment{$column},
1646         'valign' => $column eq 'statement' ? 'center' : '',
1647       };
1648     }
1649
1650     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1651
1652     if ($previous_ctid != $ref->{ctid}) {
1653       $row->{statement}->{raw_data} =
1654           $cgi->hidden('-name' => "customer_id_${row_idx}", '-value' => $ref->{ctid})
1655         . $cgi->checkbox('-name' => "statement_${row_idx}", '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1656       $row->{ct}->{data} = $ref->{name};
1657
1658       $row_idx++;
1659     }
1660
1661     $previous_ctid = $ref->{ctid};
1662
1663     $report->add_data($row);
1664   }
1665
1666   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1667
1668   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1669
1670   if ($form->{arap} eq 'ar') {
1671     $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
1672     $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1673                                                                                'PRINT_OPTIONS' => print_options(1), });
1674     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
1675                          'raw_bottom_info_text' => $raw_bottom_info_text);
1676   }
1677
1678   $report->set_options_from_form();
1679
1680   $report->generate_with_headers();
1681
1682   $lxdebug->leave_sub();
1683 }
1684
1685 sub select_all {
1686   $lxdebug->enter_sub();
1687
1688   RP->aging(\%myconfig, \%$form);
1689
1690   map { $_->{checked} = "checked" } @{ $form->{AG} };
1691
1692   &aging;
1693
1694   $lxdebug->leave_sub();
1695 }
1696
1697 sub e_mail {
1698   $lxdebug->enter_sub();
1699
1700   # get name and email addresses
1701   for $i (1 .. $form->{rowcount}) {
1702     if ($form->{"statement_$i"}) {
1703       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1704       RP->get_customer(\%myconfig, \%$form);
1705       $selected = 1;
1706       last;
1707     }
1708   }
1709
1710   $form->error($locale->text('Nothing selected!')) unless $selected;
1711
1712   if ($myconfig{role} eq 'admin') {
1713     $bcc = qq|
1714           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1715           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1716 |;
1717   }
1718
1719   $title = $locale->text('E-mail Statement to') . " $form->{$form->{ct}}";
1720
1721   $form->{media} = "email";
1722
1723   $form->header;
1724
1725   print qq|
1726 <body>
1727
1728 <form method=post action=$form->{script}>
1729
1730 <table width=100%>
1731   <tr class=listtop>
1732     <th>$title</th>
1733   </tr>
1734   <tr height="5"></tr>
1735   <tr>
1736     <td>
1737       <table width=100%>
1738         <tr>
1739           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
1740           <td><input name=email size=30 value="$form->{email}"></td>
1741           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1742           <td><input name=cc size=30 value="$form->{cc}"></td>
1743         </tr>
1744         <tr>
1745           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1746           <td><input name=subject size=30 value="$form->{subject}"></td>
1747           $bcc
1748         </tr>
1749       </table>
1750     </td>
1751   </tr>
1752   <tr>
1753     <td>
1754       <table width=100%>
1755         <tr>
1756           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1757         </tr>
1758         <tr>
1759           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1760         </tr>
1761       </table>
1762     </td>
1763   </tr>
1764   <tr>
1765     <td>
1766 |;
1767
1768   &print_options;
1769
1770   map { delete $form->{$_} }
1771     qw(action email cc bcc subject message type sendmode format header);
1772
1773   # save all other variables
1774   foreach $key (keys %$form) {
1775     $form->{$key} =~ s/\"/&quot;/g;
1776     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1777   }
1778
1779   print qq|
1780     </td>
1781   </tr>
1782   <tr>
1783     <td><hr size=3 noshade></td>
1784   </tr>
1785 </table>
1786
1787 <input type=hidden name=nextsub value=send_email>
1788
1789 <br>
1790 <input name=action class=submit type=submit value="|
1791     . $locale->text('Continue') . qq|">
1792 </form>
1793
1794 </body>
1795 </html>
1796 |;
1797
1798   $lxdebug->leave_sub();
1799 }
1800
1801 sub send_email {
1802   $lxdebug->enter_sub();
1803
1804   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1805     unless $form->{subject};
1806
1807   RP->aging(\%myconfig, \%$form);
1808
1809   $form->{"statement_1"} = 1;
1810
1811   $form->{media} = 'email';
1812   print_form();
1813
1814   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1815
1816   $lxdebug->leave_sub();
1817 }
1818
1819 sub print {
1820   $lxdebug->enter_sub();
1821
1822   if ($form->{media} eq 'printer') {
1823     $form->error($locale->text('Select postscript or PDF!'))
1824       if ($form->{format} !~ /(postscript|pdf)/);
1825   }
1826
1827   for $i (1 .. $form->{rowcount}) {
1828     if ($form->{"statement_$i"}) {
1829       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1830       $selected = 1;
1831       last;
1832     }
1833   }
1834
1835   $form->error($locale->text('Nothing selected!')) unless $selected;
1836
1837   if ($form->{media} eq 'printer') {
1838     $form->{"$form->{ct}_id"} = "";
1839   } else {
1840     $form->{"statement_1"} = 1;
1841   }
1842
1843   RP->aging(\%myconfig, \%$form);
1844
1845   print_form();
1846
1847   $form->redirect($locale->text('Statements sent to printer!'))
1848     if ($form->{media} eq 'printer');
1849
1850   $lxdebug->leave_sub();
1851 }
1852
1853 sub print_form {
1854   $lxdebug->enter_sub();
1855
1856   my %replacements =
1857     (
1858      "ä" => "ae", "ö" => "oe", "ü" => "ue",
1859      "Ä" => "Ae", "Ö" => "Oe", "Ãœ" => "Ue",
1860      "ß" => "ss",
1861      " " => "_"
1862     );
1863
1864   foreach my $key (keys %replacements) {
1865     my $new_key = SL::Iconv::convert("ISO-8859-15", $dbcharset, $key);
1866     $replacements{$new_key} = $replacements{$key} if $new_key ne $key;
1867   }
1868
1869   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1870
1871   $form->{templates} = "$myconfig{templates}";
1872
1873   my $suffix = "html";
1874   my $attachment_suffix = "html";
1875   if ($form->{format} eq 'postscript') {
1876     $form->{postscript} = 1;
1877     $suffix = "tex";
1878     $attachment_suffix = "ps";
1879   } elsif ($form->{format} eq 'pdf') {
1880     $form->{pdf} = 1;
1881     $suffix = "tex";
1882     $attachment_suffix = "pdf";
1883   }
1884
1885   $form->{IN}  = "$form->{type}.$suffix";
1886   $form->{OUT} =
1887     $form->{media} eq 'email'   ? $sendmail              :
1888     $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1889
1890   # Save $form->{email} because it will be overwritten.
1891   $form->{EMAIL_RECIPIENT} = $form->{email};
1892
1893   $i = 0;
1894   while (@{ $form->{AG} }) {
1895
1896     $ref = shift @{ $form->{AG} };
1897
1898     if ($ctid != $ref->{ctid}) {
1899
1900       $ctid = $ref->{ctid};
1901       $i++;
1902
1903       if ($form->{"statement_$i"}) {
1904
1905         @a =
1906           (name, street, zipcode, city, country, contact, email,
1907            "$form->{ct}phone", "$form->{ct}fax");
1908         map { $form->{$_} = $ref->{$_} } @a;
1909
1910         $form->{ $form->{ct} } = $form->{name};
1911         $form->{"$form->{ct}_id"} = $ref->{ctid};
1912
1913         map { $form->{$_} = () } qw(invnumber invdate duedate);
1914         $form->{total} = 0;
1915         foreach $item (qw(c0 c30 c60 c90)) {
1916           $form->{$item} = ();
1917           $form->{"${item}total"} = 0;
1918         }
1919
1920         &statement_details($ref);
1921
1922         while ($ref) {
1923
1924           if (scalar(@{ $form->{AG} }) > 0) {
1925
1926             # one or more left to go
1927             if ($ctid == $form->{AG}->[0]->{ctid}) {
1928               $ref = shift @{ $form->{AG} };
1929               &statement_details($ref);
1930
1931               # any more?
1932               $ref = scalar(@{ $form->{AG} });
1933             } else {
1934               $ref = 0;
1935             }
1936           } else {
1937
1938             # set initial ref to 0
1939             $ref = 0;
1940           }
1941
1942         }
1943
1944         map {
1945           $form->{"${_}total"} =
1946             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1947         } (c0, c30, c60, c90, "");
1948
1949         $form->{attachment_filename} = $locale->text("Statement") . "_$form->{todate}.$attachment_suffix";
1950         map({ $form->{attachment_filename} =~ s/$_/$replacements{$_}/g; } keys(%replacements));
1951
1952         $form->parse_template(\%myconfig, $userspath);
1953
1954       }
1955     }
1956   }
1957   # saving the history
1958   if(!exists $form->{addition} && $form->{id} ne "") {
1959     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1960         $form->{addition} = "PRINTED";
1961         $form->{what_done} = $form->{type};
1962         $form->save_history($form->dbconnect(\%myconfig));
1963   }
1964   # /saving the history 
1965   $lxdebug->leave_sub();
1966 }
1967
1968 sub statement_details {
1969   $lxdebug->enter_sub();
1970   my ($ref) = @_;
1971
1972   push @{ $form->{invnumber} }, $ref->{invnumber};
1973   push @{ $form->{invdate} },   $ref->{transdate};
1974   push @{ $form->{duedate} },   $ref->{duedate};
1975
1976   foreach $item (qw(c0 c30 c60 c90)) {
1977     if ($ref->{exchangerate} * 1) {
1978       $ref->{$item} =
1979         $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2);
1980     }
1981     $form->{"${item}total"} += $ref->{$item};
1982     $form->{total}          += $ref->{$item};
1983     push @{ $form->{$item} },
1984       $form->format_amount(\%myconfig, $ref->{$item}, 2);
1985   }
1986
1987   $lxdebug->leave_sub();
1988 }
1989
1990 sub generate_tax_report {
1991   $lxdebug->enter_sub();
1992
1993   RP->tax_report(\%myconfig, \%$form);
1994
1995   $descvar     = "$form->{accno}_description";
1996   $description = $form->escape($form->{$descvar});
1997   $ratevar     = "$form->{accno}_rate";
1998
1999   $department = $form->escape($form->{department});
2000
2001   # construct href
2002   $href =
2003     "$form->{script}?&action=generate_tax_report&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
2004
2005   # construct callback
2006   $description = $form->escape($form->{$descvar},   1);
2007   $department  = $form->escape($form->{department}, 1);
2008   $callback    =
2009     "$form->{script}?&action=generate_tax_report&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
2010
2011   $title = $form->escape($form->{title});
2012   $href .= "&title=$title";
2013   $title = $form->escape($form->{title}, 1);
2014   $callback .= "&title=$title";
2015
2016   $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
2017
2018   @columns =
2019     $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
2020
2021   foreach $item (@columns) {
2022     if ($form->{"l_$item"} eq "Y") {
2023       push @column_index, $item;
2024
2025       # add column to href and callback
2026       $callback .= "&l_$item=Y";
2027       $href     .= "&l_$item=Y";
2028     }
2029   }
2030
2031   if ($form->{l_subtotal} eq 'Y') {
2032     $callback .= "&l_subtotal=Y";
2033     $href     .= "&l_subtotal=Y";
2034   }
2035
2036   if ($form->{department}) {
2037     ($department) = split /--/, $form->{department};
2038     $option = $locale->text('Department') . " : $department";
2039   }
2040
2041   # if there are any dates
2042   if ($form->{fromdate} || $form->{todate}) {
2043     if ($form->{fromdate}) {
2044       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
2045     }
2046     if ($form->{todate}) {
2047       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
2048     }
2049
2050     $form->{period} = "$fromdate - $todate";
2051   } else {
2052     $form->{period} =
2053       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2054   }
2055
2056   if ($form->{db} eq 'ar') {
2057     $name    = $locale->text('Customer');
2058     $invoice = 'is.pl';
2059     $arap    = 'ar.pl';
2060   }
2061   if ($form->{db} eq 'ap') {
2062     $name    = $locale->text('Vendor');
2063     $invoice = 'ir.pl';
2064     $arap    = 'ap.pl';
2065   }
2066
2067   $option .= "<br>" if $option;
2068   $option .= "$form->{period}";
2069
2070   $column_header{id} =
2071       qq|<th><a class=listheading href=$href&sort=id>|
2072     . $locale->text('ID')
2073     . qq|</th>|;
2074   $column_header{invnumber} =
2075       qq|<th><a class=listheading href=$href&sort=invnumber>|
2076     . $locale->text('Invoice')
2077     . qq|</th>|;
2078   $column_header{transdate} =
2079       qq|<th><a class=listheading href=$href&sort=transdate>|
2080     . $locale->text('Date')
2081     . qq|</th>|;
2082   $column_header{netamount} =
2083     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
2084   $column_header{tax} =
2085     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
2086   $column_header{amount} =
2087     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
2088
2089   $column_header{name} =
2090     qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
2091
2092   $form->header;
2093
2094   print qq|
2095 <body>
2096
2097 <table width=100%>
2098   <tr>
2099     <th class=listtop colspan=$colspan>$form->{title}</th>
2100   </tr>
2101   <tr height="5"></tr>
2102   <tr>
2103     <td>$option</td>
2104   </tr>
2105   <tr>
2106     <td>
2107       <table width=100%>
2108         <tr class=listheading>
2109 |;
2110
2111   map { print "$column_header{$_}\n" } @column_index;
2112
2113   print qq|
2114         </tr>
2115 |;
2116
2117   # add sort and escape callback
2118   $callback = $form->escape($callback . "&sort=$form->{sort}");
2119
2120   if (@{ $form->{TR} }) {
2121     $sameitem = $form->{TR}->[0]->{ $form->{sort} };
2122   }
2123
2124   foreach $ref (@{ $form->{TR} }) {
2125
2126     $module = ($ref->{invoice}) ? $invoice : $arap;
2127
2128     if ($form->{l_subtotal} eq 'Y') {
2129       if ($sameitem ne $ref->{ $form->{sort} }) {
2130         &tax_subtotal;
2131         $sameitem = $ref->{ $form->{sort} };
2132       }
2133     }
2134
2135     $totalnetamount += $ref->{netamount};
2136     $totaltax       += $ref->{tax};
2137     $ref->{amount} = $ref->{netamount} + $ref->{tax};
2138
2139     $subtotalnetamount += $ref->{netamount};
2140     $subtotaltax       += $ref->{tax};
2141
2142     map {
2143       $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;");
2144     } qw(netamount tax amount);
2145
2146     $column_data{id}        = qq|<td>$ref->{id}</td>|;
2147     $column_data{invnumber} =
2148       qq|<td><a href=$module?action=edit&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{invnumber}</a></td>|;
2149     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
2150     $column_data{name}      = qq|<td>$ref->{name}&nbsp;</td>|;
2151
2152     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
2153       qw(netamount tax amount);
2154
2155     $i++;
2156     $i %= 2;
2157     print qq|
2158         <tr class=listrow$i>
2159 |;
2160
2161     map { print "$column_data{$_}\n" } @column_index;
2162
2163     print qq|
2164         </tr>
2165 |;
2166
2167   }
2168
2169   if ($form->{l_subtotal} eq 'Y') {
2170     &tax_subtotal;
2171   }
2172
2173   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
2174
2175   print qq|
2176         </tr>
2177         <tr class=listtotal>
2178 |;
2179
2180   $total =
2181     $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
2182   $totalnetamount =
2183     $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
2184   $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
2185
2186   $column_data{netamount} =
2187     qq|<th class=listtotal align=right>$totalnetamount</th>|;
2188   $column_data{tax}    = qq|<th class=listtotal align=right>$totaltax</th>|;
2189   $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
2190
2191   map { print "$column_data{$_}\n" } @column_index;
2192
2193   print qq|
2194         </tr>
2195       </table>
2196     </td>
2197   </tr>
2198   <tr>
2199     <td><hr size=3 noshade></td>
2200   </tr>
2201 </table>
2202
2203 </body>
2204 </html>
2205 |;
2206
2207   $lxdebug->leave_sub();
2208 }
2209
2210 sub tax_subtotal {
2211   $lxdebug->enter_sub();
2212
2213   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2214
2215   $subtotalnetamount =
2216     $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;");
2217   $subtotaltax = $form->format_amount(\%myconfig, $subtotaltax, 2, "&nbsp;");
2218   $subtotal =
2219     $form->format_amount(\%myconfig, $subtotalnetamount + $subtotaltax,
2220                          2, "&nbsp;");
2221
2222   $column_data{netamount} =
2223     "<th class=listsubtotal align=right>$subtotalnetamount</th>";
2224   $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
2225   $column_data{amount} = "<th class=listsubtotal align=right>$subtotal</th>";
2226
2227   $subtotalnetamount = 0;
2228   $subtotaltax       = 0;
2229
2230   print qq|
2231         <tr class=listsubtotal>
2232 |;
2233   map { print "\n$column_data{$_}" } @column_index;
2234
2235   print qq|
2236         </tr>
2237 |;
2238
2239   $lxdebug->leave_sub();
2240 }
2241
2242 sub list_payments {
2243   $lxdebug->enter_sub();
2244
2245   if ($form->{account}) {
2246     ($form->{paymentaccounts}) = split /--/, $form->{account};
2247   }
2248   if ($form->{department}) {
2249     ($department, $form->{department_id}) = split /--/, $form->{department};
2250     $option = $locale->text('Department') . " : $department";
2251   }
2252
2253   RP->payments(\%myconfig, \%$form);
2254
2255   my @hidden_variables = qw(account title department reference source memo fromdate todate
2256                             fx_transaction db prepayment paymentaccounts sort);
2257
2258   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
2259   $form->{callback} = $href;
2260
2261   my @columns     = qw(transdate invnumber name paid source memo);
2262   my %column_defs = (
2263     'name'      => { 'text' => $locale->text('Description'), },
2264     'invnumber' => { 'text' => $locale->text('Reference'), },
2265     'transdate' => { 'text' => $locale->text('Date'), },
2266     'paid'      => { 'text' => $locale->text('Amount'), },
2267     'source'    => { 'text' => $locale->text('Source'), },
2268     'memo'      => { 'text' => $locale->text('Memo'), },
2269   );
2270   my %column_alignment = ('paid' => 'right');
2271
2272   map { $column_defs{$_}->{link} = $href . "&sort=$_" } grep { $_ ne 'paid' } @columns;
2273
2274   my @options;
2275   if ($form->{fromdate}) {
2276     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{fromdate}, 1);
2277   }
2278   if ($form->{todate}) {
2279     push @options, $locale->text('bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{todate}, 1);
2280   }
2281
2282   my $report = SL::ReportGenerator->new(\%myconfig, $form);
2283
2284   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
2285
2286   $report->set_options('top_info_text'         => join("\n", @options),
2287                        'output_format'         => 'HTML',
2288                        'title'                 => $form->{title},
2289                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
2290                        'std_column_visibility' => 1,
2291     );
2292   $report->set_options_from_form();
2293
2294   $report->set_columns(%column_defs);
2295   $report->set_column_order(@columns);
2296
2297   $report->set_export_options('list_payments', @hidden_variables);
2298
2299   $report->set_sort_indicator($form->{sort}, 1);
2300
2301   my $total_paid    = 0;
2302
2303   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
2304     next unless @{ $form->{ $ref->{id} } };
2305
2306     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
2307
2308     my $subtotal_paid = 0;
2309
2310     foreach my $payment (@{ $form->{ $ref->{id} } }) {
2311       my $module = $payment->{module};
2312       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
2313       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
2314
2315       $subtotal_paid += $payment->{paid};
2316       $total_paid    += $payment->{paid};
2317
2318       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
2319
2320       my $row = { };
2321
2322       foreach my $column (@columns) {
2323         $row->{$column} = {
2324           'data'  => $payment->{$column},
2325           'align' => $column_alignment{$column},
2326         };
2327       }
2328
2329       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
2330
2331       $report->add_data($row);
2332     }
2333
2334     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
2335     $row->{paid} = {
2336       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
2337       'align' => 'right',
2338       'class' => 'listsubtotal',
2339     };
2340
2341     $report->add_data($row);
2342   }
2343
2344   $report->add_separator();
2345
2346   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
2347   $row->{paid} = {
2348     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
2349     'align' => 'right',
2350     'class' => 'listtotal',
2351   };
2352
2353   $report->add_data($row);
2354
2355   $report->generate_with_headers();
2356
2357   $lxdebug->leave_sub();
2358 }
2359
2360 sub config {
2361   $lxdebug->enter_sub();
2362   edit();
2363
2364   #$form->header;
2365   #print qq|Hallo|;
2366   $lxdebug->leave_sub();
2367 }
2368
2369 sub debug {
2370
2371   $form->debug();
2372
2373 }
2374
2375 sub winston_export {
2376   $lxdebug->enter_sub();
2377
2378   #create_winston();
2379   $form->{winston} = 1;
2380   &generate_ustva();
2381   $lxdebug->leave_sub();
2382 }
2383
2384 sub print_options {
2385   $lxdebug->enter_sub();
2386
2387   my ($dont_print) = @_;
2388
2389   $form->{sendmode} = "attachment";
2390
2391   $form->{"format"} =
2392     $form->{"format"} ? $form->{"format"} :
2393     $myconfig{"template_format"} ? $myconfig{"template_format"} :
2394     "pdf";
2395
2396   $form->{"copies"} =
2397     $form->{"copies"} ? $form->{"copies"} :
2398     $myconfig{"copies"} ? $myconfig{"copies"} :
2399     2;
2400
2401   $form->{PD}{ $form->{type} }     = "selected";
2402   $form->{DF}{ $form->{format} }   = "selected";
2403   $form->{OP}{ $form->{media} }    = "selected";
2404   $form->{SM}{ $form->{sendmode} } = "selected";
2405
2406   if ($form->{report} eq 'ustva') {
2407     $type = qq|
2408             <option value=ustva $form->{PD}{ustva}>| . $locale->text('ustva');
2409   } else {
2410     $type = qq|
2411             <option value=statement $form->{PD}{statement}>|
2412       . $locale->text('Statement');
2413   }
2414
2415   if ($form->{media} eq 'email') {
2416     $media = qq|
2417             <option value=attachment $form->{SM}{attachment}>|
2418       . $locale->text('Attachment') . qq|
2419             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
2420   } else {
2421     $media = qq|
2422             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
2423     if ($myconfig{printer} && $latex_templates) {
2424       $media .= qq|
2425             <option value=printer $form->{OP}{printer}>|
2426         . $locale->text('Printer');
2427     }
2428   }
2429
2430   if ($latex_templates) {
2431     $format .= qq|
2432             <option value=html $form->{DF}{html}>|
2433       . $locale->text('HTML') . qq|
2434             <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF');
2435     if ($form->{report} ne 'ustva') {
2436       $format . qq|
2437             <option value=postscript $form->{DF}{postscript}>|
2438         . $locale->text('Postscript');
2439     }
2440   }
2441
2442   my $output = qq|
2443 <table>
2444   <tr>
2445     <td><select name=type>$type</select></td>
2446     <td><select name=format>$format</select></td>
2447     <td><select name=media>$media</select></td>
2448 |;
2449
2450   if ($myconfig{printer} && $latex_templates && $form->{media} ne 'email') {
2451     $output .= qq|
2452       <td>| . $locale->text('Copies') . qq|
2453       <input name=copies size=2 value=$form->{copies}></td>
2454 |;
2455   }
2456
2457   $output .= qq|
2458   </tr>
2459 </table>
2460 |;
2461
2462   print $output unless $dont_print;
2463
2464   $lxdebug->leave_sub();
2465
2466   return $output;
2467 }
2468
2469 sub generate_bwa {
2470   $lxdebug->enter_sub();
2471   $form->{padding} = "&nbsp;&nbsp;";
2472   $form->{bold}    = "<b>";
2473   $form->{endbold} = "</b>";
2474   $form->{br}      = "<br>";
2475
2476   if ($form->{reporttype} eq "custom") {
2477
2478     #forgotten the year --> thisyear
2479     if ($form->{year} !~ m/^\d\d\d\d$/) {
2480       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
2481         /(\d\d\d\d)/;
2482       $form->{year} = $1;
2483     }
2484
2485     #yearly report
2486     if ($form->{duetyp} eq "13") {
2487       $form->{fromdate}        = "1.1.$form->{year}";
2488       $form->{todate}          = "31.12.$form->{year}";
2489       $form->{comparefromdate} = "1.01.$form->{year}";
2490       $form->{comparetodate}   = "31.12.$form->{year}";
2491     }
2492
2493     #Quater reports
2494     if ($form->{duetyp} eq "A") {
2495       $form->{fromdate}        = "1.1.$form->{year}";
2496       $form->{todate}          = "31.3.$form->{year}";
2497       $form->{comparefromdate} = "1.01.$form->{year}";
2498       $form->{comparetodate}   = "31.03.$form->{year}";
2499     }
2500     if ($form->{duetyp} eq "B") {
2501       $form->{fromdate}        = "1.4.$form->{year}";
2502       $form->{todate}          = "30.6.$form->{year}";
2503       $form->{comparefromdate} = "1.01.$form->{year}";
2504       $form->{comparetodate}   = "30.06.$form->{year}";
2505     }
2506     if ($form->{duetyp} eq "C") {
2507       $form->{fromdate}        = "1.7.$form->{year}";
2508       $form->{todate}          = "30.9.$form->{year}";
2509       $form->{comparefromdate} = "1.01.$form->{year}";
2510       $form->{comparetodate}   = "30.09.$form->{year}";
2511     }
2512     if ($form->{duetyp} eq "D") {
2513       $form->{fromdate}        = "1.10.$form->{year}";
2514       $form->{todate}          = "31.12.$form->{year}";
2515       $form->{comparefromdate} = "1.01.$form->{year}";
2516       $form->{comparetodate}   = "31.12.$form->{year}";
2517     }
2518
2519     #Monthly reports
2520   SWITCH: {
2521       $form->{duetyp} eq "1" && do {
2522         $form->{fromdate}        = "1.1.$form->{year}";
2523         $form->{todate}          = "31.1.$form->{year}";
2524         $form->{comparefromdate} = "1.01.$form->{year}";
2525         $form->{comparetodate}   = "31.01.$form->{year}";
2526         last SWITCH;
2527       };
2528       $form->{duetyp} eq "2" && do {
2529         $form->{fromdate} = "1.2.$form->{year}";
2530
2531         #this works from 1901 to 2099, 1900 and 2100 fail.
2532         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
2533         $form->{todate}          = "$leap.2.$form->{year}";
2534         $form->{comparefromdate} = "1.01.$form->{year}";
2535         $form->{comparetodate}   = "$leap.02.$form->{year}";
2536         last SWITCH;
2537       };
2538       $form->{duetyp} eq "3" && do {
2539         $form->{fromdate}        = "1.3.$form->{year}";
2540         $form->{todate}          = "31.3.$form->{year}";
2541         $form->{comparefromdate} = "1.01.$form->{year}";
2542         $form->{comparetodate}   = "31.03.$form->{year}";
2543         last SWITCH;
2544       };
2545       $form->{duetyp} eq "4" && do {
2546         $form->{fromdate}        = "1.4.$form->{year}";
2547         $form->{todate}          = "30.4.$form->{year}";
2548         $form->{comparefromdate} = "1.01.$form->{year}";
2549         $form->{comparetodate}   = "30.04.$form->{year}";
2550         last SWITCH;
2551       };
2552       $form->{duetyp} eq "5" && do {
2553         $form->{fromdate}        = "1.5.$form->{year}";
2554         $form->{todate}          = "31.5.$form->{year}";
2555         $form->{comparefromdate} = "1.01.$form->{year}";
2556         $form->{comparetodate}   = "31.05.$form->{year}";
2557         last SWITCH;
2558       };
2559       $form->{duetyp} eq "6" && do {
2560         $form->{fromdate}        = "1.6.$form->{year}";
2561         $form->{todate}          = "30.6.$form->{year}";
2562         $form->{comparefromdate} = "1.01.$form->{year}";
2563         $form->{comparetodate}   = "30.06.$form->{year}";
2564         last SWITCH;
2565       };
2566       $form->{duetyp} eq "7" && do {
2567         $form->{fromdate}        = "1.7.$form->{year}";
2568         $form->{todate}          = "31.7.$form->{year}";
2569         $form->{comparefromdate} = "1.01.$form->{year}";
2570         $form->{comparetodate}   = "31.07.$form->{year}";
2571         last SWITCH;
2572       };
2573       $form->{duetyp} eq "8" && do {
2574         $form->{fromdate}        = "1.8.$form->{year}";
2575         $form->{todate}          = "31.8.$form->{year}";
2576         $form->{comparefromdate} = "1.01.$form->{year}";
2577         $form->{comparetodate}   = "31.08.$form->{year}";
2578         last SWITCH;
2579       };
2580       $form->{duetyp} eq "9" && do {
2581         $form->{fromdate}        = "1.9.$form->{year}";
2582         $form->{todate}          = "30.9.$form->{year}";
2583         $form->{comparefromdate} = "1.01.$form->{year}";
2584         $form->{comparetodate}   = "30.09.$form->{year}";
2585         last SWITCH;
2586       };
2587       $form->{duetyp} eq "10" && do {
2588         $form->{fromdate}        = "1.10.$form->{year}";
2589         $form->{todate}          = "31.10.$form->{year}";
2590         $form->{comparefromdate} = "1.01.$form->{year}";
2591         $form->{comparetodate}   = "31.10.$form->{year}";
2592         last SWITCH;
2593       };
2594       $form->{duetyp} eq "11" && do {
2595         $form->{fromdate}        = "1.11.$form->{year}";
2596         $form->{todate}          = "30.11.$form->{year}";
2597         $form->{comparefromdate} = "1.01.$form->{year}";
2598         $form->{comparetodate}   = "30.11.$form->{year}";
2599         last SWITCH;
2600       };
2601       $form->{duetyp} eq "12" && do {
2602         $form->{fromdate}        = "1.12.$form->{year}";
2603         $form->{todate}          = "31.12.$form->{year}";
2604         $form->{comparefromdate} = "1.01.$form->{year}";
2605         $form->{comparetodate}   = "31.12.$form->{year}";
2606         last SWITCH;
2607       };
2608     }
2609   } else {
2610     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
2611     $form->{fromdate} = "${dd}.${mm}.${yy}";
2612     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{todate});
2613     $form->{todate}          = "${dd}.${mm}.${yy}";
2614     $form->{comparefromdate} = "01.01.$yy";
2615     $form->{comparetodate}   = $form->{todate};
2616   }
2617
2618   RP->bwa(\%myconfig, \%$form);
2619
2620   ($form->{department}) = split /--/, $form->{department};
2621
2622   $form->{period} =
2623     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2624   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
2625
2626   # if there are any dates construct a where
2627   if ($form->{fromdate} || $form->{todate}) {
2628
2629     unless ($form->{todate}) {
2630       $form->{todate} = $form->current_date(\%myconfig);
2631     }
2632
2633     my %germandate = ("dateformat" => "dd.mm.yyyy");
2634
2635     $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
2636     $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
2637
2638     $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
2639     $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
2640
2641     $form->{this_period} = "$shortfromdate\n$shorttodate";
2642     $form->{period}      =
2643         $locale->text('for Period')
2644       . qq|\n$longfromdate |
2645       . $locale->text('bis')
2646       . qq| $longtodate|;
2647   }
2648
2649   # setup variables for the form
2650   @a = qw(company address businessnumber);
2651   map { $form->{$_} = $myconfig{$_} } @a;
2652   $form->{templates} = $myconfig{templates};
2653
2654   $form->{IN} = "bwa.html";
2655
2656   $form->parse_template;
2657
2658   $lxdebug->leave_sub();
2659 }
2660
2661 sub generate_ustva {
2662   $lxdebug->enter_sub();
2663
2664   # Hier Aufruf von get_config zum Einlesen der Finanzamtdaten
2665   USTVA->get_config($userspath, 'finanzamt.ini');
2666
2667   #  &get_project(generate_bwa);
2668   @anmeldungszeitraum =
2669     qw(0401, 0402, 0403, 0404, 0405, 0405, 0406, 0407, 0408, 0409, 0410, 0411, 0412, 0441, 0442, 0443, 0444);
2670
2671   foreach $item (@anmeldungszeitraum) {
2672     $form->{$item} = "";
2673   }
2674   if ($form->{reporttype} eq "custom") {
2675
2676     #forgotten the year --> thisyear
2677     if ($form->{year} !~ m/^\d\d\d\d$/) {
2678       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
2679         /(\d\d\d\d)/;
2680       $form->{year} = $1;
2681     }
2682
2683     #yearly report
2684     if ($form->{duetyp} eq "13") {
2685       $form->{fromdate} = "1.1.$form->{year}";
2686       $form->{todate}   = "31.12.$form->{year}";
2687     }
2688
2689     #Quater reports
2690     if ($form->{duetyp} eq "A") {
2691       $form->{fromdate} = "1.1.$form->{year}";
2692       $form->{todate}   = "31.3.$form->{year}";
2693       $form->{"0441"}   = "X";
2694     }
2695     if ($form->{duetyp} eq "B") {
2696       $form->{fromdate} = "1.4.$form->{year}";
2697       $form->{todate}   = "30.6.$form->{year}";
2698       $form->{"0442"}   = "X";
2699     }
2700     if ($form->{duetyp} eq "C") {
2701       $form->{fromdate} = "1.7.$form->{year}";
2702       $form->{todate}   = "30.9.$form->{year}";
2703       $form->{"0443"}   = "X";
2704     }
2705     if ($form->{duetyp} eq "D") {
2706       $form->{fromdate} = "1.10.$form->{year}";
2707       $form->{todate}   = "31.12.$form->{year}";
2708       $form->{"0444"}   = "X";
2709     }
2710
2711     #Monthly reports
2712   SWITCH: {
2713       $form->{duetyp} eq "1" && do {
2714         $form->{fromdate} = "1.1.$form->{year}";
2715         $form->{todate}   = "31.1.$form->{year}";
2716         $form->{"0401"}   = "X";
2717         last SWITCH;
2718       };
2719       $form->{duetyp} eq "2" && do {
2720         $form->{fromdate} = "1.2.$form->{year}";
2721
2722         #this works from 1901 to 2099, 1900 and 2100 fail.
2723         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
2724         $form->{todate} = "$leap.2.$form->{year}";
2725         $form->{"0402"} = "X";
2726         last SWITCH;
2727       };
2728       $form->{duetyp} eq "3" && do {
2729         $form->{fromdate} = "1.3.$form->{year}";
2730         $form->{todate}   = "31.3.$form->{year}";
2731         $form->{"0403"}   = "X";
2732         last SWITCH;
2733       };
2734       $form->{duetyp} eq "4" && do {
2735         $form->{fromdate} = "1.4.$form->{year}";
2736         $form->{todate}   = "30.4.$form->{year}";
2737         $form->{"0404"}   = "X";
2738         last SWITCH;
2739       };
2740       $form->{duetyp} eq "5" && do {
2741         $form->{fromdate} = "1.5.$form->{year}";
2742         $form->{todate}   = "31.5.$form->{year}";
2743         $form->{"0405"}   = "X";
2744         last SWITCH;
2745       };
2746       $form->{duetyp} eq "6" && do {
2747         $form->{fromdate} = "1.6.$form->{year}";
2748         $form->{todate}   = "30.6.$form->{year}";
2749         $form->{"0406"}   = "X";
2750         last SWITCH;
2751       };
2752       $form->{duetyp} eq "7" && do {
2753         $form->{fromdate} = "1.7.$form->{year}";
2754         $form->{todate}   = "31.7.$form->{year}";
2755         $form->{"0407"}   = "X";
2756         last SWITCH;
2757       };
2758       $form->{duetyp} eq "8" && do {
2759         $form->{fromdate} = "1.8.$form->{year}";
2760         $form->{todate}   = "31.8.$form->{year}";
2761         $form->{"0408"}   = "X";
2762         last SWITCH;
2763       };
2764       $form->{duetyp} eq "9" && do {
2765         $form->{fromdate} = "1.9.$form->{year}";
2766         $form->{todate}   = "30.9.$form->{year}";
2767         $form->{"0409"}   = "X";
2768         last SWITCH;
2769       };
2770       $form->{duetyp} eq "10" && do {
2771         $form->{fromdate} = "1.10.$form->{year}";
2772         $form->{todate}   = "31.10.$form->{year}";
2773         $form->{"0410"}   = "X";
2774         last SWITCH;
2775       };
2776       $form->{duetyp} eq "11" && do {
2777         $form->{fromdate} = "1.11.$form->{year}";
2778         $form->{todate}   = "30.11.$form->{year}";
2779         $form->{"0411"}   = "X";
2780         last SWITCH;
2781       };
2782       $form->{duetyp} eq "12" && do {
2783         $form->{fromdate} = "1.12.$form->{year}";
2784         $form->{todate}   = "31.12.$form->{year}";
2785         $form->{"0412"}   = "X";
2786         last SWITCH;
2787       };
2788     }
2789   }
2790
2791   #    $locale->date(\%myconfig, $form->current_date(\%myconfig), 0)=~ /(\d\d\d\d)/;
2792   #    $form->{year}= $1;
2793   #    $form->{fromdate}="1.1.$form->{year}";
2794   #    $form->{todate}="31.3.$form->{year}";
2795   #    $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2796   #  }
2797
2798   RP->ustva(\%myconfig, \%$form);
2799
2800   ($form->{department}) = split /--/, $form->{department};
2801
2802   $form->{period} =
2803     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2804   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
2805
2806   # if there are any dates construct a where
2807   if ($form->{fromdate} || $form->{todate}) {
2808
2809     unless ($form->{todate}) {
2810       $form->{todate} = $form->current_date(\%myconfig);
2811     }
2812
2813     $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
2814     $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
2815
2816     $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
2817     $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
2818
2819     $form->{this_period} = "$shortfromdate\n$shorttodate";
2820     $form->{period}      =
2821         $locale->text('for Period')
2822       . qq|<br>\n$longfromdate |
2823       . $locale->text('bis')
2824       . qq| $longtodate|;
2825   }
2826
2827   if ($form->{comparefromdate} || $form->{comparetodate}) {
2828     $longcomparefromdate =
2829       $locale->date(\%myconfig, $form->{comparefromdate}, 1);
2830     $shortcomparefromdate =
2831       $locale->date(\%myconfig, $form->{comparefromdate}, 0);
2832
2833     $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
2834     $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
2835
2836     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
2837     $form->{period} .=
2838         "\n$longcomparefromdate "
2839       . $locale->text('bis')
2840       . qq| $longcomparetodate|;
2841   }
2842
2843   $form->{Datum_heute} =
2844     $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
2845
2846   if (   $form->{format} eq 'pdf'
2847       or $form->{format} eq 'postscript') {
2848     $form->{padding} = "~~";
2849     $form->{bold}    = "\textbf{";
2850     $form->{endbold} = "}";
2851     $form->{br}      = '\\\\';
2852
2853     @numbers = qw(51r 86r 97r 93r 96 43 45
2854       66 62 67);
2855     foreach $number (@numbers) {
2856       $form->{$number} =~ s/,/~~/g;
2857     }
2858
2859       } elsif ($form->{format} eq 'html') {
2860     $form->{padding} = "&nbsp;&nbsp;";
2861     $form->{bold}    = "<b>";
2862     $form->{endbold} = "</b>";
2863     $form->{br}      = "<br>"
2864
2865   }
2866
2867   # setup variables for the form
2868   @a = qw(company address businessnumber);
2869   map { $form->{$_} = $myconfig{$_} } @a;
2870
2871   $form->{address} =~ s/\\n/$form->{br}/g;
2872
2873   if ($form->{winston} eq '1') {
2874     create_winston();
2875
2876   } else {
2877     $form->{templates} = $myconfig{templates};
2878     $form->{IN}        = "$form->{type}";
2879     $form->{IN} .= '.tex'
2880       if (   $form->{format} eq 'pdf'
2881           or $form->{format} eq 'postscript');
2882     $form->{IN} .= '.html' if ($form->{format} eq 'html');
2883
2884     $form->parse_template(\%myconfig, $userspath);
2885
2886     # $form->parse_template;
2887   }
2888   $lxdebug->leave_sub();
2889 }