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