Druckeradministration in die Adminumgebung verschoben.
[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 sub add_payment {
2018   $main::lxdebug->enter_sub();
2019
2020   my $form     = $main::form;
2021   my %myconfig = %main::myconfig;
2022
2023   $main::auth->assert('config');
2024
2025   $form->{title} = "Add";
2026
2027   $form->{callback} = "am.pl?action=add_payment" unless $form->{callback};
2028
2029   $form->{terms_netto} = 0;
2030   $form->{terms_skonto} = 0;
2031   $form->{percent_skonto} = 0;
2032   my @languages = AM->language(\%myconfig, $form, 1);
2033   map({ $_->{"language"} = $_->{"description"};
2034         $_->{"language_id"} = $_->{"id"}; } @languages);
2035   $form->{"TRANSLATION"} = \@languages;
2036   &payment_header;
2037   &form_footer;
2038
2039   $main::lxdebug->leave_sub();
2040 }
2041
2042 sub edit_payment {
2043   $main::lxdebug->enter_sub();
2044
2045   my $form     = $main::form;
2046   my %myconfig = %main::myconfig;
2047
2048   $main::auth->assert('config');
2049
2050   $form->{title} = "Edit";
2051
2052   AM->get_payment(\%myconfig, $form);
2053   $form->{percent_skonto} =
2054     $form->format_amount(\%myconfig, $form->{percent_skonto} * 100);
2055
2056   &payment_header;
2057
2058   $form->{orphaned} = 1;
2059   &form_footer;
2060
2061   $main::lxdebug->leave_sub();
2062 }
2063
2064 sub list_payment {
2065   $main::lxdebug->enter_sub();
2066
2067   my $form     = $main::form;
2068   my %myconfig = %main::myconfig;
2069   my $locale   = $main::locale;
2070
2071   $main::auth->assert('config');
2072
2073   AM->payment(\%myconfig, \%$form);
2074
2075   $form->{callback} = build_std_url("action=list_payment");
2076
2077   my $callback = $form->escape($form->{callback});
2078
2079   $form->{title} = $locale->text('Payment Terms');
2080
2081   my @column_index = qw(up down description description_long terms_netto
2082                      terms_skonto percent_skonto);
2083   my %column_header;
2084   $column_header{up} =
2085       qq|<th class="listheading" align="center" valign="center" width="16">|
2086     . qq|<img src="image/up.png" alt="| . $locale->text("up") . qq|">|
2087     . qq|</th>|;
2088   $column_header{down} =
2089       qq|<th class="listheading" align="center" valign="center" width="16">|
2090     . qq|<img src="image/down.png" alt="| . $locale->text("down") . qq|">|
2091     . qq|</th>|;
2092   $column_header{description} =
2093       qq|<th class=listheading>|
2094     . $locale->text('Description')
2095     . qq|</th>|;
2096   $column_header{description_long} =
2097       qq|<th class=listheading>|
2098     . $locale->text('Long Description')
2099     . qq|</th>|;
2100   $column_header{terms_netto} =
2101       qq|<th class=listheading>|
2102     . $locale->text('Netto Terms')
2103     . qq|</th>|;
2104   $column_header{terms_skonto} =
2105       qq|<th class=listheading>|
2106     . $locale->text('Skonto Terms')
2107     . qq|</th>|;
2108   $column_header{percent_skonto} =
2109       qq|<th class=listheading>|
2110     . $locale->text('Skonto')
2111     . qq| %</th>|;
2112
2113   $form->header;
2114
2115   print qq|
2116 <body>
2117
2118 <table width=100%>
2119   <tr>
2120     <th class=listtop>$form->{title}</th>
2121   </tr>
2122   <tr height="5"></tr>
2123   <tr>
2124     <td>
2125       <table width=100%>
2126         <tr class=listheading>
2127 |;
2128
2129   map { print "$column_header{$_}\n" } @column_index;
2130
2131   print qq|
2132         </tr>
2133 |;
2134
2135   my $swap_link = build_std_url("action=swap_payment_terms");
2136
2137   my $row = 0;
2138   my ($i, %column_data);
2139   foreach my $ref (@{ $form->{ALL} }) {
2140
2141     $i++;
2142     $i %= 2;
2143
2144     print qq|
2145         <tr valign=top class=listrow$i>
2146 |;
2147
2148     if ($row) {
2149       my $pref = $form->{ALL}->[$row - 1];
2150       $column_data{up} =
2151         qq|<td align="center" valign="center" width="16">| .
2152         qq|<a href="${swap_link}&id1=$ref->{id}&id2=$pref->{id}">| .
2153         qq|<img border="0" src="image/up.png" alt="| . $locale->text("up") . qq|">| .
2154         qq|</a></td>|;
2155     } else {
2156       $column_data{up} = qq|<td width="16">&nbsp;</td>|;
2157     }
2158
2159     if ($row == (scalar(@{ $form->{ALL} }) - 1)) {
2160       $column_data{down} = qq|<td width="16">&nbsp;</td>|;
2161     } else {
2162       my $nref = $form->{ALL}->[$row + 1];
2163       $column_data{down} =
2164         qq|<td align="center" valign="center" width="16">| .
2165         qq|<a href="${swap_link}&id1=$ref->{id}&id2=$nref->{id}">| .
2166         qq|<img border="0" src="image/down.png" alt="| . $locale->text("down") . qq|">| .
2167         qq|</a></td>|;
2168     }
2169
2170     $column_data{description} =
2171       qq|<td><a href="| .
2172       build_std_url("action=edit_payment", "id=$ref->{id}", "callback=$callback") .
2173       qq|">| . H($ref->{description}) . qq|</a></td>|;
2174     $column_data{description_long} =
2175       qq|<td>| . H($ref->{description_long}) . qq|</td>|;
2176     $column_data{terms_netto} =
2177       qq|<td align=right>$ref->{terms_netto}</td>|;
2178     $column_data{terms_skonto} =
2179       qq|<td align=right>$ref->{terms_skonto}</td>|;
2180     $column_data{percent_skonto} =
2181       qq|<td align=right>| .
2182       $form->format_amount(\%myconfig, $ref->{percent_skonto} * 100) .
2183       qq|%</td>|;
2184     map { print "$column_data{$_}\n" } @column_index;
2185
2186     print qq|
2187        </tr>
2188 |;
2189     $row++;
2190   }
2191
2192   print qq|
2193       </table>
2194     </td>
2195   </tr>
2196   <tr>
2197   <td><hr size=3 noshade></td>
2198   </tr>
2199 </table>
2200
2201 <br>
2202 <form method=post action=am.pl>
2203
2204 <input name=callback type=hidden value="$form->{callback}">
2205
2206 <input type=hidden name=type value=payment>
2207
2208 <input class=submit type=submit name=action value="|
2209     . $locale->text('Add') . qq|">
2210
2211   </form>
2212
2213   </body>
2214   </html>
2215 |;
2216
2217   $main::lxdebug->leave_sub();
2218 }
2219
2220 sub payment_header {
2221   $main::lxdebug->enter_sub();
2222
2223   my $form     = $main::form;
2224   my $locale   = $main::locale;
2225
2226   $main::auth->assert('config');
2227
2228   $form->{title}    = $locale->text("$form->{title} Payment Terms");
2229
2230   # $locale->text('Add Payment Terms')
2231   # $locale->text('Edit Payment Terms')
2232
2233   $form->{description} =~ s/\"/&quot;/g;
2234
2235
2236
2237   $form->header;
2238
2239   print qq|
2240 <body>
2241
2242 <form method=post action=am.pl>
2243
2244 <input type=hidden name=id value=$form->{id}>
2245 <input type=hidden name=type value=payment>
2246
2247 <table width=100%>
2248   <tr>
2249     <th class=listtop colspan=2>$form->{title}</th>
2250   </tr>
2251   <tr height="5"></tr>
2252   <tr>
2253     <th align=right>| . $locale->text('Description') . qq|</th>
2254     <td><input name=description size=30 value="$form->{description}"></td>
2255   <tr>
2256   <tr>
2257     <th align=right>| . $locale->text('Long Description') . qq|</th>
2258     <td><input name=description_long size=50 value="$form->{description_long}"></td>
2259   </tr>
2260 |;
2261
2262   foreach my $language (@{ $form->{"TRANSLATION"} }) {
2263     print qq|
2264   <tr>
2265     <th align="right">| .
2266     sprintf($locale->text('Translation (%s)'),
2267             $language->{"language"})
2268     . qq|</th>
2269     <td><input name="description_long_$language->{language_id}" size="50"
2270          value="| . Q($language->{"description_long"}) . qq|"></td>
2271   </tr>
2272 |;
2273   }
2274
2275   print qq|
2276   <tr>
2277     <th align=right>| . $locale->text('Netto Terms') . qq|</th>
2278     <td><input name=terms_netto size=10 value="$form->{terms_netto}"></td>
2279   </tr>
2280   <tr>
2281     <th align=right>| . $locale->text('Skonto Terms') . qq|</th>
2282     <td><input name=terms_skonto size=10 value="$form->{terms_skonto}"></td>
2283   </tr>
2284   <tr>
2285     <th align=right>| . $locale->text('Skonto') . qq| %</th>
2286     <td><input name=percent_skonto size=10 value="$form->{percent_skonto}"></td>
2287   </tr>
2288   <td colspan=2><hr size=3 noshade></td>
2289   </tr>
2290 </table>
2291
2292 <p>| . $locale->text("You can use the following strings in the long " .
2293                      "description and all translations. They will be " .
2294                      "replaced by their actual values by Lx-Office " .
2295                      "before they're output.")
2296 . qq|</p>
2297
2298 <ul>
2299   <li>| . $locale->text("&lt;%netto_date%&gt; -- Date the payment is due in " .
2300                         "full")
2301 . qq|</li>
2302   <li>| . $locale->text("&lt;%skonto_date%&gt; -- Date the payment is due " .
2303                         "with discount")
2304 . qq|</li>
2305   <li>| . $locale->text("&lt;%skonto_amount%&gt; -- The deductible amount")
2306 . qq|</li>
2307   <li>| . $locale->text("&lt;%skonto_in_percent%&gt; -- The discount in percent")
2308 . qq|</li>
2309   <li>| . $locale->text("&lt;%total%&gt; -- Amount payable")
2310 . qq|</li>
2311   <li>| . $locale->text("&lt;%total_wo_skonto%&gt; -- Amount payable less discount")
2312 . qq|</li>
2313   <li>| . $locale->text("&lt;%invtotal%&gt; -- Invoice total")
2314 . qq|</li>
2315   <li>| . $locale->text("&lt;%invtotal_wo_skonto%&gt; -- Invoice total less discount")
2316 . qq|</li>
2317   <li>| . $locale->text("&lt;%currency%&gt; -- The selected currency")
2318 . qq|</li>
2319   <li>| . $locale->text("&lt;%terms_netto%&gt; -- The number of days for " .
2320                         "full payment")
2321 . qq|</li>
2322   <li>| . $locale->text("&lt;%account_number%&gt; -- Your account number")
2323 . qq|</li>
2324   <li>| . $locale->text("&lt;%bank%&gt; -- Your bank")
2325 . qq|</li>
2326   <li>| . $locale->text("&lt;%bank_code%&gt; -- Your bank code")
2327 . qq|</li>
2328 </ul>|;
2329
2330   $main::lxdebug->leave_sub();
2331 }
2332
2333 sub save_payment {
2334   $main::lxdebug->enter_sub();
2335
2336   my $form     = $main::form;
2337   my %myconfig = %main::myconfig;
2338   my $locale   = $main::locale;
2339
2340   $main::auth->assert('config');
2341
2342   $form->isblank("description", $locale->text('Description missing!'));
2343   $form->{"percent_skonto"} =
2344     $form->parse_amount(\%myconfig, $form->{percent_skonto}) / 100;
2345   AM->save_payment(\%myconfig, \%$form);
2346   $form->redirect($locale->text('Payment Terms saved!'));
2347
2348   $main::lxdebug->leave_sub();
2349 }
2350
2351 sub delete_payment {
2352   $main::lxdebug->enter_sub();
2353
2354   my $form     = $main::form;
2355   my %myconfig = %main::myconfig;
2356   my $locale   = $main::locale;
2357
2358   $main::auth->assert('config');
2359
2360   AM->delete_payment(\%myconfig, \%$form);
2361   $form->redirect($locale->text('Payment terms deleted!'));
2362
2363   $main::lxdebug->leave_sub();
2364 }
2365
2366 sub swap_payment_terms {
2367   $main::lxdebug->enter_sub();
2368
2369   my $form     = $main::form;
2370   my %myconfig = %main::myconfig;
2371
2372   $main::auth->assert('config');
2373
2374   AM->swap_sortkeys(\%myconfig, $form, "payment_terms");
2375   list_payment();
2376
2377   $main::lxdebug->leave_sub();
2378 }
2379
2380 sub edit_defaults {
2381   $main::lxdebug->enter_sub();
2382
2383   my $form     = $main::form;
2384   my %myconfig = %main::myconfig;
2385   my $locale   = $main::locale;
2386
2387   # get defaults for account numbers and last numbers
2388   AM->defaultaccounts(\%myconfig, \%$form);
2389   $form->{ALL_UNITS} = AM->convertible_units(AM->retrieve_all_units(), 'g');
2390
2391   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
2392
2393   foreach my $key (keys %{ $form->{IC} }) {
2394     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
2395       my $array = "ACCNOS_" . uc($key);
2396       $form->{$array} ||= [];
2397
2398       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
2399       push @{ $form->{$array} }, {
2400         'name'     => $value,
2401         'value'    => $value,
2402         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
2403       };
2404     }
2405   }
2406
2407   $form->{title} = $locale->text('Ranges of numbers and default accounts');
2408
2409   $form->header();
2410   print $form->parse_html_template('am/edit_defaults');
2411
2412   $main::lxdebug->leave_sub();
2413 }
2414
2415 sub save_defaults {
2416   $main::lxdebug->enter_sub();
2417
2418   my $form     = $main::form;
2419   my $locale   = $main::locale;
2420
2421   AM->save_defaults();
2422
2423   $form->redirect($locale->text('Defaults saved.'));
2424
2425   $main::lxdebug->leave_sub();
2426 }
2427
2428 sub _build_cfg_options {
2429   my $form     = $main::form;
2430   my %myconfig = %main::myconfig;
2431
2432   my $idx   = shift;
2433   my $array = uc($idx) . 'S';
2434
2435   $form->{$array} = [];
2436   foreach my $item (@_) {
2437     push @{ $form->{$array} }, {
2438       'name'     => $item,
2439       'value'    => $item,
2440       'selected' => $item eq $myconfig{$idx},
2441     };
2442   }
2443 }
2444
2445 sub config {
2446   $main::lxdebug->enter_sub();
2447
2448   my $form     = $main::form;
2449   my %myconfig = %main::myconfig;
2450   my $locale   = $main::locale;
2451
2452   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
2453   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
2454
2455   my @formats = ();
2456   if ($main::opendocument_templates && $main::openofficeorg_writer_bin &&
2457       $main::xvfb_bin && (-x $main::openofficeorg_writer_bin) && (-x $main::xvfb_bin)) {
2458     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
2459                      "value" => "opendocument_pdf" });
2460   }
2461   if ($main::latex_templates) {
2462     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
2463   }
2464   push(@formats, { "name" => "HTML", "value" => "html" });
2465   if ($main::latex_templates) {
2466     push(@formats, { "name" => $locale->text("Postscript"),
2467                      "value" => "postscript" });
2468   }
2469   if ($main::opendocument_templates) {
2470     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
2471                      "value" => "opendocument" });
2472   }
2473
2474   if (!$myconfig{"template_format"}) {
2475     $myconfig{"template_format"} = "pdf";
2476   }
2477   $form->{TEMPLATE_FORMATS} = [];
2478   foreach my $item (@formats) {
2479     push @{ $form->{TEMPLATE_FORMATS} }, {
2480       'name'     => $item->{name},
2481       'value'    => $item->{value},
2482       'selected' => $item->{value} eq $myconfig{template_format},
2483     };
2484   }
2485
2486   if (!$myconfig{"default_media"}) {
2487     $myconfig{"default_media"} = "screen";
2488   }
2489
2490   my %selected = ($myconfig{"default_media"} => "selected");
2491   $form->{MEDIA} = [
2492     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
2493     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
2494     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
2495     ];
2496
2497   $form->{PRINTERS} = [];
2498   foreach my $printer (SL::Printer->all_printers(%::myconfig)) {
2499     push @{ $form->{PRINTERS} }, {
2500       'name'     => $printer->{printer_description},
2501       'value'    => $printer->{id},
2502       'selected' => $printer->{id} == $myconfig{default_printer_id},
2503     };
2504   }
2505
2506   my %countrycodes = User->country_codes;
2507
2508   $form->{COUNTRYCODES} = [];
2509   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
2510     push @{ $form->{COUNTRYCODES} }, {
2511       'name'     => $countrycodes{$countrycode},
2512       'value'    => $countrycode,
2513       'selected' => $countrycode eq $myconfig{countrycode},
2514     };
2515   }
2516
2517   $form->{STYLESHEETS} = [];
2518   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css)) {
2519     push @{ $form->{STYLESHEETS} }, {
2520       'name'     => $item,
2521       'value'    => $item,
2522       'selected' => $item eq $myconfig{stylesheet},
2523     };
2524   }
2525
2526   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
2527   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
2528   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
2529
2530   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
2531
2532   $form->header();
2533   print $form->parse_html_template('am/config');
2534
2535   $main::lxdebug->leave_sub();
2536 }
2537
2538 sub save_preferences {
2539   $main::lxdebug->enter_sub();
2540
2541   my $form     = $main::form;
2542   my %myconfig = %main::myconfig;
2543   my $locale   = $main::locale;
2544
2545   $form->{stylesheet} = $form->{usestylesheet};
2546
2547   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
2548
2549   $form->redirect($locale->text('Preferences saved!')) if (AM->save_preferences(\%myconfig, \%$form, 0));
2550   $form->error($locale->text('Cannot save preferences!'));
2551
2552   $main::lxdebug->leave_sub();
2553 }
2554
2555 sub audit_control {
2556   $main::lxdebug->enter_sub();
2557
2558   my $form     = $main::form;
2559   my %myconfig = %main::myconfig;
2560   my $locale   = $main::locale;
2561
2562   $main::auth->assert('config');
2563
2564   $form->{title} = $locale->text('Audit Control');
2565
2566   AM->closedto(\%myconfig, \%$form);
2567
2568   $form->header;
2569
2570   print qq|
2571 <body>
2572
2573 <form method=post action=am.pl>
2574
2575 <table width=100%>
2576   <tr><th class=listtop>$form->{title}</th></tr>
2577   <tr height="5"></tr>
2578   <tr>
2579     <td>
2580       <table>
2581         <tr>
2582           <th>| . $locale->text('Close Books up to') . qq|</th>
2583           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
2584         </tr>
2585       </table>
2586     </td>
2587   </tr>
2588 </table>
2589
2590 <hr size=3 noshade>
2591
2592 <br>
2593 <input type=hidden name=nextsub value=doclose>
2594
2595 <input type=submit class=submit name=action value="|
2596     . $locale->text('Continue') . qq|">
2597
2598 </form>
2599
2600 </body>
2601 </html>
2602 |;
2603
2604   $main::lxdebug->leave_sub();
2605 }
2606
2607 sub doclose {
2608   $main::lxdebug->enter_sub();
2609
2610   my $form     = $main::form;
2611   my %myconfig = %main::myconfig;
2612   my $locale   = $main::locale;
2613
2614   $main::auth->assert('config');
2615
2616   AM->closebooks(\%myconfig, \%$form);
2617
2618   if ($form->{closedto}) {
2619     $form->redirect(
2620                     $locale->text('Books closed up to') . " "
2621                       . $locale->date(\%myconfig, $form->{closedto}, 1));
2622   } else {
2623     $form->redirect($locale->text('Books are open'));
2624   }
2625
2626   $main::lxdebug->leave_sub();
2627 }
2628
2629 sub edit_units {
2630   $main::lxdebug->enter_sub();
2631
2632   my $form     = $main::form;
2633   my %myconfig = %main::myconfig;
2634   my $locale   = $main::locale;
2635
2636   $main::auth->assert('config');
2637
2638   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
2639   AM->units_in_use(\%myconfig, $form, $units);
2640   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
2641
2642   my @languages = AM->language(\%myconfig, $form, 1);
2643
2644   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
2645
2646   my $i = 1;
2647   foreach (@unit_list) {
2648     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
2649     $_->{"UNITLANGUAGES"} = [];
2650     foreach my $lang (@languages) {
2651       push(@{ $_->{"UNITLANGUAGES"} },
2652            { "idx" => $i,
2653              "unit" => $_->{"name"},
2654              "language_id" => $lang->{"id"},
2655              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
2656              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
2657            });
2658     }
2659     $i++;
2660   }
2661
2662   $units = AM->retrieve_units(\%myconfig, $form);
2663   my $ddbox = AM->unit_select_data($units, undef, 1);
2664
2665   my $updownlink = build_std_url("action=swap_units");
2666
2667   $form->{"title"} = $locale->text("Add and edit units");
2668   $form->header();
2669   print($form->parse_html_template("am/edit_units",
2670                                    { "UNITS"               => \@unit_list,
2671                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
2672                                      "LANGUAGES"           => \@languages,
2673                                      "updownlink"          => $updownlink }));
2674
2675   $main::lxdebug->leave_sub();
2676 }
2677
2678 sub add_unit {
2679   $main::lxdebug->enter_sub();
2680
2681   my $form     = $main::form;
2682   my %myconfig = %main::myconfig;
2683   my $locale   = $main::locale;
2684
2685   $main::auth->assert('config');
2686
2687   $form->isblank("new_name", $locale->text("The name is missing."));
2688   my $units = AM->retrieve_units(\%myconfig, $form);
2689   my $all_units = AM->retrieve_units(\%myconfig, $form);
2690   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
2691
2692   my ($base_unit, $factor);
2693   if ($form->{"new_base_unit"}) {
2694     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
2695
2696     $form->isblank("new_factor", $locale->text("The factor is missing."));
2697     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
2698     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
2699     $base_unit = $form->{"new_base_unit"};
2700   }
2701
2702   my @languages;
2703   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
2704     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
2705     push(@languages, { "id" => $lang->{"id"},
2706                        "localized" => $form->{"new_localized_$lang->{id}"},
2707                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
2708          });
2709   }
2710
2711   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
2712
2713   $form->{"saved_message"} = $locale->text("The unit has been saved.");
2714
2715   edit_units();
2716
2717   $main::lxdebug->leave_sub();
2718 }
2719
2720 sub set_unit_languages {
2721   $main::lxdebug->enter_sub();
2722
2723   my $form     = $main::form;
2724
2725   $main::auth->assert('config');
2726
2727   my ($unit, $languages, $idx) = @_;
2728
2729   $unit->{"LANGUAGES"} = [];
2730
2731   foreach my $lang (@{$languages}) {
2732     push(@{ $unit->{"LANGUAGES"} },
2733          { "id" => $lang->{"id"},
2734            "localized" => $form->{"localized_${idx}_$lang->{id}"},
2735            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
2736          });
2737   }
2738
2739   $main::lxdebug->leave_sub();
2740 }
2741
2742 sub save_unit {
2743   $main::lxdebug->enter_sub();
2744
2745   my $form     = $main::form;
2746   my %myconfig = %main::myconfig;
2747   my $locale   = $main::locale;
2748
2749   $main::auth->assert('config');
2750
2751   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
2752   AM->units_in_use(\%myconfig, $form, $old_units);
2753
2754   my @languages = AM->language(\%myconfig, $form, 1);
2755
2756   my $new_units = {};
2757   my @delete_units = ();
2758   foreach my $i (1..($form->{"rowcount"} * 1)) {
2759     my $old_unit = $old_units->{$form->{"old_name_$i"}};
2760     if (!$old_unit) {
2761       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
2762     }
2763
2764     if ($form->{"unchangeable_$i"}) {
2765       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
2766       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
2767       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
2768       next;
2769     }
2770
2771     if ($old_unit->{"in_use"}) {
2772       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
2773     }
2774
2775     if ($form->{"delete_$i"}) {
2776       push(@delete_units, $old_unit->{"name"});
2777       next;
2778     }
2779
2780     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
2781
2782     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
2783     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
2784     $new_units->{$form->{"name_$i"}} = \%h;
2785     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
2786     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
2787   }
2788
2789   foreach my $unit (values(%{$new_units})) {
2790     next unless ($unit->{"old_name"});
2791     if ($unit->{"base_unit"}) {
2792       $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"}))
2793         unless (defined($new_units->{$unit->{"base_unit"}}));
2794       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
2795       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
2796     } else {
2797       $unit->{"base_unit"} = undef;
2798       $unit->{"factor"} = undef;
2799     }
2800   }
2801
2802   foreach my $unit (values(%{$new_units})) {
2803     next if ($unit->{"unchanged_unit"});
2804
2805     map({ $_->{"seen"} = 0; } values(%{$new_units}));
2806     my $new_unit = $unit;
2807     while ($new_unit->{"base_unit"}) {
2808       $new_unit->{"seen"} = 1;
2809       $new_unit = $new_units->{$new_unit->{"base_unit"}};
2810       if ($new_unit->{"seen"}) {
2811         $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, " .
2812                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
2813       }
2814     }
2815   }
2816
2817   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
2818
2819   $form->{"saved_message"} = $locale->text("The units have been saved.");
2820
2821   edit_units();
2822
2823   $main::lxdebug->leave_sub();
2824 }
2825
2826 sub show_history_search {
2827   $main::lxdebug->enter_sub();
2828
2829   my $form     = $main::form;
2830   my $locale   = $main::locale;
2831
2832   $main::auth->assert('config');
2833
2834   $form->{title} = $locale->text("History Search");
2835   $form->header();
2836
2837   print $form->parse_html_template("common/search_history");
2838
2839   $main::lxdebug->leave_sub();
2840 }
2841
2842 sub show_am_history {
2843   $main::lxdebug->enter_sub();
2844
2845   my $form     = $main::form;
2846   my %myconfig = %main::myconfig;
2847   my $locale   = $main::locale;
2848
2849   $main::auth->assert('config');
2850
2851   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
2852   $form->{order} ||= 'h.itime--1';
2853
2854   my %search = ( "Artikelnummer"          => "parts",
2855                  "Kundennummer"           => "customer",
2856                  "Lieferantennummer"      => "vendor",
2857                  "Projektnummer"          => "project",
2858                  "Buchungsnummer"         => "oe",
2859                  "Eingangsrechnungnummer" => "ap",
2860                  "Ausgangsrechnungnummer" => "ar",
2861                  "Mahnungsnummer"         => "dunning"
2862     );
2863   my %searchNo = ( "Artikelnummer"          => "partnumber",
2864                    "Kundennummer"           => "customernumber",
2865                    "Lieferantennummer"      => "vendornumber",
2866                    "Projektnummer"          => "projectnummer",
2867                    "Buchungsnummer"         => "ordnumber",
2868                    "Eingangsrechnungnummer" => "invnumber",
2869                    "Ausgangsrechnungnummer" => "invnumber",
2870                    "Mahnungsnummer"         => "dunning_id"
2871     );
2872
2873   my $dbh = $form->dbconnect(\%myconfig);
2874
2875   my $restriction  = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
2876   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
2877   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
2878   if ($form->{mitarbeiter} =~ m/^\d+$/) {
2879     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
2880   } elsif ($form->{mitarbeiter}) {
2881     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
2882   }
2883
2884   my $query = qq|SELECT trans_id AS id FROM history_erp | .
2885     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
2886      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
2887
2888   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
2889   my $daten .= shift @ids;
2890   $daten    .= join '', map { " OR trans_id = $_" } @ids;
2891
2892   my ($sort, $sortby) = split(/\-\-/, $form->{order});
2893   $sort =~ s/.*\.(.*)$/$1/;
2894
2895   $form->{title} = $locale->text("History Search");
2896   $form->header();
2897
2898   print $form->parse_html_template("common/show_history",
2899                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
2900                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
2901                                      "NONEWWINDOW"    => 1,
2902                                      uc($sort)        => 1,
2903                                      uc($sort) . "BY" => $sortby,
2904                                      'callback'       => $callback,
2905                                    });
2906   $dbh->disconnect();
2907
2908   $main::lxdebug->leave_sub();
2909 }
2910
2911 sub swap_units {
2912   $main::lxdebug->enter_sub();
2913
2914   my $form     = $main::form;
2915   my %myconfig = %main::myconfig;
2916
2917   $main::auth->assert('config');
2918
2919   my $dir = $form->{"dir"} eq "down" ? "down" : "up";
2920   AM->swap_units(\%myconfig, $form, $dir, $form->{"name"});
2921
2922   edit_units();
2923
2924   $main::lxdebug->leave_sub();
2925 }
2926
2927 sub add_tax {
2928   $main::lxdebug->enter_sub();
2929
2930   my $form     = $main::form;
2931   my $locale   = $main::locale;
2932
2933   $main::auth->assert('config');
2934
2935   $form->{title} =  $locale->text('Add');
2936
2937   $form->{callback} ||= "am.pl?action=add_tax";
2938
2939   _get_taxaccount_selection();
2940
2941   $form->header();
2942
2943   my $parameters_ref = {
2944 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
2945   };
2946
2947   # Ausgabe des Templates
2948   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2949
2950   $main::lxdebug->leave_sub();
2951 }
2952
2953 sub edit_tax {
2954   $main::lxdebug->enter_sub();
2955
2956   my $form     = $main::form;
2957   my %myconfig = %main::myconfig;
2958   my $locale   = $main::locale;
2959
2960   $main::auth->assert('config');
2961
2962   $form->{title} =  $locale->text('Edit');
2963
2964   AM->get_tax(\%myconfig, \%$form);
2965   _get_taxaccount_selection();
2966
2967   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
2968
2969   $form->header();
2970
2971   my $parameters_ref = {
2972   };
2973
2974   # Ausgabe des Templates
2975   print($form->parse_html_template('am/edit_tax', $parameters_ref));
2976
2977   $main::lxdebug->leave_sub();
2978 }
2979
2980 sub list_tax {
2981   $main::lxdebug->enter_sub();
2982
2983   my $form     = $main::form;
2984   my %myconfig = %main::myconfig;
2985   my $locale   = $main::locale;
2986
2987   $main::auth->assert('config');
2988
2989   AM->taxes(\%myconfig, \%$form);
2990
2991   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
2992
2993   $form->{callback} = build_std_url('action=list_tax');
2994
2995   $form->{title} = $locale->text('Tax-O-Matic');
2996
2997   $form->header();
2998
2999   my $parameters_ref = {
3000   };
3001
3002   # Ausgabe des Templates
3003   print($form->parse_html_template('am/list_tax', $parameters_ref));
3004
3005   $main::lxdebug->leave_sub();
3006 }
3007
3008 sub _get_taxaccount_selection{
3009   $main::lxdebug->enter_sub();
3010
3011   my $form     = $main::form;
3012   my %myconfig = %main::myconfig;
3013
3014   $main::auth->assert('config');
3015
3016   AM->get_tax_accounts(\%myconfig, \%$form);
3017
3018   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
3019
3020   $main::lxdebug->leave_sub();
3021 }
3022
3023 sub save_tax {
3024   $main::lxdebug->enter_sub();
3025
3026   my $form     = $main::form;
3027   my %myconfig = %main::myconfig;
3028   my $locale   = $main::locale;
3029
3030   $main::auth->assert('config');
3031
3032   $form->isblank("rate", $locale->text('Taxrate missing!'));
3033   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
3034   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
3035
3036   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
3037
3038   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
3039     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
3040   }
3041
3042   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
3043     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
3044   }
3045
3046   AM->save_tax(\%myconfig, \%$form);
3047   $form->redirect($locale->text('Tax saved!'));
3048
3049   $main::lxdebug->leave_sub();
3050 }
3051
3052 sub delete_tax {
3053   $main::lxdebug->enter_sub();
3054
3055   my $form     = $main::form;
3056   my %myconfig = %main::myconfig;
3057   my $locale   = $main::locale;
3058
3059   $main::auth->assert('config');
3060
3061   AM->delete_tax(\%myconfig, \%$form);
3062   $form->redirect($locale->text('Tax deleted!'));
3063
3064   $main::lxdebug->leave_sub();
3065 }
3066
3067 sub add_price_factor {
3068   $main::lxdebug->enter_sub();
3069
3070   my $form     = $main::form;
3071   my $locale   = $main::locale;
3072
3073   $main::auth->assert('config');
3074
3075   $form->{title}      = $locale->text('Add Price Factor');
3076   $form->{callback} ||= build_std_url('action=add_price_factor');
3077   $form->{fokus}      = 'description';
3078
3079   $form->header();
3080   print $form->parse_html_template('am/edit_price_factor');
3081
3082   $main::lxdebug->leave_sub();
3083 }
3084
3085 sub edit_price_factor {
3086   $main::lxdebug->enter_sub();
3087
3088   my $form     = $main::form;
3089   my %myconfig = %main::myconfig;
3090   my $locale   = $main::locale;
3091
3092   $main::auth->assert('config');
3093
3094   $form->{title}      = $locale->text('Edit Price Factor');
3095   $form->{callback} ||= build_std_url('action=add_price_factor');
3096   $form->{fokus}      = 'description';
3097
3098   AM->get_price_factor(\%myconfig, $form);
3099
3100   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
3101
3102   $form->header();
3103   print $form->parse_html_template('am/edit_price_factor');
3104
3105   $main::lxdebug->leave_sub();
3106 }
3107
3108 sub list_price_factors {
3109   $main::lxdebug->enter_sub();
3110
3111   my $form     = $main::form;
3112   my %myconfig = %main::myconfig;
3113   my $locale   = $main::locale;
3114
3115   $main::auth->assert('config');
3116
3117   AM->get_all_price_factors(\%myconfig, \%$form);
3118
3119   my $previous;
3120   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
3121     if ($previous) {
3122       $previous->{next_id}    = $current->{id};
3123       $current->{previous_id} = $previous->{id};
3124     }
3125
3126     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
3127
3128     $previous = $current;
3129   }
3130
3131   $form->{callback} = build_std_url('action=list_price_factors');
3132   $form->{title}    = $locale->text('Price Factors');
3133   $form->{url_base} = build_std_url('callback');
3134
3135   $form->header();
3136   print $form->parse_html_template('am/list_price_factors');
3137
3138   $main::lxdebug->leave_sub();
3139 }
3140
3141 sub save_price_factor {
3142   $main::lxdebug->enter_sub();
3143
3144   my $form     = $main::form;
3145   my %myconfig = %main::myconfig;
3146   my $locale   = $main::locale;
3147
3148   $main::auth->assert('config');
3149
3150   $form->isblank("description", $locale->text('Description missing!'));
3151   $form->isblank("factor", $locale->text('Factor missing!'));
3152
3153   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
3154
3155   AM->save_price_factor(\%myconfig, $form);
3156
3157   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
3158
3159   $form->redirect($locale->text('Price factor saved!'));
3160
3161   $main::lxdebug->leave_sub();
3162 }
3163
3164 sub delete_price_factor {
3165   $main::lxdebug->enter_sub();
3166
3167   my $form     = $main::form;
3168   my %myconfig = %main::myconfig;
3169   my $locale   = $main::locale;
3170
3171   $main::auth->assert('config');
3172
3173   AM->delete_price_factor(\%myconfig, \%$form);
3174
3175   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
3176
3177   $form->redirect($locale->text('Price factor deleted!'));
3178
3179   $main::lxdebug->leave_sub();
3180 }
3181
3182 sub swap_price_factors {
3183   $main::lxdebug->enter_sub();
3184
3185   my $form     = $main::form;
3186   my %myconfig = %main::myconfig;
3187
3188   $main::auth->assert('config');
3189
3190   AM->swap_sortkeys(\%myconfig, $form, 'price_factors');
3191   list_price_factors();
3192
3193   $main::lxdebug->leave_sub();
3194 }
3195
3196 sub add_warehouse {
3197   $main::lxdebug->enter_sub();
3198
3199   my $form     = $main::form;
3200   my $locale   = $main::locale;
3201
3202   $main::auth->assert('config');
3203
3204   $form->{title}      = $locale->text('Add Warehouse');
3205   $form->{callback} ||= build_std_url('action=add_warehouse');
3206   $form->{fokus}      = 'description';
3207
3208   $form->header();
3209   print $form->parse_html_template('am/edit_warehouse');
3210
3211   $main::lxdebug->leave_sub();
3212 }
3213
3214 sub edit_warehouse {
3215   $main::lxdebug->enter_sub();
3216
3217   my $form     = $main::form;
3218   my %myconfig = %main::myconfig;
3219   my $locale   = $main::locale;
3220
3221   $main::auth->assert('config');
3222
3223   AM->get_warehouse(\%myconfig, $form);
3224
3225   $form->get_lists('employees' => 'EMPLOYEES');
3226
3227   $form->{title}      = $locale->text('Edit Warehouse');
3228   $form->{callback} ||= build_std_url('action=list_warehouses');
3229   $form->{fokus}      = 'description';
3230
3231   $form->header();
3232   print $form->parse_html_template('am/edit_warehouse');
3233
3234   $main::lxdebug->leave_sub();
3235 }
3236
3237 sub list_warehouses {
3238   $main::lxdebug->enter_sub();
3239
3240   my $form     = $main::form;
3241   my %myconfig = %main::myconfig;
3242   my $locale   = $main::locale;
3243
3244   $main::auth->assert('config');
3245
3246   AM->get_all_warehouses(\%myconfig, $form);
3247
3248   my $previous;
3249   foreach my $current (@{ $form->{WAREHOUSES} }) {
3250     if ($previous) {
3251       $previous->{next_id}    = $current->{id};
3252       $current->{previous_id} = $previous->{id};
3253     }
3254
3255     $previous = $current;
3256   }
3257
3258   $form->{callback} = build_std_url('action=list_warehouses');
3259   $form->{title}    = $locale->text('Warehouses');
3260   $form->{url_base} = build_std_url('callback');
3261
3262   $form->header();
3263   print $form->parse_html_template('am/list_warehouses');
3264
3265   $main::lxdebug->leave_sub();
3266 }
3267
3268 sub save_warehouse {
3269   $main::lxdebug->enter_sub();
3270
3271   my $form     = $main::form;
3272   my %myconfig = %main::myconfig;
3273   my $locale   = $main::locale;
3274
3275   $main::auth->assert('config');
3276
3277   $form->isblank("description", $locale->text('Description missing!'));
3278
3279   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
3280
3281   AM->save_warehouse(\%myconfig, $form);
3282
3283   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
3284
3285   $form->redirect($locale->text('Warehouse saved.'));
3286
3287   $main::lxdebug->leave_sub();
3288 }
3289
3290 sub swap_warehouses {
3291   $main::lxdebug->enter_sub();
3292
3293   my $form     = $main::form;
3294   my %myconfig = %main::myconfig;
3295
3296   $main::auth->assert('config');
3297
3298   AM->swap_sortkeys(\%myconfig, $form, 'warehouse');
3299   list_warehouses();
3300
3301   $main::lxdebug->leave_sub();
3302 }
3303
3304 sub delete_warehouse {
3305   $main::lxdebug->enter_sub();
3306
3307   my $form     = $main::form;
3308   my %myconfig = %main::myconfig;
3309   my $locale   = $main::locale;
3310
3311   $main::auth->assert('config');
3312
3313   if (!$form->{confirmed}) {
3314     $form->{title} = $locale->text('Confirmation');
3315
3316     $form->header();
3317     print $form->parse_html_template('am/confirm_delete_warehouse');
3318     ::end_of_request();
3319   }
3320
3321   if (AM->delete_warehouse(\%myconfig, $form)) {
3322     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
3323     $form->redirect($locale->text('Warehouse deleted.'));
3324
3325   } else {
3326     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
3327   }
3328
3329   $main::lxdebug->leave_sub();
3330 }
3331
3332 sub save_bin {
3333   $main::lxdebug->enter_sub();
3334
3335   my $form     = $main::form;
3336   my %myconfig = %main::myconfig;
3337   my $locale   = $main::locale;
3338
3339   $main::auth->assert('config');
3340
3341   AM->save_bins(\%myconfig, $form);
3342
3343   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
3344
3345   $form->redirect($locale->text('Bins saved.'));
3346
3347   $main::lxdebug->leave_sub();
3348 }