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.2.0";
144 $self->{dbversion} = "2.2.0";
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;
207 my ($self, $str) = @_;
209 if ($str && !ref($str)) {
210 $str =~ s/"/"/g;
222 print qq|<input type=hidden name="$_" value="|
223 . $self->quote($self->{$_})
227 delete $self->{header};
228 for (sort keys %$self) {
229 print qq|<input type=hidden name="$_" value="|
230 . $self->quote($self->{$_})
238 $main::lxdebug->enter_sub();
240 my ($self, $msg) = @_;
242 if ($ENV{HTTP_USER_AGENT}) {
250 <h2 class=error>Error!</h2>
262 if ($self->{error_function}) {
263 &{ $self->{error_function} }($msg);
269 $main::lxdebug->leave_sub();
273 $main::lxdebug->enter_sub();
275 my ($self, $msg) = @_;
277 if ($ENV{HTTP_USER_AGENT}) {
280 if (!$self->{header}) {
293 if ($self->{info_function}) {
294 &{ $self->{info_function} }($msg);
300 $main::lxdebug->leave_sub();
304 $main::lxdebug->enter_sub();
306 my ($self, $str, $cols, $maxrows) = @_;
310 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
312 $maxrows = $rows unless defined $maxrows;
314 $main::lxdebug->leave_sub();
316 return ($rows > $maxrows) ? $maxrows : $rows;
320 $main::lxdebug->enter_sub();
322 my ($self, $msg) = @_;
324 $self->error("$msg\n" . $DBI::errstr);
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
332 my ($self, $name, $msg) = @_;
334 if ($self->{$name} =~ /^\s*$/) {
337 $main::lxdebug->leave_sub();
341 $main::lxdebug->enter_sub();
345 if ($self->{header}) {
346 $main::lxdebug->leave_sub();
350 my ($stylesheet, $favicon, $charset);
352 if ($ENV{HTTP_USER_AGENT}) {
354 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
356 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
360 if ($self->{favicon} && (-f "$self->{favicon}")) {
362 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
366 if ($self->{charset}) {
368 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
371 if ($self->{landscape}) {
372 $pagelayout = qq|<style type="text/css">
373 \@page { size:landscape; }
376 if ($self->{fokus}) {
377 $fokus = qq|<script type="text/javascript">
379 function fokus(){document.$self->{fokus}.focus();}
386 if ($self->{jsscript} == 1) {
389 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
390 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
391 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
392 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
399 ? "$self->{title} - $self->{titlebar}"
402 print qq|Content-Type: text/html
406 <title>$self->{titlebar}</title>
419 $main::lxdebug->leave_sub();
422 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
423 # changed it to accept an arbitrary number of triggers - sschoeling
425 $main::lxdebug->enter_sub();
428 my $myconfig = shift;
431 # set dateform for jsscript
433 $ifFormat = "%d.%m.%Y";
434 if ($myconfig->{dateformat} eq "dd.mm.yy") {
435 $ifFormat = "%d.%m.%Y";
437 if ($myconfig->{dateformat} eq "dd-mm-yy") {
438 $ifFormat = "%d-%m-%Y";
440 if ($myconfig->{dateformat} eq "dd/mm/yy") {
441 $ifFormat = "%d/%m/%Y";
443 if ($myconfig->{dateformat} eq "mm/dd/yy") {
444 $ifFormat = "%m/%d/%Y";
446 if ($myconfig->{dateformat} eq "mm-dd-yy") {
447 $ifFormat = "%m-%d-%Y";
449 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
450 $ifFormat = "%Y-%m-%d";
462 inputField : "| . (shift) . qq|",
463 ifFormat :"$ifFormat",
464 align : "| . (shift) . qq|",
465 button : "| . (shift) . qq|"
471 <script type="text/javascript">
472 <!--| . join("", @triggers) . qq|//-->
476 $main::lxdebug->leave_sub();
479 } #end sub write_trigger
482 $main::lxdebug->enter_sub();
484 my ($self, $msg) = @_;
486 if ($self->{callback}) {
488 ($script, $argv) = split(/\?/, $self->{callback});
489 exec("perl", "$script", $argv);
497 $main::lxdebug->leave_sub();
500 # sort of columns removed - empty sub
502 $main::lxdebug->enter_sub();
504 my ($self, @columns) = @_;
506 $main::lxdebug->leave_sub();
512 $main::lxdebug->enter_sub();
514 my ($self, $myconfig, $amount, $places, $dash) = @_;
516 #Workaround for $format_amount calls without $places
517 if (!defined $places) {
518 (my $dec) = ($amount =~ /\.(\d+)/);
519 $places = length $dec;
522 if ($places =~ /\d/) {
523 $amount = $self->round_amount($amount, $places);
526 # is the amount negative
527 my $negative = ($amount < 0);
531 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
533 my ($whole, $dec) = split /\./, "$amount";
535 $amount = join '', reverse split //, $whole;
536 $fillup = "0" x ($places - length($dec));
538 if ($myconfig->{numberformat} eq '1,000.00') {
539 $amount =~ s/\d{3,}?/$&,/g;
541 $amount = join '', reverse split //, $amount;
542 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
545 if ($myconfig->{numberformat} eq '1.000,00') {
546 $amount =~ s/\d{3,}?/$&./g;
548 $amount = join '', reverse split //, $amount;
549 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
552 if ($myconfig->{numberformat} eq '1000,00') {
554 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
558 $amount = ($negative) ? "($amount)" : "$amount";
559 } elsif ($dash =~ /DRCR/) {
560 $amount = ($negative) ? "$amount DR" : "$amount CR";
562 $amount = ($negative) ? "-$amount" : "$amount";
566 if ($dash eq "0" && $places) {
567 if ($myconfig->{numberformat} eq '1.000,00') {
568 $amount = "0" . "," . "0" x $places;
570 $amount = "0" . "." . "0" x $places;
573 $amount = ($dash ne "") ? "$dash" : "0";
577 $main::lxdebug->leave_sub();
583 $main::lxdebug->enter_sub();
585 my ($self, $myconfig, $amount) = @_;
586 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
588 if ($myconfig->{in_numberformat} == 1) {
590 # Extra input number format 1000.00 or 1000,00
591 $main::lxdebug->message(LXDebug::DEBUG2,
592 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
595 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
596 $amount = scalar reverse $amount;
598 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
599 $amount =~ s/\./DOT/;
601 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
604 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
605 $amount =~ s/DOT/\./;
607 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
608 $amount = scalar reverse $amount;
609 $main::lxdebug->message(LXDebug::DEBUG2,
610 "Parsed amount:" . $amount . "\n");
612 return ($amount * 1);
615 $main::lxdebug->message(LXDebug::DEBUG2,
616 "in_numberformat: " . $main::locale->text('equal Outputformat'));
617 $main::lxdebug->message(LXDebug::DEBUG2,
618 " = numberformat: $myconfig->{numberformat}");
619 if ( ($myconfig->{numberformat} eq '1.000,00')
620 || ($myconfig->{numberformat} eq '1000,00')) {
625 if ($myconfig->{numberformat} eq "1'000.00") {
631 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
633 $main::lxdebug->leave_sub();
635 return ($amount * 1);
639 $main::lxdebug->enter_sub();
641 my ($self, $amount, $places) = @_;
644 # Rounding like "Kaufmannsrunden"
645 # Descr. http://de.wikipedia.org/wiki/Rundung
647 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
650 $amount = $amount * (10**($places));
651 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
653 $main::lxdebug->leave_sub();
655 return $round_amount;
660 $main::lxdebug->enter_sub();
662 my ($self, $myconfig, $userspath) = @_;
665 # Some variables used for page breaks
666 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
668 my ($current_page, $current_line, $current_row) = (1, 1, 0);
674 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
675 $self->format_string(grep(/notes/, keys(%{$self})));
677 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
678 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
680 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
681 qw(email tel fax name signature));
683 open(IN, "$self->{templates}/$self->{IN}")
684 or $self->error("$self->{IN} : $!");
689 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
691 # OUT is used for the media, screen, printer, email
692 # for postscript we store a copy in a temporary file
694 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
695 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
697 $self->{OUT} = ">$self->{tmpfile}";
701 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
703 open(OUT, ">-") or $self->error("STDOUT : $!");
707 # Do we have to run LaTeX two times? This is needed if
708 # the template contains page references.
711 # first we generate a tmpfile
712 # read file and replace <%variable%>
718 $two_passes = 1 if (/\\pageref/);
721 # detect pagebreak block and its parameters
722 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
723 $chars_per_line = $1;
724 $lines_on_first_page = $2;
725 $lines_on_second_page = $3;
728 last if (/\s*<%end pagebreak%>/);
735 if (/\s*<%foreach /) {
737 # this one we need for the count
739 $var =~ s/\s*<%foreach (.+?)%>/$1/;
741 last if (/\s*<%end /);
747 # display contents of $self->{number}[] array
748 for $i (0 .. $#{ $self->{$var} }) {
751 # Try to detect whether a manual page break is necessary
752 # but only if there was a <%pagebreak ...%> block before
754 if ($chars_per_line) {
756 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
759 my $_description = $self->{"description"}[$i];
760 while ($_description =~ /\\newline/) {
762 $_description =~ s/\\newline//;
764 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
766 if ($current_page == 1) {
767 $lpp = $lines_on_first_page;
769 $lpp = $lines_on_second_page;
772 # Yes we need a manual page break -- or the user has forced one
774 (($current_line + $lines) > $lpp)
775 || ($self->{"_forced_pagebreaks"}
776 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
780 # replace the special variables <%sumcarriedforward%>
783 my $psum = $self->format_amount($myconfig, $sum, 2);
784 $pb =~ s/<%sumcarriedforward%>/$psum/g;
785 $pb =~ s/<%lastpage%>/$current_page/g;
787 # only "normal" variables are supported here
788 # (no <%if, no <%foreach, no <%include)
790 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
792 # page break block is ready to rock
797 $current_line += $lines;
800 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
804 # don't parse par, we need it for each line
806 s/<%(.+?)%>/$self->{$1}[$i]/mg;
812 # if not comes before if!
813 if (/\s*<%if not /) {
815 # check if it is not set and display
817 s/\s*<%if not (.+?)%>/$1/;
819 unless ($self->{$_}) {
821 last if (/\s*<%end /);
831 last if (/\s*<%end /);
839 # check if it is set and display
841 s/\s*<%if (.+?)%>/$1/;
845 last if (/\s*<%end /);
855 last if (/\s*<%end /);
861 # check for <%include filename%>
862 if (/\s*<%include /) {
866 $var =~ s/\s*<%include (.+?)%>/$1/;
869 $var =~ s/(\/|\.\.)//g;
871 # prevent the infinite loop!
872 next if ($self->{"$var"});
874 open(INC, "$self->{templates}/$var")
875 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
884 s/<%(.+?)%>/$self->{$1}/g;
885 s/<nobr><\/nobr>/ /g;
892 # Convert the tex file to postscript
893 if ($self->{format} =~ /(postscript|pdf)/) {
896 $self->{cwd} = cwd();
897 $self->{tmpdir} = "$self->{cwd}/$userspath";
899 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
901 $self->{tmpfile} =~ s/$userspath\///g;
903 if ($self->{format} eq 'postscript') {
905 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
907 $self->error($self->cleanup) if ($?);
910 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
912 $self->error($self->cleanup) if ($?);
915 $self->{tmpfile} =~ s/tex$/dvi/;
917 system("dvips $self->{tmpfile} -o -q > /dev/null");
918 $self->error($self->cleanup . "dvips : $!") if ($?);
919 $self->{tmpfile} =~ s/dvi$/ps/;
921 if ($self->{format} eq 'pdf') {
923 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
925 $self->error($self->cleanup) if ($?);
928 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
930 $self->error($self->cleanup) if ($?);
932 $self->{tmpfile} =~ s/tex$/pdf/;
937 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
939 if ($self->{media} eq 'email') {
943 my $mail = new Mailer;
945 map { $mail->{$_} = $self->{$_} }
946 qw(cc bcc subject message version format charset);
947 $mail->{to} = qq|$self->{email}|;
948 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
949 $mail->{fileid} = "$fileid.";
951 # if we send html or plain text inline
952 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
953 $mail->{contenttype} = "text/html";
955 $mail->{message} =~ s/\r\n/<br>\n/g;
956 $myconfig->{signature} =~ s/\\n/<br>\n/g;
957 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
959 open(IN, $self->{tmpfile})
960 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
962 $mail->{message} .= $_;
969 @{ $mail->{attachments} } = ($self->{tmpfile});
971 $myconfig->{signature} =~ s/\\n/\r\n/g;
972 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
976 my $err = $mail->send($out);
977 $self->error($self->cleanup . "$err") if ($err);
983 my $numbytes = (-s $self->{tmpfile});
984 open(IN, $self->{tmpfile})
985 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
987 $self->{copies} = 1 unless $self->{media} eq 'printer';
989 chdir("$self->{cwd}");
991 for my $i (1 .. $self->{copies}) {
993 open(OUT, $self->{OUT})
994 or $self->error($self->cleanup . "$self->{OUT} : $!");
998 print qq|Content-Type: application/$self->{format}
999 Content-Disposition: attachment; filename="$self->{tmpfile}"
1000 Content-Length: $numbytes
1004 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1024 chdir("$self->{cwd}");
1025 $main::lxdebug->leave_sub();
1029 $main::lxdebug->enter_sub();
1033 chdir("$self->{tmpdir}");
1036 if (-f "$self->{tmpfile}.err") {
1037 open(FH, "$self->{tmpfile}.err");
1042 if ($self->{tmpfile}) {
1045 $self->{tmpfile} =~ s/\.\w+$//g;
1046 my $tmpfile = $self->{tmpfile};
1047 unlink(<$tmpfile.*>);
1050 chdir("$self->{cwd}");
1052 $main::lxdebug->leave_sub();
1058 $main::lxdebug->enter_sub();
1060 my ($self, @fields) = @_;
1063 %unique_fields = map({ $_ => 1 } @fields);
1064 @fields = keys(%unique_fields);
1066 foreach my $field (@fields) {
1067 next unless ($self->{$field} =~ /\<pagebreak\>/);
1068 $self->{$field} =~ s/\<pagebreak\>//g;
1069 if ($field =~ /.*_(\d+)$/) {
1070 if (!$self->{"_forced_pagebreaks"}) {
1071 $self->{"_forced_pagebreaks"} = [];
1073 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1077 my $format = $self->{format};
1078 if ($self->{format} =~ /(postscript|pdf)/) {
1085 '<', '>', quotemeta('\n'), '
1089 '&', quotemeta('\n'), '
1091 '"', '\$', '%', '_', '#', quotemeta('^'),
1092 '{', '}', '<', '>', '£', "\r"
1098 quotemeta('\n') => '<br>',
1109 quotemeta('^') => '\^\\',
1114 quotemeta('\n') => '\newline ',
1121 foreach my $key (@{ $replace{order}{$format} }) {
1122 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1125 # Allow some HTML markup to be converted into the output format's
1126 # corresponding markup code, e.g. bold or italic.
1127 if ('html' eq $format) {
1128 my @markup_replace = ('b', 'i', 's', 'u');
1130 foreach my $key (@markup_replace) {
1131 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1134 } elsif ('tex' eq $format) {
1135 my %markup_replace = ('b' => 'textbf',
1137 'u' => 'underline');
1139 foreach my $field (@fields) {
1140 foreach my $key (keys(%markup_replace)) {
1141 my $new = $markup_replace{$key};
1143 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1148 $main::lxdebug->leave_sub();
1152 $main::lxdebug->enter_sub();
1154 my ($self, $date, $myconfig) = @_;
1156 if ($date && $date =~ /\D/) {
1158 if ($myconfig->{dateformat} =~ /^yy/) {
1159 ($yy, $mm, $dd) = split /\D/, $date;
1161 if ($myconfig->{dateformat} =~ /^mm/) {
1162 ($mm, $dd, $yy) = split /\D/, $date;
1164 if ($myconfig->{dateformat} =~ /^dd/) {
1165 ($dd, $mm, $yy) = split /\D/, $date;
1170 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1171 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1173 $dd = "0$dd" if ($dd < 10);
1174 $mm = "0$mm" if ($mm < 10);
1176 $date = "$yy$mm$dd";
1179 $main::lxdebug->leave_sub();
1184 # Database routines used throughout
1187 $main::lxdebug->enter_sub();
1189 my ($self, $myconfig) = @_;
1191 # connect to database
1193 DBI->connect($myconfig->{dbconnect},
1194 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1198 if ($myconfig->{dboptions}) {
1199 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1202 $main::lxdebug->leave_sub();
1207 sub dbconnect_noauto {
1208 $main::lxdebug->enter_sub();
1210 my ($self, $myconfig) = @_;
1212 # connect to database
1214 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1215 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1219 if ($myconfig->{dboptions}) {
1220 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1223 $main::lxdebug->leave_sub();
1228 sub update_balance {
1229 $main::lxdebug->enter_sub();
1231 my ($self, $dbh, $table, $field, $where, $value) = @_;
1233 # if we have a value, go do it
1236 # retrieve balance from table
1237 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1238 my $sth = $dbh->prepare($query);
1240 $sth->execute || $self->dberror($query);
1241 my ($balance) = $sth->fetchrow_array;
1247 $query = "UPDATE $table SET $field = $balance WHERE $where";
1248 $dbh->do($query) || $self->dberror($query);
1250 $main::lxdebug->leave_sub();
1253 sub update_exchangerate {
1254 $main::lxdebug->enter_sub();
1256 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1258 # some sanity check for currency
1260 $main::lxdebug->leave_sub();
1264 my $query = qq|SELECT e.curr FROM exchangerate e
1265 WHERE e.curr = '$curr'
1266 AND e.transdate = '$transdate'
1268 my $sth = $dbh->prepare($query);
1269 $sth->execute || $self->dberror($query);
1272 if ($buy != 0 && $sell != 0) {
1273 $set = "buy = $buy, sell = $sell";
1274 } elsif ($buy != 0) {
1275 $set = "buy = $buy";
1276 } elsif ($sell != 0) {
1277 $set = "sell = $sell";
1280 if ($sth->fetchrow_array) {
1281 $query = qq|UPDATE exchangerate
1283 WHERE curr = '$curr'
1284 AND transdate = '$transdate'|;
1286 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1287 VALUES ('$curr', $buy, $sell, '$transdate')|;
1290 $dbh->do($query) || $self->dberror($query);
1292 $main::lxdebug->leave_sub();
1295 sub save_exchangerate {
1296 $main::lxdebug->enter_sub();
1298 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1300 my $dbh = $self->dbconnect($myconfig);
1302 my ($buy, $sell) = (0, 0);
1303 $buy = $rate if $fld eq 'buy';
1304 $sell = $rate if $fld eq 'sell';
1306 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1310 $main::lxdebug->leave_sub();
1313 sub get_exchangerate {
1314 $main::lxdebug->enter_sub();
1316 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1318 unless ($transdate) {
1319 $main::lxdebug->leave_sub();
1323 my $query = qq|SELECT e.$fld FROM exchangerate e
1324 WHERE e.curr = '$curr'
1325 AND e.transdate = '$transdate'|;
1326 my $sth = $dbh->prepare($query);
1327 $sth->execute || $self->dberror($query);
1329 my ($exchangerate) = $sth->fetchrow_array;
1332 $main::lxdebug->leave_sub();
1334 return $exchangerate;
1337 sub check_exchangerate {
1338 $main::lxdebug->enter_sub();
1340 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1342 unless ($transdate) {
1343 $main::lxdebug->leave_sub();
1347 my $dbh = $self->dbconnect($myconfig);
1349 my $query = qq|SELECT e.$fld FROM exchangerate e
1350 WHERE e.curr = '$currency'
1351 AND e.transdate = '$transdate'|;
1352 my $sth = $dbh->prepare($query);
1353 $sth->execute || $self->dberror($query);
1355 my ($exchangerate) = $sth->fetchrow_array;
1359 $main::lxdebug->leave_sub();
1361 return $exchangerate;
1365 $main::lxdebug->enter_sub();
1367 my ($self, $dbh, $id) = @_;
1371 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1373 if ($self->{"shipto$item"}) {
1374 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1376 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1381 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1382 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1383 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1384 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1385 '$self->{shiptozipcode}', '$self->{shiptocity}',
1386 '$self->{shiptocountry}', '$self->{shiptocontact}',
1387 '$self->{shiptophone}', '$self->{shiptofax}',
1388 '$self->{shiptoemail}')|;
1389 $dbh->do($query) || $self->dberror($query);
1392 $main::lxdebug->leave_sub();
1396 $main::lxdebug->enter_sub();
1398 my ($self, $dbh) = @_;
1400 my $query = qq|SELECT e.id, e.name FROM employee e
1401 WHERE e.login = '$self->{login}'|;
1402 my $sth = $dbh->prepare($query);
1403 $sth->execute || $self->dberror($query);
1405 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1406 $self->{employee_id} *= 1;
1410 $main::lxdebug->leave_sub();
1413 # get other contact for transaction and form - html/tex
1415 $main::lxdebug->enter_sub();
1417 my ($self, $dbh, $id) = @_;
1419 my $query = qq|SELECT c.*
1422 $sth = $dbh->prepare($query);
1423 $sth->execute || $self->dberror($query);
1425 $ref = $sth->fetchrow_hashref(NAME_lc);
1427 push @{ $self->{$_} }, $ref;
1430 $main::lxdebug->leave_sub();
1433 # get contacts for id, if no contact return {"","","","",""}
1435 $main::lxdebug->enter_sub();
1437 my ($self, $dbh, $id) = @_;
1439 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1441 WHERE cp_cv_id=$id|;
1442 my $sth = $dbh->prepare($query);
1443 $sth->execute || $self->dberror($query);
1446 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1447 push @{ $self->{all_contacts} }, $ref;
1452 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1455 $main::lxdebug->leave_sub();
1458 # this sub gets the id and name from $table
1460 $main::lxdebug->enter_sub();
1462 my ($self, $myconfig, $table) = @_;
1464 # connect to database
1465 my $dbh = $self->dbconnect($myconfig);
1467 my $name = $self->like(lc $self->{$table});
1468 my $customernumber = $self->like(lc $self->{customernumber});
1470 if ($self->{customernumber} ne "") {
1471 $query = qq~SELECT c.id, c.name,
1472 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1474 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1477 $query = qq~SELECT c.id, c.name,
1478 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1480 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1484 if ($self->{openinvoices}) {
1485 $query = qq~SELECT DISTINCT c.id, c.name,
1486 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1487 FROM $self->{arap} a
1488 JOIN $table c ON (a.${table}_id = c.id)
1489 WHERE NOT a.amount = a.paid
1490 AND lower(c.name) LIKE '$name'
1493 my $sth = $dbh->prepare($query);
1495 $sth->execute || $self->dberror($query);
1498 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1499 push(@{ $self->{name_list} }, $ref);
1505 $main::lxdebug->leave_sub();
1510 # the selection sub is used in the AR, AP, IS, IR and OE module
1513 $main::lxdebug->enter_sub();
1515 my ($self, $myconfig, $table, $module) = @_;
1518 my $dbh = $self->dbconnect($myconfig);
1520 my $query = qq|SELECT count(*) FROM $table|;
1521 my $sth = $dbh->prepare($query);
1522 $sth->execute || $self->dberror($query);
1523 my ($count) = $sth->fetchrow_array;
1526 # build selection list
1527 if ($count < $myconfig->{vclimit}) {
1528 $query = qq|SELECT id, name
1529 FROM $table WHERE not obsolete
1531 $sth = $dbh->prepare($query);
1532 $sth->execute || $self->dberror($query);
1534 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1535 push @{ $self->{"all_$table"} }, $ref;
1543 $self->get_employee($dbh);
1545 # setup sales contacts
1546 $query = qq|SELECT e.id, e.name
1549 AND NOT e.id = $self->{employee_id}|;
1550 $sth = $dbh->prepare($query);
1551 $sth->execute || $self->dberror($query);
1553 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1554 push @{ $self->{all_employees} }, $ref;
1559 push @{ $self->{all_employees} },
1560 { id => $self->{employee_id},
1561 name => $self->{employee} };
1563 # sort the whole thing
1564 @{ $self->{all_employees} } =
1565 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1567 if ($module eq 'AR') {
1569 # prepare query for departments
1570 $query = qq|SELECT d.id, d.description
1576 $query = qq|SELECT d.id, d.description
1581 $sth = $dbh->prepare($query);
1582 $sth->execute || $self->dberror($query);
1584 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1585 push @{ $self->{all_departments} }, $ref;
1590 $main::lxdebug->leave_sub();
1593 # this is only used for reports
1594 sub all_departments {
1595 $main::lxdebug->enter_sub();
1597 my ($self, $myconfig, $table) = @_;
1599 my $dbh = $self->dbconnect($myconfig);
1600 my $where = "1 = 1";
1602 if (defined $table) {
1603 if ($table eq 'customer') {
1604 $where = " d.role = 'P'";
1608 my $query = qq|SELECT d.id, d.description
1612 my $sth = $dbh->prepare($query);
1613 $sth->execute || $self->dberror($query);
1615 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1616 push @{ $self->{all_departments} }, $ref;
1622 $main::lxdebug->leave_sub();
1626 $main::lxdebug->enter_sub();
1628 my ($self, $module, $myconfig, $table) = @_;
1630 $self->all_vc($myconfig, $table, $module);
1632 # get last customers or vendors
1635 my $dbh = $self->dbconnect($myconfig);
1639 # now get the account numbers
1640 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1642 WHERE c.link LIKE '%$module%'
1645 $sth = $dbh->prepare($query);
1646 $sth->execute || $self->dberror($query);
1648 $self->{accounts} = "";
1649 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1651 foreach my $key (split /:/, $ref->{link}) {
1652 if ($key =~ /$module/) {
1654 # cross reference for keys
1655 $xkeyref{ $ref->{accno} } = $key;
1657 push @{ $self->{"${module}_links"}{$key} },
1658 { accno => $ref->{accno},
1659 description => $ref->{description},
1660 taxkey => $ref->{taxkey_id} };
1662 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1668 if (($module eq "AP") || ($module eq "AR")) {
1670 # get tax rates and description
1671 $query = qq| SELECT * FROM tax t|;
1672 $sth = $dbh->prepare($query);
1673 $sth->execute || $self->dberror($query);
1675 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1676 push @{ $self->{TAX} }, $ref;
1682 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1684 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1685 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1686 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1687 c.name AS $table, a.department_id, d.description AS department,
1688 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1689 a.employee_id, e.name AS employee, a.gldate
1691 JOIN $table c ON (a.${table}_id = c.id)
1692 LEFT JOIN employee e ON (e.id = a.employee_id)
1693 LEFT JOIN department d ON (d.id = a.department_id)
1694 WHERE a.id = $self->{id}|;
1695 $sth = $dbh->prepare($query);
1696 $sth->execute || $self->dberror($query);
1698 $ref = $sth->fetchrow_hashref(NAME_lc);
1699 foreach $key (keys %$ref) {
1700 $self->{$key} = $ref->{$key};
1704 # get amounts from individual entries
1705 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1706 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1708 JOIN chart c ON (c.id = a.chart_id)
1709 LEFT JOIN project p ON (p.id = a.project_id)
1710 LEFT Join tax t ON (a.taxkey = t.taxkey)
1711 WHERE a.trans_id = $self->{id}
1712 AND a.fx_transaction = '0'
1713 ORDER BY a.oid,a.transdate|;
1714 $sth = $dbh->prepare($query);
1715 $sth->execute || $self->dberror($query);
1717 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1719 # get exchangerate for currency
1720 $self->{exchangerate} =
1721 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1725 # store amounts in {acc_trans}{$key} for multiple accounts
1726 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1727 $ref->{exchangerate} =
1728 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1730 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1733 $ref->{index} = $index;
1735 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1739 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1740 (SELECT c.accno FROM chart c
1741 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1742 (SELECT c.accno FROM chart c
1743 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1745 $sth = $dbh->prepare($query);
1746 $sth->execute || $self->dberror($query);
1748 $ref = $sth->fetchrow_hashref(NAME_lc);
1749 map { $self->{$_} = $ref->{$_} } keys %$ref;
1755 $query = qq|SELECT current_date AS transdate,
1756 d.curr AS currencies, d.closedto, d.revtrans,
1757 (SELECT c.accno FROM chart c
1758 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1759 (SELECT c.accno FROM chart c
1760 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1762 $sth = $dbh->prepare($query);
1763 $sth->execute || $self->dberror($query);
1765 $ref = $sth->fetchrow_hashref(NAME_lc);
1766 map { $self->{$_} = $ref->{$_} } keys %$ref;
1769 if ($self->{"$self->{vc}_id"}) {
1771 # only setup currency
1772 ($self->{currency}) = split /:/, $self->{currencies};
1776 $self->lastname_used($dbh, $myconfig, $table, $module);
1778 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1780 # get exchangerate for currency
1781 $self->{exchangerate} =
1782 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1791 $main::lxdebug->leave_sub();
1795 $main::lxdebug->enter_sub();
1797 my ($self, $dbh, $myconfig, $table, $module) = @_;
1799 my $arap = ($table eq 'customer') ? "ar" : "ap";
1800 my $where = "1 = 1";
1802 if ($self->{type} =~ /_order/) {
1804 $where = "quotation = '0'";
1806 if ($self->{type} =~ /_quotation/) {
1808 $where = "quotation = '1'";
1811 my $query = qq|SELECT MAX(id) FROM $arap
1813 AND ${table}_id > 0|;
1814 my $sth = $dbh->prepare($query);
1815 $sth->execute || $self->dberror($query);
1817 my ($trans_id) = $sth->fetchrow_array;
1821 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1822 current_date + ct.terms AS duedate, a.department_id,
1823 d.description AS department
1825 JOIN $table ct ON (a.${table}_id = ct.id)
1826 LEFT JOIN department d ON (a.department_id = d.id)
1827 WHERE a.id = $trans_id|;
1828 $sth = $dbh->prepare($query);
1829 $sth->execute || $self->dberror($query);
1831 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1832 $self->{duedate}, $self->{department_id}, $self->{department})
1833 = $sth->fetchrow_array;
1836 $main::lxdebug->leave_sub();
1840 $main::lxdebug->enter_sub();
1842 my ($self, $myconfig, $thisdate, $days) = @_;
1844 my $dbh = $self->dbconnect($myconfig);
1849 my $dateformat = $myconfig->{dateformat};
1850 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1852 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1854 $sth = $dbh->prepare($query);
1855 $sth->execute || $self->dberror($query);
1857 $query = qq|SELECT current_date AS thisdate
1859 $sth = $dbh->prepare($query);
1860 $sth->execute || $self->dberror($query);
1863 ($thisdate) = $sth->fetchrow_array;
1868 $main::lxdebug->leave_sub();
1874 $main::lxdebug->enter_sub();
1876 my ($self, $string) = @_;
1878 if ($string !~ /%/) {
1879 $string = "%$string%";
1882 $string =~ s/\'/\'\'/g;
1884 $main::lxdebug->leave_sub();
1890 $main::lxdebug->enter_sub();
1892 my ($self, $flds, $new, $count, $numrows) = @_;
1896 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1902 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1904 $j = $item->{ndx} - 1;
1905 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1909 for $i ($count + 1 .. $numrows) {
1910 map { delete $self->{"${_}_$i"} } @{$flds};
1913 $main::lxdebug->leave_sub();
1917 $main::lxdebug->enter_sub();
1919 my ($self, $myconfig) = @_;
1923 my $dbh = $self->dbconnect_noauto($myconfig);
1925 my $query = qq|DELETE FROM status
1926 WHERE formname = '$self->{formname}'
1928 my $sth = $dbh->prepare($query) || $self->dberror($query);
1930 if ($self->{formname} =~ /(check|receipt)/) {
1931 for $i (1 .. $self->{rowcount}) {
1932 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1936 $sth->execute($self->{id}) || $self->dberror($query);
1940 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1941 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1943 my %queued = split / /, $self->{queued};
1945 if ($self->{formname} =~ /(check|receipt)/) {
1947 # this is a check or receipt, add one entry for each lineitem
1948 my ($accno) = split /--/, $self->{account};
1949 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1950 chart_id) VALUES (?, '$printed',
1951 '$queued{$self->{formname}}', '$self->{prinform}',
1952 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1953 $sth = $dbh->prepare($query) || $self->dberror($query);
1955 for $i (1 .. $self->{rowcount}) {
1956 if ($self->{"checked_$i"}) {
1957 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1962 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1963 spoolfile, formname)
1964 VALUES ($self->{id}, '$printed', '$emailed',
1965 '$queued{$self->{formname}}', '$self->{formname}')|;
1966 $dbh->do($query) || $self->dberror($query);
1972 $main::lxdebug->leave_sub();
1976 $main::lxdebug->enter_sub();
1978 my ($self, $dbh) = @_;
1980 my ($query, $printed, $emailed);
1982 my $formnames = $self->{printed};
1983 my $emailforms = $self->{emailed};
1985 my $query = qq|DELETE FROM status
1986 WHERE formname = '$self->{formname}'
1987 AND trans_id = $self->{id}|;
1988 $dbh->do($query) || $self->dberror($query);
1990 # this only applies to the forms
1991 # checks and receipts are posted when printed or queued
1993 if ($self->{queued}) {
1994 my %queued = split / /, $self->{queued};
1996 foreach my $formname (keys %queued) {
1997 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1998 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2000 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2001 spoolfile, formname)
2002 VALUES ($self->{id}, '$printed', '$emailed',
2003 '$queued{$formname}', '$formname')|;
2004 $dbh->do($query) || $self->dberror($query);
2006 $formnames =~ s/$self->{formname}//;
2007 $emailforms =~ s/$self->{formname}//;
2012 # save printed, emailed info
2013 $formnames =~ s/^ +//g;
2014 $emailforms =~ s/^ +//g;
2017 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2018 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2020 foreach my $formname (keys %status) {
2021 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2022 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2024 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2025 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2026 $dbh->do($query) || $self->dberror($query);
2029 $main::lxdebug->leave_sub();
2032 sub update_defaults {
2033 $main::lxdebug->enter_sub();
2035 my ($self, $myconfig, $fld) = @_;
2037 my $dbh = $self->dbconnect_noauto($myconfig);
2038 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2039 my $sth = $dbh->prepare($query);
2041 $sth->execute || $self->dberror($query);
2042 my ($var) = $sth->fetchrow_array;
2047 $query = qq|UPDATE defaults
2049 $dbh->do($query) || $form->dberror($query);
2054 $main::lxdebug->leave_sub();
2059 sub update_business {
2060 $main::lxdebug->enter_sub();
2062 my ($self, $myconfig, $business_id) = @_;
2064 my $dbh = $self->dbconnect_noauto($myconfig);
2066 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2067 my $sth = $dbh->prepare($query);
2069 $sth->execute || $self->dberror($query);
2070 my ($var) = $sth->fetchrow_array;
2075 $query = qq|UPDATE business
2076 SET customernumberinit = '$var' WHERE id=$business_id|;
2077 $dbh->do($query) || $form->dberror($query);
2082 $main::lxdebug->leave_sub();
2088 $main::lxdebug->enter_sub();
2090 my ($self, $myconfig, $salesman) = @_;
2092 my $dbh = $self->dbconnect($myconfig);
2094 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2095 my $sth = $dbh->prepare($query);
2096 $sth->execute || $self->dberror($query);
2099 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2100 push(@{ $self->{salesman_list} }, $ref);
2104 $main::lxdebug->leave_sub();
2109 sub get_partsgroup {
2110 $main::lxdebug->enter_sub();
2112 my ($self, $myconfig, $p) = @_;
2114 my $dbh = $self->dbconnect($myconfig);
2116 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2118 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2120 if ($p->{searchitems} eq 'part') {
2122 WHERE p.inventory_accno_id > 0|;
2124 if ($p->{searchitems} eq 'service') {
2126 WHERE p.inventory_accno_id IS NULL|;
2128 if ($p->{searchitems} eq 'assembly') {
2130 WHERE p.assembly = '1'|;
2132 if ($p->{searchitems} eq 'labor') {
2134 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2138 ORDER BY partsgroup|;
2141 $query = qq|SELECT id, partsgroup FROM partsgroup
2142 ORDER BY partsgroup|;
2145 if ($p->{language_code}) {
2146 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2147 t.description AS translation
2149 JOIN parts p ON (p.partsgroup_id = pg.id)
2150 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2151 ORDER BY translation|;
2154 my $sth = $dbh->prepare($query);
2155 $sth->execute || $self->dberror($query);
2157 $self->{all_partsgroup} = ();
2158 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2159 push @{ $self->{all_partsgroup} }, $ref;
2163 $main::lxdebug->leave_sub();
2166 sub get_pricegroup {
2167 $main::lxdebug->enter_sub();
2169 my ($self, $myconfig, $p) = @_;
2171 my $dbh = $self->dbconnect($myconfig);
2173 my $query = qq|SELECT p.id, p.pricegroup
2177 ORDER BY pricegroup|;
2180 $query = qq|SELECT id, pricegroup FROM pricegroup
2181 ORDER BY pricegroup|;
2184 my $sth = $dbh->prepare($query);
2185 $sth->execute || $self->dberror($query);
2187 $self->{all_pricegroup} = ();
2188 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2189 push @{ $self->{all_pricegroup} }, $ref;
2194 $main::lxdebug->leave_sub();
2198 my ($self, $dbh, $myconfig, $audittrail) = @_;
2200 # table, $reference, $formname, $action, $id, $transdate) = @_;
2207 $dbh = $self->dbconnect($myconfig);
2211 # if we have an id add audittrail, otherwise get a new timestamp
2213 if ($audittrail->{id}) {
2215 $query = qq|SELECT audittrail FROM defaults|;
2217 if ($dbh->selectrow_array($query)) {
2218 my ($null, $employee_id) = $self->get_employee($dbh);
2220 if ($self->{audittrail} && !$myconfig) {
2221 chop $self->{audittrail};
2223 my @a = split /\|/, $self->{audittrail};
2227 my @flds = qw(tablename reference formname action transdate);
2229 # put into hash and remove dups
2231 $key = "$a[2]$a[3]";
2233 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2237 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2238 formname, action, employee_id, transdate)
2239 VALUES ($audittrail->{id}, ?, ?,
2240 ?, ?, $employee_id, ?)|;
2241 my $sth = $dbh->prepare($query) || $self->dberror($query);
2245 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2249 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2251 $sth->execute || $self->dberror;
2256 if ($audittrail->{transdate}) {
2257 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2258 formname, action, employee_id, transdate) VALUES (
2259 $audittrail->{id}, '$audittrail->{tablename}', |
2260 . $dbh->quote($audittrail->{reference}) . qq|,
2261 '$audittrail->{formname}', '$audittrail->{action}',
2262 $employee_id, '$audittrail->{transdate}')|;
2264 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2265 formname, action, employee_id) VALUES ($audittrail->{id},
2266 '$audittrail->{tablename}', |
2267 . $dbh->quote($audittrail->{reference}) . qq|,
2268 '$audittrail->{formname}', '$audittrail->{action}',
2275 $query = qq|SELECT current_timestamp FROM defaults|;
2276 my ($timestamp) = $dbh->selectrow_array($query);
2279 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2282 $dbh->disconnect if $disconnect;
2291 $main::lxdebug->enter_sub();
2293 my ($type, $country, $NLS_file) = @_;
2297 if ($country && -d "locale/$country") {
2298 $self->{countrycode} = $country;
2299 eval { require "locale/$country/$NLS_file"; };
2302 $self->{NLS_file} = $NLS_file;
2304 push @{ $self->{LONG_MONTH} },
2305 ("January", "February", "March", "April",
2306 "May ", "June", "July", "August",
2307 "September", "October", "November", "December");
2308 push @{ $self->{SHORT_MONTH} },
2309 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2311 $main::lxdebug->leave_sub();
2317 my ($self, $text) = @_;
2319 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2323 $main::lxdebug->enter_sub();
2325 my ($self, $text) = @_;
2327 if (exists $self{subs}{$text}) {
2328 $text = $self{subs}{$text};
2330 if ($self->{countrycode} && $self->{NLS_file}) {
2332 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2336 $main::lxdebug->leave_sub();
2342 $main::lxdebug->enter_sub();
2344 my ($self, $myconfig, $date, $longformat) = @_;
2347 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2352 $spc = $myconfig->{dateformat};
2354 $spc = substr($spc, 1, 1);
2356 if ($date =~ /\D/) {
2357 if ($myconfig->{dateformat} =~ /^yy/) {
2358 ($yy, $mm, $dd) = split /\D/, $date;
2360 if ($myconfig->{dateformat} =~ /^mm/) {
2361 ($mm, $dd, $yy) = split /\D/, $date;
2363 if ($myconfig->{dateformat} =~ /^dd/) {
2364 ($dd, $mm, $yy) = split /\D/, $date;
2367 $date = substr($date, 2);
2368 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2373 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2374 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2376 if ($myconfig->{dateformat} =~ /^dd/) {
2377 if (defined $longformat && $longformat == 0) {
2379 $dd = "0$dd" if ($dd < 10);
2380 $mm = "0$mm" if ($mm < 10);
2381 $longdate = "$dd$spc$mm$spc$yy";
2384 $longdate .= ($spc eq '.') ? ". " : " ";
2385 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2387 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2389 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2390 # Lx-Office is mainly used in Germany or German speaking countries.
2391 if (defined $longformat && $longformat == 0) {
2393 $dd = "0$dd" if ($dd < 10);
2394 $mm = "0$mm" if ($mm < 10);
2395 $longdate = "$yy-$mm-$dd";
2397 $longdate = "$dd. ";
2398 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2401 if (defined $longformat && $longformat == 0) {
2403 $dd = "0$dd" if ($dd < 10);
2404 $mm = "0$mm" if ($mm < 10);
2405 $longdate = "$mm$spc$dd$spc$yy";
2407 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2413 $main::lxdebug->leave_sub();
2419 $main::lxdebug->enter_sub();
2421 my ($self, $myconfig, $date, $longformat) = @_;
2424 $main::lxdebug->leave_sub();
2429 $spc = $myconfig->{dateformat};
2431 $spc = substr($spc, 1, 1);
2433 if ($date =~ /\D/) {
2434 if ($myconfig->{dateformat} =~ /^yy/) {
2435 ($yy, $mm, $dd) = split /\D/, $date;
2436 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2437 ($mm, $dd, $yy) = split /\D/, $date;
2438 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2439 ($dd, $mm, $yy) = split /\D/, $date;
2442 $date = substr($date, 2);
2443 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2448 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2449 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2451 $main::lxdebug->leave_sub();
2452 return ($yy, $mm, $dd);