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