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",
456    align    : "$align_1",    Â
457    button      : "$button_1"
466    inputField  : "$inputField_2",
467    ifFormat    :"$ifFormat",
468    align    : "$align_2",    Â
469    button      : "$button_2"
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;
546 if ($myconfig->{numberformat} eq '1.000,00') {
547 $amount =~ s/\d{3,}?/$&./g;
549 $amount = join '', reverse split //, $amount;
550 $amount .= ",$dec" .$fillup;
553 if ($myconfig->{numberformat} eq '1000,00') {
555 $amount .= ",$dec" .$fillup;
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 # $places = 3 if $places == 2;
611 if (($places * 1) >= 0) {
615 sprintf("%.${places}f",
616 $amount + (1 / (10**($places + 3))) * (($amount > 0) ? 1 : -1));
620 sprintf("%.f", $amount / (10**$places) + (($amount > 0) ? 0.1 : -0.1)) *
624 $main::lxdebug->leave_sub();
632 $main::lxdebug->enter_sub();
634 my ($self, $myconfig, $userspath) = @_;
637 # Some variables used for page breaks
638 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
640 my ($current_page, $current_line, $current_row) = (1, 1, 0);
646 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
647 $self->format_string(grep(/notes/, keys(%{$self})));
649 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
650 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
652 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
653 qw(email tel fax name signature));
655 open(IN, "$self->{templates}/$self->{IN}")
656 or $self->error("$self->{IN} : $!");
661 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
663 # OUT is used for the media, screen, printer, email
664 # for postscript we store a copy in a temporary file
666 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
667 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
669 $self->{OUT} = ">$self->{tmpfile}";
673 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
675 open(OUT, ">-") or $self->error("STDOUT : $!");
679 # Do we have to run LaTeX two times? This is needed if
680 # the template contains page references.
683 # first we generate a tmpfile
684 # read file and replace <%variable%>
690 $two_passes = 1 if (/\\pageref/);
693 # detect pagebreak block and its parameters
694 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
695 $chars_per_line = $1;
696 $lines_on_first_page = $2;
697 $lines_on_second_page = $3;
700 last if (/\s*<%end pagebreak%>/);
707 if (/\s*<%foreach /) {
709 # this one we need for the count
711 $var =~ s/\s*<%foreach (.+?)%>/$1/;
713 last if (/\s*<%end /);
719 # display contents of $self->{number}[] array
720 for $i (0 .. $#{ $self->{$var} }) {
723 # Try to detect whether a manual page break is necessary
724 # but only if there was a <%pagebreak ...%> block before
726 if ($chars_per_line) {
728 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
731 my $_description = $self->{"description"}[$i];
732 while ($_description =~ /\\newline/) {
734 $_description =~ s/\\newline//;
736 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
738 if ($current_page == 1) {
739 $lpp = $lines_on_first_page;
741 $lpp = $lines_on_second_page;
744 # Yes we need a manual page break -- or the user has forced one
745 if ((($current_line + $lines) > $lpp) ||
746 ($self->{"_forced_pagebreaks"} && grep(/^${current_row}$/, @{$self->{"_forced_pagebreaks"}}))) {
749 # replace the special variables <%sumcarriedforward%>
752 my $psum = $self->format_amount($myconfig, $sum, 2);
753 $pb =~ s/<%sumcarriedforward%>/$psum/g;
754 $pb =~ s/<%lastpage%>/$current_page/g;
756 # only "normal" variables are supported here
757 # (no <%if, no <%foreach, no <%include)
759 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
761 # page break block is ready to rock
766 $current_line += $lines;
769 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
773 # don't parse par, we need it for each line
775 s/<%(.+?)%>/$self->{$1}[$i]/mg;
781 # if not comes before if!
782 if (/\s*<%if not /) {
784 # check if it is not set and display
786 s/\s*<%if not (.+?)%>/$1/;
788 unless ($self->{$_}) {
790 last if (/\s*<%end /);
800 last if (/\s*<%end /);
808 # check if it is set and display
810 s/\s*<%if (.+?)%>/$1/;
814 last if (/\s*<%end /);
824 last if (/\s*<%end /);
830 # check for <%include filename%>
831 if (/\s*<%include /) {
835 $var =~ s/\s*<%include (.+?)%>/$1/;
838 $var =~ s/(\/|\.\.)//g;
840 # prevent the infinite loop!
841 next if ($self->{"$var"});
843 open(INC, "$self->{templates}/$var")
844 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
853 s/<%(.+?)%>/$self->{$1}/g;
860 # Convert the tex file to postscript
861 if ($self->{format} =~ /(postscript|pdf)/) {
864 $self->{cwd} = cwd();
865 $self->{tmpdir} = "$self->{cwd}/$userspath";
867 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
869 $self->{tmpfile} =~ s/$userspath\///g;
871 if ($self->{format} eq 'postscript') {
873 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
875 $self->error($self->cleanup) if ($?);
878 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
880 $self->error($self->cleanup) if ($?);
883 $self->{tmpfile} =~ s/tex$/dvi/;
885 system("dvips $self->{tmpfile} -o -q > /dev/null");
886 $self->error($self->cleanup . "dvips : $!") if ($?);
887 $self->{tmpfile} =~ s/dvi$/ps/;
889 if ($self->{format} eq 'pdf') {
891 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
893 $self->error($self->cleanup) if ($?);
896 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
898 $self->error($self->cleanup) if ($?);
900 $self->{tmpfile} =~ s/tex$/pdf/;
905 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
907 if ($self->{media} eq 'email') {
911 my $mail = new Mailer;
913 map { $mail->{$_} = $self->{$_} }
914 qw(cc bcc subject message version format charset);
915 $mail->{to} = qq|$self->{email}|;
916 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
917 $mail->{fileid} = "$fileid.";
919 # if we send html or plain text inline
920 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
921 $mail->{contenttype} = "text/html";
923 $mail->{message} =~ s/\r\n/<br>\n/g;
924 $myconfig->{signature} =~ s/\\n/<br>\n/g;
925 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
927 open(IN, $self->{tmpfile})
928 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
930 $mail->{message} .= $_;
937 @{ $mail->{attachments} } = ($self->{tmpfile});
939 $myconfig->{signature} =~ s/\\n/\r\n/g;
940 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
944 my $err = $mail->send($out);
945 $self->error($self->cleanup . "$err") if ($err);
951 my $numbytes = (-s $self->{tmpfile});
952 open(IN, $self->{tmpfile})
953 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
955 $self->{copies} = 1 unless $self->{media} eq 'printer';
957 chdir("$self->{cwd}");
959 for my $i (1 .. $self->{copies}) {
961 open(OUT, $self->{OUT})
962 or $self->error($self->cleanup . "$self->{OUT} : $!");
966 print qq|Content-Type: application/$self->{format}
967 Content-Disposition: attachment; filename="$self->{tmpfile}"
968 Content-Length: $numbytes
972 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
992 chdir("$self->{cwd}");
993 $main::lxdebug->leave_sub();
997 $main::lxdebug->enter_sub();
1001 chdir("$self->{tmpdir}");
1004 if (-f "$self->{tmpfile}.err") {
1005 open(FH, "$self->{tmpfile}.err");
1010 if ($self->{tmpfile}) {
1013 $self->{tmpfile} =~ s/\.\w+$//g;
1014 my $tmpfile = $self->{tmpfile};
1015 unlink(<$tmpfile.*>);
1018 chdir("$self->{cwd}");
1020 $main::lxdebug->leave_sub();
1026 $main::lxdebug->enter_sub();
1028 my ($self, @fields) = @_;
1031 %unique_fields = map({ $_ => 1 } @fields);
1032 @fields = keys(%unique_fields);
1034 foreach my $field (@fields) {
1035 next unless ($self->{$field} =~ /\<pagebreak\>/);
1036 $self->{$field} =~ s/\<pagebreak\>//g;
1037 if ($field =~ /.*_(\d+)$/) {
1038 if ($self->{"_forced_pagebreaks"}) {
1039 $self->{"_forced_pagebreaks"} = [];
1041 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1045 my $format = $self->{format};
1046 if ($self->{format} =~ /(postscript|pdf)/) {
1053 '<', '>', quotemeta('\n'), '
1057 '&', quotemeta('\n'), '
1059 '"', '\$', '%', '_', '#', quotemeta('^'),
1060 '{', '}', '<', '>', '£', "\r"
1066 quotemeta('\n') => '<br>',
1077 quotemeta('^') => '\^\\',
1082 quotemeta('\n') => '\newline ',
1089 foreach my $key (@{ $replace{order}{$format} }) {
1090 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1093 # Allow some HTML markup to be converted into the output format's
1094 # corresponding markup code, e.g. bold or italic.
1095 if ('html' eq $format) {
1096 my @markup_replace = ('b', 'i', 's', 'u');
1098 foreach my $key (@markup_replace) {
1099 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1102 } elsif ('tex' eq $format) {
1103 my %markup_replace = ('b' => 'textbf',
1105 'u' => 'underline');
1107 foreach my $field (@fields) {
1108 if ($field =~ /descrip/) {
1109 print(STDERR "QFT: ${field}: " . $self->{$field} . "\n");
1111 foreach my $key (keys(%markup_replace)) {
1112 my $new = $markup_replace{$key};
1114 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1119 $main::lxdebug->leave_sub();
1123 $main::lxdebug->enter_sub();
1125 my ($self, $date, $myconfig) = @_;
1127 if ($date && $date =~ /\D/) {
1129 if ($myconfig->{dateformat} =~ /^yy/) {
1130 ($yy, $mm, $dd) = split /\D/, $date;
1132 if ($myconfig->{dateformat} =~ /^mm/) {
1133 ($mm, $dd, $yy) = split /\D/, $date;
1135 if ($myconfig->{dateformat} =~ /^dd/) {
1136 ($dd, $mm, $yy) = split /\D/, $date;
1141 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1142 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1144 $dd = "0$dd" if ($dd < 10);
1145 $mm = "0$mm" if ($mm < 10);
1147 $date = "$yy$mm$dd";
1150 $main::lxdebug->leave_sub();
1155 # Database routines used throughout
1158 $main::lxdebug->enter_sub();
1160 my ($self, $myconfig) = @_;
1162 # connect to database
1164 DBI->connect($myconfig->{dbconnect},
1165 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1169 if ($myconfig->{dboptions}) {
1170 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1173 $main::lxdebug->leave_sub();
1178 sub dbconnect_noauto {
1179 $main::lxdebug->enter_sub();
1181 my ($self, $myconfig) = @_;
1183 # connect to database
1185 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1186 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1190 if ($myconfig->{dboptions}) {
1191 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1194 $main::lxdebug->leave_sub();
1199 sub update_balance {
1200 $main::lxdebug->enter_sub();
1202 my ($self, $dbh, $table, $field, $where, $value) = @_;
1204 # if we have a value, go do it
1207 # retrieve balance from table
1208 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1209 my $sth = $dbh->prepare($query);
1211 $sth->execute || $self->dberror($query);
1212 my ($balance) = $sth->fetchrow_array;
1218 $query = "UPDATE $table SET $field = $balance WHERE $where";
1219 $dbh->do($query) || $self->dberror($query);
1221 $main::lxdebug->leave_sub();
1224 sub update_exchangerate {
1225 $main::lxdebug->enter_sub();
1227 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1229 # some sanity check for currency
1231 $main::lxdebug->leave_sub();
1235 my $query = qq|SELECT e.curr FROM exchangerate e
1236 WHERE e.curr = '$curr'
1237 AND e.transdate = '$transdate'
1239 my $sth = $dbh->prepare($query);
1240 $sth->execute || $self->dberror($query);
1243 if ($buy != 0 && $sell != 0) {
1244 $set = "buy = $buy, sell = $sell";
1245 } elsif ($buy != 0) {
1246 $set = "buy = $buy";
1247 } elsif ($sell != 0) {
1248 $set = "sell = $sell";
1251 if ($sth->fetchrow_array) {
1252 $query = qq|UPDATE exchangerate
1254 WHERE curr = '$curr'
1255 AND transdate = '$transdate'|;
1257 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1258 VALUES ('$curr', $buy, $sell, '$transdate')|;
1261 $dbh->do($query) || $self->dberror($query);
1263 $main::lxdebug->leave_sub();
1266 sub save_exchangerate {
1267 $main::lxdebug->enter_sub();
1269 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1271 my $dbh = $self->dbconnect($myconfig);
1273 my ($buy, $sell) = (0, 0);
1274 $buy = $rate if $fld eq 'buy';
1275 $sell = $rate if $fld eq 'sell';
1277 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1281 $main::lxdebug->leave_sub();
1284 sub get_exchangerate {
1285 $main::lxdebug->enter_sub();
1287 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1289 my $query = qq|SELECT e.$fld FROM exchangerate e
1290 WHERE e.curr = '$curr'
1291 AND e.transdate = '$transdate'|;
1292 my $sth = $dbh->prepare($query);
1293 $sth->execute || $self->dberror($query);
1295 my ($exchangerate) = $sth->fetchrow_array;
1298 $main::lxdebug->leave_sub();
1300 return $exchangerate;
1303 sub check_exchangerate {
1304 $main::lxdebug->enter_sub();
1306 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1308 unless ($transdate) {
1309 $main::lxdebug->leave_sub();
1313 my $dbh = $self->dbconnect($myconfig);
1315 my $query = qq|SELECT e.$fld FROM exchangerate e
1316 WHERE e.curr = '$currency'
1317 AND e.transdate = '$transdate'|;
1318 my $sth = $dbh->prepare($query);
1319 $sth->execute || $self->dberror($query);
1321 my ($exchangerate) = $sth->fetchrow_array;
1325 $main::lxdebug->leave_sub();
1327 return $exchangerate;
1331 $main::lxdebug->enter_sub();
1333 my ($self, $dbh, $id) = @_;
1337 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1338 if ($self->{"shipto$item"}) {
1339 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1341 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1345 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1346 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1347 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1348 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1349 '$self->{shiptozipcode}', '$self->{shiptocity}',
1350 '$self->{shiptocountry}', '$self->{shiptocontact}',
1351 '$self->{shiptophone}', '$self->{shiptofax}',
1352 '$self->{shiptoemail}')|;
1353 $dbh->do($query) || $self->dberror($query);
1356 $main::lxdebug->leave_sub();
1360 $main::lxdebug->enter_sub();
1362 my ($self, $dbh) = @_;
1364 my $query = qq|SELECT e.id, e.name FROM employee e
1365 WHERE e.login = '$self->{login}'|;
1366 my $sth = $dbh->prepare($query);
1367 $sth->execute || $self->dberror($query);
1369 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1370 $self->{employee_id} *= 1;
1374 $main::lxdebug->leave_sub();
1377 # get other contact for transaction and form - html/tex
1379 $main::lxdebug->enter_sub();
1381 my ($self, $dbh, $id) = @_;
1383 my $query = qq|SELECT c.*
1386 $sth = $dbh->prepare($query);
1387 $sth->execute || $self->dberror($query);
1389 $ref = $sth->fetchrow_hashref(NAME_lc);
1391 push @{ $self->{$_} }, $ref;
1394 $main::lxdebug->leave_sub();
1397 # get contacts for id, if no contact return {"","","","",""}
1399 $main::lxdebug->enter_sub();
1401 my ($self, $dbh, $id) = @_;
1403 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1405 WHERE cp_cv_id=$id|;
1406 my $sth = $dbh->prepare($query);
1407 $sth->execute || $self->dberror($query);
1410 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1411 push @{ $self->{all_contacts} }, $ref;
1416 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1419 $main::lxdebug->leave_sub();
1422 # this sub gets the id and name from $table
1424 $main::lxdebug->enter_sub();
1426 my ($self, $myconfig, $table) = @_;
1428 # connect to database
1429 my $dbh = $self->dbconnect($myconfig);
1431 my $name = $self->like(lc $self->{$table});
1432 my $customernumber = $self->like(lc $self->{customernumber});
1434 if ($self->{customernumber} ne "") {
1435 $query = qq~SELECT c.id, c.name,
1436 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1438 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1441 $query = qq~SELECT c.id, c.name,
1442 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1444 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1448 if ($self->{openinvoices}) {
1449 $query = qq~SELECT DISTINCT c.id, c.name,
1450 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1451 FROM $self->{arap} a
1452 JOIN $table c ON (a.${table}_id = c.id)
1453 WHERE NOT a.amount = a.paid
1454 AND lower(c.name) LIKE '$name'
1457 my $sth = $dbh->prepare($query);
1459 $sth->execute || $self->dberror($query);
1462 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1463 push(@{ $self->{name_list} }, $ref);
1469 $main::lxdebug->leave_sub();
1474 # the selection sub is used in the AR, AP, IS, IR and OE module
1477 $main::lxdebug->enter_sub();
1479 my ($self, $myconfig, $table, $module) = @_;
1482 my $dbh = $self->dbconnect($myconfig);
1484 my $query = qq|SELECT count(*) FROM $table|;
1485 my $sth = $dbh->prepare($query);
1486 $sth->execute || $self->dberror($query);
1487 my ($count) = $sth->fetchrow_array;
1490 # build selection list
1491 if ($count < $myconfig->{vclimit}) {
1492 $query = qq|SELECT id, name
1493 FROM $table WHERE not obsolete
1495 $sth = $dbh->prepare($query);
1496 $sth->execute || $self->dberror($query);
1498 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1499 push @{ $self->{"all_$table"} }, $ref;
1507 $self->get_employee($dbh);
1509 # setup sales contacts
1510 $query = qq|SELECT e.id, e.name
1513 AND NOT e.id = $self->{employee_id}|;
1514 $sth = $dbh->prepare($query);
1515 $sth->execute || $self->dberror($query);
1517 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1518 push @{ $self->{all_employees} }, $ref;
1523 push @{ $self->{all_employees} },
1524 { id => $self->{employee_id},
1525 name => $self->{employee} };
1527 # sort the whole thing
1528 @{ $self->{all_employees} } =
1529 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1531 if ($module eq 'AR') {
1533 # prepare query for departments
1534 $query = qq|SELECT d.id, d.description
1540 $query = qq|SELECT d.id, d.description
1545 $sth = $dbh->prepare($query);
1546 $sth->execute || $self->dberror($query);
1548 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1549 push @{ $self->{all_departments} }, $ref;
1554 $main::lxdebug->leave_sub();
1557 # this is only used for reports
1558 sub all_departments {
1559 $main::lxdebug->enter_sub();
1561 my ($self, $myconfig, $table) = @_;
1563 my $dbh = $self->dbconnect($myconfig);
1564 my $where = "1 = 1";
1566 if (defined $table) {
1567 if ($table eq 'customer') {
1568 $where = " d.role = 'P'";
1572 my $query = qq|SELECT d.id, d.description
1576 my $sth = $dbh->prepare($query);
1577 $sth->execute || $self->dberror($query);
1579 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1580 push @{ $self->{all_departments} }, $ref;
1586 $main::lxdebug->leave_sub();
1590 $main::lxdebug->enter_sub();
1592 my ($self, $module, $myconfig, $table) = @_;
1594 $self->all_vc($myconfig, $table, $module);
1596 # get last customers or vendors
1599 my $dbh = $self->dbconnect($myconfig);
1603 # now get the account numbers
1605 qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1607 WHERE c.link LIKE '%$module%'
1610 $sth = $dbh->prepare($query);
1611 $sth->execute || $self->dberror($query);
1613 $self->{accounts} = "";
1614 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1616 foreach my $key (split /:/, $ref->{link}) {
1617 if ($key =~ /$module/) {
1619 # cross reference for keys
1620 $xkeyref{ $ref->{accno} } = $key;
1622 push @{ $self->{"${module}_links"}{$key} },
1623 { accno => $ref->{accno},
1624 description => $ref->{description},
1625 taxkey => $ref->{taxkey_id} };
1627 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1633 if (($module eq "AP") || ($module eq "AR")) {
1635 # get tax rates and description
1636 $query = qq| SELECT * FROM tax t|;
1637 $sth = $dbh->prepare($query);
1638 $sth->execute || $self->dberror($query);
1640 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1641 push @{ $self->{TAX} }, $ref;
1647 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1649 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1650 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1651 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1652 c.name AS $table, a.department_id, d.description AS department,
1653 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1654 a.employee_id, e.name AS employee, a.gldate
1656 JOIN $table c ON (a.${table}_id = c.id)
1657 LEFT JOIN employee e ON (e.id = a.employee_id)
1658 LEFT JOIN department d ON (d.id = a.department_id)
1659 WHERE a.id = $self->{id}|;
1660 $sth = $dbh->prepare($query);
1661 $sth->execute || $self->dberror($query);
1663 $ref = $sth->fetchrow_hashref(NAME_lc);
1664 foreach $key (keys %$ref) {
1665 $self->{$key} = $ref->{$key};
1669 # get amounts from individual entries
1670 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1671 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1673 JOIN chart c ON (c.id = a.chart_id)
1674 LEFT JOIN project p ON (p.id = a.project_id)
1675 LEFT Join tax t ON (a.taxkey = t.taxkey)
1676 WHERE a.trans_id = $self->{id}
1677 AND a.fx_transaction = '0'
1678 ORDER BY a.transdate|;
1679 $sth = $dbh->prepare($query);
1680 $sth->execute || $self->dberror($query);
1682 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1684 # get exchangerate for currency
1685 $self->{exchangerate} =
1686 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1689 # store amounts in {acc_trans}{$key} for multiple accounts
1690 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1691 $ref->{exchangerate} =
1692 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1695 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1699 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1700 (SELECT c.accno FROM chart c
1701 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1702 (SELECT c.accno FROM chart c
1703 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1705 $sth = $dbh->prepare($query);
1706 $sth->execute || $self->dberror($query);
1708 $ref = $sth->fetchrow_hashref(NAME_lc);
1709 map { $self->{$_} = $ref->{$_} } keys %$ref;
1715 $query = qq|SELECT current_date AS transdate,
1716 d.curr AS currencies, d.closedto, d.revtrans,
1717 (SELECT c.accno FROM chart c
1718 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1719 (SELECT c.accno FROM chart c
1720 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1722 $sth = $dbh->prepare($query);
1723 $sth->execute || $self->dberror($query);
1725 $ref = $sth->fetchrow_hashref(NAME_lc);
1726 map { $self->{$_} = $ref->{$_} } keys %$ref;
1729 if ($self->{"$self->{vc}_id"}) {
1731 # only setup currency
1732 ($self->{currency}) = split /:/, $self->{currencies};
1736 $self->lastname_used($dbh, $myconfig, $table, $module);
1738 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1740 # get exchangerate for currency
1741 $self->{exchangerate} =
1742 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1751 $main::lxdebug->leave_sub();
1755 $main::lxdebug->enter_sub();
1757 my ($self, $dbh, $myconfig, $table, $module) = @_;
1759 my $arap = ($table eq 'customer') ? "ar" : "ap";
1760 my $where = "1 = 1";
1762 if ($self->{type} =~ /_order/) {
1764 $where = "quotation = '0'";
1766 if ($self->{type} =~ /_quotation/) {
1768 $where = "quotation = '1'";
1771 my $query = qq|SELECT id FROM $arap
1772 WHERE id IN (SELECT MAX(id) FROM $arap
1774 AND ${table}_id > 0)|;
1775 my $sth = $dbh->prepare($query);
1776 $sth->execute || $self->dberror($query);
1778 my ($trans_id) = $sth->fetchrow_array;
1782 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1783 current_date + ct.terms AS duedate, a.department_id,
1784 d.description AS department
1786 JOIN $table ct ON (a.${table}_id = ct.id)
1787 LEFT JOIN department d ON (a.department_id = d.id)
1788 WHERE a.id = $trans_id|;
1789 $sth = $dbh->prepare($query);
1790 $sth->execute || $self->dberror($query);
1792 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1793 $self->{duedate}, $self->{department_id}, $self->{department})
1794 = $sth->fetchrow_array;
1797 $main::lxdebug->leave_sub();
1801 $main::lxdebug->enter_sub();
1803 my ($self, $myconfig, $thisdate, $days) = @_;
1805 my $dbh = $self->dbconnect($myconfig);
1810 my $dateformat = $myconfig->{dateformat};
1811 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1813 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1815 $sth = $dbh->prepare($query);
1816 $sth->execute || $self->dberror($query);
1818 $query = qq|SELECT current_date AS thisdate
1820 $sth = $dbh->prepare($query);
1821 $sth->execute || $self->dberror($query);
1824 ($thisdate) = $sth->fetchrow_array;
1829 $main::lxdebug->leave_sub();
1835 $main::lxdebug->enter_sub();
1837 my ($self, $string) = @_;
1839 if ($string !~ /%/) {
1840 $string = "%$string%";
1843 $string =~ s/\'/\'\'/g;
1845 $main::lxdebug->leave_sub();
1851 $main::lxdebug->enter_sub();
1853 my ($self, $flds, $new, $count, $numrows) = @_;
1857 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1863 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1865 $j = $item->{ndx} - 1;
1866 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1870 for $i ($count + 1 .. $numrows) {
1871 map { delete $self->{"${_}_$i"} } @{$flds};
1874 $main::lxdebug->leave_sub();
1878 $main::lxdebug->enter_sub();
1880 my ($self, $myconfig) = @_;
1884 my $dbh = $self->dbconnect_noauto($myconfig);
1886 my $query = qq|DELETE FROM status
1887 WHERE formname = '$self->{formname}'
1889 my $sth = $dbh->prepare($query) || $self->dberror($query);
1891 if ($self->{formname} =~ /(check|receipt)/) {
1892 for $i (1 .. $self->{rowcount}) {
1893 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1897 $sth->execute($self->{id}) || $self->dberror($query);
1901 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1902 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1904 my %queued = split / /, $self->{queued};
1906 if ($self->{formname} =~ /(check|receipt)/) {
1908 # this is a check or receipt, add one entry for each lineitem
1909 my ($accno) = split /--/, $self->{account};
1910 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1911 chart_id) VALUES (?, '$printed',
1912 '$queued{$self->{formname}}', '$self->{prinform}',
1913 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1914 $sth = $dbh->prepare($query) || $self->dberror($query);
1916 for $i (1 .. $self->{rowcount}) {
1917 if ($self->{"checked_$i"}) {
1918 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1923 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1924 spoolfile, formname)
1925 VALUES ($self->{id}, '$printed', '$emailed',
1926 '$queued{$self->{formname}}', '$self->{formname}')|;
1927 $dbh->do($query) || $self->dberror($query);
1933 $main::lxdebug->leave_sub();
1937 $main::lxdebug->enter_sub();
1939 my ($self, $dbh) = @_;
1941 my ($query, $printed, $emailed);
1943 my $formnames = $self->{printed};
1944 my $emailforms = $self->{emailed};
1946 my $query = qq|DELETE FROM status
1947 WHERE formname = '$self->{formname}'
1948 AND trans_id = $self->{id}|;
1949 $dbh->do($query) || $self->dberror($query);
1951 # this only applies to the forms
1952 # checks and receipts are posted when printed or queued
1954 if ($self->{queued}) {
1955 my %queued = split / /, $self->{queued};
1957 foreach my $formname (keys %queued) {
1958 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1959 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1961 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1962 spoolfile, formname)
1963 VALUES ($self->{id}, '$printed', '$emailed',
1964 '$queued{$formname}', '$formname')|;
1965 $dbh->do($query) || $self->dberror($query);
1967 $formnames =~ s/$self->{formname}//;
1968 $emailforms =~ s/$self->{formname}//;
1973 # save printed, emailed info
1974 $formnames =~ s/^ +//g;
1975 $emailforms =~ s/^ +//g;
1978 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1979 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
1981 foreach my $formname (keys %status) {
1982 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
1983 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
1985 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
1986 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
1987 $dbh->do($query) || $self->dberror($query);
1990 $main::lxdebug->leave_sub();
1993 sub update_defaults {
1994 $main::lxdebug->enter_sub();
1996 my ($self, $myconfig, $fld) = @_;
1998 my $dbh = $self->dbconnect_noauto($myconfig);
1999 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2000 my $sth = $dbh->prepare($query);
2002 $sth->execute || $self->dberror($query);
2003 my ($var) = $sth->fetchrow_array;
2008 $query = qq|UPDATE defaults
2010 $dbh->do($query) || $form->dberror($query);
2015 $main::lxdebug->leave_sub();
2020 sub update_business {
2021 $main::lxdebug->enter_sub();
2023 my ($self, $myconfig, $business_id) = @_;
2025 my $dbh = $self->dbconnect_noauto($myconfig);
2027 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2028 my $sth = $dbh->prepare($query);
2030 $sth->execute || $self->dberror($query);
2031 my ($var) = $sth->fetchrow_array;
2036 $query = qq|UPDATE business
2037 SET customernumberinit = '$var' WHERE id=$business_id|;
2038 $dbh->do($query) || $form->dberror($query);
2043 $main::lxdebug->leave_sub();
2049 $main::lxdebug->enter_sub();
2051 my ($self, $myconfig, $salesman) = @_;
2053 my $dbh = $self->dbconnect($myconfig);
2055 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2056 my $sth = $dbh->prepare($query);
2057 $sth->execute || $self->dberror($query);
2060 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2061 push(@{ $self->{salesman_list} }, $ref);
2065 $main::lxdebug->leave_sub();
2070 sub get_partsgroup {
2071 $main::lxdebug->enter_sub();
2073 my ($self, $myconfig, $p) = @_;
2075 my $dbh = $self->dbconnect($myconfig);
2077 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2079 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2081 if ($p->{searchitems} eq 'part') {
2083 WHERE p.inventory_accno_id > 0|;
2085 if ($p->{searchitems} eq 'service') {
2087 WHERE p.inventory_accno_id IS NULL|;
2089 if ($p->{searchitems} eq 'assembly') {
2091 WHERE p.assembly = '1'|;
2093 if ($p->{searchitems} eq 'labor') {
2095 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2099 ORDER BY partsgroup|;
2102 $query = qq|SELECT id, partsgroup FROM partsgroup
2103 ORDER BY partsgroup|;
2106 if ($p->{language_code}) {
2107 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2108 t.description AS translation
2110 JOIN parts p ON (p.partsgroup_id = pg.id)
2111 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2112 ORDER BY translation|;
2115 my $sth = $dbh->prepare($query);
2116 $sth->execute || $self->dberror($query);
2118 $self->{all_partsgroup} = ();
2119 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2120 push @{ $self->{all_partsgroup} }, $ref;
2124 $main::lxdebug->leave_sub();
2129 my ($self, $dbh, $myconfig, $audittrail) = @_;
2131 # table, $reference, $formname, $action, $id, $transdate) = @_;
2138 $dbh = $self->dbconnect($myconfig);
2142 # if we have an id add audittrail, otherwise get a new timestamp
2144 if ($audittrail->{id}) {
2146 $query = qq|SELECT audittrail FROM defaults|;
2148 if ($dbh->selectrow_array($query)) {
2149 my ($null, $employee_id) = $self->get_employee($dbh);
2151 if ($self->{audittrail} && !$myconfig) {
2152 chop $self->{audittrail};
2154 my @a = split /\|/, $self->{audittrail};
2158 my @flds = qw(tablename reference formname action transdate);
2160 # put into hash and remove dups
2162 $key = "$a[2]$a[3]";
2164 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2168 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2169 formname, action, employee_id, transdate)
2170 VALUES ($audittrail->{id}, ?, ?,
2171 ?, ?, $employee_id, ?)|;
2172 my $sth = $dbh->prepare($query) || $self->dberror($query);
2174 foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
2176 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2178 $sth->execute || $self->dberror;
2184 if ($audittrail->{transdate}) {
2185 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2186 formname, action, employee_id, transdate) VALUES (
2187 $audittrail->{id}, '$audittrail->{tablename}', |
2188 .$dbh->quote($audittrail->{reference}).qq|',
2189 '$audittrail->{formname}', '$audittrail->{action}',
2190 $employee_id, '$audittrail->{transdate}')|;
2192 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2193 formname, action, employee_id) VALUES ($audittrail->{id},
2194 '$audittrail->{tablename}', |
2195 .$dbh->quote($audittrail->{reference}).qq|,
2196 '$audittrail->{formname}', '$audittrail->{action}',
2203 $query = qq|SELECT current_timestamp FROM defaults|;
2204 my ($timestamp) = $dbh->selectrow_array($query);
2206 $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2209 $dbh->disconnect if $disconnect;
2218 $main::lxdebug->enter_sub();
2220 my ($type, $country, $NLS_file) = @_;
2224 if ($country && -d "locale/$country") {
2225 $self->{countrycode} = $country;
2226 eval { require "locale/$country/$NLS_file"; };
2229 $self->{NLS_file} = $NLS_file;
2231 push @{ $self->{LONG_MONTH} },
2232 ("January", "February", "March", "April",
2233 "May ", "June", "July", "August",
2234 "September", "October", "November", "December");
2235 push @{ $self->{SHORT_MONTH} },
2236 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2238 $main::lxdebug->leave_sub();
2244 my ($self, $text) = @_;
2246 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2250 $main::lxdebug->enter_sub();
2252 my ($self, $text) = @_;
2254 if (exists $self{subs}{$text}) {
2255 $text = $self{subs}{$text};
2257 if ($self->{countrycode} && $self->{NLS_file}) {
2259 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2263 $main::lxdebug->leave_sub();
2269 $main::lxdebug->enter_sub();
2271 my ($self, $myconfig, $date, $longformat) = @_;
2274 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2279 $spc = $myconfig->{dateformat};
2281 $spc = substr($spc, 1, 1);
2283 if ($date =~ /\D/) {
2284 if ($myconfig->{dateformat} =~ /^yy/) {
2285 ($yy, $mm, $dd) = split /\D/, $date;
2287 if ($myconfig->{dateformat} =~ /^mm/) {
2288 ($mm, $dd, $yy) = split /\D/, $date;
2290 if ($myconfig->{dateformat} =~ /^dd/) {
2291 ($dd, $mm, $yy) = split /\D/, $date;
2294 $date = substr($date, 2);
2295 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2300 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2301 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2303 if ($myconfig->{dateformat} =~ /^dd/) {
2304 if (defined $longformat && $longformat == 0) {
2306 $dd = "0$dd" if ($dd < 10);
2307 $mm = "0$mm" if ($mm < 10);
2308 $longdate = "$dd$spc$mm$spc$yy";
2311 $longdate .= ($spc eq '.') ? ". " : " ";
2312 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2314 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2316 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2317 # Lx-Office is mainly used in Germany or German speaking countries.
2318 if (defined $longformat && $longformat == 0) {
2320 $dd = "0$dd" if ($dd < 10);
2321 $mm = "0$mm" if ($mm < 10);
2322 $longdate = "$yy-$mm-$dd";
2324 $longdate = "$dd. ";
2325 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2328 if (defined $longformat && $longformat == 0) {
2330 $dd = "0$dd" if ($dd < 10);
2331 $mm = "0$mm" if ($mm < 10);
2332 $longdate = "$mm$spc$dd$spc$yy";
2334 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2340 $main::lxdebug->leave_sub();
2346 $main::lxdebug->enter_sub();
2348 my ($self, $myconfig, $date, $longformat) = @_;
2351 $main::lxdebug->leave_sub();
2356 $spc = $myconfig->{dateformat};
2358 $spc = substr($spc, 1, 1);
2360 if ($date =~ /\D/) {
2361 if ($myconfig->{dateformat} =~ /^yy/) {
2362 ($yy, $mm, $dd) = split /\D/, $date;
2363 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2364 ($mm, $dd, $yy) = split /\D/, $date;
2365 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2366 ($dd, $mm, $yy) = split /\D/, $date;
2369 $date = substr($date, 2);
2370 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2375 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2376 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2378 $main::lxdebug->leave_sub();
2379 return ($yy, $mm, $dd);