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 read(STDIN, $_, $ENV{CONTENT_LENGTH});
137 if ($ENV{QUERY_STRING}) {
138 $_ = $ENV{QUERY_STRING};
145 my %parameters = _request_to_hash($_);
146 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
148 $self->{action} = lc $self->{action};
149 $self->{action} =~ s/( |-|,|\#)/_/g;
151 $self->{version} = "2.4.2";
153 $main::lxdebug->leave_sub();
159 $main::lxdebug->enter_sub();
165 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
167 $main::lxdebug->leave_sub();
171 $main::lxdebug->enter_sub(2);
173 my ($self, $str) = @_;
175 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
177 $main::lxdebug->leave_sub(2);
183 $main::lxdebug->enter_sub(2);
185 my ($self, $str) = @_;
190 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
192 $main::lxdebug->leave_sub(2);
198 my ($self, $str) = @_;
200 if ($str && !ref($str)) {
201 $str =~ s/\"/"/g;
209 my ($self, $str) = @_;
211 if ($str && !ref($str)) {
212 $str =~ s/"/\"/g;
220 $main::lxdebug->enter_sub(2);
222 my ($self, $str) = @_;
225 ('order' => ['"', '<', '>'],
231 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
233 $main::lxdebug->leave_sub(2);
242 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
244 for (sort keys %$self) {
245 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
246 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
253 $main::lxdebug->enter_sub();
255 my ($self, $msg) = @_;
256 if ($ENV{HTTP_USER_AGENT}) {
258 $self->show_generic_error($msg);
262 if ($self->{error_function}) {
263 &{ $self->{error_function} }($msg);
269 $main::lxdebug->leave_sub();
273 $main::lxdebug->enter_sub();
275 my ($self, $msg) = @_;
277 if ($ENV{HTTP_USER_AGENT}) {
280 if (!$self->{header}) {
293 if ($self->{info_function}) {
294 &{ $self->{info_function} }($msg);
300 $main::lxdebug->leave_sub();
304 $main::lxdebug->enter_sub();
306 my ($self, $str, $cols, $maxrows) = @_;
310 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
312 $maxrows = $rows unless defined $maxrows;
314 $main::lxdebug->leave_sub();
316 return ($rows > $maxrows) ? $maxrows : $rows;
320 $main::lxdebug->enter_sub();
322 my ($self, $msg) = @_;
324 $self->error("$msg\n" . $DBI::errstr);
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
332 my ($self, $name, $msg) = @_;
334 if ($self->{$name} =~ /^\s*$/) {
337 $main::lxdebug->leave_sub();
341 $main::lxdebug->enter_sub();
343 my ($self, $extra_code) = @_;
345 if ($self->{header}) {
346 $main::lxdebug->leave_sub();
350 my ($stylesheet, $favicon, $charset);
352 if ($ENV{HTTP_USER_AGENT}) {
354 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
356 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
360 $self->{favicon} = "favicon.ico" unless $self->{favicon};
362 if ($self->{favicon} && (-f "$self->{favicon}")) {
364 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
368 if ($self->{charset}) {
370 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
373 if ($self->{landscape}) {
374 $pagelayout = qq|<style type="text/css">
375 \@page { size:landscape; }
379 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
383 if ($self->{jsscript} == 1) {
386 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
387 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
388 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
389 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
396 ? "$self->{title} - $self->{titlebar}"
399 foreach $item (@ { $self->{AJAX} }) {
400 $ajax .= $item->show_javascript();
402 print qq|Content-Type: text/html
406 <title>$self->{titlebar}</title>
414 <script type="text/javascript">
422 <meta name="robots" content="noindex,nofollow" />
423 <script type="text/javascript" src="js/highlight_input.js"></script>
424 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
426 <script type="text/javascript" src="js/tabcontent.js">
428 /***********************************************
429 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
430 * This notice MUST stay intact for legal use
431 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
432 ***********************************************/
443 $main::lxdebug->leave_sub();
446 sub parse_html_template {
447 $main::lxdebug->enter_sub();
449 my ($self, $file, $additional_params) = @_;
452 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
453 $language = $main::language;
455 $language = $main::myconfig{"countrycode"};
457 $language = "de" unless ($language);
459 if (-f "templates/webpages/${file}_${language}.html") {
460 if ((-f ".developer") &&
461 (-f "templates/webpages/${file}_master.html") &&
462 ((stat("templates/webpages/${file}_master.html"))[9] >
463 (stat("templates/webpages/${file}_${language}.html"))[9])) {
464 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
465 "Please re-run 'locales.pl' in 'locale/${language}'.";
466 print(qq|<pre>$info</pre>|);
470 $file = "templates/webpages/${file}_${language}.html";
471 } elsif (-f "templates/webpages/${file}.html") {
472 $file = "templates/webpages/${file}.html";
474 my $info = "Web page template '${file}' not found.\n" .
475 "Please re-run 'locales.pl' in 'locale/${language}'.";
476 print(qq|<pre>$info</pre>|);
480 my $template = HTML::Template->new("filename" => $file,
481 "die_on_bad_params" => 0,
483 "case_sensitive" => 1,
484 "loop_context_vars" => 1,
487 $additional_params = {} unless ($additional_params);
488 if ($self->{"DEBUG"}) {
489 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
492 if ($additional_params->{"DEBUG"}) {
493 $additional_params->{"DEBUG"} =
494 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
497 if (%main::myconfig) {
498 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
499 my $jsc_dateformat = $main::myconfig{"dateformat"};
500 $jsc_dateformat =~ s/d+/\%d/gi;
501 $jsc_dateformat =~ s/m+/\%m/gi;
502 $jsc_dateformat =~ s/y+/\%Y/gi;
503 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
506 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
507 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
508 $additional_params->{"conf_latex_templates"} = $main::latex;
509 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
511 my @additional_param_names = keys(%{$additional_params});
512 foreach my $key ($template->param()) {
513 my $param = $self->{$key};
514 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
515 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
516 $template->param($key => $param);
519 my $output = $template->output();
521 $main::lxdebug->leave_sub();
526 sub show_generic_error {
527 my ($self, $error, $title, $action) = @_;
530 $add_params->{"title"} = $title if ($title);
531 $self->{"label_error"} = $error;
535 map({ delete($self->{$_}); } qw(action));
536 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
537 if (!ref($self->{$_})); }
539 $add_params->{"SHOW_BUTTON"} = 1;
540 $add_params->{"BUTTON_LABEL"} = $action;
542 $add_params->{"VARIABLES"} = \@vars;
545 print($self->parse_html_template("generic/error", $add_params));
547 die("Error: $error\n");
550 sub show_generic_information {
551 my ($self, $error, $title) = @_;
554 $add_params->{"title"} = $title if ($title);
555 $self->{"label_information"} = $error;
558 print($self->parse_html_template("generic/information", $add_params));
560 die("Information: $error\n");
563 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
564 # changed it to accept an arbitrary number of triggers - sschoeling
566 $main::lxdebug->enter_sub();
569 my $myconfig = shift;
572 # set dateform for jsscript
575 "dd.mm.yy" => "%d.%m.%Y",
576 "dd-mm-yy" => "%d-%m-%Y",
577 "dd/mm/yy" => "%d/%m/%Y",
578 "mm/dd/yy" => "%m/%d/%Y",
579 "mm-dd-yy" => "%m-%d-%Y",
580 "yyyy-mm-dd" => "%Y-%m-%d",
583 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
584 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
591 inputField : "| . (shift) . qq|",
592 ifFormat :"$ifFormat",
593 align : "| . (shift) . qq|",
594 button : "| . (shift) . qq|"
600 <script type="text/javascript">
601 <!--| . join("", @triggers) . qq|//-->
605 $main::lxdebug->leave_sub();
608 } #end sub write_trigger
611 $main::lxdebug->enter_sub();
613 my ($self, $msg) = @_;
615 if ($self->{callback}) {
617 ($script, $argv) = split(/\?/, $self->{callback});
618 exec("perl", "$script", $argv);
626 $main::lxdebug->leave_sub();
629 # sort of columns removed - empty sub
631 $main::lxdebug->enter_sub();
633 my ($self, @columns) = @_;
635 $main::lxdebug->leave_sub();
641 $main::lxdebug->enter_sub(2);
643 my ($self, $myconfig, $amount, $places, $dash) = @_;
648 my $neg = ($amount =~ s/-//);
650 if (defined($places) && ($places ne '')) {
655 my ($actual_places) = ($amount =~ /\.(\d+)/);
656 $actual_places = length($actual_places);
657 $places = $actual_places > $places ? $actual_places : $places;
660 $amount = $self->round_amount($amount, $places);
663 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
664 my @p = split(/\./, $amount); # split amount at decimal point
666 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
669 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
672 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
673 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
674 ($neg ? "-$amount" : "$amount" ) ;
678 $main::lxdebug->leave_sub(2);
683 $main::lxdebug->enter_sub(2);
685 my ($self, $myconfig, $amount) = @_;
687 if ( ($myconfig->{numberformat} eq '1.000,00')
688 || ($myconfig->{numberformat} eq '1000,00')) {
693 if ($myconfig->{numberformat} eq "1'000.00") {
699 $main::lxdebug->leave_sub(2);
701 return ($amount * 1);
705 $main::lxdebug->enter_sub(2);
707 my ($self, $amount, $places) = @_;
710 # Rounding like "Kaufmannsrunden"
711 # Descr. http://de.wikipedia.org/wiki/Rundung
713 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
716 $amount = $amount * (10**($places));
717 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
719 $main::lxdebug->leave_sub(2);
721 return $round_amount;
726 $main::lxdebug->enter_sub();
728 my ($self, $myconfig, $userspath) = @_;
731 $self->{"cwd"} = getcwd();
732 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
734 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
735 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
736 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
737 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
738 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
739 } elsif (($self->{"format"} =~ /html/i) ||
740 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
741 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
742 } elsif (($self->{"format"} =~ /xml/i) ||
743 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
744 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
745 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
746 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
747 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
748 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
749 } elsif ( defined $self->{'format'}) {
750 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
751 } elsif ( $self->{'format'} eq '' ) {
752 $self->error("No Outputformat given: $self->{'format'}");
753 } else { #Catch the rest
754 $self->error("Outputformat not defined: $self->{'format'}");
757 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
758 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
760 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
761 qw(email tel fax name signature company address businessnumber
762 co_ustid taxnumber duns));
763 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
764 qw(company address signature));
765 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
767 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
769 # OUT is used for the media, screen, printer, email
770 # for postscript we store a copy in a temporary file
772 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
773 if ($template->uses_temp_file() || $self->{media} eq 'email') {
775 $self->{OUT} = ">$self->{tmpfile}";
779 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
781 open(OUT, ">-") or $self->error("STDOUT : $!");
785 if (!$template->parse(*OUT)) {
787 $self->error("$self->{IN} : " . $template->get_error());
792 if ($template->uses_temp_file() || $self->{media} eq 'email') {
794 if ($self->{media} eq 'email') {
798 my $mail = new Mailer;
800 map { $mail->{$_} = $self->{$_} }
801 qw(cc bcc subject message version format charset);
802 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
803 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
804 $mail->{fileid} = "$fileid.";
805 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
807 # if we send html or plain text inline
808 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
809 $mail->{contenttype} = "text/html";
811 $mail->{message} =~ s/\r\n/<br>\n/g;
812 $myconfig->{signature} =~ s/\\n/<br>\n/g;
813 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
815 open(IN, $self->{tmpfile})
816 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
818 $mail->{message} .= $_;
825 if (!$self->{"do_not_attach"}) {
826 @{ $mail->{attachments} } =
827 ({ "filename" => $self->{"tmpfile"},
828 "name" => $self->{"attachment_filename"} ?
829 $self->{"attachment_filename"} : $self->{"tmpfile"} });
832 $mail->{message} =~ s/\r\n/\n/g;
833 $myconfig->{signature} =~ s/\\n/\n/g;
834 $mail->{message} .= "\n-- \n$myconfig->{signature}";
838 my $err = $mail->send($out);
839 $self->error($self->cleanup . "$err") if ($err);
845 my $numbytes = (-s $self->{tmpfile});
846 open(IN, $self->{tmpfile})
847 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
849 $self->{copies} = 1 unless $self->{media} eq 'printer';
851 chdir("$self->{cwd}");
852 #print(STDERR "Kopien $self->{copies}\n");
853 #print(STDERR "OUT $self->{OUT}\n");
854 for my $i (1 .. $self->{copies}) {
856 open(OUT, $self->{OUT})
857 or $self->error($self->cleanup . "$self->{OUT} : $!");
859 $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
861 print qq|Content-Type: | . $template->get_mime_type() . qq|
862 Content-Disposition: attachment; filename="$self->{attachment_filename}"
863 Content-Length: $numbytes
867 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
887 chdir("$self->{cwd}");
888 $main::lxdebug->leave_sub();
892 $main::lxdebug->enter_sub();
896 chdir("$self->{tmpdir}");
899 if (-f "$self->{tmpfile}.err") {
900 open(FH, "$self->{tmpfile}.err");
905 if ($self->{tmpfile}) {
906 $self->{tmpfile} =~ s|.*/||g;
908 $self->{tmpfile} =~ s/\.\w+$//g;
909 my $tmpfile = $self->{tmpfile};
910 unlink(<$tmpfile.*>);
913 chdir("$self->{cwd}");
915 $main::lxdebug->leave_sub();
921 $main::lxdebug->enter_sub();
923 my ($self, $date, $myconfig) = @_;
925 if ($date && $date =~ /\D/) {
927 if ($myconfig->{dateformat} =~ /^yy/) {
928 ($yy, $mm, $dd) = split /\D/, $date;
930 if ($myconfig->{dateformat} =~ /^mm/) {
931 ($mm, $dd, $yy) = split /\D/, $date;
933 if ($myconfig->{dateformat} =~ /^dd/) {
934 ($dd, $mm, $yy) = split /\D/, $date;
939 $yy = ($yy < 70) ? $yy + 2000 : $yy;
940 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
942 $dd = "0$dd" if ($dd < 10);
943 $mm = "0$mm" if ($mm < 10);
948 $main::lxdebug->leave_sub();
953 # Database routines used throughout
956 $main::lxdebug->enter_sub(2);
958 my ($self, $myconfig) = @_;
960 # connect to database
962 DBI->connect($myconfig->{dbconnect},
963 $myconfig->{dbuser}, $myconfig->{dbpasswd})
967 if ($myconfig->{dboptions}) {
968 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
971 $main::lxdebug->leave_sub(2);
976 sub dbconnect_noauto {
977 $main::lxdebug->enter_sub();
979 my ($self, $myconfig) = @_;
981 # connect to database
983 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
984 $myconfig->{dbpasswd}, { AutoCommit => 0 })
988 if ($myconfig->{dboptions}) {
989 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
992 $main::lxdebug->leave_sub();
998 $main::lxdebug->enter_sub();
1000 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1002 # if we have a value, go do it
1005 # retrieve balance from table
1006 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1007 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1008 my ($balance) = $sth->fetchrow_array;
1014 $query = "UPDATE $table SET $field = $balance WHERE $where";
1015 do_query($self, $dbh, $query, @values);
1017 $main::lxdebug->leave_sub();
1020 sub update_exchangerate {
1021 $main::lxdebug->enter_sub();
1023 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1025 # some sanity check for currency
1027 $main::lxdebug->leave_sub();
1031 my $query = qq|SELECT e.curr FROM exchangerate e
1032 WHERE e.curr = ? AND e.transdate = ?
1034 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1037 if ($buy != 0 && $sell != 0) {
1038 $set = "buy = $buy, sell = $sell";
1039 } elsif ($buy != 0) {
1040 $set = "buy = $buy";
1041 } elsif ($sell != 0) {
1042 $set = "sell = $sell";
1045 if ($sth->fetchrow_array) {
1046 $query = qq|UPDATE exchangerate
1051 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1052 VALUES (?, $buy, $sell, ?)|;
1055 do_query($self, $dbh, $query, $curr, $transdate);
1057 $main::lxdebug->leave_sub();
1060 sub save_exchangerate {
1061 $main::lxdebug->enter_sub();
1063 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1065 my $dbh = $self->dbconnect($myconfig);
1067 my ($buy, $sell) = (0, 0);
1068 $buy = $rate if $fld eq 'buy';
1069 $sell = $rate if $fld eq 'sell';
1071 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1075 $main::lxdebug->leave_sub();
1078 sub get_exchangerate {
1079 $main::lxdebug->enter_sub();
1081 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1083 unless ($transdate) {
1084 $main::lxdebug->leave_sub();
1088 my $query = qq|SELECT e.$fld FROM exchangerate e
1089 WHERE e.curr = ? AND e.transdate = ?|;
1090 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1092 if (!$exchangerate) {
1096 $main::lxdebug->leave_sub();
1098 return $exchangerate;
1101 sub check_exchangerate {
1102 $main::lxdebug->enter_sub();
1104 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1106 unless ($transdate) {
1107 $main::lxdebug->leave_sub();
1111 my $dbh = $self->dbconnect($myconfig);
1113 my $query = qq|SELECT e.$fld FROM exchangerate e
1114 WHERE e.curr = ? AND e.transdate = ?|;
1115 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1118 $main::lxdebug->leave_sub();
1120 return $exchangerate;
1123 sub set_payment_options {
1124 $main::lxdebug->enter_sub();
1126 my ($self, $myconfig, $transdate) = @_;
1128 if ($self->{payment_id}) {
1130 my $dbh = $self->dbconnect($myconfig);
1133 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1134 qq|FROM payment_terms p | .
1137 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1138 $self->{payment_terms}) =
1139 selectrow_query($self, $dbh, $query, $self->{payment_id});
1141 if ($transdate eq "") {
1142 if ($self->{invdate}) {
1143 $transdate = $self->{invdate};
1145 $transdate = $self->{transdate};
1150 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1151 qq|FROM payment_terms|;
1152 ($self->{netto_date}, $self->{skonto_date}) =
1153 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1155 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1156 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1157 $self->{percent_skonto};
1159 $self->{skonto_amount} =
1160 $self->format_amount($myconfig, $skonto_amount, 2);
1162 if ($self->{"language_id"}) {
1164 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1165 qq|FROM translation_payment_terms t | .
1166 qq|LEFT JOIN language l ON t.language_id = l.id | .
1167 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1168 my ($description_long, $output_numberformat, $output_dateformat,
1169 $output_longdates) =
1170 selectrow_query($self, $dbh, $query,
1171 $self->{"language_id"}, $self->{"payment_id"});
1173 $self->{payment_terms} = $description_long if ($description_long);
1175 if ($output_dateformat) {
1176 foreach my $key (qw(netto_date skonto_date)) {
1178 $main::locale->reformat_date($myconfig, $self->{$key},
1184 if ($output_numberformat &&
1185 ($output_numberformat ne $myconfig->{"numberformat"})) {
1186 my $saved_numberformat = $myconfig->{"numberformat"};
1187 $myconfig->{"numberformat"} = $output_numberformat;
1188 $self->{skonto_amount} =
1189 $self->format_amount($myconfig, $skonto_amount, 2);
1190 $myconfig->{"numberformat"} = $saved_numberformat;
1194 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1195 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1196 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1197 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1198 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1199 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1200 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1201 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1202 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1203 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1208 $main::lxdebug->leave_sub();
1212 sub get_template_language {
1213 $main::lxdebug->enter_sub();
1215 my ($self, $myconfig) = @_;
1217 my $template_code = "";
1219 if ($self->{language_id}) {
1220 my $dbh = $self->dbconnect($myconfig);
1221 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1222 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1226 $main::lxdebug->leave_sub();
1228 return $template_code;
1231 sub get_printer_code {
1232 $main::lxdebug->enter_sub();
1234 my ($self, $myconfig) = @_;
1236 my $template_code = "";
1238 if ($self->{printer_id}) {
1239 my $dbh = $self->dbconnect($myconfig);
1240 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1241 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1245 $main::lxdebug->leave_sub();
1247 return $template_code;
1251 $main::lxdebug->enter_sub();
1253 my ($self, $myconfig) = @_;
1255 my $template_code = "";
1257 if ($self->{shipto_id}) {
1258 my $dbh = $self->dbconnect($myconfig);
1259 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1260 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1261 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1265 $main::lxdebug->leave_sub();
1269 $main::lxdebug->enter_sub();
1271 my ($self, $dbh, $id, $module) = @_;
1275 foreach my $item (qw(name department_1 department_2 street zipcode city country
1276 contact phone fax email)) {
1277 if ($self->{"shipto$item"}) {
1278 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1280 push(@values, $self->{"shipto${item}"});
1283 if ($self->{shipto_id}) {
1284 my $query = qq|UPDATE shipto set
1286 shiptodepartment_1 = ?,
1287 shiptodepartment_2 = ?,
1296 WHERE shipto_id = ?|;
1297 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1299 my $query = qq|SELECT * FROM shipto
1300 WHERE shiptoname = ? AND
1301 shiptodepartment_1 = ? AND
1302 shiptodepartment_2 = ? AND
1303 shiptostreet = ? AND
1304 shiptozipcode = ? AND
1306 shiptocountry = ? AND
1307 shiptocontact = ? AND
1311 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1314 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1315 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1316 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1317 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1318 do_query($self, $dbh, $query, $id, @values, $module);
1323 $main::lxdebug->leave_sub();
1327 $main::lxdebug->enter_sub();
1329 my ($self, $dbh) = @_;
1331 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1332 ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
1333 $self->{employee_id} *= 1;
1335 $main::lxdebug->leave_sub();
1339 $main::lxdebug->enter_sub();
1341 my ($self, $myconfig, $salesman_id) = @_;
1343 $main::lxdebug->leave_sub() and return unless $salesman_id;
1345 my $dbh = $self->dbconnect($myconfig);
1348 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1352 my $user = new User($main::memberfile, $login);
1353 map({ $self->{"salesman_$_"} = $user->{$_}; }
1354 qw(address businessnumber co_ustid company duns email fax name
1356 $self->{salesman_login} = $login;
1358 $self->{salesman_name} = $login
1359 if ($self->{salesman_name} eq "");
1361 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1366 $main::lxdebug->leave_sub();
1370 $main::lxdebug->enter_sub();
1372 my ($self, $myconfig) = @_;
1374 my $dbh = $self->dbconnect($myconfig);
1375 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1376 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1379 $main::lxdebug->leave_sub();
1383 $main::lxdebug->enter_sub();
1385 my ($self, $dbh, $id, $key) = @_;
1387 $key = "all_contacts" unless ($key);
1390 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1391 qq|FROM contacts | .
1392 qq|WHERE cp_cv_id = ? | .
1393 qq|ORDER BY lower(cp_name)|;
1395 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1397 $main::lxdebug->leave_sub();
1401 $main::lxdebug->enter_sub();
1403 my ($self, $dbh, $key) = @_;
1405 my ($all, $old_id, $where, @values);
1407 if (ref($key) eq "HASH") {
1410 $key = "ALL_PROJECTS";
1412 foreach my $p (keys(%{$params})) {
1414 $all = $params->{$p};
1415 } elsif ($p eq "old_id") {
1416 $old_id = $params->{$p};
1417 } elsif ($p eq "key") {
1418 $key = $params->{$p};
1424 $where = "WHERE active ";
1426 if (ref($old_id) eq "ARRAY") {
1427 my @ids = grep({ $_ } @{$old_id});
1429 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1430 push(@values, @ids);
1433 $where .= " OR (id = ?) ";
1434 push(@values, $old_id);
1440 qq|SELECT id, projectnumber, description, active | .
1443 qq|ORDER BY lower(projectnumber)|;
1445 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1447 $main::lxdebug->leave_sub();
1451 $main::lxdebug->enter_sub();
1453 my ($self, $dbh, $vc_id, $key) = @_;
1455 $key = "all_shipto" unless ($key);
1457 # get shipping addresses
1459 qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
1461 qq|WHERE trans_id = ?|;
1463 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1465 $main::lxdebug->leave_sub();
1469 $main::lxdebug->enter_sub();
1471 my ($self, $dbh, $key) = @_;
1473 $key = "all_printers" unless ($key);
1475 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1477 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1479 $main::lxdebug->leave_sub();
1483 $main::lxdebug->enter_sub();
1485 my ($self, $dbh, $params) = @_;
1487 $key = $params->{key};
1488 $key = "all_charts" unless ($key);
1490 my $transdate = quote_db_date($params->{transdate});
1493 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1495 qq|LEFT JOIN taxkeys tk ON | .
1496 qq|(tk.id = (SELECT id FROM taxkeys | .
1497 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1498 qq| ORDER BY startdate DESC LIMIT 1)) | .
1499 qq|ORDER BY c.accno|;
1501 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1503 $main::lxdebug->leave_sub();
1506 sub _get_taxcharts {
1507 $main::lxdebug->enter_sub();
1509 my ($self, $dbh, $key) = @_;
1511 $key = "all_taxcharts" unless ($key);
1513 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1515 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1517 $main::lxdebug->leave_sub();
1521 $main::lxdebug->enter_sub();
1523 my ($self, $dbh, $key) = @_;
1525 $key = "all_taxzones" unless ($key);
1527 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1529 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1531 $main::lxdebug->leave_sub();
1534 sub _get_employees {
1535 $main::lxdebug->enter_sub();
1537 my ($self, $dbh, $key) = @_;
1539 $key = "all_employees" unless ($key);
1541 selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
1543 $main::lxdebug->leave_sub();
1546 sub _get_business_types {
1547 $main::lxdebug->enter_sub();
1549 my ($self, $dbh, $key) = @_;
1551 $key = "all_business_types" unless ($key);
1553 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1555 $main::lxdebug->leave_sub();
1558 sub _get_languages {
1559 $main::lxdebug->enter_sub();
1561 my ($self, $dbh, $key) = @_;
1563 $key = "all_languages" unless ($key);
1565 my $query = qq|SELECT * FROM language ORDER BY id|;
1567 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1569 $main::lxdebug->leave_sub();
1573 $main::lxdebug->enter_sub();
1578 my $dbh = $self->dbconnect(\%main::myconfig);
1579 my ($sth, $query, $ref);
1581 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1582 my $vc_id = $self->{"${vc}_id"};
1584 if ($params{"contacts"}) {
1585 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1588 if ($params{"shipto"}) {
1589 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1592 if ($params{"projects"} || $params{"all_projects"}) {
1593 $self->_get_projects($dbh, $params{"all_projects"} ?
1594 $params{"all_projects"} : $params{"projects"},
1595 $params{"all_projects"} ? 1 : 0);
1598 if ($params{"printers"}) {
1599 $self->_get_printers($dbh, $params{"printers"});
1602 if ($params{"languages"}) {
1603 $self->_get_languages($dbh, $params{"languages"});
1606 if ($params{"charts"}) {
1607 $self->_get_charts($dbh, $params{"charts"});
1610 if ($params{"taxcharts"}) {
1611 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1614 if ($params{"taxzones"}) {
1615 $self->_get_taxzones($dbh, $params{"taxzones"});
1618 if ($params{"employees"}) {
1619 $self->_get_employees($dbh, $params{"employees"});
1622 if ($params{"business_types"}) {
1623 $self->_get_business_types($dbh, $params{"business_types"});
1628 $main::lxdebug->leave_sub();
1631 # this sub gets the id and name from $table
1633 $main::lxdebug->enter_sub();
1635 my ($self, $myconfig, $table) = @_;
1637 # connect to database
1638 my $dbh = $self->dbconnect($myconfig);
1640 $table = $table eq "customer" ? "customer" : "vendor";
1641 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1643 my ($query, @values);
1645 if (!$self->{openinvoices}) {
1647 if ($self->{customernumber} ne "") {
1648 $where = qq|(vc.customernumber ILIKE ?)|;
1649 push(@values, '%' . $self->{customernumber} . '%');
1651 $where = qq|(vc.name ILIKE ?)|;
1652 push(@values, '%' . $self->{$table} . '%');
1656 qq~SELECT vc.id, vc.name,
1657 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1659 WHERE $where AND (NOT vc.obsolete)
1663 qq~SELECT DISTINCT vc.id, vc.name,
1664 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1666 JOIN $table vc ON (a.${table}_id = vc.id)
1667 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1669 push(@values, '%' . $self->{$table} . '%');
1672 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1674 $main::lxdebug->leave_sub();
1676 return scalar(@{ $self->{name_list} });
1679 # the selection sub is used in the AR, AP, IS, IR and OE module
1682 $main::lxdebug->enter_sub();
1684 my ($self, $myconfig, $table, $module) = @_;
1687 my $dbh = $self->dbconnect($myconfig);
1689 $table = $table eq "customer" ? "customer" : "vendor";
1691 my $query = qq|SELECT count(*) FROM $table|;
1692 my ($count) = selectrow_query($self, $dbh, $query);
1694 # build selection list
1695 if ($count < $myconfig->{vclimit}) {
1696 $query = qq|SELECT id, name, salesman_id
1697 FROM $table WHERE NOT obsolete
1699 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1703 $self->get_employee($dbh);
1705 # setup sales contacts
1706 $query = qq|SELECT e.id, e.name
1708 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1709 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1712 push(@{ $self->{all_employees} },
1713 { id => $self->{employee_id},
1714 name => $self->{employee} });
1716 # sort the whole thing
1717 @{ $self->{all_employees} } =
1718 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1720 if ($module eq 'AR') {
1722 # prepare query for departments
1723 $query = qq|SELECT id, description
1726 ORDER BY description|;
1729 $query = qq|SELECT id, description
1731 ORDER BY description|;
1734 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1737 $query = qq|SELECT id, description
1741 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1744 $query = qq|SELECT printer_description, id
1746 ORDER BY printer_description|;
1748 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1751 $query = qq|SELECT id, description
1755 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1759 $main::lxdebug->leave_sub();
1762 sub language_payment {
1763 $main::lxdebug->enter_sub();
1765 my ($self, $myconfig) = @_;
1767 my $dbh = $self->dbconnect($myconfig);
1769 my $query = qq|SELECT id, description
1773 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1776 $query = qq|SELECT printer_description, id
1778 ORDER BY printer_description|;
1780 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1783 $query = qq|SELECT id, description
1787 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1789 # get buchungsgruppen
1790 $query = qq|SELECT id, description
1791 FROM buchungsgruppen|;
1793 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1796 $main::lxdebug->leave_sub();
1799 # this is only used for reports
1800 sub all_departments {
1801 $main::lxdebug->enter_sub();
1803 my ($self, $myconfig, $table) = @_;
1805 my $dbh = $self->dbconnect($myconfig);
1808 if ($table eq 'customer') {
1809 $where = "WHERE role = 'P' ";
1812 my $query = qq|SELECT id, description
1815 ORDER BY description|;
1816 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1818 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1822 $main::lxdebug->leave_sub();
1826 $main::lxdebug->enter_sub();
1828 my ($self, $module, $myconfig, $table) = @_;
1831 if ($table eq "customer") {
1840 $self->all_vc($myconfig, $table, $module);
1842 # get last customers or vendors
1843 my ($query, $sth, $ref);
1845 my $dbh = $self->dbconnect($myconfig);
1850 my $transdate = "current_date";
1851 if ($self->{transdate}) {
1852 $transdate = $dbh->quote($self->{transdate});
1855 # now get the account numbers
1856 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1857 FROM chart c, taxkeys tk
1858 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1859 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1862 $sth = $dbh->prepare($query);
1864 do_statement($self, $sth, $query, '%' . $module . '%');
1866 $self->{accounts} = "";
1867 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1869 foreach my $key (split(/:/, $ref->{link})) {
1870 if ($key =~ /$module/) {
1872 # cross reference for keys
1873 $xkeyref{ $ref->{accno} } = $key;
1875 push @{ $self->{"${module}_links"}{$key} },
1876 { accno => $ref->{accno},
1877 description => $ref->{description},
1878 taxkey => $ref->{taxkey_id},
1879 tax_id => $ref->{tax_id} };
1881 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1887 # get taxkeys and description
1888 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
1889 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
1891 if (($module eq "AP") || ($module eq "AR")) {
1892 # get tax rates and description
1893 $query = qq|SELECT * FROM tax|;
1894 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
1900 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
1901 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
1902 a.intnotes, a.department_id, a.amount AS oldinvtotal,
1903 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
1905 d.description AS department,
1908 JOIN $table c ON (a.${table}_id = c.id)
1909 LEFT JOIN employee e ON (e.id = a.employee_id)
1910 LEFT JOIN department d ON (d.id = a.department_id)
1912 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
1914 foreach $key (keys %$ref) {
1915 $self->{$key} = $ref->{$key};
1918 my $transdate = "current_date";
1919 if ($self->{transdate}) {
1920 $transdate = $dbh->quote($self->{transdate});
1923 # now get the account numbers
1924 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1925 FROM chart c, taxkeys tk
1927 AND ( tk.chart_id = c.id OR c.link LIKE '%_tax%')
1928 AND (NOT tk.chart_id = c.id OR NOT c.link LIKE '%_tax%')
1929 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
1930 OR c.link LIKE '%_tax%')
1933 $sth = $dbh->prepare($query);
1934 do_statement($self, $sth, $query, "%$module%");
1936 $self->{accounts} = "";
1937 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1939 foreach my $key (split(/:/, $ref->{link})) {
1940 if ($key =~ /$module/) {
1942 # cross reference for keys
1943 $xkeyref{ $ref->{accno} } = $key;
1945 push @{ $self->{"${module}_links"}{$key} },
1946 { accno => $ref->{accno},
1947 description => $ref->{description},
1948 taxkey => $ref->{taxkey_id},
1949 tax_id => $ref->{tax_id} };
1951 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1957 # get amounts from individual entries
1960 c.accno, c.description,
1961 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
1965 LEFT JOIN chart c ON (c.id = a.chart_id)
1966 LEFT JOIN project p ON (p.id = a.project_id)
1967 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
1968 WHERE (tk.taxkey_id=a.taxkey) AND
1969 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
1970 THEN tk.chart_id = a.chart_id
1973 OR (c.link='%tax%')) AND
1974 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
1975 WHERE a.trans_id = ?
1976 AND a.fx_transaction = '0'
1977 ORDER BY a.oid, a.transdate|;
1978 $sth = $dbh->prepare($query);
1979 do_statement($self, $sth, $query, $self->{id});
1981 # get exchangerate for currency
1982 $self->{exchangerate} =
1983 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
1986 # store amounts in {acc_trans}{$key} for multiple accounts
1987 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1988 $ref->{exchangerate} =
1989 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
1990 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1993 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1994 $ref->{amount} *= -1;
1996 $ref->{index} = $index;
1998 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2004 d.curr AS currencies, d.closedto, d.revtrans,
2005 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2006 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2008 $ref = selectfirst_hashref_query($self, $dbh, $query);
2009 map { $self->{$_} = $ref->{$_} } keys %$ref;
2016 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2017 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2018 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2020 $ref = selectfirst_hashref_query($self, $dbh, $query);
2021 map { $self->{$_} = $ref->{$_} } keys %$ref;
2023 if ($self->{"$self->{vc}_id"}) {
2025 # only setup currency
2026 ($self->{currency}) = split(/:/, $self->{currencies});
2030 $self->lastname_used($dbh, $myconfig, $table, $module);
2032 # get exchangerate for currency
2033 $self->{exchangerate} =
2034 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2042 $main::lxdebug->leave_sub();
2046 $main::lxdebug->enter_sub();
2048 my ($self, $dbh, $myconfig, $table, $module) = @_;
2050 my $arap = ($table eq 'customer') ? "ar" : "ap";
2051 $table = $table eq "customer" ? "customer" : "vendor";
2052 my $where = "1 = 1";
2054 if ($self->{type} =~ /_order/) {
2056 $where = "quotation = '0'";
2058 if ($self->{type} =~ /_quotation/) {
2060 $where = "quotation = '1'";
2063 my $query = qq|SELECT MAX(id) FROM $arap
2064 WHERE $where AND ${table}_id > 0|;
2065 my ($trans_id) = selectrow_query($self, $dbh, $query);
2070 a.curr, a.${table}_id, a.department_id,
2071 d.description AS department,
2072 ct.name, current_date + ct.terms AS duedate
2074 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2075 LEFT JOIN department d ON (a.department_id = d.id)
2077 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2078 $self->{department}, $self->{$table}, $self->{duedate})
2079 = selectrow_query($self, $dbh, $query, $trans_id);
2081 $main::lxdebug->leave_sub();
2085 $main::lxdebug->enter_sub();
2087 my ($self, $myconfig, $thisdate, $days) = @_;
2089 my $dbh = $self->dbconnect($myconfig);
2094 my $dateformat = $myconfig->{dateformat};
2095 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2096 $thisdate = $dbh->quote($thisdate);
2097 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2099 $query = qq|SELECT current_date AS thisdate|;
2102 ($thisdate) = selectrow_query($self, $dbh, $query);
2106 $main::lxdebug->leave_sub();
2112 $main::lxdebug->enter_sub();
2114 my ($self, $string) = @_;
2116 if ($string !~ /%/) {
2117 $string = "%$string%";
2120 $string =~ s/\'/\'\'/g;
2122 $main::lxdebug->leave_sub();
2128 $main::lxdebug->enter_sub();
2130 my ($self, $flds, $new, $count, $numrows) = @_;
2134 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2140 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2142 $j = $item->{ndx} - 1;
2143 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2147 for $i ($count + 1 .. $numrows) {
2148 map { delete $self->{"${_}_$i"} } @{$flds};
2151 $main::lxdebug->leave_sub();
2155 $main::lxdebug->enter_sub();
2157 my ($self, $myconfig) = @_;
2161 my $dbh = $self->dbconnect_noauto($myconfig);
2163 my $query = qq|DELETE FROM status
2164 WHERE (formname = ?) AND (trans_id = ?)|;
2165 my $sth = prepare_query($self, $dbh, $query);
2167 if ($self->{formname} =~ /(check|receipt)/) {
2168 for $i (1 .. $self->{rowcount}) {
2169 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2172 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2176 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2177 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2179 my %queued = split / /, $self->{queued};
2182 if ($self->{formname} =~ /(check|receipt)/) {
2184 # this is a check or receipt, add one entry for each lineitem
2185 my ($accno) = split /--/, $self->{account};
2186 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2187 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2188 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2189 $sth = prepare_query($self, $dbh, $query);
2191 for $i (1 .. $self->{rowcount}) {
2192 if ($self->{"checked_$i"}) {
2193 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2199 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2200 VALUES (?, ?, ?, ?, ?)|;
2201 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2202 $queued{$self->{formname}}, $self->{formname});
2208 $main::lxdebug->leave_sub();
2212 $main::lxdebug->enter_sub();
2214 my ($self, $dbh) = @_;
2216 my ($query, $printed, $emailed);
2218 my $formnames = $self->{printed};
2219 my $emailforms = $self->{emailed};
2221 my $query = qq|DELETE FROM status
2222 WHERE (formname = ?) AND (trans_id = ?)|;
2223 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2225 # this only applies to the forms
2226 # checks and receipts are posted when printed or queued
2228 if ($self->{queued}) {
2229 my %queued = split / /, $self->{queued};
2231 foreach my $formname (keys %queued) {
2232 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2233 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2235 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2236 VALUES (?, ?, ?, ?, ?)|;
2237 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2239 $formnames =~ s/$self->{formname}//;
2240 $emailforms =~ s/$self->{formname}//;
2245 # save printed, emailed info
2246 $formnames =~ s/^ +//g;
2247 $emailforms =~ s/^ +//g;
2250 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2251 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2253 foreach my $formname (keys %status) {
2254 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2255 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2257 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2258 VALUES (?, ?, ?, ?)|;
2259 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2262 $main::lxdebug->leave_sub();
2266 # $main::locale->text('SAVED')
2267 # $main::locale->text('DELETED')
2268 # $main::locale->text('ADDED')
2269 # $main::locale->text('PAYMENT POSTED')
2270 # $main::locale->text('POSTED')
2271 # $main::locale->text('POSTED AS NEW')
2272 # $main::locale->text('ELSE')
2273 # $main::locale->text('SAVED FOR DUNNING')
2274 # $main::locale->text('DUNNING STARTED')
2275 # $main::locale->text('PRINTED')
2276 # $main::locale->text('MAILED')
2277 # $main::locale->text('SCREENED')
2278 # $main::locale->text('invoice')
2279 # $main::locale->text('proforma')
2280 # $main::locale->text('sales_order')
2281 # $main::locale->text('packing_list')
2282 # $main::locale->text('pick_list')
2283 # $main::locale->text('purchase_order')
2284 # $main::locale->text('bin_list')
2285 # $main::locale->text('sales_quotation')
2286 # $main::locale->text('request_quotation')
2289 $main::lxdebug->enter_sub();
2294 if(!exists $self->{employee_id}) {
2295 &get_employee($self, $dbh);
2299 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2300 qq|VALUES (?, ?, ?, ?, ?)|;
2301 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2302 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2303 do_query($self, $dbh, $query, @values);
2305 $main::lxdebug->leave_sub();
2309 $main::lxdebug->enter_sub();
2313 my $trans_id = shift();
2314 my $restriction = shift();
2317 if ($trans_id ne "") {
2319 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 | .
2320 qq|FROM history_erp h | .
2321 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2322 qq|WHERE trans_id = ? |
2325 my $sth = $dbh->prepare($query) || $self->dberror($query);
2327 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2329 while(my $hash_ref = $sth->fetchrow_hashref()) {
2330 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2331 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2332 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2333 $tempArray[$i++] = $hash_ref;
2335 $main::lxdebug->leave_sub() and return \@tempArray
2336 if ($i > 0 && $tempArray[0] ne "");
2338 $main::lxdebug->leave_sub();
2342 sub update_defaults {
2343 $main::lxdebug->enter_sub();
2345 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2348 if ($provided_dbh) {
2349 $dbh = $provided_dbh;
2351 $dbh = $self->dbconnect_noauto($myconfig);
2353 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2354 my $sth = $dbh->prepare($query);
2356 $sth->execute || $self->dberror($query);
2357 my ($var) = $sth->fetchrow_array;
2360 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2363 $query = qq|UPDATE defaults SET $fld = ?|;
2364 do_query($self, $dbh, $query, $var);
2366 if (!$provided_dbh) {
2371 $main::lxdebug->leave_sub();
2376 sub update_business {
2377 $main::lxdebug->enter_sub();
2379 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2382 if ($provided_dbh) {
2383 $dbh = $provided_dbh;
2385 $dbh = $self->dbconnect_noauto($myconfig);
2388 qq|SELECT customernumberinit FROM business
2389 WHERE id = ? FOR UPDATE|;
2390 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2392 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2394 $query = qq|UPDATE business
2395 SET customernumberinit = ?
2397 do_query($self, $dbh, $query, $var, $business_id);
2399 if (!$provided_dbh) {
2404 $main::lxdebug->leave_sub();
2409 sub get_partsgroup {
2410 $main::lxdebug->enter_sub();
2412 my ($self, $myconfig, $p) = @_;
2414 my $dbh = $self->dbconnect($myconfig);
2416 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2418 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2421 if ($p->{searchitems} eq 'part') {
2422 $query .= qq|WHERE p.inventory_accno_id > 0|;
2424 if ($p->{searchitems} eq 'service') {
2425 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2427 if ($p->{searchitems} eq 'assembly') {
2428 $query .= qq|WHERE p.assembly = '1'|;
2430 if ($p->{searchitems} eq 'labor') {
2431 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2434 $query .= qq|ORDER BY partsgroup|;
2437 $query = qq|SELECT id, partsgroup FROM partsgroup
2438 ORDER BY partsgroup|;
2441 if ($p->{language_code}) {
2442 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2443 t.description AS translation
2445 JOIN parts p ON (p.partsgroup_id = pg.id)
2446 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2447 ORDER BY translation|;
2448 @values = ($p->{language_code});
2451 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2454 $main::lxdebug->leave_sub();
2457 sub get_pricegroup {
2458 $main::lxdebug->enter_sub();
2460 my ($self, $myconfig, $p) = @_;
2462 my $dbh = $self->dbconnect($myconfig);
2464 my $query = qq|SELECT p.id, p.pricegroup
2467 $query .= qq| ORDER BY pricegroup|;
2470 $query = qq|SELECT id, pricegroup FROM pricegroup
2471 ORDER BY pricegroup|;
2474 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2478 $main::lxdebug->leave_sub();
2482 # usage $form->all_years($myconfig, [$dbh])
2483 # return list of all years where bookings found
2486 $main::lxdebug->enter_sub();
2488 my ($self, $myconfig, $dbh) = @_;
2492 $dbh = $self->dbconnect($myconfig);
2497 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2498 (SELECT MAX(transdate) FROM acc_trans)|;
2499 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2501 if ($myconfig->{dateformat} =~ /^yy/) {
2502 ($startdate) = split /\W/, $startdate;
2503 ($enddate) = split /\W/, $enddate;
2505 (@_) = split /\W/, $startdate;
2507 (@_) = split /\W/, $enddate;
2512 $startdate = substr($startdate,0,4);
2513 $enddate = substr($enddate,0,4);
2515 while ($enddate >= $startdate) {
2516 push @all_years, $enddate--;
2519 $dbh->disconnect if $disconnect;
2523 $main::lxdebug->leave_sub();