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; }
378 if ($self->{fokus}) {
379 $fokus = qq|<script type="text/javascript">
381 function fokus(){document.$self->{fokus}.focus();}
388 if ($self->{jsscript} == 1) {
391 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
392 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
393 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
394 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
401 ? "$self->{title} - $self->{titlebar}"
404 foreach $item (@ { $self->{AJAX} }) {
405 $ajax .= $item->show_javascript();
407 print qq|Content-Type: text/html
411 <title>$self->{titlebar}</title>
419 <meta name="robots" content="noindex,nofollow" />
420 <script type="text/javascript" src="js/highlight_input.js"></script>
421 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
423 <script type="text/javascript" src="js/tabcontent.js">
425 /***********************************************
426 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
427 * This notice MUST stay intact for legal use
428 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
429 ***********************************************/
440 $main::lxdebug->leave_sub();
443 sub parse_html_template {
444 $main::lxdebug->enter_sub();
446 my ($self, $file, $additional_params) = @_;
449 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
450 $language = $main::language;
452 $language = $main::myconfig{"countrycode"};
455 if (-f "templates/webpages/${file}_${language}.html") {
456 if ((-f ".developer") &&
457 (-f "templates/webpages/${file}_master.html") &&
458 ((stat("templates/webpages/${file}_master.html"))[9] >
459 (stat("templates/webpages/${file}_${language}.html"))[9])) {
460 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
461 "Please re-run 'locales.pl' in 'locale/${language}'.";
462 print(qq|<pre>$info</pre>|);
466 $file = "templates/webpages/${file}_${language}.html";
467 } elsif (-f "templates/webpages/${file}.html") {
468 $file = "templates/webpages/${file}.html";
470 my $info = "Web page template '${file}' not found.\n" .
471 "Please re-run 'locales.pl' in 'locale/${language}'.";
472 print(qq|<pre>$info</pre>|);
476 my $template = HTML::Template->new("filename" => $file,
477 "die_on_bad_params" => 0,
479 "case_sensitive" => 1,
480 "loop_context_vars" => 1,
483 $additional_params = {} unless ($additional_params);
484 if ($self->{"DEBUG"}) {
485 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
488 if ($additional_params->{"DEBUG"}) {
489 $additional_params->{"DEBUG"} =
490 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
493 if (%main::myconfig) {
494 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
495 my $jsc_dateformat = $main::myconfig{"dateformat"};
496 $jsc_dateformat =~ s/d+/\%d/gi;
497 $jsc_dateformat =~ s/m+/\%m/gi;
498 $jsc_dateformat =~ s/y+/\%Y/gi;
499 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
502 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
503 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
504 $additional_params->{"conf_latex_templates"} = $main::latex;
505 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
507 my @additional_param_names = keys(%{$additional_params});
508 foreach my $key ($template->param()) {
509 my $param = $self->{$key};
510 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
511 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
512 $template->param($key => $param);
515 my $output = $template->output();
517 $main::lxdebug->leave_sub();
522 sub show_generic_error {
523 my ($self, $error, $title, $action) = @_;
526 $add_params->{"title"} = $title if ($title);
527 $self->{"label_error"} = $error;
531 map({ delete($self->{$_}); } qw(action));
532 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
533 if (!ref($self->{$_})); }
535 $add_params->{"SHOW_BUTTON"} = 1;
536 $add_params->{"BUTTON_LABEL"} = $action;
538 $add_params->{"VARIABLES"} = \@vars;
541 print($self->parse_html_template("generic/error", $add_params));
543 die("Error: $error\n");
546 sub show_generic_information {
547 my ($self, $error, $title) = @_;
550 $add_params->{"title"} = $title if ($title);
551 $self->{"label_information"} = $error;
554 print($self->parse_html_template("generic/information", $add_params));
556 die("Information: $error\n");
559 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
560 # changed it to accept an arbitrary number of triggers - sschoeling
562 $main::lxdebug->enter_sub();
565 my $myconfig = shift;
568 # set dateform for jsscript
571 "dd.mm.yy" => "%d.%m.%Y",
572 "dd-mm-yy" => "%d-%m-%Y",
573 "dd/mm/yy" => "%d/%m/%Y",
574 "mm/dd/yy" => "%m/%d/%Y",
575 "mm-dd-yy" => "%m-%d-%Y",
576 "yyyy-mm-dd" => "%Y-%m-%d",
579 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
580 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
587 inputField : "| . (shift) . qq|",
588 ifFormat :"$ifFormat",
589 align : "| . (shift) . qq|",
590 button : "| . (shift) . qq|"
596 <script type="text/javascript">
597 <!--| . join("", @triggers) . qq|//-->
601 $main::lxdebug->leave_sub();
604 } #end sub write_trigger
607 $main::lxdebug->enter_sub();
609 my ($self, $msg) = @_;
611 if ($self->{callback}) {
613 ($script, $argv) = split(/\?/, $self->{callback});
614 exec("perl", "$script", $argv);
622 $main::lxdebug->leave_sub();
625 # sort of columns removed - empty sub
627 $main::lxdebug->enter_sub();
629 my ($self, @columns) = @_;
631 $main::lxdebug->leave_sub();
637 $main::lxdebug->enter_sub(2);
639 my ($self, $myconfig, $amount, $places, $dash) = @_;
644 my $neg = ($amount =~ s/-//);
646 if (defined($places) && ($places ne '')) {
651 my ($actual_places) = ($amount =~ /\.(\d+)/);
652 $actual_places = length($actual_places);
653 $places = $actual_places > $places ? $actual_places : $places;
656 $amount = $self->round_amount($amount, $places);
659 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
660 my @p = split(/\./, $amount); # split amount at decimal point
662 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
665 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
668 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
669 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
670 ($neg ? "-$amount" : "$amount" ) ;
674 $main::lxdebug->leave_sub(2);
679 $main::lxdebug->enter_sub(2);
681 my ($self, $myconfig, $amount) = @_;
683 if ( ($myconfig->{numberformat} eq '1.000,00')
684 || ($myconfig->{numberformat} eq '1000,00')) {
689 if ($myconfig->{numberformat} eq "1'000.00") {
695 $main::lxdebug->leave_sub(2);
697 return ($amount * 1);
701 $main::lxdebug->enter_sub(2);
703 my ($self, $amount, $places) = @_;
706 # Rounding like "Kaufmannsrunden"
707 # Descr. http://de.wikipedia.org/wiki/Rundung
709 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
712 $amount = $amount * (10**($places));
713 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
715 $main::lxdebug->leave_sub(2);
717 return $round_amount;
722 $main::lxdebug->enter_sub();
724 my ($self, $myconfig, $userspath) = @_;
727 $self->{"cwd"} = getcwd();
728 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
730 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
731 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
732 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
733 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
734 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
735 } elsif (($self->{"format"} =~ /html/i) ||
736 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
737 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
738 } elsif (($self->{"format"} =~ /xml/i) ||
739 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
740 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
741 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
742 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
743 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
744 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
745 } elsif ( defined $self->{'format'}) {
746 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
747 } elsif ( $self->{'format'} eq '' ) {
748 $self->error("No Outputformat given: $self->{'format'}");
749 } else { #Catch the rest
750 $self->error("Outputformat not defined: $self->{'format'}");
753 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
754 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
756 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
757 qw(email tel fax name signature company address businessnumber
758 co_ustid taxnumber duns));
759 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
760 qw(company address signature));
761 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
763 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
765 # OUT is used for the media, screen, printer, email
766 # for postscript we store a copy in a temporary file
768 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
769 if ($template->uses_temp_file() || $self->{media} eq 'email') {
771 $self->{OUT} = ">$self->{tmpfile}";
775 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
777 open(OUT, ">-") or $self->error("STDOUT : $!");
781 if (!$template->parse(*OUT)) {
783 $self->error("$self->{IN} : " . $template->get_error());
788 if ($template->uses_temp_file() || $self->{media} eq 'email') {
790 if ($self->{media} eq 'email') {
794 my $mail = new Mailer;
796 map { $mail->{$_} = $self->{$_} }
797 qw(cc bcc subject message version format charset);
798 $mail->{to} = qq|$self->{email}|;
799 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
800 $mail->{fileid} = "$fileid.";
801 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
803 # if we send html or plain text inline
804 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
805 $mail->{contenttype} = "text/html";
807 $mail->{message} =~ s/\r\n/<br>\n/g;
808 $myconfig->{signature} =~ s/\\n/<br>\n/g;
809 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
811 open(IN, $self->{tmpfile})
812 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
814 $mail->{message} .= $_;
821 if (!$self->{"do_not_attach"}) {
822 @{ $mail->{attachments} } =
823 ({ "filename" => $self->{"tmpfile"},
824 "name" => $self->{"attachment_filename"} ?
825 $self->{"attachment_filename"} : $self->{"tmpfile"} });
828 $mail->{message} =~ s/\r\n/\n/g;
829 $myconfig->{signature} =~ s/\\n/\n/g;
830 $mail->{message} .= "\n-- \n$myconfig->{signature}";
834 my $err = $mail->send($out);
835 $self->error($self->cleanup . "$err") if ($err);
841 my $numbytes = (-s $self->{tmpfile});
842 open(IN, $self->{tmpfile})
843 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
845 $self->{copies} = 1 unless $self->{media} eq 'printer';
847 chdir("$self->{cwd}");
848 #print(STDERR "Kopien $self->{copies}\n");
849 #print(STDERR "OUT $self->{OUT}\n");
850 for my $i (1 .. $self->{copies}) {
852 open(OUT, $self->{OUT})
853 or $self->error($self->cleanup . "$self->{OUT} : $!");
855 $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
857 print qq|Content-Type: | . $template->get_mime_type() . qq|
858 Content-Disposition: attachment; filename="$self->{attachment_filename}"
859 Content-Length: $numbytes
863 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
883 chdir("$self->{cwd}");
884 $main::lxdebug->leave_sub();
888 $main::lxdebug->enter_sub();
892 chdir("$self->{tmpdir}");
895 if (-f "$self->{tmpfile}.err") {
896 open(FH, "$self->{tmpfile}.err");
901 if ($self->{tmpfile}) {
902 $self->{tmpfile} =~ s|.*/||g;
904 $self->{tmpfile} =~ s/\.\w+$//g;
905 my $tmpfile = $self->{tmpfile};
906 unlink(<$tmpfile.*>);
909 chdir("$self->{cwd}");
911 $main::lxdebug->leave_sub();
917 $main::lxdebug->enter_sub();
919 my ($self, $date, $myconfig) = @_;
921 if ($date && $date =~ /\D/) {
923 if ($myconfig->{dateformat} =~ /^yy/) {
924 ($yy, $mm, $dd) = split /\D/, $date;
926 if ($myconfig->{dateformat} =~ /^mm/) {
927 ($mm, $dd, $yy) = split /\D/, $date;
929 if ($myconfig->{dateformat} =~ /^dd/) {
930 ($dd, $mm, $yy) = split /\D/, $date;
935 $yy = ($yy < 70) ? $yy + 2000 : $yy;
936 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
938 $dd = "0$dd" if ($dd < 10);
939 $mm = "0$mm" if ($mm < 10);
944 $main::lxdebug->leave_sub();
949 # Database routines used throughout
952 $main::lxdebug->enter_sub(2);
954 my ($self, $myconfig) = @_;
956 # connect to database
958 DBI->connect($myconfig->{dbconnect},
959 $myconfig->{dbuser}, $myconfig->{dbpasswd})
963 if ($myconfig->{dboptions}) {
964 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
967 $main::lxdebug->leave_sub(2);
972 sub dbconnect_noauto {
973 $main::lxdebug->enter_sub();
975 my ($self, $myconfig) = @_;
977 # connect to database
979 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
980 $myconfig->{dbpasswd}, { AutoCommit => 0 })
984 if ($myconfig->{dboptions}) {
985 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
988 $main::lxdebug->leave_sub();
994 $main::lxdebug->enter_sub();
996 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
998 # if we have a value, go do it
1001 # retrieve balance from table
1002 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1003 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1004 my ($balance) = $sth->fetchrow_array;
1010 $query = "UPDATE $table SET $field = $balance WHERE $where";
1011 do_query($self, $dbh, $query, @values);
1013 $main::lxdebug->leave_sub();
1016 sub update_exchangerate {
1017 $main::lxdebug->enter_sub();
1019 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1021 # some sanity check for currency
1023 $main::lxdebug->leave_sub();
1027 my $query = qq|SELECT e.curr FROM exchangerate e
1028 WHERE e.curr = ? AND e.transdate = ?
1030 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1033 if ($buy != 0 && $sell != 0) {
1034 $set = "buy = $buy, sell = $sell";
1035 } elsif ($buy != 0) {
1036 $set = "buy = $buy";
1037 } elsif ($sell != 0) {
1038 $set = "sell = $sell";
1041 if ($sth->fetchrow_array) {
1042 $query = qq|UPDATE exchangerate
1047 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1048 VALUES (?, $buy, $sell, ?)|;
1051 do_query($self, $dbh, $query, $curr, $transdate);
1053 $main::lxdebug->leave_sub();
1056 sub save_exchangerate {
1057 $main::lxdebug->enter_sub();
1059 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1061 my $dbh = $self->dbconnect($myconfig);
1063 my ($buy, $sell) = (0, 0);
1064 $buy = $rate if $fld eq 'buy';
1065 $sell = $rate if $fld eq 'sell';
1067 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1071 $main::lxdebug->leave_sub();
1074 sub get_exchangerate {
1075 $main::lxdebug->enter_sub();
1077 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1079 unless ($transdate) {
1080 $main::lxdebug->leave_sub();
1084 my $query = qq|SELECT e.$fld FROM exchangerate e
1085 WHERE e.curr = ? AND e.transdate = ?|;
1086 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1088 if (!$exchangerate) {
1092 $main::lxdebug->leave_sub();
1094 return $exchangerate;
1097 sub check_exchangerate {
1098 $main::lxdebug->enter_sub();
1100 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1102 unless ($transdate) {
1103 $main::lxdebug->leave_sub();
1107 my $dbh = $self->dbconnect($myconfig);
1109 my $query = qq|SELECT e.$fld FROM exchangerate e
1110 WHERE e.curr = ? AND e.transdate = ?|;
1111 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1114 $main::lxdebug->leave_sub();
1116 return $exchangerate;
1119 sub set_payment_options {
1120 $main::lxdebug->enter_sub();
1122 my ($self, $myconfig, $transdate) = @_;
1124 if ($self->{payment_id}) {
1126 my $dbh = $self->dbconnect($myconfig);
1129 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1130 qq|FROM payment_terms p | .
1133 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1134 $self->{payment_terms}) =
1135 selectrow_query($self, $dbh, $query, $self->{payment_id});
1137 if ($transdate eq "") {
1138 if ($self->{invdate}) {
1139 $transdate = $self->{invdate};
1141 $transdate = $self->{transdate};
1146 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1147 qq|FROM payment_terms|;
1148 ($self->{netto_date}, $self->{skonto_date}) =
1149 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1151 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1152 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1153 $self->{percent_skonto};
1155 $self->{skonto_amount} =
1156 $self->format_amount($myconfig, $skonto_amount, 2);
1158 if ($self->{"language_id"}) {
1160 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1161 qq|FROM translation_payment_terms t | .
1162 qq|LEFT JOIN language l ON t.language_id = l.id | .
1163 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1164 my ($description_long, $output_numberformat, $output_dateformat,
1165 $output_longdates) =
1166 selectrow_query($self, $dbh, $query,
1167 $self->{"language_id"}, $self->{"payment_id"});
1169 $self->{payment_terms} = $description_long if ($description_long);
1171 if ($output_dateformat) {
1172 foreach my $key (qw(netto_date skonto_date)) {
1174 $main::locale->reformat_date($myconfig, $self->{$key},
1180 if ($output_numberformat &&
1181 ($output_numberformat ne $myconfig->{"numberformat"})) {
1182 my $saved_numberformat = $myconfig->{"numberformat"};
1183 $myconfig->{"numberformat"} = $output_numberformat;
1184 $self->{skonto_amount} =
1185 $self->format_amount($myconfig, $skonto_amount, 2);
1186 $myconfig->{"numberformat"} = $saved_numberformat;
1190 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1191 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1192 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1193 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1194 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1195 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1196 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1197 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1198 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1199 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1204 $main::lxdebug->leave_sub();
1208 sub get_template_language {
1209 $main::lxdebug->enter_sub();
1211 my ($self, $myconfig) = @_;
1213 my $template_code = "";
1215 if ($self->{language_id}) {
1216 my $dbh = $self->dbconnect($myconfig);
1217 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1218 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1222 $main::lxdebug->leave_sub();
1224 return $template_code;
1227 sub get_printer_code {
1228 $main::lxdebug->enter_sub();
1230 my ($self, $myconfig) = @_;
1232 my $template_code = "";
1234 if ($self->{printer_id}) {
1235 my $dbh = $self->dbconnect($myconfig);
1236 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1237 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1241 $main::lxdebug->leave_sub();
1243 return $template_code;
1247 $main::lxdebug->enter_sub();
1249 my ($self, $myconfig) = @_;
1251 my $template_code = "";
1253 if ($self->{shipto_id}) {
1254 my $dbh = $self->dbconnect($myconfig);
1255 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1256 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1257 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1261 $main::lxdebug->leave_sub();
1265 $main::lxdebug->enter_sub();
1267 my ($self, $dbh, $id, $module) = @_;
1271 foreach my $item (qw(name department_1 department_2 street zipcode city country
1272 contact phone fax email)) {
1273 if ($self->{"shipto$item"}) {
1274 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1276 push(@values, $self->{"shipto${item}"});
1279 if ($self->{shipto_id}) {
1280 my $query = qq|UPDATE shipto set
1282 shiptodepartment_1 = ?,
1283 shiptodepartment_2 = ?,
1292 WHERE shipto_id = ?|;
1293 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1295 my $query = qq|SELECT * FROM shipto
1296 WHERE shiptoname = ? AND
1297 shiptodepartment_1 = ? AND
1298 shiptodepartment_2 = ? AND
1299 shiptostreet = ? AND
1300 shiptozipcode = ? AND
1302 shiptocountry = ? AND
1303 shiptocontact = ? AND
1307 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1310 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1311 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1312 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1313 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1314 do_query($self, $dbh, $query, $id, @values, $module);
1319 $main::lxdebug->leave_sub();
1323 $main::lxdebug->enter_sub();
1325 my ($self, $dbh) = @_;
1327 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1328 ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
1329 $self->{employee_id} *= 1;
1331 $main::lxdebug->leave_sub();
1335 $main::lxdebug->enter_sub();
1337 my ($self, $myconfig, $salesman_id) = @_;
1339 $main::lxdebug->leave_sub() and return unless $salesman_id;
1341 my $dbh = $self->dbconnect($myconfig);
1344 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1348 my $user = new User($main::memberfile, $login);
1349 map({ $self->{"salesman_$_"} = $user->{$_}; }
1350 qw(address businessnumber co_ustid company duns email fax name
1352 $self->{salesman_login} = $login;
1354 $self->{salesman_name} = $login
1355 if ($self->{salesman_name} eq "");
1357 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1362 $main::lxdebug->leave_sub();
1366 $main::lxdebug->enter_sub();
1368 my ($self, $myconfig) = @_;
1370 my $dbh = $self->dbconnect($myconfig);
1371 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1372 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1375 $main::lxdebug->leave_sub();
1379 $main::lxdebug->enter_sub();
1381 my ($self, $dbh, $id, $key) = @_;
1383 $key = "all_contacts" unless ($key);
1386 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1387 qq|FROM contacts | .
1388 qq|WHERE cp_cv_id = ? | .
1389 qq|ORDER BY lower(cp_name)|;
1391 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1393 $main::lxdebug->leave_sub();
1397 $main::lxdebug->enter_sub();
1399 my ($self, $dbh, $key) = @_;
1401 my ($all, $old_id, $where, @values);
1403 if (ref($key) eq "HASH") {
1406 $key = "ALL_PROJECTS";
1408 foreach my $p (keys(%{$params})) {
1410 $all = $params->{$p};
1411 } elsif ($p eq "old_id") {
1412 $old_id = $params->{$p};
1413 } elsif ($p eq "key") {
1414 $key = $params->{$p};
1420 $where = "WHERE active ";
1422 if (ref($old_id) eq "ARRAY") {
1423 my @ids = grep({ $_ } @{$old_id});
1425 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1426 push(@values, @ids);
1429 $where .= " OR (id = ?) ";
1430 push(@values, $old_id);
1436 qq|SELECT id, projectnumber, description, active | .
1439 qq|ORDER BY lower(projectnumber)|;
1441 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1443 $main::lxdebug->leave_sub();
1447 $main::lxdebug->enter_sub();
1449 my ($self, $dbh, $vc_id, $key) = @_;
1451 $key = "all_shipto" unless ($key);
1453 # get shipping addresses
1455 qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
1457 qq|WHERE trans_id = ?|;
1459 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1461 $main::lxdebug->leave_sub();
1465 $main::lxdebug->enter_sub();
1467 my ($self, $dbh, $key) = @_;
1469 $key = "all_printers" unless ($key);
1471 my $query = qq|SELECT id, printer_description, printer_command FROM printers|;
1473 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1475 $main::lxdebug->leave_sub();
1479 $main::lxdebug->enter_sub();
1481 my ($self, $dbh, $params) = @_;
1483 $key = $params->{key};
1484 $key = "all_charts" unless ($key);
1486 my $transdate = quote_db_date($params->{transdate});
1489 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1491 qq|LEFT JOIN taxkeys tk ON | .
1492 qq|(tk.id = (SELECT id FROM taxkeys | .
1493 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1494 qq| ORDER BY startdate DESC LIMIT 1)) | .
1495 qq|ORDER BY c.accno|;
1497 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1499 $main::lxdebug->leave_sub();
1502 sub _get_taxcharts {
1503 $main::lxdebug->enter_sub();
1505 my ($self, $dbh, $key) = @_;
1507 $key = "all_taxcharts" unless ($key);
1509 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1511 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1513 $main::lxdebug->leave_sub();
1516 sub _get_employees {
1517 $main::lxdebug->enter_sub();
1519 my ($self, $dbh, $key) = @_;
1521 $key = "all_employees" unless ($key);
1523 selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
1525 $main::lxdebug->leave_sub();
1528 sub _get_business_types {
1529 $main::lxdebug->enter_sub();
1531 my ($self, $dbh, $key) = @_;
1533 $key = "all_business_types" unless ($key);
1535 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1537 $main::lxdebug->leave_sub();
1541 $main::lxdebug->enter_sub();
1546 my $dbh = $self->dbconnect(\%main::myconfig);
1547 my ($sth, $query, $ref);
1549 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1550 my $vc_id = $self->{"${vc}_id"};
1552 if ($params{"contacts"}) {
1553 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1556 if ($params{"shipto"}) {
1557 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1560 if ($params{"projects"} || $params{"all_projects"}) {
1561 $self->_get_projects($dbh, $params{"all_projects"} ?
1562 $params{"all_projects"} : $params{"projects"},
1563 $params{"all_projects"} ? 1 : 0);
1566 if ($params{"printers"}) {
1567 $self->_get_printers($dbh, $params{"printers"});
1570 if ($params{"charts"}) {
1571 $self->_get_charts($dbh, $params{"charts"});
1574 if ($params{"taxcharts"}) {
1575 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1578 if ($params{"employees"}) {
1579 $self->_get_employees($dbh, $params{"employees"});
1582 if ($params{"business_types"}) {
1583 $self->_get_business_types($dbh, $params{"business_types"});
1588 $main::lxdebug->leave_sub();
1591 # this sub gets the id and name from $table
1593 $main::lxdebug->enter_sub();
1595 my ($self, $myconfig, $table) = @_;
1597 # connect to database
1598 my $dbh = $self->dbconnect($myconfig);
1600 $table = $table eq "customer" ? "customer" : "vendor";
1601 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1603 my ($query, @values);
1605 if (!$self->{openinvoices}) {
1607 if ($self->{customernumber} ne "") {
1608 $where = qq|(vc.customernumber ILIKE ?)|;
1609 push(@values, '%' . $self->{customernumber} . '%');
1611 $where = qq|(vc.name ILIKE ?)|;
1612 push(@values, '%' . $self->{$table} . '%');
1616 qq~SELECT vc.id, vc.name,
1617 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1619 WHERE $where AND (NOT vc.obsolete)
1623 qq~SELECT DISTINCT vc.id, vc.name,
1624 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1626 JOIN $table vc ON (a.${table}_id = vc.id)
1627 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1629 push(@values, '%' . $self->{$table} . '%');
1632 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1634 $main::lxdebug->leave_sub();
1636 return scalar(@{ $self->{name_list} });
1639 # the selection sub is used in the AR, AP, IS, IR and OE module
1642 $main::lxdebug->enter_sub();
1644 my ($self, $myconfig, $table, $module) = @_;
1647 my $dbh = $self->dbconnect($myconfig);
1649 $table = $table eq "customer" ? "customer" : "vendor";
1651 my $query = qq|SELECT count(*) FROM $table|;
1652 my ($count) = selectrow_query($self, $dbh, $query);
1654 # build selection list
1655 if ($count < $myconfig->{vclimit}) {
1656 $query = qq|SELECT id, name, salesman_id
1657 FROM $table WHERE NOT obsolete
1659 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1663 $self->get_employee($dbh);
1665 # setup sales contacts
1666 $query = qq|SELECT e.id, e.name
1668 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1669 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1672 push(@{ $self->{all_employees} },
1673 { id => $self->{employee_id},
1674 name => $self->{employee} });
1676 # sort the whole thing
1677 @{ $self->{all_employees} } =
1678 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1680 if ($module eq 'AR') {
1682 # prepare query for departments
1683 $query = qq|SELECT id, description
1686 ORDER BY description|;
1689 $query = qq|SELECT id, description
1691 ORDER BY description|;
1694 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1697 $query = qq|SELECT id, description
1701 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1704 $query = qq|SELECT printer_description, id
1706 ORDER BY printer_description|;
1708 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1711 $query = qq|SELECT id, description
1715 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1719 $main::lxdebug->leave_sub();
1722 sub language_payment {
1723 $main::lxdebug->enter_sub();
1725 my ($self, $myconfig) = @_;
1727 my $dbh = $self->dbconnect($myconfig);
1729 my $query = qq|SELECT id, description
1733 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1736 $query = qq|SELECT printer_description, id
1738 ORDER BY printer_description|;
1740 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1743 $query = qq|SELECT id, description
1747 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1749 # get buchungsgruppen
1750 $query = qq|SELECT id, description
1751 FROM buchungsgruppen|;
1753 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1756 $main::lxdebug->leave_sub();
1759 # this is only used for reports
1760 sub all_departments {
1761 $main::lxdebug->enter_sub();
1763 my ($self, $myconfig, $table) = @_;
1765 my $dbh = $self->dbconnect($myconfig);
1768 if ($table eq 'customer') {
1769 $where = "WHERE role = 'P' ";
1772 my $query = qq|SELECT id, description
1775 ORDER BY description|;
1776 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1778 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1782 $main::lxdebug->leave_sub();
1786 $main::lxdebug->enter_sub();
1788 my ($self, $module, $myconfig, $table) = @_;
1791 if ($table eq "customer") {
1800 $self->all_vc($myconfig, $table, $module);
1802 # get last customers or vendors
1803 my ($query, $sth, $ref);
1805 my $dbh = $self->dbconnect($myconfig);
1810 my $transdate = "current_date";
1811 if ($self->{transdate}) {
1812 $transdate = $dbh->quote($self->{transdate});
1815 # now get the account numbers
1816 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1817 FROM chart c, taxkeys tk
1818 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1819 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1822 $sth = $dbh->prepare($query);
1824 do_statement($form, $sth, $query, '%' . $module . '%');
1826 $self->{accounts} = "";
1827 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1829 foreach my $key (split(/:/, $ref->{link})) {
1830 if ($key =~ /$module/) {
1832 # cross reference for keys
1833 $xkeyref{ $ref->{accno} } = $key;
1835 push @{ $self->{"${module}_links"}{$key} },
1836 { accno => $ref->{accno},
1837 description => $ref->{description},
1838 taxkey => $ref->{taxkey_id},
1839 tax_id => $ref->{tax_id} };
1841 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1847 # get taxkeys and description
1848 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
1849 $self->{TAXKEY} = selectall_hashref_query($form, $dbh, $query);
1852 $query = qq|SELECT id, description FROM tax_zones|;
1853 $self->{TAXZONE} = selectall_hashref_query($form, $dbh, $query);
1855 if (($module eq "AP") || ($module eq "AR")) {
1856 # get tax rates and description
1857 $query = qq|SELECT * FROM tax|;
1858 $self->{TAX} = selectall_hashref_query($form, $dbh, $query);
1864 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
1865 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
1866 a.intnotes, a.department_id, a.amount AS oldinvtotal,
1867 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
1869 d.description AS department,
1872 JOIN $table c ON (a.${table}_id = c.id)
1873 LEFT JOIN employee e ON (e.id = a.employee_id)
1874 LEFT JOIN department d ON (d.id = a.department_id)
1876 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
1878 foreach $key (keys %$ref) {
1879 $self->{$key} = $ref->{$key};
1882 my $transdate = "current_date";
1883 if ($self->{transdate}) {
1884 $transdate = $dbh->quote($self->{transdate});
1887 # now get the account numbers
1890 c.accno, c.description, c.link, c.taxkey_id,
1893 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
1894 WHERE (c.link LIKE ?) AND (tk.chart_id = c.id) AND NOT (c.link LIKE '%_tax%')
1897 $sth = $dbh->prepare($query);
1898 do_statement($form, $sth, $query, "%" . $module . "%");
1900 $self->{accounts} = "";
1901 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1903 foreach my $key (split(/:/, $ref->{link})) {
1904 if ($key =~ /$module/) {
1906 # cross reference for keys
1907 $xkeyref{ $ref->{accno} } = $key;
1909 push @{ $self->{"${module}_links"}{$key} },
1910 { accno => $ref->{accno},
1911 description => $ref->{description},
1912 taxkey => $ref->{taxkey_id},
1913 tax_id => $ref->{tax_id} };
1915 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1921 # get amounts from individual entries
1924 c.accno, c.description,
1925 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
1929 LEFT JOIN chart c ON (c.id = a.chart_id)
1930 LEFT JOIN project p ON (p.id = a.project_id)
1931 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
1932 WHERE (tk.taxkey_id=a.taxkey) AND
1933 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
1934 THEN tk.chart_id = a.chart_id
1937 OR (c.link='%tax%')) AND
1938 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
1939 WHERE a.trans_id = ?
1940 AND a.fx_transaction = '0'
1941 ORDER BY a.oid, a.transdate|;
1942 $sth = $dbh->prepare($query);
1943 do_statement($form, $sth, $query, $self->{id});
1945 # get exchangerate for currency
1946 $self->{exchangerate} =
1947 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
1950 # store amounts in {acc_trans}{$key} for multiple accounts
1951 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1952 $ref->{exchangerate} =
1953 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
1954 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1957 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1958 $ref->{amount} *= -1;
1960 $ref->{index} = $index;
1962 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1968 d.curr AS currencies, d.closedto, d.revtrans,
1969 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1970 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1972 $ref = selectfirst_hashref_query($self, $dbh, $query);
1973 map { $self->{$_} = $ref->{$_} } keys %$ref;
1980 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
1981 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1982 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1984 $ref = selectfirst_hashref_query($self, $dbh, $query);
1985 map { $self->{$_} = $ref->{$_} } keys %$ref;
1987 if ($self->{"$self->{vc}_id"}) {
1989 # only setup currency
1990 ($self->{currency}) = split(/:/, $self->{currencies});
1994 $self->lastname_used($dbh, $myconfig, $table, $module);
1996 # get exchangerate for currency
1997 $self->{exchangerate} =
1998 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2006 $main::lxdebug->leave_sub();
2010 $main::lxdebug->enter_sub();
2012 my ($self, $dbh, $myconfig, $table, $module) = @_;
2014 my $arap = ($table eq 'customer') ? "ar" : "ap";
2015 $table = $table eq "customer" ? "customer" : "vendor";
2016 my $where = "1 = 1";
2018 if ($self->{type} =~ /_order/) {
2020 $where = "quotation = '0'";
2022 if ($self->{type} =~ /_quotation/) {
2024 $where = "quotation = '1'";
2027 my $query = qq|SELECT MAX(id) FROM $arap
2028 WHERE $where AND ${table}_id > 0|;
2029 my ($trans_id) = selectrow_query($self, $dbh, $query);
2034 a.curr, a.${table}_id, a.department_id,
2035 d.description AS department,
2036 ct.name, current_date + ct.terms AS duedate
2038 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2039 LEFT JOIN department d ON (a.department_id = d.id)
2041 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2042 $self->{department}, $self->{$table}, $self->{duedate})
2043 = selectrow_query($self, $dbh, $query, $trans_id);
2045 $main::lxdebug->leave_sub();
2049 $main::lxdebug->enter_sub();
2051 my ($self, $myconfig, $thisdate, $days) = @_;
2053 my $dbh = $self->dbconnect($myconfig);
2058 my $dateformat = $myconfig->{dateformat};
2059 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2060 $thisdate = $dbh->quote($thisdate);
2061 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2063 $query = qq|SELECT current_date AS thisdate|;
2066 ($thisdate) = selectrow_query($self, $dbh, $query);
2070 $main::lxdebug->leave_sub();
2076 $main::lxdebug->enter_sub();
2078 my ($self, $string) = @_;
2080 if ($string !~ /%/) {
2081 $string = "%$string%";
2084 $string =~ s/\'/\'\'/g;
2086 $main::lxdebug->leave_sub();
2092 $main::lxdebug->enter_sub();
2094 my ($self, $flds, $new, $count, $numrows) = @_;
2098 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2104 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2106 $j = $item->{ndx} - 1;
2107 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2111 for $i ($count + 1 .. $numrows) {
2112 map { delete $self->{"${_}_$i"} } @{$flds};
2115 $main::lxdebug->leave_sub();
2119 $main::lxdebug->enter_sub();
2121 my ($self, $myconfig) = @_;
2125 my $dbh = $self->dbconnect_noauto($myconfig);
2127 my $query = qq|DELETE FROM status
2128 WHERE (formname = ?) AND (trans_id = ?)|;
2129 my $sth = prepare_query($self, $dbh, $query);
2131 if ($self->{formname} =~ /(check|receipt)/) {
2132 for $i (1 .. $self->{rowcount}) {
2133 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2136 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2140 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2141 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2143 my %queued = split / /, $self->{queued};
2146 if ($self->{formname} =~ /(check|receipt)/) {
2148 # this is a check or receipt, add one entry for each lineitem
2149 my ($accno) = split /--/, $self->{account};
2150 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2151 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2152 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2153 $sth = prepare_query($self, $dbh, $query);
2155 for $i (1 .. $self->{rowcount}) {
2156 if ($self->{"checked_$i"}) {
2157 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2163 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2164 VALUES (?, ?, ?, ?, ?)|;
2165 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2166 $queued{$self->{formname}}, $self->{formname});
2172 $main::lxdebug->leave_sub();
2176 $main::lxdebug->enter_sub();
2178 my ($self, $dbh) = @_;
2180 my ($query, $printed, $emailed);
2182 my $formnames = $self->{printed};
2183 my $emailforms = $self->{emailed};
2185 my $query = qq|DELETE FROM status
2186 WHERE (formname = ?) AND (trans_id = ?)|;
2187 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2189 # this only applies to the forms
2190 # checks and receipts are posted when printed or queued
2192 if ($self->{queued}) {
2193 my %queued = split / /, $self->{queued};
2195 foreach my $formname (keys %queued) {
2196 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2197 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2199 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2200 VALUES (?, ?, ?, ?, ?)|;
2201 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2203 $formnames =~ s/$self->{formname}//;
2204 $emailforms =~ s/$self->{formname}//;
2209 # save printed, emailed info
2210 $formnames =~ s/^ +//g;
2211 $emailforms =~ s/^ +//g;
2214 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2215 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2217 foreach my $formname (keys %status) {
2218 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2219 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2221 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2222 VALUES (?, ?, ?, ?)|;
2223 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2226 $main::lxdebug->leave_sub();
2230 # $main::locale->text('SAVED')
2231 # $main::locale->text('DELETED')
2232 # $main::locale->text('ADDED')
2233 # $main::locale->text('PAYMENT POSTED')
2234 # $main::locale->text('POSTED')
2235 # $main::locale->text('POSTED AS NEW')
2236 # $main::locale->text('ELSE')
2237 # $main::locale->text('SAVED FOR DUNNING')
2238 # $main::locale->text('DUNNING STARTED')
2239 # $main::locale->text('PRINTED')
2240 # $main::locale->text('MAILED')
2241 # $main::locale->text('SCREENED')
2242 # $main::locale->text('invoice')
2243 # $main::locale->text('proforma')
2244 # $main::locale->text('sales_order')
2245 # $main::locale->text('packing_list')
2246 # $main::locale->text('pick_list')
2247 # $main::locale->text('purchase_order')
2248 # $main::locale->text('bin_list')
2249 # $main::locale->text('sales_quotation')
2250 # $main::locale->text('request_quotation')
2253 $main::lxdebug->enter_sub();
2258 if(!exists $self->{employee_id}) {
2259 &get_employee($self, $dbh);
2263 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done) | .
2264 qq|VALUES (?, ?, ?, ?)|;
2265 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2266 $self->{addition}, $self->{what_done});
2267 do_query($self, $dbh, $query, @values);
2269 $main::lxdebug->leave_sub();
2273 $main::lxdebug->enter_sub();
2277 my $trans_id = shift();
2278 my $restriction = shift();
2281 if ($trans_id ne "") {
2283 qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name | .
2284 qq|FROM history_erp h | .
2285 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2286 qq|WHERE trans_id = ? |
2289 my $sth = $dbh->prepare($query) || $self->dberror($query);
2291 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2293 while(my $hash_ref = $sth->fetchrow_hashref()) {
2294 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2295 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2296 $tempArray[$i++] = $hash_ref;
2298 $main::lxdebug->leave_sub() and return \@tempArray
2299 if ($i > 0 && $tempArray[0] ne "");
2301 $main::lxdebug->leave_sub();
2305 sub update_defaults {
2306 $main::lxdebug->enter_sub();
2308 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2311 if ($provided_dbh) {
2312 $dbh = $provided_dbh;
2314 $dbh = $self->dbconnect_noauto($myconfig);
2316 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2317 my $sth = $dbh->prepare($query);
2319 $sth->execute || $self->dberror($query);
2320 my ($var) = $sth->fetchrow_array;
2325 $query = qq|UPDATE defaults SET $fld = ?|;
2326 do_query($self, $dbh, $query, $var);
2328 if (!$provided_dbh) {
2333 $main::lxdebug->leave_sub();
2338 sub update_business {
2339 $main::lxdebug->enter_sub();
2341 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2344 if ($provided_dbh) {
2345 $dbh = $provided_dbh;
2347 $dbh = $self->dbconnect_noauto($myconfig);
2350 qq|SELECT customernumberinit FROM business
2351 WHERE id = ? FOR UPDATE|;
2352 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2357 $query = qq|UPDATE business
2358 SET customernumberinit = ?
2360 do_query($self, $dbh, $query, $var, $business_id);
2362 if (!$provided_dbh) {
2367 $main::lxdebug->leave_sub();
2372 sub get_partsgroup {
2373 $main::lxdebug->enter_sub();
2375 my ($self, $myconfig, $p) = @_;
2377 my $dbh = $self->dbconnect($myconfig);
2379 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2381 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2384 if ($p->{searchitems} eq 'part') {
2385 $query .= qq|WHERE p.inventory_accno_id > 0|;
2387 if ($p->{searchitems} eq 'service') {
2388 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2390 if ($p->{searchitems} eq 'assembly') {
2391 $query .= qq|WHERE p.assembly = '1'|;
2393 if ($p->{searchitems} eq 'labor') {
2394 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2397 $query .= qq|ORDER BY partsgroup|;
2400 $query = qq|SELECT id, partsgroup FROM partsgroup
2401 ORDER BY partsgroup|;
2404 if ($p->{language_code}) {
2405 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2406 t.description AS translation
2408 JOIN parts p ON (p.partsgroup_id = pg.id)
2409 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2410 ORDER BY translation|;
2411 @values = ($p->{language_code});
2414 $self->{all_partsgroups} = selectall_hashref_query($self, $dbh, $query, @values);
2417 $main::lxdebug->leave_sub();
2420 sub get_pricegroup {
2421 $main::lxdebug->enter_sub();
2423 my ($self, $myconfig, $p) = @_;
2425 my $dbh = $self->dbconnect($myconfig);
2427 my $query = qq|SELECT p.id, p.pricegroup
2430 $query .= qq| ORDER BY pricegroup|;
2433 $query = qq|SELECT id, pricegroup FROM pricegroup
2434 ORDER BY pricegroup|;
2437 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2441 $main::lxdebug->leave_sub();
2445 # usage $form->all_years($myconfig, [$dbh])
2446 # return list of all years where bookings found
2449 $main::lxdebug->enter_sub();
2451 my ($self, $myconfig, $dbh) = @_;
2455 $dbh = $self->dbconnect($myconfig);
2460 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2461 (SELECT MAX(transdate) FROM acc_trans)|;
2462 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2464 if ($myconfig->{dateformat} =~ /^yy/) {
2465 ($startdate) = split /\W/, $startdate;
2466 ($enddate) = split /\W/, $enddate;
2468 (@_) = split /\W/, $startdate;
2470 (@_) = split /\W/, $enddate;
2475 $startdate = substr($startdate,0,4);
2476 $enddate = substr($enddate,0,4);
2478 while ($enddate >= $startdate) {
2479 push @all_years, $enddate--;
2482 $dbh->disconnect if $disconnect;
2486 $main::lxdebug->leave_sub();