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