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 #======================================================================
49 $main::lxdebug->enter_sub(2);
53 my @pairs = split(/&/, $input);
56 my ($name, $value) = split(/=/, $_, 2);
57 $in{$name} = unescape(undef, $value);
60 $main::lxdebug->leave_sub(2);
65 sub _request_to_hash {
66 $main::lxdebug->enter_sub(2);
69 my ($i, $loc, $key, $val);
70 my (%ATTACH, $f, $header, $header_body, $len, $buf);
71 my ($boundary, @list, $size, $body, $x, $blah, $name);
73 if ($ENV{'CONTENT_TYPE'}
74 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
75 $boundary = quotemeta('--' . $1);
76 @list = split(/$boundary/, $input);
78 # For some reason there are always 2 extra, that are empty
81 for ($x = 1; $x <= $size; $x++) {
82 $header_body = $list[$x];
83 $header_body =~ /\r\n\r\n|\n\n/;
85 # Here we split the header and body
90 # Now we try to get the file name
92 $name =~ /name=\"(.+)\"/;
93 ($name, $blah) = split(/\"/, $1);
95 # If the form name is not attach, then we need to parse this like
97 if ($name ne "attach") {
98 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
99 $ATTACH{$name} = $body;
101 # Otherwise it is an attachment and we need to finish it up
102 } elsif ($name eq "attach") {
103 $header =~ /filename=\"(.+)\"/;
104 $ATTACH{'FILE_NAME'} = $1;
105 $ATTACH{'FILE_NAME'} =~ s/\"//g;
106 $ATTACH{'FILE_NAME'} =~ s/\s//g;
107 $ATTACH{'FILE_CONTENT'} = $body;
109 for ($i = $x; $list[$i]; $i++) {
110 $list[$i] =~ s/^.+name=$//;
111 $list[$i] =~ /\"(\w+)\"/;
117 $main::lxdebug->leave_sub(2);
121 $main::lxdebug->leave_sub(2);
122 return _input_to_hash($input);
127 $main::lxdebug->enter_sub();
133 read(STDIN, $_, $ENV{CONTENT_LENGTH});
135 if ($ENV{QUERY_STRING}) {
136 $_ = $ENV{QUERY_STRING};
143 my %parameters = _request_to_hash($_);
144 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
146 $self->{action} = lc $self->{action};
147 $self->{action} =~ s/( |-|,|\#)/_/g;
149 $self->{version} = "2.4.0";
151 $main::lxdebug->leave_sub();
157 $main::lxdebug->enter_sub();
163 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
165 $main::lxdebug->leave_sub();
169 $main::lxdebug->enter_sub(2);
171 my ($self, $str, $beenthere) = @_;
173 # for Apache 2 we escape strings twice
174 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
175 # $str = $self->escape($str, 1);
178 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
180 $main::lxdebug->leave_sub(2);
186 $main::lxdebug->enter_sub(2);
188 my ($self, $str) = @_;
193 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
195 $main::lxdebug->leave_sub(2);
201 my ($self, $str) = @_;
203 if ($str && !ref($str)) {
204 $str =~ s/\"/"/g;
212 my ($self, $str) = @_;
214 if ($str && !ref($str)) {
215 $str =~ s/"/\"/g;
227 print qq|<input type=hidden name="$_" value="|
228 . $self->quote($self->{$_})
232 delete $self->{header};
233 for (sort keys %$self) {
234 print qq|<input type=hidden name="$_" value="|
235 . $self->quote($self->{$_})
243 $main::lxdebug->enter_sub();
245 my ($self, $msg) = @_;
246 if ($ENV{HTTP_USER_AGENT}) {
248 $self->show_generic_error($msg);
252 if ($self->{error_function}) {
253 &{ $self->{error_function} }($msg);
259 $main::lxdebug->leave_sub();
263 $main::lxdebug->enter_sub();
265 my ($self, $msg) = @_;
267 if ($ENV{HTTP_USER_AGENT}) {
270 if (!$self->{header}) {
283 if ($self->{info_function}) {
284 &{ $self->{info_function} }($msg);
290 $main::lxdebug->leave_sub();
294 $main::lxdebug->enter_sub();
296 my ($self, $str, $cols, $maxrows) = @_;
300 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
302 $maxrows = $rows unless defined $maxrows;
304 $main::lxdebug->leave_sub();
306 return ($rows > $maxrows) ? $maxrows : $rows;
310 $main::lxdebug->enter_sub();
312 my ($self, $msg) = @_;
314 $self->error("$msg\n" . $DBI::errstr);
316 $main::lxdebug->leave_sub();
320 $main::lxdebug->enter_sub();
322 my ($self, $name, $msg) = @_;
324 if ($self->{$name} =~ /^\s*$/) {
327 $main::lxdebug->leave_sub();
331 $main::lxdebug->enter_sub();
335 if ($self->{header}) {
336 $main::lxdebug->leave_sub();
340 my ($stylesheet, $favicon, $charset);
342 if ($ENV{HTTP_USER_AGENT}) {
344 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
346 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
350 if ($self->{favicon} && (-f "$self->{favicon}")) {
352 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
356 if ($self->{charset}) {
358 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
361 if ($self->{landscape}) {
362 $pagelayout = qq|<style type="text/css">
363 \@page { size:landscape; }
366 if ($self->{fokus}) {
367 $fokus = qq|<script type="text/javascript">
369 function fokus(){document.$self->{fokus}.focus();}
376 if ($self->{jsscript} == 1) {
379 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
380 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
381 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
382 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
389 ? "$self->{title} - $self->{titlebar}"
392 foreach $item (@ { $self->{AJAX} }) {
393 $ajax .= $item->show_javascript();
395 print qq|Content-Type: text/html
399 <title>$self->{titlebar}</title>
407 <script type="text/javascript" src="js/highlight_input.js"></script>
408 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
410 <script type="text/javascript" src="js/tabcontent.js">
412 /***********************************************
413 * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
414 * This notice MUST stay intact for legal use
415 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
416 ***********************************************/
426 $main::lxdebug->leave_sub();
429 sub parse_html_template {
430 $main::lxdebug->enter_sub();
432 my ($self, $file, $additional_params) = @_;
435 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
436 $language = $main::language;
438 $language = $main::myconfig{"countrycode"};
441 if (-f "templates/webpages/${file}_${language}.html") {
442 if ((-f ".developer") &&
443 (-f "templates/webpages/${file}_master.html") &&
444 ((stat("templates/webpages/${file}_master.html"))[9] >
445 (stat("templates/webpages/${file}_${language}.html"))[9])) {
446 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
447 "Please re-run 'locales.pl' in 'locale/${language}'.";
448 print(qq|<pre>$info</pre>|);
452 $file = "templates/webpages/${file}_${language}.html";
453 } elsif (-f "templates/webpages/${file}.html") {
454 $file = "templates/webpages/${file}.html";
456 my $info = "Web page template '${file}' not found.\n" .
457 "Please re-run 'locales.pl' in 'locale/${language}'.";
458 print(qq|<pre>$info</pre>|);
462 my $template = HTML::Template->new("filename" => $file,
463 "die_on_bad_params" => 0,
465 "case_sensitive" => 1,
466 "loop_context_vars" => 1,
469 $additional_params = {} unless ($additional_params);
470 if ($self->{"DEBUG"}) {
471 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
474 if ($additional_params->{"DEBUG"}) {
475 $additional_params->{"DEBUG"} =
476 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
479 if (%main::myconfig) {
480 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
481 my $jsc_dateformat = $main::myconfig{"dateformat"};
482 $jsc_dateformat =~ s/d+/\%d/gi;
483 $jsc_dateformat =~ s/m+/\%m/gi;
484 $jsc_dateformat =~ s/y+/\%Y/gi;
485 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
488 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
489 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
490 $additional_params->{"conf_latex_templates"} = $main::latex;
491 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
493 my @additional_param_names = keys(%{$additional_params});
494 foreach my $key ($template->param()) {
495 my $param = $self->{$key};
496 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
497 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
498 $template->param($key => $param);
501 my $output = $template->output();
503 $main::lxdebug->leave_sub();
508 sub show_generic_error {
509 my ($self, $error, $title, $action) = @_;
512 $add_params->{"title"} = $title if ($title);
513 $self->{"label_error"} = $error;
517 map({ delete($self->{$_}); } qw(action));
518 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
519 if (!ref($self->{$_})); }
521 $add_params->{"SHOW_BUTTON"} = 1;
522 $add_params->{"BUTTON_LABEL"} = $action;
524 $add_params->{"VARIABLES"} = \@vars;
527 print($self->parse_html_template("generic/error", $add_params));
529 die("Error: $error\n");
532 sub show_generic_information {
533 my ($self, $error, $title) = @_;
536 $add_params->{"title"} = $title if ($title);
537 $self->{"label_information"} = $error;
540 print($self->parse_html_template("generic/information", $add_params));
542 die("Information: $error\n");
545 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
546 # changed it to accept an arbitrary number of triggers - sschoeling
548 $main::lxdebug->enter_sub();
551 my $myconfig = shift;
554 # set dateform for jsscript
556 $ifFormat = "%d.%m.%Y";
557 if ($myconfig->{dateformat} eq "dd.mm.yy") {
558 $ifFormat = "%d.%m.%Y";
560 if ($myconfig->{dateformat} eq "dd-mm-yy") {
561 $ifFormat = "%d-%m-%Y";
563 if ($myconfig->{dateformat} eq "dd/mm/yy") {
564 $ifFormat = "%d/%m/%Y";
566 if ($myconfig->{dateformat} eq "mm/dd/yy") {
567 $ifFormat = "%m/%d/%Y";
569 if ($myconfig->{dateformat} eq "mm-dd-yy") {
570 $ifFormat = "%m-%d-%Y";
572 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
573 $ifFormat = "%Y-%m-%d";
585 inputField : "| . (shift) . qq|",
586 ifFormat :"$ifFormat",
587 align : "| . (shift) . qq|",
588 button : "| . (shift) . qq|"
594 <script type="text/javascript">
595 <!--| . join("", @triggers) . qq|//-->
599 $main::lxdebug->leave_sub();
602 } #end sub write_trigger
605 $main::lxdebug->enter_sub();
607 my ($self, $msg) = @_;
609 if ($self->{callback}) {
611 ($script, $argv) = split(/\?/, $self->{callback});
612 exec("perl", "$script", $argv);
620 $main::lxdebug->leave_sub();
623 # sort of columns removed - empty sub
625 $main::lxdebug->enter_sub();
627 my ($self, @columns) = @_;
629 $main::lxdebug->leave_sub();
635 $main::lxdebug->enter_sub(2);
637 my ($self, $myconfig, $amount, $places, $dash) = @_;
638 my $neg = ($amount =~ s/-//);
640 $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
642 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
643 my @p = split /\./, $amount ; # split amount at decimal point
645 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
648 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
650 $amount = ($neg) ? "($amount)" : "$amount" if $dash =~ ?-?;
651 $amount = ($neg) ? "$amount DR" : "$amount CR" if $dash =~ ?DRCR?;
652 $amount = ($neg) ? "-$amount" : "$amount" if $dash =~ ??;
655 $main::lxdebug->leave_sub(2);
660 $main::lxdebug->enter_sub(2);
662 my ($self, $myconfig, $amount) = @_;
664 if ($myconfig->{in_numberformat} == 1) {
665 # Extra input number format 1000.00 or 1000,00
667 $amount = scalar reverse $amount;
668 $amount =~ s/\./DOT/;
670 $amount =~ s/DOT/\./;
671 $amount = scalar reverse $amount;
672 $main::lxdebug->leave_sub(2);
673 return ($amount * 1);
676 if ( ($myconfig->{numberformat} eq '1.000,00')
677 || ($myconfig->{numberformat} eq '1000,00')) {
682 if ($myconfig->{numberformat} eq "1'000.00") {
688 $main::lxdebug->leave_sub(2);
690 return ($amount * 1);
694 $main::lxdebug->enter_sub(2);
696 my ($self, $amount, $places) = @_;
699 # Rounding like "Kaufmannsrunden"
700 # Descr. http://de.wikipedia.org/wiki/Rundung
702 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
705 $amount = $amount * (10**($places));
706 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
708 $main::lxdebug->leave_sub(2);
710 return $round_amount;
715 $main::lxdebug->enter_sub();
717 my ($self, $myconfig, $userspath) = @_;
720 $self->{"cwd"} = getcwd();
721 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
723 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
724 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
725 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
726 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
727 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
728 } elsif (($self->{"format"} =~ /html/i) ||
729 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
730 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
733 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
734 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
736 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
737 qw(email tel fax name signature company address businessnumber));
739 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
741 # OUT is used for the media, screen, printer, email
742 # for postscript we store a copy in a temporary file
744 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
745 if ($template->uses_temp_file() || $self->{media} eq 'email') {
747 $self->{OUT} = ">$self->{tmpfile}";
751 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
753 open(OUT, ">-") or $self->error("STDOUT : $!");
757 if (!$template->parse(*OUT)) {
759 $self->error("$self->{IN} : " . $template->get_error());
765 #print(STDERR Dumper($self));
767 if ($template->uses_temp_file() || $self->{media} eq 'email') {
769 if ($self->{media} eq 'email') {
773 my $mail = new Mailer;
775 map { $mail->{$_} = $self->{$_} }
776 qw(cc bcc subject message version format charset);
777 $mail->{to} = qq|$self->{email}|;
778 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
779 $mail->{fileid} = "$fileid.";
781 # if we send html or plain text inline
782 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
783 $mail->{contenttype} = "text/html";
785 $mail->{message} =~ s/\r\n/<br>\n/g;
786 $myconfig->{signature} =~ s/\\n/<br>\n/g;
787 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
789 open(IN, $self->{tmpfile})
790 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
792 $mail->{message} .= $_;
799 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
801 $myconfig->{signature} =~ s/\\n/\r\n/g;
802 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
806 my $err = $mail->send($out);
807 $self->error($self->cleanup . "$err") if ($err);
813 my $numbytes = (-s $self->{tmpfile});
814 open(IN, $self->{tmpfile})
815 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
817 $self->{copies} = 1 unless $self->{media} eq 'printer';
819 chdir("$self->{cwd}");
820 #print(STDERR "Kopien $self->{copies}\n");
821 #print(STDERR "OUT $self->{OUT}\n");
822 for my $i (1 .. $self->{copies}) {
824 open(OUT, $self->{OUT})
825 or $self->error($self->cleanup . "$self->{OUT} : $!");
829 print qq|Content-Type: | . $template->get_mime_type() . qq|
830 Content-Disposition: attachment; filename="$self->{tmpfile}"
831 Content-Length: $numbytes
835 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
855 chdir("$self->{cwd}");
856 $main::lxdebug->leave_sub();
860 $main::lxdebug->enter_sub();
864 chdir("$self->{tmpdir}");
867 if (-f "$self->{tmpfile}.err") {
868 open(FH, "$self->{tmpfile}.err");
873 if ($self->{tmpfile}) {
874 $self->{tmpfile} =~ s|.*/||g;
876 $self->{tmpfile} =~ s/\.\w+$//g;
877 my $tmpfile = $self->{tmpfile};
878 unlink(<$tmpfile.*>);
881 chdir("$self->{cwd}");
883 $main::lxdebug->leave_sub();
889 $main::lxdebug->enter_sub();
891 my ($self, $date, $myconfig) = @_;
893 if ($date && $date =~ /\D/) {
895 if ($myconfig->{dateformat} =~ /^yy/) {
896 ($yy, $mm, $dd) = split /\D/, $date;
898 if ($myconfig->{dateformat} =~ /^mm/) {
899 ($mm, $dd, $yy) = split /\D/, $date;
901 if ($myconfig->{dateformat} =~ /^dd/) {
902 ($dd, $mm, $yy) = split /\D/, $date;
907 $yy = ($yy < 70) ? $yy + 2000 : $yy;
908 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
910 $dd = "0$dd" if ($dd < 10);
911 $mm = "0$mm" if ($mm < 10);
916 $main::lxdebug->leave_sub();
921 # Database routines used throughout
924 $main::lxdebug->enter_sub();
926 my ($self, $myconfig) = @_;
928 # connect to database
930 DBI->connect($myconfig->{dbconnect},
931 $myconfig->{dbuser}, $myconfig->{dbpasswd})
935 if ($myconfig->{dboptions}) {
936 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
939 $main::lxdebug->leave_sub();
944 sub dbconnect_noauto {
945 $main::lxdebug->enter_sub();
947 my ($self, $myconfig) = @_;
949 # connect to database
951 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
952 $myconfig->{dbpasswd}, { AutoCommit => 0 })
956 if ($myconfig->{dboptions}) {
957 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
960 $main::lxdebug->leave_sub();
966 $main::lxdebug->enter_sub();
968 my ($self, $dbh, $table, $field, $where, $value) = @_;
970 # if we have a value, go do it
973 # retrieve balance from table
974 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
975 my $sth = $dbh->prepare($query);
977 $sth->execute || $self->dberror($query);
978 my ($balance) = $sth->fetchrow_array;
984 $query = "UPDATE $table SET $field = $balance WHERE $where";
985 $dbh->do($query) || $self->dberror($query);
987 $main::lxdebug->leave_sub();
990 sub update_exchangerate {
991 $main::lxdebug->enter_sub();
993 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
995 # some sanity check for currency
997 $main::lxdebug->leave_sub();
1001 my $query = qq|SELECT e.curr FROM exchangerate e
1002 WHERE e.curr = '$curr'
1003 AND e.transdate = '$transdate'
1005 my $sth = $dbh->prepare($query);
1006 $sth->execute || $self->dberror($query);
1009 if ($buy != 0 && $sell != 0) {
1010 $set = "buy = $buy, sell = $sell";
1011 } elsif ($buy != 0) {
1012 $set = "buy = $buy";
1013 } elsif ($sell != 0) {
1014 $set = "sell = $sell";
1017 if ($sth->fetchrow_array) {
1018 $query = qq|UPDATE exchangerate
1020 WHERE curr = '$curr'
1021 AND transdate = '$transdate'|;
1023 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1024 VALUES ('$curr', $buy, $sell, '$transdate')|;
1027 $dbh->do($query) || $self->dberror($query);
1029 $main::lxdebug->leave_sub();
1032 sub save_exchangerate {
1033 $main::lxdebug->enter_sub();
1035 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1037 my $dbh = $self->dbconnect($myconfig);
1039 my ($buy, $sell) = (0, 0);
1040 $buy = $rate if $fld eq 'buy';
1041 $sell = $rate if $fld eq 'sell';
1043 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1047 $main::lxdebug->leave_sub();
1050 sub get_exchangerate {
1051 $main::lxdebug->enter_sub();
1053 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1055 unless ($transdate) {
1056 $main::lxdebug->leave_sub();
1060 my $query = qq|SELECT e.$fld FROM exchangerate e
1061 WHERE e.curr = '$curr'
1062 AND e.transdate = '$transdate'|;
1063 my $sth = $dbh->prepare($query);
1064 $sth->execute || $self->dberror($query);
1066 my ($exchangerate) = $sth->fetchrow_array;
1069 if ($exchangerate == 0) {
1073 $main::lxdebug->leave_sub();
1075 return $exchangerate;
1078 sub set_payment_options {
1079 $main::lxdebug->enter_sub();
1081 my ($self, $myconfig, $transdate) = @_;
1083 if ($self->{payment_id}) {
1085 my $dbh = $self->dbconnect($myconfig);
1088 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1089 WHERE p.id = $self->{payment_id}|;
1090 my $sth = $dbh->prepare($query);
1091 $sth->execute || $self->dberror($query);
1093 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1096 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1098 my $sth = $dbh->prepare($query);
1099 $sth->execute || $self->dberror($query);
1100 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1103 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1105 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1106 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1107 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1112 $main::lxdebug->leave_sub();
1116 sub check_exchangerate {
1117 $main::lxdebug->enter_sub();
1119 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1121 unless ($transdate) {
1122 $main::lxdebug->leave_sub();
1126 my $dbh = $self->dbconnect($myconfig);
1128 my $query = qq|SELECT e.$fld FROM exchangerate e
1129 WHERE e.curr = '$currency'
1130 AND e.transdate = '$transdate'|;
1131 my $sth = $dbh->prepare($query);
1132 $sth->execute || $self->dberror($query);
1134 my ($exchangerate) = $sth->fetchrow_array;
1138 $main::lxdebug->leave_sub();
1140 return $exchangerate;
1143 sub get_template_language {
1144 $main::lxdebug->enter_sub();
1146 my ($self, $myconfig) = @_;
1148 my $template_code = "";
1150 if ($self->{language_id}) {
1152 my $dbh = $self->dbconnect($myconfig);
1155 my $query = qq|SELECT l.template_code FROM language l
1156 WHERE l.id = $self->{language_id}|;
1157 my $sth = $dbh->prepare($query);
1158 $sth->execute || $self->dberror($query);
1160 ($template_code) = $sth->fetchrow_array;
1165 $main::lxdebug->leave_sub();
1167 return $template_code;
1170 sub get_printer_code {
1171 $main::lxdebug->enter_sub();
1173 my ($self, $myconfig) = @_;
1175 my $template_code = "";
1177 if ($self->{printer_id}) {
1179 my $dbh = $self->dbconnect($myconfig);
1182 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1183 WHERE p.id = $self->{printer_id}|;
1184 my $sth = $dbh->prepare($query);
1185 $sth->execute || $self->dberror($query);
1187 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1192 $main::lxdebug->leave_sub();
1194 return $template_code;
1198 $main::lxdebug->enter_sub();
1200 my ($self, $myconfig) = @_;
1202 my $template_code = "";
1204 if ($self->{shipto_id}) {
1206 my $dbh = $self->dbconnect($myconfig);
1209 my $query = qq|SELECT s.* FROM shipto s
1210 WHERE s.id = $self->{shipto_id}|;
1211 my $sth = $dbh->prepare($query);
1212 $sth->execute || $self->dberror($query);
1213 $ref = $sth->fetchrow_hashref(NAME_lc);
1214 map { $form->{$_} = $ref->{$_} } keys %$ref;
1219 $main::lxdebug->leave_sub();
1224 $main::lxdebug->enter_sub();
1226 my ($self, $dbh, $id, $module) = @_;
1230 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1232 if ($self->{"shipto$item"}) {
1233 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1235 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1239 if ($self->{shipto_id}) {
1240 my $query = qq| UPDATE shipto set
1241 shiptoname = '$self->{shiptoname}',
1242 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1243 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1244 shiptostreet = '$self->{shiptostreet}',
1245 shiptozipcode = '$self->{shiptozipcode}',
1246 shiptocity = '$self->{shiptocity}',
1247 shiptocountry = '$self->{shiptocountry}',
1248 shiptocontact = '$self->{shiptocontact}',
1249 shiptophone = '$self->{shiptophone}',
1250 shiptofax = '$self->{shiptofax}',
1251 shiptoemail = '$self->{shiptoemail}'
1252 WHERE id = $self->{shipto_id}|;
1253 $dbh->do($query) || $self->dberror($query);
1256 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1257 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1258 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1259 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1260 '$self->{shiptozipcode}', '$self->{shiptocity}',
1261 '$self->{shiptocountry}', '$self->{shiptocontact}',
1262 '$self->{shiptophone}', '$self->{shiptofax}',
1263 '$self->{shiptoemail}', '$module')|;
1264 $dbh->do($query) || $self->dberror($query);
1268 $main::lxdebug->leave_sub();
1272 $main::lxdebug->enter_sub();
1274 my ($self, $dbh) = @_;
1276 my $query = qq|SELECT e.id, e.name FROM employee e
1277 WHERE e.login = '$self->{login}'|;
1278 my $sth = $dbh->prepare($query);
1279 $sth->execute || $self->dberror($query);
1281 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1282 $self->{employee_id} *= 1;
1286 $main::lxdebug->leave_sub();
1289 # get other contact for transaction and form - html/tex
1291 $main::lxdebug->enter_sub();
1293 my ($self, $dbh, $id) = @_;
1295 my $query = qq|SELECT c.*
1298 $sth = $dbh->prepare($query);
1299 $sth->execute || $self->dberror($query);
1301 $ref = $sth->fetchrow_hashref(NAME_lc);
1303 push @{ $self->{$_} }, $ref;
1306 $main::lxdebug->leave_sub();
1309 # get contacts for id, if no contact return {"","","","",""}
1311 $main::lxdebug->enter_sub();
1313 my ($self, $dbh, $id) = @_;
1315 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1317 WHERE cp_cv_id=$id|;
1318 my $sth = $dbh->prepare($query);
1319 $sth->execute || $self->dberror($query);
1322 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1323 push @{ $self->{all_contacts} }, $ref;
1328 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1331 $main::lxdebug->leave_sub();
1334 # this sub gets the id and name from $table
1336 $main::lxdebug->enter_sub();
1338 my ($self, $myconfig, $table) = @_;
1340 # connect to database
1341 my $dbh = $self->dbconnect($myconfig);
1343 my $name = $self->like(lc $self->{$table});
1344 my $customernumber = $self->like(lc $self->{customernumber});
1346 if ($self->{customernumber} ne "") {
1347 $query = qq~SELECT c.id, c.name,
1348 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1350 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1353 $query = qq~SELECT c.id, c.name,
1354 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1356 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1360 if ($self->{openinvoices}) {
1361 $query = qq~SELECT DISTINCT c.id, c.name,
1362 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1363 FROM $self->{arap} a
1364 JOIN $table c ON (a.${table}_id = c.id)
1365 WHERE NOT a.amount = a.paid
1366 AND lower(c.name) LIKE '$name'
1369 my $sth = $dbh->prepare($query);
1371 $sth->execute || $self->dberror($query);
1374 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1375 push(@{ $self->{name_list} }, $ref);
1381 $main::lxdebug->leave_sub();
1386 # the selection sub is used in the AR, AP, IS, IR and OE module
1389 $main::lxdebug->enter_sub();
1391 my ($self, $myconfig, $table, $module) = @_;
1394 my $dbh = $self->dbconnect($myconfig);
1396 my $query = qq|SELECT count(*) FROM $table|;
1397 my $sth = $dbh->prepare($query);
1398 $sth->execute || $self->dberror($query);
1399 my ($count) = $sth->fetchrow_array;
1402 # build selection list
1403 if ($count < $myconfig->{vclimit}) {
1404 $query = qq|SELECT id, name
1405 FROM $table WHERE not obsolete
1407 $sth = $dbh->prepare($query);
1408 $sth->execute || $self->dberror($query);
1410 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1411 push @{ $self->{"all_$table"} }, $ref;
1419 $self->get_employee($dbh);
1421 # setup sales contacts
1422 $query = qq|SELECT e.id, e.name
1425 AND NOT e.id = $self->{employee_id}|;
1426 $sth = $dbh->prepare($query);
1427 $sth->execute || $self->dberror($query);
1429 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1430 push @{ $self->{all_employees} }, $ref;
1435 push @{ $self->{all_employees} },
1436 { id => $self->{employee_id},
1437 name => $self->{employee} };
1439 # sort the whole thing
1440 @{ $self->{all_employees} } =
1441 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1443 if ($module eq 'AR') {
1445 # prepare query for departments
1446 $query = qq|SELECT d.id, d.description
1452 $query = qq|SELECT d.id, d.description
1457 $sth = $dbh->prepare($query);
1458 $sth->execute || $self->dberror($query);
1460 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1461 push @{ $self->{all_departments} }, $ref;
1466 $query = qq|SELECT id, description
1469 $sth = $dbh->prepare($query);
1470 $sth->execute || $form->dberror($query);
1472 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1473 push @{ $self->{languages} }, $ref;
1478 $query = qq|SELECT printer_description, id
1481 $sth = $dbh->prepare($query);
1482 $sth->execute || $form->dberror($query);
1484 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1485 push @{ $self->{printers} }, $ref;
1491 $query = qq|SELECT id, description
1494 $sth = $dbh->prepare($query);
1495 $sth->execute || $form->dberror($query);
1497 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1498 push @{ $self->{payment_terms} }, $ref;
1502 $main::lxdebug->leave_sub();
1506 sub language_payment {
1507 $main::lxdebug->enter_sub();
1509 my ($self, $myconfig) = @_;
1510 undef $self->{languages};
1511 undef $self->{payment_terms};
1512 undef $self->{printers};
1515 my $dbh = $self->dbconnect($myconfig);
1517 my $query = qq|SELECT id, description
1520 my $sth = $dbh->prepare($query);
1521 $sth->execute || $form->dberror($query);
1523 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1524 push @{ $self->{languages} }, $ref;
1529 $query = qq|SELECT printer_description, id
1532 $sth = $dbh->prepare($query);
1533 $sth->execute || $form->dberror($query);
1535 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1536 push @{ $self->{printers} }, $ref;
1541 $query = qq|SELECT id, description
1544 $sth = $dbh->prepare($query);
1545 $sth->execute || $form->dberror($query);
1547 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1548 push @{ $self->{payment_terms} }, $ref;
1552 # get buchungsgruppen
1553 $query = qq|SELECT id, description
1554 FROM buchungsgruppen|;
1555 $sth = $dbh->prepare($query);
1556 $sth->execute || $form->dberror($query);
1558 $self->{BUCHUNGSGRUPPEN} = [];
1559 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1560 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1565 $query = qq|SELECT id, adr_description, adr_code
1567 $sth = $dbh->prepare($query);
1568 $sth->execute || $form->dberror($query);
1572 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1573 push @{ $self->{ADR} }, $ref;
1577 $main::lxdebug->leave_sub();
1580 # this is only used for reports
1581 sub all_departments {
1582 $main::lxdebug->enter_sub();
1584 my ($self, $myconfig, $table) = @_;
1586 my $dbh = $self->dbconnect($myconfig);
1587 my $where = "1 = 1";
1589 if (defined $table) {
1590 if ($table eq 'customer') {
1591 $where = " d.role = 'P'";
1595 my $query = qq|SELECT d.id, d.description
1599 my $sth = $dbh->prepare($query);
1600 $sth->execute || $self->dberror($query);
1602 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1603 push @{ $self->{all_departments} }, $ref;
1609 $main::lxdebug->leave_sub();
1613 $main::lxdebug->enter_sub();
1615 my ($self, $module, $myconfig, $table) = @_;
1617 $self->all_vc($myconfig, $table, $module);
1619 # get last customers or vendors
1622 my $dbh = $self->dbconnect($myconfig);
1626 # now get the account numbers
1627 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1629 WHERE c.link LIKE '%$module%'
1632 $sth = $dbh->prepare($query);
1633 $sth->execute || $self->dberror($query);
1635 $self->{accounts} = "";
1636 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1638 foreach my $key (split /:/, $ref->{link}) {
1639 if ($key =~ /$module/) {
1641 # cross reference for keys
1642 $xkeyref{ $ref->{accno} } = $key;
1644 push @{ $self->{"${module}_links"}{$key} },
1645 { accno => $ref->{accno},
1646 description => $ref->{description},
1647 taxkey => $ref->{taxkey_id} };
1649 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1655 # get taxkeys and description
1656 $query = qq|SELECT taxkey, taxdescription
1658 $sth = $dbh->prepare($query);
1659 $sth->execute || $self->dberror($query);
1661 $ref = $sth->fetchrow_hashref(NAME_lc);
1663 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1664 push @{ $self->{TAXKEY} }, $ref;
1671 $query = qq|SELECT id, description
1673 $sth = $dbh->prepare($query);
1674 $sth->execute || $form->dberror($query);
1677 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1678 push @{ $self->{TAXZONE} }, $ref;
1682 if (($module eq "AP") || ($module eq "AR")) {
1684 # get tax rates and description
1685 $query = qq| SELECT * FROM tax t|;
1686 $sth = $dbh->prepare($query);
1687 $sth->execute || $self->dberror($query);
1689 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1690 push @{ $self->{TAX} }, $ref;
1696 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1698 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1699 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1700 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1701 c.name AS $table, a.department_id, d.description AS department,
1702 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1703 a.employee_id, e.name AS employee, a.gldate, a.type
1705 JOIN $table c ON (a.${table}_id = c.id)
1706 LEFT JOIN employee e ON (e.id = a.employee_id)
1707 LEFT JOIN department d ON (d.id = a.department_id)
1708 WHERE a.id = $self->{id}|;
1709 $sth = $dbh->prepare($query);
1710 $sth->execute || $self->dberror($query);
1712 $ref = $sth->fetchrow_hashref(NAME_lc);
1713 foreach $key (keys %$ref) {
1714 $self->{$key} = $ref->{$key};
1718 # get amounts from individual entries
1719 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1720 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1722 JOIN chart c ON (c.id = a.chart_id)
1723 LEFT JOIN project p ON (p.id = a.project_id)
1724 LEFT Join tax t ON (a.taxkey = t.taxkey)
1725 WHERE a.trans_id = $self->{id}
1726 AND a.fx_transaction = '0'
1727 ORDER BY a.oid,a.transdate|;
1728 $sth = $dbh->prepare($query);
1729 $sth->execute || $self->dberror($query);
1731 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1733 # get exchangerate for currency
1734 $self->{exchangerate} =
1735 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1739 # store amounts in {acc_trans}{$key} for multiple accounts
1740 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1741 $ref->{exchangerate} =
1742 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1744 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1747 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1748 $ref->{amount} *= -1;
1750 $ref->{index} = $index;
1752 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1756 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1757 (SELECT c.accno FROM chart c
1758 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1759 (SELECT c.accno FROM chart c
1760 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1762 $sth = $dbh->prepare($query);
1763 $sth->execute || $self->dberror($query);
1765 $ref = $sth->fetchrow_hashref(NAME_lc);
1766 map { $self->{$_} = $ref->{$_} } keys %$ref;
1772 $query = qq|SELECT current_date AS transdate,
1773 d.curr AS currencies, d.closedto, d.revtrans,
1774 (SELECT c.accno FROM chart c
1775 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1776 (SELECT c.accno FROM chart c
1777 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1779 $sth = $dbh->prepare($query);
1780 $sth->execute || $self->dberror($query);
1782 $ref = $sth->fetchrow_hashref(NAME_lc);
1783 map { $self->{$_} = $ref->{$_} } keys %$ref;
1786 if ($self->{"$self->{vc}_id"}) {
1788 # only setup currency
1789 ($self->{currency}) = split /:/, $self->{currencies};
1793 $self->lastname_used($dbh, $myconfig, $table, $module);
1795 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1797 # get exchangerate for currency
1798 $self->{exchangerate} =
1799 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1808 $main::lxdebug->leave_sub();
1812 $main::lxdebug->enter_sub();
1814 my ($self, $dbh, $myconfig, $table, $module) = @_;
1816 my $arap = ($table eq 'customer') ? "ar" : "ap";
1817 my $where = "1 = 1";
1819 if ($self->{type} =~ /_order/) {
1821 $where = "quotation = '0'";
1823 if ($self->{type} =~ /_quotation/) {
1825 $where = "quotation = '1'";
1828 my $query = qq|SELECT MAX(id) FROM $arap
1830 AND ${table}_id > 0|;
1831 my $sth = $dbh->prepare($query);
1832 $sth->execute || $self->dberror($query);
1834 my ($trans_id) = $sth->fetchrow_array;
1838 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1839 current_date + ct.terms AS duedate, a.department_id,
1840 d.description AS department
1842 JOIN $table ct ON (a.${table}_id = ct.id)
1843 LEFT JOIN department d ON (a.department_id = d.id)
1844 WHERE a.id = $trans_id|;
1845 $sth = $dbh->prepare($query);
1846 $sth->execute || $self->dberror($query);
1848 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1849 $self->{duedate}, $self->{department_id}, $self->{department})
1850 = $sth->fetchrow_array;
1853 $main::lxdebug->leave_sub();
1857 $main::lxdebug->enter_sub();
1859 my ($self, $myconfig, $thisdate, $days) = @_;
1861 my $dbh = $self->dbconnect($myconfig);
1866 my $dateformat = $myconfig->{dateformat};
1867 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1869 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1871 $sth = $dbh->prepare($query);
1872 $sth->execute || $self->dberror($query);
1874 $query = qq|SELECT current_date AS thisdate
1876 $sth = $dbh->prepare($query);
1877 $sth->execute || $self->dberror($query);
1880 ($thisdate) = $sth->fetchrow_array;
1885 $main::lxdebug->leave_sub();
1891 $main::lxdebug->enter_sub();
1893 my ($self, $string) = @_;
1895 if ($string !~ /%/) {
1896 $string = "%$string%";
1899 $string =~ s/\'/\'\'/g;
1901 $main::lxdebug->leave_sub();
1907 $main::lxdebug->enter_sub();
1909 my ($self, $flds, $new, $count, $numrows) = @_;
1913 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1919 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1921 $j = $item->{ndx} - 1;
1922 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1926 for $i ($count + 1 .. $numrows) {
1927 map { delete $self->{"${_}_$i"} } @{$flds};
1930 $main::lxdebug->leave_sub();
1934 $main::lxdebug->enter_sub();
1936 my ($self, $myconfig) = @_;
1940 my $dbh = $self->dbconnect_noauto($myconfig);
1942 my $query = qq|DELETE FROM status
1943 WHERE formname = '$self->{formname}'
1945 my $sth = $dbh->prepare($query) || $self->dberror($query);
1947 if ($self->{formname} =~ /(check|receipt)/) {
1948 for $i (1 .. $self->{rowcount}) {
1949 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1953 $sth->execute($self->{id}) || $self->dberror($query);
1957 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
1958 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
1960 my %queued = split / /, $self->{queued};
1962 if ($self->{formname} =~ /(check|receipt)/) {
1964 # this is a check or receipt, add one entry for each lineitem
1965 my ($accno) = split /--/, $self->{account};
1966 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
1967 chart_id) VALUES (?, '$printed',
1968 '$queued{$self->{formname}}', '$self->{prinform}',
1969 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
1970 $sth = $dbh->prepare($query) || $self->dberror($query);
1972 for $i (1 .. $self->{rowcount}) {
1973 if ($self->{"checked_$i"}) {
1974 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
1979 $query = qq|INSERT INTO status (trans_id, printed, emailed,
1980 spoolfile, formname)
1981 VALUES ($self->{id}, '$printed', '$emailed',
1982 '$queued{$self->{formname}}', '$self->{formname}')|;
1983 $dbh->do($query) || $self->dberror($query);
1989 $main::lxdebug->leave_sub();
1993 $main::lxdebug->enter_sub();
1995 my ($self, $dbh) = @_;
1997 my ($query, $printed, $emailed);
1999 my $formnames = $self->{printed};
2000 my $emailforms = $self->{emailed};
2002 my $query = qq|DELETE FROM status
2003 WHERE formname = '$self->{formname}'
2004 AND trans_id = $self->{id}|;
2005 $dbh->do($query) || $self->dberror($query);
2007 # this only applies to the forms
2008 # checks and receipts are posted when printed or queued
2010 if ($self->{queued}) {
2011 my %queued = split / /, $self->{queued};
2013 foreach my $formname (keys %queued) {
2014 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2015 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2017 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2018 spoolfile, formname)
2019 VALUES ($self->{id}, '$printed', '$emailed',
2020 '$queued{$formname}', '$formname')|;
2021 $dbh->do($query) || $self->dberror($query);
2023 $formnames =~ s/$self->{formname}//;
2024 $emailforms =~ s/$self->{formname}//;
2029 # save printed, emailed info
2030 $formnames =~ s/^ +//g;
2031 $emailforms =~ s/^ +//g;
2034 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2035 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2037 foreach my $formname (keys %status) {
2038 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2039 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2041 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2042 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2043 $dbh->do($query) || $self->dberror($query);
2046 $main::lxdebug->leave_sub();
2049 sub update_defaults {
2050 $main::lxdebug->enter_sub();
2052 my ($self, $myconfig, $fld) = @_;
2054 my $dbh = $self->dbconnect_noauto($myconfig);
2055 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2056 my $sth = $dbh->prepare($query);
2058 $sth->execute || $self->dberror($query);
2059 my ($var) = $sth->fetchrow_array;
2064 $query = qq|UPDATE defaults
2066 $dbh->do($query) || $self->dberror($query);
2071 $main::lxdebug->leave_sub();
2076 sub update_business {
2077 $main::lxdebug->enter_sub();
2079 my ($self, $myconfig, $business_id) = @_;
2081 my $dbh = $self->dbconnect_noauto($myconfig);
2083 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2084 my $sth = $dbh->prepare($query);
2086 $sth->execute || $self->dberror($query);
2087 my ($var) = $sth->fetchrow_array;
2092 $query = qq|UPDATE business
2093 SET customernumberinit = '$var' WHERE id=$business_id|;
2094 $dbh->do($query) || $self->dberror($query);
2099 $main::lxdebug->leave_sub();
2105 $main::lxdebug->enter_sub();
2107 my ($self, $myconfig, $salesman) = @_;
2109 my $dbh = $self->dbconnect($myconfig);
2111 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2112 my $sth = $dbh->prepare($query);
2113 $sth->execute || $self->dberror($query);
2116 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2117 push(@{ $self->{salesman_list} }, $ref);
2121 $main::lxdebug->leave_sub();
2126 sub get_partsgroup {
2127 $main::lxdebug->enter_sub();
2129 my ($self, $myconfig, $p) = @_;
2131 my $dbh = $self->dbconnect($myconfig);
2133 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2135 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2137 if ($p->{searchitems} eq 'part') {
2139 WHERE p.inventory_accno_id > 0|;
2141 if ($p->{searchitems} eq 'service') {
2143 WHERE p.inventory_accno_id IS NULL|;
2145 if ($p->{searchitems} eq 'assembly') {
2147 WHERE p.assembly = '1'|;
2149 if ($p->{searchitems} eq 'labor') {
2151 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2155 ORDER BY partsgroup|;
2158 $query = qq|SELECT id, partsgroup FROM partsgroup
2159 ORDER BY partsgroup|;
2162 if ($p->{language_code}) {
2163 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2164 t.description AS translation
2166 JOIN parts p ON (p.partsgroup_id = pg.id)
2167 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2168 ORDER BY translation|;
2171 my $sth = $dbh->prepare($query);
2172 $sth->execute || $self->dberror($query);
2174 $self->{all_partsgroup} = ();
2175 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2176 push @{ $self->{all_partsgroup} }, $ref;
2180 $main::lxdebug->leave_sub();
2183 sub get_pricegroup {
2184 $main::lxdebug->enter_sub();
2186 my ($self, $myconfig, $p) = @_;
2188 my $dbh = $self->dbconnect($myconfig);
2190 my $query = qq|SELECT p.id, p.pricegroup
2194 ORDER BY pricegroup|;
2197 $query = qq|SELECT id, pricegroup FROM pricegroup
2198 ORDER BY pricegroup|;
2201 my $sth = $dbh->prepare($query);
2202 $sth->execute || $self->dberror($query);
2204 $self->{all_pricegroup} = ();
2205 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2206 push @{ $self->{all_pricegroup} }, $ref;
2211 $main::lxdebug->leave_sub();
2215 my ($self, $dbh, $myconfig, $audittrail) = @_;
2217 # table, $reference, $formname, $action, $id, $transdate) = @_;
2224 $dbh = $self->dbconnect($myconfig);
2228 # if we have an id add audittrail, otherwise get a new timestamp
2230 if ($audittrail->{id}) {
2232 $query = qq|SELECT audittrail FROM defaults|;
2234 if ($dbh->selectrow_array($query)) {
2235 my ($null, $employee_id) = $self->get_employee($dbh);
2237 if ($self->{audittrail} && !$myconfig) {
2238 chop $self->{audittrail};
2240 my @a = split /\|/, $self->{audittrail};
2244 my @flds = qw(tablename reference formname action transdate);
2246 # put into hash and remove dups
2248 $key = "$a[2]$a[3]";
2250 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2254 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2255 formname, action, employee_id, transdate)
2256 VALUES ($audittrail->{id}, ?, ?,
2257 ?, ?, $employee_id, ?)|;
2258 my $sth = $dbh->prepare($query) || $self->dberror($query);
2262 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2266 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2268 $sth->execute || $self->dberror;
2273 if ($audittrail->{transdate}) {
2274 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2275 formname, action, employee_id, transdate) VALUES (
2276 $audittrail->{id}, '$audittrail->{tablename}', |
2277 . $dbh->quote($audittrail->{reference}) . qq|,
2278 '$audittrail->{formname}', '$audittrail->{action}',
2279 $employee_id, '$audittrail->{transdate}')|;
2281 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2282 formname, action, employee_id) VALUES ($audittrail->{id},
2283 '$audittrail->{tablename}', |
2284 . $dbh->quote($audittrail->{reference}) . qq|,
2285 '$audittrail->{formname}', '$audittrail->{action}',
2292 $query = qq|SELECT current_timestamp FROM defaults|;
2293 my ($timestamp) = $dbh->selectrow_array($query);
2296 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2299 $dbh->disconnect if $disconnect;
2308 $main::lxdebug->enter_sub();
2310 my ($type, $country, $NLS_file) = @_;
2314 if ($country && -d "locale/$country") {
2315 $self->{countrycode} = $country;
2316 eval { require "locale/$country/$NLS_file"; };
2319 $self->{NLS_file} = $NLS_file;
2321 push @{ $self->{LONG_MONTH} },
2322 ("January", "February", "March", "April",
2323 "May ", "June", "July", "August",
2324 "September", "October", "November", "December");
2325 push @{ $self->{SHORT_MONTH} },
2326 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2328 $main::lxdebug->leave_sub();
2334 my ($self, $text) = @_;
2336 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2340 $main::lxdebug->enter_sub();
2342 my ($self, $text) = @_;
2344 if (exists $self{subs}{$text}) {
2345 $text = $self{subs}{$text};
2347 if ($self->{countrycode} && $self->{NLS_file}) {
2349 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2353 $main::lxdebug->leave_sub();
2359 $main::lxdebug->enter_sub();
2361 my ($self, $myconfig, $date, $longformat) = @_;
2364 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2369 $spc = $myconfig->{dateformat};
2371 $spc = substr($spc, 1, 1);
2373 if ($date =~ /\D/) {
2374 if ($myconfig->{dateformat} =~ /^yy/) {
2375 ($yy, $mm, $dd) = split /\D/, $date;
2377 if ($myconfig->{dateformat} =~ /^mm/) {
2378 ($mm, $dd, $yy) = split /\D/, $date;
2380 if ($myconfig->{dateformat} =~ /^dd/) {
2381 ($dd, $mm, $yy) = split /\D/, $date;
2384 $date = substr($date, 2);
2385 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2390 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2391 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2393 if ($myconfig->{dateformat} =~ /^dd/) {
2394 if (defined $longformat && $longformat == 0) {
2396 $dd = "0$dd" if ($dd < 10);
2397 $mm = "0$mm" if ($mm < 10);
2398 $longdate = "$dd$spc$mm$spc$yy";
2401 $longdate .= ($spc eq '.') ? ". " : " ";
2402 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2404 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2406 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2407 # Lx-Office is mainly used in Germany or German speaking countries.
2408 if (defined $longformat && $longformat == 0) {
2410 $dd = "0$dd" if ($dd < 10);
2411 $mm = "0$mm" if ($mm < 10);
2412 $longdate = "$yy-$mm-$dd";
2414 $longdate = "$dd. ";
2415 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2418 if (defined $longformat && $longformat == 0) {
2420 $dd = "0$dd" if ($dd < 10);
2421 $mm = "0$mm" if ($mm < 10);
2422 $longdate = "$mm$spc$dd$spc$yy";
2424 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2430 $main::lxdebug->leave_sub();
2436 $main::lxdebug->enter_sub();
2438 my ($self, $myconfig, $date, $longformat) = @_;
2441 $main::lxdebug->leave_sub();
2446 $spc = $myconfig->{dateformat};
2448 $spc = substr($spc, 1, 1);
2450 if ($date =~ /\D/) {
2451 if ($myconfig->{dateformat} =~ /^yy/) {
2452 ($yy, $mm, $dd) = split /\D/, $date;
2453 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2454 ($mm, $dd, $yy) = split /\D/, $date;
2455 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2456 ($dd, $mm, $yy) = split /\D/, $date;
2459 $date = substr($date, 2);
2460 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2465 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2466 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2468 $main::lxdebug->leave_sub();
2469 return ($yy, $mm, $dd);