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