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