am/list_lead nach template portiert
[kivitendo-erp.git] / bin / mozilla / am.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # administration
31 #
32 #======================================================================
33
34 use utf8;
35
36 use SL::Auth;
37 use SL::Auth::PasswordPolicy;
38 use SL::AM;
39 use SL::CA;
40 use SL::Form;
41 use SL::User;
42 use SL::USTVA;
43 use SL::Iconv;
44 use SL::TODO;
45 use SL::Printer;
46 use CGI;
47
48 require "bin/mozilla/common.pl";
49
50 use strict;
51
52 1;
53
54 # end of main
55
56 sub add      { call_sub("add_$main::form->{type}"); }
57 sub delete   { call_sub("delete_$main::form->{type}"); }
58 sub save     { call_sub("save_$main::form->{type}"); }
59 sub edit     { call_sub("edit_$main::form->{type}"); }
60 sub continue { call_sub($main::form->{"nextsub"}); }
61 sub save_as_new { call_sub("save_as_new_$main::form->{type}"); }
62
63 sub add_account {
64   $main::lxdebug->enter_sub();
65
66   my $form     = $main::form;
67   my %myconfig = %main::myconfig;
68
69   $main::auth->assert('config');
70
71   $form->{title}     = "Add";
72   $form->{charttype} = "A";
73   AM->get_account(\%myconfig, \%$form);
74
75   $form->{callback} = "am.pl?action=list_account" unless $form->{callback};
76
77   &account_header;
78   &form_footer;
79
80   $main::lxdebug->leave_sub();
81 }
82
83 sub edit_account {
84   $main::lxdebug->enter_sub();
85
86   my $form     = $main::form;
87   my %myconfig = %main::myconfig;
88
89   $main::auth->assert('config');
90
91   $form->{title} = "Edit";
92   AM->get_account(\%myconfig, \%$form);
93
94   foreach my $item (split(/:/, $form->{link})) {
95     $form->{$item} = "checked";
96   }
97
98   &account_header;
99   &form_footer;
100
101   $main::lxdebug->leave_sub();
102 }
103
104 sub account_header {
105   $main::lxdebug->enter_sub();
106
107   my $form     = $main::form;
108   my %myconfig = %main::myconfig;
109   my $locale   = $main::locale;
110
111   $main::auth->assert('config');
112
113   if ( $form->{action} eq 'edit_account') {
114     $form->{account_exists} = '1';
115   }
116
117   $form->{title} = $locale->text("$form->{title} Account");
118
119   $form->{"$form->{charttype}_checked"} = "checked";
120   $form->{"$form->{category}_checked"}  = "checked";
121
122   $form->{select_tax} = "";
123
124   my @tax_report_pos = USTVA->report_variables({
125       myconfig   => \%myconfig,
126       form       => $form,
127       type       => '',
128       attribute  => 'position',
129       calc       => '',
130   });
131
132   if (@{ $form->{TAXKEY} }) {
133     foreach my $item (@{ $form->{TAXKEY} }) {
134       $item->{rate} = $item->{rate} * 100 . '%';
135     }
136
137     # Fill in empty row for new Taxkey
138     my $newtaxkey_ref = {
139       id             => '',
140       chart_id       => '',
141       accno          => '',
142       tax_id         => '',
143       taxdescription => '',
144       rate           => '',
145       taxkey_id      => '',
146       pos_ustva      => '',
147       startdate      => '',
148     };
149
150     push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
151
152     my $i = 0;
153     foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
154
155       # Fill in a runningnumber
156       $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
157
158       # Fill in the Taxkeys as select options
159       foreach my $item (@{ $form->{TAXKEY} }) {
160         if ($item->{id} == $taxkey_used->{tax_id}) {
161           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
162             qq|<option value="$item->{id}" selected="selected">|
163             . sprintf("%.2d", $item->{taxkey})
164             . qq|. $item->{taxdescription} ($item->{rate}) |
165             . $locale->text('Tax-o-matic Account')
166             . qq|: $item->{chart_accno}\n|;
167         }
168         else {
169           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
170             qq|<option value="$item->{id}">|
171             . sprintf("%.2d", $item->{taxkey})
172             . qq|. $item->{taxdescription} ($item->{rate}) |
173             . $locale->text('Tax-o-matic Account')
174             . qq|: $item->{chart_accno}\n|;
175         }
176
177       }
178
179       # Fill in the USTVA Numbers as select options
180       foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
181         if ($item eq ''){
182           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
183         }
184         elsif ( $item eq $taxkey_used->{pos_ustva} ) {
185           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
186         }
187         else {
188           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
189         }
190
191       }
192
193       $i++;
194     }
195   }
196
197   # Newaccount Folgekonto
198   if (@{ $form->{NEWACCOUNT} || [] }) {
199     if (!$form->{new_chart_valid}) {
200       $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
201     }
202     foreach my $item (@{ $form->{NEWACCOUNT} }) {
203       if ($item->{id} == $form->{new_chart_id}) {
204         $form->{selectnewaccount} .=
205           qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
206       } elsif (!$form->{new_chart_valid}) {
207         $form->{selectnewaccount} .=
208           qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
209       }
210
211     }
212   }
213
214   my $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
215   my %eur = (
216           1  => "Umsatzerlöse",
217           2  => "sonstige Erlöse",
218           3  => "Privatanteile",
219           4  => "Zinserträge",
220           5  => "Ausserordentliche Erträge",
221           6  => "Vereinnahmte Umsatzst.",
222           7  => "Umsatzsteuererstattungen",
223           8  => "Wareneingänge",
224           9  => "Löhne und Gehälter",
225           10 => "Gesetzl. sozialer Aufw.",
226           11 => "Mieten",
227           12 => "Gas, Strom, Wasser",
228           13 => "Instandhaltung",
229           14 => "Steuern, Versich., Beiträge",
230           15 => "Kfz-Steuern",
231           16 => "Kfz-Versicherungen",
232           17 => "Sonst. Fahrzeugkosten",
233           18 => "Werbe- und Reisekosten",
234           19 => "Instandhaltung u. Werkzeuge",
235           20 => "Fachzeitschriften, Bücher",
236           21 => "Miete für Einrichtungen",
237           22 => "Rechts- und Beratungskosten",
238           23 => "Bürobedarf, Porto, Telefon",
239           24 => "Sonstige Aufwendungen",
240           25 => "Abschreibungen auf Anlagever.",
241           26 => "Abschreibungen auf GWG",
242           27 => "Vorsteuer",
243           28 => "Umsatzsteuerzahlungen",
244           29 => "Zinsaufwand",
245           30 => "Ausserordentlicher Aufwand",
246           31 => "Betriebliche Steuern");
247   foreach my $item (sort({ $a <=> $b } keys(%eur))) {
248     my $text = H($::locale->{iconv_utf8}->convert($eur{$item}));
249     if ($item == $form->{pos_eur}) {
250       $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
251     } else {
252       $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
253     }
254
255   }
256
257   my $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
258
259   my %bwapos = (
260              1  => 'Umsatzerlöse',
261              2  => 'Best.Verdg.FE/UE',
262              3  => 'Aktiv.Eigenleistung',
263              4  => 'Mat./Wareneinkauf',
264              5  => 'So.betr.Erlöse',
265              10 => 'Personalkosten',
266              11 => 'Raumkosten',
267              12 => 'Betriebl.Steuern',
268              13 => 'Vers./Beiträge',
269              14 => 'Kfz.Kosten o.St.',
270              15 => 'Werbe-Reisek.',
271              16 => 'Kosten Warenabgabe',
272              17 => 'Abschreibungen',
273              18 => 'Rep./instandhlt.',
274              19 => 'Übrige Steuern',
275              20 => 'Sonst.Kosten',
276              30 => 'Zinsauwand',
277              31 => 'Sonst.neutr.Aufw.',
278              32 => 'Zinserträge',
279              33 => 'Sonst.neutr.Ertrag',
280              34 => 'Verr.kalk.Kosten',
281              35 => 'Steuern Eink.u.Ertr.');
282   foreach my $item (sort({ $a <=> $b } keys %bwapos)) {
283     my $text = H($::locale->{iconv_utf8}->convert($bwapos{$item}));
284     if ($item == $form->{pos_bwa}) {
285       $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
286     } else {
287       $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
288     }
289
290   }
291
292 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
293   my $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
294   foreach my $item ((1, 2, 3, 4)) {
295     if ($item == $form->{pos_bilanz}) {
296       $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
297     } else {
298       $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
299     }
300
301   }
302
303   # this is for our parser only! Do not remove.
304   # type=submit $locale->text('Add Account')
305   # type=submit $locale->text('Edit Account')
306
307   $form->{type} = "account";
308
309   # preselections category
310
311   my $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
312
313   my %category = (
314       'A'  => $locale->text('Asset'),
315       'L'  => $locale->text('Liability'),
316       'Q'  => $locale->text('Equity'),
317       'I'  => $locale->text('Revenue'),
318       'E'  => $locale->text('Expense'),
319       'C'  => $locale->text('Costs'),
320   );
321   foreach my $item ( sort({ $a <=> $b } keys %category) ) {
322     if ($item eq $form->{category}) {
323       $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
324     } else {
325       $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
326     }
327
328   }
329
330   # preselection chart type
331   my @all_charttypes = ({'name' => $locale->text('Account'), 'value' => 'A'},
332                         {'name' => $locale->text('Heading'), 'value' => 'H'},
333     );
334   my $selected_charttype = $form->{charttype};
335
336
337   # account where AR_tax or AP_tax is set are not orphaned if they are used as
338   # tax-o-matic account
339   if ( $form->{id} && !$form->{orphaned} && ($form->{link} =~ m/(AP_tax|AR_tax)/) ) {
340     if (SL::DB::Manager::Tax->find_by(chart_id => $form->{id})) {
341       $form->{orphaned} = 0;
342     }
343   }
344
345   my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
346   my $AccountIsPosted = ($form->{orphaned} ) ? "":"1";
347
348   $form->header();
349
350   my $parameters_ref = {
351     ChartTypeIsAccount         => $ChartTypeIsAccount,
352     AccountIsPosted            => $AccountIsPosted,
353     select_category            => $select_category,
354     all_charttypes             => \@all_charttypes,
355     selected_charttype         => $selected_charttype,
356     select_bwa                 => $select_bwa,
357     select_bilanz              => $select_bilanz,
358     select_eur                 => $select_eur,
359   };
360
361   # Ausgabe des Templates
362   print($form->parse_html_template('am/edit_accounts', $parameters_ref));
363
364
365   $main::lxdebug->leave_sub();
366 }
367
368 sub form_footer {
369   $::lxdebug->enter_sub;
370   $::auth->assert('config');
371
372   print $::form->parse_html_template('am/form_footer', {
373     show_save        => !$::form->{id}
374                      || ($::form->{id} && $::form->{orphaned})
375                      || ($::form->{type} eq "account" && !$::form->{new_chart_valid}),
376     show_delete      => $::form->{id} && $::form->{orphaned},
377     show_save_as_new => $::form->{id} && $::form->{type} eq "account",
378   });
379
380   $::lxdebug->leave_sub;
381 }
382
383 sub save_account {
384   $main::lxdebug->enter_sub();
385
386   my $form     = $main::form;
387   my %myconfig = %main::myconfig;
388   my $locale   = $main::locale;
389
390   $main::auth->assert('config');
391
392   $form->isblank("accno",       $locale->text('Account Number missing!'));
393   $form->isblank("description", $locale->text('Account Description missing!'));
394
395   if ($form->{charttype} eq 'A'){
396     $form->isblank("category",  $locale->text('Account Type missing!'));
397   }
398
399   $form->redirect($locale->text('Account saved!'))
400     if (AM->save_account(\%myconfig, \%$form));
401   $form->error($locale->text('Cannot save account!'));
402
403   $main::lxdebug->leave_sub();
404 }
405
406 sub save_as_new_account {
407   $main::lxdebug->enter_sub();
408
409   my $form     = $main::form;
410   my %myconfig = %main::myconfig;
411   my $locale   = $main::locale;
412
413   $main::auth->assert('config');
414
415   $form->isblank("accno",       $locale->text('Account Number missing!'));
416   $form->isblank("description", $locale->text('Account Description missing!'));
417
418   if ($form->{charttype} eq 'A'){
419     $form->isblank("category",  $locale->text('Account Type missing!'));
420   }
421
422   for my $taxkey (0 .. 9) {
423     if ($form->{"taxkey_id_$taxkey"}) {
424       $form->{"taxkey_id_$taxkey"} = "NEW";
425     }
426   }
427
428   $form->{id} = 0;
429   if ($form->{"original_accno"} &&
430       ($form->{"accno"} eq $form->{"original_accno"})) {
431     $form->error($locale->text('Account Number already used!'));
432   }
433   $form->redirect($locale->text('Account saved!'))
434     if (AM->save_account(\%myconfig, \%$form));
435   $form->error($locale->text('Cannot save account!'));
436
437   $main::lxdebug->leave_sub();
438 }
439
440 sub list_account {
441   $main::lxdebug->enter_sub();
442
443   my $form     = $main::form;
444   my %myconfig = %main::myconfig;
445   my $locale   = $main::locale;
446
447   $main::auth->assert('config');
448
449   $form->{callback}     = build_std_url('action=list_account');
450   my $link_edit_account = build_std_url('action=edit_account', 'callback');
451
452   CA->all_accounts(\%myconfig, \%$form);
453
454   foreach my $ca (@{ $form->{CA} }) {
455
456     $ca->{debit}  = "";
457     $ca->{credit} = "";
458
459     if ($ca->{amount} > 0) {
460       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
461     }
462     if ($ca->{amount} < 0) {
463       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
464     }
465     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:'';
466     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
467   }
468
469   $form->use_stylesheet("list_accounts.css");
470   $form->{title}       = $locale->text('Chart of Accounts');
471
472   $form->header;
473
474
475   my $parameters_ref = {
476   #   hidden_variables                => $_hidden_variables_ref,
477   };
478
479   # Ausgabe des Templates
480   print($form->parse_html_template('am/list_accounts', $parameters_ref));
481
482   $main::lxdebug->leave_sub();
483
484 }
485
486
487 sub list_account_details {
488 # Ajax Funktion aus list_account_details
489   $main::lxdebug->enter_sub();
490
491   my $form     = $main::form;
492   my %myconfig = %main::myconfig;
493   my $locale   = $main::locale;
494
495   $main::auth->assert('config');
496
497   my $chart_id = $form->{args};
498
499   CA->all_accounts(\%myconfig, \%$form, $chart_id);
500
501   foreach my $ca (@{ $form->{CA} }) {
502
503     $ca->{debit}  = "&nbsp;";
504     $ca->{credit} = "&nbsp;";
505
506     if ($ca->{amount} > 0) {
507       $ca->{credit} =
508         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
509     }
510     if ($ca->{amount} < 0) {
511       $ca->{debit} =
512         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
513     }
514
515     my @links = split( q{:}, $ca->{link});
516
517     $ca->{link} = q{};
518
519     foreach my $link (@links){
520       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
521                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
522                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
523                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
524                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
525                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
526                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
527                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
528                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
529                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
530                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
531                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
532                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
533                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
534                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
535                : $locale->text('Unknown Link') . ': ' . $link;
536       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
537     }
538
539     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
540                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
541                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
542                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
543                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
544                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
545                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
546                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
547   }
548
549   $form->{title} = $locale->text('Chart of Accounts');
550
551   print $form->ajax_response_header, $form->parse_html_template('am/list_account_details');
552
553   $main::lxdebug->leave_sub();
554
555 }
556
557 sub delete_account {
558   $main::lxdebug->enter_sub();
559
560   my $form     = $main::form;
561   my %myconfig = %main::myconfig;
562   my $locale   = $main::locale;
563
564   $main::auth->assert('config');
565
566   $form->{title} = $locale->text('Delete Account');
567
568   foreach my $id (
569     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
570     ) {
571     if ($form->{id} == $form->{$id}) {
572       $form->error($locale->text('Cannot delete default account!'));
573     }
574   }
575
576   $form->redirect($locale->text('Account deleted!'))
577     if (AM->delete_account(\%myconfig, \%$form));
578   $form->error($locale->text('Cannot delete account!'));
579
580   $main::lxdebug->leave_sub();
581 }
582
583 sub add_lead {
584   $main::lxdebug->enter_sub();
585
586   my $form     = $main::form;
587
588   $main::auth->assert('config');
589
590   $form->{title} = "Add";
591
592   $form->{callback} = "am.pl?action=add_lead" unless $form->{callback};
593
594   &lead_header;
595   &form_footer;
596
597   $main::lxdebug->leave_sub();
598 }
599
600 sub edit_lead {
601   $main::lxdebug->enter_sub();
602
603   my $form     = $main::form;
604   my %myconfig = %main::myconfig;
605
606   $main::auth->assert('config');
607
608   $form->{title} = "Edit";
609
610   AM->get_lead(\%myconfig, \%$form);
611
612   &lead_header;
613
614   $form->{orphaned} = 1;
615   &form_footer;
616
617   $main::lxdebug->leave_sub();
618 }
619
620 sub list_lead {
621   $::lxdebug->enter_sub;
622   $::auth->assert('config');
623
624   AM->lead(\%::myconfig, $::form);
625
626   $::form->{callback} = "am.pl?action=list_lead";
627   $::form->{title}    = $::locale->text('Lead');
628
629   $::form->header;
630   print $::form->parse_html_template('am/lead_list');
631
632   $::lxdebug->leave_sub;
633 }
634
635 sub lead_header {
636   $main::lxdebug->enter_sub();
637
638   my $form     = $main::form;
639   my $locale   = $main::locale;
640
641   $main::auth->assert('config');
642
643   $form->{title} = $locale->text("$form->{title} Lead");
644
645   # $locale->text('Add Lead')
646   # $locale->text('Edit Lead')
647
648   $form->{description} =~ s/\"/&quot;/g;
649
650   my $description =
651       qq|<input name=description size=50 value="$form->{lead}">|;
652
653   $form->header;
654
655   print qq|
656 <body>
657
658 <form method=post action=am.pl>
659
660 <input type=hidden name=id value=$form->{id}>
661 <input type=hidden name=type value=lead>
662
663 <table width=100%>
664   <tr>
665     <th class=listtop colspan=2>$form->{title}</th>
666   </tr>
667   <tr height="5"></tr>
668   <tr>
669     <th align=right>| . $locale->text('Description') . qq|</th>
670     <td>$description</td>
671   </tr>
672     <td colspan=2><hr size=3 noshade></td>
673   </tr>
674 </table>
675 |;
676
677   $main::lxdebug->leave_sub();
678 }
679
680 sub save_lead {
681   $main::lxdebug->enter_sub();
682
683   my $form     = $main::form;
684   my %myconfig = %main::myconfig;
685   my $locale   = $main::locale;
686
687   $main::auth->assert('config');
688
689   $form->isblank("description", $locale->text('Description missing!'));
690   AM->save_lead(\%myconfig, \%$form);
691   $form->redirect($locale->text('lead saved!'));
692
693   $main::lxdebug->leave_sub();
694 }
695
696 sub delete_lead {
697   $main::lxdebug->enter_sub();
698
699   my $form     = $main::form;
700   my %myconfig = %main::myconfig;
701   my $locale   = $main::locale;
702
703   $main::auth->assert('config');
704
705   AM->delete_lead(\%myconfig, \%$form);
706   $form->redirect($locale->text('lead deleted!'));
707
708   $main::lxdebug->leave_sub();
709 }
710
711 sub add_language {
712   $main::lxdebug->enter_sub();
713
714   my $form     = $main::form;
715
716   $main::auth->assert('config');
717
718   $form->{title} = "Add";
719
720   $form->{callback} = "am.pl?action=add_language" unless $form->{callback};
721
722   &language_header;
723   &form_footer;
724
725   $main::lxdebug->leave_sub();
726 }
727
728 sub edit_language {
729   $main::lxdebug->enter_sub();
730
731   my $form     = $main::form;
732   my %myconfig = %main::myconfig;
733
734   $main::auth->assert('config');
735
736   $form->{title} = "Edit";
737
738   AM->get_language(\%myconfig, \%$form);
739
740   &language_header;
741
742   $form->{orphaned} = 1;
743   &form_footer;
744
745   $main::lxdebug->leave_sub();
746 }
747
748 sub list_language {
749   $::lxdebug->enter_sub;
750   $::auth->assert('config');
751
752   AM->language(\%::myconfig, $::form);
753
754   $::form->{callback} = "am.pl?action=list_language";
755   $::form->{title}   = $::locale->text('Languages');
756
757   $::form->header;
758
759   print $::form->parse_html_template('am/language_list');
760
761   $::lxdebug->leave_sub;
762 }
763
764 sub language_header {
765   $::lxdebug->enter_sub;
766   $::auth->assert('config');
767
768   # $locale->text('Add Language')
769   # $locale->text('Edit Language')
770   $::form->{title} = $::locale->text("$::form->{title} Language");
771
772   $::form->header;
773
774   print $::form->parse_html_template('am/language_header', {
775     numberformats => [ '1,000.00', '1000.00', '1.000,00', '1000,00' ],
776     dateformats => [ qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ],
777   });
778
779   $::lxdebug->leave_sub;
780 }
781
782 sub save_language {
783   $main::lxdebug->enter_sub();
784
785   my $form     = $main::form;
786   my %myconfig = %main::myconfig;
787   my $locale   = $main::locale;
788
789   $main::auth->assert('config');
790
791   $form->isblank("description", $locale->text('Language missing!'));
792   $form->isblank("template_code", $locale->text('Template Code missing!'));
793   $form->isblank("article_code", $locale->text('Article Code missing!'));
794   AM->save_language(\%myconfig, \%$form);
795   $form->redirect($locale->text('Language saved!'));
796
797   $main::lxdebug->leave_sub();
798 }
799
800 sub delete_language {
801   $main::lxdebug->enter_sub();
802
803   my $form     = $main::form;
804   my %myconfig = %main::myconfig;
805   my $locale   = $main::locale;
806
807   $main::auth->assert('config');
808
809   AM->delete_language(\%myconfig, \%$form);
810   $form->redirect($locale->text('Language deleted!'));
811
812   $main::lxdebug->leave_sub();
813 }
814
815
816 sub add_buchungsgruppe {
817   $main::lxdebug->enter_sub();
818
819   my $form     = $main::form;
820   my %myconfig = %main::myconfig;
821   my $locale   = $main::locale;
822
823   $main::auth->assert('config');
824
825   # $locale->text("Add Buchungsgruppe")
826   # $locale->text("Edit Buchungsgruppe")
827   $form->{title} = "Add";
828
829   $form->{callback} = "am.pl?action=add_buchungsgruppe" unless $form->{callback};
830
831   AM->get_buchungsgruppe(\%myconfig, \%$form);
832   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
833   for (my $i = 0; 4 > $i; $i++) {
834     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
835         qw(income expense));
836   }
837
838   &buchungsgruppe_header;
839   &form_footer;
840
841   $main::lxdebug->leave_sub();
842 }
843
844 sub edit_buchungsgruppe {
845   $main::lxdebug->enter_sub();
846
847   my $form     = $main::form;
848   my %myconfig = %main::myconfig;
849
850   $main::auth->assert('config');
851
852   $form->{title} = "Edit";
853
854   AM->get_buchungsgruppe(\%myconfig, \%$form);
855
856   &buchungsgruppe_header;
857
858   &form_footer;
859
860   $main::lxdebug->leave_sub();
861 }
862
863 sub list_buchungsgruppe {
864   $main::lxdebug->enter_sub();
865
866   my $form     = $main::form;
867   my %myconfig = %main::myconfig;
868   my $locale   = $main::locale;
869
870   $main::auth->assert('config');
871
872   AM->buchungsgruppe(\%myconfig, \%$form);
873
874   $form->{callback} = "am.pl?action=list_buchungsgruppe";
875
876   my $callback = $form->escape($form->{callback});
877
878   $form->{title} = $locale->text('Buchungsgruppen');
879
880   my @column_index = qw(up down description inventory_accno
881                      income_accno_0 expense_accno_0
882                      income_accno_1 expense_accno_1
883                      income_accno_2 expense_accno_2
884                      income_accno_3 expense_accno_3 );
885   my %column_header;
886   $column_header{up} =
887       qq|<th class="listheading" width="16">|
888     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
889     . qq|</th>|;
890   $column_header{down} =
891       qq|<th class="listheading" width="16">|
892     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
893     . qq|</th>|;
894   $column_header{description} =
895       qq|<th class="listheading" width="40%">|
896     . $locale->text('Description')
897     . qq|</th>|;
898   $column_header{inventory_accno} =
899       qq|<th class=listheading>|
900     . $locale->text('Bestandskonto')
901     . qq|</th>|;
902   $column_header{income_accno_0} =
903       qq|<th class=listheading>|
904     . $locale->text('National Revenues')
905     . qq|</th>|;
906   $column_header{expense_accno_0} =
907       qq|<th class=listheading>|
908     . $locale->text('National Expenses')
909     . qq|</th>|;
910   $column_header{income_accno_1} =
911       qq|<th class=listheading>|
912     . $locale->text('Revenues EU with UStId')
913     . qq|</th>|;
914   $column_header{expense_accno_1} =
915       qq|<th class=listheading>|
916     . $locale->text('Expenses EU with UStId')
917     . qq|</th>|;
918   $column_header{income_accno_2} =
919       qq|<th class=listheading>|
920     . $locale->text('Revenues EU without UStId')
921     . qq|</th>|;
922   $column_header{expense_accno_2} =
923       qq|<th class=listheading>|
924     . $locale->text('Expenses EU without UStId')
925     . qq|</th>|;
926   $column_header{income_accno_3} =
927       qq|<th class=listheading>|
928     . $locale->text('Foreign Revenues')
929     . qq|</th>|;
930   $column_header{expense_accno_3} =
931       qq|<th class=listheading>|
932     . $locale->text('Foreign Expenses')
933     . qq|</th>|;
934   $form->header;
935
936   print qq|
937 <body>
938
939 <table width=100%>
940   <tr>
941     <th class=listtop>$form->{title}</th>
942   </tr>
943   <tr height="5"></tr>
944   <tr>
945     <td>
946       <table width=100%>
947         <tr class=listheading>
948 |;
949
950   map { print "$column_header{$_}\n" } @column_index;
951
952   print qq|
953         </tr>
954 |;
955
956   my $swap_link = qq|am.pl?action=swap_buchungsgruppen&|;
957
958   my $row = 0;
959   my ($i, %column_data);
960   foreach my $ref (@{ $form->{ALL} }) {
961
962     $i++;
963     $i %= 2;
964
965     print qq|
966         <tr valign=top class=listrow$i>
967 |;
968
969     if ($row) {
970       my $pref = $form->{ALL}->[$row - 1];
971       $column_data{up} =
972         qq|<td align="center" valign="center" width="16">| .
973         qq|<a href="${swap_link}id1=$ref->{id}&id2=$pref->{id}">| .
974         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
975         qq|</a></td>|;
976     } else {
977       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
978     }
979
980     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
981       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
982     } else {
983       my $nref = $form->{ALL}->[$row + 1];
984       $column_data{down} =
985         qq|<td align="center" valign="center" width="16">| .
986         qq|<a href="${swap_link}id1=$ref->{id}&id2=$nref->{id}">| .
987         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
988         qq|</a></td>|;
989     }
990
991     $column_data{description} = qq|<td><a href="am.pl?action=edit_buchungsgruppe&id=$ref->{id}&callback=$callback">$ref->{description}</td>|;
992     $column_data{inventory_accno}           = qq|<td align=right>$ref->{inventory_accno}</td>|;
993     $column_data{income_accno_0} =
994       qq|<td align=right>$ref->{income_accno_0}</td>|;
995     $column_data{expense_accno_0}           = qq|<td align=right>$ref->{expense_accno_0}</td>|;
996     $column_data{income_accno_1} =
997       qq|<td align=right>$ref->{income_accno_1}</td>|;
998     $column_data{expense_accno_1}           = qq|<td align=right>$ref->{expense_accno_1}</td>|;
999     $column_data{income_accno_2} =
1000       qq|<td align=right>$ref->{income_accno_2}</td>|;
1001     $column_data{expense_accno_2}           = qq|<td align=right>$ref->{expense_accno_2}</td>|;
1002     $column_data{income_accno_3} =
1003       qq|<td align=right>$ref->{income_accno_3}</td>|;
1004     $column_data{expense_accno_3}           = qq|<td align=right>$ref->{expense_accno_3}</td>|;
1005
1006     map { print "$column_data{$_}\n" } @column_index;
1007
1008     print qq|
1009         </tr>
1010 |;
1011
1012     $row++;
1013   }
1014
1015   print qq|
1016       </table>
1017     </td>
1018   </tr>
1019   <tr>
1020   <td><hr size=3 noshade></td>
1021   </tr>
1022 </table>
1023
1024 <br>
1025 <form method=post action=am.pl>
1026
1027 <input name=callback type=hidden value="$form->{callback}">
1028
1029 <input type=hidden name=type value=buchungsgruppe>
1030
1031 <input class=submit type=submit name=action value="|
1032     . $locale->text('Add') . qq|">
1033
1034   </form>
1035
1036   </body>
1037   </html>
1038 |;
1039
1040   $main::lxdebug->leave_sub();
1041 }
1042
1043 sub buchungsgruppe_header {
1044   $main::lxdebug->enter_sub();
1045
1046   my $form     = $main::form;
1047   my $locale   = $main::locale;
1048
1049   $main::auth->assert('config');
1050
1051   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
1052
1053   # $locale->text('Add Accounting Group')
1054   # $locale->text('Edit Accounting Group')
1055
1056   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
1057   my %acc_type_map = (
1058     "IC" => $acc_inventory,
1059     "IC_income" => $acc_income,
1060     "IC_sale" => $acc_income,
1061     "IC_expense" => $acc_expense,
1062     "IC_cogs" => $acc_expense,
1063     );
1064
1065   foreach my $key (keys(%acc_type_map)) {
1066     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1067       $acc_type_map{$key}->{$ref->{"id"}} = $ref;
1068     }
1069   }
1070
1071   foreach my $type (qw(IC IC_income IC_expense)) {
1072     $form->{"select$type"} =
1073       join("",
1074            map({ "<option value=$_->{id} $_->{selected}>" .
1075                    "$_->{accno}--" . H($_->{description}) . "</option>" }
1076                sort({ $a->{"accno"} cmp $b->{"accno"} }
1077                     values(%{$acc_type_map{$type}}))));
1078   }
1079
1080   if ($form->{id}) {
1081     $form->{selectIC} =~ s/selected//g;
1082     $form->{selectIC} =~ s/ value=\Q$form->{inventory_accno_id}\E/  value=$form->{inventory_accno_id} selected/;
1083     $form->{selectIC_income} =~ s/selected//g;
1084     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_0}\E/  value=$form->{income_accno_id_0} selected/;
1085     $form->{selectIC_expense} =~ s/selected//g;
1086     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_0}\E/  value=$form->{expense_accno_id_0} selected/;
1087   }
1088
1089   my $linkaccounts;
1090   if ( $::instance_conf->get_inventory_system eq 'perpetual' ) { # was !$::lx_office_conf{system}->{eur}) { }
1091     $linkaccounts = qq|
1092                <tr>
1093                 <th align=right>| . $locale->text('Inventory') . qq|</th>
1094                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
1095                 <input name=selectIC type=hidden value="$form->{selectIC}">
1096               </tr>|;
1097   } elsif ( $::instance_conf->get_inventory_system eq 'periodic' ) {
1098     # don't allow choice of inventory accno and don't show that line
1099     $linkaccounts = qq|
1100                 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>|;
1101   };
1102
1103
1104   $linkaccounts .= qq|
1105               <tr>
1106                 <th align=right>| . $locale->text('National Revenues') . qq|</th>
1107                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
1108               </tr>
1109               <tr>
1110                 <th align=right>| . $locale->text('National Expenses') . qq|</th>
1111                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
1112               </tr>|;
1113   if ($form->{id}) {
1114     $form->{selectIC_income} =~ s/selected//g;
1115     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_1}\E/  value=$form->{income_accno_id_1} selected/;
1116     $form->{selectIC_expense} =~ s/selected//g;
1117     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_1}\E/  value=$form->{expense_accno_id_1} selected/;
1118   }
1119   $linkaccounts .= qq|              <tr>
1120                 <th align=right>| . $locale->text('Revenues EU with UStId') . qq|</th>
1121                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
1122               </tr>
1123               <tr>
1124                 <th align=right>| . $locale->text('Expenses EU with UStId') . qq|</th>
1125                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
1126               </tr>|;
1127
1128   if ($form->{id}) {
1129     $form->{selectIC_income} =~ s/selected//g;
1130     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_2}\E/  value=$form->{income_accno_id_2} selected/;
1131     $form->{selectIC_expense} =~ s/selected//g;
1132     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_2}\E/  value=$form->{expense_accno_id_2} selected/;
1133   }
1134
1135   $linkaccounts .= qq|              <tr>
1136                 <th align=right>| . $locale->text('Revenues EU without UStId') . qq|</th>
1137                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
1138               </tr>
1139               <tr>
1140                 <th align=right>| . $locale->text('Expenses EU without UStId') . qq|</th>
1141                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
1142               </tr>|;
1143
1144   if ($form->{id}) {
1145     $form->{selectIC_income} =~ s/selected//g;
1146     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_3}\E/  value=$form->{income_accno_id_3} selected/;
1147     $form->{selectIC_expense} =~ s/selected//g;
1148     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_3}\E/  value=$form->{expense_accno_id_3} selected/;
1149   }
1150
1151   $linkaccounts .= qq|              <tr>
1152                 <th align=right>| . $locale->text('Foreign Revenues') . qq|</th>
1153                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
1154               </tr>
1155               <tr>
1156                 <th align=right>| . $locale->text('Foreign Expenses') . qq|</th>
1157                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
1158               </tr>
1159 |;
1160
1161
1162   $form->header;
1163
1164   print qq|
1165 <body>
1166
1167 <form method=post action=am.pl>
1168
1169 <input type=hidden name=id value=$form->{id}>
1170 <input type=hidden name=type value=buchungsgruppe>
1171
1172 <table width=100%>
1173   <tr>
1174     <th class=listtop colspan=2>$form->{title}</th>
1175   </tr>
1176   <tr height="5"></tr>
1177   <tr>
1178     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
1179     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1180   <tr>
1181   $linkaccounts
1182   <td colspan=2><hr size=3 noshade></td>
1183   </tr>
1184 </table>
1185 |;
1186
1187   $main::lxdebug->leave_sub();
1188 }
1189
1190 sub save_buchungsgruppe {
1191   $main::lxdebug->enter_sub();
1192
1193   my $form     = $main::form;
1194   my %myconfig = %main::myconfig;
1195   my $locale   = $main::locale;
1196
1197   $main::auth->assert('config');
1198
1199   $form->isblank("description", $locale->text('Description missing!'));
1200
1201   AM->save_buchungsgruppe(\%myconfig, \%$form);
1202   $form->redirect($locale->text('Accounting Group saved!'));
1203
1204   $main::lxdebug->leave_sub();
1205 }
1206
1207 sub delete_buchungsgruppe {
1208   $main::lxdebug->enter_sub();
1209
1210   my $form     = $main::form;
1211   my %myconfig = %main::myconfig;
1212   my $locale   = $main::locale;
1213
1214   $main::auth->assert('config');
1215
1216   AM->delete_buchungsgruppe(\%myconfig, \%$form);
1217   $form->redirect($locale->text('Accounting Group deleted!'));
1218
1219   $main::lxdebug->leave_sub();
1220 }
1221
1222 sub swap_buchungsgruppen {
1223   $main::lxdebug->enter_sub();
1224
1225   my $form     = $main::form;
1226   my %myconfig = %main::myconfig;
1227
1228   $main::auth->assert('config');
1229
1230   AM->swap_sortkeys(\%myconfig, $form, "buchungsgruppen");
1231   list_buchungsgruppe();
1232
1233   $main::lxdebug->leave_sub();
1234 }
1235
1236 sub edit_defaults {
1237   $main::lxdebug->enter_sub();
1238
1239   my $form     = $main::form;
1240   my %myconfig = %main::myconfig;
1241   my $locale   = $main::locale;
1242
1243   # get defaults for account numbers and last numbers
1244   AM->defaultaccounts(\%myconfig, \%$form);
1245   $form->{ALL_UNITS} = AM->convertible_units(AM->retrieve_all_units(), 'g');
1246
1247   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
1248
1249   # default language
1250   my $all_languages = SL::DB::Manager::Language->get_all;
1251
1252 # EÜR = cash, Bilanzierung = accrual
1253
1254   foreach my $key (keys %{ $form->{IC} }) {
1255     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
1256       my $array = "ACCNOS_" . uc($key);
1257       $form->{$array} ||= [];
1258
1259       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
1260       push @{ $form->{$array} }, {
1261         'name'     => $value,
1262         'value'    => $value,
1263         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
1264       };
1265     }
1266   }
1267
1268   $form->{title} = $locale->text('Ranges of numbers and default accounts');
1269
1270   $form->header();
1271   print $form->parse_html_template('am/edit_defaults',
1272                                    { ALL_LANGUAGES => $all_languages, });
1273
1274   $main::lxdebug->leave_sub();
1275 }
1276
1277 sub save_defaults {
1278   $main::lxdebug->enter_sub();
1279
1280   my $form     = $main::form;
1281   my $locale   = $main::locale;
1282
1283   AM->save_defaults();
1284
1285   $form->redirect($locale->text('Defaults saved.'));
1286
1287   $main::lxdebug->leave_sub();
1288 }
1289
1290 sub _build_cfg_options {
1291   my $form     = $main::form;
1292   my %myconfig = %main::myconfig;
1293
1294   my $idx   = shift;
1295   my $array = uc($idx) . 'S';
1296
1297   $form->{$array} = [];
1298   foreach my $item (@_) {
1299     push @{ $form->{$array} }, {
1300       'name'     => $item,
1301       'value'    => $item,
1302       'selected' => $item eq $myconfig{$idx},
1303     };
1304   }
1305 }
1306
1307 sub config {
1308   $main::lxdebug->enter_sub();
1309
1310   my $form     = $main::form;
1311   my %myconfig = %main::myconfig;
1312   my $locale   = $main::locale;
1313
1314   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
1315   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
1316
1317   my @formats = ();
1318   if ($::lx_office_conf{print_templates}->{opendocument}
1319       && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
1320       && $::lx_office_conf{applications}->{xvfb}                 && (-x $::lx_office_conf{applications}->{xvfb})) {
1321     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
1322                      "value" => "opendocument_pdf" });
1323   }
1324   if ($::lx_office_conf{print_templates}->{latex}) {
1325     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
1326   }
1327   push(@formats, { "name" => "HTML", "value" => "html" });
1328   if ($::lx_office_conf{print_templates}->{latex}) {
1329     push(@formats, { "name" => $locale->text("Postscript"),
1330                      "value" => "postscript" });
1331   }
1332   if ($::lx_office_conf{print_templates}->{opendocument}) {
1333     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
1334                      "value" => "opendocument" });
1335   }
1336
1337   if (!$myconfig{"template_format"}) {
1338     $myconfig{"template_format"} = "pdf";
1339   }
1340   $form->{TEMPLATE_FORMATS} = [];
1341   foreach my $item (@formats) {
1342     push @{ $form->{TEMPLATE_FORMATS} }, {
1343       'name'     => $item->{name},
1344       'value'    => $item->{value},
1345       'selected' => $item->{value} eq $myconfig{template_format},
1346     };
1347   }
1348
1349   if (!$myconfig{"default_media"}) {
1350     $myconfig{"default_media"} = "screen";
1351   }
1352
1353   my %selected = ($myconfig{"default_media"} => "selected");
1354   $form->{MEDIA} = [
1355     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
1356     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
1357     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
1358     ];
1359
1360   $form->{PRINTERS} = [ SL::Printer->all_printers(%::myconfig) ];
1361
1362   my %countrycodes = User->country_codes;
1363
1364   $form->{COUNTRYCODES} = [];
1365   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
1366     push @{ $form->{COUNTRYCODES} }, {
1367       'name'     => $countrycodes{$countrycode},
1368       'value'    => $countrycode,
1369       'selected' => $countrycode eq $myconfig{countrycode},
1370     };
1371   }
1372
1373   $form->{STYLESHEETS} = [];
1374   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css)) {
1375     push @{ $form->{STYLESHEETS} }, {
1376       'name'     => $item,
1377       'value'    => $item,
1378       'selected' => $item eq $myconfig{stylesheet},
1379     };
1380   }
1381
1382   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
1383   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
1384   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
1385
1386   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
1387
1388   $form->header();
1389   print $form->parse_html_template('am/config');
1390
1391   $main::lxdebug->leave_sub();
1392 }
1393
1394 sub save_preferences {
1395   $main::lxdebug->enter_sub();
1396
1397   my $form     = $main::form;
1398   my %myconfig = %main::myconfig;
1399   my $locale   = $main::locale;
1400
1401   $form->{stylesheet} = $form->{usestylesheet};
1402
1403   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
1404
1405   if (AM->save_preferences(\%myconfig, $form)) {
1406     if ($::auth->can_change_password()
1407         && defined $form->{new_password}
1408         && ($form->{new_password} ne '********')) {
1409       my $verifier = SL::Auth::PasswordPolicy->new;
1410       my $result   = $verifier->verify($form->{new_password});
1411
1412       if ($result != SL::Auth::PasswordPolicy->OK()) {
1413         $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
1414       }
1415
1416       $::auth->change_password($form->{login}, $form->{new_password});
1417
1418       $form->{password} = $form->{new_password};
1419       $::auth->set_session_value('password', $form->{password});
1420       $::auth->create_or_refresh_session();
1421     }
1422
1423     $form->redirect($locale->text('Preferences saved!'));
1424   }
1425
1426   $form->error($locale->text('Cannot save preferences!'));
1427
1428   $main::lxdebug->leave_sub();
1429 }
1430
1431 sub audit_control {
1432   $main::lxdebug->enter_sub();
1433
1434   my $form     = $main::form;
1435   my %myconfig = %main::myconfig;
1436   my $locale   = $main::locale;
1437
1438   $main::auth->assert('config');
1439
1440   $form->{title} = $locale->text('Audit Control');
1441
1442   AM->closedto(\%myconfig, \%$form);
1443
1444   $form->header;
1445
1446   print qq|
1447 <body>
1448
1449 <form method=post action=am.pl>
1450
1451 <table width=100%>
1452   <tr><th class=listtop>$form->{title}</th></tr>
1453   <tr height="5"></tr>
1454   <tr>
1455     <td>
1456       <table>
1457         <tr>
1458           <th>| . $locale->text('Close Books up to') . qq|</th>
1459           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
1460         </tr>
1461       </table>
1462     </td>
1463   </tr>
1464 </table>
1465
1466 <hr size=3 noshade>
1467
1468 <br>
1469 <input type=hidden name=nextsub value=doclose>
1470
1471 <input type=submit class=submit name=action value="|
1472     . $locale->text('Continue') . qq|">
1473
1474 </form>
1475
1476 </body>
1477 </html>
1478 |;
1479
1480   $main::lxdebug->leave_sub();
1481 }
1482
1483 sub doclose {
1484   $main::lxdebug->enter_sub();
1485
1486   my $form     = $main::form;
1487   my %myconfig = %main::myconfig;
1488   my $locale   = $main::locale;
1489
1490   $main::auth->assert('config');
1491
1492   AM->closebooks(\%myconfig, \%$form);
1493
1494   if ($form->{closedto}) {
1495     $form->redirect(
1496                     $locale->text('Books closed up to') . " "
1497                       . $locale->date(\%myconfig, $form->{closedto}, 1));
1498   } else {
1499     $form->redirect($locale->text('Books are open'));
1500   }
1501
1502   $main::lxdebug->leave_sub();
1503 }
1504
1505 sub edit_units {
1506   $main::lxdebug->enter_sub();
1507
1508   my $form     = $main::form;
1509   my %myconfig = %main::myconfig;
1510   my $locale   = $main::locale;
1511
1512   $main::auth->assert('config');
1513
1514   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1515   AM->units_in_use(\%myconfig, $form, $units);
1516   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
1517
1518   my @languages = AM->language(\%myconfig, $form, 1);
1519
1520   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
1521
1522   my $i = 1;
1523   foreach (@unit_list) {
1524     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
1525     $_->{"UNITLANGUAGES"} = [];
1526     foreach my $lang (@languages) {
1527       push(@{ $_->{"UNITLANGUAGES"} },
1528            { "idx" => $i,
1529              "unit" => $_->{"name"},
1530              "language_id" => $lang->{"id"},
1531              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
1532              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
1533            });
1534     }
1535     $i++;
1536   }
1537
1538   $units = AM->retrieve_units(\%myconfig, $form);
1539   my $ddbox = AM->unit_select_data($units, undef, 1);
1540
1541   $form->{"title"} = $locale->text("Add and edit units");
1542   $form->header();
1543   print($form->parse_html_template("am/edit_units",
1544                                    { "UNITS"               => \@unit_list,
1545                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
1546                                      "LANGUAGES"           => \@languages,
1547                                    }));
1548
1549   $main::lxdebug->leave_sub();
1550 }
1551
1552 sub add_unit {
1553   $main::lxdebug->enter_sub();
1554
1555   my $form     = $main::form;
1556   my %myconfig = %main::myconfig;
1557   my $locale   = $main::locale;
1558
1559   $main::auth->assert('config');
1560
1561   $form->isblank("new_name", $locale->text("The name is missing."));
1562   my $units = AM->retrieve_units(\%myconfig, $form);
1563   my $all_units = AM->retrieve_units(\%myconfig, $form);
1564   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
1565
1566   my ($base_unit, $factor);
1567   if ($form->{"new_base_unit"}) {
1568     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
1569
1570     $form->isblank("new_factor", $locale->text("The factor is missing."));
1571     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
1572     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
1573     $base_unit = $form->{"new_base_unit"};
1574   }
1575
1576   my @languages;
1577   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
1578     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
1579     push(@languages, { "id" => $lang->{"id"},
1580                        "localized" => $form->{"new_localized_$lang->{id}"},
1581                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
1582          });
1583   }
1584
1585   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
1586
1587   $form->{"saved_message"} = $locale->text("The unit has been saved.");
1588
1589   edit_units();
1590
1591   $main::lxdebug->leave_sub();
1592 }
1593
1594 sub set_unit_languages {
1595   $main::lxdebug->enter_sub();
1596
1597   my $form     = $main::form;
1598
1599   $main::auth->assert('config');
1600
1601   my ($unit, $languages, $idx) = @_;
1602
1603   $unit->{"LANGUAGES"} = [];
1604
1605   foreach my $lang (@{$languages}) {
1606     push(@{ $unit->{"LANGUAGES"} },
1607          { "id" => $lang->{"id"},
1608            "localized" => $form->{"localized_${idx}_$lang->{id}"},
1609            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
1610          });
1611   }
1612
1613   $main::lxdebug->leave_sub();
1614 }
1615
1616 sub save_unit {
1617   $main::lxdebug->enter_sub();
1618
1619   my $form     = $main::form;
1620   my %myconfig = %main::myconfig;
1621   my $locale   = $main::locale;
1622
1623   $main::auth->assert('config');
1624
1625   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1626   AM->units_in_use(\%myconfig, $form, $old_units);
1627
1628   my @languages = AM->language(\%myconfig, $form, 1);
1629
1630   my $new_units = {};
1631   my @delete_units = ();
1632   foreach my $i (1..($form->{"rowcount"} * 1)) {
1633     my $old_unit = $old_units->{$form->{"old_name_$i"}};
1634     if (!$old_unit) {
1635       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
1636     }
1637
1638     if ($form->{"unchangeable_$i"}) {
1639       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
1640       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
1641       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1642       next;
1643     }
1644
1645     if ($old_unit->{"in_use"}) {
1646       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
1647     }
1648
1649     if ($form->{"delete_$i"}) {
1650       push(@delete_units, $old_unit->{"name"});
1651       next;
1652     }
1653
1654     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
1655
1656     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
1657     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
1658     $new_units->{$form->{"name_$i"}} = \%h;
1659     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
1660     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1661   }
1662
1663   foreach my $unit (values(%{$new_units})) {
1664     next unless ($unit->{"old_name"});
1665     if ($unit->{"base_unit"}) {
1666       $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"}))
1667         unless (defined($new_units->{$unit->{"base_unit"}}));
1668       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
1669       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
1670     } else {
1671       $unit->{"base_unit"} = undef;
1672       $unit->{"factor"} = undef;
1673     }
1674   }
1675
1676   foreach my $unit (values(%{$new_units})) {
1677     next if ($unit->{"unchanged_unit"});
1678
1679     map({ $_->{"seen"} = 0; } values(%{$new_units}));
1680     my $new_unit = $unit;
1681     while ($new_unit->{"base_unit"}) {
1682       $new_unit->{"seen"} = 1;
1683       $new_unit = $new_units->{$new_unit->{"base_unit"}};
1684       if ($new_unit->{"seen"}) {
1685         $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, " .
1686                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
1687       }
1688     }
1689   }
1690
1691   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
1692
1693   $form->{"saved_message"} = $locale->text("The units have been saved.");
1694
1695   edit_units();
1696
1697   $main::lxdebug->leave_sub();
1698 }
1699
1700 sub show_history_search {
1701   $main::lxdebug->enter_sub();
1702
1703   my $form     = $main::form;
1704   my $locale   = $main::locale;
1705
1706   $main::auth->assert('config');
1707
1708   $form->{title} = $locale->text("History Search");
1709   $form->header();
1710
1711   print $form->parse_html_template("common/search_history");
1712
1713   $main::lxdebug->leave_sub();
1714 }
1715
1716 sub show_am_history {
1717   $main::lxdebug->enter_sub();
1718
1719   my $form     = $main::form;
1720   my %myconfig = %main::myconfig;
1721   my $locale   = $main::locale;
1722
1723   $main::auth->assert('config');
1724
1725   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
1726   $form->{order} ||= 'h.itime--1';
1727
1728   my %search = ( "Artikelnummer"          => "parts",
1729                  "Kundennummer"           => "customer",
1730                  "Lieferantennummer"      => "vendor",
1731                  "Projektnummer"          => "project",
1732                  "Buchungsnummer"         => "oe",
1733                  "Eingangsrechnungnummer" => "ap",
1734                  "Ausgangsrechnungnummer" => "ar",
1735                  "Mahnungsnummer"         => "dunning"
1736     );
1737   my %searchNo = ( "Artikelnummer"          => "partnumber",
1738                    "Kundennummer"           => "customernumber",
1739                    "Lieferantennummer"      => "vendornumber",
1740                    "Projektnummer"          => "projectnumber",
1741                    "Buchungsnummer"         => "ordnumber",
1742                    "Eingangsrechnungnummer" => "invnumber",
1743                    "Ausgangsrechnungnummer" => "invnumber",
1744                    "Mahnungsnummer"         => "dunning_id"
1745     );
1746
1747   my $dbh = $form->dbconnect(\%myconfig);
1748
1749   my $restriction;
1750   $restriction     = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
1751   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
1752   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
1753   if ($form->{mitarbeiter} =~ m/^\d+$/) {
1754     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
1755   } elsif ($form->{mitarbeiter}) {
1756     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
1757   }
1758
1759   my $query = qq|SELECT trans_id AS id FROM history_erp | .
1760     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
1761      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
1762
1763   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
1764   my $daten .= shift @ids;
1765   $daten    .= join '', map { " OR trans_id = $_" } @ids;
1766
1767   my ($sort, $sortby) = split(/\-\-/, $form->{order});
1768   $sort =~ s/.*\.(.*)$/$1/;
1769
1770   $form->{title} = $locale->text("History Search");
1771   $form->header();
1772
1773   print $form->parse_html_template("common/show_history",
1774                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
1775                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
1776                                      "NONEWWINDOW"    => 1,
1777                                      uc($sort)        => 1,
1778                                      uc($sort) . "BY" => $sortby,
1779                                      'callback'       => $callback,
1780                                    });
1781   $dbh->disconnect();
1782
1783   $main::lxdebug->leave_sub();
1784 }
1785
1786 sub add_tax {
1787   $main::lxdebug->enter_sub();
1788
1789   my $form     = $main::form;
1790   my $locale   = $main::locale;
1791
1792   $main::auth->assert('config');
1793
1794   $form->{title} =  $locale->text('Add');
1795
1796   $form->{callback} ||= "am.pl?action=add_tax";
1797
1798   _get_taxaccount_selection();
1799
1800   $form->header();
1801
1802   my $parameters_ref = {
1803 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
1804   };
1805
1806   # Ausgabe des Templates
1807   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1808
1809   $main::lxdebug->leave_sub();
1810 }
1811
1812 sub edit_tax {
1813   $main::lxdebug->enter_sub();
1814
1815   my $form     = $main::form;
1816   my %myconfig = %main::myconfig;
1817   my $locale   = $main::locale;
1818
1819   $main::auth->assert('config');
1820
1821   $form->{title} =  $locale->text('Edit');
1822
1823   AM->get_tax(\%myconfig, \%$form);
1824   _get_taxaccount_selection();
1825
1826   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
1827
1828   $form->header();
1829
1830   my $parameters_ref = {
1831   };
1832
1833   # Ausgabe des Templates
1834   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1835
1836   $main::lxdebug->leave_sub();
1837 }
1838
1839 sub list_tax {
1840   $main::lxdebug->enter_sub();
1841
1842   my $form     = $main::form;
1843   my %myconfig = %main::myconfig;
1844   my $locale   = $main::locale;
1845
1846   $main::auth->assert('config');
1847
1848   AM->taxes(\%myconfig, \%$form);
1849
1850   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
1851
1852   $form->{callback} = build_std_url('action=list_tax');
1853
1854   $form->{title} = $locale->text('Tax-O-Matic');
1855
1856   $form->header();
1857
1858   my $parameters_ref = {
1859   };
1860
1861   # Ausgabe des Templates
1862   print($form->parse_html_template('am/list_tax', $parameters_ref));
1863
1864   $main::lxdebug->leave_sub();
1865 }
1866
1867 sub _get_taxaccount_selection{
1868   $main::lxdebug->enter_sub();
1869
1870   my $form     = $main::form;
1871   my %myconfig = %main::myconfig;
1872
1873   $main::auth->assert('config');
1874
1875   AM->get_tax_accounts(\%myconfig, \%$form);
1876
1877   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
1878
1879   $main::lxdebug->leave_sub();
1880 }
1881
1882 sub save_tax {
1883   $main::lxdebug->enter_sub();
1884
1885   my $form     = $main::form;
1886   my %myconfig = %main::myconfig;
1887   my $locale   = $main::locale;
1888
1889   $main::auth->assert('config');
1890
1891   $form->isblank("rate", $locale->text('Taxrate missing!'));
1892   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
1893   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
1894
1895   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
1896
1897   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
1898     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1899   }
1900
1901   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
1902     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1903   }
1904
1905   AM->save_tax(\%myconfig, \%$form);
1906   $form->redirect($locale->text('Tax saved!'));
1907
1908   $main::lxdebug->leave_sub();
1909 }
1910
1911 sub delete_tax {
1912   $main::lxdebug->enter_sub();
1913
1914   my $form     = $main::form;
1915   my %myconfig = %main::myconfig;
1916   my $locale   = $main::locale;
1917
1918   $main::auth->assert('config');
1919
1920   AM->delete_tax(\%myconfig, \%$form);
1921   $form->redirect($locale->text('Tax deleted!'));
1922
1923   $main::lxdebug->leave_sub();
1924 }
1925
1926 sub add_price_factor {
1927   $main::lxdebug->enter_sub();
1928
1929   my $form     = $main::form;
1930   my $locale   = $main::locale;
1931
1932   $main::auth->assert('config');
1933
1934   $form->{title}      = $locale->text('Add Price Factor');
1935   $form->{callback} ||= build_std_url('action=add_price_factor');
1936   $form->{fokus}      = 'description';
1937
1938   $form->header();
1939   print $form->parse_html_template('am/edit_price_factor');
1940
1941   $main::lxdebug->leave_sub();
1942 }
1943
1944 sub edit_price_factor {
1945   $main::lxdebug->enter_sub();
1946
1947   my $form     = $main::form;
1948   my %myconfig = %main::myconfig;
1949   my $locale   = $main::locale;
1950
1951   $main::auth->assert('config');
1952
1953   $form->{title}      = $locale->text('Edit Price Factor');
1954   $form->{callback} ||= build_std_url('action=add_price_factor');
1955   $form->{fokus}      = 'description';
1956
1957   AM->get_price_factor(\%myconfig, $form);
1958
1959   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
1960
1961   $form->header();
1962   print $form->parse_html_template('am/edit_price_factor');
1963
1964   $main::lxdebug->leave_sub();
1965 }
1966
1967 sub list_price_factors {
1968   $main::lxdebug->enter_sub();
1969
1970   my $form     = $main::form;
1971   my %myconfig = %main::myconfig;
1972   my $locale   = $main::locale;
1973
1974   $main::auth->assert('config');
1975
1976   AM->get_all_price_factors(\%myconfig, \%$form);
1977
1978   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
1979     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
1980   }
1981
1982   $form->{callback} = build_std_url('action=list_price_factors');
1983   $form->{title}    = $locale->text('Price Factors');
1984   $form->{url_base} = build_std_url('callback');
1985
1986   $form->header();
1987   print $form->parse_html_template('am/list_price_factors');
1988
1989   $main::lxdebug->leave_sub();
1990 }
1991
1992 sub save_price_factor {
1993   $main::lxdebug->enter_sub();
1994
1995   my $form     = $main::form;
1996   my %myconfig = %main::myconfig;
1997   my $locale   = $main::locale;
1998
1999   $main::auth->assert('config');
2000
2001   $form->isblank("description", $locale->text('Description missing!'));
2002   $form->isblank("factor", $locale->text('Factor missing!'));
2003
2004   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
2005
2006   AM->save_price_factor(\%myconfig, $form);
2007
2008   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
2009
2010   $form->redirect($locale->text('Price factor saved!'));
2011
2012   $main::lxdebug->leave_sub();
2013 }
2014
2015 sub delete_price_factor {
2016   $main::lxdebug->enter_sub();
2017
2018   my $form     = $main::form;
2019   my %myconfig = %main::myconfig;
2020   my $locale   = $main::locale;
2021
2022   $main::auth->assert('config');
2023
2024   AM->delete_price_factor(\%myconfig, \%$form);
2025
2026   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
2027
2028   $form->redirect($locale->text('Price factor deleted!'));
2029
2030   $main::lxdebug->leave_sub();
2031 }
2032
2033 sub add_warehouse {
2034   $main::lxdebug->enter_sub();
2035
2036   my $form     = $main::form;
2037   my $locale   = $main::locale;
2038
2039   $main::auth->assert('config');
2040
2041   $form->{title}      = $locale->text('Add Warehouse');
2042   $form->{callback} ||= build_std_url('action=add_warehouse');
2043   $form->{fokus}      = 'description';
2044
2045   $form->header();
2046   print $form->parse_html_template('am/edit_warehouse');
2047
2048   $main::lxdebug->leave_sub();
2049 }
2050
2051 sub edit_warehouse {
2052   $main::lxdebug->enter_sub();
2053
2054   my $form     = $main::form;
2055   my %myconfig = %main::myconfig;
2056   my $locale   = $main::locale;
2057
2058   $main::auth->assert('config');
2059
2060   AM->get_warehouse(\%myconfig, $form);
2061
2062   $form->get_lists('employees' => 'EMPLOYEES');
2063
2064   $form->{title}      = $locale->text('Edit Warehouse');
2065   $form->{callback} ||= build_std_url('action=list_warehouses');
2066   $form->{fokus}      = 'description';
2067
2068   $form->header();
2069   print $form->parse_html_template('am/edit_warehouse');
2070
2071   $main::lxdebug->leave_sub();
2072 }
2073
2074 sub list_warehouses {
2075   $main::lxdebug->enter_sub();
2076
2077   my $form     = $main::form;
2078   my %myconfig = %main::myconfig;
2079   my $locale   = $main::locale;
2080
2081   $main::auth->assert('config');
2082
2083   AM->get_all_warehouses(\%myconfig, $form);
2084
2085   $form->{callback} = build_std_url('action=list_warehouses');
2086   $form->{title}    = $locale->text('Warehouses');
2087   $form->{url_base} = build_std_url('callback');
2088
2089   $form->header();
2090   print $form->parse_html_template('am/list_warehouses');
2091
2092   $main::lxdebug->leave_sub();
2093 }
2094
2095 sub save_warehouse {
2096   $main::lxdebug->enter_sub();
2097
2098   my $form     = $main::form;
2099   my %myconfig = %main::myconfig;
2100   my $locale   = $main::locale;
2101
2102   $main::auth->assert('config');
2103
2104   $form->isblank("description", $locale->text('Description missing!'));
2105
2106   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
2107
2108   AM->save_warehouse(\%myconfig, $form);
2109
2110   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
2111
2112   $form->redirect($locale->text('Warehouse saved.'));
2113
2114   $main::lxdebug->leave_sub();
2115 }
2116
2117 sub delete_warehouse {
2118   $main::lxdebug->enter_sub();
2119
2120   my $form     = $main::form;
2121   my %myconfig = %main::myconfig;
2122   my $locale   = $main::locale;
2123
2124   $main::auth->assert('config');
2125
2126   if (!$form->{confirmed}) {
2127     $form->{title} = $locale->text('Confirmation');
2128
2129     $form->header();
2130     print $form->parse_html_template('am/confirm_delete_warehouse');
2131     ::end_of_request();
2132   }
2133
2134   if (AM->delete_warehouse(\%myconfig, $form)) {
2135     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
2136     $form->redirect($locale->text('Warehouse deleted.'));
2137
2138   } else {
2139     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
2140   }
2141
2142   $main::lxdebug->leave_sub();
2143 }
2144
2145 sub save_bin {
2146   $main::lxdebug->enter_sub();
2147
2148   my $form     = $main::form;
2149   my %myconfig = %main::myconfig;
2150   my $locale   = $main::locale;
2151
2152   $main::auth->assert('config');
2153
2154   AM->save_bins(\%myconfig, $form);
2155
2156   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
2157
2158   $form->redirect($locale->text('Bins saved.'));
2159
2160   $main::lxdebug->leave_sub();
2161 }