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 #======================================================================
44 $main::lxdebug->enter_sub(2);
48 my @pairs = split(/&/, $input);
51 my ($name, $value) = split(/=/, $_, 2);
52 $in{$name} = unescape(undef, $value);
55 $main::lxdebug->leave_sub(2);
60 sub _request_to_hash {
61 $main::lxdebug->enter_sub(2);
64 my ($i, $loc, $key, $val);
65 my (%ATTACH, $f, $header, $header_body, $len, $buf);
66 my ($boundary, @list, $size, $body, $x, $blah, $name);
68 if ($ENV{'CONTENT_TYPE'}
69 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
70 $boundary = quotemeta('--' . $1);
71 @list = split(/$boundary/, $input);
73 # For some reason there are always 2 extra, that are empty
76 for ($x = 1; $x <= $size; $x++) {
77 $header_body = $list[$x];
78 $header_body =~ /\r\n\r\n|\n\n/;
80 # Here we split the header and body
85 # Now we try to get the file name
87 $name =~ /name=\"(.+)\"/;
88 ($name, $blah) = split(/\"/, $1);
90 # If the form name is not attach, then we need to parse this like
92 if ($name ne "attach") {
93 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
94 $ATTACH{$name} = $body;
96 # Otherwise it is an attachment and we need to finish it up
97 } elsif ($name eq "attach") {
98 $header =~ /filename=\"(.+)\"/;
99 $ATTACH{'FILE_NAME'} = $1;
100 $ATTACH{'FILE_NAME'} =~ s/\"//g;
101 $ATTACH{'FILE_NAME'} =~ s/\s//g;
102 $ATTACH{'FILE_CONTENT'} = $body;
104 for ($i = $x; $list[$i]; $i++) {
105 $list[$i] =~ s/^.+name=$//;
106 $list[$i] =~ /\"(\w+)\"/;
112 $main::lxdebug->leave_sub(2);
116 $main::lxdebug->leave_sub(2);
117 return _input_to_hash($input);
122 $main::lxdebug->enter_sub();
128 read(STDIN, $_, $ENV{CONTENT_LENGTH});
130 if ($ENV{QUERY_STRING}) {
131 $_ = $ENV{QUERY_STRING};
138 my %parameters = _request_to_hash($_);
139 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
141 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
143 $self->{action} = lc $self->{action};
144 $self->{action} =~ s/( |-|,|#)/_/g;
146 $self->{version} = "2.3.0";
148 $main::lxdebug->leave_sub();
154 $main::lxdebug->enter_sub();
160 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
162 $main::lxdebug->leave_sub();
166 $main::lxdebug->enter_sub(2);
168 my ($self, $str, $beenthere) = @_;
170 # for Apache 2 we escape strings twice
171 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
172 # $str = $self->escape($str, 1);
175 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
177 $main::lxdebug->leave_sub(2);
183 $main::lxdebug->enter_sub(2);
185 my ($self, $str) = @_;
190 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
192 $main::lxdebug->leave_sub(2);
198 my ($self, $str) = @_;
200 if ($str && !ref($str)) {
201 $str =~ s/\"/"/g;
209 my ($self, $str) = @_;
211 if ($str && !ref($str)) {
212 $str =~ s/"/\"/g;
224 print qq|<input type=hidden name="$_" value="|
225 . $self->quote($self->{$_})
229 delete $self->{header};
230 for (sort keys %$self) {
231 print qq|<input type=hidden name="$_" value="|
232 . $self->quote($self->{$_})
240 $main::lxdebug->enter_sub();
242 my ($self, $msg) = @_;
244 if ($ENV{HTTP_USER_AGENT}) {
246 $self->show_generic_error($msg);
250 if ($self->{error_function}) {
251 &{ $self->{error_function} }($msg);
257 $main::lxdebug->leave_sub();
261 $main::lxdebug->enter_sub();
263 my ($self, $msg) = @_;
265 if ($ENV{HTTP_USER_AGENT}) {
268 if (!$self->{header}) {
281 if ($self->{info_function}) {
282 &{ $self->{info_function} }($msg);
288 $main::lxdebug->leave_sub();
292 $main::lxdebug->enter_sub();
294 my ($self, $str, $cols, $maxrows) = @_;
298 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
300 $maxrows = $rows unless defined $maxrows;
302 $main::lxdebug->leave_sub();
304 return ($rows > $maxrows) ? $maxrows : $rows;
308 $main::lxdebug->enter_sub();
310 my ($self, $msg) = @_;
312 $self->error("$msg\n" . $DBI::errstr);
314 $main::lxdebug->leave_sub();
318 $main::lxdebug->enter_sub();
320 my ($self, $name, $msg) = @_;
322 if ($self->{$name} =~ /^\s*$/) {
325 $main::lxdebug->leave_sub();
329 $main::lxdebug->enter_sub();
333 if ($self->{header}) {
334 $main::lxdebug->leave_sub();
338 my ($stylesheet, $favicon, $charset);
340 if ($ENV{HTTP_USER_AGENT}) {
342 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
344 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
348 if ($self->{favicon} && (-f "$self->{favicon}")) {
350 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
354 if ($self->{charset}) {
356 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
359 if ($self->{landscape}) {
360 $pagelayout = qq|<style type="text/css">
361 \@page { size:landscape; }
364 if ($self->{fokus}) {
365 $fokus = qq|<script type="text/javascript">
367 function fokus(){document.$self->{fokus}.focus();}
374 if ($self->{jsscript} == 1) {
377 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
378 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
379 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
380 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
387 ? "$self->{title} - $self->{titlebar}"
390 print qq|Content-Type: text/html
394 <title>$self->{titlebar}</title>
407 $main::lxdebug->leave_sub();
410 sub parse_html_template {
411 $main::lxdebug->enter_sub();
413 my ($self, $file, $additional_params) = @_;
416 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
417 $language = $main::language;
419 $language = $main::myconfig{"countrycode"};
422 if (-f "templates/webpages/${file}_${language}.html") {
423 if ((-f ".developer") &&
424 (-f "templates/webpages/${file}_master.html") &&
425 ((stat("templates/webpages/${file}_master.html"))[9] >
426 (stat("templates/webpages/${file}_${language}.html"))[9])) {
427 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
428 "Please re-run 'locales.pl' in 'locale/${language}'.";
429 print(qq|<pre>$info</pre>|);
433 $file = "templates/webpages/${file}_${language}.html";
434 } elsif (-f "templates/webpages/${file}.html") {
435 $file = "templates/webpages/${file}.html";
437 my $info = "Web page template '${file}' not found.\n" .
438 "Please re-run 'locales.pl' in 'locale/${language}'.";
439 print(qq|<pre>$info</pre>|);
443 my $template = HTML::Template->new("filename" => $file,
444 "die_on_bad_params" => 0,
446 "case_sensitive" => 1,
447 "loop_context_vars" => 1,
450 $additional_params = {} unless ($additional_params);
451 if ($self->{"DEBUG"}) {
452 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
455 if ($additional_params->{"DEBUG"}) {
456 $additional_params->{"DEBUG"} =
457 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
460 if (%main::myconfig) {
461 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
462 my $jsc_dateformat = $main::myconfig{"dateformat"};
463 $jsc_dateformat =~ s/d+/\%d/gi;
464 $jsc_dateformat =~ s/m+/\%m/gi;
465 $jsc_dateformat =~ s/y+/\%Y/gi;
466 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
469 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
470 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
471 $additional_params->{"conf_latex_templates"} = $main::latex;
472 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
475 if (-f $self->{"login"} . "_menu.ini") {
476 $menu = Menu->new($self->{"login"} . "_menu.ini");
478 $menu = Menu->new("menu.ini");
480 $menu->generate_acl("", $additional_params);
482 my @additional_param_names = keys(%{$additional_params});
484 foreach my $key ($template->param()) {
485 if (grep(/^${key}$/, @additional_param_names)) {
486 $template->param($key => $additional_params->{$key});
488 $template->param($key => $self->{$key});
492 my $output = $template->output();
494 $main::lxdebug->leave_sub();
499 sub show_generic_error {
500 my ($self, $error, $title) = @_;
503 $add_params->{"title"} = $title if ($title);
504 $self->{"label_error"} = $error;
507 print($self->parse_html_template("generic/error", $add_params));
509 die("Error: $error\n");
512 sub show_generic_information {
513 my ($self, $error, $title) = @_;
516 $add_params->{"title"} = $title if ($title);
517 $self->{"label_information"} = $error;
520 print($self->parse_html_template("generic/information", $add_params));
522 die("Information: $error\n");
525 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
526 # changed it to accept an arbitrary number of triggers - sschoeling
528 $main::lxdebug->enter_sub();
531 my $myconfig = shift;
534 # set dateform for jsscript
536 $ifFormat = "%d.%m.%Y";
537 if ($myconfig->{dateformat} eq "dd.mm.yy") {
538 $ifFormat = "%d.%m.%Y";
540 if ($myconfig->{dateformat} eq "dd-mm-yy") {
541 $ifFormat = "%d-%m-%Y";
543 if ($myconfig->{dateformat} eq "dd/mm/yy") {
544 $ifFormat = "%d/%m/%Y";
546 if ($myconfig->{dateformat} eq "mm/dd/yy") {
547 $ifFormat = "%m/%d/%Y";
549 if ($myconfig->{dateformat} eq "mm-dd-yy") {
550 $ifFormat = "%m-%d-%Y";
552 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
553 $ifFormat = "%Y-%m-%d";
565 inputField : "| . (shift) . qq|",
566 ifFormat :"$ifFormat",
567 align : "| . (shift) . qq|",
568 button : "| . (shift) . qq|"
574 <script type="text/javascript">
575 <!--| . join("", @triggers) . qq|//-->
579 $main::lxdebug->leave_sub();
582 } #end sub write_trigger
585 $main::lxdebug->enter_sub();
587 my ($self, $msg) = @_;
589 if ($self->{callback}) {
591 ($script, $argv) = split(/\?/, $self->{callback});
592 exec("perl", "$script", $argv);
600 $main::lxdebug->leave_sub();
603 # sort of columns removed - empty sub
605 $main::lxdebug->enter_sub();
607 my ($self, @columns) = @_;
609 $main::lxdebug->leave_sub();
615 $main::lxdebug->enter_sub(2);
617 my ($self, $myconfig, $amount, $places, $dash) = @_;
619 #Workaround for $format_amount calls without $places
620 if (!defined $places) {
621 (my $dec) = ($amount =~ /\.(\d+)/);
622 $places = length $dec;
625 if ($places =~ /\d/) {
626 $amount = $self->round_amount($amount, $places);
629 # is the amount negative
630 my $negative = ($amount < 0);
634 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
636 my ($whole, $dec) = split /\./, "$amount";
638 $amount = join '', reverse split //, $whole;
639 $fillup = "0" x ($places - length($dec));
641 if ($myconfig->{numberformat} eq '1,000.00') {
642 $amount =~ s/\d{3,}?/$&,/g;
644 $amount = join '', reverse split //, $amount;
645 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
648 if ($myconfig->{numberformat} eq '1.000,00') {
649 $amount =~ s/\d{3,}?/$&./g;
651 $amount = join '', reverse split //, $amount;
652 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
655 if ($myconfig->{numberformat} eq '1000,00') {
657 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
661 $amount = ($negative) ? "($amount)" : "$amount";
662 } elsif ($dash =~ /DRCR/) {
663 $amount = ($negative) ? "$amount DR" : "$amount CR";
665 $amount = ($negative) ? "-$amount" : "$amount";
669 if ($dash eq "0" && $places) {
670 if ($myconfig->{numberformat} eq '1.000,00') {
671 $amount = "0" . "," . "0" x $places;
673 $amount = "0" . "." . "0" x $places;
676 $amount = ($dash ne "") ? "$dash" : "0";
680 $main::lxdebug->leave_sub(2);
686 $main::lxdebug->enter_sub(2);
688 my ($self, $myconfig, $amount) = @_;
690 if ($myconfig->{in_numberformat} == 1) {
691 # Extra input number format 1000.00 or 1000,00
693 $amount = scalar reverse $amount;
694 $amount =~ s/\./DOT/;
696 $amount =~ s/DOT/\./;
697 $amount = scalar reverse $amount;
698 $main::lxdebug->leave_sub(2);
699 return ($amount * 1);
702 if ( ($myconfig->{numberformat} eq '1.000,00')
703 || ($myconfig->{numberformat} eq '1000,00')) {
708 if ($myconfig->{numberformat} eq "1'000.00") {
714 $main::lxdebug->leave_sub(2);
716 return ($amount * 1);
720 $main::lxdebug->enter_sub(2);
722 my ($self, $amount, $places) = @_;
725 # Rounding like "Kaufmannsrunden"
726 # Descr. http://de.wikipedia.org/wiki/Rundung
728 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
731 $amount = $amount * (10**($places));
732 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
734 $main::lxdebug->leave_sub(2);
736 return $round_amount;
741 $main::lxdebug->enter_sub();
743 my ($self, $myconfig, $userspath) = @_;
746 # Some variables used for page breaks
747 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
749 my ($current_page, $current_line, $current_row) = (1, 1, 0);
754 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
755 $self->format_string(grep(/notes/, keys(%{$self})));
757 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
758 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
760 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
761 qw(email tel fax name signature company address businessnumber));
763 open(IN, "$self->{templates}/$self->{IN}")
764 or $self->error("$self->{IN} : $!");
769 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
771 # OUT is used for the media, screen, printer, email
772 # for postscript we store a copy in a temporary file
774 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
775 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
777 $self->{OUT} = ">$self->{tmpfile}";
781 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
783 open(OUT, ">-") or $self->error("STDOUT : $!");
787 # Do we have to run LaTeX two times? This is needed if
788 # the template contains page references.
791 # first we generate a tmpfile
792 # read file and replace <%variable%>
798 # Switch <%analyse%> for template checking
799 # If <%analyse%> is set in the template, you'll find the
800 # parsed output in the user Directory for analysing
802 # <%analyse%> is a switch (allways off, on if set), not a Variable
803 # Set $form->{analysing}="" for system state: never analyse.
804 # Set $form->{analysing}="1" for system state: ever analyse.
805 $self->{analysing} = "1" if (/<%analyse%>/ && !defined $self->{analysing});
807 $two_passes = 1 if (/\\pageref/);
810 # detect pagebreak block and its parameters
811 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
812 $chars_per_line = $1;
813 $lines_on_first_page = $2;
814 $lines_on_second_page = $3;
817 last if (/\s*<%end pagebreak%>/);
824 if (/\s*<%foreach /) {
826 # this one we need for the count
828 $var =~ s/\s*<%foreach (.+?)%>/$1/;
830 last if (/\s*<%end /);
836 # display contents of $self->{number}[] array
837 for $i (0 .. $#{ $self->{$var} }) {
840 # Try to detect whether a manual page break is necessary
841 # but only if there was a <%pagebreak ...%> block before
843 if ($chars_per_line) {
845 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
848 my $_description = $self->{"description"}[$i];
849 while ($_description =~ /\\newline/) {
851 $_description =~ s/\\newline//;
853 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
855 if ($current_page == 1) {
856 $lpp = $lines_on_first_page;
858 $lpp = $lines_on_second_page;
861 # Yes we need a manual page break -- or the user has forced one
863 (($current_line + $lines) > $lpp)
864 || ($self->{"_forced_pagebreaks"}
865 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
869 # replace the special variables <%sumcarriedforward%>
872 my $psum = $self->format_amount($myconfig, $sum, 2);
873 $pb =~ s/<%sumcarriedforward%>/$psum/g;
874 $pb =~ s/<%lastpage%>/$current_page/g;
876 # only "normal" variables are supported here
877 # (no <%if, no <%foreach, no <%include)
879 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
881 # page break block is ready to rock
886 $current_line += $lines;
889 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
893 # don't parse par, we need it for each line
895 s/<%(.+?)%>/$self->{$1}[$i]/mg;
901 # if not comes before if!
902 if (/\s*<%if not /) {
904 # check if it is not set and display
906 s/\s*<%if not (.+?)%>/$1/;
908 unless ($self->{$_}) {
910 last if (/\s*<%end /);
920 last if (/\s*<%end /);
928 # check if it is set and display
930 s/\s*<%if (.+?)%>/$1/;
934 last if (/\s*<%end /);
944 last if (/\s*<%end /);
950 # check for <%include filename%>
951 if (/\s*<%include /) {
953 # get the directory/filename
955 $var =~ s/\s*<%include (.+?)%>/$1/;
957 # mangle filename on basedir
958 $var =~ s/^(\/|\.\.)//g;
960 # prevent the infinite loop!
961 next if ($self->{"$var"});
963 open(INC, "$self->{templates}/$var")
964 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
973 s/<%(.+?)%>/$self->{$1}/g;
974 s/<nobr><\/nobr>/ /g;
981 # Convert the tex file to postscript
982 if ($self->{format} =~ /(postscript|pdf)/) {
985 $self->{cwd} = cwd();
986 $self->{tmpdir} = "$self->{cwd}/$userspath";
988 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
990 $self->{tmpfile} =~ s/$userspath\///g;
992 if ($self->{format} eq 'postscript') {
994 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
996 $self->error($self->cleanup) if ($?);
999 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1001 $self->error($self->cleanup) if ($?);
1004 $self->{tmpfile} =~ s/tex$/dvi/;
1006 system("dvips $self->{tmpfile} -o -q > /dev/null");
1007 $self->error($self->cleanup . "dvips : $!") if ($?);
1008 $self->{tmpfile} =~ s/dvi$/ps/;
1010 if ($self->{format} eq 'pdf') {
1012 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1014 $self->error($self->cleanup) if ($?);
1017 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1019 $self->error($self->cleanup) if ($?);
1021 $self->{tmpfile} =~ s/tex$/pdf/;
1026 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
1028 if ($self->{media} eq 'email') {
1032 my $mail = new Mailer;
1034 map { $mail->{$_} = $self->{$_} }
1035 qw(cc bcc subject message version format charset);
1036 $mail->{to} = qq|$self->{email}|;
1037 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1038 $mail->{fileid} = "$fileid.";
1040 # if we send html or plain text inline
1041 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1042 $mail->{contenttype} = "text/html";
1044 $mail->{message} =~ s/\r\n/<br>\n/g;
1045 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1046 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1048 open(IN, $self->{tmpfile})
1049 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1051 $mail->{message} .= $_;
1058 @{ $mail->{attachments} } = ($self->{tmpfile});
1060 $myconfig->{signature} =~ s/\\n/\r\n/g;
1061 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1065 my $err = $mail->send($out);
1066 $self->error($self->cleanup . "$err") if ($err);
1070 $self->{OUT} = $out;
1072 my $numbytes = (-s $self->{tmpfile});
1073 open(IN, $self->{tmpfile})
1074 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1076 $self->{copies} = 1 unless $self->{media} eq 'printer';
1078 chdir("$self->{cwd}");
1080 for my $i (1 .. $self->{copies}) {
1082 open(OUT, $self->{OUT})
1083 or $self->error($self->cleanup . "$self->{OUT} : $!");
1086 # launch application
1087 print qq|Content-Type: application/$self->{format}
1088 Content-Disposition: attachment; filename="$self->{tmpfile}"
1089 Content-Length: $numbytes
1093 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1113 chdir("$self->{cwd}");
1114 $main::lxdebug->leave_sub();
1118 $main::lxdebug->enter_sub();
1122 chdir("$self->{tmpdir}");
1125 if (-f "$self->{tmpfile}.err") {
1126 open(FH, "$self->{tmpfile}.err");
1131 if ($self->{analysing} eq "") {
1132 if ($self->{tmpfile}) {
1135 $self->{tmpfile} =~ s/\.\w+$//g;
1136 my $tmpfile = $self->{tmpfile};
1137 unlink(<$tmpfile.*>);
1141 chdir("$self->{cwd}");
1143 $main::lxdebug->leave_sub();
1149 $main::lxdebug->enter_sub();
1151 my ($self, @fields) = @_;
1154 %unique_fields = map({ $_ => 1 } @fields);
1155 @fields = keys(%unique_fields);
1157 foreach my $field (@fields) {
1158 next unless ($self->{$field} =~ /\<pagebreak\>/);
1159 $self->{$field} =~ s/\<pagebreak\>//g;
1160 if ($field =~ /.*_(\d+)$/) {
1161 if (!$self->{"_forced_pagebreaks"}) {
1162 $self->{"_forced_pagebreaks"} = [];
1164 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1168 my $format = $self->{format};
1169 if ($self->{format} =~ /(postscript|pdf)/) {
1176 '<', '>', quotemeta('\n'), '
1180 '&', quotemeta('\n'), '
1182 '"', '\$', '%', '_', '#', quotemeta('^'),
1183 '{', '}', '<', '>', '£', "\r", '²'
1189 quotemeta('\n') => '<br>',
1200 quotemeta('^') => '\^\\',
1205 quotemeta('\n') => '\newline ',
1213 foreach my $key (@{ $replace{order}{$format} }) {
1214 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1217 # Allow some HTML markup to be converted into the output format's
1218 # corresponding markup code, e.g. bold or italic.
1219 if ('html' eq $format) {
1220 my @markup_replace = ('b', 'i', 's', 'u');
1222 foreach my $key (@markup_replace) {
1223 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1226 } elsif ('tex' eq $format) {
1227 my %markup_replace = ('b' => 'textbf',
1229 'u' => 'underline');
1231 foreach my $field (@fields) {
1232 foreach my $key (keys(%markup_replace)) {
1233 my $new = $markup_replace{$key};
1235 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1240 $main::lxdebug->leave_sub();
1244 $main::lxdebug->enter_sub();
1246 my ($self, $date, $myconfig) = @_;
1248 if ($date && $date =~ /\D/) {
1250 if ($myconfig->{dateformat} =~ /^yy/) {
1251 ($yy, $mm, $dd) = split /\D/, $date;
1253 if ($myconfig->{dateformat} =~ /^mm/) {
1254 ($mm, $dd, $yy) = split /\D/, $date;
1256 if ($myconfig->{dateformat} =~ /^dd/) {
1257 ($dd, $mm, $yy) = split /\D/, $date;
1262 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1263 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1265 $dd = "0$dd" if ($dd < 10);
1266 $mm = "0$mm" if ($mm < 10);
1268 $date = "$yy$mm$dd";
1271 $main::lxdebug->leave_sub();
1276 # Database routines used throughout
1279 $main::lxdebug->enter_sub();
1281 my ($self, $myconfig) = @_;
1283 # connect to database
1285 DBI->connect($myconfig->{dbconnect},
1286 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1290 if ($myconfig->{dboptions}) {
1291 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1294 $main::lxdebug->leave_sub();
1299 sub dbconnect_noauto {
1300 $main::lxdebug->enter_sub();
1302 my ($self, $myconfig) = @_;
1304 # connect to database
1306 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1307 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1311 if ($myconfig->{dboptions}) {
1312 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1315 $main::lxdebug->leave_sub();
1320 sub update_balance {
1321 $main::lxdebug->enter_sub();
1323 my ($self, $dbh, $table, $field, $where, $value) = @_;
1325 # if we have a value, go do it
1328 # retrieve balance from table
1329 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1330 my $sth = $dbh->prepare($query);
1332 $sth->execute || $self->dberror($query);
1333 my ($balance) = $sth->fetchrow_array;
1339 $query = "UPDATE $table SET $field = $balance WHERE $where";
1340 $dbh->do($query) || $self->dberror($query);
1342 $main::lxdebug->leave_sub();
1345 sub update_exchangerate {
1346 $main::lxdebug->enter_sub();
1348 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1350 # some sanity check for currency
1352 $main::lxdebug->leave_sub();
1356 my $query = qq|SELECT e.curr FROM exchangerate e
1357 WHERE e.curr = '$curr'
1358 AND e.transdate = '$transdate'
1360 my $sth = $dbh->prepare($query);
1361 $sth->execute || $self->dberror($query);
1364 if ($buy != 0 && $sell != 0) {
1365 $set = "buy = $buy, sell = $sell";
1366 } elsif ($buy != 0) {
1367 $set = "buy = $buy";
1368 } elsif ($sell != 0) {
1369 $set = "sell = $sell";
1372 if ($sth->fetchrow_array) {
1373 $query = qq|UPDATE exchangerate
1375 WHERE curr = '$curr'
1376 AND transdate = '$transdate'|;
1378 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1379 VALUES ('$curr', $buy, $sell, '$transdate')|;
1382 $dbh->do($query) || $self->dberror($query);
1384 $main::lxdebug->leave_sub();
1387 sub save_exchangerate {
1388 $main::lxdebug->enter_sub();
1390 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1392 my $dbh = $self->dbconnect($myconfig);
1394 my ($buy, $sell) = (0, 0);
1395 $buy = $rate if $fld eq 'buy';
1396 $sell = $rate if $fld eq 'sell';
1398 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1402 $main::lxdebug->leave_sub();
1405 sub get_exchangerate {
1406 $main::lxdebug->enter_sub();
1408 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1410 unless ($transdate) {
1411 $main::lxdebug->leave_sub();
1415 my $query = qq|SELECT e.$fld FROM exchangerate e
1416 WHERE e.curr = '$curr'
1417 AND e.transdate = '$transdate'|;
1418 my $sth = $dbh->prepare($query);
1419 $sth->execute || $self->dberror($query);
1421 my ($exchangerate) = $sth->fetchrow_array;
1424 $main::lxdebug->leave_sub();
1426 return $exchangerate;
1429 sub check_exchangerate {
1430 $main::lxdebug->enter_sub();
1432 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1434 unless ($transdate) {
1435 $main::lxdebug->leave_sub();
1439 my $dbh = $self->dbconnect($myconfig);
1441 my $query = qq|SELECT e.$fld FROM exchangerate e
1442 WHERE e.curr = '$currency'
1443 AND e.transdate = '$transdate'|;
1444 my $sth = $dbh->prepare($query);
1445 $sth->execute || $self->dberror($query);
1447 my ($exchangerate) = $sth->fetchrow_array;
1451 $main::lxdebug->leave_sub();
1453 return $exchangerate;
1457 $main::lxdebug->enter_sub();
1459 my ($self, $dbh, $id) = @_;
1463 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1465 if ($self->{"shipto$item"}) {
1466 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1468 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1473 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1474 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1475 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1476 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1477 '$self->{shiptozipcode}', '$self->{shiptocity}',
1478 '$self->{shiptocountry}', '$self->{shiptocontact}',
1479 '$self->{shiptophone}', '$self->{shiptofax}',
1480 '$self->{shiptoemail}')|;
1481 $dbh->do($query) || $self->dberror($query);
1484 $main::lxdebug->leave_sub();
1488 $main::lxdebug->enter_sub();
1490 my ($self, $dbh) = @_;
1492 my $query = qq|SELECT e.id, e.name FROM employee e
1493 WHERE e.login = '$self->{login}'|;
1494 my $sth = $dbh->prepare($query);
1495 $sth->execute || $self->dberror($query);
1497 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1498 $self->{employee_id} *= 1;
1502 $main::lxdebug->leave_sub();
1505 # get other contact for transaction and form - html/tex
1507 $main::lxdebug->enter_sub();
1509 my ($self, $dbh, $id) = @_;
1511 my $query = qq|SELECT c.*
1514 $sth = $dbh->prepare($query);
1515 $sth->execute || $self->dberror($query);
1517 $ref = $sth->fetchrow_hashref(NAME_lc);
1519 push @{ $self->{$_} }, $ref;
1522 $main::lxdebug->leave_sub();
1525 # get contacts for id, if no contact return {"","","","",""}
1527 $main::lxdebug->enter_sub();
1529 my ($self, $dbh, $id) = @_;
1531 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1533 WHERE cp_cv_id=$id|;
1534 my $sth = $dbh->prepare($query);
1535 $sth->execute || $self->dberror($query);
1538 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1539 push @{ $self->{all_contacts} }, $ref;
1544 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1547 $main::lxdebug->leave_sub();
1550 # this sub gets the id and name from $table
1552 $main::lxdebug->enter_sub();
1554 my ($self, $myconfig, $table) = @_;
1556 # connect to database
1557 my $dbh = $self->dbconnect($myconfig);
1559 my $name = $self->like(lc $self->{$table});
1560 my $customernumber = $self->like(lc $self->{customernumber});
1562 if ($self->{customernumber} ne "") {
1563 $query = qq~SELECT c.id, c.name,
1564 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1566 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1569 $query = qq~SELECT c.id, c.name,
1570 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1572 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1576 if ($self->{openinvoices}) {
1577 $query = qq~SELECT DISTINCT c.id, c.name,
1578 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1579 FROM $self->{arap} a
1580 JOIN $table c ON (a.${table}_id = c.id)
1581 WHERE NOT a.amount = a.paid
1582 AND lower(c.name) LIKE '$name'
1585 my $sth = $dbh->prepare($query);
1587 $sth->execute || $self->dberror($query);
1590 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1591 push(@{ $self->{name_list} }, $ref);
1597 $main::lxdebug->leave_sub();
1602 # the selection sub is used in the AR, AP, IS, IR and OE module
1605 $main::lxdebug->enter_sub();
1607 my ($self, $myconfig, $table, $module) = @_;
1610 my $dbh = $self->dbconnect($myconfig);
1612 my $query = qq|SELECT count(*) FROM $table|;
1613 my $sth = $dbh->prepare($query);
1614 $sth->execute || $self->dberror($query);
1615 my ($count) = $sth->fetchrow_array;
1618 # build selection list
1619 if ($count < $myconfig->{vclimit}) {
1620 $query = qq|SELECT id, name
1621 FROM $table WHERE not obsolete
1623 $sth = $dbh->prepare($query);
1624 $sth->execute || $self->dberror($query);
1626 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1627 push @{ $self->{"all_$table"} }, $ref;
1635 $self->get_employee($dbh);
1637 # setup sales contacts
1638 $query = qq|SELECT e.id, e.name
1641 AND NOT e.id = $self->{employee_id}|;
1642 $sth = $dbh->prepare($query);
1643 $sth->execute || $self->dberror($query);
1645 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1646 push @{ $self->{all_employees} }, $ref;
1651 push @{ $self->{all_employees} },
1652 { id => $self->{employee_id},
1653 name => $self->{employee} };
1655 # sort the whole thing
1656 @{ $self->{all_employees} } =
1657 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1659 if ($module eq 'AR') {
1661 # prepare query for departments
1662 $query = qq|SELECT d.id, d.description
1668 $query = qq|SELECT d.id, d.description
1673 $sth = $dbh->prepare($query);
1674 $sth->execute || $self->dberror($query);
1676 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1677 push @{ $self->{all_departments} }, $ref;
1682 $main::lxdebug->leave_sub();
1685 # this is only used for reports
1686 sub all_departments {
1687 $main::lxdebug->enter_sub();
1689 my ($self, $myconfig, $table) = @_;
1691 my $dbh = $self->dbconnect($myconfig);
1692 my $where = "1 = 1";
1694 if (defined $table) {
1695 if ($table eq 'customer') {
1696 $where = " d.role = 'P'";
1700 my $query = qq|SELECT d.id, d.description
1704 my $sth = $dbh->prepare($query);
1705 $sth->execute || $self->dberror($query);
1707 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1708 push @{ $self->{all_departments} }, $ref;
1714 $main::lxdebug->leave_sub();
1718 $main::lxdebug->enter_sub();
1720 my ($self, $module, $myconfig, $table) = @_;
1722 $self->all_vc($myconfig, $table, $module);
1724 # get last customers or vendors
1727 my $dbh = $self->dbconnect($myconfig);
1731 # now get the account numbers
1732 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1734 WHERE c.link LIKE '%$module%'
1737 $sth = $dbh->prepare($query);
1738 $sth->execute || $self->dberror($query);
1740 $self->{accounts} = "";
1741 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1743 foreach my $key (split /:/, $ref->{link}) {
1744 if ($key =~ /$module/) {
1746 # cross reference for keys
1747 $xkeyref{ $ref->{accno} } = $key;
1749 push @{ $self->{"${module}_links"}{$key} },
1750 { accno => $ref->{accno},
1751 description => $ref->{description},
1752 taxkey => $ref->{taxkey_id} };
1754 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1760 if (($module eq "AP") || ($module eq "AR")) {
1762 # get tax rates and description
1763 $query = qq| SELECT * FROM tax t|;
1764 $sth = $dbh->prepare($query);
1765 $sth->execute || $self->dberror($query);
1767 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1768 push @{ $self->{TAX} }, $ref;
1774 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1776 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1777 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1778 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1779 c.name AS $table, a.department_id, d.description AS department,
1780 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1781 a.employee_id, e.name AS employee, a.gldate
1783 JOIN $table c ON (a.${table}_id = c.id)
1784 LEFT JOIN employee e ON (e.id = a.employee_id)
1785 LEFT JOIN department d ON (d.id = a.department_id)
1786 WHERE a.id = $self->{id}|;
1787 $sth = $dbh->prepare($query);
1788 $sth->execute || $self->dberror($query);
1790 $ref = $sth->fetchrow_hashref(NAME_lc);
1791 foreach $key (keys %$ref) {
1792 $self->{$key} = $ref->{$key};
1796 # get amounts from individual entries
1797 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1798 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1800 JOIN chart c ON (c.id = a.chart_id)
1801 LEFT JOIN project p ON (p.id = a.project_id)
1802 LEFT Join tax t ON (a.taxkey = t.taxkey)
1803 WHERE a.trans_id = $self->{id}
1804 AND a.fx_transaction = '0'
1805 ORDER BY a.oid,a.transdate|;
1806 $sth = $dbh->prepare($query);
1807 $sth->execute || $self->dberror($query);
1809 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1811 # get exchangerate for currency
1812 $self->{exchangerate} =
1813 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1817 # store amounts in {acc_trans}{$key} for multiple accounts
1818 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1819 $ref->{exchangerate} =
1820 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1822 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1825 $ref->{index} = $index;
1827 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1831 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1832 (SELECT c.accno FROM chart c
1833 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1834 (SELECT c.accno FROM chart c
1835 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1837 $sth = $dbh->prepare($query);
1838 $sth->execute || $self->dberror($query);
1840 $ref = $sth->fetchrow_hashref(NAME_lc);
1841 map { $self->{$_} = $ref->{$_} } keys %$ref;
1847 $query = qq|SELECT current_date AS transdate,
1848 d.curr AS currencies, d.closedto, d.revtrans,
1849 (SELECT c.accno FROM chart c
1850 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1851 (SELECT c.accno FROM chart c
1852 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1854 $sth = $dbh->prepare($query);
1855 $sth->execute || $self->dberror($query);
1857 $ref = $sth->fetchrow_hashref(NAME_lc);
1858 map { $self->{$_} = $ref->{$_} } keys %$ref;
1861 if ($self->{"$self->{vc}_id"}) {
1863 # only setup currency
1864 ($self->{currency}) = split /:/, $self->{currencies};
1868 $self->lastname_used($dbh, $myconfig, $table, $module);
1870 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1872 # get exchangerate for currency
1873 $self->{exchangerate} =
1874 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1883 $main::lxdebug->leave_sub();
1887 $main::lxdebug->enter_sub();
1889 my ($self, $dbh, $myconfig, $table, $module) = @_;
1891 my $arap = ($table eq 'customer') ? "ar" : "ap";
1892 my $where = "1 = 1";
1894 if ($self->{type} =~ /_order/) {
1896 $where = "quotation = '0'";
1898 if ($self->{type} =~ /_quotation/) {
1900 $where = "quotation = '1'";
1903 my $query = qq|SELECT MAX(id) FROM $arap
1905 AND ${table}_id > 0|;
1906 my $sth = $dbh->prepare($query);
1907 $sth->execute || $self->dberror($query);
1909 my ($trans_id) = $sth->fetchrow_array;
1913 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1914 current_date + ct.terms AS duedate, a.department_id,
1915 d.description AS department
1917 JOIN $table ct ON (a.${table}_id = ct.id)
1918 LEFT JOIN department d ON (a.department_id = d.id)
1919 WHERE a.id = $trans_id|;
1920 $sth = $dbh->prepare($query);
1921 $sth->execute || $self->dberror($query);
1923 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1924 $self->{duedate}, $self->{department_id}, $self->{department})
1925 = $sth->fetchrow_array;
1928 $main::lxdebug->leave_sub();
1932 $main::lxdebug->enter_sub();
1934 my ($self, $myconfig, $thisdate, $days) = @_;
1936 my $dbh = $self->dbconnect($myconfig);
1941 my $dateformat = $myconfig->{dateformat};
1942 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1944 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1946 $sth = $dbh->prepare($query);
1947 $sth->execute || $self->dberror($query);
1949 $query = qq|SELECT current_date AS thisdate
1951 $sth = $dbh->prepare($query);
1952 $sth->execute || $self->dberror($query);
1955 ($thisdate) = $sth->fetchrow_array;
1960 $main::lxdebug->leave_sub();
1966 $main::lxdebug->enter_sub();
1968 my ($self, $string) = @_;
1970 if ($string !~ /%/) {
1971 $string = "%$string%";
1974 $string =~ s/\'/\'\'/g;
1976 $main::lxdebug->leave_sub();
1982 $main::lxdebug->enter_sub();
1984 my ($self, $flds, $new, $count, $numrows) = @_;
1988 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1994 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1996 $j = $item->{ndx} - 1;
1997 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2001 for $i ($count + 1 .. $numrows) {
2002 map { delete $self->{"${_}_$i"} } @{$flds};
2005 $main::lxdebug->leave_sub();
2009 $main::lxdebug->enter_sub();
2011 my ($self, $myconfig) = @_;
2015 my $dbh = $self->dbconnect_noauto($myconfig);
2017 my $query = qq|DELETE FROM status
2018 WHERE formname = '$self->{formname}'
2020 my $sth = $dbh->prepare($query) || $self->dberror($query);
2022 if ($self->{formname} =~ /(check|receipt)/) {
2023 for $i (1 .. $self->{rowcount}) {
2024 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2028 $sth->execute($self->{id}) || $self->dberror($query);
2032 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2033 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2035 my %queued = split / /, $self->{queued};
2037 if ($self->{formname} =~ /(check|receipt)/) {
2039 # this is a check or receipt, add one entry for each lineitem
2040 my ($accno) = split /--/, $self->{account};
2041 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2042 chart_id) VALUES (?, '$printed',
2043 '$queued{$self->{formname}}', '$self->{prinform}',
2044 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2045 $sth = $dbh->prepare($query) || $self->dberror($query);
2047 for $i (1 .. $self->{rowcount}) {
2048 if ($self->{"checked_$i"}) {
2049 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2054 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2055 spoolfile, formname)
2056 VALUES ($self->{id}, '$printed', '$emailed',
2057 '$queued{$self->{formname}}', '$self->{formname}')|;
2058 $dbh->do($query) || $self->dberror($query);
2064 $main::lxdebug->leave_sub();
2068 $main::lxdebug->enter_sub();
2070 my ($self, $dbh) = @_;
2072 my ($query, $printed, $emailed);
2074 my $formnames = $self->{printed};
2075 my $emailforms = $self->{emailed};
2077 my $query = qq|DELETE FROM status
2078 WHERE formname = '$self->{formname}'
2079 AND trans_id = $self->{id}|;
2080 $dbh->do($query) || $self->dberror($query);
2082 # this only applies to the forms
2083 # checks and receipts are posted when printed or queued
2085 if ($self->{queued}) {
2086 my %queued = split / /, $self->{queued};
2088 foreach my $formname (keys %queued) {
2089 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2090 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2092 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2093 spoolfile, formname)
2094 VALUES ($self->{id}, '$printed', '$emailed',
2095 '$queued{$formname}', '$formname')|;
2096 $dbh->do($query) || $self->dberror($query);
2098 $formnames =~ s/$self->{formname}//;
2099 $emailforms =~ s/$self->{formname}//;
2104 # save printed, emailed info
2105 $formnames =~ s/^ +//g;
2106 $emailforms =~ s/^ +//g;
2109 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2110 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2112 foreach my $formname (keys %status) {
2113 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2114 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2116 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2117 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2118 $dbh->do($query) || $self->dberror($query);
2121 $main::lxdebug->leave_sub();
2124 sub update_defaults {
2125 $main::lxdebug->enter_sub();
2127 my ($self, $myconfig, $fld) = @_;
2129 my $dbh = $self->dbconnect_noauto($myconfig);
2130 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2131 my $sth = $dbh->prepare($query);
2133 $sth->execute || $self->dberror($query);
2134 my ($var) = $sth->fetchrow_array;
2139 $query = qq|UPDATE defaults
2141 $dbh->do($query) || $self->dberror($query);
2146 $main::lxdebug->leave_sub();
2151 sub update_business {
2152 $main::lxdebug->enter_sub();
2154 my ($self, $myconfig, $business_id) = @_;
2156 my $dbh = $self->dbconnect_noauto($myconfig);
2158 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2159 my $sth = $dbh->prepare($query);
2161 $sth->execute || $self->dberror($query);
2162 my ($var) = $sth->fetchrow_array;
2167 $query = qq|UPDATE business
2168 SET customernumberinit = '$var' WHERE id=$business_id|;
2169 $dbh->do($query) || $self->dberror($query);
2174 $main::lxdebug->leave_sub();
2180 $main::lxdebug->enter_sub();
2182 my ($self, $myconfig, $salesman) = @_;
2184 my $dbh = $self->dbconnect($myconfig);
2186 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2187 my $sth = $dbh->prepare($query);
2188 $sth->execute || $self->dberror($query);
2191 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2192 push(@{ $self->{salesman_list} }, $ref);
2196 $main::lxdebug->leave_sub();
2201 sub get_partsgroup {
2202 $main::lxdebug->enter_sub();
2204 my ($self, $myconfig, $p) = @_;
2206 my $dbh = $self->dbconnect($myconfig);
2208 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2210 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2212 if ($p->{searchitems} eq 'part') {
2214 WHERE p.inventory_accno_id > 0|;
2216 if ($p->{searchitems} eq 'service') {
2218 WHERE p.inventory_accno_id IS NULL|;
2220 if ($p->{searchitems} eq 'assembly') {
2222 WHERE p.assembly = '1'|;
2224 if ($p->{searchitems} eq 'labor') {
2226 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2230 ORDER BY partsgroup|;
2233 $query = qq|SELECT id, partsgroup FROM partsgroup
2234 ORDER BY partsgroup|;
2237 if ($p->{language_code}) {
2238 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2239 t.description AS translation
2241 JOIN parts p ON (p.partsgroup_id = pg.id)
2242 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2243 ORDER BY translation|;
2246 my $sth = $dbh->prepare($query);
2247 $sth->execute || $self->dberror($query);
2249 $self->{all_partsgroup} = ();
2250 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2251 push @{ $self->{all_partsgroup} }, $ref;
2255 $main::lxdebug->leave_sub();
2258 sub get_pricegroup {
2259 $main::lxdebug->enter_sub();
2261 my ($self, $myconfig, $p) = @_;
2263 my $dbh = $self->dbconnect($myconfig);
2265 my $query = qq|SELECT p.id, p.pricegroup
2269 ORDER BY pricegroup|;
2272 $query = qq|SELECT id, pricegroup FROM pricegroup
2273 ORDER BY pricegroup|;
2276 my $sth = $dbh->prepare($query);
2277 $sth->execute || $self->dberror($query);
2279 $self->{all_pricegroup} = ();
2280 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2281 push @{ $self->{all_pricegroup} }, $ref;
2286 $main::lxdebug->leave_sub();
2290 my ($self, $dbh, $myconfig, $audittrail) = @_;
2292 # table, $reference, $formname, $action, $id, $transdate) = @_;
2299 $dbh = $self->dbconnect($myconfig);
2303 # if we have an id add audittrail, otherwise get a new timestamp
2305 if ($audittrail->{id}) {
2307 $query = qq|SELECT audittrail FROM defaults|;
2309 if ($dbh->selectrow_array($query)) {
2310 my ($null, $employee_id) = $self->get_employee($dbh);
2312 if ($self->{audittrail} && !$myconfig) {
2313 chop $self->{audittrail};
2315 my @a = split /\|/, $self->{audittrail};
2319 my @flds = qw(tablename reference formname action transdate);
2321 # put into hash and remove dups
2323 $key = "$a[2]$a[3]";
2325 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2329 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2330 formname, action, employee_id, transdate)
2331 VALUES ($audittrail->{id}, ?, ?,
2332 ?, ?, $employee_id, ?)|;
2333 my $sth = $dbh->prepare($query) || $self->dberror($query);
2337 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2341 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2343 $sth->execute || $self->dberror;
2348 if ($audittrail->{transdate}) {
2349 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2350 formname, action, employee_id, transdate) VALUES (
2351 $audittrail->{id}, '$audittrail->{tablename}', |
2352 . $dbh->quote($audittrail->{reference}) . qq|,
2353 '$audittrail->{formname}', '$audittrail->{action}',
2354 $employee_id, '$audittrail->{transdate}')|;
2356 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2357 formname, action, employee_id) VALUES ($audittrail->{id},
2358 '$audittrail->{tablename}', |
2359 . $dbh->quote($audittrail->{reference}) . qq|,
2360 '$audittrail->{formname}', '$audittrail->{action}',
2367 $query = qq|SELECT current_timestamp FROM defaults|;
2368 my ($timestamp) = $dbh->selectrow_array($query);
2371 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2374 $dbh->disconnect if $disconnect;
2383 $main::lxdebug->enter_sub();
2385 my ($type, $country, $NLS_file) = @_;
2389 if ($country && -d "locale/$country") {
2390 $self->{countrycode} = $country;
2391 eval { require "locale/$country/$NLS_file"; };
2394 $self->{NLS_file} = $NLS_file;
2396 push @{ $self->{LONG_MONTH} },
2397 ("January", "February", "March", "April",
2398 "May ", "June", "July", "August",
2399 "September", "October", "November", "December");
2400 push @{ $self->{SHORT_MONTH} },
2401 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2403 $main::lxdebug->leave_sub();
2409 my ($self, $text) = @_;
2411 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2415 $main::lxdebug->enter_sub();
2417 my ($self, $text) = @_;
2419 if (exists $self{subs}{$text}) {
2420 $text = $self{subs}{$text};
2422 if ($self->{countrycode} && $self->{NLS_file}) {
2424 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2428 $main::lxdebug->leave_sub();
2434 $main::lxdebug->enter_sub();
2436 my ($self, $myconfig, $date, $longformat) = @_;
2439 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2444 $spc = $myconfig->{dateformat};
2446 $spc = substr($spc, 1, 1);
2448 if ($date =~ /\D/) {
2449 if ($myconfig->{dateformat} =~ /^yy/) {
2450 ($yy, $mm, $dd) = split /\D/, $date;
2452 if ($myconfig->{dateformat} =~ /^mm/) {
2453 ($mm, $dd, $yy) = split /\D/, $date;
2455 if ($myconfig->{dateformat} =~ /^dd/) {
2456 ($dd, $mm, $yy) = split /\D/, $date;
2459 $date = substr($date, 2);
2460 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2465 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2466 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2468 if ($myconfig->{dateformat} =~ /^dd/) {
2469 if (defined $longformat && $longformat == 0) {
2471 $dd = "0$dd" if ($dd < 10);
2472 $mm = "0$mm" if ($mm < 10);
2473 $longdate = "$dd$spc$mm$spc$yy";
2476 $longdate .= ($spc eq '.') ? ". " : " ";
2477 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2479 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2481 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2482 # Lx-Office is mainly used in Germany or German speaking countries.
2483 if (defined $longformat && $longformat == 0) {
2485 $dd = "0$dd" if ($dd < 10);
2486 $mm = "0$mm" if ($mm < 10);
2487 $longdate = "$yy-$mm-$dd";
2489 $longdate = "$dd. ";
2490 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2493 if (defined $longformat && $longformat == 0) {
2495 $dd = "0$dd" if ($dd < 10);
2496 $mm = "0$mm" if ($mm < 10);
2497 $longdate = "$mm$spc$dd$spc$yy";
2499 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2505 $main::lxdebug->leave_sub();
2511 $main::lxdebug->enter_sub();
2513 my ($self, $myconfig, $date, $longformat) = @_;
2516 $main::lxdebug->leave_sub();
2521 $spc = $myconfig->{dateformat};
2523 $spc = substr($spc, 1, 1);
2525 if ($date =~ /\D/) {
2526 if ($myconfig->{dateformat} =~ /^yy/) {
2527 ($yy, $mm, $dd) = split /\D/, $date;
2528 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2529 ($mm, $dd, $yy) = split /\D/, $date;
2530 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2531 ($dd, $mm, $yy) = split /\D/, $date;
2534 $date = substr($date, 2);
2535 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2540 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2541 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2543 $main::lxdebug->leave_sub();
2544 return ($yy, $mm, $dd);