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