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) = @_;
639 #Workaround for $format_amount calls without $places
640 if (!defined $places) {
641 (my $dec) = ($amount =~ /\.(\d+)/);
642 $places = length $dec;
645 if ($places =~ /\d/) {
646 $amount = $self->round_amount($amount, $places);
649 # is the amount negative
650 my $negative = ($amount < 0);
654 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
656 my ($whole, $dec) = split /\./, "$amount";
658 $amount = join '', reverse split //, $whole;
659 $fillup = "0" x ($places - length($dec));
661 if ($myconfig->{numberformat} eq '1,000.00') {
662 $amount =~ s/\d{3,}?/$&,/g;
664 $amount = join '', reverse split //, $amount;
665 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
668 if ($myconfig->{numberformat} eq '1.000,00') {
669 $amount =~ s/\d{3,}?/$&./g;
671 $amount = join '', reverse split //, $amount;
672 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
675 if ($myconfig->{numberformat} eq '1000,00') {
677 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
681 $amount = ($negative) ? "($amount)" : "$amount";
682 } elsif ($dash =~ /DRCR/) {
683 $amount = ($negative) ? "$amount DR" : "$amount CR";
685 $amount = ($negative) ? "-$amount" : "$amount";
689 if ($dash eq "0" && $places) {
690 if ($myconfig->{numberformat} eq '1.000,00') {
691 $amount = "0" . "," . "0" x $places;
693 $amount = "0" . "." . "0" x $places;
696 $amount = ($dash ne "") ? "$dash" : "0";
700 $main::lxdebug->leave_sub(2);
706 $main::lxdebug->enter_sub(2);
708 my ($self, $myconfig, $amount) = @_;
710 if ($myconfig->{in_numberformat} == 1) {
711 # Extra input number format 1000.00 or 1000,00
713 $amount = scalar reverse $amount;
714 $amount =~ s/\./DOT/;
716 $amount =~ s/DOT/\./;
717 $amount = scalar reverse $amount;
718 $main::lxdebug->leave_sub(2);
719 return ($amount * 1);
722 if ( ($myconfig->{numberformat} eq '1.000,00')
723 || ($myconfig->{numberformat} eq '1000,00')) {
728 if ($myconfig->{numberformat} eq "1'000.00") {
734 $main::lxdebug->leave_sub(2);
736 return ($amount * 1);
740 $main::lxdebug->enter_sub(2);
742 my ($self, $amount, $places) = @_;
745 # Rounding like "Kaufmannsrunden"
746 # Descr. http://de.wikipedia.org/wiki/Rundung
748 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
751 $amount = $amount * (10**($places));
752 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
754 $main::lxdebug->leave_sub(2);
756 return $round_amount;
761 $main::lxdebug->enter_sub();
763 my ($self, $myconfig, $userspath) = @_;
766 $self->{"cwd"} = getcwd();
767 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
769 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
770 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
771 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
772 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
773 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
774 } elsif (($self->{"format"} =~ /html/i) ||
775 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
776 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
779 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
780 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
782 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
783 qw(email tel fax name signature company address businessnumber));
785 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
787 # OUT is used for the media, screen, printer, email
788 # for postscript we store a copy in a temporary file
790 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
791 if ($template->uses_temp_file() || $self->{media} eq 'email') {
793 $self->{OUT} = ">$self->{tmpfile}";
797 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
799 open(OUT, ">-") or $self->error("STDOUT : $!");
803 if (!$template->parse(*OUT)) {
805 $self->error("$self->{IN} : " . $template->get_error());
811 #print(STDERR Dumper($self));
813 if ($template->uses_temp_file() || $self->{media} eq 'email') {
815 if ($self->{media} eq 'email') {
819 my $mail = new Mailer;
821 map { $mail->{$_} = $self->{$_} }
822 qw(cc bcc subject message version format charset);
823 $mail->{to} = qq|$self->{email}|;
824 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
825 $mail->{fileid} = "$fileid.";
827 # if we send html or plain text inline
828 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
829 $mail->{contenttype} = "text/html";
831 $mail->{message} =~ s/\r\n/<br>\n/g;
832 $myconfig->{signature} =~ s/\\n/<br>\n/g;
833 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
835 open(IN, $self->{tmpfile})
836 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
838 $mail->{message} .= $_;
845 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
847 $myconfig->{signature} =~ s/\\n/\r\n/g;
848 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
852 my $err = $mail->send($out);
853 $self->error($self->cleanup . "$err") if ($err);
859 my $numbytes = (-s $self->{tmpfile});
860 open(IN, $self->{tmpfile})
861 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
863 $self->{copies} = 1 unless $self->{media} eq 'printer';
865 chdir("$self->{cwd}");
866 #print(STDERR "Kopien $self->{copies}\n");
867 #print(STDERR "OUT $self->{OUT}\n");
868 for my $i (1 .. $self->{copies}) {
870 open(OUT, $self->{OUT})
871 or $self->error($self->cleanup . "$self->{OUT} : $!");
875 print qq|Content-Type: | . $template->get_mime_type() . qq|
876 Content-Disposition: attachment; filename="$self->{tmpfile}"
877 Content-Length: $numbytes
881 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
901 chdir("$self->{cwd}");
902 $main::lxdebug->leave_sub();
906 $main::lxdebug->enter_sub();
910 chdir("$self->{tmpdir}");
913 if (-f "$self->{tmpfile}.err") {
914 open(FH, "$self->{tmpfile}.err");
919 if ($self->{tmpfile}) {
920 $self->{tmpfile} =~ s|.*/||g;
922 $self->{tmpfile} =~ s/\.\w+$//g;
923 my $tmpfile = $self->{tmpfile};
924 unlink(<$tmpfile.*>);
927 chdir("$self->{cwd}");
929 $main::lxdebug->leave_sub();
935 $main::lxdebug->enter_sub();
937 my ($self, $date, $myconfig) = @_;
939 if ($date && $date =~ /\D/) {
941 if ($myconfig->{dateformat} =~ /^yy/) {
942 ($yy, $mm, $dd) = split /\D/, $date;
944 if ($myconfig->{dateformat} =~ /^mm/) {
945 ($mm, $dd, $yy) = split /\D/, $date;
947 if ($myconfig->{dateformat} =~ /^dd/) {
948 ($dd, $mm, $yy) = split /\D/, $date;
953 $yy = ($yy < 70) ? $yy + 2000 : $yy;
954 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
956 $dd = "0$dd" if ($dd < 10);
957 $mm = "0$mm" if ($mm < 10);
962 $main::lxdebug->leave_sub();
967 # Database routines used throughout
970 $main::lxdebug->enter_sub();
972 my ($self, $myconfig) = @_;
974 # connect to database
976 DBI->connect($myconfig->{dbconnect},
977 $myconfig->{dbuser}, $myconfig->{dbpasswd})
981 if ($myconfig->{dboptions}) {
982 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
985 $main::lxdebug->leave_sub();
990 sub dbconnect_noauto {
991 $main::lxdebug->enter_sub();
993 my ($self, $myconfig) = @_;
995 # connect to database
997 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
998 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1002 if ($myconfig->{dboptions}) {
1003 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1006 $main::lxdebug->leave_sub();
1011 sub update_balance {
1012 $main::lxdebug->enter_sub();
1014 my ($self, $dbh, $table, $field, $where, $value) = @_;
1016 # if we have a value, go do it
1019 # retrieve balance from table
1020 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1021 my $sth = $dbh->prepare($query);
1023 $sth->execute || $self->dberror($query);
1024 my ($balance) = $sth->fetchrow_array;
1030 $query = "UPDATE $table SET $field = $balance WHERE $where";
1031 $dbh->do($query) || $self->dberror($query);
1033 $main::lxdebug->leave_sub();
1036 sub update_exchangerate {
1037 $main::lxdebug->enter_sub();
1039 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1041 # some sanity check for currency
1043 $main::lxdebug->leave_sub();
1047 my $query = qq|SELECT e.curr FROM exchangerate e
1048 WHERE e.curr = '$curr'
1049 AND e.transdate = '$transdate'
1051 my $sth = $dbh->prepare($query);
1052 $sth->execute || $self->dberror($query);
1055 if ($buy != 0 && $sell != 0) {
1056 $set = "buy = $buy, sell = $sell";
1057 } elsif ($buy != 0) {
1058 $set = "buy = $buy";
1059 } elsif ($sell != 0) {
1060 $set = "sell = $sell";
1063 if ($sth->fetchrow_array) {
1064 $query = qq|UPDATE exchangerate
1066 WHERE curr = '$curr'
1067 AND transdate = '$transdate'|;
1069 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1070 VALUES ('$curr', $buy, $sell, '$transdate')|;
1073 $dbh->do($query) || $self->dberror($query);
1075 $main::lxdebug->leave_sub();
1078 sub save_exchangerate {
1079 $main::lxdebug->enter_sub();
1081 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1083 my $dbh = $self->dbconnect($myconfig);
1085 my ($buy, $sell) = (0, 0);
1086 $buy = $rate if $fld eq 'buy';
1087 $sell = $rate if $fld eq 'sell';
1089 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1093 $main::lxdebug->leave_sub();
1096 sub get_exchangerate {
1097 $main::lxdebug->enter_sub();
1099 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1101 unless ($transdate) {
1102 $main::lxdebug->leave_sub();
1106 my $query = qq|SELECT e.$fld FROM exchangerate e
1107 WHERE e.curr = '$curr'
1108 AND e.transdate = '$transdate'|;
1109 my $sth = $dbh->prepare($query);
1110 $sth->execute || $self->dberror($query);
1112 my ($exchangerate) = $sth->fetchrow_array;
1115 if ($exchangerate == 0) {
1119 $main::lxdebug->leave_sub();
1121 return $exchangerate;
1124 sub set_payment_options {
1125 $main::lxdebug->enter_sub();
1127 my ($self, $myconfig, $transdate) = @_;
1129 if ($self->{payment_id}) {
1131 my $dbh = $self->dbconnect($myconfig);
1134 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1135 WHERE p.id = $self->{payment_id}|;
1136 my $sth = $dbh->prepare($query);
1137 $sth->execute || $self->dberror($query);
1139 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1142 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1144 my $sth = $dbh->prepare($query);
1145 $sth->execute || $self->dberror($query);
1146 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1149 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1151 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1152 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1153 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1158 $main::lxdebug->leave_sub();
1162 sub check_exchangerate {
1163 $main::lxdebug->enter_sub();
1165 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1167 unless ($transdate) {
1168 $main::lxdebug->leave_sub();
1172 my $dbh = $self->dbconnect($myconfig);
1174 my $query = qq|SELECT e.$fld FROM exchangerate e
1175 WHERE e.curr = '$currency'
1176 AND e.transdate = '$transdate'|;
1177 my $sth = $dbh->prepare($query);
1178 $sth->execute || $self->dberror($query);
1180 my ($exchangerate) = $sth->fetchrow_array;
1184 $main::lxdebug->leave_sub();
1186 return $exchangerate;
1189 sub get_template_language {
1190 $main::lxdebug->enter_sub();
1192 my ($self, $myconfig) = @_;
1194 my $template_code = "";
1196 if ($self->{language_id}) {
1198 my $dbh = $self->dbconnect($myconfig);
1201 my $query = qq|SELECT l.template_code FROM language l
1202 WHERE l.id = $self->{language_id}|;
1203 my $sth = $dbh->prepare($query);
1204 $sth->execute || $self->dberror($query);
1206 ($template_code) = $sth->fetchrow_array;
1211 $main::lxdebug->leave_sub();
1213 return $template_code;
1216 sub get_printer_code {
1217 $main::lxdebug->enter_sub();
1219 my ($self, $myconfig) = @_;
1221 my $template_code = "";
1223 if ($self->{printer_id}) {
1225 my $dbh = $self->dbconnect($myconfig);
1228 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1229 WHERE p.id = $self->{printer_id}|;
1230 my $sth = $dbh->prepare($query);
1231 $sth->execute || $self->dberror($query);
1233 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1238 $main::lxdebug->leave_sub();
1240 return $template_code;
1244 $main::lxdebug->enter_sub();
1246 my ($self, $myconfig) = @_;
1248 my $template_code = "";
1250 if ($self->{shipto_id}) {
1252 my $dbh = $self->dbconnect($myconfig);
1255 my $query = qq|SELECT s.* FROM shipto s
1256 WHERE s.id = $self->{shipto_id}|;
1257 my $sth = $dbh->prepare($query);
1258 $sth->execute || $self->dberror($query);
1259 $ref = $sth->fetchrow_hashref(NAME_lc);
1260 map { $form->{$_} = $ref->{$_} } keys %$ref;
1265 $main::lxdebug->leave_sub();
1270 $main::lxdebug->enter_sub();
1272 my ($self, $dbh, $id, $module) = @_;
1276 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1278 if ($self->{"shipto$item"}) {
1279 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1281 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1285 if ($self->{shipto_id}) {
1286 my $query = qq| UPDATE shipto set
1287 shiptoname = '$self->{shiptoname}',
1288 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1289 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1290 shiptostreet = '$self->{shiptostreet}',
1291 shiptozipcode = '$self->{shiptozipcode}',
1292 shiptocity = '$self->{shiptocity}',
1293 shiptocountry = '$self->{shiptocountry}',
1294 shiptocontact = '$self->{shiptocontact}',
1295 shiptophone = '$self->{shiptophone}',
1296 shiptofax = '$self->{shiptofax}',
1297 shiptoemail = '$self->{shiptoemail}'
1298 WHERE id = $self->{shipto_id}|;
1299 $dbh->do($query) || $self->dberror($query);
1302 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1303 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1304 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1305 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1306 '$self->{shiptozipcode}', '$self->{shiptocity}',
1307 '$self->{shiptocountry}', '$self->{shiptocontact}',
1308 '$self->{shiptophone}', '$self->{shiptofax}',
1309 '$self->{shiptoemail}', '$module')|;
1310 $dbh->do($query) || $self->dberror($query);
1314 $main::lxdebug->leave_sub();
1318 $main::lxdebug->enter_sub();
1320 my ($self, $dbh) = @_;
1322 my $query = qq|SELECT e.id, e.name FROM employee e
1323 WHERE e.login = '$self->{login}'|;
1324 my $sth = $dbh->prepare($query);
1325 $sth->execute || $self->dberror($query);
1327 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1328 $self->{employee_id} *= 1;
1332 $main::lxdebug->leave_sub();
1335 # get other contact for transaction and form - html/tex
1337 $main::lxdebug->enter_sub();
1339 my ($self, $dbh, $id) = @_;
1341 my $query = qq|SELECT c.*
1344 $sth = $dbh->prepare($query);
1345 $sth->execute || $self->dberror($query);
1347 $ref = $sth->fetchrow_hashref(NAME_lc);
1349 push @{ $self->{$_} }, $ref;
1352 $main::lxdebug->leave_sub();
1355 # get contacts for id, if no contact return {"","","","",""}
1357 $main::lxdebug->enter_sub();
1359 my ($self, $dbh, $id) = @_;
1361 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1363 WHERE cp_cv_id=$id|;
1364 my $sth = $dbh->prepare($query);
1365 $sth->execute || $self->dberror($query);
1368 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1369 push @{ $self->{all_contacts} }, $ref;
1374 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1377 $main::lxdebug->leave_sub();
1380 # this sub gets the id and name from $table
1382 $main::lxdebug->enter_sub();
1384 my ($self, $myconfig, $table) = @_;
1386 # connect to database
1387 my $dbh = $self->dbconnect($myconfig);
1389 my $name = $self->like(lc $self->{$table});
1390 my $customernumber = $self->like(lc $self->{customernumber});
1392 if ($self->{customernumber} ne "") {
1393 $query = qq~SELECT c.id, c.name,
1394 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1396 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1399 $query = qq~SELECT c.id, c.name,
1400 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1402 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1406 if ($self->{openinvoices}) {
1407 $query = qq~SELECT DISTINCT c.id, c.name,
1408 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1409 FROM $self->{arap} a
1410 JOIN $table c ON (a.${table}_id = c.id)
1411 WHERE NOT a.amount = a.paid
1412 AND lower(c.name) LIKE '$name'
1415 my $sth = $dbh->prepare($query);
1417 $sth->execute || $self->dberror($query);
1420 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1421 push(@{ $self->{name_list} }, $ref);
1427 $main::lxdebug->leave_sub();
1432 # the selection sub is used in the AR, AP, IS, IR and OE module
1435 $main::lxdebug->enter_sub();
1437 my ($self, $myconfig, $table, $module) = @_;
1440 my $dbh = $self->dbconnect($myconfig);
1442 my $query = qq|SELECT count(*) FROM $table|;
1443 my $sth = $dbh->prepare($query);
1444 $sth->execute || $self->dberror($query);
1445 my ($count) = $sth->fetchrow_array;
1448 # build selection list
1449 if ($count < $myconfig->{vclimit}) {
1450 $query = qq|SELECT id, name
1451 FROM $table WHERE not obsolete
1453 $sth = $dbh->prepare($query);
1454 $sth->execute || $self->dberror($query);
1456 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1457 push @{ $self->{"all_$table"} }, $ref;
1465 $self->get_employee($dbh);
1467 # setup sales contacts
1468 $query = qq|SELECT e.id, e.name
1471 AND NOT e.id = $self->{employee_id}|;
1472 $sth = $dbh->prepare($query);
1473 $sth->execute || $self->dberror($query);
1475 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1476 push @{ $self->{all_employees} }, $ref;
1481 push @{ $self->{all_employees} },
1482 { id => $self->{employee_id},
1483 name => $self->{employee} };
1485 # sort the whole thing
1486 @{ $self->{all_employees} } =
1487 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1489 if ($module eq 'AR') {
1491 # prepare query for departments
1492 $query = qq|SELECT d.id, d.description
1498 $query = qq|SELECT d.id, d.description
1503 $sth = $dbh->prepare($query);
1504 $sth->execute || $self->dberror($query);
1506 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1507 push @{ $self->{all_departments} }, $ref;
1512 $query = qq|SELECT id, description
1515 $sth = $dbh->prepare($query);
1516 $sth->execute || $form->dberror($query);
1518 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1519 push @{ $self->{languages} }, $ref;
1524 $query = qq|SELECT printer_description, id
1527 $sth = $dbh->prepare($query);
1528 $sth->execute || $form->dberror($query);
1530 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1531 push @{ $self->{printers} }, $ref;
1537 $query = qq|SELECT id, description
1540 $sth = $dbh->prepare($query);
1541 $sth->execute || $form->dberror($query);
1543 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1544 push @{ $self->{payment_terms} }, $ref;
1548 $main::lxdebug->leave_sub();
1552 sub language_payment {
1553 $main::lxdebug->enter_sub();
1555 my ($self, $myconfig) = @_;
1556 undef $self->{languages};
1557 undef $self->{payment_terms};
1558 undef $self->{printers};
1561 my $dbh = $self->dbconnect($myconfig);
1563 my $query = qq|SELECT id, description
1566 my $sth = $dbh->prepare($query);
1567 $sth->execute || $form->dberror($query);
1569 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1570 push @{ $self->{languages} }, $ref;
1575 $query = qq|SELECT printer_description, id
1578 $sth = $dbh->prepare($query);
1579 $sth->execute || $form->dberror($query);
1581 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1582 push @{ $self->{printers} }, $ref;
1587 $query = qq|SELECT id, description
1590 $sth = $dbh->prepare($query);
1591 $sth->execute || $form->dberror($query);
1593 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1594 push @{ $self->{payment_terms} }, $ref;
1598 # get buchungsgruppen
1599 $query = qq|SELECT id, description
1600 FROM buchungsgruppen|;
1601 $sth = $dbh->prepare($query);
1602 $sth->execute || $form->dberror($query);
1604 $self->{BUCHUNGSGRUPPEN} = [];
1605 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1606 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1611 $query = qq|SELECT id, adr_description, adr_code
1613 $sth = $dbh->prepare($query);
1614 $sth->execute || $form->dberror($query);
1618 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1619 push @{ $self->{ADR} }, $ref;
1623 $main::lxdebug->leave_sub();
1626 # this is only used for reports
1627 sub all_departments {
1628 $main::lxdebug->enter_sub();
1630 my ($self, $myconfig, $table) = @_;
1632 my $dbh = $self->dbconnect($myconfig);
1633 my $where = "1 = 1";
1635 if (defined $table) {
1636 if ($table eq 'customer') {
1637 $where = " d.role = 'P'";
1641 my $query = qq|SELECT d.id, d.description
1645 my $sth = $dbh->prepare($query);
1646 $sth->execute || $self->dberror($query);
1648 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1649 push @{ $self->{all_departments} }, $ref;
1655 $main::lxdebug->leave_sub();
1659 $main::lxdebug->enter_sub();
1661 my ($self, $module, $myconfig, $table) = @_;
1663 $self->all_vc($myconfig, $table, $module);
1665 # get last customers or vendors
1668 my $dbh = $self->dbconnect($myconfig);
1672 # now get the account numbers
1673 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1675 WHERE c.link LIKE '%$module%'
1678 $sth = $dbh->prepare($query);
1679 $sth->execute || $self->dberror($query);
1681 $self->{accounts} = "";
1682 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1684 foreach my $key (split /:/, $ref->{link}) {
1685 if ($key =~ /$module/) {
1687 # cross reference for keys
1688 $xkeyref{ $ref->{accno} } = $key;
1690 push @{ $self->{"${module}_links"}{$key} },
1691 { accno => $ref->{accno},
1692 description => $ref->{description},
1693 taxkey => $ref->{taxkey_id} };
1695 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1701 # get taxkeys and description
1702 $query = qq|SELECT taxkey, taxdescription
1704 $sth = $dbh->prepare($query);
1705 $sth->execute || $self->dberror($query);
1707 $ref = $sth->fetchrow_hashref(NAME_lc);
1709 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1710 push @{ $self->{TAXKEY} }, $ref;
1717 $query = qq|SELECT id, description
1719 $sth = $dbh->prepare($query);
1720 $sth->execute || $form->dberror($query);
1723 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1724 push @{ $self->{TAXZONE} }, $ref;
1728 if (($module eq "AP") || ($module eq "AR")) {
1730 # get tax rates and description
1731 $query = qq| SELECT * FROM tax t|;
1732 $sth = $dbh->prepare($query);
1733 $sth->execute || $self->dberror($query);
1735 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1736 push @{ $self->{TAX} }, $ref;
1742 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1744 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1745 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1746 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1747 c.name AS $table, a.department_id, d.description AS department,
1748 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1749 a.employee_id, e.name AS employee, a.gldate, a.type
1751 JOIN $table c ON (a.${table}_id = c.id)
1752 LEFT JOIN employee e ON (e.id = a.employee_id)
1753 LEFT JOIN department d ON (d.id = a.department_id)
1754 WHERE a.id = $self->{id}|;
1755 $sth = $dbh->prepare($query);
1756 $sth->execute || $self->dberror($query);
1758 $ref = $sth->fetchrow_hashref(NAME_lc);
1759 foreach $key (keys %$ref) {
1760 $self->{$key} = $ref->{$key};
1764 # get amounts from individual entries
1765 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1766 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1768 JOIN chart c ON (c.id = a.chart_id)
1769 LEFT JOIN project p ON (p.id = a.project_id)
1770 LEFT Join tax t ON (a.taxkey = t.taxkey)
1771 WHERE a.trans_id = $self->{id}
1772 AND a.fx_transaction = '0'
1773 ORDER BY a.oid,a.transdate|;
1774 $sth = $dbh->prepare($query);
1775 $sth->execute || $self->dberror($query);
1777 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1779 # get exchangerate for currency
1780 $self->{exchangerate} =
1781 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1785 # store amounts in {acc_trans}{$key} for multiple accounts
1786 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1787 $ref->{exchangerate} =
1788 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1790 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1793 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1794 $ref->{amount} *= -1;
1796 $ref->{index} = $index;
1798 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1802 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1803 (SELECT c.accno FROM chart c
1804 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1805 (SELECT c.accno FROM chart c
1806 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1808 $sth = $dbh->prepare($query);
1809 $sth->execute || $self->dberror($query);
1811 $ref = $sth->fetchrow_hashref(NAME_lc);
1812 map { $self->{$_} = $ref->{$_} } keys %$ref;
1818 $query = qq|SELECT current_date AS transdate,
1819 d.curr AS currencies, d.closedto, d.revtrans,
1820 (SELECT c.accno FROM chart c
1821 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1822 (SELECT c.accno FROM chart c
1823 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1825 $sth = $dbh->prepare($query);
1826 $sth->execute || $self->dberror($query);
1828 $ref = $sth->fetchrow_hashref(NAME_lc);
1829 map { $self->{$_} = $ref->{$_} } keys %$ref;
1832 if ($self->{"$self->{vc}_id"}) {
1834 # only setup currency
1835 ($self->{currency}) = split /:/, $self->{currencies};
1839 $self->lastname_used($dbh, $myconfig, $table, $module);
1841 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1843 # get exchangerate for currency
1844 $self->{exchangerate} =
1845 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1854 $main::lxdebug->leave_sub();
1858 $main::lxdebug->enter_sub();
1860 my ($self, $dbh, $myconfig, $table, $module) = @_;
1862 my $arap = ($table eq 'customer') ? "ar" : "ap";
1863 my $where = "1 = 1";
1865 if ($self->{type} =~ /_order/) {
1867 $where = "quotation = '0'";
1869 if ($self->{type} =~ /_quotation/) {
1871 $where = "quotation = '1'";
1874 my $query = qq|SELECT MAX(id) FROM $arap
1876 AND ${table}_id > 0|;
1877 my $sth = $dbh->prepare($query);
1878 $sth->execute || $self->dberror($query);
1880 my ($trans_id) = $sth->fetchrow_array;
1884 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1885 current_date + ct.terms AS duedate, a.department_id,
1886 d.description AS department
1888 JOIN $table ct ON (a.${table}_id = ct.id)
1889 LEFT JOIN department d ON (a.department_id = d.id)
1890 WHERE a.id = $trans_id|;
1891 $sth = $dbh->prepare($query);
1892 $sth->execute || $self->dberror($query);
1894 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1895 $self->{duedate}, $self->{department_id}, $self->{department})
1896 = $sth->fetchrow_array;
1899 $main::lxdebug->leave_sub();
1903 $main::lxdebug->enter_sub();
1905 my ($self, $myconfig, $thisdate, $days) = @_;
1907 my $dbh = $self->dbconnect($myconfig);
1912 my $dateformat = $myconfig->{dateformat};
1913 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1915 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1917 $sth = $dbh->prepare($query);
1918 $sth->execute || $self->dberror($query);
1920 $query = qq|SELECT current_date AS thisdate
1922 $sth = $dbh->prepare($query);
1923 $sth->execute || $self->dberror($query);
1926 ($thisdate) = $sth->fetchrow_array;
1931 $main::lxdebug->leave_sub();
1937 $main::lxdebug->enter_sub();
1939 my ($self, $string) = @_;
1941 if ($string !~ /%/) {
1942 $string = "%$string%";
1945 $string =~ s/\'/\'\'/g;
1947 $main::lxdebug->leave_sub();
1953 $main::lxdebug->enter_sub();
1955 my ($self, $flds, $new, $count, $numrows) = @_;
1959 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1965 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1967 $j = $item->{ndx} - 1;
1968 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1972 for $i ($count + 1 .. $numrows) {
1973 map { delete $self->{"${_}_$i"} } @{$flds};
1976 $main::lxdebug->leave_sub();
1980 $main::lxdebug->enter_sub();
1982 my ($self, $myconfig) = @_;
1986 my $dbh = $self->dbconnect_noauto($myconfig);
1988 my $query = qq|DELETE FROM status
1989 WHERE formname = '$self->{formname}'
1991 my $sth = $dbh->prepare($query) || $self->dberror($query);
1993 if ($self->{formname} =~ /(check|receipt)/) {
1994 for $i (1 .. $self->{rowcount}) {
1995 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1999 $sth->execute($self->{id}) || $self->dberror($query);
2003 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2004 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2006 my %queued = split / /, $self->{queued};
2008 if ($self->{formname} =~ /(check|receipt)/) {
2010 # this is a check or receipt, add one entry for each lineitem
2011 my ($accno) = split /--/, $self->{account};
2012 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2013 chart_id) VALUES (?, '$printed',
2014 '$queued{$self->{formname}}', '$self->{prinform}',
2015 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2016 $sth = $dbh->prepare($query) || $self->dberror($query);
2018 for $i (1 .. $self->{rowcount}) {
2019 if ($self->{"checked_$i"}) {
2020 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2025 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2026 spoolfile, formname)
2027 VALUES ($self->{id}, '$printed', '$emailed',
2028 '$queued{$self->{formname}}', '$self->{formname}')|;
2029 $dbh->do($query) || $self->dberror($query);
2035 $main::lxdebug->leave_sub();
2039 $main::lxdebug->enter_sub();
2041 my ($self, $dbh) = @_;
2043 my ($query, $printed, $emailed);
2045 my $formnames = $self->{printed};
2046 my $emailforms = $self->{emailed};
2048 my $query = qq|DELETE FROM status
2049 WHERE formname = '$self->{formname}'
2050 AND trans_id = $self->{id}|;
2051 $dbh->do($query) || $self->dberror($query);
2053 # this only applies to the forms
2054 # checks and receipts are posted when printed or queued
2056 if ($self->{queued}) {
2057 my %queued = split / /, $self->{queued};
2059 foreach my $formname (keys %queued) {
2060 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2061 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2063 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2064 spoolfile, formname)
2065 VALUES ($self->{id}, '$printed', '$emailed',
2066 '$queued{$formname}', '$formname')|;
2067 $dbh->do($query) || $self->dberror($query);
2069 $formnames =~ s/$self->{formname}//;
2070 $emailforms =~ s/$self->{formname}//;
2075 # save printed, emailed info
2076 $formnames =~ s/^ +//g;
2077 $emailforms =~ s/^ +//g;
2080 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2081 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2083 foreach my $formname (keys %status) {
2084 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2085 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2087 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2088 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2089 $dbh->do($query) || $self->dberror($query);
2092 $main::lxdebug->leave_sub();
2095 sub update_defaults {
2096 $main::lxdebug->enter_sub();
2098 my ($self, $myconfig, $fld) = @_;
2100 my $dbh = $self->dbconnect_noauto($myconfig);
2101 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2102 my $sth = $dbh->prepare($query);
2104 $sth->execute || $self->dberror($query);
2105 my ($var) = $sth->fetchrow_array;
2110 $query = qq|UPDATE defaults
2112 $dbh->do($query) || $self->dberror($query);
2117 $main::lxdebug->leave_sub();
2122 sub update_business {
2123 $main::lxdebug->enter_sub();
2125 my ($self, $myconfig, $business_id) = @_;
2127 my $dbh = $self->dbconnect_noauto($myconfig);
2129 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2130 my $sth = $dbh->prepare($query);
2132 $sth->execute || $self->dberror($query);
2133 my ($var) = $sth->fetchrow_array;
2138 $query = qq|UPDATE business
2139 SET customernumberinit = '$var' WHERE id=$business_id|;
2140 $dbh->do($query) || $self->dberror($query);
2145 $main::lxdebug->leave_sub();
2151 $main::lxdebug->enter_sub();
2153 my ($self, $myconfig, $salesman) = @_;
2155 my $dbh = $self->dbconnect($myconfig);
2157 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2158 my $sth = $dbh->prepare($query);
2159 $sth->execute || $self->dberror($query);
2162 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2163 push(@{ $self->{salesman_list} }, $ref);
2167 $main::lxdebug->leave_sub();
2172 sub get_partsgroup {
2173 $main::lxdebug->enter_sub();
2175 my ($self, $myconfig, $p) = @_;
2177 my $dbh = $self->dbconnect($myconfig);
2179 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2181 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2183 if ($p->{searchitems} eq 'part') {
2185 WHERE p.inventory_accno_id > 0|;
2187 if ($p->{searchitems} eq 'service') {
2189 WHERE p.inventory_accno_id IS NULL|;
2191 if ($p->{searchitems} eq 'assembly') {
2193 WHERE p.assembly = '1'|;
2195 if ($p->{searchitems} eq 'labor') {
2197 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2201 ORDER BY partsgroup|;
2204 $query = qq|SELECT id, partsgroup FROM partsgroup
2205 ORDER BY partsgroup|;
2208 if ($p->{language_code}) {
2209 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2210 t.description AS translation
2212 JOIN parts p ON (p.partsgroup_id = pg.id)
2213 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2214 ORDER BY translation|;
2217 my $sth = $dbh->prepare($query);
2218 $sth->execute || $self->dberror($query);
2220 $self->{all_partsgroup} = ();
2221 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2222 push @{ $self->{all_partsgroup} }, $ref;
2226 $main::lxdebug->leave_sub();
2229 sub get_pricegroup {
2230 $main::lxdebug->enter_sub();
2232 my ($self, $myconfig, $p) = @_;
2234 my $dbh = $self->dbconnect($myconfig);
2236 my $query = qq|SELECT p.id, p.pricegroup
2240 ORDER BY pricegroup|;
2243 $query = qq|SELECT id, pricegroup FROM pricegroup
2244 ORDER BY pricegroup|;
2247 my $sth = $dbh->prepare($query);
2248 $sth->execute || $self->dberror($query);
2250 $self->{all_pricegroup} = ();
2251 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2252 push @{ $self->{all_pricegroup} }, $ref;
2257 $main::lxdebug->leave_sub();
2261 my ($self, $dbh, $myconfig, $audittrail) = @_;
2263 # table, $reference, $formname, $action, $id, $transdate) = @_;
2270 $dbh = $self->dbconnect($myconfig);
2274 # if we have an id add audittrail, otherwise get a new timestamp
2276 if ($audittrail->{id}) {
2278 $query = qq|SELECT audittrail FROM defaults|;
2280 if ($dbh->selectrow_array($query)) {
2281 my ($null, $employee_id) = $self->get_employee($dbh);
2283 if ($self->{audittrail} && !$myconfig) {
2284 chop $self->{audittrail};
2286 my @a = split /\|/, $self->{audittrail};
2290 my @flds = qw(tablename reference formname action transdate);
2292 # put into hash and remove dups
2294 $key = "$a[2]$a[3]";
2296 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2300 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2301 formname, action, employee_id, transdate)
2302 VALUES ($audittrail->{id}, ?, ?,
2303 ?, ?, $employee_id, ?)|;
2304 my $sth = $dbh->prepare($query) || $self->dberror($query);
2308 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2312 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2314 $sth->execute || $self->dberror;
2319 if ($audittrail->{transdate}) {
2320 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2321 formname, action, employee_id, transdate) VALUES (
2322 $audittrail->{id}, '$audittrail->{tablename}', |
2323 . $dbh->quote($audittrail->{reference}) . qq|,
2324 '$audittrail->{formname}', '$audittrail->{action}',
2325 $employee_id, '$audittrail->{transdate}')|;
2327 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2328 formname, action, employee_id) VALUES ($audittrail->{id},
2329 '$audittrail->{tablename}', |
2330 . $dbh->quote($audittrail->{reference}) . qq|,
2331 '$audittrail->{formname}', '$audittrail->{action}',
2338 $query = qq|SELECT current_timestamp FROM defaults|;
2339 my ($timestamp) = $dbh->selectrow_array($query);
2342 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2345 $dbh->disconnect if $disconnect;
2354 $main::lxdebug->enter_sub();
2356 my ($type, $country, $NLS_file) = @_;
2360 if ($country && -d "locale/$country") {
2361 $self->{countrycode} = $country;
2362 eval { require "locale/$country/$NLS_file"; };
2365 $self->{NLS_file} = $NLS_file;
2367 push @{ $self->{LONG_MONTH} },
2368 ("January", "February", "March", "April",
2369 "May ", "June", "July", "August",
2370 "September", "October", "November", "December");
2371 push @{ $self->{SHORT_MONTH} },
2372 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2374 $main::lxdebug->leave_sub();
2380 my ($self, $text) = @_;
2382 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2386 $main::lxdebug->enter_sub();
2388 my ($self, $text) = @_;
2390 if (exists $self{subs}{$text}) {
2391 $text = $self{subs}{$text};
2393 if ($self->{countrycode} && $self->{NLS_file}) {
2395 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2399 $main::lxdebug->leave_sub();
2405 $main::lxdebug->enter_sub();
2407 my ($self, $myconfig, $date, $longformat) = @_;
2410 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2415 $spc = $myconfig->{dateformat};
2417 $spc = substr($spc, 1, 1);
2419 if ($date =~ /\D/) {
2420 if ($myconfig->{dateformat} =~ /^yy/) {
2421 ($yy, $mm, $dd) = split /\D/, $date;
2423 if ($myconfig->{dateformat} =~ /^mm/) {
2424 ($mm, $dd, $yy) = split /\D/, $date;
2426 if ($myconfig->{dateformat} =~ /^dd/) {
2427 ($dd, $mm, $yy) = split /\D/, $date;
2430 $date = substr($date, 2);
2431 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2436 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2437 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2439 if ($myconfig->{dateformat} =~ /^dd/) {
2440 if (defined $longformat && $longformat == 0) {
2442 $dd = "0$dd" if ($dd < 10);
2443 $mm = "0$mm" if ($mm < 10);
2444 $longdate = "$dd$spc$mm$spc$yy";
2447 $longdate .= ($spc eq '.') ? ". " : " ";
2448 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2450 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2452 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2453 # Lx-Office is mainly used in Germany or German speaking countries.
2454 if (defined $longformat && $longformat == 0) {
2456 $dd = "0$dd" if ($dd < 10);
2457 $mm = "0$mm" if ($mm < 10);
2458 $longdate = "$yy-$mm-$dd";
2460 $longdate = "$dd. ";
2461 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2464 if (defined $longformat && $longformat == 0) {
2466 $dd = "0$dd" if ($dd < 10);
2467 $mm = "0$mm" if ($mm < 10);
2468 $longdate = "$mm$spc$dd$spc$yy";
2470 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2476 $main::lxdebug->leave_sub();
2482 $main::lxdebug->enter_sub();
2484 my ($self, $myconfig, $date, $longformat) = @_;
2487 $main::lxdebug->leave_sub();
2492 $spc = $myconfig->{dateformat};
2494 $spc = substr($spc, 1, 1);
2496 if ($date =~ /\D/) {
2497 if ($myconfig->{dateformat} =~ /^yy/) {
2498 ($yy, $mm, $dd) = split /\D/, $date;
2499 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2500 ($mm, $dd, $yy) = split /\D/, $date;
2501 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2502 ($dd, $mm, $yy) = split /\D/, $date;
2505 $date = substr($date, 2);
2506 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2511 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2512 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2514 $main::lxdebug->leave_sub();
2515 return ($yy, $mm, $dd);