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