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