Umstellung von eur zu 3 Variablen in defaults
[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 # EÜR = cash, Bilanzierung = accrual 
1553
1554   foreach my $key (keys %{ $form->{IC} }) {
1555     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
1556       my $array = "ACCNOS_" . uc($key);
1557       $form->{$array} ||= [];
1558
1559       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
1560       push @{ $form->{$array} }, {
1561         'name'     => $value,
1562         'value'    => $value,
1563         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
1564       };
1565     }
1566   }
1567
1568   $form->{title} = $locale->text('Ranges of numbers and default accounts');
1569
1570   $form->header();
1571   print $form->parse_html_template('am/edit_defaults');
1572
1573   $main::lxdebug->leave_sub();
1574 }
1575
1576 sub save_defaults {
1577   $main::lxdebug->enter_sub();
1578
1579   my $form     = $main::form;
1580   my $locale   = $main::locale;
1581
1582   AM->save_defaults();
1583
1584   $form->redirect($locale->text('Defaults saved.'));
1585
1586   $main::lxdebug->leave_sub();
1587 }
1588
1589 sub _build_cfg_options {
1590   my $form     = $main::form;
1591   my %myconfig = %main::myconfig;
1592
1593   my $idx   = shift;
1594   my $array = uc($idx) . 'S';
1595
1596   $form->{$array} = [];
1597   foreach my $item (@_) {
1598     push @{ $form->{$array} }, {
1599       'name'     => $item,
1600       'value'    => $item,
1601       'selected' => $item eq $myconfig{$idx},
1602     };
1603   }
1604 }
1605
1606 sub config {
1607   $main::lxdebug->enter_sub();
1608
1609   my $form     = $main::form;
1610   my %myconfig = %main::myconfig;
1611   my $locale   = $main::locale;
1612
1613   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
1614   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
1615
1616   my @formats = ();
1617   if ($::lx_office_conf{print_templates}->{opendocument}
1618       && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
1619       && $::lx_office_conf{applications}->{xvfb}                 && (-x $::lx_office_conf{applications}->{xvfb})) {
1620     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
1621                      "value" => "opendocument_pdf" });
1622   }
1623   if ($::lx_office_conf{print_templates}->{latex}) {
1624     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
1625   }
1626   push(@formats, { "name" => "HTML", "value" => "html" });
1627   if ($::lx_office_conf{print_templates}->{latex}) {
1628     push(@formats, { "name" => $locale->text("Postscript"),
1629                      "value" => "postscript" });
1630   }
1631   if ($::lx_office_conf{print_templates}->{opendocument}) {
1632     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
1633                      "value" => "opendocument" });
1634   }
1635
1636   if (!$myconfig{"template_format"}) {
1637     $myconfig{"template_format"} = "pdf";
1638   }
1639   $form->{TEMPLATE_FORMATS} = [];
1640   foreach my $item (@formats) {
1641     push @{ $form->{TEMPLATE_FORMATS} }, {
1642       'name'     => $item->{name},
1643       'value'    => $item->{value},
1644       'selected' => $item->{value} eq $myconfig{template_format},
1645     };
1646   }
1647
1648   if (!$myconfig{"default_media"}) {
1649     $myconfig{"default_media"} = "screen";
1650   }
1651
1652   my %selected = ($myconfig{"default_media"} => "selected");
1653   $form->{MEDIA} = [
1654     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
1655     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
1656     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
1657     ];
1658
1659   $form->{PRINTERS} = [];
1660   foreach my $printer (SL::Printer->all_printers(%::myconfig)) {
1661     push @{ $form->{PRINTERS} }, {
1662       'name'     => $printer->{printer_description},
1663       'value'    => $printer->{id},
1664       'selected' => $printer->{id} == $myconfig{default_printer_id},
1665     };
1666   }
1667
1668   my %countrycodes = User->country_codes;
1669
1670   $form->{COUNTRYCODES} = [];
1671   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
1672     push @{ $form->{COUNTRYCODES} }, {
1673       'name'     => $countrycodes{$countrycode},
1674       'value'    => $countrycode,
1675       'selected' => $countrycode eq $myconfig{countrycode},
1676     };
1677   }
1678
1679   $form->{STYLESHEETS} = [];
1680   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css)) {
1681     push @{ $form->{STYLESHEETS} }, {
1682       'name'     => $item,
1683       'value'    => $item,
1684       'selected' => $item eq $myconfig{stylesheet},
1685     };
1686   }
1687
1688   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
1689   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
1690   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
1691
1692   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
1693
1694   $form->header();
1695   print $form->parse_html_template('am/config');
1696
1697   $main::lxdebug->leave_sub();
1698 }
1699
1700 sub save_preferences {
1701   $main::lxdebug->enter_sub();
1702
1703   my $form     = $main::form;
1704   my %myconfig = %main::myconfig;
1705   my $locale   = $main::locale;
1706
1707   $form->{stylesheet} = $form->{usestylesheet};
1708
1709   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
1710
1711   if (AM->save_preferences(\%myconfig, $form)) {
1712     if ($::auth->can_change_password()
1713         && defined $form->{new_password}
1714         && ($form->{new_password} ne '********')) {
1715       my $verifier = SL::Auth::PasswordPolicy->new;
1716       my $result   = $verifier->verify($form->{new_password});
1717
1718       if ($result != SL::Auth::PasswordPolicy->OK()) {
1719         $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
1720       }
1721
1722       $::auth->change_password($form->{login}, $form->{new_password});
1723
1724       $form->{password} = $form->{new_password};
1725       $::auth->set_session_value('password', $form->{password});
1726       $::auth->create_or_refresh_session();
1727     }
1728
1729     $form->redirect($locale->text('Preferences saved!'));
1730   }
1731
1732   $form->error($locale->text('Cannot save preferences!'));
1733
1734   $main::lxdebug->leave_sub();
1735 }
1736
1737 sub audit_control {
1738   $main::lxdebug->enter_sub();
1739
1740   my $form     = $main::form;
1741   my %myconfig = %main::myconfig;
1742   my $locale   = $main::locale;
1743
1744   $main::auth->assert('config');
1745
1746   $form->{title} = $locale->text('Audit Control');
1747
1748   AM->closedto(\%myconfig, \%$form);
1749
1750   $form->header;
1751
1752   print qq|
1753 <body>
1754
1755 <form method=post action=am.pl>
1756
1757 <table width=100%>
1758   <tr><th class=listtop>$form->{title}</th></tr>
1759   <tr height="5"></tr>
1760   <tr>
1761     <td>
1762       <table>
1763         <tr>
1764           <th>| . $locale->text('Close Books up to') . qq|</th>
1765           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
1766         </tr>
1767       </table>
1768     </td>
1769   </tr>
1770 </table>
1771
1772 <hr size=3 noshade>
1773
1774 <br>
1775 <input type=hidden name=nextsub value=doclose>
1776
1777 <input type=submit class=submit name=action value="|
1778     . $locale->text('Continue') . qq|">
1779
1780 </form>
1781
1782 </body>
1783 </html>
1784 |;
1785
1786   $main::lxdebug->leave_sub();
1787 }
1788
1789 sub doclose {
1790   $main::lxdebug->enter_sub();
1791
1792   my $form     = $main::form;
1793   my %myconfig = %main::myconfig;
1794   my $locale   = $main::locale;
1795
1796   $main::auth->assert('config');
1797
1798   AM->closebooks(\%myconfig, \%$form);
1799
1800   if ($form->{closedto}) {
1801     $form->redirect(
1802                     $locale->text('Books closed up to') . " "
1803                       . $locale->date(\%myconfig, $form->{closedto}, 1));
1804   } else {
1805     $form->redirect($locale->text('Books are open'));
1806   }
1807
1808   $main::lxdebug->leave_sub();
1809 }
1810
1811 sub edit_units {
1812   $main::lxdebug->enter_sub();
1813
1814   my $form     = $main::form;
1815   my %myconfig = %main::myconfig;
1816   my $locale   = $main::locale;
1817
1818   $main::auth->assert('config');
1819
1820   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1821   AM->units_in_use(\%myconfig, $form, $units);
1822   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
1823
1824   my @languages = AM->language(\%myconfig, $form, 1);
1825
1826   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
1827
1828   my $i = 1;
1829   foreach (@unit_list) {
1830     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
1831     $_->{"UNITLANGUAGES"} = [];
1832     foreach my $lang (@languages) {
1833       push(@{ $_->{"UNITLANGUAGES"} },
1834            { "idx" => $i,
1835              "unit" => $_->{"name"},
1836              "language_id" => $lang->{"id"},
1837              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
1838              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
1839            });
1840     }
1841     $i++;
1842   }
1843
1844   $units = AM->retrieve_units(\%myconfig, $form);
1845   my $ddbox = AM->unit_select_data($units, undef, 1);
1846
1847   $form->{"title"} = $locale->text("Add and edit units");
1848   $form->header();
1849   print($form->parse_html_template("am/edit_units",
1850                                    { "UNITS"               => \@unit_list,
1851                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
1852                                      "LANGUAGES"           => \@languages,
1853                                    }));
1854
1855   $main::lxdebug->leave_sub();
1856 }
1857
1858 sub add_unit {
1859   $main::lxdebug->enter_sub();
1860
1861   my $form     = $main::form;
1862   my %myconfig = %main::myconfig;
1863   my $locale   = $main::locale;
1864
1865   $main::auth->assert('config');
1866
1867   $form->isblank("new_name", $locale->text("The name is missing."));
1868   my $units = AM->retrieve_units(\%myconfig, $form);
1869   my $all_units = AM->retrieve_units(\%myconfig, $form);
1870   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
1871
1872   my ($base_unit, $factor);
1873   if ($form->{"new_base_unit"}) {
1874     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
1875
1876     $form->isblank("new_factor", $locale->text("The factor is missing."));
1877     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
1878     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
1879     $base_unit = $form->{"new_base_unit"};
1880   }
1881
1882   my @languages;
1883   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
1884     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
1885     push(@languages, { "id" => $lang->{"id"},
1886                        "localized" => $form->{"new_localized_$lang->{id}"},
1887                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
1888          });
1889   }
1890
1891   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
1892
1893   $form->{"saved_message"} = $locale->text("The unit has been saved.");
1894
1895   edit_units();
1896
1897   $main::lxdebug->leave_sub();
1898 }
1899
1900 sub set_unit_languages {
1901   $main::lxdebug->enter_sub();
1902
1903   my $form     = $main::form;
1904
1905   $main::auth->assert('config');
1906
1907   my ($unit, $languages, $idx) = @_;
1908
1909   $unit->{"LANGUAGES"} = [];
1910
1911   foreach my $lang (@{$languages}) {
1912     push(@{ $unit->{"LANGUAGES"} },
1913          { "id" => $lang->{"id"},
1914            "localized" => $form->{"localized_${idx}_$lang->{id}"},
1915            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
1916          });
1917   }
1918
1919   $main::lxdebug->leave_sub();
1920 }
1921
1922 sub save_unit {
1923   $main::lxdebug->enter_sub();
1924
1925   my $form     = $main::form;
1926   my %myconfig = %main::myconfig;
1927   my $locale   = $main::locale;
1928
1929   $main::auth->assert('config');
1930
1931   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1932   AM->units_in_use(\%myconfig, $form, $old_units);
1933
1934   my @languages = AM->language(\%myconfig, $form, 1);
1935
1936   my $new_units = {};
1937   my @delete_units = ();
1938   foreach my $i (1..($form->{"rowcount"} * 1)) {
1939     my $old_unit = $old_units->{$form->{"old_name_$i"}};
1940     if (!$old_unit) {
1941       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
1942     }
1943
1944     if ($form->{"unchangeable_$i"}) {
1945       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
1946       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
1947       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1948       next;
1949     }
1950
1951     if ($old_unit->{"in_use"}) {
1952       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
1953     }
1954
1955     if ($form->{"delete_$i"}) {
1956       push(@delete_units, $old_unit->{"name"});
1957       next;
1958     }
1959
1960     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
1961
1962     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
1963     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
1964     $new_units->{$form->{"name_$i"}} = \%h;
1965     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
1966     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1967   }
1968
1969   foreach my $unit (values(%{$new_units})) {
1970     next unless ($unit->{"old_name"});
1971     if ($unit->{"base_unit"}) {
1972       $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"}))
1973         unless (defined($new_units->{$unit->{"base_unit"}}));
1974       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
1975       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
1976     } else {
1977       $unit->{"base_unit"} = undef;
1978       $unit->{"factor"} = undef;
1979     }
1980   }
1981
1982   foreach my $unit (values(%{$new_units})) {
1983     next if ($unit->{"unchanged_unit"});
1984
1985     map({ $_->{"seen"} = 0; } values(%{$new_units}));
1986     my $new_unit = $unit;
1987     while ($new_unit->{"base_unit"}) {
1988       $new_unit->{"seen"} = 1;
1989       $new_unit = $new_units->{$new_unit->{"base_unit"}};
1990       if ($new_unit->{"seen"}) {
1991         $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, " .
1992                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
1993       }
1994     }
1995   }
1996
1997   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
1998
1999   $form->{"saved_message"} = $locale->text("The units have been saved.");
2000
2001   edit_units();
2002
2003   $main::lxdebug->leave_sub();
2004 }
2005
2006 sub show_history_search {
2007   $main::lxdebug->enter_sub();
2008
2009   my $form     = $main::form;
2010   my $locale   = $main::locale;
2011
2012   $main::auth->assert('config');
2013
2014   $form->{title} = $locale->text("History Search");
2015   $form->header();
2016
2017   print $form->parse_html_template("common/search_history");
2018
2019   $main::lxdebug->leave_sub();
2020 }
2021
2022 sub show_am_history {
2023   $main::lxdebug->enter_sub();
2024
2025   my $form     = $main::form;
2026   my %myconfig = %main::myconfig;
2027   my $locale   = $main::locale;
2028
2029   $main::auth->assert('config');
2030
2031   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
2032   $form->{order} ||= 'h.itime--1';
2033
2034   my %search = ( "Artikelnummer"          => "parts",
2035                  "Kundennummer"           => "customer",
2036                  "Lieferantennummer"      => "vendor",
2037                  "Projektnummer"          => "project",
2038                  "Buchungsnummer"         => "oe",
2039                  "Eingangsrechnungnummer" => "ap",
2040                  "Ausgangsrechnungnummer" => "ar",
2041                  "Mahnungsnummer"         => "dunning"
2042     );
2043   my %searchNo = ( "Artikelnummer"          => "partnumber",
2044                    "Kundennummer"           => "customernumber",
2045                    "Lieferantennummer"      => "vendornumber",
2046                    "Projektnummer"          => "projectnumber",
2047                    "Buchungsnummer"         => "ordnumber",
2048                    "Eingangsrechnungnummer" => "invnumber",
2049                    "Ausgangsrechnungnummer" => "invnumber",
2050                    "Mahnungsnummer"         => "dunning_id"
2051     );
2052
2053   my $dbh = $form->dbconnect(\%myconfig);
2054
2055   my $restriction  = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
2056   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
2057   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
2058   if ($form->{mitarbeiter} =~ m/^\d+$/) {
2059     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
2060   } elsif ($form->{mitarbeiter}) {
2061     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
2062   }
2063
2064   my $query = qq|SELECT trans_id AS id FROM history_erp | .
2065     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
2066      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
2067
2068   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
2069   my $daten .= shift @ids;
2070   $daten    .= join '', map { " OR trans_id = $_" } @ids;
2071
2072   my ($sort, $sortby) = split(/\-\-/, $form->{order});
2073   $sort =~ s/.*\.(.*)$/$1/;
2074
2075   $form->{title} = $locale->text("History Search");
2076   $form->header();
2077
2078   print $form->parse_html_template("common/show_history",
2079                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
2080                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
2081                                      "NONEWWINDOW"    => 1,
2082                                      uc($sort)        => 1,
2083                                      uc($sort) . "BY" => $sortby,
2084                                      'callback'       => $callback,
2085                                    });
2086   $dbh->disconnect();
2087
2088   $main::lxdebug->leave_sub();
2089 }
2090
2091 sub add_tax {
2092   $main::lxdebug->enter_sub();
2093
2094   my $form     = $main::form;
2095   my $locale   = $main::locale;
2096
2097   $main::auth->assert('config');
2098
2099   $form->{title} =  $locale->text('Add');
2100
2101   $form->{callback} ||= "am.pl?action=add_tax";
2102
2103   _get_taxaccount_selection();
2104
2105   $form->header();
2106
2107   my $parameters_ref = {
2108 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
2109   };
2110
2111   # Ausgabe des Templates
2112   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2113
2114   $main::lxdebug->leave_sub();
2115 }
2116
2117 sub edit_tax {
2118   $main::lxdebug->enter_sub();
2119
2120   my $form     = $main::form;
2121   my %myconfig = %main::myconfig;
2122   my $locale   = $main::locale;
2123
2124   $main::auth->assert('config');
2125
2126   $form->{title} =  $locale->text('Edit');
2127
2128   AM->get_tax(\%myconfig, \%$form);
2129   _get_taxaccount_selection();
2130
2131   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
2132
2133   $form->header();
2134
2135   my $parameters_ref = {
2136   };
2137
2138   # Ausgabe des Templates
2139   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2140
2141   $main::lxdebug->leave_sub();
2142 }
2143
2144 sub list_tax {
2145   $main::lxdebug->enter_sub();
2146
2147   my $form     = $main::form;
2148   my %myconfig = %main::myconfig;
2149   my $locale   = $main::locale;
2150
2151   $main::auth->assert('config');
2152
2153   AM->taxes(\%myconfig, \%$form);
2154
2155   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
2156
2157   $form->{callback} = build_std_url('action=list_tax');
2158
2159   $form->{title} = $locale->text('Tax-O-Matic');
2160
2161   $form->header();
2162
2163   my $parameters_ref = {
2164   };
2165
2166   # Ausgabe des Templates
2167   print($form->parse_html_template('am/list_tax', $parameters_ref));
2168
2169   $main::lxdebug->leave_sub();
2170 }
2171
2172 sub _get_taxaccount_selection{
2173   $main::lxdebug->enter_sub();
2174
2175   my $form     = $main::form;
2176   my %myconfig = %main::myconfig;
2177
2178   $main::auth->assert('config');
2179
2180   AM->get_tax_accounts(\%myconfig, \%$form);
2181
2182   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
2183
2184   $main::lxdebug->leave_sub();
2185 }
2186
2187 sub save_tax {
2188   $main::lxdebug->enter_sub();
2189
2190   my $form     = $main::form;
2191   my %myconfig = %main::myconfig;
2192   my $locale   = $main::locale;
2193
2194   $main::auth->assert('config');
2195
2196   $form->isblank("rate", $locale->text('Taxrate missing!'));
2197   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
2198   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
2199
2200   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
2201
2202   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
2203     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
2204   }
2205
2206   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
2207     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
2208   }
2209
2210   AM->save_tax(\%myconfig, \%$form);
2211   $form->redirect($locale->text('Tax saved!'));
2212
2213   $main::lxdebug->leave_sub();
2214 }
2215
2216 sub delete_tax {
2217   $main::lxdebug->enter_sub();
2218
2219   my $form     = $main::form;
2220   my %myconfig = %main::myconfig;
2221   my $locale   = $main::locale;
2222
2223   $main::auth->assert('config');
2224
2225   AM->delete_tax(\%myconfig, \%$form);
2226   $form->redirect($locale->text('Tax deleted!'));
2227
2228   $main::lxdebug->leave_sub();
2229 }
2230
2231 sub add_price_factor {
2232   $main::lxdebug->enter_sub();
2233
2234   my $form     = $main::form;
2235   my $locale   = $main::locale;
2236
2237   $main::auth->assert('config');
2238
2239   $form->{title}      = $locale->text('Add Price Factor');
2240   $form->{callback} ||= build_std_url('action=add_price_factor');
2241   $form->{fokus}      = 'description';
2242
2243   $form->header();
2244   print $form->parse_html_template('am/edit_price_factor');
2245
2246   $main::lxdebug->leave_sub();
2247 }
2248
2249 sub edit_price_factor {
2250   $main::lxdebug->enter_sub();
2251
2252   my $form     = $main::form;
2253   my %myconfig = %main::myconfig;
2254   my $locale   = $main::locale;
2255
2256   $main::auth->assert('config');
2257
2258   $form->{title}      = $locale->text('Edit Price Factor');
2259   $form->{callback} ||= build_std_url('action=add_price_factor');
2260   $form->{fokus}      = 'description';
2261
2262   AM->get_price_factor(\%myconfig, $form);
2263
2264   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
2265
2266   $form->header();
2267   print $form->parse_html_template('am/edit_price_factor');
2268
2269   $main::lxdebug->leave_sub();
2270 }
2271
2272 sub list_price_factors {
2273   $main::lxdebug->enter_sub();
2274
2275   my $form     = $main::form;
2276   my %myconfig = %main::myconfig;
2277   my $locale   = $main::locale;
2278
2279   $main::auth->assert('config');
2280
2281   AM->get_all_price_factors(\%myconfig, \%$form);
2282
2283   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
2284     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
2285   }
2286
2287   $form->{callback} = build_std_url('action=list_price_factors');
2288   $form->{title}    = $locale->text('Price Factors');
2289   $form->{url_base} = build_std_url('callback');
2290
2291   $form->header();
2292   print $form->parse_html_template('am/list_price_factors');
2293
2294   $main::lxdebug->leave_sub();
2295 }
2296
2297 sub save_price_factor {
2298   $main::lxdebug->enter_sub();
2299
2300   my $form     = $main::form;
2301   my %myconfig = %main::myconfig;
2302   my $locale   = $main::locale;
2303
2304   $main::auth->assert('config');
2305
2306   $form->isblank("description", $locale->text('Description missing!'));
2307   $form->isblank("factor", $locale->text('Factor missing!'));
2308
2309   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
2310
2311   AM->save_price_factor(\%myconfig, $form);
2312
2313   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
2314
2315   $form->redirect($locale->text('Price factor saved!'));
2316
2317   $main::lxdebug->leave_sub();
2318 }
2319
2320 sub delete_price_factor {
2321   $main::lxdebug->enter_sub();
2322
2323   my $form     = $main::form;
2324   my %myconfig = %main::myconfig;
2325   my $locale   = $main::locale;
2326
2327   $main::auth->assert('config');
2328
2329   AM->delete_price_factor(\%myconfig, \%$form);
2330
2331   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
2332
2333   $form->redirect($locale->text('Price factor deleted!'));
2334
2335   $main::lxdebug->leave_sub();
2336 }
2337
2338 sub add_warehouse {
2339   $main::lxdebug->enter_sub();
2340
2341   my $form     = $main::form;
2342   my $locale   = $main::locale;
2343
2344   $main::auth->assert('config');
2345
2346   $form->{title}      = $locale->text('Add Warehouse');
2347   $form->{callback} ||= build_std_url('action=add_warehouse');
2348   $form->{fokus}      = 'description';
2349
2350   $form->header();
2351   print $form->parse_html_template('am/edit_warehouse');
2352
2353   $main::lxdebug->leave_sub();
2354 }
2355
2356 sub edit_warehouse {
2357   $main::lxdebug->enter_sub();
2358
2359   my $form     = $main::form;
2360   my %myconfig = %main::myconfig;
2361   my $locale   = $main::locale;
2362
2363   $main::auth->assert('config');
2364
2365   AM->get_warehouse(\%myconfig, $form);
2366
2367   $form->get_lists('employees' => 'EMPLOYEES');
2368
2369   $form->{title}      = $locale->text('Edit Warehouse');
2370   $form->{callback} ||= build_std_url('action=list_warehouses');
2371   $form->{fokus}      = 'description';
2372
2373   $form->header();
2374   print $form->parse_html_template('am/edit_warehouse');
2375
2376   $main::lxdebug->leave_sub();
2377 }
2378
2379 sub list_warehouses {
2380   $main::lxdebug->enter_sub();
2381
2382   my $form     = $main::form;
2383   my %myconfig = %main::myconfig;
2384   my $locale   = $main::locale;
2385
2386   $main::auth->assert('config');
2387
2388   AM->get_all_warehouses(\%myconfig, $form);
2389
2390   $form->{callback} = build_std_url('action=list_warehouses');
2391   $form->{title}    = $locale->text('Warehouses');
2392   $form->{url_base} = build_std_url('callback');
2393
2394   $form->header();
2395   print $form->parse_html_template('am/list_warehouses');
2396
2397   $main::lxdebug->leave_sub();
2398 }
2399
2400 sub save_warehouse {
2401   $main::lxdebug->enter_sub();
2402
2403   my $form     = $main::form;
2404   my %myconfig = %main::myconfig;
2405   my $locale   = $main::locale;
2406
2407   $main::auth->assert('config');
2408
2409   $form->isblank("description", $locale->text('Description missing!'));
2410
2411   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
2412
2413   AM->save_warehouse(\%myconfig, $form);
2414
2415   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
2416
2417   $form->redirect($locale->text('Warehouse saved.'));
2418
2419   $main::lxdebug->leave_sub();
2420 }
2421
2422 sub delete_warehouse {
2423   $main::lxdebug->enter_sub();
2424
2425   my $form     = $main::form;
2426   my %myconfig = %main::myconfig;
2427   my $locale   = $main::locale;
2428
2429   $main::auth->assert('config');
2430
2431   if (!$form->{confirmed}) {
2432     $form->{title} = $locale->text('Confirmation');
2433
2434     $form->header();
2435     print $form->parse_html_template('am/confirm_delete_warehouse');
2436     ::end_of_request();
2437   }
2438
2439   if (AM->delete_warehouse(\%myconfig, $form)) {
2440     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
2441     $form->redirect($locale->text('Warehouse deleted.'));
2442
2443   } else {
2444     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
2445   }
2446
2447   $main::lxdebug->leave_sub();
2448 }
2449
2450 sub save_bin {
2451   $main::lxdebug->enter_sub();
2452
2453   my $form     = $main::form;
2454   my %myconfig = %main::myconfig;
2455   my $locale   = $main::locale;
2456
2457   $main::auth->assert('config');
2458
2459   AM->save_bins(\%myconfig, $form);
2460
2461   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
2462
2463   $form->redirect($locale->text('Bins saved.'));
2464
2465   $main::lxdebug->leave_sub();
2466 }