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 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) = (1, 1);
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
740 if (($current_line + $lines) > $lpp) {
743 # replace the special variables <%sumcarriedforward%>
746 my $psum = $self->format_amount($myconfig, $sum, 2);
747 $pb =~ s/<%sumcarriedforward%>/$psum/g;
748 $pb =~ s/<%lastpage%>/$current_page/g;
750 # only "normal" variables are supported here
751 # (no <%if, no <%foreach, no <%include)
753 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
755 # page break block is ready to rock
760 $current_line += $lines;
762 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
766 # don't parse par, we need it for each line
768 s/<%(.+?)%>/$self->{$1}[$i]/mg;
774 # if not comes before if!
775 if (/\s*<%if not /) {
777 # check if it is not set and display
779 s/\s*<%if not (.+?)%>/$1/;
781 unless ($self->{$_}) {
783 last if (/\s*<%end /);
793 last if (/\s*<%end /);
801 # check if it is set and display
803 s/\s*<%if (.+?)%>/$1/;
807 last if (/\s*<%end /);
817 last if (/\s*<%end /);
823 # check for <%include filename%>
824 if (/\s*<%include /) {
828 $var =~ s/\s*<%include (.+?)%>/$1/;
831 $var =~ s/(\/|\.\.)//g;
833 # prevent the infinite loop!
834 next if ($self->{"$var"});
836 open(INC, "$self->{templates}/$var")
837 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
846 s/<%(.+?)%>/$self->{$1}/g;
853 # Convert the tex file to postscript
854 if ($self->{format} =~ /(postscript|pdf)/) {
857 $self->{cwd} = cwd();
858 $self->{tmpdir} = "$self->{cwd}/$userspath";
860 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
862 $self->{tmpfile} =~ s/$userspath\///g;
864 if ($self->{format} eq 'postscript') {
866 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
868 $self->error($self->cleanup) if ($?);
871 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
873 $self->error($self->cleanup) if ($?);
876 $self->{tmpfile} =~ s/tex$/dvi/;
878 system("dvips $self->{tmpfile} -o -q > /dev/null");
879 $self->error($self->cleanup . "dvips : $!") if ($?);
880 $self->{tmpfile} =~ s/dvi$/ps/;
882 if ($self->{format} eq 'pdf') {
884 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
886 $self->error($self->cleanup) if ($?);
889 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
891 $self->error($self->cleanup) if ($?);
893 $self->{tmpfile} =~ s/tex$/pdf/;
898 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
900 if ($self->{media} eq 'email') {
904 my $mail = new Mailer;
906 map { $mail->{$_} = $self->{$_} }
907 qw(cc bcc subject message version format charset);
908 $mail->{to} = qq|$self->{email}|;
909 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
910 $mail->{fileid} = "$fileid.";
912 # if we send html or plain text inline
913 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
914 $mail->{contenttype} = "text/html";
916 $mail->{message} =~ s/\r\n/<br>\n/g;
917 $myconfig->{signature} =~ s/\\n/<br>\n/g;
918 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
920 open(IN, $self->{tmpfile})
921 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
923 $mail->{message} .= $_;
930 @{ $mail->{attachments} } = ($self->{tmpfile});
932 $myconfig->{signature} =~ s/\\n/\r\n/g;
933 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
937 my $err = $mail->send($out);
938 $self->error($self->cleanup . "$err") if ($err);
944 my $numbytes = (-s $self->{tmpfile});
945 open(IN, $self->{tmpfile})
946 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
948 $self->{copies} = 1 unless $self->{media} eq 'printer';
950 chdir("$self->{cwd}");
952 for my $i (1 .. $self->{copies}) {
954 open(OUT, $self->{OUT})
955 or $self->error($self->cleanup . "$self->{OUT} : $!");
959 print qq|Content-Type: application/$self->{format}
960 Content-Disposition: attachment; filename="$self->{tmpfile}"
961 Content-Length: $numbytes
965 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
985 chdir("$self->{cwd}");
986 $main::lxdebug->leave_sub();
990 $main::lxdebug->enter_sub();
994 chdir("$self->{tmpdir}");
997 if (-f "$self->{tmpfile}.err") {
998 open(FH, "$self->{tmpfile}.err");
1003 if ($self->{tmpfile}) {
1006 $self->{tmpfile} =~ s/\.\w+$//g;
1007 my $tmpfile = $self->{tmpfile};
1008 unlink(<$tmpfile.*>);
1011 chdir("$self->{cwd}");
1013 $main::lxdebug->leave_sub();
1019 $main::lxdebug->enter_sub();
1021 my ($self, @fields) = @_;
1024 %unique_fields = map({ $_ => 1 } @fields);
1025 @fields = keys(%unique_fields);
1026 my $format = $self->{format};
1027 if ($self->{format} =~ /(postscript|pdf)/) {
1034 '<', '>', quotemeta('\n'), '
1038 '&', quotemeta('\n'), '
1040 '"', '\$', '%', '_', '#', quotemeta('^'),
1041 '{', '}', '<', '>', '£', "\r"
1047 quotemeta('\n') => '<br>',
1058 quotemeta('^') => '\^\\',
1063 quotemeta('\n') => '\newline ',
1070 foreach my $key (@{ $replace{order}{$format} }) {
1071 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1074 # Allow some HTML markup to be converted into the output format's
1075 # corresponding markup code, e.g. bold or italic.
1076 if ('html' eq $format) {
1077 my @markup_replace = ('b', 'i', 's', 'u');
1079 foreach my $key (@markup_replace) {
1080 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1083 } elsif ('tex' eq $format) {
1084 my %markup_replace = ('b' => 'textbf',
1086 'u' => 'underline');
1088 foreach my $field (@fields) {
1089 if ($field =~ /descrip/) {
1090 print(STDERR "QFT: ${field}: " . $self->{$field} . "\n");
1092 foreach my $key (keys(%markup_replace)) {
1093 my $new = $markup_replace{$key};
1095 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1100 $main::lxdebug->leave_sub();
1104 $main::lxdebug->enter_sub();
1106 my ($self, $date, $myconfig) = @_;
1108 if ($date && $date =~ /\D/) {
1110 if ($myconfig->{dateformat} =~ /^yy/) {
1111 ($yy, $mm, $dd) = split /\D/, $date;
1113 if ($myconfig->{dateformat} =~ /^mm/) {
1114 ($mm, $dd, $yy) = split /\D/, $date;
1116 if ($myconfig->{dateformat} =~ /^dd/) {
1117 ($dd, $mm, $yy) = split /\D/, $date;
1122 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1123 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1125 $dd = "0$dd" if ($dd < 10);
1126 $mm = "0$mm" if ($mm < 10);
1128 $date = "$yy$mm$dd";
1131 $main::lxdebug->leave_sub();
1136 # Database routines used throughout
1139 $main::lxdebug->enter_sub();
1141 my ($self, $myconfig) = @_;
1143 # connect to database
1145 DBI->connect($myconfig->{dbconnect},
1146 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1150 if ($myconfig->{dboptions}) {
1151 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1154 $main::lxdebug->leave_sub();
1159 sub dbconnect_noauto {
1160 $main::lxdebug->enter_sub();
1162 my ($self, $myconfig) = @_;
1164 # connect to database
1166 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1167 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1171 if ($myconfig->{dboptions}) {
1172 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1175 $main::lxdebug->leave_sub();
1180 sub update_balance {
1181 $main::lxdebug->enter_sub();
1183 my ($self, $dbh, $table, $field, $where, $value) = @_;
1185 # if we have a value, go do it
1188 # retrieve balance from table
1189 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1190 my $sth = $dbh->prepare($query);
1192 $sth->execute || $self->dberror($query);
1193 my ($balance) = $sth->fetchrow_array;
1199 $query = "UPDATE $table SET $field = $balance WHERE $where";
1200 $dbh->do($query) || $self->dberror($query);
1202 $main::lxdebug->leave_sub();
1205 sub update_exchangerate {
1206 $main::lxdebug->enter_sub();
1208 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1210 # some sanity check for currency
1212 $main::lxdebug->leave_sub();
1216 my $query = qq|SELECT e.curr FROM exchangerate e
1217 WHERE e.curr = '$curr'
1218 AND e.transdate = '$transdate'
1220 my $sth = $dbh->prepare($query);
1221 $sth->execute || $self->dberror($query);
1224 if ($buy != 0 && $sell != 0) {
1225 $set = "buy = $buy, sell = $sell";
1226 } elsif ($buy != 0) {
1227 $set = "buy = $buy";
1228 } elsif ($sell != 0) {
1229 $set = "sell = $sell";
1232 if ($sth->fetchrow_array) {
1233 $query = qq|UPDATE exchangerate
1235 WHERE curr = '$curr'
1236 AND transdate = '$transdate'|;
1238 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1239 VALUES ('$curr', $buy, $sell, '$transdate')|;
1242 $dbh->do($query) || $self->dberror($query);
1244 $main::lxdebug->leave_sub();
1247 sub save_exchangerate {
1248 $main::lxdebug->enter_sub();
1250 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1252 my $dbh = $self->dbconnect($myconfig);
1254 my ($buy, $sell) = (0, 0);
1255 $buy = $rate if $fld eq 'buy';
1256 $sell = $rate if $fld eq 'sell';
1258 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1262 $main::lxdebug->leave_sub();
1265 sub get_exchangerate {
1266 $main::lxdebug->enter_sub();
1268 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1270 my $query = qq|SELECT e.$fld FROM exchangerate e
1271 WHERE e.curr = '$curr'
1272 AND e.transdate = '$transdate'|;
1273 my $sth = $dbh->prepare($query);
1274 $sth->execute || $self->dberror($query);
1276 my ($exchangerate) = $sth->fetchrow_array;
1279 $main::lxdebug->leave_sub();
1281 return $exchangerate;
1284 sub check_exchangerate {
1285 $main::lxdebug->enter_sub();
1287 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1289 unless ($transdate) {
1290 $main::lxdebug->leave_sub();
1294 my $dbh = $self->dbconnect($myconfig);
1296 my $query = qq|SELECT e.$fld FROM exchangerate e
1297 WHERE e.curr = '$currency'
1298 AND e.transdate = '$transdate'|;
1299 my $sth = $dbh->prepare($query);
1300 $sth->execute || $self->dberror($query);
1302 my ($exchangerate) = $sth->fetchrow_array;
1306 $main::lxdebug->leave_sub();
1308 return $exchangerate;
1312 $main::lxdebug->enter_sub();
1314 my ($self, $dbh, $id) = @_;
1318 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1319 if ($self->{"shipto$item"}) {
1320 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1322 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1326 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1327 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1328 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1329 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1330 '$self->{shiptozipcode}', '$self->{shiptocity}',
1331 '$self->{shiptocountry}', '$self->{shiptocontact}',
1332 '$self->{shiptophone}', '$self->{shiptofax}',
1333 '$self->{shiptoemail}')|;
1334 $dbh->do($query) || $self->dberror($query);
1337 $main::lxdebug->leave_sub();
1341 $main::lxdebug->enter_sub();
1343 my ($self, $dbh) = @_;
1345 my $query = qq|SELECT e.id, e.name FROM employee e
1346 WHERE e.login = '$self->{login}'|;
1347 my $sth = $dbh->prepare($query);
1348 $sth->execute || $self->dberror($query);
1350 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1351 $self->{employee_id} *= 1;
1355 $main::lxdebug->leave_sub();
1358 # get other contact for transaction and form - html/tex
1360 $main::lxdebug->enter_sub();
1362 my ($self, $dbh, $id) = @_;
1364 my $query = qq|SELECT c.*
1367 $sth = $dbh->prepare($query);
1368 $sth->execute || $self->dberror($query);
1370 $ref = $sth->fetchrow_hashref(NAME_lc);
1372 push @{ $self->{$_} }, $ref;
1375 $main::lxdebug->leave_sub();
1378 # get contacts for id, if no contact return {"","","","",""}
1380 $main::lxdebug->enter_sub();
1382 my ($self, $dbh, $id) = @_;
1384 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1386 WHERE cp_cv_id=$id|;
1387 my $sth = $dbh->prepare($query);
1388 $sth->execute || $self->dberror($query);
1391 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1392 push @{ $self->{all_contacts} }, $ref;
1397 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1400 $main::lxdebug->leave_sub();
1403 # this sub gets the id and name from $table
1405 $main::lxdebug->enter_sub();
1407 my ($self, $myconfig, $table) = @_;
1409 # connect to database
1410 my $dbh = $self->dbconnect($myconfig);
1412 my $name = $self->like(lc $self->{$table});
1413 my $customernumber = $self->like(lc $self->{customernumber});
1415 if ($self->{customernumber} ne "") {
1416 $query = qq~SELECT c.id, c.name,
1417 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1419 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1422 $query = qq~SELECT c.id, c.name,
1423 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1425 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1429 if ($self->{openinvoices}) {
1430 $query = qq~SELECT DISTINCT c.id, c.name,
1431 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1432 FROM $self->{arap} a
1433 JOIN $table c ON (a.${table}_id = c.id)
1434 WHERE NOT a.amount = a.paid
1435 AND lower(c.name) LIKE '$name'
1438 my $sth = $dbh->prepare($query);
1440 $sth->execute || $self->dberror($query);
1443 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1444 push(@{ $self->{name_list} }, $ref);
1450 $main::lxdebug->leave_sub();
1455 # the selection sub is used in the AR, AP, IS, IR and OE module
1458 $main::lxdebug->enter_sub();
1460 my ($self, $myconfig, $table, $module) = @_;
1463 my $dbh = $self->dbconnect($myconfig);
1465 my $query = qq|SELECT count(*) FROM $table|;
1466 my $sth = $dbh->prepare($query);
1467 $sth->execute || $self->dberror($query);
1468 my ($count) = $sth->fetchrow_array;
1471 # build selection list
1472 if ($count < $myconfig->{vclimit}) {
1473 $query = qq|SELECT id, name
1474 FROM $table WHERE not obsolete
1476 $sth = $dbh->prepare($query);
1477 $sth->execute || $self->dberror($query);
1479 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1480 push @{ $self->{"all_$table"} }, $ref;
1488 $self->get_employee($dbh);
1490 # setup sales contacts
1491 $query = qq|SELECT e.id, e.name
1494 AND NOT e.id = $self->{employee_id}|;
1495 $sth = $dbh->prepare($query);
1496 $sth->execute || $self->dberror($query);
1498 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1499 push @{ $self->{all_employees} }, $ref;
1504 push @{ $self->{all_employees} },
1505 { id => $self->{employee_id},
1506 name => $self->{employee} };
1508 # sort the whole thing
1509 @{ $self->{all_employees} } =
1510 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1512 if ($module eq 'AR') {
1514 # prepare query for departments
1515 $query = qq|SELECT d.id, d.description
1521 $query = qq|SELECT d.id, d.description
1526 $sth = $dbh->prepare($query);
1527 $sth->execute || $self->dberror($query);
1529 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1530 push @{ $self->{all_departments} }, $ref;
1535 $main::lxdebug->leave_sub();
1538 # this is only used for reports
1539 sub all_departments {
1540 $main::lxdebug->enter_sub();
1542 my ($self, $myconfig, $table) = @_;
1544 my $dbh = $self->dbconnect($myconfig);
1545 my $where = "1 = 1";
1547 if (defined $table) {
1548 if ($table eq 'customer') {
1549 $where = " d.role = 'P'";
1553 my $query = qq|SELECT d.id, d.description
1557 my $sth = $dbh->prepare($query);
1558 $sth->execute || $self->dberror($query);
1560 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1561 push @{ $self->{all_departments} }, $ref;
1567 $main::lxdebug->leave_sub();
1571 $main::lxdebug->enter_sub();
1573 my ($self, $module, $myconfig, $table) = @_;
1575 $self->all_vc($myconfig, $table, $module);
1577 # get last customers or vendors
1580 my $dbh = $self->dbconnect($myconfig);
1584 # now get the account numbers
1586 qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1588 WHERE c.link LIKE '%$module%'
1591 $sth = $dbh->prepare($query);
1592 $sth->execute || $self->dberror($query);
1594 $self->{accounts} = "";
1595 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1597 foreach my $key (split /:/, $ref->{link}) {
1598 if ($key =~ /$module/) {
1600 # cross reference for keys
1601 $xkeyref{ $ref->{accno} } = $key;
1603 push @{ $self->{"${module}_links"}{$key} },
1604 { accno => $ref->{accno},
1605 description => $ref->{description},
1606 taxkey => $ref->{taxkey_id} };
1608 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1614 if (($module eq "AP") || ($module eq "AR")) {
1616 # get tax rates and description
1617 $query = qq| SELECT * FROM tax t|;
1618 $sth = $dbh->prepare($query);
1619 $sth->execute || $self->dberror($query);
1621 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1622 push @{ $self->{TAX} }, $ref;
1628 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1630 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1631 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1632 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1633 c.name AS $table, a.department_id, d.description AS department,
1634 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1635 a.employee_id, e.name AS employee, a.gldate
1637 JOIN $table c ON (a.${table}_id = c.id)
1638 LEFT JOIN employee e ON (e.id = a.employee_id)
1639 LEFT JOIN department d ON (d.id = a.department_id)
1640 WHERE a.id = $self->{id}|;
1641 $sth = $dbh->prepare($query);
1642 $sth->execute || $self->dberror($query);
1644 $ref = $sth->fetchrow_hashref(NAME_lc);
1645 foreach $key (keys %$ref) {
1646 $self->{$key} = $ref->{$key};
1650 # get amounts from individual entries
1651 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1652 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1654 JOIN chart c ON (c.id = a.chart_id)
1655 LEFT JOIN project p ON (p.id = a.project_id)
1656 LEFT Join tax t ON (a.taxkey = t.taxkey)
1657 WHERE a.trans_id = $self->{id}
1658 AND a.fx_transaction = '0'
1659 ORDER BY a.transdate|;
1660 $sth = $dbh->prepare($query);
1661 $sth->execute || $self->dberror($query);
1663 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1665 # get exchangerate for currency
1666 $self->{exchangerate} =
1667 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1670 # store amounts in {acc_trans}{$key} for multiple accounts
1671 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1672 $ref->{exchangerate} =
1673 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1676 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1680 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1681 (SELECT c.accno FROM chart c
1682 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1683 (SELECT c.accno FROM chart c
1684 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1686 $sth = $dbh->prepare($query);
1687 $sth->execute || $self->dberror($query);
1689 $ref = $sth->fetchrow_hashref(NAME_lc);
1690 map { $self->{$_} = $ref->{$_} } keys %$ref;
1696 $query = qq|SELECT current_date AS transdate,
1697 d.curr AS currencies, d.closedto, d.revtrans,
1698 (SELECT c.accno FROM chart c
1699 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1700 (SELECT c.accno FROM chart c
1701 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1703 $sth = $dbh->prepare($query);
1704 $sth->execute || $self->dberror($query);
1706 $ref = $sth->fetchrow_hashref(NAME_lc);
1707 map { $self->{$_} = $ref->{$_} } keys %$ref;
1710 if ($self->{"$self->{vc}_id"}) {
1712 # only setup currency
1713 ($self->{currency}) = split /:/, $self->{currencies};
1717 $self->lastname_used($dbh, $myconfig, $table, $module);
1719 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1721 # get exchangerate for currency
1722 $self->{exchangerate} =
1723 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1732 $main::lxdebug->leave_sub();
1736 $main::lxdebug->enter_sub();
1738 my ($self, $dbh, $myconfig, $table, $module) = @_;
1740 my $arap = ($table eq 'customer') ? "ar" : "ap";
1741 my $where = "1 = 1";
1743 if ($self->{type} =~ /_order/) {
1745 $where = "quotation = '0'";
1747 if ($self->{type} =~ /_quotation/) {
1749 $where = "quotation = '1'";
1752 my $query = qq|SELECT id FROM $arap
1753 WHERE id IN (SELECT MAX(id) FROM $arap
1755 AND ${table}_id > 0)|;
1756 my $sth = $dbh->prepare($query);
1757 $sth->execute || $self->dberror($query);
1759 my ($trans_id) = $sth->fetchrow_array;
1763 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1764 current_date + ct.terms AS duedate, a.department_id,
1765 d.description AS department
1767 JOIN $table ct ON (a.${table}_id = ct.id)
1768 LEFT JOIN department d ON (a.department_id = d.id)
1769 WHERE a.id = $trans_id|;
1770 $sth = $dbh->prepare($query);
1771 $sth->execute || $self->dberror($query);
1773 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1774 $self->{duedate}, $self->{department_id}, $self->{department})
1775 = $sth->fetchrow_array;
1778 $main::lxdebug->leave_sub();
1782 $main::lxdebug->enter_sub();
1784 my ($self, $myconfig, $thisdate, $days) = @_;
1786 my $dbh = $self->dbconnect($myconfig);
1791 my $dateformat = $myconfig->{dateformat};
1792 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1794 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1796 $sth = $dbh->prepare($query);
1797 $sth->execute || $self->dberror($query);
1799 $query = qq|SELECT current_date AS thisdate
1801 $sth = $dbh->prepare($query);
1802 $sth->execute || $self->dberror($query);
1805 ($thisdate) = $sth->fetchrow_array;
1810 $main::lxdebug->leave_sub();
1816 $main::lxdebug->enter_sub();
1818 my ($self, $string) = @_;
1820 if ($string !~ /%/) {
1821 $string = "%$string%";
1824 $string =~ s/\'/\'\'/g;
1826 $main::lxdebug->leave_sub();
1832 $main::lxdebug->enter_sub();
1834 my ($self, $flds, $new, $count, $numrows) = @_;
1838 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1844 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1846 $j = $item->{ndx} - 1;
1847 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1851 for $i ($count + 1 .. $numrows) {
1852 map { delete $self->{"${_}_$i"} } @{$flds};
1855 $main::lxdebug->leave_sub();
1859 $main::lxdebug->enter_sub();
1861 my ($self, $myconfig) = @_;
1865 my $dbh = $self->dbconnect_noauto($myconfig);
1867 my $query = qq|DELETE FROM status
1868 WHERE formname = '$self->{formname}'
1870 my $sth = $dbh->prepare($query) || $self->dberror($query);
1872 if ($self->{formname} =~ /(check|receipt)/) {
1873 for $i (1 .. $self->{rowcount}) {
1874 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1878 $sth->execute($self->{id}) || $self->dberror($query);
1882 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1883 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1885 my %queued = split / /, $self->{queued};
1887 if ($self->{formname} =~ /(check|receipt)/) {
1889 # this is a check or receipt, add one entry for each lineitem
1890 my ($accno) = split /--/, $self->{account};
1891 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1892 chart_id) VALUES (?, '$printed',
1893 '$queued{$self->{formname}}', '$self->{prinform}',
1894 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1895 $sth = $dbh->prepare($query) || $self->dberror($query);
1897 for $i (1 .. $self->{rowcount}) {
1898 if ($self->{"checked_$i"}) {
1899 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1904 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1905 spoolfile, formname)
1906 VALUES ($self->{id}, '$printed', '$emailed',
1907 '$queued{$self->{formname}}', '$self->{formname}')|;
1908 $dbh->do($query) || $self->dberror($query);
1914 $main::lxdebug->leave_sub();
1918 $main::lxdebug->enter_sub();
1920 my ($self, $dbh) = @_;
1922 my ($query, $printed, $emailed);
1924 my $formnames = $self->{printed};
1925 my $emailforms = $self->{emailed};
1927 my $query = qq|DELETE FROM status
1928 WHERE formname = '$self->{formname}'
1929 AND trans_id = $self->{id}|;
1930 $dbh->do($query) || $self->dberror($query);
1932 # this only applies to the forms
1933 # checks and receipts are posted when printed or queued
1935 if ($self->{queued}) {
1936 my %queued = split / /, $self->{queued};
1938 foreach my $formname (keys %queued) {
1939 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1940 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1942 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1943 spoolfile, formname)
1944 VALUES ($self->{id}, '$printed', '$emailed',
1945 '$queued{$formname}', '$formname')|;
1946 $dbh->do($query) || $self->dberror($query);
1948 $formnames =~ s/$self->{formname}//;
1949 $emailforms =~ s/$self->{formname}//;
1954 # save printed, emailed info
1955 $formnames =~ s/^ +//g;
1956 $emailforms =~ s/^ +//g;
1959 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1960 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
1962 foreach my $formname (keys %status) {
1963 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
1964 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
1966 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
1967 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
1968 $dbh->do($query) || $self->dberror($query);
1971 $main::lxdebug->leave_sub();
1974 sub update_defaults {
1975 $main::lxdebug->enter_sub();
1977 my ($self, $myconfig, $fld) = @_;
1979 my $dbh = $self->dbconnect_noauto($myconfig);
1980 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
1981 my $sth = $dbh->prepare($query);
1983 $sth->execute || $self->dberror($query);
1984 my ($var) = $sth->fetchrow_array;
1989 $query = qq|UPDATE defaults
1991 $dbh->do($query) || $form->dberror($query);
1996 $main::lxdebug->leave_sub();
2001 sub update_business {
2002 $main::lxdebug->enter_sub();
2004 my ($self, $myconfig, $business_id) = @_;
2006 my $dbh = $self->dbconnect_noauto($myconfig);
2008 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2009 my $sth = $dbh->prepare($query);
2011 $sth->execute || $self->dberror($query);
2012 my ($var) = $sth->fetchrow_array;
2017 $query = qq|UPDATE business
2018 SET customernumberinit = '$var' WHERE id=$business_id|;
2019 $dbh->do($query) || $form->dberror($query);
2024 $main::lxdebug->leave_sub();
2030 $main::lxdebug->enter_sub();
2032 my ($self, $myconfig, $salesman) = @_;
2034 my $dbh = $self->dbconnect($myconfig);
2036 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2037 my $sth = $dbh->prepare($query);
2038 $sth->execute || $self->dberror($query);
2041 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2042 push(@{ $self->{salesman_list} }, $ref);
2046 $main::lxdebug->leave_sub();
2051 sub get_partsgroup {
2052 $main::lxdebug->enter_sub();
2054 my ($self, $myconfig, $p) = @_;
2056 my $dbh = $self->dbconnect($myconfig);
2058 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2060 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2062 if ($p->{searchitems} eq 'part') {
2064 WHERE p.inventory_accno_id > 0|;
2066 if ($p->{searchitems} eq 'service') {
2068 WHERE p.inventory_accno_id IS NULL|;
2070 if ($p->{searchitems} eq 'assembly') {
2072 WHERE p.assembly = '1'|;
2074 if ($p->{searchitems} eq 'labor') {
2076 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2080 ORDER BY partsgroup|;
2083 $query = qq|SELECT id, partsgroup FROM partsgroup
2084 ORDER BY partsgroup|;
2087 if ($p->{language_code}) {
2088 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2089 t.description AS translation
2091 JOIN parts p ON (p.partsgroup_id = pg.id)
2092 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2093 ORDER BY translation|;
2096 my $sth = $dbh->prepare($query);
2097 $sth->execute || $self->dberror($query);
2099 $self->{all_partsgroup} = ();
2100 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2101 push @{ $self->{all_partsgroup} }, $ref;
2105 $main::lxdebug->leave_sub();
2110 my ($self, $dbh, $myconfig, $audittrail) = @_;
2112 # table, $reference, $formname, $action, $id, $transdate) = @_;
2119 $dbh = $self->dbconnect($myconfig);
2123 # if we have an id add audittrail, otherwise get a new timestamp
2125 if ($audittrail->{id}) {
2127 $query = qq|SELECT audittrail FROM defaults|;
2129 if ($dbh->selectrow_array($query)) {
2130 my ($null, $employee_id) = $self->get_employee($dbh);
2132 if ($self->{audittrail} && !$myconfig) {
2133 chop $self->{audittrail};
2135 my @a = split /\|/, $self->{audittrail};
2139 my @flds = qw(tablename reference formname action transdate);
2141 # put into hash and remove dups
2143 $key = "$a[2]$a[3]";
2145 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2149 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2150 formname, action, employee_id, transdate)
2151 VALUES ($audittrail->{id}, ?, ?,
2152 ?, ?, $employee_id, ?)|;
2153 my $sth = $dbh->prepare($query) || $self->dberror($query);
2155 foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
2157 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2159 $sth->execute || $self->dberror;
2165 if ($audittrail->{transdate}) {
2166 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2167 formname, action, employee_id, transdate) VALUES (
2168 $audittrail->{id}, '$audittrail->{tablename}', |
2169 .$dbh->quote($audittrail->{reference}).qq|',
2170 '$audittrail->{formname}', '$audittrail->{action}',
2171 $employee_id, '$audittrail->{transdate}')|;
2173 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2174 formname, action, employee_id) VALUES ($audittrail->{id},
2175 '$audittrail->{tablename}', |
2176 .$dbh->quote($audittrail->{reference}).qq|,
2177 '$audittrail->{formname}', '$audittrail->{action}',
2184 $query = qq|SELECT current_timestamp FROM defaults|;
2185 my ($timestamp) = $dbh->selectrow_array($query);
2187 $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2190 $dbh->disconnect if $disconnect;
2199 $main::lxdebug->enter_sub();
2201 my ($type, $country, $NLS_file) = @_;
2205 if ($country && -d "locale/$country") {
2206 $self->{countrycode} = $country;
2207 eval { require "locale/$country/$NLS_file"; };
2210 $self->{NLS_file} = $NLS_file;
2212 push @{ $self->{LONG_MONTH} },
2213 ("January", "February", "March", "April",
2214 "May ", "June", "July", "August",
2215 "September", "October", "November", "December");
2216 push @{ $self->{SHORT_MONTH} },
2217 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2219 $main::lxdebug->leave_sub();
2225 my ($self, $text) = @_;
2227 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2231 $main::lxdebug->enter_sub();
2233 my ($self, $text) = @_;
2235 if (exists $self{subs}{$text}) {
2236 $text = $self{subs}{$text};
2238 if ($self->{countrycode} && $self->{NLS_file}) {
2240 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2244 $main::lxdebug->leave_sub();
2250 $main::lxdebug->enter_sub();
2252 my ($self, $myconfig, $date, $longformat) = @_;
2255 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2260 $spc = $myconfig->{dateformat};
2262 $spc = substr($spc, 1, 1);
2264 if ($date =~ /\D/) {
2265 if ($myconfig->{dateformat} =~ /^yy/) {
2266 ($yy, $mm, $dd) = split /\D/, $date;
2268 if ($myconfig->{dateformat} =~ /^mm/) {
2269 ($mm, $dd, $yy) = split /\D/, $date;
2271 if ($myconfig->{dateformat} =~ /^dd/) {
2272 ($dd, $mm, $yy) = split /\D/, $date;
2275 $date = substr($date, 2);
2276 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2281 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2282 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2284 if ($myconfig->{dateformat} =~ /^dd/) {
2285 if (defined $longformat && $longformat == 0) {
2287 $dd = "0$dd" if ($dd < 10);
2288 $mm = "0$mm" if ($mm < 10);
2289 $longdate = "$dd$spc$mm$spc$yy";
2292 $longdate .= ($spc eq '.') ? ". " : " ";
2293 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2295 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2297 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2298 # Lx-Office is mainly used in Germany or German speaking countries.
2299 if (defined $longformat && $longformat == 0) {
2301 $dd = "0$dd" if ($dd < 10);
2302 $mm = "0$mm" if ($mm < 10);
2303 $longdate = "$yy-$mm-$dd";
2305 $longdate = "$dd. ";
2306 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2309 if (defined $longformat && $longformat == 0) {
2311 $dd = "0$dd" if ($dd < 10);
2312 $mm = "0$mm" if ($mm < 10);
2313 $longdate = "$mm$spc$dd$spc$yy";
2315 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2321 $main::lxdebug->leave_sub();
2327 $main::lxdebug->enter_sub();
2329 my ($self, $myconfig, $date, $longformat) = @_;
2332 $main::lxdebug->leave_sub();
2337 $spc = $myconfig->{dateformat};
2339 $spc = substr($spc, 1, 1);
2341 if ($date =~ /\D/) {
2342 if ($myconfig->{dateformat} =~ /^yy/) {
2343 ($yy, $mm, $dd) = split /\D/, $date;
2344 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2345 ($mm, $dd, $yy) = split /\D/, $date;
2346 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2347 ($dd, $mm, $yy) = split /\D/, $date;
2350 $date = substr($date, 2);
2351 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2356 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2357 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2359 $main::lxdebug->leave_sub();
2360 return ($yy, $mm, $dd);