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