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