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 #======================================================================
51 $main::lxdebug->enter_sub(2);
55 my @pairs = split(/&/, $input);
58 my ($name, $value) = split(/=/, $_, 2);
59 $in{$name} = unescape(undef, $value);
62 $main::lxdebug->leave_sub(2);
67 sub _request_to_hash {
68 $main::lxdebug->enter_sub(2);
71 my ($i, $loc, $key, $val);
72 my (%ATTACH, $f, $header, $header_body, $len, $buf);
73 my ($boundary, @list, $size, $body, $x, $blah, $name);
75 if ($ENV{'CONTENT_TYPE'}
76 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
77 $boundary = quotemeta('--' . $1);
78 @list = split(/$boundary/, $input);
80 # For some reason there are always 2 extra, that are empty
83 for ($x = 1; $x <= $size; $x++) {
84 $header_body = $list[$x];
85 $header_body =~ /\r\n\r\n|\n\n/;
87 # Here we split the header and body
92 # Now we try to get the file name
94 $name =~ /name=\"(.+)\"/;
95 ($name, $blah) = split(/\"/, $1);
97 # If the form name is not attach, then we need to parse this like
99 if ($name ne "attach") {
100 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
101 $ATTACH{$name} = $body;
103 # Otherwise it is an attachment and we need to finish it up
104 } elsif ($name eq "attach") {
105 $header =~ /filename=\"(.+)\"/;
106 $ATTACH{'FILE_NAME'} = $1;
107 $ATTACH{'FILE_NAME'} =~ s/\"//g;
108 $ATTACH{'FILE_NAME'} =~ s/\s//g;
109 $ATTACH{'FILE_CONTENT'} = $body;
111 for ($i = $x; $list[$i]; $i++) {
112 $list[$i] =~ s/^.+name=$//;
113 $list[$i] =~ /\"(\w+)\"/;
119 $main::lxdebug->leave_sub(2);
123 $main::lxdebug->leave_sub(2);
124 return _input_to_hash($input);
129 $main::lxdebug->enter_sub();
135 if ($LXDebug::watch_form) {
136 require SL::Watchdog;
137 tie %{ $self }, 'SL::Watchdog';
140 read(STDIN, $_, $ENV{CONTENT_LENGTH});
142 if ($ENV{QUERY_STRING}) {
143 $_ = $ENV{QUERY_STRING};
150 my %parameters = _request_to_hash($_);
151 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
153 $self->{action} = lc $self->{action};
154 $self->{action} =~ s/( |-|,|\#)/_/g;
156 $self->{version} = "2.4.2";
158 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
170 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
172 $main::lxdebug->leave_sub();
176 $main::lxdebug->enter_sub(2);
178 my ($self, $str) = @_;
180 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
182 $main::lxdebug->leave_sub(2);
188 $main::lxdebug->enter_sub(2);
190 my ($self, $str) = @_;
195 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
197 $main::lxdebug->leave_sub(2);
203 my ($self, $str) = @_;
205 if ($str && !ref($str)) {
206 $str =~ s/\"/"/g;
214 my ($self, $str) = @_;
216 if ($str && !ref($str)) {
217 $str =~ s/"/\"/g;
225 $main::lxdebug->enter_sub(2);
227 my ($self, $str) = @_;
230 ('order' => ['"', '<', '>'],
236 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
238 $main::lxdebug->leave_sub(2);
247 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
249 for (sort keys %$self) {
250 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
251 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
258 $main::lxdebug->enter_sub();
260 my ($self, $msg) = @_;
261 if ($ENV{HTTP_USER_AGENT}) {
263 $self->show_generic_error($msg);
267 if ($self->{error_function}) {
268 &{ $self->{error_function} }($msg);
274 $main::lxdebug->leave_sub();
278 $main::lxdebug->enter_sub();
280 my ($self, $msg) = @_;
282 if ($ENV{HTTP_USER_AGENT}) {
285 if (!$self->{header}) {
298 if ($self->{info_function}) {
299 &{ $self->{info_function} }($msg);
305 $main::lxdebug->leave_sub();
309 $main::lxdebug->enter_sub();
311 my ($self, $str, $cols, $maxrows) = @_;
315 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
317 $maxrows = $rows unless defined $maxrows;
319 $main::lxdebug->leave_sub();
321 return ($rows > $maxrows) ? $maxrows : $rows;
325 $main::lxdebug->enter_sub();
327 my ($self, $msg) = @_;
329 $self->error("$msg\n" . $DBI::errstr);
331 $main::lxdebug->leave_sub();
335 $main::lxdebug->enter_sub();
337 my ($self, $name, $msg) = @_;
339 if ($self->{$name} =~ /^\s*$/) {
342 $main::lxdebug->leave_sub();
346 $main::lxdebug->enter_sub();
348 my ($self, $extra_code) = @_;
350 if ($self->{header}) {
351 $main::lxdebug->leave_sub();
355 my ($stylesheet, $favicon, $charset);
357 if ($ENV{HTTP_USER_AGENT}) {
359 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
361 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
365 $self->{favicon} = "favicon.ico" unless $self->{favicon};
367 if ($self->{favicon} && (-f "$self->{favicon}")) {
369 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
373 if ($self->{charset}) {
375 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{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=$self->{charset};
411 <title>$self->{titlebar}</title>
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 charset);
807 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
808 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
809 $mail->{fileid} = "$fileid.";
810 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
812 # if we send html or plain text inline
813 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
814 $mail->{contenttype} = "text/html";
816 $mail->{message} =~ s/\r\n/<br>\n/g;
817 $myconfig->{signature} =~ s/\\n/<br>\n/g;
818 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
820 open(IN, $self->{tmpfile})
821 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
823 $mail->{message} .= $_;
830 if (!$self->{"do_not_attach"}) {
831 @{ $mail->{attachments} } =
832 ({ "filename" => $self->{"tmpfile"},
833 "name" => $self->{"attachment_filename"} ?
834 $self->{"attachment_filename"} : $self->{"tmpfile"} });
837 $mail->{message} =~ s/\r\n/\n/g;
838 $myconfig->{signature} =~ s/\\n/\n/g;
839 $mail->{message} .= "\n-- \n$myconfig->{signature}";
843 my $err = $mail->send($out);
844 $self->error($self->cleanup . "$err") if ($err);
850 my $numbytes = (-s $self->{tmpfile});
851 open(IN, $self->{tmpfile})
852 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
854 $self->{copies} = 1 unless $self->{media} eq 'printer';
856 chdir("$self->{cwd}");
857 #print(STDERR "Kopien $self->{copies}\n");
858 #print(STDERR "OUT $self->{OUT}\n");
859 for my $i (1 .. $self->{copies}) {
861 open(OUT, $self->{OUT})
862 or $self->error($self->cleanup . "$self->{OUT} : $!");
864 $self->{attachment_filename} = $self->generate_attachment_filename();
867 print qq|Content-Type: | . $template->get_mime_type() . qq|
868 Content-Disposition: attachment; filename="$self->{attachment_filename}"
869 Content-Length: $numbytes
873 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
893 chdir("$self->{cwd}");
894 $main::lxdebug->leave_sub();
897 sub generate_attachment_filename {
900 my %formname_translations = (
901 bin_list => $main::locale->text('Bin List'),
902 credit_note => $main::locale->text('Credit Note'),
903 invoice => $main::locale->text('Invoice'),
904 packing_list => $main::locale->text('Packing List'),
905 pick_list => $main::locale->text('Pick List'),
906 proforma => $main::locale->text('Proforma Invoice'),
907 purchase_order => $main::locale->text('Purchase Order'),
908 request_quotation => $main::locale->text('RFQ'),
909 sales_order => $main::locale->text('Confirmation'),
910 sales_quotation => $main::locale->text('Quotation'),
911 storno_invoice => $main::locale->text('Storno Invoice'),
912 storno_packing_list => $main::locale->text('Storno Packing List'),
915 my $attachment_filename = $formname_translations{$self->{"formname"}};
917 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
918 : ($self->{"type"} =~ /_quotation$/) ? "quo"
921 if ($attachment_filename && $self->{"${prefix}number"}) {
922 $attachment_filename .= "_" . $self->{"${prefix}number"}
923 . ( $self->{format} =~ /pdf/i ? ".pdf"
924 : $self->{format} =~ /postscript/i ? ".ps"
925 : $self->{format} =~ /opendocument/i ? ".odt"
926 : $self->{format} =~ /html/i ? ".html"
928 $attachment_filename =~ s/ /_/g;
929 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
930 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
931 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
933 $attachment_filename = "";
936 return $attachment_filename;
940 $main::lxdebug->enter_sub();
944 chdir("$self->{tmpdir}");
947 if (-f "$self->{tmpfile}.err") {
948 open(FH, "$self->{tmpfile}.err");
953 if ($self->{tmpfile}) {
954 $self->{tmpfile} =~ s|.*/||g;
956 $self->{tmpfile} =~ s/\.\w+$//g;
957 my $tmpfile = $self->{tmpfile};
958 unlink(<$tmpfile.*>);
961 chdir("$self->{cwd}");
963 $main::lxdebug->leave_sub();
969 $main::lxdebug->enter_sub();
971 my ($self, $date, $myconfig) = @_;
973 if ($date && $date =~ /\D/) {
975 if ($myconfig->{dateformat} =~ /^yy/) {
976 ($yy, $mm, $dd) = split /\D/, $date;
978 if ($myconfig->{dateformat} =~ /^mm/) {
979 ($mm, $dd, $yy) = split /\D/, $date;
981 if ($myconfig->{dateformat} =~ /^dd/) {
982 ($dd, $mm, $yy) = split /\D/, $date;
987 $yy = ($yy < 70) ? $yy + 2000 : $yy;
988 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
990 $dd = "0$dd" if ($dd < 10);
991 $mm = "0$mm" if ($mm < 10);
996 $main::lxdebug->leave_sub();
1001 # Database routines used throughout
1004 $main::lxdebug->enter_sub(2);
1006 my ($self, $myconfig) = @_;
1008 # connect to database
1010 DBI->connect($myconfig->{dbconnect},
1011 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1015 if ($myconfig->{dboptions}) {
1016 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1019 $main::lxdebug->leave_sub(2);
1024 sub dbconnect_noauto {
1025 $main::lxdebug->enter_sub();
1027 my ($self, $myconfig) = @_;
1029 # connect to database
1031 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1032 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1036 if ($myconfig->{dboptions}) {
1037 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1040 $main::lxdebug->leave_sub();
1045 sub update_balance {
1046 $main::lxdebug->enter_sub();
1048 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1050 # if we have a value, go do it
1053 # retrieve balance from table
1054 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1055 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1056 my ($balance) = $sth->fetchrow_array;
1062 $query = "UPDATE $table SET $field = $balance WHERE $where";
1063 do_query($self, $dbh, $query, @values);
1065 $main::lxdebug->leave_sub();
1068 sub update_exchangerate {
1069 $main::lxdebug->enter_sub();
1071 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1073 # some sanity check for currency
1075 $main::lxdebug->leave_sub();
1079 my $query = qq|SELECT e.curr FROM exchangerate e
1080 WHERE e.curr = ? AND e.transdate = ?
1082 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1085 if ($buy != 0 && $sell != 0) {
1086 $set = "buy = $buy, sell = $sell";
1087 } elsif ($buy != 0) {
1088 $set = "buy = $buy";
1089 } elsif ($sell != 0) {
1090 $set = "sell = $sell";
1093 if ($sth->fetchrow_array) {
1094 $query = qq|UPDATE exchangerate
1099 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1100 VALUES (?, $buy, $sell, ?)|;
1103 do_query($self, $dbh, $query, $curr, $transdate);
1105 $main::lxdebug->leave_sub();
1108 sub save_exchangerate {
1109 $main::lxdebug->enter_sub();
1111 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1113 my $dbh = $self->dbconnect($myconfig);
1115 my ($buy, $sell) = (0, 0);
1116 $buy = $rate if $fld eq 'buy';
1117 $sell = $rate if $fld eq 'sell';
1119 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1123 $main::lxdebug->leave_sub();
1126 sub get_exchangerate {
1127 $main::lxdebug->enter_sub();
1129 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1131 unless ($transdate) {
1132 $main::lxdebug->leave_sub();
1136 my $query = qq|SELECT e.$fld FROM exchangerate e
1137 WHERE e.curr = ? AND e.transdate = ?|;
1138 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1140 if (!$exchangerate) {
1144 $main::lxdebug->leave_sub();
1146 return $exchangerate;
1149 sub check_exchangerate {
1150 $main::lxdebug->enter_sub();
1152 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1154 unless ($transdate) {
1155 $main::lxdebug->leave_sub();
1159 my $dbh = $self->dbconnect($myconfig);
1161 my $query = qq|SELECT e.$fld FROM exchangerate e
1162 WHERE e.curr = ? AND e.transdate = ?|;
1163 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1166 $main::lxdebug->leave_sub();
1168 return $exchangerate;
1171 sub set_payment_options {
1172 $main::lxdebug->enter_sub();
1174 my ($self, $myconfig, $transdate) = @_;
1176 if ($self->{payment_id}) {
1178 my $dbh = $self->dbconnect($myconfig);
1181 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1182 qq|FROM payment_terms p | .
1185 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1186 $self->{payment_terms}) =
1187 selectrow_query($self, $dbh, $query, $self->{payment_id});
1189 if ($transdate eq "") {
1190 if ($self->{invdate}) {
1191 $transdate = $self->{invdate};
1193 $transdate = $self->{transdate};
1198 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1199 qq|FROM payment_terms|;
1200 ($self->{netto_date}, $self->{skonto_date}) =
1201 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1203 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1204 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1205 $self->{percent_skonto};
1207 $self->{skonto_amount} =
1208 $self->format_amount($myconfig, $skonto_amount, 2);
1210 if ($self->{"language_id"}) {
1212 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1213 qq|FROM translation_payment_terms t | .
1214 qq|LEFT JOIN language l ON t.language_id = l.id | .
1215 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1216 my ($description_long, $output_numberformat, $output_dateformat,
1217 $output_longdates) =
1218 selectrow_query($self, $dbh, $query,
1219 $self->{"language_id"}, $self->{"payment_id"});
1221 $self->{payment_terms} = $description_long if ($description_long);
1223 if ($output_dateformat) {
1224 foreach my $key (qw(netto_date skonto_date)) {
1226 $main::locale->reformat_date($myconfig, $self->{$key},
1232 if ($output_numberformat &&
1233 ($output_numberformat ne $myconfig->{"numberformat"})) {
1234 my $saved_numberformat = $myconfig->{"numberformat"};
1235 $myconfig->{"numberformat"} = $output_numberformat;
1236 $self->{skonto_amount} =
1237 $self->format_amount($myconfig, $skonto_amount, 2);
1238 $myconfig->{"numberformat"} = $saved_numberformat;
1242 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1243 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1244 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1245 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1246 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1247 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1248 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1249 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1250 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1251 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1256 $main::lxdebug->leave_sub();
1260 sub get_template_language {
1261 $main::lxdebug->enter_sub();
1263 my ($self, $myconfig) = @_;
1265 my $template_code = "";
1267 if ($self->{language_id}) {
1268 my $dbh = $self->dbconnect($myconfig);
1269 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1270 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1274 $main::lxdebug->leave_sub();
1276 return $template_code;
1279 sub get_printer_code {
1280 $main::lxdebug->enter_sub();
1282 my ($self, $myconfig) = @_;
1284 my $template_code = "";
1286 if ($self->{printer_id}) {
1287 my $dbh = $self->dbconnect($myconfig);
1288 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1289 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1293 $main::lxdebug->leave_sub();
1295 return $template_code;
1299 $main::lxdebug->enter_sub();
1301 my ($self, $myconfig) = @_;
1303 my $template_code = "";
1305 if ($self->{shipto_id}) {
1306 my $dbh = $self->dbconnect($myconfig);
1307 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1308 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1309 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1313 $main::lxdebug->leave_sub();
1317 $main::lxdebug->enter_sub();
1319 my ($self, $dbh, $id, $module) = @_;
1323 foreach my $item (qw(name department_1 department_2 street zipcode city country
1324 contact phone fax email)) {
1325 if ($self->{"shipto$item"}) {
1326 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1328 push(@values, $self->{"shipto${item}"});
1331 if ($self->{shipto_id}) {
1332 my $query = qq|UPDATE shipto set
1334 shiptodepartment_1 = ?,
1335 shiptodepartment_2 = ?,
1344 WHERE shipto_id = ?|;
1345 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1347 my $query = qq|SELECT * FROM shipto
1348 WHERE shiptoname = ? AND
1349 shiptodepartment_1 = ? AND
1350 shiptodepartment_2 = ? AND
1351 shiptostreet = ? AND
1352 shiptozipcode = ? AND
1354 shiptocountry = ? AND
1355 shiptocontact = ? AND
1359 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1362 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1363 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1364 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1365 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1366 do_query($self, $dbh, $query, $id, @values, $module);
1371 $main::lxdebug->leave_sub();
1375 $main::lxdebug->enter_sub();
1377 my ($self, $dbh) = @_;
1379 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1380 ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
1381 $self->{employee_id} *= 1;
1383 $main::lxdebug->leave_sub();
1387 $main::lxdebug->enter_sub();
1389 my ($self, $myconfig, $salesman_id) = @_;
1391 $main::lxdebug->leave_sub() and return unless $salesman_id;
1393 my $dbh = $self->dbconnect($myconfig);
1396 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1400 my $user = new User($main::memberfile, $login);
1401 map({ $self->{"salesman_$_"} = $user->{$_}; }
1402 qw(address businessnumber co_ustid company duns email fax name
1404 $self->{salesman_login} = $login;
1406 $self->{salesman_name} = $login
1407 if ($self->{salesman_name} eq "");
1409 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1414 $main::lxdebug->leave_sub();
1418 $main::lxdebug->enter_sub();
1420 my ($self, $myconfig) = @_;
1422 my $dbh = $self->dbconnect($myconfig);
1423 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1424 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1427 $main::lxdebug->leave_sub();
1431 $main::lxdebug->enter_sub();
1433 my ($self, $dbh, $id, $key) = @_;
1435 $key = "all_contacts" unless ($key);
1438 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1439 qq|FROM contacts | .
1440 qq|WHERE cp_cv_id = ? | .
1441 qq|ORDER BY lower(cp_name)|;
1443 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1445 $main::lxdebug->leave_sub();
1449 $main::lxdebug->enter_sub();
1451 my ($self, $dbh, $key) = @_;
1453 my ($all, $old_id, $where, @values);
1455 if (ref($key) eq "HASH") {
1458 $key = "ALL_PROJECTS";
1460 foreach my $p (keys(%{$params})) {
1462 $all = $params->{$p};
1463 } elsif ($p eq "old_id") {
1464 $old_id = $params->{$p};
1465 } elsif ($p eq "key") {
1466 $key = $params->{$p};
1472 $where = "WHERE active ";
1474 if (ref($old_id) eq "ARRAY") {
1475 my @ids = grep({ $_ } @{$old_id});
1477 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1478 push(@values, @ids);
1481 $where .= " OR (id = ?) ";
1482 push(@values, $old_id);
1488 qq|SELECT id, projectnumber, description, active | .
1491 qq|ORDER BY lower(projectnumber)|;
1493 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1495 $main::lxdebug->leave_sub();
1499 $main::lxdebug->enter_sub();
1501 my ($self, $dbh, $vc_id, $key) = @_;
1503 $key = "all_shipto" unless ($key);
1505 # get shipping addresses
1507 qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
1509 qq|WHERE trans_id = ?|;
1511 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1513 $main::lxdebug->leave_sub();
1517 $main::lxdebug->enter_sub();
1519 my ($self, $dbh, $key) = @_;
1521 $key = "all_printers" unless ($key);
1523 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1525 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1527 $main::lxdebug->leave_sub();
1531 $main::lxdebug->enter_sub();
1533 my ($self, $dbh, $params) = @_;
1535 $key = $params->{key};
1536 $key = "all_charts" unless ($key);
1538 my $transdate = quote_db_date($params->{transdate});
1541 qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1543 qq|LEFT JOIN taxkeys tk ON | .
1544 qq|(tk.id = (SELECT id FROM taxkeys | .
1545 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1546 qq| ORDER BY startdate DESC LIMIT 1)) | .
1547 qq|ORDER BY c.accno|;
1549 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1551 $main::lxdebug->leave_sub();
1554 sub _get_taxcharts {
1555 $main::lxdebug->enter_sub();
1557 my ($self, $dbh, $key) = @_;
1559 $key = "all_taxcharts" unless ($key);
1561 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1563 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1565 $main::lxdebug->leave_sub();
1569 $main::lxdebug->enter_sub();
1571 my ($self, $dbh, $key) = @_;
1573 $key = "all_taxzones" unless ($key);
1575 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1577 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1579 $main::lxdebug->leave_sub();
1582 sub _get_employees {
1583 $main::lxdebug->enter_sub();
1585 my ($self, $dbh, $key) = @_;
1587 $key = "all_employees" unless ($key);
1589 selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
1591 $main::lxdebug->leave_sub();
1594 sub _get_business_types {
1595 $main::lxdebug->enter_sub();
1597 my ($self, $dbh, $key) = @_;
1599 $key = "all_business_types" unless ($key);
1601 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1603 $main::lxdebug->leave_sub();
1606 sub _get_languages {
1607 $main::lxdebug->enter_sub();
1609 my ($self, $dbh, $key) = @_;
1611 $key = "all_languages" unless ($key);
1613 my $query = qq|SELECT * FROM language ORDER BY id|;
1615 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1617 $main::lxdebug->leave_sub();
1620 sub _get_dunning_configs {
1621 $main::lxdebug->enter_sub();
1623 my ($self, $dbh, $key) = @_;
1625 $key = "all_dunning_configs" unless ($key);
1627 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1629 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1631 $main::lxdebug->leave_sub();
1634 sub _get_currencies {
1635 $main::lxdebug->enter_sub();
1637 my ($self, $dbh, $key) = @_;
1639 $key = "all_currencies" unless ($key);
1641 my $query = qq|SELECT curr AS currency FROM defaults|;
1643 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1645 $main::lxdebug->leave_sub();
1648 sub _get_customers {
1649 $main::lxdebug->enter_sub();
1651 my ($self, $dbh, $key) = @_;
1653 $key = "all_customers" unless ($key);
1655 my $query = qq|SELECT * FROM customer LIMIT $main::myconfig{vclimit}|;
1657 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1659 $main::lxdebug->leave_sub();
1663 $main::lxdebug->enter_sub();
1665 my ($self, $dbh, $key) = @_;
1667 $key = "all_vendors" unless ($key);
1669 my $query = qq|SELECT * FROM vendor|; # LIMIT $main::myconfig{vclimit}|;
1671 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1673 $main::lxdebug->leave_sub();
1677 $main::lxdebug->enter_sub();
1682 my $dbh = $self->dbconnect(\%main::myconfig);
1683 my ($sth, $query, $ref);
1685 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1686 my $vc_id = $self->{"${vc}_id"};
1688 if ($params{"contacts"}) {
1689 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1692 if ($params{"shipto"}) {
1693 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1696 if ($params{"projects"} || $params{"all_projects"}) {
1697 $self->_get_projects($dbh, $params{"all_projects"} ?
1698 $params{"all_projects"} : $params{"projects"},
1699 $params{"all_projects"} ? 1 : 0);
1702 if ($params{"printers"}) {
1703 $self->_get_printers($dbh, $params{"printers"});
1706 if ($params{"languages"}) {
1707 $self->_get_languages($dbh, $params{"languages"});
1710 if ($params{"charts"}) {
1711 $self->_get_charts($dbh, $params{"charts"});
1714 if ($params{"taxcharts"}) {
1715 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1718 if ($params{"taxzones"}) {
1719 $self->_get_taxzones($dbh, $params{"taxzones"});
1722 if ($params{"employees"}) {
1723 $self->_get_employees($dbh, $params{"employees"});
1726 if ($params{"business_types"}) {
1727 $self->_get_business_types($dbh, $params{"business_types"});
1730 if ($params{"dunning_configs"}) {
1731 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1734 if($params{"currencies"}) {
1735 $self->_get_currencies($dbh, $params{"currencies"});
1738 if($params{"customers"}) {
1739 $self->_get_customers($dbh, $params{"customers"});
1742 if($params{"vendors"}) {
1743 $self->_get_vendors($dbh, $params{"vendors"});
1748 $main::lxdebug->leave_sub();
1751 # this sub gets the id and name from $table
1753 $main::lxdebug->enter_sub();
1755 my ($self, $myconfig, $table) = @_;
1757 # connect to database
1758 my $dbh = $self->dbconnect($myconfig);
1760 $table = $table eq "customer" ? "customer" : "vendor";
1761 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1763 my ($query, @values);
1765 if (!$self->{openinvoices}) {
1767 if ($self->{customernumber} ne "") {
1768 $where = qq|(vc.customernumber ILIKE ?)|;
1769 push(@values, '%' . $self->{customernumber} . '%');
1771 $where = qq|(vc.name ILIKE ?)|;
1772 push(@values, '%' . $self->{$table} . '%');
1776 qq~SELECT vc.id, vc.name,
1777 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1779 WHERE $where AND (NOT vc.obsolete)
1783 qq~SELECT DISTINCT vc.id, vc.name,
1784 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1786 JOIN $table vc ON (a.${table}_id = vc.id)
1787 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1789 push(@values, '%' . $self->{$table} . '%');
1792 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1794 $main::lxdebug->leave_sub();
1796 return scalar(@{ $self->{name_list} });
1799 # the selection sub is used in the AR, AP, IS, IR and OE module
1802 $main::lxdebug->enter_sub();
1804 my ($self, $myconfig, $table, $module) = @_;
1807 my $dbh = $self->dbconnect($myconfig);
1809 $table = $table eq "customer" ? "customer" : "vendor";
1811 my $query = qq|SELECT count(*) FROM $table|;
1812 my ($count) = selectrow_query($self, $dbh, $query);
1814 # build selection list
1815 if ($count < $myconfig->{vclimit}) {
1816 $query = qq|SELECT id, name, salesman_id
1817 FROM $table WHERE NOT obsolete
1819 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1823 $self->get_employee($dbh);
1825 # setup sales contacts
1826 $query = qq|SELECT e.id, e.name
1828 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1829 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1832 push(@{ $self->{all_employees} },
1833 { id => $self->{employee_id},
1834 name => $self->{employee} });
1836 # sort the whole thing
1837 @{ $self->{all_employees} } =
1838 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1840 if ($module eq 'AR') {
1842 # prepare query for departments
1843 $query = qq|SELECT id, description
1846 ORDER BY description|;
1849 $query = qq|SELECT id, description
1851 ORDER BY description|;
1854 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1857 $query = qq|SELECT id, description
1861 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1864 $query = qq|SELECT printer_description, id
1866 ORDER BY printer_description|;
1868 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1871 $query = qq|SELECT id, description
1875 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1879 $main::lxdebug->leave_sub();
1882 sub language_payment {
1883 $main::lxdebug->enter_sub();
1885 my ($self, $myconfig) = @_;
1887 my $dbh = $self->dbconnect($myconfig);
1889 my $query = qq|SELECT id, description
1893 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1896 $query = qq|SELECT printer_description, id
1898 ORDER BY printer_description|;
1900 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1903 $query = qq|SELECT id, description
1907 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1909 # get buchungsgruppen
1910 $query = qq|SELECT id, description
1911 FROM buchungsgruppen|;
1913 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1916 $main::lxdebug->leave_sub();
1919 # this is only used for reports
1920 sub all_departments {
1921 $main::lxdebug->enter_sub();
1923 my ($self, $myconfig, $table) = @_;
1925 my $dbh = $self->dbconnect($myconfig);
1928 if ($table eq 'customer') {
1929 $where = "WHERE role = 'P' ";
1932 my $query = qq|SELECT id, description
1935 ORDER BY description|;
1936 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1938 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1942 $main::lxdebug->leave_sub();
1946 $main::lxdebug->enter_sub();
1948 my ($self, $module, $myconfig, $table) = @_;
1951 if ($table eq "customer") {
1960 $self->all_vc($myconfig, $table, $module);
1962 # get last customers or vendors
1963 my ($query, $sth, $ref);
1965 my $dbh = $self->dbconnect($myconfig);
1970 my $transdate = "current_date";
1971 if ($self->{transdate}) {
1972 $transdate = $dbh->quote($self->{transdate});
1975 # now get the account numbers
1976 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1977 FROM chart c, taxkeys tk
1978 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1979 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1982 $sth = $dbh->prepare($query);
1984 do_statement($self, $sth, $query, '%' . $module . '%');
1986 $self->{accounts} = "";
1987 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1989 foreach my $key (split(/:/, $ref->{link})) {
1990 if ($key =~ /$module/) {
1992 # cross reference for keys
1993 $xkeyref{ $ref->{accno} } = $key;
1995 push @{ $self->{"${module}_links"}{$key} },
1996 { accno => $ref->{accno},
1997 description => $ref->{description},
1998 taxkey => $ref->{taxkey_id},
1999 tax_id => $ref->{tax_id} };
2001 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2007 # get taxkeys and description
2008 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2009 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2011 if (($module eq "AP") || ($module eq "AR")) {
2012 # get tax rates and description
2013 $query = qq|SELECT * FROM tax|;
2014 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2020 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2021 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2022 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2023 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2025 d.description AS department,
2028 JOIN $table c ON (a.${table}_id = c.id)
2029 LEFT JOIN employee e ON (e.id = a.employee_id)
2030 LEFT JOIN department d ON (d.id = a.department_id)
2032 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2034 foreach $key (keys %$ref) {
2035 $self->{$key} = $ref->{$key};
2038 my $transdate = "current_date";
2039 if ($self->{transdate}) {
2040 $transdate = $dbh->quote($self->{transdate});
2043 # now get the account numbers
2044 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2046 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2048 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2049 OR c.link LIKE '%_tax%')
2052 $sth = $dbh->prepare($query);
2053 do_statement($self, $sth, $query, "%$module%");
2055 $self->{accounts} = "";
2056 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2058 foreach my $key (split(/:/, $ref->{link})) {
2059 if ($key =~ /$module/) {
2061 # cross reference for keys
2062 $xkeyref{ $ref->{accno} } = $key;
2064 push @{ $self->{"${module}_links"}{$key} },
2065 { accno => $ref->{accno},
2066 description => $ref->{description},
2067 taxkey => $ref->{taxkey_id},
2068 tax_id => $ref->{tax_id} };
2070 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2076 # get amounts from individual entries
2079 c.accno, c.description,
2080 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2084 LEFT JOIN chart c ON (c.id = a.chart_id)
2085 LEFT JOIN project p ON (p.id = a.project_id)
2086 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2087 WHERE (tk.taxkey_id=a.taxkey) AND
2088 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2089 THEN tk.chart_id = a.chart_id
2092 OR (c.link='%tax%')) AND
2093 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2094 WHERE a.trans_id = ?
2095 AND a.fx_transaction = '0'
2096 ORDER BY a.oid, a.transdate|;
2097 $sth = $dbh->prepare($query);
2098 do_statement($self, $sth, $query, $self->{id});
2100 # get exchangerate for currency
2101 $self->{exchangerate} =
2102 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2105 # store amounts in {acc_trans}{$key} for multiple accounts
2106 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2107 $ref->{exchangerate} =
2108 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2109 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2112 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2113 $ref->{amount} *= -1;
2115 $ref->{index} = $index;
2117 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2123 d.curr AS currencies, d.closedto, d.revtrans,
2124 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2125 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2127 $ref = selectfirst_hashref_query($self, $dbh, $query);
2128 map { $self->{$_} = $ref->{$_} } keys %$ref;
2135 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2136 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2137 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2139 $ref = selectfirst_hashref_query($self, $dbh, $query);
2140 map { $self->{$_} = $ref->{$_} } keys %$ref;
2142 if ($self->{"$self->{vc}_id"}) {
2144 # only setup currency
2145 ($self->{currency}) = split(/:/, $self->{currencies});
2149 $self->lastname_used($dbh, $myconfig, $table, $module);
2151 # get exchangerate for currency
2152 $self->{exchangerate} =
2153 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2161 $main::lxdebug->leave_sub();
2165 $main::lxdebug->enter_sub();
2167 my ($self, $dbh, $myconfig, $table, $module) = @_;
2169 my $arap = ($table eq 'customer') ? "ar" : "ap";
2170 $table = $table eq "customer" ? "customer" : "vendor";
2171 my $where = "1 = 1";
2173 if ($self->{type} =~ /_order/) {
2175 $where = "quotation = '0'";
2177 if ($self->{type} =~ /_quotation/) {
2179 $where = "quotation = '1'";
2182 my $query = qq|SELECT MAX(id) FROM $arap
2183 WHERE $where AND ${table}_id > 0|;
2184 my ($trans_id) = selectrow_query($self, $dbh, $query);
2189 a.curr, a.${table}_id, a.department_id,
2190 d.description AS department,
2191 ct.name, current_date + ct.terms AS duedate
2193 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2194 LEFT JOIN department d ON (a.department_id = d.id)
2196 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2197 $self->{department}, $self->{$table}, $self->{duedate})
2198 = selectrow_query($self, $dbh, $query, $trans_id);
2200 $main::lxdebug->leave_sub();
2204 $main::lxdebug->enter_sub();
2206 my ($self, $myconfig, $thisdate, $days) = @_;
2208 my $dbh = $self->dbconnect($myconfig);
2213 my $dateformat = $myconfig->{dateformat};
2214 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2215 $thisdate = $dbh->quote($thisdate);
2216 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2218 $query = qq|SELECT current_date AS thisdate|;
2221 ($thisdate) = selectrow_query($self, $dbh, $query);
2225 $main::lxdebug->leave_sub();
2231 $main::lxdebug->enter_sub();
2233 my ($self, $string) = @_;
2235 if ($string !~ /%/) {
2236 $string = "%$string%";
2239 $string =~ s/\'/\'\'/g;
2241 $main::lxdebug->leave_sub();
2247 $main::lxdebug->enter_sub();
2249 my ($self, $flds, $new, $count, $numrows) = @_;
2253 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2259 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2261 $j = $item->{ndx} - 1;
2262 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2266 for $i ($count + 1 .. $numrows) {
2267 map { delete $self->{"${_}_$i"} } @{$flds};
2270 $main::lxdebug->leave_sub();
2274 $main::lxdebug->enter_sub();
2276 my ($self, $myconfig) = @_;
2280 my $dbh = $self->dbconnect_noauto($myconfig);
2282 my $query = qq|DELETE FROM status
2283 WHERE (formname = ?) AND (trans_id = ?)|;
2284 my $sth = prepare_query($self, $dbh, $query);
2286 if ($self->{formname} =~ /(check|receipt)/) {
2287 for $i (1 .. $self->{rowcount}) {
2288 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2291 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2295 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2296 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2298 my %queued = split / /, $self->{queued};
2301 if ($self->{formname} =~ /(check|receipt)/) {
2303 # this is a check or receipt, add one entry for each lineitem
2304 my ($accno) = split /--/, $self->{account};
2305 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2306 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2307 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2308 $sth = prepare_query($self, $dbh, $query);
2310 for $i (1 .. $self->{rowcount}) {
2311 if ($self->{"checked_$i"}) {
2312 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2318 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2319 VALUES (?, ?, ?, ?, ?)|;
2320 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2321 $queued{$self->{formname}}, $self->{formname});
2327 $main::lxdebug->leave_sub();
2331 $main::lxdebug->enter_sub();
2333 my ($self, $dbh) = @_;
2335 my ($query, $printed, $emailed);
2337 my $formnames = $self->{printed};
2338 my $emailforms = $self->{emailed};
2340 my $query = qq|DELETE FROM status
2341 WHERE (formname = ?) AND (trans_id = ?)|;
2342 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2344 # this only applies to the forms
2345 # checks and receipts are posted when printed or queued
2347 if ($self->{queued}) {
2348 my %queued = split / /, $self->{queued};
2350 foreach my $formname (keys %queued) {
2351 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2352 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2354 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2355 VALUES (?, ?, ?, ?, ?)|;
2356 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2358 $formnames =~ s/$self->{formname}//;
2359 $emailforms =~ s/$self->{formname}//;
2364 # save printed, emailed info
2365 $formnames =~ s/^ +//g;
2366 $emailforms =~ s/^ +//g;
2369 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2370 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2372 foreach my $formname (keys %status) {
2373 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2374 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2376 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2377 VALUES (?, ?, ?, ?)|;
2378 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2381 $main::lxdebug->leave_sub();
2385 # $main::locale->text('SAVED')
2386 # $main::locale->text('DELETED')
2387 # $main::locale->text('ADDED')
2388 # $main::locale->text('PAYMENT POSTED')
2389 # $main::locale->text('POSTED')
2390 # $main::locale->text('POSTED AS NEW')
2391 # $main::locale->text('ELSE')
2392 # $main::locale->text('SAVED FOR DUNNING')
2393 # $main::locale->text('DUNNING STARTED')
2394 # $main::locale->text('PRINTED')
2395 # $main::locale->text('MAILED')
2396 # $main::locale->text('SCREENED')
2397 # $main::locale->text('CANCELED')
2398 # $main::locale->text('invoice')
2399 # $main::locale->text('proforma')
2400 # $main::locale->text('sales_order')
2401 # $main::locale->text('packing_list')
2402 # $main::locale->text('pick_list')
2403 # $main::locale->text('purchase_order')
2404 # $main::locale->text('bin_list')
2405 # $main::locale->text('sales_quotation')
2406 # $main::locale->text('request_quotation')
2409 $main::lxdebug->enter_sub();
2414 if(!exists $self->{employee_id}) {
2415 &get_employee($self, $dbh);
2419 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2420 qq|VALUES (?, ?, ?, ?, ?)|;
2421 my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2422 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2423 do_query($self, $dbh, $query, @values);
2425 $main::lxdebug->leave_sub();
2429 $main::lxdebug->enter_sub();
2433 my $trans_id = shift();
2434 my $restriction = shift();
2437 if ($trans_id ne "") {
2439 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 | .
2440 qq|FROM history_erp h | .
2441 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2442 qq|WHERE trans_id = ? |
2445 my $sth = $dbh->prepare($query) || $self->dberror($query);
2447 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2449 while(my $hash_ref = $sth->fetchrow_hashref()) {
2450 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2451 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2452 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2453 $tempArray[$i++] = $hash_ref;
2455 $main::lxdebug->leave_sub() and return \@tempArray
2456 if ($i > 0 && $tempArray[0] ne "");
2458 $main::lxdebug->leave_sub();
2462 sub update_defaults {
2463 $main::lxdebug->enter_sub();
2465 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2468 if ($provided_dbh) {
2469 $dbh = $provided_dbh;
2471 $dbh = $self->dbconnect_noauto($myconfig);
2473 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2474 my $sth = $dbh->prepare($query);
2476 $sth->execute || $self->dberror($query);
2477 my ($var) = $sth->fetchrow_array;
2480 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2483 $query = qq|UPDATE defaults SET $fld = ?|;
2484 do_query($self, $dbh, $query, $var);
2486 if (!$provided_dbh) {
2491 $main::lxdebug->leave_sub();
2496 sub update_business {
2497 $main::lxdebug->enter_sub();
2499 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2502 if ($provided_dbh) {
2503 $dbh = $provided_dbh;
2505 $dbh = $self->dbconnect_noauto($myconfig);
2508 qq|SELECT customernumberinit FROM business
2509 WHERE id = ? FOR UPDATE|;
2510 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2512 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2514 $query = qq|UPDATE business
2515 SET customernumberinit = ?
2517 do_query($self, $dbh, $query, $var, $business_id);
2519 if (!$provided_dbh) {
2524 $main::lxdebug->leave_sub();
2529 sub get_partsgroup {
2530 $main::lxdebug->enter_sub();
2532 my ($self, $myconfig, $p) = @_;
2534 my $dbh = $self->dbconnect($myconfig);
2536 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2538 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2541 if ($p->{searchitems} eq 'part') {
2542 $query .= qq|WHERE p.inventory_accno_id > 0|;
2544 if ($p->{searchitems} eq 'service') {
2545 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2547 if ($p->{searchitems} eq 'assembly') {
2548 $query .= qq|WHERE p.assembly = '1'|;
2550 if ($p->{searchitems} eq 'labor') {
2551 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2554 $query .= qq|ORDER BY partsgroup|;
2557 $query = qq|SELECT id, partsgroup FROM partsgroup
2558 ORDER BY partsgroup|;
2561 if ($p->{language_code}) {
2562 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2563 t.description AS translation
2565 JOIN parts p ON (p.partsgroup_id = pg.id)
2566 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2567 ORDER BY translation|;
2568 @values = ($p->{language_code});
2571 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2574 $main::lxdebug->leave_sub();
2577 sub get_pricegroup {
2578 $main::lxdebug->enter_sub();
2580 my ($self, $myconfig, $p) = @_;
2582 my $dbh = $self->dbconnect($myconfig);
2584 my $query = qq|SELECT p.id, p.pricegroup
2587 $query .= qq| ORDER BY pricegroup|;
2590 $query = qq|SELECT id, pricegroup FROM pricegroup
2591 ORDER BY pricegroup|;
2594 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2598 $main::lxdebug->leave_sub();
2602 # usage $form->all_years($myconfig, [$dbh])
2603 # return list of all years where bookings found
2606 $main::lxdebug->enter_sub();
2608 my ($self, $myconfig, $dbh) = @_;
2612 $dbh = $self->dbconnect($myconfig);
2617 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2618 (SELECT MAX(transdate) FROM acc_trans)|;
2619 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2621 if ($myconfig->{dateformat} =~ /^yy/) {
2622 ($startdate) = split /\W/, $startdate;
2623 ($enddate) = split /\W/, $enddate;
2625 (@_) = split /\W/, $startdate;
2627 (@_) = split /\W/, $enddate;
2632 $startdate = substr($startdate,0,4);
2633 $enddate = substr($enddate,0,4);
2635 while ($enddate >= $startdate) {
2636 push @all_years, $enddate--;
2639 $dbh->disconnect if $disconnect;
2643 $main::lxdebug->leave_sub();