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