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