Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[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 utf8;
35
36 use SL::Auth;
37 use SL::Auth::PasswordPolicy;
38 use SL::AM;
39 use SL::CA;
40 use SL::Form;
41 use SL::User;
42 use SL::USTVA;
43 use SL::Iconv;
44 use SL::TODO;
45 use SL::Printer;
46 use CGI;
47
48 require "bin/mozilla/common.pl";
49
50 use strict;
51
52 1;
53
54 # end of main
55
56 sub add      { call_sub("add_$main::form->{type}"); }
57 sub delete   { call_sub("delete_$main::form->{type}"); }
58 sub save     { call_sub("save_$main::form->{type}"); }
59 sub edit     { call_sub("edit_$main::form->{type}"); }
60 sub continue { call_sub($main::form->{"nextsub"}); }
61 sub save_as_new { call_sub("save_as_new_$main::form->{type}"); }
62
63 sub add_account {
64   $main::lxdebug->enter_sub();
65
66   my $form     = $main::form;
67   my %myconfig = %main::myconfig;
68
69   $main::auth->assert('config');
70
71   $form->{title}     = "Add";
72   $form->{charttype} = "A";
73   AM->get_account(\%myconfig, \%$form);
74
75   $form->{callback} = "am.pl?action=list_account" unless $form->{callback};
76
77   &account_header;
78   &form_footer;
79
80   $main::lxdebug->leave_sub();
81 }
82
83 sub edit_account {
84   $main::lxdebug->enter_sub();
85
86   my $form     = $main::form;
87   my %myconfig = %main::myconfig;
88
89   $main::auth->assert('config');
90
91   $form->{title} = "Edit";
92   AM->get_account(\%myconfig, \%$form);
93
94   foreach my $item (split(/:/, $form->{link})) {
95     $form->{$item} = "checked";
96   }
97
98   &account_header;
99   &form_footer;
100
101   $main::lxdebug->leave_sub();
102 }
103
104 sub account_header {
105   $main::lxdebug->enter_sub();
106
107   my $form     = $main::form;
108   my %myconfig = %main::myconfig;
109   my $locale   = $main::locale;
110
111   $main::auth->assert('config');
112
113   if ( $form->{action} eq 'edit_account') {
114     $form->{account_exists} = '1';
115   }
116
117   $form->{title} = $locale->text("$form->{title} Account");
118
119   $form->{"$form->{charttype}_checked"} = "checked";
120   $form->{"$form->{category}_checked"}  = "checked";
121
122   $form->{select_tax} = "";
123
124   my @tax_report_pos = USTVA->report_variables({
125       myconfig   => \%myconfig,
126       form       => $form,
127       type       => '',
128       attribute  => 'position',
129       calc       => '',
130   });
131
132   if (@{ $form->{TAXKEY} }) {
133     foreach my $item (@{ $form->{TAXKEY} }) {
134       $item->{rate} = $item->{rate} * 100 . '%';
135     }
136
137     # Fill in empty row for new Taxkey
138     my $newtaxkey_ref = {
139       id             => '',
140       chart_id       => '',
141       accno          => '',
142       tax_id         => '',
143       taxdescription => '',
144       rate           => '',
145       taxkey_id      => '',
146       pos_ustva      => '',
147       startdate      => '',
148     };
149
150     push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
151
152     my $i = 0;
153     foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
154
155       # Fill in a runningnumber
156       $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
157
158       # Fill in the Taxkeys as select options
159       foreach my $item (@{ $form->{TAXKEY} }) {
160         if ($item->{id} == $taxkey_used->{tax_id}) {
161           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
162             qq|<option value="$item->{id}" selected="selected">|
163             . sprintf("%.2d", $item->{taxkey})
164             . qq|. $item->{taxdescription} ($item->{rate}) |
165             . $locale->text('Tax-o-matic Account')
166             . qq|: $item->{chart_accno}\n|;
167         }
168         else {
169           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
170             qq|<option value="$item->{id}">|
171             . sprintf("%.2d", $item->{taxkey})
172             . qq|. $item->{taxdescription} ($item->{rate}) |
173             . $locale->text('Tax-o-matic Account')
174             . qq|: $item->{chart_accno}\n|;
175         }
176
177       }
178
179       # Fill in the USTVA Numbers as select options
180       foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
181         if ($item eq ''){
182           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
183         }
184         elsif ( $item eq $taxkey_used->{pos_ustva} ) {
185           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
186         }
187         else {
188           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
189         }
190
191       }
192
193       $i++;
194     }
195   }
196
197   # Newaccount Folgekonto
198   if (@{ $form->{NEWACCOUNT} || [] }) {
199     if (!$form->{new_chart_valid}) {
200       $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
201     }
202     foreach my $item (@{ $form->{NEWACCOUNT} }) {
203       if ($item->{id} == $form->{new_chart_id}) {
204         $form->{selectnewaccount} .=
205           qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
206       } elsif (!$form->{new_chart_valid}) {
207         $form->{selectnewaccount} .=
208           qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
209       }
210
211     }
212   }
213
214   my $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
215   my %eur = (
216           1  => "Umsatzerlöse",
217           2  => "sonstige Erlöse",
218           3  => "Privatanteile",
219           4  => "Zinserträge",
220           5  => "Ausserordentliche Erträge",
221           6  => "Vereinnahmte Umsatzst.",
222           7  => "Umsatzsteuererstattungen",
223           8  => "Wareneingänge",
224           9  => "Löhne und Gehälter",
225           10 => "Gesetzl. sozialer Aufw.",
226           11 => "Mieten",
227           12 => "Gas, Strom, Wasser",
228           13 => "Instandhaltung",
229           14 => "Steuern, Versich., Beiträge",
230           15 => "Kfz-Steuern",
231           16 => "Kfz-Versicherungen",
232           17 => "Sonst. Fahrzeugkosten",
233           18 => "Werbe- und Reisekosten",
234           19 => "Instandhaltung u. Werkzeuge",
235           20 => "Fachzeitschriften, Bücher",
236           21 => "Miete für Einrichtungen",
237           22 => "Rechts- und Beratungskosten",
238           23 => "Bürobedarf, Porto, Telefon",
239           24 => "Sonstige Aufwendungen",
240           25 => "Abschreibungen auf Anlagever.",
241           26 => "Abschreibungen auf GWG",
242           27 => "Vorsteuer",
243           28 => "Umsatzsteuerzahlungen",
244           29 => "Zinsaufwand",
245           30 => "Ausserordentlicher Aufwand",
246           31 => "Betriebliche Steuern");
247   foreach my $item (sort({ $a <=> $b } keys(%eur))) {
248     my $text = H($::locale->{iconv_utf8}->convert($eur{$item}));
249     if ($item == $form->{pos_eur}) {
250       $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
251     } else {
252       $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
253     }
254
255   }
256
257   my $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
258
259   my %bwapos = (
260              1  => 'Umsatzerlöse',
261              2  => 'Best.Verdg.FE/UE',
262              3  => 'Aktiv.Eigenleistung',
263              4  => 'Mat./Wareneinkauf',
264              5  => 'So.betr.Erlöse',
265              10 => 'Personalkosten',
266              11 => 'Raumkosten',
267              12 => 'Betriebl.Steuern',
268              13 => 'Vers./Beiträge',
269              14 => 'Kfz.Kosten o.St.',
270              15 => 'Werbe-Reisek.',
271              16 => 'Kosten Warenabgabe',
272              17 => 'Abschreibungen',
273              18 => 'Rep./instandhlt.',
274              19 => 'Übrige Steuern',
275              20 => 'Sonst.Kosten',
276              30 => 'Zinsauwand',
277              31 => 'Sonst.neutr.Aufw.',
278              32 => 'Zinserträge',
279              33 => 'Sonst.neutr.Ertrag',
280              34 => 'Verr.kalk.Kosten',
281              35 => 'Steuern Eink.u.Ertr.');
282   foreach my $item (sort({ $a <=> $b } keys %bwapos)) {
283     my $text = H($::locale->{iconv_utf8}->convert($bwapos{$item}));
284     if ($item == $form->{pos_bwa}) {
285       $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
286     } else {
287       $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
288     }
289
290   }
291
292 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
293   my $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
294   foreach my $item ((1, 2, 3, 4)) {
295     if ($item == $form->{pos_bilanz}) {
296       $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
297     } else {
298       $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
299     }
300
301   }
302
303   # this is for our parser only! Do not remove.
304   # type=submit $locale->text('Add Account')
305   # type=submit $locale->text('Edit Account')
306
307   $form->{type} = "account";
308
309   # preselections category
310
311   my $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
312
313   my %category = (
314       'A'  => $locale->text('Asset'),
315       'L'  => $locale->text('Liability'),
316       'Q'  => $locale->text('Equity'),
317       'I'  => $locale->text('Revenue'),
318       'E'  => $locale->text('Expense'),
319       'C'  => $locale->text('Costs'),
320   );
321   foreach my $item ( sort({ $a <=> $b } keys %category) ) {
322     if ($item eq $form->{category}) {
323       $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
324     } else {
325       $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
326     }
327
328   }
329
330   # preselection chart type
331   my $select_charttype = q{};
332
333   my %charttype = (
334       'A'  => $locale->text('Account'),
335       'H'  => $locale->text('Heading'),
336   );
337
338   foreach my $item ( sort({ $a <=> $b } keys %charttype) ) {
339     if ($item eq $form->{charttype}) {
340       $select_charttype .= qq|<option value="$item" selected="selected">$charttype{$item}\n|;
341
342     } else {
343       $select_charttype .= qq|<option value="$item">$charttype{$item}\n|;
344     }
345
346   }
347
348   my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
349   my $AccountIsPosted = ($form->{orphaned} ) ? "":"1";
350
351   $form->header();
352
353   my $parameters_ref = {
354     ChartTypeIsAccount         => $ChartTypeIsAccount,
355     AccountIsPosted            => $AccountIsPosted,
356     select_category            => $select_category,
357     select_charttype           => $select_charttype,
358     select_bwa                 => $select_bwa,
359     select_bilanz              => $select_bilanz,
360     select_eur                 => $select_eur,
361   };
362
363   # Ausgabe des Templates
364   print($form->parse_html_template('am/edit_accounts', $parameters_ref));
365
366
367   $main::lxdebug->leave_sub();
368 }
369
370 sub form_footer {
371   $main::lxdebug->enter_sub();
372
373   my $form     = $main::form;
374   my $locale   = $main::locale;
375
376   $main::auth->assert('config');
377
378   print qq|
379
380 <input name=callback type=hidden value="| . H($form->{callback}) . qq|">
381
382 <br>|;
383   if ((!$form->{id}) || ($form->{id} && $form->{orphaned}) || (($form->{type} eq "account") && (!$form->{new_chart_valid}))) {
384     print qq|
385 <input type=submit class=submit name=action value="|
386     . $locale->text('Save') . qq|">
387 |;
388 }
389
390   if ($form->{id} && $form->{orphaned}) {
391     print qq|<input type=submit class=submit name=action value="|
392       . $locale->text('Delete') . qq|">|;
393   }
394
395   if ($form->{id} && $form->{type} eq "account") {
396     print qq|
397     <input class=submit type=submit name=action value="|
398       . $locale->text('Save as new') . qq|">|;
399   }
400
401   print qq|
402 </form>
403
404 </body>
405 </html>
406 |;
407
408   $main::lxdebug->leave_sub();
409 }
410
411 sub save_account {
412   $main::lxdebug->enter_sub();
413
414   my $form     = $main::form;
415   my %myconfig = %main::myconfig;
416   my $locale   = $main::locale;
417
418   $main::auth->assert('config');
419
420   $form->isblank("accno",       $locale->text('Account Number missing!'));
421   $form->isblank("description", $locale->text('Account Description missing!'));
422
423   if ($form->{charttype} eq 'A'){
424     $form->isblank("category",  $locale->text('Account Type missing!'));
425   }
426
427   $form->redirect($locale->text('Account saved!'))
428     if (AM->save_account(\%myconfig, \%$form));
429   $form->error($locale->text('Cannot save account!'));
430
431   $main::lxdebug->leave_sub();
432 }
433
434 sub save_as_new_account {
435   $main::lxdebug->enter_sub();
436
437   my $form     = $main::form;
438   my %myconfig = %main::myconfig;
439   my $locale   = $main::locale;
440
441   $main::auth->assert('config');
442
443   $form->isblank("accno",       $locale->text('Account Number missing!'));
444   $form->isblank("description", $locale->text('Account Description missing!'));
445
446   if ($form->{charttype} eq 'A'){
447     $form->isblank("category",  $locale->text('Account Type missing!'));
448   }
449
450   for my $taxkey (0 .. 9) {
451     if ($form->{"taxkey_id_$taxkey"}) {
452       $form->{"taxkey_id_$taxkey"} = "NEW";
453     }
454   }
455
456   $form->{id} = 0;
457   if ($form->{"original_accno"} &&
458       ($form->{"accno"} eq $form->{"original_accno"})) {
459     $form->error($locale->text('Account Number already used!'));
460   }
461   $form->redirect($locale->text('Account saved!'))
462     if (AM->save_account(\%myconfig, \%$form));
463   $form->error($locale->text('Cannot save account!'));
464
465   $main::lxdebug->leave_sub();
466 }
467
468 sub list_account {
469   $main::lxdebug->enter_sub();
470
471   my $form     = $main::form;
472   my %myconfig = %main::myconfig;
473   my $locale   = $main::locale;
474
475   $main::auth->assert('config');
476
477   $form->{callback}     = build_std_url('action=list_account');
478   my $link_edit_account = build_std_url('action=edit_account', 'callback');
479
480   CA->all_accounts(\%myconfig, \%$form);
481
482   foreach my $ca (@{ $form->{CA} }) {
483
484     $ca->{debit}  = "";
485     $ca->{credit} = "";
486
487     if ($ca->{amount} > 0) {
488       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
489     }
490     if ($ca->{amount} < 0) {
491       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
492     }
493     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:'';
494     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
495   }
496
497   $form->use_stylesheet("list_accounts.css");
498   $form->{title}       = $locale->text('Chart of Accounts');
499
500   $form->header;
501
502
503   my $parameters_ref = {
504   #   hidden_variables                => $_hidden_variables_ref,
505   };
506
507   # Ausgabe des Templates
508   print($form->parse_html_template('am/list_accounts', $parameters_ref));
509
510   $main::lxdebug->leave_sub();
511
512 }
513
514
515 sub list_account_details {
516 # Ajax Funktion aus list_account_details
517   $main::lxdebug->enter_sub();
518
519   my $form     = $main::form;
520   my %myconfig = %main::myconfig;
521   my $locale   = $main::locale;
522
523   $main::auth->assert('config');
524
525   my $chart_id = $form->{args};
526
527   CA->all_accounts(\%myconfig, \%$form, $chart_id);
528
529   foreach my $ca (@{ $form->{CA} }) {
530
531     $ca->{debit}  = "&nbsp;";
532     $ca->{credit} = "&nbsp;";
533
534     if ($ca->{amount} > 0) {
535       $ca->{credit} =
536         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
537     }
538     if ($ca->{amount} < 0) {
539       $ca->{debit} =
540         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
541     }
542
543     my @links = split( q{:}, $ca->{link});
544
545     $ca->{link} = q{};
546
547     foreach my $link (@links){
548       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
549                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
550                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
551                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
552                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
553                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
554                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
555                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
556                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
557                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
558                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
559                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
560                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
561                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
562                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
563 #               : ( $link eq 'CT_tax' )        ? $locale->text('Account Link CT_tax')
564                : $locale->text('Unknown Link') . ': ' . $link;
565       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
566     }
567
568     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
569                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
570                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
571                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
572                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
573                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
574                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
575                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
576   }
577
578   $form->{title} = $locale->text('Chart of Accounts');
579
580   print $form->ajax_response_header, $form->parse_html_template('am/list_account_details');
581
582   $main::lxdebug->leave_sub();
583
584 }
585
586 sub delete_account {
587   $main::lxdebug->enter_sub();
588
589   my $form     = $main::form;
590   my %myconfig = %main::myconfig;
591   my $locale   = $main::locale;
592
593   $main::auth->assert('config');
594
595   $form->{title} = $locale->text('Delete Account');
596
597   foreach my $id (
598     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
599     ) {
600     if ($form->{id} == $form->{$id}) {
601       $form->error($locale->text('Cannot delete default account!'));
602     }
603   }
604
605   $form->redirect($locale->text('Account deleted!'))
606     if (AM->delete_account(\%myconfig, \%$form));
607   $form->error($locale->text('Cannot delete account!'));
608
609   $main::lxdebug->leave_sub();
610 }
611
612 sub add_lead {
613   $main::lxdebug->enter_sub();
614
615   my $form     = $main::form;
616
617   $main::auth->assert('config');
618
619   $form->{title} = "Add";
620
621   $form->{callback} = "am.pl?action=add_lead" unless $form->{callback};
622
623   &lead_header;
624   &form_footer;
625
626   $main::lxdebug->leave_sub();
627 }
628
629 sub edit_lead {
630   $main::lxdebug->enter_sub();
631
632   my $form     = $main::form;
633   my %myconfig = %main::myconfig;
634
635   $main::auth->assert('config');
636
637   $form->{title} = "Edit";
638
639   AM->get_lead(\%myconfig, \%$form);
640
641   &lead_header;
642
643   $form->{orphaned} = 1;
644   &form_footer;
645
646   $main::lxdebug->leave_sub();
647 }
648
649 sub list_lead {
650   $main::lxdebug->enter_sub();
651
652   my $form     = $main::form;
653   my %myconfig = %main::myconfig;
654   my $locale   = $main::locale;
655
656   $main::auth->assert('config');
657
658   AM->lead(\%myconfig, \%$form);
659
660   $form->{callback} = "am.pl?action=list_lead";
661
662   my $callback = $form->escape($form->{callback});
663
664   $form->{title} = $locale->text('Lead');
665
666   my @column_index = qw(description cost profit);
667   my %column_header;
668   $column_header{description} =
669       qq|<th class=listheading width=100%>|
670     . $locale->text('Description')
671     . qq|</th>|;
672
673   $form->header;
674
675   print qq|
676 <body>
677
678 <table width=100%>
679   <tr>
680     <th class=listtop>$form->{title}</th>
681   </tr>
682   <tr height="5"></tr>
683   <tr class=listheading>
684 |;
685
686   map { print "$column_header{$_}\n" } @column_index;
687
688   print qq|
689   </tr>
690 |;
691
692   my ($i, %column_data);
693   foreach my $ref (@{ $form->{ALL} }) {
694
695     $i++;
696     $i %= 2;
697
698     print qq|
699   <tr valign=top class=listrow$i>
700 |;
701
702 #    $lead = $ref->{lead};
703
704     $column_data{description} = qq|<td><a href="am.pl?action=edit_lead&id=$ref->{id}&callback=$callback">$ref->{lead}</td>|;
705
706     map { print "$column_data{$_}\n" } @column_index;
707
708     print qq|
709   </tr>
710 |;
711   }
712
713   print qq|
714   <tr>
715   <td><hr size=3 noshade></td>
716   </tr>
717 </table>
718
719 <br>
720 <form method=post action=am.pl>
721
722 <input name=callback type=hidden value="$form->{callback}">
723
724 <input type=hidden name=type value=lead>
725
726 <input class=submit type=submit name=action value="|
727     . $locale->text('Add') . qq|">
728
729   </form>
730
731   </body>
732   </html>
733 |;
734
735   $main::lxdebug->leave_sub();
736 }
737
738 sub lead_header {
739   $main::lxdebug->enter_sub();
740
741   my $form     = $main::form;
742   my $locale   = $main::locale;
743
744   $main::auth->assert('config');
745
746   $form->{title} = $locale->text("$form->{title} Lead");
747
748   # $locale->text('Add Lead')
749   # $locale->text('Edit Lead')
750
751   $form->{description} =~ s/\"/&quot;/g;
752
753   my $description =
754       qq|<input name=description size=50 value="$form->{lead}">|;
755
756   $form->header;
757
758   print qq|
759 <body>
760
761 <form method=post action=am.pl>
762
763 <input type=hidden name=id value=$form->{id}>
764 <input type=hidden name=type value=lead>
765
766 <table width=100%>
767   <tr>
768     <th class=listtop colspan=2>$form->{title}</th>
769   </tr>
770   <tr height="5"></tr>
771   <tr>
772     <th align=right>| . $locale->text('Description') . qq|</th>
773     <td>$description</td>
774   </tr>
775     <td colspan=2><hr size=3 noshade></td>
776   </tr>
777 </table>
778 |;
779
780   $main::lxdebug->leave_sub();
781 }
782
783 sub save_lead {
784   $main::lxdebug->enter_sub();
785
786   my $form     = $main::form;
787   my %myconfig = %main::myconfig;
788   my $locale   = $main::locale;
789
790   $main::auth->assert('config');
791
792   $form->isblank("description", $locale->text('Description missing!'));
793   AM->save_lead(\%myconfig, \%$form);
794   $form->redirect($locale->text('lead saved!'));
795
796   $main::lxdebug->leave_sub();
797 }
798
799 sub delete_lead {
800   $main::lxdebug->enter_sub();
801
802   my $form     = $main::form;
803   my %myconfig = %main::myconfig;
804   my $locale   = $main::locale;
805
806   $main::auth->assert('config');
807
808   AM->delete_lead(\%myconfig, \%$form);
809   $form->redirect($locale->text('lead deleted!'));
810
811   $main::lxdebug->leave_sub();
812 }
813
814 sub add_language {
815   $main::lxdebug->enter_sub();
816
817   my $form     = $main::form;
818
819   $main::auth->assert('config');
820
821   $form->{title} = "Add";
822
823   $form->{callback} = "am.pl?action=add_language" unless $form->{callback};
824
825   &language_header;
826   &form_footer;
827
828   $main::lxdebug->leave_sub();
829 }
830
831 sub edit_language {
832   $main::lxdebug->enter_sub();
833
834   my $form     = $main::form;
835   my %myconfig = %main::myconfig;
836
837   $main::auth->assert('config');
838
839   $form->{title} = "Edit";
840
841   AM->get_language(\%myconfig, \%$form);
842
843   &language_header;
844
845   $form->{orphaned} = 1;
846   &form_footer;
847
848   $main::lxdebug->leave_sub();
849 }
850
851 sub list_language {
852   $main::lxdebug->enter_sub();
853
854   my $form     = $main::form;
855   my %myconfig = %main::myconfig;
856   my $locale   = $main::locale;
857
858   $main::auth->assert('config');
859
860   AM->language(\%myconfig, \%$form);
861
862   $form->{callback} = "am.pl?action=list_language";
863
864   my $callback = $form->escape($form->{callback});
865
866   $form->{title} = $locale->text('Languages');
867
868   my @column_index = qw(description template_code article_code output_numberformat output_dateformat output_longdates);
869   my %column_header;
870   $column_header{description} =
871       qq|<th class=listheading width=60%>|
872     . $locale->text('Description')
873     . qq|</th>|;
874   $column_header{template_code} =
875       qq|<th class=listheading width=10%>|
876     . $locale->text('Template Code')
877     . qq|</th>|;
878   $column_header{article_code} =
879       qq|<th class=listheading>|
880     . $locale->text('Article Code')
881     . qq|</th>|;
882   $column_header{output_numberformat} =
883       qq|<th class=listheading>|
884     . $locale->text('Number Format')
885     . qq|</th>|;
886   $column_header{output_dateformat} =
887       qq|<th class=listheading>|
888     . $locale->text('Date Format')
889     . qq|</th>|;
890   $column_header{output_longdates} =
891       qq|<th class=listheading>|
892     . $locale->text('Long Dates')
893     . qq|</th>|;
894
895   $form->header;
896
897   print qq|
898 <body>
899
900 <table width=100%>
901   <tr>
902     <th class=listtop>$form->{title}</th>
903   </tr>
904   <tr height="5"></tr>
905   <tr>
906     <td>
907       <table width=100%>
908         <tr class=listheading>
909 |;
910
911   map { print "$column_header{$_}\n" } @column_index;
912
913   print qq|
914         </tr>
915 |;
916
917   my ($i, %column_data);
918   foreach my $ref (@{ $form->{ALL} }) {
919
920     $i++;
921     $i %= 2;
922
923     print qq|
924         <tr valign=top class=listrow$i>
925 |;
926
927
928     $column_data{description} =
929       qq|<td><a href="am.pl?action=edit_language&id=$ref->{id}&callback=$callback">$ref->{description}</td>|;
930     $column_data{template_code}           = qq|<td align=right>$ref->{template_code}</td>|;
931     $column_data{article_code} =
932       qq|<td align=right>$ref->{article_code}</td>|;
933     $column_data{output_numberformat} =
934       "<td nowrap>" .
935       ($ref->{output_numberformat} ? $ref->{output_numberformat} :
936        $locale->text("use program settings")) .
937       "</td>";
938     $column_data{output_dateformat} =
939       "<td nowrap>" .
940       ($ref->{output_dateformat} ? $ref->{output_dateformat} :
941        $locale->text("use program settings")) .
942       "</td>";
943     $column_data{output_longdates} =
944       "<td nowrap>" .
945       ($ref->{output_longdates} ? $locale->text("Yes") : $locale->text("No")) .
946       "</td>";
947
948     map { print "$column_data{$_}\n" } @column_index;
949
950     print qq|
951         </tr>
952 |;
953   }
954
955   print qq|
956       </table>
957     </td>
958   </tr>
959   <tr>
960   <td><hr size=3 noshade></td>
961   </tr>
962 </table>
963
964 <br>
965 <form method=post action=am.pl>
966
967 <input name=callback type=hidden value="$form->{callback}">
968
969 <input type=hidden name=type value=language>
970
971 <input class=submit type=submit name=action value="|
972     . $locale->text('Add') . qq|">
973
974   </form>
975
976   </body>
977   </html>
978 |;
979
980   $main::lxdebug->leave_sub();
981 }
982
983 sub language_header {
984   $main::lxdebug->enter_sub();
985
986   my $form     = $main::form;
987   my $locale   = $main::locale;
988
989   $main::auth->assert('config');
990
991   $form->{title}    = $locale->text("$form->{title} Language");
992
993   # $locale->text('Add Language')
994   # $locale->text('Edit Language')
995
996   $form->{description} =~ s/\"/&quot;/g;
997   $form->{template_code} =~ s/\"/&quot;/g;
998   $form->{article_code} =~ s/\"/&quot;/g;
999
1000
1001   $form->header;
1002
1003   my $numberformat =
1004     qq|<option value="">| . $locale->text("use program settings") .
1005     qq|</option>|;
1006   foreach my $item (('1,000.00', '1000.00', '1.000,00', '1000,00')) {
1007     $numberformat .=
1008       ($item eq $form->{output_numberformat})
1009       ? "<option selected>$item"
1010       : "<option>$item"
1011       . "</option>";
1012   }
1013
1014   my $dateformat =
1015     qq|<option value="">| . $locale->text("use program settings") .
1016     qq|</option>|;
1017   foreach my $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
1018     $dateformat .=
1019       ($item eq $form->{output_dateformat})
1020       ? "<option selected>$item"
1021       : "<option>$item"
1022       . "</option>";
1023   }
1024
1025   print qq|
1026 <body>
1027
1028 <form method=post action=am.pl>
1029
1030 <input type=hidden name=id value=$form->{id}>
1031 <input type=hidden name=type value=language>
1032
1033 <table width=100%>
1034   <tr>
1035     <th class=listtop colspan=2>$form->{title}</th>
1036   </tr>
1037   <tr height="5"></tr>
1038   <tr>
1039     <th align=right>| . $locale->text('Language') . qq|</th>
1040     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1041   <tr>
1042   <tr>
1043     <th align=right>| . $locale->text('Template Code') . qq|</th>
1044     <td><input name=template_code size=5 value="| . $form->quote($form->{template_code}) . qq|"></td>
1045   </tr>
1046   <tr>
1047     <th align=right>| . $locale->text('Article Code') . qq|</th>
1048     <td><input name=article_code size=10 value="| . $form->quote($form->{article_code}) . qq|"></td>
1049   </tr>
1050   <tr>
1051     <th align=right>| . $locale->text('Number Format') . qq|</th>
1052     <td><select name="output_numberformat">$numberformat</select></td>
1053   </tr>
1054   <tr>
1055     <th align=right>| . $locale->text('Date Format') . qq|</th>
1056     <td><select name="output_dateformat">$dateformat</select></td>
1057   </tr>
1058   <tr>
1059     <th align=right>| . $locale->text('Long Dates') . qq|</th>
1060     <td><input type="radio" name="output_longdates" value="1"| .
1061     ($form->{output_longdates} ? " checked" : "") .
1062     qq|>| . $locale->text("Yes") .
1063     qq|<input type="radio" name="output_longdates" value="0"| .
1064     ($form->{output_longdates} ? "" : " checked") .
1065     qq|>| . $locale->text("No") .
1066     qq|</td>
1067   </tr>
1068   <td colspan=2><hr size=3 noshade></td>
1069   </tr>
1070 </table>
1071 |;
1072
1073   $main::lxdebug->leave_sub();
1074 }
1075
1076 sub save_language {
1077   $main::lxdebug->enter_sub();
1078
1079   my $form     = $main::form;
1080   my %myconfig = %main::myconfig;
1081   my $locale   = $main::locale;
1082
1083   $main::auth->assert('config');
1084
1085   $form->isblank("description", $locale->text('Language missing!'));
1086   $form->isblank("template_code", $locale->text('Template Code missing!'));
1087   $form->isblank("article_code", $locale->text('Article Code missing!'));
1088   AM->save_language(\%myconfig, \%$form);
1089   $form->redirect($locale->text('Language saved!'));
1090
1091   $main::lxdebug->leave_sub();
1092 }
1093
1094 sub delete_language {
1095   $main::lxdebug->enter_sub();
1096
1097   my $form     = $main::form;
1098   my %myconfig = %main::myconfig;
1099   my $locale   = $main::locale;
1100
1101   $main::auth->assert('config');
1102
1103   AM->delete_language(\%myconfig, \%$form);
1104   $form->redirect($locale->text('Language deleted!'));
1105
1106   $main::lxdebug->leave_sub();
1107 }
1108
1109
1110 sub add_buchungsgruppe {
1111   $main::lxdebug->enter_sub();
1112
1113   my $form     = $main::form;
1114   my %myconfig = %main::myconfig;
1115   my $locale   = $main::locale;
1116
1117   $main::auth->assert('config');
1118
1119   # $locale->text("Add Buchungsgruppe")
1120   # $locale->text("Edit Buchungsgruppe")
1121   $form->{title} = "Add";
1122
1123   $form->{callback} = "am.pl?action=add_buchungsgruppe" unless $form->{callback};
1124
1125   AM->get_buchungsgruppe(\%myconfig, \%$form);
1126   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
1127   for (my $i = 0; 4 > $i; $i++) {
1128     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
1129         qw(income expense));
1130   }
1131
1132   &buchungsgruppe_header;
1133   &form_footer;
1134
1135   $main::lxdebug->leave_sub();
1136 }
1137
1138 sub edit_buchungsgruppe {
1139   $main::lxdebug->enter_sub();
1140
1141   my $form     = $main::form;
1142   my %myconfig = %main::myconfig;
1143
1144   $main::auth->assert('config');
1145
1146   $form->{title} = "Edit";
1147
1148   AM->get_buchungsgruppe(\%myconfig, \%$form);
1149
1150   &buchungsgruppe_header;
1151
1152   &form_footer;
1153
1154   $main::lxdebug->leave_sub();
1155 }
1156
1157 sub list_buchungsgruppe {
1158   $main::lxdebug->enter_sub();
1159
1160   my $form     = $main::form;
1161   my %myconfig = %main::myconfig;
1162   my $locale   = $main::locale;
1163
1164   $main::auth->assert('config');
1165
1166   AM->buchungsgruppe(\%myconfig, \%$form);
1167
1168   $form->{callback} = "am.pl?action=list_buchungsgruppe";
1169
1170   my $callback = $form->escape($form->{callback});
1171
1172   $form->{title} = $locale->text('Buchungsgruppen');
1173
1174   my @column_index = qw(up down description inventory_accno
1175                      income_accno_0 expense_accno_0
1176                      income_accno_1 expense_accno_1
1177                      income_accno_2 expense_accno_2
1178                      income_accno_3 expense_accno_3 );
1179   my %column_header;
1180   $column_header{up} =
1181       qq|<th class="listheading" width="16">|
1182     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
1183     . qq|</th>|;
1184   $column_header{down} =
1185       qq|<th class="listheading" width="16">|
1186     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
1187     . qq|</th>|;
1188   $column_header{description} =
1189       qq|<th class="listheading" width="40%">|
1190     . $locale->text('Description')
1191     . qq|</th>|;
1192   $column_header{inventory_accno} =
1193       qq|<th class=listheading>|
1194     . $locale->text('Bestandskonto')
1195     . qq|</th>|;
1196   $column_header{income_accno_0} =
1197       qq|<th class=listheading>|
1198     . $locale->text('National Revenues')
1199     . qq|</th>|;
1200   $column_header{expense_accno_0} =
1201       qq|<th class=listheading>|
1202     . $locale->text('National Expenses')
1203     . qq|</th>|;
1204   $column_header{income_accno_1} =
1205       qq|<th class=listheading>|
1206     . $locale->text('Revenues EU with UStId')
1207     . qq|</th>|;
1208   $column_header{expense_accno_1} =
1209       qq|<th class=listheading>|
1210     . $locale->text('Expenses EU with UStId')
1211     . qq|</th>|;
1212   $column_header{income_accno_2} =
1213       qq|<th class=listheading>|
1214     . $locale->text('Revenues EU without UStId')
1215     . qq|</th>|;
1216   $column_header{expense_accno_2} =
1217       qq|<th class=listheading>|
1218     . $locale->text('Expenses EU without UStId')
1219     . qq|</th>|;
1220   $column_header{income_accno_3} =
1221       qq|<th class=listheading>|
1222     . $locale->text('Foreign Revenues')
1223     . qq|</th>|;
1224   $column_header{expense_accno_3} =
1225       qq|<th class=listheading>|
1226     . $locale->text('Foreign Expenses')
1227     . qq|</th>|;
1228   $form->header;
1229
1230   print qq|
1231 <body>
1232
1233 <table width=100%>
1234   <tr>
1235     <th class=listtop>$form->{title}</th>
1236   </tr>
1237   <tr height="5"></tr>
1238   <tr>
1239     <td>
1240       <table width=100%>
1241         <tr class=listheading>
1242 |;
1243
1244   map { print "$column_header{$_}\n" } @column_index;
1245
1246   print qq|
1247         </tr>
1248 |;
1249
1250   my $swap_link = qq|am.pl?action=swap_buchungsgruppen&|;
1251
1252   my $row = 0;
1253   my ($i, %column_data);
1254   foreach my $ref (@{ $form->{ALL} }) {
1255
1256     $i++;
1257     $i %= 2;
1258
1259     print qq|
1260         <tr valign=top class=listrow$i>
1261 |;
1262
1263     if ($row) {
1264       my $pref = $form->{ALL}->[$row - 1];
1265       $column_data{up} =
1266         qq|<td align="center" valign="center" width="16">| .
1267         qq|<a href="${swap_link}id1=$ref->{id}&id2=$pref->{id}">| .
1268         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
1269         qq|</a></td>|;
1270     } else {
1271       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
1272     }
1273
1274     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
1275       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
1276     } else {
1277       my $nref = $form->{ALL}->[$row + 1];
1278       $column_data{down} =
1279         qq|<td align="center" valign="center" width="16">| .
1280         qq|<a href="${swap_link}id1=$ref->{id}&id2=$nref->{id}">| .
1281         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
1282         qq|</a></td>|;
1283     }
1284
1285     $column_data{description} = qq|<td><a href="am.pl?action=edit_buchungsgruppe&id=$ref->{id}&callback=$callback">$ref->{description}</td>|;
1286     $column_data{inventory_accno}           = qq|<td align=right>$ref->{inventory_accno}</td>|;
1287     $column_data{income_accno_0} =
1288       qq|<td align=right>$ref->{income_accno_0}</td>|;
1289     $column_data{expense_accno_0}           = qq|<td align=right>$ref->{expense_accno_0}</td>|;
1290     $column_data{income_accno_1} =
1291       qq|<td align=right>$ref->{income_accno_1}</td>|;
1292     $column_data{expense_accno_1}           = qq|<td align=right>$ref->{expense_accno_1}</td>|;
1293     $column_data{income_accno_2} =
1294       qq|<td align=right>$ref->{income_accno_2}</td>|;
1295     $column_data{expense_accno_2}           = qq|<td align=right>$ref->{expense_accno_2}</td>|;
1296     $column_data{income_accno_3} =
1297       qq|<td align=right>$ref->{income_accno_3}</td>|;
1298     $column_data{expense_accno_3}           = qq|<td align=right>$ref->{expense_accno_3}</td>|;
1299
1300     map { print "$column_data{$_}\n" } @column_index;
1301
1302     print qq|
1303         </tr>
1304 |;
1305
1306     $row++;
1307   }
1308
1309   print qq|
1310       </table>
1311     </td>
1312   </tr>
1313   <tr>
1314   <td><hr size=3 noshade></td>
1315   </tr>
1316 </table>
1317
1318 <br>
1319 <form method=post action=am.pl>
1320
1321 <input name=callback type=hidden value="$form->{callback}">
1322
1323 <input type=hidden name=type value=buchungsgruppe>
1324
1325 <input class=submit type=submit name=action value="|
1326     . $locale->text('Add') . qq|">
1327
1328   </form>
1329
1330   </body>
1331   </html>
1332 |;
1333
1334   $main::lxdebug->leave_sub();
1335 }
1336
1337 sub buchungsgruppe_header {
1338   $main::lxdebug->enter_sub();
1339
1340   my $form     = $main::form;
1341   my $locale   = $main::locale;
1342
1343   $main::auth->assert('config');
1344
1345   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
1346
1347   # $locale->text('Add Accounting Group')
1348   # $locale->text('Edit Accounting Group')
1349
1350   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
1351   my %acc_type_map = (
1352     "IC" => $acc_inventory,
1353     "IC_income" => $acc_income,
1354     "IC_sale" => $acc_income,
1355     "IC_expense" => $acc_expense,
1356     "IC_cogs" => $acc_expense,
1357     );
1358
1359   foreach my $key (keys(%acc_type_map)) {
1360     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1361       $acc_type_map{$key}->{$ref->{"id"}} = $ref;
1362     }
1363   }
1364
1365   foreach my $type (qw(IC IC_income IC_expense)) {
1366     $form->{"select$type"} =
1367       join("",
1368            map({ "<option value=$_->{id} $_->{selected}>" .
1369                    "$_->{accno}--" . H($_->{description}) . "</option>" }
1370                sort({ $a->{"accno"} cmp $b->{"accno"} }
1371                     values(%{$acc_type_map{$type}}))));
1372   }
1373
1374   if ($form->{id}) {
1375     $form->{selectIC} =~ s/selected//g;
1376     $form->{selectIC} =~ s/ value=\Q$form->{inventory_accno_id}\E/  value=$form->{inventory_accno_id} selected/;
1377     $form->{selectIC_income} =~ s/selected//g;
1378     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_0}\E/  value=$form->{income_accno_id_0} selected/;
1379     $form->{selectIC_expense} =~ s/selected//g;
1380     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_0}\E/  value=$form->{expense_accno_id_0} selected/;
1381   }
1382
1383   my $linkaccounts;
1384   if ( $::instance_conf->get_inventory_system eq 'perpetual' ) { # was !$::lx_office_conf{system}->{eur}) {
1385     $linkaccounts = qq|
1386                <tr>
1387                 <th align=right>| . $locale->text('Inventory') . qq|</th>
1388                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
1389                 <input name=selectIC type=hidden value="$form->{selectIC}">
1390               </tr>|;
1391   } elsif ( $::instance_conf->get_inventory_system eq 'periodic' ) {
1392     # don't allow choice of inventory accno and don't show that line
1393     $linkaccounts = qq|
1394                 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>|;
1395   };
1396
1397
1398   $linkaccounts .= qq|
1399               <tr>
1400                 <th align=right>| . $locale->text('National Revenues') . qq|</th>
1401                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
1402               </tr>
1403               <tr>
1404                 <th align=right>| . $locale->text('National Expenses') . qq|</th>
1405                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
1406               </tr>|;
1407   if ($form->{id}) {
1408     $form->{selectIC_income} =~ s/selected//g;
1409     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_1}\E/  value=$form->{income_accno_id_1} selected/;
1410     $form->{selectIC_expense} =~ s/selected//g;
1411     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_1}\E/  value=$form->{expense_accno_id_1} selected/;
1412   }
1413   $linkaccounts .= qq|              <tr>
1414                 <th align=right>| . $locale->text('Revenues EU with UStId') . qq|</th>
1415                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
1416               </tr>
1417               <tr>
1418                 <th align=right>| . $locale->text('Expenses EU with UStId') . qq|</th>
1419                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
1420               </tr>|;
1421
1422   if ($form->{id}) {
1423     $form->{selectIC_income} =~ s/selected//g;
1424     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_2}\E/  value=$form->{income_accno_id_2} selected/;
1425     $form->{selectIC_expense} =~ s/selected//g;
1426     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_2}\E/  value=$form->{expense_accno_id_2} selected/;
1427   }
1428
1429   $linkaccounts .= qq|              <tr>
1430                 <th align=right>| . $locale->text('Revenues EU without UStId') . qq|</th>
1431                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
1432               </tr>
1433               <tr>
1434                 <th align=right>| . $locale->text('Expenses EU without UStId') . qq|</th>
1435                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
1436               </tr>|;
1437
1438   if ($form->{id}) {
1439     $form->{selectIC_income} =~ s/selected//g;
1440     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_3}\E/  value=$form->{income_accno_id_3} selected/;
1441     $form->{selectIC_expense} =~ s/selected//g;
1442     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_3}\E/  value=$form->{expense_accno_id_3} selected/;
1443   }
1444
1445   $linkaccounts .= qq|              <tr>
1446                 <th align=right>| . $locale->text('Foreign Revenues') . qq|</th>
1447                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
1448               </tr>
1449               <tr>
1450                 <th align=right>| . $locale->text('Foreign Expenses') . qq|</th>
1451                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
1452               </tr>
1453 |;
1454
1455
1456   $form->header;
1457
1458   print qq|
1459 <body>
1460
1461 <form method=post action=am.pl>
1462
1463 <input type=hidden name=id value=$form->{id}>
1464 <input type=hidden name=type value=buchungsgruppe>
1465
1466 <table width=100%>
1467   <tr>
1468     <th class=listtop colspan=2>$form->{title}</th>
1469   </tr>
1470   <tr height="5"></tr>
1471   <tr>
1472     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
1473     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1474   <tr>
1475   $linkaccounts
1476   <td colspan=2><hr size=3 noshade></td>
1477   </tr>
1478 </table>
1479 |;
1480
1481   $main::lxdebug->leave_sub();
1482 }
1483
1484 sub save_buchungsgruppe {
1485   $main::lxdebug->enter_sub();
1486
1487   my $form     = $main::form;
1488   my %myconfig = %main::myconfig;
1489   my $locale   = $main::locale;
1490
1491   $main::auth->assert('config');
1492
1493   $form->isblank("description", $locale->text('Description missing!'));
1494
1495   AM->save_buchungsgruppe(\%myconfig, \%$form);
1496   $form->redirect($locale->text('Accounting Group saved!'));
1497
1498   $main::lxdebug->leave_sub();
1499 }
1500
1501 sub delete_buchungsgruppe {
1502   $main::lxdebug->enter_sub();
1503
1504   my $form     = $main::form;
1505   my %myconfig = %main::myconfig;
1506   my $locale   = $main::locale;
1507
1508   $main::auth->assert('config');
1509
1510   AM->delete_buchungsgruppe(\%myconfig, \%$form);
1511   $form->redirect($locale->text('Accounting Group deleted!'));
1512
1513   $main::lxdebug->leave_sub();
1514 }
1515
1516 sub swap_buchungsgruppen {
1517   $main::lxdebug->enter_sub();
1518
1519   my $form     = $main::form;
1520   my %myconfig = %main::myconfig;
1521
1522   $main::auth->assert('config');
1523
1524   AM->swap_sortkeys(\%myconfig, $form, "buchungsgruppen");
1525   list_buchungsgruppe();
1526
1527   $main::lxdebug->leave_sub();
1528 }
1529
1530 sub edit_defaults {
1531   $main::lxdebug->enter_sub();
1532
1533   my $form     = $main::form;
1534   my %myconfig = %main::myconfig;
1535   my $locale   = $main::locale;
1536
1537   # get defaults for account numbers and last numbers
1538   AM->defaultaccounts(\%myconfig, \%$form);
1539   $form->{ALL_UNITS} = AM->convertible_units(AM->retrieve_all_units(), 'g');
1540
1541   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
1542
1543   # default language
1544   my $all_languages = SL::DB::Manager::Language->get_all;
1545
1546 # EÜR = cash, Bilanzierung = accrual
1547
1548   foreach my $key (keys %{ $form->{IC} }) {
1549     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
1550       my $array = "ACCNOS_" . uc($key);
1551       $form->{$array} ||= [];
1552
1553       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
1554       push @{ $form->{$array} }, {
1555         'name'     => $value,
1556         'value'    => $value,
1557         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
1558       };
1559     }
1560   }
1561
1562   $form->{title} = $locale->text('Ranges of numbers and default accounts');
1563
1564   $form->header();
1565   print $form->parse_html_template('am/edit_defaults',
1566                                    { ALL_LANGUAGES => $all_languages, });
1567
1568   $main::lxdebug->leave_sub();
1569 }
1570
1571 sub save_defaults {
1572   $main::lxdebug->enter_sub();
1573
1574   my $form     = $main::form;
1575   my $locale   = $main::locale;
1576
1577   AM->save_defaults();
1578
1579   $form->redirect($locale->text('Defaults saved.'));
1580
1581   $main::lxdebug->leave_sub();
1582 }
1583
1584 sub _build_cfg_options {
1585   my $form     = $main::form;
1586   my %myconfig = %main::myconfig;
1587
1588   my $idx   = shift;
1589   my $array = uc($idx) . 'S';
1590
1591   $form->{$array} = [];
1592   foreach my $item (@_) {
1593     push @{ $form->{$array} }, {
1594       'name'     => $item,
1595       'value'    => $item,
1596       'selected' => $item eq $myconfig{$idx},
1597     };
1598   }
1599 }
1600
1601 sub config {
1602   $main::lxdebug->enter_sub();
1603
1604   my $form     = $main::form;
1605   my %myconfig = %main::myconfig;
1606   my $locale   = $main::locale;
1607
1608   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
1609   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
1610
1611   my @formats = ();
1612   if ($::lx_office_conf{print_templates}->{opendocument}
1613       && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
1614       && $::lx_office_conf{applications}->{xvfb}                 && (-x $::lx_office_conf{applications}->{xvfb})) {
1615     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
1616                      "value" => "opendocument_pdf" });
1617   }
1618   if ($::lx_office_conf{print_templates}->{latex}) {
1619     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
1620   }
1621   push(@formats, { "name" => "HTML", "value" => "html" });
1622   if ($::lx_office_conf{print_templates}->{latex}) {
1623     push(@formats, { "name" => $locale->text("Postscript"),
1624                      "value" => "postscript" });
1625   }
1626   if ($::lx_office_conf{print_templates}->{opendocument}) {
1627     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
1628                      "value" => "opendocument" });
1629   }
1630
1631   if (!$myconfig{"template_format"}) {
1632     $myconfig{"template_format"} = "pdf";
1633   }
1634   $form->{TEMPLATE_FORMATS} = [];
1635   foreach my $item (@formats) {
1636     push @{ $form->{TEMPLATE_FORMATS} }, {
1637       'name'     => $item->{name},
1638       'value'    => $item->{value},
1639       'selected' => $item->{value} eq $myconfig{template_format},
1640     };
1641   }
1642
1643   if (!$myconfig{"default_media"}) {
1644     $myconfig{"default_media"} = "screen";
1645   }
1646
1647   my %selected = ($myconfig{"default_media"} => "selected");
1648   $form->{MEDIA} = [
1649     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
1650     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
1651     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
1652     ];
1653
1654   $form->{PRINTERS} = [ SL::Printer->all_printers(%::myconfig) ];
1655
1656   my %countrycodes = User->country_codes;
1657
1658   $form->{COUNTRYCODES} = [];
1659   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
1660     push @{ $form->{COUNTRYCODES} }, {
1661       'name'     => $countrycodes{$countrycode},
1662       'value'    => $countrycode,
1663       'selected' => $countrycode eq $myconfig{countrycode},
1664     };
1665   }
1666
1667   $form->{STYLESHEETS} = [];
1668   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css)) {
1669     push @{ $form->{STYLESHEETS} }, {
1670       'name'     => $item,
1671       'value'    => $item,
1672       'selected' => $item eq $myconfig{stylesheet},
1673     };
1674   }
1675
1676   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
1677   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
1678   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
1679
1680   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
1681
1682   $form->header();
1683   print $form->parse_html_template('am/config');
1684
1685   $main::lxdebug->leave_sub();
1686 }
1687
1688 sub save_preferences {
1689   $main::lxdebug->enter_sub();
1690
1691   my $form     = $main::form;
1692   my %myconfig = %main::myconfig;
1693   my $locale   = $main::locale;
1694
1695   $form->{stylesheet} = $form->{usestylesheet};
1696
1697   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
1698
1699   if (AM->save_preferences(\%myconfig, $form)) {
1700     if ($::auth->can_change_password()
1701         && defined $form->{new_password}
1702         && ($form->{new_password} ne '********')) {
1703       my $verifier = SL::Auth::PasswordPolicy->new;
1704       my $result   = $verifier->verify($form->{new_password});
1705
1706       if ($result != SL::Auth::PasswordPolicy->OK()) {
1707         $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
1708       }
1709
1710       $::auth->change_password($form->{login}, $form->{new_password});
1711
1712       $form->{password} = $form->{new_password};
1713       $::auth->set_session_value('password', $form->{password});
1714       $::auth->create_or_refresh_session();
1715     }
1716
1717     $form->redirect($locale->text('Preferences saved!'));
1718   }
1719
1720   $form->error($locale->text('Cannot save preferences!'));
1721
1722   $main::lxdebug->leave_sub();
1723 }
1724
1725 sub audit_control {
1726   $main::lxdebug->enter_sub();
1727
1728   my $form     = $main::form;
1729   my %myconfig = %main::myconfig;
1730   my $locale   = $main::locale;
1731
1732   $main::auth->assert('config');
1733
1734   $form->{title} = $locale->text('Audit Control');
1735
1736   AM->closedto(\%myconfig, \%$form);
1737
1738   $form->header;
1739
1740   print qq|
1741 <body>
1742
1743 <form method=post action=am.pl>
1744
1745 <table width=100%>
1746   <tr><th class=listtop>$form->{title}</th></tr>
1747   <tr height="5"></tr>
1748   <tr>
1749     <td>
1750       <table>
1751         <tr>
1752           <th>| . $locale->text('Close Books up to') . qq|</th>
1753           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
1754         </tr>
1755       </table>
1756     </td>
1757   </tr>
1758 </table>
1759
1760 <hr size=3 noshade>
1761
1762 <br>
1763 <input type=hidden name=nextsub value=doclose>
1764
1765 <input type=submit class=submit name=action value="|
1766     . $locale->text('Continue') . qq|">
1767
1768 </form>
1769
1770 </body>
1771 </html>
1772 |;
1773
1774   $main::lxdebug->leave_sub();
1775 }
1776
1777 sub doclose {
1778   $main::lxdebug->enter_sub();
1779
1780   my $form     = $main::form;
1781   my %myconfig = %main::myconfig;
1782   my $locale   = $main::locale;
1783
1784   $main::auth->assert('config');
1785
1786   AM->closebooks(\%myconfig, \%$form);
1787
1788   if ($form->{closedto}) {
1789     $form->redirect(
1790                     $locale->text('Books closed up to') . " "
1791                       . $locale->date(\%myconfig, $form->{closedto}, 1));
1792   } else {
1793     $form->redirect($locale->text('Books are open'));
1794   }
1795
1796   $main::lxdebug->leave_sub();
1797 }
1798
1799 sub edit_units {
1800   $main::lxdebug->enter_sub();
1801
1802   my $form     = $main::form;
1803   my %myconfig = %main::myconfig;
1804   my $locale   = $main::locale;
1805
1806   $main::auth->assert('config');
1807
1808   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1809   AM->units_in_use(\%myconfig, $form, $units);
1810   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
1811
1812   my @languages = AM->language(\%myconfig, $form, 1);
1813
1814   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
1815
1816   my $i = 1;
1817   foreach (@unit_list) {
1818     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
1819     $_->{"UNITLANGUAGES"} = [];
1820     foreach my $lang (@languages) {
1821       push(@{ $_->{"UNITLANGUAGES"} },
1822            { "idx" => $i,
1823              "unit" => $_->{"name"},
1824              "language_id" => $lang->{"id"},
1825              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
1826              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
1827            });
1828     }
1829     $i++;
1830   }
1831
1832   $units = AM->retrieve_units(\%myconfig, $form);
1833   my $ddbox = AM->unit_select_data($units, undef, 1);
1834
1835   $form->{"title"} = $locale->text("Add and edit units");
1836   $form->header();
1837   print($form->parse_html_template("am/edit_units",
1838                                    { "UNITS"               => \@unit_list,
1839                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
1840                                      "LANGUAGES"           => \@languages,
1841                                    }));
1842
1843   $main::lxdebug->leave_sub();
1844 }
1845
1846 sub add_unit {
1847   $main::lxdebug->enter_sub();
1848
1849   my $form     = $main::form;
1850   my %myconfig = %main::myconfig;
1851   my $locale   = $main::locale;
1852
1853   $main::auth->assert('config');
1854
1855   $form->isblank("new_name", $locale->text("The name is missing."));
1856   my $units = AM->retrieve_units(\%myconfig, $form);
1857   my $all_units = AM->retrieve_units(\%myconfig, $form);
1858   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
1859
1860   my ($base_unit, $factor);
1861   if ($form->{"new_base_unit"}) {
1862     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
1863
1864     $form->isblank("new_factor", $locale->text("The factor is missing."));
1865     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
1866     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
1867     $base_unit = $form->{"new_base_unit"};
1868   }
1869
1870   my @languages;
1871   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
1872     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
1873     push(@languages, { "id" => $lang->{"id"},
1874                        "localized" => $form->{"new_localized_$lang->{id}"},
1875                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
1876          });
1877   }
1878
1879   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
1880
1881   $form->{"saved_message"} = $locale->text("The unit has been saved.");
1882
1883   edit_units();
1884
1885   $main::lxdebug->leave_sub();
1886 }
1887
1888 sub set_unit_languages {
1889   $main::lxdebug->enter_sub();
1890
1891   my $form     = $main::form;
1892
1893   $main::auth->assert('config');
1894
1895   my ($unit, $languages, $idx) = @_;
1896
1897   $unit->{"LANGUAGES"} = [];
1898
1899   foreach my $lang (@{$languages}) {
1900     push(@{ $unit->{"LANGUAGES"} },
1901          { "id" => $lang->{"id"},
1902            "localized" => $form->{"localized_${idx}_$lang->{id}"},
1903            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
1904          });
1905   }
1906
1907   $main::lxdebug->leave_sub();
1908 }
1909
1910 sub save_unit {
1911   $main::lxdebug->enter_sub();
1912
1913   my $form     = $main::form;
1914   my %myconfig = %main::myconfig;
1915   my $locale   = $main::locale;
1916
1917   $main::auth->assert('config');
1918
1919   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1920   AM->units_in_use(\%myconfig, $form, $old_units);
1921
1922   my @languages = AM->language(\%myconfig, $form, 1);
1923
1924   my $new_units = {};
1925   my @delete_units = ();
1926   foreach my $i (1..($form->{"rowcount"} * 1)) {
1927     my $old_unit = $old_units->{$form->{"old_name_$i"}};
1928     if (!$old_unit) {
1929       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
1930     }
1931
1932     if ($form->{"unchangeable_$i"}) {
1933       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
1934       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
1935       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1936       next;
1937     }
1938
1939     if ($old_unit->{"in_use"}) {
1940       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
1941     }
1942
1943     if ($form->{"delete_$i"}) {
1944       push(@delete_units, $old_unit->{"name"});
1945       next;
1946     }
1947
1948     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
1949
1950     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
1951     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
1952     $new_units->{$form->{"name_$i"}} = \%h;
1953     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
1954     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1955   }
1956
1957   foreach my $unit (values(%{$new_units})) {
1958     next unless ($unit->{"old_name"});
1959     if ($unit->{"base_unit"}) {
1960       $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"}))
1961         unless (defined($new_units->{$unit->{"base_unit"}}));
1962       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
1963       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
1964     } else {
1965       $unit->{"base_unit"} = undef;
1966       $unit->{"factor"} = undef;
1967     }
1968   }
1969
1970   foreach my $unit (values(%{$new_units})) {
1971     next if ($unit->{"unchanged_unit"});
1972
1973     map({ $_->{"seen"} = 0; } values(%{$new_units}));
1974     my $new_unit = $unit;
1975     while ($new_unit->{"base_unit"}) {
1976       $new_unit->{"seen"} = 1;
1977       $new_unit = $new_units->{$new_unit->{"base_unit"}};
1978       if ($new_unit->{"seen"}) {
1979         $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, " .
1980                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
1981       }
1982     }
1983   }
1984
1985   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
1986
1987   $form->{"saved_message"} = $locale->text("The units have been saved.");
1988
1989   edit_units();
1990
1991   $main::lxdebug->leave_sub();
1992 }
1993
1994 sub show_history_search {
1995   $main::lxdebug->enter_sub();
1996
1997   my $form     = $main::form;
1998   my $locale   = $main::locale;
1999
2000   $main::auth->assert('config');
2001
2002   $form->{title} = $locale->text("History Search");
2003   $form->header();
2004
2005   print $form->parse_html_template("common/search_history");
2006
2007   $main::lxdebug->leave_sub();
2008 }
2009
2010 sub show_am_history {
2011   $main::lxdebug->enter_sub();
2012
2013   my $form     = $main::form;
2014   my %myconfig = %main::myconfig;
2015   my $locale   = $main::locale;
2016
2017   $main::auth->assert('config');
2018
2019   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
2020   $form->{order} ||= 'h.itime--1';
2021
2022   my %search = ( "Artikelnummer"          => "parts",
2023                  "Kundennummer"           => "customer",
2024                  "Lieferantennummer"      => "vendor",
2025                  "Projektnummer"          => "project",
2026                  "Buchungsnummer"         => "oe",
2027                  "Eingangsrechnungnummer" => "ap",
2028                  "Ausgangsrechnungnummer" => "ar",
2029                  "Mahnungsnummer"         => "dunning"
2030     );
2031   my %searchNo = ( "Artikelnummer"          => "partnumber",
2032                    "Kundennummer"           => "customernumber",
2033                    "Lieferantennummer"      => "vendornumber",
2034                    "Projektnummer"          => "projectnumber",
2035                    "Buchungsnummer"         => "ordnumber",
2036                    "Eingangsrechnungnummer" => "invnumber",
2037                    "Ausgangsrechnungnummer" => "invnumber",
2038                    "Mahnungsnummer"         => "dunning_id"
2039     );
2040
2041   my $dbh = $form->dbconnect(\%myconfig);
2042
2043   my $restriction;
2044   $restriction     = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
2045   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
2046   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
2047   if ($form->{mitarbeiter} =~ m/^\d+$/) {
2048     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
2049   } elsif ($form->{mitarbeiter}) {
2050     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
2051   }
2052
2053   my $query = qq|SELECT trans_id AS id FROM history_erp | .
2054     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
2055      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
2056
2057   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
2058   my $daten .= shift @ids;
2059   $daten    .= join '', map { " OR trans_id = $_" } @ids;
2060
2061   my ($sort, $sortby) = split(/\-\-/, $form->{order});
2062   $sort =~ s/.*\.(.*)$/$1/;
2063
2064   $form->{title} = $locale->text("History Search");
2065   $form->header();
2066
2067   print $form->parse_html_template("common/show_history",
2068                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
2069                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
2070                                      "NONEWWINDOW"    => 1,
2071                                      uc($sort)        => 1,
2072                                      uc($sort) . "BY" => $sortby,
2073                                      'callback'       => $callback,
2074                                    });
2075   $dbh->disconnect();
2076
2077   $main::lxdebug->leave_sub();
2078 }
2079
2080 sub add_tax {
2081   $main::lxdebug->enter_sub();
2082
2083   my $form     = $main::form;
2084   my $locale   = $main::locale;
2085
2086   $main::auth->assert('config');
2087
2088   $form->{title} =  $locale->text('Add');
2089
2090   $form->{callback} ||= "am.pl?action=add_tax";
2091
2092   _get_taxaccount_selection();
2093
2094   $form->header();
2095
2096   my $parameters_ref = {
2097 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
2098   };
2099
2100   # Ausgabe des Templates
2101   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2102
2103   $main::lxdebug->leave_sub();
2104 }
2105
2106 sub edit_tax {
2107   $main::lxdebug->enter_sub();
2108
2109   my $form     = $main::form;
2110   my %myconfig = %main::myconfig;
2111   my $locale   = $main::locale;
2112
2113   $main::auth->assert('config');
2114
2115   $form->{title} =  $locale->text('Edit');
2116
2117   AM->get_tax(\%myconfig, \%$form);
2118   _get_taxaccount_selection();
2119
2120   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
2121
2122   $form->header();
2123
2124   my $parameters_ref = {
2125   };
2126
2127   # Ausgabe des Templates
2128   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2129
2130   $main::lxdebug->leave_sub();
2131 }
2132
2133 sub list_tax {
2134   $main::lxdebug->enter_sub();
2135
2136   my $form     = $main::form;
2137   my %myconfig = %main::myconfig;
2138   my $locale   = $main::locale;
2139
2140   $main::auth->assert('config');
2141
2142   AM->taxes(\%myconfig, \%$form);
2143
2144   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
2145
2146   $form->{callback} = build_std_url('action=list_tax');
2147
2148   $form->{title} = $locale->text('Tax-O-Matic');
2149
2150   $form->header();
2151
2152   my $parameters_ref = {
2153   };
2154
2155   # Ausgabe des Templates
2156   print($form->parse_html_template('am/list_tax', $parameters_ref));
2157
2158   $main::lxdebug->leave_sub();
2159 }
2160
2161 sub _get_taxaccount_selection{
2162   $main::lxdebug->enter_sub();
2163
2164   my $form     = $main::form;
2165   my %myconfig = %main::myconfig;
2166
2167   $main::auth->assert('config');
2168
2169   AM->get_tax_accounts(\%myconfig, \%$form);
2170
2171   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
2172
2173   $main::lxdebug->leave_sub();
2174 }
2175
2176 sub save_tax {
2177   $main::lxdebug->enter_sub();
2178
2179   my $form     = $main::form;
2180   my %myconfig = %main::myconfig;
2181   my $locale   = $main::locale;
2182
2183   $main::auth->assert('config');
2184
2185   $form->isblank("rate", $locale->text('Taxrate missing!'));
2186   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
2187   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
2188
2189   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
2190
2191   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
2192     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
2193   }
2194
2195   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
2196     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
2197   }
2198
2199   AM->save_tax(\%myconfig, \%$form);
2200   $form->redirect($locale->text('Tax saved!'));
2201
2202   $main::lxdebug->leave_sub();
2203 }
2204
2205 sub delete_tax {
2206   $main::lxdebug->enter_sub();
2207
2208   my $form     = $main::form;
2209   my %myconfig = %main::myconfig;
2210   my $locale   = $main::locale;
2211
2212   $main::auth->assert('config');
2213
2214   AM->delete_tax(\%myconfig, \%$form);
2215   $form->redirect($locale->text('Tax deleted!'));
2216
2217   $main::lxdebug->leave_sub();
2218 }
2219
2220 sub add_price_factor {
2221   $main::lxdebug->enter_sub();
2222
2223   my $form     = $main::form;
2224   my $locale   = $main::locale;
2225
2226   $main::auth->assert('config');
2227
2228   $form->{title}      = $locale->text('Add Price Factor');
2229   $form->{callback} ||= build_std_url('action=add_price_factor');
2230   $form->{fokus}      = 'description';
2231
2232   $form->header();
2233   print $form->parse_html_template('am/edit_price_factor');
2234
2235   $main::lxdebug->leave_sub();
2236 }
2237
2238 sub edit_price_factor {
2239   $main::lxdebug->enter_sub();
2240
2241   my $form     = $main::form;
2242   my %myconfig = %main::myconfig;
2243   my $locale   = $main::locale;
2244
2245   $main::auth->assert('config');
2246
2247   $form->{title}      = $locale->text('Edit Price Factor');
2248   $form->{callback} ||= build_std_url('action=add_price_factor');
2249   $form->{fokus}      = 'description';
2250
2251   AM->get_price_factor(\%myconfig, $form);
2252
2253   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
2254
2255   $form->header();
2256   print $form->parse_html_template('am/edit_price_factor');
2257
2258   $main::lxdebug->leave_sub();
2259 }
2260
2261 sub list_price_factors {
2262   $main::lxdebug->enter_sub();
2263
2264   my $form     = $main::form;
2265   my %myconfig = %main::myconfig;
2266   my $locale   = $main::locale;
2267
2268   $main::auth->assert('config');
2269
2270   AM->get_all_price_factors(\%myconfig, \%$form);
2271
2272   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
2273     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
2274   }
2275
2276   $form->{callback} = build_std_url('action=list_price_factors');
2277   $form->{title}    = $locale->text('Price Factors');
2278   $form->{url_base} = build_std_url('callback');
2279
2280   $form->header();
2281   print $form->parse_html_template('am/list_price_factors');
2282
2283   $main::lxdebug->leave_sub();
2284 }
2285
2286 sub save_price_factor {
2287   $main::lxdebug->enter_sub();
2288
2289   my $form     = $main::form;
2290   my %myconfig = %main::myconfig;
2291   my $locale   = $main::locale;
2292
2293   $main::auth->assert('config');
2294
2295   $form->isblank("description", $locale->text('Description missing!'));
2296   $form->isblank("factor", $locale->text('Factor missing!'));
2297
2298   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
2299
2300   AM->save_price_factor(\%myconfig, $form);
2301
2302   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
2303
2304   $form->redirect($locale->text('Price factor saved!'));
2305
2306   $main::lxdebug->leave_sub();
2307 }
2308
2309 sub delete_price_factor {
2310   $main::lxdebug->enter_sub();
2311
2312   my $form     = $main::form;
2313   my %myconfig = %main::myconfig;
2314   my $locale   = $main::locale;
2315
2316   $main::auth->assert('config');
2317
2318   AM->delete_price_factor(\%myconfig, \%$form);
2319
2320   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
2321
2322   $form->redirect($locale->text('Price factor deleted!'));
2323
2324   $main::lxdebug->leave_sub();
2325 }
2326
2327 sub add_warehouse {
2328   $main::lxdebug->enter_sub();
2329
2330   my $form     = $main::form;
2331   my $locale   = $main::locale;
2332
2333   $main::auth->assert('config');
2334
2335   $form->{title}      = $locale->text('Add Warehouse');
2336   $form->{callback} ||= build_std_url('action=add_warehouse');
2337   $form->{fokus}      = 'description';
2338
2339   $form->header();
2340   print $form->parse_html_template('am/edit_warehouse');
2341
2342   $main::lxdebug->leave_sub();
2343 }
2344
2345 sub edit_warehouse {
2346   $main::lxdebug->enter_sub();
2347
2348   my $form     = $main::form;
2349   my %myconfig = %main::myconfig;
2350   my $locale   = $main::locale;
2351
2352   $main::auth->assert('config');
2353
2354   AM->get_warehouse(\%myconfig, $form);
2355
2356   $form->get_lists('employees' => 'EMPLOYEES');
2357
2358   $form->{title}      = $locale->text('Edit Warehouse');
2359   $form->{callback} ||= build_std_url('action=list_warehouses');
2360   $form->{fokus}      = 'description';
2361
2362   $form->header();
2363   print $form->parse_html_template('am/edit_warehouse');
2364
2365   $main::lxdebug->leave_sub();
2366 }
2367
2368 sub list_warehouses {
2369   $main::lxdebug->enter_sub();
2370
2371   my $form     = $main::form;
2372   my %myconfig = %main::myconfig;
2373   my $locale   = $main::locale;
2374
2375   $main::auth->assert('config');
2376
2377   AM->get_all_warehouses(\%myconfig, $form);
2378
2379   $form->{callback} = build_std_url('action=list_warehouses');
2380   $form->{title}    = $locale->text('Warehouses');
2381   $form->{url_base} = build_std_url('callback');
2382
2383   $form->header();
2384   print $form->parse_html_template('am/list_warehouses');
2385
2386   $main::lxdebug->leave_sub();
2387 }
2388
2389 sub save_warehouse {
2390   $main::lxdebug->enter_sub();
2391
2392   my $form     = $main::form;
2393   my %myconfig = %main::myconfig;
2394   my $locale   = $main::locale;
2395
2396   $main::auth->assert('config');
2397
2398   $form->isblank("description", $locale->text('Description missing!'));
2399
2400   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
2401
2402   AM->save_warehouse(\%myconfig, $form);
2403
2404   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
2405
2406   $form->redirect($locale->text('Warehouse saved.'));
2407
2408   $main::lxdebug->leave_sub();
2409 }
2410
2411 sub delete_warehouse {
2412   $main::lxdebug->enter_sub();
2413
2414   my $form     = $main::form;
2415   my %myconfig = %main::myconfig;
2416   my $locale   = $main::locale;
2417
2418   $main::auth->assert('config');
2419
2420   if (!$form->{confirmed}) {
2421     $form->{title} = $locale->text('Confirmation');
2422
2423     $form->header();
2424     print $form->parse_html_template('am/confirm_delete_warehouse');
2425     ::end_of_request();
2426   }
2427
2428   if (AM->delete_warehouse(\%myconfig, $form)) {
2429     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
2430     $form->redirect($locale->text('Warehouse deleted.'));
2431
2432   } else {
2433     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
2434   }
2435
2436   $main::lxdebug->leave_sub();
2437 }
2438
2439 sub save_bin {
2440   $main::lxdebug->enter_sub();
2441
2442   my $form     = $main::form;
2443   my %myconfig = %main::myconfig;
2444   my $locale   = $main::locale;
2445
2446   $main::auth->assert('config');
2447
2448   AM->save_bins(\%myconfig, $form);
2449
2450   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
2451
2452   $form->redirect($locale->text('Bins saved.'));
2453
2454   $main::lxdebug->leave_sub();
2455 }