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