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