Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / bin / mozilla / am.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # administration
31 #
32 #======================================================================
33
34 use utf8;
35
36 use SL::Auth;
37 use SL::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;
47
48 require "bin/mozilla/common.pl";
49
50 use strict;
51
52 1;
53
54 # end of main
55
56 sub add      { call_sub("add_$main::form->{type}"); }
57 sub delete   { call_sub("delete_$main::form->{type}"); }
58 sub save     { call_sub("save_$main::form->{type}"); }
59 sub edit     { call_sub("edit_$main::form->{type}"); }
60 sub continue { call_sub($main::form->{"nextsub"}); }
61 sub save_as_new { call_sub("save_as_new_$main::form->{type}"); }
62
63 sub add_account {
64   $main::lxdebug->enter_sub();
65
66   my $form     = $main::form;
67   my %myconfig = %main::myconfig;
68
69   $main::auth->assert('config');
70
71   $form->{title}     = "Add";
72   $form->{charttype} = "A";
73   AM->get_account(\%myconfig, \%$form);
74
75   $form->{callback} = "am.pl?action=list_account" unless $form->{callback};
76
77   &account_header;
78   &form_footer;
79
80   $main::lxdebug->leave_sub();
81 }
82
83 sub edit_account {
84   $main::lxdebug->enter_sub();
85
86   my $form     = $main::form;
87   my %myconfig = %main::myconfig;
88
89   $main::auth->assert('config');
90
91   $form->{title} = "Edit";
92   AM->get_account(\%myconfig, \%$form);
93
94   foreach my $item (split(/:/, $form->{link})) {
95     $form->{$item} = "checked";
96   }
97
98   &account_header;
99   &form_footer;
100
101   $main::lxdebug->leave_sub();
102 }
103
104 sub account_header {
105   $main::lxdebug->enter_sub();
106
107   my $form     = $main::form;
108   my %myconfig = %main::myconfig;
109   my $locale   = $main::locale;
110
111   $main::auth->assert('config');
112
113   if ( $form->{action} eq 'edit_account') {
114     $form->{account_exists} = '1';
115   }
116
117   $form->{title} = $locale->text("$form->{title} Account");
118
119   $form->{"$form->{charttype}_checked"} = "checked";
120   $form->{"$form->{category}_checked"}  = "checked";
121
122   $form->{select_tax} = "";
123
124   my @tax_report_pos = USTVA->report_variables({
125       myconfig   => \%myconfig,
126       form       => $form,
127       type       => '',
128       attribute  => 'position',
129       calc       => '',
130   });
131
132   if (@{ $form->{TAXKEY} }) {
133     foreach my $item (@{ $form->{TAXKEY} }) {
134       $item->{rate} = $item->{rate} * 100 . '%';
135     }
136
137     # Fill in empty row for new Taxkey
138     my $newtaxkey_ref = {
139       id             => '',
140       chart_id       => '',
141       accno          => '',
142       tax_id         => '',
143       taxdescription => '',
144       rate           => '',
145       taxkey_id      => '',
146       pos_ustva      => '',
147       startdate      => '',
148     };
149
150     push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
151
152     my $i = 0;
153     foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
154
155       # Fill in a runningnumber
156       $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
157
158       # Fill in the Taxkeys as select options
159       foreach my $item (@{ $form->{TAXKEY} }) {
160         if ($item->{id} == $taxkey_used->{tax_id}) {
161           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
162             qq|<option value="$item->{id}" selected="selected">|
163             . sprintf("%.2d", $item->{taxkey})
164             . qq|. $item->{taxdescription} ($item->{rate}) |
165             . $locale->text('Tax-o-matic Account')
166             . qq|: $item->{chart_accno}\n|;
167         }
168         else {
169           $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
170             qq|<option value="$item->{id}">|
171             . sprintf("%.2d", $item->{taxkey})
172             . qq|. $item->{taxdescription} ($item->{rate}) |
173             . $locale->text('Tax-o-matic Account')
174             . qq|: $item->{chart_accno}\n|;
175         }
176
177       }
178
179       # Fill in the USTVA Numbers as select options
180       foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
181         if ($item eq ''){
182           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
183         }
184         elsif ( $item eq $taxkey_used->{pos_ustva} ) {
185           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
186         }
187         else {
188           $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
189         }
190
191       }
192
193       $i++;
194     }
195   }
196
197   # Newaccount Folgekonto
198   if (@{ $form->{NEWACCOUNT} || [] }) {
199     if (!$form->{new_chart_valid}) {
200       $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
201     }
202     foreach my $item (@{ $form->{NEWACCOUNT} }) {
203       if ($item->{id} == $form->{new_chart_id}) {
204         $form->{selectnewaccount} .=
205           qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
206       } elsif (!$form->{new_chart_valid}) {
207         $form->{selectnewaccount} .=
208           qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
209       }
210
211     }
212   }
213
214   my $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
215   my %eur = (
216           1  => "Umsatzerlöse",
217           2  => "sonstige Erlöse",
218           3  => "Privatanteile",
219           4  => "Zinserträge",
220           5  => "Ausserordentliche Erträge",
221           6  => "Vereinnahmte Umsatzst.",
222           7  => "Umsatzsteuererstattungen",
223           8  => "Wareneingänge",
224           9  => "Löhne und Gehälter",
225           10 => "Gesetzl. sozialer Aufw.",
226           11 => "Mieten",
227           12 => "Gas, Strom, Wasser",
228           13 => "Instandhaltung",
229           14 => "Steuern, Versich., Beiträge",
230           15 => "Kfz-Steuern",
231           16 => "Kfz-Versicherungen",
232           17 => "Sonst. Fahrzeugkosten",
233           18 => "Werbe- und Reisekosten",
234           19 => "Instandhaltung u. Werkzeuge",
235           20 => "Fachzeitschriften, Bücher",
236           21 => "Miete für Einrichtungen",
237           22 => "Rechts- und Beratungskosten",
238           23 => "Bürobedarf, Porto, Telefon",
239           24 => "Sonstige Aufwendungen",
240           25 => "Abschreibungen auf Anlagever.",
241           26 => "Abschreibungen auf GWG",
242           27 => "Vorsteuer",
243           28 => "Umsatzsteuerzahlungen",
244           29 => "Zinsaufwand",
245           30 => "Ausserordentlicher Aufwand",
246           31 => "Betriebliche Steuern");
247   foreach my $item (sort({ $a <=> $b } keys(%eur))) {
248     my $text = H($::locale->{iconv_utf8}->convert($eur{$item}));
249     if ($item == $form->{pos_eur}) {
250       $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
251     } else {
252       $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
253     }
254
255   }
256
257   my $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
258
259   my %bwapos = (
260              1  => 'Umsatzerlöse',
261              2  => 'Best.Verdg.FE/UE',
262              3  => 'Aktiv.Eigenleistung',
263              4  => 'Mat./Wareneinkauf',
264              5  => 'So.betr.Erlöse',
265              10 => 'Personalkosten',
266              11 => 'Raumkosten',
267              12 => 'Betriebl.Steuern',
268              13 => 'Vers./Beiträge',
269              14 => 'Kfz.Kosten o.St.',
270              15 => 'Werbe-Reisek.',
271              16 => 'Kosten Warenabgabe',
272              17 => 'Abschreibungen',
273              18 => 'Rep./instandhlt.',
274              19 => 'Übrige Steuern',
275              20 => 'Sonst.Kosten',
276              30 => 'Zinsauwand',
277              31 => 'Sonst.neutr.Aufw.',
278              32 => 'Zinserträge',
279              33 => 'Sonst.neutr.Ertrag',
280              34 => 'Verr.kalk.Kosten',
281              35 => 'Steuern Eink.u.Ertr.');
282   foreach my $item (sort({ $a <=> $b } keys %bwapos)) {
283     my $text = H($::locale->{iconv_utf8}->convert($bwapos{$item}));
284     if ($item == $form->{pos_bwa}) {
285       $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
286     } else {
287       $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
288     }
289
290   }
291
292 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
293   my $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
294   foreach my $item ((1, 2, 3, 4)) {
295     if ($item == $form->{pos_bilanz}) {
296       $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
297     } else {
298       $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
299     }
300
301   }
302
303   # this is for our parser only! Do not remove.
304   # type=submit $locale->text('Add Account')
305   # type=submit $locale->text('Edit Account')
306
307   $form->{type} = "account";
308
309   # preselections category
310
311   my $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
312
313   my %category = (
314       'A'  => $locale->text('Asset'),
315       'L'  => $locale->text('Liability'),
316       'Q'  => $locale->text('Equity'),
317       'I'  => $locale->text('Revenue'),
318       'E'  => $locale->text('Expense'),
319       'C'  => $locale->text('Costs'),
320   );
321   foreach my $item ( sort({ $a <=> $b } keys %category) ) {
322     if ($item eq $form->{category}) {
323       $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
324     } else {
325       $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
326     }
327
328   }
329
330   # preselection chart type
331   my @all_charttypes = ({'name' => $locale->text('Account'), 'value' => 'A'},
332                         {'name' => $locale->text('Heading'), 'value' => 'H'},
333     );
334   my $selected_charttype = $form->{charttype};
335
336
337   # account where AR_tax or AP_tax is set are not orphaned if they are used as
338   # tax-o-matic account
339   if ( $form->{id} && $form->{orphaned} && ($form->{link} =~ m/(AP_tax|AR_tax)/) ) {
340     if (SL::DB::Manager::Tax->find_by(chart_id => $form->{id})) {
341       $form->{orphaned} = 0;
342     }
343   }
344
345   my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
346   my $AccountIsPosted = ($form->{orphaned} ) ? "":"1";
347
348   $form->header();
349
350   my $parameters_ref = {
351     ChartTypeIsAccount         => $ChartTypeIsAccount,
352     AccountIsPosted            => $AccountIsPosted,
353     select_category            => $select_category,
354     all_charttypes             => \@all_charttypes,
355     selected_charttype         => $selected_charttype,
356     select_bwa                 => $select_bwa,
357     select_bilanz              => $select_bilanz,
358     select_eur                 => $select_eur,
359   };
360
361   # Ausgabe des Templates
362   print($form->parse_html_template('am/edit_accounts', $parameters_ref));
363
364
365   $main::lxdebug->leave_sub();
366 }
367
368 sub form_footer {
369   $::lxdebug->enter_sub;
370   $::auth->assert('config');
371
372   print $::form->parse_html_template('am/form_footer', {
373     show_save        => !$::form->{id}
374                      || ($::form->{id} && $::form->{orphaned})
375                      || ($::form->{type} eq "account" && !$::form->{new_chart_valid}),
376     show_delete      => $::form->{id} && $::form->{orphaned},
377     show_save_as_new => $::form->{id} && $::form->{type} eq "account",
378   });
379
380   $::lxdebug->leave_sub;
381 }
382
383 sub save_account {
384   $main::lxdebug->enter_sub();
385
386   my $form     = $main::form;
387   my %myconfig = %main::myconfig;
388   my $locale   = $main::locale;
389
390   $main::auth->assert('config');
391
392   $form->isblank("accno",       $locale->text('Account Number missing!'));
393   $form->isblank("description", $locale->text('Account Description missing!'));
394
395   if ($form->{charttype} eq 'A'){
396     $form->isblank("category",  $locale->text('Account Type missing!'));
397   }
398
399   $form->redirect($locale->text('Account saved!'))
400     if (AM->save_account(\%myconfig, \%$form));
401   $form->error($locale->text('Cannot save account!'));
402
403   $main::lxdebug->leave_sub();
404 }
405
406 sub save_as_new_account {
407   $main::lxdebug->enter_sub();
408
409   my $form     = $main::form;
410   my %myconfig = %main::myconfig;
411   my $locale   = $main::locale;
412
413   $main::auth->assert('config');
414
415   $form->isblank("accno",       $locale->text('Account Number missing!'));
416   $form->isblank("description", $locale->text('Account Description missing!'));
417
418   if ($form->{charttype} eq 'A'){
419     $form->isblank("category",  $locale->text('Account Type missing!'));
420   }
421
422   for my $taxkey (0 .. 9) {
423     if ($form->{"taxkey_id_$taxkey"}) {
424       $form->{"taxkey_id_$taxkey"} = "NEW";
425     }
426   }
427
428   $form->{id} = 0;
429   $form->redirect($locale->text('Account saved!'))
430     if (AM->save_account(\%myconfig, \%$form));
431   $form->error($locale->text('Cannot save account!'));
432
433   $main::lxdebug->leave_sub();
434 }
435
436 sub list_account {
437   $main::lxdebug->enter_sub();
438
439   my $form     = $main::form;
440   my %myconfig = %main::myconfig;
441   my $locale   = $main::locale;
442
443   $main::auth->assert('config');
444
445   $form->{callback}     = build_std_url('action=list_account');
446   my $link_edit_account = build_std_url('action=edit_account', 'callback');
447
448   CA->all_accounts(\%myconfig, \%$form);
449
450   foreach my $ca (@{ $form->{CA} }) {
451
452     $ca->{debit}  = "";
453     $ca->{credit} = "";
454
455     if ($ca->{amount} > 0) {
456       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
457     }
458     if ($ca->{amount} < 0) {
459       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
460     }
461     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:'';
462     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
463   }
464
465   $::request->{layout}->use_stylesheet("list_accounts.css");
466   $form->{title}       = $locale->text('Chart of Accounts');
467
468   $form->header;
469
470
471   my $parameters_ref = {
472   #   hidden_variables                => $_hidden_variables_ref,
473   };
474
475   # Ausgabe des Templates
476   print($form->parse_html_template('am/list_accounts', $parameters_ref));
477
478   $main::lxdebug->leave_sub();
479
480 }
481
482
483 sub list_account_details {
484 # Ajax Funktion aus list_account_details
485   $main::lxdebug->enter_sub();
486
487   my $form     = $main::form;
488   my %myconfig = %main::myconfig;
489   my $locale   = $main::locale;
490
491   $main::auth->assert('config');
492
493   my $chart_id = $form->{args};
494
495   CA->all_accounts(\%myconfig, \%$form, $chart_id);
496
497   foreach my $ca (@{ $form->{CA} }) {
498
499     $ca->{debit}  = "&nbsp;";
500     $ca->{credit} = "&nbsp;";
501
502     if ($ca->{amount} > 0) {
503       $ca->{credit} =
504         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
505     }
506     if ($ca->{amount} < 0) {
507       $ca->{debit} =
508         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
509     }
510
511     my @links = split( q{:}, $ca->{link});
512
513     $ca->{link} = q{};
514
515     foreach my $link (@links){
516       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
517                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
518                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
519                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
520                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
521                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
522                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
523                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
524                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
525                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
526                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
527                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
528                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
529                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
530                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
531                : $locale->text('Unknown Link') . ': ' . $link;
532       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
533     }
534
535     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
536                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
537                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
538                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
539                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
540                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
541                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
542                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
543   }
544
545   $form->{title} = $locale->text('Chart of Accounts');
546
547   print $form->ajax_response_header, $form->parse_html_template('am/list_account_details');
548
549   $main::lxdebug->leave_sub();
550
551 }
552
553 sub delete_account {
554   $main::lxdebug->enter_sub();
555
556   my $form     = $main::form;
557   my %myconfig = %main::myconfig;
558   my $locale   = $main::locale;
559
560   $main::auth->assert('config');
561
562   $form->{title} = $locale->text('Delete Account');
563
564   foreach my $id (
565     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
566     ) {
567     if ($form->{id} == $form->{$id}) {
568       $form->error($locale->text('Cannot delete default account!'));
569     }
570   }
571
572   $form->redirect($locale->text('Account deleted!'))
573     if (AM->delete_account(\%myconfig, \%$form));
574   $form->error($locale->text('Cannot delete account!'));
575
576   $main::lxdebug->leave_sub();
577 }
578
579 sub add_lead {
580   $main::lxdebug->enter_sub();
581
582   my $form     = $main::form;
583
584   $main::auth->assert('config');
585
586   $form->{title} = "Add";
587
588   $form->{callback} = "am.pl?action=add_lead" unless $form->{callback};
589
590   &lead_header;
591   &form_footer;
592
593   $main::lxdebug->leave_sub();
594 }
595
596 sub edit_lead {
597   $main::lxdebug->enter_sub();
598
599   my $form     = $main::form;
600   my %myconfig = %main::myconfig;
601
602   $main::auth->assert('config');
603
604   $form->{title} = "Edit";
605
606   AM->get_lead(\%myconfig, \%$form);
607
608   &lead_header;
609
610   $form->{orphaned} = 1;
611   &form_footer;
612
613   $main::lxdebug->leave_sub();
614 }
615
616 sub list_lead {
617   $::lxdebug->enter_sub;
618   $::auth->assert('config');
619
620   AM->lead(\%::myconfig, $::form);
621
622   $::form->{callback} = "am.pl?action=list_lead";
623   $::form->{title}    = $::locale->text('Lead');
624
625   $::form->header;
626   print $::form->parse_html_template('am/lead_list');
627
628   $::lxdebug->leave_sub;
629 }
630
631 sub lead_header {
632   $::lxdebug->enter_sub;
633   $::auth->assert('config');
634
635   # $locale->text('Add Lead')
636   # $locale->text('Edit Lead')
637   $::form->{title} = $::locale->text("$::form->{title} Lead");
638
639   $::form->header;
640   print $::form->parse_html_template('am/lead_header');
641
642   $::lxdebug->leave_sub;
643 }
644
645 sub save_lead {
646   $main::lxdebug->enter_sub();
647
648   my $form     = $main::form;
649   my %myconfig = %main::myconfig;
650   my $locale   = $main::locale;
651
652   $main::auth->assert('config');
653
654   $form->isblank("description", $locale->text('Description missing!'));
655   AM->save_lead(\%myconfig, \%$form);
656   $form->redirect($locale->text('lead saved!'));
657
658   $main::lxdebug->leave_sub();
659 }
660
661 sub delete_lead {
662   $main::lxdebug->enter_sub();
663
664   my $form     = $main::form;
665   my %myconfig = %main::myconfig;
666   my $locale   = $main::locale;
667
668   $main::auth->assert('config');
669
670   AM->delete_lead(\%myconfig, \%$form);
671   $form->redirect($locale->text('lead deleted!'));
672
673   $main::lxdebug->leave_sub();
674 }
675
676 sub add_language {
677   $main::lxdebug->enter_sub();
678
679   my $form     = $main::form;
680
681   $main::auth->assert('config');
682
683   $form->{title} = "Add";
684
685   $form->{callback} = "am.pl?action=add_language" unless $form->{callback};
686
687   &language_header;
688   &form_footer;
689
690   $main::lxdebug->leave_sub();
691 }
692
693 sub edit_language {
694   $main::lxdebug->enter_sub();
695
696   my $form     = $main::form;
697   my %myconfig = %main::myconfig;
698
699   $main::auth->assert('config');
700
701   $form->{title} = "Edit";
702
703   AM->get_language(\%myconfig, \%$form);
704
705   &language_header;
706
707   $form->{orphaned} = 1;
708   &form_footer;
709
710   $main::lxdebug->leave_sub();
711 }
712
713 sub list_language {
714   $::lxdebug->enter_sub;
715   $::auth->assert('config');
716
717   AM->language(\%::myconfig, $::form);
718
719   $::form->{callback} = "am.pl?action=list_language";
720   $::form->{title}   = $::locale->text('Languages');
721
722   $::form->header;
723
724   print $::form->parse_html_template('am/language_list');
725
726   $::lxdebug->leave_sub;
727 }
728
729 sub language_header {
730   $::lxdebug->enter_sub;
731   $::auth->assert('config');
732
733   # $locale->text('Add Language')
734   # $locale->text('Edit Language')
735   $::form->{title} = $::locale->text("$::form->{title} Language");
736
737   $::form->header;
738
739   print $::form->parse_html_template('am/language_header', {
740     numberformats => [ '1,000.00', '1000.00', '1.000,00', '1000,00' ],
741     dateformats => [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ],
742   });
743
744   $::lxdebug->leave_sub;
745 }
746
747 sub save_language {
748   $main::lxdebug->enter_sub();
749
750   my $form     = $main::form;
751   my %myconfig = %main::myconfig;
752   my $locale   = $main::locale;
753
754   $main::auth->assert('config');
755
756   $form->isblank("description", $locale->text('Language missing!'));
757   $form->isblank("template_code", $locale->text('Template Code missing!'));
758   $form->isblank("article_code", $locale->text('Article Code missing!'));
759   AM->save_language(\%myconfig, \%$form);
760   $form->redirect($locale->text('Language saved!'));
761
762   $main::lxdebug->leave_sub();
763 }
764
765 sub delete_language {
766   $main::lxdebug->enter_sub();
767
768   my $form     = $main::form;
769   my %myconfig = %main::myconfig;
770   my $locale   = $main::locale;
771
772   $main::auth->assert('config');
773
774   AM->delete_language(\%myconfig, \%$form);
775   $form->redirect($locale->text('Language deleted!'));
776
777   $main::lxdebug->leave_sub();
778 }
779
780
781 sub add_buchungsgruppe {
782   $main::lxdebug->enter_sub();
783
784   my $form     = $main::form;
785   my %myconfig = %main::myconfig;
786   my $locale   = $main::locale;
787
788   $main::auth->assert('config');
789
790   # $locale->text("Add Buchungsgruppe")
791   # $locale->text("Edit Buchungsgruppe")
792   $form->{title} = "Add";
793
794   $form->{callback} = "am.pl?action=add_buchungsgruppe" unless $form->{callback};
795
796   AM->get_buchungsgruppe(\%myconfig, \%$form);
797   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
798   for (my $i = 0; 4 > $i; $i++) {
799     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
800         qw(income expense));
801   }
802
803   &buchungsgruppe_header;
804   &form_footer;
805
806   $main::lxdebug->leave_sub();
807 }
808
809 sub edit_buchungsgruppe {
810   $main::lxdebug->enter_sub();
811
812   my $form     = $main::form;
813   my %myconfig = %main::myconfig;
814
815   $main::auth->assert('config');
816
817   $form->{title} = "Edit";
818
819   AM->get_buchungsgruppe(\%myconfig, \%$form);
820
821   &buchungsgruppe_header;
822
823   &form_footer;
824
825   $main::lxdebug->leave_sub();
826 }
827
828 sub list_buchungsgruppe {
829   $::lxdebug->enter_sub;
830   $::auth->assert('config');
831
832   AM->buchungsgruppe(\%::myconfig, $::form);
833
834   $::form->{callback} = "am.pl?action=list_buchungsgruppe";
835   $::form->{title}    = $::locale->text('Buchungsgruppen');
836   $::form->header;
837
838   print $::form->parse_html_template('am/buchungsgruppe_list', {
839     swap_link => qq|am.pl?action=swap_buchungsgruppen&|,
840   });
841
842   $::lxdebug->leave_sub;
843 }
844
845 sub buchungsgruppe_header {
846   $::lxdebug->enter_sub;
847   $::auth->assert('config');
848
849   # $locale->text('Add Accounting Group')
850   # $locale->text('Edit Accounting Group')
851   $::form->{title}    = $::locale->text("$::form->{title} Buchungsgruppe");
852
853   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
854   my %acc_type_map = (
855     IC         => $acc_inventory,
856     IC_income  => $acc_income,
857     IC_sale    => $acc_income,
858     IC_expense => $acc_expense,
859     IC_cogs    => $acc_expense,
860   );
861
862   for my $key (keys %acc_type_map) {
863     for my $ref (@{ $::form->{IC_links}{$key} }) {
864       $acc_type_map{$key}{$ref->{id}} = $ref;
865     }
866   }
867
868   my %sorted_accounts = map {
869     $_ => [ sort { $a->{accno} cmp $b->{accno} } values %{ $acc_type_map{$_} } ],
870   } keys %acc_type_map;
871
872   $::form->header;
873   print $::form->parse_html_template('am/buchungsgruppe_header', {
874     accounts      => \%sorted_accounts,
875     account_label => sub { "$_[0]{accno}--$_[0]{description}" },
876   });
877
878   $::lxdebug->leave_sub;
879 }
880
881 sub save_buchungsgruppe {
882   $main::lxdebug->enter_sub();
883
884   my $form     = $main::form;
885   my %myconfig = %main::myconfig;
886   my $locale   = $main::locale;
887
888   $main::auth->assert('config');
889
890   $form->isblank("description", $locale->text('Description missing!'));
891
892   AM->save_buchungsgruppe(\%myconfig, \%$form);
893   $form->redirect($locale->text('Accounting Group saved!'));
894
895   $main::lxdebug->leave_sub();
896 }
897
898 sub delete_buchungsgruppe {
899   $main::lxdebug->enter_sub();
900
901   my $form     = $main::form;
902   my %myconfig = %main::myconfig;
903   my $locale   = $main::locale;
904
905   $main::auth->assert('config');
906
907   AM->delete_buchungsgruppe(\%myconfig, \%$form);
908   $form->redirect($locale->text('Accounting Group deleted!'));
909
910   $main::lxdebug->leave_sub();
911 }
912
913 sub swap_buchungsgruppen {
914   $main::lxdebug->enter_sub();
915
916   my $form     = $main::form;
917   my %myconfig = %main::myconfig;
918
919   $main::auth->assert('config');
920
921   AM->swap_sortkeys(\%myconfig, $form, "buchungsgruppen");
922   list_buchungsgruppe();
923
924   $main::lxdebug->leave_sub();
925 }
926
927 sub edit_defaults {
928   $main::lxdebug->enter_sub();
929
930   my $form     = $main::form;
931   my %myconfig = %main::myconfig;
932   my $locale   = $main::locale;
933
934   # get defaults for account numbers and last numbers
935   AM->defaultaccounts(\%myconfig, \%$form);
936   $form->{ALL_UNITS} = AM->convertible_units(AM->retrieve_all_units(), 'g');
937
938   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
939
940   # default language
941   my $all_languages = SL::DB::Manager::Language->get_all;
942
943 # cash = IST-Versteuerung, accrual = SOLL-Versteuerung
944
945   foreach my $key (keys %{ $form->{IC} }) {
946     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
947       my $array = "ACCNOS_" . uc($key);
948       $form->{$array} ||= [];
949
950       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
951       push @{ $form->{$array} }, {
952         'name'     => $value,
953         'value'    => $value,
954         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
955       };
956     }
957   }
958
959   $form->{title} = $locale->text('Ranges of numbers and default accounts');
960
961   $form->header();
962   print $form->parse_html_template('am/edit_defaults',
963                                    { ALL_LANGUAGES => $all_languages, });
964
965   $main::lxdebug->leave_sub();
966 }
967
968 sub save_defaults {
969   $main::lxdebug->enter_sub();
970
971   my $form     = $main::form;
972   my $locale   = $main::locale;
973
974   AM->save_defaults();
975
976   $form->redirect($locale->text('Defaults saved.'));
977
978   $main::lxdebug->leave_sub();
979 }
980
981 sub _build_cfg_options {
982   my $form     = $main::form;
983   my %myconfig = %main::myconfig;
984
985   my $idx   = shift;
986   my $array = uc($idx) . 'S';
987
988   $form->{$array} = [];
989   foreach my $item (@_) {
990     push @{ $form->{$array} }, {
991       'name'     => $item,
992       'value'    => $item,
993       'selected' => $item eq $myconfig{$idx},
994     };
995   }
996 }
997
998 sub config {
999   $main::lxdebug->enter_sub();
1000
1001   my $form     = $main::form;
1002   my %myconfig = %main::myconfig;
1003   my $locale   = $main::locale;
1004
1005   _build_cfg_options('dateformat', qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
1006   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
1007
1008   my @formats = ();
1009   if ($::lx_office_conf{print_templates}->{opendocument}
1010       && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
1011       && $::lx_office_conf{applications}->{xvfb}                 && (-x $::lx_office_conf{applications}->{xvfb})) {
1012     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
1013                      "value" => "opendocument_pdf" });
1014   }
1015   if ($::lx_office_conf{print_templates}->{latex}) {
1016     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
1017   }
1018   push(@formats, { "name" => "HTML", "value" => "html" });
1019   if ($::lx_office_conf{print_templates}->{latex}) {
1020     push(@formats, { "name" => $locale->text("Postscript"),
1021                      "value" => "postscript" });
1022   }
1023   if ($::lx_office_conf{print_templates}->{opendocument}) {
1024     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
1025                      "value" => "opendocument" });
1026   }
1027
1028   if (!$myconfig{"template_format"}) {
1029     $myconfig{"template_format"} = "pdf";
1030   }
1031   $form->{TEMPLATE_FORMATS} = [];
1032   foreach my $item (@formats) {
1033     push @{ $form->{TEMPLATE_FORMATS} }, {
1034       'name'     => $item->{name},
1035       'value'    => $item->{value},
1036       'selected' => $item->{value} eq $myconfig{template_format},
1037     };
1038   }
1039
1040   if (!$myconfig{"default_media"}) {
1041     $myconfig{"default_media"} = "screen";
1042   }
1043
1044   my %selected = ($myconfig{"default_media"} => "selected");
1045   $form->{MEDIA} = [
1046     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
1047     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
1048     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
1049     ];
1050
1051   $form->{PRINTERS} = [ SL::Printer->all_printers(%::myconfig) ];
1052
1053   my %countrycodes = User->country_codes;
1054
1055   $form->{COUNTRYCODES} = [];
1056   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
1057     push @{ $form->{COUNTRYCODES} }, {
1058       'name'     => $countrycodes{$countrycode},
1059       'value'    => $countrycode,
1060       'selected' => $countrycode eq $myconfig{countrycode},
1061     };
1062   }
1063
1064   $form->{STYLESHEETS} = [];
1065   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css kivitendo.css)) {
1066     push @{ $form->{STYLESHEETS} }, {
1067       'name'     => $item,
1068       'value'    => $item,
1069       'selected' => $item eq $myconfig{stylesheet},
1070     };
1071   }
1072
1073   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
1074   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
1075   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
1076
1077   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
1078
1079   $form->header();
1080   print $form->parse_html_template('am/config');
1081
1082   $main::lxdebug->leave_sub();
1083 }
1084
1085 sub save_preferences {
1086   $main::lxdebug->enter_sub();
1087
1088   my $form     = $main::form;
1089   my %myconfig = %main::myconfig;
1090   my $locale   = $main::locale;
1091
1092   $form->{stylesheet} = $form->{usestylesheet};
1093
1094   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
1095
1096   if (AM->save_preferences(\%myconfig, $form)) {
1097     if ($::auth->can_change_password()
1098         && defined $form->{new_password}
1099         && ($form->{new_password} ne '********')) {
1100       my $verifier = SL::Auth::PasswordPolicy->new;
1101       my $result   = $verifier->verify($form->{new_password});
1102
1103       if ($result != SL::Auth::PasswordPolicy->OK()) {
1104         $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
1105       }
1106
1107       $::auth->change_password($form->{login}, $form->{new_password});
1108
1109       $form->{password} = $form->{new_password};
1110       $::auth->set_session_value('password', $form->{password});
1111       $::auth->create_or_refresh_session();
1112     }
1113
1114     $form->redirect($locale->text('Preferences saved!'));
1115   }
1116
1117   $form->error($locale->text('Cannot save preferences!'));
1118
1119   $main::lxdebug->leave_sub();
1120 }
1121
1122 sub audit_control {
1123   $::lxdebug->enter_sub;
1124   $::auth->assert('config');
1125
1126   $::form->{title} = $::locale->text('Audit Control');
1127
1128   AM->closedto(\%::myconfig, $::form);
1129
1130   $::form->header;
1131   print $::form->parse_html_template('am/audit_control');
1132
1133   $::lxdebug->leave_sub;
1134 }
1135
1136 sub doclose {
1137   $main::lxdebug->enter_sub();
1138
1139   my $form     = $main::form;
1140   my %myconfig = %main::myconfig;
1141   my $locale   = $main::locale;
1142
1143   $main::auth->assert('config');
1144
1145   AM->closebooks(\%myconfig, \%$form);
1146
1147   if ($form->{closedto}) {
1148     $form->redirect(
1149                     $locale->text('Books closed up to') . " "
1150                       . $locale->date(\%myconfig, $form->{closedto}, 1));
1151   } else {
1152     $form->redirect($locale->text('Books are open'));
1153   }
1154
1155   $main::lxdebug->leave_sub();
1156 }
1157
1158 sub edit_units {
1159   $main::lxdebug->enter_sub();
1160
1161   my $form     = $main::form;
1162   my %myconfig = %main::myconfig;
1163   my $locale   = $main::locale;
1164
1165   $main::auth->assert('config');
1166
1167   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1168   AM->units_in_use(\%myconfig, $form, $units);
1169   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
1170
1171   my @languages = AM->language(\%myconfig, $form, 1);
1172
1173   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
1174
1175   my $i = 1;
1176   foreach (@unit_list) {
1177     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
1178     $_->{"UNITLANGUAGES"} = [];
1179     foreach my $lang (@languages) {
1180       push(@{ $_->{"UNITLANGUAGES"} },
1181            { "idx" => $i,
1182              "unit" => $_->{"name"},
1183              "language_id" => $lang->{"id"},
1184              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
1185              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
1186            });
1187     }
1188     $i++;
1189   }
1190
1191   $units = AM->retrieve_units(\%myconfig, $form);
1192   my $ddbox = AM->unit_select_data($units, undef, 1);
1193
1194   $form->{"title"} = $locale->text("Add and edit units");
1195   $form->header();
1196   print($form->parse_html_template("am/edit_units",
1197                                    { "UNITS"               => \@unit_list,
1198                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
1199                                      "LANGUAGES"           => \@languages,
1200                                    }));
1201
1202   $main::lxdebug->leave_sub();
1203 }
1204
1205 sub add_unit {
1206   $main::lxdebug->enter_sub();
1207
1208   my $form     = $main::form;
1209   my %myconfig = %main::myconfig;
1210   my $locale   = $main::locale;
1211
1212   $main::auth->assert('config');
1213
1214   $form->isblank("new_name", $locale->text("The name is missing."));
1215   my $units = AM->retrieve_units(\%myconfig, $form);
1216   my $all_units = AM->retrieve_units(\%myconfig, $form);
1217   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
1218
1219   my ($base_unit, $factor);
1220   if ($form->{"new_base_unit"}) {
1221     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
1222
1223     $form->isblank("new_factor", $locale->text("The factor is missing."));
1224     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
1225     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
1226     $base_unit = $form->{"new_base_unit"};
1227   }
1228
1229   my @languages;
1230   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
1231     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
1232     push(@languages, { "id" => $lang->{"id"},
1233                        "localized" => $form->{"new_localized_$lang->{id}"},
1234                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
1235          });
1236   }
1237
1238   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
1239
1240   $form->{"saved_message"} = $locale->text("The unit has been saved.");
1241
1242   edit_units();
1243
1244   $main::lxdebug->leave_sub();
1245 }
1246
1247 sub set_unit_languages {
1248   $main::lxdebug->enter_sub();
1249
1250   my $form     = $main::form;
1251
1252   $main::auth->assert('config');
1253
1254   my ($unit, $languages, $idx) = @_;
1255
1256   $unit->{"LANGUAGES"} = [];
1257
1258   foreach my $lang (@{$languages}) {
1259     push(@{ $unit->{"LANGUAGES"} },
1260          { "id" => $lang->{"id"},
1261            "localized" => $form->{"localized_${idx}_$lang->{id}"},
1262            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
1263          });
1264   }
1265
1266   $main::lxdebug->leave_sub();
1267 }
1268
1269 sub save_unit {
1270   $main::lxdebug->enter_sub();
1271
1272   my $form     = $main::form;
1273   my %myconfig = %main::myconfig;
1274   my $locale   = $main::locale;
1275
1276   $main::auth->assert('config');
1277
1278   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1279   AM->units_in_use(\%myconfig, $form, $old_units);
1280
1281   my @languages = AM->language(\%myconfig, $form, 1);
1282
1283   my $new_units = {};
1284   my @delete_units = ();
1285   foreach my $i (1..($form->{"rowcount"} * 1)) {
1286     my $old_unit = $old_units->{$form->{"old_name_$i"}};
1287     if (!$old_unit) {
1288       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
1289     }
1290
1291     if ($form->{"unchangeable_$i"}) {
1292       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
1293       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
1294       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1295       next;
1296     }
1297
1298     if ($old_unit->{"in_use"}) {
1299       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
1300     }
1301
1302     if ($form->{"delete_$i"}) {
1303       push(@delete_units, $old_unit->{"name"});
1304       next;
1305     }
1306
1307     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
1308
1309     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
1310     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
1311     $new_units->{$form->{"name_$i"}} = \%h;
1312     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
1313     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1314   }
1315
1316   foreach my $unit (values(%{$new_units})) {
1317     next unless ($unit->{"old_name"});
1318     if ($unit->{"base_unit"}) {
1319       $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"}))
1320         unless (defined($new_units->{$unit->{"base_unit"}}));
1321       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
1322       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
1323     } else {
1324       $unit->{"base_unit"} = undef;
1325       $unit->{"factor"} = undef;
1326     }
1327   }
1328
1329   foreach my $unit (values(%{$new_units})) {
1330     next if ($unit->{"unchanged_unit"});
1331
1332     map({ $_->{"seen"} = 0; } values(%{$new_units}));
1333     my $new_unit = $unit;
1334     while ($new_unit->{"base_unit"}) {
1335       $new_unit->{"seen"} = 1;
1336       $new_unit = $new_units->{$new_unit->{"base_unit"}};
1337       if ($new_unit->{"seen"}) {
1338         $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, " .
1339                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
1340       }
1341     }
1342   }
1343
1344   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
1345
1346   $form->{"saved_message"} = $locale->text("The units have been saved.");
1347
1348   edit_units();
1349
1350   $main::lxdebug->leave_sub();
1351 }
1352
1353 sub show_history_search {
1354   $main::lxdebug->enter_sub();
1355
1356   my $form     = $main::form;
1357   my $locale   = $main::locale;
1358
1359   $main::auth->assert('config');
1360
1361   $form->{title} = $locale->text("History Search");
1362   $form->header();
1363
1364   print $form->parse_html_template("common/search_history");
1365
1366   $main::lxdebug->leave_sub();
1367 }
1368
1369 sub show_am_history {
1370   $main::lxdebug->enter_sub();
1371
1372   my $form     = $main::form;
1373   my %myconfig = %main::myconfig;
1374   my $locale   = $main::locale;
1375
1376   $main::auth->assert('config');
1377
1378   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
1379   $form->{order} ||= 'h.itime--1';
1380
1381   my %search = ( "Artikelnummer"          => "parts",
1382                  "Kundennummer"           => "customer",
1383                  "Lieferantennummer"      => "vendor",
1384                  "Projektnummer"          => "project",
1385                  "Buchungsnummer"         => "oe",
1386                  "Eingangsrechnungnummer" => "ap",
1387                  "Ausgangsrechnungnummer" => "ar",
1388                  "Mahnungsnummer"         => "dunning"
1389     );
1390   my %searchNo = ( "Artikelnummer"          => "partnumber",
1391                    "Kundennummer"           => "customernumber",
1392                    "Lieferantennummer"      => "vendornumber",
1393                    "Projektnummer"          => "projectnumber",
1394                    "Buchungsnummer"         => "ordnumber",
1395                    "Eingangsrechnungnummer" => "invnumber",
1396                    "Ausgangsrechnungnummer" => "invnumber",
1397                    "Mahnungsnummer"         => "dunning_id"
1398     );
1399
1400   my $dbh = $form->dbconnect(\%myconfig);
1401
1402   my $restriction;
1403   $restriction     = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
1404   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
1405   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
1406   if ($form->{mitarbeiter} =~ m/^\d+$/) {
1407     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
1408   } elsif ($form->{mitarbeiter}) {
1409     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
1410   }
1411
1412   my $query = qq|SELECT trans_id AS id FROM history_erp | .
1413     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
1414      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
1415
1416   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
1417   my $daten .= shift @ids;
1418   $daten    .= join '', map { " OR trans_id = $_" } @ids;
1419
1420   my ($sort, $sortby) = split(/\-\-/, $form->{order});
1421   $sort =~ s/.*\.(.*)$/$1/;
1422
1423   $form->{title} = $locale->text("History Search");
1424   $form->header();
1425
1426   print $form->parse_html_template("common/show_history",
1427                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
1428                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
1429                                      "NONEWWINDOW"    => 1,
1430                                      uc($sort)        => 1,
1431                                      uc($sort) . "BY" => $sortby,
1432                                      'callback'       => $callback,
1433                                    });
1434   $dbh->disconnect();
1435
1436   $main::lxdebug->leave_sub();
1437 }
1438
1439 sub add_tax {
1440   $main::lxdebug->enter_sub();
1441
1442   my $form     = $main::form;
1443   my $locale   = $main::locale;
1444
1445   $main::auth->assert('config');
1446
1447   $form->{title} =  $locale->text('Add');
1448
1449   $form->{callback} ||= "am.pl?action=add_tax";
1450
1451   _get_taxaccount_selection();
1452
1453   $form->header();
1454
1455   my $parameters_ref = {
1456 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
1457   };
1458
1459   # Ausgabe des Templates
1460   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1461
1462   $main::lxdebug->leave_sub();
1463 }
1464
1465 sub edit_tax {
1466   $main::lxdebug->enter_sub();
1467
1468   my $form     = $main::form;
1469   my %myconfig = %main::myconfig;
1470   my $locale   = $main::locale;
1471
1472   $main::auth->assert('config');
1473
1474   $form->{title} =  $locale->text('Edit');
1475
1476   AM->get_tax(\%myconfig, \%$form);
1477   _get_taxaccount_selection();
1478
1479   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
1480
1481   $form->header();
1482
1483   my $parameters_ref = {
1484   };
1485
1486   # Ausgabe des Templates
1487   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1488
1489   $main::lxdebug->leave_sub();
1490 }
1491
1492 sub list_tax {
1493   $main::lxdebug->enter_sub();
1494
1495   my $form     = $main::form;
1496   my %myconfig = %main::myconfig;
1497   my $locale   = $main::locale;
1498
1499   $main::auth->assert('config');
1500
1501   AM->taxes(\%myconfig, \%$form);
1502
1503   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
1504
1505   $form->{callback} = build_std_url('action=list_tax');
1506
1507   $form->{title} = $locale->text('Tax-O-Matic');
1508
1509   $form->header();
1510
1511   my $parameters_ref = {
1512   };
1513
1514   # Ausgabe des Templates
1515   print($form->parse_html_template('am/list_tax', $parameters_ref));
1516
1517   $main::lxdebug->leave_sub();
1518 }
1519
1520 sub _get_taxaccount_selection{
1521   $main::lxdebug->enter_sub();
1522
1523   my $form     = $main::form;
1524   my %myconfig = %main::myconfig;
1525
1526   $main::auth->assert('config');
1527
1528   AM->get_tax_accounts(\%myconfig, \%$form);
1529
1530   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
1531
1532   $main::lxdebug->leave_sub();
1533 }
1534
1535 sub save_tax {
1536   $main::lxdebug->enter_sub();
1537
1538   my $form     = $main::form;
1539   my %myconfig = %main::myconfig;
1540   my $locale   = $main::locale;
1541
1542   $main::auth->assert('config');
1543
1544   $form->isblank("rate", $locale->text('Taxrate missing!'));
1545   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
1546   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
1547
1548   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
1549
1550   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
1551     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1552   }
1553
1554   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
1555     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1556   }
1557
1558   AM->save_tax(\%myconfig, \%$form);
1559   $form->redirect($locale->text('Tax saved!'));
1560
1561   $main::lxdebug->leave_sub();
1562 }
1563
1564 sub delete_tax {
1565   $main::lxdebug->enter_sub();
1566
1567   my $form     = $main::form;
1568   my %myconfig = %main::myconfig;
1569   my $locale   = $main::locale;
1570
1571   $main::auth->assert('config');
1572
1573   AM->delete_tax(\%myconfig, \%$form);
1574   $form->redirect($locale->text('Tax deleted!'));
1575
1576   $main::lxdebug->leave_sub();
1577 }
1578
1579 sub add_price_factor {
1580   $main::lxdebug->enter_sub();
1581
1582   my $form     = $main::form;
1583   my $locale   = $main::locale;
1584
1585   $main::auth->assert('config');
1586
1587   $form->{title}      = $locale->text('Add Price Factor');
1588   $form->{callback} ||= build_std_url('action=add_price_factor');
1589   $::request->{layout}->focus('#description');
1590
1591   $form->header();
1592   print $form->parse_html_template('am/edit_price_factor');
1593
1594   $main::lxdebug->leave_sub();
1595 }
1596
1597 sub edit_price_factor {
1598   $main::lxdebug->enter_sub();
1599
1600   my $form     = $main::form;
1601   my %myconfig = %main::myconfig;
1602   my $locale   = $main::locale;
1603
1604   $main::auth->assert('config');
1605
1606   $form->{title}      = $locale->text('Edit Price Factor');
1607   $form->{callback} ||= build_std_url('action=add_price_factor');
1608   $::request->{layout}->focus('#description');
1609
1610   AM->get_price_factor(\%myconfig, $form);
1611
1612   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
1613
1614   $form->header();
1615   print $form->parse_html_template('am/edit_price_factor');
1616
1617   $main::lxdebug->leave_sub();
1618 }
1619
1620 sub list_price_factors {
1621   $main::lxdebug->enter_sub();
1622
1623   my $form     = $main::form;
1624   my %myconfig = %main::myconfig;
1625   my $locale   = $main::locale;
1626
1627   $main::auth->assert('config');
1628
1629   AM->get_all_price_factors(\%myconfig, \%$form);
1630
1631   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
1632     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
1633   }
1634
1635   $form->{callback} = build_std_url('action=list_price_factors');
1636   $form->{title}    = $locale->text('Price Factors');
1637   $form->{url_base} = build_std_url('callback');
1638
1639   $form->header();
1640   print $form->parse_html_template('am/list_price_factors');
1641
1642   $main::lxdebug->leave_sub();
1643 }
1644
1645 sub save_price_factor {
1646   $main::lxdebug->enter_sub();
1647
1648   my $form     = $main::form;
1649   my %myconfig = %main::myconfig;
1650   my $locale   = $main::locale;
1651
1652   $main::auth->assert('config');
1653
1654   $form->isblank("description", $locale->text('Description missing!'));
1655   $form->isblank("factor", $locale->text('Factor missing!'));
1656
1657   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
1658
1659   AM->save_price_factor(\%myconfig, $form);
1660
1661   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
1662
1663   $form->redirect($locale->text('Price factor saved!'));
1664
1665   $main::lxdebug->leave_sub();
1666 }
1667
1668 sub delete_price_factor {
1669   $main::lxdebug->enter_sub();
1670
1671   my $form     = $main::form;
1672   my %myconfig = %main::myconfig;
1673   my $locale   = $main::locale;
1674
1675   $main::auth->assert('config');
1676
1677   AM->delete_price_factor(\%myconfig, \%$form);
1678
1679   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
1680
1681   $form->redirect($locale->text('Price factor deleted!'));
1682
1683   $main::lxdebug->leave_sub();
1684 }
1685
1686 sub add_warehouse {
1687   $main::lxdebug->enter_sub();
1688
1689   my $form     = $main::form;
1690   my $locale   = $main::locale;
1691
1692   $main::auth->assert('config');
1693
1694   $form->{title}      = $locale->text('Add Warehouse');
1695   $form->{callback} ||= build_std_url('action=add_warehouse');
1696   $::request->{layout}->focus('#description');
1697
1698   $form->header();
1699   print $form->parse_html_template('am/edit_warehouse');
1700
1701   $main::lxdebug->leave_sub();
1702 }
1703
1704 sub edit_warehouse {
1705   $main::lxdebug->enter_sub();
1706
1707   my $form     = $main::form;
1708   my %myconfig = %main::myconfig;
1709   my $locale   = $main::locale;
1710
1711   $main::auth->assert('config');
1712
1713   AM->get_warehouse(\%myconfig, $form);
1714
1715   $form->get_lists('employees' => 'EMPLOYEES');
1716
1717   $form->{title}      = $locale->text('Edit Warehouse');
1718   $form->{callback} ||= build_std_url('action=list_warehouses');
1719   $::request->{layout}->focus('#description');
1720
1721   $form->header();
1722   print $form->parse_html_template('am/edit_warehouse');
1723
1724   $main::lxdebug->leave_sub();
1725 }
1726
1727 sub list_warehouses {
1728   $main::lxdebug->enter_sub();
1729
1730   my $form     = $main::form;
1731   my %myconfig = %main::myconfig;
1732   my $locale   = $main::locale;
1733
1734   $main::auth->assert('config');
1735
1736   AM->get_all_warehouses(\%myconfig, $form);
1737
1738   $form->{callback} = build_std_url('action=list_warehouses');
1739   $form->{title}    = $locale->text('Warehouses');
1740   $form->{url_base} = build_std_url('callback');
1741
1742   $form->header();
1743   print $form->parse_html_template('am/list_warehouses');
1744
1745   $main::lxdebug->leave_sub();
1746 }
1747
1748 sub save_warehouse {
1749   $main::lxdebug->enter_sub();
1750
1751   my $form     = $main::form;
1752   my %myconfig = %main::myconfig;
1753   my $locale   = $main::locale;
1754
1755   $main::auth->assert('config');
1756
1757   $form->isblank("description", $locale->text('Description missing!'));
1758
1759   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
1760
1761   AM->save_warehouse(\%myconfig, $form);
1762
1763   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
1764
1765   $form->redirect($locale->text('Warehouse saved.'));
1766
1767   $main::lxdebug->leave_sub();
1768 }
1769
1770 sub delete_warehouse {
1771   $main::lxdebug->enter_sub();
1772
1773   my $form     = $main::form;
1774   my %myconfig = %main::myconfig;
1775   my $locale   = $main::locale;
1776
1777   $main::auth->assert('config');
1778
1779   if (!$form->{confirmed}) {
1780     $form->{title} = $locale->text('Confirmation');
1781
1782     $form->header();
1783     print $form->parse_html_template('am/confirm_delete_warehouse');
1784     ::end_of_request();
1785   }
1786
1787   if (AM->delete_warehouse(\%myconfig, $form)) {
1788     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
1789     $form->redirect($locale->text('Warehouse deleted.'));
1790
1791   } else {
1792     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
1793   }
1794
1795   $main::lxdebug->leave_sub();
1796 }
1797
1798 sub save_bin {
1799   $main::lxdebug->enter_sub();
1800
1801   my $form     = $main::form;
1802   my %myconfig = %main::myconfig;
1803   my $locale   = $main::locale;
1804
1805   $main::auth->assert('config');
1806
1807   AM->save_bins(\%myconfig, $form);
1808
1809   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
1810
1811   $form->redirect($locale->text('Bins saved.'));
1812
1813   $main::lxdebug->leave_sub();
1814 }