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;
494 if (-f $self->{"login"} . "_menu.ini") {
495 $menu = Menu->new($self->{"login"} . "_menu.ini");
497 $menu = Menu->new("menu.ini");
499 $menu->generate_acl("", $additional_params);
501 my @additional_param_names = keys(%{$additional_params});
502 foreach my $key ($template->param()) {
503 if (grep(/^${key}$/, @additional_param_names)) {
504 $template->param($key => $additional_params->{$key});
506 $template->param($key => $self->{$key});
509 my $output = $template->output();
511 $main::lxdebug->leave_sub();
516 sub show_generic_error {
517 my ($self, $error, $title) = @_;
520 $add_params->{"title"} = $title if ($title);
521 $self->{"label_error"} = $error;
524 print($self->parse_html_template("generic/error", $add_params));
526 die("Error: $error\n");
529 sub show_generic_information {
530 my ($self, $error, $title) = @_;
533 $add_params->{"title"} = $title if ($title);
534 $self->{"label_information"} = $error;
537 print($self->parse_html_template("generic/information", $add_params));
539 die("Information: $error\n");
542 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
543 # changed it to accept an arbitrary number of triggers - sschoeling
545 $main::lxdebug->enter_sub();
548 my $myconfig = shift;
551 # set dateform for jsscript
553 $ifFormat = "%d.%m.%Y";
554 if ($myconfig->{dateformat} eq "dd.mm.yy") {
555 $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 "mm/dd/yy") {
564 $ifFormat = "%m/%d/%Y";
566 if ($myconfig->{dateformat} eq "mm-dd-yy") {
567 $ifFormat = "%m-%d-%Y";
569 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
570 $ifFormat = "%Y-%m-%d";
582 inputField : "| . (shift) . qq|",
583 ifFormat :"$ifFormat",
584 align : "| . (shift) . qq|",
585 button : "| . (shift) . qq|"
591 <script type="text/javascript">
592 <!--| . join("", @triggers) . qq|//-->
596 $main::lxdebug->leave_sub();
599 } #end sub write_trigger
602 $main::lxdebug->enter_sub();
604 my ($self, $msg) = @_;
606 if ($self->{callback}) {
608 ($script, $argv) = split(/\?/, $self->{callback});
609 exec("perl", "$script", $argv);
617 $main::lxdebug->leave_sub();
620 # sort of columns removed - empty sub
622 $main::lxdebug->enter_sub();
624 my ($self, @columns) = @_;
626 $main::lxdebug->leave_sub();
632 $main::lxdebug->enter_sub(2);
634 my ($self, $myconfig, $amount, $places, $dash) = @_;
636 #Workaround for $format_amount calls without $places
637 if (!defined $places) {
638 (my $dec) = ($amount =~ /\.(\d+)/);
639 $places = length $dec;
642 if ($places =~ /\d/) {
643 $amount = $self->round_amount($amount, $places);
646 # is the amount negative
647 my $negative = ($amount < 0);
651 if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
653 my ($whole, $dec) = split /\./, "$amount";
655 $amount = join '', reverse split //, $whole;
656 $fillup = "0" x ($places - length($dec));
658 if ($myconfig->{numberformat} eq '1,000.00') {
659 $amount =~ s/\d{3,}?/$&,/g;
661 $amount = join '', reverse split //, $amount;
662 $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
665 if ($myconfig->{numberformat} eq '1.000,00') {
666 $amount =~ s/\d{3,}?/$&./g;
668 $amount = join '', reverse split //, $amount;
669 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
672 if ($myconfig->{numberformat} eq '1000,00') {
674 $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
678 $amount = ($negative) ? "($amount)" : "$amount";
679 } elsif ($dash =~ /DRCR/) {
680 $amount = ($negative) ? "$amount DR" : "$amount CR";
682 $amount = ($negative) ? "-$amount" : "$amount";
686 if ($dash eq "0" && $places) {
687 if ($myconfig->{numberformat} eq '1.000,00') {
688 $amount = "0" . "," . "0" x $places;
690 $amount = "0" . "." . "0" x $places;
693 $amount = ($dash ne "") ? "$dash" : "0";
697 $main::lxdebug->leave_sub(2);
703 $main::lxdebug->enter_sub(2);
705 my ($self, $myconfig, $amount) = @_;
707 if ($myconfig->{in_numberformat} == 1) {
708 # Extra input number format 1000.00 or 1000,00
710 $amount = scalar reverse $amount;
711 $amount =~ s/\./DOT/;
713 $amount =~ s/DOT/\./;
714 $amount = scalar reverse $amount;
715 $main::lxdebug->leave_sub(2);
716 return ($amount * 1);
719 if ( ($myconfig->{numberformat} eq '1.000,00')
720 || ($myconfig->{numberformat} eq '1000,00')) {
725 if ($myconfig->{numberformat} eq "1'000.00") {
731 $main::lxdebug->leave_sub(2);
733 return ($amount * 1);
737 $main::lxdebug->enter_sub(2);
739 my ($self, $amount, $places) = @_;
742 # Rounding like "Kaufmannsrunden"
743 # Descr. http://de.wikipedia.org/wiki/Rundung
745 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
748 $amount = $amount * (10**($places));
749 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
751 $main::lxdebug->leave_sub(2);
753 return $round_amount;
758 $main::lxdebug->enter_sub();
760 my ($self, $myconfig, $userspath) = @_;
763 $self->{"cwd"} = getcwd();
764 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
766 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
767 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
768 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
769 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
770 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
771 } elsif (($self->{"format"} =~ /html/i) ||
772 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
773 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
776 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
777 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
779 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
780 qw(email tel fax name signature company address businessnumber));
782 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
784 # OUT is used for the media, screen, printer, email
785 # for postscript we store a copy in a temporary file
787 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
788 if ($template->uses_temp_file() || $self->{media} eq 'email') {
790 $self->{OUT} = ">$self->{tmpfile}";
794 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
796 open(OUT, ">-") or $self->error("STDOUT : $!");
800 if (!$template->parse(*OUT)) {
802 $self->error("$self->{IN} : " . $template->get_error());
808 #print(STDERR Dumper($self));
810 if ($template->uses_temp_file() || $self->{media} eq 'email') {
812 if ($self->{media} eq 'email') {
816 my $mail = new Mailer;
818 map { $mail->{$_} = $self->{$_} }
819 qw(cc bcc subject message version format charset);
820 $mail->{to} = qq|$self->{email}|;
821 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
822 $mail->{fileid} = "$fileid.";
824 # if we send html or plain text inline
825 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
826 $mail->{contenttype} = "text/html";
828 $mail->{message} =~ s/\r\n/<br>\n/g;
829 $myconfig->{signature} =~ s/\\n/<br>\n/g;
830 $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
832 open(IN, $self->{tmpfile})
833 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
835 $mail->{message} .= $_;
842 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
844 $myconfig->{signature} =~ s/\\n/\r\n/g;
845 $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
849 my $err = $mail->send($out);
850 $self->error($self->cleanup . "$err") if ($err);
856 my $numbytes = (-s $self->{tmpfile});
857 open(IN, $self->{tmpfile})
858 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
860 $self->{copies} = 1 unless $self->{media} eq 'printer';
862 chdir("$self->{cwd}");
863 #print(STDERR "Kopien $self->{copies}\n");
864 #print(STDERR "OUT $self->{OUT}\n");
865 for my $i (1 .. $self->{copies}) {
867 open(OUT, $self->{OUT})
868 or $self->error($self->cleanup . "$self->{OUT} : $!");
872 print qq|Content-Type: | . $template->get_mime_type() . qq|
873 Content-Disposition: attachment; filename="$self->{tmpfile}"
874 Content-Length: $numbytes
878 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
898 chdir("$self->{cwd}");
899 $main::lxdebug->leave_sub();
903 $main::lxdebug->enter_sub();
907 chdir("$self->{tmpdir}");
910 if (-f "$self->{tmpfile}.err") {
911 open(FH, "$self->{tmpfile}.err");
916 if ($self->{tmpfile}) {
917 $self->{tmpfile} =~ s|.*/||g;
919 $self->{tmpfile} =~ s/\.\w+$//g;
920 my $tmpfile = $self->{tmpfile};
921 unlink(<$tmpfile.*>);
924 chdir("$self->{cwd}");
926 $main::lxdebug->leave_sub();
932 $main::lxdebug->enter_sub();
934 my ($self, $date, $myconfig) = @_;
936 if ($date && $date =~ /\D/) {
938 if ($myconfig->{dateformat} =~ /^yy/) {
939 ($yy, $mm, $dd) = split /\D/, $date;
941 if ($myconfig->{dateformat} =~ /^mm/) {
942 ($mm, $dd, $yy) = split /\D/, $date;
944 if ($myconfig->{dateformat} =~ /^dd/) {
945 ($dd, $mm, $yy) = split /\D/, $date;
950 $yy = ($yy < 70) ? $yy + 2000 : $yy;
951 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
953 $dd = "0$dd" if ($dd < 10);
954 $mm = "0$mm" if ($mm < 10);
959 $main::lxdebug->leave_sub();
964 # Database routines used throughout
967 $main::lxdebug->enter_sub();
969 my ($self, $myconfig) = @_;
971 # connect to database
973 DBI->connect($myconfig->{dbconnect},
974 $myconfig->{dbuser}, $myconfig->{dbpasswd})
978 if ($myconfig->{dboptions}) {
979 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
982 $main::lxdebug->leave_sub();
987 sub dbconnect_noauto {
988 $main::lxdebug->enter_sub();
990 my ($self, $myconfig) = @_;
992 # connect to database
994 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
995 $myconfig->{dbpasswd}, { AutoCommit => 0 })
999 if ($myconfig->{dboptions}) {
1000 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1003 $main::lxdebug->leave_sub();
1008 sub update_balance {
1009 $main::lxdebug->enter_sub();
1011 my ($self, $dbh, $table, $field, $where, $value) = @_;
1013 # if we have a value, go do it
1016 # retrieve balance from table
1017 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1018 my $sth = $dbh->prepare($query);
1020 $sth->execute || $self->dberror($query);
1021 my ($balance) = $sth->fetchrow_array;
1027 $query = "UPDATE $table SET $field = $balance WHERE $where";
1028 $dbh->do($query) || $self->dberror($query);
1030 $main::lxdebug->leave_sub();
1033 sub update_exchangerate {
1034 $main::lxdebug->enter_sub();
1036 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1038 # some sanity check for currency
1040 $main::lxdebug->leave_sub();
1044 my $query = qq|SELECT e.curr FROM exchangerate e
1045 WHERE e.curr = '$curr'
1046 AND e.transdate = '$transdate'
1048 my $sth = $dbh->prepare($query);
1049 $sth->execute || $self->dberror($query);
1052 if ($buy != 0 && $sell != 0) {
1053 $set = "buy = $buy, sell = $sell";
1054 } elsif ($buy != 0) {
1055 $set = "buy = $buy";
1056 } elsif ($sell != 0) {
1057 $set = "sell = $sell";
1060 if ($sth->fetchrow_array) {
1061 $query = qq|UPDATE exchangerate
1063 WHERE curr = '$curr'
1064 AND transdate = '$transdate'|;
1066 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1067 VALUES ('$curr', $buy, $sell, '$transdate')|;
1070 $dbh->do($query) || $self->dberror($query);
1072 $main::lxdebug->leave_sub();
1075 sub save_exchangerate {
1076 $main::lxdebug->enter_sub();
1078 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1080 my $dbh = $self->dbconnect($myconfig);
1082 my ($buy, $sell) = (0, 0);
1083 $buy = $rate if $fld eq 'buy';
1084 $sell = $rate if $fld eq 'sell';
1086 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1090 $main::lxdebug->leave_sub();
1093 sub get_exchangerate {
1094 $main::lxdebug->enter_sub();
1096 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1098 unless ($transdate) {
1099 $main::lxdebug->leave_sub();
1103 my $query = qq|SELECT e.$fld FROM exchangerate e
1104 WHERE e.curr = '$curr'
1105 AND e.transdate = '$transdate'|;
1106 my $sth = $dbh->prepare($query);
1107 $sth->execute || $self->dberror($query);
1109 my ($exchangerate) = $sth->fetchrow_array;
1112 if ($exchangerate == 0) {
1116 $main::lxdebug->leave_sub();
1118 return $exchangerate;
1121 sub set_payment_options {
1122 $main::lxdebug->enter_sub();
1124 my ($self, $myconfig, $transdate) = @_;
1126 if ($self->{payment_id}) {
1128 my $dbh = $self->dbconnect($myconfig);
1131 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1132 WHERE p.id = $self->{payment_id}|;
1133 my $sth = $dbh->prepare($query);
1134 $sth->execute || $self->dberror($query);
1136 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1139 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1141 my $sth = $dbh->prepare($query);
1142 $sth->execute || $self->dberror($query);
1143 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1146 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1148 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1149 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1150 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1155 $main::lxdebug->leave_sub();
1159 sub check_exchangerate {
1160 $main::lxdebug->enter_sub();
1162 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1164 unless ($transdate) {
1165 $main::lxdebug->leave_sub();
1169 my $dbh = $self->dbconnect($myconfig);
1171 my $query = qq|SELECT e.$fld FROM exchangerate e
1172 WHERE e.curr = '$currency'
1173 AND e.transdate = '$transdate'|;
1174 my $sth = $dbh->prepare($query);
1175 $sth->execute || $self->dberror($query);
1177 my ($exchangerate) = $sth->fetchrow_array;
1181 $main::lxdebug->leave_sub();
1183 return $exchangerate;
1186 sub get_template_language {
1187 $main::lxdebug->enter_sub();
1189 my ($self, $myconfig) = @_;
1191 my $template_code = "";
1193 if ($self->{language_id}) {
1195 my $dbh = $self->dbconnect($myconfig);
1198 my $query = qq|SELECT l.template_code FROM language l
1199 WHERE l.id = $self->{language_id}|;
1200 my $sth = $dbh->prepare($query);
1201 $sth->execute || $self->dberror($query);
1203 ($template_code) = $sth->fetchrow_array;
1208 $main::lxdebug->leave_sub();
1210 return $template_code;
1213 sub get_printer_code {
1214 $main::lxdebug->enter_sub();
1216 my ($self, $myconfig) = @_;
1218 my $template_code = "";
1220 if ($self->{printer_id}) {
1222 my $dbh = $self->dbconnect($myconfig);
1225 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1226 WHERE p.id = $self->{printer_id}|;
1227 my $sth = $dbh->prepare($query);
1228 $sth->execute || $self->dberror($query);
1230 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1235 $main::lxdebug->leave_sub();
1237 return $template_code;
1241 $main::lxdebug->enter_sub();
1243 my ($self, $myconfig) = @_;
1245 my $template_code = "";
1247 if ($self->{shipto_id}) {
1249 my $dbh = $self->dbconnect($myconfig);
1252 my $query = qq|SELECT s.* FROM shipto s
1253 WHERE s.id = $self->{shipto_id}|;
1254 my $sth = $dbh->prepare($query);
1255 $sth->execute || $self->dberror($query);
1256 $ref = $sth->fetchrow_hashref(NAME_lc);
1257 map { $form->{$_} = $ref->{$_} } keys %$ref;
1262 $main::lxdebug->leave_sub();
1267 $main::lxdebug->enter_sub();
1269 my ($self, $dbh, $id, $module) = @_;
1273 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1275 if ($self->{"shipto$item"}) {
1276 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1278 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1282 if ($self->{shipto_id}) {
1283 my $query = qq| UPDATE shipto set
1284 shiptoname = '$self->{shiptoname}',
1285 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1286 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1287 shiptostreet = '$self->{shiptostreet}',
1288 shiptozipcode = '$self->{shiptozipcode}',
1289 shiptocity = '$self->{shiptocity}',
1290 shiptocountry = '$self->{shiptocountry}',
1291 shiptocontact = '$self->{shiptocontact}',
1292 shiptophone = '$self->{shiptophone}',
1293 shiptofax = '$self->{shiptofax}',
1294 shiptoemail = '$self->{shiptoemail}'
1295 WHERE id = $self->{shipto_id}|;
1296 $dbh->do($query) || $self->dberror($query);
1299 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1300 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1301 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1302 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1303 '$self->{shiptozipcode}', '$self->{shiptocity}',
1304 '$self->{shiptocountry}', '$self->{shiptocontact}',
1305 '$self->{shiptophone}', '$self->{shiptofax}',
1306 '$self->{shiptoemail}', '$module')|;
1307 $dbh->do($query) || $self->dberror($query);
1311 $main::lxdebug->leave_sub();
1315 $main::lxdebug->enter_sub();
1317 my ($self, $dbh) = @_;
1319 my $query = qq|SELECT e.id, e.name FROM employee e
1320 WHERE e.login = '$self->{login}'|;
1321 my $sth = $dbh->prepare($query);
1322 $sth->execute || $self->dberror($query);
1324 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1325 $self->{employee_id} *= 1;
1329 $main::lxdebug->leave_sub();
1332 # get other contact for transaction and form - html/tex
1334 $main::lxdebug->enter_sub();
1336 my ($self, $dbh, $id) = @_;
1338 my $query = qq|SELECT c.*
1341 $sth = $dbh->prepare($query);
1342 $sth->execute || $self->dberror($query);
1344 $ref = $sth->fetchrow_hashref(NAME_lc);
1346 push @{ $self->{$_} }, $ref;
1349 $main::lxdebug->leave_sub();
1352 # get contacts for id, if no contact return {"","","","",""}
1354 $main::lxdebug->enter_sub();
1356 my ($self, $dbh, $id) = @_;
1358 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1360 WHERE cp_cv_id=$id|;
1361 my $sth = $dbh->prepare($query);
1362 $sth->execute || $self->dberror($query);
1365 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1366 push @{ $self->{all_contacts} }, $ref;
1371 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1374 $main::lxdebug->leave_sub();
1377 # this sub gets the id and name from $table
1379 $main::lxdebug->enter_sub();
1381 my ($self, $myconfig, $table) = @_;
1383 # connect to database
1384 my $dbh = $self->dbconnect($myconfig);
1386 my $name = $self->like(lc $self->{$table});
1387 my $customernumber = $self->like(lc $self->{customernumber});
1389 if ($self->{customernumber} ne "") {
1390 $query = qq~SELECT c.id, c.name,
1391 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1393 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1396 $query = qq~SELECT c.id, c.name,
1397 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1399 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1403 if ($self->{openinvoices}) {
1404 $query = qq~SELECT DISTINCT c.id, c.name,
1405 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1406 FROM $self->{arap} a
1407 JOIN $table c ON (a.${table}_id = c.id)
1408 WHERE NOT a.amount = a.paid
1409 AND lower(c.name) LIKE '$name'
1412 my $sth = $dbh->prepare($query);
1414 $sth->execute || $self->dberror($query);
1417 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1418 push(@{ $self->{name_list} }, $ref);
1424 $main::lxdebug->leave_sub();
1429 # the selection sub is used in the AR, AP, IS, IR and OE module
1432 $main::lxdebug->enter_sub();
1434 my ($self, $myconfig, $table, $module) = @_;
1437 my $dbh = $self->dbconnect($myconfig);
1439 my $query = qq|SELECT count(*) FROM $table|;
1440 my $sth = $dbh->prepare($query);
1441 $sth->execute || $self->dberror($query);
1442 my ($count) = $sth->fetchrow_array;
1445 # build selection list
1446 if ($count < $myconfig->{vclimit}) {
1447 $query = qq|SELECT id, name
1448 FROM $table WHERE not obsolete
1450 $sth = $dbh->prepare($query);
1451 $sth->execute || $self->dberror($query);
1453 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1454 push @{ $self->{"all_$table"} }, $ref;
1462 $self->get_employee($dbh);
1464 # setup sales contacts
1465 $query = qq|SELECT e.id, e.name
1468 AND NOT e.id = $self->{employee_id}|;
1469 $sth = $dbh->prepare($query);
1470 $sth->execute || $self->dberror($query);
1472 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1473 push @{ $self->{all_employees} }, $ref;
1478 push @{ $self->{all_employees} },
1479 { id => $self->{employee_id},
1480 name => $self->{employee} };
1482 # sort the whole thing
1483 @{ $self->{all_employees} } =
1484 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1486 if ($module eq 'AR') {
1488 # prepare query for departments
1489 $query = qq|SELECT d.id, d.description
1495 $query = qq|SELECT d.id, d.description
1500 $sth = $dbh->prepare($query);
1501 $sth->execute || $self->dberror($query);
1503 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1504 push @{ $self->{all_departments} }, $ref;
1509 $query = qq|SELECT id, description
1512 $sth = $dbh->prepare($query);
1513 $sth->execute || $form->dberror($query);
1515 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1516 push @{ $self->{languages} }, $ref;
1521 $query = qq|SELECT printer_description, id
1524 $sth = $dbh->prepare($query);
1525 $sth->execute || $form->dberror($query);
1527 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1528 push @{ $self->{printers} }, $ref;
1534 $query = qq|SELECT id, description
1537 $sth = $dbh->prepare($query);
1538 $sth->execute || $form->dberror($query);
1540 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1541 push @{ $self->{payment_terms} }, $ref;
1545 $main::lxdebug->leave_sub();
1549 sub language_payment {
1550 $main::lxdebug->enter_sub();
1552 my ($self, $myconfig) = @_;
1553 undef $self->{languages};
1554 undef $self->{payment_terms};
1555 undef $self->{printers};
1558 my $dbh = $self->dbconnect($myconfig);
1560 my $query = qq|SELECT id, description
1563 my $sth = $dbh->prepare($query);
1564 $sth->execute || $form->dberror($query);
1566 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1567 push @{ $self->{languages} }, $ref;
1572 $query = qq|SELECT printer_description, id
1575 $sth = $dbh->prepare($query);
1576 $sth->execute || $form->dberror($query);
1578 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1579 push @{ $self->{printers} }, $ref;
1584 $query = qq|SELECT id, description
1587 $sth = $dbh->prepare($query);
1588 $sth->execute || $form->dberror($query);
1590 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1591 push @{ $self->{payment_terms} }, $ref;
1595 # get buchungsgruppen
1596 $query = qq|SELECT id, description
1597 FROM buchungsgruppen|;
1598 $sth = $dbh->prepare($query);
1599 $sth->execute || $form->dberror($query);
1601 $self->{BUCHUNGSGRUPPEN} = [];
1602 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1603 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1608 $query = qq|SELECT id, adr_description, adr_code
1610 $sth = $dbh->prepare($query);
1611 $sth->execute || $form->dberror($query);
1615 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1616 push @{ $self->{ADR} }, $ref;
1620 $main::lxdebug->leave_sub();
1623 # this is only used for reports
1624 sub all_departments {
1625 $main::lxdebug->enter_sub();
1627 my ($self, $myconfig, $table) = @_;
1629 my $dbh = $self->dbconnect($myconfig);
1630 my $where = "1 = 1";
1632 if (defined $table) {
1633 if ($table eq 'customer') {
1634 $where = " d.role = 'P'";
1638 my $query = qq|SELECT d.id, d.description
1642 my $sth = $dbh->prepare($query);
1643 $sth->execute || $self->dberror($query);
1645 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1646 push @{ $self->{all_departments} }, $ref;
1652 $main::lxdebug->leave_sub();
1656 $main::lxdebug->enter_sub();
1658 my ($self, $module, $myconfig, $table) = @_;
1660 $self->all_vc($myconfig, $table, $module);
1662 # get last customers or vendors
1665 my $dbh = $self->dbconnect($myconfig);
1669 # now get the account numbers
1670 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
1672 WHERE c.link LIKE '%$module%'
1675 $sth = $dbh->prepare($query);
1676 $sth->execute || $self->dberror($query);
1678 $self->{accounts} = "";
1679 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1681 foreach my $key (split /:/, $ref->{link}) {
1682 if ($key =~ /$module/) {
1684 # cross reference for keys
1685 $xkeyref{ $ref->{accno} } = $key;
1687 push @{ $self->{"${module}_links"}{$key} },
1688 { accno => $ref->{accno},
1689 description => $ref->{description},
1690 taxkey => $ref->{taxkey_id} };
1692 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1698 # get taxkeys and description
1699 $query = qq|SELECT taxkey, taxdescription
1701 $sth = $dbh->prepare($query);
1702 $sth->execute || $self->dberror($query);
1704 $ref = $sth->fetchrow_hashref(NAME_lc);
1706 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1707 push @{ $self->{TAXKEY} }, $ref;
1714 $query = qq|SELECT id, description
1716 $sth = $dbh->prepare($query);
1717 $sth->execute || $form->dberror($query);
1720 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1721 push @{ $self->{TAXZONE} }, $ref;
1725 if (($module eq "AP") || ($module eq "AR")) {
1727 # get tax rates and description
1728 $query = qq| SELECT * FROM tax t|;
1729 $sth = $dbh->prepare($query);
1730 $sth->execute || $self->dberror($query);
1732 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1733 push @{ $self->{TAX} }, $ref;
1739 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1741 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1742 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1743 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1744 c.name AS $table, a.department_id, d.description AS department,
1745 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1746 a.employee_id, e.name AS employee, a.gldate, a.type
1748 JOIN $table c ON (a.${table}_id = c.id)
1749 LEFT JOIN employee e ON (e.id = a.employee_id)
1750 LEFT JOIN department d ON (d.id = a.department_id)
1751 WHERE a.id = $self->{id}|;
1752 $sth = $dbh->prepare($query);
1753 $sth->execute || $self->dberror($query);
1755 $ref = $sth->fetchrow_hashref(NAME_lc);
1756 foreach $key (keys %$ref) {
1757 $self->{$key} = $ref->{$key};
1761 # get amounts from individual entries
1762 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1763 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
1765 JOIN chart c ON (c.id = a.chart_id)
1766 LEFT JOIN project p ON (p.id = a.project_id)
1767 LEFT Join tax t ON (a.taxkey = t.taxkey)
1768 WHERE a.trans_id = $self->{id}
1769 AND a.fx_transaction = '0'
1770 ORDER BY a.oid,a.transdate|;
1771 $sth = $dbh->prepare($query);
1772 $sth->execute || $self->dberror($query);
1774 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1776 # get exchangerate for currency
1777 $self->{exchangerate} =
1778 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1782 # store amounts in {acc_trans}{$key} for multiple accounts
1783 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1784 $ref->{exchangerate} =
1785 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1787 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1790 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1791 $ref->{amount} *= -1;
1793 $ref->{index} = $index;
1795 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1799 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1800 (SELECT c.accno FROM chart c
1801 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1802 (SELECT c.accno FROM chart c
1803 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1805 $sth = $dbh->prepare($query);
1806 $sth->execute || $self->dberror($query);
1808 $ref = $sth->fetchrow_hashref(NAME_lc);
1809 map { $self->{$_} = $ref->{$_} } keys %$ref;
1815 $query = qq|SELECT current_date AS transdate,
1816 d.curr AS currencies, d.closedto, d.revtrans,
1817 (SELECT c.accno FROM chart c
1818 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1819 (SELECT c.accno FROM chart c
1820 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1822 $sth = $dbh->prepare($query);
1823 $sth->execute || $self->dberror($query);
1825 $ref = $sth->fetchrow_hashref(NAME_lc);
1826 map { $self->{$_} = $ref->{$_} } keys %$ref;
1829 if ($self->{"$self->{vc}_id"}) {
1831 # only setup currency
1832 ($self->{currency}) = split /:/, $self->{currencies};
1836 $self->lastname_used($dbh, $myconfig, $table, $module);
1838 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1840 # get exchangerate for currency
1841 $self->{exchangerate} =
1842 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1851 $main::lxdebug->leave_sub();
1855 $main::lxdebug->enter_sub();
1857 my ($self, $dbh, $myconfig, $table, $module) = @_;
1859 my $arap = ($table eq 'customer') ? "ar" : "ap";
1860 my $where = "1 = 1";
1862 if ($self->{type} =~ /_order/) {
1864 $where = "quotation = '0'";
1866 if ($self->{type} =~ /_quotation/) {
1868 $where = "quotation = '1'";
1871 my $query = qq|SELECT MAX(id) FROM $arap
1873 AND ${table}_id > 0|;
1874 my $sth = $dbh->prepare($query);
1875 $sth->execute || $self->dberror($query);
1877 my ($trans_id) = $sth->fetchrow_array;
1881 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1882 current_date + ct.terms AS duedate, a.department_id,
1883 d.description AS department
1885 JOIN $table ct ON (a.${table}_id = ct.id)
1886 LEFT JOIN department d ON (a.department_id = d.id)
1887 WHERE a.id = $trans_id|;
1888 $sth = $dbh->prepare($query);
1889 $sth->execute || $self->dberror($query);
1891 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1892 $self->{duedate}, $self->{department_id}, $self->{department})
1893 = $sth->fetchrow_array;
1896 $main::lxdebug->leave_sub();
1900 $main::lxdebug->enter_sub();
1902 my ($self, $myconfig, $thisdate, $days) = @_;
1904 my $dbh = $self->dbconnect($myconfig);
1909 my $dateformat = $myconfig->{dateformat};
1910 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1912 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1914 $sth = $dbh->prepare($query);
1915 $sth->execute || $self->dberror($query);
1917 $query = qq|SELECT current_date AS thisdate
1919 $sth = $dbh->prepare($query);
1920 $sth->execute || $self->dberror($query);
1923 ($thisdate) = $sth->fetchrow_array;
1928 $main::lxdebug->leave_sub();
1934 $main::lxdebug->enter_sub();
1936 my ($self, $string) = @_;
1938 if ($string !~ /%/) {
1939 $string = "%$string%";
1942 $string =~ s/\'/\'\'/g;
1944 $main::lxdebug->leave_sub();
1950 $main::lxdebug->enter_sub();
1952 my ($self, $flds, $new, $count, $numrows) = @_;
1956 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1962 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1964 $j = $item->{ndx} - 1;
1965 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1969 for $i ($count + 1 .. $numrows) {
1970 map { delete $self->{"${_}_$i"} } @{$flds};
1973 $main::lxdebug->leave_sub();
1977 $main::lxdebug->enter_sub();
1979 my ($self, $myconfig) = @_;
1983 my $dbh = $self->dbconnect_noauto($myconfig);
1985 my $query = qq|DELETE FROM status
1986 WHERE formname = '$self->{formname}'
1988 my $sth = $dbh->prepare($query) || $self->dberror($query);
1990 if ($self->{formname} =~ /(check|receipt)/) {
1991 for $i (1 .. $self->{rowcount}) {
1992 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
1996 $sth->execute($self->{id}) || $self->dberror($query);
2000 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2001 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2003 my %queued = split / /, $self->{queued};
2005 if ($self->{formname} =~ /(check|receipt)/) {
2007 # this is a check or receipt, add one entry for each lineitem
2008 my ($accno) = split /--/, $self->{account};
2009 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2010 chart_id) VALUES (?, '$printed',
2011 '$queued{$self->{formname}}', '$self->{prinform}',
2012 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2013 $sth = $dbh->prepare($query) || $self->dberror($query);
2015 for $i (1 .. $self->{rowcount}) {
2016 if ($self->{"checked_$i"}) {
2017 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2022 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2023 spoolfile, formname)
2024 VALUES ($self->{id}, '$printed', '$emailed',
2025 '$queued{$self->{formname}}', '$self->{formname}')|;
2026 $dbh->do($query) || $self->dberror($query);
2032 $main::lxdebug->leave_sub();
2036 $main::lxdebug->enter_sub();
2038 my ($self, $dbh) = @_;
2040 my ($query, $printed, $emailed);
2042 my $formnames = $self->{printed};
2043 my $emailforms = $self->{emailed};
2045 my $query = qq|DELETE FROM status
2046 WHERE formname = '$self->{formname}'
2047 AND trans_id = $self->{id}|;
2048 $dbh->do($query) || $self->dberror($query);
2050 # this only applies to the forms
2051 # checks and receipts are posted when printed or queued
2053 if ($self->{queued}) {
2054 my %queued = split / /, $self->{queued};
2056 foreach my $formname (keys %queued) {
2057 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2058 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2060 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2061 spoolfile, formname)
2062 VALUES ($self->{id}, '$printed', '$emailed',
2063 '$queued{$formname}', '$formname')|;
2064 $dbh->do($query) || $self->dberror($query);
2066 $formnames =~ s/$self->{formname}//;
2067 $emailforms =~ s/$self->{formname}//;
2072 # save printed, emailed info
2073 $formnames =~ s/^ +//g;
2074 $emailforms =~ s/^ +//g;
2077 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2078 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2080 foreach my $formname (keys %status) {
2081 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2082 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2084 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2085 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2086 $dbh->do($query) || $self->dberror($query);
2089 $main::lxdebug->leave_sub();
2092 sub update_defaults {
2093 $main::lxdebug->enter_sub();
2095 my ($self, $myconfig, $fld) = @_;
2097 my $dbh = $self->dbconnect_noauto($myconfig);
2098 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2099 my $sth = $dbh->prepare($query);
2101 $sth->execute || $self->dberror($query);
2102 my ($var) = $sth->fetchrow_array;
2107 $query = qq|UPDATE defaults
2109 $dbh->do($query) || $self->dberror($query);
2114 $main::lxdebug->leave_sub();
2119 sub update_business {
2120 $main::lxdebug->enter_sub();
2122 my ($self, $myconfig, $business_id) = @_;
2124 my $dbh = $self->dbconnect_noauto($myconfig);
2126 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2127 my $sth = $dbh->prepare($query);
2129 $sth->execute || $self->dberror($query);
2130 my ($var) = $sth->fetchrow_array;
2135 $query = qq|UPDATE business
2136 SET customernumberinit = '$var' WHERE id=$business_id|;
2137 $dbh->do($query) || $self->dberror($query);
2142 $main::lxdebug->leave_sub();
2148 $main::lxdebug->enter_sub();
2150 my ($self, $myconfig, $salesman) = @_;
2152 my $dbh = $self->dbconnect($myconfig);
2154 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2155 my $sth = $dbh->prepare($query);
2156 $sth->execute || $self->dberror($query);
2159 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2160 push(@{ $self->{salesman_list} }, $ref);
2164 $main::lxdebug->leave_sub();
2169 sub get_partsgroup {
2170 $main::lxdebug->enter_sub();
2172 my ($self, $myconfig, $p) = @_;
2174 my $dbh = $self->dbconnect($myconfig);
2176 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2178 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2180 if ($p->{searchitems} eq 'part') {
2182 WHERE p.inventory_accno_id > 0|;
2184 if ($p->{searchitems} eq 'service') {
2186 WHERE p.inventory_accno_id IS NULL|;
2188 if ($p->{searchitems} eq 'assembly') {
2190 WHERE p.assembly = '1'|;
2192 if ($p->{searchitems} eq 'labor') {
2194 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2198 ORDER BY partsgroup|;
2201 $query = qq|SELECT id, partsgroup FROM partsgroup
2202 ORDER BY partsgroup|;
2205 if ($p->{language_code}) {
2206 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2207 t.description AS translation
2209 JOIN parts p ON (p.partsgroup_id = pg.id)
2210 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2211 ORDER BY translation|;
2214 my $sth = $dbh->prepare($query);
2215 $sth->execute || $self->dberror($query);
2217 $self->{all_partsgroup} = ();
2218 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2219 push @{ $self->{all_partsgroup} }, $ref;
2223 $main::lxdebug->leave_sub();
2226 sub get_pricegroup {
2227 $main::lxdebug->enter_sub();
2229 my ($self, $myconfig, $p) = @_;
2231 my $dbh = $self->dbconnect($myconfig);
2233 my $query = qq|SELECT p.id, p.pricegroup
2237 ORDER BY pricegroup|;
2240 $query = qq|SELECT id, pricegroup FROM pricegroup
2241 ORDER BY pricegroup|;
2244 my $sth = $dbh->prepare($query);
2245 $sth->execute || $self->dberror($query);
2247 $self->{all_pricegroup} = ();
2248 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2249 push @{ $self->{all_pricegroup} }, $ref;
2254 $main::lxdebug->leave_sub();
2258 my ($self, $dbh, $myconfig, $audittrail) = @_;
2260 # table, $reference, $formname, $action, $id, $transdate) = @_;
2267 $dbh = $self->dbconnect($myconfig);
2271 # if we have an id add audittrail, otherwise get a new timestamp
2273 if ($audittrail->{id}) {
2275 $query = qq|SELECT audittrail FROM defaults|;
2277 if ($dbh->selectrow_array($query)) {
2278 my ($null, $employee_id) = $self->get_employee($dbh);
2280 if ($self->{audittrail} && !$myconfig) {
2281 chop $self->{audittrail};
2283 my @a = split /\|/, $self->{audittrail};
2287 my @flds = qw(tablename reference formname action transdate);
2289 # put into hash and remove dups
2291 $key = "$a[2]$a[3]";
2293 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2297 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2298 formname, action, employee_id, transdate)
2299 VALUES ($audittrail->{id}, ?, ?,
2300 ?, ?, $employee_id, ?)|;
2301 my $sth = $dbh->prepare($query) || $self->dberror($query);
2305 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2309 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2311 $sth->execute || $self->dberror;
2316 if ($audittrail->{transdate}) {
2317 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2318 formname, action, employee_id, transdate) VALUES (
2319 $audittrail->{id}, '$audittrail->{tablename}', |
2320 . $dbh->quote($audittrail->{reference}) . qq|,
2321 '$audittrail->{formname}', '$audittrail->{action}',
2322 $employee_id, '$audittrail->{transdate}')|;
2324 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2325 formname, action, employee_id) VALUES ($audittrail->{id},
2326 '$audittrail->{tablename}', |
2327 . $dbh->quote($audittrail->{reference}) . qq|,
2328 '$audittrail->{formname}', '$audittrail->{action}',
2335 $query = qq|SELECT current_timestamp FROM defaults|;
2336 my ($timestamp) = $dbh->selectrow_array($query);
2339 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2342 $dbh->disconnect if $disconnect;
2351 $main::lxdebug->enter_sub();
2353 my ($type, $country, $NLS_file) = @_;
2357 if ($country && -d "locale/$country") {
2358 $self->{countrycode} = $country;
2359 eval { require "locale/$country/$NLS_file"; };
2362 $self->{NLS_file} = $NLS_file;
2364 push @{ $self->{LONG_MONTH} },
2365 ("January", "February", "March", "April",
2366 "May ", "June", "July", "August",
2367 "September", "October", "November", "December");
2368 push @{ $self->{SHORT_MONTH} },
2369 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2371 $main::lxdebug->leave_sub();
2377 my ($self, $text) = @_;
2379 return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
2383 $main::lxdebug->enter_sub();
2385 my ($self, $text) = @_;
2387 if (exists $self{subs}{$text}) {
2388 $text = $self{subs}{$text};
2390 if ($self->{countrycode} && $self->{NLS_file}) {
2392 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2396 $main::lxdebug->leave_sub();
2402 $main::lxdebug->enter_sub();
2404 my ($self, $myconfig, $date, $longformat) = @_;
2407 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2412 $spc = $myconfig->{dateformat};
2414 $spc = substr($spc, 1, 1);
2416 if ($date =~ /\D/) {
2417 if ($myconfig->{dateformat} =~ /^yy/) {
2418 ($yy, $mm, $dd) = split /\D/, $date;
2420 if ($myconfig->{dateformat} =~ /^mm/) {
2421 ($mm, $dd, $yy) = split /\D/, $date;
2423 if ($myconfig->{dateformat} =~ /^dd/) {
2424 ($dd, $mm, $yy) = split /\D/, $date;
2427 $date = substr($date, 2);
2428 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2433 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2434 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2436 if ($myconfig->{dateformat} =~ /^dd/) {
2437 if (defined $longformat && $longformat == 0) {
2439 $dd = "0$dd" if ($dd < 10);
2440 $mm = "0$mm" if ($mm < 10);
2441 $longdate = "$dd$spc$mm$spc$yy";
2444 $longdate .= ($spc eq '.') ? ". " : " ";
2445 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2447 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2449 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2450 # Lx-Office is mainly used in Germany or German speaking countries.
2451 if (defined $longformat && $longformat == 0) {
2453 $dd = "0$dd" if ($dd < 10);
2454 $mm = "0$mm" if ($mm < 10);
2455 $longdate = "$yy-$mm-$dd";
2457 $longdate = "$dd. ";
2458 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2461 if (defined $longformat && $longformat == 0) {
2463 $dd = "0$dd" if ($dd < 10);
2464 $mm = "0$mm" if ($mm < 10);
2465 $longdate = "$mm$spc$dd$spc$yy";
2467 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2473 $main::lxdebug->leave_sub();
2479 $main::lxdebug->enter_sub();
2481 my ($self, $myconfig, $date, $longformat) = @_;
2484 $main::lxdebug->leave_sub();
2489 $spc = $myconfig->{dateformat};
2491 $spc = substr($spc, 1, 1);
2493 if ($date =~ /\D/) {
2494 if ($myconfig->{dateformat} =~ /^yy/) {
2495 ($yy, $mm, $dd) = split /\D/, $date;
2496 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2497 ($mm, $dd, $yy) = split /\D/, $date;
2498 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2499 ($dd, $mm, $yy) = split /\D/, $date;
2502 $date = substr($date, 2);
2503 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2508 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2509 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2511 $main::lxdebug->leave_sub();
2512 return ($yy, $mm, $dd);