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