1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 # Antti Kaihola <akaihola@siba.fi>
17 # Moritz Bunkus (tex code)
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #======================================================================
32 # Utilities for parsing forms
33 # and supporting routines for linking account numbers
34 # used in AR, AP and IS, IR modules
36 #======================================================================
41 $main::lxdebug->enter_sub();
45 my @pairs = split(/&/, $input);
48 my ($name, $value) = split(/=/, $_, 2);
49 $in{$name} = unescape(undef, $value);
52 $main::lxdebug->leave_sub();
57 sub _request_to_hash {
58 $main::lxdebug->enter_sub();
61 my ($i, $loc, $key, $val);
62 my (%ATTACH, $f, $header, $header_body, $len, $buf);
63 my ($boundary, @list, $size, $body, $x, $blah, $name);
65 if ($ENV{'CONTENT_TYPE'}
66 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
67 $boundary = quotemeta('--' . $1);
68 @list = split(/$boundary/, $input);
70 # For some reason there are always 2 extra, that are empty
73 for ($x = 1; $x <= $size; $x++) {
74 $header_body = $list[$x];
75 $header_body =~ /\r\n\r\n|\n\n/;
77 # Here we split the header and body
82 # Now we try to get the file name
84 $name =~ /name=\"(.+)\"/;
85 ($name, $blah) = split(/\"/, $1);
87 # If the form name is not attach, then we need to parse this like
89 if ($name ne "attach") {
90 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
91 $ATTACH{$name} = $body;
93 # Otherwise it is an attachment and we need to finish it up
94 } elsif ($name eq "attach") {
95 $header =~ /filename=\"(.+)\"/;
96 $ATTACH{'FILE_NAME'} = $1;
97 $ATTACH{'FILE_NAME'} =~ s/\"//g;
98 $ATTACH{'FILE_NAME'} =~ s/\s//g;
99 $ATTACH{'FILE_CONTENT'} = $body;
101 for ($i = $x; $list[$i]; $i++) {
102 $list[$i] =~ s/^.+name=$//;
103 $list[$i] =~ /\"(\w+)\"/;
109 $main::lxdebug->leave_sub();
113 $main::lxdebug->leave_sub();
114 return _input_to_hash($input);
119 $main::lxdebug->enter_sub();
125 read(STDIN, $_, $ENV{CONTENT_LENGTH});
127 if ($ENV{QUERY_STRING}) {
128 $_ = $ENV{QUERY_STRING};
135 my %parameters = _request_to_hash($_);
136 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
138 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
140 $self->{action} = lc $self->{action};
141 $self->{action} =~ s/( |-|,|#)/_/g;
143 $self->{version} = "2.1.2";
144 $self->{dbversion} = "2.1.2";
146 $main::lxdebug->leave_sub();
152 $main::lxdebug->enter_sub();
158 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
160 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
166 my ($self, $str, $beenthere) = @_;
168 # for Apache 2 we escape strings twice
169 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
170 # $str = $self->escape($str, 1);
173 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
175 $main::lxdebug->leave_sub();
181 $main::lxdebug->enter_sub();
183 my ($self, $str) = @_;
188 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
190 $main::lxdebug->leave_sub();
196 my ($self, $str) = @_;
198 if ($str && ! ref($str)) {
199 $str =~ s/"/"/g;
208 my ($self, $str) = @_;
210 if ($str && ! ref($str)) {
211 $str =~ s/"/"/g;
223 for (@_) { print qq|<input type=hidden name="$_" value="|.$self->quote($self->{$_}).qq|">\n| }
225 delete $self->{header};
226 for (sort keys %$self) { print qq|<input type=hidden name="$_" value="|.$self->quote($self->{$_}).qq|">\n| }
232 $main::lxdebug->enter_sub();
234 my ($self, $msg) = @_;
236 if ($ENV{HTTP_USER_AGENT}) {
244 <h2 class=error>Error!</h2>
256 if ($self->{error_function}) {
257 &{ $self->{error_function} }($msg);
263 $main::lxdebug->leave_sub();
267 $main::lxdebug->enter_sub();
269 my ($self, $msg) = @_;
271 if ($ENV{HTTP_USER_AGENT}) {
274 if (!$self->{header}) {
287 if ($self->{info_function}) {
288 &{ $self->{info_function} }($msg);
294 $main::lxdebug->leave_sub();
298 $main::lxdebug->enter_sub();
300 my ($self, $str, $cols, $maxrows) = @_;
304 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
306 $maxrows = $rows unless defined $maxrows;
308 $main::lxdebug->leave_sub();
310 return ($rows > $maxrows) ? $maxrows : $rows;
314 $main::lxdebug->enter_sub();
316 my ($self, $msg) = @_;
318 $self->error("$msg\n" . $DBI::errstr);
320 $main::lxdebug->leave_sub();
324 $main::lxdebug->enter_sub();
326 my ($self, $name, $msg) = @_;
328 if ($self->{$name} =~ /^\s*$/) {
331 $main::lxdebug->leave_sub();
335 $main::lxdebug->enter_sub();
339 if ($self->{header}) {
340 $main::lxdebug->leave_sub();
344 my ($stylesheet, $favicon, $charset);
346 if ($ENV{HTTP_USER_AGENT}) {
348 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
350 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
354 if ($self->{favicon} && (-f "$self->{favicon}")) {
356 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
360 if ($self->{charset}) {
362 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
365 if ($self->{landscape}) {
366 $pagelayout = qq|<style type="text/css">
367 \@page { size:landscape; }
370 if ($self->{fokus}) {
371 $fokus = qq|<script type="text/javascript">
373 function fokus(){document.$self->{fokus}.focus();}
380 if ($self->{jsscript} == 1) {
383 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
384 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
385 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
386 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
393 ? "$self->{title} - $self->{titlebar}"
396 print qq|Content-Type: text/html
399 <title>$self->{titlebar}</title>
412 $main::lxdebug->leave_sub();
415 # write Trigger JavaScript-Code ($qty = 1 - only one Trigger)
417 $main::lxdebug->enter_sub();
419 my ($self, $myconfig, $qty,
420 $inputField_1, $align_1, $button_1,
421 $inputField_2, $align_2, $button_2)
424 # set dateform for jsscript
426 $ifFormat = "%d.%m.%Y";
427 if ($myconfig->{dateformat} eq "dd.mm.yy") {
428 $ifFormat = "%d.%m.%Y";
430 if ($myconfig->{dateformat} eq "dd-mm-yy") {
431 $ifFormat = "%d-%m-%Y";
433 if ($myconfig->{dateformat} eq "dd/mm/yy") {
434 $ifFormat = "%d/%m/%Y";
436 if ($myconfig->{dateformat} eq "mm/dd/yy") {
437 $ifFormat = "%m/%d/%Y";
439 if ($myconfig->{dateformat} eq "mm-dd-yy") {
440 $ifFormat = "%m-%d-%Y";
442 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
443 $ifFormat = "%Y-%m-%d";
454 inputField : "$inputField_1",
455 ifFormat :"$ifFormat",
466 inputField : "$inputField_2",
467 ifFormat :"$ifFormat",
475 <script type="text/javascript">
483 $main::lxdebug->leave_sub();
486 } #end sub write_trigger
489 $main::lxdebug->enter_sub();
491 my ($self, $msg) = @_;
493 if ($self->{callback}) {
495 ($script, $argv) = split(/\?/, $self->{callback});
496 exec("perl", "$script", $argv);
504 $main::lxdebug->leave_sub();
507 # sort of columns removed - empty sub
509 $main::lxdebug->enter_sub();
511 my ($self, @columns) = @_;
513 $main::lxdebug->leave_sub();
519 $main::lxdebug->enter_sub();
521 my ($self, $myconfig, $amount, $places, $dash) = @_;
523 #Workaround for $format_amount calls without $places
524 if (!defined $places){
525 (my $dec) = ($amount =~ /\.(\d+)/);
526 $places = length $dec;
529 if ($places =~ /\d/) {
530 $amount = $self->round_amount($amount, $places);
533 # is the amount negative
534 my $negative = ($amount < 0);
538 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
540 my ($whole, $dec) = split /\./, "$amount";
542 $amount = join '', reverse split //, $whole;
543 $fillup = "0" x ($places - length($dec));
545 if ($myconfig->{numberformat} eq '1,000.00') {
546 $amount =~ s/\d{3,}?/$&,/g;
548 $amount = join '', reverse split //, $amount;
549 $amount .= "\.$dec".$fillup if ($places ne '' && $places*1 != 0);
552 if ($myconfig->{numberformat} eq '1.000,00') {
553 $amount =~ s/\d{3,}?/$&./g;
555 $amount = join '', reverse split //, $amount;
556 $amount .= ",$dec".$fillup if ($places ne '' && $places*1 != 0);
559 if ($myconfig->{numberformat} eq '1000,00') {
561 $amount .= ",$dec" .$fillup if ($places ne '' && $places*1 != 0);
565 $amount = ($negative) ? "($amount)" : "$amount";
566 } elsif ($dash =~ /DRCR/) {
567 $amount = ($negative) ? "$amount DR" : "$amount CR";
569 $amount = ($negative) ? "-$amount" : "$amount";
573 if ($dash eq "0" && $places) {
574 if ($myconfig->{numberformat} eq '1.000,00') {
575 $amount = "0" . "," . "0" x $places;
577 $amount = "0" . "." . "0" x $places;
580 $amount = ($dash ne "") ? "$dash" : "0";
584 $main::lxdebug->leave_sub();
590 $main::lxdebug->enter_sub();
592 my ($self, $myconfig, $amount) = @_;
593 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
595 if ($myconfig->{in_numberformat} == 1){
596 # Extra input number format 1000.00 or 1000,00
597 $main::lxdebug->message(LXDebug::DEBUG2, "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
599 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
600 $amount = scalar reverse $amount;
601 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
602 $amount =~ s/\./DOT/;
603 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
605 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
606 $amount =~ s/DOT/\./;
607 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
608 $amount = scalar reverse $amount ;
609 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n");
611 return ($amount * 1);
614 $main::lxdebug->message(LXDebug::DEBUG2, "in_numberformat: " . $main::locale->text('equal Outputformat'));
615 $main::lxdebug->message(LXDebug::DEBUG2, " = numberformat: $myconfig->{numberformat}");
616 if ( ($myconfig->{numberformat} eq '1.000,00')
617 || ($myconfig->{numberformat} eq '1000,00')) {
622 if ($myconfig->{numberformat} eq "1'000.00") {
628 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount. "\n") if ($amount);
629 $main::lxdebug->leave_sub();
631 return ($amount * 1);
635 $main::lxdebug->enter_sub();
637 my ($self, $amount, $places) = @_;
640 # Rounding like "Kaufmannsrunden"
641 # Descr. http://de.wikipedia.org/wiki/Rundung
643 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
646 $amount = $amount * (10 ** ($places));
647 $round_amount = int($amount + .5 * ($amount <=> 0))/(10**($places));
649 $main::lxdebug->leave_sub();
651 return $round_amount;
658 $main::lxdebug->enter_sub();
660 my ($self, $myconfig, $userspath) = @_;
663 # Some variables used for page breaks
664 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
666 my ($current_page, $current_line, $current_row) = (1, 1, 0);
672 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
673 $self->format_string(grep(/notes/, keys(%{$self})));
675 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
676 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
678 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
679 qw(email tel fax name signature));
681 open(IN, "$self->{templates}/$self->{IN}")
682 or $self->error("$self->{IN} : $!");
687 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
689 # OUT is used for the media, screen, printer, email
690 # for postscript we store a copy in a temporary file
692 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
693 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
695 $self->{OUT} = ">$self->{tmpfile}";
699 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
701 open(OUT, ">-") or $self->error("STDOUT : $!");
705 # Do we have to run LaTeX two times? This is needed if
706 # the template contains page references.
709 # first we generate a tmpfile
710 # read file and replace <%variable%>
716 $two_passes = 1 if (/\\pageref/);
719 # detect pagebreak block and its parameters
720 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
721 $chars_per_line = $1;
722 $lines_on_first_page = $2;
723 $lines_on_second_page = $3;
726 last if (/\s*<%end pagebreak%>/);
733 if (/\s*<%foreach /) {
735 # this one we need for the count
737 $var =~ s/\s*<%foreach (.+?)%>/$1/;
739 last if (/\s*<%end /);
745 # display contents of $self->{number}[] array
746 for $i (0 .. $#{ $self->{$var} }) {
749 # Try to detect whether a manual page break is necessary
750 # but only if there was a <%pagebreak ...%> block before
752 if ($chars_per_line) {
754 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
757 my $_description = $self->{"description"}[$i];
758 while ($_description =~ /\\newline/) {
760 $_description =~ s/\\newline//;
762 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
764 if ($current_page == 1) {
765 $lpp = $lines_on_first_page;
767 $lpp = $lines_on_second_page;
770 # Yes we need a manual page break -- or the user has forced one
771 if ((($current_line + $lines) > $lpp) ||
772 ($self->{"_forced_pagebreaks"} && grep(/^${current_row}$/, @{$self->{"_forced_pagebreaks"}}))) {
775 # replace the special variables <%sumcarriedforward%>
778 my $psum = $self->format_amount($myconfig, $sum, 2);
779 $pb =~ s/<%sumcarriedforward%>/$psum/g;
780 $pb =~ s/<%lastpage%>/$current_page/g;
782 # only "normal" variables are supported here
783 # (no <%if, no <%foreach, no <%include)
785 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
787 # page break block is ready to rock
792 $current_line += $lines;
795 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
799 # don't parse par, we need it for each line
801 s/<%(.+?)%>/$self->{$1}[$i]/mg;
807 # if not comes before if!
808 if (/\s*<%if not /) {
810 # check if it is not set and display
812 s/\s*<%if not (.+?)%>/$1/;
814 unless ($self->{$_}) {
816 last if (/\s*<%end /);
826 last if (/\s*<%end /);
834 # check if it is set and display
836 s/\s*<%if (.+?)%>/$1/;
840 last if (/\s*<%end /);
850 last if (/\s*<%end /);
856 # check for <%include filename%>
857 if (/\s*<%include /) {
861 $var =~ s/\s*<%include (.+?)%>/$1/;
864 $var =~ s/(\/|\.\.)//g;
866 # prevent the infinite loop!
867 next if ($self->{"$var"});
869 open(INC, "$self->{templates}/$var")
870 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
879 s/<%(.+?)%>/$self->{$1}/g;
886 # Convert the tex file to postscript
887 if ($self->{format} =~ /(postscript|pdf)/) {
890 $self->{cwd} = cwd();
891 $self->{tmpdir} = "$self->{cwd}/$userspath";
893 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
895 $self->{tmpfile} =~ s/$userspath\///g;
897 if ($self->{format} eq 'postscript') {
899 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
901 $self->error($self->cleanup) if ($?);
904 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
906 $self->error($self->cleanup) if ($?);
909 $self->{tmpfile} =~ s/tex$/dvi/;
911 system("dvips $self->{tmpfile} -o -q > /dev/null");
912 $self->error($self->cleanup . "dvips : $!") if ($?);
913 $self->{tmpfile} =~ s/dvi$/ps/;
915 if ($self->{format} eq 'pdf') {
917 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
919 $self->error($self->cleanup) if ($?);
922 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
924 $self->error($self->cleanup) if ($?);
926 $self->{tmpfile} =~ s/tex$/pdf/;
931 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
933 if ($self->{media} eq 'email') {
937 my $mail = new Mailer;
939 map { $mail->{$_} = $self->{$_} }
940 qw(cc bcc subject message version format charset);
941 $mail->{to} = qq|$self->{email}|;
942 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
943 $mail->{fileid} = "$fileid.";
945 # if we send html or plain text inline
946 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
947 $mail->{contenttype} = "text/html";
949 $mail->{message} =~ s/\r\n/<br>\n/g;
950 $myconfig->{signature} =~ s/\\n/<br>\n/g;
951 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
953 open(IN, $self->{tmpfile})
954 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
956 $mail->{message} .= $_;
963 @{ $mail->{attachments} } = ($self->{tmpfile});
965 $myconfig->{signature} =~ s/\\n/\r\n/g;
966 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
970 my $err = $mail->send($out);
971 $self->error($self->cleanup . "$err") if ($err);
977 my $numbytes = (-s $self->{tmpfile});
978 open(IN, $self->{tmpfile})
979 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
981 $self->{copies} = 1 unless $self->{media} eq 'printer';
983 chdir("$self->{cwd}");
985 for my $i (1 .. $self->{copies}) {
987 open(OUT, $self->{OUT})
988 or $self->error($self->cleanup . "$self->{OUT} : $!");
992 print qq|Content-Type: application/$self->{format}
993 Content-Disposition: attachment; filename="$self->{tmpfile}"
994 Content-Length: $numbytes
998 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1018 chdir("$self->{cwd}");
1019 $main::lxdebug->leave_sub();
1023 $main::lxdebug->enter_sub();
1027 chdir("$self->{tmpdir}");
1030 if (-f "$self->{tmpfile}.err") {
1031 open(FH, "$self->{tmpfile}.err");
1036 if ($self->{tmpfile}) {
1039 $self->{tmpfile} =~ s/\.\w+$//g;
1040 my $tmpfile = $self->{tmpfile};
1041 unlink(<$tmpfile.*>);
1044 chdir("$self->{cwd}");
1046 $main::lxdebug->leave_sub();
1052 $main::lxdebug->enter_sub();
1054 my ($self, @fields) = @_;
1057 %unique_fields = map({ $_ => 1 } @fields);
1058 @fields = keys(%unique_fields);
1060 foreach my $field (@fields) {
1061 next unless ($self->{$field} =~ /\<pagebreak\>/);
1062 $self->{$field} =~ s/\<pagebreak\>//g;
1063 if ($field =~ /.*_(\d+)$/) {
1064 if (!$self->{"_forced_pagebreaks"}) {
1065 $self->{"_forced_pagebreaks"} = [];
1067 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1071 my $format = $self->{format};
1072 if ($self->{format} =~ /(postscript|pdf)/) {
1079 '<', '>', quotemeta('\n'), '
1083 '&', quotemeta('\n'), '
1085 '"', '\$', '%', '_', '#', quotemeta('^'),
1086 '{', '}', '<', '>', '£', "\r"
1092 quotemeta('\n') => '<br>',
1103 quotemeta('^') => '\^\\',
1108 quotemeta('\n') => '\newline ',
1115 foreach my $key (@{ $replace{order}{$format} }) {
1116 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1119 # Allow some HTML markup to be converted into the output format's
1120 # corresponding markup code, e.g. bold or italic.
1121 if ('html' eq $format) {
1122 my @markup_replace = ('b', 'i', 's', 'u');
1124 foreach my $key (@markup_replace) {
1125 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1128 } elsif ('tex' eq $format) {
1129 my %markup_replace = ('b' => 'textbf',
1131 'u' => 'underline');
1133 foreach my $field (@fields) {
1134 foreach my $key (keys(%markup_replace)) {
1135 my $new = $markup_replace{$key};
1137 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1142 $main::lxdebug->leave_sub();
1146 $main::lxdebug->enter_sub();
1148 my ($self, $date, $myconfig) = @_;
1150 if ($date && $date =~ /\D/) {
1152 if ($myconfig->{dateformat} =~ /^yy/) {
1153 ($yy, $mm, $dd) = split /\D/, $date;
1155 if ($myconfig->{dateformat} =~ /^mm/) {
1156 ($mm, $dd, $yy) = split /\D/, $date;
1158 if ($myconfig->{dateformat} =~ /^dd/) {
1159 ($dd, $mm, $yy) = split /\D/, $date;
1164 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1165 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1167 $dd = "0$dd" if ($dd < 10);
1168 $mm = "0$mm" if ($mm < 10);
1170 $date = "$yy$mm$dd";
1173 $main::lxdebug->leave_sub();
1178 # Database routines used throughout
1181 $main::lxdebug->enter_sub();
1183 my ($self, $myconfig) = @_;
1185 # connect to database
1187 DBI->connect($myconfig->{dbconnect},
1188 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1192 if ($myconfig->{dboptions}) {
1193 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1196 $main::lxdebug->leave_sub();
1201 sub dbconnect_noauto {
1202 $main::lxdebug->enter_sub();
1204 my ($self, $myconfig) = @_;
1206 # connect to database
1208 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1209 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1213 if ($myconfig->{dboptions}) {
1214 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1217 $main::lxdebug->leave_sub();
1222 sub update_balance {
1223 $main::lxdebug->enter_sub();
1225 my ($self, $dbh, $table, $field, $where, $value) = @_;
1227 # if we have a value, go do it
1230 # retrieve balance from table
1231 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1232 my $sth = $dbh->prepare($query);
1234 $sth->execute || $self->dberror($query);
1235 my ($balance) = $sth->fetchrow_array;
1241 $query = "UPDATE $table SET $field = $balance WHERE $where";
1242 $dbh->do($query) || $self->dberror($query);
1244 $main::lxdebug->leave_sub();
1247 sub update_exchangerate {
1248 $main::lxdebug->enter_sub();
1250 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1252 # some sanity check for currency
1254 $main::lxdebug->leave_sub();
1258 my $query = qq|SELECT e.curr FROM exchangerate e
1259 WHERE e.curr = '$curr'
1260 AND e.transdate = '$transdate'
1262 my $sth = $dbh->prepare($query);
1263 $sth->execute || $self->dberror($query);
1266 if ($buy != 0 && $sell != 0) {
1267 $set = "buy = $buy, sell = $sell";
1268 } elsif ($buy != 0) {
1269 $set = "buy = $buy";
1270 } elsif ($sell != 0) {
1271 $set = "sell = $sell";
1274 if ($sth->fetchrow_array) {
1275 $query = qq|UPDATE exchangerate
1277 WHERE curr = '$curr'
1278 AND transdate = '$transdate'|;
1280 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1281 VALUES ('$curr', $buy, $sell, '$transdate')|;
1284 $dbh->do($query) || $self->dberror($query);
1286 $main::lxdebug->leave_sub();
1289 sub save_exchangerate {
1290 $main::lxdebug->enter_sub();
1292 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1294 my $dbh = $self->dbconnect($myconfig);
1296 my ($buy, $sell) = (0, 0);
1297 $buy = $rate if $fld eq 'buy';
1298 $sell = $rate if $fld eq 'sell';
1300 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1304 $main::lxdebug->leave_sub();
1307 sub get_exchangerate {
1308 $main::lxdebug->enter_sub();
1310 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1312 unless ($transdate) {
1313 $main::lxdebug->leave_sub();
1317 my $query = qq|SELECT e.$fld FROM exchangerate e
1318 WHERE e.curr = '$curr'
1319 AND e.transdate = '$transdate'|;
1320 my $sth = $dbh->prepare($query);
1321 $sth->execute || $self->dberror($query);
1323 my ($exchangerate) = $sth->fetchrow_array;
1326 $main::lxdebug->leave_sub();
1328 return $exchangerate;
1331 sub check_exchangerate {
1332 $main::lxdebug->enter_sub();
1334 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1336 unless ($transdate) {
1337 $main::lxdebug->leave_sub();
1341 my $dbh = $self->dbconnect($myconfig);
1343 my $query = qq|SELECT e.$fld FROM exchangerate e
1344 WHERE e.curr = '$currency'
1345 AND e.transdate = '$transdate'|;
1346 my $sth = $dbh->prepare($query);
1347 $sth->execute || $self->dberror($query);
1349 my ($exchangerate) = $sth->fetchrow_array;
1353 $main::lxdebug->leave_sub();
1355 return $exchangerate;
1359 $main::lxdebug->enter_sub();
1361 my ($self, $dbh, $id) = @_;
1365 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1366 if ($self->{"shipto$item"}) {
1367 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1369 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1373 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1374 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1375 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1376 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1377 '$self->{shiptozipcode}', '$self->{shiptocity}',
1378 '$self->{shiptocountry}', '$self->{shiptocontact}',
1379 '$self->{shiptophone}', '$self->{shiptofax}',
1380 '$self->{shiptoemail}')|;
1381 $dbh->do($query) || $self->dberror($query);
1384 $main::lxdebug->leave_sub();
1388 $main::lxdebug->enter_sub();
1390 my ($self, $dbh) = @_;
1392 my $query = qq|SELECT e.id, e.name FROM employee e
1393 WHERE e.login = '$self->{login}'|;
1394 my $sth = $dbh->prepare($query);
1395 $sth->execute || $self->dberror($query);
1397 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1398 $self->{employee_id} *= 1;
1402 $main::lxdebug->leave_sub();
1405 # get other contact for transaction and form - html/tex
1407 $main::lxdebug->enter_sub();
1409 my ($self, $dbh, $id) = @_;
1411 my $query = qq|SELECT c.*
1414 $sth = $dbh->prepare($query);
1415 $sth->execute || $self->dberror($query);
1417 $ref = $sth->fetchrow_hashref(NAME_lc);
1419 push @{ $self->{$_} }, $ref;
1422 $main::lxdebug->leave_sub();
1425 # get contacts for id, if no contact return {"","","","",""}
1427 $main::lxdebug->enter_sub();
1429 my ($self, $dbh, $id) = @_;
1431 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1433 WHERE cp_cv_id=$id|;
1434 my $sth = $dbh->prepare($query);
1435 $sth->execute || $self->dberror($query);
1438 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1439 push @{ $self->{all_contacts} }, $ref;
1444 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1447 $main::lxdebug->leave_sub();
1450 # this sub gets the id and name from $table
1452 $main::lxdebug->enter_sub();
1454 my ($self, $myconfig, $table) = @_;
1456 # connect to database
1457 my $dbh = $self->dbconnect($myconfig);
1459 my $name = $self->like(lc $self->{$table});
1460 my $customernumber = $self->like(lc $self->{customernumber});
1462 if ($self->{customernumber} ne "") {
1463 $query = qq~SELECT c.id, c.name,
1464 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1466 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1469 $query = qq~SELECT c.id, c.name,
1470 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1472 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1476 if ($self->{openinvoices}) {
1477 $query = qq~SELECT DISTINCT c.id, c.name,
1478 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1479 FROM $self->{arap} a
1480 JOIN $table c ON (a.${table}_id = c.id)
1481 WHERE NOT a.amount = a.paid
1482 AND lower(c.name) LIKE '$name'
1485 my $sth = $dbh->prepare($query);
1487 $sth->execute || $self->dberror($query);
1490 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1491 push(@{ $self->{name_list} }, $ref);
1497 $main::lxdebug->leave_sub();
1502 # the selection sub is used in the AR, AP, IS, IR and OE module
1505 $main::lxdebug->enter_sub();
1507 my ($self, $myconfig, $table, $module) = @_;
1510 my $dbh = $self->dbconnect($myconfig);
1512 my $query = qq|SELECT count(*) FROM $table|;
1513 my $sth = $dbh->prepare($query);
1514 $sth->execute || $self->dberror($query);
1515 my ($count) = $sth->fetchrow_array;
1518 # build selection list
1519 if ($count < $myconfig->{vclimit}) {
1520 $query = qq|SELECT id, name
1521 FROM $table WHERE not obsolete
1523 $sth = $dbh->prepare($query);
1524 $sth->execute || $self->dberror($query);
1526 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1527 push @{ $self->{"all_$table"} }, $ref;
1535 $self->get_employee($dbh);
1537 # setup sales contacts
1538 $query = qq|SELECT e.id, e.name
1541 AND NOT e.id = $self->{employee_id}|;
1542 $sth = $dbh->prepare($query);
1543 $sth->execute || $self->dberror($query);
1545 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1546 push @{ $self->{all_employees} }, $ref;
1551 push @{ $self->{all_employees} },
1552 { id => $self->{employee_id},
1553 name => $self->{employee} };
1555 # sort the whole thing
1556 @{ $self->{all_employees} } =
1557 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1559 if ($module eq 'AR') {
1561 # prepare query for departments
1562 $query = qq|SELECT d.id, d.description
1568 $query = qq|SELECT d.id, d.description
1573 $sth = $dbh->prepare($query);
1574 $sth->execute || $self->dberror($query);
1576 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1577 push @{ $self->{all_departments} }, $ref;
1582 $main::lxdebug->leave_sub();
1585 # this is only used for reports
1586 sub all_departments {
1587 $main::lxdebug->enter_sub();
1589 my ($self, $myconfig, $table) = @_;
1591 my $dbh = $self->dbconnect($myconfig);
1592 my $where = "1 = 1";
1594 if (defined $table) {
1595 if ($table eq 'customer') {
1596 $where = " d.role = 'P'";
1600 my $query = qq|SELECT d.id, d.description
1604 my $sth = $dbh->prepare($query);
1605 $sth->execute || $self->dberror($query);
1607 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1608 push @{ $self->{all_departments} }, $ref;
1614 $main::lxdebug->leave_sub();
1618 $main::lxdebug->enter_sub();
1620 my ($self, $module, $myconfig, $table) = @_;
1622 $self->all_vc($myconfig, $table, $module);
1624 # get last customers or vendors
1627 my $dbh = $self->dbconnect($myconfig);
1631 # now get the account numbers
1633 qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1635 WHERE c.link LIKE '%$module%'
1638 $sth = $dbh->prepare($query);
1639 $sth->execute || $self->dberror($query);
1641 $self->{accounts} = "";
1642 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1644 foreach my $key (split /:/, $ref->{link}) {
1645 if ($key =~ /$module/) {
1647 # cross reference for keys
1648 $xkeyref{ $ref->{accno} } = $key;
1650 push @{ $self->{"${module}_links"}{$key} },
1651 { accno => $ref->{accno},
1652 description => $ref->{description},
1653 taxkey => $ref->{taxkey_id} };
1655 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1661 if (($module eq "AP") || ($module eq "AR")) {
1663 # get tax rates and description
1664 $query = qq| SELECT * FROM tax t|;
1665 $sth = $dbh->prepare($query);
1666 $sth->execute || $self->dberror($query);
1668 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1669 push @{ $self->{TAX} }, $ref;
1675 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1677 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1678 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1679 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1680 c.name AS $table, a.department_id, d.description AS department,
1681 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1682 a.employee_id, e.name AS employee, a.gldate
1684 JOIN $table c ON (a.${table}_id = c.id)
1685 LEFT JOIN employee e ON (e.id = a.employee_id)
1686 LEFT JOIN department d ON (d.id = a.department_id)
1687 WHERE a.id = $self->{id}|;
1688 $sth = $dbh->prepare($query);
1689 $sth->execute || $self->dberror($query);
1691 $ref = $sth->fetchrow_hashref(NAME_lc);
1692 foreach $key (keys %$ref) {
1693 $self->{$key} = $ref->{$key};
1697 # get amounts from individual entries
1698 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1699 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1701 JOIN chart c ON (c.id = a.chart_id)
1702 LEFT JOIN project p ON (p.id = a.project_id)
1703 LEFT Join tax t ON (a.taxkey = t.taxkey)
1704 WHERE a.trans_id = $self->{id}
1705 AND a.fx_transaction = '0'
1706 ORDER BY a.transdate|;
1707 $sth = $dbh->prepare($query);
1708 $sth->execute || $self->dberror($query);
1710 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1712 # get exchangerate for currency
1713 $self->{exchangerate} =
1714 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1717 # store amounts in {acc_trans}{$key} for multiple accounts
1718 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1719 $ref->{exchangerate} =
1720 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1723 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1727 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1728 (SELECT c.accno FROM chart c
1729 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1730 (SELECT c.accno FROM chart c
1731 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1733 $sth = $dbh->prepare($query);
1734 $sth->execute || $self->dberror($query);
1736 $ref = $sth->fetchrow_hashref(NAME_lc);
1737 map { $self->{$_} = $ref->{$_} } keys %$ref;
1743 $query = qq|SELECT current_date AS transdate,
1744 d.curr AS currencies, d.closedto, d.revtrans,
1745 (SELECT c.accno FROM chart c
1746 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1747 (SELECT c.accno FROM chart c
1748 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1750 $sth = $dbh->prepare($query);
1751 $sth->execute || $self->dberror($query);
1753 $ref = $sth->fetchrow_hashref(NAME_lc);
1754 map { $self->{$_} = $ref->{$_} } keys %$ref;
1757 if ($self->{"$self->{vc}_id"}) {
1759 # only setup currency
1760 ($self->{currency}) = split /:/, $self->{currencies};
1764 $self->lastname_used($dbh, $myconfig, $table, $module);
1766 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1768 # get exchangerate for currency
1769 $self->{exchangerate} =
1770 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1779 $main::lxdebug->leave_sub();
1783 $main::lxdebug->enter_sub();
1785 my ($self, $dbh, $myconfig, $table, $module) = @_;
1787 my $arap = ($table eq 'customer') ? "ar" : "ap";
1788 my $where = "1 = 1";
1790 if ($self->{type} =~ /_order/) {
1792 $where = "quotation = '0'";
1794 if ($self->{type} =~ /_quotation/) {
1796 $where = "quotation = '1'";
1799 my $query = qq|SELECT MAX(id) FROM $arap
1801 AND ${table}_id > 0|;
1802 my $sth = $dbh->prepare($query);
1803 $sth->execute || $self->dberror($query);
1805 my ($trans_id) = $sth->fetchrow_array;
1809 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1810 current_date + ct.terms AS duedate, a.department_id,
1811 d.description AS department
1813 JOIN $table ct ON (a.${table}_id = ct.id)
1814 LEFT JOIN department d ON (a.department_id = d.id)
1815 WHERE a.id = $trans_id|;
1816 $sth = $dbh->prepare($query);
1817 $sth->execute || $self->dberror($query);
1819 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1820 $self->{duedate}, $self->{department_id}, $self->{department})
1821 = $sth->fetchrow_array;
1824 $main::lxdebug->leave_sub();
1828 $main::lxdebug->enter_sub();
1830 my ($self, $myconfig, $thisdate, $days) = @_;
1832 my $dbh = $self->dbconnect($myconfig);
1837 my $dateformat = $myconfig->{dateformat};
1838 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1840 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1842 $sth = $dbh->prepare($query);
1843 $sth->execute || $self->dberror($query);
1845 $query = qq|SELECT current_date AS thisdate
1847 $sth = $dbh->prepare($query);
1848 $sth->execute || $self->dberror($query);
1851 ($thisdate) = $sth->fetchrow_array;
1856 $main::lxdebug->leave_sub();
1862 $main::lxdebug->enter_sub();
1864 my ($self, $string) = @_;
1866 if ($string !~ /%/) {
1867 $string = "%$string%";
1870 $string =~ s/\'/\'\'/g;
1872 $main::lxdebug->leave_sub();
1878 $main::lxdebug->enter_sub();
1880 my ($self, $flds, $new, $count, $numrows) = @_;
1884 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1890 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1892 $j = $item->{ndx} - 1;
1893 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1897 for $i ($count + 1 .. $numrows) {
1898 map { delete $self->{"${_}_$i"} } @{$flds};
1901 $main::lxdebug->leave_sub();
1905 $main::lxdebug->enter_sub();
1907 my ($self, $myconfig) = @_;
1911 my $dbh = $self->dbconnect_noauto($myconfig);
1913 my $query = qq|DELETE FROM status
1914 WHERE formname = '$self->{formname}'
1916 my $sth = $dbh->prepare($query) || $self->dberror($query);
1918 if ($self->{formname} =~ /(check|receipt)/) {
1919 for $i (1 .. $self->{rowcount}) {
1920 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1924 $sth->execute($self->{id}) || $self->dberror($query);
1928 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1929 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1931 my %queued = split / /, $self->{queued};
1933 if ($self->{formname} =~ /(check|receipt)/) {
1935 # this is a check or receipt, add one entry for each lineitem
1936 my ($accno) = split /--/, $self->{account};
1937 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1938 chart_id) VALUES (?, '$printed',
1939 '$queued{$self->{formname}}', '$self->{prinform}',
1940 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1941 $sth = $dbh->prepare($query) || $self->dberror($query);
1943 for $i (1 .. $self->{rowcount}) {
1944 if ($self->{"checked_$i"}) {
1945 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1950 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1951 spoolfile, formname)
1952 VALUES ($self->{id}, '$printed', '$emailed',
1953 '$queued{$self->{formname}}', '$self->{formname}')|;
1954 $dbh->do($query) || $self->dberror($query);
1960 $main::lxdebug->leave_sub();
1964 $main::lxdebug->enter_sub();
1966 my ($self, $dbh) = @_;
1968 my ($query, $printed, $emailed);
1970 my $formnames = $self->{printed};
1971 my $emailforms = $self->{emailed};
1973 my $query = qq|DELETE FROM status
1974 WHERE formname = '$self->{formname}'
1975 AND trans_id = $self->{id}|;
1976 $dbh->do($query) || $self->dberror($query);
1978 # this only applies to the forms
1979 # checks and receipts are posted when printed or queued
1981 if ($self->{queued}) {
1982 my %queued = split / /, $self->{queued};
1984 foreach my $formname (keys %queued) {
1985 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1986 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1988 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1989 spoolfile, formname)
1990 VALUES ($self->{id}, '$printed', '$emailed',
1991 '$queued{$formname}', '$formname')|;
1992 $dbh->do($query) || $self->dberror($query);
1994 $formnames =~ s/$self->{formname}//;
1995 $emailforms =~ s/$self->{formname}//;
2000 # save printed, emailed info
2001 $formnames =~ s/^ +//g;
2002 $emailforms =~ s/^ +//g;
2005 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2006 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2008 foreach my $formname (keys %status) {
2009 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2010 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2012 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2013 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2014 $dbh->do($query) || $self->dberror($query);
2017 $main::lxdebug->leave_sub();
2020 sub update_defaults {
2021 $main::lxdebug->enter_sub();
2023 my ($self, $myconfig, $fld) = @_;
2025 my $dbh = $self->dbconnect_noauto($myconfig);
2026 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2027 my $sth = $dbh->prepare($query);
2029 $sth->execute || $self->dberror($query);
2030 my ($var) = $sth->fetchrow_array;
2035 $query = qq|UPDATE defaults
2037 $dbh->do($query) || $form->dberror($query);
2042 $main::lxdebug->leave_sub();
2047 sub update_business {
2048 $main::lxdebug->enter_sub();
2050 my ($self, $myconfig, $business_id) = @_;
2052 my $dbh = $self->dbconnect_noauto($myconfig);
2054 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2055 my $sth = $dbh->prepare($query);
2057 $sth->execute || $self->dberror($query);
2058 my ($var) = $sth->fetchrow_array;
2063 $query = qq|UPDATE business
2064 SET customernumberinit = '$var' WHERE id=$business_id|;
2065 $dbh->do($query) || $form->dberror($query);
2070 $main::lxdebug->leave_sub();
2076 $main::lxdebug->enter_sub();
2078 my ($self, $myconfig, $salesman) = @_;
2080 my $dbh = $self->dbconnect($myconfig);
2082 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2083 my $sth = $dbh->prepare($query);
2084 $sth->execute || $self->dberror($query);
2087 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2088 push(@{ $self->{salesman_list} }, $ref);
2092 $main::lxdebug->leave_sub();
2097 sub get_partsgroup {
2098 $main::lxdebug->enter_sub();
2100 my ($self, $myconfig, $p) = @_;
2102 my $dbh = $self->dbconnect($myconfig);
2104 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2106 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2108 if ($p->{searchitems} eq 'part') {
2110 WHERE p.inventory_accno_id > 0|;
2112 if ($p->{searchitems} eq 'service') {
2114 WHERE p.inventory_accno_id IS NULL|;
2116 if ($p->{searchitems} eq 'assembly') {
2118 WHERE p.assembly = '1'|;
2120 if ($p->{searchitems} eq 'labor') {
2122 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2126 ORDER BY partsgroup|;
2129 $query = qq|SELECT id, partsgroup FROM partsgroup
2130 ORDER BY partsgroup|;
2133 if ($p->{language_code}) {
2134 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2135 t.description AS translation
2137 JOIN parts p ON (p.partsgroup_id = pg.id)
2138 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2139 ORDER BY translation|;
2142 my $sth = $dbh->prepare($query);
2143 $sth->execute || $self->dberror($query);
2145 $self->{all_partsgroup} = ();
2146 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2147 push @{ $self->{all_partsgroup} }, $ref;
2151 $main::lxdebug->leave_sub();
2155 sub get_pricegroup {
2156 $main::lxdebug->enter_sub();
2158 my ($self, $myconfig, $p) = @_;
2160 my $dbh = $self->dbconnect($myconfig);
2162 my $query = qq|SELECT p.id, p.pricegroup
2166 ORDER BY pricegroup|;
2169 $query = qq|SELECT id, pricegroup FROM pricegroup
2170 ORDER BY pricegroup|;
2173 my $sth = $dbh->prepare($query);
2174 $sth->execute || $self->dberror($query);
2176 $self->{all_pricegroup} = ();
2177 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2178 push @{ $self->{all_pricegroup} }, $ref;
2183 $main::lxdebug->leave_sub();
2188 my ($self, $dbh, $myconfig, $audittrail) = @_;
2190 # table, $reference, $formname, $action, $id, $transdate) = @_;
2197 $dbh = $self->dbconnect($myconfig);
2201 # if we have an id add audittrail, otherwise get a new timestamp
2203 if ($audittrail->{id}) {
2205 $query = qq|SELECT audittrail FROM defaults|;
2207 if ($dbh->selectrow_array($query)) {
2208 my ($null, $employee_id) = $self->get_employee($dbh);
2210 if ($self->{audittrail} && !$myconfig) {
2211 chop $self->{audittrail};
2213 my @a = split /\|/, $self->{audittrail};
2217 my @flds = qw(tablename reference formname action transdate);
2219 # put into hash and remove dups
2221 $key = "$a[2]$a[3]";
2223 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2227 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2228 formname, action, employee_id, transdate)
2229 VALUES ($audittrail->{id}, ?, ?,
2230 ?, ?, $employee_id, ?)|;
2231 my $sth = $dbh->prepare($query) || $self->dberror($query);
2233 foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
2235 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2237 $sth->execute || $self->dberror;
2243 if ($audittrail->{transdate}) {
2244 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2245 formname, action, employee_id, transdate) VALUES (
2246 $audittrail->{id}, '$audittrail->{tablename}', |
2247 .$dbh->quote($audittrail->{reference}).qq|,
2248 '$audittrail->{formname}', '$audittrail->{action}',
2249 $employee_id, '$audittrail->{transdate}')|;
2251 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2252 formname, action, employee_id) VALUES ($audittrail->{id},
2253 '$audittrail->{tablename}', |
2254 .$dbh->quote($audittrail->{reference}).qq|,
2255 '$audittrail->{formname}', '$audittrail->{action}',
2262 $query = qq|SELECT current_timestamp FROM defaults|;
2263 my ($timestamp) = $dbh->selectrow_array($query);
2265 $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2268 $dbh->disconnect if $disconnect;
2277 $main::lxdebug->enter_sub();
2279 my ($type, $country, $NLS_file) = @_;
2283 if ($country && -d "locale/$country") {
2284 $self->{countrycode} = $country;
2285 eval { require "locale/$country/$NLS_file"; };
2288 $self->{NLS_file} = $NLS_file;
2290 push @{ $self->{LONG_MONTH} },
2291 ("January", "February", "March", "April",
2292 "May ", "June", "July", "August",
2293 "September", "October", "November", "December");
2294 push @{ $self->{SHORT_MONTH} },
2295 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2297 $main::lxdebug->leave_sub();
2303 my ($self, $text) = @_;
2305 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2309 $main::lxdebug->enter_sub();
2311 my ($self, $text) = @_;
2313 if (exists $self{subs}{$text}) {
2314 $text = $self{subs}{$text};
2316 if ($self->{countrycode} && $self->{NLS_file}) {
2318 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2322 $main::lxdebug->leave_sub();
2328 $main::lxdebug->enter_sub();
2330 my ($self, $myconfig, $date, $longformat) = @_;
2333 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2338 $spc = $myconfig->{dateformat};
2340 $spc = substr($spc, 1, 1);
2342 if ($date =~ /\D/) {
2343 if ($myconfig->{dateformat} =~ /^yy/) {
2344 ($yy, $mm, $dd) = split /\D/, $date;
2346 if ($myconfig->{dateformat} =~ /^mm/) {
2347 ($mm, $dd, $yy) = split /\D/, $date;
2349 if ($myconfig->{dateformat} =~ /^dd/) {
2350 ($dd, $mm, $yy) = split /\D/, $date;
2353 $date = substr($date, 2);
2354 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2359 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2360 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2362 if ($myconfig->{dateformat} =~ /^dd/) {
2363 if (defined $longformat && $longformat == 0) {
2365 $dd = "0$dd" if ($dd < 10);
2366 $mm = "0$mm" if ($mm < 10);
2367 $longdate = "$dd$spc$mm$spc$yy";
2370 $longdate .= ($spc eq '.') ? ". " : " ";
2371 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2373 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2375 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2376 # Lx-Office is mainly used in Germany or German speaking countries.
2377 if (defined $longformat && $longformat == 0) {
2379 $dd = "0$dd" if ($dd < 10);
2380 $mm = "0$mm" if ($mm < 10);
2381 $longdate = "$yy-$mm-$dd";
2383 $longdate = "$dd. ";
2384 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2387 if (defined $longformat && $longformat == 0) {
2389 $dd = "0$dd" if ($dd < 10);
2390 $mm = "0$mm" if ($mm < 10);
2391 $longdate = "$mm$spc$dd$spc$yy";
2393 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2399 $main::lxdebug->leave_sub();
2405 $main::lxdebug->enter_sub();
2407 my ($self, $myconfig, $date, $longformat) = @_;
2410 $main::lxdebug->leave_sub();
2415 $spc = $myconfig->{dateformat};
2417 $spc = substr($spc, 1, 1);
2419 if ($date =~ /\D/) {
2420 if ($myconfig->{dateformat} =~ /^yy/) {
2421 ($yy, $mm, $dd) = split /\D/, $date;
2422 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2423 ($mm, $dd, $yy) = split /\D/, $date;
2424 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2425 ($dd, $mm, $yy) = split /\D/, $date;
2428 $date = substr($date, 2);
2429 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2434 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2435 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2437 $main::lxdebug->leave_sub();
2438 return ($yy, $mm, $dd);