Das Buchungsjournal auf die Verwendung der ReportGenerator-Klasse umgestellt.
[kivitendo-erp.git] / bin / mozilla / gl.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) 1998-2002
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 # Genereal Ledger
31 #
32 #======================================================================
33
34 use POSIX qw(strftime);
35
36 use SL::GL;
37 use SL::IS;
38 use SL::PE;
39 use SL::ReportGenerator;
40
41 require "bin/mozilla/arap.pl";
42 require "bin/mozilla/common.pl";
43 require "bin/mozilla/report_generator.pl";
44
45 1;
46
47 # end of main
48
49 # this is for our long dates
50 # $locale->text('January')
51 # $locale->text('February')
52 # $locale->text('March')
53 # $locale->text('April')
54 # $locale->text('May ')
55 # $locale->text('June')
56 # $locale->text('July')
57 # $locale->text('August')
58 # $locale->text('September')
59 # $locale->text('October')
60 # $locale->text('November')
61 # $locale->text('December')
62
63 # this is for our short month
64 # $locale->text('Jan')
65 # $locale->text('Feb')
66 # $locale->text('Mar')
67 # $locale->text('Apr')
68 # $locale->text('May')
69 # $locale->text('Jun')
70 # $locale->text('Jul')
71 # $locale->text('Aug')
72 # $locale->text('Sep')
73 # $locale->text('Oct')
74 # $locale->text('Nov')
75 # $locale->text('Dec')
76
77 sub add {
78   $lxdebug->enter_sub();
79
80   $form->{title} = "Add";
81
82   $form->{callback} =
83     "$form->{script}?action=add&login=$form->{login}&password=$form->{password}"
84     unless $form->{callback};
85
86   # we use this only to set a default date
87   GL->transaction(\%myconfig, \%$form);
88
89   map {
90     $tax .=
91       qq|<option value="$_->{id}--$_->{rate}">$_->{taxdescription}  |
92       . ($_->{rate} * 100) . qq| %|
93   } @{ $form->{TAX} };
94
95   $form->{rowcount}  = 2;
96
97   $form->{debit}  = 0;
98   $form->{credit} = 0;
99   $form->{tax}    = 0;
100
101   # departments
102   $form->all_departments(\%myconfig);
103   if (@{ $form->{all_departments} }) {
104     $form->{selectdepartment} = "<option>\n";
105
106     map {
107       $form->{selectdepartment} .=
108         "<option>$_->{description}--$_->{id}\n"
109     } (@{ $form->{all_departments} });
110   }
111
112   $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
113
114   &display_form(1);
115   $lxdebug->leave_sub();
116
117 }
118
119 sub prepare_transaction {
120   $lxdebug->enter_sub();
121
122   GL->transaction(\%myconfig, \%$form);
123
124   map {
125     $tax .=
126       qq|<option value="$_->{id}--$_->{rate}">$_->{taxdescription}  |
127       . ($_->{rate} * 100) . qq| %|
128   } @{ $form->{TAX} };
129
130   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
131
132   # departments
133   $form->all_departments(\%myconfig);
134   if (@{ $form->{all_departments} }) {
135     $form->{selectdepartment} = "<option>\n";
136
137     map {
138       $form->{selectdepartment} .=
139         "<option>$_->{description}--$_->{id}\n"
140     } (@{ $form->{all_departments} });
141   }
142
143   my $i        = 1;
144   my $tax      = 0;
145   my $taxaccno = "";
146   foreach $ref (@{ $form->{GL} }) {
147     $j = $i - 1;
148     if ($tax && ($ref->{accno} eq $taxaccno)) {
149       $form->{"tax_$j"}      = abs($ref->{amount});
150       $form->{"taxchart_$j"} = $ref->{id} . "--" . $ref->{taxrate};
151       if ($form->{taxincluded}) {
152         if ($ref->{amount} < 0) {
153           $form->{"debit_$j"} += $form->{"tax_$j"};
154         } else {
155           $form->{"credit_$j"} += $form->{"tax_$j"};
156         }
157       }
158       $form->{"project_id_$j"} = $ref->{project_id};
159
160     } else {
161       $form->{"accno_$i"} = "$ref->{accno}--$ref->{tax_id}";
162       for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
163       if ($ref->{amount} < 0) {
164         $form->{totaldebit} -= $ref->{amount};
165         $form->{"debit_$i"} = $ref->{amount} * -1;
166       } else {
167         $form->{totalcredit} += $ref->{amount};
168         $form->{"credit_$i"} = $ref->{amount};
169       }
170       $form->{"taxchart_$i"} = "0--0.00";
171       $form->{"project_id_$i"} = $ref->{project_id};
172       $i++;
173     }
174     if ($ref->{taxaccno} && !$tax) {
175       $taxaccno = $ref->{taxaccno};
176       $tax      = 1;
177     } else {
178       $taxaccno = "";
179       $tax      = 0;
180     }
181   }
182
183   $form->{rowcount} = $i;
184   $form->{locked}   =
185     ($form->datetonum($form->{transdate}, \%myconfig) <=
186      $form->datetonum($form->{closedto}, \%myconfig));
187
188   $lxdebug->leave_sub();
189 }
190
191 sub edit {
192   $lxdebug->enter_sub();
193
194   prepare_transaction();
195
196   $form->{title} = "Edit";
197
198   $form->{show_details} = $myconfig{show_form_details} unless defined $form->{show_details};
199
200   form_header();
201   display_rows();
202   form_footer();
203
204   $lxdebug->leave_sub();
205 }
206
207
208 sub search {
209   $lxdebug->enter_sub();
210
211   $form->{title} = $locale->text('Journal');
212
213   $form->all_departments(\%myconfig);
214
215   # departments
216   if (@{ $form->{all_departments} }) {
217     $form->{selectdepartment} = "<option>\n";
218
219     map {
220       $form->{selectdepartment} .=
221         "<option>$_->{description}--$_->{id}\n"
222     } (@{ $form->{all_departments} });
223   }
224
225   $department = qq|
226         <tr>
227           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
228           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
229         </tr>
230 | if $form->{selectdepartment};
231
232   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
233                                    "all" => 1 });
234
235   my %project_labels = ();
236   my @project_values = ("");
237   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
238     push(@project_values, $item->{"id"});
239     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
240   }
241
242   my $projectnumber =
243     NTI($cgi->popup_menu('-name' => "project_id",
244                          '-values' => \@project_values,
245                          '-labels' => \%project_labels));
246
247   # use JavaScript Calendar or not
248   $form->{jsscript} = 1;
249   $jsscript = "";
250   if ($form->{jsscript}) {
251
252     # with JavaScript Calendar
253     $button1 = qq|
254        <td><input name=datefrom id=datefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
255        <input type=button name=datefrom id="trigger1" value=|
256       . $locale->text('button') . qq|></td>  
257        |;
258     $button2 = qq|
259        <td><input name=dateto id=dateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
260        <input type=button name=dateto id="trigger2" value=|
261       . $locale->text('button') . qq|></td>
262      |;
263
264     #write Trigger
265     $jsscript =
266       Form->write_trigger(\%myconfig, "2", "datefrom", "BR", "trigger1",
267                           "dateto", "BL", "trigger2");
268   } else {
269
270     # without JavaScript Calendar
271     $button1 =
272       qq|<td><input name=datefrom id=datefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
273     $button2 =
274       qq|<td><input name=dateto id=dateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
275   }
276   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
277   $form->header;
278   $onload = qq|focus()|;
279   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
280   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
281   print qq|
282 <body onLoad="$onload">
283
284 <form method=post action=$form->{script}>
285
286 <input type=hidden name=sort value=transdate>
287
288 <table width=100%>
289   <tr>
290     <th class=listtop>$form->{title}</th>
291   </tr>
292   <tr height="5"></tr>
293   <tr>
294     <td>
295       <table>
296         <tr>
297           <th align=right>| . $locale->text('Reference') . qq|</th>
298           <td><input name=reference size=20></td>
299           <th align=right>| . $locale->text('Source') . qq|</th>
300           <td><input name=source size=20></td>
301         </tr>
302         $department
303         <tr>
304           <th align=right>| . $locale->text('Description') . qq|</th>
305           <td colspan=3><input name=description size=40></td>
306         </tr>
307         <tr>
308           <th align=right>| . $locale->text('Notes') . qq|</th>
309           <td colspan=3><input name=notes size=40></td>
310         </tr>
311         <tr>
312           <th align=right>| . $locale->text('Project Number') . qq|</th>
313           <td colspan=3>$projectnumber</td>
314         </tr>
315         <tr>
316           <th align=right>| . $locale->text('From') . qq|</th>
317           $button1
318           <th align=right>| . $locale->text('To (time)') . qq|</th>
319           $button2
320         </tr>
321         <tr>
322           <th align=right>| . $locale->text('Include in Report') . qq|</th>
323           <td colspan=3>
324             <table>
325               <tr>
326                 <td>
327                   <input name="category" class=radio type=radio value=X checked>&nbsp;|
328     . $locale->text('All') . qq|
329                   <input name="category" class=radio type=radio value=A>&nbsp;|
330     . $locale->text('Asset') . qq|
331                   <input name="category" class=radio type=radio value=L>&nbsp;|
332     . $locale->text('Liability') . qq|
333                   <input name="category" class=radio type=radio value=I>&nbsp;|
334     . $locale->text('Revenue') . qq|
335                   <input name="category" class=radio type=radio value=E>&nbsp;|
336     . $locale->text('Expense') . qq|
337                 </td>
338               </tr>
339               <tr>
340                 <table>
341                   <tr>
342                     <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
343                     <td>| . $locale->text('ID') . qq|</td>
344                     <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
345                     <td>| . $locale->text('Date') . qq|</td>
346                     <td align=right><input name="l_reference" class=checkbox type=checkbox value=Y checked></td>
347                     <td>| . $locale->text('Reference') . qq|</td>
348                     <td align=right><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
349                     <td>| . $locale->text('Description') . qq|</td>
350                     <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
351                     <td>| . $locale->text('Notes') . qq|</td>
352                   </tr>
353                   <tr>
354                     <td align=right><input name="l_debit" class=checkbox type=checkbox value=Y checked></td>
355                     <td>| . $locale->text('Debit') . qq|</td>
356                     <td align=right><input name="l_credit" class=checkbox type=checkbox value=Y checked></td>
357                     <td>| . $locale->text('Credit') . qq|</td>
358                     <td align=right><input name="l_source" class=checkbox type=checkbox value=Y checked></td>
359                     <td>| . $locale->text('Source') . qq|</td>
360                     <td align=right><input name="l_accno" class=checkbox type=checkbox value=Y checked></td>
361                     <td>| . $locale->text('Account') . qq|</td>
362                   </tr>
363                   <tr>
364                     <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
365                     <td>| . $locale->text('Subtotal') . qq|</td>
366                     <td align=right><input name="l_projectnumbers" class=checkbox type=checkbox value=Y></td>
367                     <td>| . $locale->text('Project Number') . qq|</td>
368                   </tr>
369                 </table>
370               </tr>
371             </table>
372         </tr>
373       </table>
374     </td>
375   </tr>
376   <tr>
377     <td><hr size=3 noshade></td>
378   </tr>
379 </table>
380
381 $jsscript
382
383 <input type=hidden name=nextsub value=generate_report>
384
385 <input type=hidden name=login value=$form->{login}>
386 <input type=hidden name=password value=$form->{password}>
387
388 <br>
389 <input class=submit type=submit name=action value="|
390     . $locale->text('Continue') . qq|">
391 </form>
392
393 </body>
394 </html>
395 |;
396   $lxdebug->leave_sub();
397 }
398
399 sub create_subtotal_row {
400   $lxdebug->enter_sub();
401
402   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
403
404   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
405
406   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
407
408   map { $totals->{$_} = 0 } @{ $subtotal_columns };
409
410   $lxdebug->leave_sub();
411
412   return $row;
413 }
414
415 sub generate_report {
416   $lxdebug->enter_sub();
417
418   $form->{sort} ||= "transdate";
419
420   GL->all_transactions(\%myconfig, \%$form);
421
422   my %acctype = ('A' => $locale->text('Asset'),
423                  'C' => $locale->text('Contra'),
424                  'L' => $locale->text('Liability'),
425                  'Q' => $locale->text('Equity'),
426                  'I' => $locale->text('Revenue'),
427                  'E' => $locale->text('Expense'),);
428
429   $form->{title} = $locale->text('Journal');
430   if ($form->{category} ne 'X') {
431     $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
432   }
433
434   $form->{landscape} = 1;
435
436   my $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
437
438   my @columns = qw(
439     transdate      id               reference      description
440     notes          source           debit          debit_accno
441     credit         credit_accno     debit_tax      debit_tax_accno
442     credit_tax     credit_tax_accno projectnumbers balance
443   );
444
445   my @hidden_variables = qw(accno source reference department description notes project_id datefrom dateto category l_subtotal);
446   push @hidden_variables, map { "l_${_}" } @columns;
447
448   my (@options, $date_option);
449   if ($form->{accno}) {
450     push @options, $locale->text('Account') . " : $form->{accno} $form->{account_description}";
451   }
452   if ($form->{source}) {
453     push @options, $locale->text('Source') . " : $form->{source}";
454   }
455   if ($form->{reference}) {
456     push @options, $locale->text('Reference') . " : $form->{reference}";
457   }
458   if ($form->{department}) {
459     my ($department) = split /--/, $form->{department};
460     push @options, $locale->text('Department') . " : $department";
461   }
462   if ($form->{description}) {
463     push @options, $locale->text('Description') . " : $form->{description}";
464   }
465   if ($form->{notes}) {
466     push @options, $locale->text('Notes') . " : $form->{notes}";
467   }
468   if ($form->{datefrom}) {
469     $date_option = $locale->text('From') . " " . $locale->date(\%myconfig, $form->{datefrom}, 1);
470   }
471   if ($form->{dateto}) {
472     if ($form->{datefrom}) {
473       $date_option .= " ";
474     }
475     $date_option .= $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{dateto}, 1);
476   }
477   push @options, $date_option if $date_option;
478
479   my $callback = build_std_url('action=generate_report', @hidden_variables);
480
481   $form->{l_credit_accno}     = 'Y';
482   $form->{l_debit_accno}      = 'Y';
483   $form->{l_credit_tax}       = 'Y';
484   $form->{l_debit_tax}        = 'Y';
485   $form->{l_credit_tax_accno} = 'Y';
486   $form->{l_debit_tax_accno}  = 'Y';
487   $form->{l_balance}          = $form->{accno} ? 'Y' : '';
488
489   my %column_defs = (
490     'id'               => { 'text' => $locale->text('ID'), },
491     'transdate'        => { 'text' => $locale->text('Date'), },
492     'reference'        => { 'text' => $locale->text('Reference'), },
493     'source'           => { 'text' => $locale->text('Source'), },
494     'description'      => { 'text' => $locale->text('Description'), },
495     'notes'            => { 'text' => $locale->text('Notes'), },
496     'debit'            => { 'text' => $locale->text('Debit'), },
497     'debit_accno'      => { 'text' => $locale->text('Debit Account'), },
498     'credit'           => { 'text' => $locale->text('Credit'), },
499     'credit_accno'     => { 'text' => $locale->text('Credit Account'), },
500     'debit_tax'        => { 'text' => $locale->text('Debit Tax'), },
501     'debit_tax_accno'  => { 'text' => $locale->text('Debit Tax Account'), },
502     'credit_tax'       => { 'text' => $locale->text('Credit Tax'), },
503     'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
504     'balance'          => { 'text' => $locale->text('Balance'), },
505     'projectnumbers'   => { 'text' => $locale->text('Project Numbers'), },
506   );
507
508   map { $column_defs{$_}->{link}    = $callback . "&sort=${_}" }  qw(id transdate reference source description);
509   map { $column_defs{$_}->{link}    = $callback . "&sort=accno" } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno debit_tax credit_tax);
510   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
511   map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
512
513   my %column_alignment;
514   map { $column_alignment{$_} = 'right' }  qw(balance id debit credit debit_tax credit_tax);
515   map { $column_alignment{$_} = 'center' } qw(transdate reference description source notes debit_accno credit_accno debit_tax_accno credit_tax_accno);
516
517   my $report = SL::ReportGenerator->new(\%myconfig, $form);
518
519   $report->set_columns(%column_defs);
520   $report->set_column_order(@columns);
521
522   $report->set_export_options('generate_report', @hidden_variables);
523
524   $report->set_sort_indicator($form->{sort}, 1);
525
526   $report->set_options('top_info_text'        => join("\n", @options),
527                        'output_format'        => 'HTML',
528                        'title'                => $form->{title},
529                        'attachment_basename'  => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
530     );
531   $report->set_options_from_form();
532
533   # add sort to callback
534   $form->{callback} = "$callback&sort=" . E($form->{sort});
535
536   $form->{balance} *= $ml;
537
538   if ($form->{accno} && $form->{balance}) {
539     my $row = {
540       'balance' => {
541         'data'  => $form->format_amount(\%myconfig, $form->{balance}, 2),
542         'align' => 'right',
543       },
544     };
545
546     $report->add_data($row);
547   }
548
549   my @totals_columns = qw(debit credit debit_tax credit_tax);
550   my %subtotals      = map { $_ => 0 } @totals_columns;
551   my %totals         = map { $_ => 0 } @totals_columns;
552   my $idx            = 0;
553
554   foreach $ref (@{ $form->{GL} }) {
555     $form->{balance} *= $ml;
556
557     my %rows;
558
559     foreach my $key (qw(debit credit debit_tax credit_tax)) {
560       $rows{$key} = [];
561       foreach my $idx (sort keys(%{ $ref->{$key} })) {
562         my $value         = $ref->{$key}->{$idx};
563         $subtotals{$key} += $value;
564         $totals{$key}    += $value;
565         $form->{balance}  = abs($form->{balance}) - abs($value);
566         push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
567       }
568     }
569
570     foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate)) {
571       my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
572       $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
573     }
574
575     my $row = { };
576     map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
577
578     $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2);
579     $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
580
581     map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description source notes);
582
583     map { $row->{$_}->{data} = join "\n", @{ $rows{$_} }; } qw(transdate debit credit);
584
585     map { $row->{$_}->{data} = join "\n", @{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
586
587     foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
588       if (lc $report->{options}->{output_format} eq 'html') {
589         $row->{$col}->{raw_data} = join "<br>", map { "<a href=\"${callback}&accno=" . E($_) . "\">$_</a>" } @{ $rows{$col} };
590       } else {
591         $row->{$col}->{data} = join "\n", @{ $rows{$col} };
592       }
593     }
594
595     $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
596
597     my $row_set = [ $row ];
598
599     if (($form->{l_subtotal} eq 'Y')
600         && (($idx == (scalar @{ $form->{GL} } - 1))
601             || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
602       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
603     }
604
605     $report->add_data($row_set);
606
607     $idx++;
608   }
609
610   $report->add_separator();
611
612   # = 0 for balanced ledger
613   my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
614
615   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
616   $row->{balance} = {
617     'data'  => $form->format_amount(\%myconfig, $form->{balance} * $ml, 2),
618     'align' => 'right',
619     'class' => 'listtotal',
620   };
621   $report->add_data($row);
622
623   my $raw_bottom_info_text;
624
625   if (!$form->{accno} && (abs($balanced_ledger) >  0.001)) {
626     $raw_bottom_info_text .=
627         '<p><span class="unbalanced_ledger">'
628       . $locale->text('Unbalanced Ledger')
629       . ': '
630       . $form->format_amount(\%myconfig, $balanced_ledger, 3)
631       . '</span></p> ';
632   }
633
634   $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
635
636   $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
637
638   $report->generate_with_headers();
639
640   $lxdebug->leave_sub();
641 }
642
643 sub update {
644   $lxdebug->enter_sub();
645
646   $form->{oldtransdate} = $form->{transdate};
647
648   my @a           = ();
649   my $count       = 0;
650   my $debittax    = 0;
651   my $credittax   = 0;
652   my $debitcount  = 0;
653   my $creditcount = 0;
654   $debitlock  = 0;
655   $creditlock = 0;
656
657   my @flds =
658     qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
659
660   for my $i (1 .. $form->{rowcount}) {
661
662     unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
663       for (qw(debit credit tax)) {
664         $form->{"${_}_$i"} =
665           $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
666       }
667
668       push @a, {};
669       $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
670       if ($debitcredit) {
671         $debitcount++;
672       } else {
673         $creditcount++;
674       }
675
676       if (($debitcount >= 2) && ($creditcount == 2)) {
677         $form->{"credit_$i"} = 0;
678         $form->{"tax_$i"}    = 0;
679         $creditcount--;
680         $creditlock = 1;
681       }
682       if (($creditcount >= 2) && ($debitcount == 2)) {
683         $form->{"debit_$i"} = 0;
684         $form->{"tax_$i"}   = 0;
685         $debitcount--;
686         $debitlock = 1;
687       }
688       if (($creditcount == 1) && ($debitcount == 2)) {
689         $creditlock = 1;
690       }
691       if (($creditcount == 2) && ($debitcount == 1)) {
692         $debitlock = 1;
693       }
694       if ($debitcredit && $credittax) {
695         $form->{"taxchart_$i"} = "0--0.00";
696       }
697       if (!$debitcredit && $debittax) {
698         $form->{"taxchart_$i"} = "0--0.00";
699       }
700       $amount =
701         ($form->{"debit_$i"} == 0)
702         ? $form->{"credit_$i"}
703         : $form->{"debit_$i"};
704       $j = $#a;
705       if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
706         $form->{"taxchart_$i"} = "0--0.00";
707         $form->{"tax_$i"}      = 0;
708       }
709       if (!$form->{"korrektur_$i"}) {
710         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
711         if ($taxkey > 1) {
712           if ($debitcredit) {
713             $debittax = 1;
714           } else {
715             $credittax = 1;
716           }
717           if ($form->{taxincluded}) {
718             $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
719           } else {
720             $form->{"tax_$i"} = $amount * $rate;
721           }
722         } else {
723           $form->{"tax_$i"} = 0;
724         }
725       }
726
727       for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
728       $count++;
729     }
730   }
731
732   for $i (1 .. $count) {
733     $j = $i - 1;
734     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
735   }
736
737   for $i ($count + 1 .. $form->{rowcount}) {
738     for (@flds) { delete $form->{"${_}_$i"} }
739   }
740
741   $form->{rowcount} = $count + 1;
742
743   &display_form;
744   $lxdebug->leave_sub();
745
746 }
747
748 sub display_form {
749   my ($init) = @_;
750   $lxdebug->enter_sub();
751
752   &form_header($init);
753
754   #   for $i (1 .. $form->{rowcount}) {
755   #     $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
756   #     $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
757   #
758   #     &form_row($i);
759   #   }
760   &display_rows($init);
761   &form_footer;
762   $lxdebug->leave_sub();
763
764 }
765
766 sub display_rows {
767   my ($init) = @_;
768   $lxdebug->enter_sub();
769
770   $form->{debit_1}     = 0 if !$form->{"debit_1"};
771   $form->{totaldebit}  = 0;
772   $form->{totalcredit} = 0;
773
774   my @old_project_ids = ();
775   map({ push(@old_project_ids, $form->{"project_id_$_"})
776           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
777
778   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
779                                    "all" => 0,
780                                    "old_id" => \@old_project_ids },
781                    "charts" => { "key" => "ALL_CHARTS",
782                                  "transdate" => $form->{transdate} },
783                    "taxcharts" => "ALL_TAXCHARTS");
784
785   my %project_labels = ();
786   my @project_values = ("");
787   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
788     push(@project_values, $item->{"id"});
789     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
790   }
791
792   my %chart_labels = ();
793   my @chart_values = ();
794   my %charts = ();
795   my $taxchart_init;
796   foreach my $item (@{ $form->{ALL_CHARTS} }) {
797     my $key = Q($item->{accno}) . "--" . Q($item->{tax_id});
798     $taxchart_init = $item->{taxkey_id} unless (@chart_values);
799     push(@chart_values, $key);
800     $chart_labels{$key} = H($item->{accno}) . "--" . H($item->{description});
801     $charts{$item->{accno}} = $item;
802   }
803
804   my %taxchart_labels = ();
805   my @taxchart_values = ();
806   my %taxcharts = ();
807   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
808     my $key = Q($item->{id}) . "--" . Q($item->{rate});
809     $taxchart_init = $key if ($taxchart_init eq $item->{taxkey});
810     push(@taxchart_values, $key);
811     $taxchart_labels{$key} = H($item->{taxdescription}) . " " .
812       H($item->{rate} * 100) . ' %';
813     $taxcharts{$item->{id}} = $item;
814   }
815
816   for $i (1 .. $form->{rowcount}) {
817
818     $source = qq|
819     <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
820     $memo = qq|
821     <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
822
823     my $selected_accno_full;
824     my ($accno_row) = split(/--/, $form->{"accno_$i"});
825     my $item = $charts{$accno_row};
826     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
827
828     my $selected_taxchart = $form->{"taxchart_$i"};
829     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
830     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_accno_$i"});
831
832     if ($previous_accno &&
833         ($previous_accno eq $selected_accno) &&
834         ($previous_tax_id ne $selected_tax_id)) {
835       my $item = $taxcharts{$selected_tax_id};
836       $selected_taxchart = "$item->{id}--$item->{rate}";
837     }
838
839     $selected_accno = '' if ($init);
840     $selected_taxchart = $taxchart_init unless ($selected_taxchart ne "");
841
842     $accno = qq|<td>| .
843       NTI($cgi->popup_menu('-name' => "accno_$i",
844                            '-id' => "accno_$i",
845                            '-onChange' => "setTaxkey(this, $i)",
846                            '-style' => 'width:200px',
847                            '-values' => \@chart_values,
848                            '-labels' => \%chart_labels,
849                            '-default' => $selected_accno_full))
850       . $cgi->hidden('-name' => "previous_accno_$i",
851                      '-default' => $selected_accno_full)
852       . qq|</td>|;
853     $tax = qq|<td>| .
854       NTI($cgi->popup_menu('-name' => "taxchart_$i",
855                            '-id' => "taxchart_$i",
856                            '-style' => 'width:200px',
857                            '-values' => \@taxchart_values,
858                            '-labels' => \%taxchart_labels,
859                            '-default' => $selected_taxchart))
860       . qq|</td>|;
861
862     if ($init) {
863       $korrektur =
864         qq|<td><input type="checkbox" name="korrektur_$i" value="1"></td>|;
865       if ($form->{transfer}) {
866         $fx_transaction = qq|
867         <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
868     |;
869       }
870
871     } else {
872       if ($form->{"debit_$i"} != 0) {
873         $form->{totaldebit} += $form->{"debit_$i"};
874         if (!$form->{taxincluded}) {
875           $form->{totaldebit} += $form->{"tax_$i"};
876         }
877       } else {
878         $form->{totalcredit} += $form->{"credit_$i"};
879         if (!$form->{taxincluded}) {
880           $form->{totalcredit} += $form->{"tax_$i"};
881         }
882       }
883
884       for (qw(debit credit tax)) {
885         $form->{"${_}_$i"} =
886           ($form->{"${_}_$i"})
887           ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
888           : "";
889       }
890
891       if ($i < $form->{rowcount}) {
892         if ($form->{transfer}) {
893           $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
894           $x = ($checked) ? "x" : "";
895           $fx_transaction = qq|
896       <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
897     |;
898         }
899         $checked = ($form->{"korrektur_$i"}) ? "checked" : "";
900         $korrektur =
901           qq|<td><input type="checkbox" name="korrektur_$i" value="1" $checked></td>|;
902         $form->hide_form("accno_$i");
903
904       } else {
905         $korrektur =
906           qq|<td><input type="checkbox" name="korrektur_$i" value="1"></td>|;
907         if ($form->{transfer}) {
908           $fx_transaction = qq|
909       <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
910     |;
911         }
912       }
913     }
914     my $debitreadonly  = "";
915     my $creditreadonly = "";
916     if ($i == $form->{rowcount}) {
917       if ($debitlock) {
918         $debitreadonly = "readonly";
919       } elsif ($creditlock) {
920         $creditreadonly = "readonly";
921       }
922     }
923
924     my $projectnumber =
925       NTI($cgi->popup_menu('-name' => "project_id_$i",
926                            '-values' => \@project_values,
927                            '-labels' => \%project_labels,
928                            '-default' => $form->{"project_id_$i"} ));
929
930     my $copy2credit = 'onkeyup="copy_debit_to_credit()"' if $i == 1;
931
932     print qq|<tr valign=top>
933     $accno
934     $fx_transaction
935     <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
936     <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
937     <td><input name="tax_$i" size=6 value="$form->{"tax_$i"}"></td>
938     $korrektur
939     $tax|;
940
941     if ($form->{show_details}) {
942       print qq|
943     $source
944     $memo
945     <td>$projectnumber</td>
946 |;
947     }
948     print qq|
949   </tr>
950 |;
951   }
952
953   $form->hide_form(qw(rowcount selectaccno));
954
955   $lxdebug->leave_sub();
956
957 }
958
959 sub form_header {
960   my ($init) = @_;
961   $lxdebug->enter_sub();
962   $title         = $form->{title};
963   $form->{title} = $locale->text("$title General Ledger Transaction");
964   $readonly      = ($form->{id}) ? "readonly" : "";
965
966   $show_details_checked = "checked" if $form->{show_details};
967
968   # $locale->text('Add General Ledger Transaction')
969   # $locale->text('Edit General Ledger Transaction')
970
971   map { $form->{$_} =~ s/\"/&quot;/g }
972     qw(reference description chart taxchart);
973
974   $form->{javascript} = qq|<script type="text/javascript">
975   <!--
976   function setTaxkey(accno, row) {
977     var taxkey = accno.options[accno.selectedIndex].value;
978     var reg = /--([0-9]*)/;
979     var found = reg.exec(taxkey);
980     var index = found[1];
981     index = parseInt(index);
982     var tax = 'taxchart_' + row;
983     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
984       var reg2 = new RegExp("^"+ index, "");
985       if (reg2.exec(document.getElementById(tax).options[i].value)) {
986         document.getElementById(tax).options[i].selected = true;
987         break;
988       }
989     }
990   };
991
992   function copy_debit_to_credit() {
993     var txt = document.getElementsByName('debit_1')[0].value;
994     document.getElementsByName('credit_2')[0].value = txt;
995   };
996
997   //-->
998   </script>|;
999   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
1000
1001   $form->{selectdepartment} =~ s/ selected//;
1002   $form->{selectdepartment} =~
1003     s/option>\Q$form->{department}\E/option selected>$form->{department}/;
1004
1005   if (($rows = $form->numtextrows($form->{description}, 50)) > 1) {
1006     $description =
1007       qq|<textarea name=description rows=$rows cols=50 wrap=soft $readonly >$form->{description}</textarea>|;
1008   } else {
1009     $description =
1010       qq|<input name=description size=50 value="$form->{description}" $readonly>|;
1011   }
1012
1013   $taxincluded = ($form->{taxincluded}) ? "checked" : "";
1014
1015   if ($init) {
1016     $taxincluded = "checked";
1017   }
1018
1019   $department = qq|
1020         <tr>
1021           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1022           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1023           <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
1024         </tr>
1025 | if $form->{selectdepartment};
1026   if ($init) {
1027     $form->{fokus} = "gl.reference";
1028   } else {
1029     $form->{fokus} = qq|gl.accno_$form->{rowcount}|;
1030   }
1031
1032   # use JavaScript Calendar or not
1033   $form->{jsscript} = 1;
1034   $jsscript = "";
1035   if ($form->{jsscript}) {
1036
1037     # with JavaScript Calendar
1038     $button1 = qq|
1039        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" $readonly onBlur=\"check_right_date_format(this)\">
1040        <input type=button name=transdate id="trigger1" value=|
1041       . $locale->text('button') . qq|></td>  
1042        |;
1043
1044     #write Trigger
1045     $jsscript =
1046       Form->write_trigger(\%myconfig, "1", "transdate", "BL", "trigger1");
1047   } else {
1048
1049     # without JavaScript Calendar
1050     $button1 =
1051       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" $readonly onBlur=\"check_right_date_format(this)\"></td>|;
1052   }
1053
1054   $form->{previous_id}     ||= "--";
1055   $form->{previous_gldate} ||= "--";
1056
1057   $form->header;
1058
1059   print qq|
1060 <body onLoad="fokus()">
1061
1062 <form method=post name="gl" action=$form->{script}>
1063 |;
1064
1065   $form->hide_form(qw(id closedto locked storno storno_id previous_id previous_gldate));
1066
1067   print qq|
1068 <input type=hidden name=title value="$title">
1069
1070
1071 <table width=100%>
1072   <tr>
1073     <th class=listtop>$form->{title}</th>
1074   </tr>
1075   <tr height="5"></tr>
1076   <tr>
1077     <td>
1078       <table width=100%>
1079         <tr>
1080           <td colspan="6" align="left">|
1081     . $locale->text("Previous transnumber text")
1082     . " $form->{previous_id} "
1083     . $locale->text("Previous transdate text")
1084     . " $form->{previous_gldate}"
1085     . qq|</td>
1086         </tr>
1087         <tr>
1088           <th align=right>| . $locale->text('Reference') . qq|</th>
1089           <td><input name=reference size=20 value="$form->{reference}" $readonly></td>
1090           <td align=left>
1091             <table>
1092               <tr>
1093                 <th align=right nowrap>| . $locale->text('Date') . qq|</th>
1094                 $button1
1095               </tr>
1096             </table>
1097           </td>
1098         </tr>|;
1099   if ($form->{id}) {
1100     print qq|
1101         <tr>
1102           <th align=right>| . $locale->text('Belegnummer') . qq|</th>
1103           <td><input name=id size=20 value="$form->{id}" $readonly></td>
1104           <td align=left>
1105           <table>
1106               <tr>
1107                 <th align=right width=50%>| . $locale->text('Buchungsdatum') . qq|</th>
1108                 <td align=left><input name=gldate size=11 title="$myconfig{dateformat}" value=$form->{gldate} $readonly onBlur=\"check_right_date_format(this)\"></td>
1109               </tr>
1110             </table>
1111           </td>
1112         </tr>|;
1113   }
1114   print qq|     
1115         $department|;
1116   if ($form->{id}) {
1117     print qq|
1118         <tr>
1119           <th align=right width=1%>| . $locale->text('Description') . qq|</th>
1120           <td width=1%>$description</td>
1121           <td>
1122             <table>
1123               <tr>
1124                 <th align=left>| . $locale->text('MwSt. inkl.') . qq|</th>
1125                 <td><input type=checkbox name=taxincluded value=1 $taxincluded></td>
1126               </tr>
1127             </table>
1128          </td>
1129           <td align=left>
1130             <table width=100%>
1131               <tr>
1132                 <th align=right width=50%>| . $locale->text('Mitarbeiter') . qq|</th>
1133                 <td align=left><input name=employee size=20  value="| . H($form->{employee}) . qq|" readonly></td>
1134               </tr>
1135             </table>
1136           </td>
1137         </tr>|;
1138   } else {
1139     print qq|
1140         <tr>
1141           <th align=left width=1%>| . $locale->text('Description') . qq|</th>
1142           <td width=1%>$description</td>
1143           <td>
1144             <table>
1145               <tr>
1146                 <th align=left>| . $locale->text('MwSt. inkl.') . qq|</th>
1147                 <td><input type=checkbox name=taxincluded value=1 $taxincluded></td>
1148               </tr>
1149             </table>
1150          </td>
1151         </tr>|;
1152   }
1153
1154   print qq|<tr>
1155        <td width="1%" align="right" nowrap>| . $locale->text('Show details') . qq|</td>
1156        <td width="1%"><input type="checkbox" onclick="show_form_details();" name="show_details" value="1" $show_details_checked></td>
1157       </tr>|;
1158
1159   print qq|
1160       <tr>
1161       <td colspan=4>
1162           <table width=100%>
1163            <tr class=listheading>
1164           <th class=listheading style="width:15%">|
1165     . $locale->text('Account') . qq|</th>
1166           <th class=listheading style="width:10%">|
1167     . $locale->text('Debit') . qq|</th>
1168           <th class=listheading style="width:10%">|
1169     . $locale->text('Credit') . qq|</th>
1170           <th class=listheading style="width:10%">|
1171     . $locale->text('Tax') . qq|</th>
1172           <th class=listheading style="width:5%">|
1173     . $locale->text('Korrektur') . qq|</th>
1174           <th class=listheading style="width:10%">|
1175     . $locale->text('Taxkey') . qq|</th>|;
1176
1177   if ($form->{show_details}) {
1178     print qq|
1179           <th class=listheading style="width:20%">| . $locale->text('Source') . qq|</th>
1180           <th class=listheading style="width:20%">| . $locale->text('Memo') . qq|</th>
1181           <th class=listheading style="width:20%">| . $locale->text('Project Number') . qq|</th>
1182 |;
1183   }
1184
1185   print qq|
1186         </tr>
1187
1188 $jsscript
1189 |;
1190   $lxdebug->leave_sub();
1191
1192 }
1193
1194 sub form_footer {
1195   $lxdebug->enter_sub();
1196   ($dec) = ($form->{totaldebit} =~ /\.(\d+)/);
1197   $dec = length $dec;
1198   $decimalplaces = ($dec > 2) ? $dec : 2;
1199   $radieren = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
1200
1201   map {
1202     $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 2, "&nbsp;")
1203   } qw(totaldebit totalcredit);
1204
1205   print qq|
1206     <tr class=listtotal>
1207     <td></td>
1208     <th align=right class=listtotal> $form->{totaldebit}</th>
1209     <th align=right class=listtotal> $form->{totalcredit}</th> 
1210     <td colspan=6></td>
1211     </tr>
1212   </table>
1213   </td>
1214   </tr>
1215 </table>
1216
1217 <input type=hidden name=login value=$form->{login}>
1218 <input type=hidden name=password value=$form->{password}>
1219
1220 <input name=callback type=hidden value="$form->{callback}">
1221
1222 <br>
1223 |;
1224
1225   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1226   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
1227
1228   if ($form->{id}) {
1229
1230     if (!$form->{storno}) {
1231       print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|">|;
1232     }
1233
1234     # Löschen und Ändern von Buchungen nicht mehr möglich (GoB) nur am selben Tag möglich
1235     if (!$form->{locked} && $radieren) {
1236       print qq|
1237         <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|" accesskey="b">
1238         <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|">|;
1239     }
1240
1241   } else {
1242     if ($transdate > $closedto) {
1243       print qq|
1244         <input class=submit type=submit name=action id=update_button value="| . $locale->text('Update') . qq|">
1245         <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">|;
1246     }
1247   }
1248
1249   print "
1250   </form>
1251
1252 </body>
1253 </html>
1254 ";
1255   $lxdebug->leave_sub();
1256
1257 }
1258
1259 sub delete {
1260   $lxdebug->enter_sub();
1261
1262   $form->header;
1263
1264   print qq|
1265 <body>
1266
1267 <form method=post action=$form->{script}>
1268 |;
1269
1270   map { $form->{$_} =~ s/\"/&quot;/g } qw(reference description);
1271
1272   delete $form->{header};
1273
1274   foreach $key (keys %$form) {
1275     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1276   }
1277
1278   print qq|
1279 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1280
1281 <h4>|
1282     . $locale->text('Are you sure you want to delete Transaction')
1283     . qq| $form->{reference}</h4>
1284
1285 <input name=action class=submit type=submit value="|
1286     . $locale->text('Yes') . qq|">
1287 </form>
1288 |;
1289   $lxdebug->leave_sub();
1290
1291 }
1292
1293 sub yes {
1294   $lxdebug->enter_sub();
1295   if (GL->delete_transaction(\%myconfig, \%$form)){
1296     # saving the history
1297       if(!exists $form->{addition} && $form->{id} ne "") {
1298         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1299             $form->{addition} = "DELETED";
1300             $form->save_history($form->dbconnect(\%myconfig));
1301       }
1302     # /saving the history 
1303     $form->redirect($locale->text('Transaction deleted!'))
1304   }
1305   $form->error($locale->text('Cannot delete transaction!'));
1306   $lxdebug->leave_sub();
1307
1308 }
1309
1310 sub post_transaction {
1311   $lxdebug->enter_sub();
1312
1313   # check if there is something in reference and date
1314   $form->isblank("reference",   $locale->text('Reference missing!'));
1315   $form->isblank("transdate",   $locale->text('Transaction Date missing!'));
1316   $form->isblank("description", $locale->text('Description missing!'));
1317
1318   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1319   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
1320
1321   my @a           = ();
1322   my $count       = 0;
1323   my $debittax    = 0;
1324   my $credittax   = 0;
1325   my $debitcount  = 0;
1326   my $creditcount = 0;
1327   $creditlock = 0;
1328   $debitlock  = 0;
1329
1330   my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
1331
1332   for my $i (1 .. $form->{rowcount}) {
1333     next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
1334
1335     for (qw(debit credit tax)) {
1336       $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
1337     }
1338
1339     push @a, {};
1340     $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
1341
1342     if ($debitcredit) {
1343       $debitcount++;
1344     } else {
1345       $creditcount++;
1346     }
1347
1348     if (($debitcount >= 2) && ($creditcount == 2)) {
1349       $form->{"credit_$i"} = 0;
1350       $form->{"tax_$i"}    = 0;
1351       $creditcount--;
1352       $creditlock = 1;
1353     }
1354     if (($creditcount >= 2) && ($debitcount == 2)) {
1355       $form->{"debit_$i"} = 0;
1356       $form->{"tax_$i"}   = 0;
1357       $debitcount--;
1358       $debitlock = 1;
1359     }
1360     if (($creditcount == 1) && ($debitcount == 2)) {
1361       $creditlock = 1;
1362     }
1363     if (($creditcount == 2) && ($debitcount == 1)) {
1364       $debitlock = 1;
1365     }
1366     if ($debitcredit && $credittax) {
1367       $form->{"taxchart_$i"} = "0--0.00";
1368     }
1369     if (!$debitcredit && $debittax) {
1370       $form->{"taxchart_$i"} = "0--0.00";
1371     }
1372     $amount = ($form->{"debit_$i"} == 0)
1373             ? $form->{"credit_$i"}
1374             : $form->{"debit_$i"};
1375     $j = $#a;
1376     if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1377       $form->{"taxchart_$i"} = "0--0.00";
1378       $form->{"tax_$i"}      = 0;
1379     }
1380     if (!$form->{"korrektur_$i"}) {
1381       ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1382       if ($taxkey > 1) {
1383         if ($debitcredit) {
1384           $debittax = 1;
1385         } else {
1386           $credittax = 1;
1387         }
1388         if ($form->{taxincluded}) {
1389           $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
1390           if ($debitcredit) {
1391             $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
1392           } else {
1393             $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
1394           }
1395         } else {
1396           $form->{"tax_$i"} = $amount * $rate;
1397         }
1398       } else {
1399         $form->{"tax_$i"} = 0;
1400       }
1401     } elsif ($form->{taxincluded}) {
1402       if ($debitcredit) {
1403         $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
1404       } else {
1405         $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
1406       }
1407     }
1408
1409     for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1410     $count++;
1411   }
1412
1413   for $i (1 .. $count) {
1414     $j = $i - 1;
1415     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1416   }
1417
1418   for $i ($count + 1 .. $form->{rowcount}) {
1419     for (@flds) { delete $form->{"${_}_$i"} }
1420   }
1421
1422   for $i (1 .. $form->{rowcount}) {
1423     $dr  = $form->{"debit_$i"};
1424     $cr  = $form->{"credit_$i"};
1425     $tax = $form->{"tax_$i"};
1426     if ($dr && $cr) {
1427       $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
1428     }
1429     $debit    += $dr + $tax if $dr;
1430     $credit   += $cr + $tax if $cr;
1431     $taxtotal += $tax if $form->{taxincluded}
1432   }
1433   
1434   $form->{taxincluded} = 0 if !$taxtotal;
1435
1436   # this is just for the wise guys
1437   $form->error($locale->text('Cannot post transaction for a closed period!'))
1438     if ($transdate <= $closedto);
1439   if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1440     $form->error($locale->text('Out of balance transaction!'));
1441   }
1442   
1443   if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1444     $form->error($locale->text('Empty transaction!'));
1445   }
1446   
1447   if (($errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1448     $errno *= -1;
1449     $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1450     $err[2] = $locale->text('Debit and credit out of balance!');
1451     $err[3] = $locale->text('Cannot post a transaction without a value!');
1452
1453     $form->error($err[$errno]);
1454   }
1455   undef($form->{callback});
1456   # saving the history
1457   if(!exists $form->{addition} && $form->{id} ne "") {
1458     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1459     $form->{addition} = "SAVED";
1460     $form->{what_done} = $locale->text("Buchungsnummer") . " = " . $form->{id}; 
1461     $form->save_history($form->dbconnect(\%myconfig));
1462   }
1463   # /saving the history 
1464
1465   $lxdebug->leave_sub();
1466 }
1467
1468 sub post {
1469   $lxdebug->enter_sub();
1470
1471   $form->{title}  = $locale->text("$form->{title} General Ledger Transaction");
1472   $form->{storno} = 0;
1473
1474   post_transaction();
1475
1476   $form->{callback} = build_std_url("action=add", "show_details");
1477   $form->redirect($form->{callback});
1478
1479   $lxdebug->leave_sub();
1480 }
1481
1482 sub post_as_new {
1483   $lxdebug->enter_sub();
1484
1485   $form->{id} = 0;
1486   &add;
1487   $lxdebug->leave_sub();
1488
1489 }
1490
1491 sub storno {
1492   $lxdebug->enter_sub();
1493
1494   # don't cancel cancelled transactions
1495   if (IS->has_storno(\%myconfig, $form, 'gl')) {
1496     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1497     $form->error($locale->text("Transaction has already been cancelled!"));
1498   }
1499
1500   GL->storno($form, \%myconfig, $form->{id});
1501
1502   # saving the history
1503   if(!exists $form->{addition} && $form->{id} ne "") {
1504     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1505     $form->{addition} = "STORNO";
1506     $form->save_history($form->dbconnect(\%myconfig));
1507   }
1508   # /saving the history 
1509
1510   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id}); 
1511
1512   $lxdebug->leave_sub();
1513 }
1514