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