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 #======================================================================
52 $main::lxdebug->enter_sub(2);
56 my @pairs = split(/&/, $input);
59 my ($name, $value) = split(/=/, $_, 2);
60 $in{$name} = unescape(undef, $value);
63 $main::lxdebug->leave_sub(2);
68 sub _request_to_hash {
69 $main::lxdebug->enter_sub(2);
72 my ($i, $loc, $key, $val);
73 my (%ATTACH, $f, $header, $header_body, $len, $buf);
74 my ($boundary, @list, $size, $body, $x, $blah, $name);
76 if ($ENV{'CONTENT_TYPE'}
77 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
78 $boundary = quotemeta('--' . $1);
79 @list = split(/$boundary/, $input);
81 # For some reason there are always 2 extra, that are empty
84 for ($x = 1; $x <= $size; $x++) {
85 $header_body = $list[$x];
86 $header_body =~ /\r\n\r\n|\n\n/;
88 # Here we split the header and body
93 # Now we try to get the file name
95 $name =~ /name=\"(.+)\"/;
96 ($name, $blah) = split(/\"/, $1);
98 # If the form name is not attach, then we need to parse this like
100 if ($name ne "attach") {
101 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
102 $ATTACH{$name} = $body;
104 # Otherwise it is an attachment and we need to finish it up
105 } elsif ($name eq "attach") {
106 $header =~ /filename=\"(.+)\"/;
107 $ATTACH{'FILE_NAME'} = $1;
108 $ATTACH{'FILE_NAME'} =~ s/\"//g;
109 $ATTACH{'FILE_NAME'} =~ s/\s//g;
110 $ATTACH{'FILE_CONTENT'} = $body;
112 for ($i = $x; $list[$i]; $i++) {
113 $list[$i] =~ s/^.+name=$//;
114 $list[$i] =~ /\"(\w+)\"/;
120 $main::lxdebug->leave_sub(2);
124 $main::lxdebug->leave_sub(2);
125 return _input_to_hash($input);
130 $main::lxdebug->enter_sub();
136 if ($LXDebug::watch_form) {
137 require SL::Watchdog;
138 tie %{ $self }, 'SL::Watchdog';
141 read(STDIN, $_, $ENV{CONTENT_LENGTH});
143 if ($ENV{QUERY_STRING}) {
144 $_ = $ENV{QUERY_STRING};
151 my %parameters = _request_to_hash($_);
152 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
154 $self->{action} = lc $self->{action};
155 $self->{action} =~ s/( |-|,|\#)/_/g;
157 $self->{version} = "2.4.2";
159 $main::lxdebug->leave_sub();
165 $main::lxdebug->enter_sub();
171 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
173 $main::lxdebug->leave_sub();
177 $main::lxdebug->enter_sub(2);
179 my ($self, $str) = @_;
181 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
183 $main::lxdebug->leave_sub(2);
189 $main::lxdebug->enter_sub(2);
191 my ($self, $str) = @_;
196 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
198 $main::lxdebug->leave_sub(2);
204 my ($self, $str) = @_;
206 if ($str && !ref($str)) {
207 $str =~ s/\"/"/g;
215 my ($self, $str) = @_;
217 if ($str && !ref($str)) {
218 $str =~ s/"/\"/g;
226 $main::lxdebug->enter_sub(2);
228 my ($self, $str) = @_;
231 ('order' => ['"', '<', '>'],
237 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
239 $main::lxdebug->leave_sub(2);
248 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
250 for (sort keys %$self) {
251 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
252 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
259 $main::lxdebug->enter_sub();
261 $main::lxdebug->show_backtrace();
263 my ($self, $msg) = @_;
264 if ($ENV{HTTP_USER_AGENT}) {
266 $self->show_generic_error($msg);
270 if ($self->{error_function}) {
271 &{ $self->{error_function} }($msg);
277 $main::lxdebug->leave_sub();
281 $main::lxdebug->enter_sub();
283 my ($self, $msg) = @_;
285 if ($ENV{HTTP_USER_AGENT}) {
288 if (!$self->{header}) {
301 if ($self->{info_function}) {
302 &{ $self->{info_function} }($msg);
308 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub();
314 my ($self, $str, $cols, $maxrows) = @_;
318 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
320 $maxrows = $rows unless defined $maxrows;
322 $main::lxdebug->leave_sub();
324 return ($rows > $maxrows) ? $maxrows : $rows;
328 $main::lxdebug->enter_sub();
330 my ($self, $msg) = @_;
332 $self->error("$msg\n" . $DBI::errstr);
334 $main::lxdebug->leave_sub();
338 $main::lxdebug->enter_sub();
340 my ($self, $name, $msg) = @_;
342 if ($self->{$name} =~ /^\s*$/) {
345 $main::lxdebug->leave_sub();
349 $main::lxdebug->enter_sub();
351 my ($self, $extra_code) = @_;
353 if ($self->{header}) {
354 $main::lxdebug->leave_sub();
358 my ($stylesheet, $favicon);
360 if ($ENV{HTTP_USER_AGENT}) {
362 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
364 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
368 $self->{favicon} = "favicon.ico" unless $self->{favicon};
370 if ($self->{favicon} && (-f "$self->{favicon}")) {
372 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
376 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
378 if ($self->{landscape}) {
379 $pagelayout = qq|<style type="text/css">
380 \@page { size:landscape; }
384 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
388 if ($self->{jsscript} == 1) {
391 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
392 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
393 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
394 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
401 ? "$self->{title} - $self->{titlebar}"
404 foreach $item (@ { $self->{AJAX} }) {
405 $ajax .= $item->show_javascript();
407 print qq|Content-Type: text/html; charset=${db_charset};
411 <title>$self->{titlebar}</title>
415 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
419 <script type="text/javascript">
427 <meta name="robots" content="noindex,nofollow" />
428 <script type="text/javascript" src="js/highlight_input.js"></script>
429 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
431 <script type="text/javascript" src="js/tabcontent.js">
433 /***********************************************
434 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
435 * This notice MUST stay intact for legal use
436 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
437 ***********************************************/
448 $main::lxdebug->leave_sub();
451 sub parse_html_template {
452 $main::lxdebug->enter_sub();
454 my ($self, $file, $additional_params) = @_;
457 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
458 $language = $main::language;
460 $language = $main::myconfig{"countrycode"};
462 $language = "de" unless ($language);
464 if (-f "templates/webpages/${file}_${language}.html") {
465 if ((-f ".developer") &&
466 (-f "templates/webpages/${file}_master.html") &&
467 ((stat("templates/webpages/${file}_master.html"))[9] >
468 (stat("templates/webpages/${file}_${language}.html"))[9])) {
469 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
470 "Please re-run 'locales.pl' in 'locale/${language}'.";
471 print(qq|<pre>$info</pre>|);
475 $file = "templates/webpages/${file}_${language}.html";
476 } elsif (-f "templates/webpages/${file}.html") {
477 $file = "templates/webpages/${file}.html";
479 my $info = "Web page template '${file}' not found.\n" .
480 "Please re-run 'locales.pl' in 'locale/${language}'.";
481 print(qq|<pre>$info</pre>|);
485 my $template = HTML::Template->new("filename" => $file,
486 "die_on_bad_params" => 0,
488 "case_sensitive" => 1,
489 "loop_context_vars" => 1,
492 $additional_params = {} unless ($additional_params);
493 if ($self->{"DEBUG"}) {
494 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
497 if ($additional_params->{"DEBUG"}) {
498 $additional_params->{"DEBUG"} =
499 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
502 if (%main::myconfig) {
503 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
504 my $jsc_dateformat = $main::myconfig{"dateformat"};
505 $jsc_dateformat =~ s/d+/\%d/gi;
506 $jsc_dateformat =~ s/m+/\%m/gi;
507 $jsc_dateformat =~ s/y+/\%Y/gi;
508 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
511 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
512 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
513 $additional_params->{"conf_latex_templates"} = $main::latex;
514 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
516 my @additional_param_names = keys(%{$additional_params});
517 foreach my $key ($template->param()) {
518 my $param = $self->{$key};
519 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
520 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
521 $template->param($key => $param);
524 my $output = $template->output();
526 $main::lxdebug->leave_sub();
531 sub show_generic_error {
532 my ($self, $error, $title, $action) = @_;
535 $add_params->{"title"} = $title if ($title);
536 $self->{"label_error"} = $error;
540 map({ delete($self->{$_}); } qw(action));
541 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
542 if (!ref($self->{$_})); }
544 $add_params->{"SHOW_BUTTON"} = 1;
545 $add_params->{"BUTTON_LABEL"} = $action;
547 $add_params->{"VARIABLES"} = \@vars;
550 print($self->parse_html_template("generic/error", $add_params));
552 die("Error: $error\n");
555 sub show_generic_information {
556 my ($self, $error, $title) = @_;
559 $add_params->{"title"} = $title if ($title);
560 $self->{"label_information"} = $error;
563 print($self->parse_html_template("generic/information", $add_params));
565 die("Information: $error\n");
568 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
569 # changed it to accept an arbitrary number of triggers - sschoeling
571 $main::lxdebug->enter_sub();
574 my $myconfig = shift;
577 # set dateform for jsscript
580 "dd.mm.yy" => "%d.%m.%Y",
581 "dd-mm-yy" => "%d-%m-%Y",
582 "dd/mm/yy" => "%d/%m/%Y",
583 "mm/dd/yy" => "%m/%d/%Y",
584 "mm-dd-yy" => "%m-%d-%Y",
585 "yyyy-mm-dd" => "%Y-%m-%d",
588 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
589 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
596 inputField : "| . (shift) . qq|",
597 ifFormat :"$ifFormat",
598 align : "| . (shift) . qq|",
599 button : "| . (shift) . qq|"
605 <script type="text/javascript">
606 <!--| . join("", @triggers) . qq|//-->
610 $main::lxdebug->leave_sub();
613 } #end sub write_trigger
616 $main::lxdebug->enter_sub();
618 my ($self, $msg) = @_;
620 if ($self->{callback}) {
622 ($script, $argv) = split(/\?/, $self->{callback});
623 exec("perl", "$script", $argv);
631 $main::lxdebug->leave_sub();
634 # sort of columns removed - empty sub
636 $main::lxdebug->enter_sub();
638 my ($self, @columns) = @_;
640 $main::lxdebug->leave_sub();
646 $main::lxdebug->enter_sub(2);
648 my ($self, $myconfig, $amount, $places, $dash) = @_;
653 my $neg = ($amount =~ s/-//);
655 if (defined($places) && ($places ne '')) {
660 my ($actual_places) = ($amount =~ /\.(\d+)/);
661 $actual_places = length($actual_places);
662 $places = $actual_places > $places ? $actual_places : $places;
665 $amount = $self->round_amount($amount, $places);
668 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
669 my @p = split(/\./, $amount); # split amount at decimal point
671 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
674 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
677 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
678 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
679 ($neg ? "-$amount" : "$amount" ) ;
683 $main::lxdebug->leave_sub(2);
688 $main::lxdebug->enter_sub(2);
690 my ($self, $myconfig, $amount) = @_;
692 if ( ($myconfig->{numberformat} eq '1.000,00')
693 || ($myconfig->{numberformat} eq '1000,00')) {
698 if ($myconfig->{numberformat} eq "1'000.00") {
704 $main::lxdebug->leave_sub(2);
706 return ($amount * 1);
710 $main::lxdebug->enter_sub(2);
712 my ($self, $amount, $places) = @_;
715 # Rounding like "Kaufmannsrunden"
716 # Descr. http://de.wikipedia.org/wiki/Rundung
718 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
721 $amount = $amount * (10**($places));
722 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
724 $main::lxdebug->leave_sub(2);
726 return $round_amount;
731 $main::lxdebug->enter_sub();
733 my ($self, $myconfig, $userspath) = @_;
736 $self->{"cwd"} = getcwd();
737 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
739 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
740 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
741 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
742 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
743 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
744 } elsif (($self->{"format"} =~ /html/i) ||
745 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
746 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
747 } elsif (($self->{"format"} =~ /xml/i) ||
748 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
749 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
750 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
751 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
752 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
753 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
754 } elsif ( defined $self->{'format'}) {
755 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
756 } elsif ( $self->{'format'} eq '' ) {
757 $self->error("No Outputformat given: $self->{'format'}");
758 } else { #Catch the rest
759 $self->error("Outputformat not defined: $self->{'format'}");
762 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
763 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
765 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
766 qw(email tel fax name signature company address businessnumber
767 co_ustid taxnumber duns));
768 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
769 qw(company address signature));
770 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
772 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
774 # OUT is used for the media, screen, printer, email
775 # for postscript we store a copy in a temporary file
777 $self->{tmpfile} ||= "$userspath/${fileid}.$self->{IN}";
778 if ($template->uses_temp_file() || $self->{media} eq 'email') {
780 $self->{OUT} = ">$self->{tmpfile}";
784 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
786 open(OUT, ">-") or $self->error("STDOUT : $!");
790 if (!$template->parse(*OUT)) {
792 $self->error("$self->{IN} : " . $template->get_error());
797 if ($template->uses_temp_file() || $self->{media} eq 'email') {
799 if ($self->{media} eq 'email') {
803 my $mail = new Mailer;
805 map { $mail->{$_} = $self->{$_} }
806 qw(cc bcc subject message version format);
807 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
808 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
809 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
810 $mail->{fileid} = "$fileid.";
811 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
813 # if we send html or plain text inline
814 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
815 $mail->{contenttype} = "text/html";
817 $mail->{message} =~ s/\r\n/<br>\n/g;
818 $myconfig->{signature} =~ s/\\n/<br>\n/g;
819 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
821 open(IN, $self->{tmpfile})
822 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
824 $mail->{message} .= $_;
831 if (!$self->{"do_not_attach"}) {
832 @{ $mail->{attachments} } =
833 ({ "filename" => $self->{"tmpfile"},
834 "name" => $self->{"attachment_filename"} ?
835 $self->{"attachment_filename"} : $self->{"tmpfile"} });
838 $mail->{message} =~ s/\r\n/\n/g;
839 $myconfig->{signature} =~ s/\\n/\n/g;
840 $mail->{message} .= "\n-- \n$myconfig->{signature}";
844 my $err = $mail->send($out);
845 $self->error($self->cleanup . "$err") if ($err);
851 my $numbytes = (-s $self->{tmpfile});
852 open(IN, $self->{tmpfile})
853 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
855 $self->{copies} = 1 unless $self->{media} eq 'printer';
857 chdir("$self->{cwd}");
858 #print(STDERR "Kopien $self->{copies}\n");
859 #print(STDERR "OUT $self->{OUT}\n");
860 for my $i (1 .. $self->{copies}) {
862 open(OUT, $self->{OUT})
863 or $self->error($self->cleanup . "$self->{OUT} : $!");
865 $self->{attachment_filename} = $self->generate_attachment_filename();
868 print qq|Content-Type: | . $template->get_mime_type() . qq|
869 Content-Disposition: attachment; filename="$self->{attachment_filename}"
870 Content-Length: $numbytes
874 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
894 chdir("$self->{cwd}");
895 $main::lxdebug->leave_sub();
898 sub generate_attachment_filename {
901 my %formname_translations = (
902 bin_list => $main::locale->text('Bin List'),
903 credit_note => $main::locale->text('Credit Note'),
904 invoice => $main::locale->text('Invoice'),
905 packing_list => $main::locale->text('Packing List'),
906 pick_list => $main::locale->text('Pick List'),
907 proforma => $main::locale->text('Proforma Invoice'),
908 purchase_order => $main::locale->text('Purchase Order'),
909 request_quotation => $main::locale->text('RFQ'),
910 sales_order => $main::locale->text('Confirmation'),
911 sales_quotation => $main::locale->text('Quotation'),
912 storno_invoice => $main::locale->text('Storno Invoice'),
913 storno_packing_list => $main::locale->text('Storno Packing List'),
916 my $attachment_filename = $formname_translations{$self->{"formname"}};
918 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
919 : ($self->{"type"} =~ /_quotation$/) ? "quo"
922 if ($attachment_filename && $self->{"${prefix}number"}) {
923 $attachment_filename .= "_" . $self->{"${prefix}number"}
924 . ( $self->{format} =~ /pdf/i ? ".pdf"
925 : $self->{format} =~ /postscript/i ? ".ps"
926 : $self->{format} =~ /opendocument/i ? ".odt"
927 : $self->{format} =~ /html/i ? ".html"
929 $attachment_filename =~ s/ /_/g;
930 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
931 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
932 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
934 $attachment_filename = "";
937 return $attachment_filename;
941 $main::lxdebug->enter_sub();
945 chdir("$self->{tmpdir}");
948 if (-f "$self->{tmpfile}.err") {
949 open(FH, "$self->{tmpfile}.err");
954 if ($self->{tmpfile}) {
955 $self->{tmpfile} =~ s|.*/||g;
957 $self->{tmpfile} =~ s/\.\w+$//g;
958 my $tmpfile = $self->{tmpfile};
959 unlink(<$tmpfile.*>);
962 chdir("$self->{cwd}");
964 $main::lxdebug->leave_sub();
970 $main::lxdebug->enter_sub();
972 my ($self, $date, $myconfig) = @_;
974 if ($date && $date =~ /\D/) {
976 if ($myconfig->{dateformat} =~ /^yy/) {
977 ($yy, $mm, $dd) = split /\D/, $date;
979 if ($myconfig->{dateformat} =~ /^mm/) {
980 ($mm, $dd, $yy) = split /\D/, $date;
982 if ($myconfig->{dateformat} =~ /^dd/) {
983 ($dd, $mm, $yy) = split /\D/, $date;
988 $yy = ($yy < 70) ? $yy + 2000 : $yy;
989 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
991 $dd = "0$dd" if ($dd < 10);
992 $mm = "0$mm" if ($mm < 10);
997 $main::lxdebug->leave_sub();
1002 # Database routines used throughout
1005 $main::lxdebug->enter_sub(2);
1007 my ($self, $myconfig) = @_;
1009 # connect to database
1011 DBI->connect($myconfig->{dbconnect},
1012 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1016 if ($myconfig->{dboptions}) {
1017 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1020 $main::lxdebug->leave_sub(2);
1025 sub dbconnect_noauto {
1026 $main::lxdebug->enter_sub();
1028 my ($self, $myconfig) = @_;
1030 # connect to database
1032 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1033 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1037 if ($myconfig->{dboptions}) {
1038 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1041 $main::lxdebug->leave_sub();
1046 sub update_balance {
1047 $main::lxdebug->enter_sub();
1049 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1051 # if we have a value, go do it
1054 # retrieve balance from table
1055 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1056 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1057 my ($balance) = $sth->fetchrow_array;
1063 $query = "UPDATE $table SET $field = $balance WHERE $where";
1064 do_query($self, $dbh, $query, @values);
1066 $main::lxdebug->leave_sub();
1069 sub update_exchangerate {
1070 $main::lxdebug->enter_sub();
1072 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1074 # some sanity check for currency
1076 $main::lxdebug->leave_sub();
1080 my $query = qq|SELECT e.curr FROM exchangerate e
1081 WHERE e.curr = ? AND e.transdate = ?
1083 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1086 if ($buy != 0 && $sell != 0) {
1087 $set = "buy = $buy, sell = $sell";
1088 } elsif ($buy != 0) {
1089 $set = "buy = $buy";
1090 } elsif ($sell != 0) {
1091 $set = "sell = $sell";
1094 if ($sth->fetchrow_array) {
1095 $query = qq|UPDATE exchangerate
1100 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1101 VALUES (?, $buy, $sell, ?)|;
1104 do_query($self, $dbh, $query, $curr, $transdate);
1106 $main::lxdebug->leave_sub();
1109 sub save_exchangerate {
1110 $main::lxdebug->enter_sub();
1112 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1114 my $dbh = $self->dbconnect($myconfig);
1116 my ($buy, $sell) = (0, 0);
1117 $buy = $rate if $fld eq 'buy';
1118 $sell = $rate if $fld eq 'sell';
1120 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1124 $main::lxdebug->leave_sub();
1127 sub get_exchangerate {
1128 $main::lxdebug->enter_sub();
1130 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1132 unless ($transdate) {
1133 $main::lxdebug->leave_sub();
1137 my $query = qq|SELECT e.$fld FROM exchangerate e
1138 WHERE e.curr = ? AND e.transdate = ?|;
1139 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1141 if (!$exchangerate) {
1145 $main::lxdebug->leave_sub();
1147 return $exchangerate;
1150 sub check_exchangerate {
1151 $main::lxdebug->enter_sub();
1153 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1155 unless ($transdate) {
1156 $main::lxdebug->leave_sub();
1160 my $dbh = $self->dbconnect($myconfig);
1162 my $query = qq|SELECT e.$fld FROM exchangerate e
1163 WHERE e.curr = ? AND e.transdate = ?|;
1164 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1167 $main::lxdebug->leave_sub();
1169 return $exchangerate;
1172 sub set_payment_options {
1173 $main::lxdebug->enter_sub();
1175 my ($self, $myconfig, $transdate) = @_;
1177 if ($self->{payment_id}) {
1179 my $dbh = $self->dbconnect($myconfig);
1182 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1183 qq|FROM payment_terms p | .
1186 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1187 $self->{payment_terms}) =
1188 selectrow_query($self, $dbh, $query, $self->{payment_id});
1190 if ($transdate eq "") {
1191 if ($self->{invdate}) {
1192 $transdate = $self->{invdate};
1194 $transdate = $self->{transdate};
1199 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1200 qq|FROM payment_terms|;
1201 ($self->{netto_date}, $self->{skonto_date}) =
1202 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1204 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1205 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1206 $self->{percent_skonto};
1208 $self->{skonto_amount} =
1209 $self->format_amount($myconfig, $skonto_amount, 2);
1211 if ($self->{"language_id"}) {
1213 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1214 qq|FROM translation_payment_terms t | .
1215 qq|LEFT JOIN language l ON t.language_id = l.id | .
1216 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1217 my ($description_long, $output_numberformat, $output_dateformat,
1218 $output_longdates) =
1219 selectrow_query($self, $dbh, $query,
1220 $self->{"language_id"}, $self->{"payment_id"});
1222 $self->{payment_terms} = $description_long if ($description_long);
1224 if ($output_dateformat) {
1225 foreach my $key (qw(netto_date skonto_date)) {
1227 $main::locale->reformat_date($myconfig, $self->{$key},
1233 if ($output_numberformat &&
1234 ($output_numberformat ne $myconfig->{"numberformat"})) {
1235 my $saved_numberformat = $myconfig->{"numberformat"};
1236 $myconfig->{"numberformat"} = $output_numberformat;
1237 $self->{skonto_amount} =
1238 $self->format_amount($myconfig, $skonto_amount, 2);
1239 $myconfig->{"numberformat"} = $saved_numberformat;
1243 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1244 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1245 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1246 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1247 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1248 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1249 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1250 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1251 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1252 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1257 $main::lxdebug->leave_sub();
1261 sub get_template_language {
1262 $main::lxdebug->enter_sub();
1264 my ($self, $myconfig) = @_;
1266 my $template_code = "";
1268 if ($self->{language_id}) {
1269 my $dbh = $self->dbconnect($myconfig);
1270 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1271 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1275 $main::lxdebug->leave_sub();
1277 return $template_code;
1280 sub get_printer_code {
1281 $main::lxdebug->enter_sub();
1283 my ($self, $myconfig) = @_;
1285 my $template_code = "";
1287 if ($self->{printer_id}) {
1288 my $dbh = $self->dbconnect($myconfig);
1289 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1290 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1294 $main::lxdebug->leave_sub();
1296 return $template_code;
1300 $main::lxdebug->enter_sub();
1302 my ($self, $myconfig) = @_;
1304 my $template_code = "";
1306 if ($self->{shipto_id}) {
1307 my $dbh = $self->dbconnect($myconfig);
1308 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1309 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1310 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1314 $main::lxdebug->leave_sub();
1318 $main::lxdebug->enter_sub();
1320 my ($self, $dbh, $id, $module) = @_;
1324 foreach my $item (qw(name department_1 department_2 street zipcode city country
1325 contact phone fax email)) {
1326 if ($self->{"shipto$item"}) {
1327 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1329 push(@values, $self->{"shipto${item}"});
1332 if ($self->{shipto_id}) {
1333 my $query = qq|UPDATE shipto set
1335 shiptodepartment_1 = ?,
1336 shiptodepartment_2 = ?,
1345 WHERE shipto_id = ?|;
1346 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1348 my $query = qq|SELECT * FROM shipto
1349 WHERE shiptoname = ? AND
1350 shiptodepartment_1 = ? AND
1351 shiptodepartment_2 = ? AND
1352 shiptostreet = ? AND
1353 shiptozipcode = ? AND
1355 shiptocountry = ? AND
1356 shiptocontact = ? AND
1360 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1363 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1364 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1365 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1366 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1367 do_query($self, $dbh, $query, $id, @values, $module);
1372 $main::lxdebug->leave_sub();
1376 $main::lxdebug->enter_sub();
1378 my ($self, $dbh) = @_;
1380 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1381 ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
1382 $self->{employee_id} *= 1;
1384 $main::lxdebug->leave_sub();
1388 $main::lxdebug->enter_sub();
1390 my ($self, $myconfig, $salesman_id) = @_;
1392 $main::lxdebug->leave_sub() and return unless $salesman_id;
1394 my $dbh = $self->dbconnect($myconfig);
1397 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1401 my $user = new User($main::memberfile, $login);
1402 map({ $self->{"salesman_$_"} = $user->{$_}; }
1403 qw(address businessnumber co_ustid company duns email fax name
1405 $self->{salesman_login} = $login;
1407 $self->{salesman_name} = $login
1408 if ($self->{salesman_name} eq "");
1410 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1415 $main::lxdebug->leave_sub();
1419 $main::lxdebug->enter_sub();
1421 my ($self, $myconfig) = @_;
1423 my $dbh = $self->dbconnect($myconfig);
1424 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1425 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1428 $main::lxdebug->leave_sub();
1432 $main::lxdebug->enter_sub();
1434 my ($self, $dbh, $id, $key) = @_;
1436 $key = "all_contacts" unless ($key);
1439 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1440 qq|FROM contacts | .
1441 qq|WHERE cp_cv_id = ? | .
1442 qq|ORDER BY lower(cp_name)|;
1444 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1446 $main::lxdebug->leave_sub();
1450 $main::lxdebug->enter_sub();
1452 my ($self, $dbh, $key) = @_;
1454 my ($all, $old_id, $where, @values);
1456 if (ref($key) eq "HASH") {
1459 $key = "ALL_PROJECTS";
1461 foreach my $p (keys(%{$params})) {
1463 $all = $params->{$p};
1464 } elsif ($p eq "old_id") {
1465 $old_id = $params->{$p};
1466 } elsif ($p eq "key") {
1467 $key = $params->{$p};
1473 $where = "WHERE active ";
1475 if (ref($old_id) eq "ARRAY") {
1476 my @ids = grep({ $_ } @{$old_id});
1478 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1479 push(@values, @ids);
1482 $where .= " OR (id = ?) ";
1483 push(@values, $old_id);
1489 qq|SELECT id, projectnumber, description, active | .
1492 qq|ORDER BY lower(projectnumber)|;
1494 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1496 $main::lxdebug->leave_sub();
1500 $main::lxdebug->enter_sub();
1502 my ($self, $dbh, $vc_id, $key) = @_;
1504 $key = "all_shipto" unless ($key);
1506 # get shipping addresses
1508 qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
1510 qq|WHERE trans_id = ?|;
1512 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1514 $main::lxdebug->leave_sub();
1518 $main::lxdebug->enter_sub();
1520 my ($self, $dbh, $key) = @_;
1522 $key = "all_printers" unless ($key);
1524 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1526 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1528 $main::lxdebug->leave_sub();
1532 $main::lxdebug->enter_sub();
1534 my ($self, $dbh, $params) = @_;
1536 $key = $params->{key};
1537 $key = "all_charts" unless ($key);
1539 my $transdate = quote_db_date($params->{transdate});
1542 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1544 qq|LEFT JOIN taxkeys tk ON | .
1545 qq|(tk.id = (SELECT id FROM taxkeys | .
1546 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1547 qq| ORDER BY startdate DESC LIMIT 1)) | .
1548 qq|ORDER BY c.accno|;
1550 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1552 $main::lxdebug->leave_sub();
1555 sub _get_taxcharts {
1556 $main::lxdebug->enter_sub();
1558 my ($self, $dbh, $key) = @_;
1560 $key = "all_taxcharts" unless ($key);
1562 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1564 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1566 $main::lxdebug->leave_sub();
1570 $main::lxdebug->enter_sub();
1572 my ($self, $dbh, $key) = @_;
1574 $key = "all_taxzones" unless ($key);
1576 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1578 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1580 $main::lxdebug->leave_sub();
1583 sub _get_employees {
1584 $main::lxdebug->enter_sub();
1586 my ($self, $dbh, $key) = @_;
1588 $key = "all_employees" unless ($key);
1590 selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
1592 $main::lxdebug->leave_sub();
1595 sub _get_business_types {
1596 $main::lxdebug->enter_sub();
1598 my ($self, $dbh, $key) = @_;
1600 $key = "all_business_types" unless ($key);
1602 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1604 $main::lxdebug->leave_sub();
1607 sub _get_languages {
1608 $main::lxdebug->enter_sub();
1610 my ($self, $dbh, $key) = @_;
1612 $key = "all_languages" unless ($key);
1614 my $query = qq|SELECT * FROM language ORDER BY id|;
1616 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1618 $main::lxdebug->leave_sub();
1621 sub _get_dunning_configs {
1622 $main::lxdebug->enter_sub();
1624 my ($self, $dbh, $key) = @_;
1626 $key = "all_dunning_configs" unless ($key);
1628 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1630 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1632 $main::lxdebug->leave_sub();
1635 sub _get_currencies {
1636 $main::lxdebug->enter_sub();
1638 my ($self, $dbh, $key) = @_;
1640 $key = "all_currencies" unless ($key);
1642 my $query = qq|SELECT curr AS currency FROM defaults|;
1644 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1646 $main::lxdebug->leave_sub();
1650 $main::lxdebug->enter_sub();
1652 my ($self, $dbh, $key) = @_;
1654 $key = "all_payments" unless ($key);
1656 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1658 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1660 $main::lxdebug->leave_sub();
1663 sub _get_customers {
1664 $main::lxdebug->enter_sub();
1666 my ($self, $dbh, $key) = @_;
1668 $key = "all_customers" unless ($key);
1670 my $query = qq|SELECT * FROM customer LIMIT $main::myconfig{vclimit}|;
1672 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1674 $main::lxdebug->leave_sub();
1678 $main::lxdebug->enter_sub();
1680 my ($self, $dbh, $key) = @_;
1682 $key = "all_vendors" unless ($key);
1684 my $query = qq|SELECT * FROM vendor|; # LIMIT $main::myconfig{vclimit}|;
1686 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1688 $main::lxdebug->leave_sub();
1692 $main::lxdebug->enter_sub();
1697 my $dbh = $self->dbconnect(\%main::myconfig);
1698 my ($sth, $query, $ref);
1700 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1701 my $vc_id = $self->{"${vc}_id"};
1703 if ($params{"contacts"}) {
1704 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1707 if ($params{"shipto"}) {
1708 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1711 if ($params{"projects"} || $params{"all_projects"}) {
1712 $self->_get_projects($dbh, $params{"all_projects"} ?
1713 $params{"all_projects"} : $params{"projects"},
1714 $params{"all_projects"} ? 1 : 0);
1717 if ($params{"printers"}) {
1718 $self->_get_printers($dbh, $params{"printers"});
1721 if ($params{"languages"}) {
1722 $self->_get_languages($dbh, $params{"languages"});
1725 if ($params{"charts"}) {
1726 $self->_get_charts($dbh, $params{"charts"});
1729 if ($params{"taxcharts"}) {
1730 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1733 if ($params{"taxzones"}) {
1734 $self->_get_taxzones($dbh, $params{"taxzones"});
1737 if ($params{"employees"}) {
1738 $self->_get_employees($dbh, $params{"employees"});
1741 if ($params{"business_types"}) {
1742 $self->_get_business_types($dbh, $params{"business_types"});
1745 if ($params{"dunning_configs"}) {
1746 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1749 if($params{"currencies"}) {
1750 $self->_get_currencies($dbh, $params{"currencies"});
1753 if($params{"customers"}) {
1754 $self->_get_customers($dbh, $params{"customers"});
1757 if($params{"vendors"}) {
1758 $self->_get_vendors($dbh, $params{"vendors"});
1761 if($params{"payments"}) {
1762 $self->_get_payments($dbh, $params{"payments"});
1767 $main::lxdebug->leave_sub();
1770 # this sub gets the id and name from $table
1772 $main::lxdebug->enter_sub();
1774 my ($self, $myconfig, $table) = @_;
1776 # connect to database
1777 my $dbh = $self->dbconnect($myconfig);
1779 $table = $table eq "customer" ? "customer" : "vendor";
1780 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1782 my ($query, @values);
1784 if (!$self->{openinvoices}) {
1786 if ($self->{customernumber} ne "") {
1787 $where = qq|(vc.customernumber ILIKE ?)|;
1788 push(@values, '%' . $self->{customernumber} . '%');
1790 $where = qq|(vc.name ILIKE ?)|;
1791 push(@values, '%' . $self->{$table} . '%');
1795 qq~SELECT vc.id, vc.name,
1796 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1798 WHERE $where AND (NOT vc.obsolete)
1802 qq~SELECT DISTINCT vc.id, vc.name,
1803 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1805 JOIN $table vc ON (a.${table}_id = vc.id)
1806 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1808 push(@values, '%' . $self->{$table} . '%');
1811 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1813 $main::lxdebug->leave_sub();
1815 return scalar(@{ $self->{name_list} });
1818 # the selection sub is used in the AR, AP, IS, IR and OE module
1821 $main::lxdebug->enter_sub();
1823 my ($self, $myconfig, $table, $module) = @_;
1826 my $dbh = $self->dbconnect($myconfig);
1828 $table = $table eq "customer" ? "customer" : "vendor";
1830 my $query = qq|SELECT count(*) FROM $table|;
1831 my ($count) = selectrow_query($self, $dbh, $query);
1833 # build selection list
1834 if ($count < $myconfig->{vclimit}) {
1835 $query = qq|SELECT id, name, salesman_id
1836 FROM $table WHERE NOT obsolete
1838 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1842 $self->get_employee($dbh);
1844 # setup sales contacts
1845 $query = qq|SELECT e.id, e.name
1847 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1848 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1851 push(@{ $self->{all_employees} },
1852 { id => $self->{employee_id},
1853 name => $self->{employee} });
1855 # sort the whole thing
1856 @{ $self->{all_employees} } =
1857 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1859 if ($module eq 'AR') {
1861 # prepare query for departments
1862 $query = qq|SELECT id, description
1865 ORDER BY description|;
1868 $query = qq|SELECT id, description
1870 ORDER BY description|;
1873 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1876 $query = qq|SELECT id, description
1880 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1883 $query = qq|SELECT printer_description, id
1885 ORDER BY printer_description|;
1887 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1890 $query = qq|SELECT id, description
1894 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1898 $main::lxdebug->leave_sub();
1901 sub language_payment {
1902 $main::lxdebug->enter_sub();
1904 my ($self, $myconfig) = @_;
1906 my $dbh = $self->dbconnect($myconfig);
1908 my $query = qq|SELECT id, description
1912 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1915 $query = qq|SELECT printer_description, id
1917 ORDER BY printer_description|;
1919 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1922 $query = qq|SELECT id, description
1926 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1928 # get buchungsgruppen
1929 $query = qq|SELECT id, description
1930 FROM buchungsgruppen|;
1932 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1935 $main::lxdebug->leave_sub();
1938 # this is only used for reports
1939 sub all_departments {
1940 $main::lxdebug->enter_sub();
1942 my ($self, $myconfig, $table) = @_;
1944 my $dbh = $self->dbconnect($myconfig);
1947 if ($table eq 'customer') {
1948 $where = "WHERE role = 'P' ";
1951 my $query = qq|SELECT id, description
1954 ORDER BY description|;
1955 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1957 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1961 $main::lxdebug->leave_sub();
1965 $main::lxdebug->enter_sub();
1967 my ($self, $module, $myconfig, $table) = @_;
1970 if ($table eq "customer") {
1979 $self->all_vc($myconfig, $table, $module);
1981 # get last customers or vendors
1982 my ($query, $sth, $ref);
1984 my $dbh = $self->dbconnect($myconfig);
1989 my $transdate = "current_date";
1990 if ($self->{transdate}) {
1991 $transdate = $dbh->quote($self->{transdate});
1994 # now get the account numbers
1995 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1996 FROM chart c, taxkeys tk
1997 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1998 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2001 $sth = $dbh->prepare($query);
2003 do_statement($self, $sth, $query, '%' . $module . '%');
2005 $self->{accounts} = "";
2006 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2008 foreach my $key (split(/:/, $ref->{link})) {
2009 if ($key =~ /$module/) {
2011 # cross reference for keys
2012 $xkeyref{ $ref->{accno} } = $key;
2014 push @{ $self->{"${module}_links"}{$key} },
2015 { accno => $ref->{accno},
2016 description => $ref->{description},
2017 taxkey => $ref->{taxkey_id},
2018 tax_id => $ref->{tax_id} };
2020 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2026 # get taxkeys and description
2027 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2028 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2030 if (($module eq "AP") || ($module eq "AR")) {
2031 # get tax rates and description
2032 $query = qq|SELECT * FROM tax|;
2033 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2039 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2040 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2041 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2042 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2044 d.description AS department,
2047 JOIN $table c ON (a.${table}_id = c.id)
2048 LEFT JOIN employee e ON (e.id = a.employee_id)
2049 LEFT JOIN department d ON (d.id = a.department_id)
2051 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2053 foreach $key (keys %$ref) {
2054 $self->{$key} = $ref->{$key};
2057 my $transdate = "current_date";
2058 if ($self->{transdate}) {
2059 $transdate = $dbh->quote($self->{transdate});
2062 # now get the account numbers
2063 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2065 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2067 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2068 OR c.link LIKE '%_tax%')
2071 $sth = $dbh->prepare($query);
2072 do_statement($self, $sth, $query, "%$module%");
2074 $self->{accounts} = "";
2075 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2077 foreach my $key (split(/:/, $ref->{link})) {
2078 if ($key =~ /$module/) {
2080 # cross reference for keys
2081 $xkeyref{ $ref->{accno} } = $key;
2083 push @{ $self->{"${module}_links"}{$key} },
2084 { accno => $ref->{accno},
2085 description => $ref->{description},
2086 taxkey => $ref->{taxkey_id},
2087 tax_id => $ref->{tax_id} };
2089 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2095 # get amounts from individual entries
2098 c.accno, c.description,
2099 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2103 LEFT JOIN chart c ON (c.id = a.chart_id)
2104 LEFT JOIN project p ON (p.id = a.project_id)
2105 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2106 WHERE (tk.taxkey_id=a.taxkey) AND
2107 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2108 THEN tk.chart_id = a.chart_id
2111 OR (c.link='%tax%')) AND
2112 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2113 WHERE a.trans_id = ?
2114 AND a.fx_transaction = '0'
2115 ORDER BY a.oid, a.transdate|;
2116 $sth = $dbh->prepare($query);
2117 do_statement($self, $sth, $query, $self->{id});
2119 # get exchangerate for currency
2120 $self->{exchangerate} =
2121 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2124 # store amounts in {acc_trans}{$key} for multiple accounts
2125 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2126 $ref->{exchangerate} =
2127 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2128 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2131 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2132 $ref->{amount} *= -1;
2134 $ref->{index} = $index;
2136 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2142 d.curr AS currencies, d.closedto, d.revtrans,
2143 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2144 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2146 $ref = selectfirst_hashref_query($self, $dbh, $query);
2147 map { $self->{$_} = $ref->{$_} } keys %$ref;
2154 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2155 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2156 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2158 $ref = selectfirst_hashref_query($self, $dbh, $query);
2159 map { $self->{$_} = $ref->{$_} } keys %$ref;
2161 if ($self->{"$self->{vc}_id"}) {
2163 # only setup currency
2164 ($self->{currency}) = split(/:/, $self->{currencies});
2168 $self->lastname_used($dbh, $myconfig, $table, $module);
2170 # get exchangerate for currency
2171 $self->{exchangerate} =
2172 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2180 $main::lxdebug->leave_sub();
2184 $main::lxdebug->enter_sub();
2186 my ($self, $dbh, $myconfig, $table, $module) = @_;
2188 my $arap = ($table eq 'customer') ? "ar" : "ap";
2189 $table = $table eq "customer" ? "customer" : "vendor";
2190 my $where = "1 = 1";
2192 if ($self->{type} =~ /_order/) {
2194 $where = "quotation = '0'";
2196 if ($self->{type} =~ /_quotation/) {
2198 $where = "quotation = '1'";
2201 my $query = qq|SELECT MAX(id) FROM $arap
2202 WHERE $where AND ${table}_id > 0|;
2203 my ($trans_id) = selectrow_query($self, $dbh, $query);
2208 a.curr, a.${table}_id, a.department_id,
2209 d.description AS department,
2210 ct.name, current_date + ct.terms AS duedate
2212 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2213 LEFT JOIN department d ON (a.department_id = d.id)
2215 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2216 $self->{department}, $self->{$table}, $self->{duedate})
2217 = selectrow_query($self, $dbh, $query, $trans_id);
2219 $main::lxdebug->leave_sub();
2223 $main::lxdebug->enter_sub();
2225 my ($self, $myconfig, $thisdate, $days) = @_;
2227 my $dbh = $self->dbconnect($myconfig);
2232 my $dateformat = $myconfig->{dateformat};
2233 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2234 $thisdate = $dbh->quote($thisdate);
2235 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2237 $query = qq|SELECT current_date AS thisdate|;
2240 ($thisdate) = selectrow_query($self, $dbh, $query);
2244 $main::lxdebug->leave_sub();
2250 $main::lxdebug->enter_sub();
2252 my ($self, $string) = @_;
2254 if ($string !~ /%/) {
2255 $string = "%$string%";
2258 $string =~ s/\'/\'\'/g;
2260 $main::lxdebug->leave_sub();
2266 $main::lxdebug->enter_sub();
2268 my ($self, $flds, $new, $count, $numrows) = @_;
2272 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2278 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2280 $j = $item->{ndx} - 1;
2281 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2285 for $i ($count + 1 .. $numrows) {
2286 map { delete $self->{"${_}_$i"} } @{$flds};
2289 $main::lxdebug->leave_sub();
2293 $main::lxdebug->enter_sub();
2295 my ($self, $myconfig) = @_;
2299 my $dbh = $self->dbconnect_noauto($myconfig);
2301 my $query = qq|DELETE FROM status
2302 WHERE (formname = ?) AND (trans_id = ?)|;
2303 my $sth = prepare_query($self, $dbh, $query);
2305 if ($self->{formname} =~ /(check|receipt)/) {
2306 for $i (1 .. $self->{rowcount}) {
2307 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2310 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2314 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2315 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2317 my %queued = split / /, $self->{queued};
2320 if ($self->{formname} =~ /(check|receipt)/) {
2322 # this is a check or receipt, add one entry for each lineitem
2323 my ($accno) = split /--/, $self->{account};
2324 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2325 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2326 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2327 $sth = prepare_query($self, $dbh, $query);
2329 for $i (1 .. $self->{rowcount}) {
2330 if ($self->{"checked_$i"}) {
2331 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2337 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2338 VALUES (?, ?, ?, ?, ?)|;
2339 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2340 $queued{$self->{formname}}, $self->{formname});
2346 $main::lxdebug->leave_sub();
2350 $main::lxdebug->enter_sub();
2352 my ($self, $dbh) = @_;
2354 my ($query, $printed, $emailed);
2356 my $formnames = $self->{printed};
2357 my $emailforms = $self->{emailed};
2359 my $query = qq|DELETE FROM status
2360 WHERE (formname = ?) AND (trans_id = ?)|;
2361 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2363 # this only applies to the forms
2364 # checks and receipts are posted when printed or queued
2366 if ($self->{queued}) {
2367 my %queued = split / /, $self->{queued};
2369 foreach my $formname (keys %queued) {
2370 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2371 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2373 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2374 VALUES (?, ?, ?, ?, ?)|;
2375 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2377 $formnames =~ s/$self->{formname}//;
2378 $emailforms =~ s/$self->{formname}//;
2383 # save printed, emailed info
2384 $formnames =~ s/^ +//g;
2385 $emailforms =~ s/^ +//g;
2388 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2389 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2391 foreach my $formname (keys %status) {
2392 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2393 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2395 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2396 VALUES (?, ?, ?, ?)|;
2397 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2400 $main::lxdebug->leave_sub();
2404 # $main::locale->text('SAVED')
2405 # $main::locale->text('DELETED')
2406 # $main::locale->text('ADDED')
2407 # $main::locale->text('PAYMENT POSTED')
2408 # $main::locale->text('POSTED')
2409 # $main::locale->text('POSTED AS NEW')
2410 # $main::locale->text('ELSE')
2411 # $main::locale->text('SAVED FOR DUNNING')
2412 # $main::locale->text('DUNNING STARTED')
2413 # $main::locale->text('PRINTED')
2414 # $main::locale->text('MAILED')
2415 # $main::locale->text('SCREENED')
2416 # $main::locale->text('CANCELED')
2417 # $main::locale->text('invoice')
2418 # $main::locale->text('proforma')
2419 # $main::locale->text('sales_order')
2420 # $main::locale->text('packing_list')
2421 # $main::locale->text('pick_list')
2422 # $main::locale->text('purchase_order')
2423 # $main::locale->text('bin_list')
2424 # $main::locale->text('sales_quotation')
2425 # $main::locale->text('request_quotation')
2428 $main::lxdebug->enter_sub();
2433 if(!exists $self->{employee_id}) {
2434 &get_employee($self, $dbh);
2438 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2439 qq|VALUES (?, ?, ?, ?, ?)|;
2440 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2441 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2442 do_query($self, $dbh, $query, @values);
2444 $main::lxdebug->leave_sub();
2448 $main::lxdebug->enter_sub();
2452 my $trans_id = shift();
2453 my $restriction = shift();
2456 if ($trans_id ne "") {
2458 qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name, h.snumbers, h.trans_id AS id | .
2459 qq|FROM history_erp h | .
2460 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2461 qq|WHERE trans_id = ? |
2464 my $sth = $dbh->prepare($query) || $self->dberror($query);
2466 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2468 while(my $hash_ref = $sth->fetchrow_hashref()) {
2469 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2470 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2471 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2472 $tempArray[$i++] = $hash_ref;
2474 $main::lxdebug->leave_sub() and return \@tempArray
2475 if ($i > 0 && $tempArray[0] ne "");
2477 $main::lxdebug->leave_sub();
2481 sub update_defaults {
2482 $main::lxdebug->enter_sub();
2484 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2487 if ($provided_dbh) {
2488 $dbh = $provided_dbh;
2490 $dbh = $self->dbconnect_noauto($myconfig);
2492 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2493 my $sth = $dbh->prepare($query);
2495 $sth->execute || $self->dberror($query);
2496 my ($var) = $sth->fetchrow_array;
2499 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2502 $query = qq|UPDATE defaults SET $fld = ?|;
2503 do_query($self, $dbh, $query, $var);
2505 if (!$provided_dbh) {
2510 $main::lxdebug->leave_sub();
2515 sub update_business {
2516 $main::lxdebug->enter_sub();
2518 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2521 if ($provided_dbh) {
2522 $dbh = $provided_dbh;
2524 $dbh = $self->dbconnect_noauto($myconfig);
2527 qq|SELECT customernumberinit FROM business
2528 WHERE id = ? FOR UPDATE|;
2529 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2531 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2533 $query = qq|UPDATE business
2534 SET customernumberinit = ?
2536 do_query($self, $dbh, $query, $var, $business_id);
2538 if (!$provided_dbh) {
2543 $main::lxdebug->leave_sub();
2548 sub get_partsgroup {
2549 $main::lxdebug->enter_sub();
2551 my ($self, $myconfig, $p) = @_;
2553 my $dbh = $self->dbconnect($myconfig);
2555 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2557 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2560 if ($p->{searchitems} eq 'part') {
2561 $query .= qq|WHERE p.inventory_accno_id > 0|;
2563 if ($p->{searchitems} eq 'service') {
2564 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2566 if ($p->{searchitems} eq 'assembly') {
2567 $query .= qq|WHERE p.assembly = '1'|;
2569 if ($p->{searchitems} eq 'labor') {
2570 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2573 $query .= qq|ORDER BY partsgroup|;
2576 $query = qq|SELECT id, partsgroup FROM partsgroup
2577 ORDER BY partsgroup|;
2580 if ($p->{language_code}) {
2581 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2582 t.description AS translation
2584 JOIN parts p ON (p.partsgroup_id = pg.id)
2585 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2586 ORDER BY translation|;
2587 @values = ($p->{language_code});
2590 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2593 $main::lxdebug->leave_sub();
2596 sub get_pricegroup {
2597 $main::lxdebug->enter_sub();
2599 my ($self, $myconfig, $p) = @_;
2601 my $dbh = $self->dbconnect($myconfig);
2603 my $query = qq|SELECT p.id, p.pricegroup
2606 $query .= qq| ORDER BY pricegroup|;
2609 $query = qq|SELECT id, pricegroup FROM pricegroup
2610 ORDER BY pricegroup|;
2613 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2617 $main::lxdebug->leave_sub();
2621 # usage $form->all_years($myconfig, [$dbh])
2622 # return list of all years where bookings found
2625 $main::lxdebug->enter_sub();
2627 my ($self, $myconfig, $dbh) = @_;
2631 $dbh = $self->dbconnect($myconfig);
2636 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2637 (SELECT MAX(transdate) FROM acc_trans)|;
2638 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2640 if ($myconfig->{dateformat} =~ /^yy/) {
2641 ($startdate) = split /\W/, $startdate;
2642 ($enddate) = split /\W/, $enddate;
2644 (@_) = split /\W/, $startdate;
2646 (@_) = split /\W/, $enddate;
2651 $startdate = substr($startdate,0,4);
2652 $enddate = substr($enddate,0,4);
2654 while ($enddate >= $startdate) {
2655 push @all_years, $enddate--;
2658 $dbh->disconnect if $disconnect;
2662 $main::lxdebug->leave_sub();