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 # 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;
626 $main::lxdebug->enter_sub();
628 my ($self, $myconfig, $userspath) = @_;
631 # Some variables used for page breaks
632 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
634 my ($current_page, $current_line) = (1, 1);
640 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
641 $self->format_string(grep(/notes/, keys(%{$self})));
643 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
644 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
646 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
647 qw(email tel fax name signature));
649 open(IN, "$self->{templates}/$self->{IN}")
650 or $self->error("$self->{IN} : $!");
655 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
657 # OUT is used for the media, screen, printer, email
658 # for postscript we store a copy in a temporary file
660 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
661 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
663 $self->{OUT} = ">$self->{tmpfile}";
667 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
669 open(OUT, ">-") or $self->error("STDOUT : $!");
673 # Do we have to run LaTeX two times? This is needed if
674 # the template contains page references.
677 # first we generate a tmpfile
678 # read file and replace <%variable%>
684 $two_passes = 1 if (/\\pageref/);
687 # detect pagebreak block and its parameters
688 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
689 $chars_per_line = $1;
690 $lines_on_first_page = $2;
691 $lines_on_second_page = $3;
694 last if (/\s*<%end pagebreak%>/);
701 if (/\s*<%foreach /) {
703 # this one we need for the count
705 $var =~ s/\s*<%foreach (.+?)%>/$1/;
707 last if (/\s*<%end /);
713 # display contents of $self->{number}[] array
714 for $i (0 .. $#{ $self->{$var} }) {
717 # Try to detect whether a manual page break is necessary
718 # but only if there was a <%pagebreak ...%> block before
720 if ($chars_per_line) {
722 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
725 my $_description = $self->{"description"}[$i];
726 while ($_description =~ /\\newline/) {
728 $_description =~ s/\\newline//;
730 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
732 if ($current_page == 1) {
733 $lpp = $lines_on_first_page;
735 $lpp = $lines_on_second_page;
738 # Yes we need a manual page break
739 if (($current_line + $lines) > $lpp) {
742 # replace the special variables <%sumcarriedforward%>
745 my $psum = $self->format_amount($myconfig, $sum, 2);
746 $pb =~ s/<%sumcarriedforward%>/$psum/g;
747 $pb =~ s/<%lastpage%>/$current_page/g;
749 # only "normal" variables are supported here
750 # (no <%if, no <%foreach, no <%include)
752 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
754 # page break block is ready to rock
759 $current_line += $lines;
761 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
765 # don't parse par, we need it for each line
767 s/<%(.+?)%>/$self->{$1}[$i]/mg;
773 # if not comes before if!
774 if (/\s*<%if not /) {
776 # check if it is not set and display
778 s/\s*<%if not (.+?)%>/$1/;
780 unless ($self->{$_}) {
782 last if (/\s*<%end /);
792 last if (/\s*<%end /);
800 # check if it is set and display
802 s/\s*<%if (.+?)%>/$1/;
806 last if (/\s*<%end /);
816 last if (/\s*<%end /);
822 # check for <%include filename%>
823 if (/\s*<%include /) {
827 $var =~ s/\s*<%include (.+?)%>/$1/;
830 $var =~ s/(\/|\.\.)//g;
832 # prevent the infinite loop!
833 next if ($self->{"$var"});
835 open(INC, "$self->{templates}/$var")
836 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
845 s/<%(.+?)%>/$self->{$1}/g;
852 # Convert the tex file to postscript
853 if ($self->{format} =~ /(postscript|pdf)/) {
856 $self->{cwd} = cwd();
857 $self->{tmpdir} = "$self->{cwd}/$userspath";
859 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
861 $self->{tmpfile} =~ s/$userspath\///g;
863 if ($self->{format} eq 'postscript') {
865 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
867 $self->error($self->cleanup) if ($?);
870 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
872 $self->error($self->cleanup) if ($?);
875 $self->{tmpfile} =~ s/tex$/dvi/;
877 system("dvips $self->{tmpfile} -o -q > /dev/null");
878 $self->error($self->cleanup . "dvips : $!") if ($?);
879 $self->{tmpfile} =~ s/dvi$/ps/;
881 if ($self->{format} eq 'pdf') {
883 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
885 $self->error($self->cleanup) if ($?);
888 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
890 $self->error($self->cleanup) if ($?);
892 $self->{tmpfile} =~ s/tex$/pdf/;
897 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
899 if ($self->{media} eq 'email') {
903 my $mail = new Mailer;
905 map { $mail->{$_} = $self->{$_} }
906 qw(cc bcc subject message version format charset);
907 $mail->{to} = qq|$self->{email}|;
908 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
909 $mail->{fileid} = "$fileid.";
911 # if we send html or plain text inline
912 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
913 $mail->{contenttype} = "text/html";
915 $mail->{message} =~ s/\r\n/<br>\n/g;
916 $myconfig->{signature} =~ s/\\n/<br>\n/g;
917 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
919 open(IN, $self->{tmpfile})
920 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
922 $mail->{message} .= $_;
929 @{ $mail->{attachments} } = ($self->{tmpfile});
931 $myconfig->{signature} =~ s/\\n/\r\n/g;
932 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
936 my $err = $mail->send($out);
937 $self->error($self->cleanup . "$err") if ($err);
943 my $numbytes = (-s $self->{tmpfile});
944 open(IN, $self->{tmpfile})
945 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
947 $self->{copies} = 1 unless $self->{media} eq 'printer';
949 chdir("$self->{cwd}");
951 for my $i (1 .. $self->{copies}) {
953 open(OUT, $self->{OUT})
954 or $self->error($self->cleanup . "$self->{OUT} : $!");
958 print qq|Content-Type: application/$self->{format}
959 Content-Disposition: attachment; filename="$self->{tmpfile}"
960 Content-Length: $numbytes
964 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
984 chdir("$self->{cwd}");
985 $main::lxdebug->leave_sub();
989 $main::lxdebug->enter_sub();
993 chdir("$self->{tmpdir}");
996 if (-f "$self->{tmpfile}.err") {
997 open(FH, "$self->{tmpfile}.err");
1002 if ($self->{tmpfile}) {
1005 $self->{tmpfile} =~ s/\.\w+$//g;
1006 my $tmpfile = $self->{tmpfile};
1007 unlink(<$tmpfile.*>);
1010 chdir("$self->{cwd}");
1012 $main::lxdebug->leave_sub();
1018 $main::lxdebug->enter_sub();
1020 my ($self, @fields) = @_;
1023 %unique_fields = map({ $_ => 1 } @fields);
1024 @fields = keys(%unique_fields);
1025 my $format = $self->{format};
1026 if ($self->{format} =~ /(postscript|pdf)/) {
1033 '<', '>', quotemeta('\n'), '
1037 '&', quotemeta('\n'), '
1039 '"', '\$', '%', '_', '#', quotemeta('^'),
1040 '{', '}', '<', '>', '£', "\r"
1046 quotemeta('\n') => '<br>',
1057 quotemeta('^') => '\^\\',
1062 quotemeta('\n') => '\newline ',
1069 foreach my $key (@{ $replace{order}{$format} }) {
1070 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1073 $main::lxdebug->leave_sub();
1077 $main::lxdebug->enter_sub();
1079 my ($self, $date, $myconfig) = @_;
1081 if ($date && $date =~ /\D/) {
1083 if ($myconfig->{dateformat} =~ /^yy/) {
1084 ($yy, $mm, $dd) = split /\D/, $date;
1086 if ($myconfig->{dateformat} =~ /^mm/) {
1087 ($mm, $dd, $yy) = split /\D/, $date;
1089 if ($myconfig->{dateformat} =~ /^dd/) {
1090 ($dd, $mm, $yy) = split /\D/, $date;
1095 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1096 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1098 $dd = "0$dd" if ($dd < 10);
1099 $mm = "0$mm" if ($mm < 10);
1101 $date = "$yy$mm$dd";
1104 $main::lxdebug->leave_sub();
1109 # Database routines used throughout
1112 $main::lxdebug->enter_sub();
1114 my ($self, $myconfig) = @_;
1116 # connect to database
1118 DBI->connect($myconfig->{dbconnect},
1119 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1123 if ($myconfig->{dboptions}) {
1124 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1127 $main::lxdebug->leave_sub();
1132 sub dbconnect_noauto {
1133 $main::lxdebug->enter_sub();
1135 my ($self, $myconfig) = @_;
1137 # connect to database
1139 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1140 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1144 if ($myconfig->{dboptions}) {
1145 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1148 $main::lxdebug->leave_sub();
1153 sub update_balance {
1154 $main::lxdebug->enter_sub();
1156 my ($self, $dbh, $table, $field, $where, $value) = @_;
1158 # if we have a value, go do it
1161 # retrieve balance from table
1162 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1163 my $sth = $dbh->prepare($query);
1165 $sth->execute || $self->dberror($query);
1166 my ($balance) = $sth->fetchrow_array;
1172 $query = "UPDATE $table SET $field = $balance WHERE $where";
1173 $dbh->do($query) || $self->dberror($query);
1175 $main::lxdebug->leave_sub();
1178 sub update_exchangerate {
1179 $main::lxdebug->enter_sub();
1181 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1183 # some sanity check for currency
1185 $main::lxdebug->leave_sub();
1189 my $query = qq|SELECT e.curr FROM exchangerate e
1190 WHERE e.curr = '$curr'
1191 AND e.transdate = '$transdate'
1193 my $sth = $dbh->prepare($query);
1194 $sth->execute || $self->dberror($query);
1197 if ($buy != 0 && $sell != 0) {
1198 $set = "buy = $buy, sell = $sell";
1199 } elsif ($buy != 0) {
1200 $set = "buy = $buy";
1201 } elsif ($sell != 0) {
1202 $set = "sell = $sell";
1205 if ($sth->fetchrow_array) {
1206 $query = qq|UPDATE exchangerate
1208 WHERE curr = '$curr'
1209 AND transdate = '$transdate'|;
1211 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1212 VALUES ('$curr', $buy, $sell, '$transdate')|;
1215 $dbh->do($query) || $self->dberror($query);
1217 $main::lxdebug->leave_sub();
1220 sub save_exchangerate {
1221 $main::lxdebug->enter_sub();
1223 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1225 my $dbh = $self->dbconnect($myconfig);
1227 my ($buy, $sell) = (0, 0);
1228 $buy = $rate if $fld eq 'buy';
1229 $sell = $rate if $fld eq 'sell';
1231 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1235 $main::lxdebug->leave_sub();
1238 sub get_exchangerate {
1239 $main::lxdebug->enter_sub();
1241 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1243 my $query = qq|SELECT e.$fld FROM exchangerate e
1244 WHERE e.curr = '$curr'
1245 AND e.transdate = '$transdate'|;
1246 my $sth = $dbh->prepare($query);
1247 $sth->execute || $self->dberror($query);
1249 my ($exchangerate) = $sth->fetchrow_array;
1252 $main::lxdebug->leave_sub();
1254 return $exchangerate;
1257 sub check_exchangerate {
1258 $main::lxdebug->enter_sub();
1260 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1262 unless ($transdate) {
1263 $main::lxdebug->leave_sub();
1267 my $dbh = $self->dbconnect($myconfig);
1269 my $query = qq|SELECT e.$fld FROM exchangerate e
1270 WHERE e.curr = '$currency'
1271 AND e.transdate = '$transdate'|;
1272 my $sth = $dbh->prepare($query);
1273 $sth->execute || $self->dberror($query);
1275 my ($exchangerate) = $sth->fetchrow_array;
1279 $main::lxdebug->leave_sub();
1281 return $exchangerate;
1285 $main::lxdebug->enter_sub();
1287 my ($self, $dbh, $id) = @_;
1291 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1292 if ($self->{"shipto$item"}) {
1293 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1295 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1299 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1300 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1301 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1302 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1303 '$self->{shiptozipcode}', '$self->{shiptocity}',
1304 '$self->{shiptocountry}', '$self->{shiptocontact}',
1305 '$self->{shiptophone}', '$self->{shiptofax}',
1306 '$self->{shiptoemail}')|;
1307 $dbh->do($query) || $self->dberror($query);
1310 $main::lxdebug->leave_sub();
1314 $main::lxdebug->enter_sub();
1316 my ($self, $dbh) = @_;
1318 my $query = qq|SELECT e.id, e.name FROM employee e
1319 WHERE e.login = '$self->{login}'|;
1320 my $sth = $dbh->prepare($query);
1321 $sth->execute || $self->dberror($query);
1323 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1324 $self->{employee_id} *= 1;
1328 $main::lxdebug->leave_sub();
1331 # get other contact for transaction and form - html/tex
1333 $main::lxdebug->enter_sub();
1335 my ($self, $dbh, $id) = @_;
1337 my $query = qq|SELECT c.*
1340 $sth = $dbh->prepare($query);
1341 $sth->execute || $self->dberror($query);
1343 $ref = $sth->fetchrow_hashref(NAME_lc);
1345 push @{ $self->{$_} }, $ref;
1348 $main::lxdebug->leave_sub();
1351 # get contacts for id, if no contact return {"","","","",""}
1353 $main::lxdebug->enter_sub();
1355 my ($self, $dbh, $id) = @_;
1357 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1359 WHERE cp_cv_id=$id|;
1360 my $sth = $dbh->prepare($query);
1361 $sth->execute || $self->dberror($query);
1364 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1365 push @{ $self->{all_contacts} }, $ref;
1370 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1373 $main::lxdebug->leave_sub();
1376 # this sub gets the id and name from $table
1378 $main::lxdebug->enter_sub();
1380 my ($self, $myconfig, $table) = @_;
1382 # connect to database
1383 my $dbh = $self->dbconnect($myconfig);
1385 my $name = $self->like(lc $self->{$table});
1386 my $customernumber = $self->like(lc $self->{customernumber});
1388 if ($self->{customernumber} ne "") {
1389 $query = qq~SELECT c.id, c.name,
1390 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1392 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1395 $query = qq~SELECT c.id, c.name,
1396 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1398 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1402 if ($self->{openinvoices}) {
1403 $query = qq~SELECT DISTINCT c.id, c.name,
1404 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1405 FROM $self->{arap} a
1406 JOIN $table c ON (a.${table}_id = c.id)
1407 WHERE NOT a.amount = a.paid
1408 AND lower(c.name) LIKE '$name'
1411 my $sth = $dbh->prepare($query);
1413 $sth->execute || $self->dberror($query);
1416 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1417 push(@{ $self->{name_list} }, $ref);
1423 $main::lxdebug->leave_sub();
1428 # the selection sub is used in the AR, AP, IS, IR and OE module
1431 $main::lxdebug->enter_sub();
1433 my ($self, $myconfig, $table, $module) = @_;
1436 my $dbh = $self->dbconnect($myconfig);
1438 my $query = qq|SELECT count(*) FROM $table|;
1439 my $sth = $dbh->prepare($query);
1440 $sth->execute || $self->dberror($query);
1441 my ($count) = $sth->fetchrow_array;
1444 # build selection list
1445 if ($count < $myconfig->{vclimit}) {
1446 $query = qq|SELECT id, name
1447 FROM $table WHERE not obsolete
1449 $sth = $dbh->prepare($query);
1450 $sth->execute || $self->dberror($query);
1452 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1453 push @{ $self->{"all_$table"} }, $ref;
1461 $self->get_employee($dbh);
1463 # setup sales contacts
1464 $query = qq|SELECT e.id, e.name
1467 AND NOT e.id = $self->{employee_id}|;
1468 $sth = $dbh->prepare($query);
1469 $sth->execute || $self->dberror($query);
1471 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1472 push @{ $self->{all_employees} }, $ref;
1477 push @{ $self->{all_employees} },
1478 { id => $self->{employee_id},
1479 name => $self->{employee} };
1481 # sort the whole thing
1482 @{ $self->{all_employees} } =
1483 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1485 if ($module eq 'AR') {
1487 # prepare query for departments
1488 $query = qq|SELECT d.id, d.description
1494 $query = qq|SELECT d.id, d.description
1499 $sth = $dbh->prepare($query);
1500 $sth->execute || $self->dberror($query);
1502 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1503 push @{ $self->{all_departments} }, $ref;
1508 $main::lxdebug->leave_sub();
1511 # this is only used for reports
1512 sub all_departments {
1513 $main::lxdebug->enter_sub();
1515 my ($self, $myconfig, $table) = @_;
1517 my $dbh = $self->dbconnect($myconfig);
1518 my $where = "1 = 1";
1520 if (defined $table) {
1521 if ($table eq 'customer') {
1522 $where = " d.role = 'P'";
1526 my $query = qq|SELECT d.id, d.description
1530 my $sth = $dbh->prepare($query);
1531 $sth->execute || $self->dberror($query);
1533 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1534 push @{ $self->{all_departments} }, $ref;
1540 $main::lxdebug->leave_sub();
1544 $main::lxdebug->enter_sub();
1546 my ($self, $module, $myconfig, $table) = @_;
1548 $self->all_vc($myconfig, $table, $module);
1550 # get last customers or vendors
1553 my $dbh = $self->dbconnect($myconfig);
1557 # now get the account numbers
1559 qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1561 WHERE c.link LIKE '%$module%'
1564 $sth = $dbh->prepare($query);
1565 $sth->execute || $self->dberror($query);
1567 $self->{accounts} = "";
1568 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1570 foreach my $key (split /:/, $ref->{link}) {
1571 if ($key =~ /$module/) {
1573 # cross reference for keys
1574 $xkeyref{ $ref->{accno} } = $key;
1576 push @{ $self->{"${module}_links"}{$key} },
1577 { accno => $ref->{accno},
1578 description => $ref->{description},
1579 taxkey => $ref->{taxkey_id} };
1581 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1587 if (($module eq "AP") || ($module eq "AR")) {
1589 # get tax rates and description
1590 $query = qq| SELECT * FROM tax t|;
1591 $sth = $dbh->prepare($query);
1592 $sth->execute || $self->dberror($query);
1594 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1595 push @{ $self->{TAX} }, $ref;
1601 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1603 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1604 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1605 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1606 c.name AS $table, a.department_id, d.description AS department,
1607 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1608 a.employee_id, e.name AS employee, a.gldate
1610 JOIN $table c ON (a.${table}_id = c.id)
1611 LEFT JOIN employee e ON (e.id = a.employee_id)
1612 LEFT JOIN department d ON (d.id = a.department_id)
1613 WHERE a.id = $self->{id}|;
1614 $sth = $dbh->prepare($query);
1615 $sth->execute || $self->dberror($query);
1617 $ref = $sth->fetchrow_hashref(NAME_lc);
1618 foreach $key (keys %$ref) {
1619 $self->{$key} = $ref->{$key};
1623 # get amounts from individual entries
1624 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1625 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1627 JOIN chart c ON (c.id = a.chart_id)
1628 LEFT JOIN project p ON (p.id = a.project_id)
1629 LEFT Join tax t ON (a.taxkey = t.taxkey)
1630 WHERE a.trans_id = $self->{id}
1631 AND a.fx_transaction = '0'
1632 ORDER BY a.transdate|;
1633 $sth = $dbh->prepare($query);
1634 $sth->execute || $self->dberror($query);
1636 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1638 # get exchangerate for currency
1639 $self->{exchangerate} =
1640 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1643 # store amounts in {acc_trans}{$key} for multiple accounts
1644 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1645 $ref->{exchangerate} =
1646 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1649 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1653 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1654 (SELECT c.accno FROM chart c
1655 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1656 (SELECT c.accno FROM chart c
1657 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1659 $sth = $dbh->prepare($query);
1660 $sth->execute || $self->dberror($query);
1662 $ref = $sth->fetchrow_hashref(NAME_lc);
1663 map { $self->{$_} = $ref->{$_} } keys %$ref;
1669 $query = qq|SELECT current_date AS transdate,
1670 d.curr AS currencies, d.closedto, d.revtrans,
1671 (SELECT c.accno FROM chart c
1672 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1673 (SELECT c.accno FROM chart c
1674 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1676 $sth = $dbh->prepare($query);
1677 $sth->execute || $self->dberror($query);
1679 $ref = $sth->fetchrow_hashref(NAME_lc);
1680 map { $self->{$_} = $ref->{$_} } keys %$ref;
1683 if ($self->{"$self->{vc}_id"}) {
1685 # only setup currency
1686 ($self->{currency}) = split /:/, $self->{currencies};
1690 $self->lastname_used($dbh, $myconfig, $table, $module);
1692 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1694 # get exchangerate for currency
1695 $self->{exchangerate} =
1696 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1705 $main::lxdebug->leave_sub();
1709 $main::lxdebug->enter_sub();
1711 my ($self, $dbh, $myconfig, $table, $module) = @_;
1713 my $arap = ($table eq 'customer') ? "ar" : "ap";
1714 my $where = "1 = 1";
1716 if ($self->{type} =~ /_order/) {
1718 $where = "quotation = '0'";
1720 if ($self->{type} =~ /_quotation/) {
1722 $where = "quotation = '1'";
1725 my $query = qq|SELECT id FROM $arap
1726 WHERE id IN (SELECT MAX(id) FROM $arap
1728 AND ${table}_id > 0)|;
1729 my $sth = $dbh->prepare($query);
1730 $sth->execute || $self->dberror($query);
1732 my ($trans_id) = $sth->fetchrow_array;
1736 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1737 current_date + ct.terms AS duedate, a.department_id,
1738 d.description AS department
1740 JOIN $table ct ON (a.${table}_id = ct.id)
1741 LEFT JOIN department d ON (a.department_id = d.id)
1742 WHERE a.id = $trans_id|;
1743 $sth = $dbh->prepare($query);
1744 $sth->execute || $self->dberror($query);
1746 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1747 $self->{duedate}, $self->{department_id}, $self->{department})
1748 = $sth->fetchrow_array;
1751 $main::lxdebug->leave_sub();
1755 $main::lxdebug->enter_sub();
1757 my ($self, $myconfig, $thisdate, $days) = @_;
1759 my $dbh = $self->dbconnect($myconfig);
1764 my $dateformat = $myconfig->{dateformat};
1765 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1767 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1769 $sth = $dbh->prepare($query);
1770 $sth->execute || $self->dberror($query);
1772 $query = qq|SELECT current_date AS thisdate
1774 $sth = $dbh->prepare($query);
1775 $sth->execute || $self->dberror($query);
1778 ($thisdate) = $sth->fetchrow_array;
1783 $main::lxdebug->leave_sub();
1789 $main::lxdebug->enter_sub();
1791 my ($self, $string) = @_;
1793 if ($string !~ /%/) {
1794 $string = "%$string%";
1797 $string =~ s/\'/\'\'/g;
1799 $main::lxdebug->leave_sub();
1805 $main::lxdebug->enter_sub();
1807 my ($self, $flds, $new, $count, $numrows) = @_;
1811 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1817 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1819 $j = $item->{ndx} - 1;
1820 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1824 for $i ($count + 1 .. $numrows) {
1825 map { delete $self->{"${_}_$i"} } @{$flds};
1828 $main::lxdebug->leave_sub();
1832 $main::lxdebug->enter_sub();
1834 my ($self, $myconfig) = @_;
1838 my $dbh = $self->dbconnect_noauto($myconfig);
1840 my $query = qq|DELETE FROM status
1841 WHERE formname = '$self->{formname}'
1843 my $sth = $dbh->prepare($query) || $self->dberror($query);
1845 if ($self->{formname} =~ /(check|receipt)/) {
1846 for $i (1 .. $self->{rowcount}) {
1847 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1851 $sth->execute($self->{id}) || $self->dberror($query);
1855 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1856 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1858 my %queued = split / /, $self->{queued};
1860 if ($self->{formname} =~ /(check|receipt)/) {
1862 # this is a check or receipt, add one entry for each lineitem
1863 my ($accno) = split /--/, $self->{account};
1864 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1865 chart_id) VALUES (?, '$printed',
1866 '$queued{$self->{formname}}', '$self->{prinform}',
1867 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1868 $sth = $dbh->prepare($query) || $self->dberror($query);
1870 for $i (1 .. $self->{rowcount}) {
1871 if ($self->{"checked_$i"}) {
1872 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1877 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1878 spoolfile, formname)
1879 VALUES ($self->{id}, '$printed', '$emailed',
1880 '$queued{$self->{formname}}', '$self->{formname}')|;
1881 $dbh->do($query) || $self->dberror($query);
1887 $main::lxdebug->leave_sub();
1891 $main::lxdebug->enter_sub();
1893 my ($self, $dbh) = @_;
1895 my ($query, $printed, $emailed);
1897 my $formnames = $self->{printed};
1898 my $emailforms = $self->{emailed};
1900 my $query = qq|DELETE FROM status
1901 WHERE formname = '$self->{formname}'
1902 AND trans_id = $self->{id}|;
1903 $dbh->do($query) || $self->dberror($query);
1905 # this only applies to the forms
1906 # checks and receipts are posted when printed or queued
1908 if ($self->{queued}) {
1909 my %queued = split / /, $self->{queued};
1911 foreach my $formname (keys %queued) {
1912 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1913 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1915 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1916 spoolfile, formname)
1917 VALUES ($self->{id}, '$printed', '$emailed',
1918 '$queued{$formname}', '$formname')|;
1919 $dbh->do($query) || $self->dberror($query);
1921 $formnames =~ s/$self->{formname}//;
1922 $emailforms =~ s/$self->{formname}//;
1927 # save printed, emailed info
1928 $formnames =~ s/^ +//g;
1929 $emailforms =~ s/^ +//g;
1932 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1933 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
1935 foreach my $formname (keys %status) {
1936 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
1937 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
1939 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
1940 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
1941 $dbh->do($query) || $self->dberror($query);
1944 $main::lxdebug->leave_sub();
1947 sub update_defaults {
1948 $main::lxdebug->enter_sub();
1950 my ($self, $myconfig, $fld) = @_;
1952 my $dbh = $self->dbconnect_noauto($myconfig);
1953 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
1954 my $sth = $dbh->prepare($query);
1956 $sth->execute || $self->dberror($query);
1957 my ($var) = $sth->fetchrow_array;
1962 $query = qq|UPDATE defaults
1964 $dbh->do($query) || $form->dberror($query);
1969 $main::lxdebug->leave_sub();
1974 sub update_business {
1975 $main::lxdebug->enter_sub();
1977 my ($self, $myconfig, $business_id) = @_;
1979 my $dbh = $self->dbconnect_noauto($myconfig);
1981 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
1982 my $sth = $dbh->prepare($query);
1984 $sth->execute || $self->dberror($query);
1985 my ($var) = $sth->fetchrow_array;
1990 $query = qq|UPDATE business
1991 SET customernumberinit = '$var' WHERE id=$business_id|;
1992 $dbh->do($query) || $form->dberror($query);
1997 $main::lxdebug->leave_sub();
2003 $main::lxdebug->enter_sub();
2005 my ($self, $myconfig, $salesman) = @_;
2007 my $dbh = $self->dbconnect($myconfig);
2009 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2010 my $sth = $dbh->prepare($query);
2011 $sth->execute || $self->dberror($query);
2014 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2015 push(@{ $self->{salesman_list} }, $ref);
2019 $main::lxdebug->leave_sub();
2024 sub get_partsgroup {
2025 $main::lxdebug->enter_sub();
2027 my ($self, $myconfig, $p) = @_;
2029 my $dbh = $self->dbconnect($myconfig);
2031 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2033 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2035 if ($p->{searchitems} eq 'part') {
2037 WHERE p.inventory_accno_id > 0|;
2039 if ($p->{searchitems} eq 'service') {
2041 WHERE p.inventory_accno_id IS NULL|;
2043 if ($p->{searchitems} eq 'assembly') {
2045 WHERE p.assembly = '1'|;
2047 if ($p->{searchitems} eq 'labor') {
2049 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2053 ORDER BY partsgroup|;
2056 $query = qq|SELECT id, partsgroup FROM partsgroup
2057 ORDER BY partsgroup|;
2060 if ($p->{language_code}) {
2061 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2062 t.description AS translation
2064 JOIN parts p ON (p.partsgroup_id = pg.id)
2065 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2066 ORDER BY translation|;
2069 my $sth = $dbh->prepare($query);
2070 $sth->execute || $self->dberror($query);
2072 $self->{all_partsgroup} = ();
2073 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2074 push @{ $self->{all_partsgroup} }, $ref;
2078 $main::lxdebug->leave_sub();
2083 my ($self, $dbh, $myconfig, $audittrail) = @_;
2085 # table, $reference, $formname, $action, $id, $transdate) = @_;
2092 $dbh = $self->dbconnect($myconfig);
2096 # if we have an id add audittrail, otherwise get a new timestamp
2098 if ($audittrail->{id}) {
2100 $query = qq|SELECT audittrail FROM defaults|;
2102 if ($dbh->selectrow_array($query)) {
2103 my ($null, $employee_id) = $self->get_employee($dbh);
2105 if ($self->{audittrail} && !$myconfig) {
2106 chop $self->{audittrail};
2108 my @a = split /\|/, $self->{audittrail};
2112 my @flds = qw(tablename reference formname action transdate);
2114 # put into hash and remove dups
2116 $key = "$a[2]$a[3]";
2118 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2122 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2123 formname, action, employee_id, transdate)
2124 VALUES ($audittrail->{id}, ?, ?,
2125 ?, ?, $employee_id, ?)|;
2126 my $sth = $dbh->prepare($query) || $self->dberror($query);
2128 foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
2130 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2132 $sth->execute || $self->dberror;
2138 if ($audittrail->{transdate}) {
2139 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2140 formname, action, employee_id, transdate) VALUES (
2141 $audittrail->{id}, '$audittrail->{tablename}', |
2142 .$dbh->quote($audittrail->{reference}).qq|',
2143 '$audittrail->{formname}', '$audittrail->{action}',
2144 $employee_id, '$audittrail->{transdate}')|;
2146 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2147 formname, action, employee_id) VALUES ($audittrail->{id},
2148 '$audittrail->{tablename}', |
2149 .$dbh->quote($audittrail->{reference}).qq|,
2150 '$audittrail->{formname}', '$audittrail->{action}',
2157 $query = qq|SELECT current_timestamp FROM defaults|;
2158 my ($timestamp) = $dbh->selectrow_array($query);
2160 $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2163 $dbh->disconnect if $disconnect;
2172 $main::lxdebug->enter_sub();
2174 my ($type, $country, $NLS_file) = @_;
2178 if ($country && -d "locale/$country") {
2179 $self->{countrycode} = $country;
2180 eval { require "locale/$country/$NLS_file"; };
2183 $self->{NLS_file} = $NLS_file;
2185 push @{ $self->{LONG_MONTH} },
2186 ("January", "February", "March", "April",
2187 "May ", "June", "July", "August",
2188 "September", "October", "November", "December");
2189 push @{ $self->{SHORT_MONTH} },
2190 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2192 $main::lxdebug->leave_sub();
2198 my ($self, $text) = @_;
2200 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2204 $main::lxdebug->enter_sub();
2206 my ($self, $text) = @_;
2208 if (exists $self{subs}{$text}) {
2209 $text = $self{subs}{$text};
2211 if ($self->{countrycode} && $self->{NLS_file}) {
2213 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2217 $main::lxdebug->leave_sub();
2223 $main::lxdebug->enter_sub();
2225 my ($self, $myconfig, $date, $longformat) = @_;
2228 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2233 $spc = $myconfig->{dateformat};
2235 $spc = substr($spc, 1, 1);
2237 if ($date =~ /\D/) {
2238 if ($myconfig->{dateformat} =~ /^yy/) {
2239 ($yy, $mm, $dd) = split /\D/, $date;
2241 if ($myconfig->{dateformat} =~ /^mm/) {
2242 ($mm, $dd, $yy) = split /\D/, $date;
2244 if ($myconfig->{dateformat} =~ /^dd/) {
2245 ($dd, $mm, $yy) = split /\D/, $date;
2248 $date = substr($date, 2);
2249 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2254 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2255 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2257 if ($myconfig->{dateformat} =~ /^dd/) {
2258 if (defined $longformat && $longformat == 0) {
2260 $dd = "0$dd" if ($dd < 10);
2261 $mm = "0$mm" if ($mm < 10);
2262 $longdate = "$dd$spc$mm$spc$yy";
2265 $longdate .= ($spc eq '.') ? ". " : " ";
2266 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2268 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2270 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2271 # Lx-Office is mainly used in Germany or German speaking countries.
2272 if (defined $longformat && $longformat == 0) {
2274 $dd = "0$dd" if ($dd < 10);
2275 $mm = "0$mm" if ($mm < 10);
2276 $longdate = "$yy-$mm-$dd";
2278 $longdate = "$dd. ";
2279 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2282 if (defined $longformat && $longformat == 0) {
2284 $dd = "0$dd" if ($dd < 10);
2285 $mm = "0$mm" if ($mm < 10);
2286 $longdate = "$mm$spc$dd$spc$yy";
2288 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2294 $main::lxdebug->leave_sub();
2300 $main::lxdebug->enter_sub();
2302 my ($self, $myconfig, $date, $longformat) = @_;
2305 $main::lxdebug->leave_sub();
2310 $spc = $myconfig->{dateformat};
2312 $spc = substr($spc, 1, 1);
2314 if ($date =~ /\D/) {
2315 if ($myconfig->{dateformat} =~ /^yy/) {
2316 ($yy, $mm, $dd) = split /\D/, $date;
2317 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2318 ($mm, $dd, $yy) = split /\D/, $date;
2319 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2320 ($dd, $mm, $yy) = split /\D/, $date;
2323 $date = substr($date, 2);
2324 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2329 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2330 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2332 $main::lxdebug->leave_sub();
2333 return ($yy, $mm, $dd);