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