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 $file = "templates/webpages/${file}_" . $main::myconfig{"countrycode"} .
423 } elsif (-f "templates/webpages/${file}.html") {
424 $file = "templates/webpages/${file}.html";
426 $self->error("Web page template '${file}' not found.");
429 my $template = HTML::Template->new("filename" => $file,
430 "die_on_bad_params" => 0,
432 "case_sensitive" => 1,
433 "loop_context_vars" => 1,
436 $additional_params = {} unless ($additional_params);
437 if ($self->{"DEBUG"}) {
438 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
441 if ($additional_params->{"DEBUG"}) {
442 $additional_params->{"DEBUG"} =
443 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
446 my @additional_param_names = keys(%{$additional_params});
448 foreach my $key ($template->param()) {
449 if (grep(/^${key}$/, @additional_param_names)) {
450 $template->param($key => $additional_params->{$key});
452 $template->param($key => $self->{$key});
456 my $output = $template->output();
458 $main::lxdebug->leave_sub();
463 sub show_generic_error {
464 my ($self, $error, $title) = @_;
467 $add_params->{"title"} = $title if ($title);
468 $self->{"label_error"} = $error;
470 print($self->parse_html_template("generic/error", $add_params));
473 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
474 # changed it to accept an arbitrary number of triggers - sschoeling
476 $main::lxdebug->enter_sub();
479 my $myconfig = shift;
482 # set dateform for jsscript
484 $ifFormat = "%d.%m.%Y";
485 if ($myconfig->{dateformat} eq "dd.mm.yy") {
486 $ifFormat = "%d.%m.%Y";
488 if ($myconfig->{dateformat} eq "dd-mm-yy") {
489 $ifFormat = "%d-%m-%Y";
491 if ($myconfig->{dateformat} eq "dd/mm/yy") {
492 $ifFormat = "%d/%m/%Y";
494 if ($myconfig->{dateformat} eq "mm/dd/yy") {
495 $ifFormat = "%m/%d/%Y";
497 if ($myconfig->{dateformat} eq "mm-dd-yy") {
498 $ifFormat = "%m-%d-%Y";
500 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
501 $ifFormat = "%Y-%m-%d";
513 inputField : "| . (shift) . qq|",
514 ifFormat :"$ifFormat",
515 align : "| . (shift) . qq|",
516 button : "| . (shift) . qq|"
522 <script type="text/javascript">
523 <!--| . join("", @triggers) . qq|//-->
527 $main::lxdebug->leave_sub();
530 } #end sub write_trigger
533 $main::lxdebug->enter_sub();
535 my ($self, $msg) = @_;
537 if ($self->{callback}) {
539 ($script, $argv) = split(/\?/, $self->{callback});
540 exec("perl", "$script", $argv);
548 $main::lxdebug->leave_sub();
551 # sort of columns removed - empty sub
553 $main::lxdebug->enter_sub();
555 my ($self, @columns) = @_;
557 $main::lxdebug->leave_sub();
563 $main::lxdebug->enter_sub();
565 my ($self, $myconfig, $amount, $places, $dash) = @_;
567 #Workaround for $format_amount calls without $places
568 if (!defined $places) {
569 (my $dec) = ($amount =~ /\.(\d+)/);
570 $places = length $dec;
573 if ($places =~ /\d/) {
574 $amount = $self->round_amount($amount, $places);
577 # is the amount negative
578 my $negative = ($amount < 0);
582 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
584 my ($whole, $dec) = split /\./, "$amount";
586 $amount = join '', reverse split //, $whole;
587 $fillup = "0" x ($places - length($dec));
589 if ($myconfig->{numberformat} eq '1,000.00') {
590 $amount =~ s/\d{3,}?/$&,/g;
592 $amount = join '', reverse split //, $amount;
593 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
596 if ($myconfig->{numberformat} eq '1.000,00') {
597 $amount =~ s/\d{3,}?/$&./g;
599 $amount = join '', reverse split //, $amount;
600 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
603 if ($myconfig->{numberformat} eq '1000,00') {
605 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
609 $amount = ($negative) ? "($amount)" : "$amount";
610 } elsif ($dash =~ /DRCR/) {
611 $amount = ($negative) ? "$amount DR" : "$amount CR";
613 $amount = ($negative) ? "-$amount" : "$amount";
617 if ($dash eq "0" && $places) {
618 if ($myconfig->{numberformat} eq '1.000,00') {
619 $amount = "0" . "," . "0" x $places;
621 $amount = "0" . "." . "0" x $places;
624 $amount = ($dash ne "") ? "$dash" : "0";
628 $main::lxdebug->leave_sub();
634 $main::lxdebug->enter_sub();
636 my ($self, $myconfig, $amount) = @_;
637 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
639 if ($myconfig->{in_numberformat} == 1) {
641 # Extra input number format 1000.00 or 1000,00
642 $main::lxdebug->message(LXDebug::DEBUG2,
643 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
646 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
647 $amount = scalar reverse $amount;
649 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
650 $amount =~ s/\./DOT/;
652 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
655 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
656 $amount =~ s/DOT/\./;
658 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
659 $amount = scalar reverse $amount;
660 $main::lxdebug->message(LXDebug::DEBUG2,
661 "Parsed amount:" . $amount . "\n");
663 return ($amount * 1);
666 $main::lxdebug->message(LXDebug::DEBUG2,
667 "in_numberformat: " . $main::locale->text('equal Outputformat'));
668 $main::lxdebug->message(LXDebug::DEBUG2,
669 " = numberformat: $myconfig->{numberformat}");
670 if ( ($myconfig->{numberformat} eq '1.000,00')
671 || ($myconfig->{numberformat} eq '1000,00')) {
676 if ($myconfig->{numberformat} eq "1'000.00") {
682 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
684 $main::lxdebug->leave_sub();
686 return ($amount * 1);
690 $main::lxdebug->enter_sub();
692 my ($self, $amount, $places) = @_;
695 # Rounding like "Kaufmannsrunden"
696 # Descr. http://de.wikipedia.org/wiki/Rundung
698 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
701 $amount = $amount * (10**($places));
702 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
704 $main::lxdebug->leave_sub();
706 return $round_amount;
711 $main::lxdebug->enter_sub();
713 my ($self, $myconfig, $userspath) = @_;
716 # Some variables used for page breaks
717 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
719 my ($current_page, $current_line, $current_row) = (1, 1, 0);
725 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
726 $self->format_string(grep(/notes/, keys(%{$self})));
728 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
729 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
731 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
732 qw(email tel fax name signature));
734 open(IN, "$self->{templates}/$self->{IN}")
735 or $self->error("$self->{IN} : $!");
740 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
742 # OUT is used for the media, screen, printer, email
743 # for postscript we store a copy in a temporary file
745 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
746 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
748 $self->{OUT} = ">$self->{tmpfile}";
752 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
754 open(OUT, ">-") or $self->error("STDOUT : $!");
758 # Do we have to run LaTeX two times? This is needed if
759 # the template contains page references.
762 # first we generate a tmpfile
763 # read file and replace <%variable%>
769 $two_passes = 1 if (/\\pageref/);
772 # detect pagebreak block and its parameters
773 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
774 $chars_per_line = $1;
775 $lines_on_first_page = $2;
776 $lines_on_second_page = $3;
779 last if (/\s*<%end pagebreak%>/);
786 if (/\s*<%foreach /) {
788 # this one we need for the count
790 $var =~ s/\s*<%foreach (.+?)%>/$1/;
792 last if (/\s*<%end /);
798 # display contents of $self->{number}[] array
799 for $i (0 .. $#{ $self->{$var} }) {
802 # Try to detect whether a manual page break is necessary
803 # but only if there was a <%pagebreak ...%> block before
805 if ($chars_per_line) {
807 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
810 my $_description = $self->{"description"}[$i];
811 while ($_description =~ /\\newline/) {
813 $_description =~ s/\\newline//;
815 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
817 if ($current_page == 1) {
818 $lpp = $lines_on_first_page;
820 $lpp = $lines_on_second_page;
823 # Yes we need a manual page break -- or the user has forced one
825 (($current_line + $lines) > $lpp)
826 || ($self->{"_forced_pagebreaks"}
827 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
831 # replace the special variables <%sumcarriedforward%>
834 my $psum = $self->format_amount($myconfig, $sum, 2);
835 $pb =~ s/<%sumcarriedforward%>/$psum/g;
836 $pb =~ s/<%lastpage%>/$current_page/g;
838 # only "normal" variables are supported here
839 # (no <%if, no <%foreach, no <%include)
841 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
843 # page break block is ready to rock
848 $current_line += $lines;
851 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
855 # don't parse par, we need it for each line
857 s/<%(.+?)%>/$self->{$1}[$i]/mg;
863 # if not comes before if!
864 if (/\s*<%if not /) {
866 # check if it is not set and display
868 s/\s*<%if not (.+?)%>/$1/;
870 unless ($self->{$_}) {
872 last if (/\s*<%end /);
882 last if (/\s*<%end /);
890 # check if it is set and display
892 s/\s*<%if (.+?)%>/$1/;
896 last if (/\s*<%end /);
906 last if (/\s*<%end /);
912 # check for <%include filename%>
913 if (/\s*<%include /) {
917 $var =~ s/\s*<%include (.+?)%>/$1/;
920 $var =~ s/(\/|\.\.)//g;
922 # prevent the infinite loop!
923 next if ($self->{"$var"});
925 open(INC, "$self->{templates}/$var")
926 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
935 s/<%(.+?)%>/$self->{$1}/g;
936 s/<nobr><\/nobr>/ /g;
943 # Convert the tex file to postscript
944 if ($self->{format} =~ /(postscript|pdf)/) {
947 $self->{cwd} = cwd();
948 $self->{tmpdir} = "$self->{cwd}/$userspath";
950 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
952 $self->{tmpfile} =~ s/$userspath\///g;
954 if ($self->{format} eq 'postscript') {
956 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
958 $self->error($self->cleanup) if ($?);
961 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
963 $self->error($self->cleanup) if ($?);
966 $self->{tmpfile} =~ s/tex$/dvi/;
968 system("dvips $self->{tmpfile} -o -q > /dev/null");
969 $self->error($self->cleanup . "dvips : $!") if ($?);
970 $self->{tmpfile} =~ s/dvi$/ps/;
972 if ($self->{format} eq 'pdf') {
974 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
976 $self->error($self->cleanup) if ($?);
979 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
981 $self->error($self->cleanup) if ($?);
983 $self->{tmpfile} =~ s/tex$/pdf/;
988 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
990 if ($self->{media} eq 'email') {
994 my $mail = new Mailer;
996 map { $mail->{$_} = $self->{$_} }
997 qw(cc bcc subject message version format charset);
998 $mail->{to} = qq|$self->{email}|;
999 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1000 $mail->{fileid} = "$fileid.";
1002 # if we send html or plain text inline
1003 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1004 $mail->{contenttype} = "text/html";
1006 $mail->{message} =~ s/\r\n/<br>\n/g;
1007 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1008 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1010 open(IN, $self->{tmpfile})
1011 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1013 $mail->{message} .= $_;
1020 @{ $mail->{attachments} } = ($self->{tmpfile});
1022 $myconfig->{signature} =~ s/\\n/\r\n/g;
1023 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1027 my $err = $mail->send($out);
1028 $self->error($self->cleanup . "$err") if ($err);
1032 $self->{OUT} = $out;
1034 my $numbytes = (-s $self->{tmpfile});
1035 open(IN, $self->{tmpfile})
1036 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1038 $self->{copies} = 1 unless $self->{media} eq 'printer';
1040 chdir("$self->{cwd}");
1042 for my $i (1 .. $self->{copies}) {
1044 open(OUT, $self->{OUT})
1045 or $self->error($self->cleanup . "$self->{OUT} : $!");
1048 # launch application
1049 print qq|Content-Type: application/$self->{format}
1050 Content-Disposition: attachment; filename="$self->{tmpfile}"
1051 Content-Length: $numbytes
1055 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1075 chdir("$self->{cwd}");
1076 $main::lxdebug->leave_sub();
1080 $main::lxdebug->enter_sub();
1084 chdir("$self->{tmpdir}");
1087 if (-f "$self->{tmpfile}.err") {
1088 open(FH, "$self->{tmpfile}.err");
1093 if ($self->{tmpfile}) {
1096 $self->{tmpfile} =~ s/\.\w+$//g;
1097 my $tmpfile = $self->{tmpfile};
1098 unlink(<$tmpfile.*>);
1101 chdir("$self->{cwd}");
1103 $main::lxdebug->leave_sub();
1109 $main::lxdebug->enter_sub();
1111 my ($self, @fields) = @_;
1114 %unique_fields = map({ $_ => 1 } @fields);
1115 @fields = keys(%unique_fields);
1117 foreach my $field (@fields) {
1118 next unless ($self->{$field} =~ /\<pagebreak\>/);
1119 $self->{$field} =~ s/\<pagebreak\>//g;
1120 if ($field =~ /.*_(\d+)$/) {
1121 if (!$self->{"_forced_pagebreaks"}) {
1122 $self->{"_forced_pagebreaks"} = [];
1124 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1128 my $format = $self->{format};
1129 if ($self->{format} =~ /(postscript|pdf)/) {
1136 '<', '>', quotemeta('\n'), '
1140 '&', quotemeta('\n'), '
1142 '"', '\$', '%', '_', '#', quotemeta('^'),
1143 '{', '}', '<', '>', '£', "\r"
1149 quotemeta('\n') => '<br>',
1160 quotemeta('^') => '\^\\',
1165 quotemeta('\n') => '\newline ',
1172 foreach my $key (@{ $replace{order}{$format} }) {
1173 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1176 # Allow some HTML markup to be converted into the output format's
1177 # corresponding markup code, e.g. bold or italic.
1178 if ('html' eq $format) {
1179 my @markup_replace = ('b', 'i', 's', 'u');
1181 foreach my $key (@markup_replace) {
1182 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1185 } elsif ('tex' eq $format) {
1186 my %markup_replace = ('b' => 'textbf',
1188 'u' => 'underline');
1190 foreach my $field (@fields) {
1191 foreach my $key (keys(%markup_replace)) {
1192 my $new = $markup_replace{$key};
1194 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1199 $main::lxdebug->leave_sub();
1203 $main::lxdebug->enter_sub();
1205 my ($self, $date, $myconfig) = @_;
1207 if ($date && $date =~ /\D/) {
1209 if ($myconfig->{dateformat} =~ /^yy/) {
1210 ($yy, $mm, $dd) = split /\D/, $date;
1212 if ($myconfig->{dateformat} =~ /^mm/) {
1213 ($mm, $dd, $yy) = split /\D/, $date;
1215 if ($myconfig->{dateformat} =~ /^dd/) {
1216 ($dd, $mm, $yy) = split /\D/, $date;
1221 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1222 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1224 $dd = "0$dd" if ($dd < 10);
1225 $mm = "0$mm" if ($mm < 10);
1227 $date = "$yy$mm$dd";
1230 $main::lxdebug->leave_sub();
1235 # Database routines used throughout
1238 $main::lxdebug->enter_sub();
1240 my ($self, $myconfig) = @_;
1242 # connect to database
1244 DBI->connect($myconfig->{dbconnect},
1245 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1249 if ($myconfig->{dboptions}) {
1250 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1253 $main::lxdebug->leave_sub();
1258 sub dbconnect_noauto {
1259 $main::lxdebug->enter_sub();
1261 my ($self, $myconfig) = @_;
1263 # connect to database
1265 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1266 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1270 if ($myconfig->{dboptions}) {
1271 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1274 $main::lxdebug->leave_sub();
1279 sub update_balance {
1280 $main::lxdebug->enter_sub();
1282 my ($self, $dbh, $table, $field, $where, $value) = @_;
1284 # if we have a value, go do it
1287 # retrieve balance from table
1288 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1289 my $sth = $dbh->prepare($query);
1291 $sth->execute || $self->dberror($query);
1292 my ($balance) = $sth->fetchrow_array;
1298 $query = "UPDATE $table SET $field = $balance WHERE $where";
1299 $dbh->do($query) || $self->dberror($query);
1301 $main::lxdebug->leave_sub();
1304 sub update_exchangerate {
1305 $main::lxdebug->enter_sub();
1307 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1309 # some sanity check for currency
1311 $main::lxdebug->leave_sub();
1315 my $query = qq|SELECT e.curr FROM exchangerate e
1316 WHERE e.curr = '$curr'
1317 AND e.transdate = '$transdate'
1319 my $sth = $dbh->prepare($query);
1320 $sth->execute || $self->dberror($query);
1323 if ($buy != 0 && $sell != 0) {
1324 $set = "buy = $buy, sell = $sell";
1325 } elsif ($buy != 0) {
1326 $set = "buy = $buy";
1327 } elsif ($sell != 0) {
1328 $set = "sell = $sell";
1331 if ($sth->fetchrow_array) {
1332 $query = qq|UPDATE exchangerate
1334 WHERE curr = '$curr'
1335 AND transdate = '$transdate'|;
1337 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1338 VALUES ('$curr', $buy, $sell, '$transdate')|;
1341 $dbh->do($query) || $self->dberror($query);
1343 $main::lxdebug->leave_sub();
1346 sub save_exchangerate {
1347 $main::lxdebug->enter_sub();
1349 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1351 my $dbh = $self->dbconnect($myconfig);
1353 my ($buy, $sell) = (0, 0);
1354 $buy = $rate if $fld eq 'buy';
1355 $sell = $rate if $fld eq 'sell';
1357 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1361 $main::lxdebug->leave_sub();
1364 sub get_exchangerate {
1365 $main::lxdebug->enter_sub();
1367 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1369 unless ($transdate) {
1370 $main::lxdebug->leave_sub();
1374 my $query = qq|SELECT e.$fld FROM exchangerate e
1375 WHERE e.curr = '$curr'
1376 AND e.transdate = '$transdate'|;
1377 my $sth = $dbh->prepare($query);
1378 $sth->execute || $self->dberror($query);
1380 my ($exchangerate) = $sth->fetchrow_array;
1383 $main::lxdebug->leave_sub();
1385 return $exchangerate;
1388 sub check_exchangerate {
1389 $main::lxdebug->enter_sub();
1391 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1393 unless ($transdate) {
1394 $main::lxdebug->leave_sub();
1398 my $dbh = $self->dbconnect($myconfig);
1400 my $query = qq|SELECT e.$fld FROM exchangerate e
1401 WHERE e.curr = '$currency'
1402 AND e.transdate = '$transdate'|;
1403 my $sth = $dbh->prepare($query);
1404 $sth->execute || $self->dberror($query);
1406 my ($exchangerate) = $sth->fetchrow_array;
1410 $main::lxdebug->leave_sub();
1412 return $exchangerate;
1416 $main::lxdebug->enter_sub();
1418 my ($self, $dbh, $id) = @_;
1422 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1424 if ($self->{"shipto$item"}) {
1425 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1427 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1432 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1433 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1434 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1435 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1436 '$self->{shiptozipcode}', '$self->{shiptocity}',
1437 '$self->{shiptocountry}', '$self->{shiptocontact}',
1438 '$self->{shiptophone}', '$self->{shiptofax}',
1439 '$self->{shiptoemail}')|;
1440 $dbh->do($query) || $self->dberror($query);
1443 $main::lxdebug->leave_sub();
1447 $main::lxdebug->enter_sub();
1449 my ($self, $dbh) = @_;
1451 my $query = qq|SELECT e.id, e.name FROM employee e
1452 WHERE e.login = '$self->{login}'|;
1453 my $sth = $dbh->prepare($query);
1454 $sth->execute || $self->dberror($query);
1456 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1457 $self->{employee_id} *= 1;
1461 $main::lxdebug->leave_sub();
1464 # get other contact for transaction and form - html/tex
1466 $main::lxdebug->enter_sub();
1468 my ($self, $dbh, $id) = @_;
1470 my $query = qq|SELECT c.*
1473 $sth = $dbh->prepare($query);
1474 $sth->execute || $self->dberror($query);
1476 $ref = $sth->fetchrow_hashref(NAME_lc);
1478 push @{ $self->{$_} }, $ref;
1481 $main::lxdebug->leave_sub();
1484 # get contacts for id, if no contact return {"","","","",""}
1486 $main::lxdebug->enter_sub();
1488 my ($self, $dbh, $id) = @_;
1490 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1492 WHERE cp_cv_id=$id|;
1493 my $sth = $dbh->prepare($query);
1494 $sth->execute || $self->dberror($query);
1497 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1498 push @{ $self->{all_contacts} }, $ref;
1503 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1506 $main::lxdebug->leave_sub();
1509 # this sub gets the id and name from $table
1511 $main::lxdebug->enter_sub();
1513 my ($self, $myconfig, $table) = @_;
1515 # connect to database
1516 my $dbh = $self->dbconnect($myconfig);
1518 my $name = $self->like(lc $self->{$table});
1519 my $customernumber = $self->like(lc $self->{customernumber});
1521 if ($self->{customernumber} ne "") {
1522 $query = qq~SELECT c.id, c.name,
1523 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1525 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1528 $query = qq~SELECT c.id, c.name,
1529 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1531 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1535 if ($self->{openinvoices}) {
1536 $query = qq~SELECT DISTINCT c.id, c.name,
1537 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1538 FROM $self->{arap} a
1539 JOIN $table c ON (a.${table}_id = c.id)
1540 WHERE NOT a.amount = a.paid
1541 AND lower(c.name) LIKE '$name'
1544 my $sth = $dbh->prepare($query);
1546 $sth->execute || $self->dberror($query);
1549 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1550 push(@{ $self->{name_list} }, $ref);
1556 $main::lxdebug->leave_sub();
1561 # the selection sub is used in the AR, AP, IS, IR and OE module
1564 $main::lxdebug->enter_sub();
1566 my ($self, $myconfig, $table, $module) = @_;
1569 my $dbh = $self->dbconnect($myconfig);
1571 my $query = qq|SELECT count(*) FROM $table|;
1572 my $sth = $dbh->prepare($query);
1573 $sth->execute || $self->dberror($query);
1574 my ($count) = $sth->fetchrow_array;
1577 # build selection list
1578 if ($count < $myconfig->{vclimit}) {
1579 $query = qq|SELECT id, name
1580 FROM $table WHERE not obsolete
1582 $sth = $dbh->prepare($query);
1583 $sth->execute || $self->dberror($query);
1585 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1586 push @{ $self->{"all_$table"} }, $ref;
1594 $self->get_employee($dbh);
1596 # setup sales contacts
1597 $query = qq|SELECT e.id, e.name
1600 AND NOT e.id = $self->{employee_id}|;
1601 $sth = $dbh->prepare($query);
1602 $sth->execute || $self->dberror($query);
1604 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1605 push @{ $self->{all_employees} }, $ref;
1610 push @{ $self->{all_employees} },
1611 { id => $self->{employee_id},
1612 name => $self->{employee} };
1614 # sort the whole thing
1615 @{ $self->{all_employees} } =
1616 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1618 if ($module eq 'AR') {
1620 # prepare query for departments
1621 $query = qq|SELECT d.id, d.description
1627 $query = qq|SELECT d.id, d.description
1632 $sth = $dbh->prepare($query);
1633 $sth->execute || $self->dberror($query);
1635 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1636 push @{ $self->{all_departments} }, $ref;
1641 $main::lxdebug->leave_sub();
1644 # this is only used for reports
1645 sub all_departments {
1646 $main::lxdebug->enter_sub();
1648 my ($self, $myconfig, $table) = @_;
1650 my $dbh = $self->dbconnect($myconfig);
1651 my $where = "1 = 1";
1653 if (defined $table) {
1654 if ($table eq 'customer') {
1655 $where = " d.role = 'P'";
1659 my $query = qq|SELECT d.id, d.description
1663 my $sth = $dbh->prepare($query);
1664 $sth->execute || $self->dberror($query);
1666 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1667 push @{ $self->{all_departments} }, $ref;
1673 $main::lxdebug->leave_sub();
1677 $main::lxdebug->enter_sub();
1679 my ($self, $module, $myconfig, $table) = @_;
1681 $self->all_vc($myconfig, $table, $module);
1683 # get last customers or vendors
1686 my $dbh = $self->dbconnect($myconfig);
1690 # now get the account numbers
1691 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1693 WHERE c.link LIKE '%$module%'
1696 $sth = $dbh->prepare($query);
1697 $sth->execute || $self->dberror($query);
1699 $self->{accounts} = "";
1700 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1702 foreach my $key (split /:/, $ref->{link}) {
1703 if ($key =~ /$module/) {
1705 # cross reference for keys
1706 $xkeyref{ $ref->{accno} } = $key;
1708 push @{ $self->{"${module}_links"}{$key} },
1709 { accno => $ref->{accno},
1710 description => $ref->{description},
1711 taxkey => $ref->{taxkey_id} };
1713 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1719 if (($module eq "AP") || ($module eq "AR")) {
1721 # get tax rates and description
1722 $query = qq| SELECT * FROM tax t|;
1723 $sth = $dbh->prepare($query);
1724 $sth->execute || $self->dberror($query);
1726 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1727 push @{ $self->{TAX} }, $ref;
1733 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1735 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1736 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1737 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1738 c.name AS $table, a.department_id, d.description AS department,
1739 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1740 a.employee_id, e.name AS employee, a.gldate
1742 JOIN $table c ON (a.${table}_id = c.id)
1743 LEFT JOIN employee e ON (e.id = a.employee_id)
1744 LEFT JOIN department d ON (d.id = a.department_id)
1745 WHERE a.id = $self->{id}|;
1746 $sth = $dbh->prepare($query);
1747 $sth->execute || $self->dberror($query);
1749 $ref = $sth->fetchrow_hashref(NAME_lc);
1750 foreach $key (keys %$ref) {
1751 $self->{$key} = $ref->{$key};
1755 # get amounts from individual entries
1756 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1757 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1759 JOIN chart c ON (c.id = a.chart_id)
1760 LEFT JOIN project p ON (p.id = a.project_id)
1761 LEFT Join tax t ON (a.taxkey = t.taxkey)
1762 WHERE a.trans_id = $self->{id}
1763 AND a.fx_transaction = '0'
1764 ORDER BY a.oid,a.transdate|;
1765 $sth = $dbh->prepare($query);
1766 $sth->execute || $self->dberror($query);
1768 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1770 # get exchangerate for currency
1771 $self->{exchangerate} =
1772 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1776 # store amounts in {acc_trans}{$key} for multiple accounts
1777 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1778 $ref->{exchangerate} =
1779 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1781 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1784 $ref->{index} = $index;
1786 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1790 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1791 (SELECT c.accno FROM chart c
1792 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1793 (SELECT c.accno FROM chart c
1794 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1796 $sth = $dbh->prepare($query);
1797 $sth->execute || $self->dberror($query);
1799 $ref = $sth->fetchrow_hashref(NAME_lc);
1800 map { $self->{$_} = $ref->{$_} } keys %$ref;
1806 $query = qq|SELECT current_date AS transdate,
1807 d.curr AS currencies, d.closedto, d.revtrans,
1808 (SELECT c.accno FROM chart c
1809 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1810 (SELECT c.accno FROM chart c
1811 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1813 $sth = $dbh->prepare($query);
1814 $sth->execute || $self->dberror($query);
1816 $ref = $sth->fetchrow_hashref(NAME_lc);
1817 map { $self->{$_} = $ref->{$_} } keys %$ref;
1820 if ($self->{"$self->{vc}_id"}) {
1822 # only setup currency
1823 ($self->{currency}) = split /:/, $self->{currencies};
1827 $self->lastname_used($dbh, $myconfig, $table, $module);
1829 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1831 # get exchangerate for currency
1832 $self->{exchangerate} =
1833 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1842 $main::lxdebug->leave_sub();
1846 $main::lxdebug->enter_sub();
1848 my ($self, $dbh, $myconfig, $table, $module) = @_;
1850 my $arap = ($table eq 'customer') ? "ar" : "ap";
1851 my $where = "1 = 1";
1853 if ($self->{type} =~ /_order/) {
1855 $where = "quotation = '0'";
1857 if ($self->{type} =~ /_quotation/) {
1859 $where = "quotation = '1'";
1862 my $query = qq|SELECT MAX(id) FROM $arap
1864 AND ${table}_id > 0|;
1865 my $sth = $dbh->prepare($query);
1866 $sth->execute || $self->dberror($query);
1868 my ($trans_id) = $sth->fetchrow_array;
1872 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1873 current_date + ct.terms AS duedate, a.department_id,
1874 d.description AS department
1876 JOIN $table ct ON (a.${table}_id = ct.id)
1877 LEFT JOIN department d ON (a.department_id = d.id)
1878 WHERE a.id = $trans_id|;
1879 $sth = $dbh->prepare($query);
1880 $sth->execute || $self->dberror($query);
1882 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1883 $self->{duedate}, $self->{department_id}, $self->{department})
1884 = $sth->fetchrow_array;
1887 $main::lxdebug->leave_sub();
1891 $main::lxdebug->enter_sub();
1893 my ($self, $myconfig, $thisdate, $days) = @_;
1895 my $dbh = $self->dbconnect($myconfig);
1900 my $dateformat = $myconfig->{dateformat};
1901 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1903 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1905 $sth = $dbh->prepare($query);
1906 $sth->execute || $self->dberror($query);
1908 $query = qq|SELECT current_date AS thisdate
1910 $sth = $dbh->prepare($query);
1911 $sth->execute || $self->dberror($query);
1914 ($thisdate) = $sth->fetchrow_array;
1919 $main::lxdebug->leave_sub();
1925 $main::lxdebug->enter_sub();
1927 my ($self, $string) = @_;
1929 if ($string !~ /%/) {
1930 $string = "%$string%";
1933 $string =~ s/\'/\'\'/g;
1935 $main::lxdebug->leave_sub();
1941 $main::lxdebug->enter_sub();
1943 my ($self, $flds, $new, $count, $numrows) = @_;
1947 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1953 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1955 $j = $item->{ndx} - 1;
1956 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1960 for $i ($count + 1 .. $numrows) {
1961 map { delete $self->{"${_}_$i"} } @{$flds};
1964 $main::lxdebug->leave_sub();
1968 $main::lxdebug->enter_sub();
1970 my ($self, $myconfig) = @_;
1974 my $dbh = $self->dbconnect_noauto($myconfig);
1976 my $query = qq|DELETE FROM status
1977 WHERE formname = '$self->{formname}'
1979 my $sth = $dbh->prepare($query) || $self->dberror($query);
1981 if ($self->{formname} =~ /(check|receipt)/) {
1982 for $i (1 .. $self->{rowcount}) {
1983 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1987 $sth->execute($self->{id}) || $self->dberror($query);
1991 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1992 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1994 my %queued = split / /, $self->{queued};
1996 if ($self->{formname} =~ /(check|receipt)/) {
1998 # this is a check or receipt, add one entry for each lineitem
1999 my ($accno) = split /--/, $self->{account};
2000 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2001 chart_id) VALUES (?, '$printed',
2002 '$queued{$self->{formname}}', '$self->{prinform}',
2003 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2004 $sth = $dbh->prepare($query) || $self->dberror($query);
2006 for $i (1 .. $self->{rowcount}) {
2007 if ($self->{"checked_$i"}) {
2008 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2013 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2014 spoolfile, formname)
2015 VALUES ($self->{id}, '$printed', '$emailed',
2016 '$queued{$self->{formname}}', '$self->{formname}')|;
2017 $dbh->do($query) || $self->dberror($query);
2023 $main::lxdebug->leave_sub();
2027 $main::lxdebug->enter_sub();
2029 my ($self, $dbh) = @_;
2031 my ($query, $printed, $emailed);
2033 my $formnames = $self->{printed};
2034 my $emailforms = $self->{emailed};
2036 my $query = qq|DELETE FROM status
2037 WHERE formname = '$self->{formname}'
2038 AND trans_id = $self->{id}|;
2039 $dbh->do($query) || $self->dberror($query);
2041 # this only applies to the forms
2042 # checks and receipts are posted when printed or queued
2044 if ($self->{queued}) {
2045 my %queued = split / /, $self->{queued};
2047 foreach my $formname (keys %queued) {
2048 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2049 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2051 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2052 spoolfile, formname)
2053 VALUES ($self->{id}, '$printed', '$emailed',
2054 '$queued{$formname}', '$formname')|;
2055 $dbh->do($query) || $self->dberror($query);
2057 $formnames =~ s/$self->{formname}//;
2058 $emailforms =~ s/$self->{formname}//;
2063 # save printed, emailed info
2064 $formnames =~ s/^ +//g;
2065 $emailforms =~ s/^ +//g;
2068 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2069 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2071 foreach my $formname (keys %status) {
2072 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2073 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2075 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2076 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2077 $dbh->do($query) || $self->dberror($query);
2080 $main::lxdebug->leave_sub();
2083 sub update_defaults {
2084 $main::lxdebug->enter_sub();
2086 my ($self, $myconfig, $fld) = @_;
2088 my $dbh = $self->dbconnect_noauto($myconfig);
2089 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2090 my $sth = $dbh->prepare($query);
2092 $sth->execute || $self->dberror($query);
2093 my ($var) = $sth->fetchrow_array;
2098 $query = qq|UPDATE defaults
2100 $dbh->do($query) || $form->dberror($query);
2105 $main::lxdebug->leave_sub();
2110 sub update_business {
2111 $main::lxdebug->enter_sub();
2113 my ($self, $myconfig, $business_id) = @_;
2115 my $dbh = $self->dbconnect_noauto($myconfig);
2117 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2118 my $sth = $dbh->prepare($query);
2120 $sth->execute || $self->dberror($query);
2121 my ($var) = $sth->fetchrow_array;
2126 $query = qq|UPDATE business
2127 SET customernumberinit = '$var' WHERE id=$business_id|;
2128 $dbh->do($query) || $form->dberror($query);
2133 $main::lxdebug->leave_sub();
2139 $main::lxdebug->enter_sub();
2141 my ($self, $myconfig, $salesman) = @_;
2143 my $dbh = $self->dbconnect($myconfig);
2145 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2146 my $sth = $dbh->prepare($query);
2147 $sth->execute || $self->dberror($query);
2150 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2151 push(@{ $self->{salesman_list} }, $ref);
2155 $main::lxdebug->leave_sub();
2160 sub get_partsgroup {
2161 $main::lxdebug->enter_sub();
2163 my ($self, $myconfig, $p) = @_;
2165 my $dbh = $self->dbconnect($myconfig);
2167 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2169 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2171 if ($p->{searchitems} eq 'part') {
2173 WHERE p.inventory_accno_id > 0|;
2175 if ($p->{searchitems} eq 'service') {
2177 WHERE p.inventory_accno_id IS NULL|;
2179 if ($p->{searchitems} eq 'assembly') {
2181 WHERE p.assembly = '1'|;
2183 if ($p->{searchitems} eq 'labor') {
2185 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2189 ORDER BY partsgroup|;
2192 $query = qq|SELECT id, partsgroup FROM partsgroup
2193 ORDER BY partsgroup|;
2196 if ($p->{language_code}) {
2197 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2198 t.description AS translation
2200 JOIN parts p ON (p.partsgroup_id = pg.id)
2201 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2202 ORDER BY translation|;
2205 my $sth = $dbh->prepare($query);
2206 $sth->execute || $self->dberror($query);
2208 $self->{all_partsgroup} = ();
2209 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2210 push @{ $self->{all_partsgroup} }, $ref;
2214 $main::lxdebug->leave_sub();
2217 sub get_pricegroup {
2218 $main::lxdebug->enter_sub();
2220 my ($self, $myconfig, $p) = @_;
2222 my $dbh = $self->dbconnect($myconfig);
2224 my $query = qq|SELECT p.id, p.pricegroup
2228 ORDER BY pricegroup|;
2231 $query = qq|SELECT id, pricegroup FROM pricegroup
2232 ORDER BY pricegroup|;
2235 my $sth = $dbh->prepare($query);
2236 $sth->execute || $self->dberror($query);
2238 $self->{all_pricegroup} = ();
2239 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2240 push @{ $self->{all_pricegroup} }, $ref;
2245 $main::lxdebug->leave_sub();
2249 my ($self, $dbh, $myconfig, $audittrail) = @_;
2251 # table, $reference, $formname, $action, $id, $transdate) = @_;
2258 $dbh = $self->dbconnect($myconfig);
2262 # if we have an id add audittrail, otherwise get a new timestamp
2264 if ($audittrail->{id}) {
2266 $query = qq|SELECT audittrail FROM defaults|;
2268 if ($dbh->selectrow_array($query)) {
2269 my ($null, $employee_id) = $self->get_employee($dbh);
2271 if ($self->{audittrail} && !$myconfig) {
2272 chop $self->{audittrail};
2274 my @a = split /\|/, $self->{audittrail};
2278 my @flds = qw(tablename reference formname action transdate);
2280 # put into hash and remove dups
2282 $key = "$a[2]$a[3]";
2284 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2288 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2289 formname, action, employee_id, transdate)
2290 VALUES ($audittrail->{id}, ?, ?,
2291 ?, ?, $employee_id, ?)|;
2292 my $sth = $dbh->prepare($query) || $self->dberror($query);
2296 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2300 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2302 $sth->execute || $self->dberror;
2307 if ($audittrail->{transdate}) {
2308 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2309 formname, action, employee_id, transdate) VALUES (
2310 $audittrail->{id}, '$audittrail->{tablename}', |
2311 . $dbh->quote($audittrail->{reference}) . qq|,
2312 '$audittrail->{formname}', '$audittrail->{action}',
2313 $employee_id, '$audittrail->{transdate}')|;
2315 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2316 formname, action, employee_id) VALUES ($audittrail->{id},
2317 '$audittrail->{tablename}', |
2318 . $dbh->quote($audittrail->{reference}) . qq|,
2319 '$audittrail->{formname}', '$audittrail->{action}',
2326 $query = qq|SELECT current_timestamp FROM defaults|;
2327 my ($timestamp) = $dbh->selectrow_array($query);
2330 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2333 $dbh->disconnect if $disconnect;
2342 $main::lxdebug->enter_sub();
2344 my ($type, $country, $NLS_file) = @_;
2348 if ($country && -d "locale/$country") {
2349 $self->{countrycode} = $country;
2350 eval { require "locale/$country/$NLS_file"; };
2353 $self->{NLS_file} = $NLS_file;
2355 push @{ $self->{LONG_MONTH} },
2356 ("January", "February", "March", "April",
2357 "May ", "June", "July", "August",
2358 "September", "October", "November", "December");
2359 push @{ $self->{SHORT_MONTH} },
2360 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2362 $main::lxdebug->leave_sub();
2368 my ($self, $text) = @_;
2370 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2374 $main::lxdebug->enter_sub();
2376 my ($self, $text) = @_;
2378 if (exists $self{subs}{$text}) {
2379 $text = $self{subs}{$text};
2381 if ($self->{countrycode} && $self->{NLS_file}) {
2383 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2387 $main::lxdebug->leave_sub();
2393 $main::lxdebug->enter_sub();
2395 my ($self, $myconfig, $date, $longformat) = @_;
2398 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2403 $spc = $myconfig->{dateformat};
2405 $spc = substr($spc, 1, 1);
2407 if ($date =~ /\D/) {
2408 if ($myconfig->{dateformat} =~ /^yy/) {
2409 ($yy, $mm, $dd) = split /\D/, $date;
2411 if ($myconfig->{dateformat} =~ /^mm/) {
2412 ($mm, $dd, $yy) = split /\D/, $date;
2414 if ($myconfig->{dateformat} =~ /^dd/) {
2415 ($dd, $mm, $yy) = split /\D/, $date;
2418 $date = substr($date, 2);
2419 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2424 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2425 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2427 if ($myconfig->{dateformat} =~ /^dd/) {
2428 if (defined $longformat && $longformat == 0) {
2430 $dd = "0$dd" if ($dd < 10);
2431 $mm = "0$mm" if ($mm < 10);
2432 $longdate = "$dd$spc$mm$spc$yy";
2435 $longdate .= ($spc eq '.') ? ". " : " ";
2436 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2438 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2440 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2441 # Lx-Office is mainly used in Germany or German speaking countries.
2442 if (defined $longformat && $longformat == 0) {
2444 $dd = "0$dd" if ($dd < 10);
2445 $mm = "0$mm" if ($mm < 10);
2446 $longdate = "$yy-$mm-$dd";
2448 $longdate = "$dd. ";
2449 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2452 if (defined $longformat && $longformat == 0) {
2454 $dd = "0$dd" if ($dd < 10);
2455 $mm = "0$mm" if ($mm < 10);
2456 $longdate = "$mm$spc$dd$spc$yy";
2458 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2464 $main::lxdebug->leave_sub();
2470 $main::lxdebug->enter_sub();
2472 my ($self, $myconfig, $date, $longformat) = @_;
2475 $main::lxdebug->leave_sub();
2480 $spc = $myconfig->{dateformat};
2482 $spc = substr($spc, 1, 1);
2484 if ($date =~ /\D/) {
2485 if ($myconfig->{dateformat} =~ /^yy/) {
2486 ($yy, $mm, $dd) = split /\D/, $date;
2487 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2488 ($mm, $dd, $yy) = split /\D/, $date;
2489 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2490 ($dd, $mm, $yy) = split /\D/, $date;
2493 $date = substr($date, 2);
2494 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2499 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2500 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2502 $main::lxdebug->leave_sub();
2503 return ($yy, $mm, $dd);