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