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