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