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