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();
1578 $main::lxdebug->enter_sub();
1583 my $dbh = $self->dbconnect(\%main::myconfig);
1584 my ($sth, $query, $ref);
1586 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1587 my $vc_id = $self->{"${vc}_id"};
1589 if ($params{"contacts"}) {
1590 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1593 if ($params{"shipto"}) {
1594 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1597 if ($params{"projects"} || $params{"all_projects"}) {
1598 $self->_get_projects($dbh, $params{"all_projects"} ?
1599 $params{"all_projects"} : $params{"projects"},
1600 $params{"all_projects"} ? 1 : 0);
1603 if ($params{"printers"}) {
1604 $self->_get_printers($dbh, $params{"printers"});
1607 if ($params{"languages"}) {
1608 $self->_get_languages($dbh, $params{"languages"});
1611 if ($params{"charts"}) {
1612 $self->_get_charts($dbh, $params{"charts"});
1615 if ($params{"taxcharts"}) {
1616 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1619 if ($params{"taxzones"}) {
1620 $self->_get_taxzones($dbh, $params{"taxzones"});
1623 if ($params{"employees"}) {
1624 $self->_get_employees($dbh, $params{"employees"});
1627 if ($params{"business_types"}) {
1628 $self->_get_business_types($dbh, $params{"business_types"});
1633 $main::lxdebug->leave_sub();
1636 # this sub gets the id and name from $table
1638 $main::lxdebug->enter_sub();
1640 my ($self, $myconfig, $table) = @_;
1642 # connect to database
1643 my $dbh = $self->dbconnect($myconfig);
1645 $table = $table eq "customer" ? "customer" : "vendor";
1646 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1648 my ($query, @values);
1650 if (!$self->{openinvoices}) {
1652 if ($self->{customernumber} ne "") {
1653 $where = qq|(vc.customernumber ILIKE ?)|;
1654 push(@values, '%' . $self->{customernumber} . '%');
1656 $where = qq|(vc.name ILIKE ?)|;
1657 push(@values, '%' . $self->{$table} . '%');
1661 qq~SELECT vc.id, vc.name,
1662 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1664 WHERE $where AND (NOT vc.obsolete)
1668 qq~SELECT DISTINCT vc.id, vc.name,
1669 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1671 JOIN $table vc ON (a.${table}_id = vc.id)
1672 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1674 push(@values, '%' . $self->{$table} . '%');
1677 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1679 $main::lxdebug->leave_sub();
1681 return scalar(@{ $self->{name_list} });
1684 # the selection sub is used in the AR, AP, IS, IR and OE module
1687 $main::lxdebug->enter_sub();
1689 my ($self, $myconfig, $table, $module) = @_;
1692 my $dbh = $self->dbconnect($myconfig);
1694 $table = $table eq "customer" ? "customer" : "vendor";
1696 my $query = qq|SELECT count(*) FROM $table|;
1697 my ($count) = selectrow_query($self, $dbh, $query);
1699 # build selection list
1700 if ($count < $myconfig->{vclimit}) {
1701 $query = qq|SELECT id, name, salesman_id
1702 FROM $table WHERE NOT obsolete
1704 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1708 $self->get_employee($dbh);
1710 # setup sales contacts
1711 $query = qq|SELECT e.id, e.name
1713 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1714 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1717 push(@{ $self->{all_employees} },
1718 { id => $self->{employee_id},
1719 name => $self->{employee} });
1721 # sort the whole thing
1722 @{ $self->{all_employees} } =
1723 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1725 if ($module eq 'AR') {
1727 # prepare query for departments
1728 $query = qq|SELECT id, description
1731 ORDER BY description|;
1734 $query = qq|SELECT id, description
1736 ORDER BY description|;
1739 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1742 $query = qq|SELECT id, description
1746 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1749 $query = qq|SELECT printer_description, id
1751 ORDER BY printer_description|;
1753 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1756 $query = qq|SELECT id, description
1760 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1764 $main::lxdebug->leave_sub();
1767 sub language_payment {
1768 $main::lxdebug->enter_sub();
1770 my ($self, $myconfig) = @_;
1772 my $dbh = $self->dbconnect($myconfig);
1774 my $query = qq|SELECT id, description
1778 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1781 $query = qq|SELECT printer_description, id
1783 ORDER BY printer_description|;
1785 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1788 $query = qq|SELECT id, description
1792 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1794 # get buchungsgruppen
1795 $query = qq|SELECT id, description
1796 FROM buchungsgruppen|;
1798 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1801 $main::lxdebug->leave_sub();
1804 # this is only used for reports
1805 sub all_departments {
1806 $main::lxdebug->enter_sub();
1808 my ($self, $myconfig, $table) = @_;
1810 my $dbh = $self->dbconnect($myconfig);
1813 if ($table eq 'customer') {
1814 $where = "WHERE role = 'P' ";
1817 my $query = qq|SELECT id, description
1820 ORDER BY description|;
1821 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1823 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1827 $main::lxdebug->leave_sub();
1831 $main::lxdebug->enter_sub();
1833 my ($self, $module, $myconfig, $table) = @_;
1836 if ($table eq "customer") {
1845 $self->all_vc($myconfig, $table, $module);
1847 # get last customers or vendors
1848 my ($query, $sth, $ref);
1850 my $dbh = $self->dbconnect($myconfig);
1855 my $transdate = "current_date";
1856 if ($self->{transdate}) {
1857 $transdate = $dbh->quote($self->{transdate});
1860 # now get the account numbers
1861 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1862 FROM chart c, taxkeys tk
1863 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1864 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1867 $sth = $dbh->prepare($query);
1869 do_statement($self, $sth, $query, '%' . $module . '%');
1871 $self->{accounts} = "";
1872 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1874 foreach my $key (split(/:/, $ref->{link})) {
1875 if ($key =~ /$module/) {
1877 # cross reference for keys
1878 $xkeyref{ $ref->{accno} } = $key;
1880 push @{ $self->{"${module}_links"}{$key} },
1881 { accno => $ref->{accno},
1882 description => $ref->{description},
1883 taxkey => $ref->{taxkey_id},
1884 tax_id => $ref->{tax_id} };
1886 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1892 # get taxkeys and description
1893 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
1894 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
1896 if (($module eq "AP") || ($module eq "AR")) {
1897 # get tax rates and description
1898 $query = qq|SELECT * FROM tax|;
1899 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
1905 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
1906 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
1907 a.intnotes, a.department_id, a.amount AS oldinvtotal,
1908 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
1910 d.description AS department,
1913 JOIN $table c ON (a.${table}_id = c.id)
1914 LEFT JOIN employee e ON (e.id = a.employee_id)
1915 LEFT JOIN department d ON (d.id = a.department_id)
1917 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
1919 foreach $key (keys %$ref) {
1920 $self->{$key} = $ref->{$key};
1923 my $transdate = "current_date";
1924 if ($self->{transdate}) {
1925 $transdate = $dbh->quote($self->{transdate});
1928 # now get the account numbers
1929 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1930 FROM chart c, taxkeys tk
1932 AND ( tk.chart_id = c.id OR c.link LIKE '%_tax%')
1933 AND (NOT tk.chart_id = c.id OR NOT c.link LIKE '%_tax%')
1934 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
1935 OR c.link LIKE '%_tax%')
1938 $sth = $dbh->prepare($query);
1939 do_statement($self, $sth, $query, "%$module%");
1941 $self->{accounts} = "";
1942 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1944 foreach my $key (split(/:/, $ref->{link})) {
1945 if ($key =~ /$module/) {
1947 # cross reference for keys
1948 $xkeyref{ $ref->{accno} } = $key;
1950 push @{ $self->{"${module}_links"}{$key} },
1951 { accno => $ref->{accno},
1952 description => $ref->{description},
1953 taxkey => $ref->{taxkey_id},
1954 tax_id => $ref->{tax_id} };
1956 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1962 # get amounts from individual entries
1965 c.accno, c.description,
1966 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
1970 LEFT JOIN chart c ON (c.id = a.chart_id)
1971 LEFT JOIN project p ON (p.id = a.project_id)
1972 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
1973 WHERE (tk.taxkey_id=a.taxkey) AND
1974 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
1975 THEN tk.chart_id = a.chart_id
1978 OR (c.link='%tax%')) AND
1979 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
1980 WHERE a.trans_id = ?
1981 AND a.fx_transaction = '0'
1982 ORDER BY a.oid, a.transdate|;
1983 $sth = $dbh->prepare($query);
1984 do_statement($self, $sth, $query, $self->{id});
1986 # get exchangerate for currency
1987 $self->{exchangerate} =
1988 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
1991 # store amounts in {acc_trans}{$key} for multiple accounts
1992 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1993 $ref->{exchangerate} =
1994 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
1995 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1998 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1999 $ref->{amount} *= -1;
2001 $ref->{index} = $index;
2003 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2009 d.curr AS currencies, d.closedto, d.revtrans,
2010 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2011 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2013 $ref = selectfirst_hashref_query($self, $dbh, $query);
2014 map { $self->{$_} = $ref->{$_} } keys %$ref;
2021 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2022 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2023 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2025 $ref = selectfirst_hashref_query($self, $dbh, $query);
2026 map { $self->{$_} = $ref->{$_} } keys %$ref;
2028 if ($self->{"$self->{vc}_id"}) {
2030 # only setup currency
2031 ($self->{currency}) = split(/:/, $self->{currencies});
2035 $self->lastname_used($dbh, $myconfig, $table, $module);
2037 # get exchangerate for currency
2038 $self->{exchangerate} =
2039 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2047 $main::lxdebug->leave_sub();
2051 $main::lxdebug->enter_sub();
2053 my ($self, $dbh, $myconfig, $table, $module) = @_;
2055 my $arap = ($table eq 'customer') ? "ar" : "ap";
2056 $table = $table eq "customer" ? "customer" : "vendor";
2057 my $where = "1 = 1";
2059 if ($self->{type} =~ /_order/) {
2061 $where = "quotation = '0'";
2063 if ($self->{type} =~ /_quotation/) {
2065 $where = "quotation = '1'";
2068 my $query = qq|SELECT MAX(id) FROM $arap
2069 WHERE $where AND ${table}_id > 0|;
2070 my ($trans_id) = selectrow_query($self, $dbh, $query);
2075 a.curr, a.${table}_id, a.department_id,
2076 d.description AS department,
2077 ct.name, current_date + ct.terms AS duedate
2079 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2080 LEFT JOIN department d ON (a.department_id = d.id)
2082 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2083 $self->{department}, $self->{$table}, $self->{duedate})
2084 = selectrow_query($self, $dbh, $query, $trans_id);
2086 $main::lxdebug->leave_sub();
2090 $main::lxdebug->enter_sub();
2092 my ($self, $myconfig, $thisdate, $days) = @_;
2094 my $dbh = $self->dbconnect($myconfig);
2099 my $dateformat = $myconfig->{dateformat};
2100 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2101 $thisdate = $dbh->quote($thisdate);
2102 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2104 $query = qq|SELECT current_date AS thisdate|;
2107 ($thisdate) = selectrow_query($self, $dbh, $query);
2111 $main::lxdebug->leave_sub();
2117 $main::lxdebug->enter_sub();
2119 my ($self, $string) = @_;
2121 if ($string !~ /%/) {
2122 $string = "%$string%";
2125 $string =~ s/\'/\'\'/g;
2127 $main::lxdebug->leave_sub();
2133 $main::lxdebug->enter_sub();
2135 my ($self, $flds, $new, $count, $numrows) = @_;
2139 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2145 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2147 $j = $item->{ndx} - 1;
2148 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2152 for $i ($count + 1 .. $numrows) {
2153 map { delete $self->{"${_}_$i"} } @{$flds};
2156 $main::lxdebug->leave_sub();
2160 $main::lxdebug->enter_sub();
2162 my ($self, $myconfig) = @_;
2166 my $dbh = $self->dbconnect_noauto($myconfig);
2168 my $query = qq|DELETE FROM status
2169 WHERE (formname = ?) AND (trans_id = ?)|;
2170 my $sth = prepare_query($self, $dbh, $query);
2172 if ($self->{formname} =~ /(check|receipt)/) {
2173 for $i (1 .. $self->{rowcount}) {
2174 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2177 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2181 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2182 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2184 my %queued = split / /, $self->{queued};
2187 if ($self->{formname} =~ /(check|receipt)/) {
2189 # this is a check or receipt, add one entry for each lineitem
2190 my ($accno) = split /--/, $self->{account};
2191 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2192 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2193 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2194 $sth = prepare_query($self, $dbh, $query);
2196 for $i (1 .. $self->{rowcount}) {
2197 if ($self->{"checked_$i"}) {
2198 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2204 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2205 VALUES (?, ?, ?, ?, ?)|;
2206 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2207 $queued{$self->{formname}}, $self->{formname});
2213 $main::lxdebug->leave_sub();
2217 $main::lxdebug->enter_sub();
2219 my ($self, $dbh) = @_;
2221 my ($query, $printed, $emailed);
2223 my $formnames = $self->{printed};
2224 my $emailforms = $self->{emailed};
2226 my $query = qq|DELETE FROM status
2227 WHERE (formname = ?) AND (trans_id = ?)|;
2228 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2230 # this only applies to the forms
2231 # checks and receipts are posted when printed or queued
2233 if ($self->{queued}) {
2234 my %queued = split / /, $self->{queued};
2236 foreach my $formname (keys %queued) {
2237 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2238 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2240 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2241 VALUES (?, ?, ?, ?, ?)|;
2242 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2244 $formnames =~ s/$self->{formname}//;
2245 $emailforms =~ s/$self->{formname}//;
2250 # save printed, emailed info
2251 $formnames =~ s/^ +//g;
2252 $emailforms =~ s/^ +//g;
2255 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2256 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2258 foreach my $formname (keys %status) {
2259 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2260 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2262 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2263 VALUES (?, ?, ?, ?)|;
2264 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2267 $main::lxdebug->leave_sub();
2271 # $main::locale->text('SAVED')
2272 # $main::locale->text('DELETED')
2273 # $main::locale->text('ADDED')
2274 # $main::locale->text('PAYMENT POSTED')
2275 # $main::locale->text('POSTED')
2276 # $main::locale->text('POSTED AS NEW')
2277 # $main::locale->text('ELSE')
2278 # $main::locale->text('SAVED FOR DUNNING')
2279 # $main::locale->text('DUNNING STARTED')
2280 # $main::locale->text('PRINTED')
2281 # $main::locale->text('MAILED')
2282 # $main::locale->text('SCREENED')
2283 # $main::locale->text('invoice')
2284 # $main::locale->text('proforma')
2285 # $main::locale->text('sales_order')
2286 # $main::locale->text('packing_list')
2287 # $main::locale->text('pick_list')
2288 # $main::locale->text('purchase_order')
2289 # $main::locale->text('bin_list')
2290 # $main::locale->text('sales_quotation')
2291 # $main::locale->text('request_quotation')
2294 $main::lxdebug->enter_sub();
2299 if(!exists $self->{employee_id}) {
2300 &get_employee($self, $dbh);
2304 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2305 qq|VALUES (?, ?, ?, ?, ?)|;
2306 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2307 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2308 do_query($self, $dbh, $query, @values);
2310 $main::lxdebug->leave_sub();
2314 $main::lxdebug->enter_sub();
2318 my $trans_id = shift();
2319 my $restriction = shift();
2322 if ($trans_id ne "") {
2324 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 | .
2325 qq|FROM history_erp h | .
2326 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2327 qq|WHERE trans_id = ? |
2330 my $sth = $dbh->prepare($query) || $self->dberror($query);
2332 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2334 while(my $hash_ref = $sth->fetchrow_hashref()) {
2335 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2336 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2337 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2338 $tempArray[$i++] = $hash_ref;
2340 $main::lxdebug->leave_sub() and return \@tempArray
2341 if ($i > 0 && $tempArray[0] ne "");
2343 $main::lxdebug->leave_sub();
2347 sub update_defaults {
2348 $main::lxdebug->enter_sub();
2350 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2353 if ($provided_dbh) {
2354 $dbh = $provided_dbh;
2356 $dbh = $self->dbconnect_noauto($myconfig);
2358 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2359 my $sth = $dbh->prepare($query);
2361 $sth->execute || $self->dberror($query);
2362 my ($var) = $sth->fetchrow_array;
2365 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2368 $query = qq|UPDATE defaults SET $fld = ?|;
2369 do_query($self, $dbh, $query, $var);
2371 if (!$provided_dbh) {
2376 $main::lxdebug->leave_sub();
2381 sub update_business {
2382 $main::lxdebug->enter_sub();
2384 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2387 if ($provided_dbh) {
2388 $dbh = $provided_dbh;
2390 $dbh = $self->dbconnect_noauto($myconfig);
2393 qq|SELECT customernumberinit FROM business
2394 WHERE id = ? FOR UPDATE|;
2395 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2397 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2399 $query = qq|UPDATE business
2400 SET customernumberinit = ?
2402 do_query($self, $dbh, $query, $var, $business_id);
2404 if (!$provided_dbh) {
2409 $main::lxdebug->leave_sub();
2414 sub get_partsgroup {
2415 $main::lxdebug->enter_sub();
2417 my ($self, $myconfig, $p) = @_;
2419 my $dbh = $self->dbconnect($myconfig);
2421 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2423 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2426 if ($p->{searchitems} eq 'part') {
2427 $query .= qq|WHERE p.inventory_accno_id > 0|;
2429 if ($p->{searchitems} eq 'service') {
2430 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2432 if ($p->{searchitems} eq 'assembly') {
2433 $query .= qq|WHERE p.assembly = '1'|;
2435 if ($p->{searchitems} eq 'labor') {
2436 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2439 $query .= qq|ORDER BY partsgroup|;
2442 $query = qq|SELECT id, partsgroup FROM partsgroup
2443 ORDER BY partsgroup|;
2446 if ($p->{language_code}) {
2447 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2448 t.description AS translation
2450 JOIN parts p ON (p.partsgroup_id = pg.id)
2451 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2452 ORDER BY translation|;
2453 @values = ($p->{language_code});
2456 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2459 $main::lxdebug->leave_sub();
2462 sub get_pricegroup {
2463 $main::lxdebug->enter_sub();
2465 my ($self, $myconfig, $p) = @_;
2467 my $dbh = $self->dbconnect($myconfig);
2469 my $query = qq|SELECT p.id, p.pricegroup
2472 $query .= qq| ORDER BY pricegroup|;
2475 $query = qq|SELECT id, pricegroup FROM pricegroup
2476 ORDER BY pricegroup|;
2479 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2483 $main::lxdebug->leave_sub();
2487 # usage $form->all_years($myconfig, [$dbh])
2488 # return list of all years where bookings found
2491 $main::lxdebug->enter_sub();
2493 my ($self, $myconfig, $dbh) = @_;
2497 $dbh = $self->dbconnect($myconfig);
2502 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2503 (SELECT MAX(transdate) FROM acc_trans)|;
2504 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2506 if ($myconfig->{dateformat} =~ /^yy/) {
2507 ($startdate) = split /\W/, $startdate;
2508 ($enddate) = split /\W/, $enddate;
2510 (@_) = split /\W/, $startdate;
2512 (@_) = split /\W/, $enddate;
2517 $startdate = substr($startdate,0,4);
2518 $enddate = substr($enddate,0,4);
2520 while ($enddate >= $startdate) {
2521 push @all_years, $enddate--;
2524 $dbh->disconnect if $disconnect;
2528 $main::lxdebug->leave_sub();