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