fbb5cfd3e46ebeb67d6cde08a0b1347dc754825a
[kivitendo-erp.git] / bin / mozilla / am.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 # administration
31 #
32 #======================================================================
33
34 use SL::AM;
35 use SL::CA;
36 use SL::Form;
37 use SL::User;
38 use SL::USTVA;
39 use SL::Iconv;
40 use CGI::Ajax;
41 use CGI;
42
43 use Data::Dumper;
44
45 1;
46
47 require "bin/mozilla/common.pl";
48
49 # end of main
50
51 sub add      { call_sub("add_$form->{type}"); }
52 sub delete   { call_sub("delete_$form->{type}"); }
53 sub save     { call_sub("save_$form->{type}"); }
54 sub edit     { call_sub("edit_$form->{type}"); }
55 sub continue { call_sub($form->{"nextsub"}); }
56
57 sub add_account {
58   $lxdebug->enter_sub();
59
60   $form->{title}     = "Add";
61   $form->{charttype} = "A";
62   AM->get_account(\%myconfig, \%$form);
63
64   $form->{callback} =
65     "$form->{script}?action=list_account&login=$form->{login}&password=$form->{password}"
66     unless $form->{callback};
67
68   &account_header;
69   &form_footer;
70
71   $lxdebug->leave_sub();
72 }
73
74 sub edit_account {
75   $lxdebug->enter_sub();
76
77   $form->{title} = "Edit";
78   AM->get_account(\%myconfig, \%$form);
79
80   foreach my $item (split(/:/, $form->{link})) {
81     $form->{$item} = "checked";
82   }
83
84   &account_header;
85   &form_footer;
86
87   $lxdebug->leave_sub();
88 }
89
90 sub account_header {
91   $lxdebug->enter_sub();
92
93   if ( $form->{action} eq 'edit_account') {
94     $form->{account_exists} = '1';
95   } 
96   
97   $form->{title} = $locale->text("$form->{title} Account");
98
99   $form->{"$form->{charttype}_checked"} = "checked";
100   $form->{"$form->{category}_checked"}  = "checked";
101
102   $form->{select_tax} = "";
103
104   my @tax_report_pos = USTVA->report_variables({
105       myconfig   => \%myconfig, 
106       form       => $form, 
107       type       => '', 
108       attribute  => 'position',
109       calc       => '',
110   });
111
112   if (@{ $form->{TAXKEY} }) {
113     foreach my $item (@{ $form->{TAXKEY} }) {
114       $item->{rate} = $item->{rate} * 100 . '%';
115     }
116
117     # Fill in empty row for new Taxkey
118     $newtaxkey_ref = {
119       id             => '',
120       chart_id       => '',
121       accno          => '',
122       tax_id         => '',
123       taxdescription => '',
124       rate           => '',
125       taxkey_id      => '',
126       pos_ustva      => '',
127       startdate      => '',
128     };
129
130     push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
131
132     my $i = 0;
133     foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
134
135       # Fill in a runningnumber
136       $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
137
138       # Fill in the Taxkeys as select options
139       foreach my $item (@{ $form->{TAXKEY} }) {
140         if ($item->{id} == $taxkey_used->{tax_id}) {
141           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
142             qq|<option value="$item->{id}" selected="selected">|
143             . sprintf("%.2d", $item->{taxkey}) 
144             . qq|. $item->{taxdescription} ($item->{rate}) |
145             . $locale->text('Tax-o-matic Account') 
146             . qq|: $item->{chart_accno}\n|;
147         } 
148         else {
149           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
150             qq|<option value="$item->{id}">|
151             . sprintf("%.2d", $item->{taxkey}) 
152             . qq|. $item->{taxdescription} ($item->{rate}) |
153             . $locale->text('Tax-o-matic Account')
154             . qq|: $item->{chart_accno}\n|;
155         }
156
157       }
158       
159       # Fill in the USTVA Numbers as select options
160       foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
161         if ($item eq ''){
162           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
163         } 
164         elsif ( $item eq $taxkey_used->{pos_ustva} ) {
165           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
166         }
167         else {
168           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
169         }
170
171       }      
172
173       $i++;
174     }
175   }
176
177   # Newaccount Folgekonto 
178   if (@{ $form->{NEWACCOUNT} }) {
179     if (!$form->{new_chart_valid}) {
180       $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
181     }
182     foreach $item (@{ $form->{NEWACCOUNT} }) {
183       if ($item->{id} == $form->{new_chart_id}) {
184         $form->{selectnewaccount} .=
185           qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
186       } elsif (!$form->{new_chart_valid}) {
187         $form->{selectnewaccount} .=
188           qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
189       }
190
191     }
192   }
193
194   $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
195   %eur = (1  => "Umsatzerlöse",
196           2  => "sonstige Erlöse",
197           3  => "Privatanteile",
198           4  => "Zinserträge",
199           5  => "Ausserordentliche Erträge",
200           6  => "Vereinnahmte Umsatzst.",
201           7  => "Umsatzsteuererstattungen",
202           8  => "Wareneingänge",
203           9  => "Löhne und Gehälter",
204           10 => "Gesetzl. sozialer Aufw.",
205           11 => "Mieten",
206           12 => "Gas, Strom, Wasser",
207           13 => "Instandhaltung",
208           14 => "Steuern, Versich., Beiträge",
209           15 => "Kfz-Steuern",
210           16 => "Kfz-Versicherungen",
211           17 => "Sonst. Fahrtkosten",
212           18 => "Werbe- und Reisekosten",
213           19 => "Instandhaltung u. Werkzeuge",
214           20 => "Fachzeitschriften, Bücher",
215           21 => "Miete für Einrichtungen",
216           22 => "Rechts- und Beratungskosten",
217           23 => "Bürobedarf, Porto, Telefon",
218           24 => "Sonstige Aufwendungen",
219           25 => "Abschreibungen auf Anlagever.",
220           26 => "Abschreibungen auf GWG",
221           27 => "Vorsteuer",
222           28 => "Umsatzsteuerzahlungen",
223           29 => "Zinsaufwand",
224           30 => "Ausserordentlicher Aufwand",
225           31 => "Betriebliche Steuern");
226   foreach $item (sort({ $a <=> $b } keys(%eur))) {
227     my $text = H(SL::Iconv::convert("ISO-8859-15", $dbcharset, $eur{$item}));
228     if ($item == $form->{pos_eur}) {
229       $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
230     } else {
231       $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
232     }
233
234   }
235
236   $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
237
238   %bwapos = (1  => 'Umsatzerlöse',
239              2  => 'Best.Verdg.FE/UE',
240              3  => 'Aktiv.Eigenleistung',
241              4  => 'Mat./Wareneinkauf',
242              5  => 'So.betr.Erlöse',
243              10 => 'Personalkosten',
244              11 => 'Raumkosten',
245              12 => 'Betriebl.Steuern',
246              13 => 'Vers./Beiträge',
247              14 => 'Kfz.Kosten o.St.',
248              15 => 'Werbe-Reisek.',
249              16 => 'Kosten Warenabgabe',
250              17 => 'Abschreibungen',
251              18 => 'Rep./instandhlt.',
252              19 => 'Übrige Steuern',
253              20 => 'Sonst.Kosten',
254              30 => 'Zinsauwand',
255              31 => 'Sonst.neutr.Aufw.',
256              32 => 'Zinserträge',
257              33 => 'Sonst.neutr.Ertrag',
258              34 => 'Verr.kalk.Kosten',
259              35 => 'Steuern Eink.u.Ertr.');
260   foreach $item (sort({ $a <=> $b } keys %bwapos)) {
261     my $text = H(SL::Iconv::convert("ISO-8859-15", $dbcharset, $bwapos{$item}));
262     if ($item == $form->{pos_bwa}) {
263       $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
264     } else {
265       $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
266     }
267
268   }
269
270 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
271   $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
272   foreach $item ((1, 2, 3, 4)) {
273     if ($item == $form->{pos_bilanz}) {
274       $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
275     } else {
276       $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
277     }
278
279   }
280
281   # this is for our parser only! Do not remove.
282   # type=submit $locale->text('Add Account')
283   # type=submit $locale->text('Edit Account')
284   
285   $form->{type} = "account";
286
287   # preselections category
288  
289   $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
290
291   %category = (
292       'A'  => $locale->text('Asset'),
293       'L'  => $locale->text('Liability'),
294       'Q'  => $locale->text('Equity'),
295       'I'  => $locale->text('Revenue'),      
296       'E'  => $locale->text('Expense'),
297       'C'  => $locale->text('Costs'),
298   );
299   foreach $item ( sort({ $a <=> $b } keys %category) ) {
300     if ($item eq $form->{category}) {
301       $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
302     } else {
303       $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
304     }
305
306   }
307   
308   # preselection chart type
309   my $select_charttype = q{};
310
311   my %charttype = (
312       'A'  => $locale->text('Account'),
313       'H'  => $locale->text('Header'),
314   );
315   
316   foreach $item ( sort({ $a <=> $b } keys %charttype) ) {
317     if ($item eq $form->{charttype}) {
318       $select_charttype .= qq|<option value="$item" selected="selected">$charttype{$item}\n|;
319
320     } else {
321       $select_charttype .= qq|<option value="$item">$charttype{$item}\n|;
322     }
323
324   }
325
326   my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
327   
328   $form->header();
329   
330   my $parameters_ref = {
331     ChartTypeIsAccount         => $ChartTypeIsAccount,
332     select_category            => $select_category,
333     select_charttype           => $select_charttype,
334     newaccount                 => $newaccount,
335     checked                    => $checked,
336     select_bwa                 => $select_bwa,
337     select_bilanz              => $select_bilanz,
338     select_eur                 => $select_eur,
339   };
340   
341   # Ausgabe des Templates
342   print($form->parse_html_template('am/edit_accounts', $parameters_ref));
343
344
345   $lxdebug->leave_sub();
346 }
347
348 sub form_footer {
349   $lxdebug->enter_sub();
350
351   print qq|
352
353 <input name=callback type=hidden value="$form->{callback}">
354
355 <input type=hidden name=login value=$form->{login}>
356 <input type=hidden name=password value=$form->{password}>
357
358 <br>|;
359   if ((!$form->{id}) || ($form->{id} && $form->{orphaned}) || (($form->{type} eq "account") && (!$form->{new_chart_valid}))) {
360     print qq|
361 <input type=submit class=submit name=action value="|
362     . $locale->text('Save') . qq|">
363 |;
364 }
365
366   if ($form->{id} && $form->{orphaned}) {
367     print qq|<input type=submit class=submit name=action value="|
368       . $locale->text('Delete') . qq|">|;
369   }
370
371   print qq|
372 </form>
373
374 </body>
375 </html>
376 |;
377
378   $lxdebug->leave_sub();
379 }
380
381 sub save_account {
382   $lxdebug->enter_sub();
383
384   $form->isblank("accno",       $locale->text('Account Number missing!'));
385   $form->isblank("description", $locale->text('Account Description missing!'));
386   
387   if ($form->{charttype} eq 'A'){
388     $form->isblank("category",  $locale->text('Account Type missing!'));
389   }
390
391   $form->redirect($locale->text('Account saved!'))
392     if (AM->save_account(\%myconfig, \%$form));
393   $form->error($locale->text('Cannot save account!'));
394
395   $lxdebug->leave_sub();
396 }
397
398 sub list_account {
399   $lxdebug->enter_sub();
400
401   $form->{callback}     = build_std_url('action=list_account');
402   my $link_edit_account = build_std_url('action=edit_account', 'callback');
403
404   CA->all_accounts(\%myconfig, \%$form);
405
406   foreach $ca (@{ $form->{CA} }) {
407
408     $ca->{debit}  = "";
409     $ca->{credit} = "";
410
411     if ($ca->{amount} > 0) {
412       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
413     }
414     if ($ca->{amount} < 0) {
415       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
416     }
417     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:''; 
418     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
419   }
420   
421   # Ajax 
422   my $pjx = new CGI::Ajax('list_account_details' => build_std_url('action=list_account_details'));
423
424   # Eneable AJAX debuging
425   #$pjx->DEBUG(1);
426   #$pjx->JSDEBUG(1);
427     
428   push(@ { $form->{AJAX} }, $pjx);
429
430   $form->{stylesheets} = "list_accounts.css";
431   $form->{title}       = $locale->text('Chart of Accounts');
432
433   $form->header;
434   
435   
436   my $parameters_ref = {
437   #   hidden_variables                => $_hidden_variables_ref,
438   };
439   
440   # Ausgabe des Templates
441   print($form->parse_html_template('am/list_accounts', $parameters_ref));
442   
443   $lxdebug->leave_sub();
444
445 }
446
447
448 sub list_account_details {
449 # Ajax Funktion aus list_account_details
450   $lxdebug->enter_sub();
451
452   my $chart_id = $form->{args};
453
454   CA->all_accounts(\%myconfig, \%$form, $chart_id);
455
456   foreach $ca (@{ $form->{CA} }) {
457
458     $ca->{debit}  = "&nbsp;";
459     $ca->{credit} = "&nbsp;";
460
461     if ($ca->{amount} > 0) {
462       $ca->{credit} =
463         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
464     }
465     if ($ca->{amount} < 0) {
466       $ca->{debit} =
467         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
468     }
469
470     my @links = split( q{:}, $ca->{link});
471
472     $ca->{link} = q{};
473
474     foreach my $link (@links){
475       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
476                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
477                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
478                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
479                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
480                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
481                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
482                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
483                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
484                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
485                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
486                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
487                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
488                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
489                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
490 #               : ( $link eq 'CT_tax' )        ? $locale->text('Account Link CT_tax')
491                : $locale->text('Unknown Link') . ': ' . $link;
492       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
493     }
494
495     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
496                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
497                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
498                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
499                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
500                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
501                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
502                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
503   }
504
505   $form->{title} = $locale->text('Chart of Accounts');
506   $form->header();
507
508   print $form->parse_html_template('am/list_account_details');
509
510   $lxdebug->leave_sub();
511
512 }
513
514 sub delete_account {
515   $lxdebug->enter_sub();
516
517   $form->{title} = $locale->text('Delete Account');
518
519   foreach $id (
520     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
521     ) {
522     if ($form->{id} == $form->{$id}) {
523       $form->error($locale->text('Cannot delete default account!'));
524     }
525   }
526
527   $form->redirect($locale->text('Account deleted!'))
528     if (AM->delete_account(\%myconfig, \%$form));
529   $form->error($locale->text('Cannot delete account!'));
530
531   $lxdebug->leave_sub();
532 }
533
534 sub add_department {
535   $lxdebug->enter_sub();
536
537   $form->{title} = "Add";
538   $form->{role}  = "P";
539
540   $form->{callback} =
541     "$form->{script}?action=add_department&login=$form->{login}&password=$form->{password}"
542     unless $form->{callback};
543
544   &department_header;
545   &form_footer;
546
547   $lxdebug->leave_sub();
548 }
549
550 sub edit_department {
551   $lxdebug->enter_sub();
552
553   $form->{title} = "Edit";
554
555   AM->get_department(\%myconfig, \%$form);
556
557   &department_header;
558   &form_footer;
559
560   $lxdebug->leave_sub();
561 }
562
563 sub list_department {
564   $lxdebug->enter_sub();
565
566   AM->departments(\%myconfig, \%$form);
567
568   $form->{callback} =
569     "$form->{script}?action=list_department&login=$form->{login}&password=$form->{password}";
570
571   $callback = $form->escape($form->{callback});
572
573   $form->{title} = $locale->text('Departments');
574
575   @column_index = qw(description cost profit);
576
577   $column_header{description} =
578       qq|<th class=listheading width=90%>|
579     . $locale->text('Description')
580     . qq|</th>|;
581   $column_header{cost} =
582       qq|<th class=listheading nowrap>|
583     . $locale->text('Cost Center')
584     . qq|</th>|;
585   $column_header{profit} =
586       qq|<th class=listheading nowrap>|
587     . $locale->text('Profit Center')
588     . qq|</th>|;
589
590   $form->header;
591
592   print qq|
593 <body>
594
595 <table width=100%>
596   <tr>
597     <th class=listtop>$form->{title}</th>
598   </tr>
599   <tr height="5"></tr>
600   <tr>
601     <td>
602       <table width=100%>
603         <tr class=listheading>
604 |;
605
606   map { print "$column_header{$_}\n" } @column_index;
607
608   print qq|
609         </tr>
610 |;
611
612   foreach $ref (@{ $form->{ALL} }) {
613
614     $i++;
615     $i %= 2;
616
617     print qq|
618         <tr valign=top class=listrow$i>
619 |;
620
621     $costcenter   = ($ref->{role} eq "C") ? "X" : "";
622     $profitcenter = ($ref->{role} eq "P") ? "X" : "";
623
624     $column_data{description} =
625       qq|<td><a href=$form->{script}?action=edit_department&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
626     $column_data{cost}   = qq|<td align=center>$costcenter</td>|;
627     $column_data{profit} = qq|<td align=center>$profitcenter</td>|;
628
629     map { print "$column_data{$_}\n" } @column_index;
630
631     print qq|
632         </tr>
633 |;
634   }
635
636   print qq|
637       </table>
638     </td>
639   </tr>
640   <tr>
641   <td><hr size=3 noshade></td>
642   </tr>
643 </table>
644
645 <br>
646 <form method=post action=$form->{script}>
647
648 <input name=callback type=hidden value="$form->{callback}">
649
650 <input type=hidden name=type value=department>
651
652 <input type=hidden name=login value=$form->{login}>
653 <input type=hidden name=password value=$form->{password}>
654
655 <input class=submit type=submit name=action value="|
656     . $locale->text('Add') . qq|">
657
658   </form>
659
660   </body>
661   </html>
662 |;
663
664   $lxdebug->leave_sub();
665 }
666
667 sub department_header {
668   $lxdebug->enter_sub();
669
670   $form->{title} = $locale->text("$form->{title} Department");
671
672   # $locale->text('Add Department')
673   # $locale->text('Edit Department')
674
675   $form->{description} =~ s/\"/&quot;/g;
676
677   if (($rows = $form->numtextrows($form->{description}, 60)) > 1) {
678     $description =
679       qq|<textarea name="description" rows=$rows cols=60 wrap=soft>$form->{description}</textarea>|;
680   } else {
681     $description =
682       qq|<input name=description size=60 value="$form->{description}">|;
683   }
684
685   $costcenter   = "checked" if $form->{role} eq "C";
686   $profitcenter = "checked" if $form->{role} eq "P";
687
688   $form->header;
689
690   print qq|
691 <body>
692
693 <form method=post action=$form->{script}>
694
695 <input type=hidden name=id value=$form->{id}>
696 <input type=hidden name=type value=department>
697
698 <table width=100%>
699   <tr>
700     <th class=listtop colspan=2>$form->{title}</th>
701   </tr>
702   <tr height="5"></tr>
703   <tr>
704     <th align=right>| . $locale->text('Description') . qq|</th>
705     <td>$description</td>
706   </tr>
707   <tr>
708     <td></td>
709     <td><input type=radio style=radio name=role value="C" $costcenter> |
710     . $locale->text('Cost Center') . qq|
711         <input type=radio style=radio name=role value="P" $profitcenter> |
712     . $locale->text('Profit Center') . qq|
713     </td>
714   <tr>
715     <td colspan=2><hr size=3 noshade></td>
716   </tr>
717 </table>
718 |;
719
720   $lxdebug->leave_sub();
721 }
722
723 sub save_department {
724   $lxdebug->enter_sub();
725
726   $form->isblank("description", $locale->text('Description missing!'));
727   AM->save_department(\%myconfig, \%$form);
728   $form->redirect($locale->text('Department saved!'));
729
730   $lxdebug->leave_sub();
731 }
732
733 sub delete_department {
734   $lxdebug->enter_sub();
735
736   AM->delete_department(\%myconfig, \%$form);
737   $form->redirect($locale->text('Department deleted!'));
738
739   $lxdebug->leave_sub();
740 }
741
742 sub add_lead {
743   $lxdebug->enter_sub();
744
745   $form->{title} = "Add";
746
747   $form->{callback} =
748     "$form->{script}?action=add_lead&login=$form->{login}&password=$form->{password}"
749     unless $form->{callback};
750
751   &lead_header;
752   &form_footer;
753
754   $lxdebug->leave_sub();
755 }
756
757 sub edit_lead {
758   $lxdebug->enter_sub();
759
760   $form->{title} = "Edit";
761
762   AM->get_lead(\%myconfig, \%$form);
763
764   &lead_header;
765
766   $form->{orphaned} = 1;
767   &form_footer;
768
769   $lxdebug->leave_sub();
770 }
771
772 sub list_lead {
773   $lxdebug->enter_sub();
774
775   AM->lead(\%myconfig, \%$form);
776
777   $form->{callback} =
778     "$form->{script}?action=list_lead&login=$form->{login}&password=$form->{password}";
779
780   $callback = $form->escape($form->{callback});
781
782   $form->{title} = $locale->text('Lead');
783
784   @column_index = qw(description cost profit);
785
786   $column_header{description} =
787       qq|<th class=listheading width=100%>|
788     . $locale->text('Description')
789     . qq|</th>|;
790
791   $form->header;
792
793   print qq|
794 <body>
795
796 <table width=100%>
797   <tr>
798     <th class=listtop>$form->{title}</th>
799   </tr>
800   <tr height="5"></tr>
801   <tr class=listheading>
802 |;
803
804   map { print "$column_header{$_}\n" } @column_index;
805
806   print qq|
807         </tr>
808 |;
809
810   foreach $ref (@{ $form->{ALL} }) {
811
812     $i++;
813     $i %= 2;
814
815     print qq|
816         <tr valign=top class=listrow$i>
817 |;
818
819         $lead = $ref->{lead};
820         
821     $column_data{description} =
822       qq|<td><a href=$form->{script}?action=edit_lead&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{lead}</td>|;
823
824     map { print "$column_data{$_}\n" } @column_index;
825
826     print qq|
827         </tr>
828 |;
829   }
830
831   print qq|
832   <tr>
833   <td><hr size=3 noshade></td>
834   </tr>
835 </table>
836
837 <br>
838 <form method=post action=$form->{script}>
839
840 <input name=callback type=hidden value="$form->{callback}">
841
842 <input type=hidden name=type value=lead>
843
844 <input type=hidden name=login value=$form->{login}>
845 <input type=hidden name=password value=$form->{password}>
846
847 <input class=submit type=submit name=action value="|
848     . $locale->text('Add') . qq|">
849
850   </form>
851
852   </body>
853   </html>
854 |;
855
856   $lxdebug->leave_sub();
857 }
858
859 sub lead_header {
860   $lxdebug->enter_sub();
861
862   $form->{title} = $locale->text("$form->{title} Lead");
863
864   # $locale->text('Add Lead')
865   # $locale->text('Edit Lead')
866
867   $form->{description} =~ s/\"/&quot;/g;
868
869   $description =
870       qq|<input name=description size=50 value="$form->{lead}">|;
871
872   $form->header;
873
874   print qq|
875 <body>
876
877 <form method=post action=$form->{script}>
878
879 <input type=hidden name=id value=$form->{id}>
880 <input type=hidden name=type value=lead>
881
882 <table width=100%>
883   <tr>
884     <th class=listtop colspan=2>$form->{title}</th>
885   </tr>
886   <tr height="5"></tr>
887   <tr>
888     <th align=right>| . $locale->text('Description') . qq|</th>
889     <td>$description</td>
890   </tr>
891     <td colspan=2><hr size=3 noshade></td>
892   </tr>
893 </table>
894 |;
895
896   $lxdebug->leave_sub();
897 }
898
899 sub save_lead {
900   $lxdebug->enter_sub();
901
902   $form->isblank("description", $locale->text('Description missing!'));
903   AM->save_lead(\%myconfig, \%$form);
904   $form->redirect($locale->text('lead saved!'));
905
906   $lxdebug->leave_sub();
907 }
908
909 sub delete_lead {
910   $lxdebug->enter_sub();
911
912   AM->delete_lead(\%myconfig, \%$form);
913   $form->redirect($locale->text('lead deleted!'));
914
915   $lxdebug->leave_sub();
916 }
917
918 sub add_business {
919   $lxdebug->enter_sub();
920
921   $form->{title} = "Add";
922
923   $form->{callback} =
924     "$form->{script}?action=add_business&login=$form->{login}&password=$form->{password}"
925     unless $form->{callback};
926
927   &business_header;
928   &form_footer;
929
930   $lxdebug->leave_sub();
931 }
932
933 sub edit_business {
934   $lxdebug->enter_sub();
935
936   $form->{title} = "Edit";
937
938   AM->get_business(\%myconfig, \%$form);
939
940   &business_header;
941
942   $form->{orphaned} = 1;
943   &form_footer;
944
945   $lxdebug->leave_sub();
946 }
947
948 sub list_business {
949   $lxdebug->enter_sub();
950
951   AM->business(\%myconfig, \%$form);
952
953   $form->{callback} =
954     "$form->{script}?action=list_business&login=$form->{login}&password=$form->{password}";
955
956   $callback = $form->escape($form->{callback});
957
958   $form->{title} = $locale->text('Type of Business');
959
960   @column_index = qw(description discount customernumberinit);
961
962   $column_header{description} =
963       qq|<th class=listheading width=60%>|
964     . $locale->text('Description')
965     . qq|</th>|;
966   $column_header{discount} =
967       qq|<th class=listheading width=10%>|
968     . $locale->text('Discount')
969     . qq| %</th>|;
970   $column_header{customernumberinit} =
971       qq|<th class=listheading>|
972     . $locale->text('Customernumberinit')
973     . qq|</th>|;
974
975   $form->header;
976
977   print qq|
978 <body>
979
980 <table width=100%>
981   <tr>
982     <th class=listtop>$form->{title}</th>
983   </tr>
984   <tr height="5"></tr>
985   <tr>
986     <td>
987       <table width=100%>
988         <tr class=listheading>
989 |;
990
991   map { print "$column_header{$_}\n" } @column_index;
992
993   print qq|
994         </tr>
995 |;
996
997   foreach $ref (@{ $form->{ALL} }) {
998
999     $i++;
1000     $i %= 2;
1001
1002     print qq|
1003         <tr valign=top class=listrow$i>
1004 |;
1005
1006     $discount =
1007       $form->format_amount(\%myconfig, $ref->{discount} * 100);
1008     $description =
1009       $ref->{description};
1010     $column_data{description} =
1011       qq|<td><a href=$form->{script}?action=edit_business&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$description</td>|;
1012     $column_data{discount}           = qq|<td align=right>$discount</td>|;
1013     $column_data{customernumberinit} =
1014       qq|<td align=right>$ref->{customernumberinit}</td>|;
1015
1016     map { print "$column_data{$_}\n" } @column_index;
1017
1018     print qq|
1019         </tr>
1020 |;
1021   }
1022
1023   print qq|
1024       </table>
1025     </td>
1026   </tr>
1027   <tr>
1028   <td><hr size=3 noshade></td>
1029   </tr>
1030 </table>
1031
1032 <br>
1033 <form method=post action=$form->{script}>
1034
1035 <input name=callback type=hidden value="$form->{callback}">
1036
1037 <input type=hidden name=type value=business>
1038
1039 <input type=hidden name=login value=$form->{login}>
1040 <input type=hidden name=password value=$form->{password}>
1041
1042 <input class=submit type=submit name=action value="|
1043     . $locale->text('Add') . qq|">
1044
1045   </form>
1046
1047   </body>
1048   </html>
1049 |;
1050
1051   $lxdebug->leave_sub();
1052 }
1053
1054 sub business_header {
1055   $lxdebug->enter_sub();
1056
1057   $form->{title}    = $locale->text("$form->{title} Business");
1058
1059   # $locale->text('Add Business')
1060   # $locale->text('Edit Business')
1061
1062   $form->{description} =~ s/\"/&quot;/g;
1063   $form->{discount} =
1064     $form->format_amount(\%myconfig, $form->{discount} * 100);
1065
1066   $form->header;
1067
1068   print qq|
1069 <body>
1070
1071 <form method=post action=$form->{script}>
1072
1073 <input type=hidden name=id value=$form->{id}>
1074 <input type=hidden name=type value=business>
1075
1076 <table width=100%>
1077   <tr>
1078     <th class=listtop colspan=2>$form->{title}</th>
1079   </tr>
1080   <tr height="5"></tr>
1081   <tr>
1082     <th align=right>| . $locale->text('Type of Business') . qq|</th>
1083     <td><input name=description size=30 value="$form->{description}"></td>
1084   <tr>
1085   <tr>
1086     <th align=right>| . $locale->text('Discount') . qq| %</th>
1087     <td><input name=discount size=5 value=$form->{discount}></td>
1088   </tr>
1089   <tr>
1090     <th align=right>| . $locale->text('Customernumberinit') . qq|</th>
1091     <td><input name=customernumberinit size=10 value=$form->{customernumberinit}></td>
1092   </tr>
1093   <td colspan=2><hr size=3 noshade></td>
1094   </tr>
1095 </table>
1096 |;
1097
1098   $lxdebug->leave_sub();
1099 }
1100
1101 sub save_business {
1102   $lxdebug->enter_sub();
1103
1104   $form->isblank("description", $locale->text('Description missing!'));
1105   $form->{discount} = $form->parse_amount(\%myconfig, $form->{discount}) / 100;
1106   AM->save_business(\%myconfig, \%$form);
1107   $form->redirect($locale->text('Business saved!'));
1108
1109   $lxdebug->leave_sub();
1110 }
1111
1112 sub delete_business {
1113   $lxdebug->enter_sub();
1114
1115   AM->delete_business(\%myconfig, \%$form);
1116   $form->redirect($locale->text('Business deleted!'));
1117
1118   $lxdebug->leave_sub();
1119 }
1120
1121 sub add_language {
1122   $lxdebug->enter_sub();
1123
1124   $form->{title} = "Add";
1125
1126   $form->{callback} =
1127     "$form->{script}?action=add_language&login=$form->{login}&password=$form->{password}"
1128     unless $form->{callback};
1129
1130   &language_header;
1131   &form_footer;
1132
1133   $lxdebug->leave_sub();
1134 }
1135
1136 sub edit_language {
1137   $lxdebug->enter_sub();
1138
1139   $form->{title} = "Edit";
1140
1141   AM->get_language(\%myconfig, \%$form);
1142
1143   &language_header;
1144
1145   $form->{orphaned} = 1;
1146   &form_footer;
1147
1148   $lxdebug->leave_sub();
1149 }
1150
1151 sub list_language {
1152   $lxdebug->enter_sub();
1153
1154   AM->language(\%myconfig, \%$form);
1155
1156   $form->{callback} =
1157     "$form->{script}?action=list_language&login=$form->{login}&password=$form->{password}";
1158
1159   $callback = $form->escape($form->{callback});
1160
1161   $form->{title} = $locale->text('Languages');
1162
1163   @column_index = qw(description template_code article_code output_numberformat output_dateformat output_longdates);
1164
1165   $column_header{description} =
1166       qq|<th class=listheading width=60%>|
1167     . $locale->text('Description')
1168     . qq|</th>|;
1169   $column_header{template_code} =
1170       qq|<th class=listheading width=10%>|
1171     . $locale->text('Template Code')
1172     . qq|</th>|;
1173   $column_header{article_code} =
1174       qq|<th class=listheading>|
1175     . $locale->text('Article Code')
1176     . qq|</th>|;
1177   $column_header{output_numberformat} =
1178       qq|<th class=listheading>|
1179     . $locale->text('Number Format')
1180     . qq|</th>|;
1181   $column_header{output_dateformat} =
1182       qq|<th class=listheading>|
1183     . $locale->text('Date Format')
1184     . qq|</th>|;
1185   $column_header{output_longdates} =
1186       qq|<th class=listheading>|
1187     . $locale->text('Long Dates')
1188     . qq|</th>|;
1189
1190   $form->header;
1191
1192   print qq|
1193 <body>
1194
1195 <table width=100%>
1196   <tr>
1197     <th class=listtop>$form->{title}</th>
1198   </tr>
1199   <tr height="5"></tr>
1200   <tr>
1201     <td>
1202       <table width=100%>
1203         <tr class=listheading>
1204 |;
1205
1206   map { print "$column_header{$_}\n" } @column_index;
1207
1208   print qq|
1209         </tr>
1210 |;
1211
1212   foreach $ref (@{ $form->{ALL} }) {
1213
1214     $i++;
1215     $i %= 2;
1216
1217     print qq|
1218         <tr valign=top class=listrow$i>
1219 |;
1220
1221
1222     $column_data{description} =
1223       qq|<td><a href=$form->{script}?action=edit_language&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
1224     $column_data{template_code}           = qq|<td align=right>$ref->{template_code}</td>|;
1225     $column_data{article_code} =
1226       qq|<td align=right>$ref->{article_code}</td>|;
1227     $column_data{output_numberformat} =
1228       "<td nowrap>" .
1229       ($ref->{output_numberformat} ? $ref->{output_numberformat} :
1230        $locale->text("use program settings")) .
1231       "</td>";
1232     $column_data{output_dateformat} =
1233       "<td nowrap>" .
1234       ($ref->{output_dateformat} ? $ref->{output_dateformat} :
1235        $locale->text("use program settings")) .
1236       "</td>";
1237     $column_data{output_longdates} =
1238       "<td nowrap>" .
1239       ($ref->{output_longdates} ? $locale->text("Yes") : $locale->text("No")) .
1240       "</td>";
1241
1242     map { print "$column_data{$_}\n" } @column_index;
1243
1244     print qq|
1245         </tr>
1246 |;
1247   }
1248
1249   print qq|
1250       </table>
1251     </td>
1252   </tr>
1253   <tr>
1254   <td><hr size=3 noshade></td>
1255   </tr>
1256 </table>
1257
1258 <br>
1259 <form method=post action=$form->{script}>
1260
1261 <input name=callback type=hidden value="$form->{callback}">
1262
1263 <input type=hidden name=type value=language>
1264
1265 <input type=hidden name=login value=$form->{login}>
1266 <input type=hidden name=password value=$form->{password}>
1267
1268 <input class=submit type=submit name=action value="|
1269     . $locale->text('Add') . qq|">
1270
1271   </form>
1272
1273   </body>
1274   </html>
1275 |;
1276
1277   $lxdebug->leave_sub();
1278 }
1279
1280 sub language_header {
1281   $lxdebug->enter_sub();
1282
1283   $form->{title}    = $locale->text("$form->{title} Language");
1284
1285   # $locale->text('Add Language')
1286   # $locale->text('Edit Language')
1287
1288   $form->{description} =~ s/\"/&quot;/g;
1289   $form->{template_code} =~ s/\"/&quot;/g;
1290   $form->{article_code} =~ s/\"/&quot;/g;
1291
1292
1293   $form->header;
1294
1295   my $numberformat =
1296     qq|<option value="">| . $locale->text("use program settings") .
1297     qq|</option>|;
1298   foreach $item (qw(1,000.00 1000.00 1.000,00 1000,00)) {
1299     $numberformat .=
1300       ($item eq $form->{output_numberformat})
1301       ? "<option selected>$item"
1302       : "<option>$item"
1303       . "</option>";
1304   }
1305
1306   my $dateformat =
1307     qq|<option value="">| . $locale->text("use program settings") .
1308     qq|</option>|;
1309   foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
1310     $dateformat .=
1311       ($item eq $form->{output_dateformat})
1312       ? "<option selected>$item"
1313       : "<option>$item"
1314       . "</option>";
1315   }
1316
1317   print qq|
1318 <body>
1319
1320 <form method=post action=$form->{script}>
1321
1322 <input type=hidden name=id value=$form->{id}>
1323 <input type=hidden name=type value=language>
1324
1325 <table width=100%>
1326   <tr>
1327     <th class=listtop colspan=2>$form->{title}</th>
1328   </tr>
1329   <tr height="5"></tr>
1330   <tr>
1331     <th align=right>| . $locale->text('Language') . qq|</th>
1332     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1333   <tr>
1334   <tr>
1335     <th align=right>| . $locale->text('Template Code') . qq|</th>
1336     <td><input name=template_code size=5 value="| . $form->quote($form->{template_code}) . qq|"></td>
1337   </tr>
1338   <tr>
1339     <th align=right>| . $locale->text('Article Code') . qq|</th>
1340     <td><input name=article_code size=10 value="| . $form->quote($form->{article_code}) . qq|"></td>
1341   </tr>
1342   <tr>
1343     <th align=right>| . $locale->text('Number Format') . qq|</th>
1344     <td><select name="output_numberformat">$numberformat</select></td>
1345   </tr>
1346   <tr>
1347     <th align=right>| . $locale->text('Date Format') . qq|</th>
1348     <td><select name="output_dateformat">$dateformat</select></td>
1349   </tr>
1350   <tr>
1351     <th align=right>| . $locale->text('Long Dates') . qq|</th>
1352     <td><input type="radio" name="output_longdates" value="1"| .
1353     ($form->{output_longdates} ? " checked" : "") .
1354     qq|>| . $locale->text("Yes") .
1355     qq|<input type="radio" name="output_longdates" value="0"| .
1356     ($form->{output_longdates} ? "" : " checked") .
1357     qq|>| . $locale->text("No") .
1358     qq|</td>
1359   </tr>
1360   <td colspan=2><hr size=3 noshade></td>
1361   </tr>
1362 </table>
1363 |;
1364
1365   $lxdebug->leave_sub();
1366 }
1367
1368 sub save_language {
1369   $lxdebug->enter_sub();
1370
1371   $form->isblank("description", $locale->text('Language missing!'));
1372   $form->isblank("template_code", $locale->text('Template Code missing!'));
1373   $form->isblank("article_code", $locale->text('Article Code missing!'));
1374   AM->save_language(\%myconfig, \%$form);
1375   $form->redirect($locale->text('Language saved!'));
1376
1377   $lxdebug->leave_sub();
1378 }
1379
1380 sub delete_language {
1381   $lxdebug->enter_sub();
1382
1383   AM->delete_language(\%myconfig, \%$form);
1384   $form->redirect($locale->text('Language deleted!'));
1385
1386   $lxdebug->leave_sub();
1387 }
1388
1389
1390 sub add_buchungsgruppe {
1391   $lxdebug->enter_sub();
1392
1393   # $locale->text("Add Buchungsgruppe")
1394   # $locale->text("Edit Buchungsgruppe")
1395   $form->{title} = "Add";
1396
1397   $form->{callback} =
1398     "$form->{script}?action=add_buchungsgruppe&login=$form->{login}&password=$form->{password}"
1399     unless $form->{callback};
1400   AM->get_buchungsgruppe(\%myconfig, \%$form);
1401   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
1402   for (my $i = 0; 4 > $i; $i++) {
1403     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
1404         qw(income expense));
1405   }
1406
1407   &buchungsgruppe_header;
1408   &form_footer;
1409
1410   $lxdebug->leave_sub();
1411 }
1412
1413 sub edit_buchungsgruppe {
1414   $lxdebug->enter_sub();
1415
1416   $form->{title} = "Edit";
1417
1418   AM->get_buchungsgruppe(\%myconfig, \%$form);
1419
1420   &buchungsgruppe_header;
1421
1422   &form_footer;
1423
1424   $lxdebug->leave_sub();
1425 }
1426
1427 sub list_buchungsgruppe {
1428   $lxdebug->enter_sub();
1429
1430   AM->buchungsgruppe(\%myconfig, \%$form);
1431
1432   $form->{callback} =
1433     "$form->{script}?action=list_buchungsgruppe&login=$form->{login}&password=$form->{password}";
1434
1435   $callback = $form->escape($form->{callback});
1436
1437   $form->{title} = $locale->text('Buchungsgruppen');
1438
1439   @column_index = qw(up down description inventory_accno
1440                      income_accno_0 expense_accno_0
1441                      income_accno_1 expense_accno_1
1442                      income_accno_2 expense_accno_2
1443                      income_accno_3 expense_accno_3 );
1444
1445   $column_header{up} =
1446       qq|<th class="listheading" width="16">|
1447     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
1448     . qq|</th>|;
1449   $column_header{down} =
1450       qq|<th class="listheading" width="16">|
1451     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
1452     . qq|</th>|;
1453   $column_header{description} =
1454       qq|<th class="listheading" width="40%">|
1455     . $locale->text('Description')
1456     . qq|</th>|;
1457   $column_header{inventory_accno} =
1458       qq|<th class=listheading>|
1459     . $locale->text('Bestandskonto')
1460     . qq|</th>|;
1461   $column_header{income_accno_0} =
1462       qq|<th class=listheading>|
1463     . $locale->text('National Revenues')
1464     . qq|</th>|;
1465   $column_header{expense_accno_0} =
1466       qq|<th class=listheading>|
1467     . $locale->text('National Expenses')
1468     . qq|</th>|;
1469   $column_header{income_accno_1} =
1470       qq|<th class=listheading>|
1471     . $locale->text('Revenues EU with UStId')
1472     . qq|</th>|;
1473   $column_header{expense_accno_1} =
1474       qq|<th class=listheading>|
1475     . $locale->text('Expenses EU with UStId')
1476     . qq|</th>|;
1477   $column_header{income_accno_2} =
1478       qq|<th class=listheading>|
1479     . $locale->text('Revenues EU without UStId')
1480     . qq|</th>|;
1481   $column_header{expense_accno_2} =
1482       qq|<th class=listheading>|
1483     . $locale->text('Expenses EU without UStId')
1484     . qq|</th>|;
1485   $column_header{income_accno_3} =
1486       qq|<th class=listheading>|
1487     . $locale->text('Foreign Revenues')
1488     . qq|</th>|;
1489   $column_header{expense_accno_3} =
1490       qq|<th class=listheading>|
1491     . $locale->text('Foreign Expenses')
1492     . qq|</th>|;
1493   $form->header;
1494
1495   print qq|
1496 <body>
1497
1498 <table width=100%>
1499   <tr>
1500     <th class=listtop>$form->{title}</th>
1501   </tr>
1502   <tr height="5"></tr>
1503   <tr>
1504     <td>
1505       <table width=100%>
1506         <tr class=listheading>
1507 |;
1508
1509   map { print "$column_header{$_}\n" } @column_index;
1510
1511   print qq|
1512         </tr>
1513 |;
1514
1515   my $swap_link = qq|$form->{script}?action=swap_buchungsgruppen&|;
1516   map({ $swap_link .= $_ . "=" . $form->escape($form->{$_}) . "&" }
1517       qw(login password));
1518
1519   my $row = 0;
1520   foreach $ref (@{ $form->{ALL} }) {
1521
1522     $i++;
1523     $i %= 2;
1524
1525     print qq|
1526         <tr valign=top class=listrow$i>
1527 |;
1528
1529     if ($row) {
1530       my $pref = $form->{ALL}->[$row - 1];
1531       $column_data{up} =
1532         qq|<td align="center" valign="center" width="16">| .
1533         qq|<a href="${swap_link}id1=$ref->{id}&id2=$pref->{id}">| .
1534         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
1535         qq|</a></td>|;
1536     } else {
1537       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
1538     }
1539
1540     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
1541       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
1542     } else {
1543       my $nref = $form->{ALL}->[$row + 1];
1544       $column_data{down} =
1545         qq|<td align="center" valign="center" width="16">| .
1546         qq|<a href="${swap_link}id1=$ref->{id}&id2=$nref->{id}">| .
1547         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
1548         qq|</a></td>|;
1549     }
1550
1551     $column_data{description} =
1552       qq|<td><a href=$form->{script}?action=edit_buchungsgruppe&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
1553     $column_data{inventory_accno}           = qq|<td align=right>$ref->{inventory_accno}</td>|;
1554     $column_data{income_accno_0} =
1555       qq|<td align=right>$ref->{income_accno_0}</td>|;
1556     $column_data{expense_accno_0}           = qq|<td align=right>$ref->{expense_accno_0}</td>|;
1557     $column_data{income_accno_1} =
1558       qq|<td align=right>$ref->{income_accno_1}</td>|;
1559     $column_data{expense_accno_1}           = qq|<td align=right>$ref->{expense_accno_1}</td>|;
1560     $column_data{income_accno_2} =
1561       qq|<td align=right>$ref->{income_accno_2}</td>|;
1562     $column_data{expense_accno_2}           = qq|<td align=right>$ref->{expense_accno_2}</td>|;
1563     $column_data{income_accno_3} =
1564       qq|<td align=right>$ref->{income_accno_3}</td>|;
1565     $column_data{expense_accno_3}           = qq|<td align=right>$ref->{expense_accno_3}</td>|;
1566
1567     map { print "$column_data{$_}\n" } @column_index;
1568
1569     print qq|
1570         </tr>
1571 |;
1572
1573     $row++;
1574   }
1575
1576   print qq|
1577       </table>
1578     </td>
1579   </tr>
1580   <tr>
1581   <td><hr size=3 noshade></td>
1582   </tr>
1583 </table>
1584
1585 <br>
1586 <form method=post action=$form->{script}>
1587
1588 <input name=callback type=hidden value="$form->{callback}">
1589
1590 <input type=hidden name=type value=buchungsgruppe>
1591
1592 <input type=hidden name=login value=$form->{login}>
1593 <input type=hidden name=password value=$form->{password}>
1594
1595 <input class=submit type=submit name=action value="|
1596     . $locale->text('Add') . qq|">
1597
1598   </form>
1599
1600   </body>
1601   </html>
1602 |;
1603
1604   $lxdebug->leave_sub();
1605 }
1606
1607 sub buchungsgruppe_header {
1608   $lxdebug->enter_sub();
1609
1610   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
1611
1612   # $locale->text('Add Accounting Group')
1613   # $locale->text('Edit Accounting Group')
1614
1615   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
1616   my %acc_type_map = (
1617     "IC" => $acc_inventory,
1618     "IC_income" => $acc_income,
1619     "IC_sale" => $acc_income,
1620     "IC_expense" => $acc_expense,
1621     "IC_cogs" => $acc_expense,
1622     );
1623
1624   foreach $key (keys(%acc_type_map)) {
1625     foreach $ref (@{ $form->{IC_links}{$key} }) {
1626       $acc_type_map{$key}->{$ref->{"id"}} = $ref;
1627     }
1628   }
1629
1630   foreach my $type (qw(IC IC_income IC_expense)) {
1631     $form->{"select$type"} =
1632       join("",
1633            map({ "<option value=$_->{id} $_->{selected}>" .
1634                    "$_->{accno}--" . H($_->{description}) . "</option>" }
1635                sort({ $a->{"accno"} cmp $b->{"accno"} }
1636                     values(%{$acc_type_map{$type}}))));
1637   }
1638
1639   if ($form->{id}) {
1640     $form->{selectIC} =~ s/selected//g;
1641     $form->{selectIC} =~ s/ value=\Q$form->{inventory_accno_id}\E/  value=$form->{inventory_accno_id} selected/;
1642     $form->{selectIC_income} =~ s/selected//g;
1643     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_0}\E/  value=$form->{income_accno_id_0} selected/;
1644     $form->{selectIC_expense} =~ s/selected//g;
1645     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_0}\E/  value=$form->{expense_accno_id_0} selected/;
1646   }
1647
1648   if (!$eur) {
1649     $linkaccounts = qq|
1650                <tr>
1651                 <th align=right>| . $locale->text('Inventory') . qq|</th>
1652                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
1653                 <input name=selectIC type=hidden value="$form->{selectIC}">
1654               </tr>|;
1655   } else {
1656     $linkaccounts = qq|
1657                 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>|;
1658   }
1659
1660
1661   $linkaccounts .= qq|
1662               <tr>
1663                 <th align=right>| . $locale->text('National Revenues') . qq|</th>
1664                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
1665               </tr>
1666               <tr>
1667                 <th align=right>| . $locale->text('National Expenses') . qq|</th>
1668                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
1669               </tr>|;
1670   if ($form->{id}) {
1671     $form->{selectIC_income} =~ s/selected//g;
1672     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_1}\E/  value=$form->{income_accno_id_1} selected/;
1673     $form->{selectIC_expense} =~ s/selected//g;
1674     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_1}\E/  value=$form->{expense_accno_id_1} selected/;
1675   }
1676   $linkaccounts .= qq|        <tr>
1677                 <th align=right>| . $locale->text('Revenues EU with UStId') . qq|</th>
1678                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
1679               </tr>
1680               <tr>
1681                 <th align=right>| . $locale->text('Expenses EU with UStId') . qq|</th>
1682                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
1683               </tr>|;
1684
1685   if ($form->{id}) {
1686     $form->{selectIC_income} =~ s/selected//g;
1687     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_2}\E/  value=$form->{income_accno_id_2} selected/;
1688     $form->{selectIC_expense} =~ s/selected//g;
1689     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_2}\E/  value=$form->{expense_accno_id_2} selected/;
1690   }
1691
1692   $linkaccounts .= qq|        <tr>
1693                 <th align=right>| . $locale->text('Revenues EU without UStId') . qq|</th>
1694                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
1695               </tr>
1696               <tr>
1697                 <th align=right>| . $locale->text('Expenses EU without UStId') . qq|</th>
1698                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
1699               </tr>|;
1700
1701   if ($form->{id}) {
1702     $form->{selectIC_income} =~ s/selected//g;
1703     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_3}\E/  value=$form->{income_accno_id_3} selected/;
1704     $form->{selectIC_expense} =~ s/selected//g;
1705     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_3}\E/  value=$form->{expense_accno_id_3} selected/;
1706   }
1707
1708   $linkaccounts .= qq|        <tr>
1709                 <th align=right>| . $locale->text('Foreign Revenues') . qq|</th>
1710                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
1711               </tr>
1712               <tr>
1713                 <th align=right>| . $locale->text('Foreign Expenses') . qq|</th>
1714                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
1715               </tr>
1716 |;
1717
1718
1719   $form->header;
1720
1721   print qq|
1722 <body>
1723
1724 <form method=post action=$form->{script}>
1725
1726 <input type=hidden name=id value=$form->{id}>
1727 <input type=hidden name=type value=buchungsgruppe>
1728
1729 <table width=100%>
1730   <tr>
1731     <th class=listtop colspan=2>$form->{title}</th>
1732   </tr>
1733   <tr height="5"></tr>
1734   <tr>
1735     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
1736     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1737   <tr>
1738   $linkaccounts
1739   <td colspan=2><hr size=3 noshade></td>
1740   </tr>
1741 </table>
1742 |;
1743
1744   $lxdebug->leave_sub();
1745 }
1746
1747 sub save_buchungsgruppe {
1748   $lxdebug->enter_sub();
1749
1750   $form->isblank("description", $locale->text('Description missing!'));
1751
1752   AM->save_buchungsgruppe(\%myconfig, \%$form);
1753   $form->redirect($locale->text('Accounting Group saved!'));
1754
1755   $lxdebug->leave_sub();
1756 }
1757
1758 sub delete_buchungsgruppe {
1759   $lxdebug->enter_sub();
1760
1761   AM->delete_buchungsgruppe(\%myconfig, \%$form);
1762   $form->redirect($locale->text('Accounting Group deleted!'));
1763
1764   $lxdebug->leave_sub();
1765 }
1766
1767 sub swap_buchungsgruppen {
1768   $lxdebug->enter_sub();
1769
1770   AM->swap_sortkeys(\%myconfig, $form, "buchungsgruppen");
1771   list_buchungsgruppe();
1772
1773   $lxdebug->leave_sub();
1774 }
1775
1776
1777 sub add_printer {
1778   $lxdebug->enter_sub();
1779
1780   $form->{title} = "Add";
1781
1782   $form->{callback} =
1783     "$form->{script}?action=add_printer&login=$form->{login}&password=$form->{password}"
1784     unless $form->{callback};
1785
1786   &printer_header;
1787   &form_footer;
1788
1789   $lxdebug->leave_sub();
1790 }
1791
1792 sub edit_printer {
1793   $lxdebug->enter_sub();
1794
1795   $form->{title} = "Edit";
1796
1797   AM->get_printer(\%myconfig, \%$form);
1798
1799   &printer_header;
1800
1801   $form->{orphaned} = 1;
1802   &form_footer;
1803
1804   $lxdebug->leave_sub();
1805 }
1806
1807 sub list_printer {
1808   $lxdebug->enter_sub();
1809
1810   AM->printer(\%myconfig, \%$form);
1811
1812   $form->{callback} =
1813     "$form->{script}?action=list_printer&login=$form->{login}&password=$form->{password}";
1814
1815   $callback = $form->escape($form->{callback});
1816
1817   $form->{title} = $locale->text('Printer');
1818
1819   @column_index = qw(printer_description printer_command template_code);
1820
1821   $column_header{printer_description} =
1822       qq|<th class=listheading width=60%>|
1823     . $locale->text('Printer Description')
1824     . qq|</th>|;
1825   $column_header{printer_command} =
1826       qq|<th class=listheading width=10%>|
1827     . $locale->text('Printer Command')
1828     . qq|</th>|;
1829   $column_header{template_code} =
1830       qq|<th class=listheading>|
1831     . $locale->text('Template Code')
1832     . qq|</th>|;
1833
1834   $form->header;
1835
1836   print qq|
1837 <body>
1838
1839 <table width=100%>
1840   <tr>
1841     <th class=listtop>$form->{title}</th>
1842   </tr>
1843   <tr height="5"></tr>
1844   <tr>
1845     <td>
1846       <table width=100%>
1847         <tr class=listheading>
1848 |;
1849
1850   map { print "$column_header{$_}\n" } @column_index;
1851
1852   print qq|
1853         </tr>
1854 |;
1855
1856   foreach $ref (@{ $form->{ALL} }) {
1857
1858     $i++;
1859     $i %= 2;
1860
1861     print qq|
1862         <tr valign=top class=listrow$i>
1863 |;
1864
1865
1866     $column_data{printer_description} =
1867       qq|<td><a href=$form->{script}?action=edit_printer&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{printer_description}</td>|;
1868     $column_data{printer_command}           = qq|<td align=right>$ref->{printer_command}</td>|;
1869     $column_data{template_code} =
1870       qq|<td align=right>$ref->{template_code}</td>|;
1871
1872     map { print "$column_data{$_}\n" } @column_index;
1873
1874     print qq|
1875         </tr>
1876 |;
1877   }
1878
1879   print qq|
1880       </table>
1881     </td>
1882   </tr>
1883   <tr>
1884   <td><hr size=3 noshade></td>
1885   </tr>
1886 </table>
1887
1888 <br>
1889 <form method=post action=$form->{script}>
1890
1891 <input name=callback type=hidden value="$form->{callback}">
1892
1893 <input type=hidden name=type value=printer>
1894
1895 <input type=hidden name=login value=$form->{login}>
1896 <input type=hidden name=password value=$form->{password}>
1897
1898 <input class=submit type=submit name=action value="|
1899     . $locale->text('Add') . qq|">
1900
1901   </form>
1902
1903   </body>
1904   </html>
1905 |;
1906
1907   $lxdebug->leave_sub();
1908 }
1909
1910 sub printer_header {
1911   $lxdebug->enter_sub();
1912
1913   $form->{title}    = $locale->text("$form->{title} Printer");
1914
1915   # $locale->text('Add Printer')
1916   # $locale->text('Edit Printer')
1917
1918   $form->{printer_description} =~ s/\"/&quot;/g;
1919   $form->{template_code} =~ s/\"/&quot;/g;
1920   $form->{printer_command} =~ s/\"/&quot;/g;
1921
1922
1923   $form->header;
1924
1925   print qq|
1926 <body>
1927
1928 <form method=post action=$form->{script}>
1929
1930 <input type=hidden name=id value=$form->{id}>
1931 <input type=hidden name=type value=printer>
1932
1933 <table width=100%>
1934   <tr>
1935     <th class=listtop colspan=2>$form->{title}</th>
1936   </tr>
1937   <tr height="5"></tr>
1938   <tr>
1939     <th align=right>| . $locale->text('Printer') . qq|</th>
1940     <td><input name=printer_description size=30 value="$form->{printer_description}"></td>
1941   <tr>
1942   <tr>
1943     <th align=right>| . $locale->text('Printer Command') . qq|</th>
1944     <td><input name=printer_command size=30 value="$form->{printer_command}"></td>
1945   </tr>
1946   <tr>
1947     <th align=right>| . $locale->text('Template Code') . qq|</th>
1948     <td><input name=template_code size=5 value="$form->{template_code}"></td>
1949   </tr>
1950   <td colspan=2><hr size=3 noshade></td>
1951   </tr>
1952 </table>
1953 |;
1954
1955   $lxdebug->leave_sub();
1956 }
1957
1958 sub save_printer {
1959   $lxdebug->enter_sub();
1960
1961   $form->isblank("printer_description", $locale->text('Description missing!'));
1962   $form->isblank("printer_command", $locale->text('Printer Command missing!'));
1963   AM->save_printer(\%myconfig, \%$form);
1964   $form->redirect($locale->text('Printer saved!'));
1965
1966   $lxdebug->leave_sub();
1967 }
1968
1969 sub delete_printer {
1970   $lxdebug->enter_sub();
1971
1972   AM->delete_printer(\%myconfig, \%$form);
1973   $form->redirect($locale->text('Printer deleted!'));
1974
1975   $lxdebug->leave_sub();
1976 }
1977
1978 sub add_payment {
1979   $lxdebug->enter_sub();
1980
1981   $form->{title} = "Add";
1982
1983   $form->{callback} =
1984     "$form->{script}?action=add_payment&login=$form->{login}&password=$form->{password}"
1985     unless $form->{callback};
1986
1987   $form->{terms_netto} = 0;
1988   $form->{terms_skonto} = 0;
1989   $form->{percent_skonto} = 0;
1990   my @languages = AM->language(\%myconfig, $form, 1);
1991   map({ $_->{"language"} = $_->{"description"};
1992         $_->{"language_id"} = $_->{"id"}; } @languages);
1993   $form->{"TRANSLATION"} = \@languages;
1994   &payment_header;
1995   &form_footer;
1996
1997   $lxdebug->leave_sub();
1998 }
1999
2000 sub edit_payment {
2001   $lxdebug->enter_sub();
2002
2003   $form->{title} = "Edit";
2004
2005   AM->get_payment(\%myconfig, $form);
2006   $form->{percent_skonto} =
2007     $form->format_amount(\%myconfig, $form->{percent_skonto} * 100);
2008
2009   &payment_header;
2010
2011   $form->{orphaned} = 1;
2012   &form_footer;
2013
2014   $lxdebug->leave_sub();
2015 }
2016
2017 sub list_payment {
2018   $lxdebug->enter_sub();
2019
2020   AM->payment(\%myconfig, \%$form);
2021
2022   $form->{callback} = build_std_url("action=list_payment");
2023
2024   $callback = $form->escape($form->{callback});
2025
2026   $form->{title} = $locale->text('Payment Terms');
2027
2028   @column_index = qw(up down description description_long terms_netto
2029                      terms_skonto percent_skonto);
2030
2031   $column_header{up} =
2032       qq|<th class="listheading" align="center" valign="center" width="16">|
2033     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
2034     . qq|</th>|;
2035   $column_header{down} =
2036       qq|<th class="listheading" align="center" valign="center" width="16">|
2037     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
2038     . qq|</th>|;
2039   $column_header{description} =
2040       qq|<th class=listheading>|
2041     . $locale->text('Description')
2042     . qq|</th>|;
2043   $column_header{description_long} =
2044       qq|<th class=listheading>|
2045     . $locale->text('Long Description')
2046     . qq|</th>|;
2047   $column_header{terms_netto} =
2048       qq|<th class=listheading>|
2049     . $locale->text('Netto Terms')
2050     . qq|</th>|;
2051   $column_header{terms_skonto} =
2052       qq|<th class=listheading>|
2053     . $locale->text('Skonto Terms')
2054     . qq|</th>|;
2055   $column_header{percent_skonto} =
2056       qq|<th class=listheading>|
2057     . $locale->text('Skonto')
2058     . qq| %</th>|;
2059
2060   $form->header;
2061
2062   print qq|
2063 <body>
2064
2065 <table width=100%>
2066   <tr>
2067     <th class=listtop>$form->{title}</th>
2068   </tr>
2069   <tr height="5"></tr>
2070   <tr>
2071     <td>
2072       <table width=100%>
2073         <tr class=listheading>
2074 |;
2075
2076   map { print "$column_header{$_}\n" } @column_index;
2077
2078   print qq|
2079         </tr>
2080 |;
2081
2082   my $swap_link = build_std_url("action=swap_payment_terms");
2083
2084   my $row = 0;
2085   foreach $ref (@{ $form->{ALL} }) {
2086
2087     $i++;
2088     $i %= 2;
2089
2090     print qq|
2091         <tr valign=top class=listrow$i>
2092 |;
2093
2094     if ($row) {
2095       my $pref = $form->{ALL}->[$row - 1];
2096       $column_data{up} =
2097         qq|<td align="center" valign="center" width="16">| .
2098         qq|<a href="${swap_link}&id1=$ref->{id}&id2=$pref->{id}">| .
2099         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
2100         qq|</a></td>|;
2101     } else {
2102       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
2103     }
2104
2105     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
2106       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
2107     } else {
2108       my $nref = $form->{ALL}->[$row + 1];
2109       $column_data{down} =
2110         qq|<td align="center" valign="center" width="16">| .
2111         qq|<a href="${swap_link}&id1=$ref->{id}&id2=$nref->{id}">| .
2112         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
2113         qq|</a></td>|;
2114     }
2115
2116     $column_data{description} =
2117       qq|<td><a href="| .
2118       build_std_url("action=edit_payment", "id=$ref->{id}", "callback=$callback") .
2119       qq|">| . H($ref->{description}) . qq|</a></td>|;
2120     $column_data{description_long} =
2121       qq|<td>| . H($ref->{description_long}) . qq|</td>|;
2122     $column_data{terms_netto} =
2123       qq|<td align=right>$ref->{terms_netto}</td>|;
2124     $column_data{terms_skonto} =
2125       qq|<td align=right>$ref->{terms_skonto}</td>|;
2126     $column_data{percent_skonto} =
2127       qq|<td align=right>| .
2128       $form->format_amount(\%myconfig, $ref->{percent_skonto} * 100) .
2129       qq|%</td>|;
2130     map { print "$column_data{$_}\n" } @column_index;
2131
2132     print qq|
2133         </tr>
2134 |;
2135     $row++;
2136   }
2137
2138   print qq|
2139       </table>
2140     </td>
2141   </tr>
2142   <tr>
2143   <td><hr size=3 noshade></td>
2144   </tr>
2145 </table>
2146
2147 <br>
2148 <form method=post action=$form->{script}>
2149
2150 <input name=callback type=hidden value="$form->{callback}">
2151
2152 <input type=hidden name=type value=payment>
2153
2154 <input type=hidden name=login value=$form->{login}>
2155 <input type=hidden name=password value=$form->{password}>
2156
2157 <input class=submit type=submit name=action value="|
2158     . $locale->text('Add') . qq|">
2159
2160   </form>
2161
2162   </body>
2163   </html>
2164 |;
2165
2166   $lxdebug->leave_sub();
2167 }
2168
2169 sub payment_header {
2170   $lxdebug->enter_sub();
2171
2172   $form->{title}    = $locale->text("$form->{title} Payment Terms");
2173
2174   # $locale->text('Add Payment Terms')
2175   # $locale->text('Edit Payment Terms')
2176
2177   $form->{description} =~ s/\"/&quot;/g;
2178
2179
2180
2181   $form->header;
2182
2183   print qq|
2184 <body>
2185
2186 <form method=post action=$form->{script}>
2187
2188 <input type=hidden name=id value=$form->{id}>
2189 <input type=hidden name=type value=payment>
2190
2191 <table width=100%>
2192   <tr>
2193     <th class=listtop colspan=2>$form->{title}</th>
2194   </tr>
2195   <tr height="5"></tr>
2196   <tr>
2197     <th align=right>| . $locale->text('Description') . qq|</th>
2198     <td><input name=description size=30 value="$form->{description}"></td>
2199   <tr>
2200   <tr>
2201     <th align=right>| . $locale->text('Long Description') . qq|</th>
2202     <td><input name=description_long size=50 value="$form->{description_long}"></td>
2203   </tr>
2204 |;
2205
2206   foreach my $language (@{ $form->{"TRANSLATION"} }) {
2207     print qq|
2208   <tr>
2209     <th align="right">| .
2210     sprintf($locale->text('Translation (%s)'),
2211             $language->{"language"})
2212     . qq|</th>
2213     <td><input name="description_long_$language->{language_id}" size="50"
2214          value="| . Q($language->{"description_long"}) . qq|"></td>
2215   </tr>
2216 |;
2217   }
2218
2219   print qq|
2220   <tr>
2221     <th align=right>| . $locale->text('Netto Terms') . qq|</th>
2222     <td><input name=terms_netto size=10 value="$form->{terms_netto}"></td>
2223   </tr>
2224   <tr>
2225     <th align=right>| . $locale->text('Skonto Terms') . qq|</th>
2226     <td><input name=terms_skonto size=10 value="$form->{terms_skonto}"></td>
2227   </tr>  
2228   <tr>
2229     <th align=right>| . $locale->text('Skonto') . qq| %</th>
2230     <td><input name=percent_skonto size=10 value="$form->{percent_skonto}"></td>
2231   </tr> 
2232   <td colspan=2><hr size=3 noshade></td>
2233   </tr>
2234 </table>
2235
2236 <p>| . $locale->text("You can use the following strings in the long " .
2237                      "description and all translations. They will be " .
2238                      "replaced by their actual values by Lx-Office " .
2239                      "before they're output.")
2240 . qq|</p>
2241
2242 <ul>
2243   <li>| . $locale->text("&lt;%netto_date%&gt; -- Date the payment is due in " .
2244                         "full")
2245 . qq|</li>
2246   <li>| . $locale->text("&lt;%skonto_date%&gt; -- Date the payment is due " .
2247                         "with discount")
2248 . qq|</li>
2249   <li>| . $locale->text("&lt;%skonto_amount%&gt; -- The deductible amount")
2250 . qq|</li>
2251   <li>| . $locale->text("&lt;%total%&gt; -- Amount payable")
2252 . qq|</li>
2253   <li>| . $locale->text("&lt;%total_wo_skonto%&gt; -- Amount payable less discount")
2254 . qq|</li>
2255   <li>| . $locale->text("&lt;%invtotal%&gt; -- Invoice total")
2256 . qq|</li>
2257   <li>| . $locale->text("&lt;%invtotal_wo_skonto%&gt; -- Invoice total less discount")
2258 . qq|</li>
2259   <li>| . $locale->text("&lt;%currency%&gt; -- The selected currency")
2260 . qq|</li>
2261   <li>| . $locale->text("&lt;%terms_netto%&gt; -- The number of days for " .
2262                         "full payment")
2263 . qq|</li>
2264   <li>| . $locale->text("&lt;%account_number%&gt; -- Your account number")
2265 . qq|</li>
2266   <li>| . $locale->text("&lt;%bank%&gt; -- Your bank")
2267 . qq|</li>
2268   <li>| . $locale->text("&lt;%bank_code%&gt; -- Your bank code")
2269 . qq|</li>
2270 </ul>|;
2271
2272   $lxdebug->leave_sub();
2273 }
2274
2275 sub save_payment {
2276   $lxdebug->enter_sub();
2277
2278   $form->isblank("description", $locale->text('Description missing!'));
2279   $form->{"percent_skonto"} =
2280     $form->parse_amount(\%myconfig, $form->{percent_skonto}) / 100;
2281   AM->save_payment(\%myconfig, \%$form);
2282   $form->redirect($locale->text('Payment Terms saved!'));
2283
2284   $lxdebug->leave_sub();
2285 }
2286
2287 sub delete_payment {
2288   $lxdebug->enter_sub();
2289
2290   AM->delete_payment(\%myconfig, \%$form);
2291   $form->redirect($locale->text('Payment terms deleted!'));
2292
2293   $lxdebug->leave_sub();
2294 }
2295
2296 sub swap_payment_terms {
2297   $lxdebug->enter_sub();
2298
2299   AM->swap_sortkeys(\%myconfig, $form, "payment_terms");
2300   list_payment();
2301
2302   $lxdebug->leave_sub();
2303 }
2304
2305 sub edit_defaults {
2306   $lxdebug->enter_sub();
2307
2308   # get defaults for account numbers and last numbers
2309   AM->defaultaccounts(\%myconfig, \%$form);
2310
2311   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
2312
2313   foreach $key (keys %{ $form->{IC} }) {
2314     foreach $accno (sort keys %{ $form->{IC}->{$key} }) {
2315       my $array = "ACCNOS_" . uc($key);
2316       $form->{$array} ||= [];
2317
2318       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
2319       push @{ $form->{$array} }, {
2320         'name'     => $value,
2321         'value'    => $value,
2322         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
2323       };
2324     }
2325   }
2326
2327   $form->{title} = $locale->text('Ranges of numbers and default accounts');
2328
2329   $form->header();
2330   print $form->parse_html_template('am/edit_defaults');
2331
2332   $lxdebug->leave_sub();
2333 }
2334
2335 sub save_defaults {
2336   $lxdebug->enter_sub();
2337
2338   AM->save_defaults();
2339
2340   $form->redirect($locale->text('Defaults saved.'));
2341
2342   $lxdebug->leave_sub();
2343 }
2344
2345 sub _build_cfg_options {
2346   my $idx   = shift;
2347   my $array = uc($idx) . 'S';
2348
2349   $form->{$array} = [];
2350   foreach my $item (@_) {
2351     push @{ $form->{$array} }, {
2352       'name'     => $item,
2353       'value'    => $item,
2354       'selected' => $item eq $myconfig{$idx},
2355     };
2356   }
2357 }
2358
2359 sub config {
2360   $lxdebug->enter_sub();
2361
2362   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
2363   _build_cfg_options('numberformat', qw(1,000.00 1000.00 1.000,00 1000,00));
2364
2365   @formats = ();
2366   if ($opendocument_templates && $openofficeorg_writer_bin &&
2367       $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)) {
2368     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
2369                      "value" => "opendocument_pdf" });
2370   }
2371   if ($latex_templates) {
2372     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
2373   }
2374   push(@formats, { "name" => "HTML", "value" => "html" });
2375   if ($latex_templates) {
2376     push(@formats, { "name" => $locale->text("Postscript"),
2377                      "value" => "postscript" });
2378   }
2379   if ($opendocument_templates) {
2380     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
2381                      "value" => "opendocument" });
2382   }
2383
2384   if (!$myconfig{"template_format"}) {
2385     $myconfig{"template_format"} = "pdf";
2386   }
2387   $form->{TEMPLATE_FORMATS} = [];
2388   foreach $item (@formats) {
2389     push @{ $form->{TEMPLATE_FORMATS} }, {
2390       'name'     => $item->{name},
2391       'value'    => $item->{value},
2392       'selected' => $item->{value} eq $myconfig{template_format},
2393     };
2394   }
2395
2396   if (!$myconfig{"default_media"}) {
2397     $myconfig{"default_media"} = "screen";
2398   }
2399
2400   my %selected = ($myconfig{"default_media"} => "selected");
2401   $form->{MEDIA} = [
2402     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
2403     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
2404     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
2405     ];
2406
2407   AM->printer(\%myconfig, $form);
2408
2409   $form->{PRINTERS} = [];
2410   foreach my $printer (@{$form->{"ALL"}}) {
2411     push @{ $form->{PRINTERS} }, {
2412       'name'     => $printer->{printer_description},
2413       'value'    => $printer->{id},
2414       'selected' => $printer->{id} == $myconfig{default_printer_id},
2415     };
2416   }
2417
2418   %countrycodes = User->country_codes;
2419
2420   $countrycodes{""} = "American English";
2421   $form->{COUNTRYCODES} = [];
2422   foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
2423     push @{ $form->{COUNTRYCODES} }, {
2424       'name'     => $countrycodes{$countrycode},
2425       'value'    => $countrycode,
2426       'selected' => $countrycode eq $myconfig{countrycode},
2427     };
2428   }
2429
2430   $form->{STYLESHEETS} = [];
2431   foreach $item (qw(lx-office-erp.css Win2000.css)) {
2432     push @{ $form->{STYLESHEETS} }, {
2433       'name'     => $item,
2434       'value'    => $item,
2435       'selected' => $item eq $myconfig{stylesheet},
2436     };
2437   }
2438
2439   $myconfig{show_form_details}              = 1 unless (defined($myconfig{show_form_details}));
2440   $form->{"menustyle_$myconfig{menustyle}"} = 1;
2441
2442   $form->{title}                            = $locale->text('Edit Preferences for #1', $form->{login});
2443
2444   $form->header();
2445   print $form->parse_html_template('am/config');
2446
2447   $lxdebug->leave_sub();
2448 }
2449
2450 sub save_preferences {
2451   $lxdebug->enter_sub();
2452
2453   $form->{stylesheet} = $form->{usestylesheet};
2454
2455   $form->redirect($locale->text('Preferences saved!'))
2456     if (
2457      AM->save_preferences(\%myconfig, \%$form, $memberfile, $userspath, $webdav
2458      ));
2459   $form->error($locale->text('Cannot save preferences!'));
2460
2461   $lxdebug->leave_sub();
2462 }
2463
2464 sub audit_control {
2465   $lxdebug->enter_sub();
2466
2467   $form->{title} = $locale->text('Audit Control');
2468
2469   AM->closedto(\%myconfig, \%$form);
2470
2471   if ($form->{revtrans}) {
2472     $checked{Y} = "checked";
2473   } else {
2474     $checked{N} = "checked";
2475   }
2476
2477   $form->header;
2478
2479   print qq|
2480 <body>
2481
2482 <form method=post action=$form->{script}>
2483
2484 <input type=hidden name=login value=$form->{login}>
2485 <input type=hidden name=password value=$form->{password}>
2486
2487 <table width=100%>
2488   <tr><th class=listtop>$form->{title}</th></tr>
2489   <tr height="5"></tr>
2490   <tr>
2491     <td>
2492       <table>
2493         <tr>
2494           <td>|
2495     . $locale->text('Enforce transaction reversal for all dates') . qq|</th>
2496           <td><input name=revtrans class=radio type=radio value="1" $checked{Y}> |
2497     . $locale->text('Yes')
2498     . qq| <input name=revtrans class=radio type=radio value="0" $checked{N}> |
2499     . $locale->text('No')
2500     . qq|</td>
2501         </tr>
2502         <tr>
2503           <th>| . $locale->text('Close Books up to') . qq|</th>
2504           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
2505         </tr>
2506       </table>
2507     </td>
2508   </tr>
2509 </table>
2510
2511 <hr size=3 noshade>
2512
2513 <br>
2514 <input type=hidden name=nextsub value=doclose>
2515
2516 <input type=submit class=submit name=action value="|
2517     . $locale->text('Continue') . qq|">
2518
2519 </form>
2520
2521 </body>
2522 </html>
2523 |;
2524
2525   $lxdebug->leave_sub();
2526 }
2527
2528 sub doclose {
2529   $lxdebug->enter_sub();
2530
2531   AM->closebooks(\%myconfig, \%$form);
2532
2533   if ($form->{revtrans}) {
2534     $form->redirect(
2535                  $locale->text('Transaction reversal enforced for all dates'));
2536   } else {
2537     if ($form->{closedto}) {
2538       $form->redirect(
2539                      $locale->text('Transaction reversal enforced up to') . " "
2540                        . $locale->date(\%myconfig, $form->{closedto}, 1));
2541     } else {
2542       $form->redirect($locale->text('Books are open'));
2543     }
2544   }
2545
2546   $lxdebug->leave_sub();
2547 }
2548
2549 sub edit_units {
2550   $lxdebug->enter_sub();
2551
2552   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"}, "resolved_");
2553   AM->units_in_use(\%myconfig, $form, $units);
2554   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
2555
2556   @languages = AM->language(\%myconfig, $form, 1);
2557
2558   @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
2559
2560   my $i = 1;
2561   foreach (@unit_list) {
2562     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
2563     $_->{"UNITLANGUAGES"} = [];
2564     foreach my $lang (@languages) {
2565       push(@{ $_->{"UNITLANGUAGES"} },
2566            { "idx" => $i,
2567              "unit" => $_->{"name"},
2568              "language_id" => $lang->{"id"},
2569              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
2570              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
2571            });
2572     }
2573     $i++;
2574   }
2575
2576   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
2577   $ddbox = AM->unit_select_data($units, undef, 1);
2578
2579   my $updownlink = build_std_url("action=swap_units", "unit_type");
2580
2581   $form->{"title"} = sprintf($locale->text("Add and edit %s"), $form->{"unit_type"} eq "dimension" ? $locale->text("dimension units") : $locale->text("service units"));
2582   $form->header();
2583   print($form->parse_html_template("am/edit_units",
2584                                    { "UNITS"               => \@unit_list,
2585                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
2586                                      "LANGUAGES"           => \@languages,
2587                                      "updownlink"          => $updownlink }));
2588
2589   $lxdebug->leave_sub();
2590 }
2591
2592 sub add_unit {
2593   $lxdebug->enter_sub();
2594
2595   $form->isblank("new_name", $locale->text("The name is missing."));
2596   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
2597   $all_units = AM->retrieve_units(\%myconfig, $form);
2598   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
2599
2600   my ($base_unit, $factor);
2601   if ($form->{"new_base_unit"}) {
2602     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
2603
2604     $form->isblank("new_factor", $locale->text("The factor is missing."));
2605     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
2606     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
2607     $base_unit = $form->{"new_base_unit"};
2608   }
2609
2610   my @languages;
2611   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
2612     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
2613     push(@languages, { "id" => $lang->{"id"},
2614                        "localized" => $form->{"new_localized_$lang->{id}"},
2615                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
2616          });
2617   }
2618
2619   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, $form->{"unit_type"}, \@languages);
2620
2621   $form->{"saved_message"} = $locale->text("The unit has been saved.");
2622
2623   edit_units();
2624
2625   $lxdebug->leave_sub();
2626 }
2627
2628 sub set_unit_languages {
2629   $lxdebug->enter_sub();
2630
2631   my ($unit, $languages, $idx) = @_;
2632
2633   $unit->{"LANGUAGES"} = [];
2634
2635   foreach my $lang (@{$languages}) {
2636     push(@{ $unit->{"LANGUAGES"} },
2637          { "id" => $lang->{"id"},
2638            "localized" => $form->{"localized_${idx}_$lang->{id}"},
2639            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
2640          });
2641   }
2642
2643   $lxdebug->leave_sub();
2644 }
2645
2646 sub save_unit {
2647   $lxdebug->enter_sub();
2648
2649   $old_units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"}, "resolved_");
2650   AM->units_in_use(\%myconfig, $form, $old_units);
2651
2652   @languages = AM->language(\%myconfig, $form, 1);
2653
2654   $new_units = {};
2655   @delete_units = ();
2656   foreach $i (1..($form->{"rowcount"} * 1)) {
2657     $old_unit = $old_units->{$form->{"old_name_$i"}};
2658     if (!$old_unit) {
2659       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
2660     }
2661
2662     if ($form->{"unchangeable_$i"}) {
2663       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
2664       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
2665       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
2666       next;
2667     }
2668
2669     if ($old_unit->{"in_use"}) {
2670       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
2671     }
2672
2673     if ($form->{"delete_$i"}) {
2674       push(@delete_units, $old_unit->{"name"});
2675       next;
2676     }
2677
2678     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
2679
2680     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
2681     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
2682     $new_units->{$form->{"name_$i"}} = \%h;
2683     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
2684     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
2685   }
2686
2687   foreach $unit (values(%{$new_units})) {
2688     next unless ($unit->{"old_name"});
2689     if ($unit->{"base_unit"}) {
2690       $form->show_generic_error(sprintf($locale->text("The base unit does not exist or it is about to be deleted in row %d."), $unit->{"row"}))
2691         unless (defined($new_units->{$unit->{"base_unit"}}));
2692       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
2693       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
2694     } else {
2695       $unit->{"base_unit"} = undef;
2696       $unit->{"factor"} = undef;
2697     }
2698   }
2699
2700   foreach $unit (values(%{$new_units})) {
2701     next if ($unit->{"unchanged_unit"});
2702
2703     map({ $_->{"seen"} = 0; } values(%{$new_units}));
2704     $new_unit = $unit;
2705     while ($new_unit->{"base_unit"}) {
2706       $new_unit->{"seen"} = 1;
2707       $new_unit = $new_units->{$new_unit->{"base_unit"}};
2708       if ($new_unit->{"seen"}) {
2709         $form->show_generic_error(sprintf($locale->text("The base unit relations must not contain loops (e.g. by saying that unit A's base unit is B, " .
2710                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
2711       }
2712     }
2713   }
2714
2715   AM->save_units(\%myconfig, $form, $form->{"unit_type"}, $new_units, \@delete_units);
2716
2717   $form->{"saved_message"} = $locale->text("The units have been saved.");
2718
2719   edit_units();
2720
2721   $lxdebug->leave_sub();
2722 }
2723
2724 sub show_history_search {
2725         $lxdebug->enter_sub();
2726         
2727         $form->{title} = $locale->text("History Search");
2728     $form->header();
2729     
2730     print $form->parse_html_template("common/search_history");
2731         
2732         $lxdebug->leave_sub();
2733 }
2734
2735 sub show_am_history {
2736         $lxdebug->enter_sub();
2737         my %search = ( "Artikelnummer" => "parts",
2738                                    "Kundennummer"  => "customer",
2739                                    "Lieferantennummer" => "vendor",
2740                                    "Projektnummer" => "project",
2741                                    "Buchungsnummer" => "oe",
2742                                    "Eingangsrechnungnummer" => "ap",
2743                                    "Ausgangsrechnungnummer" => "ar",
2744            "Mahnungsnummer" => "dunning"
2745                 );
2746         my %searchNo = ( "Artikelnummer" => "partnumber",
2747                                      "Kundennummer"  => "customernumber",
2748                                      "Lieferantennummer" => "vendornumber",
2749                                      "Projektnummer" => "projectnummer",
2750                                      "Buchungsnummer" => "ordnumber",
2751                                      "Eingangsrechnungnummer" => "invnumber",
2752                                      "Ausgangsrechnungnummer" => "invnumber",
2753              "Mahnungsnummer" => "dunning_id"
2754                 );
2755         
2756         my $restriction;
2757         my $tempNo = 0;
2758         foreach(split(/\,/, $form->{einschraenkungen})) {
2759                 if($tempNo == 0) {
2760                         $restriction .= " AND addition = '" . $_ . "'";
2761                         $tempNo = 1;
2762                 } 
2763                 else {
2764                         $restriction .= " OR addition = '" . $_ . "'";
2765                 }
2766         }
2767         $restriction .= (($form->{transdate} ne "" && $form->{reqdate} ne "") 
2768                                                 ? qq| AND st.itime::date >= '| . $form->{transdate} . qq|' AND st.itime::date <= '| . $form->{reqdate} . qq|'|
2769                                                 : (($form->{transdate} ne "" && $form->{reqdate} eq "") 
2770                                                         ? qq| AND st.itime::date >= '| . $form->{transdate} . qq|'|
2771                                                         : ($form->{transdate} eq "" && $form->{reqdate} ne "") 
2772                                                                 ? qq| AND st.itime::date <= '| . $form->{reqdate} . qq|'|
2773                                                                 : ""
2774                                                         )
2775                                                 );
2776   $restriction .= ($form->{mitarbeiter} eq "" ? "" 
2777           : ($form->{mitarbeiter} =~ /^[0-9]*$/  
2778             ? " AND employee_id = " . $form->{mitarbeiter} 
2779             : " AND employee_id = " . &get_employee_id($form->{mitarbeiter}, $dbh)));
2780   
2781         my $dbh = $form->dbconnect(\%myconfig);
2782         my $query = qq|SELECT trans_id AS id FROM history_erp | . 
2783                 ($form->{'searchid'} ? 
2784                   qq| WHERE snumbers = '| . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'| : 
2785                   qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
2786
2787   my $sth = $dbh->prepare($query);
2788         
2789         $sth->execute() || $form->dberror($query);
2790   
2791   $form->{title} = $locale->text("History Search");
2792         $form->header();
2793         
2794   my $i = 1;
2795   my $daten = qq||;
2796   while(my $hash_ref = $sth->fetchrow_hashref()){
2797     if($i) {
2798       $daten .= $hash_ref->{id};
2799       $i = 0;
2800     }
2801     else {
2802       $daten .= " OR trans_id = " . $hash_ref->{id};
2803     }
2804   }
2805   
2806   my ($sort, $sortby) = split(/\-\-/, $form->{order});
2807   $sort =~ s/.*\.(.*)$/$1/;
2808
2809         print $form->parse_html_template("common/show_history", 
2810     {"DATEN" => $form->get_history($dbh, $daten, $restriction, $form->{order}),
2811      "SUCCESS" => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
2812      "NONEWWINDOW" => 1,
2813      uc($sort) => 1,
2814      uc($sort)."BY" => $sortby
2815     });
2816         $dbh->disconnect();
2817   $lxdebug->leave_sub();
2818 }
2819
2820 sub get_employee_id {
2821         $lxdebug->enter_sub();
2822         my $query = qq|SELECT id FROM employee WHERE name = '| . $_[0] . qq|'|;
2823         my $sth = $_[1]->prepare($query);
2824         $sth->execute() || $form->dberror($query);
2825         my $return = $sth->fetch();
2826         $sth->finish();
2827         return ${$return}[0];
2828         $lxdebug->leave_sub();
2829 }
2830
2831 sub swap_units {
2832   $lxdebug->enter_sub();
2833
2834   my $dir = $form->{"dir"} eq "down" ? "down" : "up";
2835   my $unit_type = $form->{"unit_type"} eq "dimension" ?
2836     "dimension" : "service";
2837   AM->swap_units(\%myconfig, $form, $dir, $form->{"name"}, $unit_type);
2838
2839   edit_units();
2840
2841   $lxdebug->leave_sub();
2842 }
2843
2844 sub add_tax {
2845   $lxdebug->enter_sub();
2846
2847   $form->{title} =  $locale->text('Add');
2848
2849   $form->{callback} =
2850     "$form->{script}?action=add_tax&login=$form->{login}&password=$form->{password}"
2851     unless $form->{callback};
2852
2853   _get_taxaccount_selection();
2854
2855   $form->header();
2856   
2857   my $parameters_ref = {
2858 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
2859   };
2860   
2861   # Ausgabe des Templates
2862   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2863
2864   $lxdebug->leave_sub();
2865 }
2866
2867 sub edit_tax {
2868   $lxdebug->enter_sub();
2869
2870   $form->{title} =  $locale->text('Edit');
2871
2872   AM->get_tax(\%myconfig, \%$form);
2873   _get_taxaccount_selection();
2874
2875   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
2876
2877   $form->header();
2878   
2879   my $parameters_ref = {
2880   };
2881   
2882   # Ausgabe des Templates
2883   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2884
2885   $lxdebug->leave_sub();
2886 }
2887
2888 sub list_tax {
2889   $lxdebug->enter_sub();
2890
2891   AM->taxes(\%myconfig, \%$form);
2892
2893   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
2894
2895   $form->{callback} = build_std_url('action=list_tax');
2896
2897   $form->{title} = $locale->text('Tax-O-Matic');
2898
2899   $form->header();
2900   
2901   # Ausgabe des Templates
2902   print($form->parse_html_template('am/list_tax', $parameters_ref));
2903
2904   $lxdebug->leave_sub();
2905 }
2906
2907 sub _get_taxaccount_selection{
2908   $lxdebug->enter_sub();
2909
2910   AM->get_tax_accounts(\%myconfig, \%$form);
2911
2912   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
2913
2914   $lxdebug->leave_sub();
2915 }
2916
2917 sub save_tax {
2918   $lxdebug->enter_sub();
2919
2920   $form->isblank("rate", $locale->text('Taxrate missing!'));
2921   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
2922   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
2923
2924   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
2925
2926   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
2927     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
2928   }
2929
2930   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
2931     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
2932   }  
2933
2934   AM->save_tax(\%myconfig, \%$form);
2935   $form->redirect($locale->text('Tax saved!'));
2936
2937   $lxdebug->leave_sub();
2938 }
2939
2940 sub delete_tax {
2941   $lxdebug->enter_sub();
2942
2943   AM->delete_tax(\%myconfig, \%$form);
2944   $form->redirect($locale->text('Tax deleted!'));
2945
2946   $lxdebug->leave_sub();
2947 }
2948
2949 sub add_price_factor {
2950   $lxdebug->enter_sub();
2951
2952   $form->{title}      = $locale->text('Add Price Factor');
2953   $form->{callback} ||= build_std_url('action=add_price_factor');
2954   $form->{fokus}      = 'description';
2955
2956   $form->header();
2957   print $form->parse_html_template('am/edit_price_factor');
2958
2959   $lxdebug->leave_sub();
2960 }
2961
2962 sub edit_price_factor {
2963   $lxdebug->enter_sub();
2964
2965   $form->{title}      = $locale->text('Edit Price Factor');
2966   $form->{callback} ||= build_std_url('action=add_price_factor');
2967   $form->{fokus}      = 'description';
2968
2969   AM->get_price_factor(\%myconfig, $form);
2970
2971   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
2972
2973   $form->header();
2974   print $form->parse_html_template('am/edit_price_factor');
2975
2976   $lxdebug->leave_sub();
2977 }
2978
2979 sub list_price_factors {
2980   $lxdebug->enter_sub();
2981
2982   AM->get_all_price_factors(\%myconfig, \%$form);
2983
2984   my $previous;
2985   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
2986     if ($previous) {
2987       $previous->{next_id}    = $current->{id};
2988       $current->{previous_id} = $previous->{id};
2989     }
2990
2991     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
2992
2993     $previous = $current;
2994   }
2995
2996   $form->{callback} = build_std_url('action=list_price_factors');
2997   $form->{title}    = $locale->text('Price Factors');
2998   $form->{url_base} = build_std_url('callback');
2999
3000   $form->header();
3001   print $form->parse_html_template('am/list_price_factors');
3002
3003   $lxdebug->leave_sub();
3004 }
3005
3006 sub save_price_factor {
3007   $lxdebug->enter_sub();
3008
3009   $form->isblank("description", $locale->text('Description missing!'));
3010   $form->isblank("factor", $locale->text('Factor missing!'));
3011
3012   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
3013
3014   AM->save_price_factor(\%myconfig, $form);
3015
3016   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
3017
3018   $form->redirect($locale->text('Price factor saved!'));
3019
3020   $lxdebug->leave_sub();
3021 }
3022
3023 sub delete_price_factor {
3024   $lxdebug->enter_sub();
3025
3026   AM->delete_price_factor(\%myconfig, \%$form);
3027
3028   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
3029
3030   $form->redirect($locale->text('Price factor deleted!'));
3031
3032   $lxdebug->leave_sub();
3033 }
3034
3035 sub swap_price_factors {
3036   $lxdebug->enter_sub();
3037
3038   AM->swap_sortkeys(\%myconfig, $form, 'price_factors');
3039   list_price_factors();
3040
3041   $lxdebug->leave_sub();
3042 }
3043