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