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->{menubar} = 1 if $self->{path} =~ /lynx/i;
148 $self->{action} = lc $self->{action};
149 $self->{action} =~ s/( |-|,|\#)/_/g;
151 $self->{version} = "2.4.0";
153 $main::lxdebug->leave_sub();
159 $main::lxdebug->enter_sub();
165 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
167 $main::lxdebug->leave_sub();
171 $main::lxdebug->enter_sub(2);
173 my ($self, $str, $beenthere) = @_;
175 # for Apache 2 we escape strings twice
176 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
177 # $str = $self->escape($str, 1);
180 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
182 $main::lxdebug->leave_sub(2);
188 $main::lxdebug->enter_sub(2);
190 my ($self, $str) = @_;
195 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
197 $main::lxdebug->leave_sub(2);
203 my ($self, $str) = @_;
205 if ($str && !ref($str)) {
206 $str =~ s/\"/"/g;
214 my ($self, $str) = @_;
216 if ($str && !ref($str)) {
217 $str =~ s/"/\"/g;
229 print qq|<input type=hidden name="$_" value="|
230 . $self->quote($self->{$_})
234 delete $self->{header};
235 for (sort keys %$self) {
236 print qq|<input type=hidden name="$_" value="|
237 . $self->quote($self->{$_})
245 $main::lxdebug->enter_sub();
247 my ($self, $msg) = @_;
248 if ($ENV{HTTP_USER_AGENT}) {
250 $self->show_generic_error($msg);
254 if ($self->{error_function}) {
255 &{ $self->{error_function} }($msg);
261 $main::lxdebug->leave_sub();
265 $main::lxdebug->enter_sub();
267 my ($self, $msg) = @_;
269 if ($ENV{HTTP_USER_AGENT}) {
272 if (!$self->{header}) {
285 if ($self->{info_function}) {
286 &{ $self->{info_function} }($msg);
292 $main::lxdebug->leave_sub();
296 $main::lxdebug->enter_sub();
298 my ($self, $str, $cols, $maxrows) = @_;
302 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
304 $maxrows = $rows unless defined $maxrows;
306 $main::lxdebug->leave_sub();
308 return ($rows > $maxrows) ? $maxrows : $rows;
312 $main::lxdebug->enter_sub();
314 my ($self, $msg) = @_;
316 $self->error("$msg\n" . $DBI::errstr);
318 $main::lxdebug->leave_sub();
322 $main::lxdebug->enter_sub();
324 my ($self, $name, $msg) = @_;
326 if ($self->{$name} =~ /^\s*$/) {
329 $main::lxdebug->leave_sub();
333 $main::lxdebug->enter_sub();
337 if ($self->{header}) {
338 $main::lxdebug->leave_sub();
342 my ($stylesheet, $favicon, $charset);
344 if ($ENV{HTTP_USER_AGENT}) {
346 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
348 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
352 if ($self->{favicon} && (-f "$self->{favicon}")) {
354 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
358 if ($self->{charset}) {
360 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
363 if ($self->{landscape}) {
364 $pagelayout = qq|<style type="text/css">
365 \@page { size:landscape; }
368 if ($self->{fokus}) {
369 $fokus = qq|<script type="text/javascript">
371 function fokus(){document.$self->{fokus}.focus();}
378 if ($self->{jsscript} == 1) {
381 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
382 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
383 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
384 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
391 ? "$self->{title} - $self->{titlebar}"
394 foreach $item (@ { $self->{AJAX} }) {
395 $ajax .= $item->show_javascript();
397 print qq|Content-Type: text/html
401 <title>$self->{titlebar}</title>
409 <script type="text/javascript" src="js/highlight_input.js"></script>
410 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
412 <script type="text/javascript" src="js/tabcontent.js">
414 /***********************************************
415 * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
416 * This notice MUST stay intact for legal use
417 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
418 ***********************************************/
428 $main::lxdebug->leave_sub();
431 sub parse_html_template {
432 $main::lxdebug->enter_sub();
434 my ($self, $file, $additional_params) = @_;
437 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
438 $language = $main::language;
440 $language = $main::myconfig{"countrycode"};
443 if (-f "templates/webpages/${file}_${language}.html") {
444 if ((-f ".developer") &&
445 (-f "templates/webpages/${file}_master.html") &&
446 ((stat("templates/webpages/${file}_master.html"))[9] >
447 (stat("templates/webpages/${file}_${language}.html"))[9])) {
448 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
449 "Please re-run 'locales.pl' in 'locale/${language}'.";
450 print(qq|<pre>$info</pre>|);
454 $file = "templates/webpages/${file}_${language}.html";
455 } elsif (-f "templates/webpages/${file}.html") {
456 $file = "templates/webpages/${file}.html";
458 my $info = "Web page template '${file}' not found.\n" .
459 "Please re-run 'locales.pl' in 'locale/${language}'.";
460 print(qq|<pre>$info</pre>|);
464 my $template = HTML::Template->new("filename" => $file,
465 "die_on_bad_params" => 0,
467 "case_sensitive" => 1,
468 "loop_context_vars" => 1,
471 $additional_params = {} unless ($additional_params);
472 if ($self->{"DEBUG"}) {
473 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
476 if ($additional_params->{"DEBUG"}) {
477 $additional_params->{"DEBUG"} =
478 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
481 if (%main::myconfig) {
482 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
483 my $jsc_dateformat = $main::myconfig{"dateformat"};
484 $jsc_dateformat =~ s/d+/\%d/gi;
485 $jsc_dateformat =~ s/m+/\%m/gi;
486 $jsc_dateformat =~ s/y+/\%Y/gi;
487 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
490 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
491 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
492 $additional_params->{"conf_latex_templates"} = $main::latex;
493 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
496 if (-f $self->{"login"} . "_menu.ini") {
497 $menu = Menu->new($self->{"login"} . "_menu.ini");
499 $menu = Menu->new("menu.ini");
501 $menu->generate_acl("", $additional_params);
503 my @additional_param_names = keys(%{$additional_params});
504 foreach my $key ($template->param()) {
505 if (grep(/^${key}$/, @additional_param_names)) {
506 $template->param($key => $additional_params->{$key});
508 $template->param($key => $self->{$key});
511 my $output = $template->output();
513 $main::lxdebug->leave_sub();
518 sub show_generic_error {
519 my ($self, $error, $title) = @_;
522 $add_params->{"title"} = $title if ($title);
523 $self->{"label_error"} = $error;
526 print($self->parse_html_template("generic/error", $add_params));
528 die("Error: $error\n");
531 sub show_generic_information {
532 my ($self, $error, $title) = @_;
535 $add_params->{"title"} = $title if ($title);
536 $self->{"label_information"} = $error;
539 print($self->parse_html_template("generic/information", $add_params));
541 die("Information: $error\n");
544 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
545 # changed it to accept an arbitrary number of triggers - sschoeling
547 $main::lxdebug->enter_sub();
550 my $myconfig = shift;
553 # set dateform for jsscript
555 $ifFormat = "%d.%m.%Y";
556 if ($myconfig->{dateformat} eq "dd.mm.yy") {
557 $ifFormat = "%d.%m.%Y";
559 if ($myconfig->{dateformat} eq "dd-mm-yy") {
560 $ifFormat = "%d-%m-%Y";
562 if ($myconfig->{dateformat} eq "dd/mm/yy") {
563 $ifFormat = "%d/%m/%Y";
565 if ($myconfig->{dateformat} eq "mm/dd/yy") {
566 $ifFormat = "%m/%d/%Y";
568 if ($myconfig->{dateformat} eq "mm-dd-yy") {
569 $ifFormat = "%m-%d-%Y";
571 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
572 $ifFormat = "%Y-%m-%d";
584 inputField : "| . (shift) . qq|",
585 ifFormat :"$ifFormat",
586 align : "| . (shift) . qq|",
587 button : "| . (shift) . qq|"
593 <script type="text/javascript">
594 <!--| . join("", @triggers) . qq|//-->
598 $main::lxdebug->leave_sub();
601 } #end sub write_trigger
604 $main::lxdebug->enter_sub();
606 my ($self, $msg) = @_;
608 if ($self->{callback}) {
610 ($script, $argv) = split(/\?/, $self->{callback});
611 exec("perl", "$script", $argv);
619 $main::lxdebug->leave_sub();
622 # sort of columns removed - empty sub
624 $main::lxdebug->enter_sub();
626 my ($self, @columns) = @_;
628 $main::lxdebug->leave_sub();
634 $main::lxdebug->enter_sub(2);
636 my ($self, $myconfig, $amount, $places, $dash) = @_;
638 #Workaround for $format_amount calls without $places
639 if (!defined $places) {
640 (my $dec) = ($amount =~ /\.(\d+)/);
641 $places = length $dec;
644 if ($places =~ /\d/) {
645 $amount = $self->round_amount($amount, $places);
648 # is the amount negative
649 my $negative = ($amount < 0);
653 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
655 my ($whole, $dec) = split /\./, "$amount";
657 $amount = join '', reverse split //, $whole;
658 $fillup = "0" x ($places - length($dec));
660 if ($myconfig->{numberformat} eq '1,000.00') {
661 $amount =~ s/\d{3,}?/$&,/g;
663 $amount = join '', reverse split //, $amount;
664 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
667 if ($myconfig->{numberformat} eq '1.000,00') {
668 $amount =~ s/\d{3,}?/$&./g;
670 $amount = join '', reverse split //, $amount;
671 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
674 if ($myconfig->{numberformat} eq '1000,00') {
676 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
680 $amount = ($negative) ? "($amount)" : "$amount";
681 } elsif ($dash =~ /DRCR/) {
682 $amount = ($negative) ? "$amount DR" : "$amount CR";
684 $amount = ($negative) ? "-$amount" : "$amount";
688 if ($dash eq "0" && $places) {
689 if ($myconfig->{numberformat} eq '1.000,00') {
690 $amount = "0" . "," . "0" x $places;
692 $amount = "0" . "." . "0" x $places;
695 $amount = ($dash ne "") ? "$dash" : "0";
699 $main::lxdebug->leave_sub(2);
705 $main::lxdebug->enter_sub(2);
707 my ($self, $myconfig, $amount) = @_;
709 if ($myconfig->{in_numberformat} == 1) {
710 # Extra input number format 1000.00 or 1000,00
712 $amount = scalar reverse $amount;
713 $amount =~ s/\./DOT/;
715 $amount =~ s/DOT/\./;
716 $amount = scalar reverse $amount;
717 $main::lxdebug->leave_sub(2);
718 return ($amount * 1);
721 if ( ($myconfig->{numberformat} eq '1.000,00')
722 || ($myconfig->{numberformat} eq '1000,00')) {
727 if ($myconfig->{numberformat} eq "1'000.00") {
733 $main::lxdebug->leave_sub(2);
735 return ($amount * 1);
739 $main::lxdebug->enter_sub(2);
741 my ($self, $amount, $places) = @_;
744 # Rounding like "Kaufmannsrunden"
745 # Descr. http://de.wikipedia.org/wiki/Rundung
747 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
750 $amount = $amount * (10**($places));
751 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
753 $main::lxdebug->leave_sub(2);
755 return $round_amount;
760 $main::lxdebug->enter_sub();
762 my ($self, $myconfig, $userspath) = @_;
765 $self->{"cwd"} = getcwd();
766 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
768 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
769 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
770 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
771 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
772 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
773 } elsif (($self->{"format"} =~ /html/i) ||
774 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
775 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
778 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
779 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
781 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
782 qw(email tel fax name signature company address businessnumber));
784 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
786 # OUT is used for the media, screen, printer, email
787 # for postscript we store a copy in a temporary file
789 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
790 if ($template->uses_temp_file() || $self->{media} eq 'email') {
792 $self->{OUT} = ">$self->{tmpfile}";
796 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
798 open(OUT, ">-") or $self->error("STDOUT : $!");
802 if (!$template->parse(*OUT)) {
804 $self->error("$self->{IN} : " . $template->get_error());
810 #print(STDERR Dumper($self));
812 if ($template->uses_temp_file() || $self->{media} eq 'email') {
814 if ($self->{media} eq 'email') {
818 my $mail = new Mailer;
820 map { $mail->{$_} = $self->{$_} }
821 qw(cc bcc subject message version format charset);
822 $mail->{to} = qq|$self->{email}|;
823 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
824 $mail->{fileid} = "$fileid.";
826 # if we send html or plain text inline
827 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
828 $mail->{contenttype} = "text/html";
830 $mail->{message} =~ s/\r\n/<br>\n/g;
831 $myconfig->{signature} =~ s/\\n/<br>\n/g;
832 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
834 open(IN, $self->{tmpfile})
835 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
837 $mail->{message} .= $_;
844 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
846 $myconfig->{signature} =~ s/\\n/\r\n/g;
847 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
851 my $err = $mail->send($out);
852 $self->error($self->cleanup . "$err") if ($err);
858 my $numbytes = (-s $self->{tmpfile});
859 open(IN, $self->{tmpfile})
860 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
862 $self->{copies} = 1 unless $self->{media} eq 'printer';
864 chdir("$self->{cwd}");
865 #print(STDERR "Kopien $self->{copies}\n");
866 #print(STDERR "OUT $self->{OUT}\n");
867 for my $i (1 .. $self->{copies}) {
869 open(OUT, $self->{OUT})
870 or $self->error($self->cleanup . "$self->{OUT} : $!");
874 print qq|Content-Type: | . $template->get_mime_type() . qq|
875 Content-Disposition: attachment; filename="$self->{tmpfile}"
876 Content-Length: $numbytes
880 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
900 chdir("$self->{cwd}");
901 $main::lxdebug->leave_sub();
905 $main::lxdebug->enter_sub();
909 chdir("$self->{tmpdir}");
912 if (-f "$self->{tmpfile}.err") {
913 open(FH, "$self->{tmpfile}.err");
918 if ($self->{tmpfile}) {
919 $self->{tmpfile} =~ s|.*/||g;
921 $self->{tmpfile} =~ s/\.\w+$//g;
922 my $tmpfile = $self->{tmpfile};
923 unlink(<$tmpfile.*>);
926 chdir("$self->{cwd}");
928 $main::lxdebug->leave_sub();
934 $main::lxdebug->enter_sub();
936 my ($self, $date, $myconfig) = @_;
938 if ($date && $date =~ /\D/) {
940 if ($myconfig->{dateformat} =~ /^yy/) {
941 ($yy, $mm, $dd) = split /\D/, $date;
943 if ($myconfig->{dateformat} =~ /^mm/) {
944 ($mm, $dd, $yy) = split /\D/, $date;
946 if ($myconfig->{dateformat} =~ /^dd/) {
947 ($dd, $mm, $yy) = split /\D/, $date;
952 $yy = ($yy < 70) ? $yy + 2000 : $yy;
953 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
955 $dd = "0$dd" if ($dd < 10);
956 $mm = "0$mm" if ($mm < 10);
961 $main::lxdebug->leave_sub();
966 # Database routines used throughout
969 $main::lxdebug->enter_sub();
971 my ($self, $myconfig) = @_;
973 # connect to database
975 DBI->connect($myconfig->{dbconnect},
976 $myconfig->{dbuser}, $myconfig->{dbpasswd})
980 if ($myconfig->{dboptions}) {
981 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
984 $main::lxdebug->leave_sub();
989 sub dbconnect_noauto {
990 $main::lxdebug->enter_sub();
992 my ($self, $myconfig) = @_;
994 # connect to database
996 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
997 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1001 if ($myconfig->{dboptions}) {
1002 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1005 $main::lxdebug->leave_sub();
1010 sub update_balance {
1011 $main::lxdebug->enter_sub();
1013 my ($self, $dbh, $table, $field, $where, $value) = @_;
1015 # if we have a value, go do it
1018 # retrieve balance from table
1019 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1020 my $sth = $dbh->prepare($query);
1022 $sth->execute || $self->dberror($query);
1023 my ($balance) = $sth->fetchrow_array;
1029 $query = "UPDATE $table SET $field = $balance WHERE $where";
1030 $dbh->do($query) || $self->dberror($query);
1032 $main::lxdebug->leave_sub();
1035 sub update_exchangerate {
1036 $main::lxdebug->enter_sub();
1038 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1040 # some sanity check for currency
1042 $main::lxdebug->leave_sub();
1046 my $query = qq|SELECT e.curr FROM exchangerate e
1047 WHERE e.curr = '$curr'
1048 AND e.transdate = '$transdate'
1050 my $sth = $dbh->prepare($query);
1051 $sth->execute || $self->dberror($query);
1054 if ($buy != 0 && $sell != 0) {
1055 $set = "buy = $buy, sell = $sell";
1056 } elsif ($buy != 0) {
1057 $set = "buy = $buy";
1058 } elsif ($sell != 0) {
1059 $set = "sell = $sell";
1062 if ($sth->fetchrow_array) {
1063 $query = qq|UPDATE exchangerate
1065 WHERE curr = '$curr'
1066 AND transdate = '$transdate'|;
1068 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1069 VALUES ('$curr', $buy, $sell, '$transdate')|;
1072 $dbh->do($query) || $self->dberror($query);
1074 $main::lxdebug->leave_sub();
1077 sub save_exchangerate {
1078 $main::lxdebug->enter_sub();
1080 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1082 my $dbh = $self->dbconnect($myconfig);
1084 my ($buy, $sell) = (0, 0);
1085 $buy = $rate if $fld eq 'buy';
1086 $sell = $rate if $fld eq 'sell';
1088 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1092 $main::lxdebug->leave_sub();
1095 sub get_exchangerate {
1096 $main::lxdebug->enter_sub();
1098 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1100 unless ($transdate) {
1101 $main::lxdebug->leave_sub();
1105 my $query = qq|SELECT e.$fld FROM exchangerate e
1106 WHERE e.curr = '$curr'
1107 AND e.transdate = '$transdate'|;
1108 my $sth = $dbh->prepare($query);
1109 $sth->execute || $self->dberror($query);
1111 my ($exchangerate) = $sth->fetchrow_array;
1114 if ($exchangerate == 0) {
1118 $main::lxdebug->leave_sub();
1120 return $exchangerate;
1123 sub set_payment_options {
1124 $main::lxdebug->enter_sub();
1126 my ($self, $myconfig, $transdate) = @_;
1128 if ($self->{payment_id}) {
1130 my $dbh = $self->dbconnect($myconfig);
1133 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1134 WHERE p.id = $self->{payment_id}|;
1135 my $sth = $dbh->prepare($query);
1136 $sth->execute || $self->dberror($query);
1138 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1141 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1143 my $sth = $dbh->prepare($query);
1144 $sth->execute || $self->dberror($query);
1145 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1148 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1150 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1151 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1152 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1157 $main::lxdebug->leave_sub();
1161 sub check_exchangerate {
1162 $main::lxdebug->enter_sub();
1164 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1166 unless ($transdate) {
1167 $main::lxdebug->leave_sub();
1171 my $dbh = $self->dbconnect($myconfig);
1173 my $query = qq|SELECT e.$fld FROM exchangerate e
1174 WHERE e.curr = '$currency'
1175 AND e.transdate = '$transdate'|;
1176 my $sth = $dbh->prepare($query);
1177 $sth->execute || $self->dberror($query);
1179 my ($exchangerate) = $sth->fetchrow_array;
1183 $main::lxdebug->leave_sub();
1185 return $exchangerate;
1188 sub get_template_language {
1189 $main::lxdebug->enter_sub();
1191 my ($self, $myconfig) = @_;
1193 my $template_code = "";
1195 if ($self->{language_id}) {
1197 my $dbh = $self->dbconnect($myconfig);
1200 my $query = qq|SELECT l.template_code FROM language l
1201 WHERE l.id = $self->{language_id}|;
1202 my $sth = $dbh->prepare($query);
1203 $sth->execute || $self->dberror($query);
1205 ($template_code) = $sth->fetchrow_array;
1210 $main::lxdebug->leave_sub();
1212 return $template_code;
1215 sub get_printer_code {
1216 $main::lxdebug->enter_sub();
1218 my ($self, $myconfig) = @_;
1220 my $template_code = "";
1222 if ($self->{printer_id}) {
1224 my $dbh = $self->dbconnect($myconfig);
1227 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1228 WHERE p.id = $self->{printer_id}|;
1229 my $sth = $dbh->prepare($query);
1230 $sth->execute || $self->dberror($query);
1232 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1237 $main::lxdebug->leave_sub();
1239 return $template_code;
1243 $main::lxdebug->enter_sub();
1245 my ($self, $myconfig) = @_;
1247 my $template_code = "";
1249 if ($self->{shipto_id}) {
1251 my $dbh = $self->dbconnect($myconfig);
1254 my $query = qq|SELECT s.* FROM shipto s
1255 WHERE s.id = $self->{shipto_id}|;
1256 my $sth = $dbh->prepare($query);
1257 $sth->execute || $self->dberror($query);
1258 $ref = $sth->fetchrow_hashref(NAME_lc);
1259 map { $form->{$_} = $ref->{$_} } keys %$ref;
1264 $main::lxdebug->leave_sub();
1269 $main::lxdebug->enter_sub();
1271 my ($self, $dbh, $id, $module) = @_;
1275 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1277 if ($self->{"shipto$item"}) {
1278 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1280 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1284 if ($self->{shipto_id}) {
1285 my $query = qq| UPDATE shipto set
1286 shiptoname = '$self->{shiptoname}',
1287 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1288 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1289 shiptostreet = '$self->{shiptostreet}',
1290 shiptozipcode = '$self->{shiptozipcode}',
1291 shiptocity = '$self->{shiptocity}',
1292 shiptocountry = '$self->{shiptocountry}',
1293 shiptocontact = '$self->{shiptocontact}',
1294 shiptophone = '$self->{shiptophone}',
1295 shiptofax = '$self->{shiptofax}',
1296 shiptoemail = '$self->{shiptoemail}'
1297 WHERE id = $self->{shipto_id}|;
1298 $dbh->do($query) || $self->dberror($query);
1301 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1302 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1303 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1304 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1305 '$self->{shiptozipcode}', '$self->{shiptocity}',
1306 '$self->{shiptocountry}', '$self->{shiptocontact}',
1307 '$self->{shiptophone}', '$self->{shiptofax}',
1308 '$self->{shiptoemail}', '$module')|;
1309 $dbh->do($query) || $self->dberror($query);
1313 $main::lxdebug->leave_sub();
1317 $main::lxdebug->enter_sub();
1319 my ($self, $dbh) = @_;
1321 my $query = qq|SELECT e.id, e.name FROM employee e
1322 WHERE e.login = '$self->{login}'|;
1323 my $sth = $dbh->prepare($query);
1324 $sth->execute || $self->dberror($query);
1326 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1327 $self->{employee_id} *= 1;
1331 $main::lxdebug->leave_sub();
1334 # get other contact for transaction and form - html/tex
1336 $main::lxdebug->enter_sub();
1338 my ($self, $dbh, $id) = @_;
1340 my $query = qq|SELECT c.*
1343 $sth = $dbh->prepare($query);
1344 $sth->execute || $self->dberror($query);
1346 $ref = $sth->fetchrow_hashref(NAME_lc);
1348 push @{ $self->{$_} }, $ref;
1351 $main::lxdebug->leave_sub();
1354 # get contacts for id, if no contact return {"","","","",""}
1356 $main::lxdebug->enter_sub();
1358 my ($self, $dbh, $id) = @_;
1360 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1362 WHERE cp_cv_id=$id|;
1363 my $sth = $dbh->prepare($query);
1364 $sth->execute || $self->dberror($query);
1367 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1368 push @{ $self->{all_contacts} }, $ref;
1373 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1376 $main::lxdebug->leave_sub();
1379 # this sub gets the id and name from $table
1381 $main::lxdebug->enter_sub();
1383 my ($self, $myconfig, $table) = @_;
1385 # connect to database
1386 my $dbh = $self->dbconnect($myconfig);
1388 my $name = $self->like(lc $self->{$table});
1389 my $customernumber = $self->like(lc $self->{customernumber});
1391 if ($self->{customernumber} ne "") {
1392 $query = qq~SELECT c.id, c.name,
1393 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1395 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1398 $query = qq~SELECT c.id, c.name,
1399 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1401 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1405 if ($self->{openinvoices}) {
1406 $query = qq~SELECT DISTINCT c.id, c.name,
1407 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1408 FROM $self->{arap} a
1409 JOIN $table c ON (a.${table}_id = c.id)
1410 WHERE NOT a.amount = a.paid
1411 AND lower(c.name) LIKE '$name'
1414 my $sth = $dbh->prepare($query);
1416 $sth->execute || $self->dberror($query);
1419 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1420 push(@{ $self->{name_list} }, $ref);
1426 $main::lxdebug->leave_sub();
1431 # the selection sub is used in the AR, AP, IS, IR and OE module
1434 $main::lxdebug->enter_sub();
1436 my ($self, $myconfig, $table, $module) = @_;
1439 my $dbh = $self->dbconnect($myconfig);
1441 my $query = qq|SELECT count(*) FROM $table|;
1442 my $sth = $dbh->prepare($query);
1443 $sth->execute || $self->dberror($query);
1444 my ($count) = $sth->fetchrow_array;
1447 # build selection list
1448 if ($count < $myconfig->{vclimit}) {
1449 $query = qq|SELECT id, name
1450 FROM $table WHERE not obsolete
1452 $sth = $dbh->prepare($query);
1453 $sth->execute || $self->dberror($query);
1455 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1456 push @{ $self->{"all_$table"} }, $ref;
1464 $self->get_employee($dbh);
1466 # setup sales contacts
1467 $query = qq|SELECT e.id, e.name
1470 AND NOT e.id = $self->{employee_id}|;
1471 $sth = $dbh->prepare($query);
1472 $sth->execute || $self->dberror($query);
1474 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1475 push @{ $self->{all_employees} }, $ref;
1480 push @{ $self->{all_employees} },
1481 { id => $self->{employee_id},
1482 name => $self->{employee} };
1484 # sort the whole thing
1485 @{ $self->{all_employees} } =
1486 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1488 if ($module eq 'AR') {
1490 # prepare query for departments
1491 $query = qq|SELECT d.id, d.description
1497 $query = qq|SELECT d.id, d.description
1502 $sth = $dbh->prepare($query);
1503 $sth->execute || $self->dberror($query);
1505 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1506 push @{ $self->{all_departments} }, $ref;
1511 $query = qq|SELECT id, description
1514 $sth = $dbh->prepare($query);
1515 $sth->execute || $form->dberror($query);
1517 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1518 push @{ $self->{languages} }, $ref;
1523 $query = qq|SELECT printer_description, id
1526 $sth = $dbh->prepare($query);
1527 $sth->execute || $form->dberror($query);
1529 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1530 push @{ $self->{printers} }, $ref;
1536 $query = qq|SELECT id, description
1539 $sth = $dbh->prepare($query);
1540 $sth->execute || $form->dberror($query);
1542 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1543 push @{ $self->{payment_terms} }, $ref;
1547 $main::lxdebug->leave_sub();
1551 sub language_payment {
1552 $main::lxdebug->enter_sub();
1554 my ($self, $myconfig) = @_;
1555 undef $self->{languages};
1556 undef $self->{payment_terms};
1557 undef $self->{printers};
1560 my $dbh = $self->dbconnect($myconfig);
1562 my $query = qq|SELECT id, description
1565 my $sth = $dbh->prepare($query);
1566 $sth->execute || $form->dberror($query);
1568 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1569 push @{ $self->{languages} }, $ref;
1574 $query = qq|SELECT printer_description, id
1577 $sth = $dbh->prepare($query);
1578 $sth->execute || $form->dberror($query);
1580 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1581 push @{ $self->{printers} }, $ref;
1586 $query = qq|SELECT id, description
1589 $sth = $dbh->prepare($query);
1590 $sth->execute || $form->dberror($query);
1592 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1593 push @{ $self->{payment_terms} }, $ref;
1597 # get buchungsgruppen
1598 $query = qq|SELECT id, description
1599 FROM buchungsgruppen|;
1600 $sth = $dbh->prepare($query);
1601 $sth->execute || $form->dberror($query);
1603 $self->{BUCHUNGSGRUPPEN} = [];
1604 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1605 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1610 $query = qq|SELECT id, adr_description, adr_code
1612 $sth = $dbh->prepare($query);
1613 $sth->execute || $form->dberror($query);
1617 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1618 push @{ $self->{ADR} }, $ref;
1622 $main::lxdebug->leave_sub();
1625 # this is only used for reports
1626 sub all_departments {
1627 $main::lxdebug->enter_sub();
1629 my ($self, $myconfig, $table) = @_;
1631 my $dbh = $self->dbconnect($myconfig);
1632 my $where = "1 = 1";
1634 if (defined $table) {
1635 if ($table eq 'customer') {
1636 $where = " d.role = 'P'";
1640 my $query = qq|SELECT d.id, d.description
1644 my $sth = $dbh->prepare($query);
1645 $sth->execute || $self->dberror($query);
1647 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1648 push @{ $self->{all_departments} }, $ref;
1654 $main::lxdebug->leave_sub();
1658 $main::lxdebug->enter_sub();
1660 my ($self, $module, $myconfig, $table) = @_;
1662 $self->all_vc($myconfig, $table, $module);
1664 # get last customers or vendors
1667 my $dbh = $self->dbconnect($myconfig);
1671 # now get the account numbers
1672 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1674 WHERE c.link LIKE '%$module%'
1677 $sth = $dbh->prepare($query);
1678 $sth->execute || $self->dberror($query);
1680 $self->{accounts} = "";
1681 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1683 foreach my $key (split /:/, $ref->{link}) {
1684 if ($key =~ /$module/) {
1686 # cross reference for keys
1687 $xkeyref{ $ref->{accno} } = $key;
1689 push @{ $self->{"${module}_links"}{$key} },
1690 { accno => $ref->{accno},
1691 description => $ref->{description},
1692 taxkey => $ref->{taxkey_id} };
1694 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1700 # get taxkeys and description
1701 $query = qq|SELECT taxkey, taxdescription
1703 $sth = $dbh->prepare($query);
1704 $sth->execute || $self->dberror($query);
1706 $ref = $sth->fetchrow_hashref(NAME_lc);
1708 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1709 push @{ $self->{TAXKEY} }, $ref;
1716 $query = qq|SELECT id, description
1718 $sth = $dbh->prepare($query);
1719 $sth->execute || $form->dberror($query);
1722 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1723 push @{ $self->{TAXZONE} }, $ref;
1727 if (($module eq "AP") || ($module eq "AR")) {
1729 # get tax rates and description
1730 $query = qq| SELECT * FROM tax t|;
1731 $sth = $dbh->prepare($query);
1732 $sth->execute || $self->dberror($query);
1734 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1735 push @{ $self->{TAX} }, $ref;
1741 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1743 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1744 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1745 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1746 c.name AS $table, a.department_id, d.description AS department,
1747 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1748 a.employee_id, e.name AS employee, a.gldate, a.type
1750 JOIN $table c ON (a.${table}_id = c.id)
1751 LEFT JOIN employee e ON (e.id = a.employee_id)
1752 LEFT JOIN department d ON (d.id = a.department_id)
1753 WHERE a.id = $self->{id}|;
1754 $sth = $dbh->prepare($query);
1755 $sth->execute || $self->dberror($query);
1757 $ref = $sth->fetchrow_hashref(NAME_lc);
1758 foreach $key (keys %$ref) {
1759 $self->{$key} = $ref->{$key};
1763 # get amounts from individual entries
1764 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1765 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1767 JOIN chart c ON (c.id = a.chart_id)
1768 LEFT JOIN project p ON (p.id = a.project_id)
1769 LEFT Join tax t ON (a.taxkey = t.taxkey)
1770 WHERE a.trans_id = $self->{id}
1771 AND a.fx_transaction = '0'
1772 ORDER BY a.oid,a.transdate|;
1773 $sth = $dbh->prepare($query);
1774 $sth->execute || $self->dberror($query);
1776 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1778 # get exchangerate for currency
1779 $self->{exchangerate} =
1780 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1784 # store amounts in {acc_trans}{$key} for multiple accounts
1785 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1786 $ref->{exchangerate} =
1787 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1789 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1792 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1793 $ref->{amount} *= -1;
1795 $ref->{index} = $index;
1797 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1801 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1802 (SELECT c.accno FROM chart c
1803 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1804 (SELECT c.accno FROM chart c
1805 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1807 $sth = $dbh->prepare($query);
1808 $sth->execute || $self->dberror($query);
1810 $ref = $sth->fetchrow_hashref(NAME_lc);
1811 map { $self->{$_} = $ref->{$_} } keys %$ref;
1817 $query = qq|SELECT current_date AS transdate,
1818 d.curr AS currencies, d.closedto, d.revtrans,
1819 (SELECT c.accno FROM chart c
1820 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1821 (SELECT c.accno FROM chart c
1822 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1824 $sth = $dbh->prepare($query);
1825 $sth->execute || $self->dberror($query);
1827 $ref = $sth->fetchrow_hashref(NAME_lc);
1828 map { $self->{$_} = $ref->{$_} } keys %$ref;
1831 if ($self->{"$self->{vc}_id"}) {
1833 # only setup currency
1834 ($self->{currency}) = split /:/, $self->{currencies};
1838 $self->lastname_used($dbh, $myconfig, $table, $module);
1840 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1842 # get exchangerate for currency
1843 $self->{exchangerate} =
1844 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1853 $main::lxdebug->leave_sub();
1857 $main::lxdebug->enter_sub();
1859 my ($self, $dbh, $myconfig, $table, $module) = @_;
1861 my $arap = ($table eq 'customer') ? "ar" : "ap";
1862 my $where = "1 = 1";
1864 if ($self->{type} =~ /_order/) {
1866 $where = "quotation = '0'";
1868 if ($self->{type} =~ /_quotation/) {
1870 $where = "quotation = '1'";
1873 my $query = qq|SELECT MAX(id) FROM $arap
1875 AND ${table}_id > 0|;
1876 my $sth = $dbh->prepare($query);
1877 $sth->execute || $self->dberror($query);
1879 my ($trans_id) = $sth->fetchrow_array;
1883 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1884 current_date + ct.terms AS duedate, a.department_id,
1885 d.description AS department
1887 JOIN $table ct ON (a.${table}_id = ct.id)
1888 LEFT JOIN department d ON (a.department_id = d.id)
1889 WHERE a.id = $trans_id|;
1890 $sth = $dbh->prepare($query);
1891 $sth->execute || $self->dberror($query);
1893 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1894 $self->{duedate}, $self->{department_id}, $self->{department})
1895 = $sth->fetchrow_array;
1898 $main::lxdebug->leave_sub();
1902 $main::lxdebug->enter_sub();
1904 my ($self, $myconfig, $thisdate, $days) = @_;
1906 my $dbh = $self->dbconnect($myconfig);
1911 my $dateformat = $myconfig->{dateformat};
1912 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1914 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1916 $sth = $dbh->prepare($query);
1917 $sth->execute || $self->dberror($query);
1919 $query = qq|SELECT current_date AS thisdate
1921 $sth = $dbh->prepare($query);
1922 $sth->execute || $self->dberror($query);
1925 ($thisdate) = $sth->fetchrow_array;
1930 $main::lxdebug->leave_sub();
1936 $main::lxdebug->enter_sub();
1938 my ($self, $string) = @_;
1940 if ($string !~ /%/) {
1941 $string = "%$string%";
1944 $string =~ s/\'/\'\'/g;
1946 $main::lxdebug->leave_sub();
1952 $main::lxdebug->enter_sub();
1954 my ($self, $flds, $new, $count, $numrows) = @_;
1958 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1964 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1966 $j = $item->{ndx} - 1;
1967 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1971 for $i ($count + 1 .. $numrows) {
1972 map { delete $self->{"${_}_$i"} } @{$flds};
1975 $main::lxdebug->leave_sub();
1979 $main::lxdebug->enter_sub();
1981 my ($self, $myconfig) = @_;
1985 my $dbh = $self->dbconnect_noauto($myconfig);
1987 my $query = qq|DELETE FROM status
1988 WHERE formname = '$self->{formname}'
1990 my $sth = $dbh->prepare($query) || $self->dberror($query);
1992 if ($self->{formname} =~ /(check|receipt)/) {
1993 for $i (1 .. $self->{rowcount}) {
1994 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1998 $sth->execute($self->{id}) || $self->dberror($query);
2002 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2003 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2005 my %queued = split / /, $self->{queued};
2007 if ($self->{formname} =~ /(check|receipt)/) {
2009 # this is a check or receipt, add one entry for each lineitem
2010 my ($accno) = split /--/, $self->{account};
2011 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2012 chart_id) VALUES (?, '$printed',
2013 '$queued{$self->{formname}}', '$self->{prinform}',
2014 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2015 $sth = $dbh->prepare($query) || $self->dberror($query);
2017 for $i (1 .. $self->{rowcount}) {
2018 if ($self->{"checked_$i"}) {
2019 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2024 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2025 spoolfile, formname)
2026 VALUES ($self->{id}, '$printed', '$emailed',
2027 '$queued{$self->{formname}}', '$self->{formname}')|;
2028 $dbh->do($query) || $self->dberror($query);
2034 $main::lxdebug->leave_sub();
2038 $main::lxdebug->enter_sub();
2040 my ($self, $dbh) = @_;
2042 my ($query, $printed, $emailed);
2044 my $formnames = $self->{printed};
2045 my $emailforms = $self->{emailed};
2047 my $query = qq|DELETE FROM status
2048 WHERE formname = '$self->{formname}'
2049 AND trans_id = $self->{id}|;
2050 $dbh->do($query) || $self->dberror($query);
2052 # this only applies to the forms
2053 # checks and receipts are posted when printed or queued
2055 if ($self->{queued}) {
2056 my %queued = split / /, $self->{queued};
2058 foreach my $formname (keys %queued) {
2059 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2060 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2062 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2063 spoolfile, formname)
2064 VALUES ($self->{id}, '$printed', '$emailed',
2065 '$queued{$formname}', '$formname')|;
2066 $dbh->do($query) || $self->dberror($query);
2068 $formnames =~ s/$self->{formname}//;
2069 $emailforms =~ s/$self->{formname}//;
2074 # save printed, emailed info
2075 $formnames =~ s/^ +//g;
2076 $emailforms =~ s/^ +//g;
2079 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2080 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2082 foreach my $formname (keys %status) {
2083 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2084 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2086 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2087 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2088 $dbh->do($query) || $self->dberror($query);
2091 $main::lxdebug->leave_sub();
2094 sub update_defaults {
2095 $main::lxdebug->enter_sub();
2097 my ($self, $myconfig, $fld) = @_;
2099 my $dbh = $self->dbconnect_noauto($myconfig);
2100 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2101 my $sth = $dbh->prepare($query);
2103 $sth->execute || $self->dberror($query);
2104 my ($var) = $sth->fetchrow_array;
2109 $query = qq|UPDATE defaults
2111 $dbh->do($query) || $self->dberror($query);
2116 $main::lxdebug->leave_sub();
2121 sub update_business {
2122 $main::lxdebug->enter_sub();
2124 my ($self, $myconfig, $business_id) = @_;
2126 my $dbh = $self->dbconnect_noauto($myconfig);
2128 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2129 my $sth = $dbh->prepare($query);
2131 $sth->execute || $self->dberror($query);
2132 my ($var) = $sth->fetchrow_array;
2137 $query = qq|UPDATE business
2138 SET customernumberinit = '$var' WHERE id=$business_id|;
2139 $dbh->do($query) || $self->dberror($query);
2144 $main::lxdebug->leave_sub();
2150 $main::lxdebug->enter_sub();
2152 my ($self, $myconfig, $salesman) = @_;
2154 my $dbh = $self->dbconnect($myconfig);
2156 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2157 my $sth = $dbh->prepare($query);
2158 $sth->execute || $self->dberror($query);
2161 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2162 push(@{ $self->{salesman_list} }, $ref);
2166 $main::lxdebug->leave_sub();
2171 sub get_partsgroup {
2172 $main::lxdebug->enter_sub();
2174 my ($self, $myconfig, $p) = @_;
2176 my $dbh = $self->dbconnect($myconfig);
2178 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2180 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2182 if ($p->{searchitems} eq 'part') {
2184 WHERE p.inventory_accno_id > 0|;
2186 if ($p->{searchitems} eq 'service') {
2188 WHERE p.inventory_accno_id IS NULL|;
2190 if ($p->{searchitems} eq 'assembly') {
2192 WHERE p.assembly = '1'|;
2194 if ($p->{searchitems} eq 'labor') {
2196 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2200 ORDER BY partsgroup|;
2203 $query = qq|SELECT id, partsgroup FROM partsgroup
2204 ORDER BY partsgroup|;
2207 if ($p->{language_code}) {
2208 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2209 t.description AS translation
2211 JOIN parts p ON (p.partsgroup_id = pg.id)
2212 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2213 ORDER BY translation|;
2216 my $sth = $dbh->prepare($query);
2217 $sth->execute || $self->dberror($query);
2219 $self->{all_partsgroup} = ();
2220 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2221 push @{ $self->{all_partsgroup} }, $ref;
2225 $main::lxdebug->leave_sub();
2228 sub get_pricegroup {
2229 $main::lxdebug->enter_sub();
2231 my ($self, $myconfig, $p) = @_;
2233 my $dbh = $self->dbconnect($myconfig);
2235 my $query = qq|SELECT p.id, p.pricegroup
2239 ORDER BY pricegroup|;
2242 $query = qq|SELECT id, pricegroup FROM pricegroup
2243 ORDER BY pricegroup|;
2246 my $sth = $dbh->prepare($query);
2247 $sth->execute || $self->dberror($query);
2249 $self->{all_pricegroup} = ();
2250 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2251 push @{ $self->{all_pricegroup} }, $ref;
2256 $main::lxdebug->leave_sub();
2260 my ($self, $dbh, $myconfig, $audittrail) = @_;
2262 # table, $reference, $formname, $action, $id, $transdate) = @_;
2269 $dbh = $self->dbconnect($myconfig);
2273 # if we have an id add audittrail, otherwise get a new timestamp
2275 if ($audittrail->{id}) {
2277 $query = qq|SELECT audittrail FROM defaults|;
2279 if ($dbh->selectrow_array($query)) {
2280 my ($null, $employee_id) = $self->get_employee($dbh);
2282 if ($self->{audittrail} && !$myconfig) {
2283 chop $self->{audittrail};
2285 my @a = split /\|/, $self->{audittrail};
2289 my @flds = qw(tablename reference formname action transdate);
2291 # put into hash and remove dups
2293 $key = "$a[2]$a[3]";
2295 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2299 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2300 formname, action, employee_id, transdate)
2301 VALUES ($audittrail->{id}, ?, ?,
2302 ?, ?, $employee_id, ?)|;
2303 my $sth = $dbh->prepare($query) || $self->dberror($query);
2307 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2311 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2313 $sth->execute || $self->dberror;
2318 if ($audittrail->{transdate}) {
2319 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2320 formname, action, employee_id, transdate) VALUES (
2321 $audittrail->{id}, '$audittrail->{tablename}', |
2322 . $dbh->quote($audittrail->{reference}) . qq|,
2323 '$audittrail->{formname}', '$audittrail->{action}',
2324 $employee_id, '$audittrail->{transdate}')|;
2326 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2327 formname, action, employee_id) VALUES ($audittrail->{id},
2328 '$audittrail->{tablename}', |
2329 . $dbh->quote($audittrail->{reference}) . qq|,
2330 '$audittrail->{formname}', '$audittrail->{action}',
2337 $query = qq|SELECT current_timestamp FROM defaults|;
2338 my ($timestamp) = $dbh->selectrow_array($query);
2341 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2344 $dbh->disconnect if $disconnect;
2353 $main::lxdebug->enter_sub();
2355 my ($type, $country, $NLS_file) = @_;
2359 if ($country && -d "locale/$country") {
2360 $self->{countrycode} = $country;
2361 eval { require "locale/$country/$NLS_file"; };
2364 $self->{NLS_file} = $NLS_file;
2366 push @{ $self->{LONG_MONTH} },
2367 ("January", "February", "March", "April",
2368 "May ", "June", "July", "August",
2369 "September", "October", "November", "December");
2370 push @{ $self->{SHORT_MONTH} },
2371 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2373 $main::lxdebug->leave_sub();
2379 my ($self, $text) = @_;
2381 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2385 $main::lxdebug->enter_sub();
2387 my ($self, $text) = @_;
2389 if (exists $self{subs}{$text}) {
2390 $text = $self{subs}{$text};
2392 if ($self->{countrycode} && $self->{NLS_file}) {
2394 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2398 $main::lxdebug->leave_sub();
2404 $main::lxdebug->enter_sub();
2406 my ($self, $myconfig, $date, $longformat) = @_;
2409 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2414 $spc = $myconfig->{dateformat};
2416 $spc = substr($spc, 1, 1);
2418 if ($date =~ /\D/) {
2419 if ($myconfig->{dateformat} =~ /^yy/) {
2420 ($yy, $mm, $dd) = split /\D/, $date;
2422 if ($myconfig->{dateformat} =~ /^mm/) {
2423 ($mm, $dd, $yy) = split /\D/, $date;
2425 if ($myconfig->{dateformat} =~ /^dd/) {
2426 ($dd, $mm, $yy) = split /\D/, $date;
2429 $date = substr($date, 2);
2430 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2435 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2436 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2438 if ($myconfig->{dateformat} =~ /^dd/) {
2439 if (defined $longformat && $longformat == 0) {
2441 $dd = "0$dd" if ($dd < 10);
2442 $mm = "0$mm" if ($mm < 10);
2443 $longdate = "$dd$spc$mm$spc$yy";
2446 $longdate .= ($spc eq '.') ? ". " : " ";
2447 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2449 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2451 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2452 # Lx-Office is mainly used in Germany or German speaking countries.
2453 if (defined $longformat && $longformat == 0) {
2455 $dd = "0$dd" if ($dd < 10);
2456 $mm = "0$mm" if ($mm < 10);
2457 $longdate = "$yy-$mm-$dd";
2459 $longdate = "$dd. ";
2460 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2463 if (defined $longformat && $longformat == 0) {
2465 $dd = "0$dd" if ($dd < 10);
2466 $mm = "0$mm" if ($mm < 10);
2467 $longdate = "$mm$spc$dd$spc$yy";
2469 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2475 $main::lxdebug->leave_sub();
2481 $main::lxdebug->enter_sub();
2483 my ($self, $myconfig, $date, $longformat) = @_;
2486 $main::lxdebug->leave_sub();
2491 $spc = $myconfig->{dateformat};
2493 $spc = substr($spc, 1, 1);
2495 if ($date =~ /\D/) {
2496 if ($myconfig->{dateformat} =~ /^yy/) {
2497 ($yy, $mm, $dd) = split /\D/, $date;
2498 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2499 ($mm, $dd, $yy) = split /\D/, $date;
2500 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2501 ($dd, $mm, $yy) = split /\D/, $date;
2504 $date = substr($date, 2);
2505 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2510 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2511 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2513 $main::lxdebug->leave_sub();
2514 return ($yy, $mm, $dd);