a1e227fa8a03f02772b4c4a8a2204a415cdbc58f
[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 SL::Auth;
35 use SL::AM;
36 use SL::CA;
37 use SL::Form;
38 use SL::User;
39 use SL::USTVA;
40 use SL::Iconv;
41 use SL::TODO;
42 use CGI::Ajax;
43 use CGI;
44
45 use Data::Dumper;
46
47 require "bin/mozilla/common.pl";
48
49 use strict;
50
51 1;
52
53 # end of main
54
55 sub add      { call_sub("add_$main::form->{type}"); }
56 sub delete   { call_sub("delete_$main::form->{type}"); }
57 sub save     { call_sub("save_$main::form->{type}"); }
58 sub edit     { call_sub("edit_$main::form->{type}"); }
59 sub continue { call_sub($main::form->{"nextsub"}); }
60 sub save_as_new { call_sub("save_as_new_$main::form->{type}"); }
61
62 sub add_account {
63   $main::lxdebug->enter_sub();
64
65   my $form     = $main::form;
66   my %myconfig = %main::myconfig;
67
68   $main::auth->assert('config');
69
70   $form->{title}     = "Add";
71   $form->{charttype} = "A";
72   AM->get_account(\%myconfig, \%$form);
73
74   $form->{callback} = "am.pl?action=list_account" unless $form->{callback};
75
76   &account_header;
77   &form_footer;
78
79   $main::lxdebug->leave_sub();
80 }
81
82 sub edit_account {
83   $main::lxdebug->enter_sub();
84
85   my $form     = $main::form;
86   my %myconfig = %main::myconfig;
87
88   $main::auth->assert('config');
89
90   $form->{title} = "Edit";
91   AM->get_account(\%myconfig, \%$form);
92
93   foreach my $item (split(/:/, $form->{link})) {
94     $form->{$item} = "checked";
95   }
96
97   &account_header;
98   &form_footer;
99
100   $main::lxdebug->leave_sub();
101 }
102
103 sub account_header {
104   $main::lxdebug->enter_sub();
105
106   my $form     = $main::form;
107   my %myconfig = %main::myconfig;
108   my $locale   = $main::locale;
109
110   $main::auth->assert('config');
111
112   if ( $form->{action} eq 'edit_account') {
113     $form->{account_exists} = '1';
114   }
115
116   $form->{title} = $locale->text("$form->{title} Account");
117
118   $form->{"$form->{charttype}_checked"} = "checked";
119   $form->{"$form->{category}_checked"}  = "checked";
120
121   $form->{select_tax} = "";
122
123   my @tax_report_pos = USTVA->report_variables({
124       myconfig   => \%myconfig,
125       form       => $form,
126       type       => '',
127       attribute  => 'position',
128       calc       => '',
129   });
130
131   if (@{ $form->{TAXKEY} }) {
132     foreach my $item (@{ $form->{TAXKEY} }) {
133       $item->{rate} = $item->{rate} * 100 . '%';
134     }
135
136     # Fill in empty row for new Taxkey
137     my $newtaxkey_ref = {
138       id             => '',
139       chart_id       => '',
140       accno          => '',
141       tax_id         => '',
142       taxdescription => '',
143       rate           => '',
144       taxkey_id      => '',
145       pos_ustva      => '',
146       startdate      => '',
147     };
148
149     push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
150
151     my $i = 0;
152     foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
153
154       # Fill in a runningnumber
155       $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
156
157       # Fill in the Taxkeys as select options
158       foreach my $item (@{ $form->{TAXKEY} }) {
159         if ($item->{id} == $taxkey_used->{tax_id}) {
160           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
161             qq|<option value="$item->{id}" selected="selected">|
162             . sprintf("%.2d", $item->{taxkey})
163             . qq|. $item->{taxdescription} ($item->{rate}) |
164             . $locale->text('Tax-o-matic Account')
165             . qq|: $item->{chart_accno}\n|;
166         }
167         else {
168           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
169             qq|<option value="$item->{id}">|
170             . sprintf("%.2d", $item->{taxkey})
171             . qq|. $item->{taxdescription} ($item->{rate}) |
172             . $locale->text('Tax-o-matic Account')
173             . qq|: $item->{chart_accno}\n|;
174         }
175
176       }
177
178       # Fill in the USTVA Numbers as select options
179       foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
180         if ($item eq ''){
181           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
182         }
183         elsif ( $item eq $taxkey_used->{pos_ustva} ) {
184           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
185         }
186         else {
187           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
188         }
189
190       }
191
192       $i++;
193     }
194   }
195
196   # Newaccount Folgekonto
197   if (@{ $form->{NEWACCOUNT} || [] }) {
198     if (!$form->{new_chart_valid}) {
199       $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
200     }
201     foreach my $item (@{ $form->{NEWACCOUNT} }) {
202       if ($item->{id} == $form->{new_chart_id}) {
203         $form->{selectnewaccount} .=
204           qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
205       } elsif (!$form->{new_chart_valid}) {
206         $form->{selectnewaccount} .=
207           qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
208       }
209
210     }
211   }
212
213   my $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
214   my %eur = (
215           1  => "Umsatzerlöse",
216           2  => "sonstige Erlöse",
217           3  => "Privatanteile",
218           4  => "Zinserträge",
219           5  => "Ausserordentliche Erträge",
220           6  => "Vereinnahmte Umsatzst.",
221           7  => "Umsatzsteuererstattungen",
222           8  => "Wareneingänge",
223           9  => "Löhne und Gehälter",
224           10 => "Gesetzl. sozialer Aufw.",
225           11 => "Mieten",
226           12 => "Gas, Strom, Wasser",
227           13 => "Instandhaltung",
228           14 => "Steuern, Versich., Beiträge",
229           15 => "Kfz-Steuern",
230           16 => "Kfz-Versicherungen",
231           17 => "Sonst. Fahrzeugkosten",
232           18 => "Werbe- und Reisekosten",
233           19 => "Instandhaltung u. Werkzeuge",
234           20 => "Fachzeitschriften, Bücher",
235           21 => "Miete für Einrichtungen",
236           22 => "Rechts- und Beratungskosten",
237           23 => "Bürobedarf, Porto, Telefon",
238           24 => "Sonstige Aufwendungen",
239           25 => "Abschreibungen auf Anlagever.",
240           26 => "Abschreibungen auf GWG",
241           27 => "Vorsteuer",
242           28 => "Umsatzsteuerzahlungen",
243           29 => "Zinsaufwand",
244           30 => "Ausserordentlicher Aufwand",
245           31 => "Betriebliche Steuern");
246   foreach my $item (sort({ $a <=> $b } keys(%eur))) {
247     my $text = H(SL::Iconv::convert("ISO-8859-15", $main::dbcharset, $eur{$item}));
248     if ($item == $form->{pos_eur}) {
249       $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
250     } else {
251       $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
252     }
253
254   }
255
256   my $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
257
258   my %bwapos = (
259              1  => 'Umsatzerlöse',
260              2  => 'Best.Verdg.FE/UE',
261              3  => 'Aktiv.Eigenleistung',
262              4  => 'Mat./Wareneinkauf',
263              5  => 'So.betr.Erlöse',
264              10 => 'Personalkosten',
265              11 => 'Raumkosten',
266              12 => 'Betriebl.Steuern',
267              13 => 'Vers./Beiträge',
268              14 => 'Kfz.Kosten o.St.',
269              15 => 'Werbe-Reisek.',
270              16 => 'Kosten Warenabgabe',
271              17 => 'Abschreibungen',
272              18 => 'Rep./instandhlt.',
273              19 => 'Übrige Steuern',
274              20 => 'Sonst.Kosten',
275              30 => 'Zinsauwand',
276              31 => 'Sonst.neutr.Aufw.',
277              32 => 'Zinserträge',
278              33 => 'Sonst.neutr.Ertrag',
279              34 => 'Verr.kalk.Kosten',
280              35 => 'Steuern Eink.u.Ertr.');
281   foreach my $item (sort({ $a <=> $b } keys %bwapos)) {
282     my $text = H(SL::Iconv::convert("ISO-8859-15", $main::dbcharset, $bwapos{$item}));
283     if ($item == $form->{pos_bwa}) {
284       $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
285     } else {
286       $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
287     }
288
289   }
290
291 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
292   my $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
293   foreach my $item ((1, 2, 3, 4)) {
294     if ($item == $form->{pos_bilanz}) {
295       $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
296     } else {
297       $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
298     }
299
300   }
301
302   # this is for our parser only! Do not remove.
303   # type=submit $locale->text('Add Account')
304   # type=submit $locale->text('Edit Account')
305
306   $form->{type} = "account";
307
308   # preselections category
309
310   my $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
311
312   my %category = (
313       'A'  => $locale->text('Asset'),
314       'L'  => $locale->text('Liability'),
315       'Q'  => $locale->text('Equity'),
316       'I'  => $locale->text('Revenue'),
317       'E'  => $locale->text('Expense'),
318       'C'  => $locale->text('Costs'),
319   );
320   foreach my $item ( sort({ $a <=> $b } keys %category) ) {
321     if ($item eq $form->{category}) {
322       $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
323     } else {
324       $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
325     }
326
327   }
328
329   # preselection chart type
330   my $select_charttype = q{};
331
332   my %charttype = (
333       'A'  => $locale->text('Account'),
334       'H'  => $locale->text('Header'),
335   );
336
337   foreach my $item ( sort({ $a <=> $b } keys %charttype) ) {
338     if ($item eq $form->{charttype}) {
339       $select_charttype .= qq|<option value="$item" selected="selected">$charttype{$item}\n|;
340
341     } else {
342       $select_charttype .= qq|<option value="$item">$charttype{$item}\n|;
343     }
344
345   }
346
347   my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
348
349   $form->header();
350
351   my $parameters_ref = {
352     ChartTypeIsAccount         => $ChartTypeIsAccount,
353     select_category            => $select_category,
354     select_charttype           => $select_charttype,
355     select_bwa                 => $select_bwa,
356     select_bilanz              => $select_bilanz,
357     select_eur                 => $select_eur,
358   };
359
360   # Ausgabe des Templates
361   print($form->parse_html_template('am/edit_accounts', $parameters_ref));
362
363
364   $main::lxdebug->leave_sub();
365 }
366
367 sub form_footer {
368   $main::lxdebug->enter_sub();
369
370   my $form     = $main::form;
371   my $locale   = $main::locale;
372
373   $main::auth->assert('config');
374
375   print qq|
376
377 <input name=callback type=hidden value="| . H($form->{callback}) . qq|">
378
379 <br>|;
380   if ((!$form->{id}) || ($form->{id} && $form->{orphaned}) || (($form->{type} eq "account") && (!$form->{new_chart_valid}))) {
381     print qq|
382 <input type=submit class=submit name=action value="|
383     . $locale->text('Save') . qq|">
384 |;
385 }
386
387   if ($form->{id} && $form->{orphaned}) {
388     print qq|<input type=submit class=submit name=action value="|
389       . $locale->text('Delete') . qq|">|;
390   }
391
392   if ($form->{id} && $form->{type} eq "account") {
393     print qq|
394     <input class=submit type=submit name=action value="|
395       . $locale->text('Save as new') . qq|">|;
396   }
397
398   print qq|
399 </form>
400
401 </body>
402 </html>
403 |;
404
405   $main::lxdebug->leave_sub();
406 }
407
408 sub save_account {
409   $main::lxdebug->enter_sub();
410
411   my $form     = $main::form;
412   my %myconfig = %main::myconfig;
413   my $locale   = $main::locale;
414
415   $main::auth->assert('config');
416
417   $form->isblank("accno",       $locale->text('Account Number missing!'));
418   $form->isblank("description", $locale->text('Account Description missing!'));
419
420   if ($form->{charttype} eq 'A'){
421     $form->isblank("category",  $locale->text('Account Type missing!'));
422   }
423
424   $form->redirect($locale->text('Account saved!'))
425     if (AM->save_account(\%myconfig, \%$form));
426   $form->error($locale->text('Cannot save account!'));
427
428   $main::lxdebug->leave_sub();
429 }
430
431 sub save_as_new_account {
432   $main::lxdebug->enter_sub();
433
434   my $form     = $main::form;
435   my %myconfig = %main::myconfig;
436   my $locale   = $main::locale;
437
438   $main::auth->assert('config');
439
440   $form->isblank("accno",       $locale->text('Account Number missing!'));
441   $form->isblank("description", $locale->text('Account Description missing!'));
442
443   if ($form->{charttype} eq 'A'){
444     $form->isblank("category",  $locale->text('Account Type missing!'));
445   }
446
447   for my $taxkey (0 .. 9) {
448     if ($form->{"taxkey_id_$taxkey"}) {
449       $form->{"taxkey_id_$taxkey"} = "NEW";
450     }
451   }
452
453   $form->{id} = 0;
454   if ($form->{"original_accno"} &&
455       ($form->{"accno"} eq $form->{"original_accno"})) {
456     $form->error($locale->text('Account Number already used!'));
457   }
458   $form->redirect($locale->text('Account saved!'))
459     if (AM->save_account(\%myconfig, \%$form));
460   $form->error($locale->text('Cannot save account!'));
461
462   $main::lxdebug->leave_sub();
463 }
464
465 sub list_account {
466   $main::lxdebug->enter_sub();
467
468   my $form     = $main::form;
469   my %myconfig = %main::myconfig;
470   my $locale   = $main::locale;
471
472   $main::auth->assert('config');
473
474   $form->{callback}     = build_std_url('action=list_account');
475   my $link_edit_account = build_std_url('action=edit_account', 'callback');
476
477   CA->all_accounts(\%myconfig, \%$form);
478
479   foreach my $ca (@{ $form->{CA} }) {
480
481     $ca->{debit}  = "";
482     $ca->{credit} = "";
483
484     if ($ca->{amount} > 0) {
485       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
486     }
487     if ($ca->{amount} < 0) {
488       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
489     }
490     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:'';
491     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
492   }
493
494   # Ajax
495   my $pjx = new CGI::Ajax('list_account_details' => build_std_url('action=list_account_details'));
496
497   # Eneable AJAX debuging
498   #$pjx->DEBUG(1);
499   #$pjx->JSDEBUG(1);
500
501   push(@ { $form->{AJAX} }, $pjx);
502
503   $form->{stylesheets} = "list_accounts.css";
504   $form->{title}       = $locale->text('Chart of Accounts');
505
506   $form->header;
507
508
509   my $parameters_ref = {
510   #   hidden_variables                => $_hidden_variables_ref,
511   };
512
513   # Ausgabe des Templates
514   print($form->parse_html_template('am/list_accounts', $parameters_ref));
515
516   $main::lxdebug->leave_sub();
517
518 }
519
520
521 sub list_account_details {
522 # Ajax Funktion aus list_account_details
523   $main::lxdebug->enter_sub();
524
525   my $form     = $main::form;
526   my %myconfig = %main::myconfig;
527   my $locale   = $main::locale;
528
529   $main::auth->assert('config');
530
531   my $chart_id = $form->{args};
532
533   CA->all_accounts(\%myconfig, \%$form, $chart_id);
534
535   foreach my $ca (@{ $form->{CA} }) {
536
537     $ca->{debit}  = "&nbsp;";
538     $ca->{credit} = "&nbsp;";
539
540     if ($ca->{amount} > 0) {
541       $ca->{credit} =
542         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
543     }
544     if ($ca->{amount} < 0) {
545       $ca->{debit} =
546         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
547     }
548
549     my @links = split( q{:}, $ca->{link});
550
551     $ca->{link} = q{};
552
553     foreach my $link (@links){
554       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
555                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
556                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
557                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
558                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
559                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
560                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
561                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
562                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
563                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
564                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
565                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
566                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
567                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
568                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
569 #               : ( $link eq 'CT_tax' )        ? $locale->text('Account Link CT_tax')
570                : $locale->text('Unknown Link') . ': ' . $link;
571       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
572     }
573
574     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
575                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
576                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
577                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
578                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
579                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
580                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
581                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
582   }
583
584   $form->{title} = $locale->text('Chart of Accounts');
585   $form->header();
586
587   print $form->parse_html_template('am/list_account_details');
588
589   $main::lxdebug->leave_sub();
590
591 }
592
593 sub delete_account {
594   $main::lxdebug->enter_sub();
595
596   my $form     = $main::form;
597   my %myconfig = %main::myconfig;
598   my $locale   = $main::locale;
599
600   $main::auth->assert('config');
601
602   $form->{title} = $locale->text('Delete Account');
603
604   foreach my $id (
605     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
606     ) {
607     if ($form->{id} == $form->{$id}) {
608       $form->error($locale->text('Cannot delete default account!'));
609     }
610   }
611
612   $form->redirect($locale->text('Account deleted!'))
613     if (AM->delete_account(\%myconfig, \%$form));
614   $form->error($locale->text('Cannot delete account!'));
615
616   $main::lxdebug->leave_sub();
617 }
618
619 sub add_department {
620   $main::lxdebug->enter_sub();
621
622   my $form     = $main::form;
623
624   $main::auth->assert('config');
625
626   $form->{title} = "Add";
627   $form->{role}  = "P";
628
629   $form->{callback} = "am.pl?action=add_department" unless $form->{callback};
630
631   &department_header;
632   &form_footer;
633
634   $main::lxdebug->leave_sub();
635 }
636
637 sub edit_department {
638   $main::lxdebug->enter_sub();
639
640   my $form     = $main::form;
641   my %myconfig = %main::myconfig;
642
643   $main::auth->assert('config');
644
645   $form->{title} = "Edit";
646
647   AM->get_department(\%myconfig, \%$form);
648
649   &department_header;
650   &form_footer;
651
652   $main::lxdebug->leave_sub();
653 }
654
655 sub list_department {
656   $main::lxdebug->enter_sub();
657
658   my $form     = $main::form;
659   my %myconfig = %main::myconfig;
660   my $locale   = $main::locale;
661
662   $main::auth->assert('config');
663
664   AM->departments(\%myconfig, \%$form);
665
666   $form->{callback} = "am.pl?action=list_department";
667
668   my $callback = $form->escape($form->{callback});
669
670   $form->{title} = $locale->text('Departments');
671
672   my @column_index = qw(description cost profit);
673   my %column_header;
674   $column_header{description} =
675       qq|<th class=listheading width=90%>|
676     . $locale->text('Description')
677     . qq|</th>|;
678   $column_header{cost} =
679       qq|<th class=listheading nowrap>|
680     . $locale->text('Cost Center')
681     . qq|</th>|;
682   $column_header{profit} =
683       qq|<th class=listheading nowrap>|
684     . $locale->text('Profit Center')
685     . qq|</th>|;
686
687   $form->header;
688
689   print qq|
690 <body>
691
692 <table width=100%>
693   <tr>
694     <th class=listtop>$form->{title}</th>
695   </tr>
696   <tr height="5"></tr>
697   <tr>
698     <td>
699       <table width=100%>
700         <tr class=listheading>
701 |;
702
703   map { print "$column_header{$_}\n" } @column_index;
704
705   print qq|
706         </tr>
707 |;
708
709   my ($i, %column_data);
710   foreach my $ref (@{ $form->{ALL} }) {
711
712     $i++;
713     $i %= 2;
714
715     print qq|
716         <tr valign=top class=listrow$i>
717 |;
718
719     my $costcenter   = ($ref->{role} eq "C") ? "X" : "";
720     my $profitcenter = ($ref->{role} eq "P") ? "X" : "";
721
722     $column_data{description} =
723       qq|<td><a href="am.pl?action=edit_department&id=$ref->{id}&callback=$callback">$ref->{description}</td>|;
724     $column_data{cost}   = qq|<td align=center>$costcenter</td>|;
725     $column_data{profit} = qq|<td align=center>$profitcenter</td>|;
726
727     map { print "$column_data{$_}\n" } @column_index;
728
729     print qq|
730         </tr>
731 |;
732   }
733
734   print qq|
735       </table>
736     </td>
737   </tr>
738   <tr>
739   <td><hr size=3 noshade></td>
740   </tr>
741 </table>
742
743 <br>
744 <form method=post action=am.pl>
745
746 <input name=callback type=hidden value="$form->{callback}">
747
748 <input type=hidden name=type value=department>
749
750 <input class=submit type=submit name=action value="|
751     . $locale->text('Add') . qq|">
752
753   </form>
754
755   </body>
756   </html>
757 |;
758
759   $main::lxdebug->leave_sub();
760 }
761
762 sub department_header {
763   $main::lxdebug->enter_sub();
764
765   my $form     = $main::form;
766   my $locale   = $main::locale;
767
768   $main::auth->assert('config');
769
770   $form->{title} = $locale->text("$form->{title} Department");
771
772   # $locale->text('Add Department')
773   # $locale->text('Edit Department')
774
775   $form->{description} =~ s/\"/&quot;/g;
776
777   my ($rows, $description);
778   if (($rows = $form->numtextrows($form->{description}, 60)) > 1) {
779     $description =
780       qq|<textarea name="description" rows=$rows cols=60 wrap=soft>$form->{description}</textarea>|;
781   } else {
782     $description =
783       qq|<input name=description size=60 value="$form->{description}">|;
784   }
785
786   my $costcenter   = "checked" if $form->{role} eq "C";
787   my $profitcenter = "checked" if $form->{role} eq "P";
788
789   $form->header;
790
791   print qq|
792 <body>
793
794 <form method=post action=am.pl>
795
796 <input type=hidden name=id value=$form->{id}>
797 <input type=hidden name=type value=department>
798
799 <table width=100%>
800   <tr>
801     <th class=listtop colspan=2>$form->{title}</th>
802   </tr>
803   <tr height="5"></tr>
804   <tr>
805     <th align=right>| . $locale->text('Description') . qq|</th>
806     <td>$description</td>
807   </tr>
808   <tr>
809     <td></td>
810     <td><input type=radio style=radio name=role value="C" $costcenter> |
811     . $locale->text('Cost Center') . qq|
812         <input type=radio style=radio name=role value="P" $profitcenter> |
813     . $locale->text('Profit Center') . qq|
814     </td>
815   <tr>
816     <td colspan=2><hr size=3 noshade></td>
817   </tr>
818 </table>
819 |;
820
821   $main::lxdebug->leave_sub();
822 }
823
824 sub save_department {
825   $main::lxdebug->enter_sub();
826
827   my $form     = $main::form;
828   my %myconfig = %main::myconfig;
829   my $locale   = $main::locale;
830
831   $main::auth->assert('config');
832
833   $form->isblank("description", $locale->text('Description missing!'));
834   AM->save_department(\%myconfig, \%$form);
835   $form->redirect($locale->text('Department saved!'));
836
837   $main::lxdebug->leave_sub();
838 }
839
840 sub delete_department {
841   $main::lxdebug->enter_sub();
842
843   my $form     = $main::form;
844   my %myconfig = %main::myconfig;
845   my $locale   = $main::locale;
846
847   $main::auth->assert('config');
848
849   AM->delete_department(\%myconfig, \%$form);
850   $form->redirect($locale->text('Department deleted!'));
851
852   $main::lxdebug->leave_sub();
853 }
854
855 sub add_lead {
856   $main::lxdebug->enter_sub();
857
858   my $form     = $main::form;
859
860   $main::auth->assert('config');
861
862   $form->{title} = "Add";
863
864   $form->{callback} = "am.pl?action=add_lead" unless $form->{callback};
865
866   &lead_header;
867   &form_footer;
868
869   $main::lxdebug->leave_sub();
870 }
871
872 sub edit_lead {
873   $main::lxdebug->enter_sub();
874
875   my $form     = $main::form;
876   my %myconfig = %main::myconfig;
877
878   $main::auth->assert('config');
879
880   $form->{title} = "Edit";
881
882   AM->get_lead(\%myconfig, \%$form);
883
884   &lead_header;
885
886   $form->{orphaned} = 1;
887   &form_footer;
888
889   $main::lxdebug->leave_sub();
890 }
891
892 sub list_lead {
893   $main::lxdebug->enter_sub();
894
895   my $form     = $main::form;
896   my %myconfig = %main::myconfig;
897   my $locale   = $main::locale;
898
899   $main::auth->assert('config');
900
901   AM->lead(\%myconfig, \%$form);
902
903   $form->{callback} = "am.pl?action=list_lead";
904
905   my $callback = $form->escape($form->{callback});
906
907   $form->{title} = $locale->text('Lead');
908
909   my @column_index = qw(description cost profit);
910   my %column_header;
911   $column_header{description} =
912       qq|<th class=listheading width=100%>|
913     . $locale->text('Description')
914     . qq|</th>|;
915
916   $form->header;
917
918   print qq|
919 <body>
920
921 <table width=100%>
922   <tr>
923     <th class=listtop>$form->{title}</th>
924   </tr>
925   <tr height="5"></tr>
926   <tr class=listheading>
927 |;
928
929   map { print "$column_header{$_}\n" } @column_index;
930
931   print qq|
932   </tr>
933 |;
934
935   my ($i, %column_data);
936   foreach my $ref (@{ $form->{ALL} }) {
937
938     $i++;
939     $i %= 2;
940
941     print qq|
942   <tr valign=top class=listrow$i>
943 |;
944
945 #    $lead = $ref->{lead};
946
947     $column_data{description} = qq|<td><a href="am.pl?action=edit_lead&id=$ref->{id}&callback=$callback">$ref->{lead}</td>|;
948
949     map { print "$column_data{$_}\n" } @column_index;
950
951     print qq|
952   </tr>
953 |;
954   }
955
956   print qq|
957   <tr>
958   <td><hr size=3 noshade></td>
959   </tr>
960 </table>
961
962 <br>
963 <form method=post action=am.pl>
964
965 <input name=callback type=hidden value="$form->{callback}">
966
967 <input type=hidden name=type value=lead>
968
969 <input class=submit type=submit name=action value="|
970     . $locale->text('Add') . qq|">
971
972   </form>
973
974   </body>
975   </html>
976 |;
977
978   $main::lxdebug->leave_sub();
979 }
980
981 sub lead_header {
982   $main::lxdebug->enter_sub();
983
984   my $form     = $main::form;
985   my $locale   = $main::locale;
986
987   $main::auth->assert('config');
988
989   $form->{title} = $locale->text("$form->{title} Lead");
990
991   # $locale->text('Add Lead')
992   # $locale->text('Edit Lead')
993
994   $form->{description} =~ s/\"/&quot;/g;
995
996   my $description =
997       qq|<input name=description size=50 value="$form->{lead}">|;
998
999   $form->header;
1000
1001   print qq|
1002 <body>
1003
1004 <form method=post action=am.pl>
1005
1006 <input type=hidden name=id value=$form->{id}>
1007 <input type=hidden name=type value=lead>
1008
1009 <table width=100%>
1010   <tr>
1011     <th class=listtop colspan=2>$form->{title}</th>
1012   </tr>
1013   <tr height="5"></tr>
1014   <tr>
1015     <th align=right>| . $locale->text('Description') . qq|</th>
1016     <td>$description</td>
1017   </tr>
1018     <td colspan=2><hr size=3 noshade></td>
1019   </tr>
1020 </table>
1021 |;
1022
1023   $main::lxdebug->leave_sub();
1024 }
1025
1026 sub save_lead {
1027   $main::lxdebug->enter_sub();
1028
1029   my $form     = $main::form;
1030   my %myconfig = %main::myconfig;
1031   my $locale   = $main::locale;
1032
1033   $main::auth->assert('config');
1034
1035   $form->isblank("description", $locale->text('Description missing!'));
1036   AM->save_lead(\%myconfig, \%$form);
1037   $form->redirect($locale->text('lead saved!'));
1038
1039   $main::lxdebug->leave_sub();
1040 }
1041
1042 sub delete_lead {
1043   $main::lxdebug->enter_sub();
1044
1045   my $form     = $main::form;
1046   my %myconfig = %main::myconfig;
1047   my $locale   = $main::locale;
1048
1049   $main::auth->assert('config');
1050
1051   AM->delete_lead(\%myconfig, \%$form);
1052   $form->redirect($locale->text('lead deleted!'));
1053
1054   $main::lxdebug->leave_sub();
1055 }
1056
1057 sub add_business {
1058   $main::lxdebug->enter_sub();
1059
1060   my $form     = $main::form;
1061
1062   $main::auth->assert('config');
1063
1064   $form->{title} = "Add";
1065
1066   $form->{callback} = "am.pl?action=add_business" unless $form->{callback};
1067
1068   &business_header;
1069   &form_footer;
1070
1071   $main::lxdebug->leave_sub();
1072 }
1073
1074 sub edit_business {
1075   $main::lxdebug->enter_sub();
1076
1077   my $form     = $main::form;
1078   my %myconfig = %main::myconfig;
1079
1080   $form->{title} = "Edit";
1081
1082   AM->get_business(\%myconfig, \%$form);
1083
1084   &business_header;
1085
1086   $form->{orphaned} = 1;
1087   &form_footer;
1088
1089   $main::lxdebug->leave_sub();
1090 }
1091
1092 sub list_business {
1093   $main::lxdebug->enter_sub();
1094
1095   my $form     = $main::form;
1096   my %myconfig = %main::myconfig;
1097   my $locale   = $main::locale;
1098
1099   $main::auth->assert('config');
1100
1101   AM->business(\%myconfig, \%$form);
1102
1103   $form->{callback} = "am.pl?action=list_business";
1104
1105   my $callback = $form->escape($form->{callback});
1106
1107   $form->{title} = $locale->text('Type of Business');
1108
1109   my @column_index = qw(description discount customernumberinit);
1110   push @column_index, 'salesman' if $::vertreter;
1111   my %column_header;
1112   $column_header{description} =
1113       qq|<th class=listheading width=60%>|
1114     . $locale->text('Description')
1115     . qq|</th>|;
1116   $column_header{discount} =
1117       qq|<th class=listheading width=10%>|
1118     . $locale->text('Discount')
1119     . qq| %</th>|;
1120   $column_header{customernumberinit} =
1121       qq|<th class=listheading>|
1122     . $locale->text('Customernumberinit')
1123     . qq|</th>|;
1124   $column_header{salesman} =
1125       qq|<th class=listheading>|
1126     . $locale->text('Representative')
1127     . qq|</th>|;
1128
1129   $form->header;
1130
1131   print qq|
1132 <body>
1133
1134 <table width=100%>
1135   <tr>
1136     <th class=listtop>$form->{title}</th>
1137   </tr>
1138   <tr height="5"></tr>
1139   <tr>
1140     <td>
1141       <table width=100%>
1142         <tr class=listheading>
1143 |;
1144
1145   map { print "$column_header{$_}\n" } @column_index;
1146
1147   print qq|
1148         </tr>
1149 |;
1150
1151   my ($i, %column_data);
1152   foreach my $ref (@{ $form->{ALL} }) {
1153
1154     $i++;
1155     $i %= 2;
1156
1157     print qq|
1158         <tr valign=top class=listrow$i>
1159 |;
1160
1161     my $discount    = $form->format_amount(\%myconfig, $ref->{discount} * 100);
1162     my $description = $ref->{description};
1163     $column_data{description} = qq|<td><a href="am.pl?action=edit_business&id=$ref->{id}&callback=$callback">$description</td>|;
1164     $column_data{discount}           = qq|<td align=right>$discount</td>|;
1165     $column_data{customernumberinit} =
1166       qq|<td align=right>$ref->{customernumberinit}</td>|;
1167     $column_data{salesman} = '<td>' . ($ref->{salesman} ? $::locale->text('Yes') : $::locale->text('No')) . '</td>';
1168
1169     map { print "$column_data{$_}\n" } @column_index;
1170
1171     print qq|
1172         </tr>
1173 |;
1174   }
1175
1176   print qq|
1177       </table>
1178     </td>
1179   </tr>
1180   <tr>
1181   <td><hr size=3 noshade></td>
1182   </tr>
1183 </table>
1184
1185 <br>
1186 <form method=post action=am.pl>
1187
1188 <input name=callback type=hidden value="$form->{callback}">
1189
1190 <input type=hidden name=type value=business>
1191
1192 <input class=submit type=submit name=action value="|
1193     . $locale->text('Add') . qq|">
1194
1195   </form>
1196
1197   </body>
1198   </html>
1199 |;
1200
1201   $main::lxdebug->leave_sub();
1202 }
1203
1204 sub business_header {
1205   $main::lxdebug->enter_sub();
1206
1207   my $form     = $main::form;
1208   my %myconfig = %main::myconfig;
1209   my $locale   = $main::locale;
1210
1211   $main::auth->assert('config');
1212
1213   $form->{title}    = $locale->text("$form->{title} Business");
1214
1215   # $locale->text('Add Business')
1216   # $locale->text('Edit Business')
1217
1218   $form->{description} =~ s/\"/&quot;/g;
1219   $form->{discount} =
1220     $form->format_amount(\%myconfig, $form->{discount} * 100);
1221
1222   my $salesman_code;
1223   if ($::vertreter) {
1224     $salesman_code = qq|
1225   <tr>
1226     <th align="right">| . $locale->text('Representative') . qq|</th>
1227     <td>| . $::cgi->checkbox(-name => "salesman", -value => 1, -label => '', 'checked' => $form->{salesman} ? 1 : 0) . qq|</td>
1228   </tr>
1229 |;
1230   } else {
1231     $salesman_code = $::cgi->hidden(-name => 'salesman', -value => $form->{salesman} ? 1 : 0);
1232   }
1233
1234   $form->header;
1235
1236   print qq|
1237 <body>
1238
1239 <form method=post action=am.pl>
1240
1241 <input type=hidden name=id value=$form->{id}>
1242 <input type=hidden name=type value=business>
1243
1244 <table width=100%>
1245   <tr>
1246     <th class=listtop colspan=2>$form->{title}</th>
1247   </tr>
1248   <tr height="5"></tr>
1249   <tr>
1250     <th align=right>| . $locale->text('Type of Business') . qq|</th>
1251     <td><input name=description size=30 value="$form->{description}"></td>
1252   <tr>
1253   <tr>
1254     <th align=right>| . $locale->text('Discount') . qq| %</th>
1255     <td><input name=discount size=5 value=$form->{discount}></td>
1256   </tr>
1257   <tr>
1258     <th align=right>| . $locale->text('Customernumberinit') . qq|</th>
1259     <td><input name=customernumberinit size=10 value=$form->{customernumberinit}></td>
1260   </tr>
1261 $salesman_code
1262   <td colspan=2><hr size=3 noshade></td>
1263   </tr>
1264 </table>
1265 |;
1266
1267   $main::lxdebug->leave_sub();
1268 }
1269
1270 sub save_business {
1271   $main::lxdebug->enter_sub();
1272
1273   my $form     = $main::form;
1274   my %myconfig = %main::myconfig;
1275   my $locale   = $main::locale;
1276
1277   $main::auth->assert('config');
1278
1279   $form->isblank("description", $locale->text('Description missing!'));
1280   $form->{discount} = $form->parse_amount(\%myconfig, $form->{discount}) / 100;
1281   AM->save_business(\%myconfig, \%$form);
1282   $form->redirect($locale->text('Business saved!'));
1283
1284   $main::lxdebug->leave_sub();
1285 }
1286
1287 sub delete_business {
1288   $main::lxdebug->enter_sub();
1289
1290   my $form     = $main::form;
1291   my %myconfig = %main::myconfig;
1292   my $locale   = $main::locale;
1293
1294   $main::auth->assert('config');
1295
1296   AM->delete_business(\%myconfig, \%$form);
1297   $form->redirect($locale->text('Business deleted!'));
1298
1299   $main::lxdebug->leave_sub();
1300 }
1301
1302 sub add_language {
1303   $main::lxdebug->enter_sub();
1304
1305   my $form     = $main::form;
1306
1307   $main::auth->assert('config');
1308
1309   $form->{title} = "Add";
1310
1311   $form->{callback} = "am.pl?action=add_language" unless $form->{callback};
1312
1313   &language_header;
1314   &form_footer;
1315
1316   $main::lxdebug->leave_sub();
1317 }
1318
1319 sub edit_language {
1320   $main::lxdebug->enter_sub();
1321
1322   my $form     = $main::form;
1323   my %myconfig = %main::myconfig;
1324
1325   $main::auth->assert('config');
1326
1327   $form->{title} = "Edit";
1328
1329   AM->get_language(\%myconfig, \%$form);
1330
1331   &language_header;
1332
1333   $form->{orphaned} = 1;
1334   &form_footer;
1335
1336   $main::lxdebug->leave_sub();
1337 }
1338
1339 sub list_language {
1340   $main::lxdebug->enter_sub();
1341
1342   my $form     = $main::form;
1343   my %myconfig = %main::myconfig;
1344   my $locale   = $main::locale;
1345
1346   $main::auth->assert('config');
1347
1348   AM->language(\%myconfig, \%$form);
1349
1350   $form->{callback} = "am.pl?action=list_language";
1351
1352   my $callback = $form->escape($form->{callback});
1353
1354   $form->{title} = $locale->text('Languages');
1355
1356   my @column_index = qw(description template_code article_code output_numberformat output_dateformat output_longdates);
1357   my %column_header;
1358   $column_header{description} =
1359       qq|<th class=listheading width=60%>|
1360     . $locale->text('Description')
1361     . qq|</th>|;
1362   $column_header{template_code} =
1363       qq|<th class=listheading width=10%>|
1364     . $locale->text('Template Code')
1365     . qq|</th>|;
1366   $column_header{article_code} =
1367       qq|<th class=listheading>|
1368     . $locale->text('Article Code')
1369     . qq|</th>|;
1370   $column_header{output_numberformat} =
1371       qq|<th class=listheading>|
1372     . $locale->text('Number Format')
1373     . qq|</th>|;
1374   $column_header{output_dateformat} =
1375       qq|<th class=listheading>|
1376     . $locale->text('Date Format')
1377     . qq|</th>|;
1378   $column_header{output_longdates} =
1379       qq|<th class=listheading>|
1380     . $locale->text('Long Dates')
1381     . qq|</th>|;
1382
1383   $form->header;
1384
1385   print qq|
1386 <body>
1387
1388 <table width=100%>
1389   <tr>
1390     <th class=listtop>$form->{title}</th>
1391   </tr>
1392   <tr height="5"></tr>
1393   <tr>
1394     <td>
1395       <table width=100%>
1396         <tr class=listheading>
1397 |;
1398
1399   map { print "$column_header{$_}\n" } @column_index;
1400
1401   print qq|
1402         </tr>
1403 |;
1404
1405   my ($i, %column_data);
1406   foreach my $ref (@{ $form->{ALL} }) {
1407
1408     $i++;
1409     $i %= 2;
1410
1411     print qq|
1412         <tr valign=top class=listrow$i>
1413 |;
1414
1415
1416     $column_data{description} =
1417       qq|<td><a href="am.pl?action=edit_language&id=$ref->{id}&callback=$callback">$ref->{description}</td>|;
1418     $column_data{template_code}           = qq|<td align=right>$ref->{template_code}</td>|;
1419     $column_data{article_code} =
1420       qq|<td align=right>$ref->{article_code}</td>|;
1421     $column_data{output_numberformat} =
1422       "<td nowrap>" .
1423       ($ref->{output_numberformat} ? $ref->{output_numberformat} :
1424        $locale->text("use program settings")) .
1425       "</td>";
1426     $column_data{output_dateformat} =
1427       "<td nowrap>" .
1428       ($ref->{output_dateformat} ? $ref->{output_dateformat} :
1429        $locale->text("use program settings")) .
1430       "</td>";
1431     $column_data{output_longdates} =
1432       "<td nowrap>" .
1433       ($ref->{output_longdates} ? $locale->text("Yes") : $locale->text("No")) .
1434       "</td>";
1435
1436     map { print "$column_data{$_}\n" } @column_index;
1437
1438     print qq|
1439         </tr>
1440 |;
1441   }
1442
1443   print qq|
1444       </table>
1445     </td>
1446   </tr>
1447   <tr>
1448   <td><hr size=3 noshade></td>
1449   </tr>
1450 </table>
1451
1452 <br>
1453 <form method=post action=am.pl>
1454
1455 <input name=callback type=hidden value="$form->{callback}">
1456
1457 <input type=hidden name=type value=language>
1458
1459 <input class=submit type=submit name=action value="|
1460     . $locale->text('Add') . qq|">
1461
1462   </form>
1463
1464   </body>
1465   </html>
1466 |;
1467
1468   $main::lxdebug->leave_sub();
1469 }
1470
1471 sub language_header {
1472   $main::lxdebug->enter_sub();
1473
1474   my $form     = $main::form;
1475   my $locale   = $main::locale;
1476
1477   $main::auth->assert('config');
1478
1479   $form->{title}    = $locale->text("$form->{title} Language");
1480
1481   # $locale->text('Add Language')
1482   # $locale->text('Edit Language')
1483
1484   $form->{description} =~ s/\"/&quot;/g;
1485   $form->{template_code} =~ s/\"/&quot;/g;
1486   $form->{article_code} =~ s/\"/&quot;/g;
1487
1488
1489   $form->header;
1490
1491   my $numberformat =
1492     qq|<option value="">| . $locale->text("use program settings") .
1493     qq|</option>|;
1494   foreach my $item (('1,000.00', '1000.00', '1.000,00', '1000,00')) {
1495     $numberformat .=
1496       ($item eq $form->{output_numberformat})
1497       ? "<option selected>$item"
1498       : "<option>$item"
1499       . "</option>";
1500   }
1501
1502   my $dateformat =
1503     qq|<option value="">| . $locale->text("use program settings") .
1504     qq|</option>|;
1505   foreach my $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
1506     $dateformat .=
1507       ($item eq $form->{output_dateformat})
1508       ? "<option selected>$item"
1509       : "<option>$item"
1510       . "</option>";
1511   }
1512
1513   print qq|
1514 <body>
1515
1516 <form method=post action=am.pl>
1517
1518 <input type=hidden name=id value=$form->{id}>
1519 <input type=hidden name=type value=language>
1520
1521 <table width=100%>
1522   <tr>
1523     <th class=listtop colspan=2>$form->{title}</th>
1524   </tr>
1525   <tr height="5"></tr>
1526   <tr>
1527     <th align=right>| . $locale->text('Language') . qq|</th>
1528     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1529   <tr>
1530   <tr>
1531     <th align=right>| . $locale->text('Template Code') . qq|</th>
1532     <td><input name=template_code size=5 value="| . $form->quote($form->{template_code}) . qq|"></td>
1533   </tr>
1534   <tr>
1535     <th align=right>| . $locale->text('Article Code') . qq|</th>
1536     <td><input name=article_code size=10 value="| . $form->quote($form->{article_code}) . qq|"></td>
1537   </tr>
1538   <tr>
1539     <th align=right>| . $locale->text('Number Format') . qq|</th>
1540     <td><select name="output_numberformat">$numberformat</select></td>
1541   </tr>
1542   <tr>
1543     <th align=right>| . $locale->text('Date Format') . qq|</th>
1544     <td><select name="output_dateformat">$dateformat</select></td>
1545   </tr>
1546   <tr>
1547     <th align=right>| . $locale->text('Long Dates') . qq|</th>
1548     <td><input type="radio" name="output_longdates" value="1"| .
1549     ($form->{output_longdates} ? " checked" : "") .
1550     qq|>| . $locale->text("Yes") .
1551     qq|<input type="radio" name="output_longdates" value="0"| .
1552     ($form->{output_longdates} ? "" : " checked") .
1553     qq|>| . $locale->text("No") .
1554     qq|</td>
1555   </tr>
1556   <td colspan=2><hr size=3 noshade></td>
1557   </tr>
1558 </table>
1559 |;
1560
1561   $main::lxdebug->leave_sub();
1562 }
1563
1564 sub save_language {
1565   $main::lxdebug->enter_sub();
1566
1567   my $form     = $main::form;
1568   my %myconfig = %main::myconfig;
1569   my $locale   = $main::locale;
1570
1571   $main::auth->assert('config');
1572
1573   $form->isblank("description", $locale->text('Language missing!'));
1574   $form->isblank("template_code", $locale->text('Template Code missing!'));
1575   $form->isblank("article_code", $locale->text('Article Code missing!'));
1576   AM->save_language(\%myconfig, \%$form);
1577   $form->redirect($locale->text('Language saved!'));
1578
1579   $main::lxdebug->leave_sub();
1580 }
1581
1582 sub delete_language {
1583   $main::lxdebug->enter_sub();
1584
1585   my $form     = $main::form;
1586   my %myconfig = %main::myconfig;
1587   my $locale   = $main::locale;
1588
1589   $main::auth->assert('config');
1590
1591   AM->delete_language(\%myconfig, \%$form);
1592   $form->redirect($locale->text('Language deleted!'));
1593
1594   $main::lxdebug->leave_sub();
1595 }
1596
1597
1598 sub add_buchungsgruppe {
1599   $main::lxdebug->enter_sub();
1600
1601   my $form     = $main::form;
1602   my %myconfig = %main::myconfig;
1603   my $locale   = $main::locale;
1604
1605   $main::auth->assert('config');
1606
1607   # $locale->text("Add Buchungsgruppe")
1608   # $locale->text("Edit Buchungsgruppe")
1609   $form->{title} = "Add";
1610
1611   $form->{callback} = "am.pl?action=add_buchungsgruppe" unless $form->{callback};
1612
1613   AM->get_buchungsgruppe(\%myconfig, \%$form);
1614   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
1615   for (my $i = 0; 4 > $i; $i++) {
1616     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
1617         qw(income expense));
1618   }
1619
1620   &buchungsgruppe_header;
1621   &form_footer;
1622
1623   $main::lxdebug->leave_sub();
1624 }
1625
1626 sub edit_buchungsgruppe {
1627   $main::lxdebug->enter_sub();
1628
1629   my $form     = $main::form;
1630   my %myconfig = %main::myconfig;
1631
1632   $main::auth->assert('config');
1633
1634   $form->{title} = "Edit";
1635
1636   AM->get_buchungsgruppe(\%myconfig, \%$form);
1637
1638   &buchungsgruppe_header;
1639
1640   &form_footer;
1641
1642   $main::lxdebug->leave_sub();
1643 }
1644
1645 sub list_buchungsgruppe {
1646   $main::lxdebug->enter_sub();
1647
1648   my $form     = $main::form;
1649   my %myconfig = %main::myconfig;
1650   my $locale   = $main::locale;
1651
1652   $main::auth->assert('config');
1653
1654   AM->buchungsgruppe(\%myconfig, \%$form);
1655
1656   $form->{callback} = "am.pl?action=list_buchungsgruppe";
1657
1658   my $callback = $form->escape($form->{callback});
1659
1660   $form->{title} = $locale->text('Buchungsgruppen');
1661
1662   my @column_index = qw(up down description inventory_accno
1663                      income_accno_0 expense_accno_0
1664                      income_accno_1 expense_accno_1
1665                      income_accno_2 expense_accno_2
1666                      income_accno_3 expense_accno_3 );
1667   my %column_header;
1668   $column_header{up} =
1669       qq|<th class="listheading" width="16">|
1670     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
1671     . qq|</th>|;
1672   $column_header{down} =
1673       qq|<th class="listheading" width="16">|
1674     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
1675     . qq|</th>|;
1676   $column_header{description} =
1677       qq|<th class="listheading" width="40%">|
1678     . $locale->text('Description')
1679     . qq|</th>|;
1680   $column_header{inventory_accno} =
1681       qq|<th class=listheading>|
1682     . $locale->text('Bestandskonto')
1683     . qq|</th>|;
1684   $column_header{income_accno_0} =
1685       qq|<th class=listheading>|
1686     . $locale->text('National Revenues')
1687     . qq|</th>|;
1688   $column_header{expense_accno_0} =
1689       qq|<th class=listheading>|
1690     . $locale->text('National Expenses')
1691     . qq|</th>|;
1692   $column_header{income_accno_1} =
1693       qq|<th class=listheading>|
1694     . $locale->text('Revenues EU with UStId')
1695     . qq|</th>|;
1696   $column_header{expense_accno_1} =
1697       qq|<th class=listheading>|
1698     . $locale->text('Expenses EU with UStId')
1699     . qq|</th>|;
1700   $column_header{income_accno_2} =
1701       qq|<th class=listheading>|
1702     . $locale->text('Revenues EU without UStId')
1703     . qq|</th>|;
1704   $column_header{expense_accno_2} =
1705       qq|<th class=listheading>|
1706     . $locale->text('Expenses EU without UStId')
1707     . qq|</th>|;
1708   $column_header{income_accno_3} =
1709       qq|<th class=listheading>|
1710     . $locale->text('Foreign Revenues')
1711     . qq|</th>|;
1712   $column_header{expense_accno_3} =
1713       qq|<th class=listheading>|
1714     . $locale->text('Foreign Expenses')
1715     . qq|</th>|;
1716   $form->header;
1717
1718   print qq|
1719 <body>
1720
1721 <table width=100%>
1722   <tr>
1723     <th class=listtop>$form->{title}</th>
1724   </tr>
1725   <tr height="5"></tr>
1726   <tr>
1727     <td>
1728       <table width=100%>
1729         <tr class=listheading>
1730 |;
1731
1732   map { print "$column_header{$_}\n" } @column_index;
1733
1734   print qq|
1735         </tr>
1736 |;
1737
1738   my $swap_link = qq|am.pl?action=swap_buchungsgruppen&|;
1739
1740   my $row = 0;
1741   my ($i, %column_data);
1742   foreach my $ref (@{ $form->{ALL} }) {
1743
1744     $i++;
1745     $i %= 2;
1746
1747     print qq|
1748         <tr valign=top class=listrow$i>
1749 |;
1750
1751     if ($row) {
1752       my $pref = $form->{ALL}->[$row - 1];
1753       $column_data{up} =
1754         qq|<td align="center" valign="center" width="16">| .
1755         qq|<a href="${swap_link}id1=$ref->{id}&id2=$pref->{id}">| .
1756         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
1757         qq|</a></td>|;
1758     } else {
1759       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
1760     }
1761
1762     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
1763       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
1764     } else {
1765       my $nref = $form->{ALL}->[$row + 1];
1766       $column_data{down} =
1767         qq|<td align="center" valign="center" width="16">| .
1768         qq|<a href="${swap_link}id1=$ref->{id}&id2=$nref->{id}">| .
1769         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
1770         qq|</a></td>|;
1771     }
1772
1773     $column_data{description} = qq|<td><a href="am.pl?action=edit_buchungsgruppe&id=$ref->{id}&callback=$callback">$ref->{description}</td>|;
1774     $column_data{inventory_accno}           = qq|<td align=right>$ref->{inventory_accno}</td>|;
1775     $column_data{income_accno_0} =
1776       qq|<td align=right>$ref->{income_accno_0}</td>|;
1777     $column_data{expense_accno_0}           = qq|<td align=right>$ref->{expense_accno_0}</td>|;
1778     $column_data{income_accno_1} =
1779       qq|<td align=right>$ref->{income_accno_1}</td>|;
1780     $column_data{expense_accno_1}           = qq|<td align=right>$ref->{expense_accno_1}</td>|;
1781     $column_data{income_accno_2} =
1782       qq|<td align=right>$ref->{income_accno_2}</td>|;
1783     $column_data{expense_accno_2}           = qq|<td align=right>$ref->{expense_accno_2}</td>|;
1784     $column_data{income_accno_3} =
1785       qq|<td align=right>$ref->{income_accno_3}</td>|;
1786     $column_data{expense_accno_3}           = qq|<td align=right>$ref->{expense_accno_3}</td>|;
1787
1788     map { print "$column_data{$_}\n" } @column_index;
1789
1790     print qq|
1791         </tr>
1792 |;
1793
1794     $row++;
1795   }
1796
1797   print qq|
1798       </table>
1799     </td>
1800   </tr>
1801   <tr>
1802   <td><hr size=3 noshade></td>
1803   </tr>
1804 </table>
1805
1806 <br>
1807 <form method=post action=am.pl>
1808
1809 <input name=callback type=hidden value="$form->{callback}">
1810
1811 <input type=hidden name=type value=buchungsgruppe>
1812
1813 <input class=submit type=submit name=action value="|
1814     . $locale->text('Add') . qq|">
1815
1816   </form>
1817
1818   </body>
1819   </html>
1820 |;
1821
1822   $main::lxdebug->leave_sub();
1823 }
1824
1825 sub buchungsgruppe_header {
1826   $main::lxdebug->enter_sub();
1827
1828   my $form     = $main::form;
1829   my $locale   = $main::locale;
1830
1831   $main::auth->assert('config');
1832
1833   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
1834
1835   # $locale->text('Add Accounting Group')
1836   # $locale->text('Edit Accounting Group')
1837
1838   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
1839   my %acc_type_map = (
1840     "IC" => $acc_inventory,
1841     "IC_income" => $acc_income,
1842     "IC_sale" => $acc_income,
1843     "IC_expense" => $acc_expense,
1844     "IC_cogs" => $acc_expense,
1845     );
1846
1847   foreach my $key (keys(%acc_type_map)) {
1848     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1849       $acc_type_map{$key}->{$ref->{"id"}} = $ref;
1850     }
1851   }
1852
1853   foreach my $type (qw(IC IC_income IC_expense)) {
1854     $form->{"select$type"} =
1855       join("",
1856            map({ "<option value=$_->{id} $_->{selected}>" .
1857                    "$_->{accno}--" . H($_->{description}) . "</option>" }
1858                sort({ $a->{"accno"} cmp $b->{"accno"} }
1859                     values(%{$acc_type_map{$type}}))));
1860   }
1861
1862   if ($form->{id}) {
1863     $form->{selectIC} =~ s/selected//g;
1864     $form->{selectIC} =~ s/ value=\Q$form->{inventory_accno_id}\E/  value=$form->{inventory_accno_id} selected/;
1865     $form->{selectIC_income} =~ s/selected//g;
1866     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_0}\E/  value=$form->{income_accno_id_0} selected/;
1867     $form->{selectIC_expense} =~ s/selected//g;
1868     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_0}\E/  value=$form->{expense_accno_id_0} selected/;
1869   }
1870
1871   my $linkaccounts;
1872   if (!$main::eur) {
1873     $linkaccounts = qq|
1874                <tr>
1875                 <th align=right>| . $locale->text('Inventory') . qq|</th>
1876                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
1877                 <input name=selectIC type=hidden value="$form->{selectIC}">
1878               </tr>|;
1879   } else {
1880     $linkaccounts = qq|
1881                 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>|;
1882   }
1883
1884
1885   $linkaccounts .= qq|
1886               <tr>
1887                 <th align=right>| . $locale->text('National Revenues') . qq|</th>
1888                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
1889               </tr>
1890               <tr>
1891                 <th align=right>| . $locale->text('National Expenses') . qq|</th>
1892                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
1893               </tr>|;
1894   if ($form->{id}) {
1895     $form->{selectIC_income} =~ s/selected//g;
1896     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_1}\E/  value=$form->{income_accno_id_1} selected/;
1897     $form->{selectIC_expense} =~ s/selected//g;
1898     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_1}\E/  value=$form->{expense_accno_id_1} selected/;
1899   }
1900   $linkaccounts .= qq|              <tr>
1901                 <th align=right>| . $locale->text('Revenues EU with UStId') . qq|</th>
1902                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
1903               </tr>
1904               <tr>
1905                 <th align=right>| . $locale->text('Expenses EU with UStId') . qq|</th>
1906                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
1907               </tr>|;
1908
1909   if ($form->{id}) {
1910     $form->{selectIC_income} =~ s/selected//g;
1911     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_2}\E/  value=$form->{income_accno_id_2} selected/;
1912     $form->{selectIC_expense} =~ s/selected//g;
1913     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_2}\E/  value=$form->{expense_accno_id_2} selected/;
1914   }
1915
1916   $linkaccounts .= qq|              <tr>
1917                 <th align=right>| . $locale->text('Revenues EU without UStId') . qq|</th>
1918                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
1919               </tr>
1920               <tr>
1921                 <th align=right>| . $locale->text('Expenses EU without UStId') . qq|</th>
1922                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
1923               </tr>|;
1924
1925   if ($form->{id}) {
1926     $form->{selectIC_income} =~ s/selected//g;
1927     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_3}\E/  value=$form->{income_accno_id_3} selected/;
1928     $form->{selectIC_expense} =~ s/selected//g;
1929     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_3}\E/  value=$form->{expense_accno_id_3} selected/;
1930   }
1931
1932   $linkaccounts .= qq|              <tr>
1933                 <th align=right>| . $locale->text('Foreign Revenues') . qq|</th>
1934                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
1935               </tr>
1936               <tr>
1937                 <th align=right>| . $locale->text('Foreign Expenses') . qq|</th>
1938                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
1939               </tr>
1940 |;
1941
1942
1943   $form->header;
1944
1945   print qq|
1946 <body>
1947
1948 <form method=post action=am.pl>
1949
1950 <input type=hidden name=id value=$form->{id}>
1951 <input type=hidden name=type value=buchungsgruppe>
1952
1953 <table width=100%>
1954   <tr>
1955     <th class=listtop colspan=2>$form->{title}</th>
1956   </tr>
1957   <tr height="5"></tr>
1958   <tr>
1959     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
1960     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
1961   <tr>
1962   $linkaccounts
1963   <td colspan=2><hr size=3 noshade></td>
1964   </tr>
1965 </table>
1966 |;
1967
1968   $main::lxdebug->leave_sub();
1969 }
1970
1971 sub save_buchungsgruppe {
1972   $main::lxdebug->enter_sub();
1973
1974   my $form     = $main::form;
1975   my %myconfig = %main::myconfig;
1976   my $locale   = $main::locale;
1977
1978   $main::auth->assert('config');
1979
1980   $form->isblank("description", $locale->text('Description missing!'));
1981
1982   AM->save_buchungsgruppe(\%myconfig, \%$form);
1983   $form->redirect($locale->text('Accounting Group saved!'));
1984
1985   $main::lxdebug->leave_sub();
1986 }
1987
1988 sub delete_buchungsgruppe {
1989   $main::lxdebug->enter_sub();
1990
1991   my $form     = $main::form;
1992   my %myconfig = %main::myconfig;
1993   my $locale   = $main::locale;
1994
1995   $main::auth->assert('config');
1996
1997   AM->delete_buchungsgruppe(\%myconfig, \%$form);
1998   $form->redirect($locale->text('Accounting Group deleted!'));
1999
2000   $main::lxdebug->leave_sub();
2001 }
2002
2003 sub swap_buchungsgruppen {
2004   $main::lxdebug->enter_sub();
2005
2006   my $form     = $main::form;
2007   my %myconfig = %main::myconfig;
2008
2009   $main::auth->assert('config');
2010
2011   AM->swap_sortkeys(\%myconfig, $form, "buchungsgruppen");
2012   list_buchungsgruppe();
2013
2014   $main::lxdebug->leave_sub();
2015 }
2016
2017
2018 sub add_printer {
2019   $main::lxdebug->enter_sub();
2020
2021   my $form     = $main::form;
2022
2023   $main::auth->assert('config');
2024
2025   $form->{title} = "Add";
2026
2027   $form->{callback} = "am.pl?action=add_printer" unless $form->{callback};
2028
2029   &printer_header;
2030   &form_footer;
2031
2032   $main::lxdebug->leave_sub();
2033 }
2034
2035 sub edit_printer {
2036   $main::lxdebug->enter_sub();
2037
2038   my $form     = $main::form;
2039   my %myconfig = %main::myconfig;
2040
2041   $main::auth->assert('config');
2042
2043   $form->{title} = "Edit";
2044
2045   AM->get_printer(\%myconfig, \%$form);
2046
2047   &printer_header;
2048
2049   $form->{orphaned} = 1;
2050   &form_footer;
2051
2052   $main::lxdebug->leave_sub();
2053 }
2054
2055 sub list_printer {
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   AM->printer(\%myconfig, \%$form);
2065
2066   $form->{callback} = "am.pl?action=list_printer";
2067
2068   my $callback = $form->escape($form->{callback});
2069
2070   $form->{title} = $locale->text('Printer');
2071
2072   my @column_index = qw(printer_description printer_command template_code);
2073   my %column_header;
2074   $column_header{printer_description} =
2075       qq|<th class=listheading width=60%>|
2076     . $locale->text('Printer Description')
2077     . qq|</th>|;
2078   $column_header{printer_command} =
2079       qq|<th class=listheading width=10%>|
2080     . $locale->text('Printer Command')
2081     . qq|</th>|;
2082   $column_header{template_code} =
2083       qq|<th class=listheading>|
2084     . $locale->text('Template Code')
2085     . qq|</th>|;
2086
2087   $form->header;
2088
2089   print qq|
2090 <body>
2091
2092 <table width=100%>
2093   <tr>
2094     <th class=listtop>$form->{title}</th>
2095   </tr>
2096   <tr height="5"></tr>
2097   <tr>
2098     <td>
2099       <table width=100%>
2100         <tr class=listheading>
2101 |;
2102
2103   map { print "$column_header{$_}\n" } @column_index;
2104
2105   print qq|
2106         </tr>
2107 |;
2108
2109   my ($i, %column_data);
2110   foreach my $ref (@{ $form->{ALL} }) {
2111
2112     $i++;
2113     $i %= 2;
2114
2115     print qq|
2116         <tr valign=top class=listrow$i>
2117 |;
2118
2119
2120     $column_data{printer_description} = qq|<td><a href="am.pl?action=edit_printer&id=$ref->{id}&callback=$callback">$ref->{printer_description}</td>|;
2121     $column_data{printer_command}           = qq|<td align=right>$ref->{printer_command}</td>|;
2122     $column_data{template_code} =
2123       qq|<td align=right>$ref->{template_code}</td>|;
2124
2125     map { print "$column_data{$_}\n" } @column_index;
2126
2127     print qq|
2128         </tr>
2129 |;
2130   }
2131
2132   print qq|
2133       </table>
2134     </td>
2135   </tr>
2136   <tr>
2137   <td><hr size=3 noshade></td>
2138   </tr>
2139 </table>
2140
2141 <br>
2142 <form method=post action=am.pl>
2143
2144 <input name=callback type=hidden value="$form->{callback}">
2145
2146 <input type=hidden name=type value=printer>
2147
2148 <input class=submit type=submit name=action value="|
2149     . $locale->text('Add') . qq|">
2150
2151   </form>
2152
2153   </body>
2154   </html>
2155 |;
2156
2157   $main::lxdebug->leave_sub();
2158 }
2159
2160 sub printer_header {
2161   $main::lxdebug->enter_sub();
2162
2163   my $form     = $main::form;
2164   my $locale   = $main::locale;
2165
2166   $main::auth->assert('config');
2167
2168   $form->{title}    = $locale->text("$form->{title} Printer");
2169
2170   # $locale->text('Add Printer')
2171   # $locale->text('Edit Printer')
2172
2173   $form->{printer_description} =~ s/\"/&quot;/g;
2174   $form->{template_code} =~ s/\"/&quot;/g;
2175   $form->{printer_command} =~ s/\"/&quot;/g;
2176
2177
2178   $form->header;
2179
2180   print qq|
2181 <body>
2182
2183 <form method=post action=am.pl>
2184
2185 <input type=hidden name=id value=$form->{id}>
2186 <input type=hidden name=type value=printer>
2187
2188 <table width=100%>
2189   <tr>
2190     <th class=listtop colspan=2>$form->{title}</th>
2191   </tr>
2192   <tr height="5"></tr>
2193   <tr>
2194     <th align=right>| . $locale->text('Printer') . qq|</th>
2195     <td><input name=printer_description size=30 value="$form->{printer_description}"></td>
2196   <tr>
2197   <tr>
2198     <th align=right>| . $locale->text('Printer Command') . qq|</th>
2199     <td><input name=printer_command size=30 value="$form->{printer_command}"></td>
2200   </tr>
2201   <tr>
2202     <th align=right>| . $locale->text('Template Code') . qq|</th>
2203     <td><input name=template_code size=5 value="$form->{template_code}"></td>
2204   </tr>
2205   <td colspan=2><hr size=3 noshade></td>
2206   </tr>
2207 </table>
2208 |;
2209
2210   $main::lxdebug->leave_sub();
2211 }
2212
2213 sub save_printer {
2214   $main::lxdebug->enter_sub();
2215
2216   my $form     = $main::form;
2217   my %myconfig = %main::myconfig;
2218   my $locale   = $main::locale;
2219
2220   $main::auth->assert('config');
2221
2222   $form->isblank("printer_description", $locale->text('Description missing!'));
2223   $form->isblank("printer_command", $locale->text('Printer Command missing!'));
2224   AM->save_printer(\%myconfig, \%$form);
2225   $form->redirect($locale->text('Printer saved!'));
2226
2227   $main::lxdebug->leave_sub();
2228 }
2229
2230 sub delete_printer {
2231   $main::lxdebug->enter_sub();
2232
2233   my $form     = $main::form;
2234   my %myconfig = %main::myconfig;
2235   my $locale   = $main::locale;
2236
2237   $main::auth->assert('config');
2238
2239   AM->delete_printer(\%myconfig, \%$form);
2240   $form->redirect($locale->text('Printer deleted!'));
2241
2242   $main::lxdebug->leave_sub();
2243 }
2244
2245 sub add_payment {
2246   $main::lxdebug->enter_sub();
2247
2248   my $form     = $main::form;
2249   my %myconfig = %main::myconfig;
2250
2251   $main::auth->assert('config');
2252
2253   $form->{title} = "Add";
2254
2255   $form->{callback} = "am.pl?action=add_payment" unless $form->{callback};
2256
2257   $form->{terms_netto} = 0;
2258   $form->{terms_skonto} = 0;
2259   $form->{percent_skonto} = 0;
2260   my @languages = AM->language(\%myconfig, $form, 1);
2261   map({ $_->{"language"} = $_->{"description"};
2262         $_->{"language_id"} = $_->{"id"}; } @languages);
2263   $form->{"TRANSLATION"} = \@languages;
2264   &payment_header;
2265   &form_footer;
2266
2267   $main::lxdebug->leave_sub();
2268 }
2269
2270 sub edit_payment {
2271   $main::lxdebug->enter_sub();
2272
2273   my $form     = $main::form;
2274   my %myconfig = %main::myconfig;
2275
2276   $main::auth->assert('config');
2277
2278   $form->{title} = "Edit";
2279
2280   AM->get_payment(\%myconfig, $form);
2281   $form->{percent_skonto} =
2282     $form->format_amount(\%myconfig, $form->{percent_skonto} * 100);
2283
2284   &payment_header;
2285
2286   $form->{orphaned} = 1;
2287   &form_footer;
2288
2289   $main::lxdebug->leave_sub();
2290 }
2291
2292 sub list_payment {
2293   $main::lxdebug->enter_sub();
2294
2295   my $form     = $main::form;
2296   my %myconfig = %main::myconfig;
2297   my $locale   = $main::locale;
2298
2299   $main::auth->assert('config');
2300
2301   AM->payment(\%myconfig, \%$form);
2302
2303   $form->{callback} = build_std_url("action=list_payment");
2304
2305   my $callback = $form->escape($form->{callback});
2306
2307   $form->{title} = $locale->text('Payment Terms');
2308
2309   my @column_index = qw(up down description description_long terms_netto
2310                      terms_skonto percent_skonto);
2311   my %column_header;
2312   $column_header{up} =
2313       qq|<th class="listheading" align="center" valign="center" width="16">|
2314     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
2315     . qq|</th>|;
2316   $column_header{down} =
2317       qq|<th class="listheading" align="center" valign="center" width="16">|
2318     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
2319     . qq|</th>|;
2320   $column_header{description} =
2321       qq|<th class=listheading>|
2322     . $locale->text('Description')
2323     . qq|</th>|;
2324   $column_header{description_long} =
2325       qq|<th class=listheading>|
2326     . $locale->text('Long Description')
2327     . qq|</th>|;
2328   $column_header{terms_netto} =
2329       qq|<th class=listheading>|
2330     . $locale->text('Netto Terms')
2331     . qq|</th>|;
2332   $column_header{terms_skonto} =
2333       qq|<th class=listheading>|
2334     . $locale->text('Skonto Terms')
2335     . qq|</th>|;
2336   $column_header{percent_skonto} =
2337       qq|<th class=listheading>|
2338     . $locale->text('Skonto')
2339     . qq| %</th>|;
2340
2341   $form->header;
2342
2343   print qq|
2344 <body>
2345
2346 <table width=100%>
2347   <tr>
2348     <th class=listtop>$form->{title}</th>
2349   </tr>
2350   <tr height="5"></tr>
2351   <tr>
2352     <td>
2353       <table width=100%>
2354         <tr class=listheading>
2355 |;
2356
2357   map { print "$column_header{$_}\n" } @column_index;
2358
2359   print qq|
2360         </tr>
2361 |;
2362
2363   my $swap_link = build_std_url("action=swap_payment_terms");
2364
2365   my $row = 0;
2366   my ($i, %column_data);
2367   foreach my $ref (@{ $form->{ALL} }) {
2368
2369     $i++;
2370     $i %= 2;
2371
2372     print qq|
2373         <tr valign=top class=listrow$i>
2374 |;
2375
2376     if ($row) {
2377       my $pref = $form->{ALL}->[$row - 1];
2378       $column_data{up} =
2379         qq|<td align="center" valign="center" width="16">| .
2380         qq|<a href="${swap_link}&id1=$ref->{id}&id2=$pref->{id}">| .
2381         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
2382         qq|</a></td>|;
2383     } else {
2384       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
2385     }
2386
2387     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
2388       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
2389     } else {
2390       my $nref = $form->{ALL}->[$row + 1];
2391       $column_data{down} =
2392         qq|<td align="center" valign="center" width="16">| .
2393         qq|<a href="${swap_link}&id1=$ref->{id}&id2=$nref->{id}">| .
2394         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
2395         qq|</a></td>|;
2396     }
2397
2398     $column_data{description} =
2399       qq|<td><a href="| .
2400       build_std_url("action=edit_payment", "id=$ref->{id}", "callback=$callback") .
2401       qq|">| . H($ref->{description}) . qq|</a></td>|;
2402     $column_data{description_long} =
2403       qq|<td>| . H($ref->{description_long}) . qq|</td>|;
2404     $column_data{terms_netto} =
2405       qq|<td align=right>$ref->{terms_netto}</td>|;
2406     $column_data{terms_skonto} =
2407       qq|<td align=right>$ref->{terms_skonto}</td>|;
2408     $column_data{percent_skonto} =
2409       qq|<td align=right>| .
2410       $form->format_amount(\%myconfig, $ref->{percent_skonto} * 100) .
2411       qq|%</td>|;
2412     map { print "$column_data{$_}\n" } @column_index;
2413
2414     print qq|
2415        </tr>
2416 |;
2417     $row++;
2418   }
2419
2420   print qq|
2421       </table>
2422     </td>
2423   </tr>
2424   <tr>
2425   <td><hr size=3 noshade></td>
2426   </tr>
2427 </table>
2428
2429 <br>
2430 <form method=post action=am.pl>
2431
2432 <input name=callback type=hidden value="$form->{callback}">
2433
2434 <input type=hidden name=type value=payment>
2435
2436 <input class=submit type=submit name=action value="|
2437     . $locale->text('Add') . qq|">
2438
2439   </form>
2440
2441   </body>
2442   </html>
2443 |;
2444
2445   $main::lxdebug->leave_sub();
2446 }
2447
2448 sub payment_header {
2449   $main::lxdebug->enter_sub();
2450
2451   my $form     = $main::form;
2452   my $locale   = $main::locale;
2453
2454   $main::auth->assert('config');
2455
2456   $form->{title}    = $locale->text("$form->{title} Payment Terms");
2457
2458   # $locale->text('Add Payment Terms')
2459   # $locale->text('Edit Payment Terms')
2460
2461   $form->{description} =~ s/\"/&quot;/g;
2462
2463
2464
2465   $form->header;
2466
2467   print qq|
2468 <body>
2469
2470 <form method=post action=am.pl>
2471
2472 <input type=hidden name=id value=$form->{id}>
2473 <input type=hidden name=type value=payment>
2474
2475 <table width=100%>
2476   <tr>
2477     <th class=listtop colspan=2>$form->{title}</th>
2478   </tr>
2479   <tr height="5"></tr>
2480   <tr>
2481     <th align=right>| . $locale->text('Description') . qq|</th>
2482     <td><input name=description size=30 value="$form->{description}"></td>
2483   <tr>
2484   <tr>
2485     <th align=right>| . $locale->text('Long Description') . qq|</th>
2486     <td><input name=description_long size=50 value="$form->{description_long}"></td>
2487   </tr>
2488 |;
2489
2490   foreach my $language (@{ $form->{"TRANSLATION"} }) {
2491     print qq|
2492   <tr>
2493     <th align="right">| .
2494     sprintf($locale->text('Translation (%s)'),
2495             $language->{"language"})
2496     . qq|</th>
2497     <td><input name="description_long_$language->{language_id}" size="50"
2498          value="| . Q($language->{"description_long"}) . qq|"></td>
2499   </tr>
2500 |;
2501   }
2502
2503   print qq|
2504   <tr>
2505     <th align=right>| . $locale->text('Netto Terms') . qq|</th>
2506     <td><input name=terms_netto size=10 value="$form->{terms_netto}"></td>
2507   </tr>
2508   <tr>
2509     <th align=right>| . $locale->text('Skonto Terms') . qq|</th>
2510     <td><input name=terms_skonto size=10 value="$form->{terms_skonto}"></td>
2511   </tr>
2512   <tr>
2513     <th align=right>| . $locale->text('Skonto') . qq| %</th>
2514     <td><input name=percent_skonto size=10 value="$form->{percent_skonto}"></td>
2515   </tr>
2516   <td colspan=2><hr size=3 noshade></td>
2517   </tr>
2518 </table>
2519
2520 <p>| . $locale->text("You can use the following strings in the long " .
2521                      "description and all translations. They will be " .
2522                      "replaced by their actual values by Lx-Office " .
2523                      "before they're output.")
2524 . qq|</p>
2525
2526 <ul>
2527   <li>| . $locale->text("&lt;%netto_date%&gt; -- Date the payment is due in " .
2528                         "full")
2529 . qq|</li>
2530   <li>| . $locale->text("&lt;%skonto_date%&gt; -- Date the payment is due " .
2531                         "with discount")
2532 . qq|</li>
2533   <li>| . $locale->text("&lt;%skonto_amount%&gt; -- The deductible amount")
2534 . qq|</li>
2535   <li>| . $locale->text("&lt;%skonto_in_percent%&gt; -- The discount in percent")
2536 . qq|</li>
2537   <li>| . $locale->text("&lt;%total%&gt; -- Amount payable")
2538 . qq|</li>
2539   <li>| . $locale->text("&lt;%total_wo_skonto%&gt; -- Amount payable less discount")
2540 . qq|</li>
2541   <li>| . $locale->text("&lt;%invtotal%&gt; -- Invoice total")
2542 . qq|</li>
2543   <li>| . $locale->text("&lt;%invtotal_wo_skonto%&gt; -- Invoice total less discount")
2544 . qq|</li>
2545   <li>| . $locale->text("&lt;%currency%&gt; -- The selected currency")
2546 . qq|</li>
2547   <li>| . $locale->text("&lt;%terms_netto%&gt; -- The number of days for " .
2548                         "full payment")
2549 . qq|</li>
2550   <li>| . $locale->text("&lt;%account_number%&gt; -- Your account number")
2551 . qq|</li>
2552   <li>| . $locale->text("&lt;%bank%&gt; -- Your bank")
2553 . qq|</li>
2554   <li>| . $locale->text("&lt;%bank_code%&gt; -- Your bank code")
2555 . qq|</li>
2556 </ul>|;
2557
2558   $main::lxdebug->leave_sub();
2559 }
2560
2561 sub save_payment {
2562   $main::lxdebug->enter_sub();
2563
2564   my $form     = $main::form;
2565   my %myconfig = %main::myconfig;
2566   my $locale   = $main::locale;
2567
2568   $main::auth->assert('config');
2569
2570   $form->isblank("description", $locale->text('Description missing!'));
2571   $form->{"percent_skonto"} =
2572     $form->parse_amount(\%myconfig, $form->{percent_skonto}) / 100;
2573   AM->save_payment(\%myconfig, \%$form);
2574   $form->redirect($locale->text('Payment Terms saved!'));
2575
2576   $main::lxdebug->leave_sub();
2577 }
2578
2579 sub delete_payment {
2580   $main::lxdebug->enter_sub();
2581
2582   my $form     = $main::form;
2583   my %myconfig = %main::myconfig;
2584   my $locale   = $main::locale;
2585
2586   $main::auth->assert('config');
2587
2588   AM->delete_payment(\%myconfig, \%$form);
2589   $form->redirect($locale->text('Payment terms deleted!'));
2590
2591   $main::lxdebug->leave_sub();
2592 }
2593
2594 sub swap_payment_terms {
2595   $main::lxdebug->enter_sub();
2596
2597   my $form     = $main::form;
2598   my %myconfig = %main::myconfig;
2599
2600   $main::auth->assert('config');
2601
2602   AM->swap_sortkeys(\%myconfig, $form, "payment_terms");
2603   list_payment();
2604
2605   $main::lxdebug->leave_sub();
2606 }
2607
2608 sub edit_defaults {
2609   $main::lxdebug->enter_sub();
2610
2611   my $form     = $main::form;
2612   my %myconfig = %main::myconfig;
2613   my $locale   = $main::locale;
2614
2615   # get defaults for account numbers and last numbers
2616   AM->defaultaccounts(\%myconfig, \%$form);
2617   $form->{ALL_UNITS} = AM->convertible_units(AM->retrieve_all_units(), 'g');
2618
2619   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
2620
2621   foreach my $key (keys %{ $form->{IC} }) {
2622     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
2623       my $array = "ACCNOS_" . uc($key);
2624       $form->{$array} ||= [];
2625
2626       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
2627       push @{ $form->{$array} }, {
2628         'name'     => $value,
2629         'value'    => $value,
2630         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
2631       };
2632     }
2633   }
2634
2635   $form->{title} = $locale->text('Ranges of numbers and default accounts');
2636
2637   $form->header();
2638   print $form->parse_html_template('am/edit_defaults');
2639
2640   $main::lxdebug->leave_sub();
2641 }
2642
2643 sub save_defaults {
2644   $main::lxdebug->enter_sub();
2645
2646   my $form     = $main::form;
2647   my $locale   = $main::locale;
2648
2649   AM->save_defaults();
2650
2651   $form->redirect($locale->text('Defaults saved.'));
2652
2653   $main::lxdebug->leave_sub();
2654 }
2655
2656 sub _build_cfg_options {
2657   my $form     = $main::form;
2658   my %myconfig = %main::myconfig;
2659
2660   my $idx   = shift;
2661   my $array = uc($idx) . 'S';
2662
2663   $form->{$array} = [];
2664   foreach my $item (@_) {
2665     push @{ $form->{$array} }, {
2666       'name'     => $item,
2667       'value'    => $item,
2668       'selected' => $item eq $myconfig{$idx},
2669     };
2670   }
2671 }
2672
2673 sub config {
2674   $main::lxdebug->enter_sub();
2675
2676   my $form     = $main::form;
2677   my %myconfig = %main::myconfig;
2678   my $locale   = $main::locale;
2679
2680   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
2681   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
2682
2683   my @formats = ();
2684   if ($main::opendocument_templates && $main::openofficeorg_writer_bin &&
2685       $main::xvfb_bin && (-x $main::openofficeorg_writer_bin) && (-x $main::xvfb_bin)) {
2686     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
2687                      "value" => "opendocument_pdf" });
2688   }
2689   if ($main::latex_templates) {
2690     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
2691   }
2692   push(@formats, { "name" => "HTML", "value" => "html" });
2693   if ($main::latex_templates) {
2694     push(@formats, { "name" => $locale->text("Postscript"),
2695                      "value" => "postscript" });
2696   }
2697   if ($main::opendocument_templates) {
2698     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
2699                      "value" => "opendocument" });
2700   }
2701
2702   if (!$myconfig{"template_format"}) {
2703     $myconfig{"template_format"} = "pdf";
2704   }
2705   $form->{TEMPLATE_FORMATS} = [];
2706   foreach my $item (@formats) {
2707     push @{ $form->{TEMPLATE_FORMATS} }, {
2708       'name'     => $item->{name},
2709       'value'    => $item->{value},
2710       'selected' => $item->{value} eq $myconfig{template_format},
2711     };
2712   }
2713
2714   if (!$myconfig{"default_media"}) {
2715     $myconfig{"default_media"} = "screen";
2716   }
2717
2718   my %selected = ($myconfig{"default_media"} => "selected");
2719   $form->{MEDIA} = [
2720     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
2721     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
2722     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
2723     ];
2724
2725   AM->printer(\%myconfig, $form);
2726
2727   $form->{PRINTERS} = [];
2728   foreach my $printer (@{$form->{"ALL"}}) {
2729     push @{ $form->{PRINTERS} }, {
2730       'name'     => $printer->{printer_description},
2731       'value'    => $printer->{id},
2732       'selected' => $printer->{id} == $myconfig{default_printer_id},
2733     };
2734   }
2735
2736   my %countrycodes = User->country_codes;
2737
2738   $form->{COUNTRYCODES} = [];
2739   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
2740     push @{ $form->{COUNTRYCODES} }, {
2741       'name'     => $countrycodes{$countrycode},
2742       'value'    => $countrycode,
2743       'selected' => $countrycode eq $myconfig{countrycode},
2744     };
2745   }
2746
2747   $form->{STYLESHEETS} = [];
2748   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css)) {
2749     push @{ $form->{STYLESHEETS} }, {
2750       'name'     => $item,
2751       'value'    => $item,
2752       'selected' => $item eq $myconfig{stylesheet},
2753     };
2754   }
2755
2756   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
2757   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
2758   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
2759
2760   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
2761
2762   $form->header();
2763   print $form->parse_html_template('am/config');
2764
2765   $main::lxdebug->leave_sub();
2766 }
2767
2768 sub save_preferences {
2769   $main::lxdebug->enter_sub();
2770
2771   my $form     = $main::form;
2772   my %myconfig = %main::myconfig;
2773   my $locale   = $main::locale;
2774
2775   $form->{stylesheet} = $form->{usestylesheet};
2776
2777   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
2778
2779   $form->redirect($locale->text('Preferences saved!')) if (AM->save_preferences(\%myconfig, \%$form, 0));
2780   $form->error($locale->text('Cannot save preferences!'));
2781
2782   $main::lxdebug->leave_sub();
2783 }
2784
2785 sub audit_control {
2786   $main::lxdebug->enter_sub();
2787
2788   my $form     = $main::form;
2789   my %myconfig = %main::myconfig;
2790   my $locale   = $main::locale;
2791
2792   $main::auth->assert('config');
2793
2794   $form->{title} = $locale->text('Audit Control');
2795
2796   AM->closedto(\%myconfig, \%$form);
2797
2798   $form->header;
2799
2800   print qq|
2801 <body>
2802
2803 <form method=post action=am.pl>
2804
2805 <table width=100%>
2806   <tr><th class=listtop>$form->{title}</th></tr>
2807   <tr height="5"></tr>
2808   <tr>
2809     <td>
2810       <table>
2811         <tr>
2812           <th>| . $locale->text('Close Books up to') . qq|</th>
2813           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
2814         </tr>
2815       </table>
2816     </td>
2817   </tr>
2818 </table>
2819
2820 <hr size=3 noshade>
2821
2822 <br>
2823 <input type=hidden name=nextsub value=doclose>
2824
2825 <input type=submit class=submit name=action value="|
2826     . $locale->text('Continue') . qq|">
2827
2828 </form>
2829
2830 </body>
2831 </html>
2832 |;
2833
2834   $main::lxdebug->leave_sub();
2835 }
2836
2837 sub doclose {
2838   $main::lxdebug->enter_sub();
2839
2840   my $form     = $main::form;
2841   my %myconfig = %main::myconfig;
2842   my $locale   = $main::locale;
2843
2844   $main::auth->assert('config');
2845
2846   AM->closebooks(\%myconfig, \%$form);
2847
2848   if ($form->{closedto}) {
2849     $form->redirect(
2850                     $locale->text('Books closed up to') . " "
2851                       . $locale->date(\%myconfig, $form->{closedto}, 1));
2852   } else {
2853     $form->redirect($locale->text('Books are open'));
2854   }
2855
2856   $main::lxdebug->leave_sub();
2857 }
2858
2859 sub edit_units {
2860   $main::lxdebug->enter_sub();
2861
2862   my $form     = $main::form;
2863   my %myconfig = %main::myconfig;
2864   my $locale   = $main::locale;
2865
2866   $main::auth->assert('config');
2867
2868   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
2869   AM->units_in_use(\%myconfig, $form, $units);
2870   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
2871
2872   my @languages = AM->language(\%myconfig, $form, 1);
2873
2874   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
2875
2876   my $i = 1;
2877   foreach (@unit_list) {
2878     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
2879     $_->{"UNITLANGUAGES"} = [];
2880     foreach my $lang (@languages) {
2881       push(@{ $_->{"UNITLANGUAGES"} },
2882            { "idx" => $i,
2883              "unit" => $_->{"name"},
2884              "language_id" => $lang->{"id"},
2885              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
2886              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
2887            });
2888     }
2889     $i++;
2890   }
2891
2892   $units = AM->retrieve_units(\%myconfig, $form);
2893   my $ddbox = AM->unit_select_data($units, undef, 1);
2894
2895   my $updownlink = build_std_url("action=swap_units");
2896
2897   $form->{"title"} = $locale->text("Add and edit units");
2898   $form->header();
2899   print($form->parse_html_template("am/edit_units",
2900                                    { "UNITS"               => \@unit_list,
2901                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
2902                                      "LANGUAGES"           => \@languages,
2903                                      "updownlink"          => $updownlink }));
2904
2905   $main::lxdebug->leave_sub();
2906 }
2907
2908 sub add_unit {
2909   $main::lxdebug->enter_sub();
2910
2911   my $form     = $main::form;
2912   my %myconfig = %main::myconfig;
2913   my $locale   = $main::locale;
2914
2915   $main::auth->assert('config');
2916
2917   $form->isblank("new_name", $locale->text("The name is missing."));
2918   my $units = AM->retrieve_units(\%myconfig, $form);
2919   my $all_units = AM->retrieve_units(\%myconfig, $form);
2920   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
2921
2922   my ($base_unit, $factor);
2923   if ($form->{"new_base_unit"}) {
2924     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
2925
2926     $form->isblank("new_factor", $locale->text("The factor is missing."));
2927     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
2928     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
2929     $base_unit = $form->{"new_base_unit"};
2930   }
2931
2932   my @languages;
2933   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
2934     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
2935     push(@languages, { "id" => $lang->{"id"},
2936                        "localized" => $form->{"new_localized_$lang->{id}"},
2937                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
2938          });
2939   }
2940
2941   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
2942
2943   $form->{"saved_message"} = $locale->text("The unit has been saved.");
2944
2945   edit_units();
2946
2947   $main::lxdebug->leave_sub();
2948 }
2949
2950 sub set_unit_languages {
2951   $main::lxdebug->enter_sub();
2952
2953   my $form     = $main::form;
2954
2955   $main::auth->assert('config');
2956
2957   my ($unit, $languages, $idx) = @_;
2958
2959   $unit->{"LANGUAGES"} = [];
2960
2961   foreach my $lang (@{$languages}) {
2962     push(@{ $unit->{"LANGUAGES"} },
2963          { "id" => $lang->{"id"},
2964            "localized" => $form->{"localized_${idx}_$lang->{id}"},
2965            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
2966          });
2967   }
2968
2969   $main::lxdebug->leave_sub();
2970 }
2971
2972 sub save_unit {
2973   $main::lxdebug->enter_sub();
2974
2975   my $form     = $main::form;
2976   my %myconfig = %main::myconfig;
2977   my $locale   = $main::locale;
2978
2979   $main::auth->assert('config');
2980
2981   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
2982   AM->units_in_use(\%myconfig, $form, $old_units);
2983
2984   my @languages = AM->language(\%myconfig, $form, 1);
2985
2986   my $new_units = {};
2987   my @delete_units = ();
2988   foreach my $i (1..($form->{"rowcount"} * 1)) {
2989     my $old_unit = $old_units->{$form->{"old_name_$i"}};
2990     if (!$old_unit) {
2991       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
2992     }
2993
2994     if ($form->{"unchangeable_$i"}) {
2995       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
2996       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
2997       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
2998       next;
2999     }
3000
3001     if ($old_unit->{"in_use"}) {
3002       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
3003     }
3004
3005     if ($form->{"delete_$i"}) {
3006       push(@delete_units, $old_unit->{"name"});
3007       next;
3008     }
3009
3010     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
3011
3012     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
3013     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
3014     $new_units->{$form->{"name_$i"}} = \%h;
3015     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
3016     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
3017   }
3018
3019   foreach my $unit (values(%{$new_units})) {
3020     next unless ($unit->{"old_name"});
3021     if ($unit->{"base_unit"}) {
3022       $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"}))
3023         unless (defined($new_units->{$unit->{"base_unit"}}));
3024       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
3025       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
3026     } else {
3027       $unit->{"base_unit"} = undef;
3028       $unit->{"factor"} = undef;
3029     }
3030   }
3031
3032   foreach my $unit (values(%{$new_units})) {
3033     next if ($unit->{"unchanged_unit"});
3034
3035     map({ $_->{"seen"} = 0; } values(%{$new_units}));
3036     my $new_unit = $unit;
3037     while ($new_unit->{"base_unit"}) {
3038       $new_unit->{"seen"} = 1;
3039       $new_unit = $new_units->{$new_unit->{"base_unit"}};
3040       if ($new_unit->{"seen"}) {
3041         $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, " .
3042                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
3043       }
3044     }
3045   }
3046
3047   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
3048
3049   $form->{"saved_message"} = $locale->text("The units have been saved.");
3050
3051   edit_units();
3052
3053   $main::lxdebug->leave_sub();
3054 }
3055
3056 sub show_history_search {
3057   $main::lxdebug->enter_sub();
3058
3059   my $form     = $main::form;
3060   my $locale   = $main::locale;
3061
3062   $main::auth->assert('config');
3063
3064   $form->{title} = $locale->text("History Search");
3065   $form->header();
3066
3067   print $form->parse_html_template("common/search_history");
3068
3069   $main::lxdebug->leave_sub();
3070 }
3071
3072 sub show_am_history {
3073   $main::lxdebug->enter_sub();
3074
3075   my $form     = $main::form;
3076   my %myconfig = %main::myconfig;
3077   my $locale   = $main::locale;
3078
3079   $main::auth->assert('config');
3080
3081   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
3082   $form->{order} ||= 'h.itime--1';
3083
3084   my %search = ( "Artikelnummer"          => "parts",
3085                  "Kundennummer"           => "customer",
3086                  "Lieferantennummer"      => "vendor",
3087                  "Projektnummer"          => "project",
3088                  "Buchungsnummer"         => "oe",
3089                  "Eingangsrechnungnummer" => "ap",
3090                  "Ausgangsrechnungnummer" => "ar",
3091                  "Mahnungsnummer"         => "dunning"
3092     );
3093   my %searchNo = ( "Artikelnummer"          => "partnumber",
3094                    "Kundennummer"           => "customernumber",
3095                    "Lieferantennummer"      => "vendornumber",
3096                    "Projektnummer"          => "projectnummer",
3097                    "Buchungsnummer"         => "ordnumber",
3098                    "Eingangsrechnungnummer" => "invnumber",
3099                    "Ausgangsrechnungnummer" => "invnumber",
3100                    "Mahnungsnummer"         => "dunning_id"
3101     );
3102
3103   my $dbh = $form->dbconnect(\%myconfig);
3104
3105   my $restriction  = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
3106   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
3107   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
3108   if ($form->{mitarbeiter} =~ m/^\d+$/) {
3109     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
3110   } elsif ($form->{mitarbeiter}) {
3111     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
3112   }
3113
3114   my $query = qq|SELECT trans_id AS id FROM history_erp | .
3115     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
3116      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
3117
3118   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
3119   my $daten .= shift @ids;
3120   $daten    .= join '', map { " OR trans_id = $_" } @ids;
3121
3122   my ($sort, $sortby) = split(/\-\-/, $form->{order});
3123   $sort =~ s/.*\.(.*)$/$1/;
3124
3125   $form->{title} = $locale->text("History Search");
3126   $form->header();
3127
3128   print $form->parse_html_template("common/show_history",
3129                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
3130                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
3131                                      "NONEWWINDOW"    => 1,
3132                                      uc($sort)        => 1,
3133                                      uc($sort) . "BY" => $sortby,
3134                                      'callback'       => $callback,
3135                                    });
3136   $dbh->disconnect();
3137
3138   $main::lxdebug->leave_sub();
3139 }
3140
3141 sub swap_units {
3142   $main::lxdebug->enter_sub();
3143
3144   my $form     = $main::form;
3145   my %myconfig = %main::myconfig;
3146
3147   $main::auth->assert('config');
3148
3149   my $dir = $form->{"dir"} eq "down" ? "down" : "up";
3150   AM->swap_units(\%myconfig, $form, $dir, $form->{"name"});
3151
3152   edit_units();
3153
3154   $main::lxdebug->leave_sub();
3155 }
3156
3157 sub add_tax {
3158   $main::lxdebug->enter_sub();
3159
3160   my $form     = $main::form;
3161   my $locale   = $main::locale;
3162
3163   $main::auth->assert('config');
3164
3165   $form->{title} =  $locale->text('Add');
3166
3167   $form->{callback} ||= "am.pl?action=add_tax";
3168
3169   _get_taxaccount_selection();
3170
3171   $form->header();
3172
3173   my $parameters_ref = {
3174 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
3175   };
3176
3177   # Ausgabe des Templates
3178   print($form->parse_html_template('am/edit_tax', $parameters_ref));
3179
3180   $main::lxdebug->leave_sub();
3181 }
3182
3183 sub edit_tax {
3184   $main::lxdebug->enter_sub();
3185
3186   my $form     = $main::form;
3187   my %myconfig = %main::myconfig;
3188   my $locale   = $main::locale;
3189
3190   $main::auth->assert('config');
3191
3192   $form->{title} =  $locale->text('Edit');
3193
3194   AM->get_tax(\%myconfig, \%$form);
3195   _get_taxaccount_selection();
3196
3197   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
3198
3199   $form->header();
3200
3201   my $parameters_ref = {
3202   };
3203
3204   # Ausgabe des Templates
3205   print($form->parse_html_template('am/edit_tax', $parameters_ref));
3206
3207   $main::lxdebug->leave_sub();
3208 }
3209
3210 sub list_tax {
3211   $main::lxdebug->enter_sub();
3212
3213   my $form     = $main::form;
3214   my %myconfig = %main::myconfig;
3215   my $locale   = $main::locale;
3216
3217   $main::auth->assert('config');
3218
3219   AM->taxes(\%myconfig, \%$form);
3220
3221   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
3222
3223   $form->{callback} = build_std_url('action=list_tax');
3224
3225   $form->{title} = $locale->text('Tax-O-Matic');
3226
3227   $form->header();
3228
3229   my $parameters_ref = {
3230   };
3231
3232   # Ausgabe des Templates
3233   print($form->parse_html_template('am/list_tax', $parameters_ref));
3234
3235   $main::lxdebug->leave_sub();
3236 }
3237
3238 sub _get_taxaccount_selection{
3239   $main::lxdebug->enter_sub();
3240
3241   my $form     = $main::form;
3242   my %myconfig = %main::myconfig;
3243
3244   $main::auth->assert('config');
3245
3246   AM->get_tax_accounts(\%myconfig, \%$form);
3247
3248   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
3249
3250   $main::lxdebug->leave_sub();
3251 }
3252
3253 sub save_tax {
3254   $main::lxdebug->enter_sub();
3255
3256   my $form     = $main::form;
3257   my %myconfig = %main::myconfig;
3258   my $locale   = $main::locale;
3259
3260   $main::auth->assert('config');
3261
3262   $form->isblank("rate", $locale->text('Taxrate missing!'));
3263   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
3264   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
3265
3266   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
3267
3268   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
3269     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
3270   }
3271
3272   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
3273     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
3274   }
3275
3276   AM->save_tax(\%myconfig, \%$form);
3277   $form->redirect($locale->text('Tax saved!'));
3278
3279   $main::lxdebug->leave_sub();
3280 }
3281
3282 sub delete_tax {
3283   $main::lxdebug->enter_sub();
3284
3285   my $form     = $main::form;
3286   my %myconfig = %main::myconfig;
3287   my $locale   = $main::locale;
3288
3289   $main::auth->assert('config');
3290
3291   AM->delete_tax(\%myconfig, \%$form);
3292   $form->redirect($locale->text('Tax deleted!'));
3293
3294   $main::lxdebug->leave_sub();
3295 }
3296
3297 sub add_price_factor {
3298   $main::lxdebug->enter_sub();
3299
3300   my $form     = $main::form;
3301   my $locale   = $main::locale;
3302
3303   $main::auth->assert('config');
3304
3305   $form->{title}      = $locale->text('Add Price Factor');
3306   $form->{callback} ||= build_std_url('action=add_price_factor');
3307   $form->{fokus}      = 'description';
3308
3309   $form->header();
3310   print $form->parse_html_template('am/edit_price_factor');
3311
3312   $main::lxdebug->leave_sub();
3313 }
3314
3315 sub edit_price_factor {
3316   $main::lxdebug->enter_sub();
3317
3318   my $form     = $main::form;
3319   my %myconfig = %main::myconfig;
3320   my $locale   = $main::locale;
3321
3322   $main::auth->assert('config');
3323
3324   $form->{title}      = $locale->text('Edit Price Factor');
3325   $form->{callback} ||= build_std_url('action=add_price_factor');
3326   $form->{fokus}      = 'description';
3327
3328   AM->get_price_factor(\%myconfig, $form);
3329
3330   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
3331
3332   $form->header();
3333   print $form->parse_html_template('am/edit_price_factor');
3334
3335   $main::lxdebug->leave_sub();
3336 }
3337
3338 sub list_price_factors {
3339   $main::lxdebug->enter_sub();
3340
3341   my $form     = $main::form;
3342   my %myconfig = %main::myconfig;
3343   my $locale   = $main::locale;
3344
3345   $main::auth->assert('config');
3346
3347   AM->get_all_price_factors(\%myconfig, \%$form);
3348
3349   my $previous;
3350   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
3351     if ($previous) {
3352       $previous->{next_id}    = $current->{id};
3353       $current->{previous_id} = $previous->{id};
3354     }
3355
3356     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
3357
3358     $previous = $current;
3359   }
3360
3361   $form->{callback} = build_std_url('action=list_price_factors');
3362   $form->{title}    = $locale->text('Price Factors');
3363   $form->{url_base} = build_std_url('callback');
3364
3365   $form->header();
3366   print $form->parse_html_template('am/list_price_factors');
3367
3368   $main::lxdebug->leave_sub();
3369 }
3370
3371 sub save_price_factor {
3372   $main::lxdebug->enter_sub();
3373
3374   my $form     = $main::form;
3375   my %myconfig = %main::myconfig;
3376   my $locale   = $main::locale;
3377
3378   $main::auth->assert('config');
3379
3380   $form->isblank("description", $locale->text('Description missing!'));
3381   $form->isblank("factor", $locale->text('Factor missing!'));
3382
3383   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
3384
3385   AM->save_price_factor(\%myconfig, $form);
3386
3387   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
3388
3389   $form->redirect($locale->text('Price factor saved!'));
3390
3391   $main::lxdebug->leave_sub();
3392 }
3393
3394 sub delete_price_factor {
3395   $main::lxdebug->enter_sub();
3396
3397   my $form     = $main::form;
3398   my %myconfig = %main::myconfig;
3399   my $locale   = $main::locale;
3400
3401   $main::auth->assert('config');
3402
3403   AM->delete_price_factor(\%myconfig, \%$form);
3404
3405   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
3406
3407   $form->redirect($locale->text('Price factor deleted!'));
3408
3409   $main::lxdebug->leave_sub();
3410 }
3411
3412 sub swap_price_factors {
3413   $main::lxdebug->enter_sub();
3414
3415   my $form     = $main::form;
3416   my %myconfig = %main::myconfig;
3417
3418   $main::auth->assert('config');
3419
3420   AM->swap_sortkeys(\%myconfig, $form, 'price_factors');
3421   list_price_factors();
3422
3423   $main::lxdebug->leave_sub();
3424 }
3425
3426 sub add_warehouse {
3427   $main::lxdebug->enter_sub();
3428
3429   my $form     = $main::form;
3430   my $locale   = $main::locale;
3431
3432   $main::auth->assert('config');
3433
3434   $form->{title}      = $locale->text('Add Warehouse');
3435   $form->{callback} ||= build_std_url('action=add_warehouse');
3436   $form->{fokus}      = 'description';
3437
3438   $form->header();
3439   print $form->parse_html_template('am/edit_warehouse');
3440
3441   $main::lxdebug->leave_sub();
3442 }
3443
3444 sub edit_warehouse {
3445   $main::lxdebug->enter_sub();
3446
3447   my $form     = $main::form;
3448   my %myconfig = %main::myconfig;
3449   my $locale   = $main::locale;
3450
3451   $main::auth->assert('config');
3452
3453   AM->get_warehouse(\%myconfig, $form);
3454
3455   $form->get_lists('employees' => 'EMPLOYEES');
3456
3457   $form->{title}      = $locale->text('Edit Warehouse');
3458   $form->{callback} ||= build_std_url('action=list_warehouses');
3459   $form->{fokus}      = 'description';
3460
3461   $form->header();
3462   print $form->parse_html_template('am/edit_warehouse');
3463
3464   $main::lxdebug->leave_sub();
3465 }
3466
3467 sub list_warehouses {
3468   $main::lxdebug->enter_sub();
3469
3470   my $form     = $main::form;
3471   my %myconfig = %main::myconfig;
3472   my $locale   = $main::locale;
3473
3474   $main::auth->assert('config');
3475
3476   AM->get_all_warehouses(\%myconfig, $form);
3477
3478   my $previous;
3479   foreach my $current (@{ $form->{WAREHOUSES} }) {
3480     if ($previous) {
3481       $previous->{next_id}    = $current->{id};
3482       $current->{previous_id} = $previous->{id};
3483     }
3484
3485     $previous = $current;
3486   }
3487
3488   $form->{callback} = build_std_url('action=list_warehouses');
3489   $form->{title}    = $locale->text('Warehouses');
3490   $form->{url_base} = build_std_url('callback');
3491
3492   $form->header();
3493   print $form->parse_html_template('am/list_warehouses');
3494
3495   $main::lxdebug->leave_sub();
3496 }
3497
3498 sub save_warehouse {
3499   $main::lxdebug->enter_sub();
3500
3501   my $form     = $main::form;
3502   my %myconfig = %main::myconfig;
3503   my $locale   = $main::locale;
3504
3505   $main::auth->assert('config');
3506
3507   $form->isblank("description", $locale->text('Description missing!'));
3508
3509   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
3510
3511   AM->save_warehouse(\%myconfig, $form);
3512
3513   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
3514
3515   $form->redirect($locale->text('Warehouse saved.'));
3516
3517   $main::lxdebug->leave_sub();
3518 }
3519
3520 sub swap_warehouses {
3521   $main::lxdebug->enter_sub();
3522
3523   my $form     = $main::form;
3524   my %myconfig = %main::myconfig;
3525
3526   $main::auth->assert('config');
3527
3528   AM->swap_sortkeys(\%myconfig, $form, 'warehouse');
3529   list_warehouses();
3530
3531   $main::lxdebug->leave_sub();
3532 }
3533
3534 sub delete_warehouse {
3535   $main::lxdebug->enter_sub();
3536
3537   my $form     = $main::form;
3538   my %myconfig = %main::myconfig;
3539   my $locale   = $main::locale;
3540
3541   $main::auth->assert('config');
3542
3543   if (!$form->{confirmed}) {
3544     $form->{title} = $locale->text('Confirmation');
3545
3546     $form->header();
3547     print $form->parse_html_template('am/confirm_delete_warehouse');
3548     exit 0;
3549   }
3550
3551   if (AM->delete_warehouse(\%myconfig, $form)) {
3552     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
3553     $form->redirect($locale->text('Warehouse deleted.'));
3554
3555   } else {
3556     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
3557   }
3558
3559   $main::lxdebug->leave_sub();
3560 }
3561
3562 sub save_bin {
3563   $main::lxdebug->enter_sub();
3564
3565   my $form     = $main::form;
3566   my %myconfig = %main::myconfig;
3567   my $locale   = $main::locale;
3568
3569   $main::auth->assert('config');
3570
3571   AM->save_bins(\%myconfig, $form);
3572
3573   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
3574
3575   $form->redirect($locale->text('Bins saved.'));
3576
3577   $main::lxdebug->leave_sub();
3578 }