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;
208 my ($self, $str) = @_;
210 if ($str && ! ref($str)) {
211 $str =~ s/"/"/g;
223 for (@_) { print qq|<input type=hidden name="$_" value="|.$self->quote($self->{$_}).qq|">\n| }
225 delete $self->{header};
226 for (sort keys %$self) { print qq|<input type=hidden name="$_" value="|.$self->quote($self->{$_}).qq|">\n| }
232 $main::lxdebug->enter_sub();
234 my ($self, $msg) = @_;
236 if ($ENV{HTTP_USER_AGENT}) {
244 <h2 class=error>Error!</h2>
256 if ($self->{error_function}) {
257 &{ $self->{error_function} }($msg);
263 $main::lxdebug->leave_sub();
267 $main::lxdebug->enter_sub();
269 my ($self, $msg) = @_;
271 if ($ENV{HTTP_USER_AGENT}) {
274 if (!$self->{header}) {
287 if ($self->{info_function}) {
288 &{ $self->{info_function} }($msg);
294 $main::lxdebug->leave_sub();
298 $main::lxdebug->enter_sub();
300 my ($self, $str, $cols, $maxrows) = @_;
304 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
306 $maxrows = $rows unless defined $maxrows;
308 $main::lxdebug->leave_sub();
310 return ($rows > $maxrows) ? $maxrows : $rows;
314 $main::lxdebug->enter_sub();
316 my ($self, $msg) = @_;
318 $self->error("$msg\n" . $DBI::errstr);
320 $main::lxdebug->leave_sub();
324 $main::lxdebug->enter_sub();
326 my ($self, $name, $msg) = @_;
328 if ($self->{$name} =~ /^\s*$/) {
331 $main::lxdebug->leave_sub();
335 $main::lxdebug->enter_sub();
339 if ($self->{header}) {
340 $main::lxdebug->leave_sub();
344 my ($stylesheet, $favicon, $charset);
346 if ($ENV{HTTP_USER_AGENT}) {
348 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
350 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
354 if ($self->{favicon} && (-f "$self->{favicon}")) {
356 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
360 if ($self->{charset}) {
362 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
365 if ($self->{landscape}) {
366 $pagelayout = qq|<style type="text/css">
367 \@page { size:landscape; }
370 if ($self->{fokus}) {
371 $fokus = qq|<script type="text/javascript">
373 function fokus(){document.$self->{fokus}.focus();}
380 if ($self->{jsscript} == 1) {
383 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
384 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
385 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
386 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
393 ? "$self->{title} - $self->{titlebar}"
396 print qq|Content-Type: text/html
399 <title>$self->{titlebar}</title>
412 $main::lxdebug->leave_sub();
415 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
416 # changed it to accept an arbitrary number of triggers - sschoeling
418 $main::lxdebug->enter_sub();
421 my $myconfig = shift;
424 # set dateform for jsscript
426 $ifFormat = "%d.%m.%Y";
427 if ($myconfig->{dateformat} eq "dd.mm.yy") {
428 $ifFormat = "%d.%m.%Y";
430 if ($myconfig->{dateformat} eq "dd-mm-yy") {
431 $ifFormat = "%d-%m-%Y";
433 if ($myconfig->{dateformat} eq "dd/mm/yy") {
434 $ifFormat = "%d/%m/%Y";
436 if ($myconfig->{dateformat} eq "mm/dd/yy") {
437 $ifFormat = "%m/%d/%Y";
439 if ($myconfig->{dateformat} eq "mm-dd-yy") {
440 $ifFormat = "%m-%d-%Y";
442 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
443 $ifFormat = "%Y-%m-%d";
455 inputField : "|.(shift).qq|",
456 ifFormat :"$ifFormat",
457 align : "|.(shift).qq|",
458 button : "|.(shift).qq|"
464 <script type="text/javascript">
465 <!--|.join("", @triggers).qq|//-->
469 $main::lxdebug->leave_sub();
472 } #end sub write_trigger
475 $main::lxdebug->enter_sub();
477 my ($self, $msg) = @_;
479 if ($self->{callback}) {
481 ($script, $argv) = split(/\?/, $self->{callback});
482 exec("perl", "$script", $argv);
490 $main::lxdebug->leave_sub();
493 # sort of columns removed - empty sub
495 $main::lxdebug->enter_sub();
497 my ($self, @columns) = @_;
499 $main::lxdebug->leave_sub();
505 $main::lxdebug->enter_sub();
507 my ($self, $myconfig, $amount, $places, $dash) = @_;
509 #Workaround for $format_amount calls without $places
510 if (!defined $places){
511 (my $dec) = ($amount =~ /\.(\d+)/);
512 $places = length $dec;
515 if ($places =~ /\d/) {
516 $amount = $self->round_amount($amount, $places);
519 # is the amount negative
520 my $negative = ($amount < 0);
524 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
526 my ($whole, $dec) = split /\./, "$amount";
528 $amount = join '', reverse split //, $whole;
529 $fillup = "0" x ($places - length($dec));
531 if ($myconfig->{numberformat} eq '1,000.00') {
532 $amount =~ s/\d{3,}?/$&,/g;
534 $amount = join '', reverse split //, $amount;
535 $amount .= "\.$dec".$fillup if ($places ne '' && $places*1 != 0);
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 '1000,00') {
547 $amount .= ",$dec" .$fillup if ($places ne '' && $places*1 != 0);
551 $amount = ($negative) ? "($amount)" : "$amount";
552 } elsif ($dash =~ /DRCR/) {
553 $amount = ($negative) ? "$amount DR" : "$amount CR";
555 $amount = ($negative) ? "-$amount" : "$amount";
559 if ($dash eq "0" && $places) {
560 if ($myconfig->{numberformat} eq '1.000,00') {
561 $amount = "0" . "," . "0" x $places;
563 $amount = "0" . "." . "0" x $places;
566 $amount = ($dash ne "") ? "$dash" : "0";
570 $main::lxdebug->leave_sub();
576 $main::lxdebug->enter_sub();
578 my ($self, $myconfig, $amount) = @_;
579 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
581 if ($myconfig->{in_numberformat} == 1){
582 # Extra input number format 1000.00 or 1000,00
583 $main::lxdebug->message(LXDebug::DEBUG2, "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
585 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
586 $amount = scalar reverse $amount;
587 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
588 $amount =~ s/\./DOT/;
589 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
591 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
592 $amount =~ s/DOT/\./;
593 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
594 $amount = scalar reverse $amount ;
595 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n");
597 return ($amount * 1);
600 $main::lxdebug->message(LXDebug::DEBUG2, "in_numberformat: " . $main::locale->text('equal Outputformat'));
601 $main::lxdebug->message(LXDebug::DEBUG2, " = numberformat: $myconfig->{numberformat}");
602 if ( ($myconfig->{numberformat} eq '1.000,00')
603 || ($myconfig->{numberformat} eq '1000,00')) {
608 if ($myconfig->{numberformat} eq "1'000.00") {
614 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount. "\n") if ($amount);
615 $main::lxdebug->leave_sub();
617 return ($amount * 1);
621 $main::lxdebug->enter_sub();
623 my ($self, $amount, $places) = @_;
626 # Rounding like "Kaufmannsrunden"
627 # Descr. http://de.wikipedia.org/wiki/Rundung
629 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
632 $amount = $amount * (10 ** ($places));
633 $round_amount = int($amount + .5 * ($amount <=> 0))/(10**($places));
635 $main::lxdebug->leave_sub();
637 return $round_amount;
644 $main::lxdebug->enter_sub();
646 my ($self, $myconfig, $userspath) = @_;
649 # Some variables used for page breaks
650 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
652 my ($current_page, $current_line, $current_row) = (1, 1, 0);
658 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
659 $self->format_string(grep(/notes/, keys(%{$self})));
661 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
662 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
664 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
665 qw(email tel fax name signature));
667 open(IN, "$self->{templates}/$self->{IN}")
668 or $self->error("$self->{IN} : $!");
673 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
675 # OUT is used for the media, screen, printer, email
676 # for postscript we store a copy in a temporary file
678 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
679 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
681 $self->{OUT} = ">$self->{tmpfile}";
685 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
687 open(OUT, ">-") or $self->error("STDOUT : $!");
691 # Do we have to run LaTeX two times? This is needed if
692 # the template contains page references.
695 # first we generate a tmpfile
696 # read file and replace <%variable%>
702 $two_passes = 1 if (/\\pageref/);
705 # detect pagebreak block and its parameters
706 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
707 $chars_per_line = $1;
708 $lines_on_first_page = $2;
709 $lines_on_second_page = $3;
712 last if (/\s*<%end pagebreak%>/);
719 if (/\s*<%foreach /) {
721 # this one we need for the count
723 $var =~ s/\s*<%foreach (.+?)%>/$1/;
725 last if (/\s*<%end /);
731 # display contents of $self->{number}[] array
732 for $i (0 .. $#{ $self->{$var} }) {
735 # Try to detect whether a manual page break is necessary
736 # but only if there was a <%pagebreak ...%> block before
738 if ($chars_per_line) {
740 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
743 my $_description = $self->{"description"}[$i];
744 while ($_description =~ /\\newline/) {
746 $_description =~ s/\\newline//;
748 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
750 if ($current_page == 1) {
751 $lpp = $lines_on_first_page;
753 $lpp = $lines_on_second_page;
756 # Yes we need a manual page break -- or the user has forced one
757 if ((($current_line + $lines) > $lpp) ||
758 ($self->{"_forced_pagebreaks"} && grep(/^${current_row}$/, @{$self->{"_forced_pagebreaks"}}))) {
761 # replace the special variables <%sumcarriedforward%>
764 my $psum = $self->format_amount($myconfig, $sum, 2);
765 $pb =~ s/<%sumcarriedforward%>/$psum/g;
766 $pb =~ s/<%lastpage%>/$current_page/g;
768 # only "normal" variables are supported here
769 # (no <%if, no <%foreach, no <%include)
771 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
773 # page break block is ready to rock
778 $current_line += $lines;
781 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
785 # don't parse par, we need it for each line
787 s/<%(.+?)%>/$self->{$1}[$i]/mg;
793 # if not comes before if!
794 if (/\s*<%if not /) {
796 # check if it is not set and display
798 s/\s*<%if not (.+?)%>/$1/;
800 unless ($self->{$_}) {
802 last if (/\s*<%end /);
812 last if (/\s*<%end /);
820 # check if it is set and display
822 s/\s*<%if (.+?)%>/$1/;
826 last if (/\s*<%end /);
836 last if (/\s*<%end /);
842 # check for <%include filename%>
843 if (/\s*<%include /) {
847 $var =~ s/\s*<%include (.+?)%>/$1/;
850 $var =~ s/(\/|\.\.)//g;
852 # prevent the infinite loop!
853 next if ($self->{"$var"});
855 open(INC, "$self->{templates}/$var")
856 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
865 s/<%(.+?)%>/$self->{$1}/g;
866 s/<nobr><\/nobr>/ /g;
873 # Convert the tex file to postscript
874 if ($self->{format} =~ /(postscript|pdf)/) {
877 $self->{cwd} = cwd();
878 $self->{tmpdir} = "$self->{cwd}/$userspath";
880 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
882 $self->{tmpfile} =~ s/$userspath\///g;
884 if ($self->{format} eq 'postscript') {
886 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
888 $self->error($self->cleanup) if ($?);
891 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
893 $self->error($self->cleanup) if ($?);
896 $self->{tmpfile} =~ s/tex$/dvi/;
898 system("dvips $self->{tmpfile} -o -q > /dev/null");
899 $self->error($self->cleanup . "dvips : $!") if ($?);
900 $self->{tmpfile} =~ s/dvi$/ps/;
902 if ($self->{format} eq 'pdf') {
904 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
906 $self->error($self->cleanup) if ($?);
909 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
911 $self->error($self->cleanup) if ($?);
913 $self->{tmpfile} =~ s/tex$/pdf/;
918 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
920 if ($self->{media} eq 'email') {
924 my $mail = new Mailer;
926 map { $mail->{$_} = $self->{$_} }
927 qw(cc bcc subject message version format charset);
928 $mail->{to} = qq|$self->{email}|;
929 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
930 $mail->{fileid} = "$fileid.";
932 # if we send html or plain text inline
933 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
934 $mail->{contenttype} = "text/html";
936 $mail->{message} =~ s/\r\n/<br>\n/g;
937 $myconfig->{signature} =~ s/\\n/<br>\n/g;
938 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
940 open(IN, $self->{tmpfile})
941 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
943 $mail->{message} .= $_;
950 @{ $mail->{attachments} } = ($self->{tmpfile});
952 $myconfig->{signature} =~ s/\\n/\r\n/g;
953 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
957 my $err = $mail->send($out);
958 $self->error($self->cleanup . "$err") if ($err);
964 my $numbytes = (-s $self->{tmpfile});
965 open(IN, $self->{tmpfile})
966 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
968 $self->{copies} = 1 unless $self->{media} eq 'printer';
970 chdir("$self->{cwd}");
972 for my $i (1 .. $self->{copies}) {
974 open(OUT, $self->{OUT})
975 or $self->error($self->cleanup . "$self->{OUT} : $!");
979 print qq|Content-Type: application/$self->{format}
980 Content-Disposition: attachment; filename="$self->{tmpfile}"
981 Content-Length: $numbytes
985 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1005 chdir("$self->{cwd}");
1006 $main::lxdebug->leave_sub();
1010 $main::lxdebug->enter_sub();
1014 chdir("$self->{tmpdir}");
1017 if (-f "$self->{tmpfile}.err") {
1018 open(FH, "$self->{tmpfile}.err");
1023 if ($self->{tmpfile}) {
1026 $self->{tmpfile} =~ s/\.\w+$//g;
1027 my $tmpfile = $self->{tmpfile};
1028 unlink(<$tmpfile.*>);
1031 chdir("$self->{cwd}");
1033 $main::lxdebug->leave_sub();
1039 $main::lxdebug->enter_sub();
1041 my ($self, @fields) = @_;
1044 %unique_fields = map({ $_ => 1 } @fields);
1045 @fields = keys(%unique_fields);
1047 foreach my $field (@fields) {
1048 next unless ($self->{$field} =~ /\<pagebreak\>/);
1049 $self->{$field} =~ s/\<pagebreak\>//g;
1050 if ($field =~ /.*_(\d+)$/) {
1051 if (!$self->{"_forced_pagebreaks"}) {
1052 $self->{"_forced_pagebreaks"} = [];
1054 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1058 my $format = $self->{format};
1059 if ($self->{format} =~ /(postscript|pdf)/) {
1066 '<', '>', quotemeta('\n'), '
1070 '&', quotemeta('\n'), '
1072 '"', '\$', '%', '_', '#', quotemeta('^'),
1073 '{', '}', '<', '>', '£', "\r"
1079 quotemeta('\n') => '<br>',
1090 quotemeta('^') => '\^\\',
1095 quotemeta('\n') => '\newline ',
1102 foreach my $key (@{ $replace{order}{$format} }) {
1103 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1106 # Allow some HTML markup to be converted into the output format's
1107 # corresponding markup code, e.g. bold or italic.
1108 if ('html' eq $format) {
1109 my @markup_replace = ('b', 'i', 's', 'u');
1111 foreach my $key (@markup_replace) {
1112 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1115 } elsif ('tex' eq $format) {
1116 my %markup_replace = ('b' => 'textbf',
1118 'u' => 'underline');
1120 foreach my $field (@fields) {
1121 foreach my $key (keys(%markup_replace)) {
1122 my $new = $markup_replace{$key};
1124 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1129 $main::lxdebug->leave_sub();
1133 $main::lxdebug->enter_sub();
1135 my ($self, $date, $myconfig) = @_;
1137 if ($date && $date =~ /\D/) {
1139 if ($myconfig->{dateformat} =~ /^yy/) {
1140 ($yy, $mm, $dd) = split /\D/, $date;
1142 if ($myconfig->{dateformat} =~ /^mm/) {
1143 ($mm, $dd, $yy) = split /\D/, $date;
1145 if ($myconfig->{dateformat} =~ /^dd/) {
1146 ($dd, $mm, $yy) = split /\D/, $date;
1151 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1152 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1154 $dd = "0$dd" if ($dd < 10);
1155 $mm = "0$mm" if ($mm < 10);
1157 $date = "$yy$mm$dd";
1160 $main::lxdebug->leave_sub();
1165 # Database routines used throughout
1168 $main::lxdebug->enter_sub();
1170 my ($self, $myconfig) = @_;
1172 # connect to database
1174 DBI->connect($myconfig->{dbconnect},
1175 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1179 if ($myconfig->{dboptions}) {
1180 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1183 $main::lxdebug->leave_sub();
1188 sub dbconnect_noauto {
1189 $main::lxdebug->enter_sub();
1191 my ($self, $myconfig) = @_;
1193 # connect to database
1195 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1196 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1200 if ($myconfig->{dboptions}) {
1201 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1204 $main::lxdebug->leave_sub();
1209 sub update_balance {
1210 $main::lxdebug->enter_sub();
1212 my ($self, $dbh, $table, $field, $where, $value) = @_;
1214 # if we have a value, go do it
1217 # retrieve balance from table
1218 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1219 my $sth = $dbh->prepare($query);
1221 $sth->execute || $self->dberror($query);
1222 my ($balance) = $sth->fetchrow_array;
1228 $query = "UPDATE $table SET $field = $balance WHERE $where";
1229 $dbh->do($query) || $self->dberror($query);
1231 $main::lxdebug->leave_sub();
1234 sub update_exchangerate {
1235 $main::lxdebug->enter_sub();
1237 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1239 # some sanity check for currency
1241 $main::lxdebug->leave_sub();
1245 my $query = qq|SELECT e.curr FROM exchangerate e
1246 WHERE e.curr = '$curr'
1247 AND e.transdate = '$transdate'
1249 my $sth = $dbh->prepare($query);
1250 $sth->execute || $self->dberror($query);
1253 if ($buy != 0 && $sell != 0) {
1254 $set = "buy = $buy, sell = $sell";
1255 } elsif ($buy != 0) {
1256 $set = "buy = $buy";
1257 } elsif ($sell != 0) {
1258 $set = "sell = $sell";
1261 if ($sth->fetchrow_array) {
1262 $query = qq|UPDATE exchangerate
1264 WHERE curr = '$curr'
1265 AND transdate = '$transdate'|;
1267 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1268 VALUES ('$curr', $buy, $sell, '$transdate')|;
1271 $dbh->do($query) || $self->dberror($query);
1273 $main::lxdebug->leave_sub();
1276 sub save_exchangerate {
1277 $main::lxdebug->enter_sub();
1279 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1281 my $dbh = $self->dbconnect($myconfig);
1283 my ($buy, $sell) = (0, 0);
1284 $buy = $rate if $fld eq 'buy';
1285 $sell = $rate if $fld eq 'sell';
1287 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1291 $main::lxdebug->leave_sub();
1294 sub get_exchangerate {
1295 $main::lxdebug->enter_sub();
1297 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1299 unless ($transdate) {
1300 $main::lxdebug->leave_sub();
1304 my $query = qq|SELECT e.$fld FROM exchangerate e
1305 WHERE e.curr = '$curr'
1306 AND e.transdate = '$transdate'|;
1307 my $sth = $dbh->prepare($query);
1308 $sth->execute || $self->dberror($query);
1310 my ($exchangerate) = $sth->fetchrow_array;
1313 $main::lxdebug->leave_sub();
1315 return $exchangerate;
1318 sub check_exchangerate {
1319 $main::lxdebug->enter_sub();
1321 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1323 unless ($transdate) {
1324 $main::lxdebug->leave_sub();
1328 my $dbh = $self->dbconnect($myconfig);
1330 my $query = qq|SELECT e.$fld FROM exchangerate e
1331 WHERE e.curr = '$currency'
1332 AND e.transdate = '$transdate'|;
1333 my $sth = $dbh->prepare($query);
1334 $sth->execute || $self->dberror($query);
1336 my ($exchangerate) = $sth->fetchrow_array;
1340 $main::lxdebug->leave_sub();
1342 return $exchangerate;
1346 $main::lxdebug->enter_sub();
1348 my ($self, $dbh, $id) = @_;
1352 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1353 if ($self->{"shipto$item"}) {
1354 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1356 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1360 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1361 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1362 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1363 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1364 '$self->{shiptozipcode}', '$self->{shiptocity}',
1365 '$self->{shiptocountry}', '$self->{shiptocontact}',
1366 '$self->{shiptophone}', '$self->{shiptofax}',
1367 '$self->{shiptoemail}')|;
1368 $dbh->do($query) || $self->dberror($query);
1371 $main::lxdebug->leave_sub();
1375 $main::lxdebug->enter_sub();
1377 my ($self, $dbh) = @_;
1379 my $query = qq|SELECT e.id, e.name FROM employee e
1380 WHERE e.login = '$self->{login}'|;
1381 my $sth = $dbh->prepare($query);
1382 $sth->execute || $self->dberror($query);
1384 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1385 $self->{employee_id} *= 1;
1389 $main::lxdebug->leave_sub();
1392 # get other contact for transaction and form - html/tex
1394 $main::lxdebug->enter_sub();
1396 my ($self, $dbh, $id) = @_;
1398 my $query = qq|SELECT c.*
1401 $sth = $dbh->prepare($query);
1402 $sth->execute || $self->dberror($query);
1404 $ref = $sth->fetchrow_hashref(NAME_lc);
1406 push @{ $self->{$_} }, $ref;
1409 $main::lxdebug->leave_sub();
1412 # get contacts for id, if no contact return {"","","","",""}
1414 $main::lxdebug->enter_sub();
1416 my ($self, $dbh, $id) = @_;
1418 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1420 WHERE cp_cv_id=$id|;
1421 my $sth = $dbh->prepare($query);
1422 $sth->execute || $self->dberror($query);
1425 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1426 push @{ $self->{all_contacts} }, $ref;
1431 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1434 $main::lxdebug->leave_sub();
1437 # this sub gets the id and name from $table
1439 $main::lxdebug->enter_sub();
1441 my ($self, $myconfig, $table) = @_;
1443 # connect to database
1444 my $dbh = $self->dbconnect($myconfig);
1446 my $name = $self->like(lc $self->{$table});
1447 my $customernumber = $self->like(lc $self->{customernumber});
1449 if ($self->{customernumber} ne "") {
1450 $query = qq~SELECT c.id, c.name,
1451 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1453 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1456 $query = qq~SELECT c.id, c.name,
1457 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1459 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1463 if ($self->{openinvoices}) {
1464 $query = qq~SELECT DISTINCT c.id, c.name,
1465 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1466 FROM $self->{arap} a
1467 JOIN $table c ON (a.${table}_id = c.id)
1468 WHERE NOT a.amount = a.paid
1469 AND lower(c.name) LIKE '$name'
1472 my $sth = $dbh->prepare($query);
1474 $sth->execute || $self->dberror($query);
1477 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1478 push(@{ $self->{name_list} }, $ref);
1484 $main::lxdebug->leave_sub();
1489 # the selection sub is used in the AR, AP, IS, IR and OE module
1492 $main::lxdebug->enter_sub();
1494 my ($self, $myconfig, $table, $module) = @_;
1497 my $dbh = $self->dbconnect($myconfig);
1499 my $query = qq|SELECT count(*) FROM $table|;
1500 my $sth = $dbh->prepare($query);
1501 $sth->execute || $self->dberror($query);
1502 my ($count) = $sth->fetchrow_array;
1505 # build selection list
1506 if ($count < $myconfig->{vclimit}) {
1507 $query = qq|SELECT id, name
1508 FROM $table WHERE not obsolete
1510 $sth = $dbh->prepare($query);
1511 $sth->execute || $self->dberror($query);
1513 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1514 push @{ $self->{"all_$table"} }, $ref;
1522 $self->get_employee($dbh);
1524 # setup sales contacts
1525 $query = qq|SELECT e.id, e.name
1528 AND NOT e.id = $self->{employee_id}|;
1529 $sth = $dbh->prepare($query);
1530 $sth->execute || $self->dberror($query);
1532 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1533 push @{ $self->{all_employees} }, $ref;
1538 push @{ $self->{all_employees} },
1539 { id => $self->{employee_id},
1540 name => $self->{employee} };
1542 # sort the whole thing
1543 @{ $self->{all_employees} } =
1544 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1546 if ($module eq 'AR') {
1548 # prepare query for departments
1549 $query = qq|SELECT d.id, d.description
1555 $query = qq|SELECT d.id, d.description
1560 $sth = $dbh->prepare($query);
1561 $sth->execute || $self->dberror($query);
1563 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1564 push @{ $self->{all_departments} }, $ref;
1569 $main::lxdebug->leave_sub();
1572 # this is only used for reports
1573 sub all_departments {
1574 $main::lxdebug->enter_sub();
1576 my ($self, $myconfig, $table) = @_;
1578 my $dbh = $self->dbconnect($myconfig);
1579 my $where = "1 = 1";
1581 if (defined $table) {
1582 if ($table eq 'customer') {
1583 $where = " d.role = 'P'";
1587 my $query = qq|SELECT d.id, d.description
1591 my $sth = $dbh->prepare($query);
1592 $sth->execute || $self->dberror($query);
1594 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1595 push @{ $self->{all_departments} }, $ref;
1601 $main::lxdebug->leave_sub();
1605 $main::lxdebug->enter_sub();
1607 my ($self, $module, $myconfig, $table) = @_;
1609 $self->all_vc($myconfig, $table, $module);
1611 # get last customers or vendors
1614 my $dbh = $self->dbconnect($myconfig);
1618 # now get the account numbers
1620 qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1622 WHERE c.link LIKE '%$module%'
1625 $sth = $dbh->prepare($query);
1626 $sth->execute || $self->dberror($query);
1628 $self->{accounts} = "";
1629 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1631 foreach my $key (split /:/, $ref->{link}) {
1632 if ($key =~ /$module/) {
1634 # cross reference for keys
1635 $xkeyref{ $ref->{accno} } = $key;
1637 push @{ $self->{"${module}_links"}{$key} },
1638 { accno => $ref->{accno},
1639 description => $ref->{description},
1640 taxkey => $ref->{taxkey_id} };
1642 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1648 if (($module eq "AP") || ($module eq "AR")) {
1650 # get tax rates and description
1651 $query = qq| SELECT * FROM tax t|;
1652 $sth = $dbh->prepare($query);
1653 $sth->execute || $self->dberror($query);
1655 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1656 push @{ $self->{TAX} }, $ref;
1662 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1664 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1665 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1666 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1667 c.name AS $table, a.department_id, d.description AS department,
1668 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1669 a.employee_id, e.name AS employee, a.gldate
1671 JOIN $table c ON (a.${table}_id = c.id)
1672 LEFT JOIN employee e ON (e.id = a.employee_id)
1673 LEFT JOIN department d ON (d.id = a.department_id)
1674 WHERE a.id = $self->{id}|;
1675 $sth = $dbh->prepare($query);
1676 $sth->execute || $self->dberror($query);
1678 $ref = $sth->fetchrow_hashref(NAME_lc);
1679 foreach $key (keys %$ref) {
1680 $self->{$key} = $ref->{$key};
1684 # get amounts from individual entries
1685 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1686 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1688 JOIN chart c ON (c.id = a.chart_id)
1689 LEFT JOIN project p ON (p.id = a.project_id)
1690 LEFT Join tax t ON (a.taxkey = t.taxkey)
1691 WHERE a.trans_id = $self->{id}
1692 AND a.fx_transaction = '0'
1693 ORDER BY a.oid,a.transdate|;
1694 $sth = $dbh->prepare($query);
1695 $sth->execute || $self->dberror($query);
1697 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1699 # get exchangerate for currency
1700 $self->{exchangerate} =
1701 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1705 # store amounts in {acc_trans}{$key} for multiple accounts
1706 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1707 $ref->{exchangerate} =
1708 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1710 if ( !($xkeyref{ $ref->{accno} } =~ /tax/)) {
1713 $ref->{index} = $index;
1715 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1719 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1720 (SELECT c.accno FROM chart c
1721 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1722 (SELECT c.accno FROM chart c
1723 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1725 $sth = $dbh->prepare($query);
1726 $sth->execute || $self->dberror($query);
1728 $ref = $sth->fetchrow_hashref(NAME_lc);
1729 map { $self->{$_} = $ref->{$_} } keys %$ref;
1735 $query = qq|SELECT current_date AS transdate,
1736 d.curr AS currencies, d.closedto, d.revtrans,
1737 (SELECT c.accno FROM chart c
1738 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1739 (SELECT c.accno FROM chart c
1740 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1742 $sth = $dbh->prepare($query);
1743 $sth->execute || $self->dberror($query);
1745 $ref = $sth->fetchrow_hashref(NAME_lc);
1746 map { $self->{$_} = $ref->{$_} } keys %$ref;
1749 if ($self->{"$self->{vc}_id"}) {
1751 # only setup currency
1752 ($self->{currency}) = split /:/, $self->{currencies};
1756 $self->lastname_used($dbh, $myconfig, $table, $module);
1758 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1760 # get exchangerate for currency
1761 $self->{exchangerate} =
1762 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1771 $main::lxdebug->leave_sub();
1775 $main::lxdebug->enter_sub();
1777 my ($self, $dbh, $myconfig, $table, $module) = @_;
1779 my $arap = ($table eq 'customer') ? "ar" : "ap";
1780 my $where = "1 = 1";
1782 if ($self->{type} =~ /_order/) {
1784 $where = "quotation = '0'";
1786 if ($self->{type} =~ /_quotation/) {
1788 $where = "quotation = '1'";
1791 my $query = qq|SELECT MAX(id) FROM $arap
1793 AND ${table}_id > 0|;
1794 my $sth = $dbh->prepare($query);
1795 $sth->execute || $self->dberror($query);
1797 my ($trans_id) = $sth->fetchrow_array;
1801 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1802 current_date + ct.terms AS duedate, a.department_id,
1803 d.description AS department
1805 JOIN $table ct ON (a.${table}_id = ct.id)
1806 LEFT JOIN department d ON (a.department_id = d.id)
1807 WHERE a.id = $trans_id|;
1808 $sth = $dbh->prepare($query);
1809 $sth->execute || $self->dberror($query);
1811 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1812 $self->{duedate}, $self->{department_id}, $self->{department})
1813 = $sth->fetchrow_array;
1816 $main::lxdebug->leave_sub();
1820 $main::lxdebug->enter_sub();
1822 my ($self, $myconfig, $thisdate, $days) = @_;
1824 my $dbh = $self->dbconnect($myconfig);
1829 my $dateformat = $myconfig->{dateformat};
1830 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1832 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1834 $sth = $dbh->prepare($query);
1835 $sth->execute || $self->dberror($query);
1837 $query = qq|SELECT current_date AS thisdate
1839 $sth = $dbh->prepare($query);
1840 $sth->execute || $self->dberror($query);
1843 ($thisdate) = $sth->fetchrow_array;
1848 $main::lxdebug->leave_sub();
1854 $main::lxdebug->enter_sub();
1856 my ($self, $string) = @_;
1858 if ($string !~ /%/) {
1859 $string = "%$string%";
1862 $string =~ s/\'/\'\'/g;
1864 $main::lxdebug->leave_sub();
1870 $main::lxdebug->enter_sub();
1872 my ($self, $flds, $new, $count, $numrows) = @_;
1876 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1882 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1884 $j = $item->{ndx} - 1;
1885 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1889 for $i ($count + 1 .. $numrows) {
1890 map { delete $self->{"${_}_$i"} } @{$flds};
1893 $main::lxdebug->leave_sub();
1897 $main::lxdebug->enter_sub();
1899 my ($self, $myconfig) = @_;
1903 my $dbh = $self->dbconnect_noauto($myconfig);
1905 my $query = qq|DELETE FROM status
1906 WHERE formname = '$self->{formname}'
1908 my $sth = $dbh->prepare($query) || $self->dberror($query);
1910 if ($self->{formname} =~ /(check|receipt)/) {
1911 for $i (1 .. $self->{rowcount}) {
1912 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1916 $sth->execute($self->{id}) || $self->dberror($query);
1920 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1921 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1923 my %queued = split / /, $self->{queued};
1925 if ($self->{formname} =~ /(check|receipt)/) {
1927 # this is a check or receipt, add one entry for each lineitem
1928 my ($accno) = split /--/, $self->{account};
1929 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1930 chart_id) VALUES (?, '$printed',
1931 '$queued{$self->{formname}}', '$self->{prinform}',
1932 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1933 $sth = $dbh->prepare($query) || $self->dberror($query);
1935 for $i (1 .. $self->{rowcount}) {
1936 if ($self->{"checked_$i"}) {
1937 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1942 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1943 spoolfile, formname)
1944 VALUES ($self->{id}, '$printed', '$emailed',
1945 '$queued{$self->{formname}}', '$self->{formname}')|;
1946 $dbh->do($query) || $self->dberror($query);
1952 $main::lxdebug->leave_sub();
1956 $main::lxdebug->enter_sub();
1958 my ($self, $dbh) = @_;
1960 my ($query, $printed, $emailed);
1962 my $formnames = $self->{printed};
1963 my $emailforms = $self->{emailed};
1965 my $query = qq|DELETE FROM status
1966 WHERE formname = '$self->{formname}'
1967 AND trans_id = $self->{id}|;
1968 $dbh->do($query) || $self->dberror($query);
1970 # this only applies to the forms
1971 # checks and receipts are posted when printed or queued
1973 if ($self->{queued}) {
1974 my %queued = split / /, $self->{queued};
1976 foreach my $formname (keys %queued) {
1977 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1978 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1980 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1981 spoolfile, formname)
1982 VALUES ($self->{id}, '$printed', '$emailed',
1983 '$queued{$formname}', '$formname')|;
1984 $dbh->do($query) || $self->dberror($query);
1986 $formnames =~ s/$self->{formname}//;
1987 $emailforms =~ s/$self->{formname}//;
1992 # save printed, emailed info
1993 $formnames =~ s/^ +//g;
1994 $emailforms =~ s/^ +//g;
1997 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1998 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2000 foreach my $formname (keys %status) {
2001 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2002 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2004 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2005 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2006 $dbh->do($query) || $self->dberror($query);
2009 $main::lxdebug->leave_sub();
2012 sub update_defaults {
2013 $main::lxdebug->enter_sub();
2015 my ($self, $myconfig, $fld) = @_;
2017 my $dbh = $self->dbconnect_noauto($myconfig);
2018 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2019 my $sth = $dbh->prepare($query);
2021 $sth->execute || $self->dberror($query);
2022 my ($var) = $sth->fetchrow_array;
2027 $query = qq|UPDATE defaults
2029 $dbh->do($query) || $form->dberror($query);
2034 $main::lxdebug->leave_sub();
2039 sub update_business {
2040 $main::lxdebug->enter_sub();
2042 my ($self, $myconfig, $business_id) = @_;
2044 my $dbh = $self->dbconnect_noauto($myconfig);
2046 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2047 my $sth = $dbh->prepare($query);
2049 $sth->execute || $self->dberror($query);
2050 my ($var) = $sth->fetchrow_array;
2055 $query = qq|UPDATE business
2056 SET customernumberinit = '$var' WHERE id=$business_id|;
2057 $dbh->do($query) || $form->dberror($query);
2062 $main::lxdebug->leave_sub();
2068 $main::lxdebug->enter_sub();
2070 my ($self, $myconfig, $salesman) = @_;
2072 my $dbh = $self->dbconnect($myconfig);
2074 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2075 my $sth = $dbh->prepare($query);
2076 $sth->execute || $self->dberror($query);
2079 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2080 push(@{ $self->{salesman_list} }, $ref);
2084 $main::lxdebug->leave_sub();
2089 sub get_partsgroup {
2090 $main::lxdebug->enter_sub();
2092 my ($self, $myconfig, $p) = @_;
2094 my $dbh = $self->dbconnect($myconfig);
2096 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2098 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2100 if ($p->{searchitems} eq 'part') {
2102 WHERE p.inventory_accno_id > 0|;
2104 if ($p->{searchitems} eq 'service') {
2106 WHERE p.inventory_accno_id IS NULL|;
2108 if ($p->{searchitems} eq 'assembly') {
2110 WHERE p.assembly = '1'|;
2112 if ($p->{searchitems} eq 'labor') {
2114 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2118 ORDER BY partsgroup|;
2121 $query = qq|SELECT id, partsgroup FROM partsgroup
2122 ORDER BY partsgroup|;
2125 if ($p->{language_code}) {
2126 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2127 t.description AS translation
2129 JOIN parts p ON (p.partsgroup_id = pg.id)
2130 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2131 ORDER BY translation|;
2134 my $sth = $dbh->prepare($query);
2135 $sth->execute || $self->dberror($query);
2137 $self->{all_partsgroup} = ();
2138 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2139 push @{ $self->{all_partsgroup} }, $ref;
2143 $main::lxdebug->leave_sub();
2147 sub get_pricegroup {
2148 $main::lxdebug->enter_sub();
2150 my ($self, $myconfig, $p) = @_;
2152 my $dbh = $self->dbconnect($myconfig);
2154 my $query = qq|SELECT p.id, p.pricegroup
2158 ORDER BY pricegroup|;
2161 $query = qq|SELECT id, pricegroup FROM pricegroup
2162 ORDER BY pricegroup|;
2165 my $sth = $dbh->prepare($query);
2166 $sth->execute || $self->dberror($query);
2168 $self->{all_pricegroup} = ();
2169 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2170 push @{ $self->{all_pricegroup} }, $ref;
2175 $main::lxdebug->leave_sub();
2180 my ($self, $dbh, $myconfig, $audittrail) = @_;
2182 # table, $reference, $formname, $action, $id, $transdate) = @_;
2189 $dbh = $self->dbconnect($myconfig);
2193 # if we have an id add audittrail, otherwise get a new timestamp
2195 if ($audittrail->{id}) {
2197 $query = qq|SELECT audittrail FROM defaults|;
2199 if ($dbh->selectrow_array($query)) {
2200 my ($null, $employee_id) = $self->get_employee($dbh);
2202 if ($self->{audittrail} && !$myconfig) {
2203 chop $self->{audittrail};
2205 my @a = split /\|/, $self->{audittrail};
2209 my @flds = qw(tablename reference formname action transdate);
2211 # put into hash and remove dups
2213 $key = "$a[2]$a[3]";
2215 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2219 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2220 formname, action, employee_id, transdate)
2221 VALUES ($audittrail->{id}, ?, ?,
2222 ?, ?, $employee_id, ?)|;
2223 my $sth = $dbh->prepare($query) || $self->dberror($query);
2225 foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
2227 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2229 $sth->execute || $self->dberror;
2235 if ($audittrail->{transdate}) {
2236 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2237 formname, action, employee_id, transdate) VALUES (
2238 $audittrail->{id}, '$audittrail->{tablename}', |
2239 .$dbh->quote($audittrail->{reference}).qq|,
2240 '$audittrail->{formname}', '$audittrail->{action}',
2241 $employee_id, '$audittrail->{transdate}')|;
2243 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2244 formname, action, employee_id) VALUES ($audittrail->{id},
2245 '$audittrail->{tablename}', |
2246 .$dbh->quote($audittrail->{reference}).qq|,
2247 '$audittrail->{formname}', '$audittrail->{action}',
2254 $query = qq|SELECT current_timestamp FROM defaults|;
2255 my ($timestamp) = $dbh->selectrow_array($query);
2257 $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2260 $dbh->disconnect if $disconnect;
2269 $main::lxdebug->enter_sub();
2271 my ($type, $country, $NLS_file) = @_;
2275 if ($country && -d "locale/$country") {
2276 $self->{countrycode} = $country;
2277 eval { require "locale/$country/$NLS_file"; };
2280 $self->{NLS_file} = $NLS_file;
2282 push @{ $self->{LONG_MONTH} },
2283 ("January", "February", "March", "April",
2284 "May ", "June", "July", "August",
2285 "September", "October", "November", "December");
2286 push @{ $self->{SHORT_MONTH} },
2287 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2289 $main::lxdebug->leave_sub();
2295 my ($self, $text) = @_;
2297 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2301 $main::lxdebug->enter_sub();
2303 my ($self, $text) = @_;
2305 if (exists $self{subs}{$text}) {
2306 $text = $self{subs}{$text};
2308 if ($self->{countrycode} && $self->{NLS_file}) {
2310 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2314 $main::lxdebug->leave_sub();
2320 $main::lxdebug->enter_sub();
2322 my ($self, $myconfig, $date, $longformat) = @_;
2325 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2330 $spc = $myconfig->{dateformat};
2332 $spc = substr($spc, 1, 1);
2334 if ($date =~ /\D/) {
2335 if ($myconfig->{dateformat} =~ /^yy/) {
2336 ($yy, $mm, $dd) = split /\D/, $date;
2338 if ($myconfig->{dateformat} =~ /^mm/) {
2339 ($mm, $dd, $yy) = split /\D/, $date;
2341 if ($myconfig->{dateformat} =~ /^dd/) {
2342 ($dd, $mm, $yy) = split /\D/, $date;
2345 $date = substr($date, 2);
2346 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2351 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2352 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2354 if ($myconfig->{dateformat} =~ /^dd/) {
2355 if (defined $longformat && $longformat == 0) {
2357 $dd = "0$dd" if ($dd < 10);
2358 $mm = "0$mm" if ($mm < 10);
2359 $longdate = "$dd$spc$mm$spc$yy";
2362 $longdate .= ($spc eq '.') ? ". " : " ";
2363 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2365 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2367 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2368 # Lx-Office is mainly used in Germany or German speaking countries.
2369 if (defined $longformat && $longformat == 0) {
2371 $dd = "0$dd" if ($dd < 10);
2372 $mm = "0$mm" if ($mm < 10);
2373 $longdate = "$yy-$mm-$dd";
2375 $longdate = "$dd. ";
2376 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2379 if (defined $longformat && $longformat == 0) {
2381 $dd = "0$dd" if ($dd < 10);
2382 $mm = "0$mm" if ($mm < 10);
2383 $longdate = "$mm$spc$dd$spc$yy";
2385 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2391 $main::lxdebug->leave_sub();
2397 $main::lxdebug->enter_sub();
2399 my ($self, $myconfig, $date, $longformat) = @_;
2402 $main::lxdebug->leave_sub();
2407 $spc = $myconfig->{dateformat};
2409 $spc = substr($spc, 1, 1);
2411 if ($date =~ /\D/) {
2412 if ($myconfig->{dateformat} =~ /^yy/) {
2413 ($yy, $mm, $dd) = split /\D/, $date;
2414 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2415 ($mm, $dd, $yy) = split /\D/, $date;
2416 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2417 ($dd, $mm, $yy) = split /\D/, $date;
2420 $date = substr($date, 2);
2421 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2426 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2427 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2429 $main::lxdebug->leave_sub();
2430 return ($yy, $mm, $dd);