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
405 <title>$self->{titlebar}</title>
418 $main::lxdebug->leave_sub();
421 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
422 # changed it to accept an arbitrary number of triggers - sschoeling
424 $main::lxdebug->enter_sub();
427 my $myconfig = shift;
430 # set dateform for jsscript
432 $ifFormat = "%d.%m.%Y";
433 if ($myconfig->{dateformat} eq "dd.mm.yy") {
434 $ifFormat = "%d.%m.%Y";
436 if ($myconfig->{dateformat} eq "dd-mm-yy") {
437 $ifFormat = "%d-%m-%Y";
439 if ($myconfig->{dateformat} eq "dd/mm/yy") {
440 $ifFormat = "%d/%m/%Y";
442 if ($myconfig->{dateformat} eq "mm/dd/yy") {
443 $ifFormat = "%m/%d/%Y";
445 if ($myconfig->{dateformat} eq "mm-dd-yy") {
446 $ifFormat = "%m-%d-%Y";
448 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
449 $ifFormat = "%Y-%m-%d";
461 inputField : "| . (shift) . qq|",
462 ifFormat :"$ifFormat",
463 align : "| . (shift) . qq|",
464 button : "| . (shift) . qq|"
470 <script type="text/javascript">
471 <!--| . join("", @triggers) . qq|//-->
475 $main::lxdebug->leave_sub();
478 } #end sub write_trigger
481 $main::lxdebug->enter_sub();
483 my ($self, $msg) = @_;
485 if ($self->{callback}) {
487 ($script, $argv) = split(/\?/, $self->{callback});
488 exec("perl", "$script", $argv);
496 $main::lxdebug->leave_sub();
499 # sort of columns removed - empty sub
501 $main::lxdebug->enter_sub();
503 my ($self, @columns) = @_;
505 $main::lxdebug->leave_sub();
511 $main::lxdebug->enter_sub();
513 my ($self, $myconfig, $amount, $places, $dash) = @_;
515 #Workaround for $format_amount calls without $places
516 if (!defined $places) {
517 (my $dec) = ($amount =~ /\.(\d+)/);
518 $places = length $dec;
521 if ($places =~ /\d/) {
522 $amount = $self->round_amount($amount, $places);
525 # is the amount negative
526 my $negative = ($amount < 0);
530 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
532 my ($whole, $dec) = split /\./, "$amount";
534 $amount = join '', reverse split //, $whole;
535 $fillup = "0" x ($places - length($dec));
537 if ($myconfig->{numberformat} eq '1,000.00') {
538 $amount =~ s/\d{3,}?/$&,/g;
540 $amount = join '', reverse split //, $amount;
541 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
544 if ($myconfig->{numberformat} eq '1.000,00') {
545 $amount =~ s/\d{3,}?/$&./g;
547 $amount = join '', reverse split //, $amount;
548 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
551 if ($myconfig->{numberformat} eq '1000,00') {
553 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
557 $amount = ($negative) ? "($amount)" : "$amount";
558 } elsif ($dash =~ /DRCR/) {
559 $amount = ($negative) ? "$amount DR" : "$amount CR";
561 $amount = ($negative) ? "-$amount" : "$amount";
565 if ($dash eq "0" && $places) {
566 if ($myconfig->{numberformat} eq '1.000,00') {
567 $amount = "0" . "," . "0" x $places;
569 $amount = "0" . "." . "0" x $places;
572 $amount = ($dash ne "") ? "$dash" : "0";
576 $main::lxdebug->leave_sub();
582 $main::lxdebug->enter_sub();
584 my ($self, $myconfig, $amount) = @_;
585 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
587 if ($myconfig->{in_numberformat} == 1) {
589 # Extra input number format 1000.00 or 1000,00
590 $main::lxdebug->message(LXDebug::DEBUG2,
591 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
594 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
595 $amount = scalar reverse $amount;
597 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
598 $amount =~ s/\./DOT/;
600 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
603 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
604 $amount =~ s/DOT/\./;
606 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
607 $amount = scalar reverse $amount;
608 $main::lxdebug->message(LXDebug::DEBUG2,
609 "Parsed amount:" . $amount . "\n");
611 return ($amount * 1);
614 $main::lxdebug->message(LXDebug::DEBUG2,
615 "in_numberformat: " . $main::locale->text('equal Outputformat'));
616 $main::lxdebug->message(LXDebug::DEBUG2,
617 " = numberformat: $myconfig->{numberformat}");
618 if ( ($myconfig->{numberformat} eq '1.000,00')
619 || ($myconfig->{numberformat} eq '1000,00')) {
624 if ($myconfig->{numberformat} eq "1'000.00") {
630 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
632 $main::lxdebug->leave_sub();
634 return ($amount * 1);
638 $main::lxdebug->enter_sub();
640 my ($self, $amount, $places) = @_;
643 # Rounding like "Kaufmannsrunden"
644 # Descr. http://de.wikipedia.org/wiki/Rundung
646 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
649 $amount = $amount * (10**($places));
650 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
652 $main::lxdebug->leave_sub();
654 return $round_amount;
659 $main::lxdebug->enter_sub();
661 my ($self, $myconfig, $userspath) = @_;
664 # Some variables used for page breaks
665 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
667 my ($current_page, $current_line, $current_row) = (1, 1, 0);
673 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
674 $self->format_string(grep(/notes/, keys(%{$self})));
676 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
677 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
679 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
680 qw(email tel fax name signature));
682 open(IN, "$self->{templates}/$self->{IN}")
683 or $self->error("$self->{IN} : $!");
688 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
690 # OUT is used for the media, screen, printer, email
691 # for postscript we store a copy in a temporary file
693 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
694 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
696 $self->{OUT} = ">$self->{tmpfile}";
700 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
702 open(OUT, ">-") or $self->error("STDOUT : $!");
706 # Do we have to run LaTeX two times? This is needed if
707 # the template contains page references.
710 # first we generate a tmpfile
711 # read file and replace <%variable%>
717 $two_passes = 1 if (/\\pageref/);
720 # detect pagebreak block and its parameters
721 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
722 $chars_per_line = $1;
723 $lines_on_first_page = $2;
724 $lines_on_second_page = $3;
727 last if (/\s*<%end pagebreak%>/);
734 if (/\s*<%foreach /) {
736 # this one we need for the count
738 $var =~ s/\s*<%foreach (.+?)%>/$1/;
740 last if (/\s*<%end /);
746 # display contents of $self->{number}[] array
747 for $i (0 .. $#{ $self->{$var} }) {
750 # Try to detect whether a manual page break is necessary
751 # but only if there was a <%pagebreak ...%> block before
753 if ($chars_per_line) {
755 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
758 my $_description = $self->{"description"}[$i];
759 while ($_description =~ /\\newline/) {
761 $_description =~ s/\\newline//;
763 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
765 if ($current_page == 1) {
766 $lpp = $lines_on_first_page;
768 $lpp = $lines_on_second_page;
771 # Yes we need a manual page break -- or the user has forced one
773 (($current_line + $lines) > $lpp)
774 || ($self->{"_forced_pagebreaks"}
775 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
779 # replace the special variables <%sumcarriedforward%>
782 my $psum = $self->format_amount($myconfig, $sum, 2);
783 $pb =~ s/<%sumcarriedforward%>/$psum/g;
784 $pb =~ s/<%lastpage%>/$current_page/g;
786 # only "normal" variables are supported here
787 # (no <%if, no <%foreach, no <%include)
789 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
791 # page break block is ready to rock
796 $current_line += $lines;
799 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
803 # don't parse par, we need it for each line
805 s/<%(.+?)%>/$self->{$1}[$i]/mg;
811 # if not comes before if!
812 if (/\s*<%if not /) {
814 # check if it is not set and display
816 s/\s*<%if not (.+?)%>/$1/;
818 unless ($self->{$_}) {
820 last if (/\s*<%end /);
830 last if (/\s*<%end /);
838 # check if it is set and display
840 s/\s*<%if (.+?)%>/$1/;
844 last if (/\s*<%end /);
854 last if (/\s*<%end /);
860 # check for <%include filename%>
861 if (/\s*<%include /) {
865 $var =~ s/\s*<%include (.+?)%>/$1/;
868 $var =~ s/(\/|\.\.)//g;
870 # prevent the infinite loop!
871 next if ($self->{"$var"});
873 open(INC, "$self->{templates}/$var")
874 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
883 s/<%(.+?)%>/$self->{$1}/g;
884 s/<nobr><\/nobr>/ /g;
891 # Convert the tex file to postscript
892 if ($self->{format} =~ /(postscript|pdf)/) {
895 $self->{cwd} = cwd();
896 $self->{tmpdir} = "$self->{cwd}/$userspath";
898 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
900 $self->{tmpfile} =~ s/$userspath\///g;
902 if ($self->{format} eq 'postscript') {
904 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
906 $self->error($self->cleanup) if ($?);
909 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
911 $self->error($self->cleanup) if ($?);
914 $self->{tmpfile} =~ s/tex$/dvi/;
916 system("dvips $self->{tmpfile} -o -q > /dev/null");
917 $self->error($self->cleanup . "dvips : $!") if ($?);
918 $self->{tmpfile} =~ s/dvi$/ps/;
920 if ($self->{format} eq 'pdf') {
922 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
924 $self->error($self->cleanup) if ($?);
927 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
929 $self->error($self->cleanup) if ($?);
931 $self->{tmpfile} =~ s/tex$/pdf/;
936 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
938 if ($self->{media} eq 'email') {
942 my $mail = new Mailer;
944 map { $mail->{$_} = $self->{$_} }
945 qw(cc bcc subject message version format charset);
946 $mail->{to} = qq|$self->{email}|;
947 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
948 $mail->{fileid} = "$fileid.";
950 # if we send html or plain text inline
951 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
952 $mail->{contenttype} = "text/html";
954 $mail->{message} =~ s/\r\n/<br>\n/g;
955 $myconfig->{signature} =~ s/\\n/<br>\n/g;
956 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
958 open(IN, $self->{tmpfile})
959 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
961 $mail->{message} .= $_;
968 @{ $mail->{attachments} } = ($self->{tmpfile});
970 $myconfig->{signature} =~ s/\\n/\r\n/g;
971 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
975 my $err = $mail->send($out);
976 $self->error($self->cleanup . "$err") if ($err);
982 my $numbytes = (-s $self->{tmpfile});
983 open(IN, $self->{tmpfile})
984 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
986 $self->{copies} = 1 unless $self->{media} eq 'printer';
988 chdir("$self->{cwd}");
990 for my $i (1 .. $self->{copies}) {
992 open(OUT, $self->{OUT})
993 or $self->error($self->cleanup . "$self->{OUT} : $!");
997 print qq|Content-Type: application/$self->{format}
998 Content-Disposition: attachment; filename="$self->{tmpfile}"
999 Content-Length: $numbytes
1003 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1023 chdir("$self->{cwd}");
1024 $main::lxdebug->leave_sub();
1028 $main::lxdebug->enter_sub();
1032 chdir("$self->{tmpdir}");
1035 if (-f "$self->{tmpfile}.err") {
1036 open(FH, "$self->{tmpfile}.err");
1041 if ($self->{tmpfile}) {
1044 $self->{tmpfile} =~ s/\.\w+$//g;
1045 my $tmpfile = $self->{tmpfile};
1046 unlink(<$tmpfile.*>);
1049 chdir("$self->{cwd}");
1051 $main::lxdebug->leave_sub();
1057 $main::lxdebug->enter_sub();
1059 my ($self, @fields) = @_;
1062 %unique_fields = map({ $_ => 1 } @fields);
1063 @fields = keys(%unique_fields);
1065 foreach my $field (@fields) {
1066 next unless ($self->{$field} =~ /\<pagebreak\>/);
1067 $self->{$field} =~ s/\<pagebreak\>//g;
1068 if ($field =~ /.*_(\d+)$/) {
1069 if (!$self->{"_forced_pagebreaks"}) {
1070 $self->{"_forced_pagebreaks"} = [];
1072 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1076 my $format = $self->{format};
1077 if ($self->{format} =~ /(postscript|pdf)/) {
1084 '<', '>', quotemeta('\n'), '
1088 '&', quotemeta('\n'), '
1090 '"', '\$', '%', '_', '#', quotemeta('^'),
1091 '{', '}', '<', '>', '£', "\r"
1097 quotemeta('\n') => '<br>',
1108 quotemeta('^') => '\^\\',
1113 quotemeta('\n') => '\newline ',
1120 foreach my $key (@{ $replace{order}{$format} }) {
1121 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1124 # Allow some HTML markup to be converted into the output format's
1125 # corresponding markup code, e.g. bold or italic.
1126 if ('html' eq $format) {
1127 my @markup_replace = ('b', 'i', 's', 'u');
1129 foreach my $key (@markup_replace) {
1130 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1133 } elsif ('tex' eq $format) {
1134 my %markup_replace = ('b' => 'textbf',
1136 'u' => 'underline');
1138 foreach my $field (@fields) {
1139 foreach my $key (keys(%markup_replace)) {
1140 my $new = $markup_replace{$key};
1142 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1147 $main::lxdebug->leave_sub();
1151 $main::lxdebug->enter_sub();
1153 my ($self, $date, $myconfig) = @_;
1155 if ($date && $date =~ /\D/) {
1157 if ($myconfig->{dateformat} =~ /^yy/) {
1158 ($yy, $mm, $dd) = split /\D/, $date;
1160 if ($myconfig->{dateformat} =~ /^mm/) {
1161 ($mm, $dd, $yy) = split /\D/, $date;
1163 if ($myconfig->{dateformat} =~ /^dd/) {
1164 ($dd, $mm, $yy) = split /\D/, $date;
1169 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1170 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1172 $dd = "0$dd" if ($dd < 10);
1173 $mm = "0$mm" if ($mm < 10);
1175 $date = "$yy$mm$dd";
1178 $main::lxdebug->leave_sub();
1183 # Database routines used throughout
1186 $main::lxdebug->enter_sub();
1188 my ($self, $myconfig) = @_;
1190 # connect to database
1192 DBI->connect($myconfig->{dbconnect},
1193 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1197 if ($myconfig->{dboptions}) {
1198 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1201 $main::lxdebug->leave_sub();
1206 sub dbconnect_noauto {
1207 $main::lxdebug->enter_sub();
1209 my ($self, $myconfig) = @_;
1211 # connect to database
1213 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1214 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1218 if ($myconfig->{dboptions}) {
1219 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1222 $main::lxdebug->leave_sub();
1227 sub update_balance {
1228 $main::lxdebug->enter_sub();
1230 my ($self, $dbh, $table, $field, $where, $value) = @_;
1232 # if we have a value, go do it
1235 # retrieve balance from table
1236 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1237 my $sth = $dbh->prepare($query);
1239 $sth->execute || $self->dberror($query);
1240 my ($balance) = $sth->fetchrow_array;
1246 $query = "UPDATE $table SET $field = $balance WHERE $where";
1247 $dbh->do($query) || $self->dberror($query);
1249 $main::lxdebug->leave_sub();
1252 sub update_exchangerate {
1253 $main::lxdebug->enter_sub();
1255 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1257 # some sanity check for currency
1259 $main::lxdebug->leave_sub();
1263 my $query = qq|SELECT e.curr FROM exchangerate e
1264 WHERE e.curr = '$curr'
1265 AND e.transdate = '$transdate'
1267 my $sth = $dbh->prepare($query);
1268 $sth->execute || $self->dberror($query);
1271 if ($buy != 0 && $sell != 0) {
1272 $set = "buy = $buy, sell = $sell";
1273 } elsif ($buy != 0) {
1274 $set = "buy = $buy";
1275 } elsif ($sell != 0) {
1276 $set = "sell = $sell";
1279 if ($sth->fetchrow_array) {
1280 $query = qq|UPDATE exchangerate
1282 WHERE curr = '$curr'
1283 AND transdate = '$transdate'|;
1285 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1286 VALUES ('$curr', $buy, $sell, '$transdate')|;
1289 $dbh->do($query) || $self->dberror($query);
1291 $main::lxdebug->leave_sub();
1294 sub save_exchangerate {
1295 $main::lxdebug->enter_sub();
1297 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1299 my $dbh = $self->dbconnect($myconfig);
1301 my ($buy, $sell) = (0, 0);
1302 $buy = $rate if $fld eq 'buy';
1303 $sell = $rate if $fld eq 'sell';
1305 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1309 $main::lxdebug->leave_sub();
1312 sub get_exchangerate {
1313 $main::lxdebug->enter_sub();
1315 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1317 unless ($transdate) {
1318 $main::lxdebug->leave_sub();
1322 my $query = qq|SELECT e.$fld FROM exchangerate e
1323 WHERE e.curr = '$curr'
1324 AND e.transdate = '$transdate'|;
1325 my $sth = $dbh->prepare($query);
1326 $sth->execute || $self->dberror($query);
1328 my ($exchangerate) = $sth->fetchrow_array;
1331 $main::lxdebug->leave_sub();
1333 return $exchangerate;
1336 sub check_exchangerate {
1337 $main::lxdebug->enter_sub();
1339 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1341 unless ($transdate) {
1342 $main::lxdebug->leave_sub();
1346 my $dbh = $self->dbconnect($myconfig);
1348 my $query = qq|SELECT e.$fld FROM exchangerate e
1349 WHERE e.curr = '$currency'
1350 AND e.transdate = '$transdate'|;
1351 my $sth = $dbh->prepare($query);
1352 $sth->execute || $self->dberror($query);
1354 my ($exchangerate) = $sth->fetchrow_array;
1358 $main::lxdebug->leave_sub();
1360 return $exchangerate;
1364 $main::lxdebug->enter_sub();
1366 my ($self, $dbh, $id) = @_;
1370 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1372 if ($self->{"shipto$item"}) {
1373 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1375 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1380 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1381 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1382 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1383 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1384 '$self->{shiptozipcode}', '$self->{shiptocity}',
1385 '$self->{shiptocountry}', '$self->{shiptocontact}',
1386 '$self->{shiptophone}', '$self->{shiptofax}',
1387 '$self->{shiptoemail}')|;
1388 $dbh->do($query) || $self->dberror($query);
1391 $main::lxdebug->leave_sub();
1395 $main::lxdebug->enter_sub();
1397 my ($self, $dbh) = @_;
1399 my $query = qq|SELECT e.id, e.name FROM employee e
1400 WHERE e.login = '$self->{login}'|;
1401 my $sth = $dbh->prepare($query);
1402 $sth->execute || $self->dberror($query);
1404 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1405 $self->{employee_id} *= 1;
1409 $main::lxdebug->leave_sub();
1412 # get other contact for transaction and form - html/tex
1414 $main::lxdebug->enter_sub();
1416 my ($self, $dbh, $id) = @_;
1418 my $query = qq|SELECT c.*
1421 $sth = $dbh->prepare($query);
1422 $sth->execute || $self->dberror($query);
1424 $ref = $sth->fetchrow_hashref(NAME_lc);
1426 push @{ $self->{$_} }, $ref;
1429 $main::lxdebug->leave_sub();
1432 # get contacts for id, if no contact return {"","","","",""}
1434 $main::lxdebug->enter_sub();
1436 my ($self, $dbh, $id) = @_;
1438 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1440 WHERE cp_cv_id=$id|;
1441 my $sth = $dbh->prepare($query);
1442 $sth->execute || $self->dberror($query);
1445 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1446 push @{ $self->{all_contacts} }, $ref;
1451 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1454 $main::lxdebug->leave_sub();
1457 # this sub gets the id and name from $table
1459 $main::lxdebug->enter_sub();
1461 my ($self, $myconfig, $table) = @_;
1463 # connect to database
1464 my $dbh = $self->dbconnect($myconfig);
1466 my $name = $self->like(lc $self->{$table});
1467 my $customernumber = $self->like(lc $self->{customernumber});
1469 if ($self->{customernumber} ne "") {
1470 $query = qq~SELECT c.id, c.name,
1471 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1473 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1476 $query = qq~SELECT c.id, c.name,
1477 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1479 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1483 if ($self->{openinvoices}) {
1484 $query = qq~SELECT DISTINCT c.id, c.name,
1485 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1486 FROM $self->{arap} a
1487 JOIN $table c ON (a.${table}_id = c.id)
1488 WHERE NOT a.amount = a.paid
1489 AND lower(c.name) LIKE '$name'
1492 my $sth = $dbh->prepare($query);
1494 $sth->execute || $self->dberror($query);
1497 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1498 push(@{ $self->{name_list} }, $ref);
1504 $main::lxdebug->leave_sub();
1509 # the selection sub is used in the AR, AP, IS, IR and OE module
1512 $main::lxdebug->enter_sub();
1514 my ($self, $myconfig, $table, $module) = @_;
1517 my $dbh = $self->dbconnect($myconfig);
1519 my $query = qq|SELECT count(*) FROM $table|;
1520 my $sth = $dbh->prepare($query);
1521 $sth->execute || $self->dberror($query);
1522 my ($count) = $sth->fetchrow_array;
1525 # build selection list
1526 if ($count < $myconfig->{vclimit}) {
1527 $query = qq|SELECT id, name
1528 FROM $table WHERE not obsolete
1530 $sth = $dbh->prepare($query);
1531 $sth->execute || $self->dberror($query);
1533 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1534 push @{ $self->{"all_$table"} }, $ref;
1542 $self->get_employee($dbh);
1544 # setup sales contacts
1545 $query = qq|SELECT e.id, e.name
1548 AND NOT e.id = $self->{employee_id}|;
1549 $sth = $dbh->prepare($query);
1550 $sth->execute || $self->dberror($query);
1552 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1553 push @{ $self->{all_employees} }, $ref;
1558 push @{ $self->{all_employees} },
1559 { id => $self->{employee_id},
1560 name => $self->{employee} };
1562 # sort the whole thing
1563 @{ $self->{all_employees} } =
1564 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1566 if ($module eq 'AR') {
1568 # prepare query for departments
1569 $query = qq|SELECT d.id, d.description
1575 $query = qq|SELECT d.id, d.description
1580 $sth = $dbh->prepare($query);
1581 $sth->execute || $self->dberror($query);
1583 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1584 push @{ $self->{all_departments} }, $ref;
1589 $main::lxdebug->leave_sub();
1592 # this is only used for reports
1593 sub all_departments {
1594 $main::lxdebug->enter_sub();
1596 my ($self, $myconfig, $table) = @_;
1598 my $dbh = $self->dbconnect($myconfig);
1599 my $where = "1 = 1";
1601 if (defined $table) {
1602 if ($table eq 'customer') {
1603 $where = " d.role = 'P'";
1607 my $query = qq|SELECT d.id, d.description
1611 my $sth = $dbh->prepare($query);
1612 $sth->execute || $self->dberror($query);
1614 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1615 push @{ $self->{all_departments} }, $ref;
1621 $main::lxdebug->leave_sub();
1625 $main::lxdebug->enter_sub();
1627 my ($self, $module, $myconfig, $table) = @_;
1629 $self->all_vc($myconfig, $table, $module);
1631 # get last customers or vendors
1634 my $dbh = $self->dbconnect($myconfig);
1638 # now get the account numbers
1639 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1641 WHERE c.link LIKE '%$module%'
1644 $sth = $dbh->prepare($query);
1645 $sth->execute || $self->dberror($query);
1647 $self->{accounts} = "";
1648 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1650 foreach my $key (split /:/, $ref->{link}) {
1651 if ($key =~ /$module/) {
1653 # cross reference for keys
1654 $xkeyref{ $ref->{accno} } = $key;
1656 push @{ $self->{"${module}_links"}{$key} },
1657 { accno => $ref->{accno},
1658 description => $ref->{description},
1659 taxkey => $ref->{taxkey_id} };
1661 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1667 if (($module eq "AP") || ($module eq "AR")) {
1669 # get tax rates and description
1670 $query = qq| SELECT * FROM tax t|;
1671 $sth = $dbh->prepare($query);
1672 $sth->execute || $self->dberror($query);
1674 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1675 push @{ $self->{TAX} }, $ref;
1681 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1683 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1684 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1685 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1686 c.name AS $table, a.department_id, d.description AS department,
1687 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1688 a.employee_id, e.name AS employee, a.gldate
1690 JOIN $table c ON (a.${table}_id = c.id)
1691 LEFT JOIN employee e ON (e.id = a.employee_id)
1692 LEFT JOIN department d ON (d.id = a.department_id)
1693 WHERE a.id = $self->{id}|;
1694 $sth = $dbh->prepare($query);
1695 $sth->execute || $self->dberror($query);
1697 $ref = $sth->fetchrow_hashref(NAME_lc);
1698 foreach $key (keys %$ref) {
1699 $self->{$key} = $ref->{$key};
1703 # get amounts from individual entries
1704 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1705 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1707 JOIN chart c ON (c.id = a.chart_id)
1708 LEFT JOIN project p ON (p.id = a.project_id)
1709 LEFT Join tax t ON (a.taxkey = t.taxkey)
1710 WHERE a.trans_id = $self->{id}
1711 AND a.fx_transaction = '0'
1712 ORDER BY a.oid,a.transdate|;
1713 $sth = $dbh->prepare($query);
1714 $sth->execute || $self->dberror($query);
1716 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1718 # get exchangerate for currency
1719 $self->{exchangerate} =
1720 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1724 # store amounts in {acc_trans}{$key} for multiple accounts
1725 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1726 $ref->{exchangerate} =
1727 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1729 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1732 $ref->{index} = $index;
1734 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1738 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1739 (SELECT c.accno FROM chart c
1740 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1741 (SELECT c.accno FROM chart c
1742 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1744 $sth = $dbh->prepare($query);
1745 $sth->execute || $self->dberror($query);
1747 $ref = $sth->fetchrow_hashref(NAME_lc);
1748 map { $self->{$_} = $ref->{$_} } keys %$ref;
1754 $query = qq|SELECT current_date AS transdate,
1755 d.curr AS currencies, d.closedto, d.revtrans,
1756 (SELECT c.accno FROM chart c
1757 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1758 (SELECT c.accno FROM chart c
1759 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1761 $sth = $dbh->prepare($query);
1762 $sth->execute || $self->dberror($query);
1764 $ref = $sth->fetchrow_hashref(NAME_lc);
1765 map { $self->{$_} = $ref->{$_} } keys %$ref;
1768 if ($self->{"$self->{vc}_id"}) {
1770 # only setup currency
1771 ($self->{currency}) = split /:/, $self->{currencies};
1775 $self->lastname_used($dbh, $myconfig, $table, $module);
1777 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1779 # get exchangerate for currency
1780 $self->{exchangerate} =
1781 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1790 $main::lxdebug->leave_sub();
1794 $main::lxdebug->enter_sub();
1796 my ($self, $dbh, $myconfig, $table, $module) = @_;
1798 my $arap = ($table eq 'customer') ? "ar" : "ap";
1799 my $where = "1 = 1";
1801 if ($self->{type} =~ /_order/) {
1803 $where = "quotation = '0'";
1805 if ($self->{type} =~ /_quotation/) {
1807 $where = "quotation = '1'";
1810 my $query = qq|SELECT MAX(id) FROM $arap
1812 AND ${table}_id > 0|;
1813 my $sth = $dbh->prepare($query);
1814 $sth->execute || $self->dberror($query);
1816 my ($trans_id) = $sth->fetchrow_array;
1820 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1821 current_date + ct.terms AS duedate, a.department_id,
1822 d.description AS department
1824 JOIN $table ct ON (a.${table}_id = ct.id)
1825 LEFT JOIN department d ON (a.department_id = d.id)
1826 WHERE a.id = $trans_id|;
1827 $sth = $dbh->prepare($query);
1828 $sth->execute || $self->dberror($query);
1830 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1831 $self->{duedate}, $self->{department_id}, $self->{department})
1832 = $sth->fetchrow_array;
1835 $main::lxdebug->leave_sub();
1839 $main::lxdebug->enter_sub();
1841 my ($self, $myconfig, $thisdate, $days) = @_;
1843 my $dbh = $self->dbconnect($myconfig);
1848 my $dateformat = $myconfig->{dateformat};
1849 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1851 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1853 $sth = $dbh->prepare($query);
1854 $sth->execute || $self->dberror($query);
1856 $query = qq|SELECT current_date AS thisdate
1858 $sth = $dbh->prepare($query);
1859 $sth->execute || $self->dberror($query);
1862 ($thisdate) = $sth->fetchrow_array;
1867 $main::lxdebug->leave_sub();
1873 $main::lxdebug->enter_sub();
1875 my ($self, $string) = @_;
1877 if ($string !~ /%/) {
1878 $string = "%$string%";
1881 $string =~ s/\'/\'\'/g;
1883 $main::lxdebug->leave_sub();
1889 $main::lxdebug->enter_sub();
1891 my ($self, $flds, $new, $count, $numrows) = @_;
1895 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1901 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1903 $j = $item->{ndx} - 1;
1904 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1908 for $i ($count + 1 .. $numrows) {
1909 map { delete $self->{"${_}_$i"} } @{$flds};
1912 $main::lxdebug->leave_sub();
1916 $main::lxdebug->enter_sub();
1918 my ($self, $myconfig) = @_;
1922 my $dbh = $self->dbconnect_noauto($myconfig);
1924 my $query = qq|DELETE FROM status
1925 WHERE formname = '$self->{formname}'
1927 my $sth = $dbh->prepare($query) || $self->dberror($query);
1929 if ($self->{formname} =~ /(check|receipt)/) {
1930 for $i (1 .. $self->{rowcount}) {
1931 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1935 $sth->execute($self->{id}) || $self->dberror($query);
1939 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1940 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1942 my %queued = split / /, $self->{queued};
1944 if ($self->{formname} =~ /(check|receipt)/) {
1946 # this is a check or receipt, add one entry for each lineitem
1947 my ($accno) = split /--/, $self->{account};
1948 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1949 chart_id) VALUES (?, '$printed',
1950 '$queued{$self->{formname}}', '$self->{prinform}',
1951 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1952 $sth = $dbh->prepare($query) || $self->dberror($query);
1954 for $i (1 .. $self->{rowcount}) {
1955 if ($self->{"checked_$i"}) {
1956 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1961 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1962 spoolfile, formname)
1963 VALUES ($self->{id}, '$printed', '$emailed',
1964 '$queued{$self->{formname}}', '$self->{formname}')|;
1965 $dbh->do($query) || $self->dberror($query);
1971 $main::lxdebug->leave_sub();
1975 $main::lxdebug->enter_sub();
1977 my ($self, $dbh) = @_;
1979 my ($query, $printed, $emailed);
1981 my $formnames = $self->{printed};
1982 my $emailforms = $self->{emailed};
1984 my $query = qq|DELETE FROM status
1985 WHERE formname = '$self->{formname}'
1986 AND trans_id = $self->{id}|;
1987 $dbh->do($query) || $self->dberror($query);
1989 # this only applies to the forms
1990 # checks and receipts are posted when printed or queued
1992 if ($self->{queued}) {
1993 my %queued = split / /, $self->{queued};
1995 foreach my $formname (keys %queued) {
1996 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1997 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1999 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2000 spoolfile, formname)
2001 VALUES ($self->{id}, '$printed', '$emailed',
2002 '$queued{$formname}', '$formname')|;
2003 $dbh->do($query) || $self->dberror($query);
2005 $formnames =~ s/$self->{formname}//;
2006 $emailforms =~ s/$self->{formname}//;
2011 # save printed, emailed info
2012 $formnames =~ s/^ +//g;
2013 $emailforms =~ s/^ +//g;
2016 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2017 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2019 foreach my $formname (keys %status) {
2020 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2021 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2023 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2024 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2025 $dbh->do($query) || $self->dberror($query);
2028 $main::lxdebug->leave_sub();
2031 sub update_defaults {
2032 $main::lxdebug->enter_sub();
2034 my ($self, $myconfig, $fld) = @_;
2036 my $dbh = $self->dbconnect_noauto($myconfig);
2037 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2038 my $sth = $dbh->prepare($query);
2040 $sth->execute || $self->dberror($query);
2041 my ($var) = $sth->fetchrow_array;
2046 $query = qq|UPDATE defaults
2048 $dbh->do($query) || $form->dberror($query);
2053 $main::lxdebug->leave_sub();
2058 sub update_business {
2059 $main::lxdebug->enter_sub();
2061 my ($self, $myconfig, $business_id) = @_;
2063 my $dbh = $self->dbconnect_noauto($myconfig);
2065 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2066 my $sth = $dbh->prepare($query);
2068 $sth->execute || $self->dberror($query);
2069 my ($var) = $sth->fetchrow_array;
2074 $query = qq|UPDATE business
2075 SET customernumberinit = '$var' WHERE id=$business_id|;
2076 $dbh->do($query) || $form->dberror($query);
2081 $main::lxdebug->leave_sub();
2087 $main::lxdebug->enter_sub();
2089 my ($self, $myconfig, $salesman) = @_;
2091 my $dbh = $self->dbconnect($myconfig);
2093 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2094 my $sth = $dbh->prepare($query);
2095 $sth->execute || $self->dberror($query);
2098 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2099 push(@{ $self->{salesman_list} }, $ref);
2103 $main::lxdebug->leave_sub();
2108 sub get_partsgroup {
2109 $main::lxdebug->enter_sub();
2111 my ($self, $myconfig, $p) = @_;
2113 my $dbh = $self->dbconnect($myconfig);
2115 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2117 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2119 if ($p->{searchitems} eq 'part') {
2121 WHERE p.inventory_accno_id > 0|;
2123 if ($p->{searchitems} eq 'service') {
2125 WHERE p.inventory_accno_id IS NULL|;
2127 if ($p->{searchitems} eq 'assembly') {
2129 WHERE p.assembly = '1'|;
2131 if ($p->{searchitems} eq 'labor') {
2133 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2137 ORDER BY partsgroup|;
2140 $query = qq|SELECT id, partsgroup FROM partsgroup
2141 ORDER BY partsgroup|;
2144 if ($p->{language_code}) {
2145 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2146 t.description AS translation
2148 JOIN parts p ON (p.partsgroup_id = pg.id)
2149 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2150 ORDER BY translation|;
2153 my $sth = $dbh->prepare($query);
2154 $sth->execute || $self->dberror($query);
2156 $self->{all_partsgroup} = ();
2157 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2158 push @{ $self->{all_partsgroup} }, $ref;
2162 $main::lxdebug->leave_sub();
2165 sub get_pricegroup {
2166 $main::lxdebug->enter_sub();
2168 my ($self, $myconfig, $p) = @_;
2170 my $dbh = $self->dbconnect($myconfig);
2172 my $query = qq|SELECT p.id, p.pricegroup
2176 ORDER BY pricegroup|;
2179 $query = qq|SELECT id, pricegroup FROM pricegroup
2180 ORDER BY pricegroup|;
2183 my $sth = $dbh->prepare($query);
2184 $sth->execute || $self->dberror($query);
2186 $self->{all_pricegroup} = ();
2187 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2188 push @{ $self->{all_pricegroup} }, $ref;
2193 $main::lxdebug->leave_sub();
2197 my ($self, $dbh, $myconfig, $audittrail) = @_;
2199 # table, $reference, $formname, $action, $id, $transdate) = @_;
2206 $dbh = $self->dbconnect($myconfig);
2210 # if we have an id add audittrail, otherwise get a new timestamp
2212 if ($audittrail->{id}) {
2214 $query = qq|SELECT audittrail FROM defaults|;
2216 if ($dbh->selectrow_array($query)) {
2217 my ($null, $employee_id) = $self->get_employee($dbh);
2219 if ($self->{audittrail} && !$myconfig) {
2220 chop $self->{audittrail};
2222 my @a = split /\|/, $self->{audittrail};
2226 my @flds = qw(tablename reference formname action transdate);
2228 # put into hash and remove dups
2230 $key = "$a[2]$a[3]";
2232 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2236 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2237 formname, action, employee_id, transdate)
2238 VALUES ($audittrail->{id}, ?, ?,
2239 ?, ?, $employee_id, ?)|;
2240 my $sth = $dbh->prepare($query) || $self->dberror($query);
2244 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2248 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2250 $sth->execute || $self->dberror;
2255 if ($audittrail->{transdate}) {
2256 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2257 formname, action, employee_id, transdate) VALUES (
2258 $audittrail->{id}, '$audittrail->{tablename}', |
2259 . $dbh->quote($audittrail->{reference}) . qq|,
2260 '$audittrail->{formname}', '$audittrail->{action}',
2261 $employee_id, '$audittrail->{transdate}')|;
2263 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2264 formname, action, employee_id) VALUES ($audittrail->{id},
2265 '$audittrail->{tablename}', |
2266 . $dbh->quote($audittrail->{reference}) . qq|,
2267 '$audittrail->{formname}', '$audittrail->{action}',
2274 $query = qq|SELECT current_timestamp FROM defaults|;
2275 my ($timestamp) = $dbh->selectrow_array($query);
2278 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2281 $dbh->disconnect if $disconnect;
2290 $main::lxdebug->enter_sub();
2292 my ($type, $country, $NLS_file) = @_;
2296 if ($country && -d "locale/$country") {
2297 $self->{countrycode} = $country;
2298 eval { require "locale/$country/$NLS_file"; };
2301 $self->{NLS_file} = $NLS_file;
2303 push @{ $self->{LONG_MONTH} },
2304 ("January", "February", "March", "April",
2305 "May ", "June", "July", "August",
2306 "September", "October", "November", "December");
2307 push @{ $self->{SHORT_MONTH} },
2308 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2310 $main::lxdebug->leave_sub();
2316 my ($self, $text) = @_;
2318 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2322 $main::lxdebug->enter_sub();
2324 my ($self, $text) = @_;
2326 if (exists $self{subs}{$text}) {
2327 $text = $self{subs}{$text};
2329 if ($self->{countrycode} && $self->{NLS_file}) {
2331 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2335 $main::lxdebug->leave_sub();
2341 $main::lxdebug->enter_sub();
2343 my ($self, $myconfig, $date, $longformat) = @_;
2346 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2351 $spc = $myconfig->{dateformat};
2353 $spc = substr($spc, 1, 1);
2355 if ($date =~ /\D/) {
2356 if ($myconfig->{dateformat} =~ /^yy/) {
2357 ($yy, $mm, $dd) = split /\D/, $date;
2359 if ($myconfig->{dateformat} =~ /^mm/) {
2360 ($mm, $dd, $yy) = split /\D/, $date;
2362 if ($myconfig->{dateformat} =~ /^dd/) {
2363 ($dd, $mm, $yy) = split /\D/, $date;
2366 $date = substr($date, 2);
2367 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2372 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2373 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2375 if ($myconfig->{dateformat} =~ /^dd/) {
2376 if (defined $longformat && $longformat == 0) {
2378 $dd = "0$dd" if ($dd < 10);
2379 $mm = "0$mm" if ($mm < 10);
2380 $longdate = "$dd$spc$mm$spc$yy";
2383 $longdate .= ($spc eq '.') ? ". " : " ";
2384 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2386 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2388 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2389 # Lx-Office is mainly used in Germany or German speaking countries.
2390 if (defined $longformat && $longformat == 0) {
2392 $dd = "0$dd" if ($dd < 10);
2393 $mm = "0$mm" if ($mm < 10);
2394 $longdate = "$yy-$mm-$dd";
2396 $longdate = "$dd. ";
2397 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2400 if (defined $longformat && $longformat == 0) {
2402 $dd = "0$dd" if ($dd < 10);
2403 $mm = "0$mm" if ($mm < 10);
2404 $longdate = "$mm$spc$dd$spc$yy";
2406 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2412 $main::lxdebug->leave_sub();
2418 $main::lxdebug->enter_sub();
2420 my ($self, $myconfig, $date, $longformat) = @_;
2423 $main::lxdebug->leave_sub();
2428 $spc = $myconfig->{dateformat};
2430 $spc = substr($spc, 1, 1);
2432 if ($date =~ /\D/) {
2433 if ($myconfig->{dateformat} =~ /^yy/) {
2434 ($yy, $mm, $dd) = split /\D/, $date;
2435 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2436 ($mm, $dd, $yy) = split /\D/, $date;
2437 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2438 ($dd, $mm, $yy) = split /\D/, $date;
2441 $date = substr($date, 2);
2442 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2447 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2448 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2450 $main::lxdebug->leave_sub();
2451 return ($yy, $mm, $dd);