ca strict
[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. |
289       . $locale->text('Quarter') . qq|</td>
290 |;
291     $checked = "checked";
292     print qq|
293                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;|
294       . $locale->text('January') . qq|</td>
295 |;
296     $checked = "";
297     print qq|
298                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;|
299       . $locale->text('May') . qq|</td>
300                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;|
301       . $locale->text('September') . qq|</td>
302
303         </tr>
304         <tr>
305                 <td align= right>&nbsp;</td>
306                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. |
307       . $locale->text('Quarter') . qq|</td>
308                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;|
309       . $locale->text('February') . qq|</td>
310                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;|
311       . $locale->text('June') . qq|</td>
312                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;|
313       . $locale->text('October') . qq|</td>
314         </tr>
315         <tr>
316                 <td> &nbsp;</td>
317                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. |
318       . $locale->text('Quarter') . qq|</td>
319                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;|
320       . $locale->text('March') . qq|</td>
321                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;|
322       . $locale->text('July') . qq|</td>
323                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;|
324       . $locale->text('November') . qq|</td>
325
326         </tr>
327         <tr>
328                 <td> &nbsp;</td>
329                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. |
330       . $locale->text('Quarter') . qq|&nbsp;</td>
331                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;|
332       . $locale->text('April') . qq|</td>
333                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;|
334       . $locale->text('August') . qq|</td>
335                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;|
336       . $locale->text('December') . qq|</td>
337
338         </tr>
339         <tr>
340                 <td colspan=5><hr size=3 noshade></td>
341         </tr>
342         <tr>
343           <th align=left><input name=reporttype class=radio type=radio value="free" $checked> |
344       . $locale->text('Free report period') . qq|</th>
345           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
346               $button1
347               $button1_2&nbsp;
348               | . $locale->text('Bis') . qq|&nbsp;
349               $button2
350               $button2_2
351           </td>
352         </tr>
353         <tr>
354                 <td colspan=5><hr size=3 noshade></td>
355         </tr>
356         <tr>
357           <th align=leftt>| . $locale->text('Method') . qq|</th>
358           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
359       . $locale->text('Accrual') . qq|
360           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
361       . $locale->text('EUR') . qq|</td>
362         </tr>
363         <tr>
364          <th align=right colspan=4>|
365       . $locale->text('Decimalplaces')
366       . qq|</th>
367              <td><input name=decimalplaces size=3 value="2"></td>
368          </tr>
369          <tr>
370             <td><input name="subtotal" class=checkbox type=checkbox value=1> | . $locale->text('Subtotal') . qq|</td>
371          </tr>
372
373 $jsscript
374   <tr><td colspan=5 ><hr size=3 noshade></td></tr>
375 </table>
376
377 <br><input class=submit type=submit name=action value="|
378     . $locale->text('List Transactions') . qq|">
379 </form>
380
381 </body>
382 </html>
383 |;
384
385   $main::lxdebug->leave_sub();
386 }
387
388 sub format_debit_credit {
389   $main::lxdebug->enter_sub();
390
391   my $dc = shift;
392
393   my $form     = $main::form;
394   my %myconfig = %main::myconfig;
395   my $locale   = $main::locale;
396
397   my $formatted_dc  = $form->format_amount(\%myconfig, abs($dc), 2) . ' ';
398   $formatted_dc    .= ($dc > 0) ? $locale->text('Credit (one letter abbreviation)') : $locale->text('Debit (one letter abbreviation)');
399
400   $main::lxdebug->leave_sub();
401
402   return $formatted_dc;
403 }
404
405
406 sub list_transactions {
407   $main::lxdebug->enter_sub();
408
409   my $form     = $main::form;
410   my %myconfig = %main::myconfig;
411   my $locale   = $main::locale;
412
413   $main::auth->assert('report');
414
415   $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
416
417   if ($form->{reporttype} eq "custom") {
418
419     #forgotten the year --> thisyear
420     if ($form->{year} !~ m/^\d\d\d\d$/) {
421       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
422         /(\d\d\d\d)/;
423       $form->{year} = $1;
424     }
425
426     #yearly report
427     if ($form->{duetyp} eq "13") {
428       $form->{fromdate} = "1.1.$form->{year}";
429       $form->{todate}   = "31.12.$form->{year}";
430     }
431
432     #Quater reports
433     if ($form->{duetyp} eq "A") {
434       $form->{fromdate} = "1.1.$form->{year}";
435       $form->{todate}   = "31.3.$form->{year}";
436     }
437     if ($form->{duetyp} eq "B") {
438       $form->{fromdate} = "1.4.$form->{year}";
439       $form->{todate}   = "30.6.$form->{year}";
440     }
441     if ($form->{duetyp} eq "C") {
442       $form->{fromdate} = "1.7.$form->{year}";
443       $form->{todate}   = "30.9.$form->{year}";
444     }
445     if ($form->{duetyp} eq "D") {
446       $form->{fromdate} = "1.10.$form->{year}";
447       $form->{todate}   = "31.12.$form->{year}";
448     }
449
450     #Monthly reports
451   SWITCH: {
452       $form->{duetyp} eq "1" && do {
453         $form->{fromdate} = "1.1.$form->{year}";
454         $form->{todate}   = "31.1.$form->{year}";
455         last SWITCH;
456       };
457       $form->{duetyp} eq "2" && do {
458         $form->{fromdate} = "1.2.$form->{year}";
459
460         #this works from 1901 to 2099, 1900 and 2100 fail.
461         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
462         $form->{todate} = "$leap.2.$form->{year}";
463         last SWITCH;
464       };
465       $form->{duetyp} eq "3" && do {
466         $form->{fromdate} = "1.3.$form->{year}";
467         $form->{todate}   = "31.3.$form->{year}";
468         last SWITCH;
469       };
470       $form->{duetyp} eq "4" && do {
471         $form->{fromdate} = "1.4.$form->{year}";
472         $form->{todate}   = "30.4.$form->{year}";
473         last SWITCH;
474       };
475       $form->{duetyp} eq "5" && do {
476         $form->{fromdate} = "1.5.$form->{year}";
477         $form->{todate}   = "31.5.$form->{year}";
478         last SWITCH;
479       };
480       $form->{duetyp} eq "6" && do {
481         $form->{fromdate} = "1.6.$form->{year}";
482         $form->{todate}   = "30.6.$form->{year}";
483         last SWITCH;
484       };
485       $form->{duetyp} eq "7" && do {
486         $form->{fromdate} = "1.7.$form->{year}";
487         $form->{todate}   = "31.7.$form->{year}";
488         last SWITCH;
489       };
490       $form->{duetyp} eq "8" && do {
491         $form->{fromdate} = "1.8.$form->{year}";
492         $form->{todate}   = "31.8.$form->{year}";
493         last SWITCH;
494       };
495       $form->{duetyp} eq "9" && do {
496         $form->{fromdate} = "1.9.$form->{year}";
497         $form->{todate}   = "30.9.$form->{year}";
498         last SWITCH;
499       };
500       $form->{duetyp} eq "10" && do {
501         $form->{fromdate} = "1.10.$form->{year}";
502         $form->{todate}   = "31.10.$form->{year}";
503         last SWITCH;
504       };
505       $form->{duetyp} eq "11" && do {
506         $form->{fromdate} = "1.11.$form->{year}";
507         $form->{todate}   = "30.11.$form->{year}";
508         last SWITCH;
509       };
510       $form->{duetyp} eq "12" && do {
511         $form->{fromdate} = "1.12.$form->{year}";
512         $form->{todate}   = "31.12.$form->{year}";
513         last SWITCH;
514       };
515     }
516   }
517
518   CA->all_transactions(\%myconfig, \%$form);
519
520   $form->{saldo_old} += $form->{beginning_balance};
521   $form->{saldo_new} += $form->{beginning_balance};
522   my $saldo_old = format_debit_credit($form->{saldo_old});
523   my $eb_string = format_debit_credit($form->{beginning_balance});
524   $form->{balance} = $form->{saldo_old};
525
526   my @options;
527   if ($form->{department}) {
528     my ($department) = split /--/, $form->{department};
529     push @options, $locale->text('Department') . " : $department";
530   }
531   if ($form->{projectnumber}) {
532     push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
533   }
534
535   my $period;
536   if ($form->{fromdate} || $form->{todate}) {
537     my ($fromdate, $todate);
538
539     if ($form->{fromdate}) {
540       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
541     }
542     if ($form->{todate}) {
543       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
544     }
545
546     $period = "$fromdate - $todate";
547
548   } else {
549     $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
550   }
551
552   push @options, $period;
553
554   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
555   push (@options, $form->{print_date});
556
557   $form->{company} = $locale->text('Company') . " " . $myconfig{company};
558   push (@options, $form->{company});
559
560   my @columns     = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
561   my %column_defs = (
562     'transdate'   => { 'text' => $locale->text('Date'), },
563     'reference'   => { 'text' => $locale->text('Reference'), },
564     'description' => { 'text' => $locale->text('Description'), },
565     'debit'       => { 'text' => $locale->text('Debit'), },
566     'credit'      => { 'text' => $locale->text('Credit'), },
567     'gegenkonto'     => { 'text' => $locale->text('Gegenkonto'), },
568     'ustkonto'     => { 'text' => $locale->text('USt-Konto'), },
569     'balance'          => { 'text' => $locale->text('Balance'), },
570     'ustrate'     => { 'text' => $locale->text('Satz %'), },
571  );
572
573   my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort);
574
575   my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
576
577   $form->{callback} = $link . '&sort=' . E($form->{sort});
578
579   my %column_alignment = map { $_ => 'right' } qw(debit credit);
580
581   my @custom_headers = ();
582  # Zeile 1:
583  push @custom_headers, [
584    { 'text' => 'Letzte Buchung', },
585    { 'text' => 'EB-Wert', },
586    { 'text' => 'Saldo alt', 'colspan' => 2, },
587    { 'text' => 'Jahresverkehrszahlen alt', 'colspan' => 2, },
588    { 'text' => '', 'colspan' => 2, },
589  ];
590  push @custom_headers, [
591    { 'text' => $form->{last_transaction}, },
592    { 'text' => $eb_string, },
593    { 'text' => $saldo_old, 'colspan' => 2, },
594    { 'text' => $form->format_amount(\%myconfig, abs($form->{old_balance_debit}), 2) . " S", },
595    { 'text' => $form->format_amount(\%myconfig, $form->{old_balance_credit}, 2) . " H", },
596    { 'text' => '', 'colspan' => 2, },
597  ];
598  # Zeile 2:
599  push @custom_headers, [
600    { 'text' => $locale->text('Date'), 'link' => $link . "&sort=transdate", },
601    { 'text' => $locale->text('Reference'), 'link' => $link . "&sort=reference",  },
602    { 'text' => $locale->text('Description'), 'link' => $link . "&sort=description",  },
603    { 'text' => $locale->text('Gegenkonto'), },
604    { 'text' => $locale->text('Debit'), },
605    { 'text' => $locale->text('Credit'), },
606    { 'text' => $locale->text('USt-Konto'), },
607    { 'text' => $locale->text('Satz %'), },
608    { 'text' => $locale->text('Balance'), },
609  ];
610
611
612
613
614
615   my $report = SL::ReportGenerator->new(\%myconfig, $form);
616   $report->set_custom_headers(@custom_headers);
617
618   $report->set_options('top_info_text'         => join("\n", @options),
619                        'output_format'         => 'HTML',
620                        'title'                 => $form->{title},
621                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
622                        'std_column_visibility' => 1,
623     );
624   $report->set_options_from_form();
625
626   $report->set_columns(%column_defs);
627   $report->set_column_order(@columns);
628
629   $report->set_export_options('list_transactions', @hidden_variables);
630
631   $report->set_sort_indicator($form->{sort}, 1);
632
633   $column_defs{balance}->{visible} = 1;
634
635   my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
636
637
638   my $idx       = 0;
639   my %totals    = ( 'debit' => 0, 'credit' => 0 );
640   my %subtotals = ( 'debit' => 0, 'credit' => 0 );
641   my ($previous_index, $row_set);
642
643   foreach my $ca (@{ $form->{CA} }) {
644
645     foreach (qw(debit credit)) {
646       $subtotals{$_} += $ca->{$_};
647       $totals{$_}    += $ca->{$_};
648       if ($_ =~ /debit.*/) {
649         $ml = -1;
650       } else {
651         $ml = 1;
652       }
653       $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
654       $ca->{$_}       = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
655     }
656
657     my $do_subtotal = 0;
658     if (($form->{subtotal})
659         && (($idx == scalar @{ $form->{CA} } - 1)
660             || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
661       $do_subtotal = 1;
662     }
663
664     my $row = { };
665
666     $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
667
668     if ($ca->{memo} ne "") {
669       $ca->{description} .= " \n " . $ca->{memo};
670     }
671
672
673
674     foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
675       if ($ca->{gegenkonto} eq "") {
676         $ca->{gegenkonto} = $gegenkonto->{accno};
677       } else {
678         $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
679       }
680     }
681
682     foreach (@columns) {
683       $row->{$_} = {
684         'data'  => $ca->{$_},
685         'align' => $column_alignment{$_},
686       };
687     }
688
689     $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
690
691     if ($ca->{index} ne $previous_index) {
692 #       $report->add_data($row_set) if ($row_set);
693
694 #       $row_set         = [ ];
695       $previous_index  = $ca->{index};
696
697       $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
698
699     } elsif ($ca->{index} eq $previous_index) {
700       map { $row->{$_}->{data} = '' } qw(reference description);
701       $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
702     }
703
704     my $row_set = [];
705
706     push @{ $row_set }, $row;
707
708     push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
709
710
711     $idx++;
712     $report->add_data($row_set);
713
714   }
715
716   $report->add_data($row_set) if ($row_set);
717
718   $report->add_separator();
719
720   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
721
722
723   $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
724
725   $report->add_data($row);
726
727
728   $report->add_separator();
729   $row = {
730      'transdate' => {
731        'data'    => "",
732        'class' => 'listtotal',
733      },
734      'reference' => {
735        'data'    => $locale->text('EB-Wert'),
736        'class' => 'listtotal',
737      },
738      'description'      => {
739        'data'    => $locale->text('Saldo neu'),
740        'colspan' => 2,
741        'class' => 'listtotal',
742      },
743      'debit'      => {
744        'data'    => $locale->text('Jahresverkehrszahlen neu'),
745        'colspan' => 2,
746        'align' => 'left',
747        'class' => 'listtotal',
748     },
749      'ustkonto'      => {
750        'data'    => '',
751        'colspan' => 2,
752        'align' => 'left',
753        'class' => 'listtotal',
754     },
755   };
756
757   $report->add_data($row);
758   my $saldo_new = format_debit_credit($form->{saldo_new});
759   $row = {
760      'transdate' => {
761        'data'    => "",
762        'class' => 'listtotal',
763      },
764      'reference' => {
765        'data'    => $eb_string,
766        'class' => 'listtotal',
767      },
768      'description'      => {
769        'data'    => $saldo_new,
770        'colspan' => 2,
771        'class' => 'listtotal',
772      },
773      'debit'      => {
774        'data'    => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
775        'class' => 'listtotal',
776      },
777       'credit'      => {
778        'data'    => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
779        'class' => 'listtotal',
780      },
781       'ustkonto'      => {
782        'data'    => "",
783        'colspan' => 2,
784        'class' => 'listtotal',
785      },
786   };
787
788   $report->add_data($row);
789
790   $report->generate_with_headers();
791
792   $main::lxdebug->leave_sub();
793 }
794
795 sub create_subtotal_row {
796   $main::lxdebug->enter_sub();
797
798   my $form     = $main::form;
799   my %myconfig = %main::myconfig;
800
801   my ($totals, $columns, $column_alignment, $class) = @_;
802
803   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
804
805   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
806
807   map { $totals->{$_} = 0 } qw(debit credit);
808
809   $main::lxdebug->leave_sub();
810
811   return $row;
812 }