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