ActionBar: Verwendung bei »Benutzereinstellungen«
[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., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # administration
32 #
33 #======================================================================
34
35 use utf8;
36
37 use SL::Auth;
38 use SL::Auth::PasswordPolicy;
39 use SL::AM;
40 use SL::CA;
41 use SL::Form;
42 use SL::User;
43 use SL::USTVA;
44 use SL::Iconv;
45 use SL::Locale::String qw(t8);
46 use SL::TODO;
47 use SL::DB::Printer;
48 use SL::DB::Tax;
49 use SL::DB::Language;
50 use SL::DB::Default;
51 use SL::DBUtils qw(selectall_array_query conv_dateq);
52 use CGI;
53
54 require "bin/mozilla/common.pl";
55
56 use strict;
57
58 1;
59
60 # end of main
61
62 sub add      { call_sub("add_$main::form->{type}"); }
63 sub delete   { call_sub("delete_$main::form->{type}"); }
64 sub save     { call_sub("save_$main::form->{type}"); }
65 sub edit     { call_sub("edit_$main::form->{type}"); }
66 sub continue { call_sub($main::form->{"nextsub"}); }
67 sub save_as_new { call_sub("save_as_new_$main::form->{type}"); }
68
69 sub add_account {
70   $main::lxdebug->enter_sub();
71
72   my $form     = $main::form;
73   my %myconfig = %main::myconfig;
74
75   $main::auth->assert('config');
76
77   $form->{title}     = "Add";
78   $form->{charttype} = "A";
79   AM->get_account(\%myconfig, \%$form);
80
81   $form->{callback} = "am.pl?action=list_account" unless $form->{callback};
82
83   &account_header;
84   &form_footer;
85
86   $main::lxdebug->leave_sub();
87 }
88
89 sub edit_account {
90   $main::lxdebug->enter_sub();
91
92   my $form     = $main::form;
93   my %myconfig = %main::myconfig;
94   my $defaults = SL::DB::Default->get;
95
96   $main::auth->assert('config');
97
98   $form->{title} = "Edit";
99   $form->{feature_balance} = $defaults->feature_balance;
100   $form->{feature_datev} = $defaults->feature_datev;
101   $form->{feature_erfolgsrechnung} = $defaults->feature_erfolgsrechnung;
102   $form->{feature_eurechnung} = $defaults->feature_eurechnung;
103   $form->{feature_ustva} = $defaults->feature_ustva;
104
105   AM->get_account(\%myconfig, \%$form);
106
107   foreach my $item (split(/:/, $form->{link})) {
108     $form->{$item} = "checked";
109   }
110
111   &account_header;
112   &form_footer;
113
114   $main::lxdebug->leave_sub();
115 }
116
117 sub account_header {
118   $main::lxdebug->enter_sub();
119
120   my $form     = $main::form;
121   my %myconfig = %main::myconfig;
122   my $locale   = $main::locale;
123
124   $main::auth->assert('config');
125
126   if ( $form->{action} eq 'edit_account') {
127     $form->{account_exists} = '1';
128   }
129
130   $form->{title} = $locale->text("$form->{title} Account");
131
132   $form->{"$form->{charttype}_checked"} = "checked";
133   $form->{"$form->{category}_checked"}  = "checked";
134
135   $form->{select_tax} = "";
136
137   my @tax_report_pos = USTVA->report_variables({
138       myconfig   => \%myconfig,
139       form       => $form,
140       type       => '',
141       attribute  => 'position',
142       calc       => '',
143   });
144
145   if (@{ $form->{TAXKEY} }) {
146     foreach my $item (@{ $form->{TAXKEY} }) {
147       $item->{rate} = $item->{rate} * 100 . '%';
148     }
149
150     # Fill in empty row for new Taxkey
151     my $newtaxkey_ref = {
152       id             => '',
153       chart_id       => '',
154       accno          => '',
155       tax_id         => '',
156       taxdescription => '',
157       rate           => '',
158       taxkey_id      => '',
159       pos_ustva      => '',
160       startdate      => $form->{account_exists} ? '' : DateTime->new(year => 1970, month => 1, day => 1)->to_lxoffice,
161     };
162
163     push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
164
165     my $i = 0;
166     foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
167
168       # Fill in a runningnumber
169       $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
170
171       # Fill in the Taxkeys as select options
172       foreach my $item (@{ $form->{TAXKEY} }) {
173         if ($item->{id} == $taxkey_used->{tax_id}) {
174           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
175             qq|<option value="$item->{id}" selected="selected">|
176             . sprintf("%.2d", $item->{taxkey})
177             . qq|. $item->{taxdescription} ($item->{rate}) |
178             . $locale->text('Tax-o-matic Account')
179             . qq|: $item->{chart_accno}\n|;
180         }
181         else {
182           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
183             qq|<option value="$item->{id}">|
184             . sprintf("%.2d", $item->{taxkey})
185             . qq|. $item->{taxdescription} ($item->{rate}) |
186             . $locale->text('Tax-o-matic Account')
187             . qq|: $item->{chart_accno}\n|;
188         }
189
190       }
191
192       # Fill in the USTVA Numbers as select options
193       foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
194         if ($item eq ''){
195           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
196         }
197         elsif ( $item eq $taxkey_used->{pos_ustva} ) {
198           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
199         }
200         else {
201           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
202         }
203
204       }
205
206       $i++;
207     }
208   }
209
210   # Newaccount Folgekonto
211   if (@{ $form->{NEWACCOUNT} || [] }) {
212     if (!$form->{new_chart_valid}) {
213       $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
214     }
215     foreach my $item (@{ $form->{NEWACCOUNT} }) {
216       if ($item->{id} == $form->{new_chart_id}) {
217         $form->{selectnewaccount} .=
218           qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
219       } elsif (!$form->{new_chart_valid}) {
220         $form->{selectnewaccount} .=
221           qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
222       }
223
224     }
225   }
226
227   my $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
228   my %eur = (
229           1  => "Umsatzerlöse",
230           2  => "sonstige Erlöse",
231           3  => "Privatanteile",
232           4  => "Zinserträge",
233           5  => "Ausserordentliche Erträge",
234           6  => "Vereinnahmte Umsatzst.",
235           7  => "Umsatzsteuererstattungen",
236           8  => "Wareneingänge",
237           9  => "Löhne und Gehälter",
238           10 => "Gesetzl. sozialer Aufw.",
239           11 => "Mieten",
240           12 => "Gas, Strom, Wasser",
241           13 => "Instandhaltung",
242           14 => "Steuern, Versich., Beiträge",
243           15 => "Kfz-Steuern",
244           16 => "Kfz-Versicherungen",
245           17 => "Sonst. Fahrzeugkosten",
246           18 => "Werbe- und Reisekosten",
247           19 => "Instandhaltung u. Werkzeuge",
248           20 => "Fachzeitschriften, Bücher",
249           21 => "Miete für Einrichtungen",
250           22 => "Rechts- und Beratungskosten",
251           23 => "Bürobedarf, Porto, Telefon",
252           24 => "Sonstige Aufwendungen",
253           25 => "Abschreibungen auf Anlagever.",
254           26 => "Abschreibungen auf GWG",
255           27 => "Vorsteuer",
256           28 => "Umsatzsteuerzahlungen",
257           29 => "Zinsaufwand",
258           30 => "Ausserordentlicher Aufwand",
259           31 => "Betriebliche Steuern");
260   foreach my $item (sort({ $a <=> $b } keys(%eur))) {
261     my $text = H($::locale->{iconv_utf8}->convert($eur{$item}));
262     if ($item == $form->{pos_eur}) {
263       $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
264     } else {
265       $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
266     }
267
268   }
269
270   my $select_er = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
271   my %er = (
272        1  => "Ertrag",
273        6  => "Aufwand");
274   foreach my $item (sort({ $a <=> $b } keys(%er))) {
275     my $text = H($::locale->{iconv_utf8}->convert($er{$item}));
276     if ($item == $form->{pos_er}) {
277       $select_er .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
278     } else {
279       $select_er .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
280     }
281
282   }
283
284   my $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
285
286   my %bwapos = (
287              1  => 'Umsatzerlöse',
288              2  => 'Best.Verdg.FE/UE',
289              3  => 'Aktiv.Eigenleistung',
290              4  => 'Mat./Wareneinkauf',
291              5  => 'So.betr.Erlöse',
292              10 => 'Personalkosten',
293              11 => 'Raumkosten',
294              12 => 'Betriebl.Steuern',
295              13 => 'Vers./Beiträge',
296              14 => 'Kfz.Kosten o.St.',
297              15 => 'Werbe-Reisek.',
298              16 => 'Kosten Warenabgabe',
299              17 => 'Abschreibungen',
300              18 => 'Rep./instandhlt.',
301              19 => 'Übrige Steuern',
302              20 => 'Sonst.Kosten',
303              30 => 'Zinsauwand',
304              31 => 'Sonst.neutr.Aufw.',
305              32 => 'Zinserträge',
306              33 => 'Sonst.neutr.Ertrag',
307              34 => 'Verr.kalk.Kosten',
308              35 => 'Steuern Eink.u.Ertr.');
309   foreach my $item (sort({ $a <=> $b } keys %bwapos)) {
310     my $text = H($::locale->{iconv_utf8}->convert($bwapos{$item}));
311     if ($item == $form->{pos_bwa}) {
312       $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
313     } else {
314       $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
315     }
316
317   }
318
319 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
320   my $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
321   foreach my $item ((1, 2, 3, 4)) {
322     if ($item == $form->{pos_bilanz}) {
323       $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
324     } else {
325       $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
326     }
327
328   }
329
330   # this is for our parser only! Do not remove.
331   # type=submit $locale->text('Add Account')
332   # type=submit $locale->text('Edit Account')
333
334   $form->{type} = "account";
335
336   # preselections category
337
338   my $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
339
340   my %category = (
341       'A'  => $locale->text('Asset'),
342       'L'  => $locale->text('Liability'),
343       'Q'  => $locale->text('Equity'),
344       'I'  => $locale->text('Revenue'),
345       'E'  => $locale->text('Expense'),
346       'C'  => $locale->text('Costs'),
347   );
348   foreach my $item ( sort({ $a <=> $b } keys %category) ) {
349     if ($item eq $form->{category}) {
350       $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
351     } else {
352       $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
353     }
354
355   }
356
357   # preselection chart type
358   my @all_charttypes = ({'name' => $locale->text('Account'), 'value' => 'A'},
359                         {'name' => $locale->text('Heading'), 'value' => 'H'},
360     );
361   my $selected_charttype = $form->{charttype};
362
363
364   # account where AR_tax or AP_tax is set are not orphaned if they are used as
365   # tax-o-matic account
366   if ( $form->{id} && $form->{orphaned} && ($form->{link} =~ m/(AP_tax|AR_tax)/) ) {
367     if (SL::DB::Manager::Tax->find_by(chart_id => $form->{id})) {
368       $form->{orphaned} = 0;
369     }
370   }
371
372   my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
373   my $AccountIsPosted = ($form->{orphaned} ) ? "":"1";
374
375   $form->header();
376
377   my $parameters_ref = {
378     ChartTypeIsAccount         => $ChartTypeIsAccount,
379     AccountIsPosted            => $AccountIsPosted,
380     select_category            => $select_category,
381     all_charttypes             => \@all_charttypes,
382     selected_charttype         => $selected_charttype,
383     select_bwa                 => $select_bwa,
384     select_bilanz              => $select_bilanz,
385     select_eur                 => $select_eur,
386     select_er                  => $select_er,
387   };
388
389   # Ausgabe des Templates
390   print($form->parse_html_template('am/edit_accounts', $parameters_ref));
391
392
393   $main::lxdebug->leave_sub();
394 }
395
396 sub form_footer {
397   $::lxdebug->enter_sub;
398   $::auth->assert('config');
399
400   print $::form->parse_html_template('am/form_footer', {
401     show_save        => !$::form->{id}
402                      || ($::form->{id} && $::form->{orphaned})
403                      || ($::form->{type} eq "account" && !$::form->{new_chart_valid}),
404     show_delete      => $::form->{id} && $::form->{orphaned},
405     show_save_as_new => $::form->{id} && $::form->{type} eq "account",
406   });
407
408   $::lxdebug->leave_sub;
409 }
410
411 sub save_account {
412   $main::lxdebug->enter_sub();
413
414   my $form     = $main::form;
415   my %myconfig = %main::myconfig;
416   my $locale   = $main::locale;
417
418   $main::auth->assert('config');
419
420   $form->isblank("accno",       $locale->text('Account Number missing!'));
421   $form->isblank("description", $locale->text('Account Description missing!'));
422
423   if ($form->{charttype} eq 'A'){
424     $form->isblank("category",  $locale->text('Account Type missing!'));
425
426     my $found_valid_taxkey = 0;
427     foreach my $i (0 .. 10) { # 10 is maximum count of taxkeys in form
428       if ($form->{"taxkey_startdate_$i"} and !$form->{"taxkey_del_$i"}) {
429         $found_valid_taxkey = 1;
430         last;
431       }
432     }
433     if ($found_valid_taxkey == 0) {
434       $form->error($locale->text('A valid taxkey is missing!'));
435     }
436   }
437
438   $form->redirect($locale->text('Account saved!'))
439     if (AM->save_account(\%myconfig, \%$form));
440   $form->error($locale->text('Cannot save account!'));
441
442   $main::lxdebug->leave_sub();
443 }
444
445 sub save_as_new_account {
446   $main::lxdebug->enter_sub();
447
448   my $form     = $main::form;
449   my %myconfig = %main::myconfig;
450   my $locale   = $main::locale;
451
452   $main::auth->assert('config');
453
454   $form->isblank("accno",       $locale->text('Account Number missing!'));
455   $form->isblank("description", $locale->text('Account Description missing!'));
456
457   if ($form->{charttype} eq 'A'){
458     $form->isblank("category",  $locale->text('Account Type missing!'));
459   }
460
461   for my $taxkey (0 .. 9) {
462     if ($form->{"taxkey_id_$taxkey"}) {
463       $form->{"taxkey_id_$taxkey"} = "NEW";
464     }
465   }
466
467   $form->{id} = 0;
468   $form->redirect($locale->text('Account saved!'))
469     if (AM->save_account(\%myconfig, \%$form));
470   $form->error($locale->text('Cannot save account!'));
471
472   $main::lxdebug->leave_sub();
473 }
474
475 sub list_account {
476   $main::lxdebug->enter_sub();
477
478   my $form     = $main::form;
479   my %myconfig = %main::myconfig;
480   my $locale   = $main::locale;
481
482   $main::auth->assert('config');
483
484   $form->{callback}     = build_std_url('action=list_account');
485   my $link_edit_account = build_std_url('action=edit_account', 'callback');
486
487   CA->all_accounts(\%myconfig, \%$form);
488
489   foreach my $ca (@{ $form->{CA} }) {
490
491     $ca->{debit}  = "";
492     $ca->{credit} = "";
493
494     if ($ca->{amount} > 0) {
495       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
496     }
497     if ($ca->{amount} < 0) {
498       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
499     }
500     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:'';
501     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
502   }
503
504   $::request->{layout}->use_stylesheet("list_accounts.css");
505   $form->{title}       = $locale->text('Chart of Accounts');
506
507   $form->header;
508
509
510   my $parameters_ref = {
511   #   hidden_variables                => $_hidden_variables_ref,
512   };
513
514   # Ausgabe des Templates
515   print($form->parse_html_template('am/list_accounts', $parameters_ref));
516
517   $main::lxdebug->leave_sub();
518
519 }
520
521
522 sub list_account_details {
523 # Ajax Funktion aus list_account_details
524   $main::lxdebug->enter_sub();
525
526   my $form     = $main::form;
527   my %myconfig = %main::myconfig;
528   my $locale   = $main::locale;
529
530   $main::auth->assert('config');
531
532   my $chart_id = $form->{args};
533
534   CA->all_accounts(\%myconfig, \%$form, $chart_id);
535
536   foreach my $ca (@{ $form->{CA} }) {
537
538     $ca->{debit}  = "&nbsp;";
539     $ca->{credit} = "&nbsp;";
540
541     if ($ca->{amount} > 0) {
542       $ca->{credit} =
543         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
544     }
545     if ($ca->{amount} < 0) {
546       $ca->{debit} =
547         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
548     }
549
550     my @links = split( q{:}, $ca->{link});
551
552     $ca->{link} = q{};
553
554     foreach my $link (@links){
555       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
556                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
557                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
558                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
559                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
560                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
561                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
562                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
563                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
564                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
565                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
566                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
567                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
568                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
569                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
570                : $locale->text('Unknown Link') . ': ' . $link;
571       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
572     }
573
574     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
575                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
576                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
577                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
578                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
579                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
580                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
581                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
582   }
583
584   $form->{title} = $locale->text('Chart of Accounts');
585
586   print $form->ajax_response_header, $form->parse_html_template('am/list_account_details');
587
588   $main::lxdebug->leave_sub();
589
590 }
591
592 sub delete_account {
593   $main::lxdebug->enter_sub();
594
595   my $form     = $main::form;
596   my %myconfig = %main::myconfig;
597   my $locale   = $main::locale;
598
599   $main::auth->assert('config');
600
601   $form->{title} = $locale->text('Delete Account');
602
603   foreach my $id (
604     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id rndgain_accno_id rndloss_accno_id)
605     ) {
606     if ($form->{id} == $form->{$id}) {
607       $form->error($locale->text('Cannot delete default account!'));
608     }
609   }
610
611   $form->redirect($locale->text('Account deleted!'))
612     if (AM->delete_account(\%myconfig, \%$form));
613   $form->error($locale->text('Cannot delete account!'));
614
615   $main::lxdebug->leave_sub();
616 }
617
618 sub _build_cfg_options {
619   my $form     = $main::form;
620   my %myconfig = %main::myconfig;
621
622   my $idx   = shift;
623   my $array = uc($idx) . 'S';
624
625   $form->{$array} = [];
626   foreach my $item (@_) {
627     push @{ $form->{$array} }, {
628       'name'     => $item,
629       'value'    => $item,
630       'selected' => $item eq $myconfig{$idx},
631     };
632   }
633 }
634
635 sub config {
636   $main::lxdebug->enter_sub();
637
638   my $form     = $main::form;
639   my %myconfig = %main::myconfig;
640   my $locale   = $main::locale;
641
642   _build_cfg_options('dateformat', qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
643   _build_cfg_options('timeformat', qw(hh:mm hh:mm:ss));
644   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00"));
645
646   my @formats = ();
647   if ($::lx_office_conf{print_templates}->{opendocument}
648       && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
649       && $::lx_office_conf{applications}->{xvfb}                 && (-x $::lx_office_conf{applications}->{xvfb})) {
650     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
651                      "value" => "opendocument_pdf" });
652   }
653   if ($::lx_office_conf{print_templates}->{latex}) {
654     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
655   }
656   push(@formats, { "name" => "HTML", "value" => "html" });
657   if ($::lx_office_conf{print_templates}->{latex}) {
658     push(@formats, { "name" => $locale->text("Postscript"),
659                      "value" => "postscript" });
660   }
661   if ($::lx_office_conf{print_templates}->{opendocument}) {
662     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
663                      "value" => "opendocument" });
664   }
665
666   if (!$myconfig{"template_format"}) {
667     $myconfig{"template_format"} = "pdf";
668   }
669   $form->{TEMPLATE_FORMATS} = [];
670   foreach my $item (@formats) {
671     push @{ $form->{TEMPLATE_FORMATS} }, {
672       'name'     => $item->{name},
673       'value'    => $item->{value},
674       'selected' => $item->{value} eq $myconfig{template_format},
675     };
676   }
677
678   if (!$myconfig{"default_media"}) {
679     $myconfig{"default_media"} = "screen";
680   }
681
682   my %selected = ($myconfig{"default_media"} => "selected");
683   $form->{MEDIA} = [
684     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
685     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
686     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
687     ];
688
689   $form->{PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
690
691   my %countrycodes = User->country_codes;
692
693   $form->{COUNTRYCODES} = [];
694   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
695     push @{ $form->{COUNTRYCODES} }, {
696       'name'     => $countrycodes{$countrycode},
697       'value'    => $countrycode,
698       'selected' => $countrycode eq $myconfig{countrycode},
699     };
700   }
701
702   $form->{STYLESHEETS} = [];
703   foreach my $item (qw(lx-office-erp.css kivitendo.css)) {
704     push @{ $form->{STYLESHEETS} }, {
705       'name'     => $item,
706       'value'    => $item,
707       'selected' => $item eq $myconfig{stylesheet},
708     };
709   }
710
711   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
712   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
713   $form->{todo_cfg}            = { TODO->get_user_config('login' => $::myconfig{login}) };
714
715   $form->{title}               = $locale->text('Edit Preferences for #1', $::myconfig{login});
716
717   setup_am_config_action_bar();
718
719   $form->header();
720
721   $form->{full_signature} = $form->create_email_signature();
722
723   print $form->parse_html_template('am/config');
724
725   $main::lxdebug->leave_sub();
726 }
727
728 sub save_preferences {
729   $main::lxdebug->enter_sub();
730
731   my $form     = $main::form;
732   my %myconfig = %main::myconfig;
733   my $locale   = $main::locale;
734
735   $form->{stylesheet} = $form->{usestylesheet};
736
737   TODO->save_user_config('login' => $::myconfig{login}, %{ $form->{todo_cfg} || { } });
738
739   if (AM->save_preferences($form)) {
740     if ($::auth->can_change_password()
741         && defined $form->{new_password}
742         && ($form->{new_password} ne '********')) {
743       my $verifier = SL::Auth::PasswordPolicy->new;
744       my $result   = $verifier->verify($form->{new_password});
745
746       if ($result != SL::Auth::PasswordPolicy->OK()) {
747         $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
748       }
749
750       $::auth->change_password($::myconfig{login}, $form->{new_password});
751     }
752
753     $form->redirect($locale->text('Preferences saved!'));
754   }
755
756   $form->error($locale->text('Cannot save preferences!'));
757
758   $main::lxdebug->leave_sub();
759 }
760
761 sub audit_control {
762   $::lxdebug->enter_sub;
763   $::auth->assert('config');
764
765   $::form->{title} = $::locale->text('Audit Control');
766
767   AM->closedto(\%::myconfig, $::form);
768
769   $::form->header;
770   print $::form->parse_html_template('am/audit_control');
771
772   $::lxdebug->leave_sub;
773 }
774
775 sub doclose {
776   $main::lxdebug->enter_sub();
777
778   my $form     = $main::form;
779   my %myconfig = %main::myconfig;
780   my $locale   = $main::locale;
781
782   $main::auth->assert('config');
783
784   AM->closebooks(\%myconfig, \%$form);
785
786   if ($form->{closedto}) {
787     $form->redirect(
788                     $locale->text('Books closed up to') . " "
789                       . $locale->date(\%myconfig, $form->{closedto}, 1));
790   } else {
791     $form->redirect($locale->text('Books are open'));
792   }
793
794   $main::lxdebug->leave_sub();
795 }
796
797 sub edit_units {
798   $main::lxdebug->enter_sub();
799
800   my $form     = $main::form;
801   my %myconfig = %main::myconfig;
802   my $locale   = $main::locale;
803
804   $main::auth->assert('config');
805
806   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
807   AM->units_in_use(\%myconfig, $form, $units);
808   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
809
810   my @languages = AM->language(\%myconfig, $form, 1);
811
812   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
813
814   my $i = 1;
815   foreach (@unit_list) {
816     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
817     $_->{"UNITLANGUAGES"} = [];
818     foreach my $lang (@languages) {
819       push(@{ $_->{"UNITLANGUAGES"} },
820            { "idx" => $i,
821              "unit" => $_->{"name"},
822              "language_id" => $lang->{"id"},
823              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
824              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
825            });
826     }
827     $i++;
828   }
829
830   $units = AM->retrieve_units(\%myconfig, $form);
831   my $ddbox = AM->unit_select_data($units, undef, 1);
832
833   $form->{"title"} = $locale->text("Add and edit units");
834   $form->header();
835   print($form->parse_html_template("am/edit_units",
836                                    { "UNITS"               => \@unit_list,
837                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
838                                      "LANGUAGES"           => \@languages,
839                                    }));
840
841   $main::lxdebug->leave_sub();
842 }
843
844 sub add_unit {
845   $main::lxdebug->enter_sub();
846
847   my $form     = $main::form;
848   my %myconfig = %main::myconfig;
849   my $locale   = $main::locale;
850
851   $main::auth->assert('config');
852
853   $form->isblank("new_name", $locale->text("The name is missing."));
854   my $units = AM->retrieve_units(\%myconfig, $form);
855   my $all_units = AM->retrieve_units(\%myconfig, $form);
856   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
857
858   my ($base_unit, $factor);
859   if ($form->{"new_base_unit"}) {
860     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
861
862     $form->isblank("new_factor", $locale->text("The factor is missing."));
863     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
864     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
865     $base_unit = $form->{"new_base_unit"};
866   }
867
868   my @languages;
869   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
870     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
871     push(@languages, { "id" => $lang->{"id"},
872                        "localized" => $form->{"new_localized_$lang->{id}"},
873                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
874          });
875   }
876
877   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
878
879   $form->{"saved_message"} = $locale->text("The unit has been saved.");
880
881   edit_units();
882
883   $main::lxdebug->leave_sub();
884 }
885
886 sub set_unit_languages {
887   $main::lxdebug->enter_sub();
888
889   my $form     = $main::form;
890
891   $main::auth->assert('config');
892
893   my ($unit, $languages, $idx) = @_;
894
895   $unit->{"LANGUAGES"} = [];
896
897   foreach my $lang (@{$languages}) {
898     push(@{ $unit->{"LANGUAGES"} },
899          { "id" => $lang->{"id"},
900            "localized" => $form->{"localized_${idx}_$lang->{id}"},
901            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
902          });
903   }
904
905   $main::lxdebug->leave_sub();
906 }
907
908 sub save_unit {
909   $main::lxdebug->enter_sub();
910
911   my $form     = $main::form;
912   my %myconfig = %main::myconfig;
913   my $locale   = $main::locale;
914
915   $main::auth->assert('config');
916
917   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
918   AM->units_in_use(\%myconfig, $form, $old_units);
919
920   my @languages = AM->language(\%myconfig, $form, 1);
921
922   my $new_units = {};
923   my @delete_units = ();
924   foreach my $i (1..($form->{"rowcount"} * 1)) {
925     my $old_unit = $old_units->{$form->{"old_name_$i"}};
926     if (!$old_unit) {
927       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
928     }
929
930     if ($form->{"unchangeable_$i"}) {
931       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
932       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
933       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
934       next;
935     }
936
937     if ($old_unit->{"in_use"}) {
938       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
939     }
940
941     if ($form->{"delete_$i"}) {
942       push(@delete_units, $old_unit->{"name"});
943       next;
944     }
945
946     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
947
948     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
949     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
950     $new_units->{$form->{"name_$i"}} = \%h;
951     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
952     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
953   }
954
955   foreach my $unit (values(%{$new_units})) {
956     next unless ($unit->{"old_name"});
957     if ($unit->{"base_unit"}) {
958       $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"}))
959         unless (defined($new_units->{$unit->{"base_unit"}}));
960       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
961       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
962     } else {
963       $unit->{"base_unit"} = undef;
964       $unit->{"factor"} = undef;
965     }
966   }
967
968   foreach my $unit (values(%{$new_units})) {
969     next if ($unit->{"unchanged_unit"});
970
971     map({ $_->{"seen"} = 0; } values(%{$new_units}));
972     my $new_unit = $unit;
973     while ($new_unit->{"base_unit"}) {
974       $new_unit->{"seen"} = 1;
975       $new_unit = $new_units->{$new_unit->{"base_unit"}};
976       if ($new_unit->{"seen"}) {
977         $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, " .
978                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
979       }
980     }
981   }
982
983   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
984
985   $form->{"saved_message"} = $locale->text("The units have been saved.");
986
987   edit_units();
988
989   $main::lxdebug->leave_sub();
990 }
991
992 sub show_history_search {
993   $main::lxdebug->enter_sub();
994
995   my $form     = $main::form;
996   my $locale   = $main::locale;
997
998   $main::auth->assert('config');
999
1000   $form->{title} = $locale->text("History Search");
1001   $form->header();
1002
1003   print $form->parse_html_template("common/search_history");
1004
1005   $main::lxdebug->leave_sub();
1006 }
1007
1008 sub show_am_history {
1009   $main::lxdebug->enter_sub();
1010
1011   my $form     = $main::form;
1012   my %myconfig = %main::myconfig;
1013   my $locale   = $main::locale;
1014
1015   $main::auth->assert('config');
1016
1017   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
1018   $form->{order} ||= 'h.itime--1';
1019
1020   # my %search = ( "Artikelnummer"          => "parts",
1021   #                "Kundennummer"           => "customer",
1022   #                "Lieferantennummer"      => "vendor",
1023   #                "Projektnummer"          => "project",
1024   #                "Auftragsnummer"         => "oe",
1025   #                "Angebotsnummer"         => "oe",
1026   #                "Eingangsrechnungnummer" => "ap",
1027   #                "Ausgangsrechnungnummer" => "ar",
1028   #                "Mahnungsnummer"         => "dunning",
1029   #                "Buchungsnummer"         => "gl",
1030   # );
1031
1032   my %searchNo = ( "Artikelnummer"          => "partnumber",
1033                    "Kundennummer"           => "customernumber",
1034                    "Lieferantennummer"      => "vendornumber",
1035                    "Projektnummer"          => "projectnumber",
1036                    "Auftragsnummer"         => "ordnumber",
1037                    "Angebotsnummer"         => "quonumber",
1038                    "Eingangsrechnungnummer" => "invnumber",
1039                    "Ausgangsrechnungnummer" => "invnumber",
1040                    "Mahnungsnummer"         => "dunning_id",
1041                    "Buchungsnummer"         => "gltransaction"
1042     );
1043
1044   my $dbh = $form->dbconnect(\%myconfig);
1045
1046   my $restriction;
1047   $restriction     = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
1048   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
1049   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
1050   if ($form->{mitarbeiter} =~ m/^\d+$/) {
1051     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
1052   } elsif ($form->{mitarbeiter}) {
1053     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
1054   }
1055
1056   my $snumbers_where = '';
1057   my $snumbers_value;
1058   if ($form->{'searchid'}) {
1059     $snumbers_where = ' WHERE snumbers = ?';
1060     $snumbers_value = $searchNo{$form->{'what2search'}} . '_' . $form->{'searchid'};
1061   } else {
1062     $snumbers_where = ' WHERE snumbers ~ ?';
1063     $snumbers_value = '^' . $searchNo{$form->{'what2search'}};
1064   }
1065   my $query = qq|SELECT trans_id AS id FROM history_erp $snumbers_where|;
1066
1067   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query, $snumbers_value);
1068   my $daten .= shift @ids;
1069   if (scalar(@ids) > 0 ) {
1070     $daten  .= ' OR trans_id IN (' . join(',', @ids) . ')';
1071   }
1072   my ($sort, $sortby) = split(/\-\-/, $form->{order});
1073   $sort =~ s/.*\.(.*)$/$1/;
1074
1075   $form->{title} = $locale->text("History Search");
1076   $form->header();
1077
1078   print $form->parse_html_template("common/show_history",
1079                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
1080                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
1081                                      "NONEWWINDOW"    => 1,
1082                                      uc($sort)        => 1,
1083                                      uc($sort) . "BY" => $sortby,
1084                                      'callback'       => $callback,
1085                                    });
1086   $dbh->disconnect();
1087
1088   $main::lxdebug->leave_sub();
1089 }
1090
1091 sub add_tax {
1092   $main::lxdebug->enter_sub();
1093
1094   my $form     = $main::form;
1095   my $locale   = $main::locale;
1096
1097   $main::auth->assert('config');
1098
1099   $form->{title} =  $locale->text('Add');
1100
1101   $form->{callback} ||= "am.pl?action=add_tax";
1102
1103   _get_taxaccount_selection();
1104
1105   $form->{asset}      = 1;
1106   $form->{liability}  = 1;
1107   $form->{equity}     = 1;
1108   $form->{revenue}    = 1;
1109   $form->{expense}    = 1;
1110   $form->{costs}      = 1;
1111
1112   $form->header();
1113
1114   my $parameters_ref = {
1115 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
1116     LANGUAGES => SL::DB::Manager::Language->get_all_sorted,
1117   };
1118
1119   # Ausgabe des Templates
1120   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1121
1122   $main::lxdebug->leave_sub();
1123 }
1124
1125 sub edit_tax {
1126   $main::lxdebug->enter_sub();
1127
1128   my $form     = $main::form;
1129   my %myconfig = %main::myconfig;
1130   my $locale   = $main::locale;
1131
1132   $main::auth->assert('config');
1133
1134   $form->{title} =  $locale->text('Edit');
1135
1136   AM->get_tax(\%myconfig, \%$form);
1137
1138   _get_taxaccount_selection();
1139
1140   $form->{asset}      = $form->{chart_categories} =~ 'A' ? 1 : 0;
1141   $form->{liability}  = $form->{chart_categories} =~ 'L' ? 1 : 0;
1142   $form->{equity}     = $form->{chart_categories} =~ 'Q' ? 1 : 0;
1143   $form->{revenue}    = $form->{chart_categories} =~ 'I' ? 1 : 0;
1144   $form->{expense}    = $form->{chart_categories} =~ 'E' ? 1 : 0;
1145   $form->{costs}      = $form->{chart_categories} =~ 'C' ? 1 : 0;
1146
1147   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
1148
1149   $form->header();
1150
1151   my $parameters_ref = {
1152     LANGUAGES => SL::DB::Manager::Language->get_all_sorted,
1153     TAX       => SL::DB::Manager::Tax->find_by(id => $form->{id}),
1154   };
1155
1156   # Ausgabe des Templates
1157   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1158
1159   $main::lxdebug->leave_sub();
1160 }
1161
1162 sub list_tax {
1163   $main::lxdebug->enter_sub();
1164
1165   my $form     = $main::form;
1166   my %myconfig = %main::myconfig;
1167   my $locale   = $main::locale;
1168
1169   $main::auth->assert('config');
1170
1171   AM->taxes(\%myconfig, \%$form);
1172
1173   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
1174
1175   $form->{callback} = build_std_url('action=list_tax');
1176
1177   $form->{title} = $locale->text('Tax-O-Matic');
1178
1179   $form->header();
1180
1181   my $parameters_ref = {
1182   };
1183
1184   # Ausgabe des Templates
1185   print($form->parse_html_template('am/list_tax', $parameters_ref));
1186
1187   $main::lxdebug->leave_sub();
1188 }
1189
1190 sub _get_taxaccount_selection{
1191   $main::lxdebug->enter_sub();
1192
1193   my $form     = $main::form;
1194   my %myconfig = %main::myconfig;
1195
1196   $main::auth->assert('config');
1197
1198   AM->get_tax_accounts(\%myconfig, \%$form);
1199
1200   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
1201
1202   $main::lxdebug->leave_sub();
1203 }
1204
1205 sub save_tax {
1206   $main::lxdebug->enter_sub();
1207
1208   my $form     = $main::form;
1209   my %myconfig = %main::myconfig;
1210   my $locale   = $main::locale;
1211
1212   $main::auth->assert('config');
1213
1214   $form->error($locale->text('Taxkey  missing!')) unless length($form->{taxkey}) != 0;
1215   $form->error($locale->text('Taxdescription  missing!')) unless length($form->{taxdescription}) != 0;
1216   $form->error($locale->text('Taxrate missing!')) unless length($form->{rate}) != 0;
1217
1218   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
1219
1220   if ($form->{taxkey} == 0 and $form->{rate} > 0) {
1221     $form->error($locale->text('Taxkey 0 is reserved for rate 0'));
1222   }
1223
1224   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
1225     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1226   }
1227
1228   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
1229     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1230   }
1231
1232   my @translation_keys  =  grep { $_ =~ '^translation_\d+' } keys %$form;
1233   $form->{translations} = { map { $_ =~ '^translation_(\d+)'; $1 => $form->{$_} } @translation_keys };
1234
1235   AM->save_tax(\%myconfig, \%$form);
1236   $form->redirect($locale->text('Tax saved!'));
1237
1238   $main::lxdebug->leave_sub();
1239 }
1240
1241 sub delete_tax {
1242   $main::lxdebug->enter_sub();
1243
1244   my $form     = $main::form;
1245   my %myconfig = %main::myconfig;
1246   my $locale   = $main::locale;
1247
1248   $main::auth->assert('config');
1249
1250   AM->delete_tax(\%myconfig, \%$form);
1251   $form->redirect($locale->text('Tax deleted!'));
1252
1253   $main::lxdebug->leave_sub();
1254 }
1255
1256 sub add_warehouse {
1257   $main::lxdebug->enter_sub();
1258
1259   my $form     = $main::form;
1260   my $locale   = $main::locale;
1261
1262   $main::auth->assert('config');
1263
1264   $form->{title}      = $locale->text('Add Warehouse');
1265   $form->{callback} ||= build_std_url('action=add_warehouse');
1266
1267   $form->header();
1268   print $form->parse_html_template('am/edit_warehouse');
1269
1270   $main::lxdebug->leave_sub();
1271 }
1272
1273 sub edit_warehouse {
1274   $main::lxdebug->enter_sub();
1275
1276   my $form     = $main::form;
1277   my %myconfig = %main::myconfig;
1278   my $locale   = $main::locale;
1279
1280   $main::auth->assert('config');
1281
1282   AM->get_warehouse(\%myconfig, $form);
1283
1284   $form->get_lists('employees' => 'EMPLOYEES');
1285
1286   $form->{title}      = $locale->text('Edit Warehouse');
1287   $form->{callback} ||= build_std_url('action=list_warehouses');
1288
1289   $form->header();
1290   print $form->parse_html_template('am/edit_warehouse');
1291
1292   $main::lxdebug->leave_sub();
1293 }
1294
1295 sub list_warehouses {
1296   $main::lxdebug->enter_sub();
1297
1298   my $form     = $main::form;
1299   my %myconfig = %main::myconfig;
1300   my $locale   = $main::locale;
1301
1302   $main::auth->assert('config');
1303
1304   AM->get_all_warehouses(\%myconfig, $form);
1305
1306   $form->{callback} = build_std_url('action=list_warehouses');
1307   $form->{title}    = $locale->text('Warehouses');
1308   $form->{url_base} = build_std_url('callback');
1309
1310   $form->header();
1311   print $form->parse_html_template('am/list_warehouses');
1312
1313   $main::lxdebug->leave_sub();
1314 }
1315
1316 sub save_warehouse {
1317   $main::lxdebug->enter_sub();
1318
1319   my $form     = $main::form;
1320   my %myconfig = %main::myconfig;
1321   my $locale   = $main::locale;
1322
1323   $main::auth->assert('config');
1324
1325   $form->isblank("description", $locale->text('Description missing!'));
1326
1327   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
1328
1329   AM->save_warehouse(\%myconfig, $form);
1330
1331   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
1332
1333   $form->redirect($locale->text('Warehouse saved.'));
1334
1335   $main::lxdebug->leave_sub();
1336 }
1337
1338 sub delete_warehouse {
1339   $main::lxdebug->enter_sub();
1340
1341   my $form     = $main::form;
1342   my %myconfig = %main::myconfig;
1343   my $locale   = $main::locale;
1344
1345   $main::auth->assert('config');
1346
1347   if (!$form->{confirmed}) {
1348     $form->{title} = $locale->text('Confirmation');
1349
1350     $form->header();
1351     print $form->parse_html_template('am/confirm_delete_warehouse');
1352     $::dispatcher->end_request;
1353   }
1354
1355   if (AM->delete_warehouse(\%myconfig, $form)) {
1356     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
1357     $form->redirect($locale->text('Warehouse deleted.'));
1358
1359   } else {
1360     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
1361   }
1362
1363   $main::lxdebug->leave_sub();
1364 }
1365
1366 sub save_bin {
1367   $main::lxdebug->enter_sub();
1368
1369   my $form     = $main::form;
1370   my %myconfig = %main::myconfig;
1371   my $locale   = $main::locale;
1372
1373   $main::auth->assert('config');
1374
1375   AM->save_bins(\%myconfig, $form);
1376
1377   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
1378
1379   $form->redirect($locale->text('Bins saved.'));
1380
1381   $main::lxdebug->leave_sub();
1382 }
1383
1384 sub setup_am_config_action_bar {
1385   my %params = @_;
1386
1387   for my $bar ($::request->layout->get('actionbar')) {
1388     $bar->add(
1389       action => [
1390         t8('Save'),
1391         submit    => [ '#form', { action => "save_preferences" } ],
1392         accesskey => 'enter',
1393       ],
1394     );
1395   }
1396 }