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