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