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}) {
473 <p class="message_ok"><b>$msg</b></p>
475 <script type="text/javascript">
477 setTimeout("parent.frames.main_window.location.href='login.pl?action=company_logo'",1000);
486 if ($self->{info_function}) {
487 &{ $self->{info_function} }($msg);
493 $main::lxdebug->leave_sub();
496 # calculates the number of rows in a textarea based on the content and column number
497 # can be capped with maxrows
499 $main::lxdebug->enter_sub();
500 my ($self, $str, $cols, $maxrows, $minrows) = @_;
504 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
507 $main::lxdebug->leave_sub();
509 return max(min($rows, $maxrows), $minrows);
513 $main::lxdebug->enter_sub();
515 my ($self, $msg) = @_;
517 $self->error("$msg\n" . $DBI::errstr);
519 $main::lxdebug->leave_sub();
523 $main::lxdebug->enter_sub();
525 my ($self, $name, $msg) = @_;
528 foreach my $part (split m/\./, $name) {
529 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
532 $curr = $curr->{$part};
535 $main::lxdebug->leave_sub();
538 sub _get_request_uri {
541 return URI->new($ENV{HTTP_REFERER})->canonical() if $ENV{HTTP_X_FORWARDED_FOR};
543 my $scheme = $ENV{HTTPS} && (lc $ENV{HTTPS} eq 'on') ? 'https' : 'http';
544 my $port = $ENV{SERVER_PORT} || '';
545 $port = undef if (($scheme eq 'http' ) && ($port == 80))
546 || (($scheme eq 'https') && ($port == 443));
548 my $uri = URI->new("${scheme}://");
549 $uri->scheme($scheme);
551 $uri->host($ENV{HTTP_HOST} || $ENV{SERVER_ADDR});
552 $uri->path_query($ENV{REQUEST_URI});
558 sub _add_to_request_uri {
561 my $relative_new_path = shift;
562 my $request_uri = shift || $self->_get_request_uri;
563 my $relative_new_uri = URI->new($relative_new_path);
564 my @request_segments = $request_uri->path_segments;
566 my $new_uri = $request_uri->clone;
567 $new_uri->path_segments(@request_segments[0..scalar(@request_segments) - 2], $relative_new_uri->path_segments);
572 sub create_http_response {
573 $main::lxdebug->enter_sub();
578 my $cgi = $main::cgi;
579 $cgi ||= CGI->new('');
582 if (defined $main::auth) {
583 my $uri = $self->_get_request_uri;
584 my @segments = $uri->path_segments;
586 $uri->path_segments(@segments);
588 my $session_cookie_value = $main::auth->get_session_id();
589 $session_cookie_value ||= 'NO_SESSION';
591 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
592 '-value' => $session_cookie_value,
593 '-path' => $uri->path,
594 '-secure' => $ENV{HTTPS});
597 my %cgi_params = ('-type' => $params{content_type});
598 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
600 my $output = $cgi->header('-cookie' => $session_cookie,
603 $main::lxdebug->leave_sub();
610 $main::lxdebug->enter_sub();
612 # extra code ist currently only used by menuv3 and menuv4 to set their css.
613 # it is strongly deprecated, and will be changed in a future version.
614 my ($self, $extra_code) = @_;
616 if ($self->{header}) {
617 $main::lxdebug->leave_sub();
621 my ($stylesheet, $favicon, $pagelayout);
623 if ($ENV{HTTP_USER_AGENT}) {
626 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
627 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
628 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
631 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
633 $stylesheets =~ s|^\s*||;
634 $stylesheets =~ s|\s*$||;
635 foreach my $file (split m/\s+/, $stylesheets) {
637 next if (! -f "css/$file");
639 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
642 $self->{favicon} = "favicon.ico" unless $self->{favicon};
644 if ($self->{favicon} && (-f "$self->{favicon}")) {
646 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
650 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
652 if ($self->{landscape}) {
653 $pagelayout = qq|<style type="text/css">
654 \@page { size:landscape; }
659 <script type="text/javascript">
662 document.$self->{fokus}.focus();
666 | if $self->{"fokus"};
670 if ($self->{jsscript} == 1) {
673 <script type="text/javascript" src="js/jquery.js"></script>
674 <script type="text/javascript" src="js/common.js"></script>
675 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
676 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
677 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
678 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
685 ? "$self->{title} - $self->{titlebar}"
688 for my $item (@ { $self->{AJAX} || [] }) {
689 $ajax .= $item->show_javascript();
692 print $self->create_http_response('content_type' => 'text/html',
693 'charset' => $db_charset,);
694 print qq|${doctype}<html>
696 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
697 <title>$self->{titlebar}</title>
706 <link rel="stylesheet" href="css/jquery.autocomplete.css" type="text/css" />
708 <meta name="robots" content="noindex,nofollow" />
709 <script type="text/javascript" src="js/highlight_input.js"></script>
711 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
712 <script type="text/javascript" src="js/tabcontent.js">
714 /***********************************************
715 * Tab Content script v2.2- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
716 * This notice MUST stay intact for legal use
717 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
718 ***********************************************/
729 $main::lxdebug->leave_sub();
732 sub ajax_response_header {
733 $main::lxdebug->enter_sub();
737 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
738 my $cgi = $main::cgi || CGI->new('');
739 my $output = $cgi->header('-charset' => $db_charset);
741 $main::lxdebug->leave_sub();
746 sub redirect_header {
750 my $base_uri = $self->_get_request_uri;
751 my $new_uri = URI->new_abs($new_url, $base_uri);
753 die "Headers already sent" if $::self->{header};
756 my $cgi = $main::cgi || CGI->new('');
757 return $cgi->redirect($new_uri);
760 sub set_standard_title {
761 $::lxdebug->enter_sub;
764 $self->{titlebar} = "Lx-Office " . $::locale->text('Version') . " $self->{version}";
765 $self->{titlebar} .= "- $::myconfig{name}" if $::myconfig{name};
766 $self->{titlebar} .= "- $::myconfig{dbname}" if $::myconfig{name};
768 $::lxdebug->leave_sub;
771 sub _prepare_html_template {
772 $main::lxdebug->enter_sub();
774 my ($self, $file, $additional_params) = @_;
777 if (!%::myconfig || !$::myconfig{"countrycode"}) {
778 $language = $main::language;
780 $language = $main::myconfig{"countrycode"};
782 $language = "de" unless ($language);
784 if (-f "templates/webpages/${file}.html") {
785 if ((-f ".developer") && ((stat("templates/webpages/${file}.html"))[9] > (stat("locale/${language}/all"))[9])) {
786 my $info = "Developer information: templates/webpages/${file}.html is newer than the translation file locale/${language}/all.\n" .
787 "Please re-run 'locales.pl' in 'locale/${language}'.";
788 print(qq|<pre>$info</pre>|);
792 $file = "templates/webpages/${file}.html";
795 my $info = "Web page template '${file}' not found.\n" .
796 "Please re-run 'locales.pl' in 'locale/${language}'.";
797 print(qq|<pre>$info</pre>|);
801 if ($self->{"DEBUG"}) {
802 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
805 if ($additional_params->{"DEBUG"}) {
806 $additional_params->{"DEBUG"} =
807 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
810 if (%main::myconfig) {
811 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
812 my $jsc_dateformat = $main::myconfig{"dateformat"};
813 $jsc_dateformat =~ s/d+/\%d/gi;
814 $jsc_dateformat =~ s/m+/\%m/gi;
815 $jsc_dateformat =~ s/y+/\%Y/gi;
816 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
817 $additional_params->{"myconfig"} ||= \%::myconfig;
820 $additional_params->{"conf_dbcharset"} = $main::dbcharset;
821 $additional_params->{"conf_webdav"} = $main::webdav;
822 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
823 $additional_params->{"conf_latex_templates"} = $main::latex;
824 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
825 $additional_params->{"conf_vertreter"} = $main::vertreter;
826 $additional_params->{"conf_show_best_before"} = $main::show_best_before;
828 if (%main::debug_options) {
829 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
832 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
833 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
834 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
838 $main::lxdebug->leave_sub();
843 sub parse_html_template {
844 $main::lxdebug->enter_sub();
846 my ($self, $file, $additional_params) = @_;
848 $additional_params ||= { };
850 my $real_file = $self->_prepare_html_template($file, $additional_params);
851 my $template = $self->template || $self->init_template;
853 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
856 $template->process($real_file, $additional_params, \$output) || die $template->error;
858 $main::lxdebug->leave_sub();
866 return if $self->template;
868 return $self->template(Template->new({
873 'PLUGIN_BASE' => 'SL::Template::Plugin',
874 'INCLUDE_PATH' => '.:templates/webpages',
875 'COMPILE_EXT' => '.tcc',
876 'COMPILE_DIR' => $::userspath . '/templates-cache',
882 $self->{template_object} = shift if @_;
883 return $self->{template_object};
886 sub show_generic_error {
887 $main::lxdebug->enter_sub();
889 my ($self, $error, %params) = @_;
892 'title_error' => $params{title},
893 'label_error' => $error,
896 if ($params{action}) {
899 map { delete($self->{$_}); } qw(action);
900 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
902 $add_params->{SHOW_BUTTON} = 1;
903 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
904 $add_params->{VARIABLES} = \@vars;
906 } elsif ($params{back_button}) {
907 $add_params->{SHOW_BACK_BUTTON} = 1;
910 $self->{title} = $params{title} if $params{title};
913 print $self->parse_html_template("generic/error", $add_params);
915 print STDERR "Error: $error\n";
917 $main::lxdebug->leave_sub();
922 sub show_generic_information {
923 $main::lxdebug->enter_sub();
925 my ($self, $text, $title) = @_;
928 'title_information' => $title,
929 'label_information' => $text,
932 $self->{title} = $title if ($title);
935 print $self->parse_html_template("generic/information", $add_params);
937 $main::lxdebug->leave_sub();
942 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
943 # changed it to accept an arbitrary number of triggers - sschoeling
945 $main::lxdebug->enter_sub();
948 my $myconfig = shift;
951 # set dateform for jsscript
954 "dd.mm.yy" => "%d.%m.%Y",
955 "dd-mm-yy" => "%d-%m-%Y",
956 "dd/mm/yy" => "%d/%m/%Y",
957 "mm/dd/yy" => "%m/%d/%Y",
958 "mm-dd-yy" => "%m-%d-%Y",
959 "yyyy-mm-dd" => "%Y-%m-%d",
962 my $ifFormat = defined($dateformats{$myconfig->{"dateformat"}}) ?
963 $dateformats{$myconfig->{"dateformat"}} : "%d.%m.%Y";
970 inputField : "| . (shift) . qq|",
971 ifFormat :"$ifFormat",
972 align : "| . (shift) . qq|",
973 button : "| . (shift) . qq|"
979 <script type="text/javascript">
980 <!--| . join("", @triggers) . qq|//-->
984 $main::lxdebug->leave_sub();
987 } #end sub write_trigger
990 $main::lxdebug->enter_sub();
992 my ($self, $msg) = @_;
994 if (!$self->{callback}) {
1000 # my ($script, $argv) = split(/\?/, $self->{callback}, 2);
1001 # $script =~ s|.*/||;
1002 # $script =~ s|[^a-zA-Z0-9_\.]||g;
1003 # exec("perl", "$script", $argv);
1005 print $::form->redirect_header($self->{callback});
1007 $main::lxdebug->leave_sub();
1010 # sort of columns removed - empty sub
1012 $main::lxdebug->enter_sub();
1014 my ($self, @columns) = @_;
1016 $main::lxdebug->leave_sub();
1022 $main::lxdebug->enter_sub(2);
1024 my ($self, $myconfig, $amount, $places, $dash) = @_;
1026 if ($amount eq "") {
1030 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
1032 my $neg = ($amount =~ s/^-//);
1033 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
1035 if (defined($places) && ($places ne '')) {
1041 my ($actual_places) = ($amount =~ /\.(\d+)/);
1042 $actual_places = length($actual_places);
1043 $places = $actual_places > $places ? $actual_places : $places;
1046 $amount = $self->round_amount($amount, $places);
1049 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
1050 my @p = split(/\./, $amount); # split amount at decimal point
1052 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
1055 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
1058 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
1059 ($dash =~ /DRCR/) ? ($neg ? "$amount " . $main::locale->text('DR') : "$amount " . $main::locale->text('CR') ) :
1060 ($neg ? "-$amount" : "$amount" ) ;
1064 $main::lxdebug->leave_sub(2);
1068 sub format_amount_units {
1069 $main::lxdebug->enter_sub();
1074 my $myconfig = \%main::myconfig;
1075 my $amount = $params{amount} * 1;
1076 my $places = $params{places};
1077 my $part_unit_name = $params{part_unit};
1078 my $amount_unit_name = $params{amount_unit};
1079 my $conv_units = $params{conv_units};
1080 my $max_places = $params{max_places};
1082 if (!$part_unit_name) {
1083 $main::lxdebug->leave_sub();
1087 AM->retrieve_all_units();
1088 my $all_units = $main::all_units;
1090 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
1091 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
1094 if (!scalar @{ $conv_units }) {
1095 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
1096 $main::lxdebug->leave_sub();
1100 my $part_unit = $all_units->{$part_unit_name};
1101 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
1103 $amount *= $conv_unit->{factor};
1108 foreach my $unit (@$conv_units) {
1109 my $last = $unit->{name} eq $part_unit->{name};
1111 $num = int($amount / $unit->{factor});
1112 $amount -= $num * $unit->{factor};
1115 if ($last ? $amount : $num) {
1116 push @values, { "unit" => $unit->{name},
1117 "amount" => $last ? $amount / $unit->{factor} : $num,
1118 "places" => $last ? $places : 0 };
1125 push @values, { "unit" => $part_unit_name,
1130 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
1132 $main::lxdebug->leave_sub();
1138 $main::lxdebug->enter_sub(2);
1143 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
1144 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
1145 $input =~ s/\#\#/\#/g;
1147 $main::lxdebug->leave_sub(2);
1155 $main::lxdebug->enter_sub(2);
1157 my ($self, $myconfig, $amount) = @_;
1159 if ( ($myconfig->{numberformat} eq '1.000,00')
1160 || ($myconfig->{numberformat} eq '1000,00')) {
1165 if ($myconfig->{numberformat} eq "1'000.00") {
1171 $main::lxdebug->leave_sub(2);
1173 return ($amount * 1);
1177 $main::lxdebug->enter_sub(2);
1179 my ($self, $amount, $places) = @_;
1182 # Rounding like "Kaufmannsrunden" (see http://de.wikipedia.org/wiki/Rundung )
1184 # Round amounts to eight places before rounding to the requested
1185 # number of places. This gets rid of errors due to internal floating
1186 # point representation.
1187 $amount = $self->round_amount($amount, 8) if $places < 8;
1188 $amount = $amount * (10**($places));
1189 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1191 $main::lxdebug->leave_sub(2);
1193 return $round_amount;
1197 sub parse_template {
1198 $main::lxdebug->enter_sub();
1200 my ($self, $myconfig, $userspath) = @_;
1205 $self->{"cwd"} = getcwd();
1206 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1211 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1212 $template_type = 'OpenDocument';
1213 $ext_for_format = $self->{"format"} =~ m/pdf/ ? 'pdf' : 'odt';
1215 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1216 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1217 $template_type = 'LaTeX';
1218 $ext_for_format = 'pdf';
1220 } elsif (($self->{"format"} =~ /html/i) || (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1221 $template_type = 'HTML';
1222 $ext_for_format = 'html';
1224 } elsif (($self->{"format"} =~ /xml/i) || (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1225 $template_type = 'XML';
1226 $ext_for_format = 'xml';
1228 } elsif ( $self->{"format"} =~ /elster(?:winston|taxbird)/i ) {
1229 $template_type = 'xml';
1231 } elsif ( $self->{"format"} =~ /excel/i ) {
1232 $template_type = 'Excel';
1233 $ext_for_format = 'xls';
1235 } elsif ( defined $self->{'format'}) {
1236 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1238 } elsif ( $self->{'format'} eq '' ) {
1239 $self->error("No Outputformat given: $self->{'format'}");
1241 } else { #Catch the rest
1242 $self->error("Outputformat not defined: $self->{'format'}");
1245 my $template = SL::Template::create(type => $template_type,
1246 file_name => $self->{IN},
1248 myconfig => $myconfig,
1249 userspath => $userspath);
1251 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1252 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1254 if (!$self->{employee_id}) {
1255 map { $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature company address businessnumber co_ustid taxnumber duns);
1258 map { $self->{"${_}"} = $myconfig->{$_}; } qw(co_ustid);
1260 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1262 # OUT is used for the media, screen, printer, email
1263 # for postscript we store a copy in a temporary file
1265 my $prepend_userspath;
1267 if (!$self->{tmpfile}) {
1268 $self->{tmpfile} = "${fileid}.$self->{IN}";
1269 $prepend_userspath = 1;
1272 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1274 $self->{tmpfile} =~ s|.*/||;
1275 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1276 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1278 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1279 $out = $self->{OUT};
1280 $self->{OUT} = ">$self->{tmpfile}";
1286 open OUT, "$self->{OUT}" or $self->error("$self->{OUT} : $!");
1287 $result = $template->parse(*OUT);
1292 $result = $template->parse(*STDOUT);
1297 $self->error("$self->{IN} : " . $template->get_error());
1300 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1302 if ($self->{media} eq 'email') {
1304 my $mail = new Mailer;
1306 map { $mail->{$_} = $self->{$_} }
1307 qw(cc bcc subject message version format);
1308 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1309 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1310 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1311 $mail->{fileid} = "$fileid.";
1312 $myconfig->{signature} =~ s/\r//g;
1314 # if we send html or plain text inline
1315 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1316 $mail->{contenttype} = "text/html";
1318 $mail->{message} =~ s/\r//g;
1319 $mail->{message} =~ s/\n/<br>\n/g;
1320 $myconfig->{signature} =~ s/\n/<br>\n/g;
1321 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1323 open(IN, $self->{tmpfile})
1324 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1326 $mail->{message} .= $_;
1333 if (!$self->{"do_not_attach"}) {
1334 my $attachment_name = $self->{attachment_filename} || $self->{tmpfile};
1335 $attachment_name =~ s/\.(.+?)$/.${ext_for_format}/ if ($ext_for_format);
1336 $mail->{attachments} = [{ "filename" => $self->{tmpfile},
1337 "name" => $attachment_name }];
1340 $mail->{message} =~ s/\r//g;
1341 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1345 my $err = $mail->send();
1346 $self->error($self->cleanup . "$err") if ($err);
1350 $self->{OUT} = $out;
1352 my $numbytes = (-s $self->{tmpfile});
1353 open(IN, $self->{tmpfile})
1354 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1356 $self->{copies} = 1 unless $self->{media} eq 'printer';
1358 chdir("$self->{cwd}");
1359 #print(STDERR "Kopien $self->{copies}\n");
1360 #print(STDERR "OUT $self->{OUT}\n");
1361 for my $i (1 .. $self->{copies}) {
1363 open OUT, $self->{OUT} or $self->error($self->cleanup . "$self->{OUT} : $!");
1364 print OUT while <IN>;
1369 $self->{attachment_filename} = ($self->{attachment_filename})
1370 ? $self->{attachment_filename}
1371 : $self->generate_attachment_filename();
1373 # launch application
1374 print qq|Content-Type: | . $template->get_mime_type() . qq|
1375 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1376 Content-Length: $numbytes
1380 $::locale->with_raw_io(\*STDOUT, sub { print while <IN> });
1391 chdir("$self->{cwd}");
1392 $main::lxdebug->leave_sub();
1395 sub get_formname_translation {
1396 $main::lxdebug->enter_sub();
1397 my ($self, $formname) = @_;
1399 $formname ||= $self->{formname};
1401 my %formname_translations = (
1402 bin_list => $main::locale->text('Bin List'),
1403 credit_note => $main::locale->text('Credit Note'),
1404 invoice => $main::locale->text('Invoice'),
1405 packing_list => $main::locale->text('Packing List'),
1406 pick_list => $main::locale->text('Pick List'),
1407 proforma => $main::locale->text('Proforma Invoice'),
1408 purchase_order => $main::locale->text('Purchase Order'),
1409 request_quotation => $main::locale->text('RFQ'),
1410 sales_order => $main::locale->text('Confirmation'),
1411 sales_quotation => $main::locale->text('Quotation'),
1412 storno_invoice => $main::locale->text('Storno Invoice'),
1413 storno_packing_list => $main::locale->text('Storno Packing List'),
1414 sales_delivery_order => $main::locale->text('Delivery Order'),
1415 purchase_delivery_order => $main::locale->text('Delivery Order'),
1416 dunning => $main::locale->text('Dunning'),
1419 $main::lxdebug->leave_sub();
1420 return $formname_translations{$formname}
1423 sub get_number_prefix_for_type {
1424 $main::lxdebug->enter_sub();
1428 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1429 : ($self->{type} =~ /_quotation$/) ? 'quo'
1430 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1433 $main::lxdebug->leave_sub();
1437 sub get_extension_for_format {
1438 $main::lxdebug->enter_sub();
1441 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1442 : $self->{format} =~ /postscript/i ? ".ps"
1443 : $self->{format} =~ /opendocument/i ? ".odt"
1444 : $self->{format} =~ /excel/i ? ".xls"
1445 : $self->{format} =~ /html/i ? ".html"
1448 $main::lxdebug->leave_sub();
1452 sub generate_attachment_filename {
1453 $main::lxdebug->enter_sub();
1456 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1457 my $prefix = $self->get_number_prefix_for_type();
1459 if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
1460 $attachment_filename .= ' (' . $main::locale->text('Preview') . ')' . $self->get_extension_for_format();
1462 } elsif ($attachment_filename && $self->{"${prefix}number"}) {
1463 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1466 $attachment_filename = "";
1469 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1470 $attachment_filename =~ s|[\s/\\]+|_|g;
1472 $main::lxdebug->leave_sub();
1473 return $attachment_filename;
1476 sub generate_email_subject {
1477 $main::lxdebug->enter_sub();
1480 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1481 my $prefix = $self->get_number_prefix_for_type();
1483 if ($subject && $self->{"${prefix}number"}) {
1484 $subject .= " " . $self->{"${prefix}number"}
1487 $main::lxdebug->leave_sub();
1492 $main::lxdebug->enter_sub();
1496 chdir("$self->{tmpdir}");
1499 if (-f "$self->{tmpfile}.err") {
1500 open(FH, "$self->{tmpfile}.err");
1505 if ($self->{tmpfile} && ! $::keep_temp_files) {
1506 $self->{tmpfile} =~ s|.*/||g;
1508 $self->{tmpfile} =~ s/\.\w+$//g;
1509 my $tmpfile = $self->{tmpfile};
1510 unlink(<$tmpfile.*>);
1513 chdir("$self->{cwd}");
1515 $main::lxdebug->leave_sub();
1521 $main::lxdebug->enter_sub();
1523 my ($self, $date, $myconfig) = @_;
1526 if ($date && $date =~ /\D/) {
1528 if ($myconfig->{dateformat} =~ /^yy/) {
1529 ($yy, $mm, $dd) = split /\D/, $date;
1531 if ($myconfig->{dateformat} =~ /^mm/) {
1532 ($mm, $dd, $yy) = split /\D/, $date;
1534 if ($myconfig->{dateformat} =~ /^dd/) {
1535 ($dd, $mm, $yy) = split /\D/, $date;
1540 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1541 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1543 $dd = "0$dd" if ($dd < 10);
1544 $mm = "0$mm" if ($mm < 10);
1546 $date = "$yy$mm$dd";
1549 $main::lxdebug->leave_sub();
1554 # Database routines used throughout
1556 sub _dbconnect_options {
1558 my $options = { pg_enable_utf8 => $::locale->is_utf8,
1565 $main::lxdebug->enter_sub(2);
1567 my ($self, $myconfig) = @_;
1569 # connect to database
1570 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, $self->_dbconnect_options)
1574 if ($myconfig->{dboptions}) {
1575 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1578 $main::lxdebug->leave_sub(2);
1583 sub dbconnect_noauto {
1584 $main::lxdebug->enter_sub();
1586 my ($self, $myconfig) = @_;
1588 # connect to database
1589 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, $self->_dbconnect_options(AutoCommit => 0))
1593 if ($myconfig->{dboptions}) {
1594 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1597 $main::lxdebug->leave_sub();
1602 sub get_standard_dbh {
1603 $main::lxdebug->enter_sub(2);
1606 my $myconfig = shift || \%::myconfig;
1608 if ($standard_dbh && !$standard_dbh->{Active}) {
1609 $main::lxdebug->message(LXDebug->INFO(), "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
1610 undef $standard_dbh;
1613 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1615 $main::lxdebug->leave_sub(2);
1617 return $standard_dbh;
1621 $main::lxdebug->enter_sub();
1623 my ($self, $date, $myconfig) = @_;
1624 my $dbh = $self->dbconnect($myconfig);
1626 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1627 my $sth = prepare_execute_query($self, $dbh, $query, $date);
1628 my ($closed) = $sth->fetchrow_array;
1630 $main::lxdebug->leave_sub();
1635 sub update_balance {
1636 $main::lxdebug->enter_sub();
1638 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1640 # if we have a value, go do it
1643 # retrieve balance from table
1644 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1645 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1646 my ($balance) = $sth->fetchrow_array;
1652 $query = "UPDATE $table SET $field = $balance WHERE $where";
1653 do_query($self, $dbh, $query, @values);
1655 $main::lxdebug->leave_sub();
1658 sub update_exchangerate {
1659 $main::lxdebug->enter_sub();
1661 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1663 # some sanity check for currency
1665 $main::lxdebug->leave_sub();
1668 $query = qq|SELECT curr FROM defaults|;
1670 my ($currency) = selectrow_query($self, $dbh, $query);
1671 my ($defaultcurrency) = split m/:/, $currency;
1674 if ($curr eq $defaultcurrency) {
1675 $main::lxdebug->leave_sub();
1679 $query = qq|SELECT e.curr FROM exchangerate e
1680 WHERE e.curr = ? AND e.transdate = ?
1682 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1691 $buy = conv_i($buy, "NULL");
1692 $sell = conv_i($sell, "NULL");
1695 if ($buy != 0 && $sell != 0) {
1696 $set = "buy = $buy, sell = $sell";
1697 } elsif ($buy != 0) {
1698 $set = "buy = $buy";
1699 } elsif ($sell != 0) {
1700 $set = "sell = $sell";
1703 if ($sth->fetchrow_array) {
1704 $query = qq|UPDATE exchangerate
1710 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1711 VALUES (?, $buy, $sell, ?)|;
1714 do_query($self, $dbh, $query, $curr, $transdate);
1716 $main::lxdebug->leave_sub();
1719 sub save_exchangerate {
1720 $main::lxdebug->enter_sub();
1722 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1724 my $dbh = $self->dbconnect($myconfig);
1728 $buy = $rate if $fld eq 'buy';
1729 $sell = $rate if $fld eq 'sell';
1732 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1737 $main::lxdebug->leave_sub();
1740 sub get_exchangerate {
1741 $main::lxdebug->enter_sub();
1743 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1746 unless ($transdate) {
1747 $main::lxdebug->leave_sub();
1751 $query = qq|SELECT curr FROM defaults|;
1753 my ($currency) = selectrow_query($self, $dbh, $query);
1754 my ($defaultcurrency) = split m/:/, $currency;
1756 if ($currency eq $defaultcurrency) {
1757 $main::lxdebug->leave_sub();
1761 $query = qq|SELECT e.$fld FROM exchangerate e
1762 WHERE e.curr = ? AND e.transdate = ?|;
1763 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1767 $main::lxdebug->leave_sub();
1769 return $exchangerate;
1772 sub check_exchangerate {
1773 $main::lxdebug->enter_sub();
1775 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1777 if ($fld !~/^buy|sell$/) {
1778 $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
1781 unless ($transdate) {
1782 $main::lxdebug->leave_sub();
1786 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1788 if ($currency eq $defaultcurrency) {
1789 $main::lxdebug->leave_sub();
1793 my $dbh = $self->get_standard_dbh($myconfig);
1794 my $query = qq|SELECT e.$fld FROM exchangerate e
1795 WHERE e.curr = ? AND e.transdate = ?|;
1797 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1799 $main::lxdebug->leave_sub();
1801 return $exchangerate;
1804 sub get_all_currencies {
1805 $main::lxdebug->enter_sub();
1808 my $myconfig = shift || \%::myconfig;
1809 my $dbh = $self->get_standard_dbh($myconfig);
1811 my $query = qq|SELECT curr FROM defaults|;
1813 my ($curr) = selectrow_query($self, $dbh, $query);
1814 my @currencies = grep { $_ } map { s/\s//g; $_ } split m/:/, $curr;
1816 $main::lxdebug->leave_sub();
1821 sub get_default_currency {
1822 $main::lxdebug->enter_sub();
1824 my ($self, $myconfig) = @_;
1825 my @currencies = $self->get_all_currencies($myconfig);
1827 $main::lxdebug->leave_sub();
1829 return $currencies[0];
1832 sub set_payment_options {
1833 $main::lxdebug->enter_sub();
1835 my ($self, $myconfig, $transdate) = @_;
1837 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1839 my $dbh = $self->get_standard_dbh($myconfig);
1842 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1843 qq|FROM payment_terms p | .
1846 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1847 $self->{payment_terms}) =
1848 selectrow_query($self, $dbh, $query, $self->{payment_id});
1850 if ($transdate eq "") {
1851 if ($self->{invdate}) {
1852 $transdate = $self->{invdate};
1854 $transdate = $self->{transdate};
1859 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1860 qq|FROM payment_terms|;
1861 ($self->{netto_date}, $self->{skonto_date}) =
1862 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1864 my ($invtotal, $total);
1865 my (%amounts, %formatted_amounts);
1867 if ($self->{type} =~ /_order$/) {
1868 $amounts{invtotal} = $self->{ordtotal};
1869 $amounts{total} = $self->{ordtotal};
1871 } elsif ($self->{type} =~ /_quotation$/) {
1872 $amounts{invtotal} = $self->{quototal};
1873 $amounts{total} = $self->{quototal};
1876 $amounts{invtotal} = $self->{invtotal};
1877 $amounts{total} = $self->{total};
1879 $amounts{skonto_in_percent} = 100.0 * $self->{percent_skonto};
1881 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1883 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1884 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1885 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1887 foreach (keys %amounts) {
1888 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1889 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1892 if ($self->{"language_id"}) {
1894 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1895 qq|FROM translation_payment_terms t | .
1896 qq|LEFT JOIN language l ON t.language_id = l.id | .
1897 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1898 my ($description_long, $output_numberformat, $output_dateformat,
1899 $output_longdates) =
1900 selectrow_query($self, $dbh, $query,
1901 $self->{"language_id"}, $self->{"payment_id"});
1903 $self->{payment_terms} = $description_long if ($description_long);
1905 if ($output_dateformat) {
1906 foreach my $key (qw(netto_date skonto_date)) {
1908 $main::locale->reformat_date($myconfig, $self->{$key},
1914 if ($output_numberformat &&
1915 ($output_numberformat ne $myconfig->{"numberformat"})) {
1916 my $saved_numberformat = $myconfig->{"numberformat"};
1917 $myconfig->{"numberformat"} = $output_numberformat;
1918 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1919 $myconfig->{"numberformat"} = $saved_numberformat;
1923 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1924 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1925 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1926 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1927 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1928 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1929 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1931 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1933 $self->{skonto_in_percent} = $formatted_amounts{skonto_in_percent};
1935 $main::lxdebug->leave_sub();
1939 sub get_template_language {
1940 $main::lxdebug->enter_sub();
1942 my ($self, $myconfig) = @_;
1944 my $template_code = "";
1946 if ($self->{language_id}) {
1947 my $dbh = $self->get_standard_dbh($myconfig);
1948 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1949 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1952 $main::lxdebug->leave_sub();
1954 return $template_code;
1957 sub get_printer_code {
1958 $main::lxdebug->enter_sub();
1960 my ($self, $myconfig) = @_;
1962 my $template_code = "";
1964 if ($self->{printer_id}) {
1965 my $dbh = $self->get_standard_dbh($myconfig);
1966 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1967 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1970 $main::lxdebug->leave_sub();
1972 return $template_code;
1976 $main::lxdebug->enter_sub();
1978 my ($self, $myconfig) = @_;
1980 my $template_code = "";
1982 if ($self->{shipto_id}) {
1983 my $dbh = $self->get_standard_dbh($myconfig);
1984 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1985 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1986 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1989 $main::lxdebug->leave_sub();
1993 $main::lxdebug->enter_sub();
1995 my ($self, $dbh, $id, $module) = @_;
2000 foreach my $item (qw(name department_1 department_2 street zipcode city country
2001 contact phone fax email)) {
2002 if ($self->{"shipto$item"}) {
2003 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
2005 push(@values, $self->{"shipto${item}"});
2009 if ($self->{shipto_id}) {
2010 my $query = qq|UPDATE shipto set
2012 shiptodepartment_1 = ?,
2013 shiptodepartment_2 = ?,
2022 WHERE shipto_id = ?|;
2023 do_query($self, $dbh, $query, @values, $self->{shipto_id});
2025 my $query = qq|SELECT * FROM shipto
2026 WHERE shiptoname = ? AND
2027 shiptodepartment_1 = ? AND
2028 shiptodepartment_2 = ? AND
2029 shiptostreet = ? AND
2030 shiptozipcode = ? AND
2032 shiptocountry = ? AND
2033 shiptocontact = ? AND
2039 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
2042 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
2043 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
2044 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
2045 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
2046 do_query($self, $dbh, $query, $id, @values, $module);
2051 $main::lxdebug->leave_sub();
2055 $main::lxdebug->enter_sub();
2057 my ($self, $dbh) = @_;
2059 $dbh ||= $self->get_standard_dbh(\%main::myconfig);
2061 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
2062 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
2063 $self->{"employee_id"} *= 1;
2065 $main::lxdebug->leave_sub();
2068 sub get_employee_data {
2069 $main::lxdebug->enter_sub();
2074 Common::check_params(\%params, qw(prefix));
2075 Common::check_params_x(\%params, qw(id));
2078 $main::lxdebug->leave_sub();
2082 my $myconfig = \%main::myconfig;
2083 my $dbh = $params{dbh} || $self->get_standard_dbh($myconfig);
2085 my ($login) = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
2088 my $user = User->new($login);
2089 map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
2091 $self->{$params{prefix} . '_login'} = $login;
2092 $self->{$params{prefix} . '_name'} ||= $login;
2095 $main::lxdebug->leave_sub();
2099 $main::lxdebug->enter_sub();
2101 my ($self, $myconfig, $reference_date) = @_;
2103 $reference_date = $reference_date ? conv_dateq($reference_date) . '::DATE' : 'current_date';
2105 my $dbh = $self->get_standard_dbh($myconfig);
2106 my $query = qq|SELECT ${reference_date} + terms_netto FROM payment_terms WHERE id = ?|;
2107 my ($duedate) = selectrow_query($self, $dbh, $query, $self->{payment_id});
2109 $main::lxdebug->leave_sub();
2115 $main::lxdebug->enter_sub();
2117 my ($self, $dbh, $id, $key) = @_;
2119 $key = "all_contacts" unless ($key);
2123 $main::lxdebug->leave_sub();
2128 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
2129 qq|FROM contacts | .
2130 qq|WHERE cp_cv_id = ? | .
2131 qq|ORDER BY lower(cp_name)|;
2133 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
2135 $main::lxdebug->leave_sub();
2139 $main::lxdebug->enter_sub();
2141 my ($self, $dbh, $key) = @_;
2143 my ($all, $old_id, $where, @values);
2145 if (ref($key) eq "HASH") {
2148 $key = "ALL_PROJECTS";
2150 foreach my $p (keys(%{$params})) {
2152 $all = $params->{$p};
2153 } elsif ($p eq "old_id") {
2154 $old_id = $params->{$p};
2155 } elsif ($p eq "key") {
2156 $key = $params->{$p};
2162 $where = "WHERE active ";
2164 if (ref($old_id) eq "ARRAY") {
2165 my @ids = grep({ $_ } @{$old_id});
2167 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
2168 push(@values, @ids);
2171 $where .= " OR (id = ?) ";
2172 push(@values, $old_id);
2178 qq|SELECT id, projectnumber, description, active | .
2181 qq|ORDER BY lower(projectnumber)|;
2183 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2185 $main::lxdebug->leave_sub();
2189 $main::lxdebug->enter_sub();
2191 my ($self, $dbh, $vc_id, $key) = @_;
2193 $key = "all_shipto" unless ($key);
2196 # get shipping addresses
2197 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
2199 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
2205 $main::lxdebug->leave_sub();
2209 $main::lxdebug->enter_sub();
2211 my ($self, $dbh, $key) = @_;
2213 $key = "all_printers" unless ($key);
2215 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
2217 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2219 $main::lxdebug->leave_sub();
2223 $main::lxdebug->enter_sub();
2225 my ($self, $dbh, $params) = @_;
2228 $key = $params->{key};
2229 $key = "all_charts" unless ($key);
2231 my $transdate = quote_db_date($params->{transdate});
2234 qq|SELECT c.id, c.accno, c.description, c.link, c.charttype, tk.taxkey_id, tk.tax_id | .
2236 qq|LEFT JOIN taxkeys tk ON | .
2237 qq|(tk.id = (SELECT id FROM taxkeys | .
2238 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2239 qq| ORDER BY startdate DESC LIMIT 1)) | .
2240 qq|ORDER BY c.accno|;
2242 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2244 $main::lxdebug->leave_sub();
2247 sub _get_taxcharts {
2248 $main::lxdebug->enter_sub();
2250 my ($self, $dbh, $params) = @_;
2252 my $key = "all_taxcharts";
2255 if (ref $params eq 'HASH') {
2256 $key = $params->{key} if ($params->{key});
2257 if ($params->{module} eq 'AR') {
2258 push @where, 'taxkey NOT IN (8, 9, 18, 19)';
2260 } elsif ($params->{module} eq 'AP') {
2261 push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
2268 my $where = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (@where);
2270 my $query = qq|SELECT * FROM tax $where ORDER BY taxkey|;
2272 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2274 $main::lxdebug->leave_sub();
2278 $main::lxdebug->enter_sub();
2280 my ($self, $dbh, $key) = @_;
2282 $key = "all_taxzones" unless ($key);
2284 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2286 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2288 $main::lxdebug->leave_sub();
2291 sub _get_employees {
2292 $main::lxdebug->enter_sub();
2294 my ($self, $dbh, $default_key, $key) = @_;
2296 $key = $default_key unless ($key);
2297 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2299 $main::lxdebug->leave_sub();
2302 sub _get_business_types {
2303 $main::lxdebug->enter_sub();
2305 my ($self, $dbh, $key) = @_;
2307 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2308 $options->{key} ||= "all_business_types";
2311 if (exists $options->{salesman}) {
2312 $where = 'WHERE ' . ($options->{salesman} ? '' : 'NOT ') . 'COALESCE(salesman)';
2315 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, qq|SELECT * FROM business $where ORDER BY lower(description)|);
2317 $main::lxdebug->leave_sub();
2320 sub _get_languages {
2321 $main::lxdebug->enter_sub();
2323 my ($self, $dbh, $key) = @_;
2325 $key = "all_languages" unless ($key);
2327 my $query = qq|SELECT * FROM language ORDER BY id|;
2329 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2331 $main::lxdebug->leave_sub();
2334 sub _get_dunning_configs {
2335 $main::lxdebug->enter_sub();
2337 my ($self, $dbh, $key) = @_;
2339 $key = "all_dunning_configs" unless ($key);
2341 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2343 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2345 $main::lxdebug->leave_sub();
2348 sub _get_currencies {
2349 $main::lxdebug->enter_sub();
2351 my ($self, $dbh, $key) = @_;
2353 $key = "all_currencies" unless ($key);
2355 my $query = qq|SELECT curr AS currency FROM defaults|;
2357 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2359 $main::lxdebug->leave_sub();
2363 $main::lxdebug->enter_sub();
2365 my ($self, $dbh, $key) = @_;
2367 $key = "all_payments" unless ($key);
2369 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2371 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2373 $main::lxdebug->leave_sub();
2376 sub _get_customers {
2377 $main::lxdebug->enter_sub();
2379 my ($self, $dbh, $key) = @_;
2381 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2382 $options->{key} ||= "all_customers";
2383 my $limit_clause = "LIMIT $options->{limit}" if $options->{limit};
2386 push @where, qq|business_id IN (SELECT id FROM business WHERE salesman)| if $options->{business_is_salesman};
2387 push @where, qq|NOT obsolete| if !$options->{with_obsolete};
2388 my $where_str = @where ? "WHERE " . join(" AND ", map { "($_)" } @where) : '';
2390 my $query = qq|SELECT * FROM customer $where_str ORDER BY name $limit_clause|;
2391 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, $query);
2393 $main::lxdebug->leave_sub();
2397 $main::lxdebug->enter_sub();
2399 my ($self, $dbh, $key) = @_;
2401 $key = "all_vendors" unless ($key);
2403 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2405 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2407 $main::lxdebug->leave_sub();
2410 sub _get_departments {
2411 $main::lxdebug->enter_sub();
2413 my ($self, $dbh, $key) = @_;
2415 $key = "all_departments" unless ($key);
2417 my $query = qq|SELECT * FROM department ORDER BY description|;
2419 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2421 $main::lxdebug->leave_sub();
2424 sub _get_warehouses {
2425 $main::lxdebug->enter_sub();
2427 my ($self, $dbh, $param) = @_;
2429 my ($key, $bins_key);
2431 if ('' eq ref $param) {
2435 $key = $param->{key};
2436 $bins_key = $param->{bins};
2439 my $query = qq|SELECT w.* FROM warehouse w
2440 WHERE (NOT w.invalid) AND
2441 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2442 ORDER BY w.sortkey|;
2444 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2447 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2448 my $sth = prepare_query($self, $dbh, $query);
2450 foreach my $warehouse (@{ $self->{$key} }) {
2451 do_statement($self, $sth, $query, $warehouse->{id});
2452 $warehouse->{$bins_key} = [];
2454 while (my $ref = $sth->fetchrow_hashref()) {
2455 push @{ $warehouse->{$bins_key} }, $ref;
2461 $main::lxdebug->leave_sub();
2465 $main::lxdebug->enter_sub();
2467 my ($self, $dbh, $table, $key, $sortkey) = @_;
2469 my $query = qq|SELECT * FROM $table|;
2470 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2472 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2474 $main::lxdebug->leave_sub();
2478 # $main::lxdebug->enter_sub();
2480 # my ($self, $dbh, $key) = @_;
2482 # $key ||= "all_groups";
2484 # my $groups = $main::auth->read_groups();
2486 # $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2488 # $main::lxdebug->leave_sub();
2492 $main::lxdebug->enter_sub();
2497 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2498 my ($sth, $query, $ref);
2500 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2501 my $vc_id = $self->{"${vc}_id"};
2503 if ($params{"contacts"}) {
2504 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2507 if ($params{"shipto"}) {
2508 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2511 if ($params{"projects"} || $params{"all_projects"}) {
2512 $self->_get_projects($dbh, $params{"all_projects"} ?
2513 $params{"all_projects"} : $params{"projects"},
2514 $params{"all_projects"} ? 1 : 0);
2517 if ($params{"printers"}) {
2518 $self->_get_printers($dbh, $params{"printers"});
2521 if ($params{"languages"}) {
2522 $self->_get_languages($dbh, $params{"languages"});
2525 if ($params{"charts"}) {
2526 $self->_get_charts($dbh, $params{"charts"});
2529 if ($params{"taxcharts"}) {
2530 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2533 if ($params{"taxzones"}) {
2534 $self->_get_taxzones($dbh, $params{"taxzones"});
2537 if ($params{"employees"}) {
2538 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2541 if ($params{"salesmen"}) {
2542 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2545 if ($params{"business_types"}) {
2546 $self->_get_business_types($dbh, $params{"business_types"});
2549 if ($params{"dunning_configs"}) {
2550 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2553 if($params{"currencies"}) {
2554 $self->_get_currencies($dbh, $params{"currencies"});
2557 if($params{"customers"}) {
2558 $self->_get_customers($dbh, $params{"customers"});
2561 if($params{"vendors"}) {
2562 if (ref $params{"vendors"} eq 'HASH') {
2563 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2565 $self->_get_vendors($dbh, $params{"vendors"});
2569 if($params{"payments"}) {
2570 $self->_get_payments($dbh, $params{"payments"});
2573 if($params{"departments"}) {
2574 $self->_get_departments($dbh, $params{"departments"});
2577 if ($params{price_factors}) {
2578 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2581 if ($params{warehouses}) {
2582 $self->_get_warehouses($dbh, $params{warehouses});
2585 # if ($params{groups}) {
2586 # $self->_get_groups($dbh, $params{groups});
2589 if ($params{partsgroup}) {
2590 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2593 $main::lxdebug->leave_sub();
2596 # this sub gets the id and name from $table
2598 $main::lxdebug->enter_sub();
2600 my ($self, $myconfig, $table) = @_;
2602 # connect to database
2603 my $dbh = $self->get_standard_dbh($myconfig);
2605 $table = $table eq "customer" ? "customer" : "vendor";
2606 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2608 my ($query, @values);
2610 if (!$self->{openinvoices}) {
2612 if ($self->{customernumber} ne "") {
2613 $where = qq|(vc.customernumber ILIKE ?)|;
2614 push(@values, '%' . $self->{customernumber} . '%');
2616 $where = qq|(vc.name ILIKE ?)|;
2617 push(@values, '%' . $self->{$table} . '%');
2621 qq~SELECT vc.id, vc.name,
2622 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2624 WHERE $where AND (NOT vc.obsolete)
2628 qq~SELECT DISTINCT vc.id, vc.name,
2629 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2631 JOIN $table vc ON (a.${table}_id = vc.id)
2632 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2634 push(@values, '%' . $self->{$table} . '%');
2637 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2639 $main::lxdebug->leave_sub();
2641 return scalar(@{ $self->{name_list} });
2644 # the selection sub is used in the AR, AP, IS, IR and OE module
2647 $main::lxdebug->enter_sub();
2649 my ($self, $myconfig, $table, $module) = @_;
2652 my $dbh = $self->get_standard_dbh;
2654 $table = $table eq "customer" ? "customer" : "vendor";
2656 my $query = qq|SELECT count(*) FROM $table|;
2657 my ($count) = selectrow_query($self, $dbh, $query);
2659 # build selection list
2660 if ($count <= $myconfig->{vclimit}) {
2661 $query = qq|SELECT id, name, salesman_id
2662 FROM $table WHERE NOT obsolete
2664 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2668 $self->get_employee($dbh);
2670 # setup sales contacts
2671 $query = qq|SELECT e.id, e.name
2673 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2674 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2677 push(@{ $self->{all_employees} },
2678 { id => $self->{employee_id},
2679 name => $self->{employee} });
2681 # sort the whole thing
2682 @{ $self->{all_employees} } =
2683 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2685 if ($module eq 'AR') {
2687 # prepare query for departments
2688 $query = qq|SELECT id, description
2691 ORDER BY description|;
2694 $query = qq|SELECT id, description
2696 ORDER BY description|;
2699 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2702 $query = qq|SELECT id, description
2706 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2709 $query = qq|SELECT printer_description, id
2711 ORDER BY printer_description|;
2713 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2716 $query = qq|SELECT id, description
2720 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2722 $main::lxdebug->leave_sub();
2725 sub language_payment {
2726 $main::lxdebug->enter_sub();
2728 my ($self, $myconfig) = @_;
2730 my $dbh = $self->get_standard_dbh($myconfig);
2732 my $query = qq|SELECT id, description
2736 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2739 $query = qq|SELECT printer_description, id
2741 ORDER BY printer_description|;
2743 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2746 $query = qq|SELECT id, description
2750 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2752 # get buchungsgruppen
2753 $query = qq|SELECT id, description
2754 FROM buchungsgruppen|;
2756 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2758 $main::lxdebug->leave_sub();
2761 # this is only used for reports
2762 sub all_departments {
2763 $main::lxdebug->enter_sub();
2765 my ($self, $myconfig, $table) = @_;
2767 my $dbh = $self->get_standard_dbh($myconfig);
2770 if ($table eq 'customer') {
2771 $where = "WHERE role = 'P' ";
2774 my $query = qq|SELECT id, description
2777 ORDER BY description|;
2778 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2780 delete($self->{all_departments}) unless (@{ $self->{all_departments} || [] });
2782 $main::lxdebug->leave_sub();
2786 $main::lxdebug->enter_sub();
2788 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2791 if ($table eq "customer") {
2800 $self->all_vc($myconfig, $table, $module);
2802 # get last customers or vendors
2803 my ($query, $sth, $ref);
2805 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2810 my $transdate = "current_date";
2811 if ($self->{transdate}) {
2812 $transdate = $dbh->quote($self->{transdate});
2815 # now get the account numbers
2816 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2817 FROM chart c, taxkeys tk
2818 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2819 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2822 $sth = $dbh->prepare($query);
2824 do_statement($self, $sth, $query, '%' . $module . '%');
2826 $self->{accounts} = "";
2827 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2829 foreach my $key (split(/:/, $ref->{link})) {
2830 if ($key =~ /\Q$module\E/) {
2832 # cross reference for keys
2833 $xkeyref{ $ref->{accno} } = $key;
2835 push @{ $self->{"${module}_links"}{$key} },
2836 { accno => $ref->{accno},
2837 description => $ref->{description},
2838 taxkey => $ref->{taxkey_id},
2839 tax_id => $ref->{tax_id} };
2841 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2847 # get taxkeys and description
2848 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2849 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2851 if (($module eq "AP") || ($module eq "AR")) {
2852 # get tax rates and description
2853 $query = qq|SELECT * FROM tax|;
2854 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2860 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2861 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2862 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2863 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2865 d.description AS department,
2868 JOIN $table c ON (a.${table}_id = c.id)
2869 LEFT JOIN employee e ON (e.id = a.employee_id)
2870 LEFT JOIN department d ON (d.id = a.department_id)
2872 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2874 foreach my $key (keys %$ref) {
2875 $self->{$key} = $ref->{$key};
2878 my $transdate = "current_date";
2879 if ($self->{transdate}) {
2880 $transdate = $dbh->quote($self->{transdate});
2883 # now get the account numbers
2884 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2886 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2888 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2889 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2892 $sth = $dbh->prepare($query);
2893 do_statement($self, $sth, $query, "%$module%");
2895 $self->{accounts} = "";
2896 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2898 foreach my $key (split(/:/, $ref->{link})) {
2899 if ($key =~ /\Q$module\E/) {
2901 # cross reference for keys
2902 $xkeyref{ $ref->{accno} } = $key;
2904 push @{ $self->{"${module}_links"}{$key} },
2905 { accno => $ref->{accno},
2906 description => $ref->{description},
2907 taxkey => $ref->{taxkey_id},
2908 tax_id => $ref->{tax_id} };
2910 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2916 # get amounts from individual entries
2919 c.accno, c.description,
2920 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2924 LEFT JOIN chart c ON (c.id = a.chart_id)
2925 LEFT JOIN project p ON (p.id = a.project_id)
2926 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2927 WHERE (tk.taxkey_id=a.taxkey) AND
2928 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2929 THEN tk.chart_id = a.chart_id
2932 OR (c.link='%tax%')) AND
2933 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2934 WHERE a.trans_id = ?
2935 AND a.fx_transaction = '0'
2936 ORDER BY a.acc_trans_id, a.transdate|;
2937 $sth = $dbh->prepare($query);
2938 do_statement($self, $sth, $query, $self->{id});
2940 # get exchangerate for currency
2941 $self->{exchangerate} =
2942 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2945 # store amounts in {acc_trans}{$key} for multiple accounts
2946 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2947 $ref->{exchangerate} =
2948 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2949 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2952 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2953 $ref->{amount} *= -1;
2955 $ref->{index} = $index;
2957 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2963 d.curr AS currencies, d.closedto, d.revtrans,
2964 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2965 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2967 $ref = selectfirst_hashref_query($self, $dbh, $query);
2968 map { $self->{$_} = $ref->{$_} } keys %$ref;
2975 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2976 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2977 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2979 $ref = selectfirst_hashref_query($self, $dbh, $query);
2980 map { $self->{$_} = $ref->{$_} } keys %$ref;
2982 if ($self->{"$self->{vc}_id"}) {
2984 # only setup currency
2985 ($self->{currency}) = split(/:/, $self->{currencies});
2989 $self->lastname_used($dbh, $myconfig, $table, $module);
2991 # get exchangerate for currency
2992 $self->{exchangerate} =
2993 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2999 $main::lxdebug->leave_sub();
3003 $main::lxdebug->enter_sub();
3005 my ($self, $dbh, $myconfig, $table, $module) = @_;
3009 $table = $table eq "customer" ? "customer" : "vendor";
3010 my %column_map = ("a.curr" => "currency",
3011 "a.${table}_id" => "${table}_id",
3012 "a.department_id" => "department_id",
3013 "d.description" => "department",
3014 "ct.name" => $table,
3015 "current_date + ct.terms" => "duedate",
3018 if ($self->{type} =~ /delivery_order/) {
3019 $arap = 'delivery_orders';
3020 delete $column_map{"a.curr"};
3022 } elsif ($self->{type} =~ /_order/) {
3024 $where = "quotation = '0'";
3026 } elsif ($self->{type} =~ /_quotation/) {
3028 $where = "quotation = '1'";
3030 } elsif ($table eq 'customer') {
3038 $where = "($where) AND" if ($where);
3039 my $query = qq|SELECT MAX(id) FROM $arap
3040 WHERE $where ${table}_id > 0|;
3041 my ($trans_id) = selectrow_query($self, $dbh, $query);
3044 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
3045 $query = qq|SELECT $column_spec
3047 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
3048 LEFT JOIN department d ON (a.department_id = d.id)
3050 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
3052 map { $self->{$_} = $ref->{$_} } values %column_map;
3054 $main::lxdebug->leave_sub();
3058 $main::lxdebug->enter_sub();
3061 my $myconfig = shift || \%::myconfig;
3062 my ($thisdate, $days) = @_;
3064 my $dbh = $self->get_standard_dbh($myconfig);
3069 my $dateformat = $myconfig->{dateformat};
3070 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
3071 $thisdate = $dbh->quote($thisdate);
3072 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
3074 $query = qq|SELECT current_date AS thisdate|;
3077 ($thisdate) = selectrow_query($self, $dbh, $query);
3079 $main::lxdebug->leave_sub();
3085 $main::lxdebug->enter_sub();
3087 my ($self, $string) = @_;
3089 if ($string !~ /%/) {
3090 $string = "%$string%";
3093 $string =~ s/\'/\'\'/g;
3095 $main::lxdebug->leave_sub();
3101 $main::lxdebug->enter_sub();
3103 my ($self, $flds, $new, $count, $numrows) = @_;
3107 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
3112 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
3114 my $j = $item->{ndx} - 1;
3115 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
3119 for $i ($count + 1 .. $numrows) {
3120 map { delete $self->{"${_}_$i"} } @{$flds};
3123 $main::lxdebug->leave_sub();
3127 $main::lxdebug->enter_sub();
3129 my ($self, $myconfig) = @_;
3133 my $dbh = $self->dbconnect_noauto($myconfig);
3135 my $query = qq|DELETE FROM status
3136 WHERE (formname = ?) AND (trans_id = ?)|;
3137 my $sth = prepare_query($self, $dbh, $query);
3139 if ($self->{formname} =~ /(check|receipt)/) {
3140 for $i (1 .. $self->{rowcount}) {
3141 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
3144 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
3148 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3149 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3151 my %queued = split / /, $self->{queued};
3154 if ($self->{formname} =~ /(check|receipt)/) {
3156 # this is a check or receipt, add one entry for each lineitem
3157 my ($accno) = split /--/, $self->{account};
3158 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
3159 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
3160 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
3161 $sth = prepare_query($self, $dbh, $query);
3163 for $i (1 .. $self->{rowcount}) {
3164 if ($self->{"checked_$i"}) {
3165 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
3171 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3172 VALUES (?, ?, ?, ?, ?)|;
3173 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
3174 $queued{$self->{formname}}, $self->{formname});
3180 $main::lxdebug->leave_sub();
3184 $main::lxdebug->enter_sub();
3186 my ($self, $dbh) = @_;
3188 my ($query, $printed, $emailed);
3190 my $formnames = $self->{printed};
3191 my $emailforms = $self->{emailed};
3193 $query = qq|DELETE FROM status
3194 WHERE (formname = ?) AND (trans_id = ?)|;
3195 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
3197 # this only applies to the forms
3198 # checks and receipts are posted when printed or queued
3200 if ($self->{queued}) {
3201 my %queued = split / /, $self->{queued};
3203 foreach my $formname (keys %queued) {
3204 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3205 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3207 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3208 VALUES (?, ?, ?, ?, ?)|;
3209 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
3211 $formnames =~ s/\Q$self->{formname}\E//;
3212 $emailforms =~ s/\Q$self->{formname}\E//;
3217 # save printed, emailed info
3218 $formnames =~ s/^ +//g;
3219 $emailforms =~ s/^ +//g;
3222 map { $status{$_}{printed} = 1 } split / +/, $formnames;
3223 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
3225 foreach my $formname (keys %status) {
3226 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
3227 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
3229 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
3230 VALUES (?, ?, ?, ?)|;
3231 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
3234 $main::lxdebug->leave_sub();
3238 # $main::locale->text('SAVED')
3239 # $main::locale->text('DELETED')
3240 # $main::locale->text('ADDED')
3241 # $main::locale->text('PAYMENT POSTED')
3242 # $main::locale->text('POSTED')
3243 # $main::locale->text('POSTED AS NEW')
3244 # $main::locale->text('ELSE')
3245 # $main::locale->text('SAVED FOR DUNNING')
3246 # $main::locale->text('DUNNING STARTED')
3247 # $main::locale->text('PRINTED')
3248 # $main::locale->text('MAILED')
3249 # $main::locale->text('SCREENED')
3250 # $main::locale->text('CANCELED')
3251 # $main::locale->text('invoice')
3252 # $main::locale->text('proforma')
3253 # $main::locale->text('sales_order')
3254 # $main::locale->text('packing_list')
3255 # $main::locale->text('pick_list')
3256 # $main::locale->text('purchase_order')
3257 # $main::locale->text('bin_list')
3258 # $main::locale->text('sales_quotation')
3259 # $main::locale->text('request_quotation')
3262 $main::lxdebug->enter_sub();
3265 my $dbh = shift || $self->get_standard_dbh;
3267 if(!exists $self->{employee_id}) {
3268 &get_employee($self, $dbh);
3272 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3273 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3274 my @values = (conv_i($self->{id}), $self->{login},
3275 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3276 do_query($self, $dbh, $query, @values);
3280 $main::lxdebug->leave_sub();
3284 $main::lxdebug->enter_sub();
3286 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3287 my ($orderBy, $desc) = split(/\-\-/, $order);
3288 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3291 if ($trans_id ne "") {
3293 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 | .
3294 qq|FROM history_erp h | .
3295 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3296 qq|WHERE (trans_id = | . $trans_id . qq|) $restriction | .
3299 my $sth = $dbh->prepare($query) || $self->dberror($query);
3301 $sth->execute() || $self->dberror("$query");
3303 while(my $hash_ref = $sth->fetchrow_hashref()) {
3304 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3305 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3306 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3307 $tempArray[$i++] = $hash_ref;
3309 $main::lxdebug->leave_sub() and return \@tempArray
3310 if ($i > 0 && $tempArray[0] ne "");
3312 $main::lxdebug->leave_sub();
3316 sub update_defaults {
3317 $main::lxdebug->enter_sub();
3319 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3322 if ($provided_dbh) {
3323 $dbh = $provided_dbh;
3325 $dbh = $self->dbconnect_noauto($myconfig);
3327 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3328 my $sth = $dbh->prepare($query);
3330 $sth->execute || $self->dberror($query);
3331 my ($var) = $sth->fetchrow_array;
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 defaults SET $fld = ?|;
3344 do_query($self, $dbh, $query, $var);
3346 if (!$provided_dbh) {
3351 $main::lxdebug->leave_sub();
3356 sub update_business {
3357 $main::lxdebug->enter_sub();
3359 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3362 if ($provided_dbh) {
3363 $dbh = $provided_dbh;
3365 $dbh = $self->dbconnect_noauto($myconfig);
3368 qq|SELECT customernumberinit FROM business
3369 WHERE id = ? FOR UPDATE|;
3370 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3372 return undef unless $var;
3374 if ($var =~ m/\d+$/) {
3375 my $new_var = (substr $var, $-[0]) * 1 + 1;
3376 my $len_diff = length($var) - $-[0] - length($new_var);
3377 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3383 $query = qq|UPDATE business
3384 SET customernumberinit = ?
3386 do_query($self, $dbh, $query, $var, $business_id);
3388 if (!$provided_dbh) {
3393 $main::lxdebug->leave_sub();
3398 sub get_partsgroup {
3399 $main::lxdebug->enter_sub();
3401 my ($self, $myconfig, $p) = @_;
3402 my $target = $p->{target} || 'all_partsgroup';
3404 my $dbh = $self->get_standard_dbh($myconfig);
3406 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3408 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3411 if ($p->{searchitems} eq 'part') {
3412 $query .= qq|WHERE p.inventory_accno_id > 0|;
3414 if ($p->{searchitems} eq 'service') {
3415 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3417 if ($p->{searchitems} eq 'assembly') {
3418 $query .= qq|WHERE p.assembly = '1'|;
3420 if ($p->{searchitems} eq 'labor') {
3421 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3424 $query .= qq|ORDER BY partsgroup|;
3427 $query = qq|SELECT id, partsgroup FROM partsgroup
3428 ORDER BY partsgroup|;
3431 if ($p->{language_code}) {
3432 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3433 t.description AS translation
3435 JOIN parts p ON (p.partsgroup_id = pg.id)
3436 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3437 ORDER BY translation|;
3438 @values = ($p->{language_code});
3441 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3443 $main::lxdebug->leave_sub();
3446 sub get_pricegroup {
3447 $main::lxdebug->enter_sub();
3449 my ($self, $myconfig, $p) = @_;
3451 my $dbh = $self->get_standard_dbh($myconfig);
3453 my $query = qq|SELECT p.id, p.pricegroup
3456 $query .= qq| ORDER BY pricegroup|;
3459 $query = qq|SELECT id, pricegroup FROM pricegroup
3460 ORDER BY pricegroup|;
3463 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3465 $main::lxdebug->leave_sub();
3469 # usage $form->all_years($myconfig, [$dbh])
3470 # return list of all years where bookings found
3473 $main::lxdebug->enter_sub();
3475 my ($self, $myconfig, $dbh) = @_;
3477 $dbh ||= $self->get_standard_dbh($myconfig);
3480 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3481 (SELECT MAX(transdate) FROM acc_trans)|;
3482 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3484 if ($myconfig->{dateformat} =~ /^yy/) {
3485 ($startdate) = split /\W/, $startdate;
3486 ($enddate) = split /\W/, $enddate;
3488 (@_) = split /\W/, $startdate;
3490 (@_) = split /\W/, $enddate;
3495 $startdate = substr($startdate,0,4);
3496 $enddate = substr($enddate,0,4);
3498 while ($enddate >= $startdate) {
3499 push @all_years, $enddate--;
3504 $main::lxdebug->leave_sub();
3508 $main::lxdebug->enter_sub();
3512 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if exists $self->{$_} } @vars;
3514 $main::lxdebug->leave_sub();
3518 $main::lxdebug->enter_sub();
3523 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if exists $self->{_VAR_BACKUP}->{$_} } @vars;
3525 $main::lxdebug->leave_sub();
3534 SL::Form.pm - main data object.
3538 This is the main data object of Lx-Office.
3539 Unfortunately it also acts as a god object for certain data retrieval procedures used in the entry points.
3540 Points of interest for a beginner are:
3542 - $form->error - renders a generic error in html. accepts an error message
3543 - $form->get_standard_dbh - returns a database connection for the
3545 =head1 SPECIAL FUNCTIONS
3549 =item _store_value()
3551 parses a complex var name, and stores it in the form.
3554 $form->_store_value($key, $value);
3556 keys must start with a string, and can contain various tokens.
3557 supported key structures are:
3560 simple key strings work as expected
3565 separating two keys by a dot (.) will result in a hash lookup for the inner value
3566 this is similar to the behaviour of java and templating mechanisms.
3568 filter.description => $form->{filter}->{description}
3570 3. array+hashref access
3572 adding brackets ([]) before the dot will cause the next hash to be put into an array.
3573 using [+] instead of [] will force a new array index. this is useful for recurring
3574 data structures like part lists. put a [+] into the first varname, and use [] on the
3577 repeating these names in your template:
3580 invoice.items[].parts_id
3584 $form->{invoice}->{items}->[
3598 using brackets at the end of a name will result in a pure array to be created.
3599 note that you mustn't use [+], which is reserved for array+hash access and will
3600 result in undefined behaviour in array context.
3602 filter.status[] => $form->{status}->[ val1, val2, ... ]
3604 =item update_business PARAMS
3607 \%config, - config hashref
3608 $business_id, - business id
3609 $dbh - optional database handle
3611 handles business (thats customer/vendor types) sequences.
3613 special behaviour for empty strings in customerinitnumber field:
3614 will in this case not increase the value, and return undef.
3616 =item redirect_header $url
3618 Generates a HTTP redirection header for the new C<$url>. Constructs an
3619 absolute URL including scheme, host name and port. If C<$url> is a
3620 relative URL then it is considered relative to Lx-Office base URL.
3622 This function C<die>s if headers have already been created with
3623 C<$::form-E<gt>header>.
3627 print $::form->redirect_header('oe.pl?action=edit&id=1234');
3628 print $::form->redirect_header('http://www.lx-office.org/');