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