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";
147 $main::lxdebug->leave_sub();
153 $main::lxdebug->enter_sub();
159 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
161 $main::lxdebug->leave_sub();
165 $main::lxdebug->enter_sub();
167 my ($self, $str, $beenthere) = @_;
169 # for Apache 2 we escape strings twice
170 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
171 # $str = $self->escape($str, 1);
174 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
176 $main::lxdebug->leave_sub();
182 $main::lxdebug->enter_sub();
184 my ($self, $str) = @_;
189 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
191 $main::lxdebug->leave_sub();
197 my ($self, $str) = @_;
199 if ($str && !ref($str)) {
200 $str =~ s/"/"/g;
208 my ($self, $str) = @_;
210 if ($str && !ref($str)) {
211 $str =~ s/"/"/g;
223 print qq|<input type=hidden name="$_" value="|
224 . $self->quote($self->{$_})
228 delete $self->{header};
229 for (sort keys %$self) {
230 print qq|<input type=hidden name="$_" value="|
231 . $self->quote($self->{$_})
239 $main::lxdebug->enter_sub();
241 my ($self, $msg) = @_;
243 if ($ENV{HTTP_USER_AGENT}) {
247 $self->show_generic_error($msg);
253 if ($self->{error_function}) {
254 &{ $self->{error_function} }($msg);
260 $main::lxdebug->leave_sub();
264 $main::lxdebug->enter_sub();
266 my ($self, $msg) = @_;
268 if ($ENV{HTTP_USER_AGENT}) {
271 if (!$self->{header}) {
284 if ($self->{info_function}) {
285 &{ $self->{info_function} }($msg);
291 $main::lxdebug->leave_sub();
295 $main::lxdebug->enter_sub();
297 my ($self, $str, $cols, $maxrows) = @_;
301 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
303 $maxrows = $rows unless defined $maxrows;
305 $main::lxdebug->leave_sub();
307 return ($rows > $maxrows) ? $maxrows : $rows;
311 $main::lxdebug->enter_sub();
313 my ($self, $msg) = @_;
315 $self->error("$msg\n" . $DBI::errstr);
317 $main::lxdebug->leave_sub();
321 $main::lxdebug->enter_sub();
323 my ($self, $name, $msg) = @_;
325 if ($self->{$name} =~ /^\s*$/) {
328 $main::lxdebug->leave_sub();
332 $main::lxdebug->enter_sub();
336 if ($self->{header}) {
337 $main::lxdebug->leave_sub();
341 my ($stylesheet, $favicon, $charset);
343 if ($ENV{HTTP_USER_AGENT}) {
345 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
347 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
351 if ($self->{favicon} && (-f "$self->{favicon}")) {
353 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
357 if ($self->{charset}) {
359 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
362 if ($self->{landscape}) {
363 $pagelayout = qq|<style type="text/css">
364 \@page { size:landscape; }
367 if ($self->{fokus}) {
368 $fokus = qq|<script type="text/javascript">
370 function fokus(){document.$self->{fokus}.focus();}
377 if ($self->{jsscript} == 1) {
380 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
381 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
382 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
383 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
390 ? "$self->{title} - $self->{titlebar}"
393 print qq|Content-Type: text/html
397 <title>$self->{titlebar}</title>
410 $main::lxdebug->leave_sub();
414 sub parse_html_template {
415 $main::lxdebug->enter_sub();
417 my ($self, $file, $additional_params) = @_;
419 if (-f "templates/webpages/${file}_" . $main::myconfig{"countrycode"} .
421 if ((-f ".developer") &&
422 (-f "templates/webpages/${file}_master.html") &&
423 ((stat("templates/webpages/${file}_master.html"))[9] >
424 (stat("templates/webpages/${file}_" . $main::myconfig{"countrycode"} .
426 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
427 "Please re-run 'locales.pl' in 'locale/" . $main::myconfig{"countrycode"} . "'.";
428 print(qq|<pre>$info</pre>|);
432 $file = "templates/webpages/${file}_" . $main::myconfig{"countrycode"} .
434 } elsif (-f "templates/webpages/${file}.html") {
435 $file = "templates/webpages/${file}.html";
437 my $info = "Web page template '${file}' not found.\n" .
438 "Please re-run 'locales.pl' in 'locale/" . $main::myconfig{"countrycode"} . "'.";
439 print(qq|<pre>$info</pre>|);
443 my $template = HTML::Template->new("filename" => $file,
444 "die_on_bad_params" => 0,
446 "case_sensitive" => 1,
447 "loop_context_vars" => 1,
450 $additional_params = {} unless ($additional_params);
451 if ($self->{"DEBUG"}) {
452 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
455 if ($additional_params->{"DEBUG"}) {
456 $additional_params->{"DEBUG"} =
457 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
460 my @additional_param_names = keys(%{$additional_params});
462 foreach my $key ($template->param()) {
463 if (grep(/^${key}$/, @additional_param_names)) {
464 $template->param($key => $additional_params->{$key});
466 $template->param($key => $self->{$key});
470 my $output = $template->output();
472 $main::lxdebug->leave_sub();
477 sub show_generic_error {
478 my ($self, $error, $title) = @_;
481 $add_params->{"title"} = $title if ($title);
482 $self->{"label_error"} = $error;
484 print($self->parse_html_template("generic/error", $add_params));
487 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
488 # changed it to accept an arbitrary number of triggers - sschoeling
490 $main::lxdebug->enter_sub();
493 my $myconfig = shift;
496 # set dateform for jsscript
498 $ifFormat = "%d.%m.%Y";
499 if ($myconfig->{dateformat} eq "dd.mm.yy") {
500 $ifFormat = "%d.%m.%Y";
502 if ($myconfig->{dateformat} eq "dd-mm-yy") {
503 $ifFormat = "%d-%m-%Y";
505 if ($myconfig->{dateformat} eq "dd/mm/yy") {
506 $ifFormat = "%d/%m/%Y";
508 if ($myconfig->{dateformat} eq "mm/dd/yy") {
509 $ifFormat = "%m/%d/%Y";
511 if ($myconfig->{dateformat} eq "mm-dd-yy") {
512 $ifFormat = "%m-%d-%Y";
514 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
515 $ifFormat = "%Y-%m-%d";
527 inputField : "| . (shift) . qq|",
528 ifFormat :"$ifFormat",
529 align : "| . (shift) . qq|",
530 button : "| . (shift) . qq|"
536 <script type="text/javascript">
537 <!--| . join("", @triggers) . qq|//-->
541 $main::lxdebug->leave_sub();
544 } #end sub write_trigger
547 $main::lxdebug->enter_sub();
549 my ($self, $msg) = @_;
551 if ($self->{callback}) {
553 ($script, $argv) = split(/\?/, $self->{callback});
554 exec("perl", "$script", $argv);
562 $main::lxdebug->leave_sub();
565 # sort of columns removed - empty sub
567 $main::lxdebug->enter_sub();
569 my ($self, @columns) = @_;
571 $main::lxdebug->leave_sub();
577 $main::lxdebug->enter_sub();
579 my ($self, $myconfig, $amount, $places, $dash) = @_;
581 #Workaround for $format_amount calls without $places
582 if (!defined $places) {
583 (my $dec) = ($amount =~ /\.(\d+)/);
584 $places = length $dec;
587 if ($places =~ /\d/) {
588 $amount = $self->round_amount($amount, $places);
591 # is the amount negative
592 my $negative = ($amount < 0);
596 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
598 my ($whole, $dec) = split /\./, "$amount";
600 $amount = join '', reverse split //, $whole;
601 $fillup = "0" x ($places - length($dec));
603 if ($myconfig->{numberformat} eq '1,000.00') {
604 $amount =~ s/\d{3,}?/$&,/g;
606 $amount = join '', reverse split //, $amount;
607 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
610 if ($myconfig->{numberformat} eq '1.000,00') {
611 $amount =~ s/\d{3,}?/$&./g;
613 $amount = join '', reverse split //, $amount;
614 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
617 if ($myconfig->{numberformat} eq '1000,00') {
619 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
623 $amount = ($negative) ? "($amount)" : "$amount";
624 } elsif ($dash =~ /DRCR/) {
625 $amount = ($negative) ? "$amount DR" : "$amount CR";
627 $amount = ($negative) ? "-$amount" : "$amount";
631 if ($dash eq "0" && $places) {
632 if ($myconfig->{numberformat} eq '1.000,00') {
633 $amount = "0" . "," . "0" x $places;
635 $amount = "0" . "." . "0" x $places;
638 $amount = ($dash ne "") ? "$dash" : "0";
642 $main::lxdebug->leave_sub();
648 $main::lxdebug->enter_sub();
650 my ($self, $myconfig, $amount) = @_;
651 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
653 if ($myconfig->{in_numberformat} == 1) {
655 # Extra input number format 1000.00 or 1000,00
656 $main::lxdebug->message(LXDebug::DEBUG2,
657 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
660 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
661 $amount = scalar reverse $amount;
663 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
664 $amount =~ s/\./DOT/;
666 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
669 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
670 $amount =~ s/DOT/\./;
672 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
673 $amount = scalar reverse $amount;
674 $main::lxdebug->message(LXDebug::DEBUG2,
675 "Parsed amount:" . $amount . "\n");
677 return ($amount * 1);
680 $main::lxdebug->message(LXDebug::DEBUG2,
681 "in_numberformat: " . $main::locale->text('equal Outputformat'));
682 $main::lxdebug->message(LXDebug::DEBUG2,
683 " = numberformat: $myconfig->{numberformat}");
684 if ( ($myconfig->{numberformat} eq '1.000,00')
685 || ($myconfig->{numberformat} eq '1000,00')) {
690 if ($myconfig->{numberformat} eq "1'000.00") {
696 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
698 $main::lxdebug->leave_sub();
700 return ($amount * 1);
704 $main::lxdebug->enter_sub();
706 my ($self, $amount, $places) = @_;
709 # Rounding like "Kaufmannsrunden"
710 # Descr. http://de.wikipedia.org/wiki/Rundung
712 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
715 $amount = $amount * (10**($places));
716 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
718 $main::lxdebug->leave_sub();
720 return $round_amount;
725 $main::lxdebug->enter_sub();
727 my ($self, $myconfig, $userspath) = @_;
730 # Some variables used for page breaks
731 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
733 my ($current_page, $current_line, $current_row) = (1, 1, 0);
739 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
740 $self->format_string(grep(/notes/, keys(%{$self})));
742 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
743 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
745 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
746 qw(email tel fax name signature));
748 open(IN, "$self->{templates}/$self->{IN}")
749 or $self->error("$self->{IN} : $!");
754 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
756 # OUT is used for the media, screen, printer, email
757 # for postscript we store a copy in a temporary file
759 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
760 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
762 $self->{OUT} = ">$self->{tmpfile}";
766 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
768 open(OUT, ">-") or $self->error("STDOUT : $!");
772 # Do we have to run LaTeX two times? This is needed if
773 # the template contains page references.
776 # first we generate a tmpfile
777 # read file and replace <%variable%>
783 $two_passes = 1 if (/\\pageref/);
786 # detect pagebreak block and its parameters
787 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
788 $chars_per_line = $1;
789 $lines_on_first_page = $2;
790 $lines_on_second_page = $3;
793 last if (/\s*<%end pagebreak%>/);
800 if (/\s*<%foreach /) {
802 # this one we need for the count
804 $var =~ s/\s*<%foreach (.+?)%>/$1/;
806 last if (/\s*<%end /);
812 # display contents of $self->{number}[] array
813 for $i (0 .. $#{ $self->{$var} }) {
816 # Try to detect whether a manual page break is necessary
817 # but only if there was a <%pagebreak ...%> block before
819 if ($chars_per_line) {
821 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
824 my $_description = $self->{"description"}[$i];
825 while ($_description =~ /\\newline/) {
827 $_description =~ s/\\newline//;
829 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
831 if ($current_page == 1) {
832 $lpp = $lines_on_first_page;
834 $lpp = $lines_on_second_page;
837 # Yes we need a manual page break -- or the user has forced one
839 (($current_line + $lines) > $lpp)
840 || ($self->{"_forced_pagebreaks"}
841 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
845 # replace the special variables <%sumcarriedforward%>
848 my $psum = $self->format_amount($myconfig, $sum, 2);
849 $pb =~ s/<%sumcarriedforward%>/$psum/g;
850 $pb =~ s/<%lastpage%>/$current_page/g;
852 # only "normal" variables are supported here
853 # (no <%if, no <%foreach, no <%include)
855 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
857 # page break block is ready to rock
862 $current_line += $lines;
865 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
869 # don't parse par, we need it for each line
871 s/<%(.+?)%>/$self->{$1}[$i]/mg;
877 # if not comes before if!
878 if (/\s*<%if not /) {
880 # check if it is not set and display
882 s/\s*<%if not (.+?)%>/$1/;
884 unless ($self->{$_}) {
886 last if (/\s*<%end /);
896 last if (/\s*<%end /);
904 # check if it is set and display
906 s/\s*<%if (.+?)%>/$1/;
910 last if (/\s*<%end /);
920 last if (/\s*<%end /);
926 # check for <%include filename%>
927 if (/\s*<%include /) {
931 $var =~ s/\s*<%include (.+?)%>/$1/;
934 $var =~ s/(\/|\.\.)//g;
936 # prevent the infinite loop!
937 next if ($self->{"$var"});
939 open(INC, "$self->{templates}/$var")
940 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
949 s/<%(.+?)%>/$self->{$1}/g;
950 s/<nobr><\/nobr>/ /g;
957 # Convert the tex file to postscript
958 if ($self->{format} =~ /(postscript|pdf)/) {
961 $self->{cwd} = cwd();
962 $self->{tmpdir} = "$self->{cwd}/$userspath";
964 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
966 $self->{tmpfile} =~ s/$userspath\///g;
968 if ($self->{format} eq 'postscript') {
970 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
972 $self->error($self->cleanup) if ($?);
975 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
977 $self->error($self->cleanup) if ($?);
980 $self->{tmpfile} =~ s/tex$/dvi/;
982 system("dvips $self->{tmpfile} -o -q > /dev/null");
983 $self->error($self->cleanup . "dvips : $!") if ($?);
984 $self->{tmpfile} =~ s/dvi$/ps/;
986 if ($self->{format} eq 'pdf') {
988 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
990 $self->error($self->cleanup) if ($?);
993 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
995 $self->error($self->cleanup) if ($?);
997 $self->{tmpfile} =~ s/tex$/pdf/;
1002 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
1004 if ($self->{media} eq 'email') {
1008 my $mail = new Mailer;
1010 map { $mail->{$_} = $self->{$_} }
1011 qw(cc bcc subject message version format charset);
1012 $mail->{to} = qq|$self->{email}|;
1013 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1014 $mail->{fileid} = "$fileid.";
1016 # if we send html or plain text inline
1017 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1018 $mail->{contenttype} = "text/html";
1020 $mail->{message} =~ s/\r\n/<br>\n/g;
1021 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1022 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1024 open(IN, $self->{tmpfile})
1025 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1027 $mail->{message} .= $_;
1034 @{ $mail->{attachments} } = ($self->{tmpfile});
1036 $myconfig->{signature} =~ s/\\n/\r\n/g;
1037 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1041 my $err = $mail->send($out);
1042 $self->error($self->cleanup . "$err") if ($err);
1046 $self->{OUT} = $out;
1048 my $numbytes = (-s $self->{tmpfile});
1049 open(IN, $self->{tmpfile})
1050 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1052 $self->{copies} = 1 unless $self->{media} eq 'printer';
1054 chdir("$self->{cwd}");
1056 for my $i (1 .. $self->{copies}) {
1058 open(OUT, $self->{OUT})
1059 or $self->error($self->cleanup . "$self->{OUT} : $!");
1062 # launch application
1063 print qq|Content-Type: application/$self->{format}
1064 Content-Disposition: attachment; filename="$self->{tmpfile}"
1065 Content-Length: $numbytes
1069 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1089 chdir("$self->{cwd}");
1090 $main::lxdebug->leave_sub();
1094 $main::lxdebug->enter_sub();
1098 chdir("$self->{tmpdir}");
1101 if (-f "$self->{tmpfile}.err") {
1102 open(FH, "$self->{tmpfile}.err");
1107 if ($self->{tmpfile}) {
1110 $self->{tmpfile} =~ s/\.\w+$//g;
1111 my $tmpfile = $self->{tmpfile};
1112 unlink(<$tmpfile.*>);
1115 chdir("$self->{cwd}");
1117 $main::lxdebug->leave_sub();
1123 $main::lxdebug->enter_sub();
1125 my ($self, @fields) = @_;
1128 %unique_fields = map({ $_ => 1 } @fields);
1129 @fields = keys(%unique_fields);
1131 foreach my $field (@fields) {
1132 next unless ($self->{$field} =~ /\<pagebreak\>/);
1133 $self->{$field} =~ s/\<pagebreak\>//g;
1134 if ($field =~ /.*_(\d+)$/) {
1135 if (!$self->{"_forced_pagebreaks"}) {
1136 $self->{"_forced_pagebreaks"} = [];
1138 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1142 my $format = $self->{format};
1143 if ($self->{format} =~ /(postscript|pdf)/) {
1150 '<', '>', quotemeta('\n'), '
1154 '&', quotemeta('\n'), '
1156 '"', '\$', '%', '_', '#', quotemeta('^'),
1157 '{', '}', '<', '>', '£', "\r"
1163 quotemeta('\n') => '<br>',
1174 quotemeta('^') => '\^\\',
1179 quotemeta('\n') => '\newline ',
1186 foreach my $key (@{ $replace{order}{$format} }) {
1187 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1190 # Allow some HTML markup to be converted into the output format's
1191 # corresponding markup code, e.g. bold or italic.
1192 if ('html' eq $format) {
1193 my @markup_replace = ('b', 'i', 's', 'u');
1195 foreach my $key (@markup_replace) {
1196 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1199 } elsif ('tex' eq $format) {
1200 my %markup_replace = ('b' => 'textbf',
1202 'u' => 'underline');
1204 foreach my $field (@fields) {
1205 foreach my $key (keys(%markup_replace)) {
1206 my $new = $markup_replace{$key};
1208 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1213 $main::lxdebug->leave_sub();
1217 $main::lxdebug->enter_sub();
1219 my ($self, $date, $myconfig) = @_;
1221 if ($date && $date =~ /\D/) {
1223 if ($myconfig->{dateformat} =~ /^yy/) {
1224 ($yy, $mm, $dd) = split /\D/, $date;
1226 if ($myconfig->{dateformat} =~ /^mm/) {
1227 ($mm, $dd, $yy) = split /\D/, $date;
1229 if ($myconfig->{dateformat} =~ /^dd/) {
1230 ($dd, $mm, $yy) = split /\D/, $date;
1235 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1236 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1238 $dd = "0$dd" if ($dd < 10);
1239 $mm = "0$mm" if ($mm < 10);
1241 $date = "$yy$mm$dd";
1244 $main::lxdebug->leave_sub();
1249 # Database routines used throughout
1252 $main::lxdebug->enter_sub();
1254 my ($self, $myconfig) = @_;
1256 # connect to database
1258 DBI->connect($myconfig->{dbconnect},
1259 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1263 if ($myconfig->{dboptions}) {
1264 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1267 $main::lxdebug->leave_sub();
1272 sub dbconnect_noauto {
1273 $main::lxdebug->enter_sub();
1275 my ($self, $myconfig) = @_;
1277 # connect to database
1279 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1280 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1284 if ($myconfig->{dboptions}) {
1285 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1288 $main::lxdebug->leave_sub();
1293 sub update_balance {
1294 $main::lxdebug->enter_sub();
1296 my ($self, $dbh, $table, $field, $where, $value) = @_;
1298 # if we have a value, go do it
1301 # retrieve balance from table
1302 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1303 my $sth = $dbh->prepare($query);
1305 $sth->execute || $self->dberror($query);
1306 my ($balance) = $sth->fetchrow_array;
1312 $query = "UPDATE $table SET $field = $balance WHERE $where";
1313 $dbh->do($query) || $self->dberror($query);
1315 $main::lxdebug->leave_sub();
1318 sub update_exchangerate {
1319 $main::lxdebug->enter_sub();
1321 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1323 # some sanity check for currency
1325 $main::lxdebug->leave_sub();
1329 my $query = qq|SELECT e.curr FROM exchangerate e
1330 WHERE e.curr = '$curr'
1331 AND e.transdate = '$transdate'
1333 my $sth = $dbh->prepare($query);
1334 $sth->execute || $self->dberror($query);
1337 if ($buy != 0 && $sell != 0) {
1338 $set = "buy = $buy, sell = $sell";
1339 } elsif ($buy != 0) {
1340 $set = "buy = $buy";
1341 } elsif ($sell != 0) {
1342 $set = "sell = $sell";
1345 if ($sth->fetchrow_array) {
1346 $query = qq|UPDATE exchangerate
1348 WHERE curr = '$curr'
1349 AND transdate = '$transdate'|;
1351 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1352 VALUES ('$curr', $buy, $sell, '$transdate')|;
1355 $dbh->do($query) || $self->dberror($query);
1357 $main::lxdebug->leave_sub();
1360 sub save_exchangerate {
1361 $main::lxdebug->enter_sub();
1363 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1365 my $dbh = $self->dbconnect($myconfig);
1367 my ($buy, $sell) = (0, 0);
1368 $buy = $rate if $fld eq 'buy';
1369 $sell = $rate if $fld eq 'sell';
1371 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1375 $main::lxdebug->leave_sub();
1378 sub get_exchangerate {
1379 $main::lxdebug->enter_sub();
1381 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1383 unless ($transdate) {
1384 $main::lxdebug->leave_sub();
1388 my $query = qq|SELECT e.$fld FROM exchangerate e
1389 WHERE e.curr = '$curr'
1390 AND e.transdate = '$transdate'|;
1391 my $sth = $dbh->prepare($query);
1392 $sth->execute || $self->dberror($query);
1394 my ($exchangerate) = $sth->fetchrow_array;
1397 $main::lxdebug->leave_sub();
1399 return $exchangerate;
1402 sub check_exchangerate {
1403 $main::lxdebug->enter_sub();
1405 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1407 unless ($transdate) {
1408 $main::lxdebug->leave_sub();
1412 my $dbh = $self->dbconnect($myconfig);
1414 my $query = qq|SELECT e.$fld FROM exchangerate e
1415 WHERE e.curr = '$currency'
1416 AND e.transdate = '$transdate'|;
1417 my $sth = $dbh->prepare($query);
1418 $sth->execute || $self->dberror($query);
1420 my ($exchangerate) = $sth->fetchrow_array;
1424 $main::lxdebug->leave_sub();
1426 return $exchangerate;
1430 $main::lxdebug->enter_sub();
1432 my ($self, $dbh, $id) = @_;
1436 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1438 if ($self->{"shipto$item"}) {
1439 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1441 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1446 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1447 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1448 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1449 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1450 '$self->{shiptozipcode}', '$self->{shiptocity}',
1451 '$self->{shiptocountry}', '$self->{shiptocontact}',
1452 '$self->{shiptophone}', '$self->{shiptofax}',
1453 '$self->{shiptoemail}')|;
1454 $dbh->do($query) || $self->dberror($query);
1457 $main::lxdebug->leave_sub();
1461 $main::lxdebug->enter_sub();
1463 my ($self, $dbh) = @_;
1465 my $query = qq|SELECT e.id, e.name FROM employee e
1466 WHERE e.login = '$self->{login}'|;
1467 my $sth = $dbh->prepare($query);
1468 $sth->execute || $self->dberror($query);
1470 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1471 $self->{employee_id} *= 1;
1475 $main::lxdebug->leave_sub();
1478 # get other contact for transaction and form - html/tex
1480 $main::lxdebug->enter_sub();
1482 my ($self, $dbh, $id) = @_;
1484 my $query = qq|SELECT c.*
1487 $sth = $dbh->prepare($query);
1488 $sth->execute || $self->dberror($query);
1490 $ref = $sth->fetchrow_hashref(NAME_lc);
1492 push @{ $self->{$_} }, $ref;
1495 $main::lxdebug->leave_sub();
1498 # get contacts for id, if no contact return {"","","","",""}
1500 $main::lxdebug->enter_sub();
1502 my ($self, $dbh, $id) = @_;
1504 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1506 WHERE cp_cv_id=$id|;
1507 my $sth = $dbh->prepare($query);
1508 $sth->execute || $self->dberror($query);
1511 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1512 push @{ $self->{all_contacts} }, $ref;
1517 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1520 $main::lxdebug->leave_sub();
1523 # this sub gets the id and name from $table
1525 $main::lxdebug->enter_sub();
1527 my ($self, $myconfig, $table) = @_;
1529 # connect to database
1530 my $dbh = $self->dbconnect($myconfig);
1532 my $name = $self->like(lc $self->{$table});
1533 my $customernumber = $self->like(lc $self->{customernumber});
1535 if ($self->{customernumber} ne "") {
1536 $query = qq~SELECT c.id, c.name,
1537 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1539 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1542 $query = qq~SELECT c.id, c.name,
1543 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1545 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1549 if ($self->{openinvoices}) {
1550 $query = qq~SELECT DISTINCT c.id, c.name,
1551 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1552 FROM $self->{arap} a
1553 JOIN $table c ON (a.${table}_id = c.id)
1554 WHERE NOT a.amount = a.paid
1555 AND lower(c.name) LIKE '$name'
1558 my $sth = $dbh->prepare($query);
1560 $sth->execute || $self->dberror($query);
1563 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1564 push(@{ $self->{name_list} }, $ref);
1570 $main::lxdebug->leave_sub();
1575 # the selection sub is used in the AR, AP, IS, IR and OE module
1578 $main::lxdebug->enter_sub();
1580 my ($self, $myconfig, $table, $module) = @_;
1583 my $dbh = $self->dbconnect($myconfig);
1585 my $query = qq|SELECT count(*) FROM $table|;
1586 my $sth = $dbh->prepare($query);
1587 $sth->execute || $self->dberror($query);
1588 my ($count) = $sth->fetchrow_array;
1591 # build selection list
1592 if ($count < $myconfig->{vclimit}) {
1593 $query = qq|SELECT id, name
1594 FROM $table WHERE not obsolete
1596 $sth = $dbh->prepare($query);
1597 $sth->execute || $self->dberror($query);
1599 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1600 push @{ $self->{"all_$table"} }, $ref;
1608 $self->get_employee($dbh);
1610 # setup sales contacts
1611 $query = qq|SELECT e.id, e.name
1614 AND NOT e.id = $self->{employee_id}|;
1615 $sth = $dbh->prepare($query);
1616 $sth->execute || $self->dberror($query);
1618 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1619 push @{ $self->{all_employees} }, $ref;
1624 push @{ $self->{all_employees} },
1625 { id => $self->{employee_id},
1626 name => $self->{employee} };
1628 # sort the whole thing
1629 @{ $self->{all_employees} } =
1630 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1632 if ($module eq 'AR') {
1634 # prepare query for departments
1635 $query = qq|SELECT d.id, d.description
1641 $query = qq|SELECT d.id, d.description
1646 $sth = $dbh->prepare($query);
1647 $sth->execute || $self->dberror($query);
1649 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1650 push @{ $self->{all_departments} }, $ref;
1655 $main::lxdebug->leave_sub();
1658 # this is only used for reports
1659 sub all_departments {
1660 $main::lxdebug->enter_sub();
1662 my ($self, $myconfig, $table) = @_;
1664 my $dbh = $self->dbconnect($myconfig);
1665 my $where = "1 = 1";
1667 if (defined $table) {
1668 if ($table eq 'customer') {
1669 $where = " d.role = 'P'";
1673 my $query = qq|SELECT d.id, d.description
1677 my $sth = $dbh->prepare($query);
1678 $sth->execute || $self->dberror($query);
1680 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1681 push @{ $self->{all_departments} }, $ref;
1687 $main::lxdebug->leave_sub();
1691 $main::lxdebug->enter_sub();
1693 my ($self, $module, $myconfig, $table) = @_;
1695 $self->all_vc($myconfig, $table, $module);
1697 # get last customers or vendors
1700 my $dbh = $self->dbconnect($myconfig);
1704 # now get the account numbers
1705 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1707 WHERE c.link LIKE '%$module%'
1710 $sth = $dbh->prepare($query);
1711 $sth->execute || $self->dberror($query);
1713 $self->{accounts} = "";
1714 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1716 foreach my $key (split /:/, $ref->{link}) {
1717 if ($key =~ /$module/) {
1719 # cross reference for keys
1720 $xkeyref{ $ref->{accno} } = $key;
1722 push @{ $self->{"${module}_links"}{$key} },
1723 { accno => $ref->{accno},
1724 description => $ref->{description},
1725 taxkey => $ref->{taxkey_id} };
1727 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1733 if (($module eq "AP") || ($module eq "AR")) {
1735 # get tax rates and description
1736 $query = qq| SELECT * FROM tax t|;
1737 $sth = $dbh->prepare($query);
1738 $sth->execute || $self->dberror($query);
1740 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1741 push @{ $self->{TAX} }, $ref;
1747 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1749 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1750 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1751 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1752 c.name AS $table, a.department_id, d.description AS department,
1753 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1754 a.employee_id, e.name AS employee, a.gldate
1756 JOIN $table c ON (a.${table}_id = c.id)
1757 LEFT JOIN employee e ON (e.id = a.employee_id)
1758 LEFT JOIN department d ON (d.id = a.department_id)
1759 WHERE a.id = $self->{id}|;
1760 $sth = $dbh->prepare($query);
1761 $sth->execute || $self->dberror($query);
1763 $ref = $sth->fetchrow_hashref(NAME_lc);
1764 foreach $key (keys %$ref) {
1765 $self->{$key} = $ref->{$key};
1769 # get amounts from individual entries
1770 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1771 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1773 JOIN chart c ON (c.id = a.chart_id)
1774 LEFT JOIN project p ON (p.id = a.project_id)
1775 LEFT Join tax t ON (a.taxkey = t.taxkey)
1776 WHERE a.trans_id = $self->{id}
1777 AND a.fx_transaction = '0'
1778 ORDER BY a.oid,a.transdate|;
1779 $sth = $dbh->prepare($query);
1780 $sth->execute || $self->dberror($query);
1782 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1784 # get exchangerate for currency
1785 $self->{exchangerate} =
1786 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1790 # store amounts in {acc_trans}{$key} for multiple accounts
1791 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1792 $ref->{exchangerate} =
1793 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1795 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1798 $ref->{index} = $index;
1800 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1804 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1805 (SELECT c.accno FROM chart c
1806 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1807 (SELECT c.accno FROM chart c
1808 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1810 $sth = $dbh->prepare($query);
1811 $sth->execute || $self->dberror($query);
1813 $ref = $sth->fetchrow_hashref(NAME_lc);
1814 map { $self->{$_} = $ref->{$_} } keys %$ref;
1820 $query = qq|SELECT current_date AS transdate,
1821 d.curr AS currencies, d.closedto, d.revtrans,
1822 (SELECT c.accno FROM chart c
1823 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1824 (SELECT c.accno FROM chart c
1825 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1827 $sth = $dbh->prepare($query);
1828 $sth->execute || $self->dberror($query);
1830 $ref = $sth->fetchrow_hashref(NAME_lc);
1831 map { $self->{$_} = $ref->{$_} } keys %$ref;
1834 if ($self->{"$self->{vc}_id"}) {
1836 # only setup currency
1837 ($self->{currency}) = split /:/, $self->{currencies};
1841 $self->lastname_used($dbh, $myconfig, $table, $module);
1843 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1845 # get exchangerate for currency
1846 $self->{exchangerate} =
1847 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1856 $main::lxdebug->leave_sub();
1860 $main::lxdebug->enter_sub();
1862 my ($self, $dbh, $myconfig, $table, $module) = @_;
1864 my $arap = ($table eq 'customer') ? "ar" : "ap";
1865 my $where = "1 = 1";
1867 if ($self->{type} =~ /_order/) {
1869 $where = "quotation = '0'";
1871 if ($self->{type} =~ /_quotation/) {
1873 $where = "quotation = '1'";
1876 my $query = qq|SELECT MAX(id) FROM $arap
1878 AND ${table}_id > 0|;
1879 my $sth = $dbh->prepare($query);
1880 $sth->execute || $self->dberror($query);
1882 my ($trans_id) = $sth->fetchrow_array;
1886 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1887 current_date + ct.terms AS duedate, a.department_id,
1888 d.description AS department
1890 JOIN $table ct ON (a.${table}_id = ct.id)
1891 LEFT JOIN department d ON (a.department_id = d.id)
1892 WHERE a.id = $trans_id|;
1893 $sth = $dbh->prepare($query);
1894 $sth->execute || $self->dberror($query);
1896 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1897 $self->{duedate}, $self->{department_id}, $self->{department})
1898 = $sth->fetchrow_array;
1901 $main::lxdebug->leave_sub();
1905 $main::lxdebug->enter_sub();
1907 my ($self, $myconfig, $thisdate, $days) = @_;
1909 my $dbh = $self->dbconnect($myconfig);
1914 my $dateformat = $myconfig->{dateformat};
1915 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1917 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1919 $sth = $dbh->prepare($query);
1920 $sth->execute || $self->dberror($query);
1922 $query = qq|SELECT current_date AS thisdate
1924 $sth = $dbh->prepare($query);
1925 $sth->execute || $self->dberror($query);
1928 ($thisdate) = $sth->fetchrow_array;
1933 $main::lxdebug->leave_sub();
1939 $main::lxdebug->enter_sub();
1941 my ($self, $string) = @_;
1943 if ($string !~ /%/) {
1944 $string = "%$string%";
1947 $string =~ s/\'/\'\'/g;
1949 $main::lxdebug->leave_sub();
1955 $main::lxdebug->enter_sub();
1957 my ($self, $flds, $new, $count, $numrows) = @_;
1961 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1967 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1969 $j = $item->{ndx} - 1;
1970 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1974 for $i ($count + 1 .. $numrows) {
1975 map { delete $self->{"${_}_$i"} } @{$flds};
1978 $main::lxdebug->leave_sub();
1982 $main::lxdebug->enter_sub();
1984 my ($self, $myconfig) = @_;
1988 my $dbh = $self->dbconnect_noauto($myconfig);
1990 my $query = qq|DELETE FROM status
1991 WHERE formname = '$self->{formname}'
1993 my $sth = $dbh->prepare($query) || $self->dberror($query);
1995 if ($self->{formname} =~ /(check|receipt)/) {
1996 for $i (1 .. $self->{rowcount}) {
1997 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2001 $sth->execute($self->{id}) || $self->dberror($query);
2005 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2006 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2008 my %queued = split / /, $self->{queued};
2010 if ($self->{formname} =~ /(check|receipt)/) {
2012 # this is a check or receipt, add one entry for each lineitem
2013 my ($accno) = split /--/, $self->{account};
2014 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2015 chart_id) VALUES (?, '$printed',
2016 '$queued{$self->{formname}}', '$self->{prinform}',
2017 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2018 $sth = $dbh->prepare($query) || $self->dberror($query);
2020 for $i (1 .. $self->{rowcount}) {
2021 if ($self->{"checked_$i"}) {
2022 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2027 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2028 spoolfile, formname)
2029 VALUES ($self->{id}, '$printed', '$emailed',
2030 '$queued{$self->{formname}}', '$self->{formname}')|;
2031 $dbh->do($query) || $self->dberror($query);
2037 $main::lxdebug->leave_sub();
2041 $main::lxdebug->enter_sub();
2043 my ($self, $dbh) = @_;
2045 my ($query, $printed, $emailed);
2047 my $formnames = $self->{printed};
2048 my $emailforms = $self->{emailed};
2050 my $query = qq|DELETE FROM status
2051 WHERE formname = '$self->{formname}'
2052 AND trans_id = $self->{id}|;
2053 $dbh->do($query) || $self->dberror($query);
2055 # this only applies to the forms
2056 # checks and receipts are posted when printed or queued
2058 if ($self->{queued}) {
2059 my %queued = split / /, $self->{queued};
2061 foreach my $formname (keys %queued) {
2062 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2063 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2065 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2066 spoolfile, formname)
2067 VALUES ($self->{id}, '$printed', '$emailed',
2068 '$queued{$formname}', '$formname')|;
2069 $dbh->do($query) || $self->dberror($query);
2071 $formnames =~ s/$self->{formname}//;
2072 $emailforms =~ s/$self->{formname}//;
2077 # save printed, emailed info
2078 $formnames =~ s/^ +//g;
2079 $emailforms =~ s/^ +//g;
2082 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2083 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2085 foreach my $formname (keys %status) {
2086 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2087 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2089 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2090 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2091 $dbh->do($query) || $self->dberror($query);
2094 $main::lxdebug->leave_sub();
2097 sub update_defaults {
2098 $main::lxdebug->enter_sub();
2100 my ($self, $myconfig, $fld) = @_;
2102 my $dbh = $self->dbconnect_noauto($myconfig);
2103 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2104 my $sth = $dbh->prepare($query);
2106 $sth->execute || $self->dberror($query);
2107 my ($var) = $sth->fetchrow_array;
2112 $query = qq|UPDATE defaults
2114 $dbh->do($query) || $form->dberror($query);
2119 $main::lxdebug->leave_sub();
2124 sub update_business {
2125 $main::lxdebug->enter_sub();
2127 my ($self, $myconfig, $business_id) = @_;
2129 my $dbh = $self->dbconnect_noauto($myconfig);
2131 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2132 my $sth = $dbh->prepare($query);
2134 $sth->execute || $self->dberror($query);
2135 my ($var) = $sth->fetchrow_array;
2140 $query = qq|UPDATE business
2141 SET customernumberinit = '$var' WHERE id=$business_id|;
2142 $dbh->do($query) || $form->dberror($query);
2147 $main::lxdebug->leave_sub();
2153 $main::lxdebug->enter_sub();
2155 my ($self, $myconfig, $salesman) = @_;
2157 my $dbh = $self->dbconnect($myconfig);
2159 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2160 my $sth = $dbh->prepare($query);
2161 $sth->execute || $self->dberror($query);
2164 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2165 push(@{ $self->{salesman_list} }, $ref);
2169 $main::lxdebug->leave_sub();
2174 sub get_partsgroup {
2175 $main::lxdebug->enter_sub();
2177 my ($self, $myconfig, $p) = @_;
2179 my $dbh = $self->dbconnect($myconfig);
2181 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2183 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2185 if ($p->{searchitems} eq 'part') {
2187 WHERE p.inventory_accno_id > 0|;
2189 if ($p->{searchitems} eq 'service') {
2191 WHERE p.inventory_accno_id IS NULL|;
2193 if ($p->{searchitems} eq 'assembly') {
2195 WHERE p.assembly = '1'|;
2197 if ($p->{searchitems} eq 'labor') {
2199 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2203 ORDER BY partsgroup|;
2206 $query = qq|SELECT id, partsgroup FROM partsgroup
2207 ORDER BY partsgroup|;
2210 if ($p->{language_code}) {
2211 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2212 t.description AS translation
2214 JOIN parts p ON (p.partsgroup_id = pg.id)
2215 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2216 ORDER BY translation|;
2219 my $sth = $dbh->prepare($query);
2220 $sth->execute || $self->dberror($query);
2222 $self->{all_partsgroup} = ();
2223 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2224 push @{ $self->{all_partsgroup} }, $ref;
2228 $main::lxdebug->leave_sub();
2231 sub get_pricegroup {
2232 $main::lxdebug->enter_sub();
2234 my ($self, $myconfig, $p) = @_;
2236 my $dbh = $self->dbconnect($myconfig);
2238 my $query = qq|SELECT p.id, p.pricegroup
2242 ORDER BY pricegroup|;
2245 $query = qq|SELECT id, pricegroup FROM pricegroup
2246 ORDER BY pricegroup|;
2249 my $sth = $dbh->prepare($query);
2250 $sth->execute || $self->dberror($query);
2252 $self->{all_pricegroup} = ();
2253 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2254 push @{ $self->{all_pricegroup} }, $ref;
2259 $main::lxdebug->leave_sub();
2263 my ($self, $dbh, $myconfig, $audittrail) = @_;
2265 # table, $reference, $formname, $action, $id, $transdate) = @_;
2272 $dbh = $self->dbconnect($myconfig);
2276 # if we have an id add audittrail, otherwise get a new timestamp
2278 if ($audittrail->{id}) {
2280 $query = qq|SELECT audittrail FROM defaults|;
2282 if ($dbh->selectrow_array($query)) {
2283 my ($null, $employee_id) = $self->get_employee($dbh);
2285 if ($self->{audittrail} && !$myconfig) {
2286 chop $self->{audittrail};
2288 my @a = split /\|/, $self->{audittrail};
2292 my @flds = qw(tablename reference formname action transdate);
2294 # put into hash and remove dups
2296 $key = "$a[2]$a[3]";
2298 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2302 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2303 formname, action, employee_id, transdate)
2304 VALUES ($audittrail->{id}, ?, ?,
2305 ?, ?, $employee_id, ?)|;
2306 my $sth = $dbh->prepare($query) || $self->dberror($query);
2310 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2314 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2316 $sth->execute || $self->dberror;
2321 if ($audittrail->{transdate}) {
2322 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2323 formname, action, employee_id, transdate) VALUES (
2324 $audittrail->{id}, '$audittrail->{tablename}', |
2325 . $dbh->quote($audittrail->{reference}) . qq|,
2326 '$audittrail->{formname}', '$audittrail->{action}',
2327 $employee_id, '$audittrail->{transdate}')|;
2329 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2330 formname, action, employee_id) VALUES ($audittrail->{id},
2331 '$audittrail->{tablename}', |
2332 . $dbh->quote($audittrail->{reference}) . qq|,
2333 '$audittrail->{formname}', '$audittrail->{action}',
2340 $query = qq|SELECT current_timestamp FROM defaults|;
2341 my ($timestamp) = $dbh->selectrow_array($query);
2344 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2347 $dbh->disconnect if $disconnect;
2356 $main::lxdebug->enter_sub();
2358 my ($type, $country, $NLS_file) = @_;
2362 if ($country && -d "locale/$country") {
2363 $self->{countrycode} = $country;
2364 eval { require "locale/$country/$NLS_file"; };
2367 $self->{NLS_file} = $NLS_file;
2369 push @{ $self->{LONG_MONTH} },
2370 ("January", "February", "March", "April",
2371 "May ", "June", "July", "August",
2372 "September", "October", "November", "December");
2373 push @{ $self->{SHORT_MONTH} },
2374 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2376 $main::lxdebug->leave_sub();
2382 my ($self, $text) = @_;
2384 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2388 $main::lxdebug->enter_sub();
2390 my ($self, $text) = @_;
2392 if (exists $self{subs}{$text}) {
2393 $text = $self{subs}{$text};
2395 if ($self->{countrycode} && $self->{NLS_file}) {
2397 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2401 $main::lxdebug->leave_sub();
2407 $main::lxdebug->enter_sub();
2409 my ($self, $myconfig, $date, $longformat) = @_;
2412 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2417 $spc = $myconfig->{dateformat};
2419 $spc = substr($spc, 1, 1);
2421 if ($date =~ /\D/) {
2422 if ($myconfig->{dateformat} =~ /^yy/) {
2423 ($yy, $mm, $dd) = split /\D/, $date;
2425 if ($myconfig->{dateformat} =~ /^mm/) {
2426 ($mm, $dd, $yy) = split /\D/, $date;
2428 if ($myconfig->{dateformat} =~ /^dd/) {
2429 ($dd, $mm, $yy) = split /\D/, $date;
2432 $date = substr($date, 2);
2433 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2438 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2439 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2441 if ($myconfig->{dateformat} =~ /^dd/) {
2442 if (defined $longformat && $longformat == 0) {
2444 $dd = "0$dd" if ($dd < 10);
2445 $mm = "0$mm" if ($mm < 10);
2446 $longdate = "$dd$spc$mm$spc$yy";
2449 $longdate .= ($spc eq '.') ? ". " : " ";
2450 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2452 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2454 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2455 # Lx-Office is mainly used in Germany or German speaking countries.
2456 if (defined $longformat && $longformat == 0) {
2458 $dd = "0$dd" if ($dd < 10);
2459 $mm = "0$mm" if ($mm < 10);
2460 $longdate = "$yy-$mm-$dd";
2462 $longdate = "$dd. ";
2463 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2466 if (defined $longformat && $longformat == 0) {
2468 $dd = "0$dd" if ($dd < 10);
2469 $mm = "0$mm" if ($mm < 10);
2470 $longdate = "$mm$spc$dd$spc$yy";
2472 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2478 $main::lxdebug->leave_sub();
2484 $main::lxdebug->enter_sub();
2486 my ($self, $myconfig, $date, $longformat) = @_;
2489 $main::lxdebug->leave_sub();
2494 $spc = $myconfig->{dateformat};
2496 $spc = substr($spc, 1, 1);
2498 if ($date =~ /\D/) {
2499 if ($myconfig->{dateformat} =~ /^yy/) {
2500 ($yy, $mm, $dd) = split /\D/, $date;
2501 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2502 ($mm, $dd, $yy) = split /\D/, $date;
2503 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2504 ($dd, $mm, $yy) = split /\D/, $date;
2507 $date = substr($date, 2);
2508 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2513 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2514 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2516 $main::lxdebug->leave_sub();
2517 return ($yy, $mm, $dd);