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