Umstellung von eur zu 3 Variablen in defaults
[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 ( $::instance_conf->get_accounting_method eq 'cash' ) {
88     # $form->{method} can probably be made redundant now that we have get_accounting_method
89     $form->{method} = "cash";
90   }
91
92   CA->all_accounts(\%myconfig, \%$form);
93
94   my @columns     = qw(accno description debit credit);
95   my %column_defs = (
96     'accno'       => { 'text' => $locale->text('Account'), },
97     'description' => { 'text' => $locale->text('Description'), },
98     'debit'       => { 'text' => $locale->text('Debit'), },
99     'credit'      => { 'text' => $locale->text('Credit'), },
100   );
101
102   my $report = SL::ReportGenerator->new(\%myconfig, $form);
103
104   $report->set_options('output_format'         => 'HTML',
105                        'title'                 => $form->{title},
106                        'attachment_basename'   => $locale->text('chart_of_accounts') . strftime('_%Y%m%d', localtime time),
107                        'std_column_visibility' => 1,
108     );
109   $report->set_options_from_form();
110   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
111
112   $report->set_columns(%column_defs);
113   $report->set_column_order(@columns);
114
115   $report->set_export_options('chart_of_accounts');
116
117   $report->set_sort_indicator($form->{sort}, 1);
118
119   my %totals = ('debit' => 0, 'credit' => 0);
120
121   foreach my $ca (@{ $form->{CA} }) {
122     next unless defined $ca->{amount};
123     my $row = { };
124
125     foreach (qw(debit credit)) {
126       $totals{$_} += $ca->{$_} * 1;
127       $ca->{$_}    = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_});
128     }
129
130     map { $row->{$_} = { 'data' => $ca->{$_} } } @columns;
131
132     map { $row->{$_}->{align} = 'right'       } qw(debit credit);
133     map { $row->{$_}->{class} = 'listheading' } @columns if ($ca->{charttype} eq "H");
134
135     $row->{accno}->{link} = build_std_url('action=list', 'accno=' . E($ca->{accno}), 'description=' . E($ca->{description}));
136
137     $report->add_data($row);
138   }
139
140   my $row = { map { $_ => { 'class' => 'listtotal', 'align' => 'right' } } @columns };
141   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } qw(debit credit);
142
143   $report->add_separator();
144   $report->add_data($row);
145
146   $report->generate_with_headers();
147
148   $main::lxdebug->leave_sub();
149 }
150
151 sub list {
152   $main::lxdebug->enter_sub();
153
154   my $form     = $main::form;
155   my %myconfig = %main::myconfig;
156   my $locale   = $main::locale;
157
158   $main::auth->assert('report');
159
160   $form->{title} = $locale->text('List Transactions');
161   $form->{title} .= " - " . $locale->text('Account') . " $form->{accno}";
162   my $year = (localtime)[5] + 1900;
163
164   # get departments
165   $form->all_departments(\%myconfig);
166   if (@{ $form->{all_departments} || [] }) {
167     $form->{selectdepartment} = "<option>\n";
168
169     map {
170       $form->{selectdepartment} .=
171         "<option>$_->{description}--$_->{id}\n"
172     } (@{ $form->{all_departments} || [] });
173   }
174
175   my $department = qq|
176         <tr>
177           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
178           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
179         </tr>
180 | if $form->{selectdepartment};
181   my $accrual =  $::instance_conf->get_accounting_method eq 'cash' ? ""        : "checked";
182   my $cash    =  $::instance_conf->get_accounting_method eq 'cash' ? "checked" : "";
183
184   my $name_1    = "fromdate";
185   my $id_1      = "fromdate";
186   my $value_1   = "$form->{fromdate}";
187   my $trigger_1 = "trigger1";
188   my $name_2    = "todate";
189   my $id_2      = "todate";
190   my $value_2   = "";
191   my $trigger_2 = "trigger2";
192
193   my ($button1, $button1_2, $button2, $button2_2, $jsscript);
194
195   # with JavaScript Calendar
196   if ($form->{jsscript}) {
197     if ($name_1 eq "") {
198
199       $button1 = qq|
200          <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
201       $button1_2 = qq|
202         <input type=button name=$name_2 id="$trigger_2" value=|
203         . $locale->text('button') . qq|>|;
204
205       #write Trigger
206       $jsscript =
207         Form->write_trigger(\%myconfig, "1", "$name_2", "BR", "$trigger_2");
208     } else {
209       $button1 = qq|
210          <input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\" value="$value_1">|;
211       $button1_2 = qq|
212         <input type=button name=$name_1 id="$trigger_1" value=|
213         . $locale->text('button') . qq|>|;
214       $button2 = qq|
215          <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
216       $button2_2 = qq|
217          <input type=button name=$name_2 id="$trigger_2" value=|
218         . $locale->text('button') . qq|>
219        |;
220
221       #write Trigger
222       $jsscript =
223         Form->write_trigger(\%myconfig, "2", "$name_1", "BR", "$trigger_1",
224                             "$name_2", "BL", "$trigger_2");
225     }
226   } else {
227
228     # without JavaScript Calendar
229     if ($name_1 eq "") {
230       $button1 =
231         qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
232     } else {
233       $button1 =
234         qq|<input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value="$value_1" onBlur=\"check_right_date_format(this)\">|;
235       $button2 =
236         qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
237     }
238   }
239   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
240   $form->header;
241   my $onload = qq|focus()|;
242   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
243   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
244
245
246   $form->header;
247
248   $form->{description} =~ s/\"/&quot;/g;
249
250   my $eur =  $::instance_conf->get_accounting_method eq 'cash' ? 1 : 0;
251
252   print qq|
253 <body onLoad="$onload">
254
255 <form method=post action=$form->{script}>
256
257 <input type=hidden name=accno value=$form->{accno}>
258 <input type=hidden name=description value="$form->{description}">
259 <input type=hidden name=sort value=transdate>
260 <input type=hidden name=eur value=$eur>
261 <input type=hidden name=accounttype value=$form->{accounttype}>
262
263 <table border=0 width=100%>
264   <tr>
265     <th class=listtop>$form->{title}</th>
266   </tr>
267
268 </table>
269 <table>
270         <tr>
271           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
272       . $locale->text('Customized Report') . qq|</th>
273         </tr>
274         <tr>
275           <th colspan=1>| . $locale->text('Year') . qq|</th>
276           <td><input name=year size=11 title="|
277       . $locale->text('YYYY') . qq|" value="$year"></td>
278         </tr>
279 |;
280
281     print qq|
282         <tr>
283                 <td align=right>
284 <b> | . $locale->text('Yearly') . qq|</b> </td>
285                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
286                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
287         </tr>
288         <tr>
289                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"></td>
290                 <td><input name=duetyp class=radio type=radio value="A">&nbsp;1. | . $locale->text('Quarter') . qq|</td>
291                 <td><input name=duetyp class=radio type=radio value="1" "checked">&nbsp;| . $locale->text('January') . qq|</td>
292                 <td><input name=duetyp class=radio type=radio value="5" >&nbsp;| . $locale->text('May') . qq|</td>
293                 <td><input name=duetyp class=radio type=radio value="9" >&nbsp;| . $locale->text('September') . qq|</td>
294
295         </tr>
296         <tr>
297                 <td align= right>&nbsp;</td>
298                 <td><input name=duetyp class=radio type=radio value="B">&nbsp;2. | . $locale->text('Quarter') . qq|</td>
299                 <td><input name=duetyp class=radio type=radio value="2" >&nbsp;| . $locale->text('February') . qq|</td>
300                 <td><input name=duetyp class=radio type=radio value="6" >&nbsp;| . $locale->text('June') . qq|</td>
301                 <td><input name=duetyp class=radio type=radio value="10" >&nbsp;| . $locale->text('October') . qq|</td>
302         </tr>
303         <tr>
304                 <td> &nbsp;</td>
305                 <td><input name=duetyp class=radio type=radio value="C">&nbsp;3. | . $locale->text('Quarter') . qq|</td>
306                 <td><input name=duetyp class=radio type=radio value="3" >&nbsp;| . $locale->text('March') . qq|</td>
307                 <td><input name=duetyp class=radio type=radio value="7" >&nbsp;| . $locale->text('July') . qq|</td>
308                 <td><input name=duetyp class=radio type=radio value="11" >&nbsp;| . $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">&nbsp;4. | . $locale->text('Quarter') . qq|&nbsp;</td>
314                 <td><input name=duetyp class=radio type=radio value="4" >&nbsp;| . $locale->text('April') . qq|</td>
315                 <td><input name=duetyp class=radio type=radio value="8" >&nbsp;| . $locale->text('August') . qq|</td>
316                 <td><input name=duetyp class=radio type=radio value="12" >&nbsp;| . $locale->text('December') . qq|</td>
317
318         </tr>
319         <tr>
320                    <td colspan=5><hr size=3 noshade></td>
321         </tr>
322         <tr>
323           <th align=left><input name=reporttype class=radio type=radio value="free"> | . $locale->text('Free report period') . qq|</th>
324           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
325               $button1
326               $button1_2&nbsp;
327               | . $locale->text('Bis') . qq|&nbsp;
328               $button2
329               $button2_2
330           </td>
331         </tr>
332         <tr>
333                    <td colspan=5><hr size=3 noshade></td>
334         </tr>
335         <tr>
336           <th align=leftt>| . $locale->text('Method') . qq|</th>
337           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
338           &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
339         </tr>
340         <tr>
341          <th align=right colspan=4>| . $locale->text('Decimalplaces') . qq|</th>
342              <td><input name=decimalplaces size=3 value="2"></td>
343          </tr>
344          <tr>
345             <td><input name="subtotal" class=checkbox type=checkbox value=1> | . $locale->text('Subtotal') . qq|</td>
346          </tr>
347
348 $jsscript
349   <tr><td colspan=5 ><hr size=3 noshade></td></tr>
350 </table>
351
352 <br><input class=submit type=submit name=action value="|
353     . $locale->text('List Transactions') . qq|">
354 </form>
355
356 </body>
357 </html>
358 |;
359
360   $main::lxdebug->leave_sub();
361 }
362
363 sub format_debit_credit {
364   $main::lxdebug->enter_sub();
365
366   my $dc = shift;
367
368   my $form     = $main::form;
369   my %myconfig = %main::myconfig;
370   my $locale   = $main::locale;
371
372   my $formatted_dc  = $form->format_amount(\%myconfig, abs($dc), 2) . ' ';
373   $formatted_dc    .= ($dc > 0) ? $locale->text('Credit (one letter abbreviation)') : $locale->text('Debit (one letter abbreviation)');
374
375   $main::lxdebug->leave_sub();
376
377   return $formatted_dc;
378 }
379
380
381 sub list_transactions {
382   $main::lxdebug->enter_sub();
383
384   my $form     = $main::form;
385   my %myconfig = %main::myconfig;
386   my $locale   = $main::locale;
387
388   $main::auth->assert('report');
389
390   $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
391
392   if ($form->{reporttype} eq "custom") {
393
394     #forgotten the year --> thisyear
395     if ($form->{year} !~ m/^\d\d\d\d$/) {
396       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
397         /(\d\d\d\d)/;
398       $form->{year} = $1;
399     }
400
401     #yearly report
402     if ($form->{duetyp} eq "13") {
403       $form->{fromdate} = "1.1.$form->{year}";
404       $form->{todate}   = "31.12.$form->{year}";
405     }
406
407     #Quater reports
408     if ($form->{duetyp} eq "A") {
409       $form->{fromdate} = "1.1.$form->{year}";
410       $form->{todate}   = "31.3.$form->{year}";
411     }
412     if ($form->{duetyp} eq "B") {
413       $form->{fromdate} = "1.4.$form->{year}";
414       $form->{todate}   = "30.6.$form->{year}";
415     }
416     if ($form->{duetyp} eq "C") {
417       $form->{fromdate} = "1.7.$form->{year}";
418       $form->{todate}   = "30.9.$form->{year}";
419     }
420     if ($form->{duetyp} eq "D") {
421       $form->{fromdate} = "1.10.$form->{year}";
422       $form->{todate}   = "31.12.$form->{year}";
423     }
424
425     #Monthly reports
426   SWITCH: {
427       $form->{duetyp} eq "1" && do {
428         $form->{fromdate} = "1.1.$form->{year}";
429         $form->{todate}   = "31.1.$form->{year}";
430         last SWITCH;
431       };
432       $form->{duetyp} eq "2" && do {
433         $form->{fromdate} = "1.2.$form->{year}";
434
435         #this works from 1901 to 2099, 1900 and 2100 fail.
436         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
437         $form->{todate} = "$leap.2.$form->{year}";
438         last SWITCH;
439       };
440       $form->{duetyp} eq "3" && do {
441         $form->{fromdate} = "1.3.$form->{year}";
442         $form->{todate}   = "31.3.$form->{year}";
443         last SWITCH;
444       };
445       $form->{duetyp} eq "4" && do {
446         $form->{fromdate} = "1.4.$form->{year}";
447         $form->{todate}   = "30.4.$form->{year}";
448         last SWITCH;
449       };
450       $form->{duetyp} eq "5" && do {
451         $form->{fromdate} = "1.5.$form->{year}";
452         $form->{todate}   = "31.5.$form->{year}";
453         last SWITCH;
454       };
455       $form->{duetyp} eq "6" && do {
456         $form->{fromdate} = "1.6.$form->{year}";
457         $form->{todate}   = "30.6.$form->{year}";
458         last SWITCH;
459       };
460       $form->{duetyp} eq "7" && do {
461         $form->{fromdate} = "1.7.$form->{year}";
462         $form->{todate}   = "31.7.$form->{year}";
463         last SWITCH;
464       };
465       $form->{duetyp} eq "8" && do {
466         $form->{fromdate} = "1.8.$form->{year}";
467         $form->{todate}   = "31.8.$form->{year}";
468         last SWITCH;
469       };
470       $form->{duetyp} eq "9" && do {
471         $form->{fromdate} = "1.9.$form->{year}";
472         $form->{todate}   = "30.9.$form->{year}";
473         last SWITCH;
474       };
475       $form->{duetyp} eq "10" && do {
476         $form->{fromdate} = "1.10.$form->{year}";
477         $form->{todate}   = "31.10.$form->{year}";
478         last SWITCH;
479       };
480       $form->{duetyp} eq "11" && do {
481         $form->{fromdate} = "1.11.$form->{year}";
482         $form->{todate}   = "30.11.$form->{year}";
483         last SWITCH;
484       };
485       $form->{duetyp} eq "12" && do {
486         $form->{fromdate} = "1.12.$form->{year}";
487         $form->{todate}   = "31.12.$form->{year}";
488         last SWITCH;
489       };
490     }
491   }
492
493   CA->all_transactions(\%myconfig, \%$form);
494
495   $form->{saldo_old} += $form->{beginning_balance};
496   $form->{saldo_new} += $form->{beginning_balance};
497   my $saldo_old = format_debit_credit($form->{saldo_old});
498   my $eb_string = format_debit_credit($form->{beginning_balance});
499   $form->{balance} = $form->{saldo_old};
500
501   my @options;
502   if ($form->{department}) {
503     my ($department) = split /--/, $form->{department};
504     push @options, $locale->text('Department') . " : $department";
505   }
506   if ($form->{projectnumber}) {
507     push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
508   }
509
510   my $period;
511   if ($form->{fromdate} || $form->{todate}) {
512     my ($fromdate, $todate);
513
514     if ($form->{fromdate}) {
515       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
516     }
517     if ($form->{todate}) {
518       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
519     }
520
521     $period = "$fromdate - $todate";
522
523   } else {
524     $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
525   }
526
527   push @options, $period;
528
529   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
530   push (@options, $form->{print_date});
531
532   $form->{company} = $locale->text('Company') . " " . $myconfig{company};
533   push (@options, $form->{company});
534
535   my @columns     = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
536   my %column_defs = (
537     'transdate'   => { 'text' => $locale->text('Date'), },
538     'reference'   => { 'text' => $locale->text('Reference'), },
539     'description' => { 'text' => $locale->text('Description'), },
540     'debit'       => { 'text' => $locale->text('Debit'), },
541     'credit'      => { 'text' => $locale->text('Credit'), },
542     'gegenkonto'  => { 'text' => $locale->text('Gegenkonto'), },
543     'ustkonto'    => { 'text' => $locale->text('USt-Konto'), },
544     'balance'     => { 'text' => $locale->text('Balance'), },
545     'ustrate'     => { 'text' => $locale->text('Satz %'), },
546  );
547
548   my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort method);
549
550   my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
551
552   $form->{callback} = $link . '&sort=' . E($form->{sort});
553
554   my %column_alignment = map { $_ => 'right' } qw(debit credit);
555
556   my @custom_headers = ();
557  # Zeile 1:
558  push @custom_headers, [
559    { 'text' => 'Letzte Buchung', },
560    { 'text' => 'EB-Wert', },
561    { 'text' => 'Saldo alt', 'colspan' => 2, },
562    { 'text' => 'Jahresverkehrszahlen alt', 'colspan' => 2, },
563    { 'text' => '', 'colspan' => 2, },
564  ];
565  push @custom_headers, [
566    { 'text' => $form->{last_transaction}, },
567    { 'text' => $eb_string, },
568    { 'text' => $saldo_old, 'colspan' => 2, },
569    { 'text' => $form->format_amount(\%myconfig, abs($form->{old_balance_debit}), 2) . " S", },
570    { 'text' => $form->format_amount(\%myconfig, $form->{old_balance_credit}, 2) . " H", },
571    { 'text' => '', 'colspan' => 2, },
572  ];
573  # Zeile 2:
574  push @custom_headers, [
575    { 'text' => $locale->text('Date'), 'link' => $link . "&sort=transdate", },
576    { 'text' => $locale->text('Reference'), 'link' => $link . "&sort=reference",  },
577    { 'text' => $locale->text('Description'), 'link' => $link . "&sort=description",  },
578    { 'text' => $locale->text('Gegenkonto'), },
579    { 'text' => $locale->text('Debit'), },
580    { 'text' => $locale->text('Credit'), },
581    { 'text' => $locale->text('USt-Konto'), },
582    { 'text' => $locale->text('Satz %'), },
583    { 'text' => $locale->text('Balance'), },
584  ];
585
586
587
588
589
590   my $report = SL::ReportGenerator->new(\%myconfig, $form);
591   $report->set_custom_headers(@custom_headers);
592
593   $report->set_options('top_info_text'         => join("\n", @options),
594                        'output_format'         => 'HTML',
595                        'title'                 => $form->{title},
596                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
597                        'std_column_visibility' => 1,
598     );
599   $report->set_options_from_form();
600   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
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   $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   $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   $main::lxdebug->leave_sub();
769 }
770
771 sub create_subtotal_row {
772   $main::lxdebug->enter_sub();
773
774   my $form     = $main::form;
775   my %myconfig = %main::myconfig;
776
777   my ($totals, $columns, $column_alignment, $class) = @_;
778
779   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
780
781   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
782
783   map { $totals->{$_} = 0 } qw(debit credit);
784
785   $main::lxdebug->leave_sub();
786
787   return $row;
788 }