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