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