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
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 # Antti Kaihola <akaihola@siba.fi>
17 # Moritz Bunkus (tex code)
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #======================================================================
32 # Utilities for parsing forms
33 # and supporting routines for linking account numbers
34 # used in AR, AP and IS, IR modules
36 #======================================================================
59 use List::Util qw(first max min sum);
60 use List::MoreUtils qw(any apply);
67 disconnect_standard_dbh();
70 sub disconnect_standard_dbh {
71 return unless $standard_dbh;
72 $standard_dbh->disconnect();
77 $main::lxdebug->enter_sub(2);
83 my @tokens = split /((?:\[\+?\])?(?:\.|$))/, $key;
88 $curr = \ $self->{ shift @tokens };
92 my $sep = shift @tokens;
93 my $key = shift @tokens;
95 $curr = \ $$curr->[++$#$$curr], next if $sep eq '[]';
96 $curr = \ $$curr->[max 0, $#$$curr] if $sep eq '[].';
97 $curr = \ $$curr->[++$#$$curr] if $sep eq '[+].';
98 $curr = \ $$curr->{$key}
103 $main::lxdebug->leave_sub(2);
109 $main::lxdebug->enter_sub(2);
114 my @pairs = split(/&/, $input);
117 my ($key, $value) = split(/=/, $_, 2);
118 $self->_store_value($self->unescape($key), $self->unescape($value)) if ($key);
121 $main::lxdebug->leave_sub(2);
124 sub _request_to_hash {
125 $main::lxdebug->enter_sub(2);
130 if (!$ENV{'CONTENT_TYPE'}
131 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
133 $self->_input_to_hash($input);
135 $main::lxdebug->leave_sub(2);
139 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
141 my $boundary = '--' . $1;
143 foreach my $line (split m/\n/, $input) {
144 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
146 if (($line eq $boundary) || ($line eq "$boundary\r")) {
147 ${ $previous } =~ s|\r?\n$|| if $previous;
153 $content_type = "text/plain";
160 next unless $boundary_found;
162 if (!$headers_done) {
163 $line =~ s/[\r\n]*$//;
170 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
171 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
173 substr $line, $-[0], $+[0] - $-[0], "";
176 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
178 substr $line, $-[0], $+[0] - $-[0], "";
181 $previous = $self->_store_value($name, '') if ($name);
182 $self->{FILENAME} = $filename if ($filename);
187 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
194 next unless $previous;
196 ${ $previous } .= "${line}\n";
199 ${ $previous } =~ s|\r?\n$|| if $previous;
201 $main::lxdebug->leave_sub(2);
204 sub _recode_recursively {
205 $main::lxdebug->enter_sub();
206 my ($iconv, $param) = @_;
208 if (any { ref $param eq $_ } qw(Form HASH)) {
209 foreach my $key (keys %{ $param }) {
210 if (!ref $param->{$key}) {
211 # Workaround for a bug: converting $param->{$key} directly
212 # leads to 'undef'. I don't know why. Converting a copy works,
214 $param->{$key} = $iconv->convert("" . $param->{$key});
216 _recode_recursively($iconv, $param->{$key});
220 } elsif (ref $param eq 'ARRAY') {
221 foreach my $idx (0 .. scalar(@{ $param }) - 1) {
222 if (!ref $param->[$idx]) {
223 # Workaround for a bug: converting $param->[$idx] directly
224 # leads to 'undef'. I don't know why. Converting a copy works,
226 $param->[$idx] = $iconv->convert("" . $param->[$idx]);
228 _recode_recursively($iconv, $param->[$idx]);
232 $main::lxdebug->leave_sub();
236 $main::lxdebug->enter_sub();
242 if ($LXDebug::watch_form) {
243 require SL::Watchdog;
244 tie %{ $self }, 'SL::Watchdog';
249 $self->_input_to_hash($ENV{QUERY_STRING}) if $ENV{QUERY_STRING};
250 $self->_input_to_hash($ARGV[0]) if @ARGV && $ARGV[0];
252 if ($ENV{CONTENT_LENGTH}) {
254 read STDIN, $content, $ENV{CONTENT_LENGTH};
255 $self->_request_to_hash($content);
258 my $db_charset = $main::dbcharset;
259 $db_charset ||= Common::DEFAULT_CHARSET;
261 my $encoding = $self->{INPUT_ENCODING} || $db_charset;
262 delete $self->{INPUT_ENCODING};
264 _recode_recursively(SL::Iconv->new($encoding, $db_charset), $self);
266 $self->{action} = lc $self->{action};
267 $self->{action} =~ s/( |-|,|\#)/_/g;
269 #$self->{version} = "2.6.1"; # Old hardcoded but secure style
270 open VERSION_FILE, "VERSION"; # New but flexible code reads version from VERSION-file
271 $self->{version} = <VERSION_FILE>;
273 $self->{version} =~ s/[^0-9A-Za-z\.\_\-]//g; # only allow numbers, letters, points, underscores and dashes. Prevents injecting of malicious code.
275 $main::lxdebug->leave_sub();
280 sub _flatten_variables_rec {
281 $main::lxdebug->enter_sub(2);
290 if ('' eq ref $curr->{$key}) {
291 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
293 } elsif ('HASH' eq ref $curr->{$key}) {
294 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
295 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
299 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
300 my $first_array_entry = 1;
302 foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
303 push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
304 $first_array_entry = 0;
309 $main::lxdebug->leave_sub(2);
314 sub flatten_variables {
315 $main::lxdebug->enter_sub(2);
323 push @variables, $self->_flatten_variables_rec($self, '', $_);
326 $main::lxdebug->leave_sub(2);
331 sub flatten_standard_variables {
332 $main::lxdebug->enter_sub(2);
335 my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
339 foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
340 push @variables, $self->_flatten_variables_rec($self, '', $_);
343 $main::lxdebug->leave_sub(2);
349 $main::lxdebug->enter_sub();
355 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
357 $main::lxdebug->leave_sub();
361 $main::lxdebug->enter_sub(2);
364 my $password = $self->{password};
366 $self->{password} = 'X' x 8;
368 local $Data::Dumper::Sortkeys = 1;
369 my $output = Dumper($self);
371 $self->{password} = $password;
373 $main::lxdebug->leave_sub(2);
379 $main::lxdebug->enter_sub(2);
381 my ($self, $str) = @_;
383 $str = Encode::encode('utf-8-strict', $str) if $::locale->is_utf8;
384 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
386 $main::lxdebug->leave_sub(2);
392 $main::lxdebug->enter_sub(2);
394 my ($self, $str) = @_;
399 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
401 $main::lxdebug->leave_sub(2);
407 $main::lxdebug->enter_sub();
408 my ($self, $str) = @_;
410 if ($str && !ref($str)) {
411 $str =~ s/\"/"/g;
414 $main::lxdebug->leave_sub();
420 $main::lxdebug->enter_sub();
421 my ($self, $str) = @_;
423 if ($str && !ref($str)) {
424 $str =~ s/"/\"/g;
427 $main::lxdebug->leave_sub();
433 $main::lxdebug->enter_sub();
437 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
439 for (sort keys %$self) {
440 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
441 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
444 $main::lxdebug->leave_sub();
448 $main::lxdebug->enter_sub();
450 $main::lxdebug->show_backtrace();
452 my ($self, $msg) = @_;
453 if ($ENV{HTTP_USER_AGENT}) {
455 $self->show_generic_error($msg);
458 print STDERR "Error: $msg\n";
462 $main::lxdebug->leave_sub();
466 $main::lxdebug->enter_sub();
468 my ($self, $msg) = @_;
470 if ($ENV{HTTP_USER_AGENT}) {
473 if (!$self->{header}) {
479 <p class="message_ok"><b>$msg</b></p>
481 <script type="text/javascript">
483 // If JavaScript is enabled, the whole thing will be reloaded.
484 // The reason is: When one changes his menu setup (HTML / XUL / CSS ...)
485 // it now loads the correct code into the browser instead of do nothing.
486 setTimeout("top.frames.location.href='login.pl'",500);
495 if ($self->{info_function}) {
496 &{ $self->{info_function} }($msg);
502 $main::lxdebug->leave_sub();
505 # calculates the number of rows in a textarea based on the content and column number
506 # can be capped with maxrows
508 $main::lxdebug->enter_sub();
509 my ($self, $str, $cols, $maxrows, $minrows) = @_;
513 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
516 $main::lxdebug->leave_sub();
518 return max(min($rows, $maxrows), $minrows);
522 $main::lxdebug->enter_sub();
524 my ($self, $msg) = @_;
526 $self->error("$msg\n" . $DBI::errstr);
528 $main::lxdebug->leave_sub();
532 $main::lxdebug->enter_sub();
534 my ($self, $name, $msg) = @_;
537 foreach my $part (split m/\./, $name) {
538 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
541 $curr = $curr->{$part};
544 $main::lxdebug->leave_sub();
547 sub _get_request_uri {
550 return URI->new($ENV{HTTP_REFERER})->canonical() if $ENV{HTTP_X_FORWARDED_FOR};
552 my $scheme = $ENV{HTTPS} && (lc $ENV{HTTPS} eq 'on') ? 'https' : 'http';
553 my $port = $ENV{SERVER_PORT} || '';
554 $port = undef if (($scheme eq 'http' ) && ($port == 80))
555 || (($scheme eq 'https') && ($port == 443));
557 my $uri = URI->new("${scheme}://");
558 $uri->scheme($scheme);
560 $uri->host($ENV{HTTP_HOST} || $ENV{SERVER_ADDR});
561 $uri->path_query($ENV{REQUEST_URI});
567 sub _add_to_request_uri {
570 my $relative_new_path = shift;
571 my $request_uri = shift || $self->_get_request_uri;
572 my $relative_new_uri = URI->new($relative_new_path);
573 my @request_segments = $request_uri->path_segments;
575 my $new_uri = $request_uri->clone;
576 $new_uri->path_segments(@request_segments[0..scalar(@request_segments) - 2], $relative_new_uri->path_segments);
581 sub create_http_response {
582 $main::lxdebug->enter_sub();
587 my $cgi = $main::cgi;
588 $cgi ||= CGI->new('');
591 if (defined $main::auth) {
592 my $uri = $self->_get_request_uri;
593 my @segments = $uri->path_segments;
595 $uri->path_segments(@segments);
597 my $session_cookie_value = $main::auth->get_session_id();
598 $session_cookie_value ||= 'NO_SESSION';
600 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
601 '-value' => $session_cookie_value,
602 '-path' => $uri->path,
603 '-secure' => $ENV{HTTPS});
606 my %cgi_params = ('-type' => $params{content_type});
607 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
609 my $output = $cgi->header('-cookie' => $session_cookie,
612 $main::lxdebug->leave_sub();
619 $::lxdebug->enter_sub;
621 # extra code is currently only used by menuv3 and menuv4 to set their css.
622 # it is strongly deprecated, and will be changed in a future version.
623 my ($self, $extra_code) = @_;
624 my $db_charset = $::dbcharset || Common::DEFAULT_CHARSET;
627 $::lxdebug->leave_sub and return if !$ENV{HTTP_USER_AGENT} || $self->{header}++;
629 $self->{favicon} ||= "favicon.ico";
630 $self->{titlebar} = "$self->{title} - $self->{titlebar}" if $self->{title};
633 if ($self->{refresh_url} || $self->{refresh_time}) {
634 my $refresh_time = $self->{refresh_time} || 3;
635 my $refresh_url = $self->{refresh_url} || $ENV{REFERER};
636 push @header, "<meta http-equiv='refresh' content='$refresh_time;$refresh_url'>";
639 push @header, "<link rel='stylesheet' href='css/$_' type='text/css' title='Lx-Office stylesheet'>"
640 for grep { -f "css/$_" } apply { s|.*/|| } $self->{stylesheet}, $self->{stylesheets};
642 push @header, "<style type='text/css'>\@page { size:landscape; }</style>" if $self->{landscape};
643 push @header, "<link rel='shortcut icon' href='$self->{favicon}' type='image/x-icon'>" if -f $self->{favicon};
644 push @header, '<script type="text/javascript" src="js/jquery.js"></script>',
645 '<script type="text/javascript" src="js/common.js"></script>',
646 '<style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>',
647 '<script type="text/javascript" src="js/jscalendar/calendar.js"></script>',
648 '<script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>',
649 '<script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>',
650 '<script type="text/javascript" src="js/part_selection.js"></script>';
651 push @header, $self->{javascript} if $self->{javascript};
652 push @header, map { $_->show_javascript } @{ $self->{AJAX} || [] };
653 push @header, "<script type='text/javascript'>function fokus(){ document.$self->{fokus}.focus(); }</script>" if $self->{fokus};
654 push @header, sprintf "<script type='text/javascript'>top.document.title='%s';</script>",
655 join ' - ', grep $_, $self->{title}, $self->{login}, $::myconfig{dbname}, $self->{version} if $self->{title};
658 print $self->create_http_response(content_type => 'text/html', charset => $db_charset);
659 print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>\n"
660 if $ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/; # Other browsers may choke on menu scripts with DOCTYPE.
664 <meta http-equiv="Content-Type" content="text/html; charset=$db_charset">
665 <title>$self->{titlebar}</title>
667 print " $_\n" for @header;
669 <link rel="stylesheet" href="css/jquery.autocomplete.css" type="text/css" />
670 <meta name="robots" content="noindex,nofollow" />
671 <script type="text/javascript" src="js/highlight_input.js"></script>
672 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
673 <script type="text/javascript" src="js/tabcontent.js">
675 /***********************************************
676 * Tab Content script v2.2- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
677 * This notice MUST stay intact for legal use
678 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
679 ***********************************************/
687 $::lxdebug->leave_sub;
690 sub ajax_response_header {
691 $main::lxdebug->enter_sub();
695 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
696 my $cgi = $main::cgi || CGI->new('');
697 my $output = $cgi->header('-charset' => $db_charset);
699 $main::lxdebug->leave_sub();
704 sub redirect_header {
708 my $base_uri = $self->_get_request_uri;
709 my $new_uri = URI->new_abs($new_url, $base_uri);
711 die "Headers already sent" if $::self->{header};
714 my $cgi = $main::cgi || CGI->new('');
715 return $cgi->redirect($new_uri);
718 sub set_standard_title {
719 $::lxdebug->enter_sub;
722 $self->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $self->{version}";
723 $self->{titlebar} .= "- $::myconfig{name}" if $::myconfig{name};
724 $self->{titlebar} .= "- $::myconfig{dbname}" if $::myconfig{name};
726 $::lxdebug->leave_sub;
729 sub _prepare_html_template {
730 $main::lxdebug->enter_sub();
732 my ($self, $file, $additional_params) = @_;
735 if (!%::myconfig || !$::myconfig{"countrycode"}) {
736 $language = $main::language;
738 $language = $main::myconfig{"countrycode"};
740 $language = "de" unless ($language);
742 if (-f "templates/webpages/${file}.html") {
743 if ((-f ".developer") && ((stat("templates/webpages/${file}.html"))[9] > (stat("locale/${language}/all"))[9])) {
744 my $info = "Developer information: templates/webpages/${file}.html is newer than the translation file locale/${language}/all.\n" .
745 "Please re-run 'locales.pl' in 'locale/${language}'.";
746 print(qq|<pre>$info</pre>|);
750 $file = "templates/webpages/${file}.html";
753 my $info = "Web page template '${file}' not found.\n" .
754 "Please re-run 'locales.pl' in 'locale/${language}'.";
755 print(qq|<pre>$info</pre>|);
759 if ($self->{"DEBUG"}) {
760 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
763 if ($additional_params->{"DEBUG"}) {
764 $additional_params->{"DEBUG"} =
765 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
768 if (%main::myconfig) {
769 $::myconfig{jsc_dateformat} = apply {
773 } $::myconfig{"dateformat"};
774 $additional_params->{"myconfig"} ||= \%::myconfig;
775 map { $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys %::myconfig;
778 $additional_params->{"conf_dbcharset"} = $main::dbcharset;
779 $additional_params->{"conf_webdav"} = $main::webdav;
780 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
781 $additional_params->{"conf_latex_templates"} = $main::latex;
782 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
783 $additional_params->{"conf_vertreter"} = $main::vertreter;
784 $additional_params->{"conf_show_best_before"} = $main::show_best_before;
786 if (%main::debug_options) {
787 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
790 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
791 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
792 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
796 $main::lxdebug->leave_sub();
801 sub parse_html_template {
802 $main::lxdebug->enter_sub();
804 my ($self, $file, $additional_params) = @_;
806 $additional_params ||= { };
808 my $real_file = $self->_prepare_html_template($file, $additional_params);
809 my $template = $self->template || $self->init_template;
811 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
814 $template->process($real_file, $additional_params, \$output) || die $template->error;
816 $main::lxdebug->leave_sub();
824 return if $self->template;
826 return $self->template(Template->new({
831 'PLUGIN_BASE' => 'SL::Template::Plugin',
832 'INCLUDE_PATH' => '.:templates/webpages',
833 'COMPILE_EXT' => '.tcc',
834 'COMPILE_DIR' => $::userspath . '/templates-cache',
840 $self->{template_object} = shift if @_;
841 return $self->{template_object};
844 sub show_generic_error {
845 $main::lxdebug->enter_sub();
847 my ($self, $error, %params) = @_;
850 'title_error' => $params{title},
851 'label_error' => $error,
854 if ($params{action}) {
857 map { delete($self->{$_}); } qw(action);
858 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
860 $add_params->{SHOW_BUTTON} = 1;
861 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
862 $add_params->{VARIABLES} = \@vars;
864 } elsif ($params{back_button}) {
865 $add_params->{SHOW_BACK_BUTTON} = 1;
868 $self->{title} = $params{title} if $params{title};
871 print $self->parse_html_template("generic/error", $add_params);
873 print STDERR "Error: $error\n";
875 $main::lxdebug->leave_sub();
880 sub show_generic_information {
881 $main::lxdebug->enter_sub();
883 my ($self, $text, $title) = @_;
886 'title_information' => $title,
887 'label_information' => $text,
890 $self->{title} = $title if ($title);
893 print $self->parse_html_template("generic/information", $add_params);
895 $main::lxdebug->leave_sub();
900 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
901 # changed it to accept an arbitrary number of triggers - sschoeling
903 $main::lxdebug->enter_sub();
906 my $myconfig = shift;
909 # set dateform for jsscript
912 "dd.mm.yy" => "%d.%m.%Y",
913 "dd-mm-yy" => "%d-%m-%Y",
914 "dd/mm/yy" => "%d/%m/%Y",
915 "mm/dd/yy" => "%m/%d/%Y",
916 "mm-dd-yy" => "%m-%d-%Y",
917 "yyyy-mm-dd" => "%Y-%m-%d",
920 my $ifFormat = defined($dateformats{$myconfig->{"dateformat"}}) ?
921 $dateformats{$myconfig->{"dateformat"}} : "%d.%m.%Y";
928 inputField : "| . (shift) . qq|",
929 ifFormat :"$ifFormat",
930 align : "| . (shift) . qq|",
931 button : "| . (shift) . qq|"
937 <script type="text/javascript">
938 <!--| . join("", @triggers) . qq|//-->
942 $main::lxdebug->leave_sub();
945 } #end sub write_trigger
948 $main::lxdebug->enter_sub();
950 my ($self, $msg) = @_;
952 if (!$self->{callback}) {
958 # my ($script, $argv) = split(/\?/, $self->{callback}, 2);
959 # $script =~ s|.*/||;
960 # $script =~ s|[^a-zA-Z0-9_\.]||g;
961 # exec("perl", "$script", $argv);
963 print $::form->redirect_header($self->{callback});
965 $main::lxdebug->leave_sub();
968 # sort of columns removed - empty sub
970 $main::lxdebug->enter_sub();
972 my ($self, @columns) = @_;
974 $main::lxdebug->leave_sub();
980 $main::lxdebug->enter_sub(2);
982 my ($self, $myconfig, $amount, $places, $dash) = @_;
988 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
990 my $neg = ($amount =~ s/^-//);
991 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
993 if (defined($places) && ($places ne '')) {
999 my ($actual_places) = ($amount =~ /\.(\d+)/);
1000 $actual_places = length($actual_places);
1001 $places = $actual_places > $places ? $actual_places : $places;
1004 $amount = $self->round_amount($amount, $places);
1007 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
1008 my @p = split(/\./, $amount); # split amount at decimal point
1010 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
1013 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
1016 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
1017 ($dash =~ /DRCR/) ? ($neg ? "$amount " . $main::locale->text('DR') : "$amount " . $main::locale->text('CR') ) :
1018 ($neg ? "-$amount" : "$amount" ) ;
1022 $main::lxdebug->leave_sub(2);
1026 sub format_amount_units {
1027 $main::lxdebug->enter_sub();
1032 my $myconfig = \%main::myconfig;
1033 my $amount = $params{amount} * 1;
1034 my $places = $params{places};
1035 my $part_unit_name = $params{part_unit};
1036 my $amount_unit_name = $params{amount_unit};
1037 my $conv_units = $params{conv_units};
1038 my $max_places = $params{max_places};
1040 if (!$part_unit_name) {
1041 $main::lxdebug->leave_sub();
1045 AM->retrieve_all_units();
1046 my $all_units = $main::all_units;
1048 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
1049 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
1052 if (!scalar @{ $conv_units }) {
1053 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
1054 $main::lxdebug->leave_sub();
1058 my $part_unit = $all_units->{$part_unit_name};
1059 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
1061 $amount *= $conv_unit->{factor};
1066 foreach my $unit (@$conv_units) {
1067 my $last = $unit->{name} eq $part_unit->{name};
1069 $num = int($amount / $unit->{factor});
1070 $amount -= $num * $unit->{factor};
1073 if ($last ? $amount : $num) {
1074 push @values, { "unit" => $unit->{name},
1075 "amount" => $last ? $amount / $unit->{factor} : $num,
1076 "places" => $last ? $places : 0 };
1083 push @values, { "unit" => $part_unit_name,
1088 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
1090 $main::lxdebug->leave_sub();
1096 $main::lxdebug->enter_sub(2);
1101 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
1102 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
1103 $input =~ s/\#\#/\#/g;
1105 $main::lxdebug->leave_sub(2);
1113 $main::lxdebug->enter_sub(2);
1115 my ($self, $myconfig, $amount) = @_;
1117 if ( ($myconfig->{numberformat} eq '1.000,00')
1118 || ($myconfig->{numberformat} eq '1000,00')) {
1123 if ($myconfig->{numberformat} eq "1'000.00") {
1129 $main::lxdebug->leave_sub(2);
1131 return ($amount * 1);
1135 $main::lxdebug->enter_sub(2);
1137 my ($self, $amount, $places) = @_;
1140 # Rounding like "Kaufmannsrunden" (see http://de.wikipedia.org/wiki/Rundung )
1142 # Round amounts to eight places before rounding to the requested
1143 # number of places. This gets rid of errors due to internal floating
1144 # point representation.
1145 $amount = $self->round_amount($amount, 8) if $places < 8;
1146 $amount = $amount * (10**($places));
1147 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1149 $main::lxdebug->leave_sub(2);
1151 return $round_amount;
1155 sub parse_template {
1156 $main::lxdebug->enter_sub();
1158 my ($self, $myconfig, $userspath) = @_;
1163 $self->{"cwd"} = getcwd();
1164 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1169 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1170 $template_type = 'OpenDocument';
1171 $ext_for_format = $self->{"format"} =~ m/pdf/ ? 'pdf' : 'odt';
1173 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1174 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1175 $template_type = 'LaTeX';
1176 $ext_for_format = 'pdf';
1178 } elsif (($self->{"format"} =~ /html/i) || (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1179 $template_type = 'HTML';
1180 $ext_for_format = 'html';
1182 } elsif (($self->{"format"} =~ /xml/i) || (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1183 $template_type = 'XML';
1184 $ext_for_format = 'xml';
1186 } elsif ( $self->{"format"} =~ /elster(?:winston|taxbird)/i ) {
1187 $template_type = 'xml';
1189 } elsif ( $self->{"format"} =~ /excel/i ) {
1190 $template_type = 'Excel';
1191 $ext_for_format = 'xls';
1193 } elsif ( defined $self->{'format'}) {
1194 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1196 } elsif ( $self->{'format'} eq '' ) {
1197 $self->error("No Outputformat given: $self->{'format'}");
1199 } else { #Catch the rest
1200 $self->error("Outputformat not defined: $self->{'format'}");
1203 my $template = SL::Template::create(type => $template_type,
1204 file_name => $self->{IN},
1206 myconfig => $myconfig,
1207 userspath => $userspath);
1209 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1210 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1212 if (!$self->{employee_id}) {
1213 map { $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature company address businessnumber co_ustid taxnumber duns);
1216 map { $self->{"${_}"} = $myconfig->{$_}; } qw(co_ustid);
1218 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1220 # OUT is used for the media, screen, printer, email
1221 # for postscript we store a copy in a temporary file
1223 my $prepend_userspath;
1225 if (!$self->{tmpfile}) {
1226 $self->{tmpfile} = "${fileid}.$self->{IN}";
1227 $prepend_userspath = 1;
1230 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1232 $self->{tmpfile} =~ s|.*/||;
1233 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1234 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1236 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1237 $out = $self->{OUT};
1238 $self->{OUT} = ">$self->{tmpfile}";
1244 open OUT, "$self->{OUT}" or $self->error("$self->{OUT} : $!");
1245 $result = $template->parse(*OUT);
1250 $result = $template->parse(*STDOUT);
1255 $self->error("$self->{IN} : " . $template->get_error());
1258 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1260 if ($self->{media} eq 'email') {
1262 my $mail = new Mailer;
1264 map { $mail->{$_} = $self->{$_} }
1265 qw(cc bcc subject message version format);
1266 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1267 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1268 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1269 $mail->{fileid} = "$fileid.";
1270 $myconfig->{signature} =~ s/\r//g;
1272 # if we send html or plain text inline
1273 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1274 $mail->{contenttype} = "text/html";
1276 $mail->{message} =~ s/\r//g;
1277 $mail->{message} =~ s/\n/<br>\n/g;
1278 $myconfig->{signature} =~ s/\n/<br>\n/g;
1279 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1281 open(IN, $self->{tmpfile})
1282 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1284 $mail->{message} .= $_;
1291 if (!$self->{"do_not_attach"}) {
1292 my $attachment_name = $self->{attachment_filename} || $self->{tmpfile};
1293 $attachment_name =~ s/\.(.+?)$/.${ext_for_format}/ if ($ext_for_format);
1294 $mail->{attachments} = [{ "filename" => $self->{tmpfile},
1295 "name" => $attachment_name }];
1298 $mail->{message} =~ s/\r//g;
1299 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1303 my $err = $mail->send();
1304 $self->error($self->cleanup . "$err") if ($err);
1308 $self->{OUT} = $out;
1310 my $numbytes = (-s $self->{tmpfile});
1311 open(IN, $self->{tmpfile})
1312 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1314 $self->{copies} = 1 unless $self->{media} eq 'printer';
1316 chdir("$self->{cwd}");
1317 #print(STDERR "Kopien $self->{copies}\n");
1318 #print(STDERR "OUT $self->{OUT}\n");
1319 for my $i (1 .. $self->{copies}) {
1321 open OUT, $self->{OUT} or $self->error($self->cleanup . "$self->{OUT} : $!");
1322 print OUT while <IN>;
1327 $self->{attachment_filename} = ($self->{attachment_filename})
1328 ? $self->{attachment_filename}
1329 : $self->generate_attachment_filename();
1331 # launch application
1332 print qq|Content-Type: | . $template->get_mime_type() . qq|
1333 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1334 Content-Length: $numbytes
1338 $::locale->with_raw_io(\*STDOUT, sub { print while <IN> });
1349 chdir("$self->{cwd}");
1350 $main::lxdebug->leave_sub();
1353 sub get_formname_translation {
1354 $main::lxdebug->enter_sub();
1355 my ($self, $formname) = @_;
1357 $formname ||= $self->{formname};
1359 my %formname_translations = (
1360 bin_list => $main::locale->text('Bin List'),
1361 credit_note => $main::locale->text('Credit Note'),
1362 invoice => $main::locale->text('Invoice'),
1363 packing_list => $main::locale->text('Packing List'),
1364 pick_list => $main::locale->text('Pick List'),
1365 proforma => $main::locale->text('Proforma Invoice'),
1366 purchase_order => $main::locale->text('Purchase Order'),
1367 request_quotation => $main::locale->text('RFQ'),
1368 sales_order => $main::locale->text('Confirmation'),
1369 sales_quotation => $main::locale->text('Quotation'),
1370 storno_invoice => $main::locale->text('Storno Invoice'),
1371 storno_packing_list => $main::locale->text('Storno Packing List'),
1372 sales_delivery_order => $main::locale->text('Delivery Order'),
1373 purchase_delivery_order => $main::locale->text('Delivery Order'),
1374 dunning => $main::locale->text('Dunning'),
1377 $main::lxdebug->leave_sub();
1378 return $formname_translations{$formname}
1381 sub get_number_prefix_for_type {
1382 $main::lxdebug->enter_sub();
1386 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1387 : ($self->{type} =~ /_quotation$/) ? 'quo'
1388 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1391 $main::lxdebug->leave_sub();
1395 sub get_extension_for_format {
1396 $main::lxdebug->enter_sub();
1399 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1400 : $self->{format} =~ /postscript/i ? ".ps"
1401 : $self->{format} =~ /opendocument/i ? ".odt"
1402 : $self->{format} =~ /excel/i ? ".xls"
1403 : $self->{format} =~ /html/i ? ".html"
1406 $main::lxdebug->leave_sub();
1410 sub generate_attachment_filename {
1411 $main::lxdebug->enter_sub();
1414 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1415 my $prefix = $self->get_number_prefix_for_type();
1417 if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
1418 $attachment_filename .= ' (' . $main::locale->text('Preview') . ')' . $self->get_extension_for_format();
1420 } elsif ($attachment_filename && $self->{"${prefix}number"}) {
1421 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1424 $attachment_filename = "";
1427 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1428 $attachment_filename =~ s|[\s/\\]+|_|g;
1430 $main::lxdebug->leave_sub();
1431 return $attachment_filename;
1434 sub generate_email_subject {
1435 $main::lxdebug->enter_sub();
1438 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1439 my $prefix = $self->get_number_prefix_for_type();
1441 if ($subject && $self->{"${prefix}number"}) {
1442 $subject .= " " . $self->{"${prefix}number"}
1445 $main::lxdebug->leave_sub();
1450 $main::lxdebug->enter_sub();
1454 chdir("$self->{tmpdir}");
1457 if (-f "$self->{tmpfile}.err") {
1458 open(FH, "$self->{tmpfile}.err");
1463 if ($self->{tmpfile} && ! $::keep_temp_files) {
1464 $self->{tmpfile} =~ s|.*/||g;
1466 $self->{tmpfile} =~ s/\.\w+$//g;
1467 my $tmpfile = $self->{tmpfile};
1468 unlink(<$tmpfile.*>);
1471 chdir("$self->{cwd}");
1473 $main::lxdebug->leave_sub();
1479 $main::lxdebug->enter_sub();
1481 my ($self, $date, $myconfig) = @_;
1484 if ($date && $date =~ /\D/) {
1486 if ($myconfig->{dateformat} =~ /^yy/) {
1487 ($yy, $mm, $dd) = split /\D/, $date;
1489 if ($myconfig->{dateformat} =~ /^mm/) {
1490 ($mm, $dd, $yy) = split /\D/, $date;
1492 if ($myconfig->{dateformat} =~ /^dd/) {
1493 ($dd, $mm, $yy) = split /\D/, $date;
1498 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1499 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1501 $dd = "0$dd" if ($dd < 10);
1502 $mm = "0$mm" if ($mm < 10);
1504 $date = "$yy$mm$dd";
1507 $main::lxdebug->leave_sub();
1512 # Database routines used throughout
1514 sub _dbconnect_options {
1516 my $options = { pg_enable_utf8 => $::locale->is_utf8,
1523 $main::lxdebug->enter_sub(2);
1525 my ($self, $myconfig) = @_;
1527 # connect to database
1528 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, $self->_dbconnect_options)
1532 if ($myconfig->{dboptions}) {
1533 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1536 $main::lxdebug->leave_sub(2);
1541 sub dbconnect_noauto {
1542 $main::lxdebug->enter_sub();
1544 my ($self, $myconfig) = @_;
1546 # connect to database
1547 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, $self->_dbconnect_options(AutoCommit => 0))
1551 if ($myconfig->{dboptions}) {
1552 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1555 $main::lxdebug->leave_sub();
1560 sub get_standard_dbh {
1561 $main::lxdebug->enter_sub(2);
1564 my $myconfig = shift || \%::myconfig;
1566 if ($standard_dbh && !$standard_dbh->{Active}) {
1567 $main::lxdebug->message(LXDebug->INFO(), "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
1568 undef $standard_dbh;
1571 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1573 $main::lxdebug->leave_sub(2);
1575 return $standard_dbh;
1579 $main::lxdebug->enter_sub();
1581 my ($self, $date, $myconfig) = @_;
1582 my $dbh = $self->dbconnect($myconfig);
1584 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1585 my $sth = prepare_execute_query($self, $dbh, $query, $date);
1586 my ($closed) = $sth->fetchrow_array;
1588 $main::lxdebug->leave_sub();
1593 sub update_balance {
1594 $main::lxdebug->enter_sub();
1596 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1598 # if we have a value, go do it
1601 # retrieve balance from table
1602 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1603 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1604 my ($balance) = $sth->fetchrow_array;
1610 $query = "UPDATE $table SET $field = $balance WHERE $where";
1611 do_query($self, $dbh, $query, @values);
1613 $main::lxdebug->leave_sub();
1616 sub update_exchangerate {
1617 $main::lxdebug->enter_sub();
1619 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1621 # some sanity check for currency
1623 $main::lxdebug->leave_sub();
1626 $query = qq|SELECT curr FROM defaults|;
1628 my ($currency) = selectrow_query($self, $dbh, $query);
1629 my ($defaultcurrency) = split m/:/, $currency;
1632 if ($curr eq $defaultcurrency) {
1633 $main::lxdebug->leave_sub();
1637 $query = qq|SELECT e.curr FROM exchangerate e
1638 WHERE e.curr = ? AND e.transdate = ?
1640 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1649 $buy = conv_i($buy, "NULL");
1650 $sell = conv_i($sell, "NULL");
1653 if ($buy != 0 && $sell != 0) {
1654 $set = "buy = $buy, sell = $sell";
1655 } elsif ($buy != 0) {
1656 $set = "buy = $buy";
1657 } elsif ($sell != 0) {
1658 $set = "sell = $sell";
1661 if ($sth->fetchrow_array) {
1662 $query = qq|UPDATE exchangerate
1668 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1669 VALUES (?, $buy, $sell, ?)|;
1672 do_query($self, $dbh, $query, $curr, $transdate);
1674 $main::lxdebug->leave_sub();
1677 sub save_exchangerate {
1678 $main::lxdebug->enter_sub();
1680 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1682 my $dbh = $self->dbconnect($myconfig);
1686 $buy = $rate if $fld eq 'buy';
1687 $sell = $rate if $fld eq 'sell';
1690 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1695 $main::lxdebug->leave_sub();
1698 sub get_exchangerate {
1699 $main::lxdebug->enter_sub();
1701 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1704 unless ($transdate) {
1705 $main::lxdebug->leave_sub();
1709 $query = qq|SELECT curr FROM defaults|;
1711 my ($currency) = selectrow_query($self, $dbh, $query);
1712 my ($defaultcurrency) = split m/:/, $currency;
1714 if ($currency eq $defaultcurrency) {
1715 $main::lxdebug->leave_sub();
1719 $query = qq|SELECT e.$fld FROM exchangerate e
1720 WHERE e.curr = ? AND e.transdate = ?|;
1721 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1725 $main::lxdebug->leave_sub();
1727 return $exchangerate;
1730 sub check_exchangerate {
1731 $main::lxdebug->enter_sub();
1733 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1735 if ($fld !~/^buy|sell$/) {
1736 $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
1739 unless ($transdate) {
1740 $main::lxdebug->leave_sub();
1744 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1746 if ($currency eq $defaultcurrency) {
1747 $main::lxdebug->leave_sub();
1751 my $dbh = $self->get_standard_dbh($myconfig);
1752 my $query = qq|SELECT e.$fld FROM exchangerate e
1753 WHERE e.curr = ? AND e.transdate = ?|;
1755 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1757 $main::lxdebug->leave_sub();
1759 return $exchangerate;
1762 sub get_all_currencies {
1763 $main::lxdebug->enter_sub();
1766 my $myconfig = shift || \%::myconfig;
1767 my $dbh = $self->get_standard_dbh($myconfig);
1769 my $query = qq|SELECT curr FROM defaults|;
1771 my ($curr) = selectrow_query($self, $dbh, $query);
1772 my @currencies = grep { $_ } map { s/\s//g; $_ } split m/:/, $curr;
1774 $main::lxdebug->leave_sub();
1779 sub get_default_currency {
1780 $main::lxdebug->enter_sub();
1782 my ($self, $myconfig) = @_;
1783 my @currencies = $self->get_all_currencies($myconfig);
1785 $main::lxdebug->leave_sub();
1787 return $currencies[0];
1790 sub set_payment_options {
1791 $main::lxdebug->enter_sub();
1793 my ($self, $myconfig, $transdate) = @_;
1795 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1797 my $dbh = $self->get_standard_dbh($myconfig);
1800 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1801 qq|FROM payment_terms p | .
1804 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1805 $self->{payment_terms}) =
1806 selectrow_query($self, $dbh, $query, $self->{payment_id});
1808 if ($transdate eq "") {
1809 if ($self->{invdate}) {
1810 $transdate = $self->{invdate};
1812 $transdate = $self->{transdate};
1817 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1818 qq|FROM payment_terms|;
1819 ($self->{netto_date}, $self->{skonto_date}) =
1820 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1822 my ($invtotal, $total);
1823 my (%amounts, %formatted_amounts);
1825 if ($self->{type} =~ /_order$/) {
1826 $amounts{invtotal} = $self->{ordtotal};
1827 $amounts{total} = $self->{ordtotal};
1829 } elsif ($self->{type} =~ /_quotation$/) {
1830 $amounts{invtotal} = $self->{quototal};
1831 $amounts{total} = $self->{quototal};
1834 $amounts{invtotal} = $self->{invtotal};
1835 $amounts{total} = $self->{total};
1837 $amounts{skonto_in_percent} = 100.0 * $self->{percent_skonto};
1839 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1841 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1842 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1843 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1845 foreach (keys %amounts) {
1846 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1847 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1850 if ($self->{"language_id"}) {
1852 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1853 qq|FROM translation_payment_terms t | .
1854 qq|LEFT JOIN language l ON t.language_id = l.id | .
1855 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1856 my ($description_long, $output_numberformat, $output_dateformat,
1857 $output_longdates) =
1858 selectrow_query($self, $dbh, $query,
1859 $self->{"language_id"}, $self->{"payment_id"});
1861 $self->{payment_terms} = $description_long if ($description_long);
1863 if ($output_dateformat) {
1864 foreach my $key (qw(netto_date skonto_date)) {
1866 $main::locale->reformat_date($myconfig, $self->{$key},
1872 if ($output_numberformat &&
1873 ($output_numberformat ne $myconfig->{"numberformat"})) {
1874 my $saved_numberformat = $myconfig->{"numberformat"};
1875 $myconfig->{"numberformat"} = $output_numberformat;
1876 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1877 $myconfig->{"numberformat"} = $saved_numberformat;
1881 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1882 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1883 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1884 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1885 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1886 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1887 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1889 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1891 $self->{skonto_in_percent} = $formatted_amounts{skonto_in_percent};
1893 $main::lxdebug->leave_sub();
1897 sub get_template_language {
1898 $main::lxdebug->enter_sub();
1900 my ($self, $myconfig) = @_;
1902 my $template_code = "";
1904 if ($self->{language_id}) {
1905 my $dbh = $self->get_standard_dbh($myconfig);
1906 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1907 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1910 $main::lxdebug->leave_sub();
1912 return $template_code;
1915 sub get_printer_code {
1916 $main::lxdebug->enter_sub();
1918 my ($self, $myconfig) = @_;
1920 my $template_code = "";
1922 if ($self->{printer_id}) {
1923 my $dbh = $self->get_standard_dbh($myconfig);
1924 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1925 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1928 $main::lxdebug->leave_sub();
1930 return $template_code;
1934 $main::lxdebug->enter_sub();
1936 my ($self, $myconfig) = @_;
1938 my $template_code = "";
1940 if ($self->{shipto_id}) {
1941 my $dbh = $self->get_standard_dbh($myconfig);
1942 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1943 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1944 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1947 $main::lxdebug->leave_sub();
1951 $main::lxdebug->enter_sub();
1953 my ($self, $dbh, $id, $module) = @_;
1958 foreach my $item (qw(name department_1 department_2 street zipcode city country
1959 contact cp_gender phone fax email)) {
1960 if ($self->{"shipto$item"}) {
1961 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1963 push(@values, $self->{"shipto${item}"});
1967 if ($self->{shipto_id}) {
1968 my $query = qq|UPDATE shipto set
1970 shiptodepartment_1 = ?,
1971 shiptodepartment_2 = ?,
1977 shiptocp_gender = ?,
1981 WHERE shipto_id = ?|;
1982 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1984 my $query = qq|SELECT * FROM shipto
1985 WHERE shiptoname = ? AND
1986 shiptodepartment_1 = ? AND
1987 shiptodepartment_2 = ? AND
1988 shiptostreet = ? AND
1989 shiptozipcode = ? AND
1991 shiptocountry = ? AND
1992 shiptocontact = ? AND
1993 shiptocp_gender = ? AND
1999 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
2002 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
2003 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
2004 shiptocontact, shiptocp_gender, shiptophone, shiptofax, shiptoemail, module)
2005 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
2006 do_query($self, $dbh, $query, $id, @values, $module);
2011 $main::lxdebug->leave_sub();
2015 $main::lxdebug->enter_sub();
2017 my ($self, $dbh) = @_;
2019 $dbh ||= $self->get_standard_dbh(\%main::myconfig);
2021 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
2022 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
2023 $self->{"employee_id"} *= 1;
2025 $main::lxdebug->leave_sub();
2028 sub get_employee_data {
2029 $main::lxdebug->enter_sub();
2034 Common::check_params(\%params, qw(prefix));
2035 Common::check_params_x(\%params, qw(id));
2038 $main::lxdebug->leave_sub();
2042 my $myconfig = \%main::myconfig;
2043 my $dbh = $params{dbh} || $self->get_standard_dbh($myconfig);
2045 my ($login) = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
2048 my $user = User->new($login);
2049 map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
2051 $self->{$params{prefix} . '_login'} = $login;
2052 $self->{$params{prefix} . '_name'} ||= $login;
2055 $main::lxdebug->leave_sub();
2059 $main::lxdebug->enter_sub();
2061 my ($self, $myconfig, $reference_date) = @_;
2063 $reference_date = $reference_date ? conv_dateq($reference_date) . '::DATE' : 'current_date';
2065 my $dbh = $self->get_standard_dbh($myconfig);
2066 my $query = qq|SELECT ${reference_date} + terms_netto FROM payment_terms WHERE id = ?|;
2067 my ($duedate) = selectrow_query($self, $dbh, $query, $self->{payment_id});
2069 $main::lxdebug->leave_sub();
2075 $main::lxdebug->enter_sub();
2077 my ($self, $dbh, $id, $key) = @_;
2079 $key = "all_contacts" unless ($key);
2083 $main::lxdebug->leave_sub();
2088 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
2089 qq|FROM contacts | .
2090 qq|WHERE cp_cv_id = ? | .
2091 qq|ORDER BY lower(cp_name)|;
2093 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
2095 $main::lxdebug->leave_sub();
2099 $main::lxdebug->enter_sub();
2101 my ($self, $dbh, $key) = @_;
2103 my ($all, $old_id, $where, @values);
2105 if (ref($key) eq "HASH") {
2108 $key = "ALL_PROJECTS";
2110 foreach my $p (keys(%{$params})) {
2112 $all = $params->{$p};
2113 } elsif ($p eq "old_id") {
2114 $old_id = $params->{$p};
2115 } elsif ($p eq "key") {
2116 $key = $params->{$p};
2122 $where = "WHERE active ";
2124 if (ref($old_id) eq "ARRAY") {
2125 my @ids = grep({ $_ } @{$old_id});
2127 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
2128 push(@values, @ids);
2131 $where .= " OR (id = ?) ";
2132 push(@values, $old_id);
2138 qq|SELECT id, projectnumber, description, active | .
2141 qq|ORDER BY lower(projectnumber)|;
2143 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2145 $main::lxdebug->leave_sub();
2149 $main::lxdebug->enter_sub();
2151 my ($self, $dbh, $vc_id, $key) = @_;
2153 $key = "all_shipto" unless ($key);
2156 # get shipping addresses
2157 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
2159 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
2165 $main::lxdebug->leave_sub();
2169 $main::lxdebug->enter_sub();
2171 my ($self, $dbh, $key) = @_;
2173 $key = "all_printers" unless ($key);
2175 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
2177 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2179 $main::lxdebug->leave_sub();
2183 $main::lxdebug->enter_sub();
2185 my ($self, $dbh, $params) = @_;
2188 $key = $params->{key};
2189 $key = "all_charts" unless ($key);
2191 my $transdate = quote_db_date($params->{transdate});
2194 qq|SELECT c.id, c.accno, c.description, c.link, c.charttype, tk.taxkey_id, tk.tax_id | .
2196 qq|LEFT JOIN taxkeys tk ON | .
2197 qq|(tk.id = (SELECT id FROM taxkeys | .
2198 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2199 qq| ORDER BY startdate DESC LIMIT 1)) | .
2200 qq|ORDER BY c.accno|;
2202 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2204 $main::lxdebug->leave_sub();
2207 sub _get_taxcharts {
2208 $main::lxdebug->enter_sub();
2210 my ($self, $dbh, $params) = @_;
2212 my $key = "all_taxcharts";
2215 if (ref $params eq 'HASH') {
2216 $key = $params->{key} if ($params->{key});
2217 if ($params->{module} eq 'AR') {
2218 push @where, 'taxkey NOT IN (8, 9, 18, 19)';
2220 } elsif ($params->{module} eq 'AP') {
2221 push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
2228 my $where = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (@where);
2230 my $query = qq|SELECT * FROM tax $where ORDER BY taxkey|;
2232 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2234 $main::lxdebug->leave_sub();
2238 $main::lxdebug->enter_sub();
2240 my ($self, $dbh, $key) = @_;
2242 $key = "all_taxzones" unless ($key);
2244 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2246 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2248 $main::lxdebug->leave_sub();
2251 sub _get_employees {
2252 $main::lxdebug->enter_sub();
2254 my ($self, $dbh, $default_key, $key) = @_;
2256 $key = $default_key unless ($key);
2257 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2259 $main::lxdebug->leave_sub();
2262 sub _get_business_types {
2263 $main::lxdebug->enter_sub();
2265 my ($self, $dbh, $key) = @_;
2267 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2268 $options->{key} ||= "all_business_types";
2271 if (exists $options->{salesman}) {
2272 $where = 'WHERE ' . ($options->{salesman} ? '' : 'NOT ') . 'COALESCE(salesman)';
2275 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, qq|SELECT * FROM business $where ORDER BY lower(description)|);
2277 $main::lxdebug->leave_sub();
2280 sub _get_languages {
2281 $main::lxdebug->enter_sub();
2283 my ($self, $dbh, $key) = @_;
2285 $key = "all_languages" unless ($key);
2287 my $query = qq|SELECT * FROM language ORDER BY id|;
2289 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2291 $main::lxdebug->leave_sub();
2294 sub _get_dunning_configs {
2295 $main::lxdebug->enter_sub();
2297 my ($self, $dbh, $key) = @_;
2299 $key = "all_dunning_configs" unless ($key);
2301 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2303 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2305 $main::lxdebug->leave_sub();
2308 sub _get_currencies {
2309 $main::lxdebug->enter_sub();
2311 my ($self, $dbh, $key) = @_;
2313 $key = "all_currencies" unless ($key);
2315 my $query = qq|SELECT curr AS currency FROM defaults|;
2317 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2319 $main::lxdebug->leave_sub();
2323 $main::lxdebug->enter_sub();
2325 my ($self, $dbh, $key) = @_;
2327 $key = "all_payments" unless ($key);
2329 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2331 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2333 $main::lxdebug->leave_sub();
2336 sub _get_customers {
2337 $main::lxdebug->enter_sub();
2339 my ($self, $dbh, $key) = @_;
2341 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2342 $options->{key} ||= "all_customers";
2343 my $limit_clause = "LIMIT $options->{limit}" if $options->{limit};
2346 push @where, qq|business_id IN (SELECT id FROM business WHERE salesman)| if $options->{business_is_salesman};
2347 push @where, qq|NOT obsolete| if !$options->{with_obsolete};
2348 my $where_str = @where ? "WHERE " . join(" AND ", map { "($_)" } @where) : '';
2350 my $query = qq|SELECT * FROM customer $where_str ORDER BY name $limit_clause|;
2351 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, $query);
2353 $main::lxdebug->leave_sub();
2357 $main::lxdebug->enter_sub();
2359 my ($self, $dbh, $key) = @_;
2361 $key = "all_vendors" unless ($key);
2363 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2365 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2367 $main::lxdebug->leave_sub();
2370 sub _get_departments {
2371 $main::lxdebug->enter_sub();
2373 my ($self, $dbh, $key) = @_;
2375 $key = "all_departments" unless ($key);
2377 my $query = qq|SELECT * FROM department ORDER BY description|;
2379 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2381 $main::lxdebug->leave_sub();
2384 sub _get_warehouses {
2385 $main::lxdebug->enter_sub();
2387 my ($self, $dbh, $param) = @_;
2389 my ($key, $bins_key);
2391 if ('' eq ref $param) {
2395 $key = $param->{key};
2396 $bins_key = $param->{bins};
2399 my $query = qq|SELECT w.* FROM warehouse w
2400 WHERE (NOT w.invalid) AND
2401 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2402 ORDER BY w.sortkey|;
2404 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2407 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2408 my $sth = prepare_query($self, $dbh, $query);
2410 foreach my $warehouse (@{ $self->{$key} }) {
2411 do_statement($self, $sth, $query, $warehouse->{id});
2412 $warehouse->{$bins_key} = [];
2414 while (my $ref = $sth->fetchrow_hashref()) {
2415 push @{ $warehouse->{$bins_key} }, $ref;
2421 $main::lxdebug->leave_sub();
2425 $main::lxdebug->enter_sub();
2427 my ($self, $dbh, $table, $key, $sortkey) = @_;
2429 my $query = qq|SELECT * FROM $table|;
2430 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2432 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2434 $main::lxdebug->leave_sub();
2438 # $main::lxdebug->enter_sub();
2440 # my ($self, $dbh, $key) = @_;
2442 # $key ||= "all_groups";
2444 # my $groups = $main::auth->read_groups();
2446 # $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2448 # $main::lxdebug->leave_sub();
2452 $main::lxdebug->enter_sub();
2457 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2458 my ($sth, $query, $ref);
2460 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2461 my $vc_id = $self->{"${vc}_id"};
2463 if ($params{"contacts"}) {
2464 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2467 if ($params{"shipto"}) {
2468 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2471 if ($params{"projects"} || $params{"all_projects"}) {
2472 $self->_get_projects($dbh, $params{"all_projects"} ?
2473 $params{"all_projects"} : $params{"projects"},
2474 $params{"all_projects"} ? 1 : 0);
2477 if ($params{"printers"}) {
2478 $self->_get_printers($dbh, $params{"printers"});
2481 if ($params{"languages"}) {
2482 $self->_get_languages($dbh, $params{"languages"});
2485 if ($params{"charts"}) {
2486 $self->_get_charts($dbh, $params{"charts"});
2489 if ($params{"taxcharts"}) {
2490 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2493 if ($params{"taxzones"}) {
2494 $self->_get_taxzones($dbh, $params{"taxzones"});
2497 if ($params{"employees"}) {
2498 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2501 if ($params{"salesmen"}) {
2502 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2505 if ($params{"business_types"}) {
2506 $self->_get_business_types($dbh, $params{"business_types"});
2509 if ($params{"dunning_configs"}) {
2510 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2513 if($params{"currencies"}) {
2514 $self->_get_currencies($dbh, $params{"currencies"});
2517 if($params{"customers"}) {
2518 $self->_get_customers($dbh, $params{"customers"});
2521 if($params{"vendors"}) {
2522 if (ref $params{"vendors"} eq 'HASH') {
2523 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2525 $self->_get_vendors($dbh, $params{"vendors"});
2529 if($params{"payments"}) {
2530 $self->_get_payments($dbh, $params{"payments"});
2533 if($params{"departments"}) {
2534 $self->_get_departments($dbh, $params{"departments"});
2537 if ($params{price_factors}) {
2538 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2541 if ($params{warehouses}) {
2542 $self->_get_warehouses($dbh, $params{warehouses});
2545 # if ($params{groups}) {
2546 # $self->_get_groups($dbh, $params{groups});
2549 if ($params{partsgroup}) {
2550 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2553 $main::lxdebug->leave_sub();
2556 # this sub gets the id and name from $table
2558 $main::lxdebug->enter_sub();
2560 my ($self, $myconfig, $table) = @_;
2562 # connect to database
2563 my $dbh = $self->get_standard_dbh($myconfig);
2565 $table = $table eq "customer" ? "customer" : "vendor";
2566 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2568 my ($query, @values);
2570 if (!$self->{openinvoices}) {
2572 if ($self->{customernumber} ne "") {
2573 $where = qq|(vc.customernumber ILIKE ?)|;
2574 push(@values, '%' . $self->{customernumber} . '%');
2576 $where = qq|(vc.name ILIKE ?)|;
2577 push(@values, '%' . $self->{$table} . '%');
2581 qq~SELECT vc.id, vc.name,
2582 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2584 WHERE $where AND (NOT vc.obsolete)
2588 qq~SELECT DISTINCT vc.id, vc.name,
2589 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2591 JOIN $table vc ON (a.${table}_id = vc.id)
2592 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2594 push(@values, '%' . $self->{$table} . '%');
2597 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2599 $main::lxdebug->leave_sub();
2601 return scalar(@{ $self->{name_list} });
2604 # the selection sub is used in the AR, AP, IS, IR and OE module
2607 $main::lxdebug->enter_sub();
2609 my ($self, $myconfig, $table, $module) = @_;
2612 my $dbh = $self->get_standard_dbh;
2614 $table = $table eq "customer" ? "customer" : "vendor";
2616 my $query = qq|SELECT count(*) FROM $table|;
2617 my ($count) = selectrow_query($self, $dbh, $query);
2619 # build selection list
2620 if ($count <= $myconfig->{vclimit}) {
2621 $query = qq|SELECT id, name, salesman_id
2622 FROM $table WHERE NOT obsolete
2624 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2628 $self->get_employee($dbh);
2630 # setup sales contacts
2631 $query = qq|SELECT e.id, e.name
2633 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2634 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2637 push(@{ $self->{all_employees} },
2638 { id => $self->{employee_id},
2639 name => $self->{employee} });
2641 # sort the whole thing
2642 @{ $self->{all_employees} } =
2643 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2645 if ($module eq 'AR') {
2647 # prepare query for departments
2648 $query = qq|SELECT id, description
2651 ORDER BY description|;
2654 $query = qq|SELECT id, description
2656 ORDER BY description|;
2659 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2662 $query = qq|SELECT id, description
2666 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2669 $query = qq|SELECT printer_description, id
2671 ORDER BY printer_description|;
2673 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2676 $query = qq|SELECT id, description
2680 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2682 $main::lxdebug->leave_sub();
2685 sub language_payment {
2686 $main::lxdebug->enter_sub();
2688 my ($self, $myconfig) = @_;
2690 my $dbh = $self->get_standard_dbh($myconfig);
2692 my $query = qq|SELECT id, description
2696 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2699 $query = qq|SELECT printer_description, id
2701 ORDER BY printer_description|;
2703 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2706 $query = qq|SELECT id, description
2710 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2712 # get buchungsgruppen
2713 $query = qq|SELECT id, description
2714 FROM buchungsgruppen|;
2716 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2718 $main::lxdebug->leave_sub();
2721 # this is only used for reports
2722 sub all_departments {
2723 $main::lxdebug->enter_sub();
2725 my ($self, $myconfig, $table) = @_;
2727 my $dbh = $self->get_standard_dbh($myconfig);
2730 if ($table eq 'customer') {
2731 $where = "WHERE role = 'P' ";
2734 my $query = qq|SELECT id, description
2737 ORDER BY description|;
2738 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2740 delete($self->{all_departments}) unless (@{ $self->{all_departments} || [] });
2742 $main::lxdebug->leave_sub();
2746 $main::lxdebug->enter_sub();
2748 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2751 if ($table eq "customer") {
2760 $self->all_vc($myconfig, $table, $module);
2762 # get last customers or vendors
2763 my ($query, $sth, $ref);
2765 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2770 my $transdate = "current_date";
2771 if ($self->{transdate}) {
2772 $transdate = $dbh->quote($self->{transdate});
2775 # now get the account numbers
2776 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2777 FROM chart c, taxkeys tk
2778 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2779 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2782 $sth = $dbh->prepare($query);
2784 do_statement($self, $sth, $query, '%' . $module . '%');
2786 $self->{accounts} = "";
2787 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2789 foreach my $key (split(/:/, $ref->{link})) {
2790 if ($key =~ /\Q$module\E/) {
2792 # cross reference for keys
2793 $xkeyref{ $ref->{accno} } = $key;
2795 push @{ $self->{"${module}_links"}{$key} },
2796 { accno => $ref->{accno},
2797 description => $ref->{description},
2798 taxkey => $ref->{taxkey_id},
2799 tax_id => $ref->{tax_id} };
2801 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2807 # get taxkeys and description
2808 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2809 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2811 if (($module eq "AP") || ($module eq "AR")) {
2812 # get tax rates and description
2813 $query = qq|SELECT * FROM tax|;
2814 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2820 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2821 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2822 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2823 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2825 d.description AS department,
2828 JOIN $table c ON (a.${table}_id = c.id)
2829 LEFT JOIN employee e ON (e.id = a.employee_id)
2830 LEFT JOIN department d ON (d.id = a.department_id)
2832 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2834 foreach my $key (keys %$ref) {
2835 $self->{$key} = $ref->{$key};
2838 my $transdate = "current_date";
2839 if ($self->{transdate}) {
2840 $transdate = $dbh->quote($self->{transdate});
2843 # now get the account numbers
2844 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2846 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2848 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2849 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2852 $sth = $dbh->prepare($query);
2853 do_statement($self, $sth, $query, "%$module%");
2855 $self->{accounts} = "";
2856 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2858 foreach my $key (split(/:/, $ref->{link})) {
2859 if ($key =~ /\Q$module\E/) {
2861 # cross reference for keys
2862 $xkeyref{ $ref->{accno} } = $key;
2864 push @{ $self->{"${module}_links"}{$key} },
2865 { accno => $ref->{accno},
2866 description => $ref->{description},
2867 taxkey => $ref->{taxkey_id},
2868 tax_id => $ref->{tax_id} };
2870 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2876 # get amounts from individual entries
2879 c.accno, c.description,
2880 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2884 LEFT JOIN chart c ON (c.id = a.chart_id)
2885 LEFT JOIN project p ON (p.id = a.project_id)
2886 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2887 WHERE (tk.taxkey_id=a.taxkey) AND
2888 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2889 THEN tk.chart_id = a.chart_id
2892 OR (c.link='%tax%')) AND
2893 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2894 WHERE a.trans_id = ?
2895 AND a.fx_transaction = '0'
2896 ORDER BY a.acc_trans_id, a.transdate|;
2897 $sth = $dbh->prepare($query);
2898 do_statement($self, $sth, $query, $self->{id});
2900 # get exchangerate for currency
2901 $self->{exchangerate} =
2902 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2905 # store amounts in {acc_trans}{$key} for multiple accounts
2906 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2907 $ref->{exchangerate} =
2908 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2909 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2912 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2913 $ref->{amount} *= -1;
2915 $ref->{index} = $index;
2917 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2923 d.curr AS currencies, d.closedto, d.revtrans,
2924 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2925 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2927 $ref = selectfirst_hashref_query($self, $dbh, $query);
2928 map { $self->{$_} = $ref->{$_} } keys %$ref;
2935 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2936 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2937 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2939 $ref = selectfirst_hashref_query($self, $dbh, $query);
2940 map { $self->{$_} = $ref->{$_} } keys %$ref;
2942 if ($self->{"$self->{vc}_id"}) {
2944 # only setup currency
2945 ($self->{currency}) = split(/:/, $self->{currencies});
2949 $self->lastname_used($dbh, $myconfig, $table, $module);
2951 # get exchangerate for currency
2952 $self->{exchangerate} =
2953 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2959 $main::lxdebug->leave_sub();
2963 $main::lxdebug->enter_sub();
2965 my ($self, $dbh, $myconfig, $table, $module) = @_;
2969 $table = $table eq "customer" ? "customer" : "vendor";
2970 my %column_map = ("a.curr" => "currency",
2971 "a.${table}_id" => "${table}_id",
2972 "a.department_id" => "department_id",
2973 "d.description" => "department",
2974 "ct.name" => $table,
2975 "current_date + ct.terms" => "duedate",
2978 if ($self->{type} =~ /delivery_order/) {
2979 $arap = 'delivery_orders';
2980 delete $column_map{"a.curr"};
2982 } elsif ($self->{type} =~ /_order/) {
2984 $where = "quotation = '0'";
2986 } elsif ($self->{type} =~ /_quotation/) {
2988 $where = "quotation = '1'";
2990 } elsif ($table eq 'customer') {
2998 $where = "($where) AND" if ($where);
2999 my $query = qq|SELECT MAX(id) FROM $arap
3000 WHERE $where ${table}_id > 0|;
3001 my ($trans_id) = selectrow_query($self, $dbh, $query);
3004 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
3005 $query = qq|SELECT $column_spec
3007 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
3008 LEFT JOIN department d ON (a.department_id = d.id)
3010 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
3012 map { $self->{$_} = $ref->{$_} } values %column_map;
3014 $main::lxdebug->leave_sub();
3018 $main::lxdebug->enter_sub();
3021 my $myconfig = shift || \%::myconfig;
3022 my ($thisdate, $days) = @_;
3024 my $dbh = $self->get_standard_dbh($myconfig);
3029 my $dateformat = $myconfig->{dateformat};
3030 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
3031 $thisdate = $dbh->quote($thisdate);
3032 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
3034 $query = qq|SELECT current_date AS thisdate|;
3037 ($thisdate) = selectrow_query($self, $dbh, $query);
3039 $main::lxdebug->leave_sub();
3045 $main::lxdebug->enter_sub();
3047 my ($self, $string) = @_;
3049 if ($string !~ /%/) {
3050 $string = "%$string%";
3053 $string =~ s/\'/\'\'/g;
3055 $main::lxdebug->leave_sub();
3061 $main::lxdebug->enter_sub();
3063 my ($self, $flds, $new, $count, $numrows) = @_;
3067 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
3072 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
3074 my $j = $item->{ndx} - 1;
3075 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
3079 for $i ($count + 1 .. $numrows) {
3080 map { delete $self->{"${_}_$i"} } @{$flds};
3083 $main::lxdebug->leave_sub();
3087 $main::lxdebug->enter_sub();
3089 my ($self, $myconfig) = @_;
3093 my $dbh = $self->dbconnect_noauto($myconfig);
3095 my $query = qq|DELETE FROM status
3096 WHERE (formname = ?) AND (trans_id = ?)|;
3097 my $sth = prepare_query($self, $dbh, $query);
3099 if ($self->{formname} =~ /(check|receipt)/) {
3100 for $i (1 .. $self->{rowcount}) {
3101 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
3104 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
3108 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3109 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3111 my %queued = split / /, $self->{queued};
3114 if ($self->{formname} =~ /(check|receipt)/) {
3116 # this is a check or receipt, add one entry for each lineitem
3117 my ($accno) = split /--/, $self->{account};
3118 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
3119 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
3120 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
3121 $sth = prepare_query($self, $dbh, $query);
3123 for $i (1 .. $self->{rowcount}) {
3124 if ($self->{"checked_$i"}) {
3125 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
3131 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3132 VALUES (?, ?, ?, ?, ?)|;
3133 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
3134 $queued{$self->{formname}}, $self->{formname});
3140 $main::lxdebug->leave_sub();
3144 $main::lxdebug->enter_sub();
3146 my ($self, $dbh) = @_;
3148 my ($query, $printed, $emailed);
3150 my $formnames = $self->{printed};
3151 my $emailforms = $self->{emailed};
3153 $query = qq|DELETE FROM status
3154 WHERE (formname = ?) AND (trans_id = ?)|;
3155 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
3157 # this only applies to the forms
3158 # checks and receipts are posted when printed or queued
3160 if ($self->{queued}) {
3161 my %queued = split / /, $self->{queued};
3163 foreach my $formname (keys %queued) {
3164 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3165 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3167 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3168 VALUES (?, ?, ?, ?, ?)|;
3169 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
3171 $formnames =~ s/\Q$self->{formname}\E//;
3172 $emailforms =~ s/\Q$self->{formname}\E//;
3177 # save printed, emailed info
3178 $formnames =~ s/^ +//g;
3179 $emailforms =~ s/^ +//g;
3182 map { $status{$_}{printed} = 1 } split / +/, $formnames;
3183 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
3185 foreach my $formname (keys %status) {
3186 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
3187 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
3189 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
3190 VALUES (?, ?, ?, ?)|;
3191 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
3194 $main::lxdebug->leave_sub();
3198 # $main::locale->text('SAVED')
3199 # $main::locale->text('DELETED')
3200 # $main::locale->text('ADDED')
3201 # $main::locale->text('PAYMENT POSTED')
3202 # $main::locale->text('POSTED')
3203 # $main::locale->text('POSTED AS NEW')
3204 # $main::locale->text('ELSE')
3205 # $main::locale->text('SAVED FOR DUNNING')
3206 # $main::locale->text('DUNNING STARTED')
3207 # $main::locale->text('PRINTED')
3208 # $main::locale->text('MAILED')
3209 # $main::locale->text('SCREENED')
3210 # $main::locale->text('CANCELED')
3211 # $main::locale->text('invoice')
3212 # $main::locale->text('proforma')
3213 # $main::locale->text('sales_order')
3214 # $main::locale->text('packing_list')
3215 # $main::locale->text('pick_list')
3216 # $main::locale->text('purchase_order')
3217 # $main::locale->text('bin_list')
3218 # $main::locale->text('sales_quotation')
3219 # $main::locale->text('request_quotation')
3222 $main::lxdebug->enter_sub();
3225 my $dbh = shift || $self->get_standard_dbh;
3227 if(!exists $self->{employee_id}) {
3228 &get_employee($self, $dbh);
3232 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3233 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3234 my @values = (conv_i($self->{id}), $self->{login},
3235 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3236 do_query($self, $dbh, $query, @values);
3240 $main::lxdebug->leave_sub();
3244 $main::lxdebug->enter_sub();
3246 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3247 my ($orderBy, $desc) = split(/\-\-/, $order);
3248 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3251 if ($trans_id ne "") {
3253 qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name, h.snumbers, h.trans_id AS id | .
3254 qq|FROM history_erp h | .
3255 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3256 qq|WHERE (trans_id = | . $trans_id . qq|) $restriction | .
3259 my $sth = $dbh->prepare($query) || $self->dberror($query);
3261 $sth->execute() || $self->dberror("$query");
3263 while(my $hash_ref = $sth->fetchrow_hashref()) {
3264 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3265 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3266 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3267 $tempArray[$i++] = $hash_ref;
3269 $main::lxdebug->leave_sub() and return \@tempArray
3270 if ($i > 0 && $tempArray[0] ne "");
3272 $main::lxdebug->leave_sub();
3276 sub update_defaults {
3277 $main::lxdebug->enter_sub();
3279 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3282 if ($provided_dbh) {
3283 $dbh = $provided_dbh;
3285 $dbh = $self->dbconnect_noauto($myconfig);
3287 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3288 my $sth = $dbh->prepare($query);
3290 $sth->execute || $self->dberror($query);
3291 my ($var) = $sth->fetchrow_array;
3294 if ($var =~ m/\d+$/) {
3295 my $new_var = (substr $var, $-[0]) * 1 + 1;
3296 my $len_diff = length($var) - $-[0] - length($new_var);
3297 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3303 $query = qq|UPDATE defaults SET $fld = ?|;
3304 do_query($self, $dbh, $query, $var);
3306 if (!$provided_dbh) {
3311 $main::lxdebug->leave_sub();
3316 sub update_business {
3317 $main::lxdebug->enter_sub();
3319 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3322 if ($provided_dbh) {
3323 $dbh = $provided_dbh;
3325 $dbh = $self->dbconnect_noauto($myconfig);
3328 qq|SELECT customernumberinit FROM business
3329 WHERE id = ? FOR UPDATE|;
3330 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3332 return undef unless $var;
3334 if ($var =~ m/\d+$/) {
3335 my $new_var = (substr $var, $-[0]) * 1 + 1;
3336 my $len_diff = length($var) - $-[0] - length($new_var);
3337 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3343 $query = qq|UPDATE business
3344 SET customernumberinit = ?
3346 do_query($self, $dbh, $query, $var, $business_id);
3348 if (!$provided_dbh) {
3353 $main::lxdebug->leave_sub();
3358 sub get_partsgroup {
3359 $main::lxdebug->enter_sub();
3361 my ($self, $myconfig, $p) = @_;
3362 my $target = $p->{target} || 'all_partsgroup';
3364 my $dbh = $self->get_standard_dbh($myconfig);
3366 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3368 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3371 if ($p->{searchitems} eq 'part') {
3372 $query .= qq|WHERE p.inventory_accno_id > 0|;
3374 if ($p->{searchitems} eq 'service') {
3375 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3377 if ($p->{searchitems} eq 'assembly') {
3378 $query .= qq|WHERE p.assembly = '1'|;
3380 if ($p->{searchitems} eq 'labor') {
3381 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3384 $query .= qq|ORDER BY partsgroup|;
3387 $query = qq|SELECT id, partsgroup FROM partsgroup
3388 ORDER BY partsgroup|;
3391 if ($p->{language_code}) {
3392 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3393 t.description AS translation
3395 JOIN parts p ON (p.partsgroup_id = pg.id)
3396 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3397 ORDER BY translation|;
3398 @values = ($p->{language_code});
3401 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3403 $main::lxdebug->leave_sub();
3406 sub get_pricegroup {
3407 $main::lxdebug->enter_sub();
3409 my ($self, $myconfig, $p) = @_;
3411 my $dbh = $self->get_standard_dbh($myconfig);
3413 my $query = qq|SELECT p.id, p.pricegroup
3416 $query .= qq| ORDER BY pricegroup|;
3419 $query = qq|SELECT id, pricegroup FROM pricegroup
3420 ORDER BY pricegroup|;
3423 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3425 $main::lxdebug->leave_sub();
3429 # usage $form->all_years($myconfig, [$dbh])
3430 # return list of all years where bookings found
3433 $main::lxdebug->enter_sub();
3435 my ($self, $myconfig, $dbh) = @_;
3437 $dbh ||= $self->get_standard_dbh($myconfig);
3440 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3441 (SELECT MAX(transdate) FROM acc_trans)|;
3442 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3444 if ($myconfig->{dateformat} =~ /^yy/) {
3445 ($startdate) = split /\W/, $startdate;
3446 ($enddate) = split /\W/, $enddate;
3448 (@_) = split /\W/, $startdate;
3450 (@_) = split /\W/, $enddate;
3455 $startdate = substr($startdate,0,4);
3456 $enddate = substr($enddate,0,4);
3458 while ($enddate >= $startdate) {
3459 push @all_years, $enddate--;
3464 $main::lxdebug->leave_sub();
3468 $main::lxdebug->enter_sub();
3472 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if exists $self->{$_} } @vars;
3474 $main::lxdebug->leave_sub();
3478 $main::lxdebug->enter_sub();
3483 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if exists $self->{_VAR_BACKUP}->{$_} } @vars;
3485 $main::lxdebug->leave_sub();
3494 SL::Form.pm - main data object.
3498 This is the main data object of Lx-Office.
3499 Unfortunately it also acts as a god object for certain data retrieval procedures used in the entry points.
3500 Points of interest for a beginner are:
3502 - $form->error - renders a generic error in html. accepts an error message
3503 - $form->get_standard_dbh - returns a database connection for the
3505 =head1 SPECIAL FUNCTIONS
3509 =item _store_value()
3511 parses a complex var name, and stores it in the form.
3514 $form->_store_value($key, $value);
3516 keys must start with a string, and can contain various tokens.
3517 supported key structures are:
3520 simple key strings work as expected
3525 separating two keys by a dot (.) will result in a hash lookup for the inner value
3526 this is similar to the behaviour of java and templating mechanisms.
3528 filter.description => $form->{filter}->{description}
3530 3. array+hashref access
3532 adding brackets ([]) before the dot will cause the next hash to be put into an array.
3533 using [+] instead of [] will force a new array index. this is useful for recurring
3534 data structures like part lists. put a [+] into the first varname, and use [] on the
3537 repeating these names in your template:
3540 invoice.items[].parts_id
3544 $form->{invoice}->{items}->[
3558 using brackets at the end of a name will result in a pure array to be created.
3559 note that you mustn't use [+], which is reserved for array+hash access and will
3560 result in undefined behaviour in array context.
3562 filter.status[] => $form->{status}->[ val1, val2, ... ]
3564 =item update_business PARAMS
3567 \%config, - config hashref
3568 $business_id, - business id
3569 $dbh - optional database handle
3571 handles business (thats customer/vendor types) sequences.
3573 special behaviour for empty strings in customerinitnumber field:
3574 will in this case not increase the value, and return undef.
3576 =item redirect_header $url
3578 Generates a HTTP redirection header for the new C<$url>. Constructs an
3579 absolute URL including scheme, host name and port. If C<$url> is a
3580 relative URL then it is considered relative to Lx-Office base URL.
3582 This function C<die>s if headers have already been created with
3583 C<$::form-E<gt>header>.
3587 print $::form->redirect_header('oe.pl?action=edit&id=1234');
3588 print $::form->redirect_header('http://www.lx-office.org/');