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 #======================================================================
41 $main::lxdebug->enter_sub();
45 my @pairs = split(/&/, $input);
48 my ($name, $value) = split(/=/, $_, 2);
49 $in{$name} = unescape(undef, $value);
52 $main::lxdebug->leave_sub();
57 sub _request_to_hash {
58 $main::lxdebug->enter_sub();
61 my ($i, $loc, $key, $val);
62 my (%ATTACH, $f, $header, $header_body, $len, $buf);
63 my ($boundary, @list, $size, $body, $x, $blah, $name);
65 if ($ENV{'CONTENT_TYPE'}
66 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
67 $boundary = quotemeta('--' . $1);
68 @list = split(/$boundary/, $input);
70 # For some reason there are always 2 extra, that are empty
73 for ($x = 1; $x <= $size; $x++) {
74 $header_body = $list[$x];
75 $header_body =~ /\r\n\r\n|\n\n/;
77 # Here we split the header and body
82 # Now we try to get the file name
84 $name =~ /name=\"(.+)\"/;
85 ($name, $blah) = split(/\"/, $1);
87 # If the form name is not attach, then we need to parse this like
89 if ($name ne "attach") {
90 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
91 $ATTACH{$name} = $body;
93 # Otherwise it is an attachment and we need to finish it up
94 } elsif ($name eq "attach") {
95 $header =~ /filename=\"(.+)\"/;
96 $ATTACH{'FILE_NAME'} = $1;
97 $ATTACH{'FILE_NAME'} =~ s/\"//g;
98 $ATTACH{'FILE_NAME'} =~ s/\s//g;
99 $ATTACH{'FILE_CONTENT'} = $body;
101 for ($i = $x; $list[$i]; $i++) {
102 $list[$i] =~ s/^.+name=$//;
103 $list[$i] =~ /\"(\w+)\"/;
109 $main::lxdebug->leave_sub();
113 $main::lxdebug->leave_sub();
114 return _input_to_hash($input);
119 $main::lxdebug->enter_sub();
125 read(STDIN, $_, $ENV{CONTENT_LENGTH});
127 if ($ENV{QUERY_STRING}) {
128 $_ = $ENV{QUERY_STRING};
135 my %parameters = _request_to_hash($_);
136 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
138 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
140 $self->{action} = lc $self->{action};
141 $self->{action} =~ s/( |-|,|#)/_/g;
143 $self->{version} = "2.1.2";
144 $self->{dbversion} = "2.1.2";
146 $main::lxdebug->leave_sub();
152 $main::lxdebug->enter_sub();
158 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
160 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
166 my ($self, $str, $beenthere) = @_;
168 # for Apache 2 we escape strings twice
169 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
170 # $str = $self->escape($str, 1);
173 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
175 $main::lxdebug->leave_sub();
181 $main::lxdebug->enter_sub();
183 my ($self, $str) = @_;
188 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
190 $main::lxdebug->leave_sub();
196 $main::lxdebug->enter_sub();
198 my ($self, $msg) = @_;
200 if ($ENV{HTTP_USER_AGENT}) {
208 <h2 class=error>Error!</h2>
220 if ($self->{error_function}) {
221 &{ $self->{error_function} }($msg);
227 $main::lxdebug->leave_sub();
231 $main::lxdebug->enter_sub();
233 my ($self, $msg) = @_;
235 if ($ENV{HTTP_USER_AGENT}) {
238 if (!$self->{header}) {
251 if ($self->{info_function}) {
252 &{ $self->{info_function} }($msg);
258 $main::lxdebug->leave_sub();
262 $main::lxdebug->enter_sub();
264 my ($self, $str, $cols, $maxrows) = @_;
268 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
270 $maxrows = $rows unless defined $maxrows;
272 $main::lxdebug->leave_sub();
274 return ($rows > $maxrows) ? $maxrows : $rows;
278 $main::lxdebug->enter_sub();
280 my ($self, $msg) = @_;
282 $self->error("$msg\n" . $DBI::errstr);
284 $main::lxdebug->leave_sub();
288 $main::lxdebug->enter_sub();
290 my ($self, $name, $msg) = @_;
292 if ($self->{$name} =~ /^\s*$/) {
295 $main::lxdebug->leave_sub();
299 $main::lxdebug->enter_sub();
303 if ($self->{header}) {
304 $main::lxdebug->leave_sub();
308 my ($stylesheet, $favicon, $charset);
310 if ($ENV{HTTP_USER_AGENT}) {
312 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
314 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
318 if ($self->{favicon} && (-f "$self->{favicon}")) {
320 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
324 if ($self->{charset}) {
326 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
329 if ($self->{landscape}) {
330 $pagelayout = qq|<style type="text/css">
331 \@page { size:landscape; }
334 if ($self->{fokus}) {
335 $fokus = qq|<script type="text/javascript">
337 function fokus(){document.$self->{fokus}.focus();}
344 if ($self->{jsscript} == 1) {
347 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
348 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
349 Â <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
350 Â <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
356 ? "$self->{title} - $self->{titlebar}"
359 print qq|Content-Type: text/html
362 <title>$self->{titlebar}</title>
375 $main::lxdebug->leave_sub();
378 # write Trigger JavaScript-Code ($qty = 1 - only one Trigger)
380 $main::lxdebug->enter_sub();
382 my ($self, $myconfig, $qty,
383 $inputField_1, $align_1, $button_1,
384 $inputField_2, $align_2, $button_2)
387 # set dateform for jsscript
389 $ifFormat = "%d.%m.%Y";
390 if ($myconfig->{dateformat} eq "dd.mm.yy") {
391 $ifFormat = "%d.%m.%Y";
393 if ($myconfig->{dateformat} eq "dd-mm-yy") {
394 $ifFormat = "%d-%m-%Y";
396 if ($myconfig->{dateformat} eq "dd/mm/yy") {
397 $ifFormat = "%d/%m/%Y";
399 if ($myconfig->{dateformat} eq "mm/dd/yy") {
400 $ifFormat = "%m/%d/%Y";
402 if ($myconfig->{dateformat} eq "mm-dd-yy") {
403 $ifFormat = "%m-%d-%Y";
405 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
406 $ifFormat = "%Y-%m-%d";
417    inputField  : "$inputField_1",
418    ifFormat    :"$ifFormat",
419    align    : "$align_1",    Â
420    button      : "$button_1"
429    inputField  : "$inputField_2",
430    ifFormat    :"$ifFormat",
431    align    : "$align_2",    Â
432    button      : "$button_2"
438 <script type="text/javascript">
446 $main::lxdebug->leave_sub();
449 } #end sub write_trigger
452 $main::lxdebug->enter_sub();
454 my ($self, $msg) = @_;
456 if ($self->{callback}) {
458 ($script, $argv) = split(/\?/, $self->{callback});
459 exec("perl", "$script", $argv);
467 $main::lxdebug->leave_sub();
470 # sort of columns removed - empty sub
472 $main::lxdebug->enter_sub();
474 my ($self, @columns) = @_;
476 $main::lxdebug->leave_sub();
482 $main::lxdebug->enter_sub();
484 my ($self, $myconfig, $amount, $places, $dash) = @_;
486 if ($places =~ /\d/) {
487 $amount = $self->round_amount($amount, $places);
490 # is the amount negative
491 my $negative = ($amount < 0);
494 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
496 my ($whole, $dec) = split /\./, "$amount";
498 $amount = join '', reverse split //, $whole;
500 if ($myconfig->{numberformat} eq '1,000.00') {
501 $amount =~ s/\d{3,}?/$&,/g;
503 $amount = join '', reverse split //, $amount;
504 $amount .= "\.$dec" if ($dec ne "");
507 if ($myconfig->{numberformat} eq '1.000,00') {
508 $amount =~ s/\d{3,}?/$&./g;
510 $amount = join '', reverse split //, $amount;
511 $amount .= ",$dec" if ($dec ne "");
514 if ($myconfig->{numberformat} eq '1000,00') {
516 $amount .= ",$dec" if ($dec ne "");
520 $amount = ($negative) ? "($amount)" : "$amount";
521 } elsif ($dash =~ /DRCR/) {
522 $amount = ($negative) ? "$amount DR" : "$amount CR";
524 $amount = ($negative) ? "-$amount" : "$amount";
528 if ($dash eq "0" && $places) {
529 if ($myconfig->{numberformat} eq '1.000,00') {
530 $amount = "0" . "," . "0" x $places;
532 $amount = "0" . "." . "0" x $places;
535 $amount = ($dash ne "") ? "$dash" : "0";
539 $main::lxdebug->leave_sub();
545 $main::lxdebug->enter_sub();
547 my ($self, $myconfig, $amount) = @_;
549 if (!(substr($amount, -3, 1) eq ".")) {
550 if ( ($myconfig->{numberformat} eq '1.000,00')
551 || ($myconfig->{numberformat} eq '1000,00')) {
559 $main::lxdebug->leave_sub();
561 return ($amount * 1);
565 $main::lxdebug->enter_sub();
567 my ($self, $amount, $places) = @_;
570 # $places = 3 if $places == 2;
572 if (($places * 1) >= 0) {
576 sprintf("%.${places}f",
577 $amount + (1 / (10**($places + 3))) * (($amount > 0) ? 1 : -1));
581 sprintf("%.f", $amount / (10**$places) + (($amount > 0) ? 0.1 : -0.1)) *
585 $main::lxdebug->leave_sub();
591 $main::lxdebug->enter_sub();
593 my ($self, $myconfig, $userspath) = @_;
596 # Some variables used for page breaks
597 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
599 my ($current_page, $current_line, $current_row) = (1, 1, 0);
605 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
606 $self->format_string(grep(/notes/, keys(%{$self})));
608 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
609 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
611 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
612 qw(email tel fax name signature));
614 open(IN, "$self->{templates}/$self->{IN}")
615 or $self->error("$self->{IN} : $!");
620 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
622 # OUT is used for the media, screen, printer, email
623 # for postscript we store a copy in a temporary file
625 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
626 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
628 $self->{OUT} = ">$self->{tmpfile}";
632 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
634 open(OUT, ">-") or $self->error("STDOUT : $!");
638 # Do we have to run LaTeX two times? This is needed if
639 # the template contains page references.
642 # first we generate a tmpfile
643 # read file and replace <%variable%>
649 $two_passes = 1 if (/\\pageref/);
652 # detect pagebreak block and its parameters
653 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
654 $chars_per_line = $1;
655 $lines_on_first_page = $2;
656 $lines_on_second_page = $3;
659 last if (/\s*<%end pagebreak%>/);
666 if (/\s*<%foreach /) {
668 # this one we need for the count
670 $var =~ s/\s*<%foreach (.+?)%>/$1/;
672 last if (/\s*<%end /);
678 # display contents of $self->{number}[] array
679 for $i (0 .. $#{ $self->{$var} }) {
682 # Try to detect whether a manual page break is necessary
683 # but only if there was a <%pagebreak ...%> block before
685 if ($chars_per_line) {
687 int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
690 my $_description = $self->{"description"}[$i];
691 while ($_description =~ /\\newline/) {
693 $_description =~ s/\\newline//;
695 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
697 if ($current_page == 1) {
698 $lpp = $lines_on_first_page;
700 $lpp = $lines_on_second_page;
703 # Yes we need a manual page break -- or the user has forced one
704 if ((($current_line + $lines) > $lpp) ||
705 ($self->{"_forced_pagebreaks"} && grep(/^${current_row}$/, @{$self->{"_forced_pagebreaks"}}))) {
708 # replace the special variables <%sumcarriedforward%>
711 my $psum = $self->format_amount($myconfig, $sum, 2);
712 $pb =~ s/<%sumcarriedforward%>/$psum/g;
713 $pb =~ s/<%lastpage%>/$current_page/g;
715 # only "normal" variables are supported here
716 # (no <%if, no <%foreach, no <%include)
718 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
720 # page break block is ready to rock
725 $current_line += $lines;
728 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
732 # don't parse par, we need it for each line
734 s/<%(.+?)%>/$self->{$1}[$i]/mg;
740 # if not comes before if!
741 if (/\s*<%if not /) {
743 # check if it is not set and display
745 s/\s*<%if not (.+?)%>/$1/;
747 unless ($self->{$_}) {
749 last if (/\s*<%end /);
759 last if (/\s*<%end /);
767 # check if it is set and display
769 s/\s*<%if (.+?)%>/$1/;
773 last if (/\s*<%end /);
783 last if (/\s*<%end /);
789 # check for <%include filename%>
790 if (/\s*<%include /) {
794 $var =~ s/\s*<%include (.+?)%>/$1/;
797 $var =~ s/(\/|\.\.)//g;
799 # prevent the infinite loop!
800 next if ($self->{"$var"});
802 open(INC, "$self->{templates}/$var")
803 or $self->error($self->cleanup . "$self->{templates}/$var : $!");
812 s/<%(.+?)%>/$self->{$1}/g;
819 # Convert the tex file to postscript
820 if ($self->{format} =~ /(postscript|pdf)/) {
823 $self->{cwd} = cwd();
824 $self->{tmpdir} = "$self->{cwd}/$userspath";
826 chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
828 $self->{tmpfile} =~ s/$userspath\///g;
830 if ($self->{format} eq 'postscript') {
832 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
834 $self->error($self->cleanup) if ($?);
837 "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
839 $self->error($self->cleanup) if ($?);
842 $self->{tmpfile} =~ s/tex$/dvi/;
844 system("dvips $self->{tmpfile} -o -q > /dev/null");
845 $self->error($self->cleanup . "dvips : $!") if ($?);
846 $self->{tmpfile} =~ s/dvi$/ps/;
848 if ($self->{format} eq 'pdf') {
850 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
852 $self->error($self->cleanup) if ($?);
855 "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
857 $self->error($self->cleanup) if ($?);
859 $self->{tmpfile} =~ s/tex$/pdf/;
864 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
866 if ($self->{media} eq 'email') {
870 my $mail = new Mailer;
872 map { $mail->{$_} = $self->{$_} }
873 qw(cc bcc subject message version format charset);
874 $mail->{to} = qq|$self->{email}|;
875 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
876 $mail->{fileid} = "$fileid.";
878 # if we send html or plain text inline
879 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
880 $mail->{contenttype} = "text/html";
882 $mail->{message} =~ s/\r\n/<br>\n/g;
883 $myconfig->{signature} =~ s/\\n/<br>\n/g;
884 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
886 open(IN, $self->{tmpfile})
887 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
889 $mail->{message} .= $_;
896 @{ $mail->{attachments} } = ($self->{tmpfile});
898 $myconfig->{signature} =~ s/\\n/\r\n/g;
899 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
903 my $err = $mail->send($out);
904 $self->error($self->cleanup . "$err") if ($err);
910 my $numbytes = (-s $self->{tmpfile});
911 open(IN, $self->{tmpfile})
912 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
914 $self->{copies} = 1 unless $self->{media} eq 'printer';
916 chdir("$self->{cwd}");
918 for my $i (1 .. $self->{copies}) {
920 open(OUT, $self->{OUT})
921 or $self->error($self->cleanup . "$self->{OUT} : $!");
925 print qq|Content-Type: application/$self->{format}
926 Content-Disposition: attachment; filename="$self->{tmpfile}"
927 Content-Length: $numbytes
931 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
951 chdir("$self->{cwd}");
952 $main::lxdebug->leave_sub();
956 $main::lxdebug->enter_sub();
960 chdir("$self->{tmpdir}");
963 if (-f "$self->{tmpfile}.err") {
964 open(FH, "$self->{tmpfile}.err");
969 if ($self->{tmpfile}) {
972 $self->{tmpfile} =~ s/\.\w+$//g;
973 my $tmpfile = $self->{tmpfile};
974 unlink(<$tmpfile.*>);
977 chdir("$self->{cwd}");
979 $main::lxdebug->leave_sub();
985 $main::lxdebug->enter_sub();
987 my ($self, @fields) = @_;
990 %unique_fields = map({ $_ => 1 } @fields);
991 @fields = keys(%unique_fields);
993 foreach my $field (@fields) {
994 next unless ($self->{$field} =~ /\<pagebreak\>/);
995 $self->{$field} =~ s/\<pagebreak\>//g;
996 if ($field =~ /.*_(\d+)$/) {
997 if ($self->{"_forced_pagebreaks"}) {
998 $self->{"_forced_pagebreaks"} = [];
1000 push(@{ $self->{"_forced_pagebreaks"} }, "$1");
1004 my $format = $self->{format};
1005 if ($self->{format} =~ /(postscript|pdf)/) {
1012 '<', '>', quotemeta('\n'), '
1016 '&', quotemeta('\n'), '
1018 '"', '\$', '%', '_', '#', quotemeta('^'),
1019 '{', '}', '<', '>', '£', "\r"
1025 quotemeta('\n') => '<br>',
1036 quotemeta('^') => '\^\\',
1041 quotemeta('\n') => '\newline ',
1048 foreach my $key (@{ $replace{order}{$format} }) {
1049 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
1052 # Allow some HTML markup to be converted into the output format's
1053 # corresponding markup code, e.g. bold or italic.
1054 if ('html' eq $format) {
1055 my @markup_replace = ('b', 'i', 's', 'u');
1057 foreach my $key (@markup_replace) {
1058 map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
1061 } elsif ('tex' eq $format) {
1062 my %markup_replace = ('b' => 'textbf',
1064 'u' => 'underline');
1066 foreach my $field (@fields) {
1067 if ($field =~ /descrip/) {
1068 print(STDERR "QFT: ${field}: " . $self->{$field} . "\n");
1070 foreach my $key (keys(%markup_replace)) {
1071 my $new = $markup_replace{$key};
1073 s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
1078 $main::lxdebug->leave_sub();
1082 $main::lxdebug->enter_sub();
1084 my ($self, $date, $myconfig) = @_;
1086 if ($date && $date =~ /\D/) {
1088 if ($myconfig->{dateformat} =~ /^yy/) {
1089 ($yy, $mm, $dd) = split /\D/, $date;
1091 if ($myconfig->{dateformat} =~ /^mm/) {
1092 ($mm, $dd, $yy) = split /\D/, $date;
1094 if ($myconfig->{dateformat} =~ /^dd/) {
1095 ($dd, $mm, $yy) = split /\D/, $date;
1100 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1101 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1103 $dd = "0$dd" if ($dd < 10);
1104 $mm = "0$mm" if ($mm < 10);
1106 $date = "$yy$mm$dd";
1109 $main::lxdebug->leave_sub();
1114 # Database routines used throughout
1117 $main::lxdebug->enter_sub();
1119 my ($self, $myconfig) = @_;
1121 # connect to database
1123 DBI->connect($myconfig->{dbconnect},
1124 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1128 if ($myconfig->{dboptions}) {
1129 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1132 $main::lxdebug->leave_sub();
1137 sub dbconnect_noauto {
1138 $main::lxdebug->enter_sub();
1140 my ($self, $myconfig) = @_;
1142 # connect to database
1144 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1145 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1149 if ($myconfig->{dboptions}) {
1150 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1153 $main::lxdebug->leave_sub();
1158 sub update_balance {
1159 $main::lxdebug->enter_sub();
1161 my ($self, $dbh, $table, $field, $where, $value) = @_;
1163 # if we have a value, go do it
1166 # retrieve balance from table
1167 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1168 my $sth = $dbh->prepare($query);
1170 $sth->execute || $self->dberror($query);
1171 my ($balance) = $sth->fetchrow_array;
1177 $query = "UPDATE $table SET $field = $balance WHERE $where";
1178 $dbh->do($query) || $self->dberror($query);
1180 $main::lxdebug->leave_sub();
1183 sub update_exchangerate {
1184 $main::lxdebug->enter_sub();
1186 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1188 # some sanity check for currency
1190 $main::lxdebug->leave_sub();
1194 my $query = qq|SELECT e.curr FROM exchangerate e
1195 WHERE e.curr = '$curr'
1196 AND e.transdate = '$transdate'
1198 my $sth = $dbh->prepare($query);
1199 $sth->execute || $self->dberror($query);
1202 if ($buy != 0 && $sell != 0) {
1203 $set = "buy = $buy, sell = $sell";
1204 } elsif ($buy != 0) {
1205 $set = "buy = $buy";
1206 } elsif ($sell != 0) {
1207 $set = "sell = $sell";
1210 if ($sth->fetchrow_array) {
1211 $query = qq|UPDATE exchangerate
1213 WHERE curr = '$curr'
1214 AND transdate = '$transdate'|;
1216 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1217 VALUES ('$curr', $buy, $sell, '$transdate')|;
1220 $dbh->do($query) || $self->dberror($query);
1222 $main::lxdebug->leave_sub();
1225 sub save_exchangerate {
1226 $main::lxdebug->enter_sub();
1228 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1230 my $dbh = $self->dbconnect($myconfig);
1232 my ($buy, $sell) = (0, 0);
1233 $buy = $rate if $fld eq 'buy';
1234 $sell = $rate if $fld eq 'sell';
1236 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1240 $main::lxdebug->leave_sub();
1243 sub get_exchangerate {
1244 $main::lxdebug->enter_sub();
1246 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1248 my $query = qq|SELECT e.$fld FROM exchangerate e
1249 WHERE e.curr = '$curr'
1250 AND e.transdate = '$transdate'|;
1251 my $sth = $dbh->prepare($query);
1252 $sth->execute || $self->dberror($query);
1254 my ($exchangerate) = $sth->fetchrow_array;
1257 $main::lxdebug->leave_sub();
1259 return $exchangerate;
1262 sub check_exchangerate {
1263 $main::lxdebug->enter_sub();
1265 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1267 unless ($transdate) {
1268 $main::lxdebug->leave_sub();
1272 my $dbh = $self->dbconnect($myconfig);
1274 my $query = qq|SELECT e.$fld FROM exchangerate e
1275 WHERE e.curr = '$currency'
1276 AND e.transdate = '$transdate'|;
1277 my $sth = $dbh->prepare($query);
1278 $sth->execute || $self->dberror($query);
1280 my ($exchangerate) = $sth->fetchrow_array;
1284 $main::lxdebug->leave_sub();
1286 return $exchangerate;
1290 $main::lxdebug->enter_sub();
1292 my ($self, $dbh, $id) = @_;
1296 my $item (qw(name department_1 department_2 street zipcode city country contact phone fax email)) {
1297 if ($self->{"shipto$item"}) {
1298 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1300 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1304 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1305 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1306 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1307 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1308 '$self->{shiptozipcode}', '$self->{shiptocity}',
1309 '$self->{shiptocountry}', '$self->{shiptocontact}',
1310 '$self->{shiptophone}', '$self->{shiptofax}',
1311 '$self->{shiptoemail}')|;
1312 $dbh->do($query) || $self->dberror($query);
1315 $main::lxdebug->leave_sub();
1319 $main::lxdebug->enter_sub();
1321 my ($self, $dbh) = @_;
1323 my $query = qq|SELECT e.id, e.name FROM employee e
1324 WHERE e.login = '$self->{login}'|;
1325 my $sth = $dbh->prepare($query);
1326 $sth->execute || $self->dberror($query);
1328 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1329 $self->{employee_id} *= 1;
1333 $main::lxdebug->leave_sub();
1336 # get other contact for transaction and form - html/tex
1338 $main::lxdebug->enter_sub();
1340 my ($self, $dbh, $id) = @_;
1342 my $query = qq|SELECT c.*
1345 $sth = $dbh->prepare($query);
1346 $sth->execute || $self->dberror($query);
1348 $ref = $sth->fetchrow_hashref(NAME_lc);
1350 push @{ $self->{$_} }, $ref;
1353 $main::lxdebug->leave_sub();
1356 # get contacts for id, if no contact return {"","","","",""}
1358 $main::lxdebug->enter_sub();
1360 my ($self, $dbh, $id) = @_;
1362 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1364 WHERE cp_cv_id=$id|;
1365 my $sth = $dbh->prepare($query);
1366 $sth->execute || $self->dberror($query);
1369 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1370 push @{ $self->{all_contacts} }, $ref;
1375 push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
1378 $main::lxdebug->leave_sub();
1381 # this sub gets the id and name from $table
1383 $main::lxdebug->enter_sub();
1385 my ($self, $myconfig, $table) = @_;
1387 # connect to database
1388 my $dbh = $self->dbconnect($myconfig);
1390 my $name = $self->like(lc $self->{$table});
1391 my $customernumber = $self->like(lc $self->{customernumber});
1393 if ($self->{customernumber} ne "") {
1394 $query = qq~SELECT c.id, c.name,
1395 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1397 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1400 $query = qq~SELECT c.id, c.name,
1401 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1403 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1407 if ($self->{openinvoices}) {
1408 $query = qq~SELECT DISTINCT c.id, c.name,
1409 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1410 FROM $self->{arap} a
1411 JOIN $table c ON (a.${table}_id = c.id)
1412 WHERE NOT a.amount = a.paid
1413 AND lower(c.name) LIKE '$name'
1416 my $sth = $dbh->prepare($query);
1418 $sth->execute || $self->dberror($query);
1421 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1422 push(@{ $self->{name_list} }, $ref);
1428 $main::lxdebug->leave_sub();
1433 # the selection sub is used in the AR, AP, IS, IR and OE module
1436 $main::lxdebug->enter_sub();
1438 my ($self, $myconfig, $table, $module) = @_;
1441 my $dbh = $self->dbconnect($myconfig);
1443 my $query = qq|SELECT count(*) FROM $table|;
1444 my $sth = $dbh->prepare($query);
1445 $sth->execute || $self->dberror($query);
1446 my ($count) = $sth->fetchrow_array;
1449 # build selection list
1450 if ($count < $myconfig->{vclimit}) {
1451 $query = qq|SELECT id, name
1452 FROM $table WHERE not obsolete
1454 $sth = $dbh->prepare($query);
1455 $sth->execute || $self->dberror($query);
1457 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1458 push @{ $self->{"all_$table"} }, $ref;
1466 $self->get_employee($dbh);
1468 # setup sales contacts
1469 $query = qq|SELECT e.id, e.name
1472 AND NOT e.id = $self->{employee_id}|;
1473 $sth = $dbh->prepare($query);
1474 $sth->execute || $self->dberror($query);
1476 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1477 push @{ $self->{all_employees} }, $ref;
1482 push @{ $self->{all_employees} },
1483 { id => $self->{employee_id},
1484 name => $self->{employee} };
1486 # sort the whole thing
1487 @{ $self->{all_employees} } =
1488 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1490 if ($module eq 'AR') {
1492 # prepare query for departments
1493 $query = qq|SELECT d.id, d.description
1499 $query = qq|SELECT d.id, d.description
1504 $sth = $dbh->prepare($query);
1505 $sth->execute || $self->dberror($query);
1507 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1508 push @{ $self->{all_departments} }, $ref;
1513 $main::lxdebug->leave_sub();
1516 # this is only used for reports
1517 sub all_departments {
1518 $main::lxdebug->enter_sub();
1520 my ($self, $myconfig, $table) = @_;
1522 my $dbh = $self->dbconnect($myconfig);
1523 my $where = "1 = 1";
1525 if (defined $table) {
1526 if ($table eq 'customer') {
1527 $where = " d.role = 'P'";
1531 my $query = qq|SELECT d.id, d.description
1535 my $sth = $dbh->prepare($query);
1536 $sth->execute || $self->dberror($query);
1538 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1539 push @{ $self->{all_departments} }, $ref;
1545 $main::lxdebug->leave_sub();
1549 $main::lxdebug->enter_sub();
1551 my ($self, $module, $myconfig, $table) = @_;
1553 $self->all_vc($myconfig, $table, $module);
1555 # get last customers or vendors
1558 my $dbh = $self->dbconnect($myconfig);
1562 # now get the account numbers
1564 qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1566 WHERE c.link LIKE '%$module%'
1569 $sth = $dbh->prepare($query);
1570 $sth->execute || $self->dberror($query);
1572 $self->{accounts} = "";
1573 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1575 foreach my $key (split /:/, $ref->{link}) {
1576 if ($key =~ /$module/) {
1578 # cross reference for keys
1579 $xkeyref{ $ref->{accno} } = $key;
1581 push @{ $self->{"${module}_links"}{$key} },
1582 { accno => $ref->{accno},
1583 description => $ref->{description},
1584 taxkey => $ref->{taxkey_id} };
1586 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1592 if (($module eq "AP") || ($module eq "AR")) {
1594 # get tax rates and description
1595 $query = qq| SELECT * FROM tax t|;
1596 $sth = $dbh->prepare($query);
1597 $sth->execute || $self->dberror($query);
1599 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1600 push @{ $self->{TAX} }, $ref;
1606 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1608 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1609 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1610 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1611 c.name AS $table, a.department_id, d.description AS department,
1612 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1613 a.employee_id, e.name AS employee, a.gldate
1615 JOIN $table c ON (a.${table}_id = c.id)
1616 LEFT JOIN employee e ON (e.id = a.employee_id)
1617 LEFT JOIN department d ON (d.id = a.department_id)
1618 WHERE a.id = $self->{id}|;
1619 $sth = $dbh->prepare($query);
1620 $sth->execute || $self->dberror($query);
1622 $ref = $sth->fetchrow_hashref(NAME_lc);
1623 foreach $key (keys %$ref) {
1624 $self->{$key} = $ref->{$key};
1628 # get amounts from individual entries
1629 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1630 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1632 JOIN chart c ON (c.id = a.chart_id)
1633 LEFT JOIN project p ON (p.id = a.project_id)
1634 LEFT Join tax t ON (a.taxkey = t.taxkey)
1635 WHERE a.trans_id = $self->{id}
1636 AND a.fx_transaction = '0'
1637 ORDER BY a.transdate|;
1638 $sth = $dbh->prepare($query);
1639 $sth->execute || $self->dberror($query);
1641 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1643 # get exchangerate for currency
1644 $self->{exchangerate} =
1645 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1648 # store amounts in {acc_trans}{$key} for multiple accounts
1649 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1650 $ref->{exchangerate} =
1651 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1654 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1658 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1659 (SELECT c.accno FROM chart c
1660 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1661 (SELECT c.accno FROM chart c
1662 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1664 $sth = $dbh->prepare($query);
1665 $sth->execute || $self->dberror($query);
1667 $ref = $sth->fetchrow_hashref(NAME_lc);
1668 map { $self->{$_} = $ref->{$_} } keys %$ref;
1674 $query = qq|SELECT current_date AS transdate,
1675 d.curr AS currencies, d.closedto, d.revtrans,
1676 (SELECT c.accno FROM chart c
1677 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1678 (SELECT c.accno FROM chart c
1679 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1681 $sth = $dbh->prepare($query);
1682 $sth->execute || $self->dberror($query);
1684 $ref = $sth->fetchrow_hashref(NAME_lc);
1685 map { $self->{$_} = $ref->{$_} } keys %$ref;
1688 if ($self->{"$self->{vc}_id"}) {
1690 # only setup currency
1691 ($self->{currency}) = split /:/, $self->{currencies};
1695 $self->lastname_used($dbh, $myconfig, $table, $module);
1697 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1699 # get exchangerate for currency
1700 $self->{exchangerate} =
1701 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1710 $main::lxdebug->leave_sub();
1714 $main::lxdebug->enter_sub();
1716 my ($self, $dbh, $myconfig, $table, $module) = @_;
1718 my $arap = ($table eq 'customer') ? "ar" : "ap";
1719 my $where = "1 = 1";
1721 if ($self->{type} =~ /_order/) {
1723 $where = "quotation = '0'";
1725 if ($self->{type} =~ /_quotation/) {
1727 $where = "quotation = '1'";
1730 my $query = qq|SELECT id FROM $arap
1731 WHERE id IN (SELECT MAX(id) FROM $arap
1733 AND ${table}_id > 0)|;
1734 my $sth = $dbh->prepare($query);
1735 $sth->execute || $self->dberror($query);
1737 my ($trans_id) = $sth->fetchrow_array;
1741 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1742 current_date + ct.terms AS duedate, a.department_id,
1743 d.description AS department
1745 JOIN $table ct ON (a.${table}_id = ct.id)
1746 LEFT JOIN department d ON (a.department_id = d.id)
1747 WHERE a.id = $trans_id|;
1748 $sth = $dbh->prepare($query);
1749 $sth->execute || $self->dberror($query);
1751 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1752 $self->{duedate}, $self->{department_id}, $self->{department})
1753 = $sth->fetchrow_array;
1756 $main::lxdebug->leave_sub();
1760 $main::lxdebug->enter_sub();
1762 my ($self, $myconfig, $thisdate, $days) = @_;
1764 my $dbh = $self->dbconnect($myconfig);
1769 my $dateformat = $myconfig->{dateformat};
1770 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1772 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1774 $sth = $dbh->prepare($query);
1775 $sth->execute || $self->dberror($query);
1777 $query = qq|SELECT current_date AS thisdate
1779 $sth = $dbh->prepare($query);
1780 $sth->execute || $self->dberror($query);
1783 ($thisdate) = $sth->fetchrow_array;
1788 $main::lxdebug->leave_sub();
1794 $main::lxdebug->enter_sub();
1796 my ($self, $string) = @_;
1798 if ($string !~ /%/) {
1799 $string = "%$string%";
1802 $string =~ s/\'/\'\'/g;
1804 $main::lxdebug->leave_sub();
1810 $main::lxdebug->enter_sub();
1812 my ($self, $flds, $new, $count, $numrows) = @_;
1816 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1822 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1824 $j = $item->{ndx} - 1;
1825 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1829 for $i ($count + 1 .. $numrows) {
1830 map { delete $self->{"${_}_$i"} } @{$flds};
1833 $main::lxdebug->leave_sub();
1837 $main::lxdebug->enter_sub();
1839 my ($self, $myconfig) = @_;
1843 my $dbh = $self->dbconnect_noauto($myconfig);
1845 my $query = qq|DELETE FROM status
1846 WHERE formname = '$self->{formname}'
1848 my $sth = $dbh->prepare($query) || $self->dberror($query);
1850 if ($self->{formname} =~ /(check|receipt)/) {
1851 for $i (1 .. $self->{rowcount}) {
1852 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1856 $sth->execute($self->{id}) || $self->dberror($query);
1860 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1861 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1863 my %queued = split / /, $self->{queued};
1865 if ($self->{formname} =~ /(check|receipt)/) {
1867 # this is a check or receipt, add one entry for each lineitem
1868 my ($accno) = split /--/, $self->{account};
1869 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1870 chart_id) VALUES (?, '$printed',
1871 '$queued{$self->{formname}}', '$self->{prinform}',
1872 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1873 $sth = $dbh->prepare($query) || $self->dberror($query);
1875 for $i (1 .. $self->{rowcount}) {
1876 if ($self->{"checked_$i"}) {
1877 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1882 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1883 spoolfile, formname)
1884 VALUES ($self->{id}, '$printed', '$emailed',
1885 '$queued{$self->{formname}}', '$self->{formname}')|;
1886 $dbh->do($query) || $self->dberror($query);
1892 $main::lxdebug->leave_sub();
1896 $main::lxdebug->enter_sub();
1898 my ($self, $dbh) = @_;
1900 my ($query, $printed, $emailed);
1902 my $formnames = $self->{printed};
1903 my $emailforms = $self->{emailed};
1905 my $query = qq|DELETE FROM status
1906 WHERE formname = '$self->{formname}'
1907 AND trans_id = $self->{id}|;
1908 $dbh->do($query) || $self->dberror($query);
1910 # this only applies to the forms
1911 # checks and receipts are posted when printed or queued
1913 if ($self->{queued}) {
1914 my %queued = split / /, $self->{queued};
1916 foreach my $formname (keys %queued) {
1917 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1918 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1920 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1921 spoolfile, formname)
1922 VALUES ($self->{id}, '$printed', '$emailed',
1923 '$queued{$formname}', '$formname')|;
1924 $dbh->do($query) || $self->dberror($query);
1926 $formnames =~ s/$self->{formname}//;
1927 $emailforms =~ s/$self->{formname}//;
1932 # save printed, emailed info
1933 $formnames =~ s/^ +//g;
1934 $emailforms =~ s/^ +//g;
1937 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1938 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
1940 foreach my $formname (keys %status) {
1941 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
1942 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
1944 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
1945 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
1946 $dbh->do($query) || $self->dberror($query);
1949 $main::lxdebug->leave_sub();
1952 sub update_defaults {
1953 $main::lxdebug->enter_sub();
1955 my ($self, $myconfig, $fld) = @_;
1957 my $dbh = $self->dbconnect_noauto($myconfig);
1958 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
1959 my $sth = $dbh->prepare($query);
1961 $sth->execute || $self->dberror($query);
1962 my ($var) = $sth->fetchrow_array;
1967 $query = qq|UPDATE defaults
1969 $dbh->do($query) || $form->dberror($query);
1974 $main::lxdebug->leave_sub();
1979 sub update_business {
1980 $main::lxdebug->enter_sub();
1982 my ($self, $myconfig, $business_id) = @_;
1984 my $dbh = $self->dbconnect_noauto($myconfig);
1986 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
1987 my $sth = $dbh->prepare($query);
1989 $sth->execute || $self->dberror($query);
1990 my ($var) = $sth->fetchrow_array;
1995 $query = qq|UPDATE business
1996 SET customernumberinit = '$var' WHERE id=$business_id|;
1997 $dbh->do($query) || $form->dberror($query);
2002 $main::lxdebug->leave_sub();
2008 $main::lxdebug->enter_sub();
2010 my ($self, $myconfig, $salesman) = @_;
2012 my $dbh = $self->dbconnect($myconfig);
2014 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2015 my $sth = $dbh->prepare($query);
2016 $sth->execute || $self->dberror($query);
2019 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2020 push(@{ $self->{salesman_list} }, $ref);
2024 $main::lxdebug->leave_sub();
2029 sub get_partsgroup {
2030 $main::lxdebug->enter_sub();
2032 my ($self, $myconfig, $p) = @_;
2034 my $dbh = $self->dbconnect($myconfig);
2036 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2038 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2040 if ($p->{searchitems} eq 'part') {
2042 WHERE p.inventory_accno_id > 0|;
2044 if ($p->{searchitems} eq 'service') {
2046 WHERE p.inventory_accno_id IS NULL|;
2048 if ($p->{searchitems} eq 'assembly') {
2050 WHERE p.assembly = '1'|;
2052 if ($p->{searchitems} eq 'labor') {
2054 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2058 ORDER BY partsgroup|;
2061 $query = qq|SELECT id, partsgroup FROM partsgroup
2062 ORDER BY partsgroup|;
2065 if ($p->{language_code}) {
2066 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2067 t.description AS translation
2069 JOIN parts p ON (p.partsgroup_id = pg.id)
2070 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2071 ORDER BY translation|;
2074 my $sth = $dbh->prepare($query);
2075 $sth->execute || $self->dberror($query);
2077 $self->{all_partsgroup} = ();
2078 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2079 push @{ $self->{all_partsgroup} }, $ref;
2083 $main::lxdebug->leave_sub();
2089 $main::lxdebug->enter_sub();
2091 my ($type, $country, $NLS_file) = @_;
2095 if ($country && -d "locale/$country") {
2096 $self->{countrycode} = $country;
2097 eval { require "locale/$country/$NLS_file"; };
2100 $self->{NLS_file} = $NLS_file;
2102 push @{ $self->{LONG_MONTH} },
2103 ("January", "February", "March", "April",
2104 "May ", "June", "July", "August",
2105 "September", "October", "November", "December");
2106 push @{ $self->{SHORT_MONTH} },
2107 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2109 $main::lxdebug->leave_sub();
2115 my ($self, $text) = @_;
2117 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2121 $main::lxdebug->enter_sub();
2123 my ($self, $text) = @_;
2125 if (exists $self{subs}{$text}) {
2126 $text = $self{subs}{$text};
2128 if ($self->{countrycode} && $self->{NLS_file}) {
2130 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2134 $main::lxdebug->leave_sub();
2140 $main::lxdebug->enter_sub();
2142 my ($self, $myconfig, $date, $longformat) = @_;
2145 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2150 $spc = $myconfig->{dateformat};
2152 $spc = substr($spc, 1, 1);
2154 if ($date =~ /\D/) {
2155 if ($myconfig->{dateformat} =~ /^yy/) {
2156 ($yy, $mm, $dd) = split /\D/, $date;
2158 if ($myconfig->{dateformat} =~ /^mm/) {
2159 ($mm, $dd, $yy) = split /\D/, $date;
2161 if ($myconfig->{dateformat} =~ /^dd/) {
2162 ($dd, $mm, $yy) = split /\D/, $date;
2165 $date = substr($date, 2);
2166 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2171 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2172 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2174 if ($myconfig->{dateformat} =~ /^dd/) {
2175 if (defined $longformat && $longformat == 0) {
2177 $dd = "0$dd" if ($dd < 10);
2178 $mm = "0$mm" if ($mm < 10);
2179 $longdate = "$dd$spc$mm$spc$yy";
2182 $longdate .= ($spc eq '.') ? ". " : " ";
2183 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2185 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2187 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2188 # Lx-Office is mainly used in Germany or German speaking countries.
2189 if (defined $longformat && $longformat == 0) {
2191 $dd = "0$dd" if ($dd < 10);
2192 $mm = "0$mm" if ($mm < 10);
2193 $longdate = "$yy-$mm-$dd";
2195 $longdate = "$dd. ";
2196 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2199 if (defined $longformat && $longformat == 0) {
2201 $dd = "0$dd" if ($dd < 10);
2202 $mm = "0$mm" if ($mm < 10);
2203 $longdate = "$mm$spc$dd$spc$yy";
2205 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2211 $main::lxdebug->leave_sub();
2217 $main::lxdebug->enter_sub();
2219 my ($self, $myconfig, $date, $longformat) = @_;
2222 $main::lxdebug->leave_sub();
2227 $spc = $myconfig->{dateformat};
2229 $spc = substr($spc, 1, 1);
2231 if ($date =~ /\D/) {
2232 if ($myconfig->{dateformat} =~ /^yy/) {
2233 ($yy, $mm, $dd) = split /\D/, $date;
2234 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2235 ($mm, $dd, $yy) = split /\D/, $date;
2236 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2237 ($dd, $mm, $yy) = split /\D/, $date;
2240 $date = substr($date, 2);
2241 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2246 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2247 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2249 $main::lxdebug->leave_sub();
2250 return ($yy, $mm, $dd);