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