1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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,
29 #======================================================================
33 #======================================================================
38 use SL::Auth::PasswordPolicy;
45 use SL::Locale::String qw(t8);
51 use SL::DBUtils qw(selectall_array_query conv_dateq);
54 require "bin/mozilla/common.pl";
62 sub add { call_sub("add_$main::form->{type}"); }
63 sub delete { call_sub("delete_$main::form->{type}"); }
64 sub save { call_sub("save_$main::form->{type}"); }
65 sub edit { call_sub("edit_$main::form->{type}"); }
66 sub continue { call_sub($main::form->{"nextsub"}); }
67 sub save_as_new { call_sub("save_as_new_$main::form->{type}"); }
70 $main::lxdebug->enter_sub();
72 my $form = $main::form;
73 my %myconfig = %main::myconfig;
75 $main::auth->assert('config');
77 $form->{title} = "Add";
78 $form->{charttype} = "A";
79 AM->get_account(\%myconfig, \%$form);
81 $form->{callback} = "am.pl?action=list_account" unless $form->{callback};
86 $main::lxdebug->leave_sub();
90 $main::lxdebug->enter_sub();
92 my $form = $main::form;
93 my %myconfig = %main::myconfig;
94 my $defaults = SL::DB::Default->get;
96 $main::auth->assert('config');
98 $form->{title} = "Edit";
99 $form->{feature_balance} = $defaults->feature_balance;
100 $form->{feature_datev} = $defaults->feature_datev;
101 $form->{feature_erfolgsrechnung} = $defaults->feature_erfolgsrechnung;
102 $form->{feature_eurechnung} = $defaults->feature_eurechnung;
103 $form->{feature_ustva} = $defaults->feature_ustva;
105 AM->get_account(\%myconfig, \%$form);
107 foreach my $item (split(/:/, $form->{link})) {
108 $form->{$item} = "checked";
114 $main::lxdebug->leave_sub();
118 $main::lxdebug->enter_sub();
120 my $form = $main::form;
121 my %myconfig = %main::myconfig;
122 my $locale = $main::locale;
124 $main::auth->assert('config');
126 if ( $form->{action} eq 'edit_account') {
127 $form->{account_exists} = '1';
130 $form->{title} = $locale->text("$form->{title} Account");
132 $form->{"$form->{charttype}_checked"} = "checked";
133 $form->{"$form->{category}_checked"} = "checked";
135 $form->{select_tax} = "";
137 my @tax_report_pos = USTVA->report_variables({
138 myconfig => \%myconfig,
141 attribute => 'position',
145 if (@{ $form->{TAXKEY} }) {
146 foreach my $item (@{ $form->{TAXKEY} }) {
147 $item->{rate} = $item->{rate} * 100 . '%';
150 # Fill in empty row for new Taxkey
151 my $newtaxkey_ref = {
156 taxdescription => '',
160 startdate => $form->{account_exists} ? '' : DateTime->new(year => 1970, month => 1, day => 1)->to_lxoffice,
163 push @{ $form->{ACCOUNT_TAXKEYS} }, $newtaxkey_ref;
166 foreach my $taxkey_used (@{ $form->{ACCOUNT_TAXKEYS} } ) {
168 # Fill in a runningnumber
169 $form->{ACCOUNT_TAXKEYS}[$i]{runningnumber} = $i;
171 # Fill in the Taxkeys as select options
172 foreach my $item (@{ $form->{TAXKEY} }) {
173 if ($item->{id} == $taxkey_used->{tax_id}) {
174 $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
175 qq|<option value="$item->{id}" selected="selected">|
176 . sprintf("%.2d", $item->{taxkey})
177 . qq|. $item->{taxdescription} ($item->{rate}) |
178 . $locale->text('Tax-o-matic Account')
179 . qq|: $item->{chart_accno}\n|;
182 $form->{ACCOUNT_TAXKEYS}[$i]{selecttaxkey} .=
183 qq|<option value="$item->{id}">|
184 . sprintf("%.2d", $item->{taxkey})
185 . qq|. $item->{taxdescription} ($item->{rate}) |
186 . $locale->text('Tax-o-matic Account')
187 . qq|: $item->{chart_accno}\n|;
192 # Fill in the USTVA Numbers as select options
193 foreach my $item ( '', sort({ $a cmp $b } @tax_report_pos) ) {
195 $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="" selected="selected">-\n|;
197 elsif ( $item eq $taxkey_used->{pos_ustva} ) {
198 $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item" selected="selected">$item\n|;
201 $form->{ACCOUNT_TAXKEYS}[$i]{select_tax} .= qq|<option value="$item">$item\n|;
210 # Newaccount Folgekonto
211 if (@{ $form->{NEWACCOUNT} || [] }) {
212 if (!$form->{new_chart_valid}) {
213 $form->{selectnewaccount} = qq|<option value=""> |. $locale->text('None') .q|</option>|;
215 foreach my $item (@{ $form->{NEWACCOUNT} }) {
216 if ($item->{id} == $form->{new_chart_id}) {
217 $form->{selectnewaccount} .=
218 qq|<option value="$item->{id}" selected>$item->{accno}--$item->{description}</option>|;
219 } elsif (!$form->{new_chart_valid}) {
220 $form->{selectnewaccount} .=
221 qq|<option value="$item->{id}">$item->{accno}--$item->{description}</option>|;
227 my $select_eur = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
230 2 => "sonstige Erlöse",
231 3 => "Privatanteile",
233 5 => "Ausserordentliche Erträge",
234 6 => "Vereinnahmte Umsatzst.",
235 7 => "Umsatzsteuererstattungen",
236 8 => "Wareneingänge",
237 9 => "Löhne und Gehälter",
238 10 => "Gesetzl. sozialer Aufw.",
240 12 => "Gas, Strom, Wasser",
241 13 => "Instandhaltung",
242 14 => "Steuern, Versich., Beiträge",
244 16 => "Kfz-Versicherungen",
245 17 => "Sonst. Fahrzeugkosten",
246 18 => "Werbe- und Reisekosten",
247 19 => "Instandhaltung u. Werkzeuge",
248 20 => "Fachzeitschriften, Bücher",
249 21 => "Miete für Einrichtungen",
250 22 => "Rechts- und Beratungskosten",
251 23 => "Bürobedarf, Porto, Telefon",
252 24 => "Sonstige Aufwendungen",
253 25 => "Abschreibungen auf Anlagever.",
254 26 => "Abschreibungen auf GWG",
256 28 => "Umsatzsteuerzahlungen",
258 30 => "Ausserordentlicher Aufwand",
259 31 => "Betriebliche Steuern");
260 foreach my $item (sort({ $a <=> $b } keys(%eur))) {
261 my $text = H($::locale->{iconv_utf8}->convert($eur{$item}));
262 if ($item == $form->{pos_eur}) {
263 $select_eur .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
265 $select_eur .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
270 my $select_er = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
274 foreach my $item (sort({ $a <=> $b } keys(%er))) {
275 my $text = H($::locale->{iconv_utf8}->convert($er{$item}));
276 if ($item == $form->{pos_er}) {
277 $select_er .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
279 $select_er .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|. $text</option>\n|;
284 my $select_bwa = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
288 2 => 'Best.Verdg.FE/UE',
289 3 => 'Aktiv.Eigenleistung',
290 4 => 'Mat./Wareneinkauf',
291 5 => 'So.betr.Erlöse',
292 10 => 'Personalkosten',
294 12 => 'Betriebl.Steuern',
295 13 => 'Vers./Beiträge',
296 14 => 'Kfz.Kosten o.St.',
297 15 => 'Werbe-Reisek.',
298 16 => 'Kosten Warenabgabe',
299 17 => 'Abschreibungen',
300 18 => 'Rep./instandhlt.',
301 19 => 'Übrige Steuern',
302 20 => 'Sonst.Kosten',
304 31 => 'Sonst.neutr.Aufw.',
306 33 => 'Sonst.neutr.Ertrag',
307 34 => 'Verr.kalk.Kosten',
308 35 => 'Steuern Eink.u.Ertr.');
309 foreach my $item (sort({ $a <=> $b } keys %bwapos)) {
310 my $text = H($::locale->{iconv_utf8}->convert($bwapos{$item}));
311 if ($item == $form->{pos_bwa}) {
312 $select_bwa .= qq|<option value="$item" selected>|. sprintf("%.2d", $item) .qq|. $text\n|;
314 $select_bwa .= qq|<option value="$item">|. sprintf("%.2d", $item) .qq|. $text\n|;
319 # Wieder hinzugefügt zu evaluationszwecken (us) 09.03.2007
320 my $select_bilanz = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
321 foreach my $item ((1, 2, 3, 4)) {
322 if ($item == $form->{pos_bilanz}) {
323 $select_bilanz .= qq|<option value=$item selected>|. sprintf("%.2d", $item) .qq|.\n|;
325 $select_bilanz .= qq|<option value=$item>|. sprintf("%.2d", $item) .qq|.\n|;
330 # this is for our parser only! Do not remove.
331 # type=submit $locale->text('Add Account')
332 # type=submit $locale->text('Edit Account')
334 $form->{type} = "account";
336 # preselections category
338 my $select_category = q|<option value=""> |. $locale->text('None') .q|</option>\n|;
341 'A' => $locale->text('Asset'),
342 'L' => $locale->text('Liability'),
343 'Q' => $locale->text('Equity'),
344 'I' => $locale->text('Revenue'),
345 'E' => $locale->text('Expense'),
346 'C' => $locale->text('Costs'),
348 foreach my $item ( sort({ $a <=> $b } keys %category) ) {
349 if ($item eq $form->{category}) {
350 $select_category .= qq|<option value="$item" selected="selected">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
352 $select_category .= qq|<option value="$item">$category{$item} (|. sprintf("%s", $item) .qq|)\n|;
357 # preselection chart type
358 my @all_charttypes = ({'name' => $locale->text('Account'), 'value' => 'A'},
359 {'name' => $locale->text('Heading'), 'value' => 'H'},
361 my $selected_charttype = $form->{charttype};
364 # account where AR_tax or AP_tax is set are not orphaned if they are used as
365 # tax-o-matic account
366 if ( $form->{id} && $form->{orphaned} && ($form->{link} =~ m/(AP_tax|AR_tax)/) ) {
367 if (SL::DB::Manager::Tax->find_by(chart_id => $form->{id})) {
368 $form->{orphaned} = 0;
372 my $ChartTypeIsAccount = ($form->{charttype} eq "A") ? "1":"";
373 my $AccountIsPosted = ($form->{orphaned} ) ? "":"1";
377 my $parameters_ref = {
378 ChartTypeIsAccount => $ChartTypeIsAccount,
379 AccountIsPosted => $AccountIsPosted,
380 select_category => $select_category,
381 all_charttypes => \@all_charttypes,
382 selected_charttype => $selected_charttype,
383 select_bwa => $select_bwa,
384 select_bilanz => $select_bilanz,
385 select_eur => $select_eur,
386 select_er => $select_er,
389 # Ausgabe des Templates
390 print($form->parse_html_template('am/edit_accounts', $parameters_ref));
393 $main::lxdebug->leave_sub();
397 $::lxdebug->enter_sub;
398 $::auth->assert('config');
400 print $::form->parse_html_template('am/form_footer', {
401 show_save => !$::form->{id}
402 || ($::form->{id} && $::form->{orphaned})
403 || ($::form->{type} eq "account" && !$::form->{new_chart_valid}),
404 show_delete => $::form->{id} && $::form->{orphaned},
405 show_save_as_new => $::form->{id} && $::form->{type} eq "account",
408 $::lxdebug->leave_sub;
412 $main::lxdebug->enter_sub();
414 my $form = $main::form;
415 my %myconfig = %main::myconfig;
416 my $locale = $main::locale;
418 $main::auth->assert('config');
420 $form->isblank("accno", $locale->text('Account Number missing!'));
421 $form->isblank("description", $locale->text('Account Description missing!'));
423 if ($form->{charttype} eq 'A'){
424 $form->isblank("category", $locale->text('Account Type missing!'));
426 my $found_valid_taxkey = 0;
427 foreach my $i (0 .. 10) { # 10 is maximum count of taxkeys in form
428 if ($form->{"taxkey_startdate_$i"} and !$form->{"taxkey_del_$i"}) {
429 $found_valid_taxkey = 1;
433 if ($found_valid_taxkey == 0) {
434 $form->error($locale->text('A valid taxkey is missing!'));
438 $form->redirect($locale->text('Account saved!'))
439 if (AM->save_account(\%myconfig, \%$form));
440 $form->error($locale->text('Cannot save account!'));
442 $main::lxdebug->leave_sub();
445 sub save_as_new_account {
446 $main::lxdebug->enter_sub();
448 my $form = $main::form;
449 my %myconfig = %main::myconfig;
450 my $locale = $main::locale;
452 $main::auth->assert('config');
454 $form->isblank("accno", $locale->text('Account Number missing!'));
455 $form->isblank("description", $locale->text('Account Description missing!'));
457 if ($form->{charttype} eq 'A'){
458 $form->isblank("category", $locale->text('Account Type missing!'));
461 for my $taxkey (0 .. 9) {
462 if ($form->{"taxkey_id_$taxkey"}) {
463 $form->{"taxkey_id_$taxkey"} = "NEW";
468 $form->redirect($locale->text('Account saved!'))
469 if (AM->save_account(\%myconfig, \%$form));
470 $form->error($locale->text('Cannot save account!'));
472 $main::lxdebug->leave_sub();
476 $main::lxdebug->enter_sub();
478 my $form = $main::form;
479 my %myconfig = %main::myconfig;
480 my $locale = $main::locale;
482 $main::auth->assert('config');
484 $form->{callback} = build_std_url('action=list_account');
485 my $link_edit_account = build_std_url('action=edit_account', 'callback');
487 CA->all_accounts(\%myconfig, \%$form);
489 foreach my $ca (@{ $form->{CA} }) {
494 if ($ca->{amount} > 0) {
495 $ca->{credit} = $form->format_amount(\%myconfig, $ca->{amount}, 2);
497 if ($ca->{amount} < 0) {
498 $ca->{debit} = $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2);
500 $ca->{heading} = ( $ca->{charttype} eq 'H' ) ? 1:'';
501 $ca->{link_edit_account} = $link_edit_account . '&id=' . E($ca->{id});
504 $::request->{layout}->use_stylesheet("list_accounts.css");
505 $form->{title} = $locale->text('Chart of Accounts');
510 my $parameters_ref = {
511 # hidden_variables => $_hidden_variables_ref,
514 # Ausgabe des Templates
515 print($form->parse_html_template('am/list_accounts', $parameters_ref));
517 $main::lxdebug->leave_sub();
522 sub list_account_details {
523 # Ajax Funktion aus list_account_details
524 $main::lxdebug->enter_sub();
526 my $form = $main::form;
527 my %myconfig = %main::myconfig;
528 my $locale = $main::locale;
530 $main::auth->assert('config');
532 my $chart_id = $form->{args};
534 CA->all_accounts(\%myconfig, \%$form, $chart_id);
536 foreach my $ca (@{ $form->{CA} }) {
538 $ca->{debit} = " ";
539 $ca->{credit} = " ";
541 if ($ca->{amount} > 0) {
543 $form->format_amount(\%myconfig, $ca->{amount}, 2, " ");
545 if ($ca->{amount} < 0) {
547 $form->format_amount(\%myconfig, -1 * $ca->{amount}, 2, " ");
550 my @links = split( q{:}, $ca->{link});
554 foreach my $link (@links){
555 $link = ( $link eq 'AR') ? $locale->text('Account Link AR')
556 : ( $link eq 'AP') ? $locale->text('Account Link AP')
557 : ( $link eq 'IC') ? $locale->text('Account Link IC')
558 : ( $link eq 'AR_amount' ) ? $locale->text('Account Link AR_amount')
559 : ( $link eq 'AR_paid' ) ? $locale->text('Account Link AR_paid')
560 : ( $link eq 'AR_tax' ) ? $locale->text('Account Link AR_tax')
561 : ( $link eq 'AP_amount' ) ? $locale->text('Account Link AP_amount')
562 : ( $link eq 'AP_paid' ) ? $locale->text('Account Link AP_paid')
563 : ( $link eq 'AP_tax' ) ? $locale->text('Account Link AP_tax')
564 : ( $link eq 'IC_sale' ) ? $locale->text('Account Link IC_sale')
565 : ( $link eq 'IC_cogs' ) ? $locale->text('Account Link IC_cogs')
566 : ( $link eq 'IC_taxpart' ) ? $locale->text('Account Link IC_taxpart')
567 : ( $link eq 'IC_income' ) ? $locale->text('Account Link IC_income')
568 : ( $link eq 'IC_expense' ) ? $locale->text('Account Link IC_expense')
569 : ( $link eq 'IC_taxservice' ) ? $locale->text('Account Link IC_taxservice')
570 : $locale->text('Unknown Link') . ': ' . $link;
571 $ca->{link} .= ($link ne '') ? "[$link] ":'';
574 $ca->{category} = ($ca->{category} eq 'A') ? $locale->text('Account Category A')
575 : ($ca->{category} eq 'E') ? $locale->text('Account Category E')
576 : ($ca->{category} eq 'L') ? $locale->text('Account Category L')
577 : ($ca->{category} eq 'I') ? $locale->text('Account Category I')
578 : ($ca->{category} eq 'Q') ? $locale->text('Account Category Q')
579 : ($ca->{category} eq 'C') ? $locale->text('Account Category C')
580 : ($ca->{category} eq 'G') ? $locale->text('Account Category G')
581 : $locale->text('Unknown Category') . ': ' . $ca->{category};
584 $form->{title} = $locale->text('Chart of Accounts');
586 print $form->ajax_response_header, $form->parse_html_template('am/list_account_details');
588 $main::lxdebug->leave_sub();
593 $main::lxdebug->enter_sub();
595 my $form = $main::form;
596 my %myconfig = %main::myconfig;
597 my $locale = $main::locale;
599 $main::auth->assert('config');
601 $form->{title} = $locale->text('Delete Account');
604 qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id rndgain_accno_id rndloss_accno_id)
606 if ($form->{id} == $form->{$id}) {
607 $form->error($locale->text('Cannot delete default account!'));
611 $form->redirect($locale->text('Account deleted!'))
612 if (AM->delete_account(\%myconfig, \%$form));
613 $form->error($locale->text('Cannot delete account!'));
615 $main::lxdebug->leave_sub();
618 sub _build_cfg_options {
619 my $form = $main::form;
620 my %myconfig = %main::myconfig;
623 my $array = uc($idx) . 'S';
625 $form->{$array} = [];
626 foreach my $item (@_) {
627 push @{ $form->{$array} }, {
630 'selected' => $item eq $myconfig{$idx},
636 $main::lxdebug->enter_sub();
638 my $form = $main::form;
639 my %myconfig = %main::myconfig;
640 my $locale = $main::locale;
642 _build_cfg_options('dateformat', qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd));
643 _build_cfg_options('timeformat', qw(hh:mm hh:mm:ss));
644 _build_cfg_options('numberformat', ('1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00"));
647 if ($::lx_office_conf{print_templates}->{opendocument}
648 && $::lx_office_conf{applications}->{openofficeorg_writer} && (-x $::lx_office_conf{applications}->{openofficeorg_writer})
649 && $::lx_office_conf{applications}->{xvfb} && (-x $::lx_office_conf{applications}->{xvfb})) {
650 push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
651 "value" => "opendocument_pdf" });
653 if ($::lx_office_conf{print_templates}->{latex}) {
654 push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
656 push(@formats, { "name" => "HTML", "value" => "html" });
657 if ($::lx_office_conf{print_templates}->{latex}) {
658 push(@formats, { "name" => $locale->text("Postscript"),
659 "value" => "postscript" });
661 if ($::lx_office_conf{print_templates}->{opendocument}) {
662 push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
663 "value" => "opendocument" });
666 if (!$myconfig{"template_format"}) {
667 $myconfig{"template_format"} = "pdf";
669 $form->{TEMPLATE_FORMATS} = [];
670 foreach my $item (@formats) {
671 push @{ $form->{TEMPLATE_FORMATS} }, {
672 'name' => $item->{name},
673 'value' => $item->{value},
674 'selected' => $item->{value} eq $myconfig{template_format},
678 if (!$myconfig{"default_media"}) {
679 $myconfig{"default_media"} = "screen";
682 my %selected = ($myconfig{"default_media"} => "selected");
684 { 'name' => $locale->text('Screen'), 'value' => 'screen', 'selected' => $selected{screen}, },
685 { 'name' => $locale->text('Printer'), 'value' => 'printer', 'selected' => $selected{printer}, },
686 { 'name' => $locale->text('Queue'), 'value' => 'queue', 'selected' => $selected{queue}, },
689 $form->{PRINTERS} = SL::DB::Manager::Printer->get_all_sorted;
691 my %countrycodes = User->country_codes;
693 $form->{COUNTRYCODES} = [];
694 foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
695 push @{ $form->{COUNTRYCODES} }, {
696 'name' => $countrycodes{$countrycode},
697 'value' => $countrycode,
698 'selected' => $countrycode eq $myconfig{countrycode},
702 $form->{STYLESHEETS} = [];
703 foreach my $item (qw(lx-office-erp.css kivitendo.css)) {
704 push @{ $form->{STYLESHEETS} }, {
707 'selected' => $item eq $myconfig{stylesheet},
711 $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
712 $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
713 $form->{todo_cfg} = { TODO->get_user_config('login' => $::myconfig{login}) };
715 $form->{title} = $locale->text('Edit Preferences for #1', $::myconfig{login});
717 setup_am_config_action_bar();
721 $form->{full_signature} = $form->create_email_signature();
723 print $form->parse_html_template('am/config');
725 $main::lxdebug->leave_sub();
728 sub save_preferences {
729 $main::lxdebug->enter_sub();
731 my $form = $main::form;
732 my %myconfig = %main::myconfig;
733 my $locale = $main::locale;
735 $form->{stylesheet} = $form->{usestylesheet};
737 TODO->save_user_config('login' => $::myconfig{login}, %{ $form->{todo_cfg} || { } });
739 if (AM->save_preferences($form)) {
740 if ($::auth->can_change_password()
741 && defined $form->{new_password}
742 && ($form->{new_password} ne '********')) {
743 my $verifier = SL::Auth::PasswordPolicy->new;
744 my $result = $verifier->verify($form->{new_password});
746 if ($result != SL::Auth::PasswordPolicy->OK()) {
747 $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
750 $::auth->change_password($::myconfig{login}, $form->{new_password});
753 $form->redirect($locale->text('Preferences saved!'));
756 $form->error($locale->text('Cannot save preferences!'));
758 $main::lxdebug->leave_sub();
762 $::lxdebug->enter_sub;
763 $::auth->assert('config');
765 $::form->{title} = $::locale->text('Audit Control');
767 AM->closedto(\%::myconfig, $::form);
770 print $::form->parse_html_template('am/audit_control');
772 $::lxdebug->leave_sub;
776 $main::lxdebug->enter_sub();
778 my $form = $main::form;
779 my %myconfig = %main::myconfig;
780 my $locale = $main::locale;
782 $main::auth->assert('config');
784 AM->closebooks(\%myconfig, \%$form);
786 if ($form->{closedto}) {
788 $locale->text('Books closed up to') . " "
789 . $locale->date(\%myconfig, $form->{closedto}, 1));
791 $form->redirect($locale->text('Books are open'));
794 $main::lxdebug->leave_sub();
798 $main::lxdebug->enter_sub();
800 my $form = $main::form;
801 my %myconfig = %main::myconfig;
802 my $locale = $main::locale;
804 $main::auth->assert('config');
806 my $units = AM->retrieve_units(\%myconfig, $form, "resolved_");
807 AM->units_in_use(\%myconfig, $form, $units);
808 map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
810 my @languages = AM->language(\%myconfig, $form, 1);
812 my @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
815 foreach (@unit_list) {
816 $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
817 $_->{"UNITLANGUAGES"} = [];
818 foreach my $lang (@languages) {
819 push(@{ $_->{"UNITLANGUAGES"} },
821 "unit" => $_->{"name"},
822 "language_id" => $lang->{"id"},
823 "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
824 "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
830 $units = AM->retrieve_units(\%myconfig, $form);
831 my $ddbox = AM->unit_select_data($units, undef, 1);
833 $form->{"title"} = $locale->text("Add and edit units");
835 print($form->parse_html_template("am/edit_units",
836 { "UNITS" => \@unit_list,
837 "NEW_BASE_UNIT_DDBOX" => $ddbox,
838 "LANGUAGES" => \@languages,
841 $main::lxdebug->leave_sub();
845 $main::lxdebug->enter_sub();
847 my $form = $main::form;
848 my %myconfig = %main::myconfig;
849 my $locale = $main::locale;
851 $main::auth->assert('config');
853 $form->isblank("new_name", $locale->text("The name is missing."));
854 my $units = AM->retrieve_units(\%myconfig, $form);
855 my $all_units = AM->retrieve_units(\%myconfig, $form);
856 $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
858 my ($base_unit, $factor);
859 if ($form->{"new_base_unit"}) {
860 $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
862 $form->isblank("new_factor", $locale->text("The factor is missing."));
863 $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
864 $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
865 $base_unit = $form->{"new_base_unit"};
869 foreach my $lang (AM->language(\%myconfig, $form, 1)) {
870 next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
871 push(@languages, { "id" => $lang->{"id"},
872 "localized" => $form->{"new_localized_$lang->{id}"},
873 "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
877 AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, \@languages);
879 $form->{"saved_message"} = $locale->text("The unit has been saved.");
883 $main::lxdebug->leave_sub();
886 sub set_unit_languages {
887 $main::lxdebug->enter_sub();
889 my $form = $main::form;
891 $main::auth->assert('config');
893 my ($unit, $languages, $idx) = @_;
895 $unit->{"LANGUAGES"} = [];
897 foreach my $lang (@{$languages}) {
898 push(@{ $unit->{"LANGUAGES"} },
899 { "id" => $lang->{"id"},
900 "localized" => $form->{"localized_${idx}_$lang->{id}"},
901 "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
905 $main::lxdebug->leave_sub();
909 $main::lxdebug->enter_sub();
911 my $form = $main::form;
912 my %myconfig = %main::myconfig;
913 my $locale = $main::locale;
915 $main::auth->assert('config');
917 my $old_units = AM->retrieve_units(\%myconfig, $form, "resolved_");
918 AM->units_in_use(\%myconfig, $form, $old_units);
920 my @languages = AM->language(\%myconfig, $form, 1);
923 my @delete_units = ();
924 foreach my $i (1..($form->{"rowcount"} * 1)) {
925 my $old_unit = $old_units->{$form->{"old_name_$i"}};
927 $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
930 if ($form->{"unchangeable_$i"}) {
931 $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
932 $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
933 set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
937 if ($old_unit->{"in_use"}) {
938 $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
941 if ($form->{"delete_$i"}) {
942 push(@delete_units, $old_unit->{"name"});
946 $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
948 $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
949 my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
950 $new_units->{$form->{"name_$i"}} = \%h;
951 $new_units->{$form->{"name_$i"}}->{"row"} = $i;
952 set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
955 foreach my $unit (values(%{$new_units})) {
956 next unless ($unit->{"old_name"});
957 if ($unit->{"base_unit"}) {
958 $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"}))
959 unless (defined($new_units->{$unit->{"base_unit"}}));
960 $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
961 $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
963 $unit->{"base_unit"} = undef;
964 $unit->{"factor"} = undef;
968 foreach my $unit (values(%{$new_units})) {
969 next if ($unit->{"unchanged_unit"});
971 map({ $_->{"seen"} = 0; } values(%{$new_units}));
972 my $new_unit = $unit;
973 while ($new_unit->{"base_unit"}) {
974 $new_unit->{"seen"} = 1;
975 $new_unit = $new_units->{$new_unit->{"base_unit"}};
976 if ($new_unit->{"seen"}) {
977 $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, " .
978 "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
983 AM->save_units(\%myconfig, $form, $new_units, \@delete_units);
985 $form->{"saved_message"} = $locale->text("The units have been saved.");
989 $main::lxdebug->leave_sub();
992 sub show_history_search {
993 $main::lxdebug->enter_sub();
995 my $form = $main::form;
996 my $locale = $main::locale;
998 $main::auth->assert('config');
1000 $form->{title} = $locale->text("History Search");
1003 print $form->parse_html_template("common/search_history");
1005 $main::lxdebug->leave_sub();
1008 sub show_am_history {
1009 $main::lxdebug->enter_sub();
1011 my $form = $main::form;
1012 my %myconfig = %main::myconfig;
1013 my $locale = $main::locale;
1015 $main::auth->assert('config');
1017 my $callback = build_std_url(qw(action einschraenkungen fromdate todate mitarbeiter searchid what2search));
1018 $form->{order} ||= 'h.itime--1';
1020 # my %search = ( "Artikelnummer" => "parts",
1021 # "Kundennummer" => "customer",
1022 # "Lieferantennummer" => "vendor",
1023 # "Projektnummer" => "project",
1024 # "Auftragsnummer" => "oe",
1025 # "Angebotsnummer" => "oe",
1026 # "Eingangsrechnungnummer" => "ap",
1027 # "Ausgangsrechnungnummer" => "ar",
1028 # "Mahnungsnummer" => "dunning",
1029 # "Buchungsnummer" => "gl",
1032 my %searchNo = ( "Artikelnummer" => "partnumber",
1033 "Kundennummer" => "customernumber",
1034 "Lieferantennummer" => "vendornumber",
1035 "Projektnummer" => "projectnumber",
1036 "Auftragsnummer" => "ordnumber",
1037 "Angebotsnummer" => "quonumber",
1038 "Eingangsrechnungnummer" => "invnumber",
1039 "Ausgangsrechnungnummer" => "invnumber",
1040 "Mahnungsnummer" => "dunning_id",
1041 "Buchungsnummer" => "gltransaction"
1044 my $dbh = $form->dbconnect(\%myconfig);
1047 $restriction = qq| AND (| . join(' OR ', map { " addition = " . $dbh->quote($_) } split(m/\,/, $form->{einschraenkungen})) . qq|)| if $form->{einschraenkungen};
1048 $restriction .= qq| AND h.itime::date >= | . conv_dateq($form->{fromdate}) if $form->{fromdate};
1049 $restriction .= qq| AND h.itime::date <= | . conv_dateq($form->{todate}) if $form->{todate};
1050 if ($form->{mitarbeiter} =~ m/^\d+$/) {
1051 $restriction .= qq| AND employee_id = | . $form->{mitarbeiter};
1052 } elsif ($form->{mitarbeiter}) {
1053 $restriction .= qq| AND employee_id = (SELECT id FROM employee WHERE name ILIKE | . $dbh->quote('%' . $form->{mitarbeiter} . '%') . qq|)|;
1056 my $snumbers_where = '';
1058 if ($form->{'searchid'}) {
1059 $snumbers_where = ' WHERE snumbers = ?';
1060 $snumbers_value = $searchNo{$form->{'what2search'}} . '_' . $form->{'searchid'};
1062 $snumbers_where = ' WHERE snumbers ~ ?';
1063 $snumbers_value = '^' . $searchNo{$form->{'what2search'}};
1065 my $query = qq|SELECT trans_id AS id FROM history_erp $snumbers_where|;
1067 my @ids = grep { $_ * 1 } selectall_array_query($form, $dbh, $query, $snumbers_value);
1068 my $daten .= shift @ids;
1069 if (scalar(@ids) > 0 ) {
1070 $daten .= ' OR trans_id IN (' . join(',', @ids) . ')';
1072 my ($sort, $sortby) = split(/\-\-/, $form->{order});
1073 $sort =~ s/.*\.(.*)$/$1/;
1075 $form->{title} = $locale->text("History Search");
1078 print $form->parse_html_template("common/show_history",
1079 { "DATEN" => $form->get_history($dbh, $daten, $restriction, $form->{order}),
1080 "SUCCESS" => ($form->get_history($dbh, $daten, $restriction, $form->{order}) ne "0"),
1083 uc($sort) . "BY" => $sortby,
1084 'callback' => $callback,
1088 $main::lxdebug->leave_sub();
1092 $main::lxdebug->enter_sub();
1094 my $form = $main::form;
1095 my $locale = $main::locale;
1097 $main::auth->assert('config');
1099 $form->{title} = $locale->text('Add');
1101 $form->{callback} ||= "am.pl?action=add_tax";
1103 _get_taxaccount_selection();
1106 $form->{liability} = 1;
1107 $form->{equity} = 1;
1108 $form->{revenue} = 1;
1109 $form->{expense} = 1;
1114 my $parameters_ref = {
1115 # ChartTypeIsAccount => $ChartTypeIsAccount,
1116 LANGUAGES => SL::DB::Manager::Language->get_all_sorted,
1119 # Ausgabe des Templates
1120 print($form->parse_html_template('am/edit_tax', $parameters_ref));
1122 $main::lxdebug->leave_sub();
1126 $main::lxdebug->enter_sub();
1128 my $form = $main::form;
1129 my %myconfig = %main::myconfig;
1130 my $locale = $main::locale;
1132 $main::auth->assert('config');
1134 $form->{title} = $locale->text('Edit');
1136 AM->get_tax(\%myconfig, \%$form);
1138 _get_taxaccount_selection();
1140 $form->{asset} = $form->{chart_categories} =~ 'A' ? 1 : 0;
1141 $form->{liability} = $form->{chart_categories} =~ 'L' ? 1 : 0;
1142 $form->{equity} = $form->{chart_categories} =~ 'Q' ? 1 : 0;
1143 $form->{revenue} = $form->{chart_categories} =~ 'I' ? 1 : 0;
1144 $form->{expense} = $form->{chart_categories} =~ 'E' ? 1 : 0;
1145 $form->{costs} = $form->{chart_categories} =~ 'C' ? 1 : 0;
1147 $form->{rate} = $form->format_amount(\%myconfig, $form->{rate}, 2);
1151 my $parameters_ref = {
1152 LANGUAGES => SL::DB::Manager::Language->get_all_sorted,
1153 TAX => SL::DB::Manager::Tax->find_by(id => $form->{id}),
1156 # Ausgabe des Templates
1157 print($form->parse_html_template('am/edit_tax', $parameters_ref));
1159 $main::lxdebug->leave_sub();
1163 $main::lxdebug->enter_sub();
1165 my $form = $main::form;
1166 my %myconfig = %main::myconfig;
1167 my $locale = $main::locale;
1169 $main::auth->assert('config');
1171 AM->taxes(\%myconfig, \%$form);
1173 map { $_->{rate} = $form->format_amount(\%myconfig, $_->{rate}, 2) } @{ $form->{TAX} };
1175 $form->{callback} = build_std_url('action=list_tax');
1177 $form->{title} = $locale->text('Tax-O-Matic');
1181 my $parameters_ref = {
1184 # Ausgabe des Templates
1185 print($form->parse_html_template('am/list_tax', $parameters_ref));
1187 $main::lxdebug->leave_sub();
1190 sub _get_taxaccount_selection{
1191 $main::lxdebug->enter_sub();
1193 my $form = $main::form;
1194 my %myconfig = %main::myconfig;
1196 $main::auth->assert('config');
1198 AM->get_tax_accounts(\%myconfig, \%$form);
1200 map { $_->{selected} = $form->{chart_id} == $_->{id} } @{ $form->{ACCOUNTS} };
1202 $main::lxdebug->leave_sub();
1206 $main::lxdebug->enter_sub();
1208 my $form = $main::form;
1209 my %myconfig = %main::myconfig;
1210 my $locale = $main::locale;
1212 $main::auth->assert('config');
1214 $form->error($locale->text('Taxkey missing!')) unless length($form->{taxkey}) != 0;
1215 $form->error($locale->text('Taxdescription missing!')) unless length($form->{taxdescription}) != 0;
1216 $form->error($locale->text('Taxrate missing!')) unless length($form->{rate}) != 0;
1218 $form->{rate} = $form->parse_amount(\%myconfig, $form->{rate});
1220 if ($form->{taxkey} == 0 and $form->{rate} > 0) {
1221 $form->error($locale->text('Taxkey 0 is reserved for rate 0'));
1224 if ( $form->{rate} < 0 || $form->{rate} >= 100 ) {
1225 $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1228 if ( $form->{rate} <= 0.99 && $form->{rate} > 0 ) {
1229 $form->error($locale->text('Tax Percent is a number between 0 and 100'));
1232 my @translation_keys = grep { $_ =~ '^translation_\d+' } keys %$form;
1233 $form->{translations} = { map { $_ =~ '^translation_(\d+)'; $1 => $form->{$_} } @translation_keys };
1235 AM->save_tax(\%myconfig, \%$form);
1236 $form->redirect($locale->text('Tax saved!'));
1238 $main::lxdebug->leave_sub();
1242 $main::lxdebug->enter_sub();
1244 my $form = $main::form;
1245 my %myconfig = %main::myconfig;
1246 my $locale = $main::locale;
1248 $main::auth->assert('config');
1250 AM->delete_tax(\%myconfig, \%$form);
1251 $form->redirect($locale->text('Tax deleted!'));
1253 $main::lxdebug->leave_sub();
1257 $main::lxdebug->enter_sub();
1259 my $form = $main::form;
1260 my $locale = $main::locale;
1262 $main::auth->assert('config');
1264 $form->{title} = $locale->text('Add Warehouse');
1265 $form->{callback} ||= build_std_url('action=add_warehouse');
1268 print $form->parse_html_template('am/edit_warehouse');
1270 $main::lxdebug->leave_sub();
1273 sub edit_warehouse {
1274 $main::lxdebug->enter_sub();
1276 my $form = $main::form;
1277 my %myconfig = %main::myconfig;
1278 my $locale = $main::locale;
1280 $main::auth->assert('config');
1282 AM->get_warehouse(\%myconfig, $form);
1284 $form->get_lists('employees' => 'EMPLOYEES');
1286 $form->{title} = $locale->text('Edit Warehouse');
1287 $form->{callback} ||= build_std_url('action=list_warehouses');
1290 print $form->parse_html_template('am/edit_warehouse');
1292 $main::lxdebug->leave_sub();
1295 sub list_warehouses {
1296 $main::lxdebug->enter_sub();
1298 my $form = $main::form;
1299 my %myconfig = %main::myconfig;
1300 my $locale = $main::locale;
1302 $main::auth->assert('config');
1304 AM->get_all_warehouses(\%myconfig, $form);
1306 $form->{callback} = build_std_url('action=list_warehouses');
1307 $form->{title} = $locale->text('Warehouses');
1308 $form->{url_base} = build_std_url('callback');
1311 print $form->parse_html_template('am/list_warehouses');
1313 $main::lxdebug->leave_sub();
1316 sub save_warehouse {
1317 $main::lxdebug->enter_sub();
1319 my $form = $main::form;
1320 my %myconfig = %main::myconfig;
1321 my $locale = $main::locale;
1323 $main::auth->assert('config');
1325 $form->isblank("description", $locale->text('Description missing!'));
1327 $form->{number_of_new_bins} = $form->parse_amount(\%myconfig, $form->{number_of_new_bins});
1329 AM->save_warehouse(\%myconfig, $form);
1331 $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse saved.')) if ($form->{callback});
1333 $form->redirect($locale->text('Warehouse saved.'));
1335 $main::lxdebug->leave_sub();
1338 sub delete_warehouse {
1339 $main::lxdebug->enter_sub();
1341 my $form = $main::form;
1342 my %myconfig = %main::myconfig;
1343 my $locale = $main::locale;
1345 $main::auth->assert('config');
1347 if (!$form->{confirmed}) {
1348 $form->{title} = $locale->text('Confirmation');
1351 print $form->parse_html_template('am/confirm_delete_warehouse');
1352 $::dispatcher->end_request;
1355 if (AM->delete_warehouse(\%myconfig, $form)) {
1356 $form->{callback} .= '&saved_message=' . E($locale->text('Warehouse deleted.')) if ($form->{callback});
1357 $form->redirect($locale->text('Warehouse deleted.'));
1360 $form->error($locale->text('The warehouse could not be deleted because it has already been used.'));
1363 $main::lxdebug->leave_sub();
1367 $main::lxdebug->enter_sub();
1369 my $form = $main::form;
1370 my %myconfig = %main::myconfig;
1371 my $locale = $main::locale;
1373 $main::auth->assert('config');
1375 AM->save_bins(\%myconfig, $form);
1377 $form->{callback} .= '&saved_message=' . E($locale->text('Bins saved.')) if ($form->{callback});
1379 $form->redirect($locale->text('Bins saved.'));
1381 $main::lxdebug->leave_sub();
1384 sub setup_am_config_action_bar {
1387 for my $bar ($::request->layout->get('actionbar')) {
1391 submit => [ '#form', { action => "save_preferences" } ],
1392 accesskey => 'enter',