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