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