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