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