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 #======================================================================
43 $main::lxdebug->enter_sub();
47 my @pairs = split(/&/, $input);
50 my ($name, $value) = split(/=/, $_, 2);
51 $in{$name} = unescape(undef, $value);
54 $main::lxdebug->leave_sub();
59 sub _request_to_hash {
60 $main::lxdebug->enter_sub();
63 my ($i, $loc, $key, $val);
64 my (%ATTACH, $f, $header, $header_body, $len, $buf);
65 my ($boundary, @list, $size, $body, $x, $blah, $name);
67 if ($ENV{'CONTENT_TYPE'}
68 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
69 $boundary = quotemeta('--' . $1);
70 @list = split(/$boundary/, $input);
72 # For some reason there are always 2 extra, that are empty
75 for ($x = 1; $x <= $size; $x++) {
76 $header_body = $list[$x];
77 $header_body =~ /\r\n\r\n|\n\n/;
79 # Here we split the header and body
84 # Now we try to get the file name
86 $name =~ /name=\"(.+)\"/;
87 ($name, $blah) = split(/\"/, $1);
89 # If the form name is not attach, then we need to parse this like
91 if ($name ne "attach") {
92 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
93 $ATTACH{$name} = $body;
95 # Otherwise it is an attachment and we need to finish it up
96 } elsif ($name eq "attach") {
97 $header =~ /filename=\"(.+)\"/;
98 $ATTACH{'FILE_NAME'} = $1;
99 $ATTACH{'FILE_NAME'} =~ s/\"//g;
100 $ATTACH{'FILE_NAME'} =~ s/\s//g;
101 $ATTACH{'FILE_CONTENT'} = $body;
103 for ($i = $x; $list[$i]; $i++) {
104 $list[$i] =~ s/^.+name=$//;
105 $list[$i] =~ /\"(\w+)\"/;
111 $main::lxdebug->leave_sub();
115 $main::lxdebug->leave_sub();
116 return _input_to_hash($input);
121 $main::lxdebug->enter_sub();
127 read(STDIN, $_, $ENV{CONTENT_LENGTH});
129 if ($ENV{QUERY_STRING}) {
130 $_ = $ENV{QUERY_STRING};
137 my %parameters = _request_to_hash($_);
138 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
140 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
142 $self->{action} = lc $self->{action};
143 $self->{action} =~ s/( |-|,|#)/_/g;
145 $self->{version} = "2.2.0";
146 $self->{dbversion} = "2.2.0";
148 $main::lxdebug->leave_sub();
154 $main::lxdebug->enter_sub();
160 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
162 $main::lxdebug->leave_sub();
166 $main::lxdebug->enter_sub();
168 my ($self, $str, $beenthere) = @_;
170 # for Apache 2 we escape strings twice
171 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
172 # $str = $self->escape($str, 1);
175 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
177 $main::lxdebug->leave_sub();
183 $main::lxdebug->enter_sub();
185 my ($self, $str) = @_;
190 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
192 $main::lxdebug->leave_sub();
198 my ($self, $str) = @_;
200 if ($str && !ref($str)) {
201 $str =~ s/"/"/g;
209 my ($self, $str) = @_;
211 if ($str && !ref($str)) {
212 $str =~ s/"/"/g;
224 print qq|<input type=hidden name="$_" value="|
225 . $self->quote($self->{$_})
229 delete $self->{header};
230 for (sort keys %$self) {
231 print qq|<input type=hidden name="$_" value="|
232 . $self->quote($self->{$_})
240 $main::lxdebug->enter_sub();
242 my ($self, $msg) = @_;
244 if ($ENV{HTTP_USER_AGENT}) {
252 <h2 class=error>Error!</h2>
264 if ($self->{error_function}) {
265 &{ $self->{error_function} }($msg);
271 $main::lxdebug->leave_sub();
275 $main::lxdebug->enter_sub();
277 my ($self, $msg) = @_;
279 if ($ENV{HTTP_USER_AGENT}) {
282 if (!$self->{header}) {
295 if ($self->{info_function}) {
296 &{ $self->{info_function} }($msg);
302 $main::lxdebug->leave_sub();
306 $main::lxdebug->enter_sub();
308 my ($self, $str, $cols, $maxrows) = @_;
312 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
314 $maxrows = $rows unless defined $maxrows;
316 $main::lxdebug->leave_sub();
318 return ($rows > $maxrows) ? $maxrows : $rows;
322 $main::lxdebug->enter_sub();
324 my ($self, $msg) = @_;
326 $self->error("$msg\n" . $DBI::errstr);
328 $main::lxdebug->leave_sub();
332 $main::lxdebug->enter_sub();
334 my ($self, $name, $msg) = @_;
336 if ($self->{$name} =~ /^\s*$/) {
339 $main::lxdebug->leave_sub();
343 $main::lxdebug->enter_sub();
347 if ($self->{header}) {
348 $main::lxdebug->leave_sub();
352 my ($stylesheet, $favicon, $charset);
354 if ($ENV{HTTP_USER_AGENT}) {
356 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
358 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
362 if ($self->{favicon} && (-f "$self->{favicon}")) {
364 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
368 if ($self->{charset}) {
370 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
373 if ($self->{landscape}) {
374 $pagelayout = qq|<style type="text/css">
375 \@page { size:landscape; }
378 if ($self->{fokus}) {
379 $fokus = qq|<script type="text/javascript">
381 function fokus(){document.$self->{fokus}.focus();}
388 if ($self->{jsscript} == 1) {
391 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
392 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
393 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
394 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
401 ? "$self->{title} - $self->{titlebar}"
404 print qq|Content-Type: text/html
408 <title>$self->{titlebar}</title>
421 $main::lxdebug->leave_sub();
424 sub parse_html_template {
425 $main::lxdebug->enter_sub();
427 my ($self, $myconfig, $file, $additional_params) = @_;
429 if (-f "templates/webpages/${file}_" . $myconfig->{"countrycode"} .
431 $file = "templates/webpages/${file}_" . $myconfig->{"countrycode"} .
433 } elsif (-f "templates/webpages/${file}.html") {
434 $file = "templates/webpages/${file}.html";
436 $self->error("Web page template '${file}' not found.");
439 my $template = HTML::Template->new("filename" => $file,
440 "die_on_bad_params" => 0,
442 "case_sensitive" => 1,
443 "loop_context_vars" => 1,
446 $additional_params = {} unless ($additional_params);
447 if ($self->{"DEBUG"}) {
448 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
451 if ($additional_params->{"DEBUG"}) {
452 $additional_params->{"DEBUG"} =
453 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
456 my @additional_param_names = keys(%{$additional_params});
458 foreach my $key ($template->param()) {
459 if (grep(/^${key}$/, @additional_param_names)) {
460 $template->param($key => $additional_params->{$key});
462 $template->param($key => $self->{$key});
466 my $output = $template->output();
468 $main::lxdebug->leave_sub();
473 sub show_generic_error {
474 my ($self, $myconfig, $error, $title) = @_;
477 $add_params->{"title"} = $title if ($title);
478 $self->{"label_error"} = $error;
480 print($self->parse_html_template($myconfig, "generic/error", $add_params));
483 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
484 # changed it to accept an arbitrary number of triggers - sschoeling
486 $main::lxdebug->enter_sub();
489 my $myconfig = shift;
492 # set dateform for jsscript
494 $ifFormat = "%d.%m.%Y";
495 if ($myconfig->{dateformat} eq "dd.mm.yy") {
496 $ifFormat = "%d.%m.%Y";
498 if ($myconfig->{dateformat} eq "dd-mm-yy") {
499 $ifFormat = "%d-%m-%Y";
501 if ($myconfig->{dateformat} eq "dd/mm/yy") {
502 $ifFormat = "%d/%m/%Y";
504 if ($myconfig->{dateformat} eq "mm/dd/yy") {
505 $ifFormat = "%m/%d/%Y";
507 if ($myconfig->{dateformat} eq "mm-dd-yy") {
508 $ifFormat = "%m-%d-%Y";
510 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
511 $ifFormat = "%Y-%m-%d";
523 inputField : "| . (shift) . qq|",
524 ifFormat :"$ifFormat",
525 align : "| . (shift) . qq|",
526 button : "| . (shift) . qq|"
532 <script type="text/javascript">
533 <!--| . join("", @triggers) . qq|//-->
537 $main::lxdebug->leave_sub();
540 } #end sub write_trigger
543 $main::lxdebug->enter_sub();
545 my ($self, $msg) = @_;
547 if ($self->{callback}) {
549 ($script, $argv) = split(/\?/, $self->{callback});
550 exec("perl", "$script", $argv);
558 $main::lxdebug->leave_sub();
561 # sort of columns removed - empty sub
563 $main::lxdebug->enter_sub();
565 my ($self, @columns) = @_;
567 $main::lxdebug->leave_sub();
573 $main::lxdebug->enter_sub();
575 my ($self, $myconfig, $amount, $places, $dash) = @_;
577 #Workaround for $format_amount calls without $places
578 if (!defined $places) {
579 (my $dec) = ($amount =~ /\.(\d+)/);
580 $places = length $dec;
583 if ($places =~ /\d/) {
584 $amount = $self->round_amount($amount, $places);
587 # is the amount negative
588 my $negative = ($amount < 0);
592 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
594 my ($whole, $dec) = split /\./, "$amount";
596 $amount = join '', reverse split //, $whole;
597 $fillup = "0" x ($places - length($dec));
599 if ($myconfig->{numberformat} eq '1,000.00') {
600 $amount =~ s/\d{3,}?/$&,/g;
602 $amount = join '', reverse split //, $amount;
603 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
606 if ($myconfig->{numberformat} eq '1.000,00') {
607 $amount =~ s/\d{3,}?/$&./g;
609 $amount = join '', reverse split //, $amount;
610 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
613 if ($myconfig->{numberformat} eq '1000,00') {
615 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
619 $amount = ($negative) ? "($amount)" : "$amount";
620 } elsif ($dash =~ /DRCR/) {
621 $amount = ($negative) ? "$amount DR" : "$amount CR";
623 $amount = ($negative) ? "-$amount" : "$amount";
627 if ($dash eq "0" && $places) {
628 if ($myconfig->{numberformat} eq '1.000,00') {
629 $amount = "0" . "," . "0" x $places;
631 $amount = "0" . "." . "0" x $places;
634 $amount = ($dash ne "") ? "$dash" : "0";
638 $main::lxdebug->leave_sub();
644 $main::lxdebug->enter_sub();
646 my ($self, $myconfig, $amount) = @_;
647 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
649 if ($myconfig->{in_numberformat} == 1) {
651 # Extra input number format 1000.00 or 1000,00
652 $main::lxdebug->message(LXDebug::DEBUG2,
653 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
656 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
657 $amount = scalar reverse $amount;
659 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
660 $amount =~ s/\./DOT/;
662 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
665 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
666 $amount =~ s/DOT/\./;
668 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
669 $amount = scalar reverse $amount;
670 $main::lxdebug->message(LXDebug::DEBUG2,
671 "Parsed amount:" . $amount . "\n");
673 return ($amount * 1);
676 $main::lxdebug->message(LXDebug::DEBUG2,
677 "in_numberformat: " . $main::locale->text('equal Outputformat'));
678 $main::lxdebug->message(LXDebug::DEBUG2,
679 " = numberformat: $myconfig->{numberformat}");
680 if ( ($myconfig->{numberformat} eq '1.000,00')
681 || ($myconfig->{numberformat} eq '1000,00')) {
686 if ($myconfig->{numberformat} eq "1'000.00") {
692 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
694 $main::lxdebug->leave_sub();
696 return ($amount * 1);
700 $main::lxdebug->enter_sub();
702 my ($self, $amount, $places) = @_;
705 # Rounding like "Kaufmannsrunden"
706 # Descr. http://de.wikipedia.org/wiki/Rundung
708 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
711 $amount = $amount * (10**($places));
712 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
714 $main::lxdebug->leave_sub();
716 return $round_amount;
721 $main::lxdebug->enter_sub();
723 my ($self, $myconfig, $userspath) = @_;
726 # Some variables used for page breaks
727 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
729 my ($current_page, $current_line, $current_row) = (1, 1, 0);
735 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
736 $self->format_string(grep(/notes/, keys(%{$self})));
738 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
739 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
741 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
742 qw(email tel fax name signature));
744 open(IN, "$self->{templates}/$self->{IN}")
745 or $self->error("$self->{IN} : $!");
750 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
752 # OUT is used for the media, screen, printer, email
753 # for postscript we store a copy in a temporary file
755 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
756 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
758 $self->{OUT} = ">$self->{tmpfile}";
762 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
764 open(OUT, ">-") or $self->error("STDOUT : $!");
768 # Do we have to run LaTeX two times? This is needed if
769 # the template contains page references.
772 # first we generate a tmpfile
773 # read file and replace <%variable%>
779 $two_passes = 1 if (/\\pageref/);
782 # detect pagebreak block and its parameters
783 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
784 $chars_per_line = $1;
785 $lines_on_first_page = $2;
786 $lines_on_second_page = $3;
789 last if (/\s*<%end pagebreak%>/);
796 if (/\s*<%foreach /) {
798 # this one we need for the count
800 $var =~ s/\s*<%foreach (.+?)%>/$1/;
802 last if (/\s*<%end /);
808 # display contents of $self->{number}[] array
809 for $i (0 .. $#{ $self->{$var} }) {
812 # Try to detect whether a manual page break is necessary
813 # but only if there was a <%pagebreak ...%> block before
815 if ($chars_per_line) {
817 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
820 my $_description = $self->{"description"}[$i];
821 while ($_description =~ /\\newline/) {
823 $_description =~ s/\\newline//;
825 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
827 if ($current_page == 1) {
828 $lpp = $lines_on_first_page;
830 $lpp = $lines_on_second_page;
833 # Yes we need a manual page break -- or the user has forced one
835 (($current_line + $lines) > $lpp)
836 || ($self->{"_forced_pagebreaks"}
837 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
841 # replace the special variables <%sumcarriedforward%>
844 my $psum = $self->format_amount($myconfig, $sum, 2);
845 $pb =~ s/<%sumcarriedforward%>/$psum/g;
846 $pb =~ s/<%lastpage%>/$current_page/g;
848 # only "normal" variables are supported here
849 # (no <%if, no <%foreach, no <%include)
851 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
853 # page break block is ready to rock
858 $current_line += $lines;
861 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
865 # don't parse par, we need it for each line
867 s/<%(.+?)%>/$self->{$1}[$i]/mg;
873 # if not comes before if!
874 if (/\s*<%if not /) {
876 # check if it is not set and display
878 s/\s*<%if not (.+?)%>/$1/;
880 unless ($self->{$_}) {
882 last if (/\s*<%end /);
892 last if (/\s*<%end /);
900 # check if it is set and display
902 s/\s*<%if (.+?)%>/$1/;
906 last if (/\s*<%end /);
916 last if (/\s*<%end /);
922 # check for <%include filename%>
923 if (/\s*<%include /) {
927 $var =~ s/\s*<%include (.+?)%>/$1/;
930 $var =~ s/(\/|\.\.)//g;
932 # prevent the infinite loop!
933 next if ($self->{"$var"});
935 open(INC, "$self->{templates}/$var")
936 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
945 s/<%(.+?)%>/$self->{$1}/g;
946 s/<nobr><\/nobr>/ /g;
953 # Convert the tex file to postscript
954 if ($self->{format} =~ /(postscript|pdf)/) {
957 $self->{cwd} = cwd();
958 $self->{tmpdir} = "$self->{cwd}/$userspath";
960 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
962 $self->{tmpfile} =~ s/$userspath\///g;
964 if ($self->{format} eq 'postscript') {
966 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
968 $self->error($self->cleanup) if ($?);
971 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
973 $self->error($self->cleanup) if ($?);
976 $self->{tmpfile} =~ s/tex$/dvi/;
978 system("dvips $self->{tmpfile} -o -q > /dev/null");
979 $self->error($self->cleanup . "dvips : $!") if ($?);
980 $self->{tmpfile} =~ s/dvi$/ps/;
982 if ($self->{format} eq 'pdf') {
984 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
986 $self->error($self->cleanup) if ($?);
989 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
991 $self->error($self->cleanup) if ($?);
993 $self->{tmpfile} =~ s/tex$/pdf/;
998 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
1000 if ($self->{media} eq 'email') {
1004 my $mail = new Mailer;
1006 map { $mail->{$_} = $self->{$_} }
1007 qw(cc bcc subject message version format charset);
1008 $mail->{to} = qq|$self->{email}|;
1009 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1010 $mail->{fileid} = "$fileid.";
1012 # if we send html or plain text inline
1013 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1014 $mail->{contenttype} = "text/html";
1016 $mail->{message} =~ s/\r\n/<br>\n/g;
1017 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1018 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1020 open(IN, $self->{tmpfile})
1021 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1023 $mail->{message} .= $_;
1030 @{ $mail->{attachments} } = ($self->{tmpfile});
1032 $myconfig->{signature} =~ s/\\n/\r\n/g;
1033 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1037 my $err = $mail->send($out);
1038 $self->error($self->cleanup . "$err") if ($err);
1042 $self->{OUT} = $out;
1044 my $numbytes = (-s $self->{tmpfile});
1045 open(IN, $self->{tmpfile})
1046 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1048 $self->{copies} = 1 unless $self->{media} eq 'printer';
1050 chdir("$self->{cwd}");
1052 for my $i (1 .. $self->{copies}) {
1054 open(OUT, $self->{OUT})
1055 or $self->error($self->cleanup . "$self->{OUT} : $!");
1058 # launch application
1059 print qq|Content-Type: application/$self->{format}
1060 Content-Disposition: attachment; filename="$self->{tmpfile}"
1061 Content-Length: $numbytes
1065 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1085 chdir("$self->{cwd}");
1086 $main::lxdebug->leave_sub();
1090 $main::lxdebug->enter_sub();
1094 chdir("$self->{tmpdir}");
1097 if (-f "$self->{tmpfile}.err") {
1098 open(FH, "$self->{tmpfile}.err");
1103 if ($self->{tmpfile}) {
1106 $self->{tmpfile} =~ s/\.\w+$//g;
1107 my $tmpfile = $self->{tmpfile};
1108 unlink(<$tmpfile.*>);
1111 chdir("$self->{cwd}");
1113 $main::lxdebug->leave_sub();
1119 $main::lxdebug->enter_sub();
1121 my ($self, @fields) = @_;
1124 %unique_fields = map({ $_ => 1 } @fields);
1125 @fields = keys(%unique_fields);
1127 foreach my $field (@fields) {
1128 next unless ($self->{$field} =~ /\<pagebreak\>/);
1129 $self->{$field} =~ s/\<pagebreak\>//g;
1130 if ($field =~ /.*_(\d+)$/) {
1131 if (!$self->{"_forced_pagebreaks"}) {
1132 $self->{"_forced_pagebreaks"} = [];
1134 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1138 my $format = $self->{format};
1139 if ($self->{format} =~ /(postscript|pdf)/) {
1146 '<', '>', quotemeta('\n'), '
1150 '&', quotemeta('\n'), '
1152 '"', '\$', '%', '_', '#', quotemeta('^'),
1153 '{', '}', '<', '>', '£', "\r"
1159 quotemeta('\n') => '<br>',
1170 quotemeta('^') => '\^\\',
1175 quotemeta('\n') => '\newline ',
1182 foreach my $key (@{ $replace{order}{$format} }) {
1183 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1186 # Allow some HTML markup to be converted into the output format's
1187 # corresponding markup code, e.g. bold or italic.
1188 if ('html' eq $format) {
1189 my @markup_replace = ('b', 'i', 's', 'u');
1191 foreach my $key (@markup_replace) {
1192 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1195 } elsif ('tex' eq $format) {
1196 my %markup_replace = ('b' => 'textbf',
1198 'u' => 'underline');
1200 foreach my $field (@fields) {
1201 foreach my $key (keys(%markup_replace)) {
1202 my $new = $markup_replace{$key};
1204 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1209 $main::lxdebug->leave_sub();
1213 $main::lxdebug->enter_sub();
1215 my ($self, $date, $myconfig) = @_;
1217 if ($date && $date =~ /\D/) {
1219 if ($myconfig->{dateformat} =~ /^yy/) {
1220 ($yy, $mm, $dd) = split /\D/, $date;
1222 if ($myconfig->{dateformat} =~ /^mm/) {
1223 ($mm, $dd, $yy) = split /\D/, $date;
1225 if ($myconfig->{dateformat} =~ /^dd/) {
1226 ($dd, $mm, $yy) = split /\D/, $date;
1231 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1232 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1234 $dd = "0$dd" if ($dd < 10);
1235 $mm = "0$mm" if ($mm < 10);
1237 $date = "$yy$mm$dd";
1240 $main::lxdebug->leave_sub();
1245 # Database routines used throughout
1248 $main::lxdebug->enter_sub();
1250 my ($self, $myconfig) = @_;
1252 # connect to database
1254 DBI->connect($myconfig->{dbconnect},
1255 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1259 if ($myconfig->{dboptions}) {
1260 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1263 $main::lxdebug->leave_sub();
1268 sub dbconnect_noauto {
1269 $main::lxdebug->enter_sub();
1271 my ($self, $myconfig) = @_;
1273 # connect to database
1275 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1276 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1280 if ($myconfig->{dboptions}) {
1281 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1284 $main::lxdebug->leave_sub();
1289 sub update_balance {
1290 $main::lxdebug->enter_sub();
1292 my ($self, $dbh, $table, $field, $where, $value) = @_;
1294 # if we have a value, go do it
1297 # retrieve balance from table
1298 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1299 my $sth = $dbh->prepare($query);
1301 $sth->execute || $self->dberror($query);
1302 my ($balance) = $sth->fetchrow_array;
1308 $query = "UPDATE $table SET $field = $balance WHERE $where";
1309 $dbh->do($query) || $self->dberror($query);
1311 $main::lxdebug->leave_sub();
1314 sub update_exchangerate {
1315 $main::lxdebug->enter_sub();
1317 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1319 # some sanity check for currency
1321 $main::lxdebug->leave_sub();
1325 my $query = qq|SELECT e.curr FROM exchangerate e
1326 WHERE e.curr = '$curr'
1327 AND e.transdate = '$transdate'
1329 my $sth = $dbh->prepare($query);
1330 $sth->execute || $self->dberror($query);
1333 if ($buy != 0 && $sell != 0) {
1334 $set = "buy = $buy, sell = $sell";
1335 } elsif ($buy != 0) {
1336 $set = "buy = $buy";
1337 } elsif ($sell != 0) {
1338 $set = "sell = $sell";
1341 if ($sth->fetchrow_array) {
1342 $query = qq|UPDATE exchangerate
1344 WHERE curr = '$curr'
1345 AND transdate = '$transdate'|;
1347 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1348 VALUES ('$curr', $buy, $sell, '$transdate')|;
1351 $dbh->do($query) || $self->dberror($query);
1353 $main::lxdebug->leave_sub();
1356 sub save_exchangerate {
1357 $main::lxdebug->enter_sub();
1359 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1361 my $dbh = $self->dbconnect($myconfig);
1363 my ($buy, $sell) = (0, 0);
1364 $buy = $rate if $fld eq 'buy';
1365 $sell = $rate if $fld eq 'sell';
1367 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1371 $main::lxdebug->leave_sub();
1374 sub get_exchangerate {
1375 $main::lxdebug->enter_sub();
1377 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1379 unless ($transdate) {
1380 $main::lxdebug->leave_sub();
1384 my $query = qq|SELECT e.$fld FROM exchangerate e
1385 WHERE e.curr = '$curr'
1386 AND e.transdate = '$transdate'|;
1387 my $sth = $dbh->prepare($query);
1388 $sth->execute || $self->dberror($query);
1390 my ($exchangerate) = $sth->fetchrow_array;
1393 $main::lxdebug->leave_sub();
1395 return $exchangerate;
1398 sub check_exchangerate {
1399 $main::lxdebug->enter_sub();
1401 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1403 unless ($transdate) {
1404 $main::lxdebug->leave_sub();
1408 my $dbh = $self->dbconnect($myconfig);
1410 my $query = qq|SELECT e.$fld FROM exchangerate e
1411 WHERE e.curr = '$currency'
1412 AND e.transdate = '$transdate'|;
1413 my $sth = $dbh->prepare($query);
1414 $sth->execute || $self->dberror($query);
1416 my ($exchangerate) = $sth->fetchrow_array;
1420 $main::lxdebug->leave_sub();
1422 return $exchangerate;
1426 $main::lxdebug->enter_sub();
1428 my ($self, $dbh, $id) = @_;
1432 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1434 if ($self->{"shipto$item"}) {
1435 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1437 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1442 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1443 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1444 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1445 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1446 '$self->{shiptozipcode}', '$self->{shiptocity}',
1447 '$self->{shiptocountry}', '$self->{shiptocontact}',
1448 '$self->{shiptophone}', '$self->{shiptofax}',
1449 '$self->{shiptoemail}')|;
1450 $dbh->do($query) || $self->dberror($query);
1453 $main::lxdebug->leave_sub();
1457 $main::lxdebug->enter_sub();
1459 my ($self, $dbh) = @_;
1461 my $query = qq|SELECT e.id, e.name FROM employee e
1462 WHERE e.login = '$self->{login}'|;
1463 my $sth = $dbh->prepare($query);
1464 $sth->execute || $self->dberror($query);
1466 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1467 $self->{employee_id} *= 1;
1471 $main::lxdebug->leave_sub();
1474 # get other contact for transaction and form - html/tex
1476 $main::lxdebug->enter_sub();
1478 my ($self, $dbh, $id) = @_;
1480 my $query = qq|SELECT c.*
1483 $sth = $dbh->prepare($query);
1484 $sth->execute || $self->dberror($query);
1486 $ref = $sth->fetchrow_hashref(NAME_lc);
1488 push @{ $self->{$_} }, $ref;
1491 $main::lxdebug->leave_sub();
1494 # get contacts for id, if no contact return {"","","","",""}
1496 $main::lxdebug->enter_sub();
1498 my ($self, $dbh, $id) = @_;
1500 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1502 WHERE cp_cv_id=$id|;
1503 my $sth = $dbh->prepare($query);
1504 $sth->execute || $self->dberror($query);
1507 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1508 push @{ $self->{all_contacts} }, $ref;
1513 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1516 $main::lxdebug->leave_sub();
1519 # this sub gets the id and name from $table
1521 $main::lxdebug->enter_sub();
1523 my ($self, $myconfig, $table) = @_;
1525 # connect to database
1526 my $dbh = $self->dbconnect($myconfig);
1528 my $name = $self->like(lc $self->{$table});
1529 my $customernumber = $self->like(lc $self->{customernumber});
1531 if ($self->{customernumber} ne "") {
1532 $query = qq~SELECT c.id, c.name,
1533 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1535 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1538 $query = qq~SELECT c.id, c.name,
1539 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1541 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1545 if ($self->{openinvoices}) {
1546 $query = qq~SELECT DISTINCT c.id, c.name,
1547 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1548 FROM $self->{arap} a
1549 JOIN $table c ON (a.${table}_id = c.id)
1550 WHERE NOT a.amount = a.paid
1551 AND lower(c.name) LIKE '$name'
1554 my $sth = $dbh->prepare($query);
1556 $sth->execute || $self->dberror($query);
1559 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1560 push(@{ $self->{name_list} }, $ref);
1566 $main::lxdebug->leave_sub();
1571 # the selection sub is used in the AR, AP, IS, IR and OE module
1574 $main::lxdebug->enter_sub();
1576 my ($self, $myconfig, $table, $module) = @_;
1579 my $dbh = $self->dbconnect($myconfig);
1581 my $query = qq|SELECT count(*) FROM $table|;
1582 my $sth = $dbh->prepare($query);
1583 $sth->execute || $self->dberror($query);
1584 my ($count) = $sth->fetchrow_array;
1587 # build selection list
1588 if ($count < $myconfig->{vclimit}) {
1589 $query = qq|SELECT id, name
1590 FROM $table WHERE not obsolete
1592 $sth = $dbh->prepare($query);
1593 $sth->execute || $self->dberror($query);
1595 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1596 push @{ $self->{"all_$table"} }, $ref;
1604 $self->get_employee($dbh);
1606 # setup sales contacts
1607 $query = qq|SELECT e.id, e.name
1610 AND NOT e.id = $self->{employee_id}|;
1611 $sth = $dbh->prepare($query);
1612 $sth->execute || $self->dberror($query);
1614 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1615 push @{ $self->{all_employees} }, $ref;
1620 push @{ $self->{all_employees} },
1621 { id => $self->{employee_id},
1622 name => $self->{employee} };
1624 # sort the whole thing
1625 @{ $self->{all_employees} } =
1626 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1628 if ($module eq 'AR') {
1630 # prepare query for departments
1631 $query = qq|SELECT d.id, d.description
1637 $query = qq|SELECT d.id, d.description
1642 $sth = $dbh->prepare($query);
1643 $sth->execute || $self->dberror($query);
1645 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1646 push @{ $self->{all_departments} }, $ref;
1651 $main::lxdebug->leave_sub();
1654 # this is only used for reports
1655 sub all_departments {
1656 $main::lxdebug->enter_sub();
1658 my ($self, $myconfig, $table) = @_;
1660 my $dbh = $self->dbconnect($myconfig);
1661 my $where = "1 = 1";
1663 if (defined $table) {
1664 if ($table eq 'customer') {
1665 $where = " d.role = 'P'";
1669 my $query = qq|SELECT d.id, d.description
1673 my $sth = $dbh->prepare($query);
1674 $sth->execute || $self->dberror($query);
1676 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1677 push @{ $self->{all_departments} }, $ref;
1683 $main::lxdebug->leave_sub();
1687 $main::lxdebug->enter_sub();
1689 my ($self, $module, $myconfig, $table) = @_;
1691 $self->all_vc($myconfig, $table, $module);
1693 # get last customers or vendors
1696 my $dbh = $self->dbconnect($myconfig);
1700 # now get the account numbers
1701 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1703 WHERE c.link LIKE '%$module%'
1706 $sth = $dbh->prepare($query);
1707 $sth->execute || $self->dberror($query);
1709 $self->{accounts} = "";
1710 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1712 foreach my $key (split /:/, $ref->{link}) {
1713 if ($key =~ /$module/) {
1715 # cross reference for keys
1716 $xkeyref{ $ref->{accno} } = $key;
1718 push @{ $self->{"${module}_links"}{$key} },
1719 { accno => $ref->{accno},
1720 description => $ref->{description},
1721 taxkey => $ref->{taxkey_id} };
1723 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1729 if (($module eq "AP") || ($module eq "AR")) {
1731 # get tax rates and description
1732 $query = qq| SELECT * FROM tax t|;
1733 $sth = $dbh->prepare($query);
1734 $sth->execute || $self->dberror($query);
1736 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1737 push @{ $self->{TAX} }, $ref;
1743 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1745 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1746 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1747 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1748 c.name AS $table, a.department_id, d.description AS department,
1749 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1750 a.employee_id, e.name AS employee, a.gldate
1752 JOIN $table c ON (a.${table}_id = c.id)
1753 LEFT JOIN employee e ON (e.id = a.employee_id)
1754 LEFT JOIN department d ON (d.id = a.department_id)
1755 WHERE a.id = $self->{id}|;
1756 $sth = $dbh->prepare($query);
1757 $sth->execute || $self->dberror($query);
1759 $ref = $sth->fetchrow_hashref(NAME_lc);
1760 foreach $key (keys %$ref) {
1761 $self->{$key} = $ref->{$key};
1765 # get amounts from individual entries
1766 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1767 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1769 JOIN chart c ON (c.id = a.chart_id)
1770 LEFT JOIN project p ON (p.id = a.project_id)
1771 LEFT Join tax t ON (a.taxkey = t.taxkey)
1772 WHERE a.trans_id = $self->{id}
1773 AND a.fx_transaction = '0'
1774 ORDER BY a.oid,a.transdate|;
1775 $sth = $dbh->prepare($query);
1776 $sth->execute || $self->dberror($query);
1778 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1780 # get exchangerate for currency
1781 $self->{exchangerate} =
1782 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1786 # store amounts in {acc_trans}{$key} for multiple accounts
1787 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1788 $ref->{exchangerate} =
1789 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1791 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1794 $ref->{index} = $index;
1796 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1800 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1801 (SELECT c.accno FROM chart c
1802 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1803 (SELECT c.accno FROM chart c
1804 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1806 $sth = $dbh->prepare($query);
1807 $sth->execute || $self->dberror($query);
1809 $ref = $sth->fetchrow_hashref(NAME_lc);
1810 map { $self->{$_} = $ref->{$_} } keys %$ref;
1816 $query = qq|SELECT current_date AS transdate,
1817 d.curr AS currencies, d.closedto, d.revtrans,
1818 (SELECT c.accno FROM chart c
1819 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1820 (SELECT c.accno FROM chart c
1821 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1823 $sth = $dbh->prepare($query);
1824 $sth->execute || $self->dberror($query);
1826 $ref = $sth->fetchrow_hashref(NAME_lc);
1827 map { $self->{$_} = $ref->{$_} } keys %$ref;
1830 if ($self->{"$self->{vc}_id"}) {
1832 # only setup currency
1833 ($self->{currency}) = split /:/, $self->{currencies};
1837 $self->lastname_used($dbh, $myconfig, $table, $module);
1839 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1841 # get exchangerate for currency
1842 $self->{exchangerate} =
1843 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1852 $main::lxdebug->leave_sub();
1856 $main::lxdebug->enter_sub();
1858 my ($self, $dbh, $myconfig, $table, $module) = @_;
1860 my $arap = ($table eq 'customer') ? "ar" : "ap";
1861 my $where = "1 = 1";
1863 if ($self->{type} =~ /_order/) {
1865 $where = "quotation = '0'";
1867 if ($self->{type} =~ /_quotation/) {
1869 $where = "quotation = '1'";
1872 my $query = qq|SELECT MAX(id) FROM $arap
1874 AND ${table}_id > 0|;
1875 my $sth = $dbh->prepare($query);
1876 $sth->execute || $self->dberror($query);
1878 my ($trans_id) = $sth->fetchrow_array;
1882 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1883 current_date + ct.terms AS duedate, a.department_id,
1884 d.description AS department
1886 JOIN $table ct ON (a.${table}_id = ct.id)
1887 LEFT JOIN department d ON (a.department_id = d.id)
1888 WHERE a.id = $trans_id|;
1889 $sth = $dbh->prepare($query);
1890 $sth->execute || $self->dberror($query);
1892 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1893 $self->{duedate}, $self->{department_id}, $self->{department})
1894 = $sth->fetchrow_array;
1897 $main::lxdebug->leave_sub();
1901 $main::lxdebug->enter_sub();
1903 my ($self, $myconfig, $thisdate, $days) = @_;
1905 my $dbh = $self->dbconnect($myconfig);
1910 my $dateformat = $myconfig->{dateformat};
1911 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1913 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1915 $sth = $dbh->prepare($query);
1916 $sth->execute || $self->dberror($query);
1918 $query = qq|SELECT current_date AS thisdate
1920 $sth = $dbh->prepare($query);
1921 $sth->execute || $self->dberror($query);
1924 ($thisdate) = $sth->fetchrow_array;
1929 $main::lxdebug->leave_sub();
1935 $main::lxdebug->enter_sub();
1937 my ($self, $string) = @_;
1939 if ($string !~ /%/) {
1940 $string = "%$string%";
1943 $string =~ s/\'/\'\'/g;
1945 $main::lxdebug->leave_sub();
1951 $main::lxdebug->enter_sub();
1953 my ($self, $flds, $new, $count, $numrows) = @_;
1957 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1963 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1965 $j = $item->{ndx} - 1;
1966 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1970 for $i ($count + 1 .. $numrows) {
1971 map { delete $self->{"${_}_$i"} } @{$flds};
1974 $main::lxdebug->leave_sub();
1978 $main::lxdebug->enter_sub();
1980 my ($self, $myconfig) = @_;
1984 my $dbh = $self->dbconnect_noauto($myconfig);
1986 my $query = qq|DELETE FROM status
1987 WHERE formname = '$self->{formname}'
1989 my $sth = $dbh->prepare($query) || $self->dberror($query);
1991 if ($self->{formname} =~ /(check|receipt)/) {
1992 for $i (1 .. $self->{rowcount}) {
1993 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1997 $sth->execute($self->{id}) || $self->dberror($query);
2001 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2002 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2004 my %queued = split / /, $self->{queued};
2006 if ($self->{formname} =~ /(check|receipt)/) {
2008 # this is a check or receipt, add one entry for each lineitem
2009 my ($accno) = split /--/, $self->{account};
2010 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2011 chart_id) VALUES (?, '$printed',
2012 '$queued{$self->{formname}}', '$self->{prinform}',
2013 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2014 $sth = $dbh->prepare($query) || $self->dberror($query);
2016 for $i (1 .. $self->{rowcount}) {
2017 if ($self->{"checked_$i"}) {
2018 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2023 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2024 spoolfile, formname)
2025 VALUES ($self->{id}, '$printed', '$emailed',
2026 '$queued{$self->{formname}}', '$self->{formname}')|;
2027 $dbh->do($query) || $self->dberror($query);
2033 $main::lxdebug->leave_sub();
2037 $main::lxdebug->enter_sub();
2039 my ($self, $dbh) = @_;
2041 my ($query, $printed, $emailed);
2043 my $formnames = $self->{printed};
2044 my $emailforms = $self->{emailed};
2046 my $query = qq|DELETE FROM status
2047 WHERE formname = '$self->{formname}'
2048 AND trans_id = $self->{id}|;
2049 $dbh->do($query) || $self->dberror($query);
2051 # this only applies to the forms
2052 # checks and receipts are posted when printed or queued
2054 if ($self->{queued}) {
2055 my %queued = split / /, $self->{queued};
2057 foreach my $formname (keys %queued) {
2058 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2059 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2061 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2062 spoolfile, formname)
2063 VALUES ($self->{id}, '$printed', '$emailed',
2064 '$queued{$formname}', '$formname')|;
2065 $dbh->do($query) || $self->dberror($query);
2067 $formnames =~ s/$self->{formname}//;
2068 $emailforms =~ s/$self->{formname}//;
2073 # save printed, emailed info
2074 $formnames =~ s/^ +//g;
2075 $emailforms =~ s/^ +//g;
2078 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2079 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2081 foreach my $formname (keys %status) {
2082 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2083 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2085 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2086 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2087 $dbh->do($query) || $self->dberror($query);
2090 $main::lxdebug->leave_sub();
2093 sub update_defaults {
2094 $main::lxdebug->enter_sub();
2096 my ($self, $myconfig, $fld) = @_;
2098 my $dbh = $self->dbconnect_noauto($myconfig);
2099 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2100 my $sth = $dbh->prepare($query);
2102 $sth->execute || $self->dberror($query);
2103 my ($var) = $sth->fetchrow_array;
2108 $query = qq|UPDATE defaults
2110 $dbh->do($query) || $form->dberror($query);
2115 $main::lxdebug->leave_sub();
2120 sub update_business {
2121 $main::lxdebug->enter_sub();
2123 my ($self, $myconfig, $business_id) = @_;
2125 my $dbh = $self->dbconnect_noauto($myconfig);
2127 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2128 my $sth = $dbh->prepare($query);
2130 $sth->execute || $self->dberror($query);
2131 my ($var) = $sth->fetchrow_array;
2136 $query = qq|UPDATE business
2137 SET customernumberinit = '$var' WHERE id=$business_id|;
2138 $dbh->do($query) || $form->dberror($query);
2143 $main::lxdebug->leave_sub();
2149 $main::lxdebug->enter_sub();
2151 my ($self, $myconfig, $salesman) = @_;
2153 my $dbh = $self->dbconnect($myconfig);
2155 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2156 my $sth = $dbh->prepare($query);
2157 $sth->execute || $self->dberror($query);
2160 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2161 push(@{ $self->{salesman_list} }, $ref);
2165 $main::lxdebug->leave_sub();
2170 sub get_partsgroup {
2171 $main::lxdebug->enter_sub();
2173 my ($self, $myconfig, $p) = @_;
2175 my $dbh = $self->dbconnect($myconfig);
2177 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2179 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2181 if ($p->{searchitems} eq 'part') {
2183 WHERE p.inventory_accno_id > 0|;
2185 if ($p->{searchitems} eq 'service') {
2187 WHERE p.inventory_accno_id IS NULL|;
2189 if ($p->{searchitems} eq 'assembly') {
2191 WHERE p.assembly = '1'|;
2193 if ($p->{searchitems} eq 'labor') {
2195 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2199 ORDER BY partsgroup|;
2202 $query = qq|SELECT id, partsgroup FROM partsgroup
2203 ORDER BY partsgroup|;
2206 if ($p->{language_code}) {
2207 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2208 t.description AS translation
2210 JOIN parts p ON (p.partsgroup_id = pg.id)
2211 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2212 ORDER BY translation|;
2215 my $sth = $dbh->prepare($query);
2216 $sth->execute || $self->dberror($query);
2218 $self->{all_partsgroup} = ();
2219 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2220 push @{ $self->{all_partsgroup} }, $ref;
2224 $main::lxdebug->leave_sub();
2227 sub get_pricegroup {
2228 $main::lxdebug->enter_sub();
2230 my ($self, $myconfig, $p) = @_;
2232 my $dbh = $self->dbconnect($myconfig);
2234 my $query = qq|SELECT p.id, p.pricegroup
2238 ORDER BY pricegroup|;
2241 $query = qq|SELECT id, pricegroup FROM pricegroup
2242 ORDER BY pricegroup|;
2245 my $sth = $dbh->prepare($query);
2246 $sth->execute || $self->dberror($query);
2248 $self->{all_pricegroup} = ();
2249 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2250 push @{ $self->{all_pricegroup} }, $ref;
2255 $main::lxdebug->leave_sub();
2259 my ($self, $dbh, $myconfig, $audittrail) = @_;
2261 # table, $reference, $formname, $action, $id, $transdate) = @_;
2268 $dbh = $self->dbconnect($myconfig);
2272 # if we have an id add audittrail, otherwise get a new timestamp
2274 if ($audittrail->{id}) {
2276 $query = qq|SELECT audittrail FROM defaults|;
2278 if ($dbh->selectrow_array($query)) {
2279 my ($null, $employee_id) = $self->get_employee($dbh);
2281 if ($self->{audittrail} && !$myconfig) {
2282 chop $self->{audittrail};
2284 my @a = split /\|/, $self->{audittrail};
2288 my @flds = qw(tablename reference formname action transdate);
2290 # put into hash and remove dups
2292 $key = "$a[2]$a[3]";
2294 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2298 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2299 formname, action, employee_id, transdate)
2300 VALUES ($audittrail->{id}, ?, ?,
2301 ?, ?, $employee_id, ?)|;
2302 my $sth = $dbh->prepare($query) || $self->dberror($query);
2306 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2310 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2312 $sth->execute || $self->dberror;
2317 if ($audittrail->{transdate}) {
2318 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2319 formname, action, employee_id, transdate) VALUES (
2320 $audittrail->{id}, '$audittrail->{tablename}', |
2321 . $dbh->quote($audittrail->{reference}) . qq|,
2322 '$audittrail->{formname}', '$audittrail->{action}',
2323 $employee_id, '$audittrail->{transdate}')|;
2325 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2326 formname, action, employee_id) VALUES ($audittrail->{id},
2327 '$audittrail->{tablename}', |
2328 . $dbh->quote($audittrail->{reference}) . qq|,
2329 '$audittrail->{formname}', '$audittrail->{action}',
2336 $query = qq|SELECT current_timestamp FROM defaults|;
2337 my ($timestamp) = $dbh->selectrow_array($query);
2340 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2343 $dbh->disconnect if $disconnect;
2352 $main::lxdebug->enter_sub();
2354 my ($type, $country, $NLS_file) = @_;
2358 if ($country && -d "locale/$country") {
2359 $self->{countrycode} = $country;
2360 eval { require "locale/$country/$NLS_file"; };
2363 $self->{NLS_file} = $NLS_file;
2365 push @{ $self->{LONG_MONTH} },
2366 ("January", "February", "March", "April",
2367 "May ", "June", "July", "August",
2368 "September", "October", "November", "December");
2369 push @{ $self->{SHORT_MONTH} },
2370 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2372 $main::lxdebug->leave_sub();
2378 my ($self, $text) = @_;
2380 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2384 $main::lxdebug->enter_sub();
2386 my ($self, $text) = @_;
2388 if (exists $self{subs}{$text}) {
2389 $text = $self{subs}{$text};
2391 if ($self->{countrycode} && $self->{NLS_file}) {
2393 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2397 $main::lxdebug->leave_sub();
2403 $main::lxdebug->enter_sub();
2405 my ($self, $myconfig, $date, $longformat) = @_;
2408 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2413 $spc = $myconfig->{dateformat};
2415 $spc = substr($spc, 1, 1);
2417 if ($date =~ /\D/) {
2418 if ($myconfig->{dateformat} =~ /^yy/) {
2419 ($yy, $mm, $dd) = split /\D/, $date;
2421 if ($myconfig->{dateformat} =~ /^mm/) {
2422 ($mm, $dd, $yy) = split /\D/, $date;
2424 if ($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 if ($myconfig->{dateformat} =~ /^dd/) {
2438 if (defined $longformat && $longformat == 0) {
2440 $dd = "0$dd" if ($dd < 10);
2441 $mm = "0$mm" if ($mm < 10);
2442 $longdate = "$dd$spc$mm$spc$yy";
2445 $longdate .= ($spc eq '.') ? ". " : " ";
2446 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2448 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2450 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2451 # Lx-Office is mainly used in Germany or German speaking countries.
2452 if (defined $longformat && $longformat == 0) {
2454 $dd = "0$dd" if ($dd < 10);
2455 $mm = "0$mm" if ($mm < 10);
2456 $longdate = "$yy-$mm-$dd";
2458 $longdate = "$dd. ";
2459 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2462 if (defined $longformat && $longformat == 0) {
2464 $dd = "0$dd" if ($dd < 10);
2465 $mm = "0$mm" if ($mm < 10);
2466 $longdate = "$mm$spc$dd$spc$yy";
2468 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2474 $main::lxdebug->leave_sub();
2480 $main::lxdebug->enter_sub();
2482 my ($self, $myconfig, $date, $longformat) = @_;
2485 $main::lxdebug->leave_sub();
2490 $spc = $myconfig->{dateformat};
2492 $spc = substr($spc, 1, 1);
2494 if ($date =~ /\D/) {
2495 if ($myconfig->{dateformat} =~ /^yy/) {
2496 ($yy, $mm, $dd) = split /\D/, $date;
2497 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2498 ($mm, $dd, $yy) = split /\D/, $date;
2499 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2500 ($dd, $mm, $yy) = split /\D/, $date;
2503 $date = substr($date, 2);
2504 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2509 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2510 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2512 $main::lxdebug->leave_sub();
2513 return ($yy, $mm, $dd);