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.2";
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 $main::lxdebug->leave_sub();
540 sub parse_html_template {
541 $main::lxdebug->enter_sub();
543 my ($self, $file, $additional_params) = @_;
545 $additional_params ||= { };
547 $file = $self->_prepare_html_template($file, $additional_params);
549 my $template = HTML::Template->new("filename" => $file,
550 "die_on_bad_params" => 0,
552 "case_sensitive" => 1,
553 "loop_context_vars" => 1,
556 foreach my $key ($template->param()) {
557 my $param = $additional_params->{$key} || $self->{$key};
558 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
559 $template->param($key => $param);
562 my $output = $template->output();
564 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
566 $main::lxdebug->leave_sub();
571 sub parse_html_template2 {
572 $main::lxdebug->enter_sub();
574 my ($self, $file, $additional_params) = @_;
576 $additional_params ||= { };
578 $file = $self->_prepare_html_template($file, $additional_params);
580 my $template = Template->new({ 'INTERPOLATE' => 0,
581 'PRE_CHOMP' => Template::Constants::CHOMP_COLLAPSE,
582 'POST_CHOMP' => Template::Constants::CHOMP_COLLAPSE,
588 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
591 $template->process($file, $additional_params, \$output);
592 $main::lxdebug->message(0, $output);
594 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
596 $main::lxdebug->leave_sub();
601 sub show_generic_error {
602 my ($self, $error, $title, $action) = @_;
605 $add_params->{"title"} = $title if ($title);
606 $self->{"label_error"} = $error;
610 map({ delete($self->{$_}); } qw(action));
611 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
612 if (!ref($self->{$_})); }
614 $add_params->{"SHOW_BUTTON"} = 1;
615 $add_params->{"BUTTON_LABEL"} = $action;
617 $add_params->{"VARIABLES"} = \@vars;
620 print($self->parse_html_template("generic/error", $add_params));
622 die("Error: $error\n");
625 sub show_generic_information {
626 my ($self, $error, $title) = @_;
629 $add_params->{"title"} = $title if ($title);
630 $self->{"label_information"} = $error;
633 print($self->parse_html_template("generic/information", $add_params));
635 die("Information: $error\n");
638 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
639 # changed it to accept an arbitrary number of triggers - sschoeling
641 $main::lxdebug->enter_sub();
644 my $myconfig = shift;
647 # set dateform for jsscript
650 "dd.mm.yy" => "%d.%m.%Y",
651 "dd-mm-yy" => "%d-%m-%Y",
652 "dd/mm/yy" => "%d/%m/%Y",
653 "mm/dd/yy" => "%m/%d/%Y",
654 "mm-dd-yy" => "%m-%d-%Y",
655 "yyyy-mm-dd" => "%Y-%m-%d",
658 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
659 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
666 inputField : "| . (shift) . qq|",
667 ifFormat :"$ifFormat",
668 align : "| . (shift) . qq|",
669 button : "| . (shift) . qq|"
675 <script type="text/javascript">
676 <!--| . join("", @triggers) . qq|//-->
680 $main::lxdebug->leave_sub();
683 } #end sub write_trigger
686 $main::lxdebug->enter_sub();
688 my ($self, $msg) = @_;
690 if ($self->{callback}) {
692 ($script, $argv) = split(/\?/, $self->{callback});
693 exec("perl", "$script", $argv);
701 $main::lxdebug->leave_sub();
704 # sort of columns removed - empty sub
706 $main::lxdebug->enter_sub();
708 my ($self, @columns) = @_;
710 $main::lxdebug->leave_sub();
716 $main::lxdebug->enter_sub(2);
718 my ($self, $myconfig, $amount, $places, $dash) = @_;
723 my $neg = ($amount =~ s/-//);
725 if (defined($places) && ($places ne '')) {
730 my ($actual_places) = ($amount =~ /\.(\d+)/);
731 $actual_places = length($actual_places);
732 $places = $actual_places > $places ? $actual_places : $places;
735 $amount = $self->round_amount($amount, $places);
738 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
739 my @p = split(/\./, $amount); # split amount at decimal point
741 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
744 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
747 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
748 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
749 ($neg ? "-$amount" : "$amount" ) ;
753 $main::lxdebug->leave_sub(2);
758 $main::lxdebug->enter_sub(2);
760 my ($self, $myconfig, $amount) = @_;
762 if ( ($myconfig->{numberformat} eq '1.000,00')
763 || ($myconfig->{numberformat} eq '1000,00')) {
768 if ($myconfig->{numberformat} eq "1'000.00") {
774 $main::lxdebug->leave_sub(2);
776 return ($amount * 1);
780 $main::lxdebug->enter_sub(2);
782 my ($self, $amount, $places) = @_;
785 # Rounding like "Kaufmannsrunden"
786 # Descr. http://de.wikipedia.org/wiki/Rundung
788 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
791 $amount = $amount * (10**($places));
792 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
794 $main::lxdebug->leave_sub(2);
796 return $round_amount;
801 $main::lxdebug->enter_sub();
803 my ($self, $myconfig, $userspath) = @_;
804 my ($template, $out);
808 $self->{"cwd"} = getcwd();
809 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
811 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
812 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
813 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
814 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
815 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
816 } elsif (($self->{"format"} =~ /html/i) ||
817 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
818 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
819 } elsif (($self->{"format"} =~ /xml/i) ||
820 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
821 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
822 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
823 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
824 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
825 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
826 } elsif ( defined $self->{'format'}) {
827 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
828 } elsif ( $self->{'format'} eq '' ) {
829 $self->error("No Outputformat given: $self->{'format'}");
830 } else { #Catch the rest
831 $self->error("Outputformat not defined: $self->{'format'}");
834 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
835 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
837 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
838 qw(email tel fax name signature company address businessnumber
839 co_ustid taxnumber duns));
840 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
841 qw(company address signature));
842 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
844 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
846 # OUT is used for the media, screen, printer, email
847 # for postscript we store a copy in a temporary file
849 my $prepend_userspath;
851 if (!$self->{tmpfile}) {
852 $self->{tmpfile} = "${fileid}.$self->{IN}";
853 $prepend_userspath = 1;
856 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
858 $self->{tmpfile} =~ s|.*/||;
859 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
860 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
862 if ($template->uses_temp_file() || $self->{media} eq 'email') {
864 $self->{OUT} = ">$self->{tmpfile}";
868 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
870 open(OUT, ">-") or $self->error("STDOUT : $!");
874 if (!$template->parse(*OUT)) {
876 $self->error("$self->{IN} : " . $template->get_error());
881 if ($template->uses_temp_file() || $self->{media} eq 'email') {
883 if ($self->{media} eq 'email') {
885 my $mail = new Mailer;
887 map { $mail->{$_} = $self->{$_} }
888 qw(cc bcc subject message version format);
889 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
890 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
891 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
892 $mail->{fileid} = "$fileid.";
893 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
895 # if we send html or plain text inline
896 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
897 $mail->{contenttype} = "text/html";
899 $mail->{message} =~ s/\r\n/<br>\n/g;
900 $myconfig->{signature} =~ s/\\n/<br>\n/g;
901 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
903 open(IN, $self->{tmpfile})
904 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
906 $mail->{message} .= $_;
913 if (!$self->{"do_not_attach"}) {
914 @{ $mail->{attachments} } =
915 ({ "filename" => $self->{"tmpfile"},
916 "name" => $self->{"attachment_filename"} ?
917 $self->{"attachment_filename"} : $self->{"tmpfile"} });
920 $mail->{message} =~ s/\r\n/\n/g;
921 $myconfig->{signature} =~ s/\\n/\n/g;
922 $mail->{message} .= "\n-- \n$myconfig->{signature}";
926 my $err = $mail->send();
927 $self->error($self->cleanup . "$err") if ($err);
933 my $numbytes = (-s $self->{tmpfile});
934 open(IN, $self->{tmpfile})
935 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
937 $self->{copies} = 1 unless $self->{media} eq 'printer';
939 chdir("$self->{cwd}");
940 #print(STDERR "Kopien $self->{copies}\n");
941 #print(STDERR "OUT $self->{OUT}\n");
942 for my $i (1 .. $self->{copies}) {
944 open(OUT, $self->{OUT})
945 or $self->error($self->cleanup . "$self->{OUT} : $!");
947 $self->{attachment_filename} = ($self->{attachment_filename})
948 ? $self->{attachment_filename}
949 : $self->generate_attachment_filename();
952 print qq|Content-Type: | . $template->get_mime_type() . qq|
953 Content-Disposition: attachment; filename="$self->{attachment_filename}"
954 Content-Length: $numbytes
958 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
978 chdir("$self->{cwd}");
979 $main::lxdebug->leave_sub();
982 sub generate_attachment_filename {
985 my %formname_translations = (
986 bin_list => $main::locale->text('Bin List'),
987 credit_note => $main::locale->text('Credit Note'),
988 invoice => $main::locale->text('Invoice'),
989 packing_list => $main::locale->text('Packing List'),
990 pick_list => $main::locale->text('Pick List'),
991 proforma => $main::locale->text('Proforma Invoice'),
992 purchase_order => $main::locale->text('Purchase Order'),
993 request_quotation => $main::locale->text('RFQ'),
994 sales_order => $main::locale->text('Confirmation'),
995 sales_quotation => $main::locale->text('Quotation'),
996 storno_invoice => $main::locale->text('Storno Invoice'),
997 storno_packing_list => $main::locale->text('Storno Packing List'),
1000 my $attachment_filename = $formname_translations{$self->{"formname"}};
1002 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1003 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1006 if ($attachment_filename && $self->{"${prefix}number"}) {
1007 $attachment_filename .= "_" . $self->{"${prefix}number"}
1008 . ( $self->{format} =~ /pdf/i ? ".pdf"
1009 : $self->{format} =~ /postscript/i ? ".ps"
1010 : $self->{format} =~ /opendocument/i ? ".odt"
1011 : $self->{format} =~ /html/i ? ".html"
1013 $attachment_filename =~ s/ /_/g;
1014 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1015 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1016 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1018 $attachment_filename = "";
1021 return $attachment_filename;
1025 $main::lxdebug->enter_sub();
1029 chdir("$self->{tmpdir}");
1032 if (-f "$self->{tmpfile}.err") {
1033 open(FH, "$self->{tmpfile}.err");
1038 if ($self->{tmpfile}) {
1039 $self->{tmpfile} =~ s|.*/||g;
1041 $self->{tmpfile} =~ s/\.\w+$//g;
1042 my $tmpfile = $self->{tmpfile};
1043 unlink(<$tmpfile.*>);
1046 chdir("$self->{cwd}");
1048 $main::lxdebug->leave_sub();
1054 $main::lxdebug->enter_sub();
1056 my ($self, $date, $myconfig) = @_;
1058 if ($date && $date =~ /\D/) {
1060 if ($myconfig->{dateformat} =~ /^yy/) {
1061 ($yy, $mm, $dd) = split /\D/, $date;
1063 if ($myconfig->{dateformat} =~ /^mm/) {
1064 ($mm, $dd, $yy) = split /\D/, $date;
1066 if ($myconfig->{dateformat} =~ /^dd/) {
1067 ($dd, $mm, $yy) = split /\D/, $date;
1072 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1073 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1075 $dd = "0$dd" if ($dd < 10);
1076 $mm = "0$mm" if ($mm < 10);
1078 $date = "$yy$mm$dd";
1081 $main::lxdebug->leave_sub();
1086 # Database routines used throughout
1089 $main::lxdebug->enter_sub(2);
1091 my ($self, $myconfig) = @_;
1093 # connect to database
1095 DBI->connect($myconfig->{dbconnect},
1096 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1100 if ($myconfig->{dboptions}) {
1101 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1104 $main::lxdebug->leave_sub(2);
1109 sub dbconnect_noauto {
1110 $main::lxdebug->enter_sub();
1112 my ($self, $myconfig) = @_;
1114 # connect to database
1116 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1117 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1121 if ($myconfig->{dboptions}) {
1122 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1125 $main::lxdebug->leave_sub();
1130 sub get_standard_dbh {
1131 $main::lxdebug->enter_sub(2);
1133 my ($self, $myconfig) = @_;
1135 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1137 $main::lxdebug->leave_sub(2);
1139 return $standard_dbh;
1142 sub update_balance {
1143 $main::lxdebug->enter_sub();
1145 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1147 # if we have a value, go do it
1150 # retrieve balance from table
1151 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1152 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1153 my ($balance) = $sth->fetchrow_array;
1159 $query = "UPDATE $table SET $field = $balance WHERE $where";
1160 do_query($self, $dbh, $query, @values);
1162 $main::lxdebug->leave_sub();
1165 sub update_exchangerate {
1166 $main::lxdebug->enter_sub();
1168 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1170 # some sanity check for currency
1172 $main::lxdebug->leave_sub();
1175 my $query = qq|SELECT curr FROM defaults|;
1177 my ($currency) = selectrow_query($self, $dbh, $query);
1178 my ($defaultcurrency) = split m/:/, $currency;
1181 if ($curr eq $defaultcurrency) {
1182 $main::lxdebug->leave_sub();
1186 my $query = qq|SELECT e.curr FROM exchangerate e
1187 WHERE e.curr = ? AND e.transdate = ?
1189 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1198 $buy = conv_i($buy, "NULL");
1199 $sell = conv_i($sell, "NULL");
1202 if ($buy != 0 && $sell != 0) {
1203 $set = "buy = $buy, sell = $sell";
1204 } elsif ($buy != 0) {
1205 $set = "buy = $buy";
1206 } elsif ($sell != 0) {
1207 $set = "sell = $sell";
1210 if ($sth->fetchrow_array) {
1211 $query = qq|UPDATE exchangerate
1217 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1218 VALUES (?, $buy, $sell, ?)|;
1221 do_query($self, $dbh, $query, $curr, $transdate);
1223 $main::lxdebug->leave_sub();
1226 sub save_exchangerate {
1227 $main::lxdebug->enter_sub();
1229 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1231 my $dbh = $self->dbconnect($myconfig);
1235 $buy = $rate if $fld eq 'buy';
1236 $sell = $rate if $fld eq 'sell';
1239 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1244 $main::lxdebug->leave_sub();
1247 sub get_exchangerate {
1248 $main::lxdebug->enter_sub();
1250 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1252 unless ($transdate) {
1253 $main::lxdebug->leave_sub();
1257 my $query = qq|SELECT curr FROM defaults|;
1259 my ($currency) = selectrow_query($self, $dbh, $query);
1260 my ($defaultcurrency) = split m/:/, $currency;
1262 if ($currency eq $defaultcurrency) {
1263 $main::lxdebug->leave_sub();
1267 my $query = qq|SELECT e.$fld FROM exchangerate e
1268 WHERE e.curr = ? AND e.transdate = ?|;
1269 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1273 $main::lxdebug->leave_sub();
1275 return $exchangerate;
1278 sub check_exchangerate {
1279 $main::lxdebug->enter_sub();
1281 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1283 unless ($transdate) {
1284 $main::lxdebug->leave_sub();
1288 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1290 if ($currency eq $defaultcurrency) {
1291 $main::lxdebug->leave_sub();
1295 my $dbh = $self->get_standard_dbh($myconfig);
1296 my $query = qq|SELECT e.$fld FROM exchangerate e
1297 WHERE e.curr = ? AND e.transdate = ?|;
1299 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1301 $exchangerate = 1 if ($exchangerate eq "");
1303 $main::lxdebug->leave_sub();
1305 return $exchangerate;
1308 sub get_default_currency {
1309 $main::lxdebug->enter_sub();
1311 my ($self, $myconfig) = @_;
1312 my $dbh = $self->get_standard_dbh($myconfig);
1314 my $query = qq|SELECT curr FROM defaults|;
1316 my ($curr) = selectrow_query($self, $dbh, $query);
1317 my ($defaultcurrency) = split m/:/, $curr;
1319 $main::lxdebug->leave_sub();
1321 return $defaultcurrency;
1325 sub set_payment_options {
1326 $main::lxdebug->enter_sub();
1328 my ($self, $myconfig, $transdate) = @_;
1330 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1332 my $dbh = $self->get_standard_dbh($myconfig);
1335 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1336 qq|FROM payment_terms p | .
1339 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1340 $self->{payment_terms}) =
1341 selectrow_query($self, $dbh, $query, $self->{payment_id});
1343 if ($transdate eq "") {
1344 if ($self->{invdate}) {
1345 $transdate = $self->{invdate};
1347 $transdate = $self->{transdate};
1352 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1353 qq|FROM payment_terms|;
1354 ($self->{netto_date}, $self->{skonto_date}) =
1355 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1357 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1358 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1359 $self->{percent_skonto};
1361 $self->{skonto_amount} =
1362 $self->format_amount($myconfig, $skonto_amount, 2);
1364 if ($self->{"language_id"}) {
1366 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1367 qq|FROM translation_payment_terms t | .
1368 qq|LEFT JOIN language l ON t.language_id = l.id | .
1369 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1370 my ($description_long, $output_numberformat, $output_dateformat,
1371 $output_longdates) =
1372 selectrow_query($self, $dbh, $query,
1373 $self->{"language_id"}, $self->{"payment_id"});
1375 $self->{payment_terms} = $description_long if ($description_long);
1377 if ($output_dateformat) {
1378 foreach my $key (qw(netto_date skonto_date)) {
1380 $main::locale->reformat_date($myconfig, $self->{$key},
1386 if ($output_numberformat &&
1387 ($output_numberformat ne $myconfig->{"numberformat"})) {
1388 my $saved_numberformat = $myconfig->{"numberformat"};
1389 $myconfig->{"numberformat"} = $output_numberformat;
1390 $self->{skonto_amount} =
1391 $self->format_amount($myconfig, $skonto_amount, 2);
1392 $myconfig->{"numberformat"} = $saved_numberformat;
1396 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1397 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1398 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1399 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1400 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1401 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1402 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1403 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1404 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1405 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1407 $main::lxdebug->leave_sub();
1411 sub get_template_language {
1412 $main::lxdebug->enter_sub();
1414 my ($self, $myconfig) = @_;
1416 my $template_code = "";
1418 if ($self->{language_id}) {
1419 my $dbh = $self->get_standard_dbh($myconfig);
1420 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1421 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1424 $main::lxdebug->leave_sub();
1426 return $template_code;
1429 sub get_printer_code {
1430 $main::lxdebug->enter_sub();
1432 my ($self, $myconfig) = @_;
1434 my $template_code = "";
1436 if ($self->{printer_id}) {
1437 my $dbh = $self->get_standard_dbh($myconfig);
1438 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1439 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1442 $main::lxdebug->leave_sub();
1444 return $template_code;
1448 $main::lxdebug->enter_sub();
1450 my ($self, $myconfig) = @_;
1452 my $template_code = "";
1454 if ($self->{shipto_id}) {
1455 my $dbh = $self->get_standard_dbh($myconfig);
1456 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1457 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1458 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1461 $main::lxdebug->leave_sub();
1465 $main::lxdebug->enter_sub();
1467 my ($self, $dbh, $id, $module) = @_;
1471 foreach my $item (qw(name department_1 department_2 street zipcode city country
1472 contact phone fax email)) {
1473 if ($self->{"shipto$item"}) {
1474 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1476 push(@values, $self->{"shipto${item}"});
1479 if ($self->{shipto_id}) {
1480 my $query = qq|UPDATE shipto set
1482 shiptodepartment_1 = ?,
1483 shiptodepartment_2 = ?,
1492 WHERE shipto_id = ?|;
1493 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1495 my $query = qq|SELECT * FROM shipto
1496 WHERE shiptoname = ? AND
1497 shiptodepartment_1 = ? AND
1498 shiptodepartment_2 = ? AND
1499 shiptostreet = ? AND
1500 shiptozipcode = ? AND
1502 shiptocountry = ? AND
1503 shiptocontact = ? AND
1507 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1510 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1511 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1512 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1513 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1514 do_query($self, $dbh, $query, $id, @values, $module);
1519 $main::lxdebug->leave_sub();
1523 $main::lxdebug->enter_sub();
1525 my ($self, $dbh) = @_;
1527 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1528 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1529 $self->{"employee_id"} *= 1;
1531 $main::lxdebug->leave_sub();
1535 $main::lxdebug->enter_sub();
1537 my ($self, $myconfig, $salesman_id) = @_;
1539 $main::lxdebug->leave_sub() and return unless $salesman_id;
1541 my $dbh = $self->get_standard_dbh($myconfig);
1544 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1548 my $user = new User($main::memberfile, $login);
1549 map({ $self->{"salesman_$_"} = $user->{$_}; }
1550 qw(address businessnumber co_ustid company duns email fax name
1552 $self->{salesman_login} = $login;
1554 $self->{salesman_name} = $login
1555 if ($self->{salesman_name} eq "");
1557 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1560 $main::lxdebug->leave_sub();
1564 $main::lxdebug->enter_sub();
1566 my ($self, $myconfig) = @_;
1568 my $dbh = $self->get_standard_dbh($myconfig);
1569 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1570 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1572 $main::lxdebug->leave_sub();
1576 $main::lxdebug->enter_sub();
1578 my ($self, $dbh, $id, $key) = @_;
1580 $key = "all_contacts" unless ($key);
1583 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1584 qq|FROM contacts | .
1585 qq|WHERE cp_cv_id = ? | .
1586 qq|ORDER BY lower(cp_name)|;
1588 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1590 $main::lxdebug->leave_sub();
1594 $main::lxdebug->enter_sub();
1596 my ($self, $dbh, $key) = @_;
1598 my ($all, $old_id, $where, @values);
1600 if (ref($key) eq "HASH") {
1603 $key = "ALL_PROJECTS";
1605 foreach my $p (keys(%{$params})) {
1607 $all = $params->{$p};
1608 } elsif ($p eq "old_id") {
1609 $old_id = $params->{$p};
1610 } elsif ($p eq "key") {
1611 $key = $params->{$p};
1617 $where = "WHERE active ";
1619 if (ref($old_id) eq "ARRAY") {
1620 my @ids = grep({ $_ } @{$old_id});
1622 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1623 push(@values, @ids);
1626 $where .= " OR (id = ?) ";
1627 push(@values, $old_id);
1633 qq|SELECT id, projectnumber, description, active | .
1636 qq|ORDER BY lower(projectnumber)|;
1638 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1640 $main::lxdebug->leave_sub();
1644 $main::lxdebug->enter_sub();
1646 my ($self, $dbh, $vc_id, $key) = @_;
1648 $key = "all_shipto" unless ($key);
1650 # get shipping addresses
1651 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1653 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1655 $main::lxdebug->leave_sub();
1659 $main::lxdebug->enter_sub();
1661 my ($self, $dbh, $key) = @_;
1663 $key = "all_printers" unless ($key);
1665 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1667 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1669 $main::lxdebug->leave_sub();
1673 $main::lxdebug->enter_sub();
1675 my ($self, $dbh, $params) = @_;
1677 $key = $params->{key};
1678 $key = "all_charts" unless ($key);
1680 my $transdate = quote_db_date($params->{transdate});
1683 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1685 qq|LEFT JOIN taxkeys tk ON | .
1686 qq|(tk.id = (SELECT id FROM taxkeys | .
1687 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1688 qq| ORDER BY startdate DESC LIMIT 1)) | .
1689 qq|ORDER BY c.accno|;
1691 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1693 $main::lxdebug->leave_sub();
1696 sub _get_taxcharts {
1697 $main::lxdebug->enter_sub();
1699 my ($self, $dbh, $key) = @_;
1701 $key = "all_taxcharts" unless ($key);
1703 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1705 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1707 $main::lxdebug->leave_sub();
1711 $main::lxdebug->enter_sub();
1713 my ($self, $dbh, $key) = @_;
1715 $key = "all_taxzones" unless ($key);
1717 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1719 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1721 $main::lxdebug->leave_sub();
1724 sub _get_employees {
1725 $main::lxdebug->enter_sub();
1727 my ($self, $dbh, $default_key, $key) = @_;
1729 $key = $default_key unless ($key);
1730 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1732 $main::lxdebug->leave_sub();
1735 sub _get_business_types {
1736 $main::lxdebug->enter_sub();
1738 my ($self, $dbh, $key) = @_;
1740 $key = "all_business_types" unless ($key);
1742 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1744 $main::lxdebug->leave_sub();
1747 sub _get_languages {
1748 $main::lxdebug->enter_sub();
1750 my ($self, $dbh, $key) = @_;
1752 $key = "all_languages" unless ($key);
1754 my $query = qq|SELECT * FROM language ORDER BY id|;
1756 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1758 $main::lxdebug->leave_sub();
1761 sub _get_dunning_configs {
1762 $main::lxdebug->enter_sub();
1764 my ($self, $dbh, $key) = @_;
1766 $key = "all_dunning_configs" unless ($key);
1768 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1770 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1772 $main::lxdebug->leave_sub();
1775 sub _get_currencies {
1776 $main::lxdebug->enter_sub();
1778 my ($self, $dbh, $key) = @_;
1780 $key = "all_currencies" unless ($key);
1782 my $query = qq|SELECT curr AS currency FROM defaults|;
1784 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1786 $main::lxdebug->leave_sub();
1790 $main::lxdebug->enter_sub();
1792 my ($self, $dbh, $key) = @_;
1794 $key = "all_payments" unless ($key);
1796 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1798 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1800 $main::lxdebug->leave_sub();
1803 sub _get_customers {
1804 $main::lxdebug->enter_sub();
1806 my ($self, $dbh, $key) = @_;
1808 $key = "all_customers" unless ($key);
1810 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name|;
1812 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1814 $main::lxdebug->leave_sub();
1818 $main::lxdebug->enter_sub();
1820 my ($self, $dbh, $key) = @_;
1822 $key = "all_vendors" unless ($key);
1824 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1826 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1828 $main::lxdebug->leave_sub();
1831 sub _get_departments {
1832 $main::lxdebug->enter_sub();
1834 my ($self, $dbh, $key) = @_;
1836 $key = "all_departments" unless ($key);
1838 my $query = qq|SELECT * FROM department ORDER BY description|;
1840 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1842 $main::lxdebug->leave_sub();
1846 $main::lxdebug->enter_sub();
1851 my $dbh = $self->get_standard_dbh(\%main::myconfig);
1852 my ($sth, $query, $ref);
1854 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1855 my $vc_id = $self->{"${vc}_id"};
1857 if ($params{"contacts"}) {
1858 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1861 if ($params{"shipto"}) {
1862 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1865 if ($params{"projects"} || $params{"all_projects"}) {
1866 $self->_get_projects($dbh, $params{"all_projects"} ?
1867 $params{"all_projects"} : $params{"projects"},
1868 $params{"all_projects"} ? 1 : 0);
1871 if ($params{"printers"}) {
1872 $self->_get_printers($dbh, $params{"printers"});
1875 if ($params{"languages"}) {
1876 $self->_get_languages($dbh, $params{"languages"});
1879 if ($params{"charts"}) {
1880 $self->_get_charts($dbh, $params{"charts"});
1883 if ($params{"taxcharts"}) {
1884 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1887 if ($params{"taxzones"}) {
1888 $self->_get_taxzones($dbh, $params{"taxzones"});
1891 if ($params{"employees"}) {
1892 $self->_get_employees($dbh, "all_employees", $params{"employees"});
1895 if ($params{"salesmen"}) {
1896 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
1899 if ($params{"business_types"}) {
1900 $self->_get_business_types($dbh, $params{"business_types"});
1903 if ($params{"dunning_configs"}) {
1904 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1907 if($params{"currencies"}) {
1908 $self->_get_currencies($dbh, $params{"currencies"});
1911 if($params{"customers"}) {
1912 $self->_get_customers($dbh, $params{"customers"});
1915 if($params{"vendors"}) {
1916 $self->_get_vendors($dbh, $params{"vendors"});
1919 if($params{"payments"}) {
1920 $self->_get_payments($dbh, $params{"payments"});
1923 if($params{"departments"}) {
1924 $self->_get_departments($dbh, $params{"departments"});
1927 $main::lxdebug->leave_sub();
1930 # this sub gets the id and name from $table
1932 $main::lxdebug->enter_sub();
1934 my ($self, $myconfig, $table) = @_;
1936 # connect to database
1937 my $dbh = $self->get_standard_dbh($myconfig);
1939 $table = $table eq "customer" ? "customer" : "vendor";
1940 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1942 my ($query, @values);
1944 if (!$self->{openinvoices}) {
1946 if ($self->{customernumber} ne "") {
1947 $where = qq|(vc.customernumber ILIKE ?)|;
1948 push(@values, '%' . $self->{customernumber} . '%');
1950 $where = qq|(vc.name ILIKE ?)|;
1951 push(@values, '%' . $self->{$table} . '%');
1955 qq~SELECT vc.id, vc.name,
1956 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1958 WHERE $where AND (NOT vc.obsolete)
1962 qq~SELECT DISTINCT vc.id, vc.name,
1963 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1965 JOIN $table vc ON (a.${table}_id = vc.id)
1966 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1968 push(@values, '%' . $self->{$table} . '%');
1971 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1973 $main::lxdebug->leave_sub();
1975 return scalar(@{ $self->{name_list} });
1978 # the selection sub is used in the AR, AP, IS, IR and OE module
1981 $main::lxdebug->enter_sub();
1983 my ($self, $myconfig, $table, $module) = @_;
1986 my $dbh = $self->get_standard_dbh($myconfig);
1988 $table = $table eq "customer" ? "customer" : "vendor";
1990 my $query = qq|SELECT count(*) FROM $table|;
1991 my ($count) = selectrow_query($self, $dbh, $query);
1993 # build selection list
1994 if ($count < $myconfig->{vclimit}) {
1995 $query = qq|SELECT id, name, salesman_id
1996 FROM $table WHERE NOT obsolete
1998 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2002 $self->get_employee($dbh);
2004 # setup sales contacts
2005 $query = qq|SELECT e.id, e.name
2007 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2008 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2011 push(@{ $self->{all_employees} },
2012 { id => $self->{employee_id},
2013 name => $self->{employee} });
2015 # sort the whole thing
2016 @{ $self->{all_employees} } =
2017 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2019 if ($module eq 'AR') {
2021 # prepare query for departments
2022 $query = qq|SELECT id, description
2025 ORDER BY description|;
2028 $query = qq|SELECT id, description
2030 ORDER BY description|;
2033 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2036 $query = qq|SELECT id, description
2040 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2043 $query = qq|SELECT printer_description, id
2045 ORDER BY printer_description|;
2047 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2050 $query = qq|SELECT id, description
2054 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2056 $main::lxdebug->leave_sub();
2059 sub language_payment {
2060 $main::lxdebug->enter_sub();
2062 my ($self, $myconfig) = @_;
2064 my $dbh = $self->get_standard_dbh($myconfig);
2066 my $query = qq|SELECT id, description
2070 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2073 $query = qq|SELECT printer_description, id
2075 ORDER BY printer_description|;
2077 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2080 $query = qq|SELECT id, description
2084 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2086 # get buchungsgruppen
2087 $query = qq|SELECT id, description
2088 FROM buchungsgruppen|;
2090 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2092 $main::lxdebug->leave_sub();
2095 # this is only used for reports
2096 sub all_departments {
2097 $main::lxdebug->enter_sub();
2099 my ($self, $myconfig, $table) = @_;
2101 my $dbh = $self->get_standard_dbh($myconfig);
2104 if ($table eq 'customer') {
2105 $where = "WHERE role = 'P' ";
2108 my $query = qq|SELECT id, description
2111 ORDER BY description|;
2112 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2114 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2116 $main::lxdebug->leave_sub();
2120 $main::lxdebug->enter_sub();
2122 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2125 if ($table eq "customer") {
2134 $self->all_vc($myconfig, $table, $module);
2136 # get last customers or vendors
2137 my ($query, $sth, $ref);
2139 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2144 my $transdate = "current_date";
2145 if ($self->{transdate}) {
2146 $transdate = $dbh->quote($self->{transdate});
2149 # now get the account numbers
2150 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2151 FROM chart c, taxkeys tk
2152 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2153 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2156 $sth = $dbh->prepare($query);
2158 do_statement($self, $sth, $query, '%' . $module . '%');
2160 $self->{accounts} = "";
2161 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2163 foreach my $key (split(/:/, $ref->{link})) {
2164 if ($key =~ /$module/) {
2166 # cross reference for keys
2167 $xkeyref{ $ref->{accno} } = $key;
2169 push @{ $self->{"${module}_links"}{$key} },
2170 { accno => $ref->{accno},
2171 description => $ref->{description},
2172 taxkey => $ref->{taxkey_id},
2173 tax_id => $ref->{tax_id} };
2175 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2181 # get taxkeys and description
2182 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2183 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2185 if (($module eq "AP") || ($module eq "AR")) {
2186 # get tax rates and description
2187 $query = qq|SELECT * FROM tax|;
2188 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2194 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2195 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2196 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2197 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2199 d.description AS department,
2202 JOIN $table c ON (a.${table}_id = c.id)
2203 LEFT JOIN employee e ON (e.id = a.employee_id)
2204 LEFT JOIN department d ON (d.id = a.department_id)
2206 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2208 foreach $key (keys %$ref) {
2209 $self->{$key} = $ref->{$key};
2212 my $transdate = "current_date";
2213 if ($self->{transdate}) {
2214 $transdate = $dbh->quote($self->{transdate});
2217 # now get the account numbers
2218 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2220 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2222 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2223 OR c.link LIKE '%_tax%')
2226 $sth = $dbh->prepare($query);
2227 do_statement($self, $sth, $query, "%$module%");
2229 $self->{accounts} = "";
2230 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2232 foreach my $key (split(/:/, $ref->{link})) {
2233 if ($key =~ /$module/) {
2235 # cross reference for keys
2236 $xkeyref{ $ref->{accno} } = $key;
2238 push @{ $self->{"${module}_links"}{$key} },
2239 { accno => $ref->{accno},
2240 description => $ref->{description},
2241 taxkey => $ref->{taxkey_id},
2242 tax_id => $ref->{tax_id} };
2244 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2250 # get amounts from individual entries
2253 c.accno, c.description,
2254 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2258 LEFT JOIN chart c ON (c.id = a.chart_id)
2259 LEFT JOIN project p ON (p.id = a.project_id)
2260 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2261 WHERE (tk.taxkey_id=a.taxkey) AND
2262 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2263 THEN tk.chart_id = a.chart_id
2266 OR (c.link='%tax%')) AND
2267 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2268 WHERE a.trans_id = ?
2269 AND a.fx_transaction = '0'
2270 ORDER BY a.oid, a.transdate|;
2271 $sth = $dbh->prepare($query);
2272 do_statement($self, $sth, $query, $self->{id});
2274 # get exchangerate for currency
2275 $self->{exchangerate} =
2276 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2279 # store amounts in {acc_trans}{$key} for multiple accounts
2280 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2281 $ref->{exchangerate} =
2282 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2283 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2286 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2287 $ref->{amount} *= -1;
2289 $ref->{index} = $index;
2291 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2297 d.curr AS currencies, d.closedto, d.revtrans,
2298 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2299 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2301 $ref = selectfirst_hashref_query($self, $dbh, $query);
2302 map { $self->{$_} = $ref->{$_} } keys %$ref;
2309 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2310 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2311 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2313 $ref = selectfirst_hashref_query($self, $dbh, $query);
2314 map { $self->{$_} = $ref->{$_} } keys %$ref;
2316 if ($self->{"$self->{vc}_id"}) {
2318 # only setup currency
2319 ($self->{currency}) = split(/:/, $self->{currencies});
2323 $self->lastname_used($dbh, $myconfig, $table, $module);
2325 # get exchangerate for currency
2326 $self->{exchangerate} =
2327 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2333 $main::lxdebug->leave_sub();
2337 $main::lxdebug->enter_sub();
2339 my ($self, $dbh, $myconfig, $table, $module) = @_;
2341 my $arap = ($table eq 'customer') ? "ar" : "ap";
2342 $table = $table eq "customer" ? "customer" : "vendor";
2343 my $where = "1 = 1";
2345 if ($self->{type} =~ /_order/) {
2347 $where = "quotation = '0'";
2349 if ($self->{type} =~ /_quotation/) {
2351 $where = "quotation = '1'";
2354 my $query = qq|SELECT MAX(id) FROM $arap
2355 WHERE $where AND ${table}_id > 0|;
2356 my ($trans_id) = selectrow_query($self, $dbh, $query);
2361 a.curr, a.${table}_id, a.department_id,
2362 d.description AS department,
2363 ct.name, current_date + ct.terms AS duedate
2365 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2366 LEFT JOIN department d ON (a.department_id = d.id)
2368 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2369 $self->{department}, $self->{$table}, $self->{duedate})
2370 = selectrow_query($self, $dbh, $query, $trans_id);
2372 $main::lxdebug->leave_sub();
2376 $main::lxdebug->enter_sub();
2378 my ($self, $myconfig, $thisdate, $days) = @_;
2380 my $dbh = $self->get_standard_dbh($myconfig);
2385 my $dateformat = $myconfig->{dateformat};
2386 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2387 $thisdate = $dbh->quote($thisdate);
2388 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2390 $query = qq|SELECT current_date AS thisdate|;
2393 ($thisdate) = selectrow_query($self, $dbh, $query);
2395 $main::lxdebug->leave_sub();
2401 $main::lxdebug->enter_sub();
2403 my ($self, $string) = @_;
2405 if ($string !~ /%/) {
2406 $string = "%$string%";
2409 $string =~ s/\'/\'\'/g;
2411 $main::lxdebug->leave_sub();
2417 $main::lxdebug->enter_sub();
2419 my ($self, $flds, $new, $count, $numrows) = @_;
2423 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2429 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2431 $j = $item->{ndx} - 1;
2432 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2436 for $i ($count + 1 .. $numrows) {
2437 map { delete $self->{"${_}_$i"} } @{$flds};
2440 $main::lxdebug->leave_sub();
2444 $main::lxdebug->enter_sub();
2446 my ($self, $myconfig) = @_;
2450 my $dbh = $self->dbconnect_noauto($myconfig);
2452 my $query = qq|DELETE FROM status
2453 WHERE (formname = ?) AND (trans_id = ?)|;
2454 my $sth = prepare_query($self, $dbh, $query);
2456 if ($self->{formname} =~ /(check|receipt)/) {
2457 for $i (1 .. $self->{rowcount}) {
2458 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2461 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2465 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2466 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2468 my %queued = split / /, $self->{queued};
2471 if ($self->{formname} =~ /(check|receipt)/) {
2473 # this is a check or receipt, add one entry for each lineitem
2474 my ($accno) = split /--/, $self->{account};
2475 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2476 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2477 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2478 $sth = prepare_query($self, $dbh, $query);
2480 for $i (1 .. $self->{rowcount}) {
2481 if ($self->{"checked_$i"}) {
2482 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2488 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2489 VALUES (?, ?, ?, ?, ?)|;
2490 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2491 $queued{$self->{formname}}, $self->{formname});
2497 $main::lxdebug->leave_sub();
2501 $main::lxdebug->enter_sub();
2503 my ($self, $dbh) = @_;
2505 my ($query, $printed, $emailed);
2507 my $formnames = $self->{printed};
2508 my $emailforms = $self->{emailed};
2510 my $query = qq|DELETE FROM status
2511 WHERE (formname = ?) AND (trans_id = ?)|;
2512 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2514 # this only applies to the forms
2515 # checks and receipts are posted when printed or queued
2517 if ($self->{queued}) {
2518 my %queued = split / /, $self->{queued};
2520 foreach my $formname (keys %queued) {
2521 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2522 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2524 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2525 VALUES (?, ?, ?, ?, ?)|;
2526 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2528 $formnames =~ s/$self->{formname}//;
2529 $emailforms =~ s/$self->{formname}//;
2534 # save printed, emailed info
2535 $formnames =~ s/^ +//g;
2536 $emailforms =~ s/^ +//g;
2539 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2540 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2542 foreach my $formname (keys %status) {
2543 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2544 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2546 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2547 VALUES (?, ?, ?, ?)|;
2548 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2551 $main::lxdebug->leave_sub();
2555 # $main::locale->text('SAVED')
2556 # $main::locale->text('DELETED')
2557 # $main::locale->text('ADDED')
2558 # $main::locale->text('PAYMENT POSTED')
2559 # $main::locale->text('POSTED')
2560 # $main::locale->text('POSTED AS NEW')
2561 # $main::locale->text('ELSE')
2562 # $main::locale->text('SAVED FOR DUNNING')
2563 # $main::locale->text('DUNNING STARTED')
2564 # $main::locale->text('PRINTED')
2565 # $main::locale->text('MAILED')
2566 # $main::locale->text('SCREENED')
2567 # $main::locale->text('CANCELED')
2568 # $main::locale->text('invoice')
2569 # $main::locale->text('proforma')
2570 # $main::locale->text('sales_order')
2571 # $main::locale->text('packing_list')
2572 # $main::locale->text('pick_list')
2573 # $main::locale->text('purchase_order')
2574 # $main::locale->text('bin_list')
2575 # $main::locale->text('sales_quotation')
2576 # $main::locale->text('request_quotation')
2579 $main::lxdebug->enter_sub();
2584 if(!exists $self->{employee_id}) {
2585 &get_employee($self, $dbh);
2589 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2590 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2591 my @values = (conv_i($self->{id}), $self->{login},
2592 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2593 do_query($self, $dbh, $query, @values);
2595 $main::lxdebug->leave_sub();
2599 $main::lxdebug->enter_sub();
2601 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2602 my ($orderBy, $desc) = split(/\-\-/, $order);
2603 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2606 if ($trans_id ne "") {
2608 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 | .
2609 qq|FROM history_erp h | .
2610 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2611 qq|WHERE trans_id = | . $trans_id
2612 . $restriction . qq| |
2615 my $sth = $dbh->prepare($query) || $self->dberror($query);
2617 $sth->execute() || $self->dberror("$query");
2619 while(my $hash_ref = $sth->fetchrow_hashref()) {
2620 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2621 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2622 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2623 $tempArray[$i++] = $hash_ref;
2625 $main::lxdebug->leave_sub() and return \@tempArray
2626 if ($i > 0 && $tempArray[0] ne "");
2628 $main::lxdebug->leave_sub();
2632 sub update_defaults {
2633 $main::lxdebug->enter_sub();
2635 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2638 if ($provided_dbh) {
2639 $dbh = $provided_dbh;
2641 $dbh = $self->dbconnect_noauto($myconfig);
2643 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2644 my $sth = $dbh->prepare($query);
2646 $sth->execute || $self->dberror($query);
2647 my ($var) = $sth->fetchrow_array;
2650 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2653 $query = qq|UPDATE defaults SET $fld = ?|;
2654 do_query($self, $dbh, $query, $var);
2656 if (!$provided_dbh) {
2661 $main::lxdebug->leave_sub();
2666 sub update_business {
2667 $main::lxdebug->enter_sub();
2669 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2672 if ($provided_dbh) {
2673 $dbh = $provided_dbh;
2675 $dbh = $self->dbconnect_noauto($myconfig);
2678 qq|SELECT customernumberinit FROM business
2679 WHERE id = ? FOR UPDATE|;
2680 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2682 $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
2684 $query = qq|UPDATE business
2685 SET customernumberinit = ?
2687 do_query($self, $dbh, $query, $var, $business_id);
2689 if (!$provided_dbh) {
2694 $main::lxdebug->leave_sub();
2699 sub get_partsgroup {
2700 $main::lxdebug->enter_sub();
2702 my ($self, $myconfig, $p) = @_;
2704 my $dbh = $self->get_standard_dbh($myconfig);
2706 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2708 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2711 if ($p->{searchitems} eq 'part') {
2712 $query .= qq|WHERE p.inventory_accno_id > 0|;
2714 if ($p->{searchitems} eq 'service') {
2715 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2717 if ($p->{searchitems} eq 'assembly') {
2718 $query .= qq|WHERE p.assembly = '1'|;
2720 if ($p->{searchitems} eq 'labor') {
2721 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2724 $query .= qq|ORDER BY partsgroup|;
2727 $query = qq|SELECT id, partsgroup FROM partsgroup
2728 ORDER BY partsgroup|;
2731 if ($p->{language_code}) {
2732 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2733 t.description AS translation
2735 JOIN parts p ON (p.partsgroup_id = pg.id)
2736 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2737 ORDER BY translation|;
2738 @values = ($p->{language_code});
2741 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2743 $main::lxdebug->leave_sub();
2746 sub get_pricegroup {
2747 $main::lxdebug->enter_sub();
2749 my ($self, $myconfig, $p) = @_;
2751 my $dbh = $self->get_standard_dbh($myconfig);
2753 my $query = qq|SELECT p.id, p.pricegroup
2756 $query .= qq| ORDER BY pricegroup|;
2759 $query = qq|SELECT id, pricegroup FROM pricegroup
2760 ORDER BY pricegroup|;
2763 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2765 $main::lxdebug->leave_sub();
2769 # usage $form->all_years($myconfig, [$dbh])
2770 # return list of all years where bookings found
2773 $main::lxdebug->enter_sub();
2775 my ($self, $myconfig, $dbh) = @_;
2777 $dbh ||= $self->get_standard_dbh($myconfig);
2780 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2781 (SELECT MAX(transdate) FROM acc_trans)|;
2782 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2784 if ($myconfig->{dateformat} =~ /^yy/) {
2785 ($startdate) = split /\W/, $startdate;
2786 ($enddate) = split /\W/, $enddate;
2788 (@_) = split /\W/, $startdate;
2790 (@_) = split /\W/, $enddate;
2795 $startdate = substr($startdate,0,4);
2796 $enddate = substr($enddate,0,4);
2798 while ($enddate >= $startdate) {
2799 push @all_years, $enddate--;
2804 $main::lxdebug->leave_sub();