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