5069cefa7f89fd26cfbab3999b07fa99c7fd153b
[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/reportgenerator.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} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno);
584
585     foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
586       $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
587     }
588
589     map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
590
591     $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
592
593     my $row_set = [ $row ];
594
595     if (($form->{l_subtotal} eq 'Y')
596         && (($idx == (scalar @{ $form->{GL} } - 1))
597             || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
598       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
599     }
600
601     $report->add_data($row_set);
602
603     $idx++;
604   }
605
606   $report->add_separator();
607
608   # = 0 for balanced ledger
609   my $balanced_ledger = $totals{debit} + $totals{debit_tax} - $totals{credit} - $totals{credit_tax};
610
611   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, [ qw(debit credit debit_tax credit_tax) ], 'listtotal');
612   $row->{balance} = {
613     'data'  => $form->format_amount(\%myconfig, $form->{balance} * $ml, 2),
614     'align' => 'right',
615     'class' => 'listtotal',
616   };
617   $report->add_data($row);
618
619   my $raw_bottom_info_text;
620
621   if (!$form->{accno} && (abs($balanced_ledger) >  0.001)) {
622     $raw_bottom_info_text .=
623         '<p><span class="unbalanced_ledger">'
624       . $locale->text('Unbalanced Ledger')
625       . ': '
626       . $form->format_amount(\%myconfig, $balanced_ledger, 3)
627       . '</span></p> ';
628   }
629
630   $raw_bottom_info_text .= $form->parse_html_template('gl/generate_report_bottom');
631
632   $report->set_options('raw_bottom_info_text' => $raw_bottom_info_text);
633
634   $report->generate_with_headers();
635
636   $lxdebug->leave_sub();
637 }
638
639 sub update {
640   $lxdebug->enter_sub();
641
642   $form->{oldtransdate} = $form->{transdate};
643
644   my @a           = ();
645   my $count       = 0;
646   my $debittax    = 0;
647   my $credittax   = 0;
648   my $debitcount  = 0;
649   my $creditcount = 0;
650   $debitlock  = 0;
651   $creditlock = 0;
652
653   my @flds =
654     qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
655
656   for my $i (1 .. $form->{rowcount}) {
657
658     unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
659       for (qw(debit credit tax)) {
660         $form->{"${_}_$i"} =
661           $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
662       }
663
664       push @a, {};
665       $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
666       if ($debitcredit) {
667         $debitcount++;
668       } else {
669         $creditcount++;
670       }
671
672       if (($debitcount >= 2) && ($creditcount == 2)) {
673         $form->{"credit_$i"} = 0;
674         $form->{"tax_$i"}    = 0;
675         $creditcount--;
676         $creditlock = 1;
677       }
678       if (($creditcount >= 2) && ($debitcount == 2)) {
679         $form->{"debit_$i"} = 0;
680         $form->{"tax_$i"}   = 0;
681         $debitcount--;
682         $debitlock = 1;
683       }
684       if (($creditcount == 1) && ($debitcount == 2)) {
685         $creditlock = 1;
686       }
687       if (($creditcount == 2) && ($debitcount == 1)) {
688         $debitlock = 1;
689       }
690       if ($debitcredit && $credittax) {
691         $form->{"taxchart_$i"} = "0--0.00";
692       }
693       if (!$debitcredit && $debittax) {
694         $form->{"taxchart_$i"} = "0--0.00";
695       }
696       $amount =
697         ($form->{"debit_$i"} == 0)
698         ? $form->{"credit_$i"}
699         : $form->{"debit_$i"};
700       $j = $#a;
701       if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
702         $form->{"taxchart_$i"} = "0--0.00";
703         $form->{"tax_$i"}      = 0;
704       }
705       if (!$form->{"korrektur_$i"}) {
706         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
707         if ($taxkey > 1) {
708           if ($debitcredit) {
709             $debittax = 1;
710           } else {
711             $credittax = 1;
712           }
713           if ($form->{taxincluded}) {
714             $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
715           } else {
716             $form->{"tax_$i"} = $amount * $rate;
717           }
718         } else {
719           $form->{"tax_$i"} = 0;
720         }
721       }
722
723       for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
724       $count++;
725     }
726   }
727
728   for $i (1 .. $count) {
729     $j = $i - 1;
730     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
731   }
732
733   for $i ($count + 1 .. $form->{rowcount}) {
734     for (@flds) { delete $form->{"${_}_$i"} }
735   }
736
737   $form->{rowcount} = $count + 1;
738
739   &display_form;
740   $lxdebug->leave_sub();
741
742 }
743
744 sub display_form {
745   my ($init) = @_;
746   $lxdebug->enter_sub();
747
748   &form_header($init);
749
750   #   for $i (1 .. $form->{rowcount}) {
751   #     $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
752   #     $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
753   #
754   #     &form_row($i);
755   #   }
756   &display_rows($init);
757   &form_footer;
758   $lxdebug->leave_sub();
759
760 }
761
762 sub display_rows {
763   my ($init) = @_;
764   $lxdebug->enter_sub();
765
766   $form->{debit_1}     = 0 if !$form->{"debit_1"};
767   $form->{totaldebit}  = 0;
768   $form->{totalcredit} = 0;
769
770   my @old_project_ids = ();
771   map({ push(@old_project_ids, $form->{"project_id_$_"})
772           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
773
774   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
775                                    "all" => 0,
776                                    "old_id" => \@old_project_ids },
777                    "charts" => { "key" => "ALL_CHARTS",
778                                  "transdate" => $form->{transdate} },
779                    "taxcharts" => "ALL_TAXCHARTS");
780
781   my %project_labels = ();
782   my @project_values = ("");
783   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
784     push(@project_values, $item->{"id"});
785     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
786   }
787
788   my %chart_labels = ();
789   my @chart_values = ();
790   my %charts = ();
791   my $taxchart_init;
792   foreach my $item (@{ $form->{ALL_CHARTS} }) {
793     my $key = Q($item->{accno}) . "--" . Q($item->{tax_id});
794     $taxchart_init = $item->{taxkey_id} unless (@chart_values);
795     push(@chart_values, $key);
796     $chart_labels{$key} = H($item->{accno}) . "--" . H($item->{description});
797     $charts{$item->{accno}} = $item;
798   }
799
800   my %taxchart_labels = ();
801   my @taxchart_values = ();
802   my %taxcharts = ();
803   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
804     my $key = Q($item->{id}) . "--" . Q($item->{rate});
805     $taxchart_init = $key if ($taxchart_init eq $item->{taxkey});
806     push(@taxchart_values, $key);
807     $taxchart_labels{$key} = H($item->{taxdescription}) . " " .
808       H($item->{rate} * 100) . ' %';
809     $taxcharts{$item->{id}} = $item;
810   }
811
812   for $i (1 .. $form->{rowcount}) {
813
814     $source = qq|
815     <td><input name="source_$i" value="$form->{"source_$i"}" size="16"></td>|;
816     $memo = qq|
817     <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16"></td>|;
818
819     my $selected_accno_full;
820     my ($accno_row) = split(/--/, $form->{"accno_$i"});
821     my $item = $charts{$accno_row};
822     $selected_accno_full = "$item->{accno}--$item->{tax_id}";
823
824     my $selected_taxchart = $form->{"taxchart_$i"};
825     my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
826     my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_accno_$i"});
827
828     if ($previous_accno &&
829         ($previous_accno eq $selected_accno) &&
830         ($previous_tax_id ne $selected_tax_id)) {
831       my $item = $taxcharts{$selected_tax_id};
832       $selected_taxchart = "$item->{id}--$item->{rate}";
833     }
834
835     $selected_accno = '' if ($init);
836     $selected_taxchart = $taxchart_init unless ($selected_taxchart ne "");
837
838     $accno = qq|<td>| .
839       NTI($cgi->popup_menu('-name' => "accno_$i",
840                            '-id' => "accno_$i",
841                            '-onChange' => "setTaxkey(this, $i)",
842                            '-style' => 'width:200px',
843                            '-values' => \@chart_values,
844                            '-labels' => \%chart_labels,
845                            '-default' => $selected_accno_full))
846       . $cgi->hidden('-name' => "previous_accno_$i",
847                      '-default' => $selected_accno_full)
848       . qq|</td>|;
849     $tax = qq|<td>| .
850       NTI($cgi->popup_menu('-name' => "taxchart_$i",
851                            '-id' => "taxchart_$i",
852                            '-style' => 'width:200px',
853                            '-values' => \@taxchart_values,
854                            '-labels' => \%taxchart_labels,
855                            '-default' => $selected_taxchart))
856       . qq|</td>|;
857
858     if ($init) {
859       $korrektur =
860         qq|<td><input type="checkbox" name="korrektur_$i" value="1"></td>|;
861       if ($form->{transfer}) {
862         $fx_transaction = qq|
863         <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
864     |;
865       }
866
867     } else {
868       if ($form->{"debit_$i"} != 0) {
869         $form->{totaldebit} += $form->{"debit_$i"};
870         if (!$form->{taxincluded}) {
871           $form->{totaldebit} += $form->{"tax_$i"};
872         }
873       } else {
874         $form->{totalcredit} += $form->{"credit_$i"};
875         if (!$form->{taxincluded}) {
876           $form->{totalcredit} += $form->{"tax_$i"};
877         }
878       }
879
880       for (qw(debit credit tax)) {
881         $form->{"${_}_$i"} =
882           ($form->{"${_}_$i"})
883           ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
884           : "";
885       }
886
887       if ($i < $form->{rowcount}) {
888         if ($form->{transfer}) {
889           $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
890           $x = ($checked) ? "x" : "";
891           $fx_transaction = qq|
892       <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
893     |;
894         }
895         $checked = ($form->{"korrektur_$i"}) ? "checked" : "";
896         $korrektur =
897           qq|<td><input type="checkbox" name="korrektur_$i" value="1" $checked></td>|;
898         $form->hide_form("accno_$i");
899
900       } else {
901         $korrektur =
902           qq|<td><input type="checkbox" name="korrektur_$i" value="1"></td>|;
903         if ($form->{transfer}) {
904           $fx_transaction = qq|
905       <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
906     |;
907         }
908       }
909     }
910     my $debitreadonly  = "";
911     my $creditreadonly = "";
912     if ($i == $form->{rowcount}) {
913       if ($debitlock) {
914         $debitreadonly = "readonly";
915       } elsif ($creditlock) {
916         $creditreadonly = "readonly";
917       }
918     }
919
920     my $projectnumber =
921       NTI($cgi->popup_menu('-name' => "project_id_$i",
922                            '-values' => \@project_values,
923                            '-labels' => \%project_labels,
924                            '-default' => $form->{"project_id_$i"} ));
925
926     my $copy2credit = 'onkeyup="copy_debit_to_credit()"' if $i == 1;
927
928     print qq|<tr valign=top>
929     $accno
930     $fx_transaction
931     <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
932     <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
933     <td><input name="tax_$i" size=6 value="$form->{"tax_$i"}"></td>
934     $korrektur
935     $tax|;
936
937     if ($form->{show_details}) {
938       print qq|
939     $source
940     $memo
941     <td>$projectnumber</td>
942 |;
943     }
944     print qq|
945   </tr>
946 |;
947   }
948
949   $form->hide_form(qw(rowcount selectaccno));
950
951   $lxdebug->leave_sub();
952
953 }
954
955 sub form_header {
956   my ($init) = @_;
957   $lxdebug->enter_sub();
958   $title         = $form->{title};
959   $form->{title} = $locale->text("$title General Ledger Transaction");
960   $readonly      = ($form->{id}) ? "readonly" : "";
961
962   $show_details_checked = "checked" if $form->{show_details};
963
964   # $locale->text('Add General Ledger Transaction')
965   # $locale->text('Edit General Ledger Transaction')
966
967   map { $form->{$_} =~ s/\"/&quot;/g }
968     qw(reference description chart taxchart);
969
970   $form->{javascript} = qq|<script type="text/javascript">
971   <!--
972   function setTaxkey(accno, row) {
973     var taxkey = accno.options[accno.selectedIndex].value;
974     var reg = /--([0-9]*)/;
975     var found = reg.exec(taxkey);
976     var index = found[1];
977     index = parseInt(index);
978     var tax = 'taxchart_' + row;
979     for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
980       var reg2 = new RegExp("^"+ index, "");
981       if (reg2.exec(document.getElementById(tax).options[i].value)) {
982         document.getElementById(tax).options[i].selected = true;
983         break;
984       }
985     }
986   };
987
988   function copy_debit_to_credit() {
989     var txt = document.getElementsByName('debit_1')[0].value;
990     document.getElementsByName('credit_2')[0].value = txt;
991   };
992
993   //-->
994   </script>|;
995   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
996
997   $form->{selectdepartment} =~ s/ selected//;
998   $form->{selectdepartment} =~
999     s/option>\Q$form->{department}\E/option selected>$form->{department}/;
1000
1001   if (($rows = $form->numtextrows($form->{description}, 50)) > 1) {
1002     $description =
1003       qq|<textarea name=description rows=$rows cols=50 wrap=soft $readonly >$form->{description}</textarea>|;
1004   } else {
1005     $description =
1006       qq|<input name=description size=50 value="$form->{description}" $readonly>|;
1007   }
1008
1009   $taxincluded = ($form->{taxincluded}) ? "checked" : "";
1010
1011   if ($init) {
1012     $taxincluded = "checked";
1013   }
1014
1015   $department = qq|
1016         <tr>
1017           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1018           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1019           <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
1020         </tr>
1021 | if $form->{selectdepartment};
1022   if ($init) {
1023     $form->{fokus} = "gl.reference";
1024   } else {
1025     $form->{fokus} = qq|gl.accno_$form->{rowcount}|;
1026   }
1027
1028   # use JavaScript Calendar or not
1029   $form->{jsscript} = 1;
1030   $jsscript = "";
1031   if ($form->{jsscript}) {
1032
1033     # with JavaScript Calendar
1034     $button1 = qq|
1035        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" $readonly onBlur=\"check_right_date_format(this)\">
1036        <input type=button name=transdate id="trigger1" value=|
1037       . $locale->text('button') . qq|></td>  
1038        |;
1039
1040     #write Trigger
1041     $jsscript =
1042       Form->write_trigger(\%myconfig, "1", "transdate", "BL", "trigger1");
1043   } else {
1044
1045     # without JavaScript Calendar
1046     $button1 =
1047       qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" $readonly onBlur=\"check_right_date_format(this)\"></td>|;
1048   }
1049
1050   $form->{previous_id}     ||= "--";
1051   $form->{previous_gldate} ||= "--";
1052
1053   $form->header;
1054
1055   print qq|
1056 <body onLoad="fokus()">
1057
1058 <form method=post name="gl" action=$form->{script}>
1059 |;
1060
1061   $form->hide_form(qw(id closedto locked storno storno_id previous_id previous_gldate));
1062
1063   print qq|
1064 <input type=hidden name=title value="$title">
1065
1066
1067 <table width=100%>
1068   <tr>
1069     <th class=listtop>$form->{title}</th>
1070   </tr>
1071   <tr height="5"></tr>
1072   <tr>
1073     <td>
1074       <table width=100%>
1075         <tr>
1076           <td colspan="6" align="left">|
1077     . $locale->text("Previous transnumber text")
1078     . " $form->{previous_id} "
1079     . $locale->text("Previous transdate text")
1080     . " $form->{previous_gldate}"
1081     . qq|</td>
1082         </tr>
1083         <tr>
1084           <th align=right>| . $locale->text('Reference') . qq|</th>
1085           <td><input name=reference size=20 value="$form->{reference}" $readonly></td>
1086           <td align=left>
1087             <table>
1088               <tr>
1089                 <th align=right nowrap>| . $locale->text('Date') . qq|</th>
1090                 $button1
1091               </tr>
1092             </table>
1093           </td>
1094         </tr>|;
1095   if ($form->{id}) {
1096     print qq|
1097         <tr>
1098           <th align=right>| . $locale->text('Belegnummer') . qq|</th>
1099           <td><input name=id size=20 value="$form->{id}" $readonly></td>
1100           <td align=left>
1101           <table>
1102               <tr>
1103                 <th align=right width=50%>| . $locale->text('Buchungsdatum') . qq|</th>
1104                 <td align=left><input name=gldate size=11 title="$myconfig{dateformat}" value=$form->{gldate} $readonly onBlur=\"check_right_date_format(this)\"></td>
1105               </tr>
1106             </table>
1107           </td>
1108         </tr>|;
1109   }
1110   print qq|     
1111         $department|;
1112   if ($form->{id}) {
1113     print qq|
1114         <tr>
1115           <th align=right width=1%>| . $locale->text('Description') . qq|</th>
1116           <td width=1%>$description</td>
1117           <td>
1118             <table>
1119               <tr>
1120                 <th align=left>| . $locale->text('MwSt. inkl.') . qq|</th>
1121                 <td><input type=checkbox name=taxincluded value=1 $taxincluded></td>
1122               </tr>
1123             </table>
1124          </td>
1125           <td align=left>
1126             <table width=100%>
1127               <tr>
1128                 <th align=right width=50%>| . $locale->text('Mitarbeiter') . qq|</th>
1129                 <td align=left><input name=employee size=20  value="| . H($form->{employee}) . qq|" readonly></td>
1130               </tr>
1131             </table>
1132           </td>
1133         </tr>|;
1134   } else {
1135     print qq|
1136         <tr>
1137           <th align=left width=1%>| . $locale->text('Description') . qq|</th>
1138           <td width=1%>$description</td>
1139           <td>
1140             <table>
1141               <tr>
1142                 <th align=left>| . $locale->text('MwSt. inkl.') . qq|</th>
1143                 <td><input type=checkbox name=taxincluded value=1 $taxincluded></td>
1144               </tr>
1145             </table>
1146          </td>
1147         </tr>|;
1148   }
1149
1150   print qq|<tr>
1151        <td width="1%" align="right" nowrap>| . $locale->text('Show details') . qq|</td>
1152        <td width="1%"><input type="checkbox" onclick="show_form_details();" name="show_details" value="1" $show_details_checked></td>
1153       </tr>|;
1154
1155   print qq|
1156       <tr>
1157       <td colspan=4>
1158           <table width=100%>
1159            <tr class=listheading>
1160           <th class=listheading style="width:15%">|
1161     . $locale->text('Account') . qq|</th>
1162           <th class=listheading style="width:10%">|
1163     . $locale->text('Debit') . qq|</th>
1164           <th class=listheading style="width:10%">|
1165     . $locale->text('Credit') . qq|</th>
1166           <th class=listheading style="width:10%">|
1167     . $locale->text('Tax') . qq|</th>
1168           <th class=listheading style="width:5%">|
1169     . $locale->text('Korrektur') . qq|</th>
1170           <th class=listheading style="width:10%">|
1171     . $locale->text('Taxkey') . qq|</th>|;
1172
1173   if ($form->{show_details}) {
1174     print qq|
1175           <th class=listheading style="width:20%">| . $locale->text('Source') . qq|</th>
1176           <th class=listheading style="width:20%">| . $locale->text('Memo') . qq|</th>
1177           <th class=listheading style="width:20%">| . $locale->text('Project Number') . qq|</th>
1178 |;
1179   }
1180
1181   print qq|
1182         </tr>
1183
1184 $jsscript
1185 |;
1186   $lxdebug->leave_sub();
1187
1188 }
1189
1190 sub form_footer {
1191   $lxdebug->enter_sub();
1192   ($dec) = ($form->{totaldebit} =~ /\.(\d+)/);
1193   $dec = length $dec;
1194   $decimalplaces = ($dec > 2) ? $dec : 2;
1195   $radieren = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
1196
1197   map {
1198     $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 2, "&nbsp;")
1199   } qw(totaldebit totalcredit);
1200
1201   print qq|
1202     <tr class=listtotal>
1203     <td></td>
1204     <th align=right class=listtotal> $form->{totaldebit}</th>
1205     <th align=right class=listtotal> $form->{totalcredit}</th> 
1206     <td colspan=6></td>
1207     </tr>
1208   </table>
1209   </td>
1210   </tr>
1211 </table>
1212
1213 <input type=hidden name=login value=$form->{login}>
1214 <input type=hidden name=password value=$form->{password}>
1215
1216 <input name=callback type=hidden value="$form->{callback}">
1217
1218 <br>
1219 |;
1220
1221   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1222   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
1223
1224   if ($form->{id}) {
1225
1226     if (!$form->{storno}) {
1227       print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|">|;
1228     }
1229
1230     # Löschen und Ändern von Buchungen nicht mehr möglich (GoB) nur am selben Tag möglich
1231     if (!$form->{locked} && $radieren) {
1232       print qq|
1233         <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|" accesskey="b">
1234         <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|">|;
1235     }
1236
1237   } else {
1238     if ($transdate > $closedto) {
1239       print qq|
1240         <input class=submit type=submit name=action id=update_button value="| . $locale->text('Update') . qq|">
1241         <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">|;
1242     }
1243   }
1244
1245   print "
1246   </form>
1247
1248 </body>
1249 </html>
1250 ";
1251   $lxdebug->leave_sub();
1252
1253 }
1254
1255 sub delete {
1256   $lxdebug->enter_sub();
1257
1258   $form->header;
1259
1260   print qq|
1261 <body>
1262
1263 <form method=post action=$form->{script}>
1264 |;
1265
1266   map { $form->{$_} =~ s/\"/&quot;/g } qw(reference description);
1267
1268   delete $form->{header};
1269
1270   foreach $key (keys %$form) {
1271     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1272   }
1273
1274   print qq|
1275 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1276
1277 <h4>|
1278     . $locale->text('Are you sure you want to delete Transaction')
1279     . qq| $form->{reference}</h4>
1280
1281 <input name=action class=submit type=submit value="|
1282     . $locale->text('Yes') . qq|">
1283 </form>
1284 |;
1285   $lxdebug->leave_sub();
1286
1287 }
1288
1289 sub yes {
1290   $lxdebug->enter_sub();
1291   if (GL->delete_transaction(\%myconfig, \%$form)){
1292     # saving the history
1293       if(!exists $form->{addition} && $form->{id} ne "") {
1294         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1295             $form->{addition} = "DELETED";
1296             $form->save_history($form->dbconnect(\%myconfig));
1297       }
1298     # /saving the history 
1299     $form->redirect($locale->text('Transaction deleted!'))
1300   }
1301   $form->error($locale->text('Cannot delete transaction!'));
1302   $lxdebug->leave_sub();
1303
1304 }
1305
1306 sub post_transaction {
1307   $lxdebug->enter_sub();
1308
1309   # check if there is something in reference and date
1310   $form->isblank("reference",   $locale->text('Reference missing!'));
1311   $form->isblank("transdate",   $locale->text('Transaction Date missing!'));
1312   $form->isblank("description", $locale->text('Description missing!'));
1313
1314   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1315   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
1316
1317   my @a           = ();
1318   my $count       = 0;
1319   my $debittax    = 0;
1320   my $credittax   = 0;
1321   my $debitcount  = 0;
1322   my $creditcount = 0;
1323   $creditlock = 0;
1324   $debitlock  = 0;
1325
1326   my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
1327
1328   for my $i (1 .. $form->{rowcount}) {
1329     next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq "";
1330
1331     for (qw(debit credit tax)) {
1332       $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
1333     }
1334
1335     push @a, {};
1336     $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
1337
1338     if ($debitcredit) {
1339       $debitcount++;
1340     } else {
1341       $creditcount++;
1342     }
1343
1344     if (($debitcount >= 2) && ($creditcount == 2)) {
1345       $form->{"credit_$i"} = 0;
1346       $form->{"tax_$i"}    = 0;
1347       $creditcount--;
1348       $creditlock = 1;
1349     }
1350     if (($creditcount >= 2) && ($debitcount == 2)) {
1351       $form->{"debit_$i"} = 0;
1352       $form->{"tax_$i"}   = 0;
1353       $debitcount--;
1354       $debitlock = 1;
1355     }
1356     if (($creditcount == 1) && ($debitcount == 2)) {
1357       $creditlock = 1;
1358     }
1359     if (($creditcount == 2) && ($debitcount == 1)) {
1360       $debitlock = 1;
1361     }
1362     if ($debitcredit && $credittax) {
1363       $form->{"taxchart_$i"} = "0--0.00";
1364     }
1365     if (!$debitcredit && $debittax) {
1366       $form->{"taxchart_$i"} = "0--0.00";
1367     }
1368     $amount = ($form->{"debit_$i"} == 0)
1369             ? $form->{"credit_$i"}
1370             : $form->{"debit_$i"};
1371     $j = $#a;
1372     if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1373       $form->{"taxchart_$i"} = "0--0.00";
1374       $form->{"tax_$i"}      = 0;
1375     }
1376     if (!$form->{"korrektur_$i"}) {
1377       ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1378       if ($taxkey > 1) {
1379         if ($debitcredit) {
1380           $debittax = 1;
1381         } else {
1382           $credittax = 1;
1383         }
1384         if ($form->{taxincluded}) {
1385           $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
1386           if ($debitcredit) {
1387             $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
1388           } else {
1389             $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
1390           }
1391         } else {
1392           $form->{"tax_$i"} = $amount * $rate;
1393         }
1394       } else {
1395         $form->{"tax_$i"} = 0;
1396       }
1397     } elsif ($form->{taxincluded}) {
1398       if ($debitcredit) {
1399         $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
1400       } else {
1401         $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
1402       }
1403     }
1404
1405     for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1406     $count++;
1407   }
1408
1409   for $i (1 .. $count) {
1410     $j = $i - 1;
1411     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1412   }
1413
1414   for $i ($count + 1 .. $form->{rowcount}) {
1415     for (@flds) { delete $form->{"${_}_$i"} }
1416   }
1417
1418   for $i (1 .. $form->{rowcount}) {
1419     $dr  = $form->{"debit_$i"};
1420     $cr  = $form->{"credit_$i"};
1421     $tax = $form->{"tax_$i"};
1422     if ($dr && $cr) {
1423       $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!'));
1424     }
1425     $debit    += $dr + $tax if $dr;
1426     $credit   += $cr + $tax if $cr;
1427     $taxtotal += $tax if $form->{taxincluded}
1428   }
1429   
1430   $form->{taxincluded} = 0 if !$taxtotal;
1431
1432   # this is just for the wise guys
1433   $form->error($locale->text('Cannot post transaction for a closed period!'))
1434     if ($transdate <= $closedto);
1435   if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1436     $form->error($locale->text('Out of balance transaction!'));
1437   }
1438   
1439   if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1440     $form->error($locale->text('Empty transaction!'));
1441   }
1442   
1443   if (($errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1444     $errno *= -1;
1445     $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1446     $err[2] = $locale->text('Debit and credit out of balance!');
1447     $err[3] = $locale->text('Cannot post a transaction without a value!');
1448
1449     $form->error($err[$errno]);
1450   }
1451   undef($form->{callback});
1452   # saving the history
1453   if(!exists $form->{addition} && $form->{id} ne "") {
1454     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1455     $form->{addition} = "SAVED";
1456     $form->{what_done} = $locale->text("Buchungsnummer") . " = " . $form->{id}; 
1457     $form->save_history($form->dbconnect(\%myconfig));
1458   }
1459   # /saving the history 
1460
1461   $lxdebug->leave_sub();
1462 }
1463
1464 sub post {
1465   $lxdebug->enter_sub();
1466
1467   $form->{title}  = $locale->text("$form->{title} General Ledger Transaction");
1468   $form->{storno} = 0;
1469
1470   post_transaction();
1471
1472   $form->{callback} = build_std_url("action=add", "show_details");
1473   $form->redirect($form->{callback});
1474
1475   $lxdebug->leave_sub();
1476 }
1477
1478 sub post_as_new {
1479   $lxdebug->enter_sub();
1480
1481   $form->{id} = 0;
1482   &add;
1483   $lxdebug->leave_sub();
1484
1485 }
1486
1487 sub storno {
1488   $lxdebug->enter_sub();
1489
1490   # don't cancel cancelled transactions
1491   if (IS->has_storno(\%myconfig, $form, 'gl')) {
1492     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");
1493     $form->error($locale->text("Transaction has already been cancelled!"));
1494   }
1495
1496   GL->storno($form, \%myconfig, $form->{id});
1497
1498   # saving the history
1499   if(!exists $form->{addition} && $form->{id} ne "") {
1500     $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1501     $form->{addition} = "STORNO";
1502     $form->save_history($form->dbconnect(\%myconfig));
1503   }
1504   # /saving the history 
1505
1506   $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id}); 
1507
1508   $lxdebug->leave_sub();
1509 }
1510