Dialogbuchen: Die Drop-Down-Boxen bei jedem Formularaufbau aus der Datenbank heraus...
[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     } else {
152       $form->{"accno_$i"} = "$ref->{accno}--$ref->{tax_id}";
153       for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
154       if ($ref->{amount} < 0) {
155         $form->{totaldebit} -= $ref->{amount};
156         $form->{"debit_$i"} = $ref->{amount} * -1;
157       } else {
158         $form->{totalcredit} += $ref->{amount};
159         $form->{"credit_$i"} = $ref->{amount};
160       }
161       $form->{"taxchart_$i"} = "0--0.00";
162       $i++;
163     }
164     if ($ref->{taxaccno} && !$tax) {
165       $taxaccno = $ref->{taxaccno};
166       $tax      = 1;
167     } else {
168       $taxaccno = "";
169       $tax      = 0;
170     }
171
172   }
173
174   $form->{rowcount} = $i;
175   $form->{locked}   =
176     ($form->datetonum($form->{transdate}, \%myconfig) <=
177      $form->datetonum($form->{closedto}, \%myconfig));
178
179   $form->{title} = "Edit";
180
181   &form_header;
182   &display_rows;
183   &form_footer;
184   $lxdebug->leave_sub();
185
186 }
187
188 sub search {
189   $lxdebug->enter_sub();
190
191   $form->{title} = $locale->text('Buchungsjournal');
192
193   $form->all_departments(\%myconfig);
194
195   # departments
196   if (@{ $form->{all_departments} }) {
197     $form->{selectdepartment} = "<option>\n";
198
199     map {
200       $form->{selectdepartment} .=
201         "<option>$_->{description}--$_->{id}\n"
202     } (@{ $form->{all_departments} });
203   }
204
205   $department = qq|
206         <tr>
207           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
208           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
209         </tr>
210 | if $form->{selectdepartment};
211
212   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
213                                    "all" => 1 });
214
215   my %project_labels = ();
216   my @project_values = ("");
217   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
218     push(@project_values, $item->{"id"});
219     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
220   }
221
222   my $projectnumber =
223     NTI($cgi->popup_menu('-name' => "project_id",
224                          '-values' => \@project_values,
225                          '-labels' => \%project_labels));
226
227   # use JavaScript Calendar or not
228   $form->{jsscript} = $jscalendar;
229   $jsscript = "";
230   if ($form->{jsscript}) {
231
232     # with JavaScript Calendar
233     $button1 = qq|
234        <td><input name=datefrom id=datefrom size=11 title="$myconfig{dateformat}">
235        <input type=button name=datefrom id="trigger1" value=|
236       . $locale->text('button') . qq|></td>  
237        |;
238     $button2 = qq|
239        <td><input name=dateto id=dateto size=11 title="$myconfig{dateformat}">
240        <input type=button name=dateto id="trigger2" value=|
241       . $locale->text('button') . qq|></td>
242      |;
243
244     #write Trigger
245     $jsscript =
246       Form->write_trigger(\%myconfig, "2", "datefrom", "BR", "trigger1",
247                           "dateto", "BL", "trigger2");
248   } else {
249
250     # without JavaScript Calendar
251     $button1 =
252       qq|<td><input name=datefrom id=datefrom size=11 title="$myconfig{dateformat}"></td>|;
253     $button2 =
254       qq|<td><input name=dateto id=dateto size=11 title="$myconfig{dateformat}"></td>|;
255   }
256
257   $form->header;
258
259   print qq|
260 <body>
261
262 <form method=post action=$form->{script}>
263
264 <input type=hidden name=sort value=transdate>
265
266 <table width=100%>
267   <tr>
268     <th class=listtop>$form->{title}</th>
269   </tr>
270   <tr height="5"></tr>
271   <tr>
272     <td>
273       <table>
274         <tr>
275           <th align=right>| . $locale->text('Reference') . qq|</th>
276           <td><input name=reference size=20></td>
277           <th align=right>| . $locale->text('Source') . qq|</th>
278           <td><input name=source size=20></td>
279         </tr>
280         $department
281         <tr>
282           <th align=right>| . $locale->text('Description') . qq|</th>
283           <td colspan=3><input name=description size=40></td>
284         </tr>
285         <tr>
286           <th align=right>| . $locale->text('Notes') . qq|</th>
287           <td colspan=3><input name=notes size=40></td>
288         </tr>
289         <tr>
290           <th align=right>| . $locale->text('Project Number') . qq|</th>
291           <td colspan=3>$projectnumber</td>
292         </tr>
293         <tr>
294           <th align=right>| . $locale->text('From') . qq|</th>
295           $button1
296           <th align=right>| . $locale->text('To (time)') . qq|</th>
297           $button2
298         </tr>
299         <tr>
300           <th align=right>| . $locale->text('Include in Report') . qq|</th>
301           <td colspan=3>
302             <table>
303               <tr>
304                 <td>
305                   <input name="category" class=radio type=radio value=X checked>&nbsp;|
306     . $locale->text('All') . qq|
307                   <input name="category" class=radio type=radio value=A>&nbsp;|
308     . $locale->text('Asset') . qq|
309                   <input name="category" class=radio type=radio value=L>&nbsp;|
310     . $locale->text('Liability') . qq|
311                   <input name="category" class=radio type=radio value=I>&nbsp;|
312     . $locale->text('Revenue') . qq|
313                   <input name="category" class=radio type=radio value=E>&nbsp;|
314     . $locale->text('Expense') . qq|
315                 </td>
316               </tr>
317               <tr>
318                 <table>
319                   <tr>
320                     <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
321                     <td>| . $locale->text('ID') . qq|</td>
322                     <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
323                     <td>| . $locale->text('Date') . qq|</td>
324                     <td align=right><input name="l_reference" class=checkbox type=checkbox value=Y checked></td>
325                     <td>| . $locale->text('Reference') . qq|</td>
326                     <td align=right><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
327                     <td>| . $locale->text('Description') . qq|</td>
328                     <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
329                     <td>| . $locale->text('Notes') . qq|</td>
330                   </tr>
331                   <tr>
332                     <td align=right><input name="l_debit" class=checkbox type=checkbox value=Y checked></td>
333                     <td>| . $locale->text('Debit') . qq|</td>
334                     <td align=right><input name="l_credit" class=checkbox type=checkbox value=Y checked></td>
335                     <td>| . $locale->text('Credit') . qq|</td>
336                     <td align=right><input name="l_source" class=checkbox type=checkbox value=Y checked></td>
337                     <td>| . $locale->text('Source') . qq|</td>
338                     <td align=right><input name="l_accno" class=checkbox type=checkbox value=Y checked></td>
339                     <td>| . $locale->text('Account') . qq|</td>
340                     <td align=right><input name="l_gifi_accno" class=checkbox type=checkbox value=Y></td>
341                     <td>| . $locale->text('GIFI') . qq|</td>
342                   </tr>
343                   <tr>
344                     <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
345                     <td>| . $locale->text('Subtotal') . qq|</td>
346                     <td align=right><input name="l_projectnumbers" class=checkbox type=checkbox value=Y></td>
347                     <td>| . $locale->text('Project Number') . qq|</td>
348                   </tr>
349                 </table>
350               </tr>
351             </table>
352         </tr>
353       </table>
354     </td>
355   </tr>
356   <tr>
357     <td><hr size=3 noshade></td>
358   </tr>
359 </table>
360
361 $jsscript
362
363 <input type=hidden name=nextsub value=generate_report>
364
365 <input type=hidden name=path value=$form->{path}>
366 <input type=hidden name=login value=$form->{login}>
367 <input type=hidden name=password value=$form->{password}>
368
369 <br>
370 <input class=submit type=submit name=action value="|
371     . $locale->text('Continue') . qq|">
372 </form>
373
374 </body>
375 </html>
376 |;
377   $lxdebug->leave_sub();
378 }
379
380 sub generate_report {
381   $lxdebug->enter_sub();
382
383   $form->{sort} = "transdate" unless $form->{sort};
384
385   GL->all_transactions(\%myconfig, \%$form);
386
387   $callback =
388     "$form->{script}?action=generate_report&path=$form->{path}&login=$form->{login}&password=$form->{password}";
389
390   $href = $callback;
391
392   %acctype = ('A' => $locale->text('Asset'),
393               'C' => $locale->text('Contra'),
394               'L' => $locale->text('Liability'),
395               'Q' => $locale->text('Equity'),
396               'I' => $locale->text('Revenue'),
397               'E' => $locale->text('Expense'),);
398
399   $form->{title} = $locale->text('General Ledger');
400
401   $ml = ($form->{ml} =~ /(A|E|Q)/) ? -1 : 1;
402
403   unless ($form->{category} eq 'X') {
404     $form->{title} .= " : " . $locale->text($acctype{ $form->{category} });
405   }
406   if ($form->{accno}) {
407     $href .= "&accno=" . $form->escape($form->{accno});
408     $callback .= "&accno=" . $form->escape($form->{accno}, 1);
409     $option =
410       $locale->text('Account')
411       . " : $form->{accno} $form->{account_description}";
412   }
413   if ($form->{gifi_accno}) {
414     $href     .= "&gifi_accno=" . $form->escape($form->{gifi_accno});
415     $callback .= "&gifi_accno=" . $form->escape($form->{gifi_accno}, 1);
416     $option   .= "\n<br>" if $option;
417     $option   .=
418       $locale->text('GIFI')
419       . " : $form->{gifi_accno} $form->{gifi_account_description}";
420   }
421   if ($form->{source}) {
422     $href     .= "&source=" . $form->escape($form->{source});
423     $callback .= "&source=" . $form->escape($form->{source}, 1);
424     $option   .= "\n<br>" if $option;
425     $option   .= $locale->text('Source') . " : $form->{source}";
426   }
427   if ($form->{reference}) {
428     $href     .= "&reference=" . $form->escape($form->{reference});
429     $callback .= "&reference=" . $form->escape($form->{reference}, 1);
430     $option   .= "\n<br>" if $option;
431     $option   .= $locale->text('Reference') . " : $form->{reference}";
432   }
433   if ($form->{department}) {
434     $href .= "&department=" . $form->escape($form->{department});
435     $callback .= "&department=" . $form->escape($form->{department}, 1);
436     ($department) = split /--/, $form->{department};
437     $option .= "\n<br>" if $option;
438     $option .= $locale->text('Department') . " : $department";
439   }
440
441   if ($form->{description}) {
442     $href     .= "&description=" . $form->escape($form->{description});
443     $callback .= "&description=" . $form->escape($form->{description}, 1);
444     $option   .= "\n<br>" if $option;
445     $option   .= $locale->text('Description') . " : $form->{description}";
446   }
447   if ($form->{notes}) {
448     $href     .= "&notes=" . $form->escape($form->{notes});
449     $callback .= "&notes=" . $form->escape($form->{notes}, 1);
450     $option   .= "\n<br>" if $option;
451     $option   .= $locale->text('Notes') . " : $form->{notes}";
452   }
453  if ($form->{project_id}) {
454     $href     .= "&project_id=" . $form->escape($form->{project_id});
455     $callback .= "&project_id=" . $form->escape($form->{project_id});
456   }
457
458   if ($form->{datefrom}) {
459     $href     .= "&datefrom=$form->{datefrom}";
460     $callback .= "&datefrom=$form->{datefrom}";
461     $option   .= "\n<br>" if $option;
462     $option   .=
463         $locale->text('From') . " "
464       . $locale->date(\%myconfig, $form->{datefrom}, 1);
465   }
466   if ($form->{dateto}) {
467     $href     .= "&dateto=$form->{dateto}";
468     $callback .= "&dateto=$form->{dateto}";
469     if ($form->{datefrom}) {
470       $option .= " ";
471     } else {
472       $option .= "\n<br>" if $option;
473     }
474     $option .=
475         $locale->text('Bis') . " "
476       . $locale->date(\%myconfig, $form->{dateto}, 1);
477   }
478
479   @columns = $form->sort_columns( qw(
480        transdate       id                reference         description  
481        notes           source            debit             debit_accno  
482        credit          credit_accno      debit_tax         debit_tax_accno
483        credit_tax      credit_tax_accno  accno             gifi_accno
484        projectnumbers  
485        )
486   );
487
488   if ($form->{accno} || $form->{gifi_accno}) {
489     @columns = grep !/(accno|gifi_accno)/, @columns;
490     push @columns, "balance";
491     $form->{l_balance} = "Y";
492
493   }
494
495   $form->{l_credit_accno}     = "Y";
496   $form->{l_debit_accno}      = "Y";
497   $form->{l_credit_tax}       = "Y";
498   $form->{l_debit_tax}        = "Y";
499   $form->{l_credit_tax_accno} = "Y";
500   $form->{l_debit_tax_accno}  = "Y";
501   $form->{l_accno}            = "N";
502   foreach $item (@columns) {
503     if ($form->{"l_$item"} eq "Y") {
504       push @column_index, $item;
505
506       # add column to href and callback
507       $callback .= "&l_$item=Y";
508       $href     .= "&l_$item=Y";
509     }
510   }
511
512   if ($form->{l_subtotal} eq 'Y') {
513     $callback .= "&l_subtotal=Y";
514     $href     .= "&l_subtotal=Y";
515   }
516
517   $callback .= "&category=$form->{category}";
518   $href     .= "&category=$form->{category}";
519
520   $column_header{id} =
521       "<th><a class=listheading href=$href&sort=id>"
522     . $locale->text('ID')
523     . "</a></th>";
524   $column_header{transdate} =
525       "<th><a class=listheading href=$href&sort=transdate>"
526     . $locale->text('Date')
527     . "</a></th>";
528   $column_header{reference} =
529       "<th><a class=listheading href=$href&sort=reference>"
530     . $locale->text('Reference')
531     . "</a></th>";
532   $column_header{source} =
533       "<th><a class=listheading href=$href&sort=source>"
534     . $locale->text('Source')
535     . "</a></th>";
536   $column_header{description} =
537       "<th><a class=listheading href=$href&sort=description>"
538     . $locale->text('Description')
539     . "</a></th>";
540   $column_header{notes} =
541     "<th class=listheading>" . $locale->text('Notes') . "</th>";
542   $column_header{debit} =
543     "<th class=listheading>" . $locale->text('Debit') . "</th>";
544   $column_header{debit_accno} =
545       "<th><a class=listheading href=$href&sort=accno>"
546     . $locale->text('Debit Account')
547     . "</a></th>";
548   $column_header{credit} =
549     "<th class=listheading>" . $locale->text('Credit') . "</th>";
550   $column_header{credit_accno} =
551       "<th><a class=listheading href=$href&sort=accno>"
552     . $locale->text('Credit Account')
553     . "</a></th>";
554   $column_header{debit_tax} =
555       "<th><a class=listheading href=$href&sort=accno>"
556     . $locale->text('Debit Tax')
557     . "</a></th>";
558   $column_header{debit_tax_accno} =
559       "<th><a class=listheading href=$href&sort=accno>"
560     . $locale->text('Debit Tax Account')
561     . "</a></th>";
562   $column_header{credit_tax} =
563       "<th><a class=listheading href=$href&sort=accno>"
564     . $locale->text('Credit Tax')
565     . "</a></th>";
566   $column_header{credit_tax_accno} =
567       "<th><a class=listheading href=$href&sort=accno>"
568     . $locale->text('Credit Tax Account')
569     . "</a></th>";
570   $column_header{gifi_accno} =
571       "<th><a class=listheading href=$href&sort=gifi_accno>"
572     . $locale->text('GIFI')
573     . "</a></th>";
574   $column_header{balance} = "<th>" . $locale->text('Balance') . "</th>";
575   $column_header{projectnumbers} =
576       "<th class=listheading>"  . $locale->text('Project Numbers') . "</th>";
577
578   $form->{landscape} = 1;
579
580   $form->header;
581
582   print qq|
583 <body>
584
585 <table width=100%>
586   <tr>
587     <th class=listtop>$form->{title}</th>
588   </tr>
589   <tr height="5"></tr>
590   <tr>
591     <td>$option</td>
592   </tr>
593   <tr>
594     <td>
595       <table width=100%>
596        <thead>
597         <tr class=listheading>
598 |;
599
600   map { print "$column_header{$_}\n" } @column_index;
601
602   print "
603         </tr>
604         </thead>
605         </tfoot>
606         <tbody>
607 ";
608
609   # add sort to callback
610   $form->{callback} = "$callback&sort=$form->{sort}";
611   $callback = $form->escape($form->{callback});
612
613   # initial item for subtotals
614   if (@{ $form->{GL} }) {
615     $sameitem = $form->{GL}->[0]->{ $form->{sort} };
616   }
617
618   if (($form->{accno} || $form->{gifi_accno}) && $form->{balance}) {
619
620     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
621     $column_data{balance} =
622         "<td align=right>"
623       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
624       . "</td>";
625
626     $i++;
627     $i %= 2;
628     print qq|
629         <tr class=listrow$i>
630 |;
631     map { print "$column_data{$_}\n" } @column_index;
632
633     print qq|
634         </tr>
635 |;
636   }
637   $form->{balance} *= $ml;
638   foreach $ref (@{ $form->{GL} }) {
639     $form->{balance} *= $ml;
640
641     # if item ne sort print subtotal
642     if ($form->{l_subtotal} eq 'Y') {
643       if ($sameitem ne $ref->{ $form->{sort} }) {
644         &gl_subtotal;
645       }
646     }
647
648     #foreach $key (sort keys(%{ $ref->{amount} })) {
649     #  $form->{balance} += $ref->{amount}{$key};
650     #}
651
652     $debit = "";
653     foreach $key (sort keys(%{ $ref->{debit} })) {
654       $subtotaldebit += $ref->{debit}{$key};
655       $totaldebit    += $ref->{debit}{$key};
656       if ($key == 0) {
657         $debit = $form->format_amount(\%myconfig, $ref->{debit}{$key}, 2, 0);
658       } else {
659         $debit .=
660           "<br>" . $form->format_amount(\%myconfig, $ref->{debit}{$key}, 2, 0);
661       }
662       $form->{balance} = abs($form->{balance}) - abs($ref->{debit}{$key});
663     }
664
665     $credit = "";
666     foreach $key (sort keys(%{ $ref->{credit} })) {
667       $subtotalcredit += $ref->{credit}{$key};
668       $totalcredit    += $ref->{credit}{$key};
669       if ($key == 0) {
670         $credit = $form->format_amount(\%myconfig, $ref->{credit}{$key}, 2, 0);
671       } else {
672         $credit .= "<br>"
673           . $form->format_amount(\%myconfig, $ref->{credit}{$key}, 2, 0);
674       }
675       $form->{balance} = abs($form->{balance}) - abs($ref->{credit}{$key});
676     }
677
678     $debittax = "";
679     foreach $key (sort keys(%{ $ref->{debit_tax} })) {
680       $subtotaldebittax += $ref->{debit_tax}{$key};
681       $totaldebittax    += $ref->{debit_tax}{$key};
682       if ($key == 0) {
683         $debittax =
684           $form->format_amount(\%myconfig, $ref->{debit_tax}{$key}, 2, 0);
685       } else {
686         $debittax .= "<br>"
687           . $form->format_amount(\%myconfig, $ref->{debit_tax}{$key}, 2, 0);
688       }
689       $form->{balance} = abs($form->{balance}) - abs($ref->{debit_tax}{$key});
690     }
691
692     $credittax = "";
693     foreach $key (sort keys(%{ $ref->{credit_tax} })) {
694       $subtotalcredittax += $ref->{credit_tax}{$key};
695       $totalcredittax    += $ref->{credit_tax}{$key};
696       if ($key == 0) {
697         $credittax =
698           $form->format_amount(\%myconfig, $ref->{credit_tax}{$key}, 2, 0);
699       } else {
700         $credittax .= "<br>"
701           . $form->format_amount(\%myconfig, $ref->{credit_tax}{$key}, 2, 0);
702       }
703       $form->{balance} = abs($form->{balance}) - abs($ref->{credit_tax}{$key});
704     }
705
706     $debitaccno  = "";
707     $debittaxkey = "";
708     $taxaccno    = "";
709     foreach $key (sort keys(%{ $ref->{debit_accno} })) {
710       if ($key == 0) {
711         $debitaccno =
712           "<a href=$href&accno=$ref->{debit_accno}{$key}&callback=$callback>$ref->{debit_accno}{$key}</a>";
713       } else {
714         $debitaccno .=
715           "<br><a href=$href&accno=$ref->{debit_accno}{$key}&callback=$callback>$ref->{debit_accno}{$key}</a>";
716       }
717
718       #       if ($ref->{debit_taxkey}{$key} eq $debittaxkey) {
719       #         $ref->{debit_tax_accno}{$key} = $taxaccno;
720       #       }
721       $taxaccno    = $ref->{debit_tax_accno}{$key};
722       $debittaxkey = $ref->{debit_taxkey}{$key};
723     }
724
725     $creditaccno  = "";
726     $credittaxkey = "";
727     $taxaccno     = "";
728     foreach $key (sort keys(%{ $ref->{credit_accno} })) {
729       if ($key == 0) {
730         $creditaccno =
731           "<a href=$href&accno=$ref->{credit_accno}{$key}&callback=$callback>$ref->{credit_accno}{$key}</a>";
732       } else {
733         $creditaccno .=
734           "<br><a href=$href&accno=$ref->{credit_accno}{$key}&callback=$callback>$ref->{credit_accno}{$key}</a>";
735       }
736
737       #       if ($ref->{credit_taxkey}{$key} eq $credittaxkey) {
738       #         $ref->{credit_tax_accno}{$key} = $taxaccno;
739       #       }
740       $taxaccno     = $ref->{credit_tax_accno}{$key};
741       $credittaxkey = $ref->{credit_taxkey}{$key};
742     }
743
744     $debittaxaccno = "";
745     foreach $key (sort keys(%{ $ref->{debit_tax_accno} })) {
746       if ($key == 0) {
747         $debittaxaccno =
748           "<a href=$href&accno=$ref->{debit_tax_accno}{$key}&callback=$callback>$ref->{debit_tax_accno}{$key}</a>";
749       } else {
750         $debittaxaccno .=
751           "<br><a href=$href&accno=$ref->{debit_tax_accno}{$key}&callback=$callback>$ref->{debit_tax_accno}{$key}</a>";
752       }
753     }
754
755     $credittaxaccno = "";
756     foreach $key (sort keys(%{ $ref->{credit_tax_accno} })) {
757       if ($key == 0) {
758         $credittaxaccno =
759           "<a href=$href&accno=$ref->{credit_tax_accno}{$key}&callback=$callback>$ref->{credit_tax_accno}{$key}</a>";
760       } else {
761         $credittaxaccno .=
762           "<br><a href=$href&accno=$ref->{credit_tax_accno}{$key}&callback=$callback>$ref->{credit_tax_accno}{$key}</a>";
763       }
764     }
765
766     $transdate = "";
767     foreach $key (sort keys(%{ $ref->{transdate} })) {
768       if ($key == 0) {
769         $transdate = "$ref->{transdate}{$key}";
770       } else {
771         $transdate .= "<br>$ref->{transdate}{$key}";
772       }
773     }
774
775     #    $ref->{debit} = $form->format_amount(\%myconfig, $ref->{debit}, 2, "&nbsp;");
776     #    $ref->{credit} = $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;");
777
778     $column_data{id}        = "<td align=right>&nbsp;$ref->{id}&nbsp;</td>";
779     $column_data{transdate}    = "<td align=center>$transdate</td>";
780     $column_data{reference} =
781       "<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>";
782     $column_data{description}  = "<td align=center>$ref->{description}&nbsp;</td>";
783     $column_data{source}       = "<td align=center>$ref->{source}&nbsp;</td>";
784     $column_data{notes}        = "<td align=center>$ref->{notes}&nbsp;</td>";
785     $column_data{debit}        = "<td align=right>$debit</td>";
786     $column_data{debit_accno}  = "<td align=center>$debitaccno</td>";
787     $column_data{credit}       = "<td align=right>$credit</td>";
788     $column_data{credit_accno} = "<td align=center>$creditaccno</td>";
789     $column_data{debit_tax}    =
790       ($ref->{debit_tax_accno} ne "")
791       ? "<td align=right>$debittax</td>"
792       : "<td></td>";
793     $column_data{debit_tax_accno} = "<td align=center>$debittaxaccno</td>";
794     $column_data{gifi_accno}      =
795       "<td><a href=$href&gifi_accno=$ref->{gifi_accno}&callback=$callback>$ref->{gifi_accno}</a>&nbsp;</td>";
796     $column_data{credit_tax} =
797       ($ref->{credit_tax_accno} ne "")
798       ? "<td align=right>$credittax</td>"
799       : "<td></td>";
800     $column_data{credit_tax_accno} = "<td align=center>$credittaxaccno</td>";
801     $column_data{gifi_accno}       =
802       "<td><a href=$href&gifi_accno=$ref->{gifi_accno}&callback=$callback>$ref->{gifi_accno}</a>&nbsp;</td>";
803     $column_data{balance} =
804       "<td align=right>"
805       . $form->format_amount(\%myconfig, $form->{balance}, 2, 0) . "</td>";
806     $column_data{projectnumbers} =
807       "<td>" . join(", ", sort({ lc($a) cmp lc($b) } keys(%{ $ref->{projectnumbers} }))) . "</td>";
808
809     $i++;
810     $i %= 2;
811     print "
812         <tr class=listrow$i>";
813     map { print "$column_data{$_}\n" } @column_index;
814     print "</tr>";
815
816   }
817
818   &gl_subtotal if ($form->{l_subtotal} eq 'Y');
819
820   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
821
822   my $balanced_ledger = $totaldebit 
823                       + $totaldebittax 
824                       - $totalcredit 
825                       - $totalcredittax;
826                     # = 0 for balanced ledger
827                     
828   $column_data{debit} =
829     "<th align=right class=listtotal>"
830     . $form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;") . "</th>";
831   $column_data{credit} =
832     "<th align=right class=listtotal>"
833     . $form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;") . "</th>";
834   $column_data{debit_tax} =
835     "<th align=right class=listtotal>"
836     . $form->format_amount(\%myconfig, $totaldebittax, 2, "&nbsp;") . "</th>";
837   $column_data{credit_tax} =
838     "<th align=right class=listtotal>"
839     . $form->format_amount(\%myconfig, $totalcredittax, 2, "&nbsp;") . "</th>";
840   $column_data{balance} =
841     "<th align=right class=listtotal>"
842     . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0) . "</th>";
843
844   print qq|
845         <tr class=listtotal>
846 |;
847
848   map { print "$column_data{$_}\n" } @column_index;
849
850   print qq|
851         </tr>
852         <tr>|;
853
854
855   if ( abs($balanced_ledger) >  0.001 ) {
856
857     print qq|<td colspan="4" style="background-color:#FFA0A0" >|
858         . $locale->text('Unbalanced Ledger') 
859         . ": " 
860         . $form->format_amount(\%myconfig, $balanced_ledger, 3, "&nbsp;")
861
862   } elsif ( abs($balanced_ledger) <= 0.001 ) {
863
864     print qq|<td colspan="3">|
865           . $locale->text('Balanced Ledger') 
866
867   }
868
869   
870   print qq|
871          </td>
872         </tr>
873         </tbody>
874       </table>
875     </td>
876   </tr>
877   <tr>
878     <td><hr size=3 noshade></td>
879   </tr>
880 </table>
881
882 <br>
883
884 <form method=post action=$form->{script}>
885
886 <input name=callback type=hidden value="$form->{callback}">
887
888 <input type=hidden name=path value=$form->{path}>
889 <input type=hidden name=login value=$form->{login}>
890 <input type=hidden name=password value=$form->{password}>
891
892 <input class=submit type=submit name=action value="|
893     . $locale->text('GL Transaction') . qq|">
894 <input class=submit type=submit name=action value="|
895     . $locale->text('AR Transaction') . qq|">
896 <input class=submit type=submit name=action value="|
897     . $locale->text('AP Transaction') . qq|">
898 <input class=submit type=submit name=action value="|
899     . $locale->text('Sales Invoice') . qq|">
900 <input class=submit type=submit name=action value="|
901     . $locale->text('Vendor Invoice') . qq|">
902
903 </form>
904
905 </body>
906 </html>
907 |;
908   $lxdebug->leave_sub();
909
910 }
911
912 sub gl_subtotal {
913   $lxdebug->enter_sub();
914
915   $subtotaldebit =
916     $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
917   $subtotalcredit =
918     $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
919
920   map { $column_data{$_} = "<td>&nbsp;</td>" }
921     qw(transdate id reference source description accno);
922   $column_data{debit}  = "<th align=right>$subtotaldebit</td>";
923   $column_data{credit} = "<th align=right>$subtotalcredit</td>";
924
925   print "<tr class=listsubtotal>";
926   map { print "$column_data{$_}\n" } @column_index;
927   print "</tr>";
928
929   $subtotaldebit  = 0;
930   $subtotalcredit = 0;
931
932   $sameitem = $ref->{ $form->{sort} };
933   $lxdebug->leave_sub();
934
935 }
936
937 sub update {
938   $lxdebug->enter_sub();
939
940   $form->{oldtransdate} = $form->{transdate};
941
942   my @a           = ();
943   my $count       = 0;
944   my $debittax    = 0;
945   my $credittax   = 0;
946   my $debitcount  = 0;
947   my $creditcount = 0;
948   $debitlock  = 0;
949   $creditlock = 0;
950
951   my @flds =
952     qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
953
954   for my $i (1 .. $form->{rowcount}) {
955
956     unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
957       for (qw(debit credit tax)) {
958         $form->{"${_}_$i"} =
959           $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
960       }
961
962       push @a, {};
963       $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
964       if ($debitcredit) {
965         $debitcount++;
966       } else {
967         $creditcount++;
968       }
969
970       if (($debitcount >= 2) && ($creditcount == 2)) {
971         $form->{"credit_$i"} = 0;
972         $form->{"tax_$i"}    = 0;
973         $creditcount--;
974         $creditlock = 1;
975       }
976       if (($creditcount >= 2) && ($debitcount == 2)) {
977         $form->{"debit_$i"} = 0;
978         $form->{"tax_$i"}   = 0;
979         $debitcount--;
980         $debitlock = 1;
981       }
982       if (($creditcount == 1) && ($debitcount == 2)) {
983         $creditlock = 1;
984       }
985       if (($creditcount == 2) && ($debitcount == 1)) {
986         $debitlock = 1;
987       }
988       if ($debitcredit && $credittax) {
989         $form->{"taxchart_$i"} = "0--0.00";
990       }
991       if (!$debitcredit && $debittax) {
992         $form->{"taxchart_$i"} = "0--0.00";
993       }
994       $amount =
995         ($form->{"debit_$i"} == 0)
996         ? $form->{"credit_$i"}
997         : $form->{"debit_$i"};
998       $j = $#a;
999       if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1000         $form->{"taxchart_$i"} = "0--0.00";
1001         $form->{"tax_$i"}      = 0;
1002       }
1003       if (!$form->{"korrektur_$i"}) {
1004         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1005         if ($taxkey > 1) {
1006           if ($debitcredit) {
1007             $debittax = 1;
1008           } else {
1009             $credittax = 1;
1010           }
1011           if ($form->{taxincluded}) {
1012             $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
1013           } else {
1014             $form->{"tax_$i"} = $amount * $rate;
1015           }
1016         } else {
1017           $form->{"tax_$i"} = 0;
1018         }
1019       }
1020
1021       for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1022       $count++;
1023     }
1024   }
1025
1026   for $i (1 .. $count) {
1027     $j = $i - 1;
1028     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1029   }
1030
1031   for $i ($count + 1 .. $form->{rowcount}) {
1032     for (@flds) { delete $form->{"${_}_$i"} }
1033   }
1034
1035   $form->{rowcount} = $count + 1;
1036
1037   &display_form;
1038   $lxdebug->leave_sub();
1039
1040 }
1041
1042 sub display_form {
1043   my ($init) = @_;
1044   $lxdebug->enter_sub();
1045
1046   &form_header($init);
1047
1048   #   for $i (1 .. $form->{rowcount}) {
1049   #     $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
1050   #     $form->{totalcredit} += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
1051   #
1052   #     &form_row($i);
1053   #   }
1054   &display_rows($init);
1055   &form_footer;
1056   $lxdebug->leave_sub();
1057
1058 }
1059
1060 sub display_rows {
1061   my ($init) = @_;
1062   $lxdebug->enter_sub();
1063
1064   $form->{totaldebit}  = 0;
1065   $form->{totalcredit} = 0;
1066
1067   my @old_project_ids = ();
1068   map({ push(@old_project_ids, $form->{"project_id_$_"})
1069           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
1070
1071   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1072                                    "all" => 0,
1073                                    "old_id" => \@old_project_ids },
1074                    "charts" => { "key" => "ALL_CHARTS",
1075                                  "transdate" => $form->{transdate} },
1076                    "taxcharts" => "ALL_TAXCHARTS");
1077
1078   my %project_labels = ();
1079   my @project_values = ("");
1080   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1081     push(@project_values, $item->{"id"});
1082     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
1083   }
1084
1085   my %chart_labels = ();
1086   my @chart_values = ();
1087   my $taxchart_init;
1088   foreach my $item (@{ $form->{ALL_CHARTS} }) {
1089     my $key = Q($item->{accno}) . "--" . Q($item->{tax_id});
1090     $taxchart_init = $item->{taxkey_id} unless (@chart_values);
1091     push(@chart_values, $key);
1092     $chart_labels{$key} = H($item->{accno}) . "--" . H($item->{description});
1093   }
1094
1095   my %taxchart_labels = ();
1096   my @taxchart_values = ();
1097   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
1098     my $key = Q($item->{id}) . "--" . Q($item->{rate});
1099     $taxchart_init = $key if ($taxchart_init eq $item->{taxkey});
1100     push(@taxchart_values, $key);
1101     $taxchart_labels{$key} = H($item->{taxdescription}) . " " .
1102       H($item->{rate} * 100) . ' %';
1103   }
1104
1105   for $i (1 .. $form->{rowcount}) {
1106
1107     $source = qq|
1108     <td><input name="source_$i" value="$form->{"source_$i"}" size="16" tabindex=|
1109       . ($i + 11 + (($i - 1) * 8)) . qq|></td>|;
1110     $memo = qq|
1111     <td><input name="memo_$i" value="$form->{"memo_$i"}" size="16" tabindex=|
1112       . ($i + 12 + (($i - 1) * 8)) . qq|></td>|;
1113
1114     $accno = qq|<td>| .
1115       $cgi->popup_menu('-name' => "accno_$i",
1116                        '-onChange' => "setTaxkey(this, $i)",
1117                        '-style' => 'width:200px',
1118                        '-tabindex' => ($i + 5 + (($i - 1) * 8)),
1119                        '-values' => \@chart_values,
1120                        '-labels' => \%chart_labels,
1121                        '-default' => $init ? '' : $form->{"accno_$i"})
1122       . qq|</td>|;
1123     $tax = qq|<td>| .
1124       $cgi->popup_menu('-name' => "taxchart_$i",
1125                        '-style' => 'width:200px',
1126                        '-tabindex' => ($i + 10 + (($i - 1) * 8)),
1127                        '-values' => \@taxchart_values,
1128                        '-labels' => \%taxchart_labels,
1129                        '-default' => $init ? $taxchart_init : $form->{"taxchart_$i"})
1130       . qq|</td>|;
1131
1132     if ($init) {
1133       $korrektur =
1134         qq|<td><input type="checkbox" name="korrektur_$i" value="1" tabindex=|
1135         . ($i + 9 + (($i - 1) * 8))
1136         . qq|></td>|;
1137       if ($form->{transfer}) {
1138         $fx_transaction = qq|
1139         <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
1140     |;
1141       }
1142
1143     } else {
1144       if ($form->{"debit_$i"} != 0) {
1145         $form->{totaldebit} += $form->{"debit_$i"};
1146         if (!$form->{taxincluded}) {
1147           $form->{totaldebit} += $form->{"tax_$i"};
1148         }
1149       } else {
1150         $form->{totalcredit} += $form->{"credit_$i"};
1151         if (!$form->{taxincluded}) {
1152           $form->{totalcredit} += $form->{"tax_$i"};
1153         }
1154       }
1155
1156       for (qw(debit credit tax)) {
1157         $form->{"${_}_$i"} =
1158           ($form->{"${_}_$i"})
1159           ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
1160           : "";
1161       }
1162
1163       if ($i < $form->{rowcount}) {
1164         if ($form->{transfer}) {
1165           $checked = ($form->{"fx_transaction_$i"}) ? "1" : "";
1166           $x = ($checked) ? "x" : "";
1167           $fx_transaction = qq|
1168       <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
1169     |;
1170         }
1171         $checked = ($form->{"korrektur_$i"}) ? "checked" : "";
1172         $korrektur =
1173           qq|<td><input type="checkbox" name="korrektur_$i" value="1" $checked tabindex=|
1174           . ($i + 9 + (($i - 1) * 8))
1175           . qq|></td>|;
1176         $form->hide_form("accno_$i");
1177
1178       } else {
1179         $korrektur =
1180           qq|<td><input type="checkbox" name="korrektur_$i" value="1" tabindex=|
1181           . ($i + 9 + (($i - 1) * 8))
1182           . qq|></td>|;
1183         if ($form->{transfer}) {
1184           $fx_transaction = qq|
1185       <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
1186     |;
1187         }
1188       }
1189     }
1190     my $debitreadonly  = "";
1191     my $creditreadonly = "";
1192     if ($i == $form->{rowcount}) {
1193       if ($debitlock) {
1194         $debitreadonly = "readonly";
1195       } elsif ($creditlock) {
1196         $creditreadonly = "readonly";
1197       }
1198     }
1199
1200     my $projectnumber =
1201       NTI($cgi->popup_menu('-name' => "project_id_$i",
1202                            '-values' => \@project_values,
1203                            '-labels' => \%project_labels,
1204                            '-default' => $form->{"project_id_$i"} ));
1205
1206     print qq|<tr valign=top>
1207     $accno
1208     $fx_transaction
1209     <td><input name="debit_$i" size=8 value="$form->{"debit_$i"}" accesskey=$i tabindex=|
1210       . ($i + 6 + (($i - 1) * 8)) . qq| $debitreadonly></td>
1211     <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" tabindex=|
1212       . ($i + 7 + (($i - 1) * 8)) . qq| $creditreadonly></td>
1213     <td><input name="tax_$i" size=6 value="$form->{"tax_$i"}" tabindex=|
1214       . ($i + 8 + (($i - 1) * 8)) . qq|></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}" tabindex="3" $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}" tabindex="2" $readonly>
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}" tabindex="2" $readonly></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=left>| . $locale->text('Reference') . qq|</th>
1340           <td><input name=reference size=20 value="$form->{reference}" tabindex="1" $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></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=left 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 tabindex="5" $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=11  value=$form->{employee} $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 tabindex="5" $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->{addition} = "DELETED";
1548             $form->save_history($form->dbconnect(\%myconfig));
1549       }
1550     # /saving the history 
1551     $form->redirect($locale->text('Transaction deleted!'))
1552   }
1553   $form->error($locale->text('Cannot delete transaction!'));
1554   $lxdebug->leave_sub();
1555
1556 }
1557
1558 sub post {
1559   $lxdebug->enter_sub();
1560
1561   # check if there is something in reference and date
1562   $form->isblank("reference",   $locale->text('Reference missing!'));
1563   $form->isblank("transdate",   $locale->text('Transaction Date missing!'));
1564   $form->isblank("description", $locale->text('Description missing!'));
1565
1566   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1567   $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
1568
1569   my @a           = ();
1570   my $count       = 0;
1571   my $debittax    = 0;
1572   my $credittax   = 0;
1573   my $debitcount  = 0;
1574   my $creditcount = 0;
1575   $creditlock = 0;
1576   $debitlock  = 0;
1577
1578   my @flds =
1579     qw(accno debit credit projectnumber fx_transaction source memo tax taxchart);
1580   if ($form->{storno}) {
1581     for my $i (1 .. $form->{rowcount}) {
1582       unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
1583         if ($form->{"debit_$i"} ne "") {
1584           $form->{"credit_$i"} = $form->{"debit_$i"};
1585           $form->{"debit_$i"}  = "";
1586         } elsif ($form->{"credit_$i"} ne "") {
1587           $form->{"debit_$i"}  = $form->{"credit_$i"};
1588           $form->{"credit_$i"} = "";
1589         }
1590       }
1591     }
1592   }
1593
1594   for my $i (1 .. $form->{rowcount}) {
1595
1596     unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
1597       for (qw(debit credit tax)) {
1598         $form->{"${_}_$i"} =
1599           $form->parse_amount(\%myconfig, $form->{"${_}_$i"});
1600       }
1601
1602       push @a, {};
1603       $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1";
1604
1605       if ($debitcredit) {
1606         $debitcount++;
1607       } else {
1608         $creditcount++;
1609       }
1610
1611       if (($debitcount >= 2) && ($creditcount == 2)) {
1612         $form->{"credit_$i"} = 0;
1613         $form->{"tax_$i"}    = 0;
1614         $creditcount--;
1615         $creditlock = 1;
1616       }
1617       if (($creditcount >= 2) && ($debitcount == 2)) {
1618         $form->{"debit_$i"} = 0;
1619         $form->{"tax_$i"}   = 0;
1620         $debitcount--;
1621         $debitlock = 1;
1622       }
1623       if (($creditcount == 1) && ($debitcount == 2)) {
1624         $creditlock = 1;
1625       }
1626       if (($creditcount == 2) && ($debitcount == 1)) {
1627         $debitlock = 1;
1628       }
1629       if ($debitcredit && $credittax) {
1630         $form->{"taxchart_$i"} = "0--0.00";
1631       }
1632       if (!$debitcredit && $debittax) {
1633         $form->{"taxchart_$i"} = "0--0.00";
1634       }
1635       $amount =
1636         ($form->{"debit_$i"} == 0)
1637         ? $form->{"credit_$i"}
1638         : $form->{"debit_$i"};
1639       $j = $#a;
1640       if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) {
1641         $form->{"taxchart_$i"} = "0--0.00";
1642         $form->{"tax_$i"}      = 0;
1643       }
1644       if (!$form->{"korrektur_$i"}) {
1645         ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
1646         if ($taxkey > 1) {
1647           if ($debitcredit) {
1648             $debittax = 1;
1649           } else {
1650             $credittax = 1;
1651           }
1652           if ($form->{taxincluded}) {
1653             $form->{"tax_$i"} = $amount / ($rate + 1) * $rate;
1654             if ($debitcredit) {
1655               $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
1656             } else {
1657               $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
1658             }
1659           } else {
1660             $form->{"tax_$i"} = $amount * $rate;
1661           }
1662         } else {
1663           $form->{"tax_$i"} = 0;
1664         }
1665       } elsif ($form->{taxincluded}) {
1666         if ($debitcredit) {
1667           $form->{"debit_$i"} = $form->{"debit_$i"} - $form->{"tax_$i"};
1668         } else {
1669           $form->{"credit_$i"} = $form->{"credit_$i"} - $form->{"tax_$i"};
1670         }
1671       }
1672
1673       for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
1674       $count++;
1675     }
1676   }
1677
1678   for $i (1 .. $count) {
1679     $j = $i - 1;
1680     for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
1681   }
1682
1683   for $i ($count + 1 .. $form->{rowcount}) {
1684     for (@flds) { delete $form->{"${_}_$i"} }
1685   }
1686
1687   for $i (1 .. $form->{rowcount}) {
1688     $dr  = $form->{"debit_$i"};
1689     $cr  = $form->{"credit_$i"};
1690     $tax = $form->{"tax_$i"};
1691     if ($dr && $cr) {
1692       $form->error(
1693         $locale->text(
1694           'Cannot post transaction with a debit and credit entry for the same account!'
1695         ));
1696     }
1697     if ($form->{taxincluded}) {
1698       if ($dr) {
1699         $debit += $dr + $tax;
1700       }
1701       if ($cr) {
1702         $credit += $cr + $tax;
1703       }
1704       $taxtotal += $tax;
1705     } else {
1706       if ($dr) {
1707         $debit += $dr + $tax;
1708       }
1709       if ($cr) {
1710         $credit += $cr + $tax;
1711       }
1712     }
1713   }
1714   if (!$taxtotal) {
1715     $form->{taxincluded} = 0;
1716   }
1717
1718   # this is just for the wise guys
1719   $form->error($locale->text('Cannot post transaction for a closed period!'))
1720     if ($transdate <= $closedto);
1721   if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1722     $form->error($locale->text('Out of balance transaction!'));
1723   }
1724   
1725   if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) {
1726     $form->error($locale->text('Empty transaction!'));
1727   }
1728   
1729   if (($errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) {
1730     $errno *= -1;
1731     $err[1] = $locale->text('Cannot have a value in both Debit and Credit!');
1732     $err[2] = $locale->text('Debit and credit out of balance!');
1733     $err[3] = $locale->text('Cannot post a transaction without a value!');
1734
1735     $form->error($err[$errno]);
1736   }
1737   undef($form->{callback});
1738   # saving the history
1739   if(!exists $form->{addition} && $form->{id} ne "") {
1740         $form->{addition} = "SAVED";
1741         $form->{what_done} = $locale->text("Buchungsnummer") . " = " . $form->{id}; 
1742         $form->save_history($form->dbconnect(\%myconfig));
1743   }
1744   # /saving the history 
1745   $form->redirect("Buchung gespeichert. Buchungsnummer = " . $form->{id});
1746   $lxdebug->leave_sub();
1747
1748 }
1749
1750 sub post_as_new {
1751   $lxdebug->enter_sub();
1752
1753   $form->{id} = 0;
1754   &add;
1755   $lxdebug->leave_sub();
1756
1757 }
1758
1759 sub storno {
1760   $lxdebug->enter_sub();
1761
1762   $form->{id}     = 0;
1763   $form->{storno} = 1;
1764   # saving the history
1765   if(!exists $form->{addition} && $form->{id} ne "") {
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