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 #======================================================================
58 use List::Util qw(first max min sum);
59 use List::MoreUtils qw(any);
66 disconnect_standard_dbh();
69 sub disconnect_standard_dbh {
70 return unless $standard_dbh;
71 $standard_dbh->disconnect();
76 $main::lxdebug->enter_sub(2);
82 my @tokens = split /((?:\[\+?\])?(?:\.|$))/, $key;
87 $curr = \ $self->{ shift @tokens };
91 my $sep = shift @tokens;
92 my $key = shift @tokens;
94 $curr = \ $$curr->[++$#$$curr], next if $sep eq '[]';
95 $curr = \ $$curr->[max 0, $#$$curr] if $sep eq '[].';
96 $curr = \ $$curr->[++$#$$curr] if $sep eq '[+].';
97 $curr = \ $$curr->{$key}
102 $main::lxdebug->leave_sub(2);
108 $main::lxdebug->enter_sub(2);
113 my @pairs = split(/&/, $input);
116 my ($key, $value) = split(/=/, $_, 2);
117 $self->_store_value($self->unescape($key), $self->unescape($value)) if ($key);
120 $main::lxdebug->leave_sub(2);
123 sub _request_to_hash {
124 $main::lxdebug->enter_sub(2);
129 if (!$ENV{'CONTENT_TYPE'}
130 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
132 $self->_input_to_hash($input);
134 $main::lxdebug->leave_sub(2);
138 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
140 my $boundary = '--' . $1;
142 foreach my $line (split m/\n/, $input) {
143 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
145 if (($line eq $boundary) || ($line eq "$boundary\r")) {
146 ${ $previous } =~ s|\r?\n$|| if $previous;
152 $content_type = "text/plain";
159 next unless $boundary_found;
161 if (!$headers_done) {
162 $line =~ s/[\r\n]*$//;
169 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
170 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
172 substr $line, $-[0], $+[0] - $-[0], "";
175 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
177 substr $line, $-[0], $+[0] - $-[0], "";
180 $previous = $self->_store_value($name, '') if ($name);
181 $self->{FILENAME} = $filename if ($filename);
186 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
193 next unless $previous;
195 ${ $previous } .= "${line}\n";
198 ${ $previous } =~ s|\r?\n$|| if $previous;
200 $main::lxdebug->leave_sub(2);
203 sub _recode_recursively {
204 $main::lxdebug->enter_sub();
205 my ($iconv, $param) = @_;
207 if (any { ref $param eq $_ } qw(Form HASH)) {
208 foreach my $key (keys %{ $param }) {
209 if (!ref $param->{$key}) {
210 # Workaround for a bug: converting $param->{$key} directly
211 # leads to 'undef'. I don't know why. Converting a copy works,
213 $param->{$key} = $iconv->convert("" . $param->{$key});
215 _recode_recursively($iconv, $param->{$key});
219 } elsif (ref $param eq 'ARRAY') {
220 foreach my $idx (0 .. scalar(@{ $param }) - 1) {
221 if (!ref $param->[$idx]) {
222 # Workaround for a bug: converting $param->[$idx] directly
223 # leads to 'undef'. I don't know why. Converting a copy works,
225 $param->[$idx] = $iconv->convert("" . $param->[$idx]);
227 _recode_recursively($iconv, $param->[$idx]);
231 $main::lxdebug->leave_sub();
235 $main::lxdebug->enter_sub();
241 if ($LXDebug::watch_form) {
242 require SL::Watchdog;
243 tie %{ $self }, 'SL::Watchdog';
248 $self->_input_to_hash($ENV{QUERY_STRING}) if $ENV{QUERY_STRING};
249 $self->_input_to_hash($ARGV[0]) if @ARGV && $ARGV[0];
251 if ($ENV{CONTENT_LENGTH}) {
253 read STDIN, $content, $ENV{CONTENT_LENGTH};
254 $self->_request_to_hash($content);
257 my $db_charset = $main::dbcharset;
258 $db_charset ||= Common::DEFAULT_CHARSET;
260 my $encoding = $self->{INPUT_ENCODING} || $db_charset;
261 delete $self->{INPUT_ENCODING};
263 _recode_recursively(SL::Iconv->new($encoding, $db_charset), $self);
265 $self->{action} = lc $self->{action};
266 $self->{action} =~ s/( |-|,|\#)/_/g;
268 $self->{version} = "2.6.1";
270 $main::lxdebug->leave_sub();
275 sub _flatten_variables_rec {
276 $main::lxdebug->enter_sub(2);
285 if ('' eq ref $curr->{$key}) {
286 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
288 } elsif ('HASH' eq ref $curr->{$key}) {
289 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
290 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
294 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
295 my $first_array_entry = 1;
297 foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
298 push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
299 $first_array_entry = 0;
304 $main::lxdebug->leave_sub(2);
309 sub flatten_variables {
310 $main::lxdebug->enter_sub(2);
318 push @variables, $self->_flatten_variables_rec($self, '', $_);
321 $main::lxdebug->leave_sub(2);
326 sub flatten_standard_variables {
327 $main::lxdebug->enter_sub(2);
330 my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
334 foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
335 push @variables, $self->_flatten_variables_rec($self, '', $_);
338 $main::lxdebug->leave_sub(2);
344 $main::lxdebug->enter_sub();
350 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
352 $main::lxdebug->leave_sub();
356 $main::lxdebug->enter_sub(2);
359 my $password = $self->{password};
361 $self->{password} = 'X' x 8;
363 local $Data::Dumper::Sortkeys = 1;
364 my $output = Dumper($self);
366 $self->{password} = $password;
368 $main::lxdebug->leave_sub(2);
374 $main::lxdebug->enter_sub(2);
376 my ($self, $str) = @_;
378 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
380 $main::lxdebug->leave_sub(2);
386 $main::lxdebug->enter_sub(2);
388 my ($self, $str) = @_;
393 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
395 $main::lxdebug->leave_sub(2);
401 $main::lxdebug->enter_sub();
402 my ($self, $str) = @_;
404 if ($str && !ref($str)) {
405 $str =~ s/\"/"/g;
408 $main::lxdebug->leave_sub();
414 $main::lxdebug->enter_sub();
415 my ($self, $str) = @_;
417 if ($str && !ref($str)) {
418 $str =~ s/"/\"/g;
421 $main::lxdebug->leave_sub();
427 $main::lxdebug->enter_sub();
431 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
433 for (sort keys %$self) {
434 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
435 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
438 $main::lxdebug->leave_sub();
442 $main::lxdebug->enter_sub();
444 $main::lxdebug->show_backtrace();
446 my ($self, $msg) = @_;
447 if ($ENV{HTTP_USER_AGENT}) {
449 $self->show_generic_error($msg);
452 print STDERR "Error: $msg\n";
456 $main::lxdebug->leave_sub();
460 $main::lxdebug->enter_sub();
462 my ($self, $msg) = @_;
464 if ($ENV{HTTP_USER_AGENT}) {
467 if (!$self->{header}) {
480 if ($self->{info_function}) {
481 &{ $self->{info_function} }($msg);
487 $main::lxdebug->leave_sub();
490 # calculates the number of rows in a textarea based on the content and column number
491 # can be capped with maxrows
493 $main::lxdebug->enter_sub();
494 my ($self, $str, $cols, $maxrows, $minrows) = @_;
498 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
501 $main::lxdebug->leave_sub();
503 return max(min($rows, $maxrows), $minrows);
507 $main::lxdebug->enter_sub();
509 my ($self, $msg) = @_;
511 $self->error("$msg\n" . $DBI::errstr);
513 $main::lxdebug->leave_sub();
517 $main::lxdebug->enter_sub();
519 my ($self, $name, $msg) = @_;
522 foreach my $part (split m/\./, $name) {
523 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
526 $curr = $curr->{$part};
529 $main::lxdebug->leave_sub();
532 sub _get_request_uri {
535 return URI->new($ENV{HTTP_REFERER})->canonical() if $ENV{HTTP_X_FORWARDED_FOR};
537 my $scheme = $ENV{HTTPS} && (lc $ENV{HTTPS} eq 'on') ? 'https' : 'http';
538 my $port = $ENV{SERVER_PORT} || '';
539 $port = undef if (($scheme eq 'http' ) && ($port == 80))
540 || (($scheme eq 'https') && ($port == 443));
542 my $uri = URI->new("${scheme}://");
543 $uri->scheme($scheme);
545 $uri->host($ENV{HTTP_HOST} || $ENV{SERVER_ADDR});
546 $uri->path_query($ENV{REQUEST_URI});
552 sub _add_to_request_uri {
555 my $relative_new_path = shift;
556 my $request_uri = shift || $self->_get_request_uri;
557 my $relative_new_uri = URI->new($relative_new_path);
558 my @request_segments = $request_uri->path_segments;
560 my $new_uri = $request_uri->clone;
561 $new_uri->path_segments(@request_segments[0..scalar(@request_segments) - 2], $relative_new_uri->path_segments);
566 sub create_http_response {
567 $main::lxdebug->enter_sub();
572 my $cgi = $main::cgi;
573 $cgi ||= CGI->new('');
576 if (defined $main::auth) {
577 my $uri = $self->_get_request_uri;
578 my @segments = $uri->path_segments;
580 $uri->path_segments(@segments);
582 my $session_cookie_value = $main::auth->get_session_id();
583 $session_cookie_value ||= 'NO_SESSION';
585 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
586 '-value' => $session_cookie_value,
587 '-path' => $uri->path,
588 '-secure' => $ENV{HTTPS});
591 my %cgi_params = ('-type' => $params{content_type});
592 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
594 my $output = $cgi->header('-cookie' => $session_cookie,
597 $main::lxdebug->leave_sub();
604 $main::lxdebug->enter_sub();
606 # extra code ist currently only used by menuv3 and menuv4 to set their css.
607 # it is strongly deprecated, and will be changed in a future version.
608 my ($self, $extra_code) = @_;
610 if ($self->{header}) {
611 $main::lxdebug->leave_sub();
615 my ($stylesheet, $favicon, $pagelayout);
617 if ($ENV{HTTP_USER_AGENT}) {
620 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
621 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
622 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
625 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
627 $stylesheets =~ s|^\s*||;
628 $stylesheets =~ s|\s*$||;
629 foreach my $file (split m/\s+/, $stylesheets) {
631 next if (! -f "css/$file");
633 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
636 $self->{favicon} = "favicon.ico" unless $self->{favicon};
638 if ($self->{favicon} && (-f "$self->{favicon}")) {
640 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
644 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
646 if ($self->{landscape}) {
647 $pagelayout = qq|<style type="text/css">
648 \@page { size:landscape; }
653 <script type="text/javascript">
656 document.$self->{fokus}.focus();
660 | if $self->{"fokus"};
664 if ($self->{jsscript} == 1) {
667 <script type="text/javascript" src="js/jquery.js"></script>
668 <script type="text/javascript" src="js/common.js"></script>
669 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
670 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
671 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
672 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
679 ? "$self->{title} - $self->{titlebar}"
682 for my $item (@ { $self->{AJAX} || [] }) {
683 $ajax .= $item->show_javascript();
686 print $self->create_http_response('content_type' => 'text/html',
687 'charset' => $db_charset,);
688 print qq|${doctype}<html>
690 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
691 <title>$self->{titlebar}</title>
700 <link rel="stylesheet" href="css/jquery.autocomplete.css" type="text/css" />
702 <meta name="robots" content="noindex,nofollow" />
703 <script type="text/javascript" src="js/highlight_input.js"></script>
705 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
706 <script type="text/javascript" src="js/tabcontent.js">
708 /***********************************************
709 * Tab Content script v2.2- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
710 * This notice MUST stay intact for legal use
711 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
712 ***********************************************/
723 $main::lxdebug->leave_sub();
726 sub ajax_response_header {
727 $main::lxdebug->enter_sub();
731 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
732 my $cgi = $main::cgi || CGI->new('');
733 my $output = $cgi->header('-charset' => $db_charset);
735 $main::lxdebug->leave_sub();
740 sub redirect_header {
744 my $base_uri = $self->_get_request_uri;
745 my $new_uri = URI->new_abs($new_url, $base_uri);
747 die "Headers already sent" if $::self->{header};
750 my $cgi = $main::cgi || CGI->new('');
751 return $cgi->redirect($new_uri);
754 sub set_standard_title {
755 $::lxdebug->enter_sub;
758 $self->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $self->{version}";
759 $self->{titlebar} .= "- $::myconfig{name}" if $::myconfig{name};
760 $self->{titlebar} .= "- $::myconfig{dbname}" if $::myconfig{name};
762 $::lxdebug->leave_sub;
765 sub _prepare_html_template {
766 $main::lxdebug->enter_sub();
768 my ($self, $file, $additional_params) = @_;
771 if (!%::myconfig || !$::myconfig{"countrycode"}) {
772 $language = $main::language;
774 $language = $main::myconfig{"countrycode"};
776 $language = "de" unless ($language);
778 if (-f "templates/webpages/${file}.html") {
779 if ((-f ".developer") && ((stat("templates/webpages/${file}.html"))[9] > (stat("locale/${language}/all"))[9])) {
780 my $info = "Developer information: templates/webpages/${file}.html is newer than the translation file locale/${language}/all.\n" .
781 "Please re-run 'locales.pl' in 'locale/${language}'.";
782 print(qq|<pre>$info</pre>|);
786 $file = "templates/webpages/${file}.html";
789 my $info = "Web page template '${file}' not found.\n" .
790 "Please re-run 'locales.pl' in 'locale/${language}'.";
791 print(qq|<pre>$info</pre>|);
795 if ($self->{"DEBUG"}) {
796 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
799 if ($additional_params->{"DEBUG"}) {
800 $additional_params->{"DEBUG"} =
801 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
804 if (%main::myconfig) {
805 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
806 my $jsc_dateformat = $main::myconfig{"dateformat"};
807 $jsc_dateformat =~ s/d+/\%d/gi;
808 $jsc_dateformat =~ s/m+/\%m/gi;
809 $jsc_dateformat =~ s/y+/\%Y/gi;
810 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
811 $additional_params->{"myconfig"} ||= \%::myconfig;
814 $additional_params->{"conf_dbcharset"} = $main::dbcharset;
815 $additional_params->{"conf_webdav"} = $main::webdav;
816 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
817 $additional_params->{"conf_latex_templates"} = $main::latex;
818 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
819 $additional_params->{"conf_vertreter"} = $main::vertreter;
820 $additional_params->{"conf_show_best_before"} = $main::show_best_before;
822 if (%main::debug_options) {
823 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
826 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
827 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
828 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
832 $main::lxdebug->leave_sub();
837 sub parse_html_template {
838 $main::lxdebug->enter_sub();
840 my ($self, $file, $additional_params) = @_;
842 $additional_params ||= { };
844 my $real_file = $self->_prepare_html_template($file, $additional_params);
845 my $template = $self->template || $self->init_template;
847 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
850 $template->process($real_file, $additional_params, \$output) || die $template->error;
852 $main::lxdebug->leave_sub();
860 return if $self->template;
862 return $self->template(Template->new({
867 'PLUGIN_BASE' => 'SL::Template::Plugin',
868 'INCLUDE_PATH' => '.:templates/webpages',
869 'COMPILE_EXT' => '.tcc',
870 'COMPILE_DIR' => $::userspath . '/templates-cache',
876 $self->{template_object} = shift if @_;
877 return $self->{template_object};
880 sub show_generic_error {
881 $main::lxdebug->enter_sub();
883 my ($self, $error, %params) = @_;
886 'title_error' => $params{title},
887 'label_error' => $error,
890 if ($params{action}) {
893 map { delete($self->{$_}); } qw(action);
894 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
896 $add_params->{SHOW_BUTTON} = 1;
897 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
898 $add_params->{VARIABLES} = \@vars;
900 } elsif ($params{back_button}) {
901 $add_params->{SHOW_BACK_BUTTON} = 1;
904 $self->{title} = $params{title} if $params{title};
907 print $self->parse_html_template("generic/error", $add_params);
909 print STDERR "Error: $error\n";
911 $main::lxdebug->leave_sub();
916 sub show_generic_information {
917 $main::lxdebug->enter_sub();
919 my ($self, $text, $title) = @_;
922 'title_information' => $title,
923 'label_information' => $text,
926 $self->{title} = $title if ($title);
929 print $self->parse_html_template("generic/information", $add_params);
931 $main::lxdebug->leave_sub();
936 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
937 # changed it to accept an arbitrary number of triggers - sschoeling
939 $main::lxdebug->enter_sub();
942 my $myconfig = shift;
945 # set dateform for jsscript
948 "dd.mm.yy" => "%d.%m.%Y",
949 "dd-mm-yy" => "%d-%m-%Y",
950 "dd/mm/yy" => "%d/%m/%Y",
951 "mm/dd/yy" => "%m/%d/%Y",
952 "mm-dd-yy" => "%m-%d-%Y",
953 "yyyy-mm-dd" => "%Y-%m-%d",
956 my $ifFormat = defined($dateformats{$myconfig->{"dateformat"}}) ?
957 $dateformats{$myconfig->{"dateformat"}} : "%d.%m.%Y";
964 inputField : "| . (shift) . qq|",
965 ifFormat :"$ifFormat",
966 align : "| . (shift) . qq|",
967 button : "| . (shift) . qq|"
973 <script type="text/javascript">
974 <!--| . join("", @triggers) . qq|//-->
978 $main::lxdebug->leave_sub();
981 } #end sub write_trigger
984 $main::lxdebug->enter_sub();
986 my ($self, $msg) = @_;
988 if (!$self->{callback}) {
994 # my ($script, $argv) = split(/\?/, $self->{callback}, 2);
995 # $script =~ s|.*/||;
996 # $script =~ s|[^a-zA-Z0-9_\.]||g;
997 # exec("perl", "$script", $argv);
999 print $::form->redirect_header($self->{callback});
1001 $main::lxdebug->leave_sub();
1004 # sort of columns removed - empty sub
1006 $main::lxdebug->enter_sub();
1008 my ($self, @columns) = @_;
1010 $main::lxdebug->leave_sub();
1016 $main::lxdebug->enter_sub(2);
1018 my ($self, $myconfig, $amount, $places, $dash) = @_;
1020 if ($amount eq "") {
1024 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
1026 my $neg = ($amount =~ s/^-//);
1027 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
1029 if (defined($places) && ($places ne '')) {
1035 my ($actual_places) = ($amount =~ /\.(\d+)/);
1036 $actual_places = length($actual_places);
1037 $places = $actual_places > $places ? $actual_places : $places;
1040 $amount = $self->round_amount($amount, $places);
1043 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
1044 my @p = split(/\./, $amount); # split amount at decimal point
1046 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
1049 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
1052 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
1053 ($dash =~ /DRCR/) ? ($neg ? "$amount " . $main::locale->text('DR') : "$amount " . $main::locale->text('CR') ) :
1054 ($neg ? "-$amount" : "$amount" ) ;
1058 $main::lxdebug->leave_sub(2);
1062 sub format_amount_units {
1063 $main::lxdebug->enter_sub();
1068 my $myconfig = \%main::myconfig;
1069 my $amount = $params{amount} * 1;
1070 my $places = $params{places};
1071 my $part_unit_name = $params{part_unit};
1072 my $amount_unit_name = $params{amount_unit};
1073 my $conv_units = $params{conv_units};
1074 my $max_places = $params{max_places};
1076 if (!$part_unit_name) {
1077 $main::lxdebug->leave_sub();
1081 AM->retrieve_all_units();
1082 my $all_units = $main::all_units;
1084 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
1085 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
1088 if (!scalar @{ $conv_units }) {
1089 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
1090 $main::lxdebug->leave_sub();
1094 my $part_unit = $all_units->{$part_unit_name};
1095 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
1097 $amount *= $conv_unit->{factor};
1102 foreach my $unit (@$conv_units) {
1103 my $last = $unit->{name} eq $part_unit->{name};
1105 $num = int($amount / $unit->{factor});
1106 $amount -= $num * $unit->{factor};
1109 if ($last ? $amount : $num) {
1110 push @values, { "unit" => $unit->{name},
1111 "amount" => $last ? $amount / $unit->{factor} : $num,
1112 "places" => $last ? $places : 0 };
1119 push @values, { "unit" => $part_unit_name,
1124 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
1126 $main::lxdebug->leave_sub();
1132 $main::lxdebug->enter_sub(2);
1137 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
1138 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
1139 $input =~ s/\#\#/\#/g;
1141 $main::lxdebug->leave_sub(2);
1149 $main::lxdebug->enter_sub(2);
1151 my ($self, $myconfig, $amount) = @_;
1153 if ( ($myconfig->{numberformat} eq '1.000,00')
1154 || ($myconfig->{numberformat} eq '1000,00')) {
1159 if ($myconfig->{numberformat} eq "1'000.00") {
1165 $main::lxdebug->leave_sub(2);
1167 return ($amount * 1);
1171 $main::lxdebug->enter_sub(2);
1173 my ($self, $amount, $places) = @_;
1176 # Rounding like "Kaufmannsrunden" (see http://de.wikipedia.org/wiki/Rundung )
1178 # Round amounts to eight places before rounding to the requested
1179 # number of places. This gets rid of errors due to internal floating
1180 # point representation.
1181 $amount = $self->round_amount($amount, 8) if $places < 8;
1182 $amount = $amount * (10**($places));
1183 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1185 $main::lxdebug->leave_sub(2);
1187 return $round_amount;
1191 sub parse_template {
1192 $main::lxdebug->enter_sub();
1194 my ($self, $myconfig, $userspath) = @_;
1199 $self->{"cwd"} = getcwd();
1200 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1205 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1206 $template_type = 'OpenDocument';
1207 $ext_for_format = $self->{"format"} =~ m/pdf/ ? 'pdf' : 'odt';
1209 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1210 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1211 $template_type = 'LaTeX';
1212 $ext_for_format = 'pdf';
1214 } elsif (($self->{"format"} =~ /html/i) || (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1215 $template_type = 'HTML';
1216 $ext_for_format = 'html';
1218 } elsif (($self->{"format"} =~ /xml/i) || (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1219 $template_type = 'XML';
1220 $ext_for_format = 'xml';
1222 } elsif ( $self->{"format"} =~ /elster(?:winston|taxbird)/i ) {
1223 $template_type = 'xml';
1225 } elsif ( $self->{"format"} =~ /excel/i ) {
1226 $template_type = 'Excel';
1227 $ext_for_format = 'xls';
1229 } elsif ( defined $self->{'format'}) {
1230 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1232 } elsif ( $self->{'format'} eq '' ) {
1233 $self->error("No Outputformat given: $self->{'format'}");
1235 } else { #Catch the rest
1236 $self->error("Outputformat not defined: $self->{'format'}");
1239 my $template = SL::Template::create(type => $template_type,
1240 file_name => $self->{IN},
1242 myconfig => $myconfig,
1243 userspath => $userspath);
1245 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1246 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1248 if (!$self->{employee_id}) {
1249 map { $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature company address businessnumber co_ustid taxnumber duns);
1252 map { $self->{"${_}"} = $myconfig->{$_}; } qw(co_ustid);
1254 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1256 # OUT is used for the media, screen, printer, email
1257 # for postscript we store a copy in a temporary file
1259 my $prepend_userspath;
1261 if (!$self->{tmpfile}) {
1262 $self->{tmpfile} = "${fileid}.$self->{IN}";
1263 $prepend_userspath = 1;
1266 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1268 $self->{tmpfile} =~ s|.*/||;
1269 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1270 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1272 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1273 $out = $self->{OUT};
1274 $self->{OUT} = ">$self->{tmpfile}";
1280 open OUT, "$self->{OUT}" or $self->error("$self->{OUT} : $!");
1281 $result = $template->parse(*OUT);
1286 $result = $template->parse(*STDOUT);
1291 $self->error("$self->{IN} : " . $template->get_error());
1294 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1296 if ($self->{media} eq 'email') {
1298 my $mail = new Mailer;
1300 map { $mail->{$_} = $self->{$_} }
1301 qw(cc bcc subject message version format);
1302 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1303 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1304 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1305 $mail->{fileid} = "$fileid.";
1306 $myconfig->{signature} =~ s/\r//g;
1308 # if we send html or plain text inline
1309 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1310 $mail->{contenttype} = "text/html";
1312 $mail->{message} =~ s/\r//g;
1313 $mail->{message} =~ s/\n/<br>\n/g;
1314 $myconfig->{signature} =~ s/\n/<br>\n/g;
1315 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1317 open(IN, $self->{tmpfile})
1318 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1320 $mail->{message} .= $_;
1327 if (!$self->{"do_not_attach"}) {
1328 my $attachment_name = $self->{attachment_filename} || $self->{tmpfile};
1329 $attachment_name =~ s/\.(.+?)$/.${ext_for_format}/ if ($ext_for_format);
1330 $mail->{attachments} = [{ "filename" => $self->{tmpfile},
1331 "name" => $attachment_name }];
1334 $mail->{message} =~ s/\r//g;
1335 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1339 my $err = $mail->send();
1340 $self->error($self->cleanup . "$err") if ($err);
1344 $self->{OUT} = $out;
1346 my $numbytes = (-s $self->{tmpfile});
1347 open(IN, $self->{tmpfile})
1348 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1350 $self->{copies} = 1 unless $self->{media} eq 'printer';
1352 chdir("$self->{cwd}");
1353 #print(STDERR "Kopien $self->{copies}\n");
1354 #print(STDERR "OUT $self->{OUT}\n");
1355 for my $i (1 .. $self->{copies}) {
1357 open OUT, $self->{OUT} or $self->error($self->cleanup . "$self->{OUT} : $!");
1358 print OUT while <IN>;
1363 $self->{attachment_filename} = ($self->{attachment_filename})
1364 ? $self->{attachment_filename}
1365 : $self->generate_attachment_filename();
1367 # launch application
1368 print qq|Content-Type: | . $template->get_mime_type() . qq|
1369 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1370 Content-Length: $numbytes
1374 $::locale->with_raw_io(\*STDOUT, sub { print while <IN> });
1385 chdir("$self->{cwd}");
1386 $main::lxdebug->leave_sub();
1389 sub get_formname_translation {
1390 $main::lxdebug->enter_sub();
1391 my ($self, $formname) = @_;
1393 $formname ||= $self->{formname};
1395 my %formname_translations = (
1396 bin_list => $main::locale->text('Bin List'),
1397 credit_note => $main::locale->text('Credit Note'),
1398 invoice => $main::locale->text('Invoice'),
1399 packing_list => $main::locale->text('Packing List'),
1400 pick_list => $main::locale->text('Pick List'),
1401 proforma => $main::locale->text('Proforma Invoice'),
1402 purchase_order => $main::locale->text('Purchase Order'),
1403 request_quotation => $main::locale->text('RFQ'),
1404 sales_order => $main::locale->text('Confirmation'),
1405 sales_quotation => $main::locale->text('Quotation'),
1406 storno_invoice => $main::locale->text('Storno Invoice'),
1407 storno_packing_list => $main::locale->text('Storno Packing List'),
1408 sales_delivery_order => $main::locale->text('Delivery Order'),
1409 purchase_delivery_order => $main::locale->text('Delivery Order'),
1410 dunning => $main::locale->text('Dunning'),
1413 $main::lxdebug->leave_sub();
1414 return $formname_translations{$formname}
1417 sub get_number_prefix_for_type {
1418 $main::lxdebug->enter_sub();
1422 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1423 : ($self->{type} =~ /_quotation$/) ? 'quo'
1424 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1427 $main::lxdebug->leave_sub();
1431 sub get_extension_for_format {
1432 $main::lxdebug->enter_sub();
1435 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1436 : $self->{format} =~ /postscript/i ? ".ps"
1437 : $self->{format} =~ /opendocument/i ? ".odt"
1438 : $self->{format} =~ /excel/i ? ".xls"
1439 : $self->{format} =~ /html/i ? ".html"
1442 $main::lxdebug->leave_sub();
1446 sub generate_attachment_filename {
1447 $main::lxdebug->enter_sub();
1450 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1451 my $prefix = $self->get_number_prefix_for_type();
1453 if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
1454 $attachment_filename .= ' (' . $main::locale->text('Preview') . ')' . $self->get_extension_for_format();
1456 } elsif ($attachment_filename && $self->{"${prefix}number"}) {
1457 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1460 $attachment_filename = "";
1463 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1464 $attachment_filename =~ s|[\s/\\]+|_|g;
1466 $main::lxdebug->leave_sub();
1467 return $attachment_filename;
1470 sub generate_email_subject {
1471 $main::lxdebug->enter_sub();
1474 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1475 my $prefix = $self->get_number_prefix_for_type();
1477 if ($subject && $self->{"${prefix}number"}) {
1478 $subject .= " " . $self->{"${prefix}number"}
1481 $main::lxdebug->leave_sub();
1486 $main::lxdebug->enter_sub();
1490 chdir("$self->{tmpdir}");
1493 if (-f "$self->{tmpfile}.err") {
1494 open(FH, "$self->{tmpfile}.err");
1499 if ($self->{tmpfile} && ! $::keep_temp_files) {
1500 $self->{tmpfile} =~ s|.*/||g;
1502 $self->{tmpfile} =~ s/\.\w+$//g;
1503 my $tmpfile = $self->{tmpfile};
1504 unlink(<$tmpfile.*>);
1507 chdir("$self->{cwd}");
1509 $main::lxdebug->leave_sub();
1515 $main::lxdebug->enter_sub();
1517 my ($self, $date, $myconfig) = @_;
1520 if ($date && $date =~ /\D/) {
1522 if ($myconfig->{dateformat} =~ /^yy/) {
1523 ($yy, $mm, $dd) = split /\D/, $date;
1525 if ($myconfig->{dateformat} =~ /^mm/) {
1526 ($mm, $dd, $yy) = split /\D/, $date;
1528 if ($myconfig->{dateformat} =~ /^dd/) {
1529 ($dd, $mm, $yy) = split /\D/, $date;
1534 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1535 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1537 $dd = "0$dd" if ($dd < 10);
1538 $mm = "0$mm" if ($mm < 10);
1540 $date = "$yy$mm$dd";
1543 $main::lxdebug->leave_sub();
1548 # Database routines used throughout
1550 sub _dbconnect_options {
1552 my $options = { pg_enable_utf8 => $::locale->is_utf8,
1559 $main::lxdebug->enter_sub(2);
1561 my ($self, $myconfig) = @_;
1563 # connect to database
1564 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, $self->_dbconnect_options)
1568 if ($myconfig->{dboptions}) {
1569 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1572 $main::lxdebug->leave_sub(2);
1577 sub dbconnect_noauto {
1578 $main::lxdebug->enter_sub();
1580 my ($self, $myconfig) = @_;
1582 # connect to database
1583 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, $self->_dbconnect_options(AutoCommit => 0))
1587 if ($myconfig->{dboptions}) {
1588 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1591 $main::lxdebug->leave_sub();
1596 sub get_standard_dbh {
1597 $main::lxdebug->enter_sub(2);
1600 my $myconfig = shift || \%::myconfig;
1602 if ($standard_dbh && !$standard_dbh->{Active}) {
1603 $main::lxdebug->message(LXDebug->INFO(), "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
1604 undef $standard_dbh;
1607 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1609 $main::lxdebug->leave_sub(2);
1611 return $standard_dbh;
1615 $main::lxdebug->enter_sub();
1617 my ($self, $date, $myconfig) = @_;
1618 my $dbh = $self->dbconnect($myconfig);
1620 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1621 my $sth = prepare_execute_query($self, $dbh, $query, $date);
1622 my ($closed) = $sth->fetchrow_array;
1624 $main::lxdebug->leave_sub();
1629 sub update_balance {
1630 $main::lxdebug->enter_sub();
1632 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1634 # if we have a value, go do it
1637 # retrieve balance from table
1638 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1639 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1640 my ($balance) = $sth->fetchrow_array;
1646 $query = "UPDATE $table SET $field = $balance WHERE $where";
1647 do_query($self, $dbh, $query, @values);
1649 $main::lxdebug->leave_sub();
1652 sub update_exchangerate {
1653 $main::lxdebug->enter_sub();
1655 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1657 # some sanity check for currency
1659 $main::lxdebug->leave_sub();
1662 $query = qq|SELECT curr FROM defaults|;
1664 my ($currency) = selectrow_query($self, $dbh, $query);
1665 my ($defaultcurrency) = split m/:/, $currency;
1668 if ($curr eq $defaultcurrency) {
1669 $main::lxdebug->leave_sub();
1673 $query = qq|SELECT e.curr FROM exchangerate e
1674 WHERE e.curr = ? AND e.transdate = ?
1676 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1685 $buy = conv_i($buy, "NULL");
1686 $sell = conv_i($sell, "NULL");
1689 if ($buy != 0 && $sell != 0) {
1690 $set = "buy = $buy, sell = $sell";
1691 } elsif ($buy != 0) {
1692 $set = "buy = $buy";
1693 } elsif ($sell != 0) {
1694 $set = "sell = $sell";
1697 if ($sth->fetchrow_array) {
1698 $query = qq|UPDATE exchangerate
1704 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1705 VALUES (?, $buy, $sell, ?)|;
1708 do_query($self, $dbh, $query, $curr, $transdate);
1710 $main::lxdebug->leave_sub();
1713 sub save_exchangerate {
1714 $main::lxdebug->enter_sub();
1716 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1718 my $dbh = $self->dbconnect($myconfig);
1722 $buy = $rate if $fld eq 'buy';
1723 $sell = $rate if $fld eq 'sell';
1726 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1731 $main::lxdebug->leave_sub();
1734 sub get_exchangerate {
1735 $main::lxdebug->enter_sub();
1737 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1740 unless ($transdate) {
1741 $main::lxdebug->leave_sub();
1745 $query = qq|SELECT curr FROM defaults|;
1747 my ($currency) = selectrow_query($self, $dbh, $query);
1748 my ($defaultcurrency) = split m/:/, $currency;
1750 if ($currency eq $defaultcurrency) {
1751 $main::lxdebug->leave_sub();
1755 $query = qq|SELECT e.$fld FROM exchangerate e
1756 WHERE e.curr = ? AND e.transdate = ?|;
1757 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1761 $main::lxdebug->leave_sub();
1763 return $exchangerate;
1766 sub check_exchangerate {
1767 $main::lxdebug->enter_sub();
1769 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1771 if ($fld !~/^buy|sell$/) {
1772 $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
1775 unless ($transdate) {
1776 $main::lxdebug->leave_sub();
1780 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1782 if ($currency eq $defaultcurrency) {
1783 $main::lxdebug->leave_sub();
1787 my $dbh = $self->get_standard_dbh($myconfig);
1788 my $query = qq|SELECT e.$fld FROM exchangerate e
1789 WHERE e.curr = ? AND e.transdate = ?|;
1791 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1793 $main::lxdebug->leave_sub();
1795 return $exchangerate;
1798 sub get_all_currencies {
1799 $main::lxdebug->enter_sub();
1802 my $myconfig = shift || \%::myconfig;
1803 my $dbh = $self->get_standard_dbh($myconfig);
1805 my $query = qq|SELECT curr FROM defaults|;
1807 my ($curr) = selectrow_query($self, $dbh, $query);
1808 my @currencies = grep { $_ } map { s/\s//g; $_ } split m/:/, $curr;
1810 $main::lxdebug->leave_sub();
1815 sub get_default_currency {
1816 $main::lxdebug->enter_sub();
1818 my ($self, $myconfig) = @_;
1819 my @currencies = $self->get_all_currencies($myconfig);
1821 $main::lxdebug->leave_sub();
1823 return $currencies[0];
1826 sub set_payment_options {
1827 $main::lxdebug->enter_sub();
1829 my ($self, $myconfig, $transdate) = @_;
1831 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1833 my $dbh = $self->get_standard_dbh($myconfig);
1836 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1837 qq|FROM payment_terms p | .
1840 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1841 $self->{payment_terms}) =
1842 selectrow_query($self, $dbh, $query, $self->{payment_id});
1844 if ($transdate eq "") {
1845 if ($self->{invdate}) {
1846 $transdate = $self->{invdate};
1848 $transdate = $self->{transdate};
1853 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1854 qq|FROM payment_terms|;
1855 ($self->{netto_date}, $self->{skonto_date}) =
1856 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1858 my ($invtotal, $total);
1859 my (%amounts, %formatted_amounts);
1861 if ($self->{type} =~ /_order$/) {
1862 $amounts{invtotal} = $self->{ordtotal};
1863 $amounts{total} = $self->{ordtotal};
1865 } elsif ($self->{type} =~ /_quotation$/) {
1866 $amounts{invtotal} = $self->{quototal};
1867 $amounts{total} = $self->{quototal};
1870 $amounts{invtotal} = $self->{invtotal};
1871 $amounts{total} = $self->{total};
1873 $amounts{skonto_in_percent} = 100.0 * $self->{percent_skonto};
1875 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1877 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1878 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1879 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1881 foreach (keys %amounts) {
1882 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1883 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1886 if ($self->{"language_id"}) {
1888 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1889 qq|FROM translation_payment_terms t | .
1890 qq|LEFT JOIN language l ON t.language_id = l.id | .
1891 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1892 my ($description_long, $output_numberformat, $output_dateformat,
1893 $output_longdates) =
1894 selectrow_query($self, $dbh, $query,
1895 $self->{"language_id"}, $self->{"payment_id"});
1897 $self->{payment_terms} = $description_long if ($description_long);
1899 if ($output_dateformat) {
1900 foreach my $key (qw(netto_date skonto_date)) {
1902 $main::locale->reformat_date($myconfig, $self->{$key},
1908 if ($output_numberformat &&
1909 ($output_numberformat ne $myconfig->{"numberformat"})) {
1910 my $saved_numberformat = $myconfig->{"numberformat"};
1911 $myconfig->{"numberformat"} = $output_numberformat;
1912 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1913 $myconfig->{"numberformat"} = $saved_numberformat;
1917 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1918 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1919 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1920 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1921 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1922 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1923 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1925 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1927 $self->{skonto_in_percent} = $formatted_amounts{skonto_in_percent};
1929 $main::lxdebug->leave_sub();
1933 sub get_template_language {
1934 $main::lxdebug->enter_sub();
1936 my ($self, $myconfig) = @_;
1938 my $template_code = "";
1940 if ($self->{language_id}) {
1941 my $dbh = $self->get_standard_dbh($myconfig);
1942 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1943 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1946 $main::lxdebug->leave_sub();
1948 return $template_code;
1951 sub get_printer_code {
1952 $main::lxdebug->enter_sub();
1954 my ($self, $myconfig) = @_;
1956 my $template_code = "";
1958 if ($self->{printer_id}) {
1959 my $dbh = $self->get_standard_dbh($myconfig);
1960 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1961 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1964 $main::lxdebug->leave_sub();
1966 return $template_code;
1970 $main::lxdebug->enter_sub();
1972 my ($self, $myconfig) = @_;
1974 my $template_code = "";
1976 if ($self->{shipto_id}) {
1977 my $dbh = $self->get_standard_dbh($myconfig);
1978 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1979 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1980 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1983 $main::lxdebug->leave_sub();
1987 $main::lxdebug->enter_sub();
1989 my ($self, $dbh, $id, $module) = @_;
1994 foreach my $item (qw(name department_1 department_2 street zipcode city country
1995 contact phone fax email)) {
1996 if ($self->{"shipto$item"}) {
1997 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1999 push(@values, $self->{"shipto${item}"});
2003 if ($self->{shipto_id}) {
2004 my $query = qq|UPDATE shipto set
2006 shiptodepartment_1 = ?,
2007 shiptodepartment_2 = ?,
2016 WHERE shipto_id = ?|;
2017 do_query($self, $dbh, $query, @values, $self->{shipto_id});
2019 my $query = qq|SELECT * FROM shipto
2020 WHERE shiptoname = ? AND
2021 shiptodepartment_1 = ? AND
2022 shiptodepartment_2 = ? AND
2023 shiptostreet = ? AND
2024 shiptozipcode = ? AND
2026 shiptocountry = ? AND
2027 shiptocontact = ? AND
2033 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
2036 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
2037 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
2038 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
2039 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
2040 do_query($self, $dbh, $query, $id, @values, $module);
2045 $main::lxdebug->leave_sub();
2049 $main::lxdebug->enter_sub();
2051 my ($self, $dbh) = @_;
2053 $dbh ||= $self->get_standard_dbh(\%main::myconfig);
2055 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
2056 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
2057 $self->{"employee_id"} *= 1;
2059 $main::lxdebug->leave_sub();
2062 sub get_employee_data {
2063 $main::lxdebug->enter_sub();
2068 Common::check_params(\%params, qw(prefix));
2069 Common::check_params_x(\%params, qw(id));
2072 $main::lxdebug->leave_sub();
2076 my $myconfig = \%main::myconfig;
2077 my $dbh = $params{dbh} || $self->get_standard_dbh($myconfig);
2079 my ($login) = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
2082 my $user = User->new($login);
2083 map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
2085 $self->{$params{prefix} . '_login'} = $login;
2086 $self->{$params{prefix} . '_name'} ||= $login;
2089 $main::lxdebug->leave_sub();
2093 $main::lxdebug->enter_sub();
2095 my ($self, $myconfig, $reference_date) = @_;
2097 $reference_date = $reference_date ? conv_dateq($reference_date) . '::DATE' : 'current_date';
2099 my $dbh = $self->get_standard_dbh($myconfig);
2100 my $query = qq|SELECT ${reference_date} + terms_netto FROM payment_terms WHERE id = ?|;
2101 my ($duedate) = selectrow_query($self, $dbh, $query, $self->{payment_id});
2103 $main::lxdebug->leave_sub();
2109 $main::lxdebug->enter_sub();
2111 my ($self, $dbh, $id, $key) = @_;
2113 $key = "all_contacts" unless ($key);
2117 $main::lxdebug->leave_sub();
2122 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
2123 qq|FROM contacts | .
2124 qq|WHERE cp_cv_id = ? | .
2125 qq|ORDER BY lower(cp_name)|;
2127 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
2129 $main::lxdebug->leave_sub();
2133 $main::lxdebug->enter_sub();
2135 my ($self, $dbh, $key) = @_;
2137 my ($all, $old_id, $where, @values);
2139 if (ref($key) eq "HASH") {
2142 $key = "ALL_PROJECTS";
2144 foreach my $p (keys(%{$params})) {
2146 $all = $params->{$p};
2147 } elsif ($p eq "old_id") {
2148 $old_id = $params->{$p};
2149 } elsif ($p eq "key") {
2150 $key = $params->{$p};
2156 $where = "WHERE active ";
2158 if (ref($old_id) eq "ARRAY") {
2159 my @ids = grep({ $_ } @{$old_id});
2161 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
2162 push(@values, @ids);
2165 $where .= " OR (id = ?) ";
2166 push(@values, $old_id);
2172 qq|SELECT id, projectnumber, description, active | .
2175 qq|ORDER BY lower(projectnumber)|;
2177 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2179 $main::lxdebug->leave_sub();
2183 $main::lxdebug->enter_sub();
2185 my ($self, $dbh, $vc_id, $key) = @_;
2187 $key = "all_shipto" unless ($key);
2190 # get shipping addresses
2191 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
2193 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
2199 $main::lxdebug->leave_sub();
2203 $main::lxdebug->enter_sub();
2205 my ($self, $dbh, $key) = @_;
2207 $key = "all_printers" unless ($key);
2209 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
2211 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2213 $main::lxdebug->leave_sub();
2217 $main::lxdebug->enter_sub();
2219 my ($self, $dbh, $params) = @_;
2222 $key = $params->{key};
2223 $key = "all_charts" unless ($key);
2225 my $transdate = quote_db_date($params->{transdate});
2228 qq|SELECT c.id, c.accno, c.description, c.link, c.charttype, tk.taxkey_id, tk.tax_id | .
2230 qq|LEFT JOIN taxkeys tk ON | .
2231 qq|(tk.id = (SELECT id FROM taxkeys | .
2232 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2233 qq| ORDER BY startdate DESC LIMIT 1)) | .
2234 qq|ORDER BY c.accno|;
2236 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2238 $main::lxdebug->leave_sub();
2241 sub _get_taxcharts {
2242 $main::lxdebug->enter_sub();
2244 my ($self, $dbh, $params) = @_;
2246 my $key = "all_taxcharts";
2249 if (ref $params eq 'HASH') {
2250 $key = $params->{key} if ($params->{key});
2251 if ($params->{module} eq 'AR') {
2252 push @where, 'taxkey NOT IN (8, 9, 18, 19)';
2254 } elsif ($params->{module} eq 'AP') {
2255 push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
2262 my $where = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (@where);
2264 my $query = qq|SELECT * FROM tax $where ORDER BY taxkey|;
2266 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2268 $main::lxdebug->leave_sub();
2272 $main::lxdebug->enter_sub();
2274 my ($self, $dbh, $key) = @_;
2276 $key = "all_taxzones" unless ($key);
2278 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2280 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2282 $main::lxdebug->leave_sub();
2285 sub _get_employees {
2286 $main::lxdebug->enter_sub();
2288 my ($self, $dbh, $default_key, $key) = @_;
2290 $key = $default_key unless ($key);
2291 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2293 $main::lxdebug->leave_sub();
2296 sub _get_business_types {
2297 $main::lxdebug->enter_sub();
2299 my ($self, $dbh, $key) = @_;
2301 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2302 $options->{key} ||= "all_business_types";
2305 if (exists $options->{salesman}) {
2306 $where = 'WHERE ' . ($options->{salesman} ? '' : 'NOT ') . 'COALESCE(salesman)';
2309 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, qq|SELECT * FROM business $where ORDER BY lower(description)|);
2311 $main::lxdebug->leave_sub();
2314 sub _get_languages {
2315 $main::lxdebug->enter_sub();
2317 my ($self, $dbh, $key) = @_;
2319 $key = "all_languages" unless ($key);
2321 my $query = qq|SELECT * FROM language ORDER BY id|;
2323 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2325 $main::lxdebug->leave_sub();
2328 sub _get_dunning_configs {
2329 $main::lxdebug->enter_sub();
2331 my ($self, $dbh, $key) = @_;
2333 $key = "all_dunning_configs" unless ($key);
2335 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2337 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2339 $main::lxdebug->leave_sub();
2342 sub _get_currencies {
2343 $main::lxdebug->enter_sub();
2345 my ($self, $dbh, $key) = @_;
2347 $key = "all_currencies" unless ($key);
2349 my $query = qq|SELECT curr AS currency FROM defaults|;
2351 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2353 $main::lxdebug->leave_sub();
2357 $main::lxdebug->enter_sub();
2359 my ($self, $dbh, $key) = @_;
2361 $key = "all_payments" unless ($key);
2363 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2365 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2367 $main::lxdebug->leave_sub();
2370 sub _get_customers {
2371 $main::lxdebug->enter_sub();
2373 my ($self, $dbh, $key) = @_;
2375 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2376 $options->{key} ||= "all_customers";
2377 my $limit_clause = "LIMIT $options->{limit}" if $options->{limit};
2380 push @where, qq|business_id IN (SELECT id FROM business WHERE salesman)| if $options->{business_is_salesman};
2381 push @where, qq|NOT obsolete| if !$options->{with_obsolete};
2382 my $where_str = @where ? "WHERE " . join(" AND ", map { "($_)" } @where) : '';
2384 my $query = qq|SELECT * FROM customer $where_str ORDER BY name $limit_clause|;
2385 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, $query);
2387 $main::lxdebug->leave_sub();
2391 $main::lxdebug->enter_sub();
2393 my ($self, $dbh, $key) = @_;
2395 $key = "all_vendors" unless ($key);
2397 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2399 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2401 $main::lxdebug->leave_sub();
2404 sub _get_departments {
2405 $main::lxdebug->enter_sub();
2407 my ($self, $dbh, $key) = @_;
2409 $key = "all_departments" unless ($key);
2411 my $query = qq|SELECT * FROM department ORDER BY description|;
2413 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2415 $main::lxdebug->leave_sub();
2418 sub _get_warehouses {
2419 $main::lxdebug->enter_sub();
2421 my ($self, $dbh, $param) = @_;
2423 my ($key, $bins_key);
2425 if ('' eq ref $param) {
2429 $key = $param->{key};
2430 $bins_key = $param->{bins};
2433 my $query = qq|SELECT w.* FROM warehouse w
2434 WHERE (NOT w.invalid) AND
2435 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2436 ORDER BY w.sortkey|;
2438 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2441 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2442 my $sth = prepare_query($self, $dbh, $query);
2444 foreach my $warehouse (@{ $self->{$key} }) {
2445 do_statement($self, $sth, $query, $warehouse->{id});
2446 $warehouse->{$bins_key} = [];
2448 while (my $ref = $sth->fetchrow_hashref()) {
2449 push @{ $warehouse->{$bins_key} }, $ref;
2455 $main::lxdebug->leave_sub();
2459 $main::lxdebug->enter_sub();
2461 my ($self, $dbh, $table, $key, $sortkey) = @_;
2463 my $query = qq|SELECT * FROM $table|;
2464 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2466 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2468 $main::lxdebug->leave_sub();
2472 # $main::lxdebug->enter_sub();
2474 # my ($self, $dbh, $key) = @_;
2476 # $key ||= "all_groups";
2478 # my $groups = $main::auth->read_groups();
2480 # $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2482 # $main::lxdebug->leave_sub();
2486 $main::lxdebug->enter_sub();
2491 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2492 my ($sth, $query, $ref);
2494 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2495 my $vc_id = $self->{"${vc}_id"};
2497 if ($params{"contacts"}) {
2498 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2501 if ($params{"shipto"}) {
2502 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2505 if ($params{"projects"} || $params{"all_projects"}) {
2506 $self->_get_projects($dbh, $params{"all_projects"} ?
2507 $params{"all_projects"} : $params{"projects"},
2508 $params{"all_projects"} ? 1 : 0);
2511 if ($params{"printers"}) {
2512 $self->_get_printers($dbh, $params{"printers"});
2515 if ($params{"languages"}) {
2516 $self->_get_languages($dbh, $params{"languages"});
2519 if ($params{"charts"}) {
2520 $self->_get_charts($dbh, $params{"charts"});
2523 if ($params{"taxcharts"}) {
2524 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2527 if ($params{"taxzones"}) {
2528 $self->_get_taxzones($dbh, $params{"taxzones"});
2531 if ($params{"employees"}) {
2532 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2535 if ($params{"salesmen"}) {
2536 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2539 if ($params{"business_types"}) {
2540 $self->_get_business_types($dbh, $params{"business_types"});
2543 if ($params{"dunning_configs"}) {
2544 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2547 if($params{"currencies"}) {
2548 $self->_get_currencies($dbh, $params{"currencies"});
2551 if($params{"customers"}) {
2552 $self->_get_customers($dbh, $params{"customers"});
2555 if($params{"vendors"}) {
2556 if (ref $params{"vendors"} eq 'HASH') {
2557 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2559 $self->_get_vendors($dbh, $params{"vendors"});
2563 if($params{"payments"}) {
2564 $self->_get_payments($dbh, $params{"payments"});
2567 if($params{"departments"}) {
2568 $self->_get_departments($dbh, $params{"departments"});
2571 if ($params{price_factors}) {
2572 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2575 if ($params{warehouses}) {
2576 $self->_get_warehouses($dbh, $params{warehouses});
2579 # if ($params{groups}) {
2580 # $self->_get_groups($dbh, $params{groups});
2583 if ($params{partsgroup}) {
2584 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2587 $main::lxdebug->leave_sub();
2590 # this sub gets the id and name from $table
2592 $main::lxdebug->enter_sub();
2594 my ($self, $myconfig, $table) = @_;
2596 # connect to database
2597 my $dbh = $self->get_standard_dbh($myconfig);
2599 $table = $table eq "customer" ? "customer" : "vendor";
2600 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2602 my ($query, @values);
2604 if (!$self->{openinvoices}) {
2606 if ($self->{customernumber} ne "") {
2607 $where = qq|(vc.customernumber ILIKE ?)|;
2608 push(@values, '%' . $self->{customernumber} . '%');
2610 $where = qq|(vc.name ILIKE ?)|;
2611 push(@values, '%' . $self->{$table} . '%');
2615 qq~SELECT vc.id, vc.name,
2616 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2618 WHERE $where AND (NOT vc.obsolete)
2622 qq~SELECT DISTINCT vc.id, vc.name,
2623 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2625 JOIN $table vc ON (a.${table}_id = vc.id)
2626 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2628 push(@values, '%' . $self->{$table} . '%');
2631 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2633 $main::lxdebug->leave_sub();
2635 return scalar(@{ $self->{name_list} });
2638 # the selection sub is used in the AR, AP, IS, IR and OE module
2641 $main::lxdebug->enter_sub();
2643 my ($self, $myconfig, $table, $module) = @_;
2646 my $dbh = $self->get_standard_dbh;
2648 $table = $table eq "customer" ? "customer" : "vendor";
2650 my $query = qq|SELECT count(*) FROM $table|;
2651 my ($count) = selectrow_query($self, $dbh, $query);
2653 # build selection list
2654 if ($count <= $myconfig->{vclimit}) {
2655 $query = qq|SELECT id, name, salesman_id
2656 FROM $table WHERE NOT obsolete
2658 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2662 $self->get_employee($dbh);
2664 # setup sales contacts
2665 $query = qq|SELECT e.id, e.name
2667 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2668 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2671 push(@{ $self->{all_employees} },
2672 { id => $self->{employee_id},
2673 name => $self->{employee} });
2675 # sort the whole thing
2676 @{ $self->{all_employees} } =
2677 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2679 if ($module eq 'AR') {
2681 # prepare query for departments
2682 $query = qq|SELECT id, description
2685 ORDER BY description|;
2688 $query = qq|SELECT id, description
2690 ORDER BY description|;
2693 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2696 $query = qq|SELECT id, description
2700 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2703 $query = qq|SELECT printer_description, id
2705 ORDER BY printer_description|;
2707 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2710 $query = qq|SELECT id, description
2714 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2716 $main::lxdebug->leave_sub();
2719 sub language_payment {
2720 $main::lxdebug->enter_sub();
2722 my ($self, $myconfig) = @_;
2724 my $dbh = $self->get_standard_dbh($myconfig);
2726 my $query = qq|SELECT id, description
2730 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2733 $query = qq|SELECT printer_description, id
2735 ORDER BY printer_description|;
2737 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2740 $query = qq|SELECT id, description
2744 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2746 # get buchungsgruppen
2747 $query = qq|SELECT id, description
2748 FROM buchungsgruppen|;
2750 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2752 $main::lxdebug->leave_sub();
2755 # this is only used for reports
2756 sub all_departments {
2757 $main::lxdebug->enter_sub();
2759 my ($self, $myconfig, $table) = @_;
2761 my $dbh = $self->get_standard_dbh($myconfig);
2764 if ($table eq 'customer') {
2765 $where = "WHERE role = 'P' ";
2768 my $query = qq|SELECT id, description
2771 ORDER BY description|;
2772 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2774 delete($self->{all_departments}) unless (@{ $self->{all_departments} || [] });
2776 $main::lxdebug->leave_sub();
2780 $main::lxdebug->enter_sub();
2782 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2785 if ($table eq "customer") {
2794 $self->all_vc($myconfig, $table, $module);
2796 # get last customers or vendors
2797 my ($query, $sth, $ref);
2799 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2804 my $transdate = "current_date";
2805 if ($self->{transdate}) {
2806 $transdate = $dbh->quote($self->{transdate});
2809 # now get the account numbers
2810 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2811 FROM chart c, taxkeys tk
2812 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2813 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2816 $sth = $dbh->prepare($query);
2818 do_statement($self, $sth, $query, '%' . $module . '%');
2820 $self->{accounts} = "";
2821 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2823 foreach my $key (split(/:/, $ref->{link})) {
2824 if ($key =~ /\Q$module\E/) {
2826 # cross reference for keys
2827 $xkeyref{ $ref->{accno} } = $key;
2829 push @{ $self->{"${module}_links"}{$key} },
2830 { accno => $ref->{accno},
2831 description => $ref->{description},
2832 taxkey => $ref->{taxkey_id},
2833 tax_id => $ref->{tax_id} };
2835 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2841 # get taxkeys and description
2842 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2843 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2845 if (($module eq "AP") || ($module eq "AR")) {
2846 # get tax rates and description
2847 $query = qq|SELECT * FROM tax|;
2848 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2854 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2855 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2856 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2857 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2859 d.description AS department,
2862 JOIN $table c ON (a.${table}_id = c.id)
2863 LEFT JOIN employee e ON (e.id = a.employee_id)
2864 LEFT JOIN department d ON (d.id = a.department_id)
2866 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2868 foreach my $key (keys %$ref) {
2869 $self->{$key} = $ref->{$key};
2872 my $transdate = "current_date";
2873 if ($self->{transdate}) {
2874 $transdate = $dbh->quote($self->{transdate});
2877 # now get the account numbers
2878 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2880 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2882 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2883 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2886 $sth = $dbh->prepare($query);
2887 do_statement($self, $sth, $query, "%$module%");
2889 $self->{accounts} = "";
2890 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2892 foreach my $key (split(/:/, $ref->{link})) {
2893 if ($key =~ /\Q$module\E/) {
2895 # cross reference for keys
2896 $xkeyref{ $ref->{accno} } = $key;
2898 push @{ $self->{"${module}_links"}{$key} },
2899 { accno => $ref->{accno},
2900 description => $ref->{description},
2901 taxkey => $ref->{taxkey_id},
2902 tax_id => $ref->{tax_id} };
2904 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2910 # get amounts from individual entries
2913 c.accno, c.description,
2914 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2918 LEFT JOIN chart c ON (c.id = a.chart_id)
2919 LEFT JOIN project p ON (p.id = a.project_id)
2920 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2921 WHERE (tk.taxkey_id=a.taxkey) AND
2922 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2923 THEN tk.chart_id = a.chart_id
2926 OR (c.link='%tax%')) AND
2927 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2928 WHERE a.trans_id = ?
2929 AND a.fx_transaction = '0'
2930 ORDER BY a.acc_trans_id, a.transdate|;
2931 $sth = $dbh->prepare($query);
2932 do_statement($self, $sth, $query, $self->{id});
2934 # get exchangerate for currency
2935 $self->{exchangerate} =
2936 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2939 # store amounts in {acc_trans}{$key} for multiple accounts
2940 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2941 $ref->{exchangerate} =
2942 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2943 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2946 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2947 $ref->{amount} *= -1;
2949 $ref->{index} = $index;
2951 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2957 d.curr AS currencies, d.closedto, d.revtrans,
2958 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2959 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2961 $ref = selectfirst_hashref_query($self, $dbh, $query);
2962 map { $self->{$_} = $ref->{$_} } keys %$ref;
2969 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2970 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2971 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2973 $ref = selectfirst_hashref_query($self, $dbh, $query);
2974 map { $self->{$_} = $ref->{$_} } keys %$ref;
2976 if ($self->{"$self->{vc}_id"}) {
2978 # only setup currency
2979 ($self->{currency}) = split(/:/, $self->{currencies});
2983 $self->lastname_used($dbh, $myconfig, $table, $module);
2985 # get exchangerate for currency
2986 $self->{exchangerate} =
2987 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2993 $main::lxdebug->leave_sub();
2997 $main::lxdebug->enter_sub();
2999 my ($self, $dbh, $myconfig, $table, $module) = @_;
3003 $table = $table eq "customer" ? "customer" : "vendor";
3004 my %column_map = ("a.curr" => "currency",
3005 "a.${table}_id" => "${table}_id",
3006 "a.department_id" => "department_id",
3007 "d.description" => "department",
3008 "ct.name" => $table,
3009 "current_date + ct.terms" => "duedate",
3012 if ($self->{type} =~ /delivery_order/) {
3013 $arap = 'delivery_orders';
3014 delete $column_map{"a.curr"};
3016 } elsif ($self->{type} =~ /_order/) {
3018 $where = "quotation = '0'";
3020 } elsif ($self->{type} =~ /_quotation/) {
3022 $where = "quotation = '1'";
3024 } elsif ($table eq 'customer') {
3032 $where = "($where) AND" if ($where);
3033 my $query = qq|SELECT MAX(id) FROM $arap
3034 WHERE $where ${table}_id > 0|;
3035 my ($trans_id) = selectrow_query($self, $dbh, $query);
3038 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
3039 $query = qq|SELECT $column_spec
3041 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
3042 LEFT JOIN department d ON (a.department_id = d.id)
3044 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
3046 map { $self->{$_} = $ref->{$_} } values %column_map;
3048 $main::lxdebug->leave_sub();
3052 $main::lxdebug->enter_sub();
3055 my $myconfig = shift || \%::myconfig;
3056 my ($thisdate, $days) = @_;
3058 my $dbh = $self->get_standard_dbh($myconfig);
3063 my $dateformat = $myconfig->{dateformat};
3064 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
3065 $thisdate = $dbh->quote($thisdate);
3066 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
3068 $query = qq|SELECT current_date AS thisdate|;
3071 ($thisdate) = selectrow_query($self, $dbh, $query);
3073 $main::lxdebug->leave_sub();
3079 $main::lxdebug->enter_sub();
3081 my ($self, $string) = @_;
3083 if ($string !~ /%/) {
3084 $string = "%$string%";
3087 $string =~ s/\'/\'\'/g;
3089 $main::lxdebug->leave_sub();
3095 $main::lxdebug->enter_sub();
3097 my ($self, $flds, $new, $count, $numrows) = @_;
3101 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
3106 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
3108 my $j = $item->{ndx} - 1;
3109 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
3113 for $i ($count + 1 .. $numrows) {
3114 map { delete $self->{"${_}_$i"} } @{$flds};
3117 $main::lxdebug->leave_sub();
3121 $main::lxdebug->enter_sub();
3123 my ($self, $myconfig) = @_;
3127 my $dbh = $self->dbconnect_noauto($myconfig);
3129 my $query = qq|DELETE FROM status
3130 WHERE (formname = ?) AND (trans_id = ?)|;
3131 my $sth = prepare_query($self, $dbh, $query);
3133 if ($self->{formname} =~ /(check|receipt)/) {
3134 for $i (1 .. $self->{rowcount}) {
3135 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
3138 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
3142 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3143 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3145 my %queued = split / /, $self->{queued};
3148 if ($self->{formname} =~ /(check|receipt)/) {
3150 # this is a check or receipt, add one entry for each lineitem
3151 my ($accno) = split /--/, $self->{account};
3152 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
3153 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
3154 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
3155 $sth = prepare_query($self, $dbh, $query);
3157 for $i (1 .. $self->{rowcount}) {
3158 if ($self->{"checked_$i"}) {
3159 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
3165 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3166 VALUES (?, ?, ?, ?, ?)|;
3167 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
3168 $queued{$self->{formname}}, $self->{formname});
3174 $main::lxdebug->leave_sub();
3178 $main::lxdebug->enter_sub();
3180 my ($self, $dbh) = @_;
3182 my ($query, $printed, $emailed);
3184 my $formnames = $self->{printed};
3185 my $emailforms = $self->{emailed};
3187 $query = qq|DELETE FROM status
3188 WHERE (formname = ?) AND (trans_id = ?)|;
3189 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
3191 # this only applies to the forms
3192 # checks and receipts are posted when printed or queued
3194 if ($self->{queued}) {
3195 my %queued = split / /, $self->{queued};
3197 foreach my $formname (keys %queued) {
3198 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3199 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3201 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3202 VALUES (?, ?, ?, ?, ?)|;
3203 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
3205 $formnames =~ s/\Q$self->{formname}\E//;
3206 $emailforms =~ s/\Q$self->{formname}\E//;
3211 # save printed, emailed info
3212 $formnames =~ s/^ +//g;
3213 $emailforms =~ s/^ +//g;
3216 map { $status{$_}{printed} = 1 } split / +/, $formnames;
3217 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
3219 foreach my $formname (keys %status) {
3220 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
3221 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
3223 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
3224 VALUES (?, ?, ?, ?)|;
3225 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
3228 $main::lxdebug->leave_sub();
3232 # $main::locale->text('SAVED')
3233 # $main::locale->text('DELETED')
3234 # $main::locale->text('ADDED')
3235 # $main::locale->text('PAYMENT POSTED')
3236 # $main::locale->text('POSTED')
3237 # $main::locale->text('POSTED AS NEW')
3238 # $main::locale->text('ELSE')
3239 # $main::locale->text('SAVED FOR DUNNING')
3240 # $main::locale->text('DUNNING STARTED')
3241 # $main::locale->text('PRINTED')
3242 # $main::locale->text('MAILED')
3243 # $main::locale->text('SCREENED')
3244 # $main::locale->text('CANCELED')
3245 # $main::locale->text('invoice')
3246 # $main::locale->text('proforma')
3247 # $main::locale->text('sales_order')
3248 # $main::locale->text('packing_list')
3249 # $main::locale->text('pick_list')
3250 # $main::locale->text('purchase_order')
3251 # $main::locale->text('bin_list')
3252 # $main::locale->text('sales_quotation')
3253 # $main::locale->text('request_quotation')
3256 $main::lxdebug->enter_sub();
3259 my $dbh = shift || $self->get_standard_dbh;
3261 if(!exists $self->{employee_id}) {
3262 &get_employee($self, $dbh);
3266 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3267 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3268 my @values = (conv_i($self->{id}), $self->{login},
3269 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3270 do_query($self, $dbh, $query, @values);
3274 $main::lxdebug->leave_sub();
3278 $main::lxdebug->enter_sub();
3280 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3281 my ($orderBy, $desc) = split(/\-\-/, $order);
3282 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3285 if ($trans_id ne "") {
3287 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 | .
3288 qq|FROM history_erp h | .
3289 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3290 qq|WHERE (trans_id = | . $trans_id . qq|) $restriction | .
3293 my $sth = $dbh->prepare($query) || $self->dberror($query);
3295 $sth->execute() || $self->dberror("$query");
3297 while(my $hash_ref = $sth->fetchrow_hashref()) {
3298 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3299 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3300 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3301 $tempArray[$i++] = $hash_ref;
3303 $main::lxdebug->leave_sub() and return \@tempArray
3304 if ($i > 0 && $tempArray[0] ne "");
3306 $main::lxdebug->leave_sub();
3310 sub update_defaults {
3311 $main::lxdebug->enter_sub();
3313 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3316 if ($provided_dbh) {
3317 $dbh = $provided_dbh;
3319 $dbh = $self->dbconnect_noauto($myconfig);
3321 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3322 my $sth = $dbh->prepare($query);
3324 $sth->execute || $self->dberror($query);
3325 my ($var) = $sth->fetchrow_array;
3328 if ($var =~ m/\d+$/) {
3329 my $new_var = (substr $var, $-[0]) * 1 + 1;
3330 my $len_diff = length($var) - $-[0] - length($new_var);
3331 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3337 $query = qq|UPDATE defaults SET $fld = ?|;
3338 do_query($self, $dbh, $query, $var);
3340 if (!$provided_dbh) {
3345 $main::lxdebug->leave_sub();
3350 sub update_business {
3351 $main::lxdebug->enter_sub();
3353 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3356 if ($provided_dbh) {
3357 $dbh = $provided_dbh;
3359 $dbh = $self->dbconnect_noauto($myconfig);
3362 qq|SELECT customernumberinit FROM business
3363 WHERE id = ? FOR UPDATE|;
3364 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3366 return undef unless $var;
3368 if ($var =~ m/\d+$/) {
3369 my $new_var = (substr $var, $-[0]) * 1 + 1;
3370 my $len_diff = length($var) - $-[0] - length($new_var);
3371 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3377 $query = qq|UPDATE business
3378 SET customernumberinit = ?
3380 do_query($self, $dbh, $query, $var, $business_id);
3382 if (!$provided_dbh) {
3387 $main::lxdebug->leave_sub();
3392 sub get_partsgroup {
3393 $main::lxdebug->enter_sub();
3395 my ($self, $myconfig, $p) = @_;
3396 my $target = $p->{target} || 'all_partsgroup';
3398 my $dbh = $self->get_standard_dbh($myconfig);
3400 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3402 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3405 if ($p->{searchitems} eq 'part') {
3406 $query .= qq|WHERE p.inventory_accno_id > 0|;
3408 if ($p->{searchitems} eq 'service') {
3409 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3411 if ($p->{searchitems} eq 'assembly') {
3412 $query .= qq|WHERE p.assembly = '1'|;
3414 if ($p->{searchitems} eq 'labor') {
3415 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3418 $query .= qq|ORDER BY partsgroup|;
3421 $query = qq|SELECT id, partsgroup FROM partsgroup
3422 ORDER BY partsgroup|;
3425 if ($p->{language_code}) {
3426 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3427 t.description AS translation
3429 JOIN parts p ON (p.partsgroup_id = pg.id)
3430 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3431 ORDER BY translation|;
3432 @values = ($p->{language_code});
3435 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3437 $main::lxdebug->leave_sub();
3440 sub get_pricegroup {
3441 $main::lxdebug->enter_sub();
3443 my ($self, $myconfig, $p) = @_;
3445 my $dbh = $self->get_standard_dbh($myconfig);
3447 my $query = qq|SELECT p.id, p.pricegroup
3450 $query .= qq| ORDER BY pricegroup|;
3453 $query = qq|SELECT id, pricegroup FROM pricegroup
3454 ORDER BY pricegroup|;
3457 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3459 $main::lxdebug->leave_sub();
3463 # usage $form->all_years($myconfig, [$dbh])
3464 # return list of all years where bookings found
3467 $main::lxdebug->enter_sub();
3469 my ($self, $myconfig, $dbh) = @_;
3471 $dbh ||= $self->get_standard_dbh($myconfig);
3474 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3475 (SELECT MAX(transdate) FROM acc_trans)|;
3476 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3478 if ($myconfig->{dateformat} =~ /^yy/) {
3479 ($startdate) = split /\W/, $startdate;
3480 ($enddate) = split /\W/, $enddate;
3482 (@_) = split /\W/, $startdate;
3484 (@_) = split /\W/, $enddate;
3489 $startdate = substr($startdate,0,4);
3490 $enddate = substr($enddate,0,4);
3492 while ($enddate >= $startdate) {
3493 push @all_years, $enddate--;
3498 $main::lxdebug->leave_sub();
3502 $main::lxdebug->enter_sub();
3506 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if exists $self->{$_} } @vars;
3508 $main::lxdebug->leave_sub();
3512 $main::lxdebug->enter_sub();
3517 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if exists $self->{_VAR_BACKUP}->{$_} } @vars;
3519 $main::lxdebug->leave_sub();
3528 SL::Form.pm - main data object.
3532 This is the main data object of Lx-Office.
3533 Unfortunately it also acts as a god object for certain data retrieval procedures used in the entry points.
3534 Points of interest for a beginner are:
3536 - $form->error - renders a generic error in html. accepts an error message
3537 - $form->get_standard_dbh - returns a database connection for the
3539 =head1 SPECIAL FUNCTIONS
3543 =item _store_value()
3545 parses a complex var name, and stores it in the form.
3548 $form->_store_value($key, $value);
3550 keys must start with a string, and can contain various tokens.
3551 supported key structures are:
3554 simple key strings work as expected
3559 separating two keys by a dot (.) will result in a hash lookup for the inner value
3560 this is similar to the behaviour of java and templating mechanisms.
3562 filter.description => $form->{filter}->{description}
3564 3. array+hashref access
3566 adding brackets ([]) before the dot will cause the next hash to be put into an array.
3567 using [+] instead of [] will force a new array index. this is useful for recurring
3568 data structures like part lists. put a [+] into the first varname, and use [] on the
3571 repeating these names in your template:
3574 invoice.items[].parts_id
3578 $form->{invoice}->{items}->[
3592 using brackets at the end of a name will result in a pure array to be created.
3593 note that you mustn't use [+], which is reserved for array+hash access and will
3594 result in undefined behaviour in array context.
3596 filter.status[] => $form->{status}->[ val1, val2, ... ]
3598 =item update_business PARAMS
3601 \%config, - config hashref
3602 $business_id, - business id
3603 $dbh - optional database handle
3605 handles business (thats customer/vendor types) sequences.
3607 special behaviour for empty strings in customerinitnumber field:
3608 will in this case not increase the value, and return undef.
3610 =item redirect_header $url
3612 Generates a HTTP redirection header for the new C<$url>. Constructs an
3613 absolute URL including scheme, host name and port. If C<$url> is a
3614 relative URL then it is considered relative to Lx-Office base URL.
3616 This function C<die>s if headers have already been created with
3617 C<$::form-E<gt>header>.
3621 print $::form->redirect_header('oe.pl?action=edit&id=1234');
3622 print $::form->redirect_header('http://www.lx-office.org/');