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