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