1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 # Antti Kaihola <akaihola@siba.fi>
17 # Moritz Bunkus (tex code)
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #======================================================================
32 # Utilities for parsing forms
33 # and supporting routines for linking account numbers
34 # used in AR, AP and IS, IR modules
36 #======================================================================
43 $main::lxdebug->enter_sub();
47 my @pairs = split(/&/, $input);
50 my ($name, $value) = split(/=/, $_, 2);
51 $in{$name} = unescape(undef, $value);
54 $main::lxdebug->leave_sub();
59 sub _request_to_hash {
60 $main::lxdebug->enter_sub();
63 my ($i, $loc, $key, $val);
64 my (%ATTACH, $f, $header, $header_body, $len, $buf);
65 my ($boundary, @list, $size, $body, $x, $blah, $name);
67 if ($ENV{'CONTENT_TYPE'}
68 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
69 $boundary = quotemeta('--' . $1);
70 @list = split(/$boundary/, $input);
72 # For some reason there are always 2 extra, that are empty
75 for ($x = 1; $x <= $size; $x++) {
76 $header_body = $list[$x];
77 $header_body =~ /\r\n\r\n|\n\n/;
79 # Here we split the header and body
84 # Now we try to get the file name
86 $name =~ /name=\"(.+)\"/;
87 ($name, $blah) = split(/\"/, $1);
89 # If the form name is not attach, then we need to parse this like
91 if ($name ne "attach") {
92 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
93 $ATTACH{$name} = $body;
95 # Otherwise it is an attachment and we need to finish it up
96 } elsif ($name eq "attach") {
97 $header =~ /filename=\"(.+)\"/;
98 $ATTACH{'FILE_NAME'} = $1;
99 $ATTACH{'FILE_NAME'} =~ s/\"//g;
100 $ATTACH{'FILE_NAME'} =~ s/\s//g;
101 $ATTACH{'FILE_CONTENT'} = $body;
103 for ($i = $x; $list[$i]; $i++) {
104 $list[$i] =~ s/^.+name=$//;
105 $list[$i] =~ /\"(\w+)\"/;
111 $main::lxdebug->leave_sub();
115 $main::lxdebug->leave_sub();
116 return _input_to_hash($input);
121 $main::lxdebug->enter_sub();
127 read(STDIN, $_, $ENV{CONTENT_LENGTH});
129 if ($ENV{QUERY_STRING}) {
130 $_ = $ENV{QUERY_STRING};
137 my %parameters = _request_to_hash($_);
138 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
140 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
142 $self->{action} = lc $self->{action};
143 $self->{action} =~ s/( |-|,|#)/_/g;
145 $self->{version} = "2.2.0";
146 $self->{dbversion} = "2.2.0";
148 $main::lxdebug->leave_sub();
154 $main::lxdebug->enter_sub();
160 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
162 $main::lxdebug->leave_sub();
166 $main::lxdebug->enter_sub();
168 my ($self, $str, $beenthere) = @_;
170 # for Apache 2 we escape strings twice
171 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
172 # $str = $self->escape($str, 1);
175 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
177 $main::lxdebug->leave_sub();
183 $main::lxdebug->enter_sub();
185 my ($self, $str) = @_;
190 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
192 $main::lxdebug->leave_sub();
198 my ($self, $str) = @_;
200 if ($str && !ref($str)) {
201 $str =~ s/"/"/g;
209 my ($self, $str) = @_;
211 if ($str && !ref($str)) {
212 $str =~ s/"/"/g;
224 print qq|<input type=hidden name="$_" value="|
225 . $self->quote($self->{$_})
229 delete $self->{header};
230 for (sort keys %$self) {
231 print qq|<input type=hidden name="$_" value="|
232 . $self->quote($self->{$_})
240 $main::lxdebug->enter_sub();
242 my ($self, $msg) = @_;
244 if ($ENV{HTTP_USER_AGENT}) {
252 <h2 class=error>Error!</h2>
264 if ($self->{error_function}) {
265 &{ $self->{error_function} }($msg);
271 $main::lxdebug->leave_sub();
275 $main::lxdebug->enter_sub();
277 my ($self, $msg) = @_;
279 if ($ENV{HTTP_USER_AGENT}) {
282 if (!$self->{header}) {
295 if ($self->{info_function}) {
296 &{ $self->{info_function} }($msg);
302 $main::lxdebug->leave_sub();
306 $main::lxdebug->enter_sub();
308 my ($self, $str, $cols, $maxrows) = @_;
312 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
314 $maxrows = $rows unless defined $maxrows;
316 $main::lxdebug->leave_sub();
318 return ($rows > $maxrows) ? $maxrows : $rows;
322 $main::lxdebug->enter_sub();
324 my ($self, $msg) = @_;
326 $self->error("$msg\n" . $DBI::errstr);
328 $main::lxdebug->leave_sub();
332 $main::lxdebug->enter_sub();
334 my ($self, $name, $msg) = @_;
336 if ($self->{$name} =~ /^\s*$/) {
339 $main::lxdebug->leave_sub();
343 $main::lxdebug->enter_sub();
347 if ($self->{header}) {
348 $main::lxdebug->leave_sub();
352 my ($stylesheet, $favicon, $charset);
354 if ($ENV{HTTP_USER_AGENT}) {
356 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
358 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
362 if ($self->{favicon} && (-f "$self->{favicon}")) {
364 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
368 if ($self->{charset}) {
370 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
373 if ($self->{landscape}) {
374 $pagelayout = qq|<style type="text/css">
375 \@page { size:landscape; }
378 if ($self->{fokus}) {
379 $fokus = qq|<script type="text/javascript">
381 function fokus(){document.$self->{fokus}.focus();}
388 if ($self->{jsscript} == 1) {
391 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
392 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
393 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
394 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
401 ? "$self->{title} - $self->{titlebar}"
404 print qq|Content-Type: text/html
408 <title>$self->{titlebar}</title>
421 $main::lxdebug->leave_sub();
424 sub parse_html_template {
425 $main::lxdebug->enter_sub();
427 my ($self, $file) = @_;
429 my $template = HTML::Template->new("filename" => "templates/webpages/$file",
430 "die_on_bad_params" => 0,
432 "case_sensitive" => 1,
433 "loop_context_vars" => 1,
435 my @params = $template->param();
437 if (grep("DEBUG", @params) && $self->{"DEBUG"}) {
438 $template->param("DEBUG" => "<br><em>DEBUG INFORMATION:</em><pre>" .
439 $self->{"DEBUG"} . "</pre>");
442 foreach my $key (keys(%{$self})) {
443 if (("DEBUG" ne $key) && grep(${key}, @params)) {
444 $template->param($key => $self->{$key});
448 my $output = $template->output();
450 $main::lxdebug->leave_sub();
455 sub show_generic_error {
456 my ($self, $error, $title) = @_;
458 $self->{"title"} = $title if ($title);
459 $self->{"label_error"} = $error;
461 print($self->parse_html_template("generic/error.html"));
464 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
465 # changed it to accept an arbitrary number of triggers - sschoeling
467 $main::lxdebug->enter_sub();
470 my $myconfig = shift;
473 # set dateform for jsscript
475 $ifFormat = "%d.%m.%Y";
476 if ($myconfig->{dateformat} eq "dd.mm.yy") {
477 $ifFormat = "%d.%m.%Y";
479 if ($myconfig->{dateformat} eq "dd-mm-yy") {
480 $ifFormat = "%d-%m-%Y";
482 if ($myconfig->{dateformat} eq "dd/mm/yy") {
483 $ifFormat = "%d/%m/%Y";
485 if ($myconfig->{dateformat} eq "mm/dd/yy") {
486 $ifFormat = "%m/%d/%Y";
488 if ($myconfig->{dateformat} eq "mm-dd-yy") {
489 $ifFormat = "%m-%d-%Y";
491 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
492 $ifFormat = "%Y-%m-%d";
504 inputField : "| . (shift) . qq|",
505 ifFormat :"$ifFormat",
506 align : "| . (shift) . qq|",
507 button : "| . (shift) . qq|"
513 <script type="text/javascript">
514 <!--| . join("", @triggers) . qq|//-->
518 $main::lxdebug->leave_sub();
521 } #end sub write_trigger
524 $main::lxdebug->enter_sub();
526 my ($self, $msg) = @_;
528 if ($self->{callback}) {
530 ($script, $argv) = split(/\?/, $self->{callback});
531 exec("perl", "$script", $argv);
539 $main::lxdebug->leave_sub();
542 # sort of columns removed - empty sub
544 $main::lxdebug->enter_sub();
546 my ($self, @columns) = @_;
548 $main::lxdebug->leave_sub();
554 $main::lxdebug->enter_sub();
556 my ($self, $myconfig, $amount, $places, $dash) = @_;
558 #Workaround for $format_amount calls without $places
559 if (!defined $places) {
560 (my $dec) = ($amount =~ /\.(\d+)/);
561 $places = length $dec;
564 if ($places =~ /\d/) {
565 $amount = $self->round_amount($amount, $places);
568 # is the amount negative
569 my $negative = ($amount < 0);
573 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
575 my ($whole, $dec) = split /\./, "$amount";
577 $amount = join '', reverse split //, $whole;
578 $fillup = "0" x ($places - length($dec));
580 if ($myconfig->{numberformat} eq '1,000.00') {
581 $amount =~ s/\d{3,}?/$&,/g;
583 $amount = join '', reverse split //, $amount;
584 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
587 if ($myconfig->{numberformat} eq '1.000,00') {
588 $amount =~ s/\d{3,}?/$&./g;
590 $amount = join '', reverse split //, $amount;
591 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
594 if ($myconfig->{numberformat} eq '1000,00') {
596 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
600 $amount = ($negative) ? "($amount)" : "$amount";
601 } elsif ($dash =~ /DRCR/) {
602 $amount = ($negative) ? "$amount DR" : "$amount CR";
604 $amount = ($negative) ? "-$amount" : "$amount";
608 if ($dash eq "0" && $places) {
609 if ($myconfig->{numberformat} eq '1.000,00') {
610 $amount = "0" . "," . "0" x $places;
612 $amount = "0" . "." . "0" x $places;
615 $amount = ($dash ne "") ? "$dash" : "0";
619 $main::lxdebug->leave_sub();
625 $main::lxdebug->enter_sub();
627 my ($self, $myconfig, $amount) = @_;
628 $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
630 if ($myconfig->{in_numberformat} == 1) {
632 # Extra input number format 1000.00 or 1000,00
633 $main::lxdebug->message(LXDebug::DEBUG2,
634 "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
637 #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
638 $amount = scalar reverse $amount;
640 #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
641 $amount =~ s/\./DOT/;
643 #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
646 #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
647 $amount =~ s/DOT/\./;
649 #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
650 $amount = scalar reverse $amount;
651 $main::lxdebug->message(LXDebug::DEBUG2,
652 "Parsed amount:" . $amount . "\n");
654 return ($amount * 1);
657 $main::lxdebug->message(LXDebug::DEBUG2,
658 "in_numberformat: " . $main::locale->text('equal Outputformat'));
659 $main::lxdebug->message(LXDebug::DEBUG2,
660 " = numberformat: $myconfig->{numberformat}");
661 if ( ($myconfig->{numberformat} eq '1.000,00')
662 || ($myconfig->{numberformat} eq '1000,00')) {
667 if ($myconfig->{numberformat} eq "1'000.00") {
673 $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
675 $main::lxdebug->leave_sub();
677 return ($amount * 1);
681 $main::lxdebug->enter_sub();
683 my ($self, $amount, $places) = @_;
686 # Rounding like "Kaufmannsrunden"
687 # Descr. http://de.wikipedia.org/wiki/Rundung
689 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
692 $amount = $amount * (10**($places));
693 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
695 $main::lxdebug->leave_sub();
697 return $round_amount;
702 $main::lxdebug->enter_sub();
704 my ($self, $myconfig, $userspath) = @_;
707 # Some variables used for page breaks
708 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
710 my ($current_page, $current_line, $current_row) = (1, 1, 0);
716 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
717 $self->format_string(grep(/notes/, keys(%{$self})));
719 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
720 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
722 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
723 qw(email tel fax name signature));
725 open(IN, "$self->{templates}/$self->{IN}")
726 or $self->error("$self->{IN} : $!");
731 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
733 # OUT is used for the media, screen, printer, email
734 # for postscript we store a copy in a temporary file
736 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
737 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
739 $self->{OUT} = ">$self->{tmpfile}";
743 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
745 open(OUT, ">-") or $self->error("STDOUT : $!");
749 # Do we have to run LaTeX two times? This is needed if
750 # the template contains page references.
753 # first we generate a tmpfile
754 # read file and replace <%variable%>
760 $two_passes = 1 if (/\\pageref/);
763 # detect pagebreak block and its parameters
764 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
765 $chars_per_line = $1;
766 $lines_on_first_page = $2;
767 $lines_on_second_page = $3;
770 last if (/\s*<%end pagebreak%>/);
777 if (/\s*<%foreach /) {
779 # this one we need for the count
781 $var =~ s/\s*<%foreach (.+?)%>/$1/;
783 last if (/\s*<%end /);
789 # display contents of $self->{number}[] array
790 for $i (0 .. $#{ $self->{$var} }) {
793 # Try to detect whether a manual page break is necessary
794 # but only if there was a <%pagebreak ...%> block before
796 if ($chars_per_line) {
798 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
801 my $_description = $self->{"description"}[$i];
802 while ($_description =~ /\\newline/) {
804 $_description =~ s/\\newline//;
806 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
808 if ($current_page == 1) {
809 $lpp = $lines_on_first_page;
811 $lpp = $lines_on_second_page;
814 # Yes we need a manual page break -- or the user has forced one
816 (($current_line + $lines) > $lpp)
817 || ($self->{"_forced_pagebreaks"}
818 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
822 # replace the special variables <%sumcarriedforward%>
825 my $psum = $self->format_amount($myconfig, $sum, 2);
826 $pb =~ s/<%sumcarriedforward%>/$psum/g;
827 $pb =~ s/<%lastpage%>/$current_page/g;
829 # only "normal" variables are supported here
830 # (no <%if, no <%foreach, no <%include)
832 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
834 # page break block is ready to rock
839 $current_line += $lines;
842 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
846 # don't parse par, we need it for each line
848 s/<%(.+?)%>/$self->{$1}[$i]/mg;
854 # if not comes before if!
855 if (/\s*<%if not /) {
857 # check if it is not set and display
859 s/\s*<%if not (.+?)%>/$1/;
861 unless ($self->{$_}) {
863 last if (/\s*<%end /);
873 last if (/\s*<%end /);
881 # check if it is set and display
883 s/\s*<%if (.+?)%>/$1/;
887 last if (/\s*<%end /);
897 last if (/\s*<%end /);
903 # check for <%include filename%>
904 if (/\s*<%include /) {
908 $var =~ s/\s*<%include (.+?)%>/$1/;
911 $var =~ s/(\/|\.\.)//g;
913 # prevent the infinite loop!
914 next if ($self->{"$var"});
916 open(INC, "$self->{templates}/$var")
917 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
926 s/<%(.+?)%>/$self->{$1}/g;
927 s/<nobr><\/nobr>/ /g;
934 # Convert the tex file to postscript
935 if ($self->{format} =~ /(postscript|pdf)/) {
938 $self->{cwd} = cwd();
939 $self->{tmpdir} = "$self->{cwd}/$userspath";
941 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
943 $self->{tmpfile} =~ s/$userspath\///g;
945 if ($self->{format} eq 'postscript') {
947 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
949 $self->error($self->cleanup) if ($?);
952 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
954 $self->error($self->cleanup) if ($?);
957 $self->{tmpfile} =~ s/tex$/dvi/;
959 system("dvips $self->{tmpfile} -o -q > /dev/null");
960 $self->error($self->cleanup . "dvips : $!") if ($?);
961 $self->{tmpfile} =~ s/dvi$/ps/;
963 if ($self->{format} eq 'pdf') {
965 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
967 $self->error($self->cleanup) if ($?);
970 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
972 $self->error($self->cleanup) if ($?);
974 $self->{tmpfile} =~ s/tex$/pdf/;
979 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
981 if ($self->{media} eq 'email') {
985 my $mail = new Mailer;
987 map { $mail->{$_} = $self->{$_} }
988 qw(cc bcc subject message version format charset);
989 $mail->{to} = qq|$self->{email}|;
990 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
991 $mail->{fileid} = "$fileid.";
993 # if we send html or plain text inline
994 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
995 $mail->{contenttype} = "text/html";
997 $mail->{message} =~ s/\r\n/<br>\n/g;
998 $myconfig->{signature} =~ s/\\n/<br>\n/g;
999 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1001 open(IN, $self->{tmpfile})
1002 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1004 $mail->{message} .= $_;
1011 @{ $mail->{attachments} } = ($self->{tmpfile});
1013 $myconfig->{signature} =~ s/\\n/\r\n/g;
1014 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1018 my $err = $mail->send($out);
1019 $self->error($self->cleanup . "$err") if ($err);
1023 $self->{OUT} = $out;
1025 my $numbytes = (-s $self->{tmpfile});
1026 open(IN, $self->{tmpfile})
1027 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1029 $self->{copies} = 1 unless $self->{media} eq 'printer';
1031 chdir("$self->{cwd}");
1033 for my $i (1 .. $self->{copies}) {
1035 open(OUT, $self->{OUT})
1036 or $self->error($self->cleanup . "$self->{OUT} : $!");
1039 # launch application
1040 print qq|Content-Type: application/$self->{format}
1041 Content-Disposition: attachment; filename="$self->{tmpfile}"
1042 Content-Length: $numbytes
1046 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1066 chdir("$self->{cwd}");
1067 $main::lxdebug->leave_sub();
1071 $main::lxdebug->enter_sub();
1075 chdir("$self->{tmpdir}");
1078 if (-f "$self->{tmpfile}.err") {
1079 open(FH, "$self->{tmpfile}.err");
1084 if ($self->{tmpfile}) {
1087 $self->{tmpfile} =~ s/\.\w+$//g;
1088 my $tmpfile = $self->{tmpfile};
1089 unlink(<$tmpfile.*>);
1092 chdir("$self->{cwd}");
1094 $main::lxdebug->leave_sub();
1100 $main::lxdebug->enter_sub();
1102 my ($self, @fields) = @_;
1105 %unique_fields = map({ $_ => 1 } @fields);
1106 @fields = keys(%unique_fields);
1108 foreach my $field (@fields) {
1109 next unless ($self->{$field} =~ /\<pagebreak\>/);
1110 $self->{$field} =~ s/\<pagebreak\>//g;
1111 if ($field =~ /.*_(\d+)$/) {
1112 if (!$self->{"_forced_pagebreaks"}) {
1113 $self->{"_forced_pagebreaks"} = [];
1115 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1119 my $format = $self->{format};
1120 if ($self->{format} =~ /(postscript|pdf)/) {
1127 '<', '>', quotemeta('\n'), '
1131 '&', quotemeta('\n'), '
1133 '"', '\$', '%', '_', '#', quotemeta('^'),
1134 '{', '}', '<', '>', '£', "\r"
1140 quotemeta('\n') => '<br>',
1151 quotemeta('^') => '\^\\',
1156 quotemeta('\n') => '\newline ',
1163 foreach my $key (@{ $replace{order}{$format} }) {
1164 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1167 # Allow some HTML markup to be converted into the output format's
1168 # corresponding markup code, e.g. bold or italic.
1169 if ('html' eq $format) {
1170 my @markup_replace = ('b', 'i', 's', 'u');
1172 foreach my $key (@markup_replace) {
1173 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1176 } elsif ('tex' eq $format) {
1177 my %markup_replace = ('b' => 'textbf',
1179 'u' => 'underline');
1181 foreach my $field (@fields) {
1182 foreach my $key (keys(%markup_replace)) {
1183 my $new = $markup_replace{$key};
1185 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1190 $main::lxdebug->leave_sub();
1194 $main::lxdebug->enter_sub();
1196 my ($self, $date, $myconfig) = @_;
1198 if ($date && $date =~ /\D/) {
1200 if ($myconfig->{dateformat} =~ /^yy/) {
1201 ($yy, $mm, $dd) = split /\D/, $date;
1203 if ($myconfig->{dateformat} =~ /^mm/) {
1204 ($mm, $dd, $yy) = split /\D/, $date;
1206 if ($myconfig->{dateformat} =~ /^dd/) {
1207 ($dd, $mm, $yy) = split /\D/, $date;
1212 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1213 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1215 $dd = "0$dd" if ($dd < 10);
1216 $mm = "0$mm" if ($mm < 10);
1218 $date = "$yy$mm$dd";
1221 $main::lxdebug->leave_sub();
1226 # Database routines used throughout
1229 $main::lxdebug->enter_sub();
1231 my ($self, $myconfig) = @_;
1233 # connect to database
1235 DBI->connect($myconfig->{dbconnect},
1236 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1240 if ($myconfig->{dboptions}) {
1241 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1244 $main::lxdebug->leave_sub();
1249 sub dbconnect_noauto {
1250 $main::lxdebug->enter_sub();
1252 my ($self, $myconfig) = @_;
1254 # connect to database
1256 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1257 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1261 if ($myconfig->{dboptions}) {
1262 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1265 $main::lxdebug->leave_sub();
1270 sub update_balance {
1271 $main::lxdebug->enter_sub();
1273 my ($self, $dbh, $table, $field, $where, $value) = @_;
1275 # if we have a value, go do it
1278 # retrieve balance from table
1279 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1280 my $sth = $dbh->prepare($query);
1282 $sth->execute || $self->dberror($query);
1283 my ($balance) = $sth->fetchrow_array;
1289 $query = "UPDATE $table SET $field = $balance WHERE $where";
1290 $dbh->do($query) || $self->dberror($query);
1292 $main::lxdebug->leave_sub();
1295 sub update_exchangerate {
1296 $main::lxdebug->enter_sub();
1298 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1300 # some sanity check for currency
1302 $main::lxdebug->leave_sub();
1306 my $query = qq|SELECT e.curr FROM exchangerate e
1307 WHERE e.curr = '$curr'
1308 AND e.transdate = '$transdate'
1310 my $sth = $dbh->prepare($query);
1311 $sth->execute || $self->dberror($query);
1314 if ($buy != 0 && $sell != 0) {
1315 $set = "buy = $buy, sell = $sell";
1316 } elsif ($buy != 0) {
1317 $set = "buy = $buy";
1318 } elsif ($sell != 0) {
1319 $set = "sell = $sell";
1322 if ($sth->fetchrow_array) {
1323 $query = qq|UPDATE exchangerate
1325 WHERE curr = '$curr'
1326 AND transdate = '$transdate'|;
1328 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1329 VALUES ('$curr', $buy, $sell, '$transdate')|;
1332 $dbh->do($query) || $self->dberror($query);
1334 $main::lxdebug->leave_sub();
1337 sub save_exchangerate {
1338 $main::lxdebug->enter_sub();
1340 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1342 my $dbh = $self->dbconnect($myconfig);
1344 my ($buy, $sell) = (0, 0);
1345 $buy = $rate if $fld eq 'buy';
1346 $sell = $rate if $fld eq 'sell';
1348 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1352 $main::lxdebug->leave_sub();
1355 sub get_exchangerate {
1356 $main::lxdebug->enter_sub();
1358 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1360 unless ($transdate) {
1361 $main::lxdebug->leave_sub();
1365 my $query = qq|SELECT e.$fld FROM exchangerate e
1366 WHERE e.curr = '$curr'
1367 AND e.transdate = '$transdate'|;
1368 my $sth = $dbh->prepare($query);
1369 $sth->execute || $self->dberror($query);
1371 my ($exchangerate) = $sth->fetchrow_array;
1374 $main::lxdebug->leave_sub();
1376 return $exchangerate;
1379 sub check_exchangerate {
1380 $main::lxdebug->enter_sub();
1382 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1384 unless ($transdate) {
1385 $main::lxdebug->leave_sub();
1389 my $dbh = $self->dbconnect($myconfig);
1391 my $query = qq|SELECT e.$fld FROM exchangerate e
1392 WHERE e.curr = '$currency'
1393 AND e.transdate = '$transdate'|;
1394 my $sth = $dbh->prepare($query);
1395 $sth->execute || $self->dberror($query);
1397 my ($exchangerate) = $sth->fetchrow_array;
1401 $main::lxdebug->leave_sub();
1403 return $exchangerate;
1407 $main::lxdebug->enter_sub();
1409 my ($self, $dbh, $id) = @_;
1413 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1415 if ($self->{"shipto$item"}) {
1416 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1418 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1423 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1424 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1425 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1426 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1427 '$self->{shiptozipcode}', '$self->{shiptocity}',
1428 '$self->{shiptocountry}', '$self->{shiptocontact}',
1429 '$self->{shiptophone}', '$self->{shiptofax}',
1430 '$self->{shiptoemail}')|;
1431 $dbh->do($query) || $self->dberror($query);
1434 $main::lxdebug->leave_sub();
1438 $main::lxdebug->enter_sub();
1440 my ($self, $dbh) = @_;
1442 my $query = qq|SELECT e.id, e.name FROM employee e
1443 WHERE e.login = '$self->{login}'|;
1444 my $sth = $dbh->prepare($query);
1445 $sth->execute || $self->dberror($query);
1447 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1448 $self->{employee_id} *= 1;
1452 $main::lxdebug->leave_sub();
1455 # get other contact for transaction and form - html/tex
1457 $main::lxdebug->enter_sub();
1459 my ($self, $dbh, $id) = @_;
1461 my $query = qq|SELECT c.*
1464 $sth = $dbh->prepare($query);
1465 $sth->execute || $self->dberror($query);
1467 $ref = $sth->fetchrow_hashref(NAME_lc);
1469 push @{ $self->{$_} }, $ref;
1472 $main::lxdebug->leave_sub();
1475 # get contacts for id, if no contact return {"","","","",""}
1477 $main::lxdebug->enter_sub();
1479 my ($self, $dbh, $id) = @_;
1481 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1483 WHERE cp_cv_id=$id|;
1484 my $sth = $dbh->prepare($query);
1485 $sth->execute || $self->dberror($query);
1488 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1489 push @{ $self->{all_contacts} }, $ref;
1494 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1497 $main::lxdebug->leave_sub();
1500 # this sub gets the id and name from $table
1502 $main::lxdebug->enter_sub();
1504 my ($self, $myconfig, $table) = @_;
1506 # connect to database
1507 my $dbh = $self->dbconnect($myconfig);
1509 my $name = $self->like(lc $self->{$table});
1510 my $customernumber = $self->like(lc $self->{customernumber});
1512 if ($self->{customernumber} ne "") {
1513 $query = qq~SELECT c.id, c.name,
1514 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1516 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1519 $query = qq~SELECT c.id, c.name,
1520 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1522 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1526 if ($self->{openinvoices}) {
1527 $query = qq~SELECT DISTINCT c.id, c.name,
1528 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1529 FROM $self->{arap} a
1530 JOIN $table c ON (a.${table}_id = c.id)
1531 WHERE NOT a.amount = a.paid
1532 AND lower(c.name) LIKE '$name'
1535 my $sth = $dbh->prepare($query);
1537 $sth->execute || $self->dberror($query);
1540 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1541 push(@{ $self->{name_list} }, $ref);
1547 $main::lxdebug->leave_sub();
1552 # the selection sub is used in the AR, AP, IS, IR and OE module
1555 $main::lxdebug->enter_sub();
1557 my ($self, $myconfig, $table, $module) = @_;
1560 my $dbh = $self->dbconnect($myconfig);
1562 my $query = qq|SELECT count(*) FROM $table|;
1563 my $sth = $dbh->prepare($query);
1564 $sth->execute || $self->dberror($query);
1565 my ($count) = $sth->fetchrow_array;
1568 # build selection list
1569 if ($count < $myconfig->{vclimit}) {
1570 $query = qq|SELECT id, name
1571 FROM $table WHERE not obsolete
1573 $sth = $dbh->prepare($query);
1574 $sth->execute || $self->dberror($query);
1576 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1577 push @{ $self->{"all_$table"} }, $ref;
1585 $self->get_employee($dbh);
1587 # setup sales contacts
1588 $query = qq|SELECT e.id, e.name
1591 AND NOT e.id = $self->{employee_id}|;
1592 $sth = $dbh->prepare($query);
1593 $sth->execute || $self->dberror($query);
1595 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1596 push @{ $self->{all_employees} }, $ref;
1601 push @{ $self->{all_employees} },
1602 { id => $self->{employee_id},
1603 name => $self->{employee} };
1605 # sort the whole thing
1606 @{ $self->{all_employees} } =
1607 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1609 if ($module eq 'AR') {
1611 # prepare query for departments
1612 $query = qq|SELECT d.id, d.description
1618 $query = qq|SELECT d.id, d.description
1623 $sth = $dbh->prepare($query);
1624 $sth->execute || $self->dberror($query);
1626 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1627 push @{ $self->{all_departments} }, $ref;
1632 $main::lxdebug->leave_sub();
1635 # this is only used for reports
1636 sub all_departments {
1637 $main::lxdebug->enter_sub();
1639 my ($self, $myconfig, $table) = @_;
1641 my $dbh = $self->dbconnect($myconfig);
1642 my $where = "1 = 1";
1644 if (defined $table) {
1645 if ($table eq 'customer') {
1646 $where = " d.role = 'P'";
1650 my $query = qq|SELECT d.id, d.description
1654 my $sth = $dbh->prepare($query);
1655 $sth->execute || $self->dberror($query);
1657 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1658 push @{ $self->{all_departments} }, $ref;
1664 $main::lxdebug->leave_sub();
1668 $main::lxdebug->enter_sub();
1670 my ($self, $module, $myconfig, $table) = @_;
1672 $self->all_vc($myconfig, $table, $module);
1674 # get last customers or vendors
1677 my $dbh = $self->dbconnect($myconfig);
1681 # now get the account numbers
1682 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1684 WHERE c.link LIKE '%$module%'
1687 $sth = $dbh->prepare($query);
1688 $sth->execute || $self->dberror($query);
1690 $self->{accounts} = "";
1691 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1693 foreach my $key (split /:/, $ref->{link}) {
1694 if ($key =~ /$module/) {
1696 # cross reference for keys
1697 $xkeyref{ $ref->{accno} } = $key;
1699 push @{ $self->{"${module}_links"}{$key} },
1700 { accno => $ref->{accno},
1701 description => $ref->{description},
1702 taxkey => $ref->{taxkey_id} };
1704 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1710 if (($module eq "AP") || ($module eq "AR")) {
1712 # get tax rates and description
1713 $query = qq| SELECT * FROM tax t|;
1714 $sth = $dbh->prepare($query);
1715 $sth->execute || $self->dberror($query);
1717 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1718 push @{ $self->{TAX} }, $ref;
1724 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1726 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1727 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1728 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1729 c.name AS $table, a.department_id, d.description AS department,
1730 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1731 a.employee_id, e.name AS employee, a.gldate
1733 JOIN $table c ON (a.${table}_id = c.id)
1734 LEFT JOIN employee e ON (e.id = a.employee_id)
1735 LEFT JOIN department d ON (d.id = a.department_id)
1736 WHERE a.id = $self->{id}|;
1737 $sth = $dbh->prepare($query);
1738 $sth->execute || $self->dberror($query);
1740 $ref = $sth->fetchrow_hashref(NAME_lc);
1741 foreach $key (keys %$ref) {
1742 $self->{$key} = $ref->{$key};
1746 # get amounts from individual entries
1747 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1748 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1750 JOIN chart c ON (c.id = a.chart_id)
1751 LEFT JOIN project p ON (p.id = a.project_id)
1752 LEFT Join tax t ON (a.taxkey = t.taxkey)
1753 WHERE a.trans_id = $self->{id}
1754 AND a.fx_transaction = '0'
1755 ORDER BY a.oid,a.transdate|;
1756 $sth = $dbh->prepare($query);
1757 $sth->execute || $self->dberror($query);
1759 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1761 # get exchangerate for currency
1762 $self->{exchangerate} =
1763 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1767 # store amounts in {acc_trans}{$key} for multiple accounts
1768 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1769 $ref->{exchangerate} =
1770 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1772 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1775 $ref->{index} = $index;
1777 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1781 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1782 (SELECT c.accno FROM chart c
1783 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1784 (SELECT c.accno FROM chart c
1785 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1787 $sth = $dbh->prepare($query);
1788 $sth->execute || $self->dberror($query);
1790 $ref = $sth->fetchrow_hashref(NAME_lc);
1791 map { $self->{$_} = $ref->{$_} } keys %$ref;
1797 $query = qq|SELECT current_date AS transdate,
1798 d.curr AS currencies, d.closedto, d.revtrans,
1799 (SELECT c.accno FROM chart c
1800 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1801 (SELECT c.accno FROM chart c
1802 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1804 $sth = $dbh->prepare($query);
1805 $sth->execute || $self->dberror($query);
1807 $ref = $sth->fetchrow_hashref(NAME_lc);
1808 map { $self->{$_} = $ref->{$_} } keys %$ref;
1811 if ($self->{"$self->{vc}_id"}) {
1813 # only setup currency
1814 ($self->{currency}) = split /:/, $self->{currencies};
1818 $self->lastname_used($dbh, $myconfig, $table, $module);
1820 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1822 # get exchangerate for currency
1823 $self->{exchangerate} =
1824 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1833 $main::lxdebug->leave_sub();
1837 $main::lxdebug->enter_sub();
1839 my ($self, $dbh, $myconfig, $table, $module) = @_;
1841 my $arap = ($table eq 'customer') ? "ar" : "ap";
1842 my $where = "1 = 1";
1844 if ($self->{type} =~ /_order/) {
1846 $where = "quotation = '0'";
1848 if ($self->{type} =~ /_quotation/) {
1850 $where = "quotation = '1'";
1853 my $query = qq|SELECT MAX(id) FROM $arap
1855 AND ${table}_id > 0|;
1856 my $sth = $dbh->prepare($query);
1857 $sth->execute || $self->dberror($query);
1859 my ($trans_id) = $sth->fetchrow_array;
1863 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1864 current_date + ct.terms AS duedate, a.department_id,
1865 d.description AS department
1867 JOIN $table ct ON (a.${table}_id = ct.id)
1868 LEFT JOIN department d ON (a.department_id = d.id)
1869 WHERE a.id = $trans_id|;
1870 $sth = $dbh->prepare($query);
1871 $sth->execute || $self->dberror($query);
1873 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1874 $self->{duedate}, $self->{department_id}, $self->{department})
1875 = $sth->fetchrow_array;
1878 $main::lxdebug->leave_sub();
1882 $main::lxdebug->enter_sub();
1884 my ($self, $myconfig, $thisdate, $days) = @_;
1886 my $dbh = $self->dbconnect($myconfig);
1891 my $dateformat = $myconfig->{dateformat};
1892 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1894 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1896 $sth = $dbh->prepare($query);
1897 $sth->execute || $self->dberror($query);
1899 $query = qq|SELECT current_date AS thisdate
1901 $sth = $dbh->prepare($query);
1902 $sth->execute || $self->dberror($query);
1905 ($thisdate) = $sth->fetchrow_array;
1910 $main::lxdebug->leave_sub();
1916 $main::lxdebug->enter_sub();
1918 my ($self, $string) = @_;
1920 if ($string !~ /%/) {
1921 $string = "%$string%";
1924 $string =~ s/\'/\'\'/g;
1926 $main::lxdebug->leave_sub();
1932 $main::lxdebug->enter_sub();
1934 my ($self, $flds, $new, $count, $numrows) = @_;
1938 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1944 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1946 $j = $item->{ndx} - 1;
1947 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1951 for $i ($count + 1 .. $numrows) {
1952 map { delete $self->{"${_}_$i"} } @{$flds};
1955 $main::lxdebug->leave_sub();
1959 $main::lxdebug->enter_sub();
1961 my ($self, $myconfig) = @_;
1965 my $dbh = $self->dbconnect_noauto($myconfig);
1967 my $query = qq|DELETE FROM status
1968 WHERE formname = '$self->{formname}'
1970 my $sth = $dbh->prepare($query) || $self->dberror($query);
1972 if ($self->{formname} =~ /(check|receipt)/) {
1973 for $i (1 .. $self->{rowcount}) {
1974 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1978 $sth->execute($self->{id}) || $self->dberror($query);
1982 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1983 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1985 my %queued = split / /, $self->{queued};
1987 if ($self->{formname} =~ /(check|receipt)/) {
1989 # this is a check or receipt, add one entry for each lineitem
1990 my ($accno) = split /--/, $self->{account};
1991 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1992 chart_id) VALUES (?, '$printed',
1993 '$queued{$self->{formname}}', '$self->{prinform}',
1994 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1995 $sth = $dbh->prepare($query) || $self->dberror($query);
1997 for $i (1 .. $self->{rowcount}) {
1998 if ($self->{"checked_$i"}) {
1999 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2004 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2005 spoolfile, formname)
2006 VALUES ($self->{id}, '$printed', '$emailed',
2007 '$queued{$self->{formname}}', '$self->{formname}')|;
2008 $dbh->do($query) || $self->dberror($query);
2014 $main::lxdebug->leave_sub();
2018 $main::lxdebug->enter_sub();
2020 my ($self, $dbh) = @_;
2022 my ($query, $printed, $emailed);
2024 my $formnames = $self->{printed};
2025 my $emailforms = $self->{emailed};
2027 my $query = qq|DELETE FROM status
2028 WHERE formname = '$self->{formname}'
2029 AND trans_id = $self->{id}|;
2030 $dbh->do($query) || $self->dberror($query);
2032 # this only applies to the forms
2033 # checks and receipts are posted when printed or queued
2035 if ($self->{queued}) {
2036 my %queued = split / /, $self->{queued};
2038 foreach my $formname (keys %queued) {
2039 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2040 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2042 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2043 spoolfile, formname)
2044 VALUES ($self->{id}, '$printed', '$emailed',
2045 '$queued{$formname}', '$formname')|;
2046 $dbh->do($query) || $self->dberror($query);
2048 $formnames =~ s/$self->{formname}//;
2049 $emailforms =~ s/$self->{formname}//;
2054 # save printed, emailed info
2055 $formnames =~ s/^ +//g;
2056 $emailforms =~ s/^ +//g;
2059 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2060 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2062 foreach my $formname (keys %status) {
2063 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2064 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2066 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2067 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2068 $dbh->do($query) || $self->dberror($query);
2071 $main::lxdebug->leave_sub();
2074 sub update_defaults {
2075 $main::lxdebug->enter_sub();
2077 my ($self, $myconfig, $fld) = @_;
2079 my $dbh = $self->dbconnect_noauto($myconfig);
2080 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2081 my $sth = $dbh->prepare($query);
2083 $sth->execute || $self->dberror($query);
2084 my ($var) = $sth->fetchrow_array;
2089 $query = qq|UPDATE defaults
2091 $dbh->do($query) || $form->dberror($query);
2096 $main::lxdebug->leave_sub();
2101 sub update_business {
2102 $main::lxdebug->enter_sub();
2104 my ($self, $myconfig, $business_id) = @_;
2106 my $dbh = $self->dbconnect_noauto($myconfig);
2108 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2109 my $sth = $dbh->prepare($query);
2111 $sth->execute || $self->dberror($query);
2112 my ($var) = $sth->fetchrow_array;
2117 $query = qq|UPDATE business
2118 SET customernumberinit = '$var' WHERE id=$business_id|;
2119 $dbh->do($query) || $form->dberror($query);
2124 $main::lxdebug->leave_sub();
2130 $main::lxdebug->enter_sub();
2132 my ($self, $myconfig, $salesman) = @_;
2134 my $dbh = $self->dbconnect($myconfig);
2136 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2137 my $sth = $dbh->prepare($query);
2138 $sth->execute || $self->dberror($query);
2141 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2142 push(@{ $self->{salesman_list} }, $ref);
2146 $main::lxdebug->leave_sub();
2151 sub get_partsgroup {
2152 $main::lxdebug->enter_sub();
2154 my ($self, $myconfig, $p) = @_;
2156 my $dbh = $self->dbconnect($myconfig);
2158 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2160 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2162 if ($p->{searchitems} eq 'part') {
2164 WHERE p.inventory_accno_id > 0|;
2166 if ($p->{searchitems} eq 'service') {
2168 WHERE p.inventory_accno_id IS NULL|;
2170 if ($p->{searchitems} eq 'assembly') {
2172 WHERE p.assembly = '1'|;
2174 if ($p->{searchitems} eq 'labor') {
2176 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2180 ORDER BY partsgroup|;
2183 $query = qq|SELECT id, partsgroup FROM partsgroup
2184 ORDER BY partsgroup|;
2187 if ($p->{language_code}) {
2188 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2189 t.description AS translation
2191 JOIN parts p ON (p.partsgroup_id = pg.id)
2192 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2193 ORDER BY translation|;
2196 my $sth = $dbh->prepare($query);
2197 $sth->execute || $self->dberror($query);
2199 $self->{all_partsgroup} = ();
2200 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2201 push @{ $self->{all_partsgroup} }, $ref;
2205 $main::lxdebug->leave_sub();
2208 sub get_pricegroup {
2209 $main::lxdebug->enter_sub();
2211 my ($self, $myconfig, $p) = @_;
2213 my $dbh = $self->dbconnect($myconfig);
2215 my $query = qq|SELECT p.id, p.pricegroup
2219 ORDER BY pricegroup|;
2222 $query = qq|SELECT id, pricegroup FROM pricegroup
2223 ORDER BY pricegroup|;
2226 my $sth = $dbh->prepare($query);
2227 $sth->execute || $self->dberror($query);
2229 $self->{all_pricegroup} = ();
2230 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2231 push @{ $self->{all_pricegroup} }, $ref;
2236 $main::lxdebug->leave_sub();
2240 my ($self, $dbh, $myconfig, $audittrail) = @_;
2242 # table, $reference, $formname, $action, $id, $transdate) = @_;
2249 $dbh = $self->dbconnect($myconfig);
2253 # if we have an id add audittrail, otherwise get a new timestamp
2255 if ($audittrail->{id}) {
2257 $query = qq|SELECT audittrail FROM defaults|;
2259 if ($dbh->selectrow_array($query)) {
2260 my ($null, $employee_id) = $self->get_employee($dbh);
2262 if ($self->{audittrail} && !$myconfig) {
2263 chop $self->{audittrail};
2265 my @a = split /\|/, $self->{audittrail};
2269 my @flds = qw(tablename reference formname action transdate);
2271 # put into hash and remove dups
2273 $key = "$a[2]$a[3]";
2275 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2279 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2280 formname, action, employee_id, transdate)
2281 VALUES ($audittrail->{id}, ?, ?,
2282 ?, ?, $employee_id, ?)|;
2283 my $sth = $dbh->prepare($query) || $self->dberror($query);
2287 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2291 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2293 $sth->execute || $self->dberror;
2298 if ($audittrail->{transdate}) {
2299 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2300 formname, action, employee_id, transdate) VALUES (
2301 $audittrail->{id}, '$audittrail->{tablename}', |
2302 . $dbh->quote($audittrail->{reference}) . qq|,
2303 '$audittrail->{formname}', '$audittrail->{action}',
2304 $employee_id, '$audittrail->{transdate}')|;
2306 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2307 formname, action, employee_id) VALUES ($audittrail->{id},
2308 '$audittrail->{tablename}', |
2309 . $dbh->quote($audittrail->{reference}) . qq|,
2310 '$audittrail->{formname}', '$audittrail->{action}',
2317 $query = qq|SELECT current_timestamp FROM defaults|;
2318 my ($timestamp) = $dbh->selectrow_array($query);
2321 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2324 $dbh->disconnect if $disconnect;
2333 $main::lxdebug->enter_sub();
2335 my ($type, $country, $NLS_file) = @_;
2339 if ($country && -d "locale/$country") {
2340 $self->{countrycode} = $country;
2341 eval { require "locale/$country/$NLS_file"; };
2344 $self->{NLS_file} = $NLS_file;
2346 push @{ $self->{LONG_MONTH} },
2347 ("January", "February", "March", "April",
2348 "May ", "June", "July", "August",
2349 "September", "October", "November", "December");
2350 push @{ $self->{SHORT_MONTH} },
2351 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2353 $main::lxdebug->leave_sub();
2359 my ($self, $text) = @_;
2361 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2365 $main::lxdebug->enter_sub();
2367 my ($self, $text) = @_;
2369 if (exists $self{subs}{$text}) {
2370 $text = $self{subs}{$text};
2372 if ($self->{countrycode} && $self->{NLS_file}) {
2374 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2378 $main::lxdebug->leave_sub();
2384 $main::lxdebug->enter_sub();
2386 my ($self, $myconfig, $date, $longformat) = @_;
2389 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2394 $spc = $myconfig->{dateformat};
2396 $spc = substr($spc, 1, 1);
2398 if ($date =~ /\D/) {
2399 if ($myconfig->{dateformat} =~ /^yy/) {
2400 ($yy, $mm, $dd) = split /\D/, $date;
2402 if ($myconfig->{dateformat} =~ /^mm/) {
2403 ($mm, $dd, $yy) = split /\D/, $date;
2405 if ($myconfig->{dateformat} =~ /^dd/) {
2406 ($dd, $mm, $yy) = split /\D/, $date;
2409 $date = substr($date, 2);
2410 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2415 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2416 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2418 if ($myconfig->{dateformat} =~ /^dd/) {
2419 if (defined $longformat && $longformat == 0) {
2421 $dd = "0$dd" if ($dd < 10);
2422 $mm = "0$mm" if ($mm < 10);
2423 $longdate = "$dd$spc$mm$spc$yy";
2426 $longdate .= ($spc eq '.') ? ". " : " ";
2427 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2429 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2431 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2432 # Lx-Office is mainly used in Germany or German speaking countries.
2433 if (defined $longformat && $longformat == 0) {
2435 $dd = "0$dd" if ($dd < 10);
2436 $mm = "0$mm" if ($mm < 10);
2437 $longdate = "$yy-$mm-$dd";
2439 $longdate = "$dd. ";
2440 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2443 if (defined $longformat && $longformat == 0) {
2445 $dd = "0$dd" if ($dd < 10);
2446 $mm = "0$mm" if ($mm < 10);
2447 $longdate = "$mm$spc$dd$spc$yy";
2449 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2455 $main::lxdebug->leave_sub();
2461 $main::lxdebug->enter_sub();
2463 my ($self, $myconfig, $date, $longformat) = @_;
2466 $main::lxdebug->leave_sub();
2471 $spc = $myconfig->{dateformat};
2473 $spc = substr($spc, 1, 1);
2475 if ($date =~ /\D/) {
2476 if ($myconfig->{dateformat} =~ /^yy/) {
2477 ($yy, $mm, $dd) = split /\D/, $date;
2478 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2479 ($mm, $dd, $yy) = split /\D/, $date;
2480 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2481 ($dd, $mm, $yy) = split /\D/, $date;
2484 $date = substr($date, 2);
2485 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2490 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2491 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2493 $main::lxdebug->leave_sub();
2494 return ($yy, $mm, $dd);