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