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,
589 'PLUGIN_BASE' => 'SL::Template::Plugin',
592 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
595 if (!$template->process($file, $additional_params, \$output)) {
596 print STDERR $template->error();
599 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
601 $main::lxdebug->leave_sub();
606 sub show_generic_error {
607 my ($self, $error, $title, $action) = @_;
610 $add_params->{"title"} = $title if ($title);
611 $self->{"label_error"} = $error;
615 map({ delete($self->{$_}); } qw(action));
616 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
617 if (!ref($self->{$_})); }
619 $add_params->{"SHOW_BUTTON"} = 1;
620 $add_params->{"BUTTON_LABEL"} = $action;
622 $add_params->{"VARIABLES"} = \@vars;
625 print($self->parse_html_template("generic/error", $add_params));
627 die("Error: $error\n");
630 sub show_generic_information {
631 my ($self, $error, $title) = @_;
634 $add_params->{"title"} = $title if ($title);
635 $self->{"label_information"} = $error;
638 print($self->parse_html_template("generic/information", $add_params));
640 die("Information: $error\n");
643 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
644 # changed it to accept an arbitrary number of triggers - sschoeling
646 $main::lxdebug->enter_sub();
649 my $myconfig = shift;
652 # set dateform for jsscript
655 "dd.mm.yy" => "%d.%m.%Y",
656 "dd-mm-yy" => "%d-%m-%Y",
657 "dd/mm/yy" => "%d/%m/%Y",
658 "mm/dd/yy" => "%m/%d/%Y",
659 "mm-dd-yy" => "%m-%d-%Y",
660 "yyyy-mm-dd" => "%Y-%m-%d",
663 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
664 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
671 inputField : "| . (shift) . qq|",
672 ifFormat :"$ifFormat",
673 align : "| . (shift) . qq|",
674 button : "| . (shift) . qq|"
680 <script type="text/javascript">
681 <!--| . join("", @triggers) . qq|//-->
685 $main::lxdebug->leave_sub();
688 } #end sub write_trigger
691 $main::lxdebug->enter_sub();
693 my ($self, $msg) = @_;
695 if ($self->{callback}) {
697 ($script, $argv) = split(/\?/, $self->{callback}, 2);
699 $script =~ s|[^a-zA-Z0-9_\.]||g;
700 exec("perl", "$script", $argv);
708 $main::lxdebug->leave_sub();
711 # sort of columns removed - empty sub
713 $main::lxdebug->enter_sub();
715 my ($self, @columns) = @_;
717 $main::lxdebug->leave_sub();
723 $main::lxdebug->enter_sub(2);
725 my ($self, $myconfig, $amount, $places, $dash) = @_;
731 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
733 my $neg = ($amount =~ s/^-//);
734 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
736 if (defined($places) && ($places ne '')) {
742 my ($actual_places) = ($amount =~ /\.(\d+)/);
743 $actual_places = length($actual_places);
744 $places = $actual_places > $places ? $actual_places : $places;
747 $amount = $self->round_amount($amount, $places);
750 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
751 my @p = split(/\./, $amount); # split amount at decimal point
753 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
756 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
759 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
760 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
761 ($neg ? "-$amount" : "$amount" ) ;
765 $main::lxdebug->leave_sub(2);
771 $main::lxdebug->enter_sub(2);
776 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
777 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
778 $input =~ s/\#\#/\#/g;
780 $main::lxdebug->leave_sub(2);
786 $main::lxdebug->enter_sub(2);
788 my ($self, $myconfig, $amount) = @_;
790 if ( ($myconfig->{numberformat} eq '1.000,00')
791 || ($myconfig->{numberformat} eq '1000,00')) {
796 if ($myconfig->{numberformat} eq "1'000.00") {
802 $main::lxdebug->leave_sub(2);
804 return ($amount * 1);
808 $main::lxdebug->enter_sub(2);
810 my ($self, $amount, $places) = @_;
813 # Rounding like "Kaufmannsrunden"
814 # Descr. http://de.wikipedia.org/wiki/Rundung
816 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
819 $amount = $amount * (10**($places));
820 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
822 $main::lxdebug->leave_sub(2);
824 return $round_amount;
829 $main::lxdebug->enter_sub();
831 my ($self, $myconfig, $userspath) = @_;
832 my ($template, $out);
836 $self->{"cwd"} = getcwd();
837 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
839 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
840 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
841 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
842 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
843 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
844 } elsif (($self->{"format"} =~ /html/i) ||
845 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
846 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
847 } elsif (($self->{"format"} =~ /xml/i) ||
848 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
849 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
850 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
851 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
852 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
853 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
854 } elsif ( defined $self->{'format'}) {
855 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
856 } elsif ( $self->{'format'} eq '' ) {
857 $self->error("No Outputformat given: $self->{'format'}");
858 } else { #Catch the rest
859 $self->error("Outputformat not defined: $self->{'format'}");
862 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
863 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
865 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
866 qw(email tel fax name signature company address businessnumber
867 co_ustid taxnumber duns));
868 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
869 qw(company address signature));
870 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
872 map({ $self->{"${_}"} = $myconfig->{$_}; }
876 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
878 # OUT is used for the media, screen, printer, email
879 # for postscript we store a copy in a temporary file
881 my $prepend_userspath;
883 if (!$self->{tmpfile}) {
884 $self->{tmpfile} = "${fileid}.$self->{IN}";
885 $prepend_userspath = 1;
888 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
890 $self->{tmpfile} =~ s|.*/||;
891 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
892 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
894 if ($template->uses_temp_file() || $self->{media} eq 'email') {
896 $self->{OUT} = ">$self->{tmpfile}";
900 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
902 open(OUT, ">-") or $self->error("STDOUT : $!");
906 if (!$template->parse(*OUT)) {
908 $self->error("$self->{IN} : " . $template->get_error());
913 if ($template->uses_temp_file() || $self->{media} eq 'email') {
915 if ($self->{media} eq 'email') {
917 my $mail = new Mailer;
919 map { $mail->{$_} = $self->{$_} }
920 qw(cc bcc subject message version format);
921 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
922 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
923 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
924 $mail->{fileid} = "$fileid.";
925 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
927 # if we send html or plain text inline
928 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
929 $mail->{contenttype} = "text/html";
931 $mail->{message} =~ s/\r\n/<br>\n/g;
932 $myconfig->{signature} =~ s/\\n/<br>\n/g;
933 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
935 open(IN, $self->{tmpfile})
936 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
938 $mail->{message} .= $_;
945 if (!$self->{"do_not_attach"}) {
946 @{ $mail->{attachments} } =
947 ({ "filename" => $self->{"tmpfile"},
948 "name" => $self->{"attachment_filename"} ?
949 $self->{"attachment_filename"} : $self->{"tmpfile"} });
952 $mail->{message} =~ s/\r\n/\n/g;
953 $myconfig->{signature} =~ s/\\n/\n/g;
954 $mail->{message} .= "\n-- \n$myconfig->{signature}";
958 my $err = $mail->send();
959 $self->error($self->cleanup . "$err") if ($err);
965 my $numbytes = (-s $self->{tmpfile});
966 open(IN, $self->{tmpfile})
967 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
969 $self->{copies} = 1 unless $self->{media} eq 'printer';
971 chdir("$self->{cwd}");
972 #print(STDERR "Kopien $self->{copies}\n");
973 #print(STDERR "OUT $self->{OUT}\n");
974 for my $i (1 .. $self->{copies}) {
976 open(OUT, $self->{OUT})
977 or $self->error($self->cleanup . "$self->{OUT} : $!");
979 $self->{attachment_filename} = ($self->{attachment_filename})
980 ? $self->{attachment_filename}
981 : $self->generate_attachment_filename();
984 print qq|Content-Type: | . $template->get_mime_type() . qq|
985 Content-Disposition: attachment; filename="$self->{attachment_filename}"
986 Content-Length: $numbytes
990 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1010 chdir("$self->{cwd}");
1011 $main::lxdebug->leave_sub();
1014 sub get_formname_translation {
1015 my ($self, $formname) = @_;
1017 $formname ||= $self->{formname};
1019 my %formname_translations = (
1020 bin_list => $main::locale->text('Bin List'),
1021 credit_note => $main::locale->text('Credit Note'),
1022 invoice => $main::locale->text('Invoice'),
1023 packing_list => $main::locale->text('Packing List'),
1024 pick_list => $main::locale->text('Pick List'),
1025 proforma => $main::locale->text('Proforma Invoice'),
1026 purchase_order => $main::locale->text('Purchase Order'),
1027 request_quotation => $main::locale->text('RFQ'),
1028 sales_order => $main::locale->text('Confirmation'),
1029 sales_quotation => $main::locale->text('Quotation'),
1030 storno_invoice => $main::locale->text('Storno Invoice'),
1031 storno_packing_list => $main::locale->text('Storno Packing List'),
1034 return $formname_translations{$formname}
1037 sub generate_attachment_filename {
1040 my $attachment_filename = $self->get_formname_translation();
1042 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1043 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1046 if ($attachment_filename && $self->{"${prefix}number"}) {
1047 $attachment_filename .= "_" . $self->{"${prefix}number"}
1048 . ( $self->{format} =~ /pdf/i ? ".pdf"
1049 : $self->{format} =~ /postscript/i ? ".ps"
1050 : $self->{format} =~ /opendocument/i ? ".odt"
1051 : $self->{format} =~ /html/i ? ".html"
1053 $attachment_filename =~ s/ /_/g;
1054 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1055 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1056 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1058 $attachment_filename = "";
1061 return $attachment_filename;
1065 $main::lxdebug->enter_sub();
1069 chdir("$self->{tmpdir}");
1072 if (-f "$self->{tmpfile}.err") {
1073 open(FH, "$self->{tmpfile}.err");
1078 if ($self->{tmpfile}) {
1079 $self->{tmpfile} =~ s|.*/||g;
1081 $self->{tmpfile} =~ s/\.\w+$//g;
1082 my $tmpfile = $self->{tmpfile};
1083 unlink(<$tmpfile.*>);
1086 chdir("$self->{cwd}");
1088 $main::lxdebug->leave_sub();
1094 $main::lxdebug->enter_sub();
1096 my ($self, $date, $myconfig) = @_;
1098 if ($date && $date =~ /\D/) {
1100 if ($myconfig->{dateformat} =~ /^yy/) {
1101 ($yy, $mm, $dd) = split /\D/, $date;
1103 if ($myconfig->{dateformat} =~ /^mm/) {
1104 ($mm, $dd, $yy) = split /\D/, $date;
1106 if ($myconfig->{dateformat} =~ /^dd/) {
1107 ($dd, $mm, $yy) = split /\D/, $date;
1112 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1113 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1115 $dd = "0$dd" if ($dd < 10);
1116 $mm = "0$mm" if ($mm < 10);
1118 $date = "$yy$mm$dd";
1121 $main::lxdebug->leave_sub();
1126 # Database routines used throughout
1129 $main::lxdebug->enter_sub(2);
1131 my ($self, $myconfig) = @_;
1133 # connect to database
1135 DBI->connect($myconfig->{dbconnect},
1136 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1140 if ($myconfig->{dboptions}) {
1141 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1144 $main::lxdebug->leave_sub(2);
1149 sub dbconnect_noauto {
1150 $main::lxdebug->enter_sub();
1152 my ($self, $myconfig) = @_;
1154 # connect to database
1156 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1157 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1161 if ($myconfig->{dboptions}) {
1162 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1165 $main::lxdebug->leave_sub();
1170 sub get_standard_dbh {
1171 $main::lxdebug->enter_sub(2);
1173 my ($self, $myconfig) = @_;
1175 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1177 $main::lxdebug->leave_sub(2);
1179 return $standard_dbh;
1182 sub update_balance {
1183 $main::lxdebug->enter_sub();
1185 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1187 # if we have a value, go do it
1190 # retrieve balance from table
1191 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1192 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1193 my ($balance) = $sth->fetchrow_array;
1199 $query = "UPDATE $table SET $field = $balance WHERE $where";
1200 do_query($self, $dbh, $query, @values);
1202 $main::lxdebug->leave_sub();
1205 sub update_exchangerate {
1206 $main::lxdebug->enter_sub();
1208 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1210 # some sanity check for currency
1212 $main::lxdebug->leave_sub();
1215 $query = qq|SELECT curr FROM defaults|;
1217 my ($currency) = selectrow_query($self, $dbh, $query);
1218 my ($defaultcurrency) = split m/:/, $currency;
1221 if ($curr eq $defaultcurrency) {
1222 $main::lxdebug->leave_sub();
1226 $query = qq|SELECT e.curr FROM exchangerate e
1227 WHERE e.curr = ? AND e.transdate = ?
1229 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1238 $buy = conv_i($buy, "NULL");
1239 $sell = conv_i($sell, "NULL");
1242 if ($buy != 0 && $sell != 0) {
1243 $set = "buy = $buy, sell = $sell";
1244 } elsif ($buy != 0) {
1245 $set = "buy = $buy";
1246 } elsif ($sell != 0) {
1247 $set = "sell = $sell";
1250 if ($sth->fetchrow_array) {
1251 $query = qq|UPDATE exchangerate
1257 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1258 VALUES (?, $buy, $sell, ?)|;
1261 do_query($self, $dbh, $query, $curr, $transdate);
1263 $main::lxdebug->leave_sub();
1266 sub save_exchangerate {
1267 $main::lxdebug->enter_sub();
1269 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1271 my $dbh = $self->dbconnect($myconfig);
1275 $buy = $rate if $fld eq 'buy';
1276 $sell = $rate if $fld eq 'sell';
1279 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1284 $main::lxdebug->leave_sub();
1287 sub get_exchangerate {
1288 $main::lxdebug->enter_sub();
1290 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1293 unless ($transdate) {
1294 $main::lxdebug->leave_sub();
1298 $query = qq|SELECT curr FROM defaults|;
1300 my ($currency) = selectrow_query($self, $dbh, $query);
1301 my ($defaultcurrency) = split m/:/, $currency;
1303 if ($currency eq $defaultcurrency) {
1304 $main::lxdebug->leave_sub();
1308 $query = qq|SELECT e.$fld FROM exchangerate e
1309 WHERE e.curr = ? AND e.transdate = ?|;
1310 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1314 $main::lxdebug->leave_sub();
1316 return $exchangerate;
1319 sub check_exchangerate {
1320 $main::lxdebug->enter_sub();
1322 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1324 unless ($transdate) {
1325 $main::lxdebug->leave_sub();
1329 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1331 if ($currency eq $defaultcurrency) {
1332 $main::lxdebug->leave_sub();
1336 my $dbh = $self->get_standard_dbh($myconfig);
1337 my $query = qq|SELECT e.$fld FROM exchangerate e
1338 WHERE e.curr = ? AND e.transdate = ?|;
1340 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1342 $exchangerate = 1 if ($exchangerate eq "");
1344 $main::lxdebug->leave_sub();
1346 return $exchangerate;
1349 sub get_default_currency {
1350 $main::lxdebug->enter_sub();
1352 my ($self, $myconfig) = @_;
1353 my $dbh = $self->get_standard_dbh($myconfig);
1355 my $query = qq|SELECT curr FROM defaults|;
1357 my ($curr) = selectrow_query($self, $dbh, $query);
1358 my ($defaultcurrency) = split m/:/, $curr;
1360 $main::lxdebug->leave_sub();
1362 return $defaultcurrency;
1366 sub set_payment_options {
1367 $main::lxdebug->enter_sub();
1369 my ($self, $myconfig, $transdate) = @_;
1371 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1373 my $dbh = $self->get_standard_dbh($myconfig);
1376 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1377 qq|FROM payment_terms p | .
1380 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1381 $self->{payment_terms}) =
1382 selectrow_query($self, $dbh, $query, $self->{payment_id});
1384 if ($transdate eq "") {
1385 if ($self->{invdate}) {
1386 $transdate = $self->{invdate};
1388 $transdate = $self->{transdate};
1393 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1394 qq|FROM payment_terms|;
1395 ($self->{netto_date}, $self->{skonto_date}) =
1396 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1398 my ($invtotal, $total);
1399 my (%amounts, %formatted_amounts);
1401 if ($self->{type} =~ /_order$/) {
1402 $amounts{invtotal} = $self->{ordtotal};
1403 $amounts{total} = $self->{ordtotal};
1405 } elsif ($self->{type} =~ /_quotation$/) {
1406 $amounts{invtotal} = $self->{quototal};
1407 $amounts{total} = $self->{quototal};
1410 $amounts{invtotal} = $self->{invtotal};
1411 $amounts{total} = $self->{total};
1414 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1416 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1417 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1418 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1420 foreach (keys %amounts) {
1421 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1422 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1425 if ($self->{"language_id"}) {
1427 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1428 qq|FROM translation_payment_terms t | .
1429 qq|LEFT JOIN language l ON t.language_id = l.id | .
1430 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1431 my ($description_long, $output_numberformat, $output_dateformat,
1432 $output_longdates) =
1433 selectrow_query($self, $dbh, $query,
1434 $self->{"language_id"}, $self->{"payment_id"});
1436 $self->{payment_terms} = $description_long if ($description_long);
1438 if ($output_dateformat) {
1439 foreach my $key (qw(netto_date skonto_date)) {
1441 $main::locale->reformat_date($myconfig, $self->{$key},
1447 if ($output_numberformat &&
1448 ($output_numberformat ne $myconfig->{"numberformat"})) {
1449 my $saved_numberformat = $myconfig->{"numberformat"};
1450 $myconfig->{"numberformat"} = $output_numberformat;
1451 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1452 $myconfig->{"numberformat"} = $saved_numberformat;
1456 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1457 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1458 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1459 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1460 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1461 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1462 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1464 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1466 $main::lxdebug->leave_sub();
1470 sub get_template_language {
1471 $main::lxdebug->enter_sub();
1473 my ($self, $myconfig) = @_;
1475 my $template_code = "";
1477 if ($self->{language_id}) {
1478 my $dbh = $self->get_standard_dbh($myconfig);
1479 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1480 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1483 $main::lxdebug->leave_sub();
1485 return $template_code;
1488 sub get_printer_code {
1489 $main::lxdebug->enter_sub();
1491 my ($self, $myconfig) = @_;
1493 my $template_code = "";
1495 if ($self->{printer_id}) {
1496 my $dbh = $self->get_standard_dbh($myconfig);
1497 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1498 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1501 $main::lxdebug->leave_sub();
1503 return $template_code;
1507 $main::lxdebug->enter_sub();
1509 my ($self, $myconfig) = @_;
1511 my $template_code = "";
1513 if ($self->{shipto_id}) {
1514 my $dbh = $self->get_standard_dbh($myconfig);
1515 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1516 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1517 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1520 $main::lxdebug->leave_sub();
1524 $main::lxdebug->enter_sub();
1526 my ($self, $dbh, $id, $module) = @_;
1531 foreach my $item (qw(name department_1 department_2 street zipcode city country
1532 contact phone fax email)) {
1533 if ($self->{"shipto$item"}) {
1534 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1536 push(@values, $self->{"shipto${item}"});
1540 if ($self->{shipto_id}) {
1541 my $query = qq|UPDATE shipto set
1543 shiptodepartment_1 = ?,
1544 shiptodepartment_2 = ?,
1553 WHERE shipto_id = ?|;
1554 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1556 my $query = qq|SELECT * FROM shipto
1557 WHERE shiptoname = ? AND
1558 shiptodepartment_1 = ? AND
1559 shiptodepartment_2 = ? AND
1560 shiptostreet = ? AND
1561 shiptozipcode = ? AND
1563 shiptocountry = ? AND
1564 shiptocontact = ? AND
1570 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1573 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1574 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1575 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1576 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1577 do_query($self, $dbh, $query, $id, @values, $module);
1582 $main::lxdebug->leave_sub();
1586 $main::lxdebug->enter_sub();
1588 my ($self, $dbh) = @_;
1590 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1591 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1592 $self->{"employee_id"} *= 1;
1594 $main::lxdebug->leave_sub();
1598 $main::lxdebug->enter_sub();
1600 my ($self, $myconfig, $salesman_id) = @_;
1602 $main::lxdebug->leave_sub() and return unless $salesman_id;
1604 my $dbh = $self->get_standard_dbh($myconfig);
1607 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1611 my $user = new User($main::memberfile, $login);
1612 map({ $self->{"salesman_$_"} = $user->{$_}; }
1613 qw(address businessnumber co_ustid company duns email fax name
1615 $self->{salesman_login} = $login;
1617 $self->{salesman_name} = $login
1618 if ($self->{salesman_name} eq "");
1620 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1623 $main::lxdebug->leave_sub();
1627 $main::lxdebug->enter_sub();
1629 my ($self, $myconfig) = @_;
1631 my $dbh = $self->get_standard_dbh($myconfig);
1632 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1633 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1635 $main::lxdebug->leave_sub();
1639 $main::lxdebug->enter_sub();
1641 my ($self, $dbh, $id, $key) = @_;
1643 $key = "all_contacts" unless ($key);
1646 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1647 qq|FROM contacts | .
1648 qq|WHERE cp_cv_id = ? | .
1649 qq|ORDER BY lower(cp_name)|;
1651 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1653 $main::lxdebug->leave_sub();
1657 $main::lxdebug->enter_sub();
1659 my ($self, $dbh, $key) = @_;
1661 my ($all, $old_id, $where, @values);
1663 if (ref($key) eq "HASH") {
1666 $key = "ALL_PROJECTS";
1668 foreach my $p (keys(%{$params})) {
1670 $all = $params->{$p};
1671 } elsif ($p eq "old_id") {
1672 $old_id = $params->{$p};
1673 } elsif ($p eq "key") {
1674 $key = $params->{$p};
1680 $where = "WHERE active ";
1682 if (ref($old_id) eq "ARRAY") {
1683 my @ids = grep({ $_ } @{$old_id});
1685 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1686 push(@values, @ids);
1689 $where .= " OR (id = ?) ";
1690 push(@values, $old_id);
1696 qq|SELECT id, projectnumber, description, active | .
1699 qq|ORDER BY lower(projectnumber)|;
1701 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1703 $main::lxdebug->leave_sub();
1707 $main::lxdebug->enter_sub();
1709 my ($self, $dbh, $vc_id, $key) = @_;
1711 $key = "all_shipto" unless ($key);
1713 # get shipping addresses
1714 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1716 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1718 $main::lxdebug->leave_sub();
1722 $main::lxdebug->enter_sub();
1724 my ($self, $dbh, $key) = @_;
1726 $key = "all_printers" unless ($key);
1728 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1730 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1732 $main::lxdebug->leave_sub();
1736 $main::lxdebug->enter_sub();
1738 my ($self, $dbh, $params) = @_;
1740 $key = $params->{key};
1741 $key = "all_charts" unless ($key);
1743 my $transdate = quote_db_date($params->{transdate});
1746 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1748 qq|LEFT JOIN taxkeys tk ON | .
1749 qq|(tk.id = (SELECT id FROM taxkeys | .
1750 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1751 qq| ORDER BY startdate DESC LIMIT 1)) | .
1752 qq|ORDER BY c.accno|;
1754 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1756 $main::lxdebug->leave_sub();
1759 sub _get_taxcharts {
1760 $main::lxdebug->enter_sub();
1762 my ($self, $dbh, $key) = @_;
1764 $key = "all_taxcharts" unless ($key);
1766 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1768 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1770 $main::lxdebug->leave_sub();
1774 $main::lxdebug->enter_sub();
1776 my ($self, $dbh, $key) = @_;
1778 $key = "all_taxzones" unless ($key);
1780 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1782 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1784 $main::lxdebug->leave_sub();
1787 sub _get_employees {
1788 $main::lxdebug->enter_sub();
1790 my ($self, $dbh, $default_key, $key) = @_;
1792 $key = $default_key unless ($key);
1793 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1795 $main::lxdebug->leave_sub();
1798 sub _get_business_types {
1799 $main::lxdebug->enter_sub();
1801 my ($self, $dbh, $key) = @_;
1803 $key = "all_business_types" unless ($key);
1805 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1807 $main::lxdebug->leave_sub();
1810 sub _get_languages {
1811 $main::lxdebug->enter_sub();
1813 my ($self, $dbh, $key) = @_;
1815 $key = "all_languages" unless ($key);
1817 my $query = qq|SELECT * FROM language ORDER BY id|;
1819 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1821 $main::lxdebug->leave_sub();
1824 sub _get_dunning_configs {
1825 $main::lxdebug->enter_sub();
1827 my ($self, $dbh, $key) = @_;
1829 $key = "all_dunning_configs" unless ($key);
1831 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1833 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1835 $main::lxdebug->leave_sub();
1838 sub _get_currencies {
1839 $main::lxdebug->enter_sub();
1841 my ($self, $dbh, $key) = @_;
1843 $key = "all_currencies" unless ($key);
1845 my $query = qq|SELECT curr AS currency FROM defaults|;
1847 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1849 $main::lxdebug->leave_sub();
1853 $main::lxdebug->enter_sub();
1855 my ($self, $dbh, $key) = @_;
1857 $key = "all_payments" unless ($key);
1859 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1861 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1863 $main::lxdebug->leave_sub();
1866 sub _get_customers {
1867 $main::lxdebug->enter_sub();
1869 my ($self, $dbh, $key, $limit) = @_;
1871 $key = "all_customers" unless ($key);
1872 $limit_clause = "LIMIT $limit" if $limit;
1874 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
1876 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1878 $main::lxdebug->leave_sub();
1882 $main::lxdebug->enter_sub();
1884 my ($self, $dbh, $key) = @_;
1886 $key = "all_vendors" unless ($key);
1888 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1890 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1892 $main::lxdebug->leave_sub();
1895 sub _get_departments {
1896 $main::lxdebug->enter_sub();
1898 my ($self, $dbh, $key) = @_;
1900 $key = "all_departments" unless ($key);
1902 my $query = qq|SELECT * FROM department ORDER BY description|;
1904 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1906 $main::lxdebug->leave_sub();
1909 sub _get_price_factors {
1910 $main::lxdebug->enter_sub();
1912 my ($self, $dbh, $key) = @_;
1914 $key ||= "all_price_factors";
1916 my $query = qq|SELECT * FROM price_factors ORDER BY sortkey|;
1918 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1920 $main::lxdebug->leave_sub();
1924 $main::lxdebug->enter_sub();
1929 my $dbh = $self->get_standard_dbh(\%main::myconfig);
1930 my ($sth, $query, $ref);
1932 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1933 my $vc_id = $self->{"${vc}_id"};
1935 if ($params{"contacts"}) {
1936 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1939 if ($params{"shipto"}) {
1940 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1943 if ($params{"projects"} || $params{"all_projects"}) {
1944 $self->_get_projects($dbh, $params{"all_projects"} ?
1945 $params{"all_projects"} : $params{"projects"},
1946 $params{"all_projects"} ? 1 : 0);
1949 if ($params{"printers"}) {
1950 $self->_get_printers($dbh, $params{"printers"});
1953 if ($params{"languages"}) {
1954 $self->_get_languages($dbh, $params{"languages"});
1957 if ($params{"charts"}) {
1958 $self->_get_charts($dbh, $params{"charts"});
1961 if ($params{"taxcharts"}) {
1962 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1965 if ($params{"taxzones"}) {
1966 $self->_get_taxzones($dbh, $params{"taxzones"});
1969 if ($params{"employees"}) {
1970 $self->_get_employees($dbh, "all_employees", $params{"employees"});
1973 if ($params{"salesmen"}) {
1974 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
1977 if ($params{"business_types"}) {
1978 $self->_get_business_types($dbh, $params{"business_types"});
1981 if ($params{"dunning_configs"}) {
1982 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
1985 if($params{"currencies"}) {
1986 $self->_get_currencies($dbh, $params{"currencies"});
1989 if($params{"customers"}) {
1990 if (ref $params{"customers"} eq 'HASH') {
1991 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
1993 $self->_get_customers($dbh, $params{"customers"});
1997 if($params{"vendors"}) {
1998 if (ref $params{"vendors"} eq 'HASH') {
1999 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2001 $self->_get_vendors($dbh, $params{"vendors"});
2005 if($params{"payments"}) {
2006 $self->_get_payments($dbh, $params{"payments"});
2009 if($params{"departments"}) {
2010 $self->_get_departments($dbh, $params{"departments"});
2013 if ($params{price_factors}) {
2014 $self->_get_price_factors($dbh, $params{price_factors});
2017 $main::lxdebug->leave_sub();
2020 # this sub gets the id and name from $table
2022 $main::lxdebug->enter_sub();
2024 my ($self, $myconfig, $table) = @_;
2026 # connect to database
2027 my $dbh = $self->get_standard_dbh($myconfig);
2029 $table = $table eq "customer" ? "customer" : "vendor";
2030 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2032 my ($query, @values);
2034 if (!$self->{openinvoices}) {
2036 if ($self->{customernumber} ne "") {
2037 $where = qq|(vc.customernumber ILIKE ?)|;
2038 push(@values, '%' . $self->{customernumber} . '%');
2040 $where = qq|(vc.name ILIKE ?)|;
2041 push(@values, '%' . $self->{$table} . '%');
2045 qq~SELECT vc.id, vc.name,
2046 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2048 WHERE $where AND (NOT vc.obsolete)
2052 qq~SELECT DISTINCT vc.id, vc.name,
2053 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2055 JOIN $table vc ON (a.${table}_id = vc.id)
2056 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2058 push(@values, '%' . $self->{$table} . '%');
2061 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2063 $main::lxdebug->leave_sub();
2065 return scalar(@{ $self->{name_list} });
2068 # the selection sub is used in the AR, AP, IS, IR and OE module
2071 $main::lxdebug->enter_sub();
2073 my ($self, $myconfig, $table, $module) = @_;
2076 my $dbh = $self->get_standard_dbh($myconfig);
2078 $table = $table eq "customer" ? "customer" : "vendor";
2080 my $query = qq|SELECT count(*) FROM $table|;
2081 my ($count) = selectrow_query($self, $dbh, $query);
2083 # build selection list
2084 if ($count < $myconfig->{vclimit}) {
2085 $query = qq|SELECT id, name, salesman_id
2086 FROM $table WHERE NOT obsolete
2088 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2092 $self->get_employee($dbh);
2094 # setup sales contacts
2095 $query = qq|SELECT e.id, e.name
2097 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2098 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2101 push(@{ $self->{all_employees} },
2102 { id => $self->{employee_id},
2103 name => $self->{employee} });
2105 # sort the whole thing
2106 @{ $self->{all_employees} } =
2107 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2109 if ($module eq 'AR') {
2111 # prepare query for departments
2112 $query = qq|SELECT id, description
2115 ORDER BY description|;
2118 $query = qq|SELECT id, description
2120 ORDER BY description|;
2123 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2126 $query = qq|SELECT id, description
2130 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2133 $query = qq|SELECT printer_description, id
2135 ORDER BY printer_description|;
2137 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2140 $query = qq|SELECT id, description
2144 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2146 $main::lxdebug->leave_sub();
2149 sub language_payment {
2150 $main::lxdebug->enter_sub();
2152 my ($self, $myconfig) = @_;
2154 my $dbh = $self->get_standard_dbh($myconfig);
2156 my $query = qq|SELECT id, description
2160 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2163 $query = qq|SELECT printer_description, id
2165 ORDER BY printer_description|;
2167 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2170 $query = qq|SELECT id, description
2174 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2176 # get buchungsgruppen
2177 $query = qq|SELECT id, description
2178 FROM buchungsgruppen|;
2180 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2182 $main::lxdebug->leave_sub();
2185 # this is only used for reports
2186 sub all_departments {
2187 $main::lxdebug->enter_sub();
2189 my ($self, $myconfig, $table) = @_;
2191 my $dbh = $self->get_standard_dbh($myconfig);
2194 if ($table eq 'customer') {
2195 $where = "WHERE role = 'P' ";
2198 my $query = qq|SELECT id, description
2201 ORDER BY description|;
2202 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2204 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2206 $main::lxdebug->leave_sub();
2210 $main::lxdebug->enter_sub();
2212 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2215 if ($table eq "customer") {
2224 $self->all_vc($myconfig, $table, $module);
2226 # get last customers or vendors
2227 my ($query, $sth, $ref);
2229 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2234 my $transdate = "current_date";
2235 if ($self->{transdate}) {
2236 $transdate = $dbh->quote($self->{transdate});
2239 # now get the account numbers
2240 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2241 FROM chart c, taxkeys tk
2242 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2243 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2246 $sth = $dbh->prepare($query);
2248 do_statement($self, $sth, $query, '%' . $module . '%');
2250 $self->{accounts} = "";
2251 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2253 foreach my $key (split(/:/, $ref->{link})) {
2254 if ($key =~ /\Q$module\E/) {
2256 # cross reference for keys
2257 $xkeyref{ $ref->{accno} } = $key;
2259 push @{ $self->{"${module}_links"}{$key} },
2260 { accno => $ref->{accno},
2261 description => $ref->{description},
2262 taxkey => $ref->{taxkey_id},
2263 tax_id => $ref->{tax_id} };
2265 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2271 # get taxkeys and description
2272 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2273 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2275 if (($module eq "AP") || ($module eq "AR")) {
2276 # get tax rates and description
2277 $query = qq|SELECT * FROM tax|;
2278 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2284 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2285 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2286 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2287 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2289 d.description AS department,
2292 JOIN $table c ON (a.${table}_id = c.id)
2293 LEFT JOIN employee e ON (e.id = a.employee_id)
2294 LEFT JOIN department d ON (d.id = a.department_id)
2296 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2298 foreach $key (keys %$ref) {
2299 $self->{$key} = $ref->{$key};
2302 my $transdate = "current_date";
2303 if ($self->{transdate}) {
2304 $transdate = $dbh->quote($self->{transdate});
2307 # now get the account numbers
2308 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2310 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2312 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2313 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2316 $sth = $dbh->prepare($query);
2317 do_statement($self, $sth, $query, "%$module%");
2319 $self->{accounts} = "";
2320 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2322 foreach my $key (split(/:/, $ref->{link})) {
2323 if ($key =~ /\Q$module\E/) {
2325 # cross reference for keys
2326 $xkeyref{ $ref->{accno} } = $key;
2328 push @{ $self->{"${module}_links"}{$key} },
2329 { accno => $ref->{accno},
2330 description => $ref->{description},
2331 taxkey => $ref->{taxkey_id},
2332 tax_id => $ref->{tax_id} };
2334 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2340 # get amounts from individual entries
2343 c.accno, c.description,
2344 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2348 LEFT JOIN chart c ON (c.id = a.chart_id)
2349 LEFT JOIN project p ON (p.id = a.project_id)
2350 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2351 WHERE (tk.taxkey_id=a.taxkey) AND
2352 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2353 THEN tk.chart_id = a.chart_id
2356 OR (c.link='%tax%')) AND
2357 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2358 WHERE a.trans_id = ?
2359 AND a.fx_transaction = '0'
2360 ORDER BY a.oid, a.transdate|;
2361 $sth = $dbh->prepare($query);
2362 do_statement($self, $sth, $query, $self->{id});
2364 # get exchangerate for currency
2365 $self->{exchangerate} =
2366 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2369 # store amounts in {acc_trans}{$key} for multiple accounts
2370 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2371 $ref->{exchangerate} =
2372 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2373 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2376 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2377 $ref->{amount} *= -1;
2379 $ref->{index} = $index;
2381 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2387 d.curr AS currencies, d.closedto, d.revtrans,
2388 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2389 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2391 $ref = selectfirst_hashref_query($self, $dbh, $query);
2392 map { $self->{$_} = $ref->{$_} } keys %$ref;
2399 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2400 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2401 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2403 $ref = selectfirst_hashref_query($self, $dbh, $query);
2404 map { $self->{$_} = $ref->{$_} } keys %$ref;
2406 if ($self->{"$self->{vc}_id"}) {
2408 # only setup currency
2409 ($self->{currency}) = split(/:/, $self->{currencies});
2413 $self->lastname_used($dbh, $myconfig, $table, $module);
2415 # get exchangerate for currency
2416 $self->{exchangerate} =
2417 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2423 $main::lxdebug->leave_sub();
2427 $main::lxdebug->enter_sub();
2429 my ($self, $dbh, $myconfig, $table, $module) = @_;
2431 my $arap = ($table eq 'customer') ? "ar" : "ap";
2432 $table = $table eq "customer" ? "customer" : "vendor";
2433 my $where = "1 = 1";
2435 if ($self->{type} =~ /_order/) {
2437 $where = "quotation = '0'";
2439 if ($self->{type} =~ /_quotation/) {
2441 $where = "quotation = '1'";
2444 my $query = qq|SELECT MAX(id) FROM $arap
2445 WHERE $where AND ${table}_id > 0|;
2446 my ($trans_id) = selectrow_query($self, $dbh, $query);
2451 a.curr, a.${table}_id, a.department_id,
2452 d.description AS department,
2453 ct.name, current_date + ct.terms AS duedate
2455 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2456 LEFT JOIN department d ON (a.department_id = d.id)
2458 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2459 $self->{department}, $self->{$table}, $self->{duedate})
2460 = selectrow_query($self, $dbh, $query, $trans_id);
2462 $main::lxdebug->leave_sub();
2466 $main::lxdebug->enter_sub();
2468 my ($self, $myconfig, $thisdate, $days) = @_;
2470 my $dbh = $self->get_standard_dbh($myconfig);
2475 my $dateformat = $myconfig->{dateformat};
2476 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2477 $thisdate = $dbh->quote($thisdate);
2478 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2480 $query = qq|SELECT current_date AS thisdate|;
2483 ($thisdate) = selectrow_query($self, $dbh, $query);
2485 $main::lxdebug->leave_sub();
2491 $main::lxdebug->enter_sub();
2493 my ($self, $string) = @_;
2495 if ($string !~ /%/) {
2496 $string = "%$string%";
2499 $string =~ s/\'/\'\'/g;
2501 $main::lxdebug->leave_sub();
2507 $main::lxdebug->enter_sub();
2509 my ($self, $flds, $new, $count, $numrows) = @_;
2513 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2518 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2520 $j = $item->{ndx} - 1;
2521 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2525 for $i ($count + 1 .. $numrows) {
2526 map { delete $self->{"${_}_$i"} } @{$flds};
2529 $main::lxdebug->leave_sub();
2533 $main::lxdebug->enter_sub();
2535 my ($self, $myconfig) = @_;
2539 my $dbh = $self->dbconnect_noauto($myconfig);
2541 my $query = qq|DELETE FROM status
2542 WHERE (formname = ?) AND (trans_id = ?)|;
2543 my $sth = prepare_query($self, $dbh, $query);
2545 if ($self->{formname} =~ /(check|receipt)/) {
2546 for $i (1 .. $self->{rowcount}) {
2547 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2550 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2554 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2555 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2557 my %queued = split / /, $self->{queued};
2560 if ($self->{formname} =~ /(check|receipt)/) {
2562 # this is a check or receipt, add one entry for each lineitem
2563 my ($accno) = split /--/, $self->{account};
2564 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2565 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2566 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2567 $sth = prepare_query($self, $dbh, $query);
2569 for $i (1 .. $self->{rowcount}) {
2570 if ($self->{"checked_$i"}) {
2571 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2577 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2578 VALUES (?, ?, ?, ?, ?)|;
2579 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2580 $queued{$self->{formname}}, $self->{formname});
2586 $main::lxdebug->leave_sub();
2590 $main::lxdebug->enter_sub();
2592 my ($self, $dbh) = @_;
2594 my ($query, $printed, $emailed);
2596 my $formnames = $self->{printed};
2597 my $emailforms = $self->{emailed};
2599 $query = qq|DELETE FROM status
2600 WHERE (formname = ?) AND (trans_id = ?)|;
2601 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2603 # this only applies to the forms
2604 # checks and receipts are posted when printed or queued
2606 if ($self->{queued}) {
2607 my %queued = split / /, $self->{queued};
2609 foreach my $formname (keys %queued) {
2610 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2611 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2613 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2614 VALUES (?, ?, ?, ?, ?)|;
2615 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2617 $formnames =~ s/\Q$self->{formname}\E//;
2618 $emailforms =~ s/\Q$self->{formname}\E//;
2623 # save printed, emailed info
2624 $formnames =~ s/^ +//g;
2625 $emailforms =~ s/^ +//g;
2628 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2629 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2631 foreach my $formname (keys %status) {
2632 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2633 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2635 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2636 VALUES (?, ?, ?, ?)|;
2637 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2640 $main::lxdebug->leave_sub();
2644 # $main::locale->text('SAVED')
2645 # $main::locale->text('DELETED')
2646 # $main::locale->text('ADDED')
2647 # $main::locale->text('PAYMENT POSTED')
2648 # $main::locale->text('POSTED')
2649 # $main::locale->text('POSTED AS NEW')
2650 # $main::locale->text('ELSE')
2651 # $main::locale->text('SAVED FOR DUNNING')
2652 # $main::locale->text('DUNNING STARTED')
2653 # $main::locale->text('PRINTED')
2654 # $main::locale->text('MAILED')
2655 # $main::locale->text('SCREENED')
2656 # $main::locale->text('CANCELED')
2657 # $main::locale->text('invoice')
2658 # $main::locale->text('proforma')
2659 # $main::locale->text('sales_order')
2660 # $main::locale->text('packing_list')
2661 # $main::locale->text('pick_list')
2662 # $main::locale->text('purchase_order')
2663 # $main::locale->text('bin_list')
2664 # $main::locale->text('sales_quotation')
2665 # $main::locale->text('request_quotation')
2668 $main::lxdebug->enter_sub();
2673 if(!exists $self->{employee_id}) {
2674 &get_employee($self, $dbh);
2678 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2679 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2680 my @values = (conv_i($self->{id}), $self->{login},
2681 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2682 do_query($self, $dbh, $query, @values);
2684 $main::lxdebug->leave_sub();
2688 $main::lxdebug->enter_sub();
2690 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2691 my ($orderBy, $desc) = split(/\-\-/, $order);
2692 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2695 if ($trans_id ne "") {
2697 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 | .
2698 qq|FROM history_erp h | .
2699 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2700 qq|WHERE trans_id = | . $trans_id
2701 . $restriction . qq| |
2704 my $sth = $dbh->prepare($query) || $self->dberror($query);
2706 $sth->execute() || $self->dberror("$query");
2708 while(my $hash_ref = $sth->fetchrow_hashref()) {
2709 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2710 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2711 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2712 $tempArray[$i++] = $hash_ref;
2714 $main::lxdebug->leave_sub() and return \@tempArray
2715 if ($i > 0 && $tempArray[0] ne "");
2717 $main::lxdebug->leave_sub();
2721 sub update_defaults {
2722 $main::lxdebug->enter_sub();
2724 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2727 if ($provided_dbh) {
2728 $dbh = $provided_dbh;
2730 $dbh = $self->dbconnect_noauto($myconfig);
2732 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2733 my $sth = $dbh->prepare($query);
2735 $sth->execute || $self->dberror($query);
2736 my ($var) = $sth->fetchrow_array;
2739 if ($var =~ m/\d+$/) {
2740 my $new_var = (substr $var, $-[0]) * 1 + 1;
2741 my $len_diff = length($var) - $-[0] - length($new_var);
2742 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2748 $query = qq|UPDATE defaults SET $fld = ?|;
2749 do_query($self, $dbh, $query, $var);
2751 if (!$provided_dbh) {
2756 $main::lxdebug->leave_sub();
2761 sub update_business {
2762 $main::lxdebug->enter_sub();
2764 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2767 if ($provided_dbh) {
2768 $dbh = $provided_dbh;
2770 $dbh = $self->dbconnect_noauto($myconfig);
2773 qq|SELECT customernumberinit FROM business
2774 WHERE id = ? FOR UPDATE|;
2775 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2777 if ($var =~ m/\d+$/) {
2778 my $new_var = (substr $var, $-[0]) * 1 + 1;
2779 my $len_diff = length($var) - $-[0] - length($new_var);
2780 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2786 $query = qq|UPDATE business
2787 SET customernumberinit = ?
2789 do_query($self, $dbh, $query, $var, $business_id);
2791 if (!$provided_dbh) {
2796 $main::lxdebug->leave_sub();
2801 sub get_partsgroup {
2802 $main::lxdebug->enter_sub();
2804 my ($self, $myconfig, $p) = @_;
2806 my $dbh = $self->get_standard_dbh($myconfig);
2808 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2810 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2813 if ($p->{searchitems} eq 'part') {
2814 $query .= qq|WHERE p.inventory_accno_id > 0|;
2816 if ($p->{searchitems} eq 'service') {
2817 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2819 if ($p->{searchitems} eq 'assembly') {
2820 $query .= qq|WHERE p.assembly = '1'|;
2822 if ($p->{searchitems} eq 'labor') {
2823 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2826 $query .= qq|ORDER BY partsgroup|;
2829 $query = qq|SELECT id, partsgroup FROM partsgroup
2830 ORDER BY partsgroup|;
2833 if ($p->{language_code}) {
2834 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2835 t.description AS translation
2837 JOIN parts p ON (p.partsgroup_id = pg.id)
2838 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2839 ORDER BY translation|;
2840 @values = ($p->{language_code});
2843 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2845 $main::lxdebug->leave_sub();
2848 sub get_pricegroup {
2849 $main::lxdebug->enter_sub();
2851 my ($self, $myconfig, $p) = @_;
2853 my $dbh = $self->get_standard_dbh($myconfig);
2855 my $query = qq|SELECT p.id, p.pricegroup
2858 $query .= qq| ORDER BY pricegroup|;
2861 $query = qq|SELECT id, pricegroup FROM pricegroup
2862 ORDER BY pricegroup|;
2865 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2867 $main::lxdebug->leave_sub();
2871 # usage $form->all_years($myconfig, [$dbh])
2872 # return list of all years where bookings found
2875 $main::lxdebug->enter_sub();
2877 my ($self, $myconfig, $dbh) = @_;
2879 $dbh ||= $self->get_standard_dbh($myconfig);
2882 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2883 (SELECT MAX(transdate) FROM acc_trans)|;
2884 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2886 if ($myconfig->{dateformat} =~ /^yy/) {
2887 ($startdate) = split /\W/, $startdate;
2888 ($enddate) = split /\W/, $enddate;
2890 (@_) = split /\W/, $startdate;
2892 (@_) = split /\W/, $enddate;
2897 $startdate = substr($startdate,0,4);
2898 $enddate = substr($enddate,0,4);
2900 while ($enddate >= $startdate) {
2901 push @all_years, $enddate--;
2906 $main::lxdebug->leave_sub();