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