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);
69 # For some reason there are always 2 extra, that are empty
72 for ($x = 1; $x <= $size; $x++) {
73 $header_body = $list[$x];
74 $header_body =~ /\r\n\r\n|\n\n/;
75 # Here we split the header and body
79 # Now we try to get the file name
81 $name =~ /name=\"(.+)\"/;
82 ($name, $blah) = split(/\"/, $1);
83 # If the form name is not attach, then we need to parse this like
85 if ($name ne "attach") {
86 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
87 $ATTACH{$name} = $body;
88 # Otherwise it is an attachment and we need to finish it up
89 } elsif ($name eq "attach") {
90 $header =~ /filename=\"(.+)\"/;
91 $ATTACH{'FILE_NAME'} = $1;
92 $ATTACH{'FILE_NAME'} =~ s/\"//g;
93 $ATTACH{'FILE_NAME'} =~ s/\s//g;
94 $ATTACH{'FILE_CONTENT'} = $body;
96 for($i = $x; $list[$i]; $i++) {
97 $list[$i] =~ s/^.+name=$//;
98 $list[$i] =~ /\"(\w+)\"/;
104 $main::lxdebug->leave_sub();
108 $main::lxdebug->leave_sub();
109 return _input_to_hash($input);
114 $main::lxdebug->enter_sub();
120 read(STDIN, $_, $ENV{CONTENT_LENGTH});
122 if ($ENV{QUERY_STRING}) {
123 $_ = $ENV{QUERY_STRING};
130 my %parameters = _request_to_hash($_);
131 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
133 $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
135 $self->{action} = lc $self->{action};
136 $self->{action} =~ s/( |-|,|#)/_/g;
138 $self->{version} = "2.1.2";
139 $self->{dbversion} = "2.1.2";
141 $main::lxdebug->leave_sub();
148 $main::lxdebug->enter_sub();
154 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
156 $main::lxdebug->leave_sub();
161 $main::lxdebug->enter_sub();
163 my ($self, $str, $beenthere) = @_;
165 # for Apache 2 we escape strings twice
166 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
167 # $str = $self->escape($str, 1);
170 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
172 $main::lxdebug->leave_sub();
179 $main::lxdebug->enter_sub();
181 my ($self, $str) = @_;
186 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
188 $main::lxdebug->leave_sub();
195 $main::lxdebug->enter_sub();
197 my ($self, $msg) = @_;
199 if ($ENV{HTTP_USER_AGENT}) {
207 <h2 class=error>Error!</h2>
219 if ($self->{error_function}) {
220 &{ $self->{error_function} }($msg);
226 $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();
265 $main::lxdebug->enter_sub();
267 my ($self, $str, $cols, $maxrows) = @_;
271 map { $rows += int (((length) - 2)/$cols) + 1 } split /\r/, $str;
273 $maxrows = $rows unless defined $maxrows;
275 $main::lxdebug->leave_sub();
277 return ($rows > $maxrows) ? $maxrows : $rows;
282 $main::lxdebug->enter_sub();
284 my ($self, $msg) = @_;
286 $self->error("$msg\n".$DBI::errstr);
288 $main::lxdebug->leave_sub();
293 $main::lxdebug->enter_sub();
295 my ($self, $name, $msg) = @_;
297 if ($self->{$name} =~ /^\s*$/) {
300 $main::lxdebug->leave_sub();
305 $main::lxdebug->enter_sub();
309 if ($self->{header}) {
310 $main::lxdebug->leave_sub();
314 my ($stylesheet, $favicon, $charset);
316 if ($ENV{HTTP_USER_AGENT}) {
318 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
319 $stylesheet = qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
323 if ($self->{favicon} && (-f "$self->{favicon}")) {
324 $favicon = qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
328 if ($self->{charset}) {
329 $charset = qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
332 if ($self->{landscape}) {
333 $pagelayout = qq|<style type="text/css">
334 \@page { size:landscape; }
337 if ($self->{fokus}) {
338 $fokus = qq|<script type="text/javascript">
340 function fokus(){document.$self->{fokus}.focus();}
347 if ($self->{jsscript} == 1){
350 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
351 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
352 Â <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
353 Â <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
357 $self->{titlebar} = ($self->{title}) ? "$self->{title} - $self->{titlebar}" : $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, $inputField_1, $align_1, $button_1, $inputField_2, $align_2, $button_2) = @_;
384 # set dateform for jsscript
386 $ifFormat = "%d.%m.%Y";
387 if ($myconfig->{dateformat} eq "dd.mm.yy"){
388 $ifFormat = "%d.%m.%Y";
391 if ($myconfig->{dateformat} eq "dd-mm-yy"){
392 $ifFormat = "%d-%m-%Y";
395 if ($myconfig->{dateformat} eq "dd/mm/yy"){
396 $ifFormat = "%d/%m/%Y";
399 if ($myconfig->{dateformat} eq "mm/dd/yy"){
400 $ifFormat = "%m/%d/%Y";
403 if ($myconfig->{dateformat} eq "mm-dd-yy"){
404 $ifFormat = "%m-%d-%Y";
407 if ($myconfig->{dateformat} eq "yyyy-mm-dd"){
408 $ifFormat = "%Y-%m-%d";
419    inputField  : "$inputField_1",
420    ifFormat    :"$ifFormat",
421    align    : "$align_1",    Â
422    button      : "$button_1"
431    inputField  : "$inputField_2",
432    ifFormat    :"$ifFormat",
433    align    : "$align_2",    Â
434    button      : "$button_2"
440 <script type="text/javascript">
448 $main::lxdebug->leave_sub();
451 }#end sub write_trigger
454 $main::lxdebug->enter_sub();
456 my ($self, $msg) = @_;
458 if ($self->{callback}) {
460 ($script, $argv) = split(/\?/, $self->{callback});
461 exec ("perl", "$script", $argv);
469 $main::lxdebug->leave_sub();
472 # sort of columns removed - empty sub
474 $main::lxdebug->enter_sub();
476 my ($self, @columns) = @_;
478 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my ($self, $myconfig, $amount, $places, $dash) = @_;
489 if ($places =~ /\d/) {
490 $amount = $self->round_amount($amount, $places);
493 # is the amount negative
494 my $negative = ($amount < 0);
497 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00')) {
498 my ($whole, $dec) = split /\./, "$amount";
500 $amount = join '', reverse split //, $whole;
502 if ($myconfig->{numberformat} eq '1,000.00') {
503 $amount =~ s/\d{3,}?/$&,/g;
505 $amount = join '', reverse split //, $amount;
506 $amount .= "\.$dec" if ($dec ne "");
509 if ($myconfig->{numberformat} eq '1.000,00') {
510 $amount =~ s/\d{3,}?/$&./g;
512 $amount = join '', reverse split //, $amount;
513 $amount .= ",$dec" if ($dec ne "");
516 if ($myconfig->{numberformat} eq '1000,00') {
518 $amount .= ",$dec" if ($dec ne "");
522 $amount = ($negative) ? "($amount)" : "$amount";
523 } elsif ($dash =~ /DRCR/) {
524 $amount = ($negative) ? "$amount DR" : "$amount CR";
526 $amount = ($negative) ? "-$amount" : "$amount";
530 if ($dash eq "0" && $places) {
531 if ($myconfig->{numberformat} eq '1.000,00') {
532 $amount = "0".","."0" x $places;
534 $amount = "0"."."."0" x $places;
537 $amount = ($dash ne "") ? "$dash" : "0";
541 $main::lxdebug->leave_sub();
548 $main::lxdebug->enter_sub();
550 my ($self, $myconfig, $amount) = @_;
552 if (!(substr($amount, -3,1) eq ".")) {
553 if (($myconfig->{numberformat} eq '1.000,00') ||
554 ($myconfig->{numberformat} eq '1000,00')) {
562 $main::lxdebug->leave_sub();
564 return ($amount * 1);
569 $main::lxdebug->enter_sub();
571 my ($self, $amount, $places) = @_;
574 # $places = 3 if $places == 2;
576 if (($places * 1) >= 0) {
578 $rc = sprintf("%.${places}f", $amount + (1 / (10 ** ($places + 3))) * (($amount > 0) ? 1 : -1));
581 $rc = sprintf("%.f", $amount / (10 ** $places) + (($amount > 0) ? 0.1 : -0.1)) * (10 ** $places);
584 $main::lxdebug->leave_sub();
590 $main::lxdebug->enter_sub();
592 my ($self, $myconfig, $userspath) = @_;
595 # Some variables used for page breaks
596 my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) = (0, 0, 0);
597 my ($current_page, $current_line) = (1, 1);
602 # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
603 $self->format_string(grep(/notes/, keys(%{$self})));
604 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
605 $self->{"notes"} = $self->{$self->{"formname"} . "notes"};
607 map({ $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature));
609 open(IN, "$self->{templates}/$self->{IN}") or $self->error("$self->{IN} : $!");
614 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
616 # OUT is used for the media, screen, printer, email
617 # for postscript we store a copy in a temporary file
619 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
620 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
622 $self->{OUT} = ">$self->{tmpfile}";
626 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
628 open(OUT, ">-") or $self->error("STDOUT : $!");
632 # Do we have to run LaTeX two times? This is needed if
633 # the template contains page references.
636 # first we generate a tmpfile
637 # read file and replace <%variable%>
643 $two_passes = 1 if (/\\pageref/);
646 # detect pagebreak block and its parameters
647 if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
648 $chars_per_line = $1;
649 $lines_on_first_page = $2;
650 $lines_on_second_page = $3;
653 last if (/\s*<%end pagebreak%>/);
660 if (/\s*<%foreach /) {
662 # this one we need for the count
664 $var =~ s/\s*<%foreach (.+?)%>/$1/;
666 last if (/\s*<%end /);
672 # display contents of $self->{number}[] array
673 for $i (0 .. $#{ $self->{$var} }) {
676 # Try to detect whether a manual page break is necessary
677 # but only if there was a <%pagebreak ...%> block before
679 if ($chars_per_line) {
680 my $lines = int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
683 my $_description = $self->{"description"}[$i];
684 while ($_description =~ /\\newline/) {
686 $_description =~ s/\\newline//;
688 $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
690 if ($current_page == 1) {
691 $lpp = $lines_on_first_page;
693 $lpp = $lines_on_second_page;
696 # Yes we need a manual page break
697 if (($current_line + $lines) > $lpp) {
700 # replace the special variables <%sumcarriedforward%>
703 my $psum = $self->format_amount($myconfig, $sum, 2);
704 $pb =~ s/<%sumcarriedforward%>/$psum/g;
705 $pb =~ s/<%lastpage%>/$current_page/g;
707 # only "normal" variables are supported here
708 # (no <%if, no <%foreach, no <%include)
710 $pb =~ s/<%(.+?)%>/$self->{$1}/g;
712 # page break block is ready to rock
717 $current_line += $lines;
719 $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
723 # don't parse par, we need it for each line
725 s/<%(.+?)%>/$self->{$1}[$i]/mg;
731 # if not comes before if!
732 if (/\s*<%if not /) {
733 # check if it is not set and display
735 s/\s*<%if not (.+?)%>/$1/;
737 unless ($self->{$_}) {
739 last if (/\s*<%end /);
749 last if (/\s*<%end /);
756 # check if it is set and display
758 s/\s*<%if (.+?)%>/$1/;
762 last if (/\s*<%end /);
772 last if (/\s*<%end /);
778 # check for <%include filename%>
779 if (/\s*<%include /) {
783 $var =~ s/\s*<%include (.+?)%>/$1/;
786 $var =~ s/(\/|\.\.)//g;
788 # prevent the infinite loop!
789 next if ($self->{"$var"});
791 open(INC, "$self->{templates}/$var") or $self->error($self->cleanup."$self->{templates}/$var : $!");
800 s/<%(.+?)%>/$self->{$1}/g;
808 # Convert the tex file to postscript
809 if ($self->{format} =~ /(postscript|pdf)/) {
812 $self->{cwd} = cwd();
813 $self->{tmpdir} = "$self->{cwd}/$userspath";
815 chdir("$userspath") or $self->error($self->cleanup."chdir : $!");
817 $self->{tmpfile} =~ s/$userspath\///g;
819 if ($self->{format} eq 'postscript') {
820 system("latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err");
821 $self->error($self->cleanup) if ($?);
823 system("latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err");
824 $self->error($self->cleanup) if ($?);
827 $self->{tmpfile} =~ s/tex$/dvi/;
829 system("dvips $self->{tmpfile} -o -q > /dev/null");
830 $self->error($self->cleanup."dvips : $!") if ($?);
831 $self->{tmpfile} =~ s/dvi$/ps/;
833 if ($self->{format} eq 'pdf') {
834 system("pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err");
835 $self->error($self->cleanup) if ($?);
837 system("pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err");
838 $self->error($self->cleanup) if ($?);
840 $self->{tmpfile} =~ s/tex$/pdf/;
845 if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
847 if ($self->{media} eq 'email') {
851 my $mail = new Mailer;
853 map { $mail->{$_} = $self->{$_} } qw(cc bcc subject message version format charset);
854 $mail->{to} = qq|$self->{email}|;
855 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
856 $mail->{fileid} = "$fileid.";
858 # if we send html or plain text inline
859 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
860 $mail->{contenttype} = "text/html";
862 $mail->{message} =~ s/\r\n/<br>\n/g;
863 $myconfig->{signature} =~ s/\\n/<br>\n/g;
864 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
866 open(IN, $self->{tmpfile}) or $self->error($self->cleanup."$self->{tmpfile} : $!");
868 $mail->{message} .= $_;
875 @{ $mail->{attachments} } = ($self->{tmpfile});
877 $myconfig->{signature} =~ s/\\n/\r\n/g;
878 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
882 my $err = $mail->send($out);
883 $self->error($self->cleanup."$err") if ($err);
889 my $numbytes = (-s $self->{tmpfile});
890 open(IN, $self->{tmpfile}) or $self->error($self->cleanup."$self->{tmpfile} : $!");
892 $self->{copies} = 1 unless $self->{media} eq 'printer';
894 chdir("$self->{cwd}");
896 for my $i (1 .. $self->{copies}) {
898 open(OUT, $self->{OUT}) or $self->error($self->cleanup."$self->{OUT} : $!");
902 print qq|Content-Type: application/$self->{format}
903 Content-Disposition: attachment; filename="$self->{tmpfile}"
904 Content-Length: $numbytes
908 open(OUT, ">-") or $self->error($self->cleanup."$!: STDOUT");
928 chdir("$self->{cwd}");
929 $main::lxdebug->leave_sub();
934 $main::lxdebug->enter_sub();
938 chdir("$self->{tmpdir}");
941 if (-f "$self->{tmpfile}.err") {
942 open(FH, "$self->{tmpfile}.err");
947 if ($self->{tmpfile}) {
949 $self->{tmpfile} =~ s/\.\w+$//g;
950 my $tmpfile = $self->{tmpfile};
951 unlink(<$tmpfile.*>);
954 chdir("$self->{cwd}");
956 $main::lxdebug->leave_sub();
963 $main::lxdebug->enter_sub();
965 my ($self, @fields) = @_;
968 %unique_fields = map({ $_ => 1 } @fields);
969 @fields = keys(%unique_fields);
970 my $format = $self->{format};
971 if ($self->{format} =~ /(postscript|pdf)/) {
975 my %replace = ( 'order' => { 'html' => [ '<', '>', quotemeta('\n'), '
977 'tex' => [ '&', quotemeta('\n'), '
979 '"', '\$', '%', '_', '#', quotemeta('^'),
980 '{', '}', '<', '>', '£', "\r" ] },
981 'html' => { '<' => '<', '>' => '>',
982 quotemeta('\n') => '<br>', '
986 '"' => "''", '&' => '\&', '\$' => '\$', '%' => '\%', '_' => '\_',
987 '#' => '\#', quotemeta('^') => '\^\\', '{' => '\{', '}' => '\}',
988 '<' => '$<$', '>' => '$>$',
989 quotemeta('\n') => '\newline ', '
991 '£' => '\pounds ', "\r" => ""
995 foreach my $key (@{ $replace{order}{$format} }) {
996 map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
999 $main::lxdebug->leave_sub();
1004 $main::lxdebug->enter_sub();
1006 my ($self, $date, $myconfig) = @_;
1008 if ($date && $date =~ /\D/) {
1010 if ($myconfig->{dateformat} =~ /^yy/) {
1011 ($yy, $mm, $dd) = split /\D/, $date;
1013 if ($myconfig->{dateformat} =~ /^mm/) {
1014 ($mm, $dd, $yy) = split /\D/, $date;
1016 if ($myconfig->{dateformat} =~ /^dd/) {
1017 ($dd, $mm, $yy) = split /\D/, $date;
1022 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1023 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1025 $dd = "0$dd" if ($dd < 10);
1026 $mm = "0$mm" if ($mm < 10);
1028 $date = "$yy$mm$dd";
1031 $main::lxdebug->leave_sub();
1038 # Database routines used throughout
1041 $main::lxdebug->enter_sub();
1043 my ($self, $myconfig) = @_;
1045 # connect to database
1046 my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}) or $self->dberror;
1049 if ($myconfig->{dboptions}) {
1050 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1053 $main::lxdebug->leave_sub();
1059 sub dbconnect_noauto {
1060 $main::lxdebug->enter_sub();
1062 my ($self, $myconfig) = @_;
1064 # connect to database
1065 $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}, {AutoCommit => 0}) or $self->dberror;
1068 if ($myconfig->{dboptions}) {
1069 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1072 $main::lxdebug->leave_sub();
1078 sub update_balance {
1079 $main::lxdebug->enter_sub();
1081 my ($self, $dbh, $table, $field, $where, $value) = @_;
1083 # if we have a value, go do it
1085 # retrieve balance from table
1086 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1087 my $sth = $dbh->prepare($query);
1089 $sth->execute || $self->dberror($query);
1090 my ($balance) = $sth->fetchrow_array;
1095 $query = "UPDATE $table SET $field = $balance WHERE $where";
1096 $dbh->do($query) || $self->dberror($query);
1098 $main::lxdebug->leave_sub();
1103 sub update_exchangerate {
1104 $main::lxdebug->enter_sub();
1106 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1108 # some sanity check for currency
1110 $main::lxdebug->leave_sub();
1114 my $query = qq|SELECT e.curr FROM exchangerate e
1115 WHERE e.curr = '$curr'
1116 AND e.transdate = '$transdate'
1118 my $sth = $dbh->prepare($query);
1119 $sth->execute || $self->dberror($query);
1122 if ($buy != 0 && $sell != 0) {
1123 $set = "buy = $buy, sell = $sell";
1124 } elsif ($buy != 0) {
1125 $set = "buy = $buy";
1126 } elsif ($sell != 0) {
1127 $set = "sell = $sell";
1130 if ($sth->fetchrow_array) {
1131 $query = qq|UPDATE exchangerate
1133 WHERE curr = '$curr'
1134 AND transdate = '$transdate'|;
1136 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1137 VALUES ('$curr', $buy, $sell, '$transdate')|;
1140 $dbh->do($query) || $self->dberror($query);
1142 $main::lxdebug->leave_sub();
1146 sub save_exchangerate {
1147 $main::lxdebug->enter_sub();
1149 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1151 my $dbh = $self->dbconnect($myconfig);
1153 my ($buy, $sell) = (0, 0);
1154 $buy = $rate if $fld eq 'buy';
1155 $sell = $rate if $fld eq 'sell';
1157 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1161 $main::lxdebug->leave_sub();
1165 sub get_exchangerate {
1166 $main::lxdebug->enter_sub();
1168 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1170 my $query = qq|SELECT e.$fld FROM exchangerate e
1171 WHERE e.curr = '$curr'
1172 AND e.transdate = '$transdate'|;
1173 my $sth = $dbh->prepare($query);
1174 $sth->execute || $self->dberror($query);
1176 my ($exchangerate) = $sth->fetchrow_array;
1179 $main::lxdebug->leave_sub();
1181 return $exchangerate;
1185 sub check_exchangerate {
1186 $main::lxdebug->enter_sub();
1188 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1190 unless ($transdate) {
1191 $main::lxdebug->leave_sub();
1196 my $dbh = $self->dbconnect($myconfig);
1198 my $query = qq|SELECT e.$fld FROM exchangerate e
1199 WHERE e.curr = '$currency'
1200 AND e.transdate = '$transdate'|;
1201 my $sth = $dbh->prepare($query);
1202 $sth->execute || $self->dberror($query);
1204 my ($exchangerate) = $sth->fetchrow_array;
1208 $main::lxdebug->leave_sub();
1210 return $exchangerate;
1215 $main::lxdebug->enter_sub();
1217 my ($self, $dbh, $id) = @_;
1220 foreach my $item (qw(name street zipcode city country contact phone fax email)) {
1221 if ($self->{"shipto$item"}) {
1222 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1224 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1228 my $query = qq|INSERT INTO shipto (trans_id, shiptoname, shiptostreet,
1229 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1230 shiptophone, shiptofax, shiptoemail) VALUES ($id,
1231 '$self->{shiptoname}', '$self->{shiptostreet}',
1232 '$self->{shiptozipcode}', '$self->{shiptocity}',
1233 '$self->{shiptocountry}', '$self->{shiptocontact}',
1234 '$self->{shiptophone}', '$self->{shiptofax}',
1235 '$self->{shiptoemail}')|;
1236 $dbh->do($query) || $self->dberror($query);
1239 $main::lxdebug->leave_sub();
1244 $main::lxdebug->enter_sub();
1246 my ($self, $dbh) = @_;
1248 my $query = qq|SELECT e.id, e.name FROM employee e
1249 WHERE e.login = '$self->{login}'|;
1250 my $sth = $dbh->prepare($query);
1251 $sth->execute || $self->dberror($query);
1253 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1254 $self->{employee_id} *= 1;
1258 $main::lxdebug->leave_sub();
1261 # get other contact for transaction and form - html/tex
1263 $main::lxdebug->enter_sub();
1265 my ($self, $dbh,$id) = @_;
1267 my $query = qq|SELECT c.*
1270 $sth = $dbh->prepare($query);
1271 $sth->execute || $self->dberror($query);
1273 $ref = $sth->fetchrow_hashref(NAME_lc);
1275 push @{ $self->{$_} }, $ref;
1278 $main::lxdebug->leave_sub();
1281 # get contacts for id, if no contact return {"","","","",""}
1283 $main::lxdebug->enter_sub();
1285 my ($self, $dbh, $id) = @_;
1287 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
1289 WHERE cp_cv_id=$id|;
1290 my $sth = $dbh->prepare($query);
1291 $sth->execute || $self->dberror($query);
1294 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1295 push @{ $self->{all_contacts} }, $ref;
1301 push @{ $self->{all_contacts} }, {{"","","","",""}};
1304 $main::lxdebug->leave_sub();
1308 # this sub gets the id and name from $table
1310 $main::lxdebug->enter_sub();
1312 my ($self, $myconfig, $table) = @_;
1313 # connect to database
1314 my $dbh = $self->dbconnect($myconfig);
1316 my $name = $self->like(lc $self->{$table});
1317 my $customernumber = $self->like(lc $self->{customernumber});
1319 if ($self->{customernumber} ne "") {
1320 $query = qq~SELECT c.id, c.name,
1321 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1323 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1326 $query = qq~SELECT c.id, c.name,
1327 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1329 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1333 if ($self->{openinvoices}) {
1334 $query = qq~SELECT DISTINCT c.id, c.name,
1335 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1336 FROM $self->{arap} a
1337 JOIN $table c ON (a.${table}_id = c.id)
1338 WHERE NOT a.amount = a.paid
1339 AND lower(c.name) LIKE '$name'
1342 my $sth = $dbh->prepare($query);
1344 $sth->execute || $self->dberror($query);
1347 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1348 push(@{ $self->{name_list} }, $ref);
1354 $main::lxdebug->leave_sub();
1360 # the selection sub is used in the AR, AP, IS, IR and OE module
1363 $main::lxdebug->enter_sub();
1365 my ($self, $myconfig, $table, $module) = @_;
1368 my $dbh = $self->dbconnect($myconfig);
1370 my $query = qq|SELECT count(*) FROM $table|;
1371 my $sth = $dbh->prepare($query);
1372 $sth->execute || $self->dberror($query);
1373 my ($count) = $sth->fetchrow_array;
1376 # build selection list
1377 if ($count < $myconfig->{vclimit}) {
1378 $query = qq|SELECT id, name
1379 FROM $table WHERE not obsolete
1381 $sth = $dbh->prepare($query);
1382 $sth->execute || $self->dberror($query);
1384 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1385 push @{ $self->{"all_$table"} }, $ref;
1393 $self->get_employee($dbh);
1395 # setup sales contacts
1396 $query = qq|SELECT e.id, e.name
1399 AND NOT e.id = $self->{employee_id}|;
1400 $sth = $dbh->prepare($query);
1401 $sth->execute || $self->dberror($query);
1403 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1404 push @{ $self->{all_employees} }, $ref;
1409 push @{ $self->{all_employees} }, { id => $self->{employee_id},
1410 name => $self->{employee} };
1411 # sort the whole thing
1412 @{ $self->{all_employees} } = sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1414 if ($module eq 'AR') {
1415 # prepare query for departments
1416 $query = qq|SELECT d.id, d.description
1422 $query = qq|SELECT d.id, d.description
1427 $sth = $dbh->prepare($query);
1428 $sth->execute || $self->dberror($query);
1430 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1431 push @{ $self->{all_departments} }, $ref;
1437 $main::lxdebug->leave_sub();
1441 # this is only used for reports
1442 sub all_departments {
1443 $main::lxdebug->enter_sub();
1445 my ($self, $myconfig, $table) = @_;
1447 my $dbh = $self->dbconnect($myconfig);
1448 my $where = "1 = 1";
1450 if (defined $table) {
1451 if ($table eq 'customer') {
1452 $where = " d.role = 'P'";
1456 my $query = qq|SELECT d.id, d.description
1460 my $sth = $dbh->prepare($query);
1461 $sth->execute || $self->dberror($query);
1463 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1464 push @{ $self->{all_departments} }, $ref;
1470 $main::lxdebug->leave_sub();
1475 $main::lxdebug->enter_sub();
1477 my ($self, $module, $myconfig, $table) = @_;
1479 $self->all_vc($myconfig, $table, $module);
1481 # get last customers or vendors
1484 my $dbh = $self->dbconnect($myconfig);
1489 # now get the account numbers
1490 $query = qq|SELECT c.accno, SUBSTRING(c.description,1,50) as description, c.link, c.taxkey_id
1492 WHERE c.link LIKE '%$module%'
1495 $sth = $dbh->prepare($query);
1496 $sth->execute || $self->dberror($query);
1498 $self->{accounts} = "";
1499 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1501 foreach my $key (split /:/, $ref->{link}) {
1502 if ($key =~ /$module/) {
1503 # cross reference for keys
1504 $xkeyref{$ref->{accno}} = $key;
1506 push @{ $self->{"${module}_links"}{$key} }, { accno => $ref->{accno},
1507 description => $ref->{description}, taxkey => $ref->{taxkey_id} };
1509 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1515 if (($module eq "AP") || ($module eq "AR")) {
1517 # get tax rates and description
1518 $query = qq| SELECT * FROM tax t|;
1519 $sth = $dbh->prepare($query);
1520 $sth->execute || $self->dberror($query);
1522 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1523 push @{ $self->{TAX} }, $ref;
1529 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1531 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1532 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1533 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1534 c.name AS $table, a.department_id, d.description AS department,
1535 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1536 a.employee_id, e.name AS employee, a.gldate
1538 JOIN $table c ON (a.${table}_id = c.id)
1539 LEFT JOIN employee e ON (e.id = a.employee_id)
1540 LEFT JOIN department d ON (d.id = a.department_id)
1541 WHERE a.id = $self->{id}|;
1542 $sth = $dbh->prepare($query);
1543 $sth->execute || $self->dberror($query);
1545 $ref = $sth->fetchrow_hashref(NAME_lc);
1546 foreach $key (keys %$ref) {
1547 $self->{$key} = $ref->{$key};
1551 # get amounts from individual entries
1552 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1553 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1555 JOIN chart c ON (c.id = a.chart_id)
1556 LEFT JOIN project p ON (p.id = a.project_id)
1557 LEFT Join tax t ON (a.taxkey = t.taxkey)
1558 WHERE a.trans_id = $self->{id}
1559 AND a.fx_transaction = '0'
1560 ORDER BY a.transdate|;
1561 $sth = $dbh->prepare($query);
1562 $sth->execute || $self->dberror($query);
1564 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1565 # get exchangerate for currency
1566 $self->{exchangerate} = $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
1568 # store amounts in {acc_trans}{$key} for multiple accounts
1569 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1570 $ref->{exchangerate} = $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
1572 push @{ $self->{acc_trans}{$xkeyref{$ref->{accno}}} }, $ref;
1576 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1577 (SELECT c.accno FROM chart c
1578 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1579 (SELECT c.accno FROM chart c
1580 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1582 $sth = $dbh->prepare($query);
1583 $sth->execute || $self->dberror($query);
1585 $ref = $sth->fetchrow_hashref(NAME_lc);
1586 map { $self->{$_} = $ref->{$_} } keys %$ref;
1593 $query = qq|SELECT current_date AS transdate,
1594 d.curr AS currencies, d.closedto, d.revtrans,
1595 (SELECT c.accno FROM chart c
1596 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1597 (SELECT c.accno FROM chart c
1598 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1600 $sth = $dbh->prepare($query);
1601 $sth->execute || $self->dberror($query);
1603 $ref = $sth->fetchrow_hashref(NAME_lc);
1604 map { $self->{$_} = $ref->{$_} } keys %$ref;
1607 if ($self->{"$self->{vc}_id"}) {
1608 # only setup currency
1609 ($self->{currency}) = split /:/, $self->{currencies};
1613 $self->lastname_used($dbh, $myconfig, $table, $module);
1615 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1616 # get exchangerate for currency
1617 $self->{exchangerate} = $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
1625 $main::lxdebug->leave_sub();
1630 $main::lxdebug->enter_sub();
1632 my ($self, $dbh, $myconfig, $table, $module) = @_;
1634 my $arap = ($table eq 'customer') ? "ar" : "ap";
1635 my $where = "1 = 1";
1637 if ($self->{type} =~ /_order/) {
1639 $where = "quotation = '0'";
1641 if ($self->{type} =~ /_quotation/) {
1643 $where = "quotation = '1'";
1646 my $query = qq|SELECT id FROM $arap
1647 WHERE id IN (SELECT MAX(id) FROM $arap
1649 AND ${table}_id > 0)|;
1650 my $sth = $dbh->prepare($query);
1651 $sth->execute || $self->dberror($query);
1653 my ($trans_id) = $sth->fetchrow_array;
1657 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1658 current_date + ct.terms AS duedate, a.department_id,
1659 d.description AS department
1661 JOIN $table ct ON (a.${table}_id = ct.id)
1662 LEFT JOIN department d ON (a.department_id = d.id)
1663 WHERE a.id = $trans_id|;
1664 $sth = $dbh->prepare($query);
1665 $sth->execute || $self->dberror($query);
1667 ($self->{$table}, $self->{currency}, $self->{"${table}_id"}, $self->{duedate}, $self->{department_id}, $self->{department}) = $sth->fetchrow_array;
1670 $main::lxdebug->leave_sub();
1676 $main::lxdebug->enter_sub();
1678 my ($self, $myconfig, $thisdate, $days) = @_;
1680 my $dbh = $self->dbconnect($myconfig);
1685 my $dateformat = $myconfig->{dateformat};
1686 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1688 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1690 $sth = $dbh->prepare($query);
1691 $sth->execute || $self->dberror($query);
1693 $query = qq|SELECT current_date AS thisdate
1695 $sth = $dbh->prepare($query);
1696 $sth->execute || $self->dberror($query);
1699 ($thisdate) = $sth->fetchrow_array;
1704 $main::lxdebug->leave_sub();
1711 $main::lxdebug->enter_sub();
1713 my ($self, $string) = @_;
1715 if ($string !~ /%/) {
1716 $string = "%$string%";
1719 $string =~ s/\'/\'\'/g;
1721 $main::lxdebug->leave_sub();
1728 $main::lxdebug->enter_sub();
1730 my ($self, $flds, $new, $count, $numrows) = @_;
1734 map { push @ndx, { num => $new->[$_-1]->{runningnumber}, ndx => $_ } } (1 .. $count);
1738 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1740 $j = $item->{ndx} - 1;
1741 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1745 for $i ($count + 1 .. $numrows) {
1746 map { delete $self->{"${_}_$i"} } @{$flds};
1749 $main::lxdebug->leave_sub();
1754 $main::lxdebug->enter_sub();
1756 my ($self, $myconfig) = @_;
1760 my $dbh = $self->dbconnect_noauto($myconfig);
1762 my $query = qq|DELETE FROM status
1763 WHERE formname = '$self->{formname}'
1765 my $sth = $dbh->prepare($query) || $self->dberror($query);
1767 if ($self->{formname} =~ /(check|receipt)/) {
1768 for $i (1 .. $self->{rowcount}) {
1769 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1773 $sth->execute($self->{id}) || $self->dberror($query);
1777 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1778 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1780 my %queued = split / /, $self->{queued};
1782 if ($self->{formname} =~ /(check|receipt)/) {
1783 # this is a check or receipt, add one entry for each lineitem
1784 my ($accno) = split /--/, $self->{account};
1785 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1786 chart_id) VALUES (?, '$printed',
1787 '$queued{$self->{formname}}', '$self->{prinform}',
1788 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1789 $sth = $dbh->prepare($query) || $self->dberror($query);
1791 for $i (1 .. $self->{rowcount}) {
1792 if ($self->{"checked_$i"}) {
1793 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1798 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1799 spoolfile, formname)
1800 VALUES ($self->{id}, '$printed', '$emailed',
1801 '$queued{$self->{formname}}', '$self->{formname}')|;
1802 $dbh->do($query) || $self->dberror($query);
1808 $main::lxdebug->leave_sub();
1813 $main::lxdebug->enter_sub();
1815 my ($self, $dbh) = @_;
1817 my ($query, $printed, $emailed);
1819 my $formnames = $self->{printed};
1820 my $emailforms = $self->{emailed};
1822 my $query = qq|DELETE FROM status
1823 WHERE formname = '$self->{formname}'
1824 AND trans_id = $self->{id}|;
1825 $dbh->do($query) || $self->dberror($query);
1827 # this only applies to the forms
1828 # checks and receipts are posted when printed or queued
1830 if ($self->{queued}) {
1831 my %queued = split / /, $self->{queued};
1833 foreach my $formname (keys %queued) {
1834 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1835 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1837 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1838 spoolfile, formname)
1839 VALUES ($self->{id}, '$printed', '$emailed',
1840 '$queued{$formname}', '$formname')|;
1841 $dbh->do($query) || $self->dberror($query);
1843 $formnames =~ s/$self->{formname}//;
1844 $emailforms =~ s/$self->{formname}//;
1849 # save printed, emailed info
1850 $formnames =~ s/^ +//g;
1851 $emailforms =~ s/^ +//g;
1854 map { $status{$_}{printed} = 1 } split / +/, $formnames;
1855 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
1857 foreach my $formname (keys %status) {
1858 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
1859 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
1861 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
1862 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
1863 $dbh->do($query) || $self->dberror($query);
1866 $main::lxdebug->leave_sub();
1870 sub update_defaults {
1871 $main::lxdebug->enter_sub();
1873 my ($self, $myconfig, $fld) = @_;
1875 my $dbh = $self->dbconnect_noauto($myconfig);
1876 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
1877 my $sth = $dbh->prepare($query);
1879 $sth->execute || $self->dberror($query);
1880 my ($var) = $sth->fetchrow_array;
1885 $query = qq|UPDATE defaults
1887 $dbh->do($query) || $form->dberror($query);
1892 $main::lxdebug->leave_sub();
1897 sub update_business {
1898 $main::lxdebug->enter_sub();
1900 my ($self, $myconfig, $business_id) = @_;
1902 my $dbh = $self->dbconnect_noauto($myconfig);
1903 my $query = qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
1904 my $sth = $dbh->prepare($query);
1906 $sth->execute || $self->dberror($query);
1907 my ($var) = $sth->fetchrow_array;
1912 $query = qq|UPDATE business
1913 SET customernumberinit = '$var' WHERE id=$business_id|;
1914 $dbh->do($query) || $form->dberror($query);
1919 $main::lxdebug->leave_sub();
1925 $main::lxdebug->enter_sub();
1927 my ($self, $myconfig, $salesman) = @_;
1929 my $dbh = $self->dbconnect($myconfig);
1930 my $query = qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
1931 my $sth = $dbh->prepare($query);
1932 $sth->execute || $self->dberror($query);
1935 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1936 push(@{ $self->{salesman_list} }, $ref);
1940 $main::lxdebug->leave_sub();
1945 sub get_partsgroup {
1946 $main::lxdebug->enter_sub();
1948 my ($self, $myconfig, $p) = @_;
1950 my $dbh = $self->dbconnect($myconfig);
1952 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
1954 JOIN parts p ON (p.partsgroup_id = pg.id)|;
1956 if ($p->{searchitems} eq 'part') {
1958 WHERE p.inventory_accno_id > 0|;
1960 if ($p->{searchitems} eq 'service') {
1962 WHERE p.inventory_accno_id IS NULL|;
1964 if ($p->{searchitems} eq 'assembly') {
1966 WHERE p.assembly = '1'|;
1968 if ($p->{searchitems} eq 'labor') {
1970 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
1974 ORDER BY partsgroup|;
1977 $query = qq|SELECT id, partsgroup FROM partsgroup
1978 ORDER BY partsgroup|;
1981 if ($p->{language_code}) {
1982 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
1983 t.description AS translation
1985 JOIN parts p ON (p.partsgroup_id = pg.id)
1986 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
1987 ORDER BY translation|;
1990 my $sth = $dbh->prepare($query);
1991 $sth->execute || $self->dberror($query);
1993 $self->{all_partsgroup} = ();
1994 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1995 push @{ $self->{all_partsgroup} }, $ref;
1999 $main::lxdebug->leave_sub();
2006 $main::lxdebug->enter_sub();
2008 my ($type, $country, $NLS_file) = @_;
2012 if ($country && -d "locale/$country") {
2013 $self->{countrycode} = $country;
2014 eval { require "locale/$country/$NLS_file"; };
2017 $self->{NLS_file} = $NLS_file;
2019 push @{ $self->{LONG_MONTH} }, ("January", "February", "March", "April", "May ", "June", "July", "August", "September", "October", "November", "December");
2020 push @{ $self->{SHORT_MONTH} }, (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2022 $main::lxdebug->leave_sub();
2029 my ($self, $text) = @_;
2031 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2036 $main::lxdebug->enter_sub();
2038 my ($self, $text) = @_;
2040 if (exists $self{subs}{$text}) {
2041 $text = $self{subs}{$text};
2043 if ($self->{countrycode} && $self->{NLS_file}) {
2044 Form->error("$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2048 $main::lxdebug->leave_sub();
2055 $main::lxdebug->enter_sub();
2057 my ($self, $myconfig, $date, $longformat) = @_;
2060 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2064 $spc = $myconfig->{dateformat};
2066 $spc = substr($spc, 1, 1);
2068 if ($date =~ /\D/) {
2069 if ($myconfig->{dateformat} =~ /^yy/) {
2070 ($yy, $mm, $dd) = split /\D/, $date;
2072 if ($myconfig->{dateformat} =~ /^mm/) {
2073 ($mm, $dd, $yy) = split /\D/, $date;
2075 if ($myconfig->{dateformat} =~ /^dd/) {
2076 ($dd, $mm, $yy) = split /\D/, $date;
2079 $date = substr($date, 2);
2080 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2085 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2086 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2088 if ($myconfig->{dateformat} =~ /^dd/) {
2089 if (defined $longformat && $longformat == 0) {
2091 $dd = "0$dd" if ($dd < 10);
2092 $mm = "0$mm" if ($mm < 10);
2093 $longdate = "$dd$spc$mm$spc$yy";
2096 $longdate .= ($spc eq '.') ? ". " : " ";
2097 $longdate .= &text($self, $self->{$longmonth}[$mm])." $yy";
2099 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2100 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2101 # Lx-Office is mainly used in Germany or German speaking countries.
2102 if (defined $longformat && $longformat == 0) {
2104 $dd = "0$dd" if ($dd < 10);
2105 $mm = "0$mm" if ($mm < 10);
2106 $longdate = "$yy-$mm-$dd";
2108 $longdate = "$dd. ";
2109 $longdate .= &text($self, $self->{$longmonth}[$mm])." $yy";
2112 if (defined $longformat && $longformat == 0) {
2114 $dd = "0$dd" if ($dd < 10);
2115 $mm = "0$mm" if ($mm < 10);
2116 $longdate = "$mm$spc$dd$spc$yy";
2118 $longdate = &text($self, $self->{$longmonth}[$mm])." $dd, $yy";
2124 $main::lxdebug->leave_sub();
2130 $main::lxdebug->enter_sub();
2132 my ($self, $myconfig, $date, $longformat) = @_;
2135 $main::lxdebug->leave_sub();
2140 $spc = $myconfig->{dateformat};
2142 $spc = substr($spc, 1, 1);
2144 if ($date =~ /\D/) {
2145 if ($myconfig->{dateformat} =~ /^yy/) {
2146 ($yy, $mm, $dd) = split /\D/, $date;
2147 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2148 ($mm, $dd, $yy) = split /\D/, $date;
2149 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2150 ($dd, $mm, $yy) = split /\D/, $date;
2153 $date = substr($date, 2);
2154 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2159 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2160 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2162 $main::lxdebug->leave_sub();
2163 return ($yy, $mm, $dd);