]> wagnertech.de Git - kivitendo-erp.git/blob - bin/mozilla/am.pl
am/list_buchungsgruppe nach template ausgelagert
[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   if ($form->{"original_accno"} &&
430       ($form->{"accno"} eq $form->{"original_accno"})) {
431     $form->error($locale->text('Account Number already used!'));
432   }
433   $form->redirect($locale->text('Account saved!'))
434     if (AM->save_account(\%myconfig, \%$form));
435   $form->error($locale->text('Cannot save account!'));
436
437   $main::lxdebug->leave_sub();
438 }
439
440 sub list_account {
441   $main::lxdebug->enter_sub();
442
443   my $form     = $main::form;
444   my %myconfig = %main::myconfig;
445   my $locale   = $main::locale;
446
447   $main::auth->assert('config');
448
449   $form->{callback}     = build_std_url('action=list_account');
450   my $link_edit_account = build_std_url('action=edit_account', 'callback');
451
452   CA->all_accounts(\%myconfig, \%$form);
453
454   foreach my $ca (@{ $form->{CA} }) {
455
456     $ca->{debit}  = "";
457     $ca->{credit} = "";
458
459     if ($ca->{amount} > 0) {
460       $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
461     }
462     if ($ca->{amount} < 0) {
463       $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
464     }
465     $ca->{heading}   = ( $ca->{charttype} eq 'H' ) ? 1:'';
466     $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
467   }
468
469   $form->use_stylesheet("list_accounts.css");
470   $form->{title}       = $locale->text('Chart of Accounts');
471
472   $form->header;
473
474
475   my $parameters_ref = {
476   #   hidden_variables                => $_hidden_variables_ref,
477   };
478
479   # Ausgabe des Templates
480   print($form->parse_html_template('am/list_accounts', $parameters_ref));
481
482   $main::lxdebug->leave_sub();
483
484 }
485
486
487 sub list_account_details {
488 # Ajax Funktion aus list_account_details
489   $main::lxdebug->enter_sub();
490
491   my $form     = $main::form;
492   my %myconfig = %main::myconfig;
493   my $locale   = $main::locale;
494
495   $main::auth->assert('config');
496
497   my $chart_id = $form->{args};
498
499   CA->all_accounts(\%myconfig, \%$form, $chart_id);
500
501   foreach my $ca (@{ $form->{CA} }) {
502
503     $ca->{debit}  = "&nbsp;";
504     $ca->{credit} = "&nbsp;";
505
506     if ($ca->{amount} > 0) {
507       $ca->{credit} =
508         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
509     }
510     if ($ca->{amount} < 0) {
511       $ca->{debit} =
512         $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, "&nbsp;");
513     }
514
515     my @links = split( q{:}, $ca->{link});
516
517     $ca->{link} = q{};
518
519     foreach my $link (@links){
520       $link =    ( $link eq 'AR')             ? $locale->text('Account Link AR')
521                : ( $link eq 'AP')             ? $locale->text('Account Link AP')
522                : ( $link eq 'IC')             ? $locale->text('Account Link IC')
523                : ( $link eq 'AR_amount' )     ? $locale->text('Account Link AR_amount')
524                : ( $link eq 'AR_paid' )       ? $locale->text('Account Link AR_paid')
525                : ( $link eq 'AR_tax' )        ? $locale->text('Account Link AR_tax')
526                : ( $link eq 'AP_amount' )     ? $locale->text('Account Link AP_amount')
527                : ( $link eq 'AP_paid' )       ? $locale->text('Account Link AP_paid')
528                : ( $link eq 'AP_tax' )        ? $locale->text('Account Link AP_tax')
529                : ( $link eq 'IC_sale' )       ? $locale->text('Account Link IC_sale')
530                : ( $link eq 'IC_cogs' )       ? $locale->text('Account Link IC_cogs')
531                : ( $link eq 'IC_taxpart' )    ? $locale->text('Account Link IC_taxpart')
532                : ( $link eq 'IC_income' )     ? $locale->text('Account Link IC_income')
533                : ( $link eq 'IC_expense' )    ? $locale->text('Account Link IC_expense')
534                : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
535                : $locale->text('Unknown Link') . ': ' . $link;
536       $ca->{link} .= ($link ne '') ?  "[$link] ":'';
537     }
538
539     $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
540                     : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
541                     : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
542                     : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
543                     : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
544                     : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
545                     : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
546                     : $locale->text('Unknown Category') . ': ' . $ca->{category};
547   }
548
549   $form->{title} = $locale->text('Chart of Accounts');
550
551   print $form->ajax_response_header, $form->parse_html_template('am/list_account_details');
552
553   $main::lxdebug->leave_sub();
554
555 }
556
557 sub delete_account {
558   $main::lxdebug->enter_sub();
559
560   my $form     = $main::form;
561   my %myconfig = %main::myconfig;
562   my $locale   = $main::locale;
563
564   $main::auth->assert('config');
565
566   $form->{title} = $locale->text('Delete Account');
567
568   foreach my $id (
569     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
570     ) {
571     if ($form->{id} == $form->{$id}) {
572       $form->error($locale->text('Cannot delete default account!'));
573     }
574   }
575
576   $form->redirect($locale->text('Account deleted!'))
577     if (AM->delete_account(\%myconfig, \%$form));
578   $form->error($locale->text('Cannot delete account!'));
579
580   $main::lxdebug->leave_sub();
581 }
582
583 sub add_lead {
584   $main::lxdebug->enter_sub();
585
586   my $form     = $main::form;
587
588   $main::auth->assert('config');
589
590   $form->{title} = "Add";
591
592   $form->{callback} = "am.pl?action=add_lead" unless $form->{callback};
593
594   &lead_header;
595   &form_footer;
596
597   $main::lxdebug->leave_sub();
598 }
599
600 sub edit_lead {
601   $main::lxdebug->enter_sub();
602
603   my $form     = $main::form;
604   my %myconfig = %main::myconfig;
605
606   $main::auth->assert('config');
607
608   $form->{title} = "Edit";
609
610   AM->get_lead(\%myconfig, \%$form);
611
612   &lead_header;
613
614   $form->{orphaned} = 1;
615   &form_footer;
616
617   $main::lxdebug->leave_sub();
618 }
619
620 sub list_lead {
621   $::lxdebug->enter_sub;
622   $::auth->assert('config');
623
624   AM->lead(\%::myconfig, $::form);
625
626   $::form->{callback} = "am.pl?action=list_lead";
627   $::form->{title}    = $::locale->text('Lead');
628
629   $::form->header;
630   print $::form->parse_html_template('am/lead_list');
631
632   $::lxdebug->leave_sub;
633 }
634
635 sub lead_header {
636   $::lxdebug->enter_sub;
637   $::auth->assert('config');
638
639   # $locale->text('Add Lead')
640   # $locale->text('Edit Lead')
641   $::form->{title} = $::locale->text("$::form->{title} Lead");
642
643   $::form->header;
644   print $::form->parse_html_template('am/lead_header');
645
646   $::lxdebug->leave_sub;
647 }
648
649 sub save_lead {
650   $main::lxdebug->enter_sub();
651
652   my $form     = $main::form;
653   my %myconfig = %main::myconfig;
654   my $locale   = $main::locale;
655
656   $main::auth->assert('config');
657
658   $form->isblank("description", $locale->text('Description missing!'));
659   AM->save_lead(\%myconfig, \%$form);
660   $form->redirect($locale->text('lead saved!'));
661
662   $main::lxdebug->leave_sub();
663 }
664
665 sub delete_lead {
666   $main::lxdebug->enter_sub();
667
668   my $form     = $main::form;
669   my %myconfig = %main::myconfig;
670   my $locale   = $main::locale;
671
672   $main::auth->assert('config');
673
674   AM->delete_lead(\%myconfig, \%$form);
675   $form->redirect($locale->text('lead deleted!'));
676
677   $main::lxdebug->leave_sub();
678 }
679
680 sub add_language {
681   $main::lxdebug->enter_sub();
682
683   my $form     = $main::form;
684
685   $main::auth->assert('config');
686
687   $form->{title} = "Add";
688
689   $form->{callback} = "am.pl?action=add_language" unless $form->{callback};
690
691   &language_header;
692   &form_footer;
693
694   $main::lxdebug->leave_sub();
695 }
696
697 sub edit_language {
698   $main::lxdebug->enter_sub();
699
700   my $form     = $main::form;
701   my %myconfig = %main::myconfig;
702
703   $main::auth->assert('config');
704
705   $form->{title} = "Edit";
706
707   AM->get_language(\%myconfig, \%$form);
708
709   &language_header;
710
711   $form->{orphaned} = 1;
712   &form_footer;
713
714   $main::lxdebug->leave_sub();
715 }
716
717 sub list_language {
718   $::lxdebug->enter_sub;
719   $::auth->assert('config');
720
721   AM->language(\%::myconfig, $::form);
722
723   $::form->{callback} = "am.pl?action=list_language";
724   $::form->{title}   = $::locale->text('Languages');
725
726   $::form->header;
727
728   print $::form->parse_html_template('am/language_list');
729
730   $::lxdebug->leave_sub;
731 }
732
733 sub language_header {
734   $::lxdebug->enter_sub;
735   $::auth->assert('config');
736
737   # $locale->text('Add Language')
738   # $locale->text('Edit Language')
739   $::form->{title} = $::locale->text("$::form->{title} Language");
740
741   $::form->header;
742
743   print $::form->parse_html_template('am/language_header', {
744     numberformats => [ '1,000.00', '1000.00', '1.000,00', '1000,00' ],
745     dateformats => [ qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ],
746   });
747
748   $::lxdebug->leave_sub;
749 }
750
751 sub save_language {
752   $main::lxdebug->enter_sub();
753
754   my $form     = $main::form;
755   my %myconfig = %main::myconfig;
756   my $locale   = $main::locale;
757
758   $main::auth->assert('config');
759
760   $form->isblank("description", $locale->text('Language missing!'));
761   $form->isblank("template_code", $locale->text('Template Code missing!'));
762   $form->isblank("article_code", $locale->text('Article Code missing!'));
763   AM->save_language(\%myconfig, \%$form);
764   $form->redirect($locale->text('Language saved!'));
765
766   $main::lxdebug->leave_sub();
767 }
768
769 sub delete_language {
770   $main::lxdebug->enter_sub();
771
772   my $form     = $main::form;
773   my %myconfig = %main::myconfig;
774   my $locale   = $main::locale;
775
776   $main::auth->assert('config');
777
778   AM->delete_language(\%myconfig, \%$form);
779   $form->redirect($locale->text('Language deleted!'));
780
781   $main::lxdebug->leave_sub();
782 }
783
784
785 sub add_buchungsgruppe {
786   $main::lxdebug->enter_sub();
787
788   my $form     = $main::form;
789   my %myconfig = %main::myconfig;
790   my $locale   = $main::locale;
791
792   $main::auth->assert('config');
793
794   # $locale->text("Add Buchungsgruppe")
795   # $locale->text("Edit Buchungsgruppe")
796   $form->{title} = "Add";
797
798   $form->{callback} = "am.pl?action=add_buchungsgruppe" unless $form->{callback};
799
800   AM->get_buchungsgruppe(\%myconfig, \%$form);
801   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
802   for (my $i = 0; 4 > $i; $i++) {
803     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
804         qw(income expense));
805   }
806
807   &buchungsgruppe_header;
808   &form_footer;
809
810   $main::lxdebug->leave_sub();
811 }
812
813 sub edit_buchungsgruppe {
814   $main::lxdebug->enter_sub();
815
816   my $form     = $main::form;
817   my %myconfig = %main::myconfig;
818
819   $main::auth->assert('config');
820
821   $form->{title} = "Edit";
822
823   AM->get_buchungsgruppe(\%myconfig, \%$form);
824
825   &buchungsgruppe_header;
826
827   &form_footer;
828
829   $main::lxdebug->leave_sub();
830 }
831
832 sub list_buchungsgruppe {
833   $::lxdebug->enter_sub;
834   $::auth->assert('config');
835
836   AM->buchungsgruppe(\%::myconfig, $::form);
837
838   $::form->{callback} = "am.pl?action=list_buchungsgruppe";
839   $::form->{title}    = $::locale->text('Buchungsgruppen');
840   $::form->header;
841
842   print $::form->parse_html_template('am/buchungsgruppe_list', {
843     swap_link => qq|am.pl?action=swap_buchungsgruppen&|,
844   });
845
846   $::lxdebug->leave_sub;
847 }
848
849 sub buchungsgruppe_header {
850   $main::lxdebug->enter_sub();
851
852   my $form     = $main::form;
853   my $locale   = $main::locale;
854
855   $main::auth->assert('config');
856
857   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
858
859   # $locale->text('Add Accounting Group')
860   # $locale->text('Edit Accounting Group')
861
862   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
863   my %acc_type_map = (
864     "IC" => $acc_inventory,
865     "IC_income" => $acc_income,
866     "IC_sale" => $acc_income,
867     "IC_expense" => $acc_expense,
868     "IC_cogs" => $acc_expense,
869     );
870
871   foreach my $key (keys(%acc_type_map)) {
872     foreach my $ref (@{ $form->{IC_links}{$key} }) {
873       $acc_type_map{$key}->{$ref->{"id"}} = $ref;
874     }
875   }
876
877   foreach my $type (qw(IC IC_income IC_expense)) {
878     $form->{"select$type"} =
879       join("",
880            map({ "<option value=$_->{id} $_->{selected}>" .
881                    "$_->{accno}--" . H($_->{description}) . "</option>" }
882                sort({ $a->{"accno"} cmp $b->{"accno"} }
883                     values(%{$acc_type_map{$type}}))));
884   }
885
886   if ($form->{id}) {
887     $form->{selectIC} =~ s/selected//g;
888     $form->{selectIC} =~ s/ value=\Q$form->{inventory_accno_id}\E/  value=$form->{inventory_accno_id} selected/;
889     $form->{selectIC_income} =~ s/selected//g;
890     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_0}\E/  value=$form->{income_accno_id_0} selected/;
891     $form->{selectIC_expense} =~ s/selected//g;
892     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_0}\E/  value=$form->{expense_accno_id_0} selected/;
893   }
894
895   my $linkaccounts;
896   if ( $::instance_conf->get_inventory_system eq 'perpetual' ) { # was !$::lx_office_conf{system}->{eur}) { }
897     $linkaccounts = qq|
898                <tr>
899                 <th align=right>| . $locale->text('Inventory') . qq|</th>
900                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
901                 <input name=selectIC type=hidden value="$form->{selectIC}">
902               </tr>|;
903   } elsif ( $::instance_conf->get_inventory_system eq 'periodic' ) {
904     # don't allow choice of inventory accno and don't show that line
905     $linkaccounts = qq|
906                 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>|;
907   };
908
909
910   $linkaccounts .= qq|
911               <tr>
912                 <th align=right>| . $locale->text('National Revenues') . qq|</th>
913                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
914               </tr>
915               <tr>
916                 <th align=right>| . $locale->text('National Expenses') . qq|</th>
917                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
918               </tr>|;
919   if ($form->{id}) {
920     $form->{selectIC_income} =~ s/selected//g;
921     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_1}\E/  value=$form->{income_accno_id_1} selected/;
922     $form->{selectIC_expense} =~ s/selected//g;
923     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_1}\E/  value=$form->{expense_accno_id_1} selected/;
924   }
925   $linkaccounts .= qq|              <tr>
926                 <th align=right>| . $locale->text('Revenues EU with UStId') . qq|</th>
927                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
928               </tr>
929               <tr>
930                 <th align=right>| . $locale->text('Expenses EU with UStId') . qq|</th>
931                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
932               </tr>|;
933
934   if ($form->{id}) {
935     $form->{selectIC_income} =~ s/selected//g;
936     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_2}\E/  value=$form->{income_accno_id_2} selected/;
937     $form->{selectIC_expense} =~ s/selected//g;
938     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_2}\E/  value=$form->{expense_accno_id_2} selected/;
939   }
940
941   $linkaccounts .= qq|              <tr>
942                 <th align=right>| . $locale->text('Revenues EU without UStId') . qq|</th>
943                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
944               </tr>
945               <tr>
946                 <th align=right>| . $locale->text('Expenses EU without UStId') . qq|</th>
947                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
948               </tr>|;
949
950   if ($form->{id}) {
951     $form->{selectIC_income} =~ s/selected//g;
952     $form->{selectIC_income} =~ s/ value=\Q$form->{income_accno_id_3}\E/  value=$form->{income_accno_id_3} selected/;
953     $form->{selectIC_expense} =~ s/selected//g;
954     $form->{selectIC_expense} =~ s/ value=\Q$form->{expense_accno_id_3}\E/  value=$form->{expense_accno_id_3} selected/;
955   }
956
957   $linkaccounts .= qq|              <tr>
958                 <th align=right>| . $locale->text('Foreign Revenues') . qq|</th>
959                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
960               </tr>
961               <tr>
962                 <th align=right>| . $locale->text('Foreign Expenses') . qq|</th>
963                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
964               </tr>
965 |;
966
967
968   $form->header;
969
970   print qq|
971 <body>
972
973 <form method=post action=am.pl>
974
975 <input type=hidden name=id value=$form->{id}>
976 <input type=hidden name=type value=buchungsgruppe>
977
978 <table width=100%>
979   <tr>
980     <th class=listtop colspan=2>$form->{title}</th>
981   </tr>
982   <tr height="5"></tr>
983   <tr>
984     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
985     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
986   <tr>
987   $linkaccounts
988   <td colspan=2><hr size=3 noshade></td>
989   </tr>
990 </table>
991 |;
992
993   $main::lxdebug->leave_sub();
994 }
995
996 sub save_buchungsgruppe {
997   $main::lxdebug->enter_sub();
998
999   my $form     = $main::form;
1000   my %myconfig = %main::myconfig;
1001   my $locale   = $main::locale;
1002
1003   $main::auth->assert('config');
1004
1005   $form->isblank("description", $locale->text('Description missing!'));
1006
1007   AM->save_buchungsgruppe(\%myconfig, \%$form);
1008   $form->redirect($locale->text('Accounting Group saved!'));
1009
1010   $main::lxdebug->leave_sub();
1011 }
1012
1013 sub delete_buchungsgruppe {
1014   $main::lxdebug->enter_sub();
1015
1016   my $form     = $main::form;
1017   my %myconfig = %main::myconfig;
1018   my $locale   = $main::locale;
1019
1020   $main::auth->assert('config');
1021
1022   AM->delete_buchungsgruppe(\%myconfig, \%$form);
1023   $form->redirect($locale->text('Accounting Group deleted!'));
1024
1025   $main::lxdebug->leave_sub();
1026 }
1027
1028 sub swap_buchungsgruppen {
1029   $main::lxdebug->enter_sub();
1030
1031   my $form     = $main::form;
1032   my %myconfig = %main::myconfig;
1033
1034   $main::auth->assert('config');
1035
1036   AM->swap_sortkeys(\%myconfig, $form, "buchungsgruppen");
1037   list_buchungsgruppe();
1038
1039   $main::lxdebug->leave_sub();
1040 }
1041
1042 sub edit_defaults {
1043   $main::lxdebug->enter_sub();
1044
1045   my $form     = $main::form;
1046   my %myconfig = %main::myconfig;
1047   my $locale   = $main::locale;
1048
1049   # get defaults for account numbers and last numbers
1050   AM->defaultaccounts(\%myconfig, \%$form);
1051   $form->{ALL_UNITS} = AM->convertible_units(AM->retrieve_all_units(), 'g');
1052
1053   map { $form->{"defaults_${_}"} = $form->{defaults}->{$_} } keys %{ $form->{defaults} };
1054
1055   # default language
1056   my $all_languages = SL::DB::Manager::Language->get_all;
1057
1058 # EÜR = cash, Bilanzierung = accrual
1059
1060   foreach my $key (keys %{ $form->{IC} }) {
1061     foreach my $accno (sort keys %{ $form->{IC}->{$key} }) {
1062       my $array = "ACCNOS_" . uc($key);
1063       $form->{$array} ||= [];
1064
1065       my $value = "${accno}--" . $form->{IC}->{$key}->{$accno}->{description};
1066       push @{ $form->{$array} }, {
1067         'name'     => $value,
1068         'value'    => $value,
1069         'selected' => $form->{IC}->{$key}->{$accno}->{id} == $form->{defaults}->{$key},
1070       };
1071     }
1072   }
1073
1074   $form->{title} = $locale->text('Ranges of numbers and default accounts');
1075
1076   $form->header();
1077   print $form->parse_html_template('am/edit_defaults',
1078                                    { ALL_LANGUAGES => $all_languages, });
1079
1080   $main::lxdebug->leave_sub();
1081 }
1082
1083 sub save_defaults {
1084   $main::lxdebug->enter_sub();
1085
1086   my $form     = $main::form;
1087   my $locale   = $main::locale;
1088
1089   AM->save_defaults();
1090
1091   $form->redirect($locale->text('Defaults saved.'));
1092
1093   $main::lxdebug->leave_sub();
1094 }
1095
1096 sub _build_cfg_options {
1097   my $form     = $main::form;
1098   my %myconfig = %main::myconfig;
1099
1100   my $idx   = shift;
1101   my $array = uc($idx) . 'S';
1102
1103   $form->{$array} = [];
1104   foreach my $item (@_) {
1105     push @{ $form->{$array} }, {
1106       'name'     => $item,
1107       'value'    => $item,
1108       'selected' => $item eq $myconfig{$idx},
1109     };
1110   }
1111 }
1112
1113 sub config {
1114   $main::lxdebug->enter_sub();
1115
1116   my $form     = $main::form;
1117   my %myconfig = %main::myconfig;
1118   my $locale   = $main::locale;
1119
1120   _build_cfg_options('dateformat', qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
1121   _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00'));
1122
1123   my @formats = ();
1124   if ($::lx_office_conf{print_templates}->{opendocument}
1125       && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
1126       && $::lx_office_conf{applications}->{xvfb}                 && (-x $::lx_office_conf{applications}->{xvfb})) {
1127     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
1128                      "value" => "opendocument_pdf" });
1129   }
1130   if ($::lx_office_conf{print_templates}->{latex}) {
1131     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
1132   }
1133   push(@formats, { "name" => "HTML", "value" => "html" });
1134   if ($::lx_office_conf{print_templates}->{latex}) {
1135     push(@formats, { "name" => $locale->text("Postscript"),
1136                      "value" => "postscript" });
1137   }
1138   if ($::lx_office_conf{print_templates}->{opendocument}) {
1139     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
1140                      "value" => "opendocument" });
1141   }
1142
1143   if (!$myconfig{"template_format"}) {
1144     $myconfig{"template_format"} = "pdf";
1145   }
1146   $form->{TEMPLATE_FORMATS} = [];
1147   foreach my $item (@formats) {
1148     push @{ $form->{TEMPLATE_FORMATS} }, {
1149       'name'     => $item->{name},
1150       'value'    => $item->{value},
1151       'selected' => $item->{value} eq $myconfig{template_format},
1152     };
1153   }
1154
1155   if (!$myconfig{"default_media"}) {
1156     $myconfig{"default_media"} = "screen";
1157   }
1158
1159   my %selected = ($myconfig{"default_media"} => "selected");
1160   $form->{MEDIA} = [
1161     { 'name' => $locale->text('Screen'),  'value' => 'screen',  'selected' => $selected{screen}, },
1162     { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
1163     { 'name' => $locale->text('Queue'),   'value' => 'queue',   'selected' => $selected{queue}, },
1164     ];
1165
1166   $form->{PRINTERS} = [ SL::Printer->all_printers(%::myconfig) ];
1167
1168   my %countrycodes = User->country_codes;
1169
1170   $form->{COUNTRYCODES} = [];
1171   foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
1172     push @{ $form->{COUNTRYCODES} }, {
1173       'name'     => $countrycodes{$countrycode},
1174       'value'    => $countrycode,
1175       'selected' => $countrycode eq $myconfig{countrycode},
1176     };
1177   }
1178
1179   $form->{STYLESHEETS} = [];
1180   foreach my $item (qw(lx-office-erp.css Win2000.css Mobile.css)) {
1181     push @{ $form->{STYLESHEETS} }, {
1182       'name'     => $item,
1183       'value'    => $item,
1184       'selected' => $item eq $myconfig{stylesheet},
1185     };
1186   }
1187
1188   $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
1189   $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
1190   $form->{todo_cfg}            = { TODO->get_user_config('login' => $form->{login}) };
1191
1192   $form->{title}               = $locale->text('Edit Preferences for #1', $form->{login});
1193
1194   $form->header();
1195   print $form->parse_html_template('am/config');
1196
1197   $main::lxdebug->leave_sub();
1198 }
1199
1200 sub save_preferences {
1201   $main::lxdebug->enter_sub();
1202
1203   my $form     = $main::form;
1204   my %myconfig = %main::myconfig;
1205   my $locale   = $main::locale;
1206
1207   $form->{stylesheet} = $form->{usestylesheet};
1208
1209   TODO->save_user_config('login' => $form->{login}, %{ $form->{todo_cfg} || { } });
1210
1211   if (AM->save_preferences(\%myconfig, $form)) {
1212     if ($::auth->can_change_password()
1213         && defined $form->{new_password}
1214         && ($form->{new_password} ne '********')) {
1215       my $verifier = SL::Auth::PasswordPolicy->new;
1216       my $result   = $verifier->verify($form->{new_password});
1217
1218       if ($result != SL::Auth::PasswordPolicy->OK()) {
1219         $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
1220       }
1221
1222       $::auth->change_password($form->{login}, $form->{new_password});
1223
1224       $form->{password} = $form->{new_password};
1225       $::auth->set_session_value('password', $form->{password});
1226       $::auth->create_or_refresh_session();
1227     }
1228
1229     $form->redirect($locale->text('Preferences saved!'));
1230   }
1231
1232   $form->error($locale->text('Cannot save preferences!'));
1233
1234   $main::lxdebug->leave_sub();
1235 }
1236
1237 sub audit_control {
1238   $main::lxdebug->enter_sub();
1239
1240   my $form     = $main::form;
1241   my %myconfig = %main::myconfig;
1242   my $locale   = $main::locale;
1243
1244   $main::auth->assert('config');
1245
1246   $form->{title} = $locale->text('Audit Control');
1247
1248   AM->closedto(\%myconfig, \%$form);
1249
1250   $form->header;
1251
1252   print qq|
1253 <body>
1254
1255 <form method=post action=am.pl>
1256
1257 <table width=100%>
1258   <tr><th class=listtop>$form->{title}</th></tr>
1259   <tr height="5"></tr>
1260   <tr>
1261     <td>
1262       <table>
1263         <tr>
1264           <th>| . $locale->text('Close Books up to') . qq|</th>
1265           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
1266         </tr>
1267       </table>
1268     </td>
1269   </tr>
1270 </table>
1271
1272 <hr size=3 noshade>
1273
1274 <br>
1275 <input type=hidden name=nextsub value=doclose>
1276
1277 <input type=submit class=submit name=action value="|
1278     . $locale->text('Continue') . qq|">
1279
1280 </form>
1281
1282 </body>
1283 </html>
1284 |;
1285
1286   $main::lxdebug->leave_sub();
1287 }
1288
1289 sub doclose {
1290   $main::lxdebug->enter_sub();
1291
1292   my $form     = $main::form;
1293   my %myconfig = %main::myconfig;
1294   my $locale   = $main::locale;
1295
1296   $main::auth->assert('config');
1297
1298   AM->closebooks(\%myconfig, \%$form);
1299
1300   if ($form->{closedto}) {
1301     $form->redirect(
1302                     $locale->text('Books closed up to') . " "
1303                       . $locale->date(\%myconfig, $form->{closedto}, 1));
1304   } else {
1305     $form->redirect($locale->text('Books are open'));
1306   }
1307
1308   $main::lxdebug->leave_sub();
1309 }
1310
1311 sub edit_units {
1312   $main::lxdebug->enter_sub();
1313
1314   my $form     = $main::form;
1315   my %myconfig = %main::myconfig;
1316   my $locale   = $main::locale;
1317
1318   $main::auth->assert('config');
1319
1320   my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1321   AM->units_in_use(\%myconfig, $form, $units);
1322   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
1323
1324   my @languages = AM->language(\%myconfig, $form, 1);
1325
1326   my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
1327
1328   my $i = 1;
1329   foreach (@unit_list) {
1330     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
1331     $_->{"UNITLANGUAGES"} = [];
1332     foreach my $lang (@languages) {
1333       push(@{ $_->{"UNITLANGUAGES"} },
1334            { "idx" => $i,
1335              "unit" => $_->{"name"},
1336              "language_id" => $lang->{"id"},
1337              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
1338              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
1339            });
1340     }
1341     $i++;
1342   }
1343
1344   $units = AM->retrieve_units(\%myconfig, $form);
1345   my $ddbox = AM->unit_select_data($units, undef, 1);
1346
1347   $form->{"title"} = $locale->text("Add and edit units");
1348   $form->header();
1349   print($form->parse_html_template("am/edit_units",
1350                                    { "UNITS"               => \@unit_list,
1351                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
1352                                      "LANGUAGES"           => \@languages,
1353                                    }));
1354
1355   $main::lxdebug->leave_sub();
1356 }
1357
1358 sub add_unit {
1359   $main::lxdebug->enter_sub();
1360
1361   my $form     = $main::form;
1362   my %myconfig = %main::myconfig;
1363   my $locale   = $main::locale;
1364
1365   $main::auth->assert('config');
1366
1367   $form->isblank("new_name", $locale->text("The name is missing."));
1368   my $units = AM->retrieve_units(\%myconfig, $form);
1369   my $all_units = AM->retrieve_units(\%myconfig, $form);
1370   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
1371
1372   my ($base_unit, $factor);
1373   if ($form->{"new_base_unit"}) {
1374     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
1375
1376     $form->isblank("new_factor", $locale->text("The factor is missing."));
1377     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
1378     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
1379     $base_unit = $form->{"new_base_unit"};
1380   }
1381
1382   my @languages;
1383   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
1384     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
1385     push(@languages, { "id" => $lang->{"id"},
1386                        "localized" => $form->{"new_localized_$lang->{id}"},
1387                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
1388          });
1389   }
1390
1391   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
1392
1393   $form->{"saved_message"} = $locale->text("The unit has been saved.");
1394
1395   edit_units();
1396
1397   $main::lxdebug->leave_sub();
1398 }
1399
1400 sub set_unit_languages {
1401   $main::lxdebug->enter_sub();
1402
1403   my $form     = $main::form;
1404
1405   $main::auth->assert('config');
1406
1407   my ($unit, $languages, $idx) = @_;
1408
1409   $unit->{"LANGUAGES"} = [];
1410
1411   foreach my $lang (@{$languages}) {
1412     push(@{ $unit->{"LANGUAGES"} },
1413          { "id" => $lang->{"id"},
1414            "localized" => $form->{"localized_${idx}_$lang->{id}"},
1415            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
1416          });
1417   }
1418
1419   $main::lxdebug->leave_sub();
1420 }
1421
1422 sub save_unit {
1423   $main::lxdebug->enter_sub();
1424
1425   my $form     = $main::form;
1426   my %myconfig = %main::myconfig;
1427   my $locale   = $main::locale;
1428
1429   $main::auth->assert('config');
1430
1431   my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
1432   AM->units_in_use(\%myconfig, $form, $old_units);
1433
1434   my @languages = AM->language(\%myconfig, $form, 1);
1435
1436   my $new_units = {};
1437   my @delete_units = ();
1438   foreach my $i (1..($form->{"rowcount"} * 1)) {
1439     my $old_unit = $old_units->{$form->{"old_name_$i"}};
1440     if (!$old_unit) {
1441       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
1442     }
1443
1444     if ($form->{"unchangeable_$i"}) {
1445       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
1446       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
1447       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1448       next;
1449     }
1450
1451     if ($old_unit->{"in_use"}) {
1452       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
1453     }
1454
1455     if ($form->{"delete_$i"}) {
1456       push(@delete_units, $old_unit->{"name"});
1457       next;
1458     }
1459
1460     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
1461
1462     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
1463     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
1464     $new_units->{$form->{"name_$i"}} = \%h;
1465     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
1466     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
1467   }
1468
1469   foreach my $unit (values(%{$new_units})) {
1470     next unless ($unit->{"old_name"});
1471     if ($unit->{"base_unit"}) {
1472       $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"}))
1473         unless (defined($new_units->{$unit->{"base_unit"}}));
1474       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
1475       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
1476     } else {
1477       $unit->{"base_unit"} = undef;
1478       $unit->{"factor"} = undef;
1479     }
1480   }
1481
1482   foreach my $unit (values(%{$new_units})) {
1483     next if ($unit->{"unchanged_unit"});
1484
1485     map({ $_->{"seen"} = 0; } values(%{$new_units}));
1486     my $new_unit = $unit;
1487     while ($new_unit->{"base_unit"}) {
1488       $new_unit->{"seen"} = 1;
1489       $new_unit = $new_units->{$new_unit->{"base_unit"}};
1490       if ($new_unit->{"seen"}) {
1491         $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, " .
1492                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
1493       }
1494     }
1495   }
1496
1497   AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
1498
1499   $form->{"saved_message"} = $locale->text("The units have been saved.");
1500
1501   edit_units();
1502
1503   $main::lxdebug->leave_sub();
1504 }
1505
1506 sub show_history_search {
1507   $main::lxdebug->enter_sub();
1508
1509   my $form     = $main::form;
1510   my $locale   = $main::locale;
1511
1512   $main::auth->assert('config');
1513
1514   $form->{title} = $locale->text("History Search");
1515   $form->header();
1516
1517   print $form->parse_html_template("common/search_history");
1518
1519   $main::lxdebug->leave_sub();
1520 }
1521
1522 sub show_am_history {
1523   $main::lxdebug->enter_sub();
1524
1525   my $form     = $main::form;
1526   my %myconfig = %main::myconfig;
1527   my $locale   = $main::locale;
1528
1529   $main::auth->assert('config');
1530
1531   my $callback     = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
1532   $form->{order} ||= 'h.itime--1';
1533
1534   my %search = ( "Artikelnummer"          => "parts",
1535                  "Kundennummer"           => "customer",
1536                  "Lieferantennummer"      => "vendor",
1537                  "Projektnummer"          => "project",
1538                  "Buchungsnummer"         => "oe",
1539                  "Eingangsrechnungnummer" => "ap",
1540                  "Ausgangsrechnungnummer" => "ar",
1541                  "Mahnungsnummer"         => "dunning"
1542     );
1543   my %searchNo = ( "Artikelnummer"          => "partnumber",
1544                    "Kundennummer"           => "customernumber",
1545                    "Lieferantennummer"      => "vendornumber",
1546                    "Projektnummer"          => "projectnumber",
1547                    "Buchungsnummer"         => "ordnumber",
1548                    "Eingangsrechnungnummer" => "invnumber",
1549                    "Ausgangsrechnungnummer" => "invnumber",
1550                    "Mahnungsnummer"         => "dunning_id"
1551     );
1552
1553   my $dbh = $form->dbconnect(\%myconfig);
1554
1555   my $restriction;
1556   $restriction     = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
1557   $restriction    .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate})                                                          if $form->{fromdate};
1558   $restriction    .= qq| AND h.itime::date <= | . conv_dateq($form->{todate})                                                            if $form->{todate};
1559   if ($form->{mitarbeiter} =~ m/^\d+$/) {
1560     $restriction  .= qq| AND employee_id = |    . $form->{mitarbeiter};
1561   } elsif ($form->{mitarbeiter}) {
1562     $restriction  .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
1563   }
1564
1565   my $query = qq|SELECT trans_id AS id FROM history_erp | .
1566     (  $form->{'searchid'} ? qq| WHERE snumbers = '|  . $searchNo{$form->{'what2search'}} . qq|_| . $form->{'searchid'} . qq|'|
1567      :                       qq| WHERE snumbers ~ '^| . $searchNo{$form->{'what2search'}} . qq|'|);
1568
1569   my @ids    = grep { $_ * 1 } selectall_array_query($form, $dbh, $query);
1570   my $daten .= shift @ids;
1571   $daten    .= join '', map { " OR trans_id = $_" } @ids;
1572
1573   my ($sort, $sortby) = split(/\-\-/, $form->{order});
1574   $sort =~ s/.*\.(.*)$/$1/;
1575
1576   $form->{title} = $locale->text("History Search");
1577   $form->header();
1578
1579   print $form->parse_html_template("common/show_history",
1580                                    { "DATEN"          => $form->get_history($dbh, $daten, $restriction, $form->{order}),
1581                                      "SUCCESS"        => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
1582                                      "NONEWWINDOW"    => 1,
1583                                      uc($sort)        => 1,
1584                                      uc($sort) . "BY" => $sortby,
1585                                      'callback'       => $callback,
1586                                    });
1587   $dbh->disconnect();
1588
1589   $main::lxdebug->leave_sub();
1590 }
1591
1592 sub add_tax {
1593   $main::lxdebug->enter_sub();
1594
1595   my $form     = $main::form;
1596   my $locale   = $main::locale;
1597
1598   $main::auth->assert('config');
1599
1600   $form->{title} =  $locale->text('Add');
1601
1602   $form->{callback} ||= "am.pl?action=add_tax";
1603
1604   _get_taxaccount_selection();
1605
1606   $form->header();
1607
1608   my $parameters_ref = {
1609 #    ChartTypeIsAccount         => $ChartTypeIsAccount,
1610   };
1611
1612   # Ausgabe des Templates
1613   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1614
1615   $main::lxdebug->leave_sub();
1616 }
1617
1618 sub edit_tax {
1619   $main::lxdebug->enter_sub();
1620
1621   my $form     = $main::form;
1622   my %myconfig = %main::myconfig;
1623   my $locale   = $main::locale;
1624
1625   $main::auth->assert('config');
1626
1627   $form->{title} =  $locale->text('Edit');
1628
1629   AM->get_tax(\%myconfig, \%$form);
1630   _get_taxaccount_selection();
1631
1632   $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
1633
1634   $form->header();
1635
1636   my $parameters_ref = {
1637   };
1638
1639   # Ausgabe des Templates
1640   print($form->parse_html_template('am/edit_tax', $parameters_ref));
1641
1642   $main::lxdebug->leave_sub();
1643 }
1644
1645 sub list_tax {
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   AM->taxes(\%myconfig, \%$form);
1655
1656   map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
1657
1658   $form->{callback} = build_std_url('action=list_tax');
1659
1660   $form->{title} = $locale->text('Tax-O-Matic');
1661
1662   $form->header();
1663
1664   my $parameters_ref = {
1665   };
1666
1667   # Ausgabe des Templates
1668   print($form->parse_html_template('am/list_tax', $parameters_ref));
1669
1670   $main::lxdebug->leave_sub();
1671 }
1672
1673 sub _get_taxaccount_selection{
1674   $main::lxdebug->enter_sub();
1675
1676   my $form     = $main::form;
1677   my %myconfig = %main::myconfig;
1678
1679   $main::auth->assert('config');
1680
1681   AM->get_tax_accounts(\%myconfig, \%$form);
1682
1683   map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
1684
1685   $main::lxdebug->leave_sub();
1686 }
1687
1688 sub save_tax {
1689   $main::lxdebug->enter_sub();
1690
1691   my $form     = $main::form;
1692   my %myconfig = %main::myconfig;
1693   my $locale   = $main::locale;
1694
1695   $main::auth->assert('config');
1696
1697   $form->isblank("rate", $locale->text('Taxrate missing!'));
1698   $form->isblank("taxdescription", $locale->text('Taxdescription  missing!'));
1699   $form->isblank("taxkey", $locale->text('Taxkey  missing!'));
1700
1701   $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
1702
1703   if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
1704     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1705   }
1706
1707   if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
1708     $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1709   }
1710
1711   AM->save_tax(\%myconfig, \%$form);
1712   $form->redirect($locale->text('Tax saved!'));
1713
1714   $main::lxdebug->leave_sub();
1715 }
1716
1717 sub delete_tax {
1718   $main::lxdebug->enter_sub();
1719
1720   my $form     = $main::form;
1721   my %myconfig = %main::myconfig;
1722   my $locale   = $main::locale;
1723
1724   $main::auth->assert('config');
1725
1726   AM->delete_tax(\%myconfig, \%$form);
1727   $form->redirect($locale->text('Tax deleted!'));
1728
1729   $main::lxdebug->leave_sub();
1730 }
1731
1732 sub add_price_factor {
1733   $main::lxdebug->enter_sub();
1734
1735   my $form     = $main::form;
1736   my $locale   = $main::locale;
1737
1738   $main::auth->assert('config');
1739
1740   $form->{title}      = $locale->text('Add Price Factor');
1741   $form->{callback} ||= build_std_url('action=add_price_factor');
1742   $form->{fokus}      = 'description';
1743
1744   $form->header();
1745   print $form->parse_html_template('am/edit_price_factor');
1746
1747   $main::lxdebug->leave_sub();
1748 }
1749
1750 sub edit_price_factor {
1751   $main::lxdebug->enter_sub();
1752
1753   my $form     = $main::form;
1754   my %myconfig = %main::myconfig;
1755   my $locale   = $main::locale;
1756
1757   $main::auth->assert('config');
1758
1759   $form->{title}      = $locale->text('Edit Price Factor');
1760   $form->{callback} ||= build_std_url('action=add_price_factor');
1761   $form->{fokus}      = 'description';
1762
1763   AM->get_price_factor(\%myconfig, $form);
1764
1765   $form->{factor} = $form->format_amount(\%myconfig, $form->{factor} * 1);
1766
1767   $form->header();
1768   print $form->parse_html_template('am/edit_price_factor');
1769
1770   $main::lxdebug->leave_sub();
1771 }
1772
1773 sub list_price_factors {
1774   $main::lxdebug->enter_sub();
1775
1776   my $form     = $main::form;
1777   my %myconfig = %main::myconfig;
1778   my $locale   = $main::locale;
1779
1780   $main::auth->assert('config');
1781
1782   AM->get_all_price_factors(\%myconfig, \%$form);
1783
1784   foreach my $current (@{ $form->{PRICE_FACTORS} }) {
1785     $current->{factor} = $form->format_amount(\%myconfig, $current->{factor} * 1);
1786   }
1787
1788   $form->{callback} = build_std_url('action=list_price_factors');
1789   $form->{title}    = $locale->text('Price Factors');
1790   $form->{url_base} = build_std_url('callback');
1791
1792   $form->header();
1793   print $form->parse_html_template('am/list_price_factors');
1794
1795   $main::lxdebug->leave_sub();
1796 }
1797
1798 sub save_price_factor {
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   $form->isblank("description", $locale->text('Description missing!'));
1808   $form->isblank("factor", $locale->text('Factor missing!'));
1809
1810   $form->{factor} = $form->parse_amount(\%myconfig, $form->{factor});
1811
1812   AM->save_price_factor(\%myconfig, $form);
1813
1814   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor saved!')) if ($form->{callback});
1815
1816   $form->redirect($locale->text('Price factor saved!'));
1817
1818   $main::lxdebug->leave_sub();
1819 }
1820
1821 sub delete_price_factor {
1822   $main::lxdebug->enter_sub();
1823
1824   my $form     = $main::form;
1825   my %myconfig = %main::myconfig;
1826   my $locale   = $main::locale;
1827
1828   $main::auth->assert('config');
1829
1830   AM->delete_price_factor(\%myconfig, \%$form);
1831
1832   $form->{callback} .= '&MESSAGE=' . $form->escape($locale->text('Price factor deleted!')) if ($form->{callback});
1833
1834   $form->redirect($locale->text('Price factor deleted!'));
1835
1836   $main::lxdebug->leave_sub();
1837 }
1838
1839 sub add_warehouse {
1840   $main::lxdebug->enter_sub();
1841
1842   my $form     = $main::form;
1843   my $locale   = $main::locale;
1844
1845   $main::auth->assert('config');
1846
1847   $form->{title}      = $locale->text('Add Warehouse');
1848   $form->{callback} ||= build_std_url('action=add_warehouse');
1849   $form->{fokus}      = 'description';
1850
1851   $form->header();
1852   print $form->parse_html_template('am/edit_warehouse');
1853
1854   $main::lxdebug->leave_sub();
1855 }
1856
1857 sub edit_warehouse {
1858   $main::lxdebug->enter_sub();
1859
1860   my $form     = $main::form;
1861   my %myconfig = %main::myconfig;
1862   my $locale   = $main::locale;
1863
1864   $main::auth->assert('config');
1865
1866   AM->get_warehouse(\%myconfig, $form);
1867
1868   $form->get_lists('employees' => 'EMPLOYEES');
1869
1870   $form->{title}      = $locale->text('Edit Warehouse');
1871   $form->{callback} ||= build_std_url('action=list_warehouses');
1872   $form->{fokus}      = 'description';
1873
1874   $form->header();
1875   print $form->parse_html_template('am/edit_warehouse');
1876
1877   $main::lxdebug->leave_sub();
1878 }
1879
1880 sub list_warehouses {
1881   $main::lxdebug->enter_sub();
1882
1883   my $form     = $main::form;
1884   my %myconfig = %main::myconfig;
1885   my $locale   = $main::locale;
1886
1887   $main::auth->assert('config');
1888
1889   AM->get_all_warehouses(\%myconfig, $form);
1890
1891   $form->{callback} = build_std_url('action=list_warehouses');
1892   $form->{title}    = $locale->text('Warehouses');
1893   $form->{url_base} = build_std_url('callback');
1894
1895   $form->header();
1896   print $form->parse_html_template('am/list_warehouses');
1897
1898   $main::lxdebug->leave_sub();
1899 }
1900
1901 sub save_warehouse {
1902   $main::lxdebug->enter_sub();
1903
1904   my $form     = $main::form;
1905   my %myconfig = %main::myconfig;
1906   my $locale   = $main::locale;
1907
1908   $main::auth->assert('config');
1909
1910   $form->isblank("description", $locale->text('Description missing!'));
1911
1912   $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
1913
1914   AM->save_warehouse(\%myconfig, $form);
1915
1916   $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
1917
1918   $form->redirect($locale->text('Warehouse saved.'));
1919
1920   $main::lxdebug->leave_sub();
1921 }
1922
1923 sub delete_warehouse {
1924   $main::lxdebug->enter_sub();
1925
1926   my $form     = $main::form;
1927   my %myconfig = %main::myconfig;
1928   my $locale   = $main::locale;
1929
1930   $main::auth->assert('config');
1931
1932   if (!$form->{confirmed}) {
1933     $form->{title} = $locale->text('Confirmation');
1934
1935     $form->header();
1936     print $form->parse_html_template('am/confirm_delete_warehouse');
1937     ::end_of_request();
1938   }
1939
1940   if (AM->delete_warehouse(\%myconfig, $form)) {
1941     $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
1942     $form->redirect($locale->text('Warehouse deleted.'));
1943
1944   } else {
1945     $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
1946   }
1947
1948   $main::lxdebug->leave_sub();
1949 }
1950
1951 sub save_bin {
1952   $main::lxdebug->enter_sub();
1953
1954   my $form     = $main::form;
1955   my %myconfig = %main::myconfig;
1956   my $locale   = $main::locale;
1957
1958   $main::auth->assert('config');
1959
1960   AM->save_bins(\%myconfig, $form);
1961
1962   $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
1963
1964   $form->redirect($locale->text('Bins saved.'));
1965
1966   $main::lxdebug->leave_sub();
1967 }