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