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) = @_;
420 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
421 $language = $main::language;
423 $language = $main::myconfig{"countrycode"};
426 if (-f "templates/webpages/${file}_${language}.html") {
427 if ((-f ".developer") &&
428 (-f "templates/webpages/${file}_master.html") &&
429 ((stat("templates/webpages/${file}_master.html"))[9] >
430 (stat("templates/webpages/${file}_${language}.html"))[9])) {
431 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
432 "Please re-run 'locales.pl' in 'locale/${language}'.";
433 print(qq|<pre>$info</pre>|);
437 $file = "templates/webpages/${file}_${language}.html";
438 } elsif (-f "templates/webpages/${file}.html") {
439 $file = "templates/webpages/${file}.html";
441 my $info = "Web page template '${file}' not found.\n" .
442 "Please re-run 'locales.pl' in 'locale/${language}'.";
443 print(qq|<pre>$info</pre>|);
447 my $template = HTML::Template->new("filename" => $file,
448 "die_on_bad_params" => 0,
450 "case_sensitive" => 1,
451 "loop_context_vars" => 1,
454 $additional_params = {} unless ($additional_params);
455 if ($self->{"DEBUG"}) {
456 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
459 if ($additional_params->{"DEBUG"}) {
460 $additional_params->{"DEBUG"} =
461 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
464 my @additional_param_names = keys(%{$additional_params});
466 foreach my $key ($template->param()) {
467 if (grep(/^${key}$/, @additional_param_names)) {
468 $template->param($key => $additional_params->{$key});
470 $template->param($key => $self->{$key});
474 my $output = $template->output();
476 $main::lxdebug->leave_sub();
481 sub show_generic_error {
482 my ($self, $error, $title) = @_;
485 $add_params->{"title"} = $title if ($title);
486 $self->{"label_error"} = $error;
488 print($self->parse_html_template("generic/error", $add_params));
491 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
492 # changed it to accept an arbitrary number of triggers - sschoeling
494 $main::lxdebug->enter_sub();
497 my $myconfig = shift;
500 # set dateform for jsscript
502 $ifFormat = "%d.%m.%Y";
503 if ($myconfig->{dateformat} eq "dd.mm.yy") {
504 $ifFormat = "%d.%m.%Y";
506 if ($myconfig->{dateformat} eq "dd-mm-yy") {
507 $ifFormat = "%d-%m-%Y";
509 if ($myconfig->{dateformat} eq "dd/mm/yy") {
510 $ifFormat = "%d/%m/%Y";
512 if ($myconfig->{dateformat} eq "mm/dd/yy") {
513 $ifFormat = "%m/%d/%Y";
515 if ($myconfig->{dateformat} eq "mm-dd-yy") {
516 $ifFormat = "%m-%d-%Y";
518 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
519 $ifFormat = "%Y-%m-%d";
531 inputField : "| . (shift) . qq|",
532 ifFormat :"$ifFormat",
533 align : "| . (shift) . qq|",
534 button : "| . (shift) . qq|"
540 <script type="text/javascript">
541 <!--| . join("", @triggers) . qq|//-->
545 $main::lxdebug->leave_sub();
548 } #end sub write_trigger
551 $main::lxdebug->enter_sub();
553 my ($self, $msg) = @_;
555 if ($self->{callback}) {
557 ($script, $argv) = split(/\?/, $self->{callback});
558 exec("perl", "$script", $argv);
566 $main::lxdebug->leave_sub();
569 # sort of columns removed - empty sub
571 $main::lxdebug->enter_sub();
573 my ($self, @columns) = @_;
575 $main::lxdebug->leave_sub();
581 $main::lxdebug->enter_sub();
583 my ($self, $myconfig, $amount, $places, $dash) = @_;
585 #Workaround for $format_amount calls without $places
586 if (!defined $places) {
587 (my $dec) = ($amount =~ /\.(\d+)/);
588 $places = length $dec;
591 if ($places =~ /\d/) {
592 $amount = $self->round_amount($amount, $places);
595 # is the amount negative
596 my $negative = ($amount < 0);
600 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
602 my ($whole, $dec) = split /\./, "$amount";
604 $amount = join '', reverse split //, $whole;
605 $fillup = "0" x ($places - length($dec));
607 if ($myconfig->{numberformat} eq '1,000.00') {
608 $amount =~ s/\d{3,}?/$&,/g;
610 $amount = join '', reverse split //, $amount;
611 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
614 if ($myconfig->{numberformat} eq '1.000,00') {
615 $amount =~ s/\d{3,}?/$&./g;
617 $amount = join '', reverse split //, $amount;
618 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
621 if ($myconfig->{numberformat} eq '1000,00') {
623 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
627 $amount = ($negative) ? "($amount)" : "$amount";
628 } elsif ($dash =~ /DRCR/) {
629 $amount = ($negative) ? "$amount DR" : "$amount CR";
631 $amount = ($negative) ? "-$amount" : "$amount";
635 if ($dash eq "0" && $places) {
636 if ($myconfig->{numberformat} eq '1.000,00') {
637 $amount = "0" . "," . "0" x $places;
639 $amount = "0" . "." . "0" x $places;
642 $amount = ($dash ne "") ? "$dash" : "0";
646 $main::lxdebug->leave_sub();
652 $main::lxdebug->enter_sub();
654 my ($self, $myconfig, $amount) = @_;
655 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
657 if ($myconfig->{in_numberformat} == 1) {
659 # Extra input number format 1000.00 or 1000,00
660 $main::lxdebug->message(LXDebug::DEBUG2,
661 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
664 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
665 $amount = scalar reverse $amount;
667 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
668 $amount =~ s/\./DOT/;
670 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
673 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
674 $amount =~ s/DOT/\./;
676 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
677 $amount = scalar reverse $amount;
678 $main::lxdebug->message(LXDebug::DEBUG2,
679 "Parsed amount:" . $amount . "\n");
681 return ($amount * 1);
684 $main::lxdebug->message(LXDebug::DEBUG2,
685 "in_numberformat: " . $main::locale->text('equal Outputformat'));
686 $main::lxdebug->message(LXDebug::DEBUG2,
687 " = numberformat: $myconfig->{numberformat}");
688 if ( ($myconfig->{numberformat} eq '1.000,00')
689 || ($myconfig->{numberformat} eq '1000,00')) {
694 if ($myconfig->{numberformat} eq "1'000.00") {
700 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
702 $main::lxdebug->leave_sub();
704 return ($amount * 1);
708 $main::lxdebug->enter_sub();
710 my ($self, $amount, $places) = @_;
713 # Rounding like "Kaufmannsrunden"
714 # Descr. http://de.wikipedia.org/wiki/Rundung
716 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
719 $amount = $amount * (10**($places));
720 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
722 $main::lxdebug->leave_sub();
724 return $round_amount;
729 $main::lxdebug->enter_sub();
731 my ($self, $myconfig, $userspath) = @_;
734 # Some variables used for page breaks
735 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
737 my ($current_page, $current_line, $current_row) = (1, 1, 0);
743 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
744 $self->format_string(grep(/notes/, keys(%{$self})));
746 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
747 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
749 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
750 qw(email tel fax name signature));
752 open(IN, "$self->{templates}/$self->{IN}")
753 or $self->error("$self->{IN} : $!");
758 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
760 # OUT is used for the media, screen, printer, email
761 # for postscript we store a copy in a temporary file
763 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
764 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
766 $self->{OUT} = ">$self->{tmpfile}";
770 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
772 open(OUT, ">-") or $self->error("STDOUT : $!");
776 # Do we have to run LaTeX two times? This is needed if
777 # the template contains page references.
780 # first we generate a tmpfile
781 # read file and replace <%variable%>
787 # Switch <%analyse%> for template checking
788 # If <%analyse%> is set in the template, you'll find the
789 # parsed output in the user Directory for analysing
791 # <%analyse%> is a switch (allways off, on if set), not a Variable
792 # Set $form->{analysing}="" for system state: never analyse.
793 # Set $form->{analysing}="1" for system state: ever analyse.
794 $self->{analysing} = "1" if (/<%analyse%>/ && !defined $self->{analysing});
796 $two_passes = 1 if (/\\pageref/);
799 # detect pagebreak block and its parameters
800 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
801 $chars_per_line = $1;
802 $lines_on_first_page = $2;
803 $lines_on_second_page = $3;
806 last if (/\s*<%end pagebreak%>/);
813 if (/\s*<%foreach /) {
815 # this one we need for the count
817 $var =~ s/\s*<%foreach (.+?)%>/$1/;
819 last if (/\s*<%end /);
825 # display contents of $self->{number}[] array
826 for $i (0 .. $#{ $self->{$var} }) {
829 # Try to detect whether a manual page break is necessary
830 # but only if there was a <%pagebreak ...%> block before
832 if ($chars_per_line) {
834 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
837 my $_description = $self->{"description"}[$i];
838 while ($_description =~ /\\newline/) {
840 $_description =~ s/\\newline//;
842 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
844 if ($current_page == 1) {
845 $lpp = $lines_on_first_page;
847 $lpp = $lines_on_second_page;
850 # Yes we need a manual page break -- or the user has forced one
852 (($current_line + $lines) > $lpp)
853 || ($self->{"_forced_pagebreaks"}
854 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
858 # replace the special variables <%sumcarriedforward%>
861 my $psum = $self->format_amount($myconfig, $sum, 2);
862 $pb =~ s/<%sumcarriedforward%>/$psum/g;
863 $pb =~ s/<%lastpage%>/$current_page/g;
865 # only "normal" variables are supported here
866 # (no <%if, no <%foreach, no <%include)
868 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
870 # page break block is ready to rock
875 $current_line += $lines;
878 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
882 # don't parse par, we need it for each line
884 s/<%(.+?)%>/$self->{$1}[$i]/mg;
890 # if not comes before if!
891 if (/\s*<%if not /) {
893 # check if it is not set and display
895 s/\s*<%if not (.+?)%>/$1/;
897 unless ($self->{$_}) {
899 last if (/\s*<%end /);
909 last if (/\s*<%end /);
917 # check if it is set and display
919 s/\s*<%if (.+?)%>/$1/;
923 last if (/\s*<%end /);
933 last if (/\s*<%end /);
939 # check for <%include filename%>
940 if (/\s*<%include /) {
944 $var =~ s/\s*<%include (.+?)%>/$1/;
947 $var =~ s/(\/|\.\.)//g;
949 # prevent the infinite loop!
950 next if ($self->{"$var"});
952 open(INC, "$self->{templates}/$var")
953 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
962 s/<%(.+?)%>/$self->{$1}/g;
963 s/<nobr><\/nobr>/ /g;
970 # Convert the tex file to postscript
971 if ($self->{format} =~ /(postscript|pdf)/) {
974 $self->{cwd} = cwd();
975 $self->{tmpdir} = "$self->{cwd}/$userspath";
977 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
979 $self->{tmpfile} =~ s/$userspath\///g;
981 if ($self->{format} eq 'postscript') {
983 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
985 $self->error($self->cleanup) if ($?);
988 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
990 $self->error($self->cleanup) if ($?);
993 $self->{tmpfile} =~ s/tex$/dvi/;
995 system("dvips $self->{tmpfile} -o -q > /dev/null");
996 $self->error($self->cleanup . "dvips : $!") if ($?);
997 $self->{tmpfile} =~ s/dvi$/ps/;
999 if ($self->{format} eq 'pdf') {
1001 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1003 $self->error($self->cleanup) if ($?);
1006 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1008 $self->error($self->cleanup) if ($?);
1010 $self->{tmpfile} =~ s/tex$/pdf/;
1015 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
1017 if ($self->{media} eq 'email') {
1021 my $mail = new Mailer;
1023 map { $mail->{$_} = $self->{$_} }
1024 qw(cc bcc subject message version format charset);
1025 $mail->{to} = qq|$self->{email}|;
1026 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1027 $mail->{fileid} = "$fileid.";
1029 # if we send html or plain text inline
1030 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1031 $mail->{contenttype} = "text/html";
1033 $mail->{message} =~ s/\r\n/<br>\n/g;
1034 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1035 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1037 open(IN, $self->{tmpfile})
1038 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1040 $mail->{message} .= $_;
1047 @{ $mail->{attachments} } = ($self->{tmpfile});
1049 $myconfig->{signature} =~ s/\\n/\r\n/g;
1050 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1054 my $err = $mail->send($out);
1055 $self->error($self->cleanup . "$err") if ($err);
1059 $self->{OUT} = $out;
1061 my $numbytes = (-s $self->{tmpfile});
1062 open(IN, $self->{tmpfile})
1063 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1065 $self->{copies} = 1 unless $self->{media} eq 'printer';
1067 chdir("$self->{cwd}");
1069 for my $i (1 .. $self->{copies}) {
1071 open(OUT, $self->{OUT})
1072 or $self->error($self->cleanup . "$self->{OUT} : $!");
1075 # launch application
1076 print qq|Content-Type: application/$self->{format}
1077 Content-Disposition: attachment; filename="$self->{tmpfile}"
1078 Content-Length: $numbytes
1082 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1102 chdir("$self->{cwd}");
1103 $main::lxdebug->leave_sub();
1107 $main::lxdebug->enter_sub();
1111 chdir("$self->{tmpdir}");
1114 if (-f "$self->{tmpfile}.err") {
1115 open(FH, "$self->{tmpfile}.err");
1120 if ($self->{analysing} eq "") {
1121 if ($self->{tmpfile}) {
1124 $self->{tmpfile} =~ s/\.\w+$//g;
1125 my $tmpfile = $self->{tmpfile};
1126 unlink(<$tmpfile.*>);
1130 chdir("$self->{cwd}");
1132 $main::lxdebug->leave_sub();
1138 $main::lxdebug->enter_sub();
1140 my ($self, @fields) = @_;
1143 %unique_fields = map({ $_ => 1 } @fields);
1144 @fields = keys(%unique_fields);
1146 foreach my $field (@fields) {
1147 next unless ($self->{$field} =~ /\<pagebreak\>/);
1148 $self->{$field} =~ s/\<pagebreak\>//g;
1149 if ($field =~ /.*_(\d+)$/) {
1150 if (!$self->{"_forced_pagebreaks"}) {
1151 $self->{"_forced_pagebreaks"} = [];
1153 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1157 my $format = $self->{format};
1158 if ($self->{format} =~ /(postscript|pdf)/) {
1165 '<', '>', quotemeta('\n'), '
1169 '&', quotemeta('\n'), '
1171 '"', '\$', '%', '_', '#', quotemeta('^'),
1172 '{', '}', '<', '>', '£', "\r"
1178 quotemeta('\n') => '<br>',
1189 quotemeta('^') => '\^\\',
1194 quotemeta('\n') => '\newline ',
1201 foreach my $key (@{ $replace{order}{$format} }) {
1202 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1205 # Allow some HTML markup to be converted into the output format's
1206 # corresponding markup code, e.g. bold or italic.
1207 if ('html' eq $format) {
1208 my @markup_replace = ('b', 'i', 's', 'u');
1210 foreach my $key (@markup_replace) {
1211 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1214 } elsif ('tex' eq $format) {
1215 my %markup_replace = ('b' => 'textbf',
1217 'u' => 'underline');
1219 foreach my $field (@fields) {
1220 foreach my $key (keys(%markup_replace)) {
1221 my $new = $markup_replace{$key};
1223 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1228 $main::lxdebug->leave_sub();
1232 $main::lxdebug->enter_sub();
1234 my ($self, $date, $myconfig) = @_;
1236 if ($date && $date =~ /\D/) {
1238 if ($myconfig->{dateformat} =~ /^yy/) {
1239 ($yy, $mm, $dd) = split /\D/, $date;
1241 if ($myconfig->{dateformat} =~ /^mm/) {
1242 ($mm, $dd, $yy) = split /\D/, $date;
1244 if ($myconfig->{dateformat} =~ /^dd/) {
1245 ($dd, $mm, $yy) = split /\D/, $date;
1250 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1251 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1253 $dd = "0$dd" if ($dd < 10);
1254 $mm = "0$mm" if ($mm < 10);
1256 $date = "$yy$mm$dd";
1259 $main::lxdebug->leave_sub();
1264 # Database routines used throughout
1267 $main::lxdebug->enter_sub();
1269 my ($self, $myconfig) = @_;
1271 # connect to database
1273 DBI->connect($myconfig->{dbconnect},
1274 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1278 if ($myconfig->{dboptions}) {
1279 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1282 $main::lxdebug->leave_sub();
1287 sub dbconnect_noauto {
1288 $main::lxdebug->enter_sub();
1290 my ($self, $myconfig) = @_;
1292 # connect to database
1294 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1295 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1299 if ($myconfig->{dboptions}) {
1300 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1303 $main::lxdebug->leave_sub();
1308 sub update_balance {
1309 $main::lxdebug->enter_sub();
1311 my ($self, $dbh, $table, $field, $where, $value) = @_;
1313 # if we have a value, go do it
1316 # retrieve balance from table
1317 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1318 my $sth = $dbh->prepare($query);
1320 $sth->execute || $self->dberror($query);
1321 my ($balance) = $sth->fetchrow_array;
1327 $query = "UPDATE $table SET $field = $balance WHERE $where";
1328 $dbh->do($query) || $self->dberror($query);
1330 $main::lxdebug->leave_sub();
1333 sub update_exchangerate {
1334 $main::lxdebug->enter_sub();
1336 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1338 # some sanity check for currency
1340 $main::lxdebug->leave_sub();
1344 my $query = qq|SELECT e.curr FROM exchangerate e
1345 WHERE e.curr = '$curr'
1346 AND e.transdate = '$transdate'
1348 my $sth = $dbh->prepare($query);
1349 $sth->execute || $self->dberror($query);
1352 if ($buy != 0 && $sell != 0) {
1353 $set = "buy = $buy, sell = $sell";
1354 } elsif ($buy != 0) {
1355 $set = "buy = $buy";
1356 } elsif ($sell != 0) {
1357 $set = "sell = $sell";
1360 if ($sth->fetchrow_array) {
1361 $query = qq|UPDATE exchangerate
1363 WHERE curr = '$curr'
1364 AND transdate = '$transdate'|;
1366 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1367 VALUES ('$curr', $buy, $sell, '$transdate')|;
1370 $dbh->do($query) || $self->dberror($query);
1372 $main::lxdebug->leave_sub();
1375 sub save_exchangerate {
1376 $main::lxdebug->enter_sub();
1378 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1380 my $dbh = $self->dbconnect($myconfig);
1382 my ($buy, $sell) = (0, 0);
1383 $buy = $rate if $fld eq 'buy';
1384 $sell = $rate if $fld eq 'sell';
1386 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1390 $main::lxdebug->leave_sub();
1393 sub get_exchangerate {
1394 $main::lxdebug->enter_sub();
1396 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1398 unless ($transdate) {
1399 $main::lxdebug->leave_sub();
1403 my $query = qq|SELECT e.$fld FROM exchangerate e
1404 WHERE e.curr = '$curr'
1405 AND e.transdate = '$transdate'|;
1406 my $sth = $dbh->prepare($query);
1407 $sth->execute || $self->dberror($query);
1409 my ($exchangerate) = $sth->fetchrow_array;
1412 $main::lxdebug->leave_sub();
1414 return $exchangerate;
1417 sub check_exchangerate {
1418 $main::lxdebug->enter_sub();
1420 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1422 unless ($transdate) {
1423 $main::lxdebug->leave_sub();
1427 my $dbh = $self->dbconnect($myconfig);
1429 my $query = qq|SELECT e.$fld FROM exchangerate e
1430 WHERE e.curr = '$currency'
1431 AND e.transdate = '$transdate'|;
1432 my $sth = $dbh->prepare($query);
1433 $sth->execute || $self->dberror($query);
1435 my ($exchangerate) = $sth->fetchrow_array;
1439 $main::lxdebug->leave_sub();
1441 return $exchangerate;
1445 $main::lxdebug->enter_sub();
1447 my ($self, $dbh, $id) = @_;
1451 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1453 if ($self->{"shipto$item"}) {
1454 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1456 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1461 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1462 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1463 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1464 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1465 '$self->{shiptozipcode}', '$self->{shiptocity}',
1466 '$self->{shiptocountry}', '$self->{shiptocontact}',
1467 '$self->{shiptophone}', '$self->{shiptofax}',
1468 '$self->{shiptoemail}')|;
1469 $dbh->do($query) || $self->dberror($query);
1472 $main::lxdebug->leave_sub();
1476 $main::lxdebug->enter_sub();
1478 my ($self, $dbh) = @_;
1480 my $query = qq|SELECT e.id, e.name FROM employee e
1481 WHERE e.login = '$self->{login}'|;
1482 my $sth = $dbh->prepare($query);
1483 $sth->execute || $self->dberror($query);
1485 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1486 $self->{employee_id} *= 1;
1490 $main::lxdebug->leave_sub();
1493 # get other contact for transaction and form - html/tex
1495 $main::lxdebug->enter_sub();
1497 my ($self, $dbh, $id) = @_;
1499 my $query = qq|SELECT c.*
1502 $sth = $dbh->prepare($query);
1503 $sth->execute || $self->dberror($query);
1505 $ref = $sth->fetchrow_hashref(NAME_lc);
1507 push @{ $self->{$_} }, $ref;
1510 $main::lxdebug->leave_sub();
1513 # get contacts for id, if no contact return {"","","","",""}
1515 $main::lxdebug->enter_sub();
1517 my ($self, $dbh, $id) = @_;
1519 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1521 WHERE cp_cv_id=$id|;
1522 my $sth = $dbh->prepare($query);
1523 $sth->execute || $self->dberror($query);
1526 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1527 push @{ $self->{all_contacts} }, $ref;
1532 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1535 $main::lxdebug->leave_sub();
1538 # this sub gets the id and name from $table
1540 $main::lxdebug->enter_sub();
1542 my ($self, $myconfig, $table) = @_;
1544 # connect to database
1545 my $dbh = $self->dbconnect($myconfig);
1547 my $name = $self->like(lc $self->{$table});
1548 my $customernumber = $self->like(lc $self->{customernumber});
1550 if ($self->{customernumber} ne "") {
1551 $query = qq~SELECT c.id, c.name,
1552 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1554 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1557 $query = qq~SELECT c.id, c.name,
1558 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1560 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1564 if ($self->{openinvoices}) {
1565 $query = qq~SELECT DISTINCT c.id, c.name,
1566 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1567 FROM $self->{arap} a
1568 JOIN $table c ON (a.${table}_id = c.id)
1569 WHERE NOT a.amount = a.paid
1570 AND lower(c.name) LIKE '$name'
1573 my $sth = $dbh->prepare($query);
1575 $sth->execute || $self->dberror($query);
1578 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1579 push(@{ $self->{name_list} }, $ref);
1585 $main::lxdebug->leave_sub();
1590 # the selection sub is used in the AR, AP, IS, IR and OE module
1593 $main::lxdebug->enter_sub();
1595 my ($self, $myconfig, $table, $module) = @_;
1598 my $dbh = $self->dbconnect($myconfig);
1600 my $query = qq|SELECT count(*) FROM $table|;
1601 my $sth = $dbh->prepare($query);
1602 $sth->execute || $self->dberror($query);
1603 my ($count) = $sth->fetchrow_array;
1606 # build selection list
1607 if ($count < $myconfig->{vclimit}) {
1608 $query = qq|SELECT id, name
1609 FROM $table WHERE not obsolete
1611 $sth = $dbh->prepare($query);
1612 $sth->execute || $self->dberror($query);
1614 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1615 push @{ $self->{"all_$table"} }, $ref;
1623 $self->get_employee($dbh);
1625 # setup sales contacts
1626 $query = qq|SELECT e.id, e.name
1629 AND NOT e.id = $self->{employee_id}|;
1630 $sth = $dbh->prepare($query);
1631 $sth->execute || $self->dberror($query);
1633 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1634 push @{ $self->{all_employees} }, $ref;
1639 push @{ $self->{all_employees} },
1640 { id => $self->{employee_id},
1641 name => $self->{employee} };
1643 # sort the whole thing
1644 @{ $self->{all_employees} } =
1645 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1647 if ($module eq 'AR') {
1649 # prepare query for departments
1650 $query = qq|SELECT d.id, d.description
1656 $query = qq|SELECT d.id, d.description
1661 $sth = $dbh->prepare($query);
1662 $sth->execute || $self->dberror($query);
1664 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1665 push @{ $self->{all_departments} }, $ref;
1670 $main::lxdebug->leave_sub();
1673 # this is only used for reports
1674 sub all_departments {
1675 $main::lxdebug->enter_sub();
1677 my ($self, $myconfig, $table) = @_;
1679 my $dbh = $self->dbconnect($myconfig);
1680 my $where = "1 = 1";
1682 if (defined $table) {
1683 if ($table eq 'customer') {
1684 $where = " d.role = 'P'";
1688 my $query = qq|SELECT d.id, d.description
1692 my $sth = $dbh->prepare($query);
1693 $sth->execute || $self->dberror($query);
1695 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1696 push @{ $self->{all_departments} }, $ref;
1702 $main::lxdebug->leave_sub();
1706 $main::lxdebug->enter_sub();
1708 my ($self, $module, $myconfig, $table) = @_;
1710 $self->all_vc($myconfig, $table, $module);
1712 # get last customers or vendors
1715 my $dbh = $self->dbconnect($myconfig);
1719 # now get the account numbers
1720 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1722 WHERE c.link LIKE '%$module%'
1725 $sth = $dbh->prepare($query);
1726 $sth->execute || $self->dberror($query);
1728 $self->{accounts} = "";
1729 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1731 foreach my $key (split /:/, $ref->{link}) {
1732 if ($key =~ /$module/) {
1734 # cross reference for keys
1735 $xkeyref{ $ref->{accno} } = $key;
1737 push @{ $self->{"${module}_links"}{$key} },
1738 { accno => $ref->{accno},
1739 description => $ref->{description},
1740 taxkey => $ref->{taxkey_id} };
1742 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1748 if (($module eq "AP") || ($module eq "AR")) {
1750 # get tax rates and description
1751 $query = qq| SELECT * FROM tax t|;
1752 $sth = $dbh->prepare($query);
1753 $sth->execute || $self->dberror($query);
1755 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1756 push @{ $self->{TAX} }, $ref;
1762 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1764 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1765 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1766 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1767 c.name AS $table, a.department_id, d.description AS department,
1768 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1769 a.employee_id, e.name AS employee, a.gldate
1771 JOIN $table c ON (a.${table}_id = c.id)
1772 LEFT JOIN employee e ON (e.id = a.employee_id)
1773 LEFT JOIN department d ON (d.id = a.department_id)
1774 WHERE a.id = $self->{id}|;
1775 $sth = $dbh->prepare($query);
1776 $sth->execute || $self->dberror($query);
1778 $ref = $sth->fetchrow_hashref(NAME_lc);
1779 foreach $key (keys %$ref) {
1780 $self->{$key} = $ref->{$key};
1784 # get amounts from individual entries
1785 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1786 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1788 JOIN chart c ON (c.id = a.chart_id)
1789 LEFT JOIN project p ON (p.id = a.project_id)
1790 LEFT Join tax t ON (a.taxkey = t.taxkey)
1791 WHERE a.trans_id = $self->{id}
1792 AND a.fx_transaction = '0'
1793 ORDER BY a.oid,a.transdate|;
1794 $sth = $dbh->prepare($query);
1795 $sth->execute || $self->dberror($query);
1797 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1799 # get exchangerate for currency
1800 $self->{exchangerate} =
1801 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1805 # store amounts in {acc_trans}{$key} for multiple accounts
1806 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1807 $ref->{exchangerate} =
1808 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1810 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1813 $ref->{index} = $index;
1815 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1819 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1820 (SELECT c.accno FROM chart c
1821 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1822 (SELECT c.accno FROM chart c
1823 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1825 $sth = $dbh->prepare($query);
1826 $sth->execute || $self->dberror($query);
1828 $ref = $sth->fetchrow_hashref(NAME_lc);
1829 map { $self->{$_} = $ref->{$_} } keys %$ref;
1835 $query = qq|SELECT current_date AS transdate,
1836 d.curr AS currencies, d.closedto, d.revtrans,
1837 (SELECT c.accno FROM chart c
1838 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1839 (SELECT c.accno FROM chart c
1840 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1842 $sth = $dbh->prepare($query);
1843 $sth->execute || $self->dberror($query);
1845 $ref = $sth->fetchrow_hashref(NAME_lc);
1846 map { $self->{$_} = $ref->{$_} } keys %$ref;
1849 if ($self->{"$self->{vc}_id"}) {
1851 # only setup currency
1852 ($self->{currency}) = split /:/, $self->{currencies};
1856 $self->lastname_used($dbh, $myconfig, $table, $module);
1858 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1860 # get exchangerate for currency
1861 $self->{exchangerate} =
1862 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1871 $main::lxdebug->leave_sub();
1875 $main::lxdebug->enter_sub();
1877 my ($self, $dbh, $myconfig, $table, $module) = @_;
1879 my $arap = ($table eq 'customer') ? "ar" : "ap";
1880 my $where = "1 = 1";
1882 if ($self->{type} =~ /_order/) {
1884 $where = "quotation = '0'";
1886 if ($self->{type} =~ /_quotation/) {
1888 $where = "quotation = '1'";
1891 my $query = qq|SELECT MAX(id) FROM $arap
1893 AND ${table}_id > 0|;
1894 my $sth = $dbh->prepare($query);
1895 $sth->execute || $self->dberror($query);
1897 my ($trans_id) = $sth->fetchrow_array;
1901 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1902 current_date + ct.terms AS duedate, a.department_id,
1903 d.description AS department
1905 JOIN $table ct ON (a.${table}_id = ct.id)
1906 LEFT JOIN department d ON (a.department_id = d.id)
1907 WHERE a.id = $trans_id|;
1908 $sth = $dbh->prepare($query);
1909 $sth->execute || $self->dberror($query);
1911 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1912 $self->{duedate}, $self->{department_id}, $self->{department})
1913 = $sth->fetchrow_array;
1916 $main::lxdebug->leave_sub();
1920 $main::lxdebug->enter_sub();
1922 my ($self, $myconfig, $thisdate, $days) = @_;
1924 my $dbh = $self->dbconnect($myconfig);
1929 my $dateformat = $myconfig->{dateformat};
1930 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1932 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1934 $sth = $dbh->prepare($query);
1935 $sth->execute || $self->dberror($query);
1937 $query = qq|SELECT current_date AS thisdate
1939 $sth = $dbh->prepare($query);
1940 $sth->execute || $self->dberror($query);
1943 ($thisdate) = $sth->fetchrow_array;
1948 $main::lxdebug->leave_sub();
1954 $main::lxdebug->enter_sub();
1956 my ($self, $string) = @_;
1958 if ($string !~ /%/) {
1959 $string = "%$string%";
1962 $string =~ s/\'/\'\'/g;
1964 $main::lxdebug->leave_sub();
1970 $main::lxdebug->enter_sub();
1972 my ($self, $flds, $new, $count, $numrows) = @_;
1976 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1982 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1984 $j = $item->{ndx} - 1;
1985 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1989 for $i ($count + 1 .. $numrows) {
1990 map { delete $self->{"${_}_$i"} } @{$flds};
1993 $main::lxdebug->leave_sub();
1997 $main::lxdebug->enter_sub();
1999 my ($self, $myconfig) = @_;
2003 my $dbh = $self->dbconnect_noauto($myconfig);
2005 my $query = qq|DELETE FROM status
2006 WHERE formname = '$self->{formname}'
2008 my $sth = $dbh->prepare($query) || $self->dberror($query);
2010 if ($self->{formname} =~ /(check|receipt)/) {
2011 for $i (1 .. $self->{rowcount}) {
2012 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2016 $sth->execute($self->{id}) || $self->dberror($query);
2020 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2021 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2023 my %queued = split / /, $self->{queued};
2025 if ($self->{formname} =~ /(check|receipt)/) {
2027 # this is a check or receipt, add one entry for each lineitem
2028 my ($accno) = split /--/, $self->{account};
2029 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2030 chart_id) VALUES (?, '$printed',
2031 '$queued{$self->{formname}}', '$self->{prinform}',
2032 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2033 $sth = $dbh->prepare($query) || $self->dberror($query);
2035 for $i (1 .. $self->{rowcount}) {
2036 if ($self->{"checked_$i"}) {
2037 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2042 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2043 spoolfile, formname)
2044 VALUES ($self->{id}, '$printed', '$emailed',
2045 '$queued{$self->{formname}}', '$self->{formname}')|;
2046 $dbh->do($query) || $self->dberror($query);
2052 $main::lxdebug->leave_sub();
2056 $main::lxdebug->enter_sub();
2058 my ($self, $dbh) = @_;
2060 my ($query, $printed, $emailed);
2062 my $formnames = $self->{printed};
2063 my $emailforms = $self->{emailed};
2065 my $query = qq|DELETE FROM status
2066 WHERE formname = '$self->{formname}'
2067 AND trans_id = $self->{id}|;
2068 $dbh->do($query) || $self->dberror($query);
2070 # this only applies to the forms
2071 # checks and receipts are posted when printed or queued
2073 if ($self->{queued}) {
2074 my %queued = split / /, $self->{queued};
2076 foreach my $formname (keys %queued) {
2077 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2078 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2080 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2081 spoolfile, formname)
2082 VALUES ($self->{id}, '$printed', '$emailed',
2083 '$queued{$formname}', '$formname')|;
2084 $dbh->do($query) || $self->dberror($query);
2086 $formnames =~ s/$self->{formname}//;
2087 $emailforms =~ s/$self->{formname}//;
2092 # save printed, emailed info
2093 $formnames =~ s/^ +//g;
2094 $emailforms =~ s/^ +//g;
2097 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2098 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2100 foreach my $formname (keys %status) {
2101 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2102 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2104 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2105 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2106 $dbh->do($query) || $self->dberror($query);
2109 $main::lxdebug->leave_sub();
2112 sub update_defaults {
2113 $main::lxdebug->enter_sub();
2115 my ($self, $myconfig, $fld) = @_;
2117 my $dbh = $self->dbconnect_noauto($myconfig);
2118 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2119 my $sth = $dbh->prepare($query);
2121 $sth->execute || $self->dberror($query);
2122 my ($var) = $sth->fetchrow_array;
2127 $query = qq|UPDATE defaults
2129 $dbh->do($query) || $form->dberror($query);
2134 $main::lxdebug->leave_sub();
2139 sub update_business {
2140 $main::lxdebug->enter_sub();
2142 my ($self, $myconfig, $business_id) = @_;
2144 my $dbh = $self->dbconnect_noauto($myconfig);
2146 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2147 my $sth = $dbh->prepare($query);
2149 $sth->execute || $self->dberror($query);
2150 my ($var) = $sth->fetchrow_array;
2155 $query = qq|UPDATE business
2156 SET customernumberinit = '$var' WHERE id=$business_id|;
2157 $dbh->do($query) || $form->dberror($query);
2162 $main::lxdebug->leave_sub();
2168 $main::lxdebug->enter_sub();
2170 my ($self, $myconfig, $salesman) = @_;
2172 my $dbh = $self->dbconnect($myconfig);
2174 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2175 my $sth = $dbh->prepare($query);
2176 $sth->execute || $self->dberror($query);
2179 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2180 push(@{ $self->{salesman_list} }, $ref);
2184 $main::lxdebug->leave_sub();
2189 sub get_partsgroup {
2190 $main::lxdebug->enter_sub();
2192 my ($self, $myconfig, $p) = @_;
2194 my $dbh = $self->dbconnect($myconfig);
2196 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2198 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2200 if ($p->{searchitems} eq 'part') {
2202 WHERE p.inventory_accno_id > 0|;
2204 if ($p->{searchitems} eq 'service') {
2206 WHERE p.inventory_accno_id IS NULL|;
2208 if ($p->{searchitems} eq 'assembly') {
2210 WHERE p.assembly = '1'|;
2212 if ($p->{searchitems} eq 'labor') {
2214 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2218 ORDER BY partsgroup|;
2221 $query = qq|SELECT id, partsgroup FROM partsgroup
2222 ORDER BY partsgroup|;
2225 if ($p->{language_code}) {
2226 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2227 t.description AS translation
2229 JOIN parts p ON (p.partsgroup_id = pg.id)
2230 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2231 ORDER BY translation|;
2234 my $sth = $dbh->prepare($query);
2235 $sth->execute || $self->dberror($query);
2237 $self->{all_partsgroup} = ();
2238 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2239 push @{ $self->{all_partsgroup} }, $ref;
2243 $main::lxdebug->leave_sub();
2246 sub get_pricegroup {
2247 $main::lxdebug->enter_sub();
2249 my ($self, $myconfig, $p) = @_;
2251 my $dbh = $self->dbconnect($myconfig);
2253 my $query = qq|SELECT p.id, p.pricegroup
2257 ORDER BY pricegroup|;
2260 $query = qq|SELECT id, pricegroup FROM pricegroup
2261 ORDER BY pricegroup|;
2264 my $sth = $dbh->prepare($query);
2265 $sth->execute || $self->dberror($query);
2267 $self->{all_pricegroup} = ();
2268 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2269 push @{ $self->{all_pricegroup} }, $ref;
2274 $main::lxdebug->leave_sub();
2278 my ($self, $dbh, $myconfig, $audittrail) = @_;
2280 # table, $reference, $formname, $action, $id, $transdate) = @_;
2287 $dbh = $self->dbconnect($myconfig);
2291 # if we have an id add audittrail, otherwise get a new timestamp
2293 if ($audittrail->{id}) {
2295 $query = qq|SELECT audittrail FROM defaults|;
2297 if ($dbh->selectrow_array($query)) {
2298 my ($null, $employee_id) = $self->get_employee($dbh);
2300 if ($self->{audittrail} && !$myconfig) {
2301 chop $self->{audittrail};
2303 my @a = split /\|/, $self->{audittrail};
2307 my @flds = qw(tablename reference formname action transdate);
2309 # put into hash and remove dups
2311 $key = "$a[2]$a[3]";
2313 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2317 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2318 formname, action, employee_id, transdate)
2319 VALUES ($audittrail->{id}, ?, ?,
2320 ?, ?, $employee_id, ?)|;
2321 my $sth = $dbh->prepare($query) || $self->dberror($query);
2325 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2329 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2331 $sth->execute || $self->dberror;
2336 if ($audittrail->{transdate}) {
2337 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2338 formname, action, employee_id, transdate) VALUES (
2339 $audittrail->{id}, '$audittrail->{tablename}', |
2340 . $dbh->quote($audittrail->{reference}) . qq|,
2341 '$audittrail->{formname}', '$audittrail->{action}',
2342 $employee_id, '$audittrail->{transdate}')|;
2344 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2345 formname, action, employee_id) VALUES ($audittrail->{id},
2346 '$audittrail->{tablename}', |
2347 . $dbh->quote($audittrail->{reference}) . qq|,
2348 '$audittrail->{formname}', '$audittrail->{action}',
2355 $query = qq|SELECT current_timestamp FROM defaults|;
2356 my ($timestamp) = $dbh->selectrow_array($query);
2359 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2362 $dbh->disconnect if $disconnect;
2371 $main::lxdebug->enter_sub();
2373 my ($type, $country, $NLS_file) = @_;
2377 if ($country && -d "locale/$country") {
2378 $self->{countrycode} = $country;
2379 eval { require "locale/$country/$NLS_file"; };
2382 $self->{NLS_file} = $NLS_file;
2384 push @{ $self->{LONG_MONTH} },
2385 ("January", "February", "March", "April",
2386 "May ", "June", "July", "August",
2387 "September", "October", "November", "December");
2388 push @{ $self->{SHORT_MONTH} },
2389 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2391 $main::lxdebug->leave_sub();
2397 my ($self, $text) = @_;
2399 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2403 $main::lxdebug->enter_sub();
2405 my ($self, $text) = @_;
2407 if (exists $self{subs}{$text}) {
2408 $text = $self{subs}{$text};
2410 if ($self->{countrycode} && $self->{NLS_file}) {
2412 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2416 $main::lxdebug->leave_sub();
2422 $main::lxdebug->enter_sub();
2424 my ($self, $myconfig, $date, $longformat) = @_;
2427 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2432 $spc = $myconfig->{dateformat};
2434 $spc = substr($spc, 1, 1);
2436 if ($date =~ /\D/) {
2437 if ($myconfig->{dateformat} =~ /^yy/) {
2438 ($yy, $mm, $dd) = split /\D/, $date;
2440 if ($myconfig->{dateformat} =~ /^mm/) {
2441 ($mm, $dd, $yy) = split /\D/, $date;
2443 if ($myconfig->{dateformat} =~ /^dd/) {
2444 ($dd, $mm, $yy) = split /\D/, $date;
2447 $date = substr($date, 2);
2448 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2453 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2454 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2456 if ($myconfig->{dateformat} =~ /^dd/) {
2457 if (defined $longformat && $longformat == 0) {
2459 $dd = "0$dd" if ($dd < 10);
2460 $mm = "0$mm" if ($mm < 10);
2461 $longdate = "$dd$spc$mm$spc$yy";
2464 $longdate .= ($spc eq '.') ? ". " : " ";
2465 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2467 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2469 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2470 # Lx-Office is mainly used in Germany or German speaking countries.
2471 if (defined $longformat && $longformat == 0) {
2473 $dd = "0$dd" if ($dd < 10);
2474 $mm = "0$mm" if ($mm < 10);
2475 $longdate = "$yy-$mm-$dd";
2477 $longdate = "$dd. ";
2478 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2481 if (defined $longformat && $longformat == 0) {
2483 $dd = "0$dd" if ($dd < 10);
2484 $mm = "0$mm" if ($mm < 10);
2485 $longdate = "$mm$spc$dd$spc$yy";
2487 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2493 $main::lxdebug->leave_sub();
2499 $main::lxdebug->enter_sub();
2501 my ($self, $myconfig, $date, $longformat) = @_;
2504 $main::lxdebug->leave_sub();
2509 $spc = $myconfig->{dateformat};
2511 $spc = substr($spc, 1, 1);
2513 if ($date =~ /\D/) {
2514 if ($myconfig->{dateformat} =~ /^yy/) {
2515 ($yy, $mm, $dd) = split /\D/, $date;
2516 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2517 ($mm, $dd, $yy) = split /\D/, $date;
2518 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2519 ($dd, $mm, $yy) = split /\D/, $date;
2522 $date = substr($date, 2);
2523 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2528 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2529 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2531 $main::lxdebug->leave_sub();
2532 return ($yy, $mm, $dd);