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 #======================================================================
51 $main::lxdebug->enter_sub(2);
55 my @pairs = split(/&/, $input);
58 my ($name, $value) = split(/=/, $_, 2);
59 $in{$name} = unescape(undef, $value);
62 $main::lxdebug->leave_sub(2);
67 sub _request_to_hash {
68 $main::lxdebug->enter_sub(2);
71 my ($i, $loc, $key, $val);
72 my (%ATTACH, $f, $header, $header_body, $len, $buf);
73 my ($boundary, @list, $size, $body, $x, $blah, $name);
75 if ($ENV{'CONTENT_TYPE'}
76 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
77 $boundary = quotemeta('--' . $1);
78 @list = split(/$boundary/, $input);
80 # For some reason there are always 2 extra, that are empty
83 for ($x = 1; $x <= $size; $x++) {
84 $header_body = $list[$x];
85 $header_body =~ /\r\n\r\n|\n\n/;
87 # Here we split the header and body
92 # Now we try to get the file name
94 $name =~ /name=\"(.+)\"/;
95 ($name, $blah) = split(/\"/, $1);
97 # If the form name is not attach, then we need to parse this like
99 if ($name ne "attach") {
100 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
101 $ATTACH{$name} = $body;
103 # Otherwise it is an attachment and we need to finish it up
104 } elsif ($name eq "attach") {
105 $header =~ /filename=\"(.+)\"/;
106 $ATTACH{'FILE_NAME'} = $1;
107 $ATTACH{'FILE_NAME'} =~ s/\"//g;
108 $ATTACH{'FILE_NAME'} =~ s/\s//g;
109 $ATTACH{'FILE_CONTENT'} = $body;
111 for ($i = $x; $list[$i]; $i++) {
112 $list[$i] =~ s/^.+name=$//;
113 $list[$i] =~ /\"(\w+)\"/;
119 $main::lxdebug->leave_sub(2);
123 $main::lxdebug->leave_sub(2);
124 return _input_to_hash($input);
129 $main::lxdebug->enter_sub();
135 if ($LXDebug::watch_form) {
136 require SL::Watchdog;
137 tie %{ $self }, 'SL::Watchdog';
140 read(STDIN, $_, $ENV{CONTENT_LENGTH});
142 if ($ENV{QUERY_STRING}) {
143 $_ = $ENV{QUERY_STRING};
150 my %parameters = _request_to_hash($_);
151 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
153 $self->{action} = lc $self->{action};
154 $self->{action} =~ s/( |-|,|\#)/_/g;
156 $self->{version} = "2.4.2";
158 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
170 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
172 $main::lxdebug->leave_sub();
176 $main::lxdebug->enter_sub(2);
178 my ($self, $str) = @_;
180 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
182 $main::lxdebug->leave_sub(2);
188 $main::lxdebug->enter_sub(2);
190 my ($self, $str) = @_;
195 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
197 $main::lxdebug->leave_sub(2);
203 my ($self, $str) = @_;
205 if ($str && !ref($str)) {
206 $str =~ s/\"/"/g;
214 my ($self, $str) = @_;
216 if ($str && !ref($str)) {
217 $str =~ s/"/\"/g;
225 $main::lxdebug->enter_sub(2);
227 my ($self, $str) = @_;
230 ('order' => ['"', '<', '>'],
236 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
238 $main::lxdebug->leave_sub(2);
247 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
249 for (sort keys %$self) {
250 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
251 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
258 $main::lxdebug->enter_sub();
260 my ($self, $msg) = @_;
261 if ($ENV{HTTP_USER_AGENT}) {
263 $self->show_generic_error($msg);
267 if ($self->{error_function}) {
268 &{ $self->{error_function} }($msg);
274 $main::lxdebug->leave_sub();
278 $main::lxdebug->enter_sub();
280 my ($self, $msg) = @_;
282 if ($ENV{HTTP_USER_AGENT}) {
285 if (!$self->{header}) {
298 if ($self->{info_function}) {
299 &{ $self->{info_function} }($msg);
305 $main::lxdebug->leave_sub();
309 $main::lxdebug->enter_sub();
311 my ($self, $str, $cols, $maxrows) = @_;
315 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
317 $maxrows = $rows unless defined $maxrows;
319 $main::lxdebug->leave_sub();
321 return ($rows > $maxrows) ? $maxrows : $rows;
325 $main::lxdebug->enter_sub();
327 my ($self, $msg) = @_;
329 $self->error("$msg\n" . $DBI::errstr);
331 $main::lxdebug->leave_sub();
335 $main::lxdebug->enter_sub();
337 my ($self, $name, $msg) = @_;
339 if ($self->{$name} =~ /^\s*$/) {
342 $main::lxdebug->leave_sub();
346 $main::lxdebug->enter_sub();
348 my ($self, $extra_code) = @_;
350 if ($self->{header}) {
351 $main::lxdebug->leave_sub();
355 my ($stylesheet, $favicon, $charset);
357 if ($ENV{HTTP_USER_AGENT}) {
359 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
361 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
365 $self->{favicon} = "favicon.ico" unless $self->{favicon};
367 if ($self->{favicon} && (-f "$self->{favicon}")) {
369 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
373 if ($self->{charset}) {
375 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
378 if ($self->{landscape}) {
379 $pagelayout = qq|<style type="text/css">
380 \@page { size:landscape; }
384 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
388 if ($self->{jsscript} == 1) {
391 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
392 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
393 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
394 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
401 ? "$self->{title} - $self->{titlebar}"
404 foreach $item (@ { $self->{AJAX} }) {
405 $ajax .= $item->show_javascript();
407 print qq|Content-Type: text/html
411 <title>$self->{titlebar}</title>
419 <script type="text/javascript">
427 <meta name="robots" content="noindex,nofollow" />
428 <script type="text/javascript" src="js/highlight_input.js"></script>
429 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
431 <script type="text/javascript" src="js/tabcontent.js">
433 /***********************************************
434 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
435 * This notice MUST stay intact for legal use
436 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
437 ***********************************************/
448 $main::lxdebug->leave_sub();
451 sub parse_html_template {
452 $main::lxdebug->enter_sub();
454 my ($self, $file, $additional_params) = @_;
457 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
458 $language = $main::language;
460 $language = $main::myconfig{"countrycode"};
462 $language = "de" unless ($language);
464 if (-f "templates/webpages/${file}_${language}.html") {
465 if ((-f ".developer") &&
466 (-f "templates/webpages/${file}_master.html") &&
467 ((stat("templates/webpages/${file}_master.html"))[9] >
468 (stat("templates/webpages/${file}_${language}.html"))[9])) {
469 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
470 "Please re-run 'locales.pl' in 'locale/${language}'.";
471 print(qq|<pre>$info</pre>|);
475 $file = "templates/webpages/${file}_${language}.html";
476 } elsif (-f "templates/webpages/${file}.html") {
477 $file = "templates/webpages/${file}.html";
479 my $info = "Web page template '${file}' not found.\n" .
480 "Please re-run 'locales.pl' in 'locale/${language}'.";
481 print(qq|<pre>$info</pre>|);
485 my $template = HTML::Template->new("filename" => $file,
486 "die_on_bad_params" => 0,
488 "case_sensitive" => 1,
489 "loop_context_vars" => 1,
492 $additional_params = {} unless ($additional_params);
493 if ($self->{"DEBUG"}) {
494 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
497 if ($additional_params->{"DEBUG"}) {
498 $additional_params->{"DEBUG"} =
499 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
502 if (%main::myconfig) {
503 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
504 my $jsc_dateformat = $main::myconfig{"dateformat"};
505 $jsc_dateformat =~ s/d+/\%d/gi;
506 $jsc_dateformat =~ s/m+/\%m/gi;
507 $jsc_dateformat =~ s/y+/\%Y/gi;
508 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
511 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
512 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
513 $additional_params->{"conf_latex_templates"} = $main::latex;
514 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
516 my @additional_param_names = keys(%{$additional_params});
517 foreach my $key ($template->param()) {
518 my $param = $self->{$key};
519 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
520 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
521 $template->param($key => $param);
524 my $output = $template->output();
526 $main::lxdebug->leave_sub();
531 sub show_generic_error {
532 my ($self, $error, $title, $action) = @_;
535 $add_params->{"title"} = $title if ($title);
536 $self->{"label_error"} = $error;
540 map({ delete($self->{$_}); } qw(action));
541 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
542 if (!ref($self->{$_})); }
544 $add_params->{"SHOW_BUTTON"} = 1;
545 $add_params->{"BUTTON_LABEL"} = $action;
547 $add_params->{"VARIABLES"} = \@vars;
550 print($self->parse_html_template("generic/error", $add_params));
552 die("Error: $error\n");
555 sub show_generic_information {
556 my ($self, $error, $title) = @_;
559 $add_params->{"title"} = $title if ($title);
560 $self->{"label_information"} = $error;
563 print($self->parse_html_template("generic/information", $add_params));
565 die("Information: $error\n");
568 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
569 # changed it to accept an arbitrary number of triggers - sschoeling
571 $main::lxdebug->enter_sub();
574 my $myconfig = shift;
577 # set dateform for jsscript
580 "dd.mm.yy" => "%d.%m.%Y",
581 "dd-mm-yy" => "%d-%m-%Y",
582 "dd/mm/yy" => "%d/%m/%Y",
583 "mm/dd/yy" => "%m/%d/%Y",
584 "mm-dd-yy" => "%m-%d-%Y",
585 "yyyy-mm-dd" => "%Y-%m-%d",
588 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
589 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
596 inputField : "| . (shift) . qq|",
597 ifFormat :"$ifFormat",
598 align : "| . (shift) . qq|",
599 button : "| . (shift) . qq|"
605 <script type="text/javascript">
606 <!--| . join("", @triggers) . qq|//-->
610 $main::lxdebug->leave_sub();
613 } #end sub write_trigger
616 $main::lxdebug->enter_sub();
618 my ($self, $msg) = @_;
620 if ($self->{callback}) {
622 ($script, $argv) = split(/\?/, $self->{callback});
623 exec("perl", "$script", $argv);
631 $main::lxdebug->leave_sub();
634 # sort of columns removed - empty sub
636 $main::lxdebug->enter_sub();
638 my ($self, @columns) = @_;
640 $main::lxdebug->leave_sub();
646 $main::lxdebug->enter_sub(2);
648 my ($self, $myconfig, $amount, $places, $dash) = @_;
653 my $neg = ($amount =~ s/-//);
655 if (defined($places) && ($places ne '')) {
660 my ($actual_places) = ($amount =~ /\.(\d+)/);
661 $actual_places = length($actual_places);
662 $places = $actual_places > $places ? $actual_places : $places;
665 $amount = $self->round_amount($amount, $places);
668 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
669 my @p = split(/\./, $amount); # split amount at decimal point
671 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
674 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
677 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
678 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
679 ($neg ? "-$amount" : "$amount" ) ;
683 $main::lxdebug->leave_sub(2);
688 $main::lxdebug->enter_sub(2);
690 my ($self, $myconfig, $amount) = @_;
692 if ( ($myconfig->{numberformat} eq '1.000,00')
693 || ($myconfig->{numberformat} eq '1000,00')) {
698 if ($myconfig->{numberformat} eq "1'000.00") {
704 $main::lxdebug->leave_sub(2);
706 return ($amount * 1);
710 $main::lxdebug->enter_sub(2);
712 my ($self, $amount, $places) = @_;
715 # Rounding like "Kaufmannsrunden"
716 # Descr. http://de.wikipedia.org/wiki/Rundung
718 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
721 $amount = $amount * (10**($places));
722 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
724 $main::lxdebug->leave_sub(2);
726 return $round_amount;
731 $main::lxdebug->enter_sub();
733 my ($self, $myconfig, $userspath) = @_;
736 $self->{"cwd"} = getcwd();
737 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
739 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
740 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
741 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
742 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
743 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
744 } elsif (($self->{"format"} =~ /html/i) ||
745 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
746 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
747 } elsif (($self->{"format"} =~ /xml/i) ||
748 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
749 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
750 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
751 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
752 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
753 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
754 } elsif ( defined $self->{'format'}) {
755 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
756 } elsif ( $self->{'format'} eq '' ) {
757 $self->error("No Outputformat given: $self->{'format'}");
758 } else { #Catch the rest
759 $self->error("Outputformat not defined: $self->{'format'}");
762 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
763 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
765 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
766 qw(email tel fax name signature company address businessnumber
767 co_ustid taxnumber duns));
768 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
769 qw(company address signature));
770 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
772 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
774 # OUT is used for the media, screen, printer, email
775 # for postscript we store a copy in a temporary file
777 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
778 if ($template->uses_temp_file() || $self->{media} eq 'email') {
780 $self->{OUT} = ">$self->{tmpfile}";
784 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
786 open(OUT, ">-") or $self->error("STDOUT : $!");
790 if (!$template->parse(*OUT)) {
792 $self->error("$self->{IN} : " . $template->get_error());
797 if ($template->uses_temp_file() || $self->{media} eq 'email') {
799 if ($self->{media} eq 'email') {
803 my $mail = new Mailer;
805 map { $mail->{$_} = $self->{$_} }
806 qw(cc bcc subject message version format charset);
807 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
808 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
809 $mail->{fileid} = "$fileid.";
810 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
812 # if we send html or plain text inline
813 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
814 $mail->{contenttype} = "text/html";
816 $mail->{message} =~ s/\r\n/<br>\n/g;
817 $myconfig->{signature} =~ s/\\n/<br>\n/g;
818 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
820 open(IN, $self->{tmpfile})
821 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
823 $mail->{message} .= $_;
830 if (!$self->{"do_not_attach"}) {
831 @{ $mail->{attachments} } =
832 ({ "filename" => $self->{"tmpfile"},
833 "name" => $self->{"attachment_filename"} ?
834 $self->{"attachment_filename"} : $self->{"tmpfile"} });
837 $mail->{message} =~ s/\r\n/\n/g;
838 $myconfig->{signature} =~ s/\\n/\n/g;
839 $mail->{message} .= "\n-- \n$myconfig->{signature}";
843 my $err = $mail->send($out);
844 $self->error($self->cleanup . "$err") if ($err);
850 my $numbytes = (-s $self->{tmpfile});
851 open(IN, $self->{tmpfile})
852 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
854 $self->{copies} = 1 unless $self->{media} eq 'printer';
856 chdir("$self->{cwd}");
857 #print(STDERR "Kopien $self->{copies}\n");
858 #print(STDERR "OUT $self->{OUT}\n");
859 for my $i (1 .. $self->{copies}) {
861 open(OUT, $self->{OUT})
862 or $self->error($self->cleanup . "$self->{OUT} : $!");
864 $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
866 print qq|Content-Type: | . $template->get_mime_type() . qq|
867 Content-Disposition: attachment; filename="$self->{attachment_filename}"
868 Content-Length: $numbytes
872 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
892 chdir("$self->{cwd}");
893 $main::lxdebug->leave_sub();
897 $main::lxdebug->enter_sub();
901 chdir("$self->{tmpdir}");
904 if (-f "$self->{tmpfile}.err") {
905 open(FH, "$self->{tmpfile}.err");
910 if ($self->{tmpfile}) {
911 $self->{tmpfile} =~ s|.*/||g;
913 $self->{tmpfile} =~ s/\.\w+$//g;
914 my $tmpfile = $self->{tmpfile};
915 unlink(<$tmpfile.*>);
918 chdir("$self->{cwd}");
920 $main::lxdebug->leave_sub();
926 $main::lxdebug->enter_sub();
928 my ($self, $date, $myconfig) = @_;
930 if ($date && $date =~ /\D/) {
932 if ($myconfig->{dateformat} =~ /^yy/) {
933 ($yy, $mm, $dd) = split /\D/, $date;
935 if ($myconfig->{dateformat} =~ /^mm/) {
936 ($mm, $dd, $yy) = split /\D/, $date;
938 if ($myconfig->{dateformat} =~ /^dd/) {
939 ($dd, $mm, $yy) = split /\D/, $date;
944 $yy = ($yy < 70) ? $yy + 2000 : $yy;
945 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
947 $dd = "0$dd" if ($dd < 10);
948 $mm = "0$mm" if ($mm < 10);
953 $main::lxdebug->leave_sub();
958 # Database routines used throughout
961 $main::lxdebug->enter_sub(2);
963 my ($self, $myconfig) = @_;
965 # connect to database
967 DBI->connect($myconfig->{dbconnect},
968 $myconfig->{dbuser}, $myconfig->{dbpasswd})
972 if ($myconfig->{dboptions}) {
973 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
976 $main::lxdebug->leave_sub(2);
981 sub dbconnect_noauto {
982 $main::lxdebug->enter_sub();
984 my ($self, $myconfig) = @_;
986 # connect to database
988 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
989 $myconfig->{dbpasswd}, { AutoCommit => 0 })
993 if ($myconfig->{dboptions}) {
994 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
997 $main::lxdebug->leave_sub();
1002 sub update_balance {
1003 $main::lxdebug->enter_sub();
1005 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1007 # if we have a value, go do it
1010 # retrieve balance from table
1011 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1012 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1013 my ($balance) = $sth->fetchrow_array;
1019 $query = "UPDATE $table SET $field = $balance WHERE $where";
1020 do_query($self, $dbh, $query, @values);
1022 $main::lxdebug->leave_sub();
1025 sub update_exchangerate {
1026 $main::lxdebug->enter_sub();
1028 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1030 # some sanity check for currency
1032 $main::lxdebug->leave_sub();
1036 my $query = qq|SELECT e.curr FROM exchangerate e
1037 WHERE e.curr = ? AND e.transdate = ?
1039 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1042 if ($buy != 0 && $sell != 0) {
1043 $set = "buy = $buy, sell = $sell";
1044 } elsif ($buy != 0) {
1045 $set = "buy = $buy";
1046 } elsif ($sell != 0) {
1047 $set = "sell = $sell";
1050 if ($sth->fetchrow_array) {
1051 $query = qq|UPDATE exchangerate
1056 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1057 VALUES (?, $buy, $sell, ?)|;
1060 do_query($self, $dbh, $query, $curr, $transdate);
1062 $main::lxdebug->leave_sub();
1065 sub save_exchangerate {
1066 $main::lxdebug->enter_sub();
1068 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1070 my $dbh = $self->dbconnect($myconfig);
1072 my ($buy, $sell) = (0, 0);
1073 $buy = $rate if $fld eq 'buy';
1074 $sell = $rate if $fld eq 'sell';
1076 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1080 $main::lxdebug->leave_sub();
1083 sub get_exchangerate {
1084 $main::lxdebug->enter_sub();
1086 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1088 unless ($transdate) {
1089 $main::lxdebug->leave_sub();
1093 my $query = qq|SELECT e.$fld FROM exchangerate e
1094 WHERE e.curr = ? AND e.transdate = ?|;
1095 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1097 if (!$exchangerate) {
1101 $main::lxdebug->leave_sub();
1103 return $exchangerate;
1106 sub check_exchangerate {
1107 $main::lxdebug->enter_sub();
1109 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1111 unless ($transdate) {
1112 $main::lxdebug->leave_sub();
1116 my $dbh = $self->dbconnect($myconfig);
1118 my $query = qq|SELECT e.$fld FROM exchangerate e
1119 WHERE e.curr = ? AND e.transdate = ?|;
1120 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1123 $main::lxdebug->leave_sub();
1125 return $exchangerate;
1128 sub set_payment_options {
1129 $main::lxdebug->enter_sub();
1131 my ($self, $myconfig, $transdate) = @_;
1133 if ($self->{payment_id}) {
1135 my $dbh = $self->dbconnect($myconfig);
1138 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1139 qq|FROM payment_terms p | .
1142 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1143 $self->{payment_terms}) =
1144 selectrow_query($self, $dbh, $query, $self->{payment_id});
1146 if ($transdate eq "") {
1147 if ($self->{invdate}) {
1148 $transdate = $self->{invdate};
1150 $transdate = $self->{transdate};
1155 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1156 qq|FROM payment_terms|;
1157 ($self->{netto_date}, $self->{skonto_date}) =
1158 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1160 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1161 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1162 $self->{percent_skonto};
1164 $self->{skonto_amount} =
1165 $self->format_amount($myconfig, $skonto_amount, 2);
1167 if ($self->{"language_id"}) {
1169 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1170 qq|FROM translation_payment_terms t | .
1171 qq|LEFT JOIN language l ON t.language_id = l.id | .
1172 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1173 my ($description_long, $output_numberformat, $output_dateformat,
1174 $output_longdates) =
1175 selectrow_query($self, $dbh, $query,
1176 $self->{"language_id"}, $self->{"payment_id"});
1178 $self->{payment_terms} = $description_long if ($description_long);
1180 if ($output_dateformat) {
1181 foreach my $key (qw(netto_date skonto_date)) {
1183 $main::locale->reformat_date($myconfig, $self->{$key},
1189 if ($output_numberformat &&
1190 ($output_numberformat ne $myconfig->{"numberformat"})) {
1191 my $saved_numberformat = $myconfig->{"numberformat"};
1192 $myconfig->{"numberformat"} = $output_numberformat;
1193 $self->{skonto_amount} =
1194 $self->format_amount($myconfig, $skonto_amount, 2);
1195 $myconfig->{"numberformat"} = $saved_numberformat;
1199 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1200 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1201 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1202 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1203 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1204 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1205 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1206 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1207 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1208 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1213 $main::lxdebug->leave_sub();
1217 sub get_template_language {
1218 $main::lxdebug->enter_sub();
1220 my ($self, $myconfig) = @_;
1222 my $template_code = "";
1224 if ($self->{language_id}) {
1225 my $dbh = $self->dbconnect($myconfig);
1226 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1227 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1231 $main::lxdebug->leave_sub();
1233 return $template_code;
1236 sub get_printer_code {
1237 $main::lxdebug->enter_sub();
1239 my ($self, $myconfig) = @_;
1241 my $template_code = "";
1243 if ($self->{printer_id}) {
1244 my $dbh = $self->dbconnect($myconfig);
1245 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1246 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1250 $main::lxdebug->leave_sub();
1252 return $template_code;
1256 $main::lxdebug->enter_sub();
1258 my ($self, $myconfig) = @_;
1260 my $template_code = "";
1262 if ($self->{shipto_id}) {
1263 my $dbh = $self->dbconnect($myconfig);
1264 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1265 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1266 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1270 $main::lxdebug->leave_sub();
1274 $main::lxdebug->enter_sub();
1276 my ($self, $dbh, $id, $module) = @_;
1280 foreach my $item (qw(name department_1 department_2 street zipcode city country
1281 contact phone fax email)) {
1282 if ($self->{"shipto$item"}) {
1283 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1285 push(@values, $self->{"shipto${item}"});
1288 if ($self->{shipto_id}) {
1289 my $query = qq|UPDATE shipto set
1291 shiptodepartment_1 = ?,
1292 shiptodepartment_2 = ?,
1301 WHERE shipto_id = ?|;
1302 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1304 my $query = qq|SELECT * FROM shipto
1305 WHERE shiptoname = ? AND
1306 shiptodepartment_1 = ? AND
1307 shiptodepartment_2 = ? AND
1308 shiptostreet = ? AND
1309 shiptozipcode = ? AND
1311 shiptocountry = ? AND
1312 shiptocontact = ? AND
1316 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1319 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1320 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1321 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1322 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1323 do_query($self, $dbh, $query, $id, @values, $module);
1328 $main::lxdebug->leave_sub();
1332 $main::lxdebug->enter_sub();
1334 my ($self, $dbh) = @_;
1336 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1337 ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
1338 $self->{employee_id} *= 1;
1340 $main::lxdebug->leave_sub();
1344 $main::lxdebug->enter_sub();
1346 my ($self, $myconfig, $salesman_id) = @_;
1348 $main::lxdebug->leave_sub() and return unless $salesman_id;
1350 my $dbh = $self->dbconnect($myconfig);
1353 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1357 my $user = new User($main::memberfile, $login);
1358 map({ $self->{"salesman_$_"} = $user->{$_}; }
1359 qw(address businessnumber co_ustid company duns email fax name
1361 $self->{salesman_login} = $login;
1363 $self->{salesman_name} = $login
1364 if ($self->{salesman_name} eq "");
1366 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1371 $main::lxdebug->leave_sub();
1375 $main::lxdebug->enter_sub();
1377 my ($self, $myconfig) = @_;
1379 my $dbh = $self->dbconnect($myconfig);
1380 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1381 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1384 $main::lxdebug->leave_sub();
1388 $main::lxdebug->enter_sub();
1390 my ($self, $dbh, $id, $key) = @_;
1392 $key = "all_contacts" unless ($key);
1395 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1396 qq|FROM contacts | .
1397 qq|WHERE cp_cv_id = ? | .
1398 qq|ORDER BY lower(cp_name)|;
1400 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1402 $main::lxdebug->leave_sub();
1406 $main::lxdebug->enter_sub();
1408 my ($self, $dbh, $key) = @_;
1410 my ($all, $old_id, $where, @values);
1412 if (ref($key) eq "HASH") {
1415 $key = "ALL_PROJECTS";
1417 foreach my $p (keys(%{$params})) {
1419 $all = $params->{$p};
1420 } elsif ($p eq "old_id") {
1421 $old_id = $params->{$p};
1422 } elsif ($p eq "key") {
1423 $key = $params->{$p};
1429 $where = "WHERE active ";
1431 if (ref($old_id) eq "ARRAY") {
1432 my @ids = grep({ $_ } @{$old_id});
1434 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1435 push(@values, @ids);
1438 $where .= " OR (id = ?) ";
1439 push(@values, $old_id);
1445 qq|SELECT id, projectnumber, description, active | .
1448 qq|ORDER BY lower(projectnumber)|;
1450 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1452 $main::lxdebug->leave_sub();
1456 $main::lxdebug->enter_sub();
1458 my ($self, $dbh, $vc_id, $key) = @_;
1460 $key = "all_shipto" unless ($key);
1462 # get shipping addresses
1464 qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
1466 qq|WHERE trans_id = ?|;
1468 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1470 $main::lxdebug->leave_sub();
1474 $main::lxdebug->enter_sub();
1476 my ($self, $dbh, $key) = @_;
1478 $key = "all_printers" unless ($key);
1480 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1482 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1484 $main::lxdebug->leave_sub();
1488 $main::lxdebug->enter_sub();
1490 my ($self, $dbh, $params) = @_;
1492 $key = $params->{key};
1493 $key = "all_charts" unless ($key);
1495 my $transdate = quote_db_date($params->{transdate});
1498 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1500 qq|LEFT JOIN taxkeys tk ON | .
1501 qq|(tk.id = (SELECT id FROM taxkeys | .
1502 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1503 qq| ORDER BY startdate DESC LIMIT 1)) | .
1504 qq|ORDER BY c.accno|;
1506 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1508 $main::lxdebug->leave_sub();
1511 sub _get_taxcharts {
1512 $main::lxdebug->enter_sub();
1514 my ($self, $dbh, $key) = @_;
1516 $key = "all_taxcharts" unless ($key);
1518 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1520 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1522 $main::lxdebug->leave_sub();
1526 $main::lxdebug->enter_sub();
1528 my ($self, $dbh, $key) = @_;
1530 $key = "all_taxzones" unless ($key);
1532 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1534 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1536 $main::lxdebug->leave_sub();
1539 sub _get_employees {
1540 $main::lxdebug->enter_sub();
1542 my ($self, $dbh, $key) = @_;
1544 $key = "all_employees" unless ($key);
1546 selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
1548 $main::lxdebug->leave_sub();
1551 sub _get_business_types {
1552 $main::lxdebug->enter_sub();
1554 my ($self, $dbh, $key) = @_;
1556 $key = "all_business_types" unless ($key);
1558 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1560 $main::lxdebug->leave_sub();
1563 sub _get_languages {
1564 $main::lxdebug->enter_sub();
1566 my ($self, $dbh, $key) = @_;
1568 $key = "all_languages" unless ($key);
1570 my $query = qq|SELECT * FROM language ORDER BY id|;
1572 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1574 $main::lxdebug->leave_sub();
1577 sub _get_dunning_configs {
1578 $main::lxdebug->enter_sub();
1580 my ($self, $dbh, $key) = @_;
1582 $key = "all_dunning_configs" unless ($key);
1584 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1586 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1588 $main::lxdebug->leave_sub();
1591 sub _get_currencies {
1592 $main::lxdebug->enter_sub();
1594 my ($self, $dbh, $key) = @_;
1596 $key = "all_currencies" unless ($key);
1598 my $query = qq|SELECT curr AS currency FROM defaults|;
1600 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1602 $main::lxdebug->leave_sub();
1605 sub _get_customers {
1606 $main::lxdebug->enter_sub();
1608 my ($self, $dbh, $key) = @_;
1610 $key = "all_customers" unless ($key);
1612 my $query = qq|SELECT * FROM customer LIMIT $main::myconfig{vclimit}|;
1614 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1616 $main::lxdebug->leave_sub();
1620 $main::lxdebug->enter_sub();
1622 my ($self, $dbh, $key) = @_;
1624 $key = "all_vendors" unless ($key);
1626 my $query = qq|SELECT * FROM vendor|; # LIMIT $main::myconfig{vclimit}|;
1628 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1630 $main::lxdebug->leave_sub();
1634 $main::lxdebug->enter_sub();
1639 my $dbh = $self->dbconnect(\%main::myconfig);
1640 my ($sth, $query, $ref);
1642 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1643 my $vc_id = $self->{"${vc}_id"};
1645 if ($params{"contacts"}) {
1646 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1649 if ($params{"shipto"}) {
1650 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1653 if ($params{"projects"} || $params{"all_projects"}) {
1654 $self->_get_projects($dbh, $params{"all_projects"} ?
1655 $params{"all_projects"} : $params{"projects"},
1656 $params{"all_projects"} ? 1 : 0);
1659 if ($params{"printers"}) {
1660 $self->_get_printers($dbh, $params{"printers"});
1663 if ($params{"languages"}) {
1664 $self->_get_languages($dbh, $params{"languages"});
1667 if ($params{"charts"}) {
1668 $self->_get_charts($dbh, $params{"charts"});
1671 if ($params{"taxcharts"}) {
1672 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1675 if ($params{"taxzones"}) {
1676 $self->_get_taxzones($dbh, $params{"taxzones"});
1679 if ($params{"employees"}) {
1680 $self->_get_employees($dbh, $params{"employees"});
1683 if ($params{"business_types"}) {
1684 $self->_get_business_types($dbh, $params{"business_types"});
1687 if ($params{"dunning_configs"}) {
1688 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1691 if($params{"currencies"}) {
1692 $self->_get_currencies($dbh, $params{"currencies"});
1695 if($params{"customers"}) {
1696 $self->_get_customers($dbh, $params{"customers"});
1699 if($params{"vendors"}) {
1700 $self->_get_vendors($dbh, $params{"vendors"});
1705 $main::lxdebug->leave_sub();
1708 # this sub gets the id and name from $table
1710 $main::lxdebug->enter_sub();
1712 my ($self, $myconfig, $table) = @_;
1714 # connect to database
1715 my $dbh = $self->dbconnect($myconfig);
1717 $table = $table eq "customer" ? "customer" : "vendor";
1718 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1720 my ($query, @values);
1722 if (!$self->{openinvoices}) {
1724 if ($self->{customernumber} ne "") {
1725 $where = qq|(vc.customernumber ILIKE ?)|;
1726 push(@values, '%' . $self->{customernumber} . '%');
1728 $where = qq|(vc.name ILIKE ?)|;
1729 push(@values, '%' . $self->{$table} . '%');
1733 qq~SELECT vc.id, vc.name,
1734 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1736 WHERE $where AND (NOT vc.obsolete)
1740 qq~SELECT DISTINCT vc.id, vc.name,
1741 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1743 JOIN $table vc ON (a.${table}_id = vc.id)
1744 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1746 push(@values, '%' . $self->{$table} . '%');
1749 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1751 $main::lxdebug->leave_sub();
1753 return scalar(@{ $self->{name_list} });
1756 # the selection sub is used in the AR, AP, IS, IR and OE module
1759 $main::lxdebug->enter_sub();
1761 my ($self, $myconfig, $table, $module) = @_;
1764 my $dbh = $self->dbconnect($myconfig);
1766 $table = $table eq "customer" ? "customer" : "vendor";
1768 my $query = qq|SELECT count(*) FROM $table|;
1769 my ($count) = selectrow_query($self, $dbh, $query);
1771 # build selection list
1772 if ($count < $myconfig->{vclimit}) {
1773 $query = qq|SELECT id, name, salesman_id
1774 FROM $table WHERE NOT obsolete
1776 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1780 $self->get_employee($dbh);
1782 # setup sales contacts
1783 $query = qq|SELECT e.id, e.name
1785 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1786 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1789 push(@{ $self->{all_employees} },
1790 { id => $self->{employee_id},
1791 name => $self->{employee} });
1793 # sort the whole thing
1794 @{ $self->{all_employees} } =
1795 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1797 if ($module eq 'AR') {
1799 # prepare query for departments
1800 $query = qq|SELECT id, description
1803 ORDER BY description|;
1806 $query = qq|SELECT id, description
1808 ORDER BY description|;
1811 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1814 $query = qq|SELECT id, description
1818 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1821 $query = qq|SELECT printer_description, id
1823 ORDER BY printer_description|;
1825 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1828 $query = qq|SELECT id, description
1832 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1836 $main::lxdebug->leave_sub();
1839 sub language_payment {
1840 $main::lxdebug->enter_sub();
1842 my ($self, $myconfig) = @_;
1844 my $dbh = $self->dbconnect($myconfig);
1846 my $query = qq|SELECT id, description
1850 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1853 $query = qq|SELECT printer_description, id
1855 ORDER BY printer_description|;
1857 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1860 $query = qq|SELECT id, description
1864 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1866 # get buchungsgruppen
1867 $query = qq|SELECT id, description
1868 FROM buchungsgruppen|;
1870 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1873 $main::lxdebug->leave_sub();
1876 # this is only used for reports
1877 sub all_departments {
1878 $main::lxdebug->enter_sub();
1880 my ($self, $myconfig, $table) = @_;
1882 my $dbh = $self->dbconnect($myconfig);
1885 if ($table eq 'customer') {
1886 $where = "WHERE role = 'P' ";
1889 my $query = qq|SELECT id, description
1892 ORDER BY description|;
1893 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1895 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1899 $main::lxdebug->leave_sub();
1903 $main::lxdebug->enter_sub();
1905 my ($self, $module, $myconfig, $table) = @_;
1908 if ($table eq "customer") {
1917 $self->all_vc($myconfig, $table, $module);
1919 # get last customers or vendors
1920 my ($query, $sth, $ref);
1922 my $dbh = $self->dbconnect($myconfig);
1927 my $transdate = "current_date";
1928 if ($self->{transdate}) {
1929 $transdate = $dbh->quote($self->{transdate});
1932 # now get the account numbers
1933 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1934 FROM chart c, taxkeys tk
1935 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1936 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1939 $sth = $dbh->prepare($query);
1941 do_statement($self, $sth, $query, '%' . $module . '%');
1943 $self->{accounts} = "";
1944 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1946 foreach my $key (split(/:/, $ref->{link})) {
1947 if ($key =~ /$module/) {
1949 # cross reference for keys
1950 $xkeyref{ $ref->{accno} } = $key;
1952 push @{ $self->{"${module}_links"}{$key} },
1953 { accno => $ref->{accno},
1954 description => $ref->{description},
1955 taxkey => $ref->{taxkey_id},
1956 tax_id => $ref->{tax_id} };
1958 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1964 # get taxkeys and description
1965 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
1966 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
1968 if (($module eq "AP") || ($module eq "AR")) {
1969 # get tax rates and description
1970 $query = qq|SELECT * FROM tax|;
1971 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
1977 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
1978 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
1979 a.intnotes, a.department_id, a.amount AS oldinvtotal,
1980 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
1982 d.description AS department,
1985 JOIN $table c ON (a.${table}_id = c.id)
1986 LEFT JOIN employee e ON (e.id = a.employee_id)
1987 LEFT JOIN department d ON (d.id = a.department_id)
1989 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
1991 foreach $key (keys %$ref) {
1992 $self->{$key} = $ref->{$key};
1995 my $transdate = "current_date";
1996 if ($self->{transdate}) {
1997 $transdate = $dbh->quote($self->{transdate});
2000 # now get the account numbers
2001 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2003 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2005 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2006 OR c.link LIKE '%_tax%')
2009 $sth = $dbh->prepare($query);
2010 do_statement($self, $sth, $query, "%$module%");
2012 $self->{accounts} = "";
2013 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2015 foreach my $key (split(/:/, $ref->{link})) {
2016 if ($key =~ /$module/) {
2018 # cross reference for keys
2019 $xkeyref{ $ref->{accno} } = $key;
2021 push @{ $self->{"${module}_links"}{$key} },
2022 { accno => $ref->{accno},
2023 description => $ref->{description},
2024 taxkey => $ref->{taxkey_id},
2025 tax_id => $ref->{tax_id} };
2027 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2033 # get amounts from individual entries
2036 c.accno, c.description,
2037 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2041 LEFT JOIN chart c ON (c.id = a.chart_id)
2042 LEFT JOIN project p ON (p.id = a.project_id)
2043 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2044 WHERE (tk.taxkey_id=a.taxkey) AND
2045 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2046 THEN tk.chart_id = a.chart_id
2049 OR (c.link='%tax%')) AND
2050 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2051 WHERE a.trans_id = ?
2052 AND a.fx_transaction = '0'
2053 ORDER BY a.oid, a.transdate|;
2054 $sth = $dbh->prepare($query);
2055 do_statement($self, $sth, $query, $self->{id});
2057 # get exchangerate for currency
2058 $self->{exchangerate} =
2059 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2062 # store amounts in {acc_trans}{$key} for multiple accounts
2063 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2064 $ref->{exchangerate} =
2065 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2066 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2069 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2070 $ref->{amount} *= -1;
2072 $ref->{index} = $index;
2074 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2080 d.curr AS currencies, d.closedto, d.revtrans,
2081 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2082 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2084 $ref = selectfirst_hashref_query($self, $dbh, $query);
2085 map { $self->{$_} = $ref->{$_} } keys %$ref;
2092 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2093 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2094 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2096 $ref = selectfirst_hashref_query($self, $dbh, $query);
2097 map { $self->{$_} = $ref->{$_} } keys %$ref;
2099 if ($self->{"$self->{vc}_id"}) {
2101 # only setup currency
2102 ($self->{currency}) = split(/:/, $self->{currencies});
2106 $self->lastname_used($dbh, $myconfig, $table, $module);
2108 # get exchangerate for currency
2109 $self->{exchangerate} =
2110 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2118 $main::lxdebug->leave_sub();
2122 $main::lxdebug->enter_sub();
2124 my ($self, $dbh, $myconfig, $table, $module) = @_;
2126 my $arap = ($table eq 'customer') ? "ar" : "ap";
2127 $table = $table eq "customer" ? "customer" : "vendor";
2128 my $where = "1 = 1";
2130 if ($self->{type} =~ /_order/) {
2132 $where = "quotation = '0'";
2134 if ($self->{type} =~ /_quotation/) {
2136 $where = "quotation = '1'";
2139 my $query = qq|SELECT MAX(id) FROM $arap
2140 WHERE $where AND ${table}_id > 0|;
2141 my ($trans_id) = selectrow_query($self, $dbh, $query);
2146 a.curr, a.${table}_id, a.department_id,
2147 d.description AS department,
2148 ct.name, current_date + ct.terms AS duedate
2150 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2151 LEFT JOIN department d ON (a.department_id = d.id)
2153 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2154 $self->{department}, $self->{$table}, $self->{duedate})
2155 = selectrow_query($self, $dbh, $query, $trans_id);
2157 $main::lxdebug->leave_sub();
2161 $main::lxdebug->enter_sub();
2163 my ($self, $myconfig, $thisdate, $days) = @_;
2165 my $dbh = $self->dbconnect($myconfig);
2170 my $dateformat = $myconfig->{dateformat};
2171 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2172 $thisdate = $dbh->quote($thisdate);
2173 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2175 $query = qq|SELECT current_date AS thisdate|;
2178 ($thisdate) = selectrow_query($self, $dbh, $query);
2182 $main::lxdebug->leave_sub();
2188 $main::lxdebug->enter_sub();
2190 my ($self, $string) = @_;
2192 if ($string !~ /%/) {
2193 $string = "%$string%";
2196 $string =~ s/\'/\'\'/g;
2198 $main::lxdebug->leave_sub();
2204 $main::lxdebug->enter_sub();
2206 my ($self, $flds, $new, $count, $numrows) = @_;
2210 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2216 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2218 $j = $item->{ndx} - 1;
2219 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2223 for $i ($count + 1 .. $numrows) {
2224 map { delete $self->{"${_}_$i"} } @{$flds};
2227 $main::lxdebug->leave_sub();
2231 $main::lxdebug->enter_sub();
2233 my ($self, $myconfig) = @_;
2237 my $dbh = $self->dbconnect_noauto($myconfig);
2239 my $query = qq|DELETE FROM status
2240 WHERE (formname = ?) AND (trans_id = ?)|;
2241 my $sth = prepare_query($self, $dbh, $query);
2243 if ($self->{formname} =~ /(check|receipt)/) {
2244 for $i (1 .. $self->{rowcount}) {
2245 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2248 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2252 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2253 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2255 my %queued = split / /, $self->{queued};
2258 if ($self->{formname} =~ /(check|receipt)/) {
2260 # this is a check or receipt, add one entry for each lineitem
2261 my ($accno) = split /--/, $self->{account};
2262 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2263 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2264 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2265 $sth = prepare_query($self, $dbh, $query);
2267 for $i (1 .. $self->{rowcount}) {
2268 if ($self->{"checked_$i"}) {
2269 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2275 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2276 VALUES (?, ?, ?, ?, ?)|;
2277 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2278 $queued{$self->{formname}}, $self->{formname});
2284 $main::lxdebug->leave_sub();
2288 $main::lxdebug->enter_sub();
2290 my ($self, $dbh) = @_;
2292 my ($query, $printed, $emailed);
2294 my $formnames = $self->{printed};
2295 my $emailforms = $self->{emailed};
2297 my $query = qq|DELETE FROM status
2298 WHERE (formname = ?) AND (trans_id = ?)|;
2299 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2301 # this only applies to the forms
2302 # checks and receipts are posted when printed or queued
2304 if ($self->{queued}) {
2305 my %queued = split / /, $self->{queued};
2307 foreach my $formname (keys %queued) {
2308 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2309 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2311 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2312 VALUES (?, ?, ?, ?, ?)|;
2313 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2315 $formnames =~ s/$self->{formname}//;
2316 $emailforms =~ s/$self->{formname}//;
2321 # save printed, emailed info
2322 $formnames =~ s/^ +//g;
2323 $emailforms =~ s/^ +//g;
2326 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2327 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2329 foreach my $formname (keys %status) {
2330 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2331 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2333 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2334 VALUES (?, ?, ?, ?)|;
2335 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2338 $main::lxdebug->leave_sub();
2342 # $main::locale->text('SAVED')
2343 # $main::locale->text('DELETED')
2344 # $main::locale->text('ADDED')
2345 # $main::locale->text('PAYMENT POSTED')
2346 # $main::locale->text('POSTED')
2347 # $main::locale->text('POSTED AS NEW')
2348 # $main::locale->text('ELSE')
2349 # $main::locale->text('SAVED FOR DUNNING')
2350 # $main::locale->text('DUNNING STARTED')
2351 # $main::locale->text('PRINTED')
2352 # $main::locale->text('MAILED')
2353 # $main::locale->text('SCREENED')
2354 # $main::locale->text('CANCELED')
2355 # $main::locale->text('invoice')
2356 # $main::locale->text('proforma')
2357 # $main::locale->text('sales_order')
2358 # $main::locale->text('packing_list')
2359 # $main::locale->text('pick_list')
2360 # $main::locale->text('purchase_order')
2361 # $main::locale->text('bin_list')
2362 # $main::locale->text('sales_quotation')
2363 # $main::locale->text('request_quotation')
2366 $main::lxdebug->enter_sub();
2371 if(!exists $self->{employee_id}) {
2372 &get_employee($self, $dbh);
2376 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2377 qq|VALUES (?, ?, ?, ?, ?)|;
2378 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2379 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2380 do_query($self, $dbh, $query, @values);
2382 $main::lxdebug->leave_sub();
2386 $main::lxdebug->enter_sub();
2390 my $trans_id = shift();
2391 my $restriction = shift();
2394 if ($trans_id ne "") {
2396 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 | .
2397 qq|FROM history_erp h | .
2398 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2399 qq|WHERE trans_id = ? |
2402 my $sth = $dbh->prepare($query) || $self->dberror($query);
2404 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2406 while(my $hash_ref = $sth->fetchrow_hashref()) {
2407 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2408 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2409 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2410 $tempArray[$i++] = $hash_ref;
2412 $main::lxdebug->leave_sub() and return \@tempArray
2413 if ($i > 0 && $tempArray[0] ne "");
2415 $main::lxdebug->leave_sub();
2419 sub update_defaults {
2420 $main::lxdebug->enter_sub();
2422 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2425 if ($provided_dbh) {
2426 $dbh = $provided_dbh;
2428 $dbh = $self->dbconnect_noauto($myconfig);
2430 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2431 my $sth = $dbh->prepare($query);
2433 $sth->execute || $self->dberror($query);
2434 my ($var) = $sth->fetchrow_array;
2437 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2440 $query = qq|UPDATE defaults SET $fld = ?|;
2441 do_query($self, $dbh, $query, $var);
2443 if (!$provided_dbh) {
2448 $main::lxdebug->leave_sub();
2453 sub update_business {
2454 $main::lxdebug->enter_sub();
2456 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2459 if ($provided_dbh) {
2460 $dbh = $provided_dbh;
2462 $dbh = $self->dbconnect_noauto($myconfig);
2465 qq|SELECT customernumberinit FROM business
2466 WHERE id = ? FOR UPDATE|;
2467 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2469 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2471 $query = qq|UPDATE business
2472 SET customernumberinit = ?
2474 do_query($self, $dbh, $query, $var, $business_id);
2476 if (!$provided_dbh) {
2481 $main::lxdebug->leave_sub();
2486 sub get_partsgroup {
2487 $main::lxdebug->enter_sub();
2489 my ($self, $myconfig, $p) = @_;
2491 my $dbh = $self->dbconnect($myconfig);
2493 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2495 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2498 if ($p->{searchitems} eq 'part') {
2499 $query .= qq|WHERE p.inventory_accno_id > 0|;
2501 if ($p->{searchitems} eq 'service') {
2502 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2504 if ($p->{searchitems} eq 'assembly') {
2505 $query .= qq|WHERE p.assembly = '1'|;
2507 if ($p->{searchitems} eq 'labor') {
2508 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2511 $query .= qq|ORDER BY partsgroup|;
2514 $query = qq|SELECT id, partsgroup FROM partsgroup
2515 ORDER BY partsgroup|;
2518 if ($p->{language_code}) {
2519 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2520 t.description AS translation
2522 JOIN parts p ON (p.partsgroup_id = pg.id)
2523 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2524 ORDER BY translation|;
2525 @values = ($p->{language_code});
2528 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2531 $main::lxdebug->leave_sub();
2534 sub get_pricegroup {
2535 $main::lxdebug->enter_sub();
2537 my ($self, $myconfig, $p) = @_;
2539 my $dbh = $self->dbconnect($myconfig);
2541 my $query = qq|SELECT p.id, p.pricegroup
2544 $query .= qq| ORDER BY pricegroup|;
2547 $query = qq|SELECT id, pricegroup FROM pricegroup
2548 ORDER BY pricegroup|;
2551 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2555 $main::lxdebug->leave_sub();
2559 # usage $form->all_years($myconfig, [$dbh])
2560 # return list of all years where bookings found
2563 $main::lxdebug->enter_sub();
2565 my ($self, $myconfig, $dbh) = @_;
2569 $dbh = $self->dbconnect($myconfig);
2574 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2575 (SELECT MAX(transdate) FROM acc_trans)|;
2576 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2578 if ($myconfig->{dateformat} =~ /^yy/) {
2579 ($startdate) = split /\W/, $startdate;
2580 ($enddate) = split /\W/, $enddate;
2582 (@_) = split /\W/, $startdate;
2584 (@_) = split /\W/, $enddate;
2589 $startdate = substr($startdate,0,4);
2590 $enddate = substr($enddate,0,4);
2592 while ($enddate >= $startdate) {
2593 push @all_years, $enddate--;
2596 $dbh->disconnect if $disconnect;
2600 $main::lxdebug->leave_sub();