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 # The old fixed notation of <%variable%> is changed to a new dynamic one.
755 my ${pre} = '<%'; # Variable Prefix, must be regex save!
756 my ${suf} = '%>'; # Variable Suffix, must be regex save!
759 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
760 $self->format_string(grep(/notes/, keys(%{$self})));
762 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
763 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
765 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
766 qw(email tel fax name signature company address businessnumber));
768 open(IN, "$self->{templates}/$self->{IN}")
769 or $self->error("$self->{IN} : $!");
774 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
776 # OUT is used for the media, screen, printer, email
777 # for postscript we store a copy in a temporary file
779 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
780 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
782 $self->{OUT} = ">$self->{tmpfile}";
786 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
788 open(OUT, ">-") or $self->error("STDOUT : $!");
792 # Do we have to run LaTeX two times? This is needed if
793 # the template contains page references.
796 # first we generate a tmpfile
797 # read file and replace ${pre}variable${suf}
803 # Switch ${pre}analyse${suf} for template checking
804 # If ${pre}analyse${suf} is set in the template, you'll find the
805 # parsed output in the user Directory for analysing
807 # ${pre}analyse${suf} is a switch (allways off, on if set), not a Variable
808 # Set $form->{analysing}="" for system state: never analyse.
809 # Set $form->{analysing}="1" for system state: ever analyse.
810 $self->{analysing} = "1" if (/${pre}analyse${suf}/ && !defined $self->{analysing});
812 $two_passes = 1 if (/\\pageref/);
815 # detect pagebreak block and its parameters
816 if (/\s*${pre}pagebreak ([0-9]+) ([0-9]+) ([0-9]+)${suf}/) {
817 $chars_per_line = $1;
818 $lines_on_first_page = $2;
819 $lines_on_second_page = $3;
822 last if (/\s*${pre}end pagebreak${suf}/);
829 if (/\s*${pre}foreach /) {
831 # this one we need for the count
833 $var =~ s/\s*${pre}foreach (.+?)${suf}/$1/;
835 last if (/\s*${pre}end /);
841 # display contents of $self->{number}[] array
842 for $i (0 .. $#{ $self->{$var} }) {
845 # Try to detect whether a manual page break is necessary
846 # but only if there was a ${pre}pagebreak ...${suf} block before
848 if ($chars_per_line) {
850 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
853 my $_description = $self->{"description"}[$i];
854 while ($_description =~ /\\newline/) {
856 $_description =~ s/\\newline//;
858 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
860 if ($current_page == 1) {
861 $lpp = $lines_on_first_page;
863 $lpp = $lines_on_second_page;
866 # Yes we need a manual page break -- or the user has forced one
868 (($current_line + $lines) > $lpp)
869 || ($self->{"_forced_pagebreaks"}
870 && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
874 # replace the special variables ${pre}sumcarriedforward${suf}
875 # and ${pre}lastpage${suf}
877 my $psum = $self->format_amount($myconfig, $sum, 2);
878 $pb =~ s/${pre}sumcarriedforward${suf}/$psum/g;
879 $pb =~ s/${pre}lastpage${suf}/$current_page/g;
881 # only "normal" variables are supported here
882 # (no ${pre}if, no ${pre}foreach, no ${pre}include)
884 $pb =~ s/${pre}(.+?)${suf}/$self->{$1}/g;
886 # page break block is ready to rock
891 $current_line += $lines;
894 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
898 # don't parse par, we need it for each line
900 s/${pre}(.+?)${suf}/$self->{$1}[$i]/mg;
906 # if not comes before if!
907 if (/\s*${pre}if not /) {
909 # check if it is not set and display
911 s/\s*${pre}if not (.+?)${suf}/$1/;
913 unless ($self->{$_}) {
915 last if (/\s*${pre}end /);
925 last if (/\s*${pre}end /);
931 if (/\s*${pre}if /) {
933 # check if it is set and display
935 s/\s*${pre}if (.+?)${suf}/$1/;
939 last if (/\s*${pre}end /);
949 last if (/\s*${pre}end /);
955 # check for ${pre}include filename${suf}
956 if (/\s*${pre}include /) {
958 # get the directory/filename
960 $var =~ s/\s*${pre}include (.+?)${suf}/$1/;
962 # mangle filename on basedir
963 $var =~ s/^(\/|\.\.)//g;
965 # prevent the infinite loop!
966 next if ($self->{"$var"});
968 open(INC, "$self->{templates}/$var")
969 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
978 s/${pre}(.+?)${suf}/$self->{$1}/g;
979 s/<nobr><\/nobr>/ /g;
986 # Convert the tex file to postscript
987 if ($self->{format} =~ /(postscript|pdf)/) {
990 $self->{cwd} = cwd();
991 $self->{tmpdir} = "$self->{cwd}/$userspath";
993 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
995 $self->{tmpfile} =~ s/$userspath\///g;
997 if ($self->{format} eq 'postscript') {
999 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1001 $self->error($self->cleanup) if ($?);
1004 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1006 $self->error($self->cleanup) if ($?);
1009 $self->{tmpfile} =~ s/tex$/dvi/;
1011 system("dvips $self->{tmpfile} -o -q > /dev/null");
1012 $self->error($self->cleanup . "dvips : $!") if ($?);
1013 $self->{tmpfile} =~ s/dvi$/ps/;
1015 if ($self->{format} eq 'pdf') {
1017 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1019 $self->error($self->cleanup) if ($?);
1022 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
1024 $self->error($self->cleanup) if ($?);
1026 $self->{tmpfile} =~ s/tex$/pdf/;
1031 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
1033 if ($self->{media} eq 'email') {
1037 my $mail = new Mailer;
1039 map { $mail->{$_} = $self->{$_} }
1040 qw(cc bcc subject message version format charset);
1041 $mail->{to} = qq|$self->{email}|;
1042 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1043 $mail->{fileid} = "$fileid.";
1045 # if we send html or plain text inline
1046 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1047 $mail->{contenttype} = "text/html";
1049 $mail->{message} =~ s/\r\n/<br>\n/g;
1050 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1051 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
1053 open(IN, $self->{tmpfile})
1054 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1056 $mail->{message} .= $_;
1063 @{ $mail->{attachments} } = ($self->{tmpfile});
1065 $myconfig->{signature} =~ s/\\n/\r\n/g;
1066 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
1070 my $err = $mail->send($out);
1071 $self->error($self->cleanup . "$err") if ($err);
1075 $self->{OUT} = $out;
1077 my $numbytes = (-s $self->{tmpfile});
1078 open(IN, $self->{tmpfile})
1079 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1081 $self->{copies} = 1 unless $self->{media} eq 'printer';
1083 chdir("$self->{cwd}");
1085 for my $i (1 .. $self->{copies}) {
1087 open(OUT, $self->{OUT})
1088 or $self->error($self->cleanup . "$self->{OUT} : $!");
1091 # launch application
1092 print qq|Content-Type: application/$self->{format}
1093 Content-Disposition: attachment; filename="$self->{tmpfile}"
1094 Content-Length: $numbytes
1098 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1118 chdir("$self->{cwd}");
1119 $main::lxdebug->leave_sub();
1123 $main::lxdebug->enter_sub();
1127 chdir("$self->{tmpdir}");
1130 if (-f "$self->{tmpfile}.err") {
1131 open(FH, "$self->{tmpfile}.err");
1136 if ($self->{analysing} eq "") {
1137 if ($self->{tmpfile}) {
1140 $self->{tmpfile} =~ s/\.\w+$//g;
1141 my $tmpfile = $self->{tmpfile};
1142 unlink(<$tmpfile.*>);
1146 chdir("$self->{cwd}");
1148 $main::lxdebug->leave_sub();
1154 $main::lxdebug->enter_sub();
1156 my ($self, @fields) = @_;
1159 %unique_fields = map({ $_ => 1 } @fields);
1160 @fields = keys(%unique_fields);
1162 foreach my $field (@fields) {
1163 next unless ($self->{$field} =~ /\<pagebreak\>/);
1164 $self->{$field} =~ s/\<pagebreak\>//g;
1165 if ($field =~ /.*_(\d+)$/) {
1166 if (!$self->{"_forced_pagebreaks"}) {
1167 $self->{"_forced_pagebreaks"} = [];
1169 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1173 my $format = $self->{format};
1174 if ($self->{format} =~ /(postscript|pdf)/) {
1181 '<', '>', quotemeta('\n'), '
1185 '&', quotemeta('\n'), '
1187 '"', '\$', '%', '_', '#', quotemeta('^'),
1188 '{', '}', '<', '>', '£', "\r", '²'
1194 quotemeta('\n') => '<br>',
1205 quotemeta('^') => '\^\\',
1210 quotemeta('\n') => '\newline ',
1218 foreach my $key (@{ $replace{order}{$format} }) {
1219 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1222 # Allow some HTML markup to be converted into the output format's
1223 # corresponding markup code, e.g. bold or italic.
1224 if ('html' eq $format) {
1225 my @markup_replace = ('b', 'i', 's', 'u');
1227 foreach my $key (@markup_replace) {
1228 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1231 } elsif ('tex' eq $format) {
1232 my %markup_replace = ('b' => 'textbf',
1234 'u' => 'underline');
1236 foreach my $field (@fields) {
1237 foreach my $key (keys(%markup_replace)) {
1238 my $new = $markup_replace{$key};
1240 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1245 $main::lxdebug->leave_sub();
1249 $main::lxdebug->enter_sub();
1251 my ($self, $date, $myconfig) = @_;
1253 if ($date && $date =~ /\D/) {
1255 if ($myconfig->{dateformat} =~ /^yy/) {
1256 ($yy, $mm, $dd) = split /\D/, $date;
1258 if ($myconfig->{dateformat} =~ /^mm/) {
1259 ($mm, $dd, $yy) = split /\D/, $date;
1261 if ($myconfig->{dateformat} =~ /^dd/) {
1262 ($dd, $mm, $yy) = split /\D/, $date;
1267 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1268 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1270 $dd = "0$dd" if ($dd < 10);
1271 $mm = "0$mm" if ($mm < 10);
1273 $date = "$yy$mm$dd";
1276 $main::lxdebug->leave_sub();
1281 # Database routines used throughout
1284 $main::lxdebug->enter_sub();
1286 my ($self, $myconfig) = @_;
1288 # connect to database
1290 DBI->connect($myconfig->{dbconnect},
1291 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1295 if ($myconfig->{dboptions}) {
1296 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1299 $main::lxdebug->leave_sub();
1304 sub dbconnect_noauto {
1305 $main::lxdebug->enter_sub();
1307 my ($self, $myconfig) = @_;
1309 # connect to database
1311 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1312 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1316 if ($myconfig->{dboptions}) {
1317 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1320 $main::lxdebug->leave_sub();
1325 sub update_balance {
1326 $main::lxdebug->enter_sub();
1328 my ($self, $dbh, $table, $field, $where, $value) = @_;
1330 # if we have a value, go do it
1333 # retrieve balance from table
1334 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1335 my $sth = $dbh->prepare($query);
1337 $sth->execute || $self->dberror($query);
1338 my ($balance) = $sth->fetchrow_array;
1344 $query = "UPDATE $table SET $field = $balance WHERE $where";
1345 $dbh->do($query) || $self->dberror($query);
1347 $main::lxdebug->leave_sub();
1350 sub update_exchangerate {
1351 $main::lxdebug->enter_sub();
1353 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1355 # some sanity check for currency
1357 $main::lxdebug->leave_sub();
1361 my $query = qq|SELECT e.curr FROM exchangerate e
1362 WHERE e.curr = '$curr'
1363 AND e.transdate = '$transdate'
1365 my $sth = $dbh->prepare($query);
1366 $sth->execute || $self->dberror($query);
1369 if ($buy != 0 && $sell != 0) {
1370 $set = "buy = $buy, sell = $sell";
1371 } elsif ($buy != 0) {
1372 $set = "buy = $buy";
1373 } elsif ($sell != 0) {
1374 $set = "sell = $sell";
1377 if ($sth->fetchrow_array) {
1378 $query = qq|UPDATE exchangerate
1380 WHERE curr = '$curr'
1381 AND transdate = '$transdate'|;
1383 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1384 VALUES ('$curr', $buy, $sell, '$transdate')|;
1387 $dbh->do($query) || $self->dberror($query);
1389 $main::lxdebug->leave_sub();
1392 sub save_exchangerate {
1393 $main::lxdebug->enter_sub();
1395 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1397 my $dbh = $self->dbconnect($myconfig);
1399 my ($buy, $sell) = (0, 0);
1400 $buy = $rate if $fld eq 'buy';
1401 $sell = $rate if $fld eq 'sell';
1403 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1407 $main::lxdebug->leave_sub();
1410 sub get_exchangerate {
1411 $main::lxdebug->enter_sub();
1413 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1415 unless ($transdate) {
1416 $main::lxdebug->leave_sub();
1420 my $query = qq|SELECT e.$fld FROM exchangerate e
1421 WHERE e.curr = '$curr'
1422 AND e.transdate = '$transdate'|;
1423 my $sth = $dbh->prepare($query);
1424 $sth->execute || $self->dberror($query);
1426 my ($exchangerate) = $sth->fetchrow_array;
1429 $main::lxdebug->leave_sub();
1431 return $exchangerate;
1434 sub check_exchangerate {
1435 $main::lxdebug->enter_sub();
1437 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1439 unless ($transdate) {
1440 $main::lxdebug->leave_sub();
1444 my $dbh = $self->dbconnect($myconfig);
1446 my $query = qq|SELECT e.$fld FROM exchangerate e
1447 WHERE e.curr = '$currency'
1448 AND e.transdate = '$transdate'|;
1449 my $sth = $dbh->prepare($query);
1450 $sth->execute || $self->dberror($query);
1452 my ($exchangerate) = $sth->fetchrow_array;
1456 $main::lxdebug->leave_sub();
1458 return $exchangerate;
1462 $main::lxdebug->enter_sub();
1464 my ($self, $dbh, $id) = @_;
1468 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1470 if ($self->{"shipto$item"}) {
1471 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1473 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1478 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1479 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1480 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1481 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1482 '$self->{shiptozipcode}', '$self->{shiptocity}',
1483 '$self->{shiptocountry}', '$self->{shiptocontact}',
1484 '$self->{shiptophone}', '$self->{shiptofax}',
1485 '$self->{shiptoemail}')|;
1486 $dbh->do($query) || $self->dberror($query);
1489 $main::lxdebug->leave_sub();
1493 $main::lxdebug->enter_sub();
1495 my ($self, $dbh) = @_;
1497 my $query = qq|SELECT e.id, e.name FROM employee e
1498 WHERE e.login = '$self->{login}'|;
1499 my $sth = $dbh->prepare($query);
1500 $sth->execute || $self->dberror($query);
1502 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1503 $self->{employee_id} *= 1;
1507 $main::lxdebug->leave_sub();
1510 # get other contact for transaction and form - html/tex
1512 $main::lxdebug->enter_sub();
1514 my ($self, $dbh, $id) = @_;
1516 my $query = qq|SELECT c.*
1519 $sth = $dbh->prepare($query);
1520 $sth->execute || $self->dberror($query);
1522 $ref = $sth->fetchrow_hashref(NAME_lc);
1524 push @{ $self->{$_} }, $ref;
1527 $main::lxdebug->leave_sub();
1530 # get contacts for id, if no contact return {"","","","",""}
1532 $main::lxdebug->enter_sub();
1534 my ($self, $dbh, $id) = @_;
1536 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1538 WHERE cp_cv_id=$id|;
1539 my $sth = $dbh->prepare($query);
1540 $sth->execute || $self->dberror($query);
1543 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1544 push @{ $self->{all_contacts} }, $ref;
1549 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1552 $main::lxdebug->leave_sub();
1555 # this sub gets the id and name from $table
1557 $main::lxdebug->enter_sub();
1559 my ($self, $myconfig, $table) = @_;
1561 # connect to database
1562 my $dbh = $self->dbconnect($myconfig);
1564 my $name = $self->like(lc $self->{$table});
1565 my $customernumber = $self->like(lc $self->{customernumber});
1567 if ($self->{customernumber} ne "") {
1568 $query = qq~SELECT c.id, c.name,
1569 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1571 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1574 $query = qq~SELECT c.id, c.name,
1575 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1577 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1581 if ($self->{openinvoices}) {
1582 $query = qq~SELECT DISTINCT c.id, c.name,
1583 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1584 FROM $self->{arap} a
1585 JOIN $table c ON (a.${table}_id = c.id)
1586 WHERE NOT a.amount = a.paid
1587 AND lower(c.name) LIKE '$name'
1590 my $sth = $dbh->prepare($query);
1592 $sth->execute || $self->dberror($query);
1595 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1596 push(@{ $self->{name_list} }, $ref);
1602 $main::lxdebug->leave_sub();
1607 # the selection sub is used in the AR, AP, IS, IR and OE module
1610 $main::lxdebug->enter_sub();
1612 my ($self, $myconfig, $table, $module) = @_;
1615 my $dbh = $self->dbconnect($myconfig);
1617 my $query = qq|SELECT count(*) FROM $table|;
1618 my $sth = $dbh->prepare($query);
1619 $sth->execute || $self->dberror($query);
1620 my ($count) = $sth->fetchrow_array;
1623 # build selection list
1624 if ($count < $myconfig->{vclimit}) {
1625 $query = qq|SELECT id, name
1626 FROM $table WHERE not obsolete
1628 $sth = $dbh->prepare($query);
1629 $sth->execute || $self->dberror($query);
1631 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1632 push @{ $self->{"all_$table"} }, $ref;
1640 $self->get_employee($dbh);
1642 # setup sales contacts
1643 $query = qq|SELECT e.id, e.name
1646 AND NOT e.id = $self->{employee_id}|;
1647 $sth = $dbh->prepare($query);
1648 $sth->execute || $self->dberror($query);
1650 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1651 push @{ $self->{all_employees} }, $ref;
1656 push @{ $self->{all_employees} },
1657 { id => $self->{employee_id},
1658 name => $self->{employee} };
1660 # sort the whole thing
1661 @{ $self->{all_employees} } =
1662 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1664 if ($module eq 'AR') {
1666 # prepare query for departments
1667 $query = qq|SELECT d.id, d.description
1673 $query = qq|SELECT d.id, d.description
1678 $sth = $dbh->prepare($query);
1679 $sth->execute || $self->dberror($query);
1681 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1682 push @{ $self->{all_departments} }, $ref;
1687 $main::lxdebug->leave_sub();
1690 # this is only used for reports
1691 sub all_departments {
1692 $main::lxdebug->enter_sub();
1694 my ($self, $myconfig, $table) = @_;
1696 my $dbh = $self->dbconnect($myconfig);
1697 my $where = "1 = 1";
1699 if (defined $table) {
1700 if ($table eq 'customer') {
1701 $where = " d.role = 'P'";
1705 my $query = qq|SELECT d.id, d.description
1709 my $sth = $dbh->prepare($query);
1710 $sth->execute || $self->dberror($query);
1712 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1713 push @{ $self->{all_departments} }, $ref;
1719 $main::lxdebug->leave_sub();
1723 $main::lxdebug->enter_sub();
1725 my ($self, $module, $myconfig, $table) = @_;
1727 $self->all_vc($myconfig, $table, $module);
1729 # get last customers or vendors
1732 my $dbh = $self->dbconnect($myconfig);
1736 # now get the account numbers
1737 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1739 WHERE c.link LIKE '%$module%'
1742 $sth = $dbh->prepare($query);
1743 $sth->execute || $self->dberror($query);
1745 $self->{accounts} = "";
1746 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1748 foreach my $key (split /:/, $ref->{link}) {
1749 if ($key =~ /$module/) {
1751 # cross reference for keys
1752 $xkeyref{ $ref->{accno} } = $key;
1754 push @{ $self->{"${module}_links"}{$key} },
1755 { accno => $ref->{accno},
1756 description => $ref->{description},
1757 taxkey => $ref->{taxkey_id} };
1759 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1765 if (($module eq "AP") || ($module eq "AR")) {
1767 # get tax rates and description
1768 $query = qq| SELECT * FROM tax t|;
1769 $sth = $dbh->prepare($query);
1770 $sth->execute || $self->dberror($query);
1772 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1773 push @{ $self->{TAX} }, $ref;
1779 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1781 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1782 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1783 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1784 c.name AS $table, a.department_id, d.description AS department,
1785 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1786 a.employee_id, e.name AS employee, a.gldate
1788 JOIN $table c ON (a.${table}_id = c.id)
1789 LEFT JOIN employee e ON (e.id = a.employee_id)
1790 LEFT JOIN department d ON (d.id = a.department_id)
1791 WHERE a.id = $self->{id}|;
1792 $sth = $dbh->prepare($query);
1793 $sth->execute || $self->dberror($query);
1795 $ref = $sth->fetchrow_hashref(NAME_lc);
1796 foreach $key (keys %$ref) {
1797 $self->{$key} = $ref->{$key};
1801 # get amounts from individual entries
1802 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1803 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1805 JOIN chart c ON (c.id = a.chart_id)
1806 LEFT JOIN project p ON (p.id = a.project_id)
1807 LEFT Join tax t ON (a.taxkey = t.taxkey)
1808 WHERE a.trans_id = $self->{id}
1809 AND a.fx_transaction = '0'
1810 ORDER BY a.oid,a.transdate|;
1811 $sth = $dbh->prepare($query);
1812 $sth->execute || $self->dberror($query);
1814 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1816 # get exchangerate for currency
1817 $self->{exchangerate} =
1818 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1822 # store amounts in {acc_trans}{$key} for multiple accounts
1823 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1824 $ref->{exchangerate} =
1825 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1827 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1830 $ref->{index} = $index;
1832 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1836 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1837 (SELECT c.accno FROM chart c
1838 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1839 (SELECT c.accno FROM chart c
1840 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1842 $sth = $dbh->prepare($query);
1843 $sth->execute || $self->dberror($query);
1845 $ref = $sth->fetchrow_hashref(NAME_lc);
1846 map { $self->{$_} = $ref->{$_} } keys %$ref;
1852 $query = qq|SELECT current_date AS transdate,
1853 d.curr AS currencies, d.closedto, d.revtrans,
1854 (SELECT c.accno FROM chart c
1855 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1856 (SELECT c.accno FROM chart c
1857 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1859 $sth = $dbh->prepare($query);
1860 $sth->execute || $self->dberror($query);
1862 $ref = $sth->fetchrow_hashref(NAME_lc);
1863 map { $self->{$_} = $ref->{$_} } keys %$ref;
1866 if ($self->{"$self->{vc}_id"}) {
1868 # only setup currency
1869 ($self->{currency}) = split /:/, $self->{currencies};
1873 $self->lastname_used($dbh, $myconfig, $table, $module);
1875 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1877 # get exchangerate for currency
1878 $self->{exchangerate} =
1879 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1888 $main::lxdebug->leave_sub();
1892 $main::lxdebug->enter_sub();
1894 my ($self, $dbh, $myconfig, $table, $module) = @_;
1896 my $arap = ($table eq 'customer') ? "ar" : "ap";
1897 my $where = "1 = 1";
1899 if ($self->{type} =~ /_order/) {
1901 $where = "quotation = '0'";
1903 if ($self->{type} =~ /_quotation/) {
1905 $where = "quotation = '1'";
1908 my $query = qq|SELECT MAX(id) FROM $arap
1910 AND ${table}_id > 0|;
1911 my $sth = $dbh->prepare($query);
1912 $sth->execute || $self->dberror($query);
1914 my ($trans_id) = $sth->fetchrow_array;
1918 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1919 current_date + ct.terms AS duedate, a.department_id,
1920 d.description AS department
1922 JOIN $table ct ON (a.${table}_id = ct.id)
1923 LEFT JOIN department d ON (a.department_id = d.id)
1924 WHERE a.id = $trans_id|;
1925 $sth = $dbh->prepare($query);
1926 $sth->execute || $self->dberror($query);
1928 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1929 $self->{duedate}, $self->{department_id}, $self->{department})
1930 = $sth->fetchrow_array;
1933 $main::lxdebug->leave_sub();
1937 $main::lxdebug->enter_sub();
1939 my ($self, $myconfig, $thisdate, $days) = @_;
1941 my $dbh = $self->dbconnect($myconfig);
1946 my $dateformat = $myconfig->{dateformat};
1947 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1949 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1951 $sth = $dbh->prepare($query);
1952 $sth->execute || $self->dberror($query);
1954 $query = qq|SELECT current_date AS thisdate
1956 $sth = $dbh->prepare($query);
1957 $sth->execute || $self->dberror($query);
1960 ($thisdate) = $sth->fetchrow_array;
1965 $main::lxdebug->leave_sub();
1971 $main::lxdebug->enter_sub();
1973 my ($self, $string) = @_;
1975 if ($string !~ /%/) {
1976 $string = "%$string%";
1979 $string =~ s/\'/\'\'/g;
1981 $main::lxdebug->leave_sub();
1987 $main::lxdebug->enter_sub();
1989 my ($self, $flds, $new, $count, $numrows) = @_;
1993 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1999 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2001 $j = $item->{ndx} - 1;
2002 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2006 for $i ($count + 1 .. $numrows) {
2007 map { delete $self->{"${_}_$i"} } @{$flds};
2010 $main::lxdebug->leave_sub();
2014 $main::lxdebug->enter_sub();
2016 my ($self, $myconfig) = @_;
2020 my $dbh = $self->dbconnect_noauto($myconfig);
2022 my $query = qq|DELETE FROM status
2023 WHERE formname = '$self->{formname}'
2025 my $sth = $dbh->prepare($query) || $self->dberror($query);
2027 if ($self->{formname} =~ /(check|receipt)/) {
2028 for $i (1 .. $self->{rowcount}) {
2029 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2033 $sth->execute($self->{id}) || $self->dberror($query);
2037 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2038 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2040 my %queued = split / /, $self->{queued};
2042 if ($self->{formname} =~ /(check|receipt)/) {
2044 # this is a check or receipt, add one entry for each lineitem
2045 my ($accno) = split /--/, $self->{account};
2046 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2047 chart_id) VALUES (?, '$printed',
2048 '$queued{$self->{formname}}', '$self->{prinform}',
2049 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2050 $sth = $dbh->prepare($query) || $self->dberror($query);
2052 for $i (1 .. $self->{rowcount}) {
2053 if ($self->{"checked_$i"}) {
2054 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2059 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2060 spoolfile, formname)
2061 VALUES ($self->{id}, '$printed', '$emailed',
2062 '$queued{$self->{formname}}', '$self->{formname}')|;
2063 $dbh->do($query) || $self->dberror($query);
2069 $main::lxdebug->leave_sub();
2073 $main::lxdebug->enter_sub();
2075 my ($self, $dbh) = @_;
2077 my ($query, $printed, $emailed);
2079 my $formnames = $self->{printed};
2080 my $emailforms = $self->{emailed};
2082 my $query = qq|DELETE FROM status
2083 WHERE formname = '$self->{formname}'
2084 AND trans_id = $self->{id}|;
2085 $dbh->do($query) || $self->dberror($query);
2087 # this only applies to the forms
2088 # checks and receipts are posted when printed or queued
2090 if ($self->{queued}) {
2091 my %queued = split / /, $self->{queued};
2093 foreach my $formname (keys %queued) {
2094 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2095 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2097 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2098 spoolfile, formname)
2099 VALUES ($self->{id}, '$printed', '$emailed',
2100 '$queued{$formname}', '$formname')|;
2101 $dbh->do($query) || $self->dberror($query);
2103 $formnames =~ s/$self->{formname}//;
2104 $emailforms =~ s/$self->{formname}//;
2109 # save printed, emailed info
2110 $formnames =~ s/^ +//g;
2111 $emailforms =~ s/^ +//g;
2114 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2115 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2117 foreach my $formname (keys %status) {
2118 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2119 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2121 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2122 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2123 $dbh->do($query) || $self->dberror($query);
2126 $main::lxdebug->leave_sub();
2129 sub update_defaults {
2130 $main::lxdebug->enter_sub();
2132 my ($self, $myconfig, $fld) = @_;
2134 my $dbh = $self->dbconnect_noauto($myconfig);
2135 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2136 my $sth = $dbh->prepare($query);
2138 $sth->execute || $self->dberror($query);
2139 my ($var) = $sth->fetchrow_array;
2144 $query = qq|UPDATE defaults
2146 $dbh->do($query) || $self->dberror($query);
2151 $main::lxdebug->leave_sub();
2156 sub update_business {
2157 $main::lxdebug->enter_sub();
2159 my ($self, $myconfig, $business_id) = @_;
2161 my $dbh = $self->dbconnect_noauto($myconfig);
2163 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2164 my $sth = $dbh->prepare($query);
2166 $sth->execute || $self->dberror($query);
2167 my ($var) = $sth->fetchrow_array;
2172 $query = qq|UPDATE business
2173 SET customernumberinit = '$var' WHERE id=$business_id|;
2174 $dbh->do($query) || $self->dberror($query);
2179 $main::lxdebug->leave_sub();
2185 $main::lxdebug->enter_sub();
2187 my ($self, $myconfig, $salesman) = @_;
2189 my $dbh = $self->dbconnect($myconfig);
2191 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2192 my $sth = $dbh->prepare($query);
2193 $sth->execute || $self->dberror($query);
2196 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2197 push(@{ $self->{salesman_list} }, $ref);
2201 $main::lxdebug->leave_sub();
2206 sub get_partsgroup {
2207 $main::lxdebug->enter_sub();
2209 my ($self, $myconfig, $p) = @_;
2211 my $dbh = $self->dbconnect($myconfig);
2213 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2215 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2217 if ($p->{searchitems} eq 'part') {
2219 WHERE p.inventory_accno_id > 0|;
2221 if ($p->{searchitems} eq 'service') {
2223 WHERE p.inventory_accno_id IS NULL|;
2225 if ($p->{searchitems} eq 'assembly') {
2227 WHERE p.assembly = '1'|;
2229 if ($p->{searchitems} eq 'labor') {
2231 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2235 ORDER BY partsgroup|;
2238 $query = qq|SELECT id, partsgroup FROM partsgroup
2239 ORDER BY partsgroup|;
2242 if ($p->{language_code}) {
2243 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2244 t.description AS translation
2246 JOIN parts p ON (p.partsgroup_id = pg.id)
2247 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2248 ORDER BY translation|;
2251 my $sth = $dbh->prepare($query);
2252 $sth->execute || $self->dberror($query);
2254 $self->{all_partsgroup} = ();
2255 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2256 push @{ $self->{all_partsgroup} }, $ref;
2260 $main::lxdebug->leave_sub();
2263 sub get_pricegroup {
2264 $main::lxdebug->enter_sub();
2266 my ($self, $myconfig, $p) = @_;
2268 my $dbh = $self->dbconnect($myconfig);
2270 my $query = qq|SELECT p.id, p.pricegroup
2274 ORDER BY pricegroup|;
2277 $query = qq|SELECT id, pricegroup FROM pricegroup
2278 ORDER BY pricegroup|;
2281 my $sth = $dbh->prepare($query);
2282 $sth->execute || $self->dberror($query);
2284 $self->{all_pricegroup} = ();
2285 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2286 push @{ $self->{all_pricegroup} }, $ref;
2291 $main::lxdebug->leave_sub();
2295 my ($self, $dbh, $myconfig, $audittrail) = @_;
2297 # table, $reference, $formname, $action, $id, $transdate) = @_;
2304 $dbh = $self->dbconnect($myconfig);
2308 # if we have an id add audittrail, otherwise get a new timestamp
2310 if ($audittrail->{id}) {
2312 $query = qq|SELECT audittrail FROM defaults|;
2314 if ($dbh->selectrow_array($query)) {
2315 my ($null, $employee_id) = $self->get_employee($dbh);
2317 if ($self->{audittrail} && !$myconfig) {
2318 chop $self->{audittrail};
2320 my @a = split /\|/, $self->{audittrail};
2324 my @flds = qw(tablename reference formname action transdate);
2326 # put into hash and remove dups
2328 $key = "$a[2]$a[3]";
2330 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2334 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2335 formname, action, employee_id, transdate)
2336 VALUES ($audittrail->{id}, ?, ?,
2337 ?, ?, $employee_id, ?)|;
2338 my $sth = $dbh->prepare($query) || $self->dberror($query);
2342 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2346 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2348 $sth->execute || $self->dberror;
2353 if ($audittrail->{transdate}) {
2354 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2355 formname, action, employee_id, transdate) VALUES (
2356 $audittrail->{id}, '$audittrail->{tablename}', |
2357 . $dbh->quote($audittrail->{reference}) . qq|,
2358 '$audittrail->{formname}', '$audittrail->{action}',
2359 $employee_id, '$audittrail->{transdate}')|;
2361 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2362 formname, action, employee_id) VALUES ($audittrail->{id},
2363 '$audittrail->{tablename}', |
2364 . $dbh->quote($audittrail->{reference}) . qq|,
2365 '$audittrail->{formname}', '$audittrail->{action}',
2372 $query = qq|SELECT current_timestamp FROM defaults|;
2373 my ($timestamp) = $dbh->selectrow_array($query);
2376 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2379 $dbh->disconnect if $disconnect;
2388 $main::lxdebug->enter_sub();
2390 my ($type, $country, $NLS_file) = @_;
2394 if ($country && -d "locale/$country") {
2395 $self->{countrycode} = $country;
2396 eval { require "locale/$country/$NLS_file"; };
2399 $self->{NLS_file} = $NLS_file;
2401 push @{ $self->{LONG_MONTH} },
2402 ("January", "February", "March", "April",
2403 "May ", "June", "July", "August",
2404 "September", "October", "November", "December");
2405 push @{ $self->{SHORT_MONTH} },
2406 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2408 $main::lxdebug->leave_sub();
2414 my ($self, $text) = @_;
2416 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2420 $main::lxdebug->enter_sub();
2422 my ($self, $text) = @_;
2424 if (exists $self{subs}{$text}) {
2425 $text = $self{subs}{$text};
2427 if ($self->{countrycode} && $self->{NLS_file}) {
2429 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2433 $main::lxdebug->leave_sub();
2439 $main::lxdebug->enter_sub();
2441 my ($self, $myconfig, $date, $longformat) = @_;
2444 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2449 $spc = $myconfig->{dateformat};
2451 $spc = substr($spc, 1, 1);
2453 if ($date =~ /\D/) {
2454 if ($myconfig->{dateformat} =~ /^yy/) {
2455 ($yy, $mm, $dd) = split /\D/, $date;
2457 if ($myconfig->{dateformat} =~ /^mm/) {
2458 ($mm, $dd, $yy) = split /\D/, $date;
2460 if ($myconfig->{dateformat} =~ /^dd/) {
2461 ($dd, $mm, $yy) = split /\D/, $date;
2464 $date = substr($date, 2);
2465 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2470 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2471 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2473 if ($myconfig->{dateformat} =~ /^dd/) {
2474 if (defined $longformat && $longformat == 0) {
2476 $dd = "0$dd" if ($dd < 10);
2477 $mm = "0$mm" if ($mm < 10);
2478 $longdate = "$dd$spc$mm$spc$yy";
2481 $longdate .= ($spc eq '.') ? ". " : " ";
2482 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2484 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2486 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2487 # Lx-Office is mainly used in Germany or German speaking countries.
2488 if (defined $longformat && $longformat == 0) {
2490 $dd = "0$dd" if ($dd < 10);
2491 $mm = "0$mm" if ($mm < 10);
2492 $longdate = "$yy-$mm-$dd";
2494 $longdate = "$dd. ";
2495 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2498 if (defined $longformat && $longformat == 0) {
2500 $dd = "0$dd" if ($dd < 10);
2501 $mm = "0$mm" if ($mm < 10);
2502 $longdate = "$mm$spc$dd$spc$yy";
2504 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2510 $main::lxdebug->leave_sub();
2516 $main::lxdebug->enter_sub();
2518 my ($self, $myconfig, $date, $longformat) = @_;
2521 $main::lxdebug->leave_sub();
2526 $spc = $myconfig->{dateformat};
2528 $spc = substr($spc, 1, 1);
2530 if ($date =~ /\D/) {
2531 if ($myconfig->{dateformat} =~ /^yy/) {
2532 ($yy, $mm, $dd) = split /\D/, $date;
2533 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2534 ($mm, $dd, $yy) = split /\D/, $date;
2535 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2536 ($dd, $mm, $yy) = split /\D/, $date;
2539 $date = substr($date, 2);
2540 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2545 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2546 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2548 $main::lxdebug->leave_sub();
2549 return ($yy, $mm, $dd);