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 #======================================================================
41 $main::lxdebug->enter_sub();
45 my @pairs = split(/&/, $input);
48 my ($name, $value) = split(/=/, $_, 2);
49 $in{$name} = unescape(undef, $value);
52 $main::lxdebug->leave_sub();
57 sub _request_to_hash {
58 $main::lxdebug->enter_sub();
61 my ($i, $loc, $key, $val);
62 my (%ATTACH, $f, $header, $header_body, $len, $buf);
63 my ($boundary, @list, $size, $body, $x, $blah, $name);
65 if ($ENV{'CONTENT_TYPE'}
66 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
67 $boundary = quotemeta('--' . $1);
68 @list = split(/$boundary/, $input);
70 # For some reason there are always 2 extra, that are empty
73 for ($x = 1; $x <= $size; $x++) {
74 $header_body = $list[$x];
75 $header_body =~ /\r\n\r\n|\n\n/;
77 # Here we split the header and body
82 # Now we try to get the file name
84 $name =~ /name=\"(.+)\"/;
85 ($name, $blah) = split(/\"/, $1);
87 # If the form name is not attach, then we need to parse this like
89 if ($name ne "attach") {
90 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
91 $ATTACH{$name} = $body;
93 # Otherwise it is an attachment and we need to finish it up
94 } elsif ($name eq "attach") {
95 $header =~ /filename=\"(.+)\"/;
96 $ATTACH{'FILE_NAME'} = $1;
97 $ATTACH{'FILE_NAME'} =~ s/\"//g;
98 $ATTACH{'FILE_NAME'} =~ s/\s//g;
99 $ATTACH{'FILE_CONTENT'} = $body;
101 for ($i = $x; $list[$i]; $i++) {
102 $list[$i] =~ s/^.+name=$//;
103 $list[$i] =~ /\"(\w+)\"/;
109 $main::lxdebug->leave_sub();
113 $main::lxdebug->leave_sub();
114 return _input_to_hash($input);
119 $main::lxdebug->enter_sub();
125 read(STDIN, $_, $ENV{CONTENT_LENGTH});
127 if ($ENV{QUERY_STRING}) {
128 $_ = $ENV{QUERY_STRING};
135 my %parameters = _request_to_hash($_);
136 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
138 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
140 $self->{action} = lc $self->{action};
141 $self->{action} =~ s/( |-|,|#)/_/g;
143 $self->{version} = "2.1.2";
144 $self->{dbversion} = "2.1.2";
146 $main::lxdebug->leave_sub();
152 $main::lxdebug->enter_sub();
158 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
160 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
166 my ($self, $str, $beenthere) = @_;
168 # for Apache 2 we escape strings twice
169 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
170 # $str = $self->escape($str, 1);
173 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
175 $main::lxdebug->leave_sub();
181 $main::lxdebug->enter_sub();
183 my ($self, $str) = @_;
188 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
190 $main::lxdebug->leave_sub();
196 my ($self, $str) = @_;
198 if ($str && ! ref($str)) {
199 $str =~ s/"/"/g;
208 my ($self, $str) = @_;
210 if ($str && ! ref($str)) {
211 $str =~ s/"/"/g;
223 for (@_) { print qq|<input type=hidden name="$_" value="|.$self->quote($self->{$_}).qq|">\n| }
225 delete $self->{header};
226 for (sort keys %$self) { print qq|<input type=hidden name="$_" value="|.$self->quote($self->{$_}).qq|">\n| }
232 $main::lxdebug->enter_sub();
234 my ($self, $msg) = @_;
236 if ($ENV{HTTP_USER_AGENT}) {
244 <h2 class=error>Error!</h2>
256 if ($self->{error_function}) {
257 &{ $self->{error_function} }($msg);
263 $main::lxdebug->leave_sub();
267 $main::lxdebug->enter_sub();
269 my ($self, $msg) = @_;
271 if ($ENV{HTTP_USER_AGENT}) {
274 if (!$self->{header}) {
287 if ($self->{info_function}) {
288 &{ $self->{info_function} }($msg);
294 $main::lxdebug->leave_sub();
298 $main::lxdebug->enter_sub();
300 my ($self, $str, $cols, $maxrows) = @_;
304 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
306 $maxrows = $rows unless defined $maxrows;
308 $main::lxdebug->leave_sub();
310 return ($rows > $maxrows) ? $maxrows : $rows;
314 $main::lxdebug->enter_sub();
316 my ($self, $msg) = @_;
318 $self->error("$msg\n" . $DBI::errstr);
320 $main::lxdebug->leave_sub();
324 $main::lxdebug->enter_sub();
326 my ($self, $name, $msg) = @_;
328 if ($self->{$name} =~ /^\s*$/) {
331 $main::lxdebug->leave_sub();
335 $main::lxdebug->enter_sub();
339 if ($self->{header}) {
340 $main::lxdebug->leave_sub();
344 my ($stylesheet, $favicon, $charset);
346 if ($ENV{HTTP_USER_AGENT}) {
348 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
350 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
354 if ($self->{favicon} && (-f "$self->{favicon}")) {
356 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
360 if ($self->{charset}) {
362 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
365 if ($self->{landscape}) {
366 $pagelayout = qq|<style type="text/css">
367 \@page { size:landscape; }
370 if ($self->{fokus}) {
371 $fokus = qq|<script type="text/javascript">
373 function fokus(){document.$self->{fokus}.focus();}
380 if ($self->{jsscript} == 1) {
383 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
384 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
385 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
386 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
393 ? "$self->{title} - $self->{titlebar}"
396 print qq|Content-Type: text/html
399 <title>$self->{titlebar}</title>
412 $main::lxdebug->leave_sub();
415 # write Trigger JavaScript-Code ($qty = 1 - only one Trigger)
417 $main::lxdebug->enter_sub();
419 my ($self, $myconfig, $qty,
420 $inputField_1, $align_1, $button_1,
421 $inputField_2, $align_2, $button_2)
424 # set dateform for jsscript
426 $ifFormat = "%d.%m.%Y";
427 if ($myconfig->{dateformat} eq "dd.mm.yy") {
428 $ifFormat = "%d.%m.%Y";
430 if ($myconfig->{dateformat} eq "dd-mm-yy") {
431 $ifFormat = "%d-%m-%Y";
433 if ($myconfig->{dateformat} eq "dd/mm/yy") {
434 $ifFormat = "%d/%m/%Y";
436 if ($myconfig->{dateformat} eq "mm/dd/yy") {
437 $ifFormat = "%m/%d/%Y";
439 if ($myconfig->{dateformat} eq "mm-dd-yy") {
440 $ifFormat = "%m-%d-%Y";
442 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
443 $ifFormat = "%Y-%m-%d";
454 inputField : "$inputField_1",
455 ifFormat :"$ifFormat",
466 inputField : "$inputField_2",
467 ifFormat :"$ifFormat",
475 <script type="text/javascript">
483 $main::lxdebug->leave_sub();
486 } #end sub write_trigger
489 $main::lxdebug->enter_sub();
491 my ($self, $msg) = @_;
493 if ($self->{callback}) {
495 ($script, $argv) = split(/\?/, $self->{callback});
496 exec("perl", "$script", $argv);
504 $main::lxdebug->leave_sub();
507 # sort of columns removed - empty sub
509 $main::lxdebug->enter_sub();
511 my ($self, @columns) = @_;
513 $main::lxdebug->leave_sub();
519 $main::lxdebug->enter_sub();
521 my ($self, $myconfig, $amount, $places, $dash) = @_;
523 if ($places =~ /\d/) {
524 $amount = $self->round_amount($amount, $places);
527 # is the amount negative
528 my $negative = ($amount < 0);
532 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
534 my ($whole, $dec) = split /\./, "$amount";
536 $amount = join '', reverse split //, $whole;
537 $fillup = "0" x ($places - length($dec));
539 if ($myconfig->{numberformat} eq '1,000.00') {
540 $amount =~ s/\d{3,}?/$&,/g;
542 $amount = join '', reverse split //, $amount;
543 $amount .= "\.$dec".$fillup if ($places ne '' && $places*1 != 0);
546 if ($myconfig->{numberformat} eq '1.000,00') {
547 $amount =~ s/\d{3,}?/$&./g;
549 $amount = join '', reverse split //, $amount;
550 $amount .= ",$dec".$fillup if ($places ne '' && $places*1 != 0);
553 if ($myconfig->{numberformat} eq '1000,00') {
555 $amount .= ",$dec" .$fillup if ($places ne '' && $places*1 != 0);
559 $amount = ($negative) ? "($amount)" : "$amount";
560 } elsif ($dash =~ /DRCR/) {
561 $amount = ($negative) ? "$amount DR" : "$amount CR";
563 $amount = ($negative) ? "-$amount" : "$amount";
567 if ($dash eq "0" && $places) {
568 if ($myconfig->{numberformat} eq '1.000,00') {
569 $amount = "0" . "," . "0" x $places;
571 $amount = "0" . "." . "0" x $places;
574 $amount = ($dash ne "") ? "$dash" : "0";
578 $main::lxdebug->leave_sub();
584 $main::lxdebug->enter_sub();
586 my ($self, $myconfig, $amount) = @_;
588 if (!(substr($amount, -3, 1) eq ".")) {
589 if ( ($myconfig->{numberformat} eq '1.000,00')
590 || ($myconfig->{numberformat} eq '1000,00')) {
598 $main::lxdebug->leave_sub();
600 return ($amount * 1);
604 $main::lxdebug->enter_sub();
606 my ($self, $amount, $places) = @_;
609 # Rounding like "Kaufmannsrunden"
610 # Descr. http://de.wikipedia.org/wiki/Rundung
612 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
615 $amount = $amount * (10 ** ($places));
616 $round_amount = int($amount + .5 * ($amount <=> 0))/(10**($places));
618 $main::lxdebug->leave_sub();
620 return $round_amount;
627 $main::lxdebug->enter_sub();
629 my ($self, $myconfig, $userspath) = @_;
632 # Some variables used for page breaks
633 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
635 my ($current_page, $current_line, $current_row) = (1, 1, 0);
641 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
642 $self->format_string(grep(/notes/, keys(%{$self})));
644 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
645 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
647 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
648 qw(email tel fax name signature));
650 open(IN, "$self->{templates}/$self->{IN}")
651 or $self->error("$self->{IN} : $!");
656 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
658 # OUT is used for the media, screen, printer, email
659 # for postscript we store a copy in a temporary file
661 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
662 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
664 $self->{OUT} = ">$self->{tmpfile}";
668 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
670 open(OUT, ">-") or $self->error("STDOUT : $!");
674 # Do we have to run LaTeX two times? This is needed if
675 # the template contains page references.
678 # first we generate a tmpfile
679 # read file and replace <%variable%>
685 $two_passes = 1 if (/\\pageref/);
688 # detect pagebreak block and its parameters
689 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
690 $chars_per_line = $1;
691 $lines_on_first_page = $2;
692 $lines_on_second_page = $3;
695 last if (/\s*<%end pagebreak%>/);
702 if (/\s*<%foreach /) {
704 # this one we need for the count
706 $var =~ s/\s*<%foreach (.+?)%>/$1/;
708 last if (/\s*<%end /);
714 # display contents of $self->{number}[] array
715 for $i (0 .. $#{ $self->{$var} }) {
718 # Try to detect whether a manual page break is necessary
719 # but only if there was a <%pagebreak ...%> block before
721 if ($chars_per_line) {
723 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
726 my $_description = $self->{"description"}[$i];
727 while ($_description =~ /\\newline/) {
729 $_description =~ s/\\newline//;
731 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
733 if ($current_page == 1) {
734 $lpp = $lines_on_first_page;
736 $lpp = $lines_on_second_page;
739 # Yes we need a manual page break -- or the user has forced one
740 if ((($current_line + $lines) > $lpp) ||
741 ($self->{"_forced_pagebreaks"} && grep(/^${current_row}$/, @{$self->{"_forced_pagebreaks"}}))) {
744 # replace the special variables <%sumcarriedforward%>
747 my $psum = $self->format_amount($myconfig, $sum, 2);
748 $pb =~ s/<%sumcarriedforward%>/$psum/g;
749 $pb =~ s/<%lastpage%>/$current_page/g;
751 # only "normal" variables are supported here
752 # (no <%if, no <%foreach, no <%include)
754 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
756 # page break block is ready to rock
761 $current_line += $lines;
764 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
768 # don't parse par, we need it for each line
770 s/<%(.+?)%>/$self->{$1}[$i]/mg;
776 # if not comes before if!
777 if (/\s*<%if not /) {
779 # check if it is not set and display
781 s/\s*<%if not (.+?)%>/$1/;
783 unless ($self->{$_}) {
785 last if (/\s*<%end /);
795 last if (/\s*<%end /);
803 # check if it is set and display
805 s/\s*<%if (.+?)%>/$1/;
809 last if (/\s*<%end /);
819 last if (/\s*<%end /);
825 # check for <%include filename%>
826 if (/\s*<%include /) {
830 $var =~ s/\s*<%include (.+?)%>/$1/;
833 $var =~ s/(\/|\.\.)//g;
835 # prevent the infinite loop!
836 next if ($self->{"$var"});
838 open(INC, "$self->{templates}/$var")
839 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
848 s/<%(.+?)%>/$self->{$1}/g;
855 # Convert the tex file to postscript
856 if ($self->{format} =~ /(postscript|pdf)/) {
859 $self->{cwd} = cwd();
860 $self->{tmpdir} = "$self->{cwd}/$userspath";
862 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
864 $self->{tmpfile} =~ s/$userspath\///g;
866 if ($self->{format} eq 'postscript') {
868 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
870 $self->error($self->cleanup) if ($?);
873 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
875 $self->error($self->cleanup) if ($?);
878 $self->{tmpfile} =~ s/tex$/dvi/;
880 system("dvips $self->{tmpfile} -o -q > /dev/null");
881 $self->error($self->cleanup . "dvips : $!") if ($?);
882 $self->{tmpfile} =~ s/dvi$/ps/;
884 if ($self->{format} eq 'pdf') {
886 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
888 $self->error($self->cleanup) if ($?);
891 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
893 $self->error($self->cleanup) if ($?);
895 $self->{tmpfile} =~ s/tex$/pdf/;
900 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
902 if ($self->{media} eq 'email') {
906 my $mail = new Mailer;
908 map { $mail->{$_} = $self->{$_} }
909 qw(cc bcc subject message version format charset);
910 $mail->{to} = qq|$self->{email}|;
911 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
912 $mail->{fileid} = "$fileid.";
914 # if we send html or plain text inline
915 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
916 $mail->{contenttype} = "text/html";
918 $mail->{message} =~ s/\r\n/<br>\n/g;
919 $myconfig->{signature} =~ s/\\n/<br>\n/g;
920 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
922 open(IN, $self->{tmpfile})
923 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
925 $mail->{message} .= $_;
932 @{ $mail->{attachments} } = ($self->{tmpfile});
934 $myconfig->{signature} =~ s/\\n/\r\n/g;
935 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
939 my $err = $mail->send($out);
940 $self->error($self->cleanup . "$err") if ($err);
946 my $numbytes = (-s $self->{tmpfile});
947 open(IN, $self->{tmpfile})
948 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
950 $self->{copies} = 1 unless $self->{media} eq 'printer';
952 chdir("$self->{cwd}");
954 for my $i (1 .. $self->{copies}) {
956 open(OUT, $self->{OUT})
957 or $self->error($self->cleanup . "$self->{OUT} : $!");
961 print qq|Content-Type: application/$self->{format}
962 Content-Disposition: attachment; filename="$self->{tmpfile}"
963 Content-Length: $numbytes
967 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
987 chdir("$self->{cwd}");
988 $main::lxdebug->leave_sub();
992 $main::lxdebug->enter_sub();
996 chdir("$self->{tmpdir}");
999 if (-f "$self->{tmpfile}.err") {
1000 open(FH, "$self->{tmpfile}.err");
1005 if ($self->{tmpfile}) {
1008 $self->{tmpfile} =~ s/\.\w+$//g;
1009 my $tmpfile = $self->{tmpfile};
1010 unlink(<$tmpfile.*>);
1013 chdir("$self->{cwd}");
1015 $main::lxdebug->leave_sub();
1021 $main::lxdebug->enter_sub();
1023 my ($self, @fields) = @_;
1026 %unique_fields = map({ $_ => 1 } @fields);
1027 @fields = keys(%unique_fields);
1029 foreach my $field (@fields) {
1030 next unless ($self->{$field} =~ /\<pagebreak\>/);
1031 $self->{$field} =~ s/\<pagebreak\>//g;
1032 if ($field =~ /.*_(\d+)$/) {
1033 if ($self->{"_forced_pagebreaks"}) {
1034 $self->{"_forced_pagebreaks"} = [];
1036 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1040 my $format = $self->{format};
1041 if ($self->{format} =~ /(postscript|pdf)/) {
1048 '<', '>', quotemeta('\n'), '
1052 '&', quotemeta('\n'), '
1054 '"', '\$', '%', '_', '#', quotemeta('^'),
1055 '{', '}', '<', '>', '£', "\r"
1061 quotemeta('\n') => '<br>',
1072 quotemeta('^') => '\^\\',
1077 quotemeta('\n') => '\newline ',
1084 foreach my $key (@{ $replace{order}{$format} }) {
1085 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1088 # Allow some HTML markup to be converted into the output format's
1089 # corresponding markup code, e.g. bold or italic.
1090 if ('html' eq $format) {
1091 my @markup_replace = ('b', 'i', 's', 'u');
1093 foreach my $key (@markup_replace) {
1094 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1097 } elsif ('tex' eq $format) {
1098 my %markup_replace = ('b' => 'textbf',
1100 'u' => 'underline');
1102 foreach my $field (@fields) {
1103 if ($field =~ /descrip/) {
1104 print(STDERR "QFT: ${field}: " . $self->{$field} . "\n");
1106 foreach my $key (keys(%markup_replace)) {
1107 my $new = $markup_replace{$key};
1109 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1114 $main::lxdebug->leave_sub();
1118 $main::lxdebug->enter_sub();
1120 my ($self, $date, $myconfig) = @_;
1122 if ($date && $date =~ /\D/) {
1124 if ($myconfig->{dateformat} =~ /^yy/) {
1125 ($yy, $mm, $dd) = split /\D/, $date;
1127 if ($myconfig->{dateformat} =~ /^mm/) {
1128 ($mm, $dd, $yy) = split /\D/, $date;
1130 if ($myconfig->{dateformat} =~ /^dd/) {
1131 ($dd, $mm, $yy) = split /\D/, $date;
1136 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1137 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1139 $dd = "0$dd" if ($dd < 10);
1140 $mm = "0$mm" if ($mm < 10);
1142 $date = "$yy$mm$dd";
1145 $main::lxdebug->leave_sub();
1150 # Database routines used throughout
1153 $main::lxdebug->enter_sub();
1155 my ($self, $myconfig) = @_;
1157 # connect to database
1159 DBI->connect($myconfig->{dbconnect},
1160 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1164 if ($myconfig->{dboptions}) {
1165 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1168 $main::lxdebug->leave_sub();
1173 sub dbconnect_noauto {
1174 $main::lxdebug->enter_sub();
1176 my ($self, $myconfig) = @_;
1178 # connect to database
1180 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1181 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1185 if ($myconfig->{dboptions}) {
1186 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1189 $main::lxdebug->leave_sub();
1194 sub update_balance {
1195 $main::lxdebug->enter_sub();
1197 my ($self, $dbh, $table, $field, $where, $value) = @_;
1199 # if we have a value, go do it
1202 # retrieve balance from table
1203 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1204 my $sth = $dbh->prepare($query);
1206 $sth->execute || $self->dberror($query);
1207 my ($balance) = $sth->fetchrow_array;
1213 $query = "UPDATE $table SET $field = $balance WHERE $where";
1214 $dbh->do($query) || $self->dberror($query);
1216 $main::lxdebug->leave_sub();
1219 sub update_exchangerate {
1220 $main::lxdebug->enter_sub();
1222 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1224 # some sanity check for currency
1226 $main::lxdebug->leave_sub();
1230 my $query = qq|SELECT e.curr FROM exchangerate e
1231 WHERE e.curr = '$curr'
1232 AND e.transdate = '$transdate'
1234 my $sth = $dbh->prepare($query);
1235 $sth->execute || $self->dberror($query);
1238 if ($buy != 0 && $sell != 0) {
1239 $set = "buy = $buy, sell = $sell";
1240 } elsif ($buy != 0) {
1241 $set = "buy = $buy";
1242 } elsif ($sell != 0) {
1243 $set = "sell = $sell";
1246 if ($sth->fetchrow_array) {
1247 $query = qq|UPDATE exchangerate
1249 WHERE curr = '$curr'
1250 AND transdate = '$transdate'|;
1252 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1253 VALUES ('$curr', $buy, $sell, '$transdate')|;
1256 $dbh->do($query) || $self->dberror($query);
1258 $main::lxdebug->leave_sub();
1261 sub save_exchangerate {
1262 $main::lxdebug->enter_sub();
1264 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1266 my $dbh = $self->dbconnect($myconfig);
1268 my ($buy, $sell) = (0, 0);
1269 $buy = $rate if $fld eq 'buy';
1270 $sell = $rate if $fld eq 'sell';
1272 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1276 $main::lxdebug->leave_sub();
1279 sub get_exchangerate {
1280 $main::lxdebug->enter_sub();
1282 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1284 my $query = qq|SELECT e.$fld FROM exchangerate e
1285 WHERE e.curr = '$curr'
1286 AND e.transdate = '$transdate'|;
1287 my $sth = $dbh->prepare($query);
1288 $sth->execute || $self->dberror($query);
1290 my ($exchangerate) = $sth->fetchrow_array;
1293 $main::lxdebug->leave_sub();
1295 return $exchangerate;
1298 sub check_exchangerate {
1299 $main::lxdebug->enter_sub();
1301 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1303 unless ($transdate) {
1304 $main::lxdebug->leave_sub();
1308 my $dbh = $self->dbconnect($myconfig);
1310 my $query = qq|SELECT e.$fld FROM exchangerate e
1311 WHERE e.curr = '$currency'
1312 AND e.transdate = '$transdate'|;
1313 my $sth = $dbh->prepare($query);
1314 $sth->execute || $self->dberror($query);
1316 my ($exchangerate) = $sth->fetchrow_array;
1320 $main::lxdebug->leave_sub();
1322 return $exchangerate;
1326 $main::lxdebug->enter_sub();
1328 my ($self, $dbh, $id) = @_;
1332 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1333 if ($self->{"shipto$item"}) {
1334 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1336 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1340 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1341 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1342 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1343 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1344 '$self->{shiptozipcode}', '$self->{shiptocity}',
1345 '$self->{shiptocountry}', '$self->{shiptocontact}',
1346 '$self->{shiptophone}', '$self->{shiptofax}',
1347 '$self->{shiptoemail}')|;
1348 $dbh->do($query) || $self->dberror($query);
1351 $main::lxdebug->leave_sub();
1355 $main::lxdebug->enter_sub();
1357 my ($self, $dbh) = @_;
1359 my $query = qq|SELECT e.id, e.name FROM employee e
1360 WHERE e.login = '$self->{login}'|;
1361 my $sth = $dbh->prepare($query);
1362 $sth->execute || $self->dberror($query);
1364 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1365 $self->{employee_id} *= 1;
1369 $main::lxdebug->leave_sub();
1372 # get other contact for transaction and form - html/tex
1374 $main::lxdebug->enter_sub();
1376 my ($self, $dbh, $id) = @_;
1378 my $query = qq|SELECT c.*
1381 $sth = $dbh->prepare($query);
1382 $sth->execute || $self->dberror($query);
1384 $ref = $sth->fetchrow_hashref(NAME_lc);
1386 push @{ $self->{$_} }, $ref;
1389 $main::lxdebug->leave_sub();
1392 # get contacts for id, if no contact return {"","","","",""}
1394 $main::lxdebug->enter_sub();
1396 my ($self, $dbh, $id) = @_;
1398 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1400 WHERE cp_cv_id=$id|;
1401 my $sth = $dbh->prepare($query);
1402 $sth->execute || $self->dberror($query);
1405 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1406 push @{ $self->{all_contacts} }, $ref;
1411 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1414 $main::lxdebug->leave_sub();
1417 # this sub gets the id and name from $table
1419 $main::lxdebug->enter_sub();
1421 my ($self, $myconfig, $table) = @_;
1423 # connect to database
1424 my $dbh = $self->dbconnect($myconfig);
1426 my $name = $self->like(lc $self->{$table});
1427 my $customernumber = $self->like(lc $self->{customernumber});
1429 if ($self->{customernumber} ne "") {
1430 $query = qq~SELECT c.id, c.name,
1431 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1433 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1436 $query = qq~SELECT c.id, c.name,
1437 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1439 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1443 if ($self->{openinvoices}) {
1444 $query = qq~SELECT DISTINCT c.id, c.name,
1445 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1446 FROM $self->{arap} a
1447 JOIN $table c ON (a.${table}_id = c.id)
1448 WHERE NOT a.amount = a.paid
1449 AND lower(c.name) LIKE '$name'
1452 my $sth = $dbh->prepare($query);
1454 $sth->execute || $self->dberror($query);
1457 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1458 push(@{ $self->{name_list} }, $ref);
1464 $main::lxdebug->leave_sub();
1469 # the selection sub is used in the AR, AP, IS, IR and OE module
1472 $main::lxdebug->enter_sub();
1474 my ($self, $myconfig, $table, $module) = @_;
1477 my $dbh = $self->dbconnect($myconfig);
1479 my $query = qq|SELECT count(*) FROM $table|;
1480 my $sth = $dbh->prepare($query);
1481 $sth->execute || $self->dberror($query);
1482 my ($count) = $sth->fetchrow_array;
1485 # build selection list
1486 if ($count < $myconfig->{vclimit}) {
1487 $query = qq|SELECT id, name
1488 FROM $table WHERE not obsolete
1490 $sth = $dbh->prepare($query);
1491 $sth->execute || $self->dberror($query);
1493 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1494 push @{ $self->{"all_$table"} }, $ref;
1502 $self->get_employee($dbh);
1504 # setup sales contacts
1505 $query = qq|SELECT e.id, e.name
1508 AND NOT e.id = $self->{employee_id}|;
1509 $sth = $dbh->prepare($query);
1510 $sth->execute || $self->dberror($query);
1512 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1513 push @{ $self->{all_employees} }, $ref;
1518 push @{ $self->{all_employees} },
1519 { id => $self->{employee_id},
1520 name => $self->{employee} };
1522 # sort the whole thing
1523 @{ $self->{all_employees} } =
1524 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1526 if ($module eq 'AR') {
1528 # prepare query for departments
1529 $query = qq|SELECT d.id, d.description
1535 $query = qq|SELECT d.id, d.description
1540 $sth = $dbh->prepare($query);
1541 $sth->execute || $self->dberror($query);
1543 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1544 push @{ $self->{all_departments} }, $ref;
1549 $main::lxdebug->leave_sub();
1552 # this is only used for reports
1553 sub all_departments {
1554 $main::lxdebug->enter_sub();
1556 my ($self, $myconfig, $table) = @_;
1558 my $dbh = $self->dbconnect($myconfig);
1559 my $where = "1 = 1";
1561 if (defined $table) {
1562 if ($table eq 'customer') {
1563 $where = " d.role = 'P'";
1567 my $query = qq|SELECT d.id, d.description
1571 my $sth = $dbh->prepare($query);
1572 $sth->execute || $self->dberror($query);
1574 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1575 push @{ $self->{all_departments} }, $ref;
1581 $main::lxdebug->leave_sub();
1585 $main::lxdebug->enter_sub();
1587 my ($self, $module, $myconfig, $table) = @_;
1589 $self->all_vc($myconfig, $table, $module);
1591 # get last customers or vendors
1594 my $dbh = $self->dbconnect($myconfig);
1598 # now get the account numbers
1600 qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1602 WHERE c.link LIKE '%$module%'
1605 $sth = $dbh->prepare($query);
1606 $sth->execute || $self->dberror($query);
1608 $self->{accounts} = "";
1609 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1611 foreach my $key (split /:/, $ref->{link}) {
1612 if ($key =~ /$module/) {
1614 # cross reference for keys
1615 $xkeyref{ $ref->{accno} } = $key;
1617 push @{ $self->{"${module}_links"}{$key} },
1618 { accno => $ref->{accno},
1619 description => $ref->{description},
1620 taxkey => $ref->{taxkey_id} };
1622 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1628 if (($module eq "AP") || ($module eq "AR")) {
1630 # get tax rates and description
1631 $query = qq| SELECT * FROM tax t|;
1632 $sth = $dbh->prepare($query);
1633 $sth->execute || $self->dberror($query);
1635 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1636 push @{ $self->{TAX} }, $ref;
1642 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1644 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1645 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1646 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1647 c.name AS $table, a.department_id, d.description AS department,
1648 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1649 a.employee_id, e.name AS employee, a.gldate
1651 JOIN $table c ON (a.${table}_id = c.id)
1652 LEFT JOIN employee e ON (e.id = a.employee_id)
1653 LEFT JOIN department d ON (d.id = a.department_id)
1654 WHERE a.id = $self->{id}|;
1655 $sth = $dbh->prepare($query);
1656 $sth->execute || $self->dberror($query);
1658 $ref = $sth->fetchrow_hashref(NAME_lc);
1659 foreach $key (keys %$ref) {
1660 $self->{$key} = $ref->{$key};
1664 # get amounts from individual entries
1665 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1666 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1668 JOIN chart c ON (c.id = a.chart_id)
1669 LEFT JOIN project p ON (p.id = a.project_id)
1670 LEFT Join tax t ON (a.taxkey = t.taxkey)
1671 WHERE a.trans_id = $self->{id}
1672 AND a.fx_transaction = '0'
1673 ORDER BY a.transdate|;
1674 $sth = $dbh->prepare($query);
1675 $sth->execute || $self->dberror($query);
1677 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1679 # get exchangerate for currency
1680 $self->{exchangerate} =
1681 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1684 # store amounts in {acc_trans}{$key} for multiple accounts
1685 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1686 $ref->{exchangerate} =
1687 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1690 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1694 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1695 (SELECT c.accno FROM chart c
1696 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1697 (SELECT c.accno FROM chart c
1698 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1700 $sth = $dbh->prepare($query);
1701 $sth->execute || $self->dberror($query);
1703 $ref = $sth->fetchrow_hashref(NAME_lc);
1704 map { $self->{$_} = $ref->{$_} } keys %$ref;
1710 $query = qq|SELECT current_date AS transdate,
1711 d.curr AS currencies, d.closedto, d.revtrans,
1712 (SELECT c.accno FROM chart c
1713 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1714 (SELECT c.accno FROM chart c
1715 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1717 $sth = $dbh->prepare($query);
1718 $sth->execute || $self->dberror($query);
1720 $ref = $sth->fetchrow_hashref(NAME_lc);
1721 map { $self->{$_} = $ref->{$_} } keys %$ref;
1724 if ($self->{"$self->{vc}_id"}) {
1726 # only setup currency
1727 ($self->{currency}) = split /:/, $self->{currencies};
1731 $self->lastname_used($dbh, $myconfig, $table, $module);
1733 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1735 # get exchangerate for currency
1736 $self->{exchangerate} =
1737 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1746 $main::lxdebug->leave_sub();
1750 $main::lxdebug->enter_sub();
1752 my ($self, $dbh, $myconfig, $table, $module) = @_;
1754 my $arap = ($table eq 'customer') ? "ar" : "ap";
1755 my $where = "1 = 1";
1757 if ($self->{type} =~ /_order/) {
1759 $where = "quotation = '0'";
1761 if ($self->{type} =~ /_quotation/) {
1763 $where = "quotation = '1'";
1766 my $query = qq|SELECT id FROM $arap
1767 WHERE id IN (SELECT MAX(id) FROM $arap
1769 AND ${table}_id > 0)|;
1770 my $sth = $dbh->prepare($query);
1771 $sth->execute || $self->dberror($query);
1773 my ($trans_id) = $sth->fetchrow_array;
1777 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1778 current_date + ct.terms AS duedate, a.department_id,
1779 d.description AS department
1781 JOIN $table ct ON (a.${table}_id = ct.id)
1782 LEFT JOIN department d ON (a.department_id = d.id)
1783 WHERE a.id = $trans_id|;
1784 $sth = $dbh->prepare($query);
1785 $sth->execute || $self->dberror($query);
1787 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1788 $self->{duedate}, $self->{department_id}, $self->{department})
1789 = $sth->fetchrow_array;
1792 $main::lxdebug->leave_sub();
1796 $main::lxdebug->enter_sub();
1798 my ($self, $myconfig, $thisdate, $days) = @_;
1800 my $dbh = $self->dbconnect($myconfig);
1805 my $dateformat = $myconfig->{dateformat};
1806 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1808 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1810 $sth = $dbh->prepare($query);
1811 $sth->execute || $self->dberror($query);
1813 $query = qq|SELECT current_date AS thisdate
1815 $sth = $dbh->prepare($query);
1816 $sth->execute || $self->dberror($query);
1819 ($thisdate) = $sth->fetchrow_array;
1824 $main::lxdebug->leave_sub();
1830 $main::lxdebug->enter_sub();
1832 my ($self, $string) = @_;
1834 if ($string !~ /%/) {
1835 $string = "%$string%";
1838 $string =~ s/\'/\'\'/g;
1840 $main::lxdebug->leave_sub();
1846 $main::lxdebug->enter_sub();
1848 my ($self, $flds, $new, $count, $numrows) = @_;
1852 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1858 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1860 $j = $item->{ndx} - 1;
1861 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1865 for $i ($count + 1 .. $numrows) {
1866 map { delete $self->{"${_}_$i"} } @{$flds};
1869 $main::lxdebug->leave_sub();
1873 $main::lxdebug->enter_sub();
1875 my ($self, $myconfig) = @_;
1879 my $dbh = $self->dbconnect_noauto($myconfig);
1881 my $query = qq|DELETE FROM status
1882 WHERE formname = '$self->{formname}'
1884 my $sth = $dbh->prepare($query) || $self->dberror($query);
1886 if ($self->{formname} =~ /(check|receipt)/) {
1887 for $i (1 .. $self->{rowcount}) {
1888 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1892 $sth->execute($self->{id}) || $self->dberror($query);
1896 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1897 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1899 my %queued = split / /, $self->{queued};
1901 if ($self->{formname} =~ /(check|receipt)/) {
1903 # this is a check or receipt, add one entry for each lineitem
1904 my ($accno) = split /--/, $self->{account};
1905 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1906 chart_id) VALUES (?, '$printed',
1907 '$queued{$self->{formname}}', '$self->{prinform}',
1908 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1909 $sth = $dbh->prepare($query) || $self->dberror($query);
1911 for $i (1 .. $self->{rowcount}) {
1912 if ($self->{"checked_$i"}) {
1913 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1918 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1919 spoolfile, formname)
1920 VALUES ($self->{id}, '$printed', '$emailed',
1921 '$queued{$self->{formname}}', '$self->{formname}')|;
1922 $dbh->do($query) || $self->dberror($query);
1928 $main::lxdebug->leave_sub();
1932 $main::lxdebug->enter_sub();
1934 my ($self, $dbh) = @_;
1936 my ($query, $printed, $emailed);
1938 my $formnames = $self->{printed};
1939 my $emailforms = $self->{emailed};
1941 my $query = qq|DELETE FROM status
1942 WHERE formname = '$self->{formname}'
1943 AND trans_id = $self->{id}|;
1944 $dbh->do($query) || $self->dberror($query);
1946 # this only applies to the forms
1947 # checks and receipts are posted when printed or queued
1949 if ($self->{queued}) {
1950 my %queued = split / /, $self->{queued};
1952 foreach my $formname (keys %queued) {
1953 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1954 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1956 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1957 spoolfile, formname)
1958 VALUES ($self->{id}, '$printed', '$emailed',
1959 '$queued{$formname}', '$formname')|;
1960 $dbh->do($query) || $self->dberror($query);
1962 $formnames =~ s/$self->{formname}//;
1963 $emailforms =~ s/$self->{formname}//;
1968 # save printed, emailed info
1969 $formnames =~ s/^ +//g;
1970 $emailforms =~ s/^ +//g;
1973 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1974 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
1976 foreach my $formname (keys %status) {
1977 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
1978 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
1980 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
1981 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
1982 $dbh->do($query) || $self->dberror($query);
1985 $main::lxdebug->leave_sub();
1988 sub update_defaults {
1989 $main::lxdebug->enter_sub();
1991 my ($self, $myconfig, $fld) = @_;
1993 my $dbh = $self->dbconnect_noauto($myconfig);
1994 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
1995 my $sth = $dbh->prepare($query);
1997 $sth->execute || $self->dberror($query);
1998 my ($var) = $sth->fetchrow_array;
2003 $query = qq|UPDATE defaults
2005 $dbh->do($query) || $form->dberror($query);
2010 $main::lxdebug->leave_sub();
2015 sub update_business {
2016 $main::lxdebug->enter_sub();
2018 my ($self, $myconfig, $business_id) = @_;
2020 my $dbh = $self->dbconnect_noauto($myconfig);
2022 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2023 my $sth = $dbh->prepare($query);
2025 $sth->execute || $self->dberror($query);
2026 my ($var) = $sth->fetchrow_array;
2031 $query = qq|UPDATE business
2032 SET customernumberinit = '$var' WHERE id=$business_id|;
2033 $dbh->do($query) || $form->dberror($query);
2038 $main::lxdebug->leave_sub();
2044 $main::lxdebug->enter_sub();
2046 my ($self, $myconfig, $salesman) = @_;
2048 my $dbh = $self->dbconnect($myconfig);
2050 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2051 my $sth = $dbh->prepare($query);
2052 $sth->execute || $self->dberror($query);
2055 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2056 push(@{ $self->{salesman_list} }, $ref);
2060 $main::lxdebug->leave_sub();
2065 sub get_partsgroup {
2066 $main::lxdebug->enter_sub();
2068 my ($self, $myconfig, $p) = @_;
2070 my $dbh = $self->dbconnect($myconfig);
2072 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2074 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2076 if ($p->{searchitems} eq 'part') {
2078 WHERE p.inventory_accno_id > 0|;
2080 if ($p->{searchitems} eq 'service') {
2082 WHERE p.inventory_accno_id IS NULL|;
2084 if ($p->{searchitems} eq 'assembly') {
2086 WHERE p.assembly = '1'|;
2088 if ($p->{searchitems} eq 'labor') {
2090 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2094 ORDER BY partsgroup|;
2097 $query = qq|SELECT id, partsgroup FROM partsgroup
2098 ORDER BY partsgroup|;
2101 if ($p->{language_code}) {
2102 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2103 t.description AS translation
2105 JOIN parts p ON (p.partsgroup_id = pg.id)
2106 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2107 ORDER BY translation|;
2110 my $sth = $dbh->prepare($query);
2111 $sth->execute || $self->dberror($query);
2113 $self->{all_partsgroup} = ();
2114 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2115 push @{ $self->{all_partsgroup} }, $ref;
2119 $main::lxdebug->leave_sub();
2123 sub get_pricegroup {
2124 $main::lxdebug->enter_sub();
2126 my ($self, $myconfig, $p) = @_;
2128 my $dbh = $self->dbconnect($myconfig);
2130 my $query = qq|SELECT p.id, p.pricegroup
2134 ORDER BY pricegroup|;
2137 $query = qq|SELECT id, pricegroup FROM pricegroup
2138 ORDER BY pricegroup|;
2141 my $sth = $dbh->prepare($query);
2142 $sth->execute || $self->dberror($query);
2144 $self->{all_pricegroup} = ();
2145 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2146 push @{ $self->{all_pricegroup} }, $ref;
2151 $main::lxdebug->leave_sub();
2156 my ($self, $dbh, $myconfig, $audittrail) = @_;
2158 # table, $reference, $formname, $action, $id, $transdate) = @_;
2165 $dbh = $self->dbconnect($myconfig);
2169 # if we have an id add audittrail, otherwise get a new timestamp
2171 if ($audittrail->{id}) {
2173 $query = qq|SELECT audittrail FROM defaults|;
2175 if ($dbh->selectrow_array($query)) {
2176 my ($null, $employee_id) = $self->get_employee($dbh);
2178 if ($self->{audittrail} && !$myconfig) {
2179 chop $self->{audittrail};
2181 my @a = split /\|/, $self->{audittrail};
2185 my @flds = qw(tablename reference formname action transdate);
2187 # put into hash and remove dups
2189 $key = "$a[2]$a[3]";
2191 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2195 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2196 formname, action, employee_id, transdate)
2197 VALUES ($audittrail->{id}, ?, ?,
2198 ?, ?, $employee_id, ?)|;
2199 my $sth = $dbh->prepare($query) || $self->dberror($query);
2201 foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
2203 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2205 $sth->execute || $self->dberror;
2211 if ($audittrail->{transdate}) {
2212 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2213 formname, action, employee_id, transdate) VALUES (
2214 $audittrail->{id}, '$audittrail->{tablename}', |
2215 .$dbh->quote($audittrail->{reference}).qq|',
2216 '$audittrail->{formname}', '$audittrail->{action}',
2217 $employee_id, '$audittrail->{transdate}')|;
2219 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2220 formname, action, employee_id) VALUES ($audittrail->{id},
2221 '$audittrail->{tablename}', |
2222 .$dbh->quote($audittrail->{reference}).qq|,
2223 '$audittrail->{formname}', '$audittrail->{action}',
2230 $query = qq|SELECT current_timestamp FROM defaults|;
2231 my ($timestamp) = $dbh->selectrow_array($query);
2233 $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2236 $dbh->disconnect if $disconnect;
2245 $main::lxdebug->enter_sub();
2247 my ($type, $country, $NLS_file) = @_;
2251 if ($country && -d "locale/$country") {
2252 $self->{countrycode} = $country;
2253 eval { require "locale/$country/$NLS_file"; };
2256 $self->{NLS_file} = $NLS_file;
2258 push @{ $self->{LONG_MONTH} },
2259 ("January", "February", "March", "April",
2260 "May ", "June", "July", "August",
2261 "September", "October", "November", "December");
2262 push @{ $self->{SHORT_MONTH} },
2263 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2265 $main::lxdebug->leave_sub();
2271 my ($self, $text) = @_;
2273 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2277 $main::lxdebug->enter_sub();
2279 my ($self, $text) = @_;
2281 if (exists $self{subs}{$text}) {
2282 $text = $self{subs}{$text};
2284 if ($self->{countrycode} && $self->{NLS_file}) {
2286 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2290 $main::lxdebug->leave_sub();
2296 $main::lxdebug->enter_sub();
2298 my ($self, $myconfig, $date, $longformat) = @_;
2301 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2306 $spc = $myconfig->{dateformat};
2308 $spc = substr($spc, 1, 1);
2310 if ($date =~ /\D/) {
2311 if ($myconfig->{dateformat} =~ /^yy/) {
2312 ($yy, $mm, $dd) = split /\D/, $date;
2314 if ($myconfig->{dateformat} =~ /^mm/) {
2315 ($mm, $dd, $yy) = split /\D/, $date;
2317 if ($myconfig->{dateformat} =~ /^dd/) {
2318 ($dd, $mm, $yy) = split /\D/, $date;
2321 $date = substr($date, 2);
2322 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2327 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2328 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2330 if ($myconfig->{dateformat} =~ /^dd/) {
2331 if (defined $longformat && $longformat == 0) {
2333 $dd = "0$dd" if ($dd < 10);
2334 $mm = "0$mm" if ($mm < 10);
2335 $longdate = "$dd$spc$mm$spc$yy";
2338 $longdate .= ($spc eq '.') ? ". " : " ";
2339 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2341 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2343 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2344 # Lx-Office is mainly used in Germany or German speaking countries.
2345 if (defined $longformat && $longformat == 0) {
2347 $dd = "0$dd" if ($dd < 10);
2348 $mm = "0$mm" if ($mm < 10);
2349 $longdate = "$yy-$mm-$dd";
2351 $longdate = "$dd. ";
2352 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2355 if (defined $longformat && $longformat == 0) {
2357 $dd = "0$dd" if ($dd < 10);
2358 $mm = "0$mm" if ($mm < 10);
2359 $longdate = "$mm$spc$dd$spc$yy";
2361 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2367 $main::lxdebug->leave_sub();
2373 $main::lxdebug->enter_sub();
2375 my ($self, $myconfig, $date, $longformat) = @_;
2378 $main::lxdebug->leave_sub();
2383 $spc = $myconfig->{dateformat};
2385 $spc = substr($spc, 1, 1);
2387 if ($date =~ /\D/) {
2388 if ($myconfig->{dateformat} =~ /^yy/) {
2389 ($yy, $mm, $dd) = split /\D/, $date;
2390 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2391 ($mm, $dd, $yy) = split /\D/, $date;
2392 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2393 ($dd, $mm, $yy) = split /\D/, $date;
2396 $date = substr($date, 2);
2397 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2402 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2403 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2405 $main::lxdebug->leave_sub();
2406 return ($yy, $mm, $dd);