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