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