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