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