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