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