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