Tabs aus *.pl Dateien entfernt.
[kivitendo-erp.git] / bin / mozilla / ca.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) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # module for Chart of Accounts, Income Statement and Balance Sheet
31 # search and edit transactions posted by the GL, AR and AP
32 #
33 #======================================================================
34
35 use POSIX qw(strftime);
36
37 use SL::CA;
38 use SL::ReportGenerator;
39
40 require "bin/mozilla/reportgenerator.pl";
41
42 use strict;
43
44 1;
45
46 # end of main
47
48 # this is for our long dates
49 # $locale->text('January')
50 # $locale->text('February')
51 # $locale->text('March')
52 # $locale->text('April')
53 # $locale->text('May ')
54 # $locale->text('June')
55 # $locale->text('July')
56 # $locale->text('August')
57 # $locale->text('September')
58 # $locale->text('October')
59 # $locale->text('November')
60 # $locale->text('December')
61
62 # this is for our short month
63 # $locale->text('Jan')
64 # $locale->text('Feb')
65 # $locale->text('Mar')
66 # $locale->text('Apr')
67 # $locale->text('May')
68 # $locale->text('Jun')
69 # $locale->text('Jul')
70 # $locale->text('Aug')
71 # $locale->text('Sep')
72 # $locale->text('Oct')
73 # $locale->text('Nov')
74 # $locale->text('Dec')
75
76 sub chart_of_accounts {
77   $main::lxdebug->enter_sub();
78
79   my $form     = $main::form;
80   my %myconfig = %main::myconfig;
81   my $locale   = $main::locale;
82
83   $main::auth->assert('report');
84
85   $form->{title} = $locale->text('Chart of Accounts');
86
87   if ($main::eur) {
88     $form->{method} = "cash";
89   }
90
91   CA->all_accounts(\%myconfig, \%$form);
92
93   my @columns     = qw(accno description debit credit);
94   my %column_defs = (
95     'accno'       => { 'text' => $locale->text('Account'), },
96     'description' => { 'text' => $locale->text('Description'), },
97     'debit'       => { 'text' => $locale->text('Debit'), },
98     'credit'      => { 'text' => $locale->text('Credit'), },
99   );
100
101   my $report = SL::ReportGenerator->new(\%myconfig, $form);
102
103   $report->set_options('output_format'         => 'HTML',
104                        'title'                 => $form->{title},
105                        'attachment_basename'   => $locale->text('chart_of_accounts') . strftime('_%Y%m%d', localtime time),
106                        'std_column_visibility' => 1,
107     );
108   $report->set_options_from_form();
109
110   $report->set_columns(%column_defs);
111   $report->set_column_order(@columns);
112
113   $report->set_export_options('chart_of_accounts');
114
115   $report->set_sort_indicator($form->{sort}, 1);
116
117   my %totals = ('debit' => 0, 'credit' => 0);
118
119   foreach my $ca (@{ $form->{CA} }) {
120     next unless defined $ca->{amount};
121     my $row = { };
122
123     foreach (qw(debit credit)) {
124       $totals{$_} += $ca->{$_} * 1;
125       $ca->{$_}    = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_});
126     }
127
128     map { $row->{$_} = { 'data' => $ca->{$_} } } @columns;
129
130     map { $row->{$_}->{align} = 'right'       } qw(debit credit);
131     map { $row->{$_}->{class} = 'listheading' } @columns if ($ca->{charttype} eq "H");
132
133     $row->{accno}->{link} = build_std_url('action=list', 'accno=' . E($ca->{accno}), 'description=' . E($ca->{description}));
134
135     $report->add_data($row);
136   }
137
138   my $row = { map { $_ => { 'class' => 'listtotal', 'align' => 'right' } } @columns };
139   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } qw(debit credit);
140
141   $report->add_separator();
142   $report->add_data($row);
143
144   $report->generate_with_headers();
145
146   $main::lxdebug->leave_sub();
147 }
148
149 sub list {
150   $main::lxdebug->enter_sub();
151
152   my $form     = $main::form;
153   my %myconfig = %main::myconfig;
154   my $locale   = $main::locale;
155
156   $main::auth->assert('report');
157
158   $form->{title} = $locale->text('List Transactions');
159   $form->{title} .= " - " . $locale->text('Account') . " $form->{accno}";
160   my $year = (localtime)[5] + 1900;
161
162   # get departments
163   $form->all_departments(\%myconfig);
164   if (@{ $form->{all_departments} || [] }) {
165     $form->{selectdepartment} = "<option>\n";
166
167     map {
168       $form->{selectdepartment} .=
169         "<option>$_->{description}--$_->{id}\n"
170     } (@{ $form->{all_departments} || [] });
171   }
172
173   my $department = qq|
174         <tr>
175           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
176           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
177         </tr>
178 | if $form->{selectdepartment};
179   my $accrual = ($main::eur) ? ""        : "checked";
180   my $cash    = ($main::eur) ? "checked" : "";
181
182   my $name_1    = "fromdate";
183   my $id_1      = "fromdate";
184   my $value_1   = "$form->{fromdate}";
185   my $trigger_1 = "trigger1";
186   my $name_2    = "todate";
187   my $id_2      = "todate";
188   my $value_2   = "";
189   my $trigger_2 = "trigger2";
190
191   my ($button1, $button1_2, $button2, $button2_2, $jsscript);
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}" onBlur=\"check_right_date_format(this)\" value="$value_1">|;
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   my $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
243
244   $form->header;
245
246   $form->{description} =~ s/\"/&quot;/g;
247
248   print qq|
249 <body onLoad="$onload">
250
251 <form method=post action=$form->{script}>
252
253 <input type=hidden name=accno value=$form->{accno}>
254 <input type=hidden name=description value="$form->{description}">
255 <input type=hidden name=sort value=transdate>
256 <input type=hidden name=eur value=$main::eur>
257 <input type=hidden name=accounttype value=$form->{accounttype}>
258
259 <table border=0 width=100%>
260   <tr>
261     <th class=listtop>$form->{title}</th>
262   </tr>
263
264 </table>
265 <table>
266         <tr>
267           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
268       . $locale->text('Customized Report') . qq|</th>
269         </tr>
270         <tr>
271           <th colspan=1>| . $locale->text('Year') . qq|</th>
272           <td><input name=year size=11 title="|
273       . $locale->text('YYYY') . qq|" value="$year"></td>
274         </tr>
275 |;
276
277     our $checked;
278     print qq|
279         <tr>
280                 <td align=right>
281 <b> | . $locale->text('Yearly') . qq|</b> </td>
282                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
283                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
284         </tr>
285         <tr>
286                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
287 $checked></td>
288                 <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. | . $locale->text('Quarter') . qq|</td>
289 |;
290     $checked = "checked";
291     print qq|
292                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;| . $locale->text('January') . qq|</td>
293 |;
294     $checked = "";
295     print qq|
296                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;| . $locale->text('May') . qq|</td>
297                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;| . $locale->text('September') . qq|</td>
298
299         </tr>
300         <tr>
301                 <td align= right>&nbsp;</td>
302                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. | . $locale->text('Quarter') . qq|</td>
303                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;| . $locale->text('February') . qq|</td>
304                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;| . $locale->text('June') . qq|</td>
305                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;| . $locale->text('October') . qq|</td>
306         </tr>
307         <tr>
308                 <td> &nbsp;</td>
309                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. | . $locale->text('Quarter') . qq|</td>
310                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;| . $locale->text('March') . qq|</td>
311                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;| . $locale->text('July') . qq|</td>
312                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;| . $locale->text('November') . qq|</td>
313
314         </tr>
315         <tr>
316                 <td> &nbsp;</td>
317                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. | . $locale->text('Quarter') . qq|&nbsp;</td>
318                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;| . $locale->text('April') . qq|</td>
319                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;| . $locale->text('August') . qq|</td>
320                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;| . $locale->text('December') . qq|</td>
321
322         </tr>
323         <tr>
324                    <td colspan=5><hr size=3 noshade></td>
325         </tr>
326         <tr>
327           <th align=left><input name=reporttype class=radio type=radio value="free" $checked> | . $locale->text('Free report period') . qq|</th>
328           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
329               $button1
330               $button1_2&nbsp;
331               | . $locale->text('Bis') . qq|&nbsp;
332               $button2
333               $button2_2
334           </td>
335         </tr>
336         <tr>
337                    <td colspan=5><hr size=3 noshade></td>
338         </tr>
339         <tr>
340           <th align=leftt>| . $locale->text('Method') . qq|</th>
341           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
342           &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
343         </tr>
344         <tr>
345          <th align=right colspan=4>| . $locale->text('Decimalplaces') . qq|</th>
346              <td><input name=decimalplaces size=3 value="2"></td>
347          </tr>
348          <tr>
349             <td><input name="subtotal" class=checkbox type=checkbox value=1> | . $locale->text('Subtotal') . qq|</td>
350          </tr>
351
352 $jsscript
353   <tr><td colspan=5 ><hr size=3 noshade></td></tr>
354 </table>
355
356 <br><input class=submit type=submit name=action value="|
357     . $locale->text('List Transactions') . qq|">
358 </form>
359
360 </body>
361 </html>
362 |;
363
364   $main::lxdebug->leave_sub();
365 }
366
367 sub format_debit_credit {
368   $main::lxdebug->enter_sub();
369
370   my $dc = shift;
371
372   my $form     = $main::form;
373   my %myconfig = %main::myconfig;
374   my $locale   = $main::locale;
375
376   my $formatted_dc  = $form->format_amount(\%myconfig, abs($dc), 2) . ' ';
377   $formatted_dc    .= ($dc > 0) ? $locale->text('Credit (one letter abbreviation)') : $locale->text('Debit (one letter abbreviation)');
378
379   $main::lxdebug->leave_sub();
380
381   return $formatted_dc;
382 }
383
384
385 sub list_transactions {
386   $main::lxdebug->enter_sub();
387
388   my $form     = $main::form;
389   my %myconfig = %main::myconfig;
390   my $locale   = $main::locale;
391
392   $main::auth->assert('report');
393
394   $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
395
396   if ($form->{reporttype} eq "custom") {
397
398     #forgotten the year --> thisyear
399     if ($form->{year} !~ m/^\d\d\d\d$/) {
400       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
401         /(\d\d\d\d)/;
402       $form->{year} = $1;
403     }
404
405     #yearly report
406     if ($form->{duetyp} eq "13") {
407       $form->{fromdate} = "1.1.$form->{year}";
408       $form->{todate}   = "31.12.$form->{year}";
409     }
410
411     #Quater reports
412     if ($form->{duetyp} eq "A") {
413       $form->{fromdate} = "1.1.$form->{year}";
414       $form->{todate}   = "31.3.$form->{year}";
415     }
416     if ($form->{duetyp} eq "B") {
417       $form->{fromdate} = "1.4.$form->{year}";
418       $form->{todate}   = "30.6.$form->{year}";
419     }
420     if ($form->{duetyp} eq "C") {
421       $form->{fromdate} = "1.7.$form->{year}";
422       $form->{todate}   = "30.9.$form->{year}";
423     }
424     if ($form->{duetyp} eq "D") {
425       $form->{fromdate} = "1.10.$form->{year}";
426       $form->{todate}   = "31.12.$form->{year}";
427     }
428
429     #Monthly reports
430   SWITCH: {
431       $form->{duetyp} eq "1" && do {
432         $form->{fromdate} = "1.1.$form->{year}";
433         $form->{todate}   = "31.1.$form->{year}";
434         last SWITCH;
435       };
436       $form->{duetyp} eq "2" && do {
437         $form->{fromdate} = "1.2.$form->{year}";
438
439         #this works from 1901 to 2099, 1900 and 2100 fail.
440         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
441         $form->{todate} = "$leap.2.$form->{year}";
442         last SWITCH;
443       };
444       $form->{duetyp} eq "3" && do {
445         $form->{fromdate} = "1.3.$form->{year}";
446         $form->{todate}   = "31.3.$form->{year}";
447         last SWITCH;
448       };
449       $form->{duetyp} eq "4" && do {
450         $form->{fromdate} = "1.4.$form->{year}";
451         $form->{todate}   = "30.4.$form->{year}";
452         last SWITCH;
453       };
454       $form->{duetyp} eq "5" && do {
455         $form->{fromdate} = "1.5.$form->{year}";
456         $form->{todate}   = "31.5.$form->{year}";
457         last SWITCH;
458       };
459       $form->{duetyp} eq "6" && do {
460         $form->{fromdate} = "1.6.$form->{year}";
461         $form->{todate}   = "30.6.$form->{year}";
462         last SWITCH;
463       };
464       $form->{duetyp} eq "7" && do {
465         $form->{fromdate} = "1.7.$form->{year}";
466         $form->{todate}   = "31.7.$form->{year}";
467         last SWITCH;
468       };
469       $form->{duetyp} eq "8" && do {
470         $form->{fromdate} = "1.8.$form->{year}";
471         $form->{todate}   = "31.8.$form->{year}";
472         last SWITCH;
473       };
474       $form->{duetyp} eq "9" && do {
475         $form->{fromdate} = "1.9.$form->{year}";
476         $form->{todate}   = "30.9.$form->{year}";
477         last SWITCH;
478       };
479       $form->{duetyp} eq "10" && do {
480         $form->{fromdate} = "1.10.$form->{year}";
481         $form->{todate}   = "31.10.$form->{year}";
482         last SWITCH;
483       };
484       $form->{duetyp} eq "11" && do {
485         $form->{fromdate} = "1.11.$form->{year}";
486         $form->{todate}   = "30.11.$form->{year}";
487         last SWITCH;
488       };
489       $form->{duetyp} eq "12" && do {
490         $form->{fromdate} = "1.12.$form->{year}";
491         $form->{todate}   = "31.12.$form->{year}";
492         last SWITCH;
493       };
494     }
495   }
496
497   CA->all_transactions(\%myconfig, \%$form);
498
499   $form->{saldo_old} += $form->{beginning_balance};
500   $form->{saldo_new} += $form->{beginning_balance};
501   my $saldo_old = format_debit_credit($form->{saldo_old});
502   my $eb_string = format_debit_credit($form->{beginning_balance});
503   $form->{balance} = $form->{saldo_old};
504
505   my @options;
506   if ($form->{department}) {
507     my ($department) = split /--/, $form->{department};
508     push @options, $locale->text('Department') . " : $department";
509   }
510   if ($form->{projectnumber}) {
511     push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
512   }
513
514   my $period;
515   if ($form->{fromdate} || $form->{todate}) {
516     my ($fromdate, $todate);
517
518     if ($form->{fromdate}) {
519       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
520     }
521     if ($form->{todate}) {
522       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
523     }
524
525     $period = "$fromdate - $todate";
526
527   } else {
528     $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
529   }
530
531   push @options, $period;
532
533   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
534   push (@options, $form->{print_date});
535
536   $form->{company} = $locale->text('Company') . " " . $myconfig{company};
537   push (@options, $form->{company});
538
539   my @columns     = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
540   my %column_defs = (
541     'transdate'   => { 'text' => $locale->text('Date'), },
542     'reference'   => { 'text' => $locale->text('Reference'), },
543     'description' => { 'text' => $locale->text('Description'), },
544     'debit'       => { 'text' => $locale->text('Debit'), },
545     'credit'      => { 'text' => $locale->text('Credit'), },
546     'gegenkonto'     => { 'text' => $locale->text('Gegenkonto'), },
547     'ustkonto'     => { 'text' => $locale->text('USt-Konto'), },
548     'balance'          => { 'text' => $locale->text('Balance'), },
549     'ustrate'     => { 'text' => $locale->text('Satz %'), },
550  );
551
552   my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort);
553
554   my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
555
556   $form->{callback} = $link . '&sort=' . E($form->{sort});
557
558   my %column_alignment = map { $_ => 'right' } qw(debit credit);
559
560   my @custom_headers = ();
561  # Zeile 1:
562  push @custom_headers, [
563    { 'text' => 'Letzte Buchung', },
564    { 'text' => 'EB-Wert', },
565    { 'text' => 'Saldo alt', 'colspan' => 2, },
566    { 'text' => 'Jahresverkehrszahlen alt', 'colspan' => 2, },
567    { 'text' => '', 'colspan' => 2, },
568  ];
569  push @custom_headers, [
570    { 'text' => $form->{last_transaction}, },
571    { 'text' => $eb_string, },
572    { 'text' => $saldo_old, 'colspan' => 2, },
573    { 'text' => $form->format_amount(\%myconfig, abs($form->{old_balance_debit}), 2) . " S", },
574    { 'text' => $form->format_amount(\%myconfig, $form->{old_balance_credit}, 2) . " H", },
575    { 'text' => '', 'colspan' => 2, },
576  ];
577  # Zeile 2:
578  push @custom_headers, [
579    { 'text' => $locale->text('Date'), 'link' => $link . "&sort=transdate", },
580    { 'text' => $locale->text('Reference'), 'link' => $link . "&sort=reference",  },
581    { 'text' => $locale->text('Description'), 'link' => $link . "&sort=description",  },
582    { 'text' => $locale->text('Gegenkonto'), },
583    { 'text' => $locale->text('Debit'), },
584    { 'text' => $locale->text('Credit'), },
585    { 'text' => $locale->text('USt-Konto'), },
586    { 'text' => $locale->text('Satz %'), },
587    { 'text' => $locale->text('Balance'), },
588  ];
589
590
591
592
593
594   my $report = SL::ReportGenerator->new(\%myconfig, $form);
595   $report->set_custom_headers(@custom_headers);
596
597   $report->set_options('top_info_text'         => join("\n", @options),
598                        'output_format'         => 'HTML',
599                        'title'                 => $form->{title},
600                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
601                        'std_column_visibility' => 1,
602     );
603   $report->set_options_from_form();
604
605   $report->set_columns(%column_defs);
606   $report->set_column_order(@columns);
607
608   $report->set_export_options('list_transactions', @hidden_variables);
609
610   $report->set_sort_indicator($form->{sort}, 1);
611
612   $column_defs{balance}->{visible} = 1;
613
614   my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
615
616
617   my $idx       = 0;
618   my %totals    = ( 'debit' => 0, 'credit' => 0 );
619   my %subtotals = ( 'debit' => 0, 'credit' => 0 );
620   my ($previous_index, $row_set);
621
622   foreach my $ca (@{ $form->{CA} }) {
623
624     foreach (qw(debit credit)) {
625       $subtotals{$_} += $ca->{$_};
626       $totals{$_}    += $ca->{$_};
627       if ($_ =~ /debit.*/) {
628         $ml = -1;
629       } else {
630         $ml = 1;
631       }
632       $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
633       $ca->{$_}       = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
634     }
635
636     my $do_subtotal = 0;
637     if (($form->{subtotal})
638         && (($idx == scalar @{ $form->{CA} } - 1)
639             || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
640       $do_subtotal = 1;
641     }
642
643     my $row = { };
644
645     $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
646
647     if ($ca->{memo} ne "") {
648       $ca->{description} .= " \n " . $ca->{memo};
649     }
650
651
652
653     foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
654       if ($ca->{gegenkonto} eq "") {
655         $ca->{gegenkonto} = $gegenkonto->{accno};
656       } else {
657         $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
658       }
659     }
660
661     foreach (@columns) {
662       $row->{$_} = {
663         'data'  => $ca->{$_},
664         'align' => $column_alignment{$_},
665       };
666     }
667
668     $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
669
670     if ($ca->{index} ne $previous_index) {
671 #       $report->add_data($row_set) if ($row_set);
672
673 #       $row_set         = [ ];
674       $previous_index  = $ca->{index};
675
676       $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
677
678     } elsif ($ca->{index} eq $previous_index) {
679       map { $row->{$_}->{data} = '' } qw(reference description);
680       $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
681     }
682
683     my $row_set = [];
684
685     push @{ $row_set }, $row;
686
687     push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
688
689
690     $idx++;
691     $report->add_data($row_set);
692
693   }
694
695   $report->add_data($row_set) if ($row_set);
696
697   $report->add_separator();
698
699   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
700
701
702   $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
703
704   $report->add_data($row);
705
706
707   $report->add_separator();
708   $row = {
709      'transdate' => {
710        'data'    => "",
711        'class' => 'listtotal',
712      },
713      'reference' => {
714        'data'    => $locale->text('EB-Wert'),
715        'class' => 'listtotal',
716      },
717      'description'      => {
718        'data'    => $locale->text('Saldo neu'),
719        'colspan' => 2,
720        'class' => 'listtotal',
721      },
722      'debit'      => {
723        'data'    => $locale->text('Jahresverkehrszahlen neu'),
724        'colspan' => 2,
725        'align' => 'left',
726        'class' => 'listtotal',
727     },
728      'ustkonto'      => {
729        'data'    => '',
730        'colspan' => 2,
731        'align' => 'left',
732        'class' => 'listtotal',
733     },
734   };
735
736   $report->add_data($row);
737   my $saldo_new = format_debit_credit($form->{saldo_new});
738   $row = {
739      'transdate' => {
740        'data'    => "",
741        'class' => 'listtotal',
742      },
743      'reference' => {
744        'data'    => $eb_string,
745        'class' => 'listtotal',
746      },
747      'description'      => {
748        'data'    => $saldo_new,
749        'colspan' => 2,
750        'class' => 'listtotal',
751      },
752      'debit'      => {
753        'data'    => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
754        'class' => 'listtotal',
755      },
756       'credit'      => {
757        'data'    => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
758        'class' => 'listtotal',
759      },
760       'ustkonto'      => {
761        'data'    => "",
762        'colspan' => 2,
763        'class' => 'listtotal',
764      },
765   };
766
767   $report->add_data($row);
768
769   $report->generate_with_headers();
770
771   $main::lxdebug->leave_sub();
772 }
773
774 sub create_subtotal_row {
775   $main::lxdebug->enter_sub();
776
777   my $form     = $main::form;
778   my %myconfig = %main::myconfig;
779
780   my ($totals, $columns, $column_alignment, $class) = @_;
781
782   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
783
784   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
785
786   map { $totals->{$_} = 0 } qw(debit credit);
787
788   $main::lxdebug->leave_sub();
789
790   return $row;
791 }