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 = "Developper 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();
1520 sub _get_employees {
1521 $main::lxdebug->enter_sub();
1523 my ($self, $dbh, $key) = @_;
1525 $key = "all_employees" unless ($key);
1527 selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
1529 $main::lxdebug->leave_sub();
1532 sub _get_business_types {
1533 $main::lxdebug->enter_sub();
1535 my ($self, $dbh, $key) = @_;
1537 $key = "all_business_types" unless ($key);
1539 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1541 $main::lxdebug->leave_sub();
1544 sub _get_languages {
1545 $main::lxdebug->enter_sub();
1547 my ($self, $dbh, $key) = @_;
1549 $key = "all_languages" unless ($key);
1551 my $query = qq|SELECT * FROM language ORDER BY id|;
1553 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1555 $main::lxdebug->leave_sub();
1559 $main::lxdebug->enter_sub();
1564 my $dbh = $self->dbconnect(\%main::myconfig);
1565 my ($sth, $query, $ref);
1567 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1568 my $vc_id = $self->{"${vc}_id"};
1570 if ($params{"contacts"}) {
1571 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1574 if ($params{"shipto"}) {
1575 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1578 if ($params{"projects"} || $params{"all_projects"}) {
1579 $self->_get_projects($dbh, $params{"all_projects"} ?
1580 $params{"all_projects"} : $params{"projects"},
1581 $params{"all_projects"} ? 1 : 0);
1584 if ($params{"printers"}) {
1585 $self->_get_printers($dbh, $params{"printers"});
1588 if ($params{"languages"}) {
1589 $self->_get_languages($dbh, $params{"languages"});
1592 if ($params{"charts"}) {
1593 $self->_get_charts($dbh, $params{"charts"});
1596 if ($params{"taxcharts"}) {
1597 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1600 if ($params{"employees"}) {
1601 $self->_get_employees($dbh, $params{"employees"});
1604 if ($params{"business_types"}) {
1605 $self->_get_business_types($dbh, $params{"business_types"});
1610 $main::lxdebug->leave_sub();
1613 # this sub gets the id and name from $table
1615 $main::lxdebug->enter_sub();
1617 my ($self, $myconfig, $table) = @_;
1619 # connect to database
1620 my $dbh = $self->dbconnect($myconfig);
1622 $table = $table eq "customer" ? "customer" : "vendor";
1623 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1625 my ($query, @values);
1627 if (!$self->{openinvoices}) {
1629 if ($self->{customernumber} ne "") {
1630 $where = qq|(vc.customernumber ILIKE ?)|;
1631 push(@values, '%' . $self->{customernumber} . '%');
1633 $where = qq|(vc.name ILIKE ?)|;
1634 push(@values, '%' . $self->{$table} . '%');
1638 qq~SELECT vc.id, vc.name,
1639 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1641 WHERE $where AND (NOT vc.obsolete)
1645 qq~SELECT DISTINCT vc.id, vc.name,
1646 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1648 JOIN $table vc ON (a.${table}_id = vc.id)
1649 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1651 push(@values, '%' . $self->{$table} . '%');
1654 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1656 $main::lxdebug->leave_sub();
1658 return scalar(@{ $self->{name_list} });
1661 # the selection sub is used in the AR, AP, IS, IR and OE module
1664 $main::lxdebug->enter_sub();
1666 my ($self, $myconfig, $table, $module) = @_;
1669 my $dbh = $self->dbconnect($myconfig);
1671 $table = $table eq "customer" ? "customer" : "vendor";
1673 my $query = qq|SELECT count(*) FROM $table|;
1674 my ($count) = selectrow_query($self, $dbh, $query);
1676 # build selection list
1677 if ($count < $myconfig->{vclimit}) {
1678 $query = qq|SELECT id, name, salesman_id
1679 FROM $table WHERE NOT obsolete
1681 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1685 $self->get_employee($dbh);
1687 # setup sales contacts
1688 $query = qq|SELECT e.id, e.name
1690 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1691 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1694 push(@{ $self->{all_employees} },
1695 { id => $self->{employee_id},
1696 name => $self->{employee} });
1698 # sort the whole thing
1699 @{ $self->{all_employees} } =
1700 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1702 if ($module eq 'AR') {
1704 # prepare query for departments
1705 $query = qq|SELECT id, description
1708 ORDER BY description|;
1711 $query = qq|SELECT id, description
1713 ORDER BY description|;
1716 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1719 $query = qq|SELECT id, description
1723 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1726 $query = qq|SELECT printer_description, id
1728 ORDER BY printer_description|;
1730 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1733 $query = qq|SELECT id, description
1737 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1741 $main::lxdebug->leave_sub();
1744 sub language_payment {
1745 $main::lxdebug->enter_sub();
1747 my ($self, $myconfig) = @_;
1749 my $dbh = $self->dbconnect($myconfig);
1751 my $query = qq|SELECT id, description
1755 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1758 $query = qq|SELECT printer_description, id
1760 ORDER BY printer_description|;
1762 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1765 $query = qq|SELECT id, description
1769 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1771 # get buchungsgruppen
1772 $query = qq|SELECT id, description
1773 FROM buchungsgruppen|;
1775 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1778 $main::lxdebug->leave_sub();
1781 # this is only used for reports
1782 sub all_departments {
1783 $main::lxdebug->enter_sub();
1785 my ($self, $myconfig, $table) = @_;
1787 my $dbh = $self->dbconnect($myconfig);
1790 if ($table eq 'customer') {
1791 $where = "WHERE role = 'P' ";
1794 my $query = qq|SELECT id, description
1797 ORDER BY description|;
1798 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1800 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1804 $main::lxdebug->leave_sub();
1808 $main::lxdebug->enter_sub();
1810 my ($self, $module, $myconfig, $table) = @_;
1813 if ($table eq "customer") {
1822 $self->all_vc($myconfig, $table, $module);
1824 # get last customers or vendors
1825 my ($query, $sth, $ref);
1827 my $dbh = $self->dbconnect($myconfig);
1832 my $transdate = "current_date";
1833 if ($self->{transdate}) {
1834 $transdate = $dbh->quote($self->{transdate});
1837 # now get the account numbers
1838 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1839 FROM chart c, taxkeys tk
1840 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1841 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1844 $sth = $dbh->prepare($query);
1846 do_statement($self, $sth, $query, '%' . $module . '%');
1848 $self->{accounts} = "";
1849 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1851 foreach my $key (split(/:/, $ref->{link})) {
1852 if ($key =~ /$module/) {
1854 # cross reference for keys
1855 $xkeyref{ $ref->{accno} } = $key;
1857 push @{ $self->{"${module}_links"}{$key} },
1858 { accno => $ref->{accno},
1859 description => $ref->{description},
1860 taxkey => $ref->{taxkey_id},
1861 tax_id => $ref->{tax_id} };
1863 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1869 # get taxkeys and description
1870 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
1871 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
1874 $query = qq|SELECT id, description FROM tax_zones|;
1875 $self->{TAXZONE} = selectall_hashref_query($self, $dbh, $query);
1877 if (($module eq "AP") || ($module eq "AR")) {
1878 # get tax rates and description
1879 $query = qq|SELECT * FROM tax|;
1880 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
1886 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
1887 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
1888 a.intnotes, a.department_id, a.amount AS oldinvtotal,
1889 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
1891 d.description AS department,
1894 JOIN $table c ON (a.${table}_id = c.id)
1895 LEFT JOIN employee e ON (e.id = a.employee_id)
1896 LEFT JOIN department d ON (d.id = a.department_id)
1898 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
1900 foreach $key (keys %$ref) {
1901 $self->{$key} = $ref->{$key};
1904 my $transdate = "current_date";
1905 if ($self->{transdate}) {
1906 $transdate = $dbh->quote($self->{transdate});
1909 # now get the account numbers
1910 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1911 FROM chart c, taxkeys tk
1913 AND ( tk.chart_id = c.id OR c.link LIKE '%_tax%')
1914 AND (NOT tk.chart_id = c.id OR NOT c.link LIKE '%_tax%')
1915 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
1916 OR c.link LIKE '%_tax%')
1919 $sth = $dbh->prepare($query);
1920 do_statement($self, $sth, $query, "%$module%");
1922 $self->{accounts} = "";
1923 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1925 foreach my $key (split(/:/, $ref->{link})) {
1926 if ($key =~ /$module/) {
1928 # cross reference for keys
1929 $xkeyref{ $ref->{accno} } = $key;
1931 push @{ $self->{"${module}_links"}{$key} },
1932 { accno => $ref->{accno},
1933 description => $ref->{description},
1934 taxkey => $ref->{taxkey_id},
1935 tax_id => $ref->{tax_id} };
1937 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1943 # get amounts from individual entries
1946 c.accno, c.description,
1947 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
1951 LEFT JOIN chart c ON (c.id = a.chart_id)
1952 LEFT JOIN project p ON (p.id = a.project_id)
1953 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
1954 WHERE (tk.taxkey_id=a.taxkey) AND
1955 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
1956 THEN tk.chart_id = a.chart_id
1959 OR (c.link='%tax%')) AND
1960 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
1961 WHERE a.trans_id = ?
1962 AND a.fx_transaction = '0'
1963 ORDER BY a.oid, a.transdate|;
1964 $sth = $dbh->prepare($query);
1965 do_statement($self, $sth, $query, $self->{id});
1967 # get exchangerate for currency
1968 $self->{exchangerate} =
1969 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
1972 # store amounts in {acc_trans}{$key} for multiple accounts
1973 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1974 $ref->{exchangerate} =
1975 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
1976 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1979 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1980 $ref->{amount} *= -1;
1982 $ref->{index} = $index;
1984 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1990 d.curr AS currencies, d.closedto, d.revtrans,
1991 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1992 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1994 $ref = selectfirst_hashref_query($self, $dbh, $query);
1995 map { $self->{$_} = $ref->{$_} } keys %$ref;
2002 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2003 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2004 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2006 $ref = selectfirst_hashref_query($self, $dbh, $query);
2007 map { $self->{$_} = $ref->{$_} } keys %$ref;
2009 if ($self->{"$self->{vc}_id"}) {
2011 # only setup currency
2012 ($self->{currency}) = split(/:/, $self->{currencies});
2016 $self->lastname_used($dbh, $myconfig, $table, $module);
2018 # get exchangerate for currency
2019 $self->{exchangerate} =
2020 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2028 $main::lxdebug->leave_sub();
2032 $main::lxdebug->enter_sub();
2034 my ($self, $dbh, $myconfig, $table, $module) = @_;
2036 my $arap = ($table eq 'customer') ? "ar" : "ap";
2037 $table = $table eq "customer" ? "customer" : "vendor";
2038 my $where = "1 = 1";
2040 if ($self->{type} =~ /_order/) {
2042 $where = "quotation = '0'";
2044 if ($self->{type} =~ /_quotation/) {
2046 $where = "quotation = '1'";
2049 my $query = qq|SELECT MAX(id) FROM $arap
2050 WHERE $where AND ${table}_id > 0|;
2051 my ($trans_id) = selectrow_query($self, $dbh, $query);
2056 a.curr, a.${table}_id, a.department_id,
2057 d.description AS department,
2058 ct.name, current_date + ct.terms AS duedate
2060 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2061 LEFT JOIN department d ON (a.department_id = d.id)
2063 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2064 $self->{department}, $self->{$table}, $self->{duedate})
2065 = selectrow_query($self, $dbh, $query, $trans_id);
2067 $main::lxdebug->leave_sub();
2071 $main::lxdebug->enter_sub();
2073 my ($self, $myconfig, $thisdate, $days) = @_;
2075 my $dbh = $self->dbconnect($myconfig);
2080 my $dateformat = $myconfig->{dateformat};
2081 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2082 $thisdate = $dbh->quote($thisdate);
2083 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2085 $query = qq|SELECT current_date AS thisdate|;
2088 ($thisdate) = selectrow_query($self, $dbh, $query);
2092 $main::lxdebug->leave_sub();
2098 $main::lxdebug->enter_sub();
2100 my ($self, $string) = @_;
2102 if ($string !~ /%/) {
2103 $string = "%$string%";
2106 $string =~ s/\'/\'\'/g;
2108 $main::lxdebug->leave_sub();
2114 $main::lxdebug->enter_sub();
2116 my ($self, $flds, $new, $count, $numrows) = @_;
2120 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2126 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2128 $j = $item->{ndx} - 1;
2129 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2133 for $i ($count + 1 .. $numrows) {
2134 map { delete $self->{"${_}_$i"} } @{$flds};
2137 $main::lxdebug->leave_sub();
2141 $main::lxdebug->enter_sub();
2143 my ($self, $myconfig) = @_;
2147 my $dbh = $self->dbconnect_noauto($myconfig);
2149 my $query = qq|DELETE FROM status
2150 WHERE (formname = ?) AND (trans_id = ?)|;
2151 my $sth = prepare_query($self, $dbh, $query);
2153 if ($self->{formname} =~ /(check|receipt)/) {
2154 for $i (1 .. $self->{rowcount}) {
2155 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2158 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2162 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2163 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2165 my %queued = split / /, $self->{queued};
2168 if ($self->{formname} =~ /(check|receipt)/) {
2170 # this is a check or receipt, add one entry for each lineitem
2171 my ($accno) = split /--/, $self->{account};
2172 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2173 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2174 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2175 $sth = prepare_query($self, $dbh, $query);
2177 for $i (1 .. $self->{rowcount}) {
2178 if ($self->{"checked_$i"}) {
2179 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2185 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2186 VALUES (?, ?, ?, ?, ?)|;
2187 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2188 $queued{$self->{formname}}, $self->{formname});
2194 $main::lxdebug->leave_sub();
2198 $main::lxdebug->enter_sub();
2200 my ($self, $dbh) = @_;
2202 my ($query, $printed, $emailed);
2204 my $formnames = $self->{printed};
2205 my $emailforms = $self->{emailed};
2207 my $query = qq|DELETE FROM status
2208 WHERE (formname = ?) AND (trans_id = ?)|;
2209 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2211 # this only applies to the forms
2212 # checks and receipts are posted when printed or queued
2214 if ($self->{queued}) {
2215 my %queued = split / /, $self->{queued};
2217 foreach my $formname (keys %queued) {
2218 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2219 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2221 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2222 VALUES (?, ?, ?, ?, ?)|;
2223 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2225 $formnames =~ s/$self->{formname}//;
2226 $emailforms =~ s/$self->{formname}//;
2231 # save printed, emailed info
2232 $formnames =~ s/^ +//g;
2233 $emailforms =~ s/^ +//g;
2236 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2237 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2239 foreach my $formname (keys %status) {
2240 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2241 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2243 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2244 VALUES (?, ?, ?, ?)|;
2245 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2248 $main::lxdebug->leave_sub();
2252 # $main::locale->text('SAVED')
2253 # $main::locale->text('DELETED')
2254 # $main::locale->text('ADDED')
2255 # $main::locale->text('PAYMENT POSTED')
2256 # $main::locale->text('POSTED')
2257 # $main::locale->text('POSTED AS NEW')
2258 # $main::locale->text('ELSE')
2259 # $main::locale->text('SAVED FOR DUNNING')
2260 # $main::locale->text('DUNNING STARTED')
2261 # $main::locale->text('PRINTED')
2262 # $main::locale->text('MAILED')
2263 # $main::locale->text('SCREENED')
2264 # $main::locale->text('invoice')
2265 # $main::locale->text('proforma')
2266 # $main::locale->text('sales_order')
2267 # $main::locale->text('packing_list')
2268 # $main::locale->text('pick_list')
2269 # $main::locale->text('purchase_order')
2270 # $main::locale->text('bin_list')
2271 # $main::locale->text('sales_quotation')
2272 # $main::locale->text('request_quotation')
2275 $main::lxdebug->enter_sub();
2280 if(!exists $self->{employee_id}) {
2281 &get_employee($self, $dbh);
2285 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2286 qq|VALUES (?, ?, ?, ?, ?)|;
2287 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2288 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2289 do_query($self, $dbh, $query, @values);
2291 $main::lxdebug->leave_sub();
2295 $main::lxdebug->enter_sub();
2299 my $trans_id = shift();
2300 my $restriction = shift();
2303 if ($trans_id ne "") {
2305 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 | .
2306 qq|FROM history_erp h | .
2307 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2308 qq|WHERE trans_id = ? |
2311 my $sth = $dbh->prepare($query) || $self->dberror($query);
2313 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2315 while(my $hash_ref = $sth->fetchrow_hashref()) {
2316 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2317 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2318 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2319 $tempArray[$i++] = $hash_ref;
2321 return \@tempArray and $main::lxdebug->leave_sub()
2322 if ($i > 0 && $tempArray[0] ne "");
2324 $main::lxdebug->leave_sub();
2328 sub update_defaults {
2329 $main::lxdebug->enter_sub();
2331 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2334 if ($provided_dbh) {
2335 $dbh = $provided_dbh;
2337 $dbh = $self->dbconnect_noauto($myconfig);
2339 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2340 my $sth = $dbh->prepare($query);
2342 $sth->execute || $self->dberror($query);
2343 my ($var) = $sth->fetchrow_array;
2346 if ($var =~ /^(.*?)(\d+)$/) {
2347 $var = "$1" . ($2 + 1);
2352 $query = qq|UPDATE defaults SET $fld = ?|;
2353 do_query($self, $dbh, $query, $var);
2355 if (!$provided_dbh) {
2360 $main::lxdebug->leave_sub();
2365 sub update_business {
2366 $main::lxdebug->enter_sub();
2368 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2371 if ($provided_dbh) {
2372 $dbh = $provided_dbh;
2374 $dbh = $self->dbconnect_noauto($myconfig);
2377 qq|SELECT customernumberinit FROM business
2378 WHERE id = ? FOR UPDATE|;
2379 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2382 if ($var =~ /^(.*?)(\d+)$/) {
2383 $var = "$1" . ($2 + 1);
2388 $query = qq|UPDATE business
2389 SET customernumberinit = ?
2391 do_query($self, $dbh, $query, $var, $business_id);
2393 if (!$provided_dbh) {
2398 $main::lxdebug->leave_sub();
2403 sub get_partsgroup {
2404 $main::lxdebug->enter_sub();
2406 my ($self, $myconfig, $p) = @_;
2408 my $dbh = $self->dbconnect($myconfig);
2410 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2412 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2415 if ($p->{searchitems} eq 'part') {
2416 $query .= qq|WHERE p.inventory_accno_id > 0|;
2418 if ($p->{searchitems} eq 'service') {
2419 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2421 if ($p->{searchitems} eq 'assembly') {
2422 $query .= qq|WHERE p.assembly = '1'|;
2424 if ($p->{searchitems} eq 'labor') {
2425 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2428 $query .= qq|ORDER BY partsgroup|;
2431 $query = qq|SELECT id, partsgroup FROM partsgroup
2432 ORDER BY partsgroup|;
2435 if ($p->{language_code}) {
2436 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2437 t.description AS translation
2439 JOIN parts p ON (p.partsgroup_id = pg.id)
2440 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2441 ORDER BY translation|;
2442 @values = ($p->{language_code});
2445 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2448 $main::lxdebug->leave_sub();
2451 sub get_pricegroup {
2452 $main::lxdebug->enter_sub();
2454 my ($self, $myconfig, $p) = @_;
2456 my $dbh = $self->dbconnect($myconfig);
2458 my $query = qq|SELECT p.id, p.pricegroup
2461 $query .= qq| ORDER BY pricegroup|;
2464 $query = qq|SELECT id, pricegroup FROM pricegroup
2465 ORDER BY pricegroup|;
2468 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2472 $main::lxdebug->leave_sub();
2476 # usage $form->all_years($myconfig, [$dbh])
2477 # return list of all years where bookings found
2480 $main::lxdebug->enter_sub();
2482 my ($self, $myconfig, $dbh) = @_;
2486 $dbh = $self->dbconnect($myconfig);
2491 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2492 (SELECT MAX(transdate) FROM acc_trans)|;
2493 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2495 if ($myconfig->{dateformat} =~ /^yy/) {
2496 ($startdate) = split /\W/, $startdate;
2497 ($enddate) = split /\W/, $enddate;
2499 (@_) = split /\W/, $startdate;
2501 (@_) = split /\W/, $enddate;
2506 $startdate = substr($startdate,0,4);
2507 $enddate = substr($enddate,0,4);
2509 while ($enddate >= $startdate) {
2510 push @all_years, $enddate--;
2513 $dbh->disconnect if $disconnect;
2517 $main::lxdebug->leave_sub();