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