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