1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 # Antti Kaihola <akaihola@siba.fi>
17 # Moritz Bunkus (tex code)
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #======================================================================
32 # Utilities for parsing forms
33 # and supporting routines for linking account numbers
34 # used in AR, AP and IS, IR modules
36 #======================================================================
52 use List::Util qw(max min sum);
58 $standard_dbh->disconnect();
64 $main::lxdebug->enter_sub(2);
68 my @pairs = split(/&/, $input);
71 my ($name, $value) = split(/=/, $_, 2);
72 $in{$name} = unescape(undef, $value);
75 $main::lxdebug->leave_sub(2);
80 sub _request_to_hash {
81 $main::lxdebug->enter_sub(2);
85 if (!$ENV{'CONTENT_TYPE'}
86 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
87 $main::lxdebug->leave_sub(2);
88 return _input_to_hash($input);
91 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr);
94 my $boundary = '--' . $1;
96 foreach my $line (split m/\n/, $input) {
97 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
99 if (($line eq $boundary) || ($line eq "$boundary\r")) {
100 $params{$name} =~ s|\r?\n$|| if $name;
106 $content_type = "text/plain";
113 next unless $boundary_found;
115 if (!$headers_done) {
116 $line =~ s/[\r\n]*$//;
123 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
124 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
126 substr $line, $-[0], $+[0] - $-[0], "";
129 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
131 substr $line, $-[0], $+[0] - $-[0], "";
135 $params{FILENAME} = $filename if ($filename);
140 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
149 $params{$name} .= "${line}\n";
152 $params{$name} =~ s|\r?\n$|| if $name;
154 $main::lxdebug->leave_sub(2);
159 $main::lxdebug->enter_sub();
165 if ($LXDebug::watch_form) {
166 require SL::Watchdog;
167 tie %{ $self }, 'SL::Watchdog';
170 read(STDIN, $_, $ENV{CONTENT_LENGTH});
172 if ($ENV{QUERY_STRING}) {
173 $_ = $ENV{QUERY_STRING};
180 my %parameters = _request_to_hash($_);
181 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
183 $self->{action} = lc $self->{action};
184 $self->{action} =~ s/( |-|,|\#)/_/g;
186 $self->{version} = "2.4.3";
188 $main::lxdebug->leave_sub();
194 $main::lxdebug->enter_sub();
200 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
202 $main::lxdebug->leave_sub();
206 $main::lxdebug->enter_sub(2);
208 my ($self, $str) = @_;
210 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
212 $main::lxdebug->leave_sub(2);
218 $main::lxdebug->enter_sub(2);
220 my ($self, $str) = @_;
225 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
227 $main::lxdebug->leave_sub(2);
233 my ($self, $str) = @_;
235 if ($str && !ref($str)) {
236 $str =~ s/\"/"/g;
244 my ($self, $str) = @_;
246 if ($str && !ref($str)) {
247 $str =~ s/"/\"/g;
255 $main::lxdebug->enter_sub(2);
257 my ($self, $str) = @_;
260 ('order' => ['"', '<', '>'],
266 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
268 $main::lxdebug->leave_sub(2);
277 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
279 for (sort keys %$self) {
280 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
281 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
288 $main::lxdebug->enter_sub();
290 $main::lxdebug->show_backtrace();
292 my ($self, $msg) = @_;
293 if ($ENV{HTTP_USER_AGENT}) {
295 $self->show_generic_error($msg);
302 $main::lxdebug->leave_sub();
306 $main::lxdebug->enter_sub();
308 my ($self, $msg) = @_;
310 if ($ENV{HTTP_USER_AGENT}) {
313 if (!$self->{header}) {
326 if ($self->{info_function}) {
327 &{ $self->{info_function} }($msg);
333 $main::lxdebug->leave_sub();
336 # calculates the number of rows in a textarea based on the content and column number
337 # can be capped with maxrows
339 $main::lxdebug->enter_sub();
340 my ($self, $str, $cols, $maxrows) = @_;
342 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
345 $main::lxdebug->leave_sub();
346 return min $rows, $maxrows;
350 $main::lxdebug->enter_sub();
352 my ($self, $msg) = @_;
354 $self->error("$msg\n" . $DBI::errstr);
356 $main::lxdebug->leave_sub();
360 $main::lxdebug->enter_sub();
362 my ($self, $name, $msg) = @_;
364 if ($self->{$name} =~ /^\s*$/) {
367 $main::lxdebug->leave_sub();
371 $main::lxdebug->enter_sub();
373 my ($self, $extra_code) = @_;
375 if ($self->{header}) {
376 $main::lxdebug->leave_sub();
380 my ($stylesheet, $favicon);
382 if ($ENV{HTTP_USER_AGENT}) {
384 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
386 $stylesheets =~ s|^\s*||;
387 $stylesheets =~ s|\s*$||;
388 foreach my $file (split m/\s+/, $stylesheets) {
390 next if (! -f "css/$file");
392 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
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};
436 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
439 <title>$self->{titlebar}</title>
443 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
447 <script type="text/javascript">
455 <meta name="robots" content="noindex,nofollow" />
456 <script type="text/javascript" src="js/highlight_input.js"></script>
457 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
459 <script type="text/javascript" src="js/tabcontent.js">
461 /***********************************************
462 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
463 * This notice MUST stay intact for legal use
464 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
465 ***********************************************/
476 $main::lxdebug->leave_sub();
479 sub _prepare_html_template {
480 $main::lxdebug->enter_sub();
482 my ($self, $file, $additional_params) = @_;
485 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
486 $language = $main::language;
488 $language = $main::myconfig{"countrycode"};
490 $language = "de" unless ($language);
492 if (-f "templates/webpages/${file}_${language}.html") {
493 if ((-f ".developer") &&
494 (-f "templates/webpages/${file}_master.html") &&
495 ((stat("templates/webpages/${file}_master.html"))[9] >
496 (stat("templates/webpages/${file}_${language}.html"))[9])) {
497 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
498 "Please re-run 'locales.pl' in 'locale/${language}'.";
499 print(qq|<pre>$info</pre>|);
503 $file = "templates/webpages/${file}_${language}.html";
504 } elsif (-f "templates/webpages/${file}.html") {
505 $file = "templates/webpages/${file}.html";
507 my $info = "Web page template '${file}' not found.\n" .
508 "Please re-run 'locales.pl' in 'locale/${language}'.";
509 print(qq|<pre>$info</pre>|);
513 if ($self->{"DEBUG"}) {
514 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
517 if ($additional_params->{"DEBUG"}) {
518 $additional_params->{"DEBUG"} =
519 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
522 if (%main::myconfig) {
523 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
524 my $jsc_dateformat = $main::myconfig{"dateformat"};
525 $jsc_dateformat =~ s/d+/\%d/gi;
526 $jsc_dateformat =~ s/m+/\%m/gi;
527 $jsc_dateformat =~ s/y+/\%Y/gi;
528 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
531 $additional_params->{"conf_webdav"} = $main::webdav;
532 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
533 $additional_params->{"conf_latex_templates"} = $main::latex;
534 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
536 if (%main::debug_options) {
537 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
540 $main::lxdebug->leave_sub();
545 sub parse_html_template {
546 $main::lxdebug->enter_sub();
548 my ($self, $file, $additional_params) = @_;
550 $additional_params ||= { };
552 $file = $self->_prepare_html_template($file, $additional_params);
554 my $template = HTML::Template->new("filename" => $file,
555 "die_on_bad_params" => 0,
557 "case_sensitive" => 1,
558 "loop_context_vars" => 1,
561 foreach my $key ($template->param()) {
562 my $param = $additional_params->{$key} || $self->{$key};
563 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
564 $template->param($key => $param);
567 my $output = $template->output();
569 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
571 $main::lxdebug->leave_sub();
576 sub parse_html_template2 {
577 $main::lxdebug->enter_sub();
579 my ($self, $file, $additional_params) = @_;
581 $additional_params ||= { };
583 $file = $self->_prepare_html_template($file, $additional_params);
585 my $template = Template->new({ 'INTERPOLATE' => 0,
591 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
594 if (!$template->process($file, $additional_params, \$output)) {
595 print STDERR $template->error();
598 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
600 $main::lxdebug->leave_sub();
605 sub show_generic_error {
606 my ($self, $error, $title, $action) = @_;
609 $add_params->{"title"} = $title if ($title);
610 $self->{"label_error"} = $error;
614 map({ delete($self->{$_}); } qw(action));
615 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
616 if (!ref($self->{$_})); }
618 $add_params->{"SHOW_BUTTON"} = 1;
619 $add_params->{"BUTTON_LABEL"} = $action;
621 $add_params->{"VARIABLES"} = \@vars;
624 print($self->parse_html_template("generic/error", $add_params));
626 die("Error: $error\n");
629 sub show_generic_information {
630 my ($self, $error, $title) = @_;
633 $add_params->{"title"} = $title if ($title);
634 $self->{"label_information"} = $error;
637 print($self->parse_html_template("generic/information", $add_params));
639 die("Information: $error\n");
642 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
643 # changed it to accept an arbitrary number of triggers - sschoeling
645 $main::lxdebug->enter_sub();
648 my $myconfig = shift;
651 # set dateform for jsscript
654 "dd.mm.yy" => "%d.%m.%Y",
655 "dd-mm-yy" => "%d-%m-%Y",
656 "dd/mm/yy" => "%d/%m/%Y",
657 "mm/dd/yy" => "%m/%d/%Y",
658 "mm-dd-yy" => "%m-%d-%Y",
659 "yyyy-mm-dd" => "%Y-%m-%d",
662 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
663 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
670 inputField : "| . (shift) . qq|",
671 ifFormat :"$ifFormat",
672 align : "| . (shift) . qq|",
673 button : "| . (shift) . qq|"
679 <script type="text/javascript">
680 <!--| . join("", @triggers) . qq|//-->
684 $main::lxdebug->leave_sub();
687 } #end sub write_trigger
690 $main::lxdebug->enter_sub();
692 my ($self, $msg) = @_;
694 if ($self->{callback}) {
696 ($script, $argv) = split(/\?/, $self->{callback}, 2);
698 $script =~ s|[^a-zA-Z0-9_\.]||g;
699 exec("perl", "$script", $argv);
707 $main::lxdebug->leave_sub();
710 # sort of columns removed - empty sub
712 $main::lxdebug->enter_sub();
714 my ($self, @columns) = @_;
716 $main::lxdebug->leave_sub();
722 $main::lxdebug->enter_sub(2);
724 my ($self, $myconfig, $amount, $places, $dash) = @_;
730 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
732 my $neg = ($amount =~ s/^-//);
733 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
735 if (defined($places) && ($places ne '')) {
741 my ($actual_places) = ($amount =~ /\.(\d+)/);
742 $actual_places = length($actual_places);
743 $places = $actual_places > $places ? $actual_places : $places;
746 $amount = $self->round_amount($amount, $places);
749 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
750 my @p = split(/\./, $amount); # split amount at decimal point
752 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
755 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
758 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
759 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
760 ($neg ? "-$amount" : "$amount" ) ;
764 $main::lxdebug->leave_sub(2);
770 $main::lxdebug->enter_sub(2);
775 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
776 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
777 $input =~ s/\#\#/\#/g;
779 $main::lxdebug->leave_sub(2);
785 $main::lxdebug->enter_sub(2);
787 my ($self, $myconfig, $amount) = @_;
789 if ( ($myconfig->{numberformat} eq '1.000,00')
790 || ($myconfig->{numberformat} eq '1000,00')) {
795 if ($myconfig->{numberformat} eq "1'000.00") {
801 $main::lxdebug->leave_sub(2);
803 return ($amount * 1);
807 $main::lxdebug->enter_sub(2);
809 my ($self, $amount, $places) = @_;
812 # Rounding like "Kaufmannsrunden"
813 # Descr. http://de.wikipedia.org/wiki/Rundung
815 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
818 $amount = $amount * (10**($places));
819 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
821 $main::lxdebug->leave_sub(2);
823 return $round_amount;
828 $main::lxdebug->enter_sub();
830 my ($self, $myconfig, $userspath) = @_;
831 my ($template, $out);
835 $self->{"cwd"} = getcwd();
836 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
838 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
839 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
840 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
841 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
842 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
843 } elsif (($self->{"format"} =~ /html/i) ||
844 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
845 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
846 } elsif (($self->{"format"} =~ /xml/i) ||
847 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
848 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
849 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
850 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
851 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
852 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
853 } elsif ( defined $self->{'format'}) {
854 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
855 } elsif ( $self->{'format'} eq '' ) {
856 $self->error("No Outputformat given: $self->{'format'}");
857 } else { #Catch the rest
858 $self->error("Outputformat not defined: $self->{'format'}");
861 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
862 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
864 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
865 qw(email tel fax name signature company address businessnumber
866 co_ustid taxnumber duns));
867 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
868 qw(company address signature));
869 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
871 map({ $self->{"${_}"} = $myconfig->{$_}; }
875 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
877 # OUT is used for the media, screen, printer, email
878 # for postscript we store a copy in a temporary file
880 my $prepend_userspath;
882 if (!$self->{tmpfile}) {
883 $self->{tmpfile} = "${fileid}.$self->{IN}";
884 $prepend_userspath = 1;
887 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
889 $self->{tmpfile} =~ s|.*/||;
890 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
891 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
893 if ($template->uses_temp_file() || $self->{media} eq 'email') {
895 $self->{OUT} = ">$self->{tmpfile}";
899 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
901 open(OUT, ">-") or $self->error("STDOUT : $!");
905 if (!$template->parse(*OUT)) {
907 $self->error("$self->{IN} : " . $template->get_error());
912 if ($template->uses_temp_file() || $self->{media} eq 'email') {
914 if ($self->{media} eq 'email') {
916 my $mail = new Mailer;
918 map { $mail->{$_} = $self->{$_} }
919 qw(cc bcc subject message version format);
920 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
921 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
922 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
923 $mail->{fileid} = "$fileid.";
924 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
926 # if we send html or plain text inline
927 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
928 $mail->{contenttype} = "text/html";
930 $mail->{message} =~ s/\r\n/<br>\n/g;
931 $myconfig->{signature} =~ s/\\n/<br>\n/g;
932 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
934 open(IN, $self->{tmpfile})
935 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
937 $mail->{message} .= $_;
944 if (!$self->{"do_not_attach"}) {
945 @{ $mail->{attachments} } =
946 ({ "filename" => $self->{"tmpfile"},
947 "name" => $self->{"attachment_filename"} ?
948 $self->{"attachment_filename"} : $self->{"tmpfile"} });
951 $mail->{message} =~ s/\r\n/\n/g;
952 $myconfig->{signature} =~ s/\\n/\n/g;
953 $mail->{message} .= "\n-- \n$myconfig->{signature}";
957 my $err = $mail->send();
958 $self->error($self->cleanup . "$err") if ($err);
964 my $numbytes = (-s $self->{tmpfile});
965 open(IN, $self->{tmpfile})
966 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
968 $self->{copies} = 1 unless $self->{media} eq 'printer';
970 chdir("$self->{cwd}");
971 #print(STDERR "Kopien $self->{copies}\n");
972 #print(STDERR "OUT $self->{OUT}\n");
973 for my $i (1 .. $self->{copies}) {
975 open(OUT, $self->{OUT})
976 or $self->error($self->cleanup . "$self->{OUT} : $!");
978 $self->{attachment_filename} = ($self->{attachment_filename})
979 ? $self->{attachment_filename}
980 : $self->generate_attachment_filename();
983 print qq|Content-Type: | . $template->get_mime_type() . qq|
984 Content-Disposition: attachment; filename="$self->{attachment_filename}"
985 Content-Length: $numbytes
989 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1009 chdir("$self->{cwd}");
1010 $main::lxdebug->leave_sub();
1013 sub get_formname_translation {
1014 my ($self, $formname) = @_;
1016 $formname ||= $self->{formname};
1018 my %formname_translations = (
1019 bin_list => $main::locale->text('Bin List'),
1020 credit_note => $main::locale->text('Credit Note'),
1021 invoice => $main::locale->text('Invoice'),
1022 packing_list => $main::locale->text('Packing List'),
1023 pick_list => $main::locale->text('Pick List'),
1024 proforma => $main::locale->text('Proforma Invoice'),
1025 purchase_order => $main::locale->text('Purchase Order'),
1026 request_quotation => $main::locale->text('RFQ'),
1027 sales_order => $main::locale->text('Confirmation'),
1028 sales_quotation => $main::locale->text('Quotation'),
1029 storno_invoice => $main::locale->text('Storno Invoice'),
1030 storno_packing_list => $main::locale->text('Storno Packing List'),
1033 return $formname_translations{$formname}
1036 sub generate_attachment_filename {
1039 my $attachment_filename = $self->get_formname_translation();
1041 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1042 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1045 if ($attachment_filename && $self->{"${prefix}number"}) {
1046 $attachment_filename .= "_" . $self->{"${prefix}number"}
1047 . ( $self->{format} =~ /pdf/i ? ".pdf"
1048 : $self->{format} =~ /postscript/i ? ".ps"
1049 : $self->{format} =~ /opendocument/i ? ".odt"
1050 : $self->{format} =~ /html/i ? ".html"
1052 $attachment_filename =~ s/ /_/g;
1053 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1054 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1055 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1057 $attachment_filename = "";
1060 return $attachment_filename;
1064 $main::lxdebug->enter_sub();
1068 chdir("$self->{tmpdir}");
1071 if (-f "$self->{tmpfile}.err") {
1072 open(FH, "$self->{tmpfile}.err");
1077 if ($self->{tmpfile}) {
1078 $self->{tmpfile} =~ s|.*/||g;
1080 $self->{tmpfile} =~ s/\.\w+$//g;
1081 my $tmpfile = $self->{tmpfile};
1082 unlink(<$tmpfile.*>);
1085 chdir("$self->{cwd}");
1087 $main::lxdebug->leave_sub();
1093 $main::lxdebug->enter_sub();
1095 my ($self, $date, $myconfig) = @_;
1097 if ($date && $date =~ /\D/) {
1099 if ($myconfig->{dateformat} =~ /^yy/) {
1100 ($yy, $mm, $dd) = split /\D/, $date;
1102 if ($myconfig->{dateformat} =~ /^mm/) {
1103 ($mm, $dd, $yy) = split /\D/, $date;
1105 if ($myconfig->{dateformat} =~ /^dd/) {
1106 ($dd, $mm, $yy) = split /\D/, $date;
1111 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1112 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1114 $dd = "0$dd" if ($dd < 10);
1115 $mm = "0$mm" if ($mm < 10);
1117 $date = "$yy$mm$dd";
1120 $main::lxdebug->leave_sub();
1125 # Database routines used throughout
1128 $main::lxdebug->enter_sub(2);
1130 my ($self, $myconfig) = @_;
1132 # connect to database
1134 DBI->connect($myconfig->{dbconnect},
1135 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1139 if ($myconfig->{dboptions}) {
1140 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1143 $main::lxdebug->leave_sub(2);
1148 sub dbconnect_noauto {
1149 $main::lxdebug->enter_sub();
1151 my ($self, $myconfig) = @_;
1153 # connect to database
1155 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1156 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1160 if ($myconfig->{dboptions}) {
1161 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1164 $main::lxdebug->leave_sub();
1169 sub get_standard_dbh {
1170 $main::lxdebug->enter_sub(2);
1172 my ($self, $myconfig) = @_;
1174 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1176 $main::lxdebug->leave_sub(2);
1178 return $standard_dbh;
1181 sub update_balance {
1182 $main::lxdebug->enter_sub();
1184 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1186 # if we have a value, go do it
1189 # retrieve balance from table
1190 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1191 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1192 my ($balance) = $sth->fetchrow_array;
1198 $query = "UPDATE $table SET $field = $balance WHERE $where";
1199 do_query($self, $dbh, $query, @values);
1201 $main::lxdebug->leave_sub();
1204 sub update_exchangerate {
1205 $main::lxdebug->enter_sub();
1207 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1209 # some sanity check for currency
1211 $main::lxdebug->leave_sub();
1214 $query = qq|SELECT curr FROM defaults|;
1216 my ($currency) = selectrow_query($self, $dbh, $query);
1217 my ($defaultcurrency) = split m/:/, $currency;
1220 if ($curr eq $defaultcurrency) {
1221 $main::lxdebug->leave_sub();
1225 $query = qq|SELECT e.curr FROM exchangerate e
1226 WHERE e.curr = ? AND e.transdate = ?
1228 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1237 $buy = conv_i($buy, "NULL");
1238 $sell = conv_i($sell, "NULL");
1241 if ($buy != 0 && $sell != 0) {
1242 $set = "buy = $buy, sell = $sell";
1243 } elsif ($buy != 0) {
1244 $set = "buy = $buy";
1245 } elsif ($sell != 0) {
1246 $set = "sell = $sell";
1249 if ($sth->fetchrow_array) {
1250 $query = qq|UPDATE exchangerate
1256 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1257 VALUES (?, $buy, $sell, ?)|;
1260 do_query($self, $dbh, $query, $curr, $transdate);
1262 $main::lxdebug->leave_sub();
1265 sub save_exchangerate {
1266 $main::lxdebug->enter_sub();
1268 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1270 my $dbh = $self->dbconnect($myconfig);
1274 $buy = $rate if $fld eq 'buy';
1275 $sell = $rate if $fld eq 'sell';
1278 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1283 $main::lxdebug->leave_sub();
1286 sub get_exchangerate {
1287 $main::lxdebug->enter_sub();
1289 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1292 unless ($transdate) {
1293 $main::lxdebug->leave_sub();
1297 $query = qq|SELECT curr FROM defaults|;
1299 my ($currency) = selectrow_query($self, $dbh, $query);
1300 my ($defaultcurrency) = split m/:/, $currency;
1302 if ($currency eq $defaultcurrency) {
1303 $main::lxdebug->leave_sub();
1307 $query = qq|SELECT e.$fld FROM exchangerate e
1308 WHERE e.curr = ? AND e.transdate = ?|;
1309 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1313 $main::lxdebug->leave_sub();
1315 return $exchangerate;
1318 sub check_exchangerate {
1319 $main::lxdebug->enter_sub();
1321 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1323 unless ($transdate) {
1324 $main::lxdebug->leave_sub();
1328 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1330 if ($currency eq $defaultcurrency) {
1331 $main::lxdebug->leave_sub();
1335 my $dbh = $self->get_standard_dbh($myconfig);
1336 my $query = qq|SELECT e.$fld FROM exchangerate e
1337 WHERE e.curr = ? AND e.transdate = ?|;
1339 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1341 $exchangerate = 1 if ($exchangerate eq "");
1343 $main::lxdebug->leave_sub();
1345 return $exchangerate;
1348 sub get_default_currency {
1349 $main::lxdebug->enter_sub();
1351 my ($self, $myconfig) = @_;
1352 my $dbh = $self->get_standard_dbh($myconfig);
1354 my $query = qq|SELECT curr FROM defaults|;
1356 my ($curr) = selectrow_query($self, $dbh, $query);
1357 my ($defaultcurrency) = split m/:/, $curr;
1359 $main::lxdebug->leave_sub();
1361 return $defaultcurrency;
1365 sub set_payment_options {
1366 $main::lxdebug->enter_sub();
1368 my ($self, $myconfig, $transdate) = @_;
1370 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1372 my $dbh = $self->get_standard_dbh($myconfig);
1375 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1376 qq|FROM payment_terms p | .
1379 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1380 $self->{payment_terms}) =
1381 selectrow_query($self, $dbh, $query, $self->{payment_id});
1383 if ($transdate eq "") {
1384 if ($self->{invdate}) {
1385 $transdate = $self->{invdate};
1387 $transdate = $self->{transdate};
1392 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1393 qq|FROM payment_terms|;
1394 ($self->{netto_date}, $self->{skonto_date}) =
1395 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1397 my ($invtotal, $total);
1398 my (%amounts, %formatted_amounts);
1400 if ($self->{type} =~ /_order$/) {
1401 $amounts{invtotal} = $self->{ordtotal};
1402 $amounts{total} = $self->{ordtotal};
1404 } elsif ($self->{type} =~ /_quotation$/) {
1405 $amounts{invtotal} = $self->{quototal};
1406 $amounts{total} = $self->{quototal};
1409 $amounts{invtotal} = $self->{invtotal};
1410 $amounts{total} = $self->{total};
1413 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1415 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1416 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1417 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1419 foreach (keys %amounts) {
1420 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1421 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1424 if ($self->{"language_id"}) {
1426 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1427 qq|FROM translation_payment_terms t | .
1428 qq|LEFT JOIN language l ON t.language_id = l.id | .
1429 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1430 my ($description_long, $output_numberformat, $output_dateformat,
1431 $output_longdates) =
1432 selectrow_query($self, $dbh, $query,
1433 $self->{"language_id"}, $self->{"payment_id"});
1435 $self->{payment_terms} = $description_long if ($description_long);
1437 if ($output_dateformat) {
1438 foreach my $key (qw(netto_date skonto_date)) {
1440 $main::locale->reformat_date($myconfig, $self->{$key},
1446 if ($output_numberformat &&
1447 ($output_numberformat ne $myconfig->{"numberformat"})) {
1448 my $saved_numberformat = $myconfig->{"numberformat"};
1449 $myconfig->{"numberformat"} = $output_numberformat;
1450 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1451 $myconfig->{"numberformat"} = $saved_numberformat;
1455 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1456 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1457 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1458 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1459 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1460 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1461 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1463 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1465 $main::lxdebug->leave_sub();
1469 sub get_template_language {
1470 $main::lxdebug->enter_sub();
1472 my ($self, $myconfig) = @_;
1474 my $template_code = "";
1476 if ($self->{language_id}) {
1477 my $dbh = $self->get_standard_dbh($myconfig);
1478 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1479 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1482 $main::lxdebug->leave_sub();
1484 return $template_code;
1487 sub get_printer_code {
1488 $main::lxdebug->enter_sub();
1490 my ($self, $myconfig) = @_;
1492 my $template_code = "";
1494 if ($self->{printer_id}) {
1495 my $dbh = $self->get_standard_dbh($myconfig);
1496 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1497 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1500 $main::lxdebug->leave_sub();
1502 return $template_code;
1506 $main::lxdebug->enter_sub();
1508 my ($self, $myconfig) = @_;
1510 my $template_code = "";
1512 if ($self->{shipto_id}) {
1513 my $dbh = $self->get_standard_dbh($myconfig);
1514 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1515 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1516 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1519 $main::lxdebug->leave_sub();
1523 $main::lxdebug->enter_sub();
1525 my ($self, $dbh, $id, $module) = @_;
1530 foreach my $item (qw(name department_1 department_2 street zipcode city country
1531 contact phone fax email)) {
1532 if ($self->{"shipto$item"}) {
1533 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1535 push(@values, $self->{"shipto${item}"});
1539 if ($self->{shipto_id}) {
1540 my $query = qq|UPDATE shipto set
1542 shiptodepartment_1 = ?,
1543 shiptodepartment_2 = ?,
1552 WHERE shipto_id = ?|;
1553 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1555 my $query = qq|SELECT * FROM shipto
1556 WHERE shiptoname = ? AND
1557 shiptodepartment_1 = ? AND
1558 shiptodepartment_2 = ? AND
1559 shiptostreet = ? AND
1560 shiptozipcode = ? AND
1562 shiptocountry = ? AND
1563 shiptocontact = ? AND
1569 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1572 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1573 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1574 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1575 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1576 do_query($self, $dbh, $query, $id, @values, $module);
1581 $main::lxdebug->leave_sub();
1585 $main::lxdebug->enter_sub();
1587 my ($self, $dbh) = @_;
1589 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1590 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1591 $self->{"employee_id"} *= 1;
1593 $main::lxdebug->leave_sub();
1597 $main::lxdebug->enter_sub();
1599 my ($self, $myconfig, $salesman_id) = @_;
1601 $main::lxdebug->leave_sub() and return unless $salesman_id;
1603 my $dbh = $self->get_standard_dbh($myconfig);
1606 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1610 my $user = new User($main::memberfile, $login);
1611 map({ $self->{"salesman_$_"} = $user->{$_}; }
1612 qw(address businessnumber co_ustid company duns email fax name
1614 $self->{salesman_login} = $login;
1616 $self->{salesman_name} = $login
1617 if ($self->{salesman_name} eq "");
1619 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1622 $main::lxdebug->leave_sub();
1626 $main::lxdebug->enter_sub();
1628 my ($self, $myconfig) = @_;
1630 my $dbh = $self->get_standard_dbh($myconfig);
1631 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1632 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1634 $main::lxdebug->leave_sub();
1638 $main::lxdebug->enter_sub();
1640 my ($self, $dbh, $id, $key) = @_;
1642 $key = "all_contacts" unless ($key);
1645 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1646 qq|FROM contacts | .
1647 qq|WHERE cp_cv_id = ? | .
1648 qq|ORDER BY lower(cp_name)|;
1650 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1652 $main::lxdebug->leave_sub();
1656 $main::lxdebug->enter_sub();
1658 my ($self, $dbh, $key) = @_;
1660 my ($all, $old_id, $where, @values);
1662 if (ref($key) eq "HASH") {
1665 $key = "ALL_PROJECTS";
1667 foreach my $p (keys(%{$params})) {
1669 $all = $params->{$p};
1670 } elsif ($p eq "old_id") {
1671 $old_id = $params->{$p};
1672 } elsif ($p eq "key") {
1673 $key = $params->{$p};
1679 $where = "WHERE active ";
1681 if (ref($old_id) eq "ARRAY") {
1682 my @ids = grep({ $_ } @{$old_id});
1684 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1685 push(@values, @ids);
1688 $where .= " OR (id = ?) ";
1689 push(@values, $old_id);
1695 qq|SELECT id, projectnumber, description, active | .
1698 qq|ORDER BY lower(projectnumber)|;
1700 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1702 $main::lxdebug->leave_sub();
1706 $main::lxdebug->enter_sub();
1708 my ($self, $dbh, $vc_id, $key) = @_;
1710 $key = "all_shipto" unless ($key);
1712 # get shipping addresses
1713 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1715 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1717 $main::lxdebug->leave_sub();
1721 $main::lxdebug->enter_sub();
1723 my ($self, $dbh, $key) = @_;
1725 $key = "all_printers" unless ($key);
1727 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1729 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1731 $main::lxdebug->leave_sub();
1735 $main::lxdebug->enter_sub();
1737 my ($self, $dbh, $params) = @_;
1739 $key = $params->{key};
1740 $key = "all_charts" unless ($key);
1742 my $transdate = quote_db_date($params->{transdate});
1745 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1747 qq|LEFT JOIN taxkeys tk ON | .
1748 qq|(tk.id = (SELECT id FROM taxkeys | .
1749 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1750 qq| ORDER BY startdate DESC LIMIT 1)) | .
1751 qq|ORDER BY c.accno|;
1753 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1755 $main::lxdebug->leave_sub();
1758 sub _get_taxcharts {
1759 $main::lxdebug->enter_sub();
1761 my ($self, $dbh, $key) = @_;
1763 $key = "all_taxcharts" unless ($key);
1765 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1767 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1769 $main::lxdebug->leave_sub();
1773 $main::lxdebug->enter_sub();
1775 my ($self, $dbh, $key) = @_;
1777 $key = "all_taxzones" unless ($key);
1779 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1781 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1783 $main::lxdebug->leave_sub();
1786 sub _get_employees {
1787 $main::lxdebug->enter_sub();
1789 my ($self, $dbh, $default_key, $key) = @_;
1791 $key = $default_key unless ($key);
1792 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1794 $main::lxdebug->leave_sub();
1797 sub _get_business_types {
1798 $main::lxdebug->enter_sub();
1800 my ($self, $dbh, $key) = @_;
1802 $key = "all_business_types" unless ($key);
1804 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1806 $main::lxdebug->leave_sub();
1809 sub _get_languages {
1810 $main::lxdebug->enter_sub();
1812 my ($self, $dbh, $key) = @_;
1814 $key = "all_languages" unless ($key);
1816 my $query = qq|SELECT * FROM language ORDER BY id|;
1818 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1820 $main::lxdebug->leave_sub();
1823 sub _get_dunning_configs {
1824 $main::lxdebug->enter_sub();
1826 my ($self, $dbh, $key) = @_;
1828 $key = "all_dunning_configs" unless ($key);
1830 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1832 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1834 $main::lxdebug->leave_sub();
1837 sub _get_currencies {
1838 $main::lxdebug->enter_sub();
1840 my ($self, $dbh, $key) = @_;
1842 $key = "all_currencies" unless ($key);
1844 my $query = qq|SELECT curr AS currency FROM defaults|;
1846 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1848 $main::lxdebug->leave_sub();
1852 $main::lxdebug->enter_sub();
1854 my ($self, $dbh, $key) = @_;
1856 $key = "all_payments" unless ($key);
1858 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1860 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1862 $main::lxdebug->leave_sub();
1865 sub _get_customers {
1866 $main::lxdebug->enter_sub();
1868 my ($self, $dbh, $key) = @_;
1870 $key = "all_customers" unless ($key);
1872 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name|;
1874 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1876 $main::lxdebug->leave_sub();
1880 $main::lxdebug->enter_sub();
1882 my ($self, $dbh, $key) = @_;
1884 $key = "all_vendors" unless ($key);
1886 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1888 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1890 $main::lxdebug->leave_sub();
1893 sub _get_departments {
1894 $main::lxdebug->enter_sub();
1896 my ($self, $dbh, $key) = @_;
1898 $key = "all_departments" unless ($key);
1900 my $query = qq|SELECT * FROM department ORDER BY description|;
1902 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1904 $main::lxdebug->leave_sub();
1907 sub _get_price_factors {
1908 $main::lxdebug->enter_sub();
1910 my ($self, $dbh, $key) = @_;
1912 $key ||= "all_price_factors";
1914 my $query = qq|SELECT * FROM price_factors ORDER BY sortkey|;
1916 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1918 $main::lxdebug->leave_sub();
1922 $main::lxdebug->enter_sub();
1927 my $dbh = $self->get_standard_dbh(\%main::myconfig);
1928 my ($sth, $query, $ref);
1930 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1931 my $vc_id = $self->{"${vc}_id"};
1933 if ($params{"contacts"}) {
1934 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1937 if ($params{"shipto"}) {
1938 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1941 if ($params{"projects"} || $params{"all_projects"}) {
1942 $self->_get_projects($dbh, $params{"all_projects"} ?
1943 $params{"all_projects"} : $params{"projects"},
1944 $params{"all_projects"} ? 1 : 0);
1947 if ($params{"printers"}) {
1948 $self->_get_printers($dbh, $params{"printers"});
1951 if ($params{"languages"}) {
1952 $self->_get_languages($dbh, $params{"languages"});
1955 if ($params{"charts"}) {
1956 $self->_get_charts($dbh, $params{"charts"});
1959 if ($params{"taxcharts"}) {
1960 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1963 if ($params{"taxzones"}) {
1964 $self->_get_taxzones($dbh, $params{"taxzones"});
1967 if ($params{"employees"}) {
1968 $self->_get_employees($dbh, "all_employees", $params{"employees"});
1971 if ($params{"salesmen"}) {
1972 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
1975 if ($params{"business_types"}) {
1976 $self->_get_business_types($dbh, $params{"business_types"});
1979 if ($params{"dunning_configs"}) {
1980 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1983 if($params{"currencies"}) {
1984 $self->_get_currencies($dbh, $params{"currencies"});
1987 if($params{"customers"}) {
1988 $self->_get_customers($dbh, $params{"customers"});
1991 if($params{"vendors"}) {
1992 $self->_get_vendors($dbh, $params{"vendors"});
1995 if($params{"payments"}) {
1996 $self->_get_payments($dbh, $params{"payments"});
1999 if($params{"departments"}) {
2000 $self->_get_departments($dbh, $params{"departments"});
2003 if ($params{price_factors}) {
2004 $self->_get_price_factors($dbh, $params{price_factors});
2007 $main::lxdebug->leave_sub();
2010 # this sub gets the id and name from $table
2012 $main::lxdebug->enter_sub();
2014 my ($self, $myconfig, $table) = @_;
2016 # connect to database
2017 my $dbh = $self->get_standard_dbh($myconfig);
2019 $table = $table eq "customer" ? "customer" : "vendor";
2020 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2022 my ($query, @values);
2024 if (!$self->{openinvoices}) {
2026 if ($self->{customernumber} ne "") {
2027 $where = qq|(vc.customernumber ILIKE ?)|;
2028 push(@values, '%' . $self->{customernumber} . '%');
2030 $where = qq|(vc.name ILIKE ?)|;
2031 push(@values, '%' . $self->{$table} . '%');
2035 qq~SELECT vc.id, vc.name,
2036 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2038 WHERE $where AND (NOT vc.obsolete)
2042 qq~SELECT DISTINCT vc.id, vc.name,
2043 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2045 JOIN $table vc ON (a.${table}_id = vc.id)
2046 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2048 push(@values, '%' . $self->{$table} . '%');
2051 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2053 $main::lxdebug->leave_sub();
2055 return scalar(@{ $self->{name_list} });
2058 # the selection sub is used in the AR, AP, IS, IR and OE module
2061 $main::lxdebug->enter_sub();
2063 my ($self, $myconfig, $table, $module) = @_;
2066 my $dbh = $self->get_standard_dbh($myconfig);
2068 $table = $table eq "customer" ? "customer" : "vendor";
2070 my $query = qq|SELECT count(*) FROM $table|;
2071 my ($count) = selectrow_query($self, $dbh, $query);
2073 # build selection list
2074 if ($count < $myconfig->{vclimit}) {
2075 $query = qq|SELECT id, name, salesman_id
2076 FROM $table WHERE NOT obsolete
2078 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2082 $self->get_employee($dbh);
2084 # setup sales contacts
2085 $query = qq|SELECT e.id, e.name
2087 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2088 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2091 push(@{ $self->{all_employees} },
2092 { id => $self->{employee_id},
2093 name => $self->{employee} });
2095 # sort the whole thing
2096 @{ $self->{all_employees} } =
2097 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2099 if ($module eq 'AR') {
2101 # prepare query for departments
2102 $query = qq|SELECT id, description
2105 ORDER BY description|;
2108 $query = qq|SELECT id, description
2110 ORDER BY description|;
2113 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2116 $query = qq|SELECT id, description
2120 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2123 $query = qq|SELECT printer_description, id
2125 ORDER BY printer_description|;
2127 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2130 $query = qq|SELECT id, description
2134 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2136 $main::lxdebug->leave_sub();
2139 sub language_payment {
2140 $main::lxdebug->enter_sub();
2142 my ($self, $myconfig) = @_;
2144 my $dbh = $self->get_standard_dbh($myconfig);
2146 my $query = qq|SELECT id, description
2150 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2153 $query = qq|SELECT printer_description, id
2155 ORDER BY printer_description|;
2157 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2160 $query = qq|SELECT id, description
2164 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2166 # get buchungsgruppen
2167 $query = qq|SELECT id, description
2168 FROM buchungsgruppen|;
2170 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2172 $main::lxdebug->leave_sub();
2175 # this is only used for reports
2176 sub all_departments {
2177 $main::lxdebug->enter_sub();
2179 my ($self, $myconfig, $table) = @_;
2181 my $dbh = $self->get_standard_dbh($myconfig);
2184 if ($table eq 'customer') {
2185 $where = "WHERE role = 'P' ";
2188 my $query = qq|SELECT id, description
2191 ORDER BY description|;
2192 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2194 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2196 $main::lxdebug->leave_sub();
2200 $main::lxdebug->enter_sub();
2202 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2205 if ($table eq "customer") {
2214 $self->all_vc($myconfig, $table, $module);
2216 # get last customers or vendors
2217 my ($query, $sth, $ref);
2219 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2224 my $transdate = "current_date";
2225 if ($self->{transdate}) {
2226 $transdate = $dbh->quote($self->{transdate});
2229 # now get the account numbers
2230 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2231 FROM chart c, taxkeys tk
2232 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2233 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2236 $sth = $dbh->prepare($query);
2238 do_statement($self, $sth, $query, '%' . $module . '%');
2240 $self->{accounts} = "";
2241 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2243 foreach my $key (split(/:/, $ref->{link})) {
2244 if ($key =~ /\Q$module\E/) {
2246 # cross reference for keys
2247 $xkeyref{ $ref->{accno} } = $key;
2249 push @{ $self->{"${module}_links"}{$key} },
2250 { accno => $ref->{accno},
2251 description => $ref->{description},
2252 taxkey => $ref->{taxkey_id},
2253 tax_id => $ref->{tax_id} };
2255 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2261 # get taxkeys and description
2262 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2263 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2265 if (($module eq "AP") || ($module eq "AR")) {
2266 # get tax rates and description
2267 $query = qq|SELECT * FROM tax|;
2268 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2274 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2275 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2276 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2277 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2279 d.description AS department,
2282 JOIN $table c ON (a.${table}_id = c.id)
2283 LEFT JOIN employee e ON (e.id = a.employee_id)
2284 LEFT JOIN department d ON (d.id = a.department_id)
2286 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2288 foreach $key (keys %$ref) {
2289 $self->{$key} = $ref->{$key};
2292 my $transdate = "current_date";
2293 if ($self->{transdate}) {
2294 $transdate = $dbh->quote($self->{transdate});
2297 # now get the account numbers
2298 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2300 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2302 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2303 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2306 $sth = $dbh->prepare($query);
2307 do_statement($self, $sth, $query, "%$module%");
2309 $self->{accounts} = "";
2310 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2312 foreach my $key (split(/:/, $ref->{link})) {
2313 if ($key =~ /\Q$module\E/) {
2315 # cross reference for keys
2316 $xkeyref{ $ref->{accno} } = $key;
2318 push @{ $self->{"${module}_links"}{$key} },
2319 { accno => $ref->{accno},
2320 description => $ref->{description},
2321 taxkey => $ref->{taxkey_id},
2322 tax_id => $ref->{tax_id} };
2324 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2330 # get amounts from individual entries
2333 c.accno, c.description,
2334 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2338 LEFT JOIN chart c ON (c.id = a.chart_id)
2339 LEFT JOIN project p ON (p.id = a.project_id)
2340 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2341 WHERE (tk.taxkey_id=a.taxkey) AND
2342 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2343 THEN tk.chart_id = a.chart_id
2346 OR (c.link='%tax%')) AND
2347 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2348 WHERE a.trans_id = ?
2349 AND a.fx_transaction = '0'
2350 ORDER BY a.oid, a.transdate|;
2351 $sth = $dbh->prepare($query);
2352 do_statement($self, $sth, $query, $self->{id});
2354 # get exchangerate for currency
2355 $self->{exchangerate} =
2356 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2359 # store amounts in {acc_trans}{$key} for multiple accounts
2360 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2361 $ref->{exchangerate} =
2362 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2363 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2366 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2367 $ref->{amount} *= -1;
2369 $ref->{index} = $index;
2371 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2377 d.curr AS currencies, d.closedto, d.revtrans,
2378 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2379 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2381 $ref = selectfirst_hashref_query($self, $dbh, $query);
2382 map { $self->{$_} = $ref->{$_} } keys %$ref;
2389 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2390 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2391 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2393 $ref = selectfirst_hashref_query($self, $dbh, $query);
2394 map { $self->{$_} = $ref->{$_} } keys %$ref;
2396 if ($self->{"$self->{vc}_id"}) {
2398 # only setup currency
2399 ($self->{currency}) = split(/:/, $self->{currencies});
2403 $self->lastname_used($dbh, $myconfig, $table, $module);
2405 # get exchangerate for currency
2406 $self->{exchangerate} =
2407 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2413 $main::lxdebug->leave_sub();
2417 $main::lxdebug->enter_sub();
2419 my ($self, $dbh, $myconfig, $table, $module) = @_;
2421 my $arap = ($table eq 'customer') ? "ar" : "ap";
2422 $table = $table eq "customer" ? "customer" : "vendor";
2423 my $where = "1 = 1";
2425 if ($self->{type} =~ /_order/) {
2427 $where = "quotation = '0'";
2429 if ($self->{type} =~ /_quotation/) {
2431 $where = "quotation = '1'";
2434 my $query = qq|SELECT MAX(id) FROM $arap
2435 WHERE $where AND ${table}_id > 0|;
2436 my ($trans_id) = selectrow_query($self, $dbh, $query);
2441 a.curr, a.${table}_id, a.department_id,
2442 d.description AS department,
2443 ct.name, current_date + ct.terms AS duedate
2445 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2446 LEFT JOIN department d ON (a.department_id = d.id)
2448 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2449 $self->{department}, $self->{$table}, $self->{duedate})
2450 = selectrow_query($self, $dbh, $query, $trans_id);
2452 $main::lxdebug->leave_sub();
2456 $main::lxdebug->enter_sub();
2458 my ($self, $myconfig, $thisdate, $days) = @_;
2460 my $dbh = $self->get_standard_dbh($myconfig);
2465 my $dateformat = $myconfig->{dateformat};
2466 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2467 $thisdate = $dbh->quote($thisdate);
2468 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2470 $query = qq|SELECT current_date AS thisdate|;
2473 ($thisdate) = selectrow_query($self, $dbh, $query);
2475 $main::lxdebug->leave_sub();
2481 $main::lxdebug->enter_sub();
2483 my ($self, $string) = @_;
2485 if ($string !~ /%/) {
2486 $string = "%$string%";
2489 $string =~ s/\'/\'\'/g;
2491 $main::lxdebug->leave_sub();
2497 $main::lxdebug->enter_sub();
2499 my ($self, $flds, $new, $count, $numrows) = @_;
2503 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2508 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2510 $j = $item->{ndx} - 1;
2511 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2515 for $i ($count + 1 .. $numrows) {
2516 map { delete $self->{"${_}_$i"} } @{$flds};
2519 $main::lxdebug->leave_sub();
2523 $main::lxdebug->enter_sub();
2525 my ($self, $myconfig) = @_;
2529 my $dbh = $self->dbconnect_noauto($myconfig);
2531 my $query = qq|DELETE FROM status
2532 WHERE (formname = ?) AND (trans_id = ?)|;
2533 my $sth = prepare_query($self, $dbh, $query);
2535 if ($self->{formname} =~ /(check|receipt)/) {
2536 for $i (1 .. $self->{rowcount}) {
2537 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2540 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2544 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2545 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2547 my %queued = split / /, $self->{queued};
2550 if ($self->{formname} =~ /(check|receipt)/) {
2552 # this is a check or receipt, add one entry for each lineitem
2553 my ($accno) = split /--/, $self->{account};
2554 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2555 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2556 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2557 $sth = prepare_query($self, $dbh, $query);
2559 for $i (1 .. $self->{rowcount}) {
2560 if ($self->{"checked_$i"}) {
2561 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2567 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2568 VALUES (?, ?, ?, ?, ?)|;
2569 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2570 $queued{$self->{formname}}, $self->{formname});
2576 $main::lxdebug->leave_sub();
2580 $main::lxdebug->enter_sub();
2582 my ($self, $dbh) = @_;
2584 my ($query, $printed, $emailed);
2586 my $formnames = $self->{printed};
2587 my $emailforms = $self->{emailed};
2589 $query = qq|DELETE FROM status
2590 WHERE (formname = ?) AND (trans_id = ?)|;
2591 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2593 # this only applies to the forms
2594 # checks and receipts are posted when printed or queued
2596 if ($self->{queued}) {
2597 my %queued = split / /, $self->{queued};
2599 foreach my $formname (keys %queued) {
2600 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2601 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2603 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2604 VALUES (?, ?, ?, ?, ?)|;
2605 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2607 $formnames =~ s/\Q$self->{formname}\E//;
2608 $emailforms =~ s/\Q$self->{formname}\E//;
2613 # save printed, emailed info
2614 $formnames =~ s/^ +//g;
2615 $emailforms =~ s/^ +//g;
2618 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2619 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2621 foreach my $formname (keys %status) {
2622 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2623 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2625 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2626 VALUES (?, ?, ?, ?)|;
2627 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2630 $main::lxdebug->leave_sub();
2634 # $main::locale->text('SAVED')
2635 # $main::locale->text('DELETED')
2636 # $main::locale->text('ADDED')
2637 # $main::locale->text('PAYMENT POSTED')
2638 # $main::locale->text('POSTED')
2639 # $main::locale->text('POSTED AS NEW')
2640 # $main::locale->text('ELSE')
2641 # $main::locale->text('SAVED FOR DUNNING')
2642 # $main::locale->text('DUNNING STARTED')
2643 # $main::locale->text('PRINTED')
2644 # $main::locale->text('MAILED')
2645 # $main::locale->text('SCREENED')
2646 # $main::locale->text('CANCELED')
2647 # $main::locale->text('invoice')
2648 # $main::locale->text('proforma')
2649 # $main::locale->text('sales_order')
2650 # $main::locale->text('packing_list')
2651 # $main::locale->text('pick_list')
2652 # $main::locale->text('purchase_order')
2653 # $main::locale->text('bin_list')
2654 # $main::locale->text('sales_quotation')
2655 # $main::locale->text('request_quotation')
2658 $main::lxdebug->enter_sub();
2663 if(!exists $self->{employee_id}) {
2664 &get_employee($self, $dbh);
2668 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2669 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2670 my @values = (conv_i($self->{id}), $self->{login},
2671 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2672 do_query($self, $dbh, $query, @values);
2674 $main::lxdebug->leave_sub();
2678 $main::lxdebug->enter_sub();
2680 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2681 my ($orderBy, $desc) = split(/\-\-/, $order);
2682 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2685 if ($trans_id ne "") {
2687 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 | .
2688 qq|FROM history_erp h | .
2689 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2690 qq|WHERE trans_id = | . $trans_id
2691 . $restriction . qq| |
2694 my $sth = $dbh->prepare($query) || $self->dberror($query);
2696 $sth->execute() || $self->dberror("$query");
2698 while(my $hash_ref = $sth->fetchrow_hashref()) {
2699 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2700 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2701 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2702 $tempArray[$i++] = $hash_ref;
2704 $main::lxdebug->leave_sub() and return \@tempArray
2705 if ($i > 0 && $tempArray[0] ne "");
2707 $main::lxdebug->leave_sub();
2711 sub update_defaults {
2712 $main::lxdebug->enter_sub();
2714 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2717 if ($provided_dbh) {
2718 $dbh = $provided_dbh;
2720 $dbh = $self->dbconnect_noauto($myconfig);
2722 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2723 my $sth = $dbh->prepare($query);
2725 $sth->execute || $self->dberror($query);
2726 my ($var) = $sth->fetchrow_array;
2729 if ($var =~ m/\d+$/) {
2730 my $new_var = (substr $var, $-[0]) * 1 + 1;
2731 my $len_diff = length($var) - $-[0] - length($new_var);
2732 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2738 $query = qq|UPDATE defaults SET $fld = ?|;
2739 do_query($self, $dbh, $query, $var);
2741 if (!$provided_dbh) {
2746 $main::lxdebug->leave_sub();
2751 sub update_business {
2752 $main::lxdebug->enter_sub();
2754 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2757 if ($provided_dbh) {
2758 $dbh = $provided_dbh;
2760 $dbh = $self->dbconnect_noauto($myconfig);
2763 qq|SELECT customernumberinit FROM business
2764 WHERE id = ? FOR UPDATE|;
2765 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2767 if ($var =~ m/\d+$/) {
2768 my $new_var = (substr $var, $-[0]) * 1 + 1;
2769 my $len_diff = length($var) - $-[0] - length($new_var);
2770 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2776 $query = qq|UPDATE business
2777 SET customernumberinit = ?
2779 do_query($self, $dbh, $query, $var, $business_id);
2781 if (!$provided_dbh) {
2786 $main::lxdebug->leave_sub();
2791 sub get_partsgroup {
2792 $main::lxdebug->enter_sub();
2794 my ($self, $myconfig, $p) = @_;
2796 my $dbh = $self->get_standard_dbh($myconfig);
2798 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2800 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2803 if ($p->{searchitems} eq 'part') {
2804 $query .= qq|WHERE p.inventory_accno_id > 0|;
2806 if ($p->{searchitems} eq 'service') {
2807 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2809 if ($p->{searchitems} eq 'assembly') {
2810 $query .= qq|WHERE p.assembly = '1'|;
2812 if ($p->{searchitems} eq 'labor') {
2813 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2816 $query .= qq|ORDER BY partsgroup|;
2819 $query = qq|SELECT id, partsgroup FROM partsgroup
2820 ORDER BY partsgroup|;
2823 if ($p->{language_code}) {
2824 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2825 t.description AS translation
2827 JOIN parts p ON (p.partsgroup_id = pg.id)
2828 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2829 ORDER BY translation|;
2830 @values = ($p->{language_code});
2833 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2835 $main::lxdebug->leave_sub();
2838 sub get_pricegroup {
2839 $main::lxdebug->enter_sub();
2841 my ($self, $myconfig, $p) = @_;
2843 my $dbh = $self->get_standard_dbh($myconfig);
2845 my $query = qq|SELECT p.id, p.pricegroup
2848 $query .= qq| ORDER BY pricegroup|;
2851 $query = qq|SELECT id, pricegroup FROM pricegroup
2852 ORDER BY pricegroup|;
2855 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2857 $main::lxdebug->leave_sub();
2861 # usage $form->all_years($myconfig, [$dbh])
2862 # return list of all years where bookings found
2865 $main::lxdebug->enter_sub();
2867 my ($self, $myconfig, $dbh) = @_;
2869 $dbh ||= $self->get_standard_dbh($myconfig);
2872 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2873 (SELECT MAX(transdate) FROM acc_trans)|;
2874 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2876 if ($myconfig->{dateformat} =~ /^yy/) {
2877 ($startdate) = split /\W/, $startdate;
2878 ($enddate) = split /\W/, $enddate;
2880 (@_) = split /\W/, $startdate;
2882 (@_) = split /\W/, $enddate;
2887 $startdate = substr($startdate,0,4);
2888 $enddate = substr($enddate,0,4);
2890 while ($enddate >= $startdate) {
2891 push @all_years, $enddate--;
2896 $main::lxdebug->leave_sub();