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 #======================================================================
51 use List::Util qw(max min sum);
57 $standard_dbh->disconnect();
63 $main::lxdebug->enter_sub(2);
71 while ($key =~ /\[\+?\]\.|\./) {
72 substr($key, 0, $+[0]) = '';
80 if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
81 push @{ $curr->{$`} }, { };
84 $curr = $curr->{$`}->[-1];
88 $curr->{$key} = $value;
90 $main::lxdebug->leave_sub(2);
92 return \$curr->{$key};
96 $main::lxdebug->enter_sub(2);
101 my @pairs = split(/&/, $input);
104 my ($key, $value) = split(/=/, $_, 2);
105 $self->_store_value($self->unescape($key), $self->unescape($value));
108 $main::lxdebug->leave_sub(2);
111 sub _request_to_hash {
112 $main::lxdebug->enter_sub(2);
117 if (!$ENV{'CONTENT_TYPE'}
118 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
120 $self->_input_to_hash($input);
122 $main::lxdebug->leave_sub(2);
126 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
128 my $boundary = '--' . $1;
130 foreach my $line (split m/\n/, $input) {
131 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
133 if (($line eq $boundary) || ($line eq "$boundary\r")) {
134 ${ $previous } =~ s|\r?\n$|| if $previous;
140 $content_type = "text/plain";
147 next unless $boundary_found;
149 if (!$headers_done) {
150 $line =~ s/[\r\n]*$//;
157 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
158 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
160 substr $line, $-[0], $+[0] - $-[0], "";
163 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
165 substr $line, $-[0], $+[0] - $-[0], "";
168 $previous = $self->_store_value($name, '');
169 $self->{FILENAME} = $filename if ($filename);
174 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
181 next unless $previous;
183 ${ $previous } .= "${line}\n";
186 ${ $previous } =~ s|\r?\n$|| if $previous;
188 $main::lxdebug->leave_sub(2);
192 $main::lxdebug->enter_sub();
198 if ($LXDebug::watch_form) {
199 require SL::Watchdog;
200 tie %{ $self }, 'SL::Watchdog';
203 read(STDIN, $_, $ENV{CONTENT_LENGTH});
205 if ($ENV{QUERY_STRING}) {
206 $_ = $ENV{QUERY_STRING};
215 $self->_request_to_hash($_);
217 $self->{action} = lc $self->{action};
218 $self->{action} =~ s/( |-|,|\#)/_/g;
220 $self->{version} = "2.4.3";
222 $main::lxdebug->leave_sub();
228 $main::lxdebug->enter_sub();
234 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
236 $main::lxdebug->leave_sub();
240 $main::lxdebug->enter_sub(2);
242 my ($self, $str) = @_;
244 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
246 $main::lxdebug->leave_sub(2);
252 $main::lxdebug->enter_sub(2);
254 my ($self, $str) = @_;
259 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
261 $main::lxdebug->leave_sub(2);
267 my ($self, $str) = @_;
269 if ($str && !ref($str)) {
270 $str =~ s/\"/"/g;
278 my ($self, $str) = @_;
280 if ($str && !ref($str)) {
281 $str =~ s/"/\"/g;
289 $main::lxdebug->enter_sub(2);
291 my ($self, $str) = @_;
294 ('order' => ['"', '<', '>'],
300 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
302 $main::lxdebug->leave_sub(2);
311 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
313 for (sort keys %$self) {
314 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
315 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
322 $main::lxdebug->enter_sub();
324 $main::lxdebug->show_backtrace();
326 my ($self, $msg) = @_;
327 if ($ENV{HTTP_USER_AGENT}) {
329 $self->show_generic_error($msg);
336 $main::lxdebug->leave_sub();
340 $main::lxdebug->enter_sub();
342 my ($self, $msg) = @_;
344 if ($ENV{HTTP_USER_AGENT}) {
347 if (!$self->{header}) {
360 if ($self->{info_function}) {
361 &{ $self->{info_function} }($msg);
367 $main::lxdebug->leave_sub();
370 # calculates the number of rows in a textarea based on the content and column number
371 # can be capped with maxrows
373 $main::lxdebug->enter_sub();
374 my ($self, $str, $cols, $maxrows) = @_;
376 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
379 $main::lxdebug->leave_sub();
380 return min $rows, $maxrows;
384 $main::lxdebug->enter_sub();
386 my ($self, $msg) = @_;
388 $self->error("$msg\n" . $DBI::errstr);
390 $main::lxdebug->leave_sub();
394 $main::lxdebug->enter_sub();
396 my ($self, $name, $msg) = @_;
398 if ($self->{$name} =~ /^\s*$/) {
401 $main::lxdebug->leave_sub();
405 $main::lxdebug->enter_sub();
407 my ($self, $extra_code) = @_;
409 if ($self->{header}) {
410 $main::lxdebug->leave_sub();
414 my ($stylesheet, $favicon);
416 if ($ENV{HTTP_USER_AGENT}) {
419 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
420 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
421 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
424 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
426 $stylesheets =~ s|^\s*||;
427 $stylesheets =~ s|\s*$||;
428 foreach my $file (split m/\s+/, $stylesheets) {
430 next if (! -f "css/$file");
432 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
435 $self->{favicon} = "favicon.ico" unless $self->{favicon};
437 if ($self->{favicon} && (-f "$self->{favicon}")) {
439 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
443 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
445 if ($self->{landscape}) {
446 $pagelayout = qq|<style type="text/css">
447 \@page { size:landscape; }
451 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
455 if ($self->{jsscript} == 1) {
458 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
459 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
460 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
461 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
468 ? "$self->{title} - $self->{titlebar}"
471 foreach $item (@ { $self->{AJAX} }) {
472 $ajax .= $item->show_javascript();
474 print qq|Content-Type: text/html; charset=${db_charset};
478 <title>$self->{titlebar}</title>
482 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
486 <script type="text/javascript">
494 <meta name="robots" content="noindex,nofollow" />
495 <script type="text/javascript" src="js/highlight_input.js"></script>
496 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
498 <script type="text/javascript" src="js/tabcontent.js">
500 /***********************************************
501 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
502 * This notice MUST stay intact for legal use
503 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
504 ***********************************************/
515 $main::lxdebug->leave_sub();
518 sub _prepare_html_template {
519 $main::lxdebug->enter_sub();
521 my ($self, $file, $additional_params) = @_;
524 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
525 $language = $main::language;
527 $language = $main::myconfig{"countrycode"};
529 $language = "de" unless ($language);
531 if (-f "templates/webpages/${file}_${language}.html") {
532 if ((-f ".developer") &&
533 (-f "templates/webpages/${file}_master.html") &&
534 ((stat("templates/webpages/${file}_master.html"))[9] >
535 (stat("templates/webpages/${file}_${language}.html"))[9])) {
536 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
537 "Please re-run 'locales.pl' in 'locale/${language}'.";
538 print(qq|<pre>$info</pre>|);
542 $file = "templates/webpages/${file}_${language}.html";
543 } elsif (-f "templates/webpages/${file}.html") {
544 $file = "templates/webpages/${file}.html";
546 my $info = "Web page template '${file}' not found.\n" .
547 "Please re-run 'locales.pl' in 'locale/${language}'.";
548 print(qq|<pre>$info</pre>|);
552 if ($self->{"DEBUG"}) {
553 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
556 if ($additional_params->{"DEBUG"}) {
557 $additional_params->{"DEBUG"} =
558 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
561 if (%main::myconfig) {
562 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
563 my $jsc_dateformat = $main::myconfig{"dateformat"};
564 $jsc_dateformat =~ s/d+/\%d/gi;
565 $jsc_dateformat =~ s/m+/\%m/gi;
566 $jsc_dateformat =~ s/y+/\%Y/gi;
567 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
570 $additional_params->{"conf_webdav"} = $main::webdav;
571 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
572 $additional_params->{"conf_latex_templates"} = $main::latex;
573 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
575 if (%main::debug_options) {
576 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
579 $main::lxdebug->leave_sub();
584 sub parse_html_template {
585 $main::lxdebug->enter_sub();
587 my ($self, $file, $additional_params) = @_;
589 $additional_params ||= { };
591 $file = $self->_prepare_html_template($file, $additional_params);
593 my $template = Template->new({ 'INTERPOLATE' => 0,
597 'PLUGIN_BASE' => 'SL::Template::Plugin',
598 'INCLUDE_PATH' => '.:templates/webpages',
601 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
604 if (!$template->process($file, $additional_params, \$output)) {
605 print STDERR $template->error();
608 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
610 $main::lxdebug->leave_sub();
615 sub show_generic_error {
616 my ($self, $error, $title, $action) = @_;
619 'title_error' => $title,
620 'label_error' => $error,
625 map({ delete($self->{$_}); } qw(action));
626 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
627 if (!ref($self->{$_})); }
629 $add_params->{"SHOW_BUTTON"} = 1;
630 $add_params->{"BUTTON_LABEL"} = $action;
632 $add_params->{"VARIABLES"} = \@vars;
634 $self->{title} = $title if ($title);
637 print $self->parse_html_template("generic/error", $add_params);
639 die("Error: $error\n");
642 sub show_generic_information {
643 my ($self, $text, $title) = @_;
646 'title_information' => $title,
647 'label_information' => $text,
650 $self->{title} = $title if ($title);
653 print $self->parse_html_template("generic/information", $add_params);
655 die("Information: $error\n");
658 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
659 # changed it to accept an arbitrary number of triggers - sschoeling
661 $main::lxdebug->enter_sub();
664 my $myconfig = shift;
667 # set dateform for jsscript
670 "dd.mm.yy" => "%d.%m.%Y",
671 "dd-mm-yy" => "%d-%m-%Y",
672 "dd/mm/yy" => "%d/%m/%Y",
673 "mm/dd/yy" => "%m/%d/%Y",
674 "mm-dd-yy" => "%m-%d-%Y",
675 "yyyy-mm-dd" => "%Y-%m-%d",
678 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
679 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
686 inputField : "| . (shift) . qq|",
687 ifFormat :"$ifFormat",
688 align : "| . (shift) . qq|",
689 button : "| . (shift) . qq|"
695 <script type="text/javascript">
696 <!--| . join("", @triggers) . qq|//-->
700 $main::lxdebug->leave_sub();
703 } #end sub write_trigger
706 $main::lxdebug->enter_sub();
708 my ($self, $msg) = @_;
710 if ($self->{callback}) {
712 ($script, $argv) = split(/\?/, $self->{callback}, 2);
714 $script =~ s|[^a-zA-Z0-9_\.]||g;
715 exec("perl", "$script", $argv);
723 $main::lxdebug->leave_sub();
726 # sort of columns removed - empty sub
728 $main::lxdebug->enter_sub();
730 my ($self, @columns) = @_;
732 $main::lxdebug->leave_sub();
738 $main::lxdebug->enter_sub(2);
740 my ($self, $myconfig, $amount, $places, $dash) = @_;
746 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
748 my $neg = ($amount =~ s/^-//);
749 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
751 if (defined($places) && ($places ne '')) {
757 my ($actual_places) = ($amount =~ /\.(\d+)/);
758 $actual_places = length($actual_places);
759 $places = $actual_places > $places ? $actual_places : $places;
762 $amount = $self->round_amount($amount, $places);
765 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
766 my @p = split(/\./, $amount); # split amount at decimal point
768 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
771 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
774 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
775 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
776 ($neg ? "-$amount" : "$amount" ) ;
780 $main::lxdebug->leave_sub(2);
786 $main::lxdebug->enter_sub(2);
791 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
792 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
793 $input =~ s/\#\#/\#/g;
795 $main::lxdebug->leave_sub(2);
801 $main::lxdebug->enter_sub(2);
803 my ($self, $myconfig, $amount) = @_;
805 if ( ($myconfig->{numberformat} eq '1.000,00')
806 || ($myconfig->{numberformat} eq '1000,00')) {
811 if ($myconfig->{numberformat} eq "1'000.00") {
817 $main::lxdebug->leave_sub(2);
819 return ($amount * 1);
823 $main::lxdebug->enter_sub(2);
825 my ($self, $amount, $places) = @_;
828 # Rounding like "Kaufmannsrunden"
829 # Descr. http://de.wikipedia.org/wiki/Rundung
831 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
834 $amount = $amount * (10**($places));
835 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
837 $main::lxdebug->leave_sub(2);
839 return $round_amount;
844 $main::lxdebug->enter_sub();
846 my ($self, $myconfig, $userspath) = @_;
847 my ($template, $out);
851 $self->{"cwd"} = getcwd();
852 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
854 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
855 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
856 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
857 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
858 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
859 } elsif (($self->{"format"} =~ /html/i) ||
860 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
861 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
862 } elsif (($self->{"format"} =~ /xml/i) ||
863 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
864 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
865 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
866 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
867 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
868 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
869 } elsif ( defined $self->{'format'}) {
870 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
871 } elsif ( $self->{'format'} eq '' ) {
872 $self->error("No Outputformat given: $self->{'format'}");
873 } else { #Catch the rest
874 $self->error("Outputformat not defined: $self->{'format'}");
877 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
878 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
880 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
881 qw(email tel fax name signature company address businessnumber
882 co_ustid taxnumber duns));
883 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
884 qw(company address signature));
885 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
887 map({ $self->{"${_}"} = $myconfig->{$_}; }
891 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
893 # OUT is used for the media, screen, printer, email
894 # for postscript we store a copy in a temporary file
896 my $prepend_userspath;
898 if (!$self->{tmpfile}) {
899 $self->{tmpfile} = "${fileid}.$self->{IN}";
900 $prepend_userspath = 1;
903 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
905 $self->{tmpfile} =~ s|.*/||;
906 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
907 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
909 if ($template->uses_temp_file() || $self->{media} eq 'email') {
911 $self->{OUT} = ">$self->{tmpfile}";
915 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
917 open(OUT, ">-") or $self->error("STDOUT : $!");
921 if (!$template->parse(*OUT)) {
923 $self->error("$self->{IN} : " . $template->get_error());
928 if ($template->uses_temp_file() || $self->{media} eq 'email') {
930 if ($self->{media} eq 'email') {
932 my $mail = new Mailer;
934 map { $mail->{$_} = $self->{$_} }
935 qw(cc bcc subject message version format);
936 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
937 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
938 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
939 $mail->{fileid} = "$fileid.";
940 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
942 # if we send html or plain text inline
943 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
944 $mail->{contenttype} = "text/html";
946 $mail->{message} =~ s/\r\n/<br>\n/g;
947 $myconfig->{signature} =~ s/\\n/<br>\n/g;
948 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
950 open(IN, $self->{tmpfile})
951 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
953 $mail->{message} .= $_;
960 if (!$self->{"do_not_attach"}) {
961 @{ $mail->{attachments} } =
962 ({ "filename" => $self->{"tmpfile"},
963 "name" => $self->{"attachment_filename"} ?
964 $self->{"attachment_filename"} : $self->{"tmpfile"} });
967 $mail->{message} =~ s/\r\n/\n/g;
968 $myconfig->{signature} =~ s/\\n/\n/g;
969 $mail->{message} .= "\n-- \n$myconfig->{signature}";
973 my $err = $mail->send();
974 $self->error($self->cleanup . "$err") if ($err);
980 my $numbytes = (-s $self->{tmpfile});
981 open(IN, $self->{tmpfile})
982 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
984 $self->{copies} = 1 unless $self->{media} eq 'printer';
986 chdir("$self->{cwd}");
987 #print(STDERR "Kopien $self->{copies}\n");
988 #print(STDERR "OUT $self->{OUT}\n");
989 for my $i (1 .. $self->{copies}) {
991 open(OUT, $self->{OUT})
992 or $self->error($self->cleanup . "$self->{OUT} : $!");
994 $self->{attachment_filename} = ($self->{attachment_filename})
995 ? $self->{attachment_filename}
996 : $self->generate_attachment_filename();
999 print qq|Content-Type: | . $template->get_mime_type() . qq|
1000 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1001 Content-Length: $numbytes
1005 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1025 chdir("$self->{cwd}");
1026 $main::lxdebug->leave_sub();
1029 sub get_formname_translation {
1030 my ($self, $formname) = @_;
1032 $formname ||= $self->{formname};
1034 my %formname_translations = (
1035 bin_list => $main::locale->text('Bin List'),
1036 credit_note => $main::locale->text('Credit Note'),
1037 invoice => $main::locale->text('Invoice'),
1038 packing_list => $main::locale->text('Packing List'),
1039 pick_list => $main::locale->text('Pick List'),
1040 proforma => $main::locale->text('Proforma Invoice'),
1041 purchase_order => $main::locale->text('Purchase Order'),
1042 request_quotation => $main::locale->text('RFQ'),
1043 sales_order => $main::locale->text('Confirmation'),
1044 sales_quotation => $main::locale->text('Quotation'),
1045 storno_invoice => $main::locale->text('Storno Invoice'),
1046 storno_packing_list => $main::locale->text('Storno Packing List'),
1049 return $formname_translations{$formname}
1052 sub generate_attachment_filename {
1055 my $attachment_filename = $self->get_formname_translation();
1057 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1058 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1061 if ($attachment_filename && $self->{"${prefix}number"}) {
1062 $attachment_filename .= "_" . $self->{"${prefix}number"}
1063 . ( $self->{format} =~ /pdf/i ? ".pdf"
1064 : $self->{format} =~ /postscript/i ? ".ps"
1065 : $self->{format} =~ /opendocument/i ? ".odt"
1066 : $self->{format} =~ /html/i ? ".html"
1068 $attachment_filename =~ s/ /_/g;
1069 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1070 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1071 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1073 $attachment_filename = "";
1076 return $attachment_filename;
1080 $main::lxdebug->enter_sub();
1084 chdir("$self->{tmpdir}");
1087 if (-f "$self->{tmpfile}.err") {
1088 open(FH, "$self->{tmpfile}.err");
1093 if ($self->{tmpfile}) {
1094 $self->{tmpfile} =~ s|.*/||g;
1096 $self->{tmpfile} =~ s/\.\w+$//g;
1097 my $tmpfile = $self->{tmpfile};
1098 unlink(<$tmpfile.*>);
1101 chdir("$self->{cwd}");
1103 $main::lxdebug->leave_sub();
1109 $main::lxdebug->enter_sub();
1111 my ($self, $date, $myconfig) = @_;
1113 if ($date && $date =~ /\D/) {
1115 if ($myconfig->{dateformat} =~ /^yy/) {
1116 ($yy, $mm, $dd) = split /\D/, $date;
1118 if ($myconfig->{dateformat} =~ /^mm/) {
1119 ($mm, $dd, $yy) = split /\D/, $date;
1121 if ($myconfig->{dateformat} =~ /^dd/) {
1122 ($dd, $mm, $yy) = split /\D/, $date;
1127 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1128 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1130 $dd = "0$dd" if ($dd < 10);
1131 $mm = "0$mm" if ($mm < 10);
1133 $date = "$yy$mm$dd";
1136 $main::lxdebug->leave_sub();
1141 # Database routines used throughout
1144 $main::lxdebug->enter_sub(2);
1146 my ($self, $myconfig) = @_;
1148 # connect to database
1150 DBI->connect($myconfig->{dbconnect},
1151 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1155 if ($myconfig->{dboptions}) {
1156 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1159 $main::lxdebug->leave_sub(2);
1164 sub dbconnect_noauto {
1165 $main::lxdebug->enter_sub();
1167 my ($self, $myconfig) = @_;
1169 # connect to database
1171 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1172 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1176 if ($myconfig->{dboptions}) {
1177 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1180 $main::lxdebug->leave_sub();
1185 sub get_standard_dbh {
1186 $main::lxdebug->enter_sub(2);
1188 my ($self, $myconfig) = @_;
1190 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1192 $main::lxdebug->leave_sub(2);
1194 return $standard_dbh;
1197 sub update_balance {
1198 $main::lxdebug->enter_sub();
1200 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1202 # if we have a value, go do it
1205 # retrieve balance from table
1206 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1207 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1208 my ($balance) = $sth->fetchrow_array;
1214 $query = "UPDATE $table SET $field = $balance WHERE $where";
1215 do_query($self, $dbh, $query, @values);
1217 $main::lxdebug->leave_sub();
1220 sub update_exchangerate {
1221 $main::lxdebug->enter_sub();
1223 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1225 # some sanity check for currency
1227 $main::lxdebug->leave_sub();
1230 $query = qq|SELECT curr FROM defaults|;
1232 my ($currency) = selectrow_query($self, $dbh, $query);
1233 my ($defaultcurrency) = split m/:/, $currency;
1236 if ($curr eq $defaultcurrency) {
1237 $main::lxdebug->leave_sub();
1241 $query = qq|SELECT e.curr FROM exchangerate e
1242 WHERE e.curr = ? AND e.transdate = ?
1244 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1253 $buy = conv_i($buy, "NULL");
1254 $sell = conv_i($sell, "NULL");
1257 if ($buy != 0 && $sell != 0) {
1258 $set = "buy = $buy, sell = $sell";
1259 } elsif ($buy != 0) {
1260 $set = "buy = $buy";
1261 } elsif ($sell != 0) {
1262 $set = "sell = $sell";
1265 if ($sth->fetchrow_array) {
1266 $query = qq|UPDATE exchangerate
1272 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1273 VALUES (?, $buy, $sell, ?)|;
1276 do_query($self, $dbh, $query, $curr, $transdate);
1278 $main::lxdebug->leave_sub();
1281 sub save_exchangerate {
1282 $main::lxdebug->enter_sub();
1284 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1286 my $dbh = $self->dbconnect($myconfig);
1290 $buy = $rate if $fld eq 'buy';
1291 $sell = $rate if $fld eq 'sell';
1294 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1299 $main::lxdebug->leave_sub();
1302 sub get_exchangerate {
1303 $main::lxdebug->enter_sub();
1305 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1308 unless ($transdate) {
1309 $main::lxdebug->leave_sub();
1313 $query = qq|SELECT curr FROM defaults|;
1315 my ($currency) = selectrow_query($self, $dbh, $query);
1316 my ($defaultcurrency) = split m/:/, $currency;
1318 if ($currency eq $defaultcurrency) {
1319 $main::lxdebug->leave_sub();
1323 $query = qq|SELECT e.$fld FROM exchangerate e
1324 WHERE e.curr = ? AND e.transdate = ?|;
1325 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1329 $main::lxdebug->leave_sub();
1331 return $exchangerate;
1334 sub check_exchangerate {
1335 $main::lxdebug->enter_sub();
1337 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1339 unless ($transdate) {
1340 $main::lxdebug->leave_sub();
1344 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1346 if ($currency eq $defaultcurrency) {
1347 $main::lxdebug->leave_sub();
1351 my $dbh = $self->get_standard_dbh($myconfig);
1352 my $query = qq|SELECT e.$fld FROM exchangerate e
1353 WHERE e.curr = ? AND e.transdate = ?|;
1355 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1357 $exchangerate = 1 if ($exchangerate eq "");
1359 $main::lxdebug->leave_sub();
1361 return $exchangerate;
1364 sub get_default_currency {
1365 $main::lxdebug->enter_sub();
1367 my ($self, $myconfig) = @_;
1368 my $dbh = $self->get_standard_dbh($myconfig);
1370 my $query = qq|SELECT curr FROM defaults|;
1372 my ($curr) = selectrow_query($self, $dbh, $query);
1373 my ($defaultcurrency) = split m/:/, $curr;
1375 $main::lxdebug->leave_sub();
1377 return $defaultcurrency;
1381 sub set_payment_options {
1382 $main::lxdebug->enter_sub();
1384 my ($self, $myconfig, $transdate) = @_;
1386 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1388 my $dbh = $self->get_standard_dbh($myconfig);
1391 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1392 qq|FROM payment_terms p | .
1395 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1396 $self->{payment_terms}) =
1397 selectrow_query($self, $dbh, $query, $self->{payment_id});
1399 if ($transdate eq "") {
1400 if ($self->{invdate}) {
1401 $transdate = $self->{invdate};
1403 $transdate = $self->{transdate};
1408 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1409 qq|FROM payment_terms|;
1410 ($self->{netto_date}, $self->{skonto_date}) =
1411 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1413 my ($invtotal, $total);
1414 my (%amounts, %formatted_amounts);
1416 if ($self->{type} =~ /_order$/) {
1417 $amounts{invtotal} = $self->{ordtotal};
1418 $amounts{total} = $self->{ordtotal};
1420 } elsif ($self->{type} =~ /_quotation$/) {
1421 $amounts{invtotal} = $self->{quototal};
1422 $amounts{total} = $self->{quototal};
1425 $amounts{invtotal} = $self->{invtotal};
1426 $amounts{total} = $self->{total};
1429 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1431 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1432 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1433 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1435 foreach (keys %amounts) {
1436 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1437 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1440 if ($self->{"language_id"}) {
1442 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1443 qq|FROM translation_payment_terms t | .
1444 qq|LEFT JOIN language l ON t.language_id = l.id | .
1445 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1446 my ($description_long, $output_numberformat, $output_dateformat,
1447 $output_longdates) =
1448 selectrow_query($self, $dbh, $query,
1449 $self->{"language_id"}, $self->{"payment_id"});
1451 $self->{payment_terms} = $description_long if ($description_long);
1453 if ($output_dateformat) {
1454 foreach my $key (qw(netto_date skonto_date)) {
1456 $main::locale->reformat_date($myconfig, $self->{$key},
1462 if ($output_numberformat &&
1463 ($output_numberformat ne $myconfig->{"numberformat"})) {
1464 my $saved_numberformat = $myconfig->{"numberformat"};
1465 $myconfig->{"numberformat"} = $output_numberformat;
1466 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1467 $myconfig->{"numberformat"} = $saved_numberformat;
1471 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1472 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1473 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1474 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1475 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1476 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1477 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1479 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1481 $main::lxdebug->leave_sub();
1485 sub get_template_language {
1486 $main::lxdebug->enter_sub();
1488 my ($self, $myconfig) = @_;
1490 my $template_code = "";
1492 if ($self->{language_id}) {
1493 my $dbh = $self->get_standard_dbh($myconfig);
1494 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1495 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1498 $main::lxdebug->leave_sub();
1500 return $template_code;
1503 sub get_printer_code {
1504 $main::lxdebug->enter_sub();
1506 my ($self, $myconfig) = @_;
1508 my $template_code = "";
1510 if ($self->{printer_id}) {
1511 my $dbh = $self->get_standard_dbh($myconfig);
1512 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1513 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1516 $main::lxdebug->leave_sub();
1518 return $template_code;
1522 $main::lxdebug->enter_sub();
1524 my ($self, $myconfig) = @_;
1526 my $template_code = "";
1528 if ($self->{shipto_id}) {
1529 my $dbh = $self->get_standard_dbh($myconfig);
1530 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1531 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1532 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1535 $main::lxdebug->leave_sub();
1539 $main::lxdebug->enter_sub();
1541 my ($self, $dbh, $id, $module) = @_;
1546 foreach my $item (qw(name department_1 department_2 street zipcode city country
1547 contact phone fax email)) {
1548 if ($self->{"shipto$item"}) {
1549 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1551 push(@values, $self->{"shipto${item}"});
1555 if ($self->{shipto_id}) {
1556 my $query = qq|UPDATE shipto set
1558 shiptodepartment_1 = ?,
1559 shiptodepartment_2 = ?,
1568 WHERE shipto_id = ?|;
1569 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1571 my $query = qq|SELECT * FROM shipto
1572 WHERE shiptoname = ? AND
1573 shiptodepartment_1 = ? AND
1574 shiptodepartment_2 = ? AND
1575 shiptostreet = ? AND
1576 shiptozipcode = ? AND
1578 shiptocountry = ? AND
1579 shiptocontact = ? AND
1585 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1588 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1589 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1590 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1591 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1592 do_query($self, $dbh, $query, $id, @values, $module);
1597 $main::lxdebug->leave_sub();
1601 $main::lxdebug->enter_sub();
1603 my ($self, $dbh) = @_;
1605 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1606 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1607 $self->{"employee_id"} *= 1;
1609 $main::lxdebug->leave_sub();
1613 $main::lxdebug->enter_sub();
1615 my ($self, $myconfig, $salesman_id) = @_;
1617 $main::lxdebug->leave_sub() and return unless $salesman_id;
1619 my $dbh = $self->get_standard_dbh($myconfig);
1622 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1626 my $user = new User($main::memberfile, $login);
1627 map({ $self->{"salesman_$_"} = $user->{$_}; }
1628 qw(address businessnumber co_ustid company duns email fax name
1630 $self->{salesman_login} = $login;
1632 $self->{salesman_name} = $login
1633 if ($self->{salesman_name} eq "");
1635 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1638 $main::lxdebug->leave_sub();
1642 $main::lxdebug->enter_sub();
1644 my ($self, $myconfig) = @_;
1646 my $dbh = $self->get_standard_dbh($myconfig);
1647 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1648 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1650 $main::lxdebug->leave_sub();
1654 $main::lxdebug->enter_sub();
1656 my ($self, $dbh, $id, $key) = @_;
1658 $key = "all_contacts" unless ($key);
1661 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1662 qq|FROM contacts | .
1663 qq|WHERE cp_cv_id = ? | .
1664 qq|ORDER BY lower(cp_name)|;
1666 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1668 $main::lxdebug->leave_sub();
1672 $main::lxdebug->enter_sub();
1674 my ($self, $dbh, $key) = @_;
1676 my ($all, $old_id, $where, @values);
1678 if (ref($key) eq "HASH") {
1681 $key = "ALL_PROJECTS";
1683 foreach my $p (keys(%{$params})) {
1685 $all = $params->{$p};
1686 } elsif ($p eq "old_id") {
1687 $old_id = $params->{$p};
1688 } elsif ($p eq "key") {
1689 $key = $params->{$p};
1695 $where = "WHERE active ";
1697 if (ref($old_id) eq "ARRAY") {
1698 my @ids = grep({ $_ } @{$old_id});
1700 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1701 push(@values, @ids);
1704 $where .= " OR (id = ?) ";
1705 push(@values, $old_id);
1711 qq|SELECT id, projectnumber, description, active | .
1714 qq|ORDER BY lower(projectnumber)|;
1716 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1718 $main::lxdebug->leave_sub();
1722 $main::lxdebug->enter_sub();
1724 my ($self, $dbh, $vc_id, $key) = @_;
1726 $key = "all_shipto" unless ($key);
1728 # get shipping addresses
1729 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1731 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1733 $main::lxdebug->leave_sub();
1737 $main::lxdebug->enter_sub();
1739 my ($self, $dbh, $key) = @_;
1741 $key = "all_printers" unless ($key);
1743 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1745 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1747 $main::lxdebug->leave_sub();
1751 $main::lxdebug->enter_sub();
1753 my ($self, $dbh, $params) = @_;
1755 $key = $params->{key};
1756 $key = "all_charts" unless ($key);
1758 my $transdate = quote_db_date($params->{transdate});
1761 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1763 qq|LEFT JOIN taxkeys tk ON | .
1764 qq|(tk.id = (SELECT id FROM taxkeys | .
1765 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1766 qq| ORDER BY startdate DESC LIMIT 1)) | .
1767 qq|ORDER BY c.accno|;
1769 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1771 $main::lxdebug->leave_sub();
1774 sub _get_taxcharts {
1775 $main::lxdebug->enter_sub();
1777 my ($self, $dbh, $key) = @_;
1779 $key = "all_taxcharts" unless ($key);
1781 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1783 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1785 $main::lxdebug->leave_sub();
1789 $main::lxdebug->enter_sub();
1791 my ($self, $dbh, $key) = @_;
1793 $key = "all_taxzones" unless ($key);
1795 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1797 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1799 $main::lxdebug->leave_sub();
1802 sub _get_employees {
1803 $main::lxdebug->enter_sub();
1805 my ($self, $dbh, $default_key, $key) = @_;
1807 $key = $default_key unless ($key);
1808 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1810 $main::lxdebug->leave_sub();
1813 sub _get_business_types {
1814 $main::lxdebug->enter_sub();
1816 my ($self, $dbh, $key) = @_;
1818 $key = "all_business_types" unless ($key);
1820 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1822 $main::lxdebug->leave_sub();
1825 sub _get_languages {
1826 $main::lxdebug->enter_sub();
1828 my ($self, $dbh, $key) = @_;
1830 $key = "all_languages" unless ($key);
1832 my $query = qq|SELECT * FROM language ORDER BY id|;
1834 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1836 $main::lxdebug->leave_sub();
1839 sub _get_dunning_configs {
1840 $main::lxdebug->enter_sub();
1842 my ($self, $dbh, $key) = @_;
1844 $key = "all_dunning_configs" unless ($key);
1846 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1848 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1850 $main::lxdebug->leave_sub();
1853 sub _get_currencies {
1854 $main::lxdebug->enter_sub();
1856 my ($self, $dbh, $key) = @_;
1858 $key = "all_currencies" unless ($key);
1860 my $query = qq|SELECT curr AS currency FROM defaults|;
1862 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1864 $main::lxdebug->leave_sub();
1868 $main::lxdebug->enter_sub();
1870 my ($self, $dbh, $key) = @_;
1872 $key = "all_payments" unless ($key);
1874 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1876 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1878 $main::lxdebug->leave_sub();
1881 sub _get_customers {
1882 $main::lxdebug->enter_sub();
1884 my ($self, $dbh, $key, $limit) = @_;
1886 $key = "all_customers" unless ($key);
1887 $limit_clause = "LIMIT $limit" if $limit;
1889 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
1891 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1893 $main::lxdebug->leave_sub();
1897 $main::lxdebug->enter_sub();
1899 my ($self, $dbh, $key) = @_;
1901 $key = "all_vendors" unless ($key);
1903 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1905 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1907 $main::lxdebug->leave_sub();
1910 sub _get_departments {
1911 $main::lxdebug->enter_sub();
1913 my ($self, $dbh, $key) = @_;
1915 $key = "all_departments" unless ($key);
1917 my $query = qq|SELECT * FROM department ORDER BY description|;
1919 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1921 $main::lxdebug->leave_sub();
1924 sub _get_price_factors {
1925 $main::lxdebug->enter_sub();
1927 my ($self, $dbh, $key) = @_;
1929 $key ||= "all_price_factors";
1931 my $query = qq|SELECT * FROM price_factors ORDER BY sortkey|;
1933 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1935 $main::lxdebug->leave_sub();
1939 $main::lxdebug->enter_sub();
1944 my $dbh = $self->get_standard_dbh(\%main::myconfig);
1945 my ($sth, $query, $ref);
1947 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
1948 my $vc_id = $self->{"${vc}_id"};
1950 if ($params{"contacts"}) {
1951 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
1954 if ($params{"shipto"}) {
1955 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
1958 if ($params{"projects"} || $params{"all_projects"}) {
1959 $self->_get_projects($dbh, $params{"all_projects"} ?
1960 $params{"all_projects"} : $params{"projects"},
1961 $params{"all_projects"} ? 1 : 0);
1964 if ($params{"printers"}) {
1965 $self->_get_printers($dbh, $params{"printers"});
1968 if ($params{"languages"}) {
1969 $self->_get_languages($dbh, $params{"languages"});
1972 if ($params{"charts"}) {
1973 $self->_get_charts($dbh, $params{"charts"});
1976 if ($params{"taxcharts"}) {
1977 $self->_get_taxcharts($dbh, $params{"taxcharts"});
1980 if ($params{"taxzones"}) {
1981 $self->_get_taxzones($dbh, $params{"taxzones"});
1984 if ($params{"employees"}) {
1985 $self->_get_employees($dbh, "all_employees", $params{"employees"});
1988 if ($params{"salesmen"}) {
1989 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
1992 if ($params{"business_types"}) {
1993 $self->_get_business_types($dbh, $params{"business_types"});
1996 if ($params{"dunning_configs"}) {
1997 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2000 if($params{"currencies"}) {
2001 $self->_get_currencies($dbh, $params{"currencies"});
2004 if($params{"customers"}) {
2005 if (ref $params{"customers"} eq 'HASH') {
2006 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2008 $self->_get_customers($dbh, $params{"customers"});
2012 if($params{"vendors"}) {
2013 if (ref $params{"vendors"} eq 'HASH') {
2014 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2016 $self->_get_vendors($dbh, $params{"vendors"});
2020 if($params{"payments"}) {
2021 $self->_get_payments($dbh, $params{"payments"});
2024 if($params{"departments"}) {
2025 $self->_get_departments($dbh, $params{"departments"});
2028 if ($params{price_factors}) {
2029 $self->_get_price_factors($dbh, $params{price_factors});
2032 $main::lxdebug->leave_sub();
2035 # this sub gets the id and name from $table
2037 $main::lxdebug->enter_sub();
2039 my ($self, $myconfig, $table) = @_;
2041 # connect to database
2042 my $dbh = $self->get_standard_dbh($myconfig);
2044 $table = $table eq "customer" ? "customer" : "vendor";
2045 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2047 my ($query, @values);
2049 if (!$self->{openinvoices}) {
2051 if ($self->{customernumber} ne "") {
2052 $where = qq|(vc.customernumber ILIKE ?)|;
2053 push(@values, '%' . $self->{customernumber} . '%');
2055 $where = qq|(vc.name ILIKE ?)|;
2056 push(@values, '%' . $self->{$table} . '%');
2060 qq~SELECT vc.id, vc.name,
2061 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2063 WHERE $where AND (NOT vc.obsolete)
2067 qq~SELECT DISTINCT vc.id, vc.name,
2068 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2070 JOIN $table vc ON (a.${table}_id = vc.id)
2071 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2073 push(@values, '%' . $self->{$table} . '%');
2076 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2078 $main::lxdebug->leave_sub();
2080 return scalar(@{ $self->{name_list} });
2083 # the selection sub is used in the AR, AP, IS, IR and OE module
2086 $main::lxdebug->enter_sub();
2088 my ($self, $myconfig, $table, $module) = @_;
2091 my $dbh = $self->get_standard_dbh($myconfig);
2093 $table = $table eq "customer" ? "customer" : "vendor";
2095 my $query = qq|SELECT count(*) FROM $table|;
2096 my ($count) = selectrow_query($self, $dbh, $query);
2098 # build selection list
2099 if ($count < $myconfig->{vclimit}) {
2100 $query = qq|SELECT id, name, salesman_id
2101 FROM $table WHERE NOT obsolete
2103 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2107 $self->get_employee($dbh);
2109 # setup sales contacts
2110 $query = qq|SELECT e.id, e.name
2112 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2113 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2116 push(@{ $self->{all_employees} },
2117 { id => $self->{employee_id},
2118 name => $self->{employee} });
2120 # sort the whole thing
2121 @{ $self->{all_employees} } =
2122 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2124 if ($module eq 'AR') {
2126 # prepare query for departments
2127 $query = qq|SELECT id, description
2130 ORDER BY description|;
2133 $query = qq|SELECT id, description
2135 ORDER BY description|;
2138 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2141 $query = qq|SELECT id, description
2145 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2148 $query = qq|SELECT printer_description, id
2150 ORDER BY printer_description|;
2152 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2155 $query = qq|SELECT id, description
2159 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2161 $main::lxdebug->leave_sub();
2164 sub language_payment {
2165 $main::lxdebug->enter_sub();
2167 my ($self, $myconfig) = @_;
2169 my $dbh = $self->get_standard_dbh($myconfig);
2171 my $query = qq|SELECT id, description
2175 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2178 $query = qq|SELECT printer_description, id
2180 ORDER BY printer_description|;
2182 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2185 $query = qq|SELECT id, description
2189 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2191 # get buchungsgruppen
2192 $query = qq|SELECT id, description
2193 FROM buchungsgruppen|;
2195 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2197 $main::lxdebug->leave_sub();
2200 # this is only used for reports
2201 sub all_departments {
2202 $main::lxdebug->enter_sub();
2204 my ($self, $myconfig, $table) = @_;
2206 my $dbh = $self->get_standard_dbh($myconfig);
2209 if ($table eq 'customer') {
2210 $where = "WHERE role = 'P' ";
2213 my $query = qq|SELECT id, description
2216 ORDER BY description|;
2217 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2219 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2221 $main::lxdebug->leave_sub();
2225 $main::lxdebug->enter_sub();
2227 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2230 if ($table eq "customer") {
2239 $self->all_vc($myconfig, $table, $module);
2241 # get last customers or vendors
2242 my ($query, $sth, $ref);
2244 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2249 my $transdate = "current_date";
2250 if ($self->{transdate}) {
2251 $transdate = $dbh->quote($self->{transdate});
2254 # now get the account numbers
2255 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2256 FROM chart c, taxkeys tk
2257 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2258 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2261 $sth = $dbh->prepare($query);
2263 do_statement($self, $sth, $query, '%' . $module . '%');
2265 $self->{accounts} = "";
2266 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2268 foreach my $key (split(/:/, $ref->{link})) {
2269 if ($key =~ /\Q$module\E/) {
2271 # cross reference for keys
2272 $xkeyref{ $ref->{accno} } = $key;
2274 push @{ $self->{"${module}_links"}{$key} },
2275 { accno => $ref->{accno},
2276 description => $ref->{description},
2277 taxkey => $ref->{taxkey_id},
2278 tax_id => $ref->{tax_id} };
2280 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2286 # get taxkeys and description
2287 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2288 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2290 if (($module eq "AP") || ($module eq "AR")) {
2291 # get tax rates and description
2292 $query = qq|SELECT * FROM tax|;
2293 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2299 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2300 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2301 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2302 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2304 d.description AS department,
2307 JOIN $table c ON (a.${table}_id = c.id)
2308 LEFT JOIN employee e ON (e.id = a.employee_id)
2309 LEFT JOIN department d ON (d.id = a.department_id)
2311 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2313 foreach $key (keys %$ref) {
2314 $self->{$key} = $ref->{$key};
2317 my $transdate = "current_date";
2318 if ($self->{transdate}) {
2319 $transdate = $dbh->quote($self->{transdate});
2322 # now get the account numbers
2323 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2325 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2327 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2328 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2331 $sth = $dbh->prepare($query);
2332 do_statement($self, $sth, $query, "%$module%");
2334 $self->{accounts} = "";
2335 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2337 foreach my $key (split(/:/, $ref->{link})) {
2338 if ($key =~ /\Q$module\E/) {
2340 # cross reference for keys
2341 $xkeyref{ $ref->{accno} } = $key;
2343 push @{ $self->{"${module}_links"}{$key} },
2344 { accno => $ref->{accno},
2345 description => $ref->{description},
2346 taxkey => $ref->{taxkey_id},
2347 tax_id => $ref->{tax_id} };
2349 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2355 # get amounts from individual entries
2358 c.accno, c.description,
2359 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2363 LEFT JOIN chart c ON (c.id = a.chart_id)
2364 LEFT JOIN project p ON (p.id = a.project_id)
2365 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2366 WHERE (tk.taxkey_id=a.taxkey) AND
2367 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2368 THEN tk.chart_id = a.chart_id
2371 OR (c.link='%tax%')) AND
2372 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2373 WHERE a.trans_id = ?
2374 AND a.fx_transaction = '0'
2375 ORDER BY a.oid, a.transdate|;
2376 $sth = $dbh->prepare($query);
2377 do_statement($self, $sth, $query, $self->{id});
2379 # get exchangerate for currency
2380 $self->{exchangerate} =
2381 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2384 # store amounts in {acc_trans}{$key} for multiple accounts
2385 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2386 $ref->{exchangerate} =
2387 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2388 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2391 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2392 $ref->{amount} *= -1;
2394 $ref->{index} = $index;
2396 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2402 d.curr AS currencies, d.closedto, d.revtrans,
2403 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2404 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2406 $ref = selectfirst_hashref_query($self, $dbh, $query);
2407 map { $self->{$_} = $ref->{$_} } keys %$ref;
2414 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2415 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2416 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2418 $ref = selectfirst_hashref_query($self, $dbh, $query);
2419 map { $self->{$_} = $ref->{$_} } keys %$ref;
2421 if ($self->{"$self->{vc}_id"}) {
2423 # only setup currency
2424 ($self->{currency}) = split(/:/, $self->{currencies});
2428 $self->lastname_used($dbh, $myconfig, $table, $module);
2430 # get exchangerate for currency
2431 $self->{exchangerate} =
2432 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2438 $main::lxdebug->leave_sub();
2442 $main::lxdebug->enter_sub();
2444 my ($self, $dbh, $myconfig, $table, $module) = @_;
2446 my $arap = ($table eq 'customer') ? "ar" : "ap";
2447 $table = $table eq "customer" ? "customer" : "vendor";
2448 my $where = "1 = 1";
2450 if ($self->{type} =~ /_order/) {
2452 $where = "quotation = '0'";
2454 if ($self->{type} =~ /_quotation/) {
2456 $where = "quotation = '1'";
2459 my $query = qq|SELECT MAX(id) FROM $arap
2460 WHERE $where AND ${table}_id > 0|;
2461 my ($trans_id) = selectrow_query($self, $dbh, $query);
2466 a.curr, a.${table}_id, a.department_id,
2467 d.description AS department,
2468 ct.name, current_date + ct.terms AS duedate
2470 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2471 LEFT JOIN department d ON (a.department_id = d.id)
2473 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2474 $self->{department}, $self->{$table}, $self->{duedate})
2475 = selectrow_query($self, $dbh, $query, $trans_id);
2477 $main::lxdebug->leave_sub();
2481 $main::lxdebug->enter_sub();
2483 my ($self, $myconfig, $thisdate, $days) = @_;
2485 my $dbh = $self->get_standard_dbh($myconfig);
2490 my $dateformat = $myconfig->{dateformat};
2491 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2492 $thisdate = $dbh->quote($thisdate);
2493 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2495 $query = qq|SELECT current_date AS thisdate|;
2498 ($thisdate) = selectrow_query($self, $dbh, $query);
2500 $main::lxdebug->leave_sub();
2506 $main::lxdebug->enter_sub();
2508 my ($self, $string) = @_;
2510 if ($string !~ /%/) {
2511 $string = "%$string%";
2514 $string =~ s/\'/\'\'/g;
2516 $main::lxdebug->leave_sub();
2522 $main::lxdebug->enter_sub();
2524 my ($self, $flds, $new, $count, $numrows) = @_;
2528 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2533 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2535 $j = $item->{ndx} - 1;
2536 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2540 for $i ($count + 1 .. $numrows) {
2541 map { delete $self->{"${_}_$i"} } @{$flds};
2544 $main::lxdebug->leave_sub();
2548 $main::lxdebug->enter_sub();
2550 my ($self, $myconfig) = @_;
2554 my $dbh = $self->dbconnect_noauto($myconfig);
2556 my $query = qq|DELETE FROM status
2557 WHERE (formname = ?) AND (trans_id = ?)|;
2558 my $sth = prepare_query($self, $dbh, $query);
2560 if ($self->{formname} =~ /(check|receipt)/) {
2561 for $i (1 .. $self->{rowcount}) {
2562 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2565 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2569 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2570 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2572 my %queued = split / /, $self->{queued};
2575 if ($self->{formname} =~ /(check|receipt)/) {
2577 # this is a check or receipt, add one entry for each lineitem
2578 my ($accno) = split /--/, $self->{account};
2579 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2580 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2581 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2582 $sth = prepare_query($self, $dbh, $query);
2584 for $i (1 .. $self->{rowcount}) {
2585 if ($self->{"checked_$i"}) {
2586 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2592 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2593 VALUES (?, ?, ?, ?, ?)|;
2594 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2595 $queued{$self->{formname}}, $self->{formname});
2601 $main::lxdebug->leave_sub();
2605 $main::lxdebug->enter_sub();
2607 my ($self, $dbh) = @_;
2609 my ($query, $printed, $emailed);
2611 my $formnames = $self->{printed};
2612 my $emailforms = $self->{emailed};
2614 $query = qq|DELETE FROM status
2615 WHERE (formname = ?) AND (trans_id = ?)|;
2616 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2618 # this only applies to the forms
2619 # checks and receipts are posted when printed or queued
2621 if ($self->{queued}) {
2622 my %queued = split / /, $self->{queued};
2624 foreach my $formname (keys %queued) {
2625 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2626 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2628 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2629 VALUES (?, ?, ?, ?, ?)|;
2630 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2632 $formnames =~ s/\Q$self->{formname}\E//;
2633 $emailforms =~ s/\Q$self->{formname}\E//;
2638 # save printed, emailed info
2639 $formnames =~ s/^ +//g;
2640 $emailforms =~ s/^ +//g;
2643 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2644 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2646 foreach my $formname (keys %status) {
2647 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2648 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2650 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2651 VALUES (?, ?, ?, ?)|;
2652 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2655 $main::lxdebug->leave_sub();
2659 # $main::locale->text('SAVED')
2660 # $main::locale->text('DELETED')
2661 # $main::locale->text('ADDED')
2662 # $main::locale->text('PAYMENT POSTED')
2663 # $main::locale->text('POSTED')
2664 # $main::locale->text('POSTED AS NEW')
2665 # $main::locale->text('ELSE')
2666 # $main::locale->text('SAVED FOR DUNNING')
2667 # $main::locale->text('DUNNING STARTED')
2668 # $main::locale->text('PRINTED')
2669 # $main::locale->text('MAILED')
2670 # $main::locale->text('SCREENED')
2671 # $main::locale->text('CANCELED')
2672 # $main::locale->text('invoice')
2673 # $main::locale->text('proforma')
2674 # $main::locale->text('sales_order')
2675 # $main::locale->text('packing_list')
2676 # $main::locale->text('pick_list')
2677 # $main::locale->text('purchase_order')
2678 # $main::locale->text('bin_list')
2679 # $main::locale->text('sales_quotation')
2680 # $main::locale->text('request_quotation')
2683 $main::lxdebug->enter_sub();
2688 if(!exists $self->{employee_id}) {
2689 &get_employee($self, $dbh);
2693 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2694 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2695 my @values = (conv_i($self->{id}), $self->{login},
2696 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2697 do_query($self, $dbh, $query, @values);
2699 $main::lxdebug->leave_sub();
2703 $main::lxdebug->enter_sub();
2705 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2706 my ($orderBy, $desc) = split(/\-\-/, $order);
2707 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2710 if ($trans_id ne "") {
2712 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 | .
2713 qq|FROM history_erp h | .
2714 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2715 qq|WHERE trans_id = | . $trans_id
2716 . $restriction . qq| |
2719 my $sth = $dbh->prepare($query) || $self->dberror($query);
2721 $sth->execute() || $self->dberror("$query");
2723 while(my $hash_ref = $sth->fetchrow_hashref()) {
2724 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2725 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2726 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2727 $tempArray[$i++] = $hash_ref;
2729 $main::lxdebug->leave_sub() and return \@tempArray
2730 if ($i > 0 && $tempArray[0] ne "");
2732 $main::lxdebug->leave_sub();
2736 sub update_defaults {
2737 $main::lxdebug->enter_sub();
2739 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2742 if ($provided_dbh) {
2743 $dbh = $provided_dbh;
2745 $dbh = $self->dbconnect_noauto($myconfig);
2747 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2748 my $sth = $dbh->prepare($query);
2750 $sth->execute || $self->dberror($query);
2751 my ($var) = $sth->fetchrow_array;
2754 if ($var =~ m/\d+$/) {
2755 my $new_var = (substr $var, $-[0]) * 1 + 1;
2756 my $len_diff = length($var) - $-[0] - length($new_var);
2757 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2763 $query = qq|UPDATE defaults SET $fld = ?|;
2764 do_query($self, $dbh, $query, $var);
2766 if (!$provided_dbh) {
2771 $main::lxdebug->leave_sub();
2776 sub update_business {
2777 $main::lxdebug->enter_sub();
2779 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2782 if ($provided_dbh) {
2783 $dbh = $provided_dbh;
2785 $dbh = $self->dbconnect_noauto($myconfig);
2788 qq|SELECT customernumberinit FROM business
2789 WHERE id = ? FOR UPDATE|;
2790 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2792 if ($var =~ m/\d+$/) {
2793 my $new_var = (substr $var, $-[0]) * 1 + 1;
2794 my $len_diff = length($var) - $-[0] - length($new_var);
2795 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2801 $query = qq|UPDATE business
2802 SET customernumberinit = ?
2804 do_query($self, $dbh, $query, $var, $business_id);
2806 if (!$provided_dbh) {
2811 $main::lxdebug->leave_sub();
2816 sub get_partsgroup {
2817 $main::lxdebug->enter_sub();
2819 my ($self, $myconfig, $p) = @_;
2821 my $dbh = $self->get_standard_dbh($myconfig);
2823 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2825 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2828 if ($p->{searchitems} eq 'part') {
2829 $query .= qq|WHERE p.inventory_accno_id > 0|;
2831 if ($p->{searchitems} eq 'service') {
2832 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2834 if ($p->{searchitems} eq 'assembly') {
2835 $query .= qq|WHERE p.assembly = '1'|;
2837 if ($p->{searchitems} eq 'labor') {
2838 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2841 $query .= qq|ORDER BY partsgroup|;
2844 $query = qq|SELECT id, partsgroup FROM partsgroup
2845 ORDER BY partsgroup|;
2848 if ($p->{language_code}) {
2849 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2850 t.description AS translation
2852 JOIN parts p ON (p.partsgroup_id = pg.id)
2853 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2854 ORDER BY translation|;
2855 @values = ($p->{language_code});
2858 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2860 $main::lxdebug->leave_sub();
2863 sub get_pricegroup {
2864 $main::lxdebug->enter_sub();
2866 my ($self, $myconfig, $p) = @_;
2868 my $dbh = $self->get_standard_dbh($myconfig);
2870 my $query = qq|SELECT p.id, p.pricegroup
2873 $query .= qq| ORDER BY pricegroup|;
2876 $query = qq|SELECT id, pricegroup FROM pricegroup
2877 ORDER BY pricegroup|;
2880 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2882 $main::lxdebug->leave_sub();
2886 # usage $form->all_years($myconfig, [$dbh])
2887 # return list of all years where bookings found
2890 $main::lxdebug->enter_sub();
2892 my ($self, $myconfig, $dbh) = @_;
2894 $dbh ||= $self->get_standard_dbh($myconfig);
2897 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2898 (SELECT MAX(transdate) FROM acc_trans)|;
2899 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2901 if ($myconfig->{dateformat} =~ /^yy/) {
2902 ($startdate) = split /\W/, $startdate;
2903 ($enddate) = split /\W/, $enddate;
2905 (@_) = split /\W/, $startdate;
2907 (@_) = split /\W/, $enddate;
2912 $startdate = substr($startdate,0,4);
2913 $enddate = substr($enddate,0,4);
2915 while ($enddate >= $startdate) {
2916 push @all_years, $enddate--;
2921 $main::lxdebug->leave_sub();