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