Doppeltes Parsen von Zahlen vermeiden (passiert beim Speichern von Aufträgen, in...
[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    = "";
193     $id_1      = "";
194     $value_1   = "";
195     $trigger_1 = "";
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           <th align=right>| . $locale->text('Bis') . qq|</th>
792           <td>
793             $button1
794             $button1_2
795           </td>
796         </tr>
797         <input type=hidden name=type value=statement>
798         <input type=hidden name=format value=html>
799         <input type=hidden name=media value=screen>
800
801         <input type=hidden name=nextsub value=$nextsub>
802         <input type=hidden name=action value=$nextsub>
803
804 $jsscript
805 |;
806   }
807
808   # above action can be removed if there is more than one input field
809
810   if ($form->{report} =~ /(receipts|payments)$/) {
811     $form->{db} = ($form->{report} =~ /payments$/) ? "ap" : "ar";
812
813     RP->paymentaccounts(\%myconfig, \%$form);
814
815     $selection = "<option>\n";
816     foreach $ref (@{ $form->{PR} }) {
817       $paymentaccounts .= "$ref->{accno} ";
818       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
819     }
820
821     chop $paymentaccounts;
822
823     print qq|
824         <input type=hidden name=nextsub value=list_payments>
825         <tr>
826           <th align=right nowrap>| . $locale->text('Account') . qq|</th>
827           <td colspan=3><select name=account>$selection</select>
828             <input type=hidden name=paymentaccounts value="$paymentaccounts">
829           </td>
830         </tr>
831         <tr>
832           <th align=right>| . $locale->text('Reference') . qq|</th>
833           <td colspan=3><input name=reference></td>
834         </tr>
835         <tr>
836           <th align=right nowrap>| . $locale->text('Source') . qq|</th>
837           <td colspan=3><input name=source></td>
838         </tr>
839         <tr>
840           <th align=right nowrap>| . $locale->text('Memo') . qq|</th>
841           <td colspan=3><input name=memo size=30></td>
842         </tr>
843         <tr>
844           <th align=right>| . $locale->text('From') . qq|</th>
845           <td>
846             $button1
847             $button1_2
848           </td>
849           <th align=right>| . $locale->text('Bis') . qq|</th>
850           <td>
851             $button2
852             $button2_2
853           </td>
854         </tr>
855         <tr>
856           <td align=right><input type=checkbox style=checkbox name=fx_transaction value=1 checked></td>
857           <th align=left colspan=3>| . $locale->text('Include Exchangerate Difference') . qq|</td>
858         </tr>
859
860 $jsscript
861
862           <input type=hidden name=db value=$form->{db}>
863           <input type=hidden name=sort value=transdate>
864 |;
865
866   }
867
868   print qq|
869
870       </table>
871     </td>
872   </tr>
873   <tr>
874     <td><hr size=3 noshade></td>
875   </tr>
876 </table>
877
878 <br>
879 <input type=submit class=submit name=action value="| . $locale->text('Continue') . qq|">
880
881 </form>
882
883 </body>
884 </html>
885 |;
886
887   $lxdebug->leave_sub();
888 }
889
890 sub continue { call_sub($form->{"nextsub"}); }
891
892 sub get_project {
893   $lxdebug->enter_sub();
894
895   $auth->assert('report');
896
897   my $nextsub = shift;
898
899   $form->{project_id} = $form->{project_id_1};
900   if ($form->{projectnumber} && !$form->{project_id}) {
901     $form->{rowcount} = 1;
902
903     # call this instead of update
904     $form->{update}          = $nextsub;
905     $form->{projectnumber_1} = $form->{projectnumber};
906
907     delete $form->{sort};
908     &check_project;
909
910     # if there is one only, assign id
911     $form->{project_id} = $form->{project_id_1};
912   }
913
914   $lxdebug->leave_sub();
915 }
916
917 sub generate_income_statement {
918   $lxdebug->enter_sub();
919
920   $auth->assert('report');
921
922   $form->{padding} = "&nbsp;&nbsp;";
923   $form->{bold}    = "<b>";
924   $form->{endbold} = "</b>";
925   $form->{br}      = "<br>";
926
927   if ($form->{reporttype} eq "custom") {
928
929     #forgotten the year --> thisyear
930     if ($form->{year} !~ m/^\d\d\d\d$/) {
931       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
932         /(\d\d\d\d)/;
933       $form->{year} = $1;
934     }
935
936     #yearly report
937     if ($form->{duetyp} eq "13") {
938       $form->{fromdate} = "1.1.$form->{year}";
939       $form->{todate}   = "31.12.$form->{year}";
940     }
941
942     #Quater reports
943     if ($form->{duetyp} eq "A") {
944       $form->{fromdate} = "1.1.$form->{year}";
945       $form->{todate}   = "31.3.$form->{year}";
946     }
947     if ($form->{duetyp} eq "B") {
948       $form->{fromdate} = "1.4.$form->{year}";
949       $form->{todate}   = "30.6.$form->{year}";
950     }
951     if ($form->{duetyp} eq "C") {
952       $form->{fromdate} = "1.7.$form->{year}";
953       $form->{todate}   = "30.9.$form->{year}";
954     }
955     if ($form->{duetyp} eq "D") {
956       $form->{fromdate} = "1.10.$form->{year}";
957       $form->{todate}   = "31.12.$form->{year}";
958     }
959
960     #Monthly reports
961   SWITCH: {
962       $form->{duetyp} eq "1" && do {
963         $form->{fromdate} = "1.1.$form->{year}";
964         $form->{todate}   = "31.1.$form->{year}";
965         last SWITCH;
966       };
967       $form->{duetyp} eq "2" && do {
968         $form->{fromdate} = "1.2.$form->{year}";
969
970         #this works from 1901 to 2099, 1900 and 2100 fail.
971         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
972         $form->{todate} = "$leap.2.$form->{year}";
973         last SWITCH;
974       };
975       $form->{duetyp} eq "3" && do {
976         $form->{fromdate} = "1.3.$form->{year}";
977         $form->{todate}   = "31.3.$form->{year}";
978         last SWITCH;
979       };
980       $form->{duetyp} eq "4" && do {
981         $form->{fromdate} = "1.4.$form->{year}";
982         $form->{todate}   = "30.4.$form->{year}";
983         last SWITCH;
984       };
985       $form->{duetyp} eq "5" && do {
986         $form->{fromdate} = "1.5.$form->{year}";
987         $form->{todate}   = "31.5.$form->{year}";
988         last SWITCH;
989       };
990       $form->{duetyp} eq "6" && do {
991         $form->{fromdate} = "1.6.$form->{year}";
992         $form->{todate}   = "30.6.$form->{year}";
993         last SWITCH;
994       };
995       $form->{duetyp} eq "7" && do {
996         $form->{fromdate} = "1.7.$form->{year}";
997         $form->{todate}   = "31.7.$form->{year}";
998         last SWITCH;
999       };
1000       $form->{duetyp} eq "8" && do {
1001         $form->{fromdate} = "1.8.$form->{year}";
1002         $form->{todate}   = "31.8.$form->{year}";
1003         last SWITCH;
1004       };
1005       $form->{duetyp} eq "9" && do {
1006         $form->{fromdate} = "1.9.$form->{year}";
1007         $form->{todate}   = "30.9.$form->{year}";
1008         last SWITCH;
1009       };
1010       $form->{duetyp} eq "10" && do {
1011         $form->{fromdate} = "1.10.$form->{year}";
1012         $form->{todate}   = "31.10.$form->{year}";
1013         last SWITCH;
1014       };
1015       $form->{duetyp} eq "11" && do {
1016         $form->{fromdate} = "1.11.$form->{year}";
1017         $form->{todate}   = "30.11.$form->{year}";
1018         last SWITCH;
1019       };
1020       $form->{duetyp} eq "12" && do {
1021         $form->{fromdate} = "1.12.$form->{year}";
1022         $form->{todate}   = "31.12.$form->{year}";
1023         last SWITCH;
1024       };
1025     }
1026   }
1027
1028   RP->income_statement(\%myconfig, \%$form);
1029
1030   ($form->{department}) = split /--/, $form->{department};
1031
1032   $form->{period} =
1033     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1034   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1035
1036   # if there are any dates construct a where
1037   if ($form->{fromdate} || $form->{todate}) {
1038
1039     unless ($form->{todate}) {
1040       $form->{todate} = $form->current_date(\%myconfig);
1041     }
1042
1043     $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
1044     $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
1045
1046     $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
1047     $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
1048
1049     $form->{this_period} = "$shortfromdate\n$shorttodate";
1050     $form->{period}      =
1051         $locale->text('for Period')
1052       . qq|\n$longfromdate |
1053       . $locale->text('Bis')
1054       . qq| $longtodate|;
1055   }
1056
1057   if ($form->{comparefromdate} || $form->{comparetodate}) {
1058     $longcomparefromdate =
1059       $locale->date(\%myconfig, $form->{comparefromdate}, 1);
1060     $shortcomparefromdate =
1061       $locale->date(\%myconfig, $form->{comparefromdate}, 0);
1062
1063     $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
1064     $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
1065
1066     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
1067     $form->{period} .=
1068         "\n$longcomparefromdate "
1069       . $locale->text('Bis')
1070       . qq| $longcomparetodate|;
1071   }
1072
1073   # setup variables for the form
1074   @a = qw(company address businessnumber);
1075   map { $form->{$_} = $myconfig{$_} } @a;
1076
1077   $form->{templates} = $myconfig{templates};
1078
1079   $form->{IN} = "income_statement.html";
1080
1081   $form->parse_template;
1082
1083   $lxdebug->leave_sub();
1084 }
1085
1086 sub generate_balance_sheet {
1087   $lxdebug->enter_sub();
1088
1089   $auth->assert('report');
1090
1091   RP->balance_sheet(\%myconfig, \%$form);
1092
1093   $form->{asofdate} = $form->current_date(\%myconfig) unless $form->{asofdate};
1094   $form->{period} =
1095     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1096
1097   ($form->{department}) = split /--/, $form->{department};
1098
1099   # define Current Earnings account
1100   $padding = ($form->{l_heading}) ? $form->{padding} : "";
1101   push(@{ $form->{equity_account} },
1102        $padding . $locale->text('Current Earnings'));
1103
1104   $form->{this_period} = $locale->date(\%myconfig, $form->{asofdate}, 0);
1105   $form->{last_period} =
1106     $locale->date(\%myconfig, $form->{compareasofdate}, 0);
1107
1108   my $attachment_basename;
1109
1110   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1111
1112   my @hidden_variables = ();
1113   push @hidden_variables, qw(fromdate todate year cash );
1114
1115   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
1116
1117   my %column_defs = (
1118     'accno'                   => { 'text' => $locale->text('Account Number'), },
1119     'description'             => { 'text' => $locale->text('Description'), },
1120     'last_transaction'        => { 'text' => $locale->text('Last Transaction'), },
1121     'soll_eb'                 => { 'text' => $locale->text('Debit Starting Balance'), },
1122     'haben_eb'                => { 'text' => $locale->text('Credit Starting Balance'), },
1123     'soll'                    => { 'text' => $locale->text('Debit'), },
1124     'haben'                   => { 'text' => $locale->text('Credit'), },
1125     'soll_kumuliert'          => { 'text' => $locale->text('Sum Debit'), },
1126     'haben_kumuliert'         => { 'text' => $locale->text('Sum Credit'), },
1127     'soll_saldo'              => { 'text' => $locale->text('Saldo Debit'), },
1128     'haben_saldo'                => { 'text' => $locale->text('Saldo Credit'), }
1129   );
1130
1131
1132
1133   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1134
1135   map { $column_defs{$_}->{visible} =  1 } @columns;
1136
1137   $report->set_columns(%column_defs);
1138   $report->set_column_order(@columns);
1139
1140   $report->set_export_options('trial_balance', @hidden_variables);
1141
1142   $report->set_sort_indicator($form->{sort}, 1);
1143
1144   my @options;
1145
1146
1147   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . "&nbsp; - &nbsp;" . $locale->date(\%myconfig, $form->{todate}, 0);
1148   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
1149
1150   $report->set_options('output_format'        => 'HTML',
1151                        'title'                => $form->{title},
1152                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1153     );
1154   $report->set_options_from_form();
1155   # $form->parse_html_template('report_generator/html_report_bilanz', $variables));
1156   $form->{report_template} = 'report_generator/html_report_bilanz';
1157   # add sort and escape callback, this one we use for the add sub
1158   $form->{callback} = $href .= "&sort=$form->{sort}";
1159
1160   # escape callback for href
1161   $callback = $form->escape($href);
1162
1163   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1164
1165   my %totals    = map { $_ => 0 } @subtotal_columns;
1166
1167   my $edit_url = build_std_url('action=edit', 'type', 'vc');
1168
1169 #   foreach $accno (@{ $form->{TB} }) {
1170
1171 #     $accno->{soll} = $accno->{debit};
1172 #     $accno->{haben} = $accno->{credit};
1173 #     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
1174
1175 #     map { $accno->{$_} = $form->format_amount(\%myconfig, $accno->{$_}, 2) } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1176
1177 #     map { $accno->{$_} = ($accno->{$_} == 0) ? '' : $accno->{$_} } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1178
1179 #     my $row = { };
1180
1181 #     foreach my $column (@columns) {
1182 #       $row->{$column} = {
1183 #         'data'  => $accno->{$column},
1184 #         'align' => $column_alignment{$column},
1185 #       };
1186 #     }
1187
1188
1189 #     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1190
1191 #     my $row_set = [ $row ];
1192
1193
1194 #     $report->add_data($row_set);
1195
1196 #     $idx++;
1197 #   }
1198
1199 #   $report->add_separator();
1200
1201 #   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1202
1203   $report->generate_with_headers();
1204
1205
1206   $lxdebug->leave_sub();
1207 }
1208
1209 sub generate_projects {
1210   $lxdebug->enter_sub();
1211
1212   $auth->assert('report');
1213
1214   &get_project(generate_projects);
1215   $form->{projectnumber} = $form->{projectnumber_1};
1216
1217   $form->{nextsub} = "generate_projects";
1218   $form->{title}   = $locale->text('Project Transactions');
1219   RP->trial_balance(\%myconfig, \%$form);
1220
1221   list_accounts('generate_projects');
1222
1223   $lxdebug->leave_sub();
1224 }
1225
1226 # Antonio Gallardo
1227 #
1228 # D.S. Feb 16, 2001
1229 # included links to display transactions for period entered
1230 # added headers and subtotals
1231 #
1232 sub generate_trial_balance {
1233   $lxdebug->enter_sub();
1234
1235   $auth->assert('report');
1236
1237   if ($form->{reporttype} eq "custom") {
1238
1239     #forgotten the year --> thisyear
1240     if ($form->{year} !~ m/^\d\d\d\d$/) {
1241       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1242         /(\d\d\d\d)/;
1243       $form->{year} = $1;
1244     }
1245
1246     #yearly report
1247     if ($form->{duetyp} eq "13") {
1248       $form->{fromdate} = "1.1.$form->{year}";
1249       $form->{todate}   = "31.12.$form->{year}";
1250     }
1251
1252     #Quater reports
1253     if ($form->{duetyp} eq "A") {
1254       $form->{fromdate} = "1.1.$form->{year}";
1255       $form->{todate}   = "31.3.$form->{year}";
1256     }
1257     if ($form->{duetyp} eq "B") {
1258       $form->{fromdate} = "1.4.$form->{year}";
1259       $form->{todate}   = "30.6.$form->{year}";
1260     }
1261     if ($form->{duetyp} eq "C") {
1262       $form->{fromdate} = "1.7.$form->{year}";
1263       $form->{todate}   = "30.9.$form->{year}";
1264     }
1265     if ($form->{duetyp} eq "D") {
1266       $form->{fromdate} = "1.10.$form->{year}";
1267       $form->{todate}   = "31.12.$form->{year}";
1268     }
1269
1270     #Monthly reports
1271   SWITCH: {
1272       $form->{duetyp} eq "1" && do {
1273         $form->{fromdate} = "1.1.$form->{year}";
1274         $form->{todate}   = "31.1.$form->{year}";
1275         last SWITCH;
1276       };
1277       $form->{duetyp} eq "2" && do {
1278         $form->{fromdate} = "1.2.$form->{year}";
1279
1280         #this works from 1901 to 2099, 1900 and 2100 fail.
1281         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1282         $form->{todate} = "$leap.2.$form->{year}";
1283         last SWITCH;
1284       };
1285       $form->{duetyp} eq "3" && do {
1286         $form->{fromdate} = "1.3.$form->{year}";
1287         $form->{todate}   = "31.3.$form->{year}";
1288         last SWITCH;
1289       };
1290       $form->{duetyp} eq "4" && do {
1291         $form->{fromdate} = "1.4.$form->{year}";
1292         $form->{todate}   = "30.4.$form->{year}";
1293         last SWITCH;
1294       };
1295       $form->{duetyp} eq "5" && do {
1296         $form->{fromdate} = "1.5.$form->{year}";
1297         $form->{todate}   = "31.5.$form->{year}";
1298         last SWITCH;
1299       };
1300       $form->{duetyp} eq "6" && do {
1301         $form->{fromdate} = "1.6.$form->{year}";
1302         $form->{todate}   = "30.6.$form->{year}";
1303         last SWITCH;
1304       };
1305       $form->{duetyp} eq "7" && do {
1306         $form->{fromdate} = "1.7.$form->{year}";
1307         $form->{todate}   = "31.7.$form->{year}";
1308         last SWITCH;
1309       };
1310       $form->{duetyp} eq "8" && do {
1311         $form->{fromdate} = "1.8.$form->{year}";
1312         $form->{todate}   = "31.8.$form->{year}";
1313         last SWITCH;
1314       };
1315       $form->{duetyp} eq "9" && do {
1316         $form->{fromdate} = "1.9.$form->{year}";
1317         $form->{todate}   = "30.9.$form->{year}";
1318         last SWITCH;
1319       };
1320       $form->{duetyp} eq "10" && do {
1321         $form->{fromdate} = "1.10.$form->{year}";
1322         $form->{todate}   = "31.10.$form->{year}";
1323         last SWITCH;
1324       };
1325       $form->{duetyp} eq "11" && do {
1326         $form->{fromdate} = "1.11.$form->{year}";
1327         $form->{todate}   = "30.11.$form->{year}";
1328         last SWITCH;
1329       };
1330       $form->{duetyp} eq "12" && do {
1331         $form->{fromdate} = "1.12.$form->{year}";
1332         $form->{todate}   = "31.12.$form->{year}";
1333         last SWITCH;
1334       };
1335     }
1336   }
1337
1338
1339   # get for each account initial balance, debits and credits
1340   RP->trial_balance(\%myconfig, \%$form);
1341
1342
1343   $form->{rowcount} = scalar @{ $form->{TB} };
1344   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
1345
1346   my @columns = (
1347     "accno",               "description",
1348     "last_transaction",    "soll_eb",
1349     "haben_eb",
1350     "soll",                 "haben",
1351     "soll_kumuliert", "haben_kumuliert",
1352     "soll_saldo", "haben_saldo"
1353   );
1354
1355
1356   my $attachment_basename;
1357   $attachment_basename = $locale->text('trial_balance');
1358   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1359
1360   my @hidden_variables = ();
1361   push @hidden_variables, qw(fromdate todate year cash );
1362
1363   my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
1364
1365   my %column_defs = (
1366     'accno'                   => { 'text' => $locale->text('Account Number'), },
1367     'description'             => { 'text' => $locale->text('Description'), },
1368     'last_transaction'        => { 'text' => $locale->text('Last Transaction'), },
1369     'soll_eb'                 => { 'text' => $locale->text('Debit Starting Balance'), },
1370     'haben_eb'                => { 'text' => $locale->text('Credit Starting Balance'), },
1371     'soll'                    => { 'text' => $locale->text('Debit'), },
1372     'haben'                   => { 'text' => $locale->text('Credit'), },
1373     'soll_kumuliert'          => { 'text' => $locale->text('Sum Debit'), },
1374     'haben_kumuliert'         => { 'text' => $locale->text('Sum Credit'), },
1375     'soll_saldo'              => { 'text' => $locale->text('Saldo Debit'), },
1376     'haben_saldo'                => { 'text' => $locale->text('Saldo Credit'), }
1377   );
1378
1379
1380
1381   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1382
1383   map { $column_defs{$_}->{visible} =  1 } @columns;
1384
1385   $report->set_columns(%column_defs);
1386   $report->set_column_order(@columns);
1387
1388   $report->set_export_options('generate_trial_balance', @hidden_variables);
1389
1390   $report->set_sort_indicator($form->{sort}, 1);
1391
1392   my @options;
1393
1394
1395   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . "&nbsp; - &nbsp;" . $locale->date(\%myconfig, $form->{todate}, 0);
1396   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
1397
1398   $report->set_options('output_format'        => 'HTML',
1399                        'title'                => $form->{title},
1400                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1401                        'html_template'        => 'rp/html_report_susa',
1402                        'pdf_template'         => 'rp/html_report_susa',
1403     );
1404   $report->set_options_from_form();
1405
1406   # add sort and escape callback, this one we use for the add sub
1407   $form->{callback} = $href .= "&sort=$form->{sort}";
1408
1409   # escape callback for href
1410   $callback = $form->escape($href);
1411
1412   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1413
1414   my %totals    = map { $_ => 0 } @subtotal_columns;
1415
1416   my $edit_url = build_std_url('action=edit', 'type', 'vc');
1417
1418   foreach $accno (@{ $form->{TB} }) {
1419
1420     $accno->{soll} = $accno->{debit};
1421     $accno->{haben} = $accno->{credit};
1422     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
1423
1424     map { $accno->{$_} = $form->format_amount(\%myconfig, $accno->{$_}, 2) } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1425
1426     map { $accno->{$_} = ($accno->{$_} == 0) ? '' : $accno->{$_} } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1427
1428     my $row = { };
1429
1430     foreach my $column (@columns) {
1431       $row->{$column} = {
1432         'data'  => $accno->{$column},
1433         'align' => $column_alignment{$column},
1434       };
1435     }
1436
1437
1438     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1439
1440     my $row_set = [ $row ];
1441
1442
1443     $report->add_data($row_set);
1444
1445     $idx++;
1446   }
1447
1448   $report->add_separator();
1449
1450   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1451
1452   $report->generate_with_headers();
1453
1454   $lxdebug->leave_sub();
1455
1456 }
1457
1458 sub create_subtotal_row {
1459   $lxdebug->enter_sub();
1460
1461   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1462
1463   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1464
1465   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1466
1467   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1468
1469   map { $totals->{$_} = 0 } @{ $subtotal_columns };
1470
1471   $lxdebug->leave_sub();
1472
1473   return $row;
1474 }
1475
1476 sub create_list_accounts_subtotal_row {
1477   $lxdebug->enter_sub();
1478
1479   my ($subtotals, $columns, $fields, $class) = @_;
1480
1481   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1482
1483   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
1484
1485   $lxdebug->leave_sub();
1486
1487   return $row;
1488 }
1489
1490 sub list_accounts {
1491   $lxdebug->enter_sub();
1492
1493   my ($action) = @_;
1494
1495   my @options;
1496   if ($form->{department}) {
1497     my ($department) = split /--/, $form->{department};
1498     push @options, $locale->text('Department') . " : $department";
1499   }
1500   if ($form->{projectnumber}) {
1501     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
1502   }
1503
1504   # if there are any dates
1505   if ($form->{fromdate} || $form->{todate}) {
1506     my ($fromdate, $todate);
1507
1508     if ($form->{fromdate}) {
1509       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1510     }
1511     if ($form->{todate}) {
1512       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1513     }
1514
1515     push @options, "$fromdate - $todate";
1516
1517   } else {
1518     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1519   }
1520
1521   my @columns     = qw(accno description begbalance debit credit endbalance);
1522   my %column_defs = (
1523     'accno'       => { 'text' => $locale->text('Account'), },
1524     'description' => { 'text' => $locale->text('Description'), },
1525     'debit'       => { 'text' => $locale->text('Debit'), },
1526     'credit'      => { 'text' => $locale->text('Credit'), },
1527     'begbalance'  => { 'text' => $locale->text('Balance'), },
1528     'endbalance'  => { 'text' => $locale->text('Balance'), },
1529   );
1530   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
1531
1532   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
1533
1534   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
1535
1536   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1537
1538   $report->set_options('top_info_text'         => join("\n", @options),
1539                        'output_format'         => 'HTML',
1540                        'title'                 => $form->{title},
1541                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
1542                        'std_column_visibility' => 1,
1543     );
1544   $report->set_options_from_form();
1545
1546   $report->set_columns(%column_defs);
1547   $report->set_column_order(@columns);
1548
1549   $report->set_export_options($action, @hidden_variables);
1550
1551   $report->set_sort_indicator('accno', 1);
1552
1553   my @totals_columns = qw(credit debit begbalance endbalance);
1554   my %subtotals      = map { $_ => 0 } @totals_columns;
1555   my %totals         = map { $_ => 0 } @totals_columns;
1556   my $found_heading  = 0;
1557   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} };
1558
1559   # sort the whole thing by account numbers and display
1560   foreach my $idx (0 .. scalar(@tb) - 1) {
1561     my $ref  = $tb[$idx];
1562     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
1563
1564     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
1565
1566     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
1567
1568     if ($ref->{charttype} eq 'H') {
1569       next unless ($form->{l_heading});
1570
1571       %subtotals                   = map { $_ => 0 } @totals_columns;
1572       $found_heading               = 1;
1573       $row->{description}->{class} = 'listheading';
1574       $row->{description}->{data}  = $ref->{description};
1575
1576       $report->add_data($row);
1577
1578       next;
1579     }
1580
1581     foreach (qw(debit credit)) {
1582       $subtotals{$_} += $ref->{$_};
1583       $totals{$_}    += $ref->{$_};
1584     }
1585
1586     $subtotals{begbalance} += $ref->{balance} * $ml;
1587     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
1588
1589     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
1590     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
1591
1592     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
1593     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
1594
1595     $report->add_data($row);
1596
1597     if ($form->{l_heading} && $found_heading &&
1598         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
1599       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
1600     }
1601   }
1602
1603   $report->add_separator();
1604
1605   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
1606
1607   $report->generate_with_headers();
1608
1609   $lxdebug->leave_sub();
1610 }
1611
1612 sub generate_ar_aging {
1613   $lxdebug->enter_sub();
1614
1615   $auth->assert('general_ledger');
1616
1617   # split customer
1618   ($form->{customer}) = split(/--/, $form->{customer});
1619
1620   $form->{ct}   = "customer";
1621   $form->{arap} = "ar";
1622
1623   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
1624
1625   RP->aging(\%myconfig, \%$form);
1626   aging();
1627
1628   $lxdebug->leave_sub();
1629 }
1630
1631 sub generate_ap_aging {
1632   $lxdebug->enter_sub();
1633
1634   $auth->assert('general_ledger');
1635
1636   # split vendor
1637   ($form->{vendor}) = split(/--/, $form->{vendor});
1638
1639   $form->{ct}   = "vendor";
1640   $form->{arap} = "ap";
1641
1642   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
1643
1644   RP->aging(\%myconfig, \%$form);
1645   aging();
1646
1647   $lxdebug->leave_sub();
1648 }
1649
1650 sub create_aging_subtotal_row {
1651   $lxdebug->enter_sub();
1652
1653   my ($subtotals, $columns, $periods, $class) = @_;
1654
1655   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1656
1657   foreach (@{ $periods }) {
1658     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
1659     $subtotals->{$_}      = 0;
1660   }
1661
1662   $lxdebug->leave_sub();
1663
1664   return $row;
1665 }
1666
1667 sub aging {
1668   $lxdebug->enter_sub();
1669   print(STDERR "Bin in Aging\n");
1670   $auth->assert('general_ledger');
1671
1672   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1673
1674   my @columns = qw(statement ct invnumber transdate duedate amount open);
1675
1676   my %column_defs = (
1677     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1678     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1679     'invnumber' => { 'text' => $locale->text('Invoice'), },
1680     'transdate' => { 'text' => $locale->text('Date'), },
1681     'duedate'   => { 'text' => $locale->text('Due'), },
1682     'amount'        => { 'text' => $locale->text('Amount'), },
1683     'open'       => { 'text' => $locale->text('Open'), },
1684   );
1685
1686   my %column_alignment = ('statement' => 'center',
1687                           map { $_ => 'right' } qw(open amount));
1688
1689   $report->set_options('std_column_visibility' => 1);
1690   $report->set_columns(%column_defs);
1691   $report->set_column_order(@columns);
1692
1693   my @hidden_variables = qw(todate customer vendor arap title ct);
1694   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1695
1696   my @options;
1697   my $attachment_basename;
1698
1699   if ($form->{department}) {
1700     my ($department) = split /--/, $form->{department};
1701     push @options, $locale->text('Department') . " : $department";
1702     $form->{callback} .= "&department=" . E($department);
1703   }
1704
1705   if (($form->{arap} eq 'ar') && $form->{customer}) {
1706     push @options, $form->{customer};
1707     $attachment_basename = $locale->text('ar_aging_list');
1708     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1709   }
1710
1711   if (($form->{arap} eq 'ap') && $form->{vendor}) {
1712     push @options, $form->{vendor};
1713     $attachment_basename = $locale->text('ap_aging_list');
1714     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1715   }
1716
1717   push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1718
1719   my $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1720
1721   $report->set_options('top_info_text'        => join("\n", @options),
1722                        'output_format'        => 'HTML',
1723                        'title'                => $form->{title},
1724                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1725     );
1726
1727   my $previous_ctid = 0;
1728   my $row_idx       = 0;
1729   my @periods       = qw(open amount);
1730   my %subtotals     = map { $_ => 0 } @periods;
1731   my %totals        = map { $_ => 0 } @periods;
1732
1733   foreach $ref (@{ $form->{AG} }) {
1734     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1735       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1736     }
1737
1738     foreach my $key (@periods) {
1739       $subtotals{$key}  += $ref->{"$key"};
1740       $totals{$key}     += $ref->{"$key"};
1741       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1742     }
1743
1744     my $row = { };
1745
1746     foreach my $column (@columns) {
1747       $row->{$column} = {
1748         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1749         'align'  => $column_alignment{$column},
1750         'valign' => $column eq 'statement' ? 'center' : '',
1751       };
1752     }
1753
1754     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1755
1756     if ($previous_ctid != $ref->{ctid}) {
1757       $row->{statement}->{raw_data} =
1758           $cgi->hidden('-name' => "customer_id_${row_idx}", '-value' => $ref->{ctid})
1759         . $cgi->checkbox('-name' => "statement_${row_idx}", '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1760       $row->{ct}->{data} = $ref->{name};
1761
1762       $row_idx++;
1763     }
1764
1765     $previous_ctid = $ref->{ctid};
1766
1767     $report->add_data($row);
1768   }
1769
1770   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1771
1772   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1773
1774   if ($form->{arap} eq 'ar') {
1775     $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
1776     $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1777                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
1778     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
1779                          'raw_bottom_info_text' => $raw_bottom_info_text);
1780   }
1781
1782   $report->set_options_from_form();
1783
1784   $report->generate_with_headers();
1785
1786   $lxdebug->leave_sub();
1787 }
1788
1789 sub select_all {
1790   $lxdebug->enter_sub();
1791
1792   RP->aging(\%myconfig, \%$form);
1793
1794   map { $_->{checked} = "checked" } @{ $form->{AG} };
1795
1796   &aging;
1797
1798   $lxdebug->leave_sub();
1799 }
1800
1801 sub e_mail {
1802   $lxdebug->enter_sub();
1803
1804   $auth->assert('general_ledger');
1805
1806   # get name and email addresses
1807   for $i (1 .. $form->{rowcount}) {
1808     if ($form->{"statement_$i"}) {
1809       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1810       RP->get_customer(\%myconfig, \%$form);
1811       $selected = 1;
1812       last;
1813     }
1814   }
1815
1816   $form->error($locale->text('Nothing selected!')) unless $selected;
1817
1818   if ($myconfig{role} eq 'admin') {
1819     $bcc = qq|
1820           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1821           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1822 |;
1823   }
1824
1825   $title = $locale->text('E-mail Statement to') . " $form->{$form->{ct}}";
1826
1827   $form->{media} = "email";
1828
1829   $form->header;
1830
1831   print qq|
1832 <body>
1833
1834 <form method=post action=$form->{script}>
1835
1836 <table width=100%>
1837   <tr class=listtop>
1838     <th>$title</th>
1839   </tr>
1840   <tr height="5"></tr>
1841   <tr>
1842     <td>
1843       <table width=100%>
1844         <tr>
1845           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
1846           <td><input name=email size=30 value="$form->{email}"></td>
1847           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1848           <td><input name=cc size=30 value="$form->{cc}"></td>
1849         </tr>
1850         <tr>
1851           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1852           <td><input name=subject size=30 value="$form->{subject}"></td>
1853           $bcc
1854         </tr>
1855       </table>
1856     </td>
1857   </tr>
1858   <tr>
1859     <td>
1860       <table width=100%>
1861         <tr>
1862           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1863         </tr>
1864         <tr>
1865           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1866         </tr>
1867       </table>
1868     </td>
1869   </tr>
1870   <tr>
1871     <td>
1872 |;
1873
1874   &print_options;
1875
1876   map { delete $form->{$_} }
1877     qw(action email cc bcc subject message type sendmode format header);
1878
1879   # save all other variables
1880   foreach $key (keys %$form) {
1881     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1882     $form->{$key} =~ s/\"/&quot;/g;
1883     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1884   }
1885
1886   print qq|
1887     </td>
1888   </tr>
1889   <tr>
1890     <td><hr size=3 noshade></td>
1891   </tr>
1892 </table>
1893
1894 <input type=hidden name=nextsub value=send_email>
1895
1896 <br>
1897 <input name=action class=submit type=submit value="|
1898     . $locale->text('Continue') . qq|">
1899 </form>
1900
1901 </body>
1902 </html>
1903 |;
1904
1905   $lxdebug->leave_sub();
1906 }
1907
1908 sub send_email {
1909   $lxdebug->enter_sub();
1910
1911   $auth->assert('general_ledger');
1912
1913   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1914     unless $form->{subject};
1915
1916   RP->aging(\%myconfig, \%$form);
1917
1918   $form->{"statement_1"} = 1;
1919
1920   $form->{media} = 'email';
1921   print_form();
1922
1923   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1924
1925   $lxdebug->leave_sub();
1926 }
1927
1928 sub print {
1929   $lxdebug->enter_sub();
1930
1931   $auth->assert('general_ledger');
1932
1933   if ($form->{media} eq 'printer') {
1934     $form->error($locale->text('Select postscript or PDF!'))
1935       if ($form->{format} !~ /(postscript|pdf)/);
1936   }
1937
1938   for $i (1 .. $form->{rowcount}) {
1939     if ($form->{"statement_$i"}) {
1940       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1941       $selected = 1;
1942       last;
1943     }
1944   }
1945
1946   $form->error($locale->text('Nothing selected!')) unless $selected;
1947
1948   if ($form->{media} eq 'printer') {
1949     $form->{"$form->{ct}_id"} = "";
1950   } else {
1951     $form->{"statement_1"} = 1;
1952   }
1953
1954   RP->aging(\%myconfig, \%$form);
1955
1956   print_form();
1957
1958   $form->redirect($locale->text('Statements sent to printer!'))
1959     if ($form->{media} eq 'printer');
1960
1961   $lxdebug->leave_sub();
1962 }
1963
1964 sub print_form {
1965   $lxdebug->enter_sub();
1966
1967   $auth->assert('general_ledger');
1968
1969   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1970
1971   $form->{templates} = "$myconfig{templates}";
1972
1973   my $suffix = "html";
1974   my $attachment_suffix = "html";
1975   if ($form->{format} eq 'postscript') {
1976     $form->{postscript} = 1;
1977     $suffix = "tex";
1978     $attachment_suffix = "ps";
1979   } elsif ($form->{format} eq 'pdf') {
1980     $form->{pdf} = 1;
1981     $suffix = "tex";
1982     $attachment_suffix = "pdf";
1983   }
1984
1985   $form->{IN}  = "$form->{type}.$suffix";
1986   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1987
1988   # Save $form->{email} because it will be overwritten.
1989   $form->{EMAIL_RECIPIENT} = $form->{email};
1990
1991   $i = 0;
1992   while (@{ $form->{AG} }) {
1993
1994     $ref = shift @{ $form->{AG} };
1995
1996     if ($ctid != $ref->{ctid}) {
1997
1998       $ctid = $ref->{ctid};
1999       $i++;
2000
2001       if ($form->{"statement_$i"}) {
2002
2003         @a =
2004           (name, street, zipcode, city, country, contact, email,
2005            "$form->{ct}phone", "$form->{ct}fax");
2006         map { $form->{$_} = $ref->{$_} } @a;
2007
2008         $form->{ $form->{ct} } = $form->{name};
2009         $form->{"$form->{ct}_id"} = $ref->{ctid};
2010
2011         map { $form->{$_} = () } qw(invnumber invdate duedate);
2012         $form->{total} = 0;
2013         foreach $item (qw(c0 c30 c60 c90)) {
2014           $form->{$item} = ();
2015           $form->{"${item}total"} = 0;
2016         }
2017
2018         &statement_details($ref);
2019
2020         while ($ref) {
2021
2022           if (scalar(@{ $form->{AG} }) > 0) {
2023
2024             # one or more left to go
2025             if ($ctid == $form->{AG}->[0]->{ctid}) {
2026               $ref = shift @{ $form->{AG} };
2027               &statement_details($ref);
2028
2029               # any more?
2030               $ref = scalar(@{ $form->{AG} });
2031             } else {
2032               $ref = 0;
2033             }
2034           } else {
2035
2036             # set initial ref to 0
2037             $ref = 0;
2038           }
2039
2040         }
2041
2042         map {
2043           $form->{"${_}total"} =
2044             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
2045         } (c0, c30, c60, c90, "");
2046
2047         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
2048         $form->{attachment_filename} =~ s/\s+/_/g;
2049
2050         $form->parse_template(\%myconfig, $userspath);
2051
2052       }
2053     }
2054   }
2055   # saving the history
2056   if(!exists $form->{addition} && $form->{id} ne "") {
2057     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2058         $form->{addition} = "PRINTED";
2059         $form->{what_done} = $form->{type};
2060         $form->save_history($form->dbconnect(\%myconfig));
2061   }
2062   # /saving the history 
2063   $lxdebug->leave_sub();
2064 }
2065
2066 sub statement_details {
2067   $lxdebug->enter_sub();
2068
2069   $auth->assert('general_ledger');
2070
2071   my ($ref) = @_;
2072
2073   push @{ $form->{invnumber} }, $ref->{invnumber};
2074   push @{ $form->{invdate} },   $ref->{transdate};
2075   push @{ $form->{duedate} },   $ref->{duedate};
2076
2077   foreach $item (qw(c0 c30 c60 c90)) {
2078     if ($ref->{exchangerate} * 1) {
2079       $ref->{$item} =
2080         $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2);
2081     }
2082     $form->{"${item}total"} += $ref->{$item};
2083     $form->{total}          += $ref->{$item};
2084     push @{ $form->{$item} },
2085       $form->format_amount(\%myconfig, $ref->{$item}, 2);
2086   }
2087
2088   $lxdebug->leave_sub();
2089 }
2090
2091 sub generate_tax_report {
2092   $lxdebug->enter_sub();
2093
2094   $auth->assert('report');
2095
2096   RP->tax_report(\%myconfig, \%$form);
2097
2098   $descvar     = "$form->{accno}_description";
2099   $description = $form->escape($form->{$descvar});
2100   $ratevar     = "$form->{accno}_rate";
2101
2102   $department = $form->escape($form->{department});
2103
2104   # construct href
2105   $href =
2106     "$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}";
2107
2108   # construct callback
2109   $description = $form->escape($form->{$descvar},   1);
2110   $department  = $form->escape($form->{department}, 1);
2111   $callback    =
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   $title = $form->escape($form->{title});
2115   $href .= "&title=$title";
2116   $title = $form->escape($form->{title}, 1);
2117   $callback .= "&title=$title";
2118
2119   $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
2120
2121   @columns =
2122     $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
2123
2124   foreach $item (@columns) {
2125     if ($form->{"l_$item"} eq "Y") {
2126       push @column_index, $item;
2127
2128       # add column to href and callback
2129       $callback .= "&l_$item=Y";
2130       $href     .= "&l_$item=Y";
2131     }
2132   }
2133
2134   if ($form->{l_subtotal} eq 'Y') {
2135     $callback .= "&l_subtotal=Y";
2136     $href     .= "&l_subtotal=Y";
2137   }
2138
2139   if ($form->{department}) {
2140     ($department) = split /--/, $form->{department};
2141     $option = $locale->text('Department') . " : $department";
2142   }
2143
2144   # if there are any dates
2145   if ($form->{fromdate} || $form->{todate}) {
2146     if ($form->{fromdate}) {
2147       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
2148     }
2149     if ($form->{todate}) {
2150       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
2151     }
2152
2153     $form->{period} = "$fromdate - $todate";
2154   } else {
2155     $form->{period} =
2156       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2157   }
2158
2159   if ($form->{db} eq 'ar') {
2160     $name    = $locale->text('Customer');
2161     $invoice = 'is.pl';
2162     $arap    = 'ar.pl';
2163   }
2164   if ($form->{db} eq 'ap') {
2165     $name    = $locale->text('Vendor');
2166     $invoice = 'ir.pl';
2167     $arap    = 'ap.pl';
2168   }
2169
2170   $option .= "<br>" if $option;
2171   $option .= "$form->{period}";
2172
2173   $column_header{id}        = qq|<th><a class=listheading href=$href&sort=id>| . $locale->text('ID') . qq|</th>|;
2174   $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>| . $locale->text('Invoice') . qq|</th>|;
2175   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>| . $locale->text('Date') . qq|</th>|;
2176   $column_header{netamount} = qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
2177   $column_header{tax}       = qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
2178   $column_header{amount}    = qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
2179
2180   $column_header{name}      = qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
2181
2182   $form->header;
2183
2184   print qq|
2185 <body>
2186
2187 <table width=100%>
2188   <tr>
2189     <th class=listtop colspan=$colspan>$form->{title}</th>
2190   </tr>
2191   <tr height="5"></tr>
2192   <tr>
2193     <td>$option</td>
2194   </tr>
2195   <tr>
2196     <td>
2197       <table width=100%>
2198         <tr class=listheading>
2199 |;
2200
2201   map { print "$column_header{$_}\n" } @column_index;
2202
2203   print qq|
2204         </tr>
2205 |;
2206
2207   # add sort and escape callback
2208   $callback = $form->escape($callback . "&sort=$form->{sort}");
2209
2210   if (@{ $form->{TR} }) {
2211     $sameitem = $form->{TR}->[0]->{ $form->{sort} };
2212   }
2213
2214   foreach $ref (@{ $form->{TR} }) {
2215
2216     $module = ($ref->{invoice}) ? $invoice : $arap;
2217
2218     if ($form->{l_subtotal} eq 'Y') {
2219       if ($sameitem ne $ref->{ $form->{sort} }) {
2220         &tax_subtotal;
2221         $sameitem = $ref->{ $form->{sort} };
2222       }
2223     }
2224
2225     $totalnetamount += $ref->{netamount};
2226     $totaltax       += $ref->{tax};
2227     $ref->{amount} = $ref->{netamount} + $ref->{tax};
2228
2229     $subtotalnetamount += $ref->{netamount};
2230     $subtotaltax       += $ref->{tax};
2231
2232     map {
2233       $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;");
2234     } qw(netamount tax amount);
2235
2236     $column_data{id}        = qq|<td>$ref->{id}</td>|;
2237     $column_data{invnumber} =
2238       qq|<td><a href=$module?action=edit&id=$ref->{id}&callback=$callback>$ref->{invnumber}</a></td>|;
2239     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
2240     $column_data{name}      = qq|<td>$ref->{name}&nbsp;</td>|;
2241
2242     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
2243       qw(netamount tax amount);
2244
2245     $i++;
2246     $i %= 2;
2247     print qq|
2248         <tr class=listrow$i>
2249 |;
2250
2251     map { print "$column_data{$_}\n" } @column_index;
2252
2253     print qq|
2254         </tr>
2255 |;
2256
2257   }
2258
2259   if ($form->{l_subtotal} eq 'Y') {
2260     &tax_subtotal;
2261   }
2262
2263   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
2264
2265   print qq|
2266         </tr>
2267         <tr class=listtotal>
2268 |;
2269
2270   $total =
2271     $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
2272   $totalnetamount =
2273     $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
2274   $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
2275
2276   $column_data{netamount} =
2277     qq|<th class=listtotal align=right>$totalnetamount</th>|;
2278   $column_data{tax}    = qq|<th class=listtotal align=right>$totaltax</th>|;
2279   $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
2280
2281   map { print "$column_data{$_}\n" } @column_index;
2282
2283   print qq|
2284         </tr>
2285       </table>
2286     </td>
2287   </tr>
2288   <tr>
2289     <td><hr size=3 noshade></td>
2290   </tr>
2291 </table>
2292
2293 </body>
2294 </html>
2295 |;
2296
2297   $lxdebug->leave_sub();
2298 }
2299
2300 sub tax_subtotal {
2301   $lxdebug->enter_sub();
2302
2303   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2304
2305   $subtotalnetamount =
2306     $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;");
2307   $subtotaltax = $form->format_amount(\%myconfig, $subtotaltax, 2, "&nbsp;");
2308   $subtotal =
2309     $form->format_amount(\%myconfig, $subtotalnetamount + $subtotaltax,
2310                          2, "&nbsp;");
2311
2312   $column_data{netamount} =
2313     "<th class=listsubtotal align=right>$subtotalnetamount</th>";
2314   $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
2315   $column_data{amount} = "<th class=listsubtotal align=right>$subtotal</th>";
2316
2317   $subtotalnetamount = 0;
2318   $subtotaltax       = 0;
2319
2320   print qq|
2321         <tr class=listsubtotal>
2322 |;
2323   map { print "\n$column_data{$_}" } @column_index;
2324
2325   print qq|
2326         </tr>
2327 |;
2328
2329   $lxdebug->leave_sub();
2330 }
2331
2332 sub list_payments {
2333   $lxdebug->enter_sub();
2334
2335   $auth->assert('cash');
2336
2337   if ($form->{account}) {
2338     ($form->{paymentaccounts}) = split /--/, $form->{account};
2339   }
2340   if ($form->{department}) {
2341     ($department, $form->{department_id}) = split /--/, $form->{department};
2342     $option = $locale->text('Department') . " : $department";
2343   }
2344
2345   RP->payments(\%myconfig, \%$form);
2346
2347   my @hidden_variables = qw(account title department reference source memo fromdate todate
2348                             fx_transaction db prepayment paymentaccounts sort);
2349
2350   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
2351   $form->{callback} = $href;
2352
2353   my @columns     = qw(transdate invnumber name paid source memo);
2354   my %column_defs = (
2355     'name'      => { 'text' => $locale->text('Description'), },
2356     'invnumber' => { 'text' => $locale->text('Reference'), },
2357     'transdate' => { 'text' => $locale->text('Date'), },
2358     'paid'      => { 'text' => $locale->text('Amount'), },
2359     'source'    => { 'text' => $locale->text('Source'), },
2360     'memo'      => { 'text' => $locale->text('Memo'), },
2361   );
2362   my %column_alignment = ('paid' => 'right');
2363
2364   map { $column_defs{$_}->{link} = $href . "&sort=$_" } grep { $_ ne 'paid' } @columns;
2365
2366   my @options;
2367   if ($form->{fromdate}) {
2368     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{fromdate}, 1);
2369   }
2370   if ($form->{todate}) {
2371     push @options, $locale->text('bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{todate}, 1);
2372   }
2373
2374   my $report = SL::ReportGenerator->new(\%myconfig, $form);
2375
2376   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
2377
2378   $report->set_options('top_info_text'         => join("\n", @options),
2379                        'output_format'         => 'HTML',
2380                        'title'                 => $form->{title},
2381                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
2382                        'std_column_visibility' => 1,
2383     );
2384   $report->set_options_from_form();
2385
2386   $report->set_columns(%column_defs);
2387   $report->set_column_order(@columns);
2388
2389   $report->set_export_options('list_payments', @hidden_variables);
2390
2391   $report->set_sort_indicator($form->{sort}, 1);
2392
2393   my $total_paid    = 0;
2394
2395   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
2396     next unless @{ $form->{ $ref->{id} } };
2397
2398     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
2399
2400     my $subtotal_paid = 0;
2401
2402     foreach my $payment (@{ $form->{ $ref->{id} } }) {
2403       my $module = $payment->{module};
2404       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
2405       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
2406
2407       $subtotal_paid += $payment->{paid};
2408       $total_paid    += $payment->{paid};
2409
2410       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
2411
2412       my $row = { };
2413
2414       foreach my $column (@columns) {
2415         $row->{$column} = {
2416           'data'  => $payment->{$column},
2417           'align' => $column_alignment{$column},
2418         };
2419       }
2420
2421       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
2422
2423       $report->add_data($row);
2424     }
2425
2426     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
2427     $row->{paid} = {
2428       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
2429       'align' => 'right',
2430       'class' => 'listsubtotal',
2431     };
2432
2433     $report->add_data($row);
2434   }
2435
2436   $report->add_separator();
2437
2438   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
2439   $row->{paid} = {
2440     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
2441     'align' => 'right',
2442     'class' => 'listtotal',
2443   };
2444
2445   $report->add_data($row);
2446
2447   $report->generate_with_headers();
2448
2449   $lxdebug->leave_sub();
2450 }
2451
2452 sub print_options {
2453   $lxdebug->enter_sub();
2454
2455   my ($dont_print) = @_;
2456
2457   $form->{sendmode} = "attachment";
2458
2459   $form->{"format"} =
2460     $form->{"format"} ? $form->{"format"} :
2461     $myconfig{"template_format"} ? $myconfig{"template_format"} :
2462     "pdf";
2463
2464   $form->{"copies"} =
2465     $form->{"copies"} ? $form->{"copies"} :
2466     $myconfig{"copies"} ? $myconfig{"copies"} :
2467     2;
2468
2469   $form->{PD}{ $form->{type} }     = "selected";
2470   $form->{DF}{ $form->{format} }   = "selected";
2471   $form->{OP}{ $form->{media} }    = "selected";
2472   $form->{SM}{ $form->{sendmode} } = "selected";
2473
2474   $type = qq|
2475             <option value=statement $form->{PD}{statement}>|
2476       . $locale->text('Statement');
2477
2478   if ($form->{media} eq 'email') {
2479     $media = qq|
2480             <option value=attachment $form->{SM}{attachment}>|
2481       . $locale->text('Attachment') . qq|
2482             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
2483   } else {
2484     $media = qq|
2485             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
2486     if ($myconfig{printer} && $latex_templates) {
2487       $media .= qq|
2488             <option value=printer $form->{OP}{printer}>|
2489         . $locale->text('Printer');
2490     }
2491   }
2492
2493   if ($latex_templates) {
2494     $format .= qq|
2495             <option value=html $form->{DF}{html}>| . $locale->text('HTML')
2496       . qq| <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF')
2497       . qq| <option value=postscript $form->{DF}{postscript}>| . $locale->text('Postscript');
2498   }
2499
2500   my $output = qq|
2501 <table>
2502   <tr>
2503     <td><select name=type>$type</select></td>
2504     <td><select name=format>$format</select></td>
2505     <td><select name=media>$media</select></td>
2506 |;
2507
2508   if ($myconfig{printer} && $latex_templates && $form->{media} ne 'email') {
2509     $output .= qq|
2510       <td>| . $locale->text('Copies') . qq|
2511       <input name=copies size=2 value=$form->{copies}></td>
2512 |;
2513   }
2514
2515   $output .= qq|
2516   </tr>
2517 </table>
2518 |;
2519
2520   print $output unless $dont_print;
2521
2522   $lxdebug->leave_sub();
2523
2524   return $output;
2525 }
2526
2527 sub generate_bwa {
2528   $lxdebug->enter_sub();
2529
2530   $auth->assert('report');
2531
2532   $form->{padding} = "&nbsp;&nbsp;";
2533   $form->{bold}    = "<b>";
2534   $form->{endbold} = "</b>";
2535   $form->{br}      = "<br>";
2536
2537   if ($form->{reporttype} eq "custom") {
2538
2539     #forgotten the year --> thisyear
2540     if ($form->{year} !~ m/^\d\d\d\d$/) {
2541       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
2542         /(\d\d\d\d)/;
2543       $form->{year} = $1;
2544     }
2545
2546     #yearly report
2547     if ($form->{duetyp} eq "13") {
2548       $form->{fromdate}        = "1.1.$form->{year}";
2549       $form->{todate}          = "31.12.$form->{year}";
2550       $form->{comparefromdate} = "1.01.$form->{year}";
2551       $form->{comparetodate}   = "31.12.$form->{year}";
2552     }
2553
2554     #Quater reports
2555     if ($form->{duetyp} eq "A") {
2556       $form->{fromdate}        = "1.1.$form->{year}";
2557       $form->{todate}          = "31.3.$form->{year}";
2558       $form->{comparefromdate} = "1.01.$form->{year}";
2559       $form->{comparetodate}   = "31.03.$form->{year}";
2560     }
2561     if ($form->{duetyp} eq "B") {
2562       $form->{fromdate}        = "1.4.$form->{year}";
2563       $form->{todate}          = "30.6.$form->{year}";
2564       $form->{comparefromdate} = "1.01.$form->{year}";
2565       $form->{comparetodate}   = "30.06.$form->{year}";
2566     }
2567     if ($form->{duetyp} eq "C") {
2568       $form->{fromdate}        = "1.7.$form->{year}";
2569       $form->{todate}          = "30.9.$form->{year}";
2570       $form->{comparefromdate} = "1.01.$form->{year}";
2571       $form->{comparetodate}   = "30.09.$form->{year}";
2572     }
2573     if ($form->{duetyp} eq "D") {
2574       $form->{fromdate}        = "1.10.$form->{year}";
2575       $form->{todate}          = "31.12.$form->{year}";
2576       $form->{comparefromdate} = "1.01.$form->{year}";
2577       $form->{comparetodate}   = "31.12.$form->{year}";
2578     }
2579
2580     #Monthly reports
2581   SWITCH: {
2582       $form->{duetyp} eq "1" && do {
2583         $form->{fromdate}        = "1.1.$form->{year}";
2584         $form->{todate}          = "31.1.$form->{year}";
2585         $form->{comparefromdate} = "1.01.$form->{year}";
2586         $form->{comparetodate}   = "31.01.$form->{year}";
2587         last SWITCH;
2588       };
2589       $form->{duetyp} eq "2" && do {
2590         $form->{fromdate} = "1.2.$form->{year}";
2591
2592         #this works from 1901 to 2099, 1900 and 2100 fail.
2593         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
2594         $form->{todate}          = "$leap.2.$form->{year}";
2595         $form->{comparefromdate} = "1.01.$form->{year}";
2596         $form->{comparetodate}   = "$leap.02.$form->{year}";
2597         last SWITCH;
2598       };
2599       $form->{duetyp} eq "3" && do {
2600         $form->{fromdate}        = "1.3.$form->{year}";
2601         $form->{todate}          = "31.3.$form->{year}";
2602         $form->{comparefromdate} = "1.01.$form->{year}";
2603         $form->{comparetodate}   = "31.03.$form->{year}";
2604         last SWITCH;
2605       };
2606       $form->{duetyp} eq "4" && do {
2607         $form->{fromdate}        = "1.4.$form->{year}";
2608         $form->{todate}          = "30.4.$form->{year}";
2609         $form->{comparefromdate} = "1.01.$form->{year}";
2610         $form->{comparetodate}   = "30.04.$form->{year}";
2611         last SWITCH;
2612       };
2613       $form->{duetyp} eq "5" && do {
2614         $form->{fromdate}        = "1.5.$form->{year}";
2615         $form->{todate}          = "31.5.$form->{year}";
2616         $form->{comparefromdate} = "1.01.$form->{year}";
2617         $form->{comparetodate}   = "31.05.$form->{year}";
2618         last SWITCH;
2619       };
2620       $form->{duetyp} eq "6" && do {
2621         $form->{fromdate}        = "1.6.$form->{year}";
2622         $form->{todate}          = "30.6.$form->{year}";
2623         $form->{comparefromdate} = "1.01.$form->{year}";
2624         $form->{comparetodate}   = "30.06.$form->{year}";
2625         last SWITCH;
2626       };
2627       $form->{duetyp} eq "7" && do {
2628         $form->{fromdate}        = "1.7.$form->{year}";
2629         $form->{todate}          = "31.7.$form->{year}";
2630         $form->{comparefromdate} = "1.01.$form->{year}";
2631         $form->{comparetodate}   = "31.07.$form->{year}";
2632         last SWITCH;
2633       };
2634       $form->{duetyp} eq "8" && do {
2635         $form->{fromdate}        = "1.8.$form->{year}";
2636         $form->{todate}          = "31.8.$form->{year}";
2637         $form->{comparefromdate} = "1.01.$form->{year}";
2638         $form->{comparetodate}   = "31.08.$form->{year}";
2639         last SWITCH;
2640       };
2641       $form->{duetyp} eq "9" && do {
2642         $form->{fromdate}        = "1.9.$form->{year}";
2643         $form->{todate}          = "30.9.$form->{year}";
2644         $form->{comparefromdate} = "1.01.$form->{year}";
2645         $form->{comparetodate}   = "30.09.$form->{year}";
2646         last SWITCH;
2647       };
2648       $form->{duetyp} eq "10" && do {
2649         $form->{fromdate}        = "1.10.$form->{year}";
2650         $form->{todate}          = "31.10.$form->{year}";
2651         $form->{comparefromdate} = "1.01.$form->{year}";
2652         $form->{comparetodate}   = "31.10.$form->{year}";
2653         last SWITCH;
2654       };
2655       $form->{duetyp} eq "11" && do {
2656         $form->{fromdate}        = "1.11.$form->{year}";
2657         $form->{todate}          = "30.11.$form->{year}";
2658         $form->{comparefromdate} = "1.01.$form->{year}";
2659         $form->{comparetodate}   = "30.11.$form->{year}";
2660         last SWITCH;
2661       };
2662       $form->{duetyp} eq "12" && do {
2663         $form->{fromdate}        = "1.12.$form->{year}";
2664         $form->{todate}          = "31.12.$form->{year}";
2665         $form->{comparefromdate} = "1.01.$form->{year}";
2666         $form->{comparetodate}   = "31.12.$form->{year}";
2667         last SWITCH;
2668       };
2669     }
2670   } else {
2671     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
2672     $form->{fromdate} = "${dd}.${mm}.${yy}";
2673     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{todate});
2674     $form->{todate}          = "${dd}.${mm}.${yy}";
2675     $form->{comparefromdate} = "01.01.$yy";
2676     $form->{comparetodate}   = $form->{todate};
2677   }
2678
2679   RP->bwa(\%myconfig, \%$form);
2680
2681   ($form->{department}) = split /--/, $form->{department};
2682
2683   $form->{period} =
2684     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2685   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
2686
2687   # if there are any dates construct a where
2688   if ($form->{fromdate} || $form->{todate}) {
2689
2690     unless ($form->{todate}) {
2691       $form->{todate} = $form->current_date(\%myconfig);
2692     }
2693
2694     my %germandate = ("dateformat" => "dd.mm.yyyy");
2695
2696     $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
2697     $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
2698
2699     $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
2700     $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
2701
2702     $form->{this_period} = "$shortfromdate\n$shorttodate";
2703     $form->{period}      =
2704         $locale->text('for Period')
2705       . qq|\n$longfromdate |
2706       . $locale->text('bis')
2707       . qq| $longtodate|;
2708   }
2709
2710   # setup variables for the form
2711   @a = qw(company address businessnumber);
2712   map { $form->{$_} = $myconfig{$_} } @a;
2713   $form->{templates} = $myconfig{templates};
2714
2715   $form->{IN} = "bwa.html";
2716
2717   $form->parse_template;
2718
2719   $lxdebug->leave_sub();
2720 }
2721