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