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 #======================================================================
57 $standard_dbh->disconnect();
63 $main::lxdebug->enter_sub(2);
67 my @pairs = split(/&/, $input);
70 my ($name, $value) = split(/=/, $_, 2);
71 $in{$name} = unescape(undef, $value);
74 $main::lxdebug->leave_sub(2);
79 sub _request_to_hash {
80 $main::lxdebug->enter_sub(2);
84 if (!$ENV{'CONTENT_TYPE'}
85 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
86 $main::lxdebug->leave_sub(2);
87 return _input_to_hash($input);
90 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr);
93 my $boundary = '--' . $1;
95 foreach my $line (split m/\n/, $input) {
96 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
98 if (($line eq $boundary) || ($line eq "$boundary\r")) {
99 $params{$name} =~ s|\r?\n$|| if $name;
101 undef $name, $filename;
104 $content_type = "text/plain";
111 next unless $boundary_found;
113 if (!$headers_done) {
114 $line =~ s/[\r\n]*$//;
121 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
122 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
124 substr $line, $-[0], $+[0] - $-[0], "";
127 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
129 substr $line, $-[0], $+[0] - $-[0], "";
133 $params{FILENAME} = $filename if ($filename);
138 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
147 $params{$name} .= "${line}\n";
150 $params{$name} =~ s|\r?\n$|| if $name;
152 $main::lxdebug->leave_sub(2);
157 $main::lxdebug->enter_sub();
163 if ($LXDebug::watch_form) {
164 require SL::Watchdog;
165 tie %{ $self }, 'SL::Watchdog';
168 read(STDIN, $_, $ENV{CONTENT_LENGTH});
170 if ($ENV{QUERY_STRING}) {
171 $_ = $ENV{QUERY_STRING};
178 my %parameters = _request_to_hash($_);
179 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
181 $self->{action} = lc $self->{action};
182 $self->{action} =~ s/( |-|,|\#)/_/g;
184 $self->{version} = "2.4.3";
186 $main::lxdebug->leave_sub();
192 $main::lxdebug->enter_sub();
198 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
200 $main::lxdebug->leave_sub();
204 $main::lxdebug->enter_sub(2);
206 my ($self, $str) = @_;
208 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
210 $main::lxdebug->leave_sub(2);
216 $main::lxdebug->enter_sub(2);
218 my ($self, $str) = @_;
223 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
225 $main::lxdebug->leave_sub(2);
231 my ($self, $str) = @_;
233 if ($str && !ref($str)) {
234 $str =~ s/\"/"/g;
242 my ($self, $str) = @_;
244 if ($str && !ref($str)) {
245 $str =~ s/"/\"/g;
253 $main::lxdebug->enter_sub(2);
255 my ($self, $str) = @_;
258 ('order' => ['"', '<', '>'],
264 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
266 $main::lxdebug->leave_sub(2);
275 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
277 for (sort keys %$self) {
278 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
279 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
286 $main::lxdebug->enter_sub();
288 $main::lxdebug->show_backtrace();
290 my ($self, $msg) = @_;
291 if ($ENV{HTTP_USER_AGENT}) {
293 $self->show_generic_error($msg);
297 if ($self->{error_function}) {
298 &{ $self->{error_function} }($msg);
304 $main::lxdebug->leave_sub();
308 $main::lxdebug->enter_sub();
310 my ($self, $msg) = @_;
312 if ($ENV{HTTP_USER_AGENT}) {
315 if (!$self->{header}) {
328 if ($self->{info_function}) {
329 &{ $self->{info_function} }($msg);
335 $main::lxdebug->leave_sub();
339 $main::lxdebug->enter_sub();
341 my ($self, $str, $cols, $maxrows) = @_;
345 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
347 $maxrows = $rows unless defined $maxrows;
349 $main::lxdebug->leave_sub();
351 return ($rows > $maxrows) ? $maxrows : $rows;
355 $main::lxdebug->enter_sub();
357 my ($self, $msg) = @_;
359 $self->error("$msg\n" . $DBI::errstr);
361 $main::lxdebug->leave_sub();
365 $main::lxdebug->enter_sub();
367 my ($self, $name, $msg) = @_;
369 if ($self->{$name} =~ /^\s*$/) {
372 $main::lxdebug->leave_sub();
376 $main::lxdebug->enter_sub();
378 my ($self, $extra_code) = @_;
380 if ($self->{header}) {
381 $main::lxdebug->leave_sub();
385 my ($stylesheet, $favicon);
387 if ($ENV{HTTP_USER_AGENT}) {
389 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
391 $stylesheets =~ s|^\s*||;
392 $stylesheets =~ s|\s*$||;
393 foreach my $file (split m/\s+/, $stylesheets) {
395 next if (! -f "css/$file");
397 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
400 $self->{favicon} = "favicon.ico" unless $self->{favicon};
402 if ($self->{favicon} && (-f "$self->{favicon}")) {
404 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
408 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
410 if ($self->{landscape}) {
411 $pagelayout = qq|<style type="text/css">
412 \@page { size:landscape; }
416 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
420 if ($self->{jsscript} == 1) {
423 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
424 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
425 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
426 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
433 ? "$self->{title} - $self->{titlebar}"
436 foreach $item (@ { $self->{AJAX} }) {
437 $ajax .= $item->show_javascript();
439 print qq|Content-Type: text/html; charset=${db_charset};
443 <title>$self->{titlebar}</title>
447 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
451 <script type="text/javascript">
459 <meta name="robots" content="noindex,nofollow" />
460 <script type="text/javascript" src="js/highlight_input.js"></script>
461 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
463 <script type="text/javascript" src="js/tabcontent.js">
465 /***********************************************
466 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
467 * This notice MUST stay intact for legal use
468 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
469 ***********************************************/
480 $main::lxdebug->leave_sub();
483 sub _prepare_html_template {
484 $main::lxdebug->enter_sub();
486 my ($self, $file, $additional_params) = @_;
489 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
490 $language = $main::language;
492 $language = $main::myconfig{"countrycode"};
494 $language = "de" unless ($language);
496 if (-f "templates/webpages/${file}_${language}.html") {
497 if ((-f ".developer") &&
498 (-f "templates/webpages/${file}_master.html") &&
499 ((stat("templates/webpages/${file}_master.html"))[9] >
500 (stat("templates/webpages/${file}_${language}.html"))[9])) {
501 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
502 "Please re-run 'locales.pl' in 'locale/${language}'.";
503 print(qq|<pre>$info</pre>|);
507 $file = "templates/webpages/${file}_${language}.html";
508 } elsif (-f "templates/webpages/${file}.html") {
509 $file = "templates/webpages/${file}.html";
511 my $info = "Web page template '${file}' not found.\n" .
512 "Please re-run 'locales.pl' in 'locale/${language}'.";
513 print(qq|<pre>$info</pre>|);
517 if ($self->{"DEBUG"}) {
518 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
521 if ($additional_params->{"DEBUG"}) {
522 $additional_params->{"DEBUG"} =
523 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
526 if (%main::myconfig) {
527 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
528 my $jsc_dateformat = $main::myconfig{"dateformat"};
529 $jsc_dateformat =~ s/d+/\%d/gi;
530 $jsc_dateformat =~ s/m+/\%m/gi;
531 $jsc_dateformat =~ s/y+/\%Y/gi;
532 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
535 $additional_params->{"conf_webdav"} = $main::webdav;
536 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
537 $additional_params->{"conf_latex_templates"} = $main::latex;
538 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
540 if (%main::debug_options) {
541 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
544 $main::lxdebug->leave_sub();
549 sub parse_html_template {
550 $main::lxdebug->enter_sub();
552 my ($self, $file, $additional_params) = @_;
554 $additional_params ||= { };
556 $file = $self->_prepare_html_template($file, $additional_params);
558 my $template = HTML::Template->new("filename" => $file,
559 "die_on_bad_params" => 0,
561 "case_sensitive" => 1,
562 "loop_context_vars" => 1,
565 foreach my $key ($template->param()) {
566 my $param = $additional_params->{$key} || $self->{$key};
567 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
568 $template->param($key => $param);
571 my $output = $template->output();
573 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
575 $main::lxdebug->leave_sub();
580 sub parse_html_template2 {
581 $main::lxdebug->enter_sub();
583 my ($self, $file, $additional_params) = @_;
585 $additional_params ||= { };
587 $file = $self->_prepare_html_template($file, $additional_params);
589 my $template = Template->new({ 'INTERPOLATE' => 0,
595 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
598 $template->process($file, $additional_params, \$output);
600 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
602 $main::lxdebug->leave_sub();
607 sub show_generic_error {
608 my ($self, $error, $title, $action) = @_;
611 $add_params->{"title"} = $title if ($title);
612 $self->{"label_error"} = $error;
616 map({ delete($self->{$_}); } qw(action));
617 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
618 if (!ref($self->{$_})); }
620 $add_params->{"SHOW_BUTTON"} = 1;
621 $add_params->{"BUTTON_LABEL"} = $action;
623 $add_params->{"VARIABLES"} = \@vars;
626 print($self->parse_html_template("generic/error", $add_params));
628 die("Error: $error\n");
631 sub show_generic_information {
632 my ($self, $error, $title) = @_;
635 $add_params->{"title"} = $title if ($title);
636 $self->{"label_information"} = $error;
639 print($self->parse_html_template("generic/information", $add_params));
641 die("Information: $error\n");
644 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
645 # changed it to accept an arbitrary number of triggers - sschoeling
647 $main::lxdebug->enter_sub();
650 my $myconfig = shift;
653 # set dateform for jsscript
656 "dd.mm.yy" => "%d.%m.%Y",
657 "dd-mm-yy" => "%d-%m-%Y",
658 "dd/mm/yy" => "%d/%m/%Y",
659 "mm/dd/yy" => "%m/%d/%Y",
660 "mm-dd-yy" => "%m-%d-%Y",
661 "yyyy-mm-dd" => "%Y-%m-%d",
664 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
665 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
672 inputField : "| . (shift) . qq|",
673 ifFormat :"$ifFormat",
674 align : "| . (shift) . qq|",
675 button : "| . (shift) . qq|"
681 <script type="text/javascript">
682 <!--| . join("", @triggers) . qq|//-->
686 $main::lxdebug->leave_sub();
689 } #end sub write_trigger
692 $main::lxdebug->enter_sub();
694 my ($self, $msg) = @_;
696 if ($self->{callback}) {
698 ($script, $argv) = split(/\?/, $self->{callback}, 2);
700 $script =~ s|[^a-zA-Z0-9_\.]||g;
701 exec("perl", "$script", $argv);
709 $main::lxdebug->leave_sub();
712 # sort of columns removed - empty sub
714 $main::lxdebug->enter_sub();
716 my ($self, @columns) = @_;
718 $main::lxdebug->leave_sub();
724 $main::lxdebug->enter_sub(2);
726 my ($self, $myconfig, $amount, $places, $dash) = @_;
731 my $neg = ($amount =~ s/-//);
733 if (defined($places) && ($places ne '')) {
738 my ($actual_places) = ($amount =~ /\.(\d+)/);
739 $actual_places = length($actual_places);
740 $places = $actual_places > $places ? $actual_places : $places;
743 $amount = $self->round_amount($amount, $places);
746 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
747 my @p = split(/\./, $amount); # split amount at decimal point
749 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
752 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
755 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
756 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
757 ($neg ? "-$amount" : "$amount" ) ;
761 $main::lxdebug->leave_sub(2);
766 $main::lxdebug->enter_sub(2);
768 my ($self, $myconfig, $amount) = @_;
770 if ( ($myconfig->{numberformat} eq '1.000,00')
771 || ($myconfig->{numberformat} eq '1000,00')) {
776 if ($myconfig->{numberformat} eq "1'000.00") {
782 $main::lxdebug->leave_sub(2);
784 return ($amount * 1);
788 $main::lxdebug->enter_sub(2);
790 my ($self, $amount, $places) = @_;
793 # Rounding like "Kaufmannsrunden"
794 # Descr. http://de.wikipedia.org/wiki/Rundung
796 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
799 $amount = $amount * (10**($places));
800 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
802 $main::lxdebug->leave_sub(2);
804 return $round_amount;
809 $main::lxdebug->enter_sub();
811 my ($self, $myconfig, $userspath) = @_;
812 my ($template, $out);
816 $self->{"cwd"} = getcwd();
817 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
819 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
820 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
821 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
822 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
823 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
824 } elsif (($self->{"format"} =~ /html/i) ||
825 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
826 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
827 } elsif (($self->{"format"} =~ /xml/i) ||
828 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
829 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
830 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
831 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
832 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
833 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
834 } elsif ( defined $self->{'format'}) {
835 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
836 } elsif ( $self->{'format'} eq '' ) {
837 $self->error("No Outputformat given: $self->{'format'}");
838 } else { #Catch the rest
839 $self->error("Outputformat not defined: $self->{'format'}");
842 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
843 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
845 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
846 qw(email tel fax name signature company address businessnumber
847 co_ustid taxnumber duns));
848 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
849 qw(company address signature));
850 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
852 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
854 # OUT is used for the media, screen, printer, email
855 # for postscript we store a copy in a temporary file
857 my $prepend_userspath;
859 if (!$self->{tmpfile}) {
860 $self->{tmpfile} = "${fileid}.$self->{IN}";
861 $prepend_userspath = 1;
864 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
866 $self->{tmpfile} =~ s|.*/||;
867 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
868 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
870 if ($template->uses_temp_file() || $self->{media} eq 'email') {
872 $self->{OUT} = ">$self->{tmpfile}";
876 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
878 open(OUT, ">-") or $self->error("STDOUT : $!");
882 if (!$template->parse(*OUT)) {
884 $self->error("$self->{IN} : " . $template->get_error());
889 if ($template->uses_temp_file() || $self->{media} eq 'email') {
891 if ($self->{media} eq 'email') {
893 my $mail = new Mailer;
895 map { $mail->{$_} = $self->{$_} }
896 qw(cc bcc subject message version format);
897 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
898 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
899 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
900 $mail->{fileid} = "$fileid.";
901 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
903 # if we send html or plain text inline
904 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
905 $mail->{contenttype} = "text/html";
907 $mail->{message} =~ s/\r\n/<br>\n/g;
908 $myconfig->{signature} =~ s/\\n/<br>\n/g;
909 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
911 open(IN, $self->{tmpfile})
912 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
914 $mail->{message} .= $_;
921 if (!$self->{"do_not_attach"}) {
922 @{ $mail->{attachments} } =
923 ({ "filename" => $self->{"tmpfile"},
924 "name" => $self->{"attachment_filename"} ?
925 $self->{"attachment_filename"} : $self->{"tmpfile"} });
928 $mail->{message} =~ s/\r\n/\n/g;
929 $myconfig->{signature} =~ s/\\n/\n/g;
930 $mail->{message} .= "\n-- \n$myconfig->{signature}";
934 my $err = $mail->send();
935 $self->error($self->cleanup . "$err") if ($err);
941 my $numbytes = (-s $self->{tmpfile});
942 open(IN, $self->{tmpfile})
943 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
945 $self->{copies} = 1 unless $self->{media} eq 'printer';
947 chdir("$self->{cwd}");
948 #print(STDERR "Kopien $self->{copies}\n");
949 #print(STDERR "OUT $self->{OUT}\n");
950 for my $i (1 .. $self->{copies}) {
952 open(OUT, $self->{OUT})
953 or $self->error($self->cleanup . "$self->{OUT} : $!");
955 $self->{attachment_filename} = ($self->{attachment_filename})
956 ? $self->{attachment_filename}
957 : $self->generate_attachment_filename();
960 print qq|Content-Type: | . $template->get_mime_type() . qq|
961 Content-Disposition: attachment; filename="$self->{attachment_filename}"
962 Content-Length: $numbytes
966 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
986 chdir("$self->{cwd}");
987 $main::lxdebug->leave_sub();
990 sub get_formname_translation {
991 my ($self, $formname) = @_;
993 $formname ||= $self->{formname};
995 my %formname_translations = (
996 bin_list => $main::locale->text('Bin List'),
997 credit_note => $main::locale->text('Credit Note'),
998 invoice => $main::locale->text('Invoice'),
999 packing_list => $main::locale->text('Packing List'),
1000 pick_list => $main::locale->text('Pick List'),
1001 proforma => $main::locale->text('Proforma Invoice'),
1002 purchase_order => $main::locale->text('Purchase Order'),
1003 request_quotation => $main::locale->text('RFQ'),
1004 sales_order => $main::locale->text('Confirmation'),
1005 sales_quotation => $main::locale->text('Quotation'),
1006 storno_invoice => $main::locale->text('Storno Invoice'),
1007 storno_packing_list => $main::locale->text('Storno Packing List'),
1010 return $formname_translations{$formname}
1013 sub generate_attachment_filename {
1016 my $attachment_filename = $self->get_formname_translation();
1018 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1019 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1022 if ($attachment_filename && $self->{"${prefix}number"}) {
1023 $attachment_filename .= "_" . $self->{"${prefix}number"}
1024 . ( $self->{format} =~ /pdf/i ? ".pdf"
1025 : $self->{format} =~ /postscript/i ? ".ps"
1026 : $self->{format} =~ /opendocument/i ? ".odt"
1027 : $self->{format} =~ /html/i ? ".html"
1029 $attachment_filename =~ s/ /_/g;
1030 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1031 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1032 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1034 $attachment_filename = "";
1037 return $attachment_filename;
1041 $main::lxdebug->enter_sub();
1045 chdir("$self->{tmpdir}");
1048 if (-f "$self->{tmpfile}.err") {
1049 open(FH, "$self->{tmpfile}.err");
1054 if ($self->{tmpfile}) {
1055 $self->{tmpfile} =~ s|.*/||g;
1057 $self->{tmpfile} =~ s/\.\w+$//g;
1058 my $tmpfile = $self->{tmpfile};
1059 unlink(<$tmpfile.*>);
1062 chdir("$self->{cwd}");
1064 $main::lxdebug->leave_sub();
1070 $main::lxdebug->enter_sub();
1072 my ($self, $date, $myconfig) = @_;
1074 if ($date && $date =~ /\D/) {
1076 if ($myconfig->{dateformat} =~ /^yy/) {
1077 ($yy, $mm, $dd) = split /\D/, $date;
1079 if ($myconfig->{dateformat} =~ /^mm/) {
1080 ($mm, $dd, $yy) = split /\D/, $date;
1082 if ($myconfig->{dateformat} =~ /^dd/) {
1083 ($dd, $mm, $yy) = split /\D/, $date;
1088 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1089 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1091 $dd = "0$dd" if ($dd < 10);
1092 $mm = "0$mm" if ($mm < 10);
1094 $date = "$yy$mm$dd";
1097 $main::lxdebug->leave_sub();
1102 # Database routines used throughout
1105 $main::lxdebug->enter_sub(2);
1107 my ($self, $myconfig) = @_;
1109 # connect to database
1111 DBI->connect($myconfig->{dbconnect},
1112 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1116 if ($myconfig->{dboptions}) {
1117 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1120 $main::lxdebug->leave_sub(2);
1125 sub dbconnect_noauto {
1126 $main::lxdebug->enter_sub();
1128 my ($self, $myconfig) = @_;
1130 # connect to database
1132 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1133 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1137 if ($myconfig->{dboptions}) {
1138 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1141 $main::lxdebug->leave_sub();
1146 sub get_standard_dbh {
1147 $main::lxdebug->enter_sub(2);
1149 my ($self, $myconfig) = @_;
1151 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1153 $main::lxdebug->leave_sub(2);
1155 return $standard_dbh;
1158 sub update_balance {
1159 $main::lxdebug->enter_sub();
1161 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1163 # if we have a value, go do it
1166 # retrieve balance from table
1167 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1168 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1169 my ($balance) = $sth->fetchrow_array;
1175 $query = "UPDATE $table SET $field = $balance WHERE $where";
1176 do_query($self, $dbh, $query, @values);
1178 $main::lxdebug->leave_sub();
1181 sub update_exchangerate {
1182 $main::lxdebug->enter_sub();
1184 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1186 # some sanity check for currency
1188 $main::lxdebug->leave_sub();
1191 my $query = qq|SELECT curr FROM defaults|;
1193 my ($currency) = selectrow_query($self, $dbh, $query);
1194 my ($defaultcurrency) = split m/:/, $currency;
1197 if ($curr eq $defaultcurrency) {
1198 $main::lxdebug->leave_sub();
1202 my $query = qq|SELECT e.curr FROM exchangerate e
1203 WHERE e.curr = ? AND e.transdate = ?
1205 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1214 $buy = conv_i($buy, "NULL");
1215 $sell = conv_i($sell, "NULL");
1218 if ($buy != 0 && $sell != 0) {
1219 $set = "buy = $buy, sell = $sell";
1220 } elsif ($buy != 0) {
1221 $set = "buy = $buy";
1222 } elsif ($sell != 0) {
1223 $set = "sell = $sell";
1226 if ($sth->fetchrow_array) {
1227 $query = qq|UPDATE exchangerate
1233 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1234 VALUES (?, $buy, $sell, ?)|;
1237 do_query($self, $dbh, $query, $curr, $transdate);
1239 $main::lxdebug->leave_sub();
1242 sub save_exchangerate {
1243 $main::lxdebug->enter_sub();
1245 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1247 my $dbh = $self->dbconnect($myconfig);
1251 $buy = $rate if $fld eq 'buy';
1252 $sell = $rate if $fld eq 'sell';
1255 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1260 $main::lxdebug->leave_sub();
1263 sub get_exchangerate {
1264 $main::lxdebug->enter_sub();
1266 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1268 unless ($transdate) {
1269 $main::lxdebug->leave_sub();
1273 my $query = qq|SELECT curr FROM defaults|;
1275 my ($currency) = selectrow_query($self, $dbh, $query);
1276 my ($defaultcurrency) = split m/:/, $currency;
1278 if ($currency eq $defaultcurrency) {
1279 $main::lxdebug->leave_sub();
1283 my $query = qq|SELECT e.$fld FROM exchangerate e
1284 WHERE e.curr = ? AND e.transdate = ?|;
1285 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1289 $main::lxdebug->leave_sub();
1291 return $exchangerate;
1294 sub check_exchangerate {
1295 $main::lxdebug->enter_sub();
1297 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1299 unless ($transdate) {
1300 $main::lxdebug->leave_sub();
1304 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1306 if ($currency eq $defaultcurrency) {
1307 $main::lxdebug->leave_sub();
1311 my $dbh = $self->get_standard_dbh($myconfig);
1312 my $query = qq|SELECT e.$fld FROM exchangerate e
1313 WHERE e.curr = ? AND e.transdate = ?|;
1315 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1317 $exchangerate = 1 if ($exchangerate eq "");
1319 $main::lxdebug->leave_sub();
1321 return $exchangerate;
1324 sub get_default_currency {
1325 $main::lxdebug->enter_sub();
1327 my ($self, $myconfig) = @_;
1328 my $dbh = $self->get_standard_dbh($myconfig);
1330 my $query = qq|SELECT curr FROM defaults|;
1332 my ($curr) = selectrow_query($self, $dbh, $query);
1333 my ($defaultcurrency) = split m/:/, $curr;
1335 $main::lxdebug->leave_sub();
1337 return $defaultcurrency;
1341 sub set_payment_options {
1342 $main::lxdebug->enter_sub();
1344 my ($self, $myconfig, $transdate) = @_;
1346 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1348 my $dbh = $self->get_standard_dbh($myconfig);
1351 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1352 qq|FROM payment_terms p | .
1355 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1356 $self->{payment_terms}) =
1357 selectrow_query($self, $dbh, $query, $self->{payment_id});
1359 if ($transdate eq "") {
1360 if ($self->{invdate}) {
1361 $transdate = $self->{invdate};
1363 $transdate = $self->{transdate};
1368 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1369 qq|FROM payment_terms|;
1370 ($self->{netto_date}, $self->{skonto_date}) =
1371 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1373 my ($invtotal, $total);
1374 my (%amounts, %formatted_amounts);
1376 if ($self->{type} =~ /_order$/) {
1377 $amounts{invtotal} = $self->{ordtotal};
1378 $amounts{total} = $self->{ordtotal};
1380 } elsif ($self->{type} =~ /_quotation$/) {
1381 $amounts{invtotal} = $self->{quototal};
1382 $amounts{total} = $self->{quototal};
1385 $amounts{invtotal} = $self->{invtotal};
1386 $amounts{total} = $self->{total};
1389 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1391 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1392 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1393 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1395 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2) } keys %amounts;
1397 if ($self->{"language_id"}) {
1399 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1400 qq|FROM translation_payment_terms t | .
1401 qq|LEFT JOIN language l ON t.language_id = l.id | .
1402 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1403 my ($description_long, $output_numberformat, $output_dateformat,
1404 $output_longdates) =
1405 selectrow_query($self, $dbh, $query,
1406 $self->{"language_id"}, $self->{"payment_id"});
1408 $self->{payment_terms} = $description_long if ($description_long);
1410 if ($output_dateformat) {
1411 foreach my $key (qw(netto_date skonto_date)) {
1413 $main::locale->reformat_date($myconfig, $self->{$key},
1419 if ($output_numberformat &&
1420 ($output_numberformat ne $myconfig->{"numberformat"})) {
1421 my $saved_numberformat = $myconfig->{"numberformat"};
1422 $myconfig->{"numberformat"} = $output_numberformat;
1423 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1424 $myconfig->{"numberformat"} = $saved_numberformat;
1428 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1429 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1430 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1431 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1432 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1433 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1434 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1436 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1438 $main::lxdebug->leave_sub();
1442 sub get_template_language {
1443 $main::lxdebug->enter_sub();
1445 my ($self, $myconfig) = @_;
1447 my $template_code = "";
1449 if ($self->{language_id}) {
1450 my $dbh = $self->get_standard_dbh($myconfig);
1451 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1452 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1455 $main::lxdebug->leave_sub();
1457 return $template_code;
1460 sub get_printer_code {
1461 $main::lxdebug->enter_sub();
1463 my ($self, $myconfig) = @_;
1465 my $template_code = "";
1467 if ($self->{printer_id}) {
1468 my $dbh = $self->get_standard_dbh($myconfig);
1469 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1470 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1473 $main::lxdebug->leave_sub();
1475 return $template_code;
1479 $main::lxdebug->enter_sub();
1481 my ($self, $myconfig) = @_;
1483 my $template_code = "";
1485 if ($self->{shipto_id}) {
1486 my $dbh = $self->get_standard_dbh($myconfig);
1487 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1488 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1489 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1492 $main::lxdebug->leave_sub();
1496 $main::lxdebug->enter_sub();
1498 my ($self, $dbh, $id, $module) = @_;
1503 foreach my $item (qw(name department_1 department_2 street zipcode city country
1504 contact phone fax email)) {
1505 if ($self->{"shipto$item"}) {
1506 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1508 push(@values, $self->{"shipto${item}"});
1512 if ($self->{shipto_id}) {
1513 my $query = qq|UPDATE shipto set
1515 shiptodepartment_1 = ?,
1516 shiptodepartment_2 = ?,
1525 WHERE shipto_id = ?|;
1526 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1528 my $query = qq|SELECT * FROM shipto
1529 WHERE shiptoname = ? AND
1530 shiptodepartment_1 = ? AND
1531 shiptodepartment_2 = ? AND
1532 shiptostreet = ? AND
1533 shiptozipcode = ? AND
1535 shiptocountry = ? AND
1536 shiptocontact = ? AND
1542 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1545 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1546 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1547 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1548 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1549 do_query($self, $dbh, $query, $id, @values, $module);
1554 $main::lxdebug->leave_sub();
1558 $main::lxdebug->enter_sub();
1560 my ($self, $dbh) = @_;
1562 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1563 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1564 $self->{"employee_id"} *= 1;
1566 $main::lxdebug->leave_sub();
1570 $main::lxdebug->enter_sub();
1572 my ($self, $myconfig, $salesman_id) = @_;
1574 $main::lxdebug->leave_sub() and return unless $salesman_id;
1576 my $dbh = $self->get_standard_dbh($myconfig);
1579 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1583 my $user = new User($main::memberfile, $login);
1584 map({ $self->{"salesman_$_"} = $user->{$_}; }
1585 qw(address businessnumber co_ustid company duns email fax name
1587 $self->{salesman_login} = $login;
1589 $self->{salesman_name} = $login
1590 if ($self->{salesman_name} eq "");
1592 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1595 $main::lxdebug->leave_sub();
1599 $main::lxdebug->enter_sub();
1601 my ($self, $myconfig) = @_;
1603 my $dbh = $self->get_standard_dbh($myconfig);
1604 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1605 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1607 $main::lxdebug->leave_sub();
1611 $main::lxdebug->enter_sub();
1613 my ($self, $dbh, $id, $key) = @_;
1615 $key = "all_contacts" unless ($key);
1618 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1619 qq|FROM contacts | .
1620 qq|WHERE cp_cv_id = ? | .
1621 qq|ORDER BY lower(cp_name)|;
1623 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1625 $main::lxdebug->leave_sub();
1629 $main::lxdebug->enter_sub();
1631 my ($self, $dbh, $key) = @_;
1633 my ($all, $old_id, $where, @values);
1635 if (ref($key) eq "HASH") {
1638 $key = "ALL_PROJECTS";
1640 foreach my $p (keys(%{$params})) {
1642 $all = $params->{$p};
1643 } elsif ($p eq "old_id") {
1644 $old_id = $params->{$p};
1645 } elsif ($p eq "key") {
1646 $key = $params->{$p};
1652 $where = "WHERE active ";
1654 if (ref($old_id) eq "ARRAY") {
1655 my @ids = grep({ $_ } @{$old_id});
1657 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1658 push(@values, @ids);
1661 $where .= " OR (id = ?) ";
1662 push(@values, $old_id);
1668 qq|SELECT id, projectnumber, description, active | .
1671 qq|ORDER BY lower(projectnumber)|;
1673 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1675 $main::lxdebug->leave_sub();
1679 $main::lxdebug->enter_sub();
1681 my ($self, $dbh, $vc_id, $key) = @_;
1683 $key = "all_shipto" unless ($key);
1685 # get shipping addresses
1686 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1688 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1690 $main::lxdebug->leave_sub();
1694 $main::lxdebug->enter_sub();
1696 my ($self, $dbh, $key) = @_;
1698 $key = "all_printers" unless ($key);
1700 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1702 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1704 $main::lxdebug->leave_sub();
1708 $main::lxdebug->enter_sub();
1710 my ($self, $dbh, $params) = @_;
1712 $key = $params->{key};
1713 $key = "all_charts" unless ($key);
1715 my $transdate = quote_db_date($params->{transdate});
1718 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1720 qq|LEFT JOIN taxkeys tk ON | .
1721 qq|(tk.id = (SELECT id FROM taxkeys | .
1722 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1723 qq| ORDER BY startdate DESC LIMIT 1)) | .
1724 qq|ORDER BY c.accno|;
1726 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1728 $main::lxdebug->leave_sub();
1731 sub _get_taxcharts {
1732 $main::lxdebug->enter_sub();
1734 my ($self, $dbh, $key) = @_;
1736 $key = "all_taxcharts" unless ($key);
1738 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1740 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1742 $main::lxdebug->leave_sub();
1746 $main::lxdebug->enter_sub();
1748 my ($self, $dbh, $key) = @_;
1750 $key = "all_taxzones" unless ($key);
1752 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1754 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1756 $main::lxdebug->leave_sub();
1759 sub _get_employees {
1760 $main::lxdebug->enter_sub();
1762 my ($self, $dbh, $default_key, $key) = @_;
1764 $key = $default_key unless ($key);
1765 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1767 $main::lxdebug->leave_sub();
1770 sub _get_business_types {
1771 $main::lxdebug->enter_sub();
1773 my ($self, $dbh, $key) = @_;
1775 $key = "all_business_types" unless ($key);
1777 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1779 $main::lxdebug->leave_sub();
1782 sub _get_languages {
1783 $main::lxdebug->enter_sub();
1785 my ($self, $dbh, $key) = @_;
1787 $key = "all_languages" unless ($key);
1789 my $query = qq|SELECT * FROM language ORDER BY id|;
1791 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1793 $main::lxdebug->leave_sub();
1796 sub _get_dunning_configs {
1797 $main::lxdebug->enter_sub();
1799 my ($self, $dbh, $key) = @_;
1801 $key = "all_dunning_configs" unless ($key);
1803 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1805 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1807 $main::lxdebug->leave_sub();
1810 sub _get_currencies {
1811 $main::lxdebug->enter_sub();
1813 my ($self, $dbh, $key) = @_;
1815 $key = "all_currencies" unless ($key);
1817 my $query = qq|SELECT curr AS currency FROM defaults|;
1819 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1821 $main::lxdebug->leave_sub();
1825 $main::lxdebug->enter_sub();
1827 my ($self, $dbh, $key) = @_;
1829 $key = "all_payments" unless ($key);
1831 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1833 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1835 $main::lxdebug->leave_sub();
1838 sub _get_customers {
1839 $main::lxdebug->enter_sub();
1841 my ($self, $dbh, $key) = @_;
1843 $key = "all_customers" unless ($key);
1845 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name|;
1847 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1849 $main::lxdebug->leave_sub();
1853 $main::lxdebug->enter_sub();
1855 my ($self, $dbh, $key) = @_;
1857 $key = "all_vendors" unless ($key);
1859 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1861 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1863 $main::lxdebug->leave_sub();
1866 sub _get_departments {
1867 $main::lxdebug->enter_sub();
1869 my ($self, $dbh, $key) = @_;
1871 $key = "all_departments" unless ($key);
1873 my $query = qq|SELECT * FROM department ORDER BY description|;
1875 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1877 $main::lxdebug->leave_sub();
1881 $main::lxdebug->enter_sub();
1886 my $dbh = $self->get_standard_dbh(\%main::myconfig);
1887 my ($sth, $query, $ref);
1889 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1890 my $vc_id = $self->{"${vc}_id"};
1892 if ($params{"contacts"}) {
1893 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1896 if ($params{"shipto"}) {
1897 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1900 if ($params{"projects"} || $params{"all_projects"}) {
1901 $self->_get_projects($dbh, $params{"all_projects"} ?
1902 $params{"all_projects"} : $params{"projects"},
1903 $params{"all_projects"} ? 1 : 0);
1906 if ($params{"printers"}) {
1907 $self->_get_printers($dbh, $params{"printers"});
1910 if ($params{"languages"}) {
1911 $self->_get_languages($dbh, $params{"languages"});
1914 if ($params{"charts"}) {
1915 $self->_get_charts($dbh, $params{"charts"});
1918 if ($params{"taxcharts"}) {
1919 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1922 if ($params{"taxzones"}) {
1923 $self->_get_taxzones($dbh, $params{"taxzones"});
1926 if ($params{"employees"}) {
1927 $self->_get_employees($dbh, "all_employees", $params{"employees"});
1930 if ($params{"salesmen"}) {
1931 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
1934 if ($params{"business_types"}) {
1935 $self->_get_business_types($dbh, $params{"business_types"});
1938 if ($params{"dunning_configs"}) {
1939 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1942 if($params{"currencies"}) {
1943 $self->_get_currencies($dbh, $params{"currencies"});
1946 if($params{"customers"}) {
1947 $self->_get_customers($dbh, $params{"customers"});
1950 if($params{"vendors"}) {
1951 $self->_get_vendors($dbh, $params{"vendors"});
1954 if($params{"payments"}) {
1955 $self->_get_payments($dbh, $params{"payments"});
1958 if($params{"departments"}) {
1959 $self->_get_departments($dbh, $params{"departments"});
1962 $main::lxdebug->leave_sub();
1965 # this sub gets the id and name from $table
1967 $main::lxdebug->enter_sub();
1969 my ($self, $myconfig, $table) = @_;
1971 # connect to database
1972 my $dbh = $self->get_standard_dbh($myconfig);
1974 $table = $table eq "customer" ? "customer" : "vendor";
1975 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1977 my ($query, @values);
1979 if (!$self->{openinvoices}) {
1981 if ($self->{customernumber} ne "") {
1982 $where = qq|(vc.customernumber ILIKE ?)|;
1983 push(@values, '%' . $self->{customernumber} . '%');
1985 $where = qq|(vc.name ILIKE ?)|;
1986 push(@values, '%' . $self->{$table} . '%');
1990 qq~SELECT vc.id, vc.name,
1991 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1993 WHERE $where AND (NOT vc.obsolete)
1997 qq~SELECT DISTINCT vc.id, vc.name,
1998 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2000 JOIN $table vc ON (a.${table}_id = vc.id)
2001 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2003 push(@values, '%' . $self->{$table} . '%');
2006 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2008 $main::lxdebug->leave_sub();
2010 return scalar(@{ $self->{name_list} });
2013 # the selection sub is used in the AR, AP, IS, IR and OE module
2016 $main::lxdebug->enter_sub();
2018 my ($self, $myconfig, $table, $module) = @_;
2021 my $dbh = $self->get_standard_dbh($myconfig);
2023 $table = $table eq "customer" ? "customer" : "vendor";
2025 my $query = qq|SELECT count(*) FROM $table|;
2026 my ($count) = selectrow_query($self, $dbh, $query);
2028 # build selection list
2029 if ($count < $myconfig->{vclimit}) {
2030 $query = qq|SELECT id, name, salesman_id
2031 FROM $table WHERE NOT obsolete
2033 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2037 $self->get_employee($dbh);
2039 # setup sales contacts
2040 $query = qq|SELECT e.id, e.name
2042 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2043 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2046 push(@{ $self->{all_employees} },
2047 { id => $self->{employee_id},
2048 name => $self->{employee} });
2050 # sort the whole thing
2051 @{ $self->{all_employees} } =
2052 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2054 if ($module eq 'AR') {
2056 # prepare query for departments
2057 $query = qq|SELECT id, description
2060 ORDER BY description|;
2063 $query = qq|SELECT id, description
2065 ORDER BY description|;
2068 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2071 $query = qq|SELECT id, description
2075 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2078 $query = qq|SELECT printer_description, id
2080 ORDER BY printer_description|;
2082 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2085 $query = qq|SELECT id, description
2089 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2091 $main::lxdebug->leave_sub();
2094 sub language_payment {
2095 $main::lxdebug->enter_sub();
2097 my ($self, $myconfig) = @_;
2099 my $dbh = $self->get_standard_dbh($myconfig);
2101 my $query = qq|SELECT id, description
2105 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2108 $query = qq|SELECT printer_description, id
2110 ORDER BY printer_description|;
2112 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2115 $query = qq|SELECT id, description
2119 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2121 # get buchungsgruppen
2122 $query = qq|SELECT id, description
2123 FROM buchungsgruppen|;
2125 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2127 $main::lxdebug->leave_sub();
2130 # this is only used for reports
2131 sub all_departments {
2132 $main::lxdebug->enter_sub();
2134 my ($self, $myconfig, $table) = @_;
2136 my $dbh = $self->get_standard_dbh($myconfig);
2139 if ($table eq 'customer') {
2140 $where = "WHERE role = 'P' ";
2143 my $query = qq|SELECT id, description
2146 ORDER BY description|;
2147 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2149 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2151 $main::lxdebug->leave_sub();
2155 $main::lxdebug->enter_sub();
2157 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2160 if ($table eq "customer") {
2169 $self->all_vc($myconfig, $table, $module);
2171 # get last customers or vendors
2172 my ($query, $sth, $ref);
2174 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2179 my $transdate = "current_date";
2180 if ($self->{transdate}) {
2181 $transdate = $dbh->quote($self->{transdate});
2184 # now get the account numbers
2185 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2186 FROM chart c, taxkeys tk
2187 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2188 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2191 $sth = $dbh->prepare($query);
2193 do_statement($self, $sth, $query, '%' . $module . '%');
2195 $self->{accounts} = "";
2196 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2198 foreach my $key (split(/:/, $ref->{link})) {
2199 if ($key =~ /$module/) {
2201 # cross reference for keys
2202 $xkeyref{ $ref->{accno} } = $key;
2204 push @{ $self->{"${module}_links"}{$key} },
2205 { accno => $ref->{accno},
2206 description => $ref->{description},
2207 taxkey => $ref->{taxkey_id},
2208 tax_id => $ref->{tax_id} };
2210 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2216 # get taxkeys and description
2217 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2218 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2220 if (($module eq "AP") || ($module eq "AR")) {
2221 # get tax rates and description
2222 $query = qq|SELECT * FROM tax|;
2223 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2229 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2230 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2231 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2232 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2234 d.description AS department,
2237 JOIN $table c ON (a.${table}_id = c.id)
2238 LEFT JOIN employee e ON (e.id = a.employee_id)
2239 LEFT JOIN department d ON (d.id = a.department_id)
2241 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2243 foreach $key (keys %$ref) {
2244 $self->{$key} = $ref->{$key};
2247 my $transdate = "current_date";
2248 if ($self->{transdate}) {
2249 $transdate = $dbh->quote($self->{transdate});
2252 # now get the account numbers
2253 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2255 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2257 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2258 OR c.link LIKE '%_tax%')
2261 $sth = $dbh->prepare($query);
2262 do_statement($self, $sth, $query, "%$module%");
2264 $self->{accounts} = "";
2265 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2267 foreach my $key (split(/:/, $ref->{link})) {
2268 if ($key =~ /$module/) {
2270 # cross reference for keys
2271 $xkeyref{ $ref->{accno} } = $key;
2273 push @{ $self->{"${module}_links"}{$key} },
2274 { accno => $ref->{accno},
2275 description => $ref->{description},
2276 taxkey => $ref->{taxkey_id},
2277 tax_id => $ref->{tax_id} };
2279 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2285 # get amounts from individual entries
2288 c.accno, c.description,
2289 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2293 LEFT JOIN chart c ON (c.id = a.chart_id)
2294 LEFT JOIN project p ON (p.id = a.project_id)
2295 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2296 WHERE (tk.taxkey_id=a.taxkey) AND
2297 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2298 THEN tk.chart_id = a.chart_id
2301 OR (c.link='%tax%')) AND
2302 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2303 WHERE a.trans_id = ?
2304 AND a.fx_transaction = '0'
2305 ORDER BY a.oid, a.transdate|;
2306 $sth = $dbh->prepare($query);
2307 do_statement($self, $sth, $query, $self->{id});
2309 # get exchangerate for currency
2310 $self->{exchangerate} =
2311 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2314 # store amounts in {acc_trans}{$key} for multiple accounts
2315 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2316 $ref->{exchangerate} =
2317 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2318 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2321 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2322 $ref->{amount} *= -1;
2324 $ref->{index} = $index;
2326 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2332 d.curr AS currencies, d.closedto, d.revtrans,
2333 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2334 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2336 $ref = selectfirst_hashref_query($self, $dbh, $query);
2337 map { $self->{$_} = $ref->{$_} } keys %$ref;
2344 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2345 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2346 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2348 $ref = selectfirst_hashref_query($self, $dbh, $query);
2349 map { $self->{$_} = $ref->{$_} } keys %$ref;
2351 if ($self->{"$self->{vc}_id"}) {
2353 # only setup currency
2354 ($self->{currency}) = split(/:/, $self->{currencies});
2358 $self->lastname_used($dbh, $myconfig, $table, $module);
2360 # get exchangerate for currency
2361 $self->{exchangerate} =
2362 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2368 $main::lxdebug->leave_sub();
2372 $main::lxdebug->enter_sub();
2374 my ($self, $dbh, $myconfig, $table, $module) = @_;
2376 my $arap = ($table eq 'customer') ? "ar" : "ap";
2377 $table = $table eq "customer" ? "customer" : "vendor";
2378 my $where = "1 = 1";
2380 if ($self->{type} =~ /_order/) {
2382 $where = "quotation = '0'";
2384 if ($self->{type} =~ /_quotation/) {
2386 $where = "quotation = '1'";
2389 my $query = qq|SELECT MAX(id) FROM $arap
2390 WHERE $where AND ${table}_id > 0|;
2391 my ($trans_id) = selectrow_query($self, $dbh, $query);
2396 a.curr, a.${table}_id, a.department_id,
2397 d.description AS department,
2398 ct.name, current_date + ct.terms AS duedate
2400 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2401 LEFT JOIN department d ON (a.department_id = d.id)
2403 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2404 $self->{department}, $self->{$table}, $self->{duedate})
2405 = selectrow_query($self, $dbh, $query, $trans_id);
2407 $main::lxdebug->leave_sub();
2411 $main::lxdebug->enter_sub();
2413 my ($self, $myconfig, $thisdate, $days) = @_;
2415 my $dbh = $self->get_standard_dbh($myconfig);
2420 my $dateformat = $myconfig->{dateformat};
2421 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2422 $thisdate = $dbh->quote($thisdate);
2423 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2425 $query = qq|SELECT current_date AS thisdate|;
2428 ($thisdate) = selectrow_query($self, $dbh, $query);
2430 $main::lxdebug->leave_sub();
2436 $main::lxdebug->enter_sub();
2438 my ($self, $string) = @_;
2440 if ($string !~ /%/) {
2441 $string = "%$string%";
2444 $string =~ s/\'/\'\'/g;
2446 $main::lxdebug->leave_sub();
2452 $main::lxdebug->enter_sub();
2454 my ($self, $flds, $new, $count, $numrows) = @_;
2458 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2464 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2466 $j = $item->{ndx} - 1;
2467 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2471 for $i ($count + 1 .. $numrows) {
2472 map { delete $self->{"${_}_$i"} } @{$flds};
2475 $main::lxdebug->leave_sub();
2479 $main::lxdebug->enter_sub();
2481 my ($self, $myconfig) = @_;
2485 my $dbh = $self->dbconnect_noauto($myconfig);
2487 my $query = qq|DELETE FROM status
2488 WHERE (formname = ?) AND (trans_id = ?)|;
2489 my $sth = prepare_query($self, $dbh, $query);
2491 if ($self->{formname} =~ /(check|receipt)/) {
2492 for $i (1 .. $self->{rowcount}) {
2493 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2496 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2500 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2501 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2503 my %queued = split / /, $self->{queued};
2506 if ($self->{formname} =~ /(check|receipt)/) {
2508 # this is a check or receipt, add one entry for each lineitem
2509 my ($accno) = split /--/, $self->{account};
2510 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2511 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2512 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2513 $sth = prepare_query($self, $dbh, $query);
2515 for $i (1 .. $self->{rowcount}) {
2516 if ($self->{"checked_$i"}) {
2517 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2523 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2524 VALUES (?, ?, ?, ?, ?)|;
2525 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2526 $queued{$self->{formname}}, $self->{formname});
2532 $main::lxdebug->leave_sub();
2536 $main::lxdebug->enter_sub();
2538 my ($self, $dbh) = @_;
2540 my ($query, $printed, $emailed);
2542 my $formnames = $self->{printed};
2543 my $emailforms = $self->{emailed};
2545 my $query = qq|DELETE FROM status
2546 WHERE (formname = ?) AND (trans_id = ?)|;
2547 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2549 # this only applies to the forms
2550 # checks and receipts are posted when printed or queued
2552 if ($self->{queued}) {
2553 my %queued = split / /, $self->{queued};
2555 foreach my $formname (keys %queued) {
2556 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2557 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2559 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2560 VALUES (?, ?, ?, ?, ?)|;
2561 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2563 $formnames =~ s/$self->{formname}//;
2564 $emailforms =~ s/$self->{formname}//;
2569 # save printed, emailed info
2570 $formnames =~ s/^ +//g;
2571 $emailforms =~ s/^ +//g;
2574 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2575 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2577 foreach my $formname (keys %status) {
2578 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2579 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2581 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2582 VALUES (?, ?, ?, ?)|;
2583 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2586 $main::lxdebug->leave_sub();
2590 # $main::locale->text('SAVED')
2591 # $main::locale->text('DELETED')
2592 # $main::locale->text('ADDED')
2593 # $main::locale->text('PAYMENT POSTED')
2594 # $main::locale->text('POSTED')
2595 # $main::locale->text('POSTED AS NEW')
2596 # $main::locale->text('ELSE')
2597 # $main::locale->text('SAVED FOR DUNNING')
2598 # $main::locale->text('DUNNING STARTED')
2599 # $main::locale->text('PRINTED')
2600 # $main::locale->text('MAILED')
2601 # $main::locale->text('SCREENED')
2602 # $main::locale->text('CANCELED')
2603 # $main::locale->text('invoice')
2604 # $main::locale->text('proforma')
2605 # $main::locale->text('sales_order')
2606 # $main::locale->text('packing_list')
2607 # $main::locale->text('pick_list')
2608 # $main::locale->text('purchase_order')
2609 # $main::locale->text('bin_list')
2610 # $main::locale->text('sales_quotation')
2611 # $main::locale->text('request_quotation')
2614 $main::lxdebug->enter_sub();
2619 if(!exists $self->{employee_id}) {
2620 &get_employee($self, $dbh);
2624 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2625 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2626 my @values = (conv_i($self->{id}), $self->{login},
2627 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2628 do_query($self, $dbh, $query, @values);
2630 $main::lxdebug->leave_sub();
2634 $main::lxdebug->enter_sub();
2636 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2637 my ($orderBy, $desc) = split(/\-\-/, $order);
2638 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2641 if ($trans_id ne "") {
2643 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 | .
2644 qq|FROM history_erp h | .
2645 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2646 qq|WHERE trans_id = | . $trans_id
2647 . $restriction . qq| |
2650 my $sth = $dbh->prepare($query) || $self->dberror($query);
2652 $sth->execute() || $self->dberror("$query");
2654 while(my $hash_ref = $sth->fetchrow_hashref()) {
2655 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2656 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2657 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2658 $tempArray[$i++] = $hash_ref;
2660 $main::lxdebug->leave_sub() and return \@tempArray
2661 if ($i > 0 && $tempArray[0] ne "");
2663 $main::lxdebug->leave_sub();
2667 sub update_defaults {
2668 $main::lxdebug->enter_sub();
2670 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2673 if ($provided_dbh) {
2674 $dbh = $provided_dbh;
2676 $dbh = $self->dbconnect_noauto($myconfig);
2678 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2679 my $sth = $dbh->prepare($query);
2681 $sth->execute || $self->dberror($query);
2682 my ($var) = $sth->fetchrow_array;
2685 if ($var =~ m/\d+$/) {
2686 my $new_var = (substr $var, $-[0]) * 1 + 1;
2687 my $len_diff = length($var) - $-[0] - length($new_var);
2688 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2694 $query = qq|UPDATE defaults SET $fld = ?|;
2695 do_query($self, $dbh, $query, $var);
2697 if (!$provided_dbh) {
2702 $main::lxdebug->leave_sub();
2707 sub update_business {
2708 $main::lxdebug->enter_sub();
2710 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2713 if ($provided_dbh) {
2714 $dbh = $provided_dbh;
2716 $dbh = $self->dbconnect_noauto($myconfig);
2719 qq|SELECT customernumberinit FROM business
2720 WHERE id = ? FOR UPDATE|;
2721 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2723 if ($var =~ m/\d+$/) {
2724 my $new_var = (substr $var, $-[0]) * 1 + 1;
2725 my $len_diff = length($var) - $-[0] - length($new_var);
2726 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2732 $query = qq|UPDATE business
2733 SET customernumberinit = ?
2735 do_query($self, $dbh, $query, $var, $business_id);
2737 if (!$provided_dbh) {
2742 $main::lxdebug->leave_sub();
2747 sub get_partsgroup {
2748 $main::lxdebug->enter_sub();
2750 my ($self, $myconfig, $p) = @_;
2752 my $dbh = $self->get_standard_dbh($myconfig);
2754 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2756 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2759 if ($p->{searchitems} eq 'part') {
2760 $query .= qq|WHERE p.inventory_accno_id > 0|;
2762 if ($p->{searchitems} eq 'service') {
2763 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2765 if ($p->{searchitems} eq 'assembly') {
2766 $query .= qq|WHERE p.assembly = '1'|;
2768 if ($p->{searchitems} eq 'labor') {
2769 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2772 $query .= qq|ORDER BY partsgroup|;
2775 $query = qq|SELECT id, partsgroup FROM partsgroup
2776 ORDER BY partsgroup|;
2779 if ($p->{language_code}) {
2780 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2781 t.description AS translation
2783 JOIN parts p ON (p.partsgroup_id = pg.id)
2784 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2785 ORDER BY translation|;
2786 @values = ($p->{language_code});
2789 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2791 $main::lxdebug->leave_sub();
2794 sub get_pricegroup {
2795 $main::lxdebug->enter_sub();
2797 my ($self, $myconfig, $p) = @_;
2799 my $dbh = $self->get_standard_dbh($myconfig);
2801 my $query = qq|SELECT p.id, p.pricegroup
2804 $query .= qq| ORDER BY pricegroup|;
2807 $query = qq|SELECT id, pricegroup FROM pricegroup
2808 ORDER BY pricegroup|;
2811 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2813 $main::lxdebug->leave_sub();
2817 # usage $form->all_years($myconfig, [$dbh])
2818 # return list of all years where bookings found
2821 $main::lxdebug->enter_sub();
2823 my ($self, $myconfig, $dbh) = @_;
2825 $dbh ||= $self->get_standard_dbh($myconfig);
2828 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2829 (SELECT MAX(transdate) FROM acc_trans)|;
2830 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2832 if ($myconfig->{dateformat} =~ /^yy/) {
2833 ($startdate) = split /\W/, $startdate;
2834 ($enddate) = split /\W/, $enddate;
2836 (@_) = split /\W/, $startdate;
2838 (@_) = split /\W/, $enddate;
2843 $startdate = substr($startdate,0,4);
2844 $enddate = substr($enddate,0,4);
2846 while ($enddate >= $startdate) {
2847 push @all_years, $enddate--;
2852 $main::lxdebug->leave_sub();