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