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