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