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