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