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