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 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
391 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
395 $self->{favicon} = "favicon.ico" unless $self->{favicon};
397 if ($self->{favicon} && (-f "$self->{favicon}")) {
399 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
403 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
405 if ($self->{landscape}) {
406 $pagelayout = qq|<style type="text/css">
407 \@page { size:landscape; }
411 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
415 if ($self->{jsscript} == 1) {
418 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
419 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
420 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
421 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
428 ? "$self->{title} - $self->{titlebar}"
431 foreach $item (@ { $self->{AJAX} }) {
432 $ajax .= $item->show_javascript();
434 print qq|Content-Type: text/html; charset=${db_charset};
438 <title>$self->{titlebar}</title>
442 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
446 <script type="text/javascript">
454 <meta name="robots" content="noindex,nofollow" />
455 <script type="text/javascript" src="js/highlight_input.js"></script>
456 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
458 <script type="text/javascript" src="js/tabcontent.js">
460 /***********************************************
461 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
462 * This notice MUST stay intact for legal use
463 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
464 ***********************************************/
475 $main::lxdebug->leave_sub();
478 sub _prepare_html_template {
479 $main::lxdebug->enter_sub();
481 my ($self, $file, $additional_params) = @_;
484 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
485 $language = $main::language;
487 $language = $main::myconfig{"countrycode"};
489 $language = "de" unless ($language);
491 if (-f "templates/webpages/${file}_${language}.html") {
492 if ((-f ".developer") &&
493 (-f "templates/webpages/${file}_master.html") &&
494 ((stat("templates/webpages/${file}_master.html"))[9] >
495 (stat("templates/webpages/${file}_${language}.html"))[9])) {
496 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
497 "Please re-run 'locales.pl' in 'locale/${language}'.";
498 print(qq|<pre>$info</pre>|);
502 $file = "templates/webpages/${file}_${language}.html";
503 } elsif (-f "templates/webpages/${file}.html") {
504 $file = "templates/webpages/${file}.html";
506 my $info = "Web page template '${file}' not found.\n" .
507 "Please re-run 'locales.pl' in 'locale/${language}'.";
508 print(qq|<pre>$info</pre>|);
512 if ($self->{"DEBUG"}) {
513 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
516 if ($additional_params->{"DEBUG"}) {
517 $additional_params->{"DEBUG"} =
518 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
521 if (%main::myconfig) {
522 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
523 my $jsc_dateformat = $main::myconfig{"dateformat"};
524 $jsc_dateformat =~ s/d+/\%d/gi;
525 $jsc_dateformat =~ s/m+/\%m/gi;
526 $jsc_dateformat =~ s/y+/\%Y/gi;
527 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
530 $additional_params->{"conf_webdav"} = $main::webdav;
531 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
532 $additional_params->{"conf_latex_templates"} = $main::latex;
533 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
535 if (%main::debug_options) {
536 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
539 $main::lxdebug->leave_sub();
544 sub parse_html_template {
545 $main::lxdebug->enter_sub();
547 my ($self, $file, $additional_params) = @_;
549 $additional_params ||= { };
551 $file = $self->_prepare_html_template($file, $additional_params);
553 my $template = HTML::Template->new("filename" => $file,
554 "die_on_bad_params" => 0,
556 "case_sensitive" => 1,
557 "loop_context_vars" => 1,
560 foreach my $key ($template->param()) {
561 my $param = $additional_params->{$key} || $self->{$key};
562 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
563 $template->param($key => $param);
566 my $output = $template->output();
568 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
570 $main::lxdebug->leave_sub();
575 sub parse_html_template2 {
576 $main::lxdebug->enter_sub();
578 my ($self, $file, $additional_params) = @_;
580 $additional_params ||= { };
582 $file = $self->_prepare_html_template($file, $additional_params);
584 my $template = Template->new({ 'INTERPOLATE' => 0,
590 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
593 $template->process($file, $additional_params, \$output);
595 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
597 $main::lxdebug->leave_sub();
602 sub show_generic_error {
603 my ($self, $error, $title, $action) = @_;
606 $add_params->{"title"} = $title if ($title);
607 $self->{"label_error"} = $error;
611 map({ delete($self->{$_}); } qw(action));
612 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
613 if (!ref($self->{$_})); }
615 $add_params->{"SHOW_BUTTON"} = 1;
616 $add_params->{"BUTTON_LABEL"} = $action;
618 $add_params->{"VARIABLES"} = \@vars;
621 print($self->parse_html_template("generic/error", $add_params));
623 die("Error: $error\n");
626 sub show_generic_information {
627 my ($self, $error, $title) = @_;
630 $add_params->{"title"} = $title if ($title);
631 $self->{"label_information"} = $error;
634 print($self->parse_html_template("generic/information", $add_params));
636 die("Information: $error\n");
639 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
640 # changed it to accept an arbitrary number of triggers - sschoeling
642 $main::lxdebug->enter_sub();
645 my $myconfig = shift;
648 # set dateform for jsscript
651 "dd.mm.yy" => "%d.%m.%Y",
652 "dd-mm-yy" => "%d-%m-%Y",
653 "dd/mm/yy" => "%d/%m/%Y",
654 "mm/dd/yy" => "%m/%d/%Y",
655 "mm-dd-yy" => "%m-%d-%Y",
656 "yyyy-mm-dd" => "%Y-%m-%d",
659 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
660 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
667 inputField : "| . (shift) . qq|",
668 ifFormat :"$ifFormat",
669 align : "| . (shift) . qq|",
670 button : "| . (shift) . qq|"
676 <script type="text/javascript">
677 <!--| . join("", @triggers) . qq|//-->
681 $main::lxdebug->leave_sub();
684 } #end sub write_trigger
687 $main::lxdebug->enter_sub();
689 my ($self, $msg) = @_;
691 if ($self->{callback}) {
693 ($script, $argv) = split(/\?/, $self->{callback}, 2);
695 exec("perl", "$script", $argv);
703 $main::lxdebug->leave_sub();
706 # sort of columns removed - empty sub
708 $main::lxdebug->enter_sub();
710 my ($self, @columns) = @_;
712 $main::lxdebug->leave_sub();
718 $main::lxdebug->enter_sub(2);
720 my ($self, $myconfig, $amount, $places, $dash) = @_;
725 my $neg = ($amount =~ s/-//);
727 if (defined($places) && ($places ne '')) {
732 my ($actual_places) = ($amount =~ /\.(\d+)/);
733 $actual_places = length($actual_places);
734 $places = $actual_places > $places ? $actual_places : $places;
737 $amount = $self->round_amount($amount, $places);
740 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
741 my @p = split(/\./, $amount); # split amount at decimal point
743 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
746 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
749 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
750 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
751 ($neg ? "-$amount" : "$amount" ) ;
755 $main::lxdebug->leave_sub(2);
760 $main::lxdebug->enter_sub(2);
762 my ($self, $myconfig, $amount) = @_;
764 if ( ($myconfig->{numberformat} eq '1.000,00')
765 || ($myconfig->{numberformat} eq '1000,00')) {
770 if ($myconfig->{numberformat} eq "1'000.00") {
776 $main::lxdebug->leave_sub(2);
778 return ($amount * 1);
782 $main::lxdebug->enter_sub(2);
784 my ($self, $amount, $places) = @_;
787 # Rounding like "Kaufmannsrunden"
788 # Descr. http://de.wikipedia.org/wiki/Rundung
790 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
793 $amount = $amount * (10**($places));
794 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
796 $main::lxdebug->leave_sub(2);
798 return $round_amount;
803 $main::lxdebug->enter_sub();
805 my ($self, $myconfig, $userspath) = @_;
806 my ($template, $out);
810 $self->{"cwd"} = getcwd();
811 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
813 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
814 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
815 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
816 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
817 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
818 } elsif (($self->{"format"} =~ /html/i) ||
819 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
820 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
821 } elsif (($self->{"format"} =~ /xml/i) ||
822 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
823 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
824 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
825 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
826 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
827 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
828 } elsif ( defined $self->{'format'}) {
829 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
830 } elsif ( $self->{'format'} eq '' ) {
831 $self->error("No Outputformat given: $self->{'format'}");
832 } else { #Catch the rest
833 $self->error("Outputformat not defined: $self->{'format'}");
836 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
837 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
839 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
840 qw(email tel fax name signature company address businessnumber
841 co_ustid taxnumber duns));
842 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
843 qw(company address signature));
844 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
846 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
848 # OUT is used for the media, screen, printer, email
849 # for postscript we store a copy in a temporary file
851 my $prepend_userspath;
853 if (!$self->{tmpfile}) {
854 $self->{tmpfile} = "${fileid}.$self->{IN}";
855 $prepend_userspath = 1;
858 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
860 $self->{tmpfile} =~ s|.*/||;
861 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
862 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
864 if ($template->uses_temp_file() || $self->{media} eq 'email') {
866 $self->{OUT} = ">$self->{tmpfile}";
870 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
872 open(OUT, ">-") or $self->error("STDOUT : $!");
876 if (!$template->parse(*OUT)) {
878 $self->error("$self->{IN} : " . $template->get_error());
883 if ($template->uses_temp_file() || $self->{media} eq 'email') {
885 if ($self->{media} eq 'email') {
887 my $mail = new Mailer;
889 map { $mail->{$_} = $self->{$_} }
890 qw(cc bcc subject message version format);
891 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
892 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
893 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
894 $mail->{fileid} = "$fileid.";
895 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
897 # if we send html or plain text inline
898 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
899 $mail->{contenttype} = "text/html";
901 $mail->{message} =~ s/\r\n/<br>\n/g;
902 $myconfig->{signature} =~ s/\\n/<br>\n/g;
903 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
905 open(IN, $self->{tmpfile})
906 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
908 $mail->{message} .= $_;
915 if (!$self->{"do_not_attach"}) {
916 @{ $mail->{attachments} } =
917 ({ "filename" => $self->{"tmpfile"},
918 "name" => $self->{"attachment_filename"} ?
919 $self->{"attachment_filename"} : $self->{"tmpfile"} });
922 $mail->{message} =~ s/\r\n/\n/g;
923 $myconfig->{signature} =~ s/\\n/\n/g;
924 $mail->{message} .= "\n-- \n$myconfig->{signature}";
928 my $err = $mail->send();
929 $self->error($self->cleanup . "$err") if ($err);
935 my $numbytes = (-s $self->{tmpfile});
936 open(IN, $self->{tmpfile})
937 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
939 $self->{copies} = 1 unless $self->{media} eq 'printer';
941 chdir("$self->{cwd}");
942 #print(STDERR "Kopien $self->{copies}\n");
943 #print(STDERR "OUT $self->{OUT}\n");
944 for my $i (1 .. $self->{copies}) {
946 open(OUT, $self->{OUT})
947 or $self->error($self->cleanup . "$self->{OUT} : $!");
949 $self->{attachment_filename} = ($self->{attachment_filename})
950 ? $self->{attachment_filename}
951 : $self->generate_attachment_filename();
954 print qq|Content-Type: | . $template->get_mime_type() . qq|
955 Content-Disposition: attachment; filename="$self->{attachment_filename}"
956 Content-Length: $numbytes
960 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
980 chdir("$self->{cwd}");
981 $main::lxdebug->leave_sub();
984 sub generate_attachment_filename {
987 my %formname_translations = (
988 bin_list => $main::locale->text('Bin List'),
989 credit_note => $main::locale->text('Credit Note'),
990 invoice => $main::locale->text('Invoice'),
991 packing_list => $main::locale->text('Packing List'),
992 pick_list => $main::locale->text('Pick List'),
993 proforma => $main::locale->text('Proforma Invoice'),
994 purchase_order => $main::locale->text('Purchase Order'),
995 request_quotation => $main::locale->text('RFQ'),
996 sales_order => $main::locale->text('Confirmation'),
997 sales_quotation => $main::locale->text('Quotation'),
998 storno_invoice => $main::locale->text('Storno Invoice'),
999 storno_packing_list => $main::locale->text('Storno Packing List'),
1002 my $attachment_filename = $formname_translations{$self->{"formname"}};
1004 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1005 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1008 if ($attachment_filename && $self->{"${prefix}number"}) {
1009 $attachment_filename .= "_" . $self->{"${prefix}number"}
1010 . ( $self->{format} =~ /pdf/i ? ".pdf"
1011 : $self->{format} =~ /postscript/i ? ".ps"
1012 : $self->{format} =~ /opendocument/i ? ".odt"
1013 : $self->{format} =~ /html/i ? ".html"
1015 $attachment_filename =~ s/ /_/g;
1016 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1017 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1018 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1020 $attachment_filename = "";
1023 return $attachment_filename;
1027 $main::lxdebug->enter_sub();
1031 chdir("$self->{tmpdir}");
1034 if (-f "$self->{tmpfile}.err") {
1035 open(FH, "$self->{tmpfile}.err");
1040 if ($self->{tmpfile}) {
1041 $self->{tmpfile} =~ s|.*/||g;
1043 $self->{tmpfile} =~ s/\.\w+$//g;
1044 my $tmpfile = $self->{tmpfile};
1045 unlink(<$tmpfile.*>);
1048 chdir("$self->{cwd}");
1050 $main::lxdebug->leave_sub();
1056 $main::lxdebug->enter_sub();
1058 my ($self, $date, $myconfig) = @_;
1060 if ($date && $date =~ /\D/) {
1062 if ($myconfig->{dateformat} =~ /^yy/) {
1063 ($yy, $mm, $dd) = split /\D/, $date;
1065 if ($myconfig->{dateformat} =~ /^mm/) {
1066 ($mm, $dd, $yy) = split /\D/, $date;
1068 if ($myconfig->{dateformat} =~ /^dd/) {
1069 ($dd, $mm, $yy) = split /\D/, $date;
1074 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1075 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1077 $dd = "0$dd" if ($dd < 10);
1078 $mm = "0$mm" if ($mm < 10);
1080 $date = "$yy$mm$dd";
1083 $main::lxdebug->leave_sub();
1088 # Database routines used throughout
1091 $main::lxdebug->enter_sub(2);
1093 my ($self, $myconfig) = @_;
1095 # connect to database
1097 DBI->connect($myconfig->{dbconnect},
1098 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1102 if ($myconfig->{dboptions}) {
1103 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1106 $main::lxdebug->leave_sub(2);
1111 sub dbconnect_noauto {
1112 $main::lxdebug->enter_sub();
1114 my ($self, $myconfig) = @_;
1116 # connect to database
1118 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1119 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1123 if ($myconfig->{dboptions}) {
1124 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1127 $main::lxdebug->leave_sub();
1132 sub get_standard_dbh {
1133 $main::lxdebug->enter_sub(2);
1135 my ($self, $myconfig) = @_;
1137 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1139 $main::lxdebug->leave_sub(2);
1141 return $standard_dbh;
1144 sub update_balance {
1145 $main::lxdebug->enter_sub();
1147 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1149 # if we have a value, go do it
1152 # retrieve balance from table
1153 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1154 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1155 my ($balance) = $sth->fetchrow_array;
1161 $query = "UPDATE $table SET $field = $balance WHERE $where";
1162 do_query($self, $dbh, $query, @values);
1164 $main::lxdebug->leave_sub();
1167 sub update_exchangerate {
1168 $main::lxdebug->enter_sub();
1170 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1172 # some sanity check for currency
1174 $main::lxdebug->leave_sub();
1177 my $query = qq|SELECT curr FROM defaults|;
1179 my ($currency) = selectrow_query($self, $dbh, $query);
1180 my ($defaultcurrency) = split m/:/, $currency;
1183 if ($curr eq $defaultcurrency) {
1184 $main::lxdebug->leave_sub();
1188 my $query = qq|SELECT e.curr FROM exchangerate e
1189 WHERE e.curr = ? AND e.transdate = ?
1191 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1200 $buy = conv_i($buy, "NULL");
1201 $sell = conv_i($sell, "NULL");
1204 if ($buy != 0 && $sell != 0) {
1205 $set = "buy = $buy, sell = $sell";
1206 } elsif ($buy != 0) {
1207 $set = "buy = $buy";
1208 } elsif ($sell != 0) {
1209 $set = "sell = $sell";
1212 if ($sth->fetchrow_array) {
1213 $query = qq|UPDATE exchangerate
1219 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1220 VALUES (?, $buy, $sell, ?)|;
1223 do_query($self, $dbh, $query, $curr, $transdate);
1225 $main::lxdebug->leave_sub();
1228 sub save_exchangerate {
1229 $main::lxdebug->enter_sub();
1231 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1233 my $dbh = $self->dbconnect($myconfig);
1237 $buy = $rate if $fld eq 'buy';
1238 $sell = $rate if $fld eq 'sell';
1241 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1246 $main::lxdebug->leave_sub();
1249 sub get_exchangerate {
1250 $main::lxdebug->enter_sub();
1252 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1254 unless ($transdate) {
1255 $main::lxdebug->leave_sub();
1259 my $query = qq|SELECT curr FROM defaults|;
1261 my ($currency) = selectrow_query($self, $dbh, $query);
1262 my ($defaultcurrency) = split m/:/, $currency;
1264 if ($currency eq $defaultcurrency) {
1265 $main::lxdebug->leave_sub();
1269 my $query = qq|SELECT e.$fld FROM exchangerate e
1270 WHERE e.curr = ? AND e.transdate = ?|;
1271 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1275 $main::lxdebug->leave_sub();
1277 return $exchangerate;
1280 sub check_exchangerate {
1281 $main::lxdebug->enter_sub();
1283 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1285 unless ($transdate) {
1286 $main::lxdebug->leave_sub();
1290 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1292 if ($currency eq $defaultcurrency) {
1293 $main::lxdebug->leave_sub();
1297 my $dbh = $self->get_standard_dbh($myconfig);
1298 my $query = qq|SELECT e.$fld FROM exchangerate e
1299 WHERE e.curr = ? AND e.transdate = ?|;
1301 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1303 $exchangerate = 1 if ($exchangerate eq "");
1305 $main::lxdebug->leave_sub();
1307 return $exchangerate;
1310 sub get_default_currency {
1311 $main::lxdebug->enter_sub();
1313 my ($self, $myconfig) = @_;
1314 my $dbh = $self->get_standard_dbh($myconfig);
1316 my $query = qq|SELECT curr FROM defaults|;
1318 my ($curr) = selectrow_query($self, $dbh, $query);
1319 my ($defaultcurrency) = split m/:/, $curr;
1321 $main::lxdebug->leave_sub();
1323 return $defaultcurrency;
1327 sub set_payment_options {
1328 $main::lxdebug->enter_sub();
1330 my ($self, $myconfig, $transdate) = @_;
1332 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1334 my $dbh = $self->get_standard_dbh($myconfig);
1337 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1338 qq|FROM payment_terms p | .
1341 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1342 $self->{payment_terms}) =
1343 selectrow_query($self, $dbh, $query, $self->{payment_id});
1345 if ($transdate eq "") {
1346 if ($self->{invdate}) {
1347 $transdate = $self->{invdate};
1349 $transdate = $self->{transdate};
1354 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1355 qq|FROM payment_terms|;
1356 ($self->{netto_date}, $self->{skonto_date}) =
1357 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1359 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1360 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1361 $self->{percent_skonto};
1363 $self->{skonto_amount} =
1364 $self->format_amount($myconfig, $skonto_amount, 2);
1366 if ($self->{"language_id"}) {
1368 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1369 qq|FROM translation_payment_terms t | .
1370 qq|LEFT JOIN language l ON t.language_id = l.id | .
1371 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1372 my ($description_long, $output_numberformat, $output_dateformat,
1373 $output_longdates) =
1374 selectrow_query($self, $dbh, $query,
1375 $self->{"language_id"}, $self->{"payment_id"});
1377 $self->{payment_terms} = $description_long if ($description_long);
1379 if ($output_dateformat) {
1380 foreach my $key (qw(netto_date skonto_date)) {
1382 $main::locale->reformat_date($myconfig, $self->{$key},
1388 if ($output_numberformat &&
1389 ($output_numberformat ne $myconfig->{"numberformat"})) {
1390 my $saved_numberformat = $myconfig->{"numberformat"};
1391 $myconfig->{"numberformat"} = $output_numberformat;
1392 $self->{skonto_amount} =
1393 $self->format_amount($myconfig, $skonto_amount, 2);
1394 $myconfig->{"numberformat"} = $saved_numberformat;
1398 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1399 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1400 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1401 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1402 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1403 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1404 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1405 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1406 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1407 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1409 $main::lxdebug->leave_sub();
1413 sub get_template_language {
1414 $main::lxdebug->enter_sub();
1416 my ($self, $myconfig) = @_;
1418 my $template_code = "";
1420 if ($self->{language_id}) {
1421 my $dbh = $self->get_standard_dbh($myconfig);
1422 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1423 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1426 $main::lxdebug->leave_sub();
1428 return $template_code;
1431 sub get_printer_code {
1432 $main::lxdebug->enter_sub();
1434 my ($self, $myconfig) = @_;
1436 my $template_code = "";
1438 if ($self->{printer_id}) {
1439 my $dbh = $self->get_standard_dbh($myconfig);
1440 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1441 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1444 $main::lxdebug->leave_sub();
1446 return $template_code;
1450 $main::lxdebug->enter_sub();
1452 my ($self, $myconfig) = @_;
1454 my $template_code = "";
1456 if ($self->{shipto_id}) {
1457 my $dbh = $self->get_standard_dbh($myconfig);
1458 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1459 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1460 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1463 $main::lxdebug->leave_sub();
1467 $main::lxdebug->enter_sub();
1469 my ($self, $dbh, $id, $module) = @_;
1474 foreach my $item (qw(name department_1 department_2 street zipcode city country
1475 contact phone fax email)) {
1476 if ($self->{"shipto$item"}) {
1477 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1479 push(@values, $self->{"shipto${item}"});
1483 if ($self->{shipto_id}) {
1484 my $query = qq|UPDATE shipto set
1486 shiptodepartment_1 = ?,
1487 shiptodepartment_2 = ?,
1496 WHERE shipto_id = ?|;
1497 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1499 my $query = qq|SELECT * FROM shipto
1500 WHERE shiptoname = ? AND
1501 shiptodepartment_1 = ? AND
1502 shiptodepartment_2 = ? AND
1503 shiptostreet = ? AND
1504 shiptozipcode = ? AND
1506 shiptocountry = ? AND
1507 shiptocontact = ? AND
1513 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1516 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1517 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1518 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1519 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1520 do_query($self, $dbh, $query, $id, @values, $module);
1525 $main::lxdebug->leave_sub();
1529 $main::lxdebug->enter_sub();
1531 my ($self, $dbh) = @_;
1533 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1534 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1535 $self->{"employee_id"} *= 1;
1537 $main::lxdebug->leave_sub();
1541 $main::lxdebug->enter_sub();
1543 my ($self, $myconfig, $salesman_id) = @_;
1545 $main::lxdebug->leave_sub() and return unless $salesman_id;
1547 my $dbh = $self->get_standard_dbh($myconfig);
1550 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1554 my $user = new User($main::memberfile, $login);
1555 map({ $self->{"salesman_$_"} = $user->{$_}; }
1556 qw(address businessnumber co_ustid company duns email fax name
1558 $self->{salesman_login} = $login;
1560 $self->{salesman_name} = $login
1561 if ($self->{salesman_name} eq "");
1563 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1566 $main::lxdebug->leave_sub();
1570 $main::lxdebug->enter_sub();
1572 my ($self, $myconfig) = @_;
1574 my $dbh = $self->get_standard_dbh($myconfig);
1575 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1576 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1578 $main::lxdebug->leave_sub();
1582 $main::lxdebug->enter_sub();
1584 my ($self, $dbh, $id, $key) = @_;
1586 $key = "all_contacts" unless ($key);
1589 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1590 qq|FROM contacts | .
1591 qq|WHERE cp_cv_id = ? | .
1592 qq|ORDER BY lower(cp_name)|;
1594 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1596 $main::lxdebug->leave_sub();
1600 $main::lxdebug->enter_sub();
1602 my ($self, $dbh, $key) = @_;
1604 my ($all, $old_id, $where, @values);
1606 if (ref($key) eq "HASH") {
1609 $key = "ALL_PROJECTS";
1611 foreach my $p (keys(%{$params})) {
1613 $all = $params->{$p};
1614 } elsif ($p eq "old_id") {
1615 $old_id = $params->{$p};
1616 } elsif ($p eq "key") {
1617 $key = $params->{$p};
1623 $where = "WHERE active ";
1625 if (ref($old_id) eq "ARRAY") {
1626 my @ids = grep({ $_ } @{$old_id});
1628 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1629 push(@values, @ids);
1632 $where .= " OR (id = ?) ";
1633 push(@values, $old_id);
1639 qq|SELECT id, projectnumber, description, active | .
1642 qq|ORDER BY lower(projectnumber)|;
1644 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1646 $main::lxdebug->leave_sub();
1650 $main::lxdebug->enter_sub();
1652 my ($self, $dbh, $vc_id, $key) = @_;
1654 $key = "all_shipto" unless ($key);
1656 # get shipping addresses
1657 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1659 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1661 $main::lxdebug->leave_sub();
1665 $main::lxdebug->enter_sub();
1667 my ($self, $dbh, $key) = @_;
1669 $key = "all_printers" unless ($key);
1671 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1673 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1675 $main::lxdebug->leave_sub();
1679 $main::lxdebug->enter_sub();
1681 my ($self, $dbh, $params) = @_;
1683 $key = $params->{key};
1684 $key = "all_charts" unless ($key);
1686 my $transdate = quote_db_date($params->{transdate});
1689 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1691 qq|LEFT JOIN taxkeys tk ON | .
1692 qq|(tk.id = (SELECT id FROM taxkeys | .
1693 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1694 qq| ORDER BY startdate DESC LIMIT 1)) | .
1695 qq|ORDER BY c.accno|;
1697 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1699 $main::lxdebug->leave_sub();
1702 sub _get_taxcharts {
1703 $main::lxdebug->enter_sub();
1705 my ($self, $dbh, $key) = @_;
1707 $key = "all_taxcharts" unless ($key);
1709 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1711 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1713 $main::lxdebug->leave_sub();
1717 $main::lxdebug->enter_sub();
1719 my ($self, $dbh, $key) = @_;
1721 $key = "all_taxzones" unless ($key);
1723 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1725 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1727 $main::lxdebug->leave_sub();
1730 sub _get_employees {
1731 $main::lxdebug->enter_sub();
1733 my ($self, $dbh, $default_key, $key) = @_;
1735 $key = $default_key unless ($key);
1736 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1738 $main::lxdebug->leave_sub();
1741 sub _get_business_types {
1742 $main::lxdebug->enter_sub();
1744 my ($self, $dbh, $key) = @_;
1746 $key = "all_business_types" unless ($key);
1748 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1750 $main::lxdebug->leave_sub();
1753 sub _get_languages {
1754 $main::lxdebug->enter_sub();
1756 my ($self, $dbh, $key) = @_;
1758 $key = "all_languages" unless ($key);
1760 my $query = qq|SELECT * FROM language ORDER BY id|;
1762 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1764 $main::lxdebug->leave_sub();
1767 sub _get_dunning_configs {
1768 $main::lxdebug->enter_sub();
1770 my ($self, $dbh, $key) = @_;
1772 $key = "all_dunning_configs" unless ($key);
1774 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1776 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1778 $main::lxdebug->leave_sub();
1781 sub _get_currencies {
1782 $main::lxdebug->enter_sub();
1784 my ($self, $dbh, $key) = @_;
1786 $key = "all_currencies" unless ($key);
1788 my $query = qq|SELECT curr AS currency FROM defaults|;
1790 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1792 $main::lxdebug->leave_sub();
1796 $main::lxdebug->enter_sub();
1798 my ($self, $dbh, $key) = @_;
1800 $key = "all_payments" unless ($key);
1802 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1804 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1806 $main::lxdebug->leave_sub();
1809 sub _get_customers {
1810 $main::lxdebug->enter_sub();
1812 my ($self, $dbh, $key) = @_;
1814 $key = "all_customers" unless ($key);
1816 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name|;
1818 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1820 $main::lxdebug->leave_sub();
1824 $main::lxdebug->enter_sub();
1826 my ($self, $dbh, $key) = @_;
1828 $key = "all_vendors" unless ($key);
1830 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1832 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1834 $main::lxdebug->leave_sub();
1837 sub _get_departments {
1838 $main::lxdebug->enter_sub();
1840 my ($self, $dbh, $key) = @_;
1842 $key = "all_departments" unless ($key);
1844 my $query = qq|SELECT * FROM department ORDER BY description|;
1846 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1848 $main::lxdebug->leave_sub();
1852 $main::lxdebug->enter_sub();
1857 my $dbh = $self->get_standard_dbh(\%main::myconfig);
1858 my ($sth, $query, $ref);
1860 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1861 my $vc_id = $self->{"${vc}_id"};
1863 if ($params{"contacts"}) {
1864 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1867 if ($params{"shipto"}) {
1868 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1871 if ($params{"projects"} || $params{"all_projects"}) {
1872 $self->_get_projects($dbh, $params{"all_projects"} ?
1873 $params{"all_projects"} : $params{"projects"},
1874 $params{"all_projects"} ? 1 : 0);
1877 if ($params{"printers"}) {
1878 $self->_get_printers($dbh, $params{"printers"});
1881 if ($params{"languages"}) {
1882 $self->_get_languages($dbh, $params{"languages"});
1885 if ($params{"charts"}) {
1886 $self->_get_charts($dbh, $params{"charts"});
1889 if ($params{"taxcharts"}) {
1890 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1893 if ($params{"taxzones"}) {
1894 $self->_get_taxzones($dbh, $params{"taxzones"});
1897 if ($params{"employees"}) {
1898 $self->_get_employees($dbh, "all_employees", $params{"employees"});
1901 if ($params{"salesmen"}) {
1902 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
1905 if ($params{"business_types"}) {
1906 $self->_get_business_types($dbh, $params{"business_types"});
1909 if ($params{"dunning_configs"}) {
1910 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1913 if($params{"currencies"}) {
1914 $self->_get_currencies($dbh, $params{"currencies"});
1917 if($params{"customers"}) {
1918 $self->_get_customers($dbh, $params{"customers"});
1921 if($params{"vendors"}) {
1922 $self->_get_vendors($dbh, $params{"vendors"});
1925 if($params{"payments"}) {
1926 $self->_get_payments($dbh, $params{"payments"});
1929 if($params{"departments"}) {
1930 $self->_get_departments($dbh, $params{"departments"});
1933 $main::lxdebug->leave_sub();
1936 # this sub gets the id and name from $table
1938 $main::lxdebug->enter_sub();
1940 my ($self, $myconfig, $table) = @_;
1942 # connect to database
1943 my $dbh = $self->get_standard_dbh($myconfig);
1945 $table = $table eq "customer" ? "customer" : "vendor";
1946 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1948 my ($query, @values);
1950 if (!$self->{openinvoices}) {
1952 if ($self->{customernumber} ne "") {
1953 $where = qq|(vc.customernumber ILIKE ?)|;
1954 push(@values, '%' . $self->{customernumber} . '%');
1956 $where = qq|(vc.name ILIKE ?)|;
1957 push(@values, '%' . $self->{$table} . '%');
1961 qq~SELECT vc.id, vc.name,
1962 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1964 WHERE $where AND (NOT vc.obsolete)
1968 qq~SELECT DISTINCT vc.id, vc.name,
1969 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1971 JOIN $table vc ON (a.${table}_id = vc.id)
1972 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1974 push(@values, '%' . $self->{$table} . '%');
1977 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1979 $main::lxdebug->leave_sub();
1981 return scalar(@{ $self->{name_list} });
1984 # the selection sub is used in the AR, AP, IS, IR and OE module
1987 $main::lxdebug->enter_sub();
1989 my ($self, $myconfig, $table, $module) = @_;
1992 my $dbh = $self->get_standard_dbh($myconfig);
1994 $table = $table eq "customer" ? "customer" : "vendor";
1996 my $query = qq|SELECT count(*) FROM $table|;
1997 my ($count) = selectrow_query($self, $dbh, $query);
1999 # build selection list
2000 if ($count < $myconfig->{vclimit}) {
2001 $query = qq|SELECT id, name, salesman_id
2002 FROM $table WHERE NOT obsolete
2004 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2008 $self->get_employee($dbh);
2010 # setup sales contacts
2011 $query = qq|SELECT e.id, e.name
2013 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2014 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2017 push(@{ $self->{all_employees} },
2018 { id => $self->{employee_id},
2019 name => $self->{employee} });
2021 # sort the whole thing
2022 @{ $self->{all_employees} } =
2023 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2025 if ($module eq 'AR') {
2027 # prepare query for departments
2028 $query = qq|SELECT id, description
2031 ORDER BY description|;
2034 $query = qq|SELECT id, description
2036 ORDER BY description|;
2039 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2042 $query = qq|SELECT id, description
2046 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2049 $query = qq|SELECT printer_description, id
2051 ORDER BY printer_description|;
2053 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2056 $query = qq|SELECT id, description
2060 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2062 $main::lxdebug->leave_sub();
2065 sub language_payment {
2066 $main::lxdebug->enter_sub();
2068 my ($self, $myconfig) = @_;
2070 my $dbh = $self->get_standard_dbh($myconfig);
2072 my $query = qq|SELECT id, description
2076 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2079 $query = qq|SELECT printer_description, id
2081 ORDER BY printer_description|;
2083 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2086 $query = qq|SELECT id, description
2090 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2092 # get buchungsgruppen
2093 $query = qq|SELECT id, description
2094 FROM buchungsgruppen|;
2096 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2098 $main::lxdebug->leave_sub();
2101 # this is only used for reports
2102 sub all_departments {
2103 $main::lxdebug->enter_sub();
2105 my ($self, $myconfig, $table) = @_;
2107 my $dbh = $self->get_standard_dbh($myconfig);
2110 if ($table eq 'customer') {
2111 $where = "WHERE role = 'P' ";
2114 my $query = qq|SELECT id, description
2117 ORDER BY description|;
2118 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2120 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2122 $main::lxdebug->leave_sub();
2126 $main::lxdebug->enter_sub();
2128 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2131 if ($table eq "customer") {
2140 $self->all_vc($myconfig, $table, $module);
2142 # get last customers or vendors
2143 my ($query, $sth, $ref);
2145 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2150 my $transdate = "current_date";
2151 if ($self->{transdate}) {
2152 $transdate = $dbh->quote($self->{transdate});
2155 # now get the account numbers
2156 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2157 FROM chart c, taxkeys tk
2158 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2159 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2162 $sth = $dbh->prepare($query);
2164 do_statement($self, $sth, $query, '%' . $module . '%');
2166 $self->{accounts} = "";
2167 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2169 foreach my $key (split(/:/, $ref->{link})) {
2170 if ($key =~ /$module/) {
2172 # cross reference for keys
2173 $xkeyref{ $ref->{accno} } = $key;
2175 push @{ $self->{"${module}_links"}{$key} },
2176 { accno => $ref->{accno},
2177 description => $ref->{description},
2178 taxkey => $ref->{taxkey_id},
2179 tax_id => $ref->{tax_id} };
2181 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2187 # get taxkeys and description
2188 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2189 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2191 if (($module eq "AP") || ($module eq "AR")) {
2192 # get tax rates and description
2193 $query = qq|SELECT * FROM tax|;
2194 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2200 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2201 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2202 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2203 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2205 d.description AS department,
2208 JOIN $table c ON (a.${table}_id = c.id)
2209 LEFT JOIN employee e ON (e.id = a.employee_id)
2210 LEFT JOIN department d ON (d.id = a.department_id)
2212 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2214 foreach $key (keys %$ref) {
2215 $self->{$key} = $ref->{$key};
2218 my $transdate = "current_date";
2219 if ($self->{transdate}) {
2220 $transdate = $dbh->quote($self->{transdate});
2223 # now get the account numbers
2224 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2226 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2228 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2229 OR c.link LIKE '%_tax%')
2232 $sth = $dbh->prepare($query);
2233 do_statement($self, $sth, $query, "%$module%");
2235 $self->{accounts} = "";
2236 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2238 foreach my $key (split(/:/, $ref->{link})) {
2239 if ($key =~ /$module/) {
2241 # cross reference for keys
2242 $xkeyref{ $ref->{accno} } = $key;
2244 push @{ $self->{"${module}_links"}{$key} },
2245 { accno => $ref->{accno},
2246 description => $ref->{description},
2247 taxkey => $ref->{taxkey_id},
2248 tax_id => $ref->{tax_id} };
2250 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2256 # get amounts from individual entries
2259 c.accno, c.description,
2260 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2264 LEFT JOIN chart c ON (c.id = a.chart_id)
2265 LEFT JOIN project p ON (p.id = a.project_id)
2266 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2267 WHERE (tk.taxkey_id=a.taxkey) AND
2268 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2269 THEN tk.chart_id = a.chart_id
2272 OR (c.link='%tax%')) AND
2273 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2274 WHERE a.trans_id = ?
2275 AND a.fx_transaction = '0'
2276 ORDER BY a.oid, a.transdate|;
2277 $sth = $dbh->prepare($query);
2278 do_statement($self, $sth, $query, $self->{id});
2280 # get exchangerate for currency
2281 $self->{exchangerate} =
2282 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2285 # store amounts in {acc_trans}{$key} for multiple accounts
2286 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2287 $ref->{exchangerate} =
2288 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2289 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2292 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2293 $ref->{amount} *= -1;
2295 $ref->{index} = $index;
2297 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2303 d.curr AS currencies, d.closedto, d.revtrans,
2304 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2305 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2307 $ref = selectfirst_hashref_query($self, $dbh, $query);
2308 map { $self->{$_} = $ref->{$_} } keys %$ref;
2315 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2316 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2317 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2319 $ref = selectfirst_hashref_query($self, $dbh, $query);
2320 map { $self->{$_} = $ref->{$_} } keys %$ref;
2322 if ($self->{"$self->{vc}_id"}) {
2324 # only setup currency
2325 ($self->{currency}) = split(/:/, $self->{currencies});
2329 $self->lastname_used($dbh, $myconfig, $table, $module);
2331 # get exchangerate for currency
2332 $self->{exchangerate} =
2333 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2339 $main::lxdebug->leave_sub();
2343 $main::lxdebug->enter_sub();
2345 my ($self, $dbh, $myconfig, $table, $module) = @_;
2347 my $arap = ($table eq 'customer') ? "ar" : "ap";
2348 $table = $table eq "customer" ? "customer" : "vendor";
2349 my $where = "1 = 1";
2351 if ($self->{type} =~ /_order/) {
2353 $where = "quotation = '0'";
2355 if ($self->{type} =~ /_quotation/) {
2357 $where = "quotation = '1'";
2360 my $query = qq|SELECT MAX(id) FROM $arap
2361 WHERE $where AND ${table}_id > 0|;
2362 my ($trans_id) = selectrow_query($self, $dbh, $query);
2367 a.curr, a.${table}_id, a.department_id,
2368 d.description AS department,
2369 ct.name, current_date + ct.terms AS duedate
2371 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2372 LEFT JOIN department d ON (a.department_id = d.id)
2374 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2375 $self->{department}, $self->{$table}, $self->{duedate})
2376 = selectrow_query($self, $dbh, $query, $trans_id);
2378 $main::lxdebug->leave_sub();
2382 $main::lxdebug->enter_sub();
2384 my ($self, $myconfig, $thisdate, $days) = @_;
2386 my $dbh = $self->get_standard_dbh($myconfig);
2391 my $dateformat = $myconfig->{dateformat};
2392 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2393 $thisdate = $dbh->quote($thisdate);
2394 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2396 $query = qq|SELECT current_date AS thisdate|;
2399 ($thisdate) = selectrow_query($self, $dbh, $query);
2401 $main::lxdebug->leave_sub();
2407 $main::lxdebug->enter_sub();
2409 my ($self, $string) = @_;
2411 if ($string !~ /%/) {
2412 $string = "%$string%";
2415 $string =~ s/\'/\'\'/g;
2417 $main::lxdebug->leave_sub();
2423 $main::lxdebug->enter_sub();
2425 my ($self, $flds, $new, $count, $numrows) = @_;
2429 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2435 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2437 $j = $item->{ndx} - 1;
2438 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2442 for $i ($count + 1 .. $numrows) {
2443 map { delete $self->{"${_}_$i"} } @{$flds};
2446 $main::lxdebug->leave_sub();
2450 $main::lxdebug->enter_sub();
2452 my ($self, $myconfig) = @_;
2456 my $dbh = $self->dbconnect_noauto($myconfig);
2458 my $query = qq|DELETE FROM status
2459 WHERE (formname = ?) AND (trans_id = ?)|;
2460 my $sth = prepare_query($self, $dbh, $query);
2462 if ($self->{formname} =~ /(check|receipt)/) {
2463 for $i (1 .. $self->{rowcount}) {
2464 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2467 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2471 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2472 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2474 my %queued = split / /, $self->{queued};
2477 if ($self->{formname} =~ /(check|receipt)/) {
2479 # this is a check or receipt, add one entry for each lineitem
2480 my ($accno) = split /--/, $self->{account};
2481 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2482 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2483 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2484 $sth = prepare_query($self, $dbh, $query);
2486 for $i (1 .. $self->{rowcount}) {
2487 if ($self->{"checked_$i"}) {
2488 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2494 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2495 VALUES (?, ?, ?, ?, ?)|;
2496 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2497 $queued{$self->{formname}}, $self->{formname});
2503 $main::lxdebug->leave_sub();
2507 $main::lxdebug->enter_sub();
2509 my ($self, $dbh) = @_;
2511 my ($query, $printed, $emailed);
2513 my $formnames = $self->{printed};
2514 my $emailforms = $self->{emailed};
2516 my $query = qq|DELETE FROM status
2517 WHERE (formname = ?) AND (trans_id = ?)|;
2518 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2520 # this only applies to the forms
2521 # checks and receipts are posted when printed or queued
2523 if ($self->{queued}) {
2524 my %queued = split / /, $self->{queued};
2526 foreach my $formname (keys %queued) {
2527 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2528 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2530 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2531 VALUES (?, ?, ?, ?, ?)|;
2532 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2534 $formnames =~ s/$self->{formname}//;
2535 $emailforms =~ s/$self->{formname}//;
2540 # save printed, emailed info
2541 $formnames =~ s/^ +//g;
2542 $emailforms =~ s/^ +//g;
2545 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2546 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2548 foreach my $formname (keys %status) {
2549 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2550 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2552 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2553 VALUES (?, ?, ?, ?)|;
2554 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2557 $main::lxdebug->leave_sub();
2561 # $main::locale->text('SAVED')
2562 # $main::locale->text('DELETED')
2563 # $main::locale->text('ADDED')
2564 # $main::locale->text('PAYMENT POSTED')
2565 # $main::locale->text('POSTED')
2566 # $main::locale->text('POSTED AS NEW')
2567 # $main::locale->text('ELSE')
2568 # $main::locale->text('SAVED FOR DUNNING')
2569 # $main::locale->text('DUNNING STARTED')
2570 # $main::locale->text('PRINTED')
2571 # $main::locale->text('MAILED')
2572 # $main::locale->text('SCREENED')
2573 # $main::locale->text('CANCELED')
2574 # $main::locale->text('invoice')
2575 # $main::locale->text('proforma')
2576 # $main::locale->text('sales_order')
2577 # $main::locale->text('packing_list')
2578 # $main::locale->text('pick_list')
2579 # $main::locale->text('purchase_order')
2580 # $main::locale->text('bin_list')
2581 # $main::locale->text('sales_quotation')
2582 # $main::locale->text('request_quotation')
2585 $main::lxdebug->enter_sub();
2590 if(!exists $self->{employee_id}) {
2591 &get_employee($self, $dbh);
2595 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2596 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2597 my @values = (conv_i($self->{id}), $self->{login},
2598 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2599 do_query($self, $dbh, $query, @values);
2601 $main::lxdebug->leave_sub();
2605 $main::lxdebug->enter_sub();
2607 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2608 my ($orderBy, $desc) = split(/\-\-/, $order);
2609 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2612 if ($trans_id ne "") {
2614 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 | .
2615 qq|FROM history_erp h | .
2616 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2617 qq|WHERE trans_id = | . $trans_id
2618 . $restriction . qq| |
2621 my $sth = $dbh->prepare($query) || $self->dberror($query);
2623 $sth->execute() || $self->dberror("$query");
2625 while(my $hash_ref = $sth->fetchrow_hashref()) {
2626 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2627 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2628 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2629 $tempArray[$i++] = $hash_ref;
2631 $main::lxdebug->leave_sub() and return \@tempArray
2632 if ($i > 0 && $tempArray[0] ne "");
2634 $main::lxdebug->leave_sub();
2638 sub update_defaults {
2639 $main::lxdebug->enter_sub();
2641 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2644 if ($provided_dbh) {
2645 $dbh = $provided_dbh;
2647 $dbh = $self->dbconnect_noauto($myconfig);
2649 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2650 my $sth = $dbh->prepare($query);
2652 $sth->execute || $self->dberror($query);
2653 my ($var) = $sth->fetchrow_array;
2656 if ($var =~ m/\d+$/) {
2657 my $new_var = (substr $var, $-[0]) * 1 + 1;
2658 my $len_diff = length($var) - $-[0] - length($new_var);
2659 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2665 $query = qq|UPDATE defaults SET $fld = ?|;
2666 do_query($self, $dbh, $query, $var);
2668 if (!$provided_dbh) {
2673 $main::lxdebug->leave_sub();
2678 sub update_business {
2679 $main::lxdebug->enter_sub();
2681 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2684 if ($provided_dbh) {
2685 $dbh = $provided_dbh;
2687 $dbh = $self->dbconnect_noauto($myconfig);
2690 qq|SELECT customernumberinit FROM business
2691 WHERE id = ? FOR UPDATE|;
2692 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2694 if ($var =~ m/\d+$/) {
2695 my $new_var = (substr $var, $-[0]) * 1 + 1;
2696 my $len_diff = length($var) - $-[0] - length($new_var);
2697 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2703 $query = qq|UPDATE business
2704 SET customernumberinit = ?
2706 do_query($self, $dbh, $query, $var, $business_id);
2708 if (!$provided_dbh) {
2713 $main::lxdebug->leave_sub();
2718 sub get_partsgroup {
2719 $main::lxdebug->enter_sub();
2721 my ($self, $myconfig, $p) = @_;
2723 my $dbh = $self->get_standard_dbh($myconfig);
2725 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2727 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2730 if ($p->{searchitems} eq 'part') {
2731 $query .= qq|WHERE p.inventory_accno_id > 0|;
2733 if ($p->{searchitems} eq 'service') {
2734 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2736 if ($p->{searchitems} eq 'assembly') {
2737 $query .= qq|WHERE p.assembly = '1'|;
2739 if ($p->{searchitems} eq 'labor') {
2740 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2743 $query .= qq|ORDER BY partsgroup|;
2746 $query = qq|SELECT id, partsgroup FROM partsgroup
2747 ORDER BY partsgroup|;
2750 if ($p->{language_code}) {
2751 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2752 t.description AS translation
2754 JOIN parts p ON (p.partsgroup_id = pg.id)
2755 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2756 ORDER BY translation|;
2757 @values = ($p->{language_code});
2760 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2762 $main::lxdebug->leave_sub();
2765 sub get_pricegroup {
2766 $main::lxdebug->enter_sub();
2768 my ($self, $myconfig, $p) = @_;
2770 my $dbh = $self->get_standard_dbh($myconfig);
2772 my $query = qq|SELECT p.id, p.pricegroup
2775 $query .= qq| ORDER BY pricegroup|;
2778 $query = qq|SELECT id, pricegroup FROM pricegroup
2779 ORDER BY pricegroup|;
2782 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2784 $main::lxdebug->leave_sub();
2788 # usage $form->all_years($myconfig, [$dbh])
2789 # return list of all years where bookings found
2792 $main::lxdebug->enter_sub();
2794 my ($self, $myconfig, $dbh) = @_;
2796 $dbh ||= $self->get_standard_dbh($myconfig);
2799 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2800 (SELECT MAX(transdate) FROM acc_trans)|;
2801 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2803 if ($myconfig->{dateformat} =~ /^yy/) {
2804 ($startdate) = split /\W/, $startdate;
2805 ($enddate) = split /\W/, $enddate;
2807 (@_) = split /\W/, $startdate;
2809 (@_) = split /\W/, $enddate;
2814 $startdate = substr($startdate,0,4);
2815 $enddate = substr($enddate,0,4);
2817 while ($enddate >= $startdate) {
2818 push @all_years, $enddate--;
2823 $main::lxdebug->leave_sub();