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