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