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();
227 sub _flatten_variables_rec {
228 $main::lxdebug->enter_sub(2);
237 if ('' eq ref $curr->{$key}) {
238 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
240 } elsif ('HASH' eq ref $curr->{$key}) {
241 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
242 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
246 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
247 my $first_array_entry = 1;
249 foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
250 push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
251 $first_array_entry = 0;
256 $main::lxdebug->leave_sub(2);
261 sub flatten_variables {
262 $main::lxdebug->enter_sub(2);
270 push @variables, $self->_flatten_variables_rec($self, '', $_);
273 $main::lxdebug->leave_sub(2);
280 $main::lxdebug->enter_sub();
286 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
288 $main::lxdebug->leave_sub();
292 $main::lxdebug->enter_sub(2);
294 my ($self, $str) = @_;
296 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
298 $main::lxdebug->leave_sub(2);
304 $main::lxdebug->enter_sub(2);
306 my ($self, $str) = @_;
311 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
313 $main::lxdebug->leave_sub(2);
319 my ($self, $str) = @_;
321 if ($str && !ref($str)) {
322 $str =~ s/\"/"/g;
330 my ($self, $str) = @_;
332 if ($str && !ref($str)) {
333 $str =~ s/"/\"/g;
341 $main::lxdebug->enter_sub(2);
343 my ($self, $str) = @_;
346 ('order' => ['"', '<', '>'],
352 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
354 $main::lxdebug->leave_sub(2);
363 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
365 for (sort keys %$self) {
366 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
367 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
374 $main::lxdebug->enter_sub();
376 $main::lxdebug->show_backtrace();
378 my ($self, $msg) = @_;
379 if ($ENV{HTTP_USER_AGENT}) {
381 $self->show_generic_error($msg);
388 $main::lxdebug->leave_sub();
392 $main::lxdebug->enter_sub();
394 my ($self, $msg) = @_;
396 if ($ENV{HTTP_USER_AGENT}) {
399 if (!$self->{header}) {
412 if ($self->{info_function}) {
413 &{ $self->{info_function} }($msg);
419 $main::lxdebug->leave_sub();
422 # calculates the number of rows in a textarea based on the content and column number
423 # can be capped with maxrows
425 $main::lxdebug->enter_sub();
426 my ($self, $str, $cols, $maxrows) = @_;
428 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
431 $main::lxdebug->leave_sub();
432 return min $rows, $maxrows;
436 $main::lxdebug->enter_sub();
438 my ($self, $msg) = @_;
440 $self->error("$msg\n" . $DBI::errstr);
442 $main::lxdebug->leave_sub();
446 $main::lxdebug->enter_sub();
448 my ($self, $name, $msg) = @_;
451 foreach my $part (split '.', $name) {
452 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
455 $curr = $curr->{$part};
458 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 my ($self, $extra_code) = @_;
466 if ($self->{header}) {
467 $main::lxdebug->leave_sub();
471 my ($stylesheet, $favicon);
473 if ($ENV{HTTP_USER_AGENT}) {
476 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
477 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
478 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
481 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
483 $stylesheets =~ s|^\s*||;
484 $stylesheets =~ s|\s*$||;
485 foreach my $file (split m/\s+/, $stylesheets) {
487 next if (! -f "css/$file");
489 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
492 $self->{favicon} = "favicon.ico" unless $self->{favicon};
494 if ($self->{favicon} && (-f "$self->{favicon}")) {
496 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
500 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
502 if ($self->{landscape}) {
503 $pagelayout = qq|<style type="text/css">
504 \@page { size:landscape; }
508 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
512 if ($self->{jsscript} == 1) {
515 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
516 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
517 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
518 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
525 ? "$self->{title} - $self->{titlebar}"
528 foreach $item (@ { $self->{AJAX} }) {
529 $ajax .= $item->show_javascript();
531 print qq|Content-Type: text/html; charset=${db_charset};
535 <title>$self->{titlebar}</title>
539 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
543 <script type="text/javascript">
551 <meta name="robots" content="noindex,nofollow" />
552 <script type="text/javascript" src="js/highlight_input.js"></script>
553 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
555 <script type="text/javascript" src="js/tabcontent.js">
557 /***********************************************
558 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
559 * This notice MUST stay intact for legal use
560 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
561 ***********************************************/
572 $main::lxdebug->leave_sub();
575 sub _prepare_html_template {
576 $main::lxdebug->enter_sub();
578 my ($self, $file, $additional_params) = @_;
581 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
582 $language = $main::language;
584 $language = $main::myconfig{"countrycode"};
586 $language = "de" unless ($language);
588 if (-f "templates/webpages/${file}_${language}.html") {
589 if ((-f ".developer") &&
590 (-f "templates/webpages/${file}_master.html") &&
591 ((stat("templates/webpages/${file}_master.html"))[9] >
592 (stat("templates/webpages/${file}_${language}.html"))[9])) {
593 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
594 "Please re-run 'locales.pl' in 'locale/${language}'.";
595 print(qq|<pre>$info</pre>|);
599 $file = "templates/webpages/${file}_${language}.html";
600 } elsif (-f "templates/webpages/${file}.html") {
601 $file = "templates/webpages/${file}.html";
603 my $info = "Web page template '${file}' not found.\n" .
604 "Please re-run 'locales.pl' in 'locale/${language}'.";
605 print(qq|<pre>$info</pre>|);
609 if ($self->{"DEBUG"}) {
610 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
613 if ($additional_params->{"DEBUG"}) {
614 $additional_params->{"DEBUG"} =
615 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
618 if (%main::myconfig) {
619 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
620 my $jsc_dateformat = $main::myconfig{"dateformat"};
621 $jsc_dateformat =~ s/d+/\%d/gi;
622 $jsc_dateformat =~ s/m+/\%m/gi;
623 $jsc_dateformat =~ s/y+/\%Y/gi;
624 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
627 $additional_params->{"conf_webdav"} = $main::webdav;
628 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
629 $additional_params->{"conf_latex_templates"} = $main::latex;
630 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
632 if (%main::debug_options) {
633 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
636 $main::lxdebug->leave_sub();
641 sub parse_html_template {
642 $main::lxdebug->enter_sub();
644 my ($self, $file, $additional_params) = @_;
646 $additional_params ||= { };
648 $file = $self->_prepare_html_template($file, $additional_params);
650 my $template = Template->new({ 'INTERPOLATE' => 0,
654 'PLUGIN_BASE' => 'SL::Template::Plugin',
655 'INCLUDE_PATH' => '.:templates/webpages',
658 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
661 if (!$template->process($file, $additional_params, \$output)) {
662 print STDERR $template->error();
665 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
667 $main::lxdebug->leave_sub();
672 sub show_generic_error {
673 my ($self, $error, $title, $action) = @_;
676 'title_error' => $title,
677 'label_error' => $error,
682 map({ delete($self->{$_}); } qw(action));
683 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
684 if (!ref($self->{$_})); }
686 $add_params->{"SHOW_BUTTON"} = 1;
687 $add_params->{"BUTTON_LABEL"} = $action;
689 $add_params->{"VARIABLES"} = \@vars;
691 $self->{title} = $title if ($title);
694 print $self->parse_html_template("generic/error", $add_params);
696 die("Error: $error\n");
699 sub show_generic_information {
700 my ($self, $text, $title) = @_;
703 'title_information' => $title,
704 'label_information' => $text,
707 $self->{title} = $title if ($title);
710 print $self->parse_html_template("generic/information", $add_params);
712 die("Information: $error\n");
715 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
716 # changed it to accept an arbitrary number of triggers - sschoeling
718 $main::lxdebug->enter_sub();
721 my $myconfig = shift;
724 # set dateform for jsscript
727 "dd.mm.yy" => "%d.%m.%Y",
728 "dd-mm-yy" => "%d-%m-%Y",
729 "dd/mm/yy" => "%d/%m/%Y",
730 "mm/dd/yy" => "%m/%d/%Y",
731 "mm-dd-yy" => "%m-%d-%Y",
732 "yyyy-mm-dd" => "%Y-%m-%d",
735 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
736 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
743 inputField : "| . (shift) . qq|",
744 ifFormat :"$ifFormat",
745 align : "| . (shift) . qq|",
746 button : "| . (shift) . qq|"
752 <script type="text/javascript">
753 <!--| . join("", @triggers) . qq|//-->
757 $main::lxdebug->leave_sub();
760 } #end sub write_trigger
763 $main::lxdebug->enter_sub();
765 my ($self, $msg) = @_;
767 if ($self->{callback}) {
769 ($script, $argv) = split(/\?/, $self->{callback}, 2);
771 $script =~ s|[^a-zA-Z0-9_\.]||g;
772 exec("perl", "$script", $argv);
780 $main::lxdebug->leave_sub();
783 # sort of columns removed - empty sub
785 $main::lxdebug->enter_sub();
787 my ($self, @columns) = @_;
789 $main::lxdebug->leave_sub();
795 $main::lxdebug->enter_sub(2);
797 my ($self, $myconfig, $amount, $places, $dash) = @_;
803 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
805 my $neg = ($amount =~ s/^-//);
806 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
808 if (defined($places) && ($places ne '')) {
814 my ($actual_places) = ($amount =~ /\.(\d+)/);
815 $actual_places = length($actual_places);
816 $places = $actual_places > $places ? $actual_places : $places;
819 $amount = $self->round_amount($amount, $places);
822 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
823 my @p = split(/\./, $amount); # split amount at decimal point
825 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
828 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
831 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
832 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
833 ($neg ? "-$amount" : "$amount" ) ;
837 $main::lxdebug->leave_sub(2);
843 $main::lxdebug->enter_sub(2);
848 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
849 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
850 $input =~ s/\#\#/\#/g;
852 $main::lxdebug->leave_sub(2);
858 $main::lxdebug->enter_sub(2);
860 my ($self, $myconfig, $amount) = @_;
862 if ( ($myconfig->{numberformat} eq '1.000,00')
863 || ($myconfig->{numberformat} eq '1000,00')) {
868 if ($myconfig->{numberformat} eq "1'000.00") {
874 $main::lxdebug->leave_sub(2);
876 return ($amount * 1);
880 $main::lxdebug->enter_sub(2);
882 my ($self, $amount, $places) = @_;
885 # Rounding like "Kaufmannsrunden"
886 # Descr. http://de.wikipedia.org/wiki/Rundung
888 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
891 $amount = $amount * (10**($places));
892 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
894 $main::lxdebug->leave_sub(2);
896 return $round_amount;
901 $main::lxdebug->enter_sub();
903 my ($self, $myconfig, $userspath) = @_;
904 my ($template, $out);
908 $self->{"cwd"} = getcwd();
909 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
911 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
912 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
913 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
914 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
915 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
916 } elsif (($self->{"format"} =~ /html/i) ||
917 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
918 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
919 } elsif (($self->{"format"} =~ /xml/i) ||
920 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
921 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
922 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
923 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
924 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
925 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
926 } elsif ( defined $self->{'format'}) {
927 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
928 } elsif ( $self->{'format'} eq '' ) {
929 $self->error("No Outputformat given: $self->{'format'}");
930 } else { #Catch the rest
931 $self->error("Outputformat not defined: $self->{'format'}");
934 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
935 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
937 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
938 qw(email tel fax name signature company address businessnumber
939 co_ustid taxnumber duns));
940 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
941 qw(company address signature));
942 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
944 map({ $self->{"${_}"} = $myconfig->{$_}; }
948 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
950 # OUT is used for the media, screen, printer, email
951 # for postscript we store a copy in a temporary file
953 my $prepend_userspath;
955 if (!$self->{tmpfile}) {
956 $self->{tmpfile} = "${fileid}.$self->{IN}";
957 $prepend_userspath = 1;
960 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
962 $self->{tmpfile} =~ s|.*/||;
963 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
964 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
966 if ($template->uses_temp_file() || $self->{media} eq 'email') {
968 $self->{OUT} = ">$self->{tmpfile}";
972 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
974 open(OUT, ">-") or $self->error("STDOUT : $!");
978 if (!$template->parse(*OUT)) {
980 $self->error("$self->{IN} : " . $template->get_error());
985 if ($template->uses_temp_file() || $self->{media} eq 'email') {
987 if ($self->{media} eq 'email') {
989 my $mail = new Mailer;
991 map { $mail->{$_} = $self->{$_} }
992 qw(cc bcc subject message version format);
993 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
994 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
995 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
996 $mail->{fileid} = "$fileid.";
997 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
999 # if we send html or plain text inline
1000 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1001 $mail->{contenttype} = "text/html";
1003 $mail->{message} =~ s/\r\n/<br>\n/g;
1004 $myconfig->{signature} =~ s/\\n/<br>\n/g;
1005 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1007 open(IN, $self->{tmpfile})
1008 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1010 $mail->{message} .= $_;
1017 if (!$self->{"do_not_attach"}) {
1018 @{ $mail->{attachments} } =
1019 ({ "filename" => $self->{"tmpfile"},
1020 "name" => $self->{"attachment_filename"} ?
1021 $self->{"attachment_filename"} : $self->{"tmpfile"} });
1024 $mail->{message} =~ s/\r\n/\n/g;
1025 $myconfig->{signature} =~ s/\\n/\n/g;
1026 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1030 my $err = $mail->send();
1031 $self->error($self->cleanup . "$err") if ($err);
1035 $self->{OUT} = $out;
1037 my $numbytes = (-s $self->{tmpfile});
1038 open(IN, $self->{tmpfile})
1039 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1041 $self->{copies} = 1 unless $self->{media} eq 'printer';
1043 chdir("$self->{cwd}");
1044 #print(STDERR "Kopien $self->{copies}\n");
1045 #print(STDERR "OUT $self->{OUT}\n");
1046 for my $i (1 .. $self->{copies}) {
1048 open(OUT, $self->{OUT})
1049 or $self->error($self->cleanup . "$self->{OUT} : $!");
1051 $self->{attachment_filename} = ($self->{attachment_filename})
1052 ? $self->{attachment_filename}
1053 : $self->generate_attachment_filename();
1055 # launch application
1056 print qq|Content-Type: | . $template->get_mime_type() . qq|
1057 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1058 Content-Length: $numbytes
1062 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1082 chdir("$self->{cwd}");
1083 $main::lxdebug->leave_sub();
1086 sub get_formname_translation {
1087 my ($self, $formname) = @_;
1089 $formname ||= $self->{formname};
1091 my %formname_translations = (
1092 bin_list => $main::locale->text('Bin List'),
1093 credit_note => $main::locale->text('Credit Note'),
1094 invoice => $main::locale->text('Invoice'),
1095 packing_list => $main::locale->text('Packing List'),
1096 pick_list => $main::locale->text('Pick List'),
1097 proforma => $main::locale->text('Proforma Invoice'),
1098 purchase_order => $main::locale->text('Purchase Order'),
1099 request_quotation => $main::locale->text('RFQ'),
1100 sales_order => $main::locale->text('Confirmation'),
1101 sales_quotation => $main::locale->text('Quotation'),
1102 storno_invoice => $main::locale->text('Storno Invoice'),
1103 storno_packing_list => $main::locale->text('Storno Packing List'),
1106 return $formname_translations{$formname}
1109 sub generate_attachment_filename {
1112 my $attachment_filename = $self->get_formname_translation();
1114 (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1115 : ($self->{"type"} =~ /_quotation$/) ? "quo"
1118 if ($attachment_filename && $self->{"${prefix}number"}) {
1119 $attachment_filename .= "_" . $self->{"${prefix}number"}
1120 . ( $self->{format} =~ /pdf/i ? ".pdf"
1121 : $self->{format} =~ /postscript/i ? ".ps"
1122 : $self->{format} =~ /opendocument/i ? ".odt"
1123 : $self->{format} =~ /html/i ? ".html"
1125 $attachment_filename =~ s/ /_/g;
1126 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1127 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1128 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1130 $attachment_filename = "";
1133 return $attachment_filename;
1137 $main::lxdebug->enter_sub();
1141 chdir("$self->{tmpdir}");
1144 if (-f "$self->{tmpfile}.err") {
1145 open(FH, "$self->{tmpfile}.err");
1150 if ($self->{tmpfile}) {
1151 $self->{tmpfile} =~ s|.*/||g;
1153 $self->{tmpfile} =~ s/\.\w+$//g;
1154 my $tmpfile = $self->{tmpfile};
1155 unlink(<$tmpfile.*>);
1158 chdir("$self->{cwd}");
1160 $main::lxdebug->leave_sub();
1166 $main::lxdebug->enter_sub();
1168 my ($self, $date, $myconfig) = @_;
1170 if ($date && $date =~ /\D/) {
1172 if ($myconfig->{dateformat} =~ /^yy/) {
1173 ($yy, $mm, $dd) = split /\D/, $date;
1175 if ($myconfig->{dateformat} =~ /^mm/) {
1176 ($mm, $dd, $yy) = split /\D/, $date;
1178 if ($myconfig->{dateformat} =~ /^dd/) {
1179 ($dd, $mm, $yy) = split /\D/, $date;
1184 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1185 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1187 $dd = "0$dd" if ($dd < 10);
1188 $mm = "0$mm" if ($mm < 10);
1190 $date = "$yy$mm$dd";
1193 $main::lxdebug->leave_sub();
1198 # Database routines used throughout
1201 $main::lxdebug->enter_sub(2);
1203 my ($self, $myconfig) = @_;
1205 # connect to database
1207 DBI->connect($myconfig->{dbconnect},
1208 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1212 if ($myconfig->{dboptions}) {
1213 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1216 $main::lxdebug->leave_sub(2);
1221 sub dbconnect_noauto {
1222 $main::lxdebug->enter_sub();
1224 my ($self, $myconfig) = @_;
1226 # connect to database
1228 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1229 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1233 if ($myconfig->{dboptions}) {
1234 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1237 $main::lxdebug->leave_sub();
1242 sub get_standard_dbh {
1243 $main::lxdebug->enter_sub(2);
1245 my ($self, $myconfig) = @_;
1247 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1249 $main::lxdebug->leave_sub(2);
1251 return $standard_dbh;
1254 sub update_balance {
1255 $main::lxdebug->enter_sub();
1257 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1259 # if we have a value, go do it
1262 # retrieve balance from table
1263 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1264 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1265 my ($balance) = $sth->fetchrow_array;
1271 $query = "UPDATE $table SET $field = $balance WHERE $where";
1272 do_query($self, $dbh, $query, @values);
1274 $main::lxdebug->leave_sub();
1277 sub update_exchangerate {
1278 $main::lxdebug->enter_sub();
1280 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1282 # some sanity check for currency
1284 $main::lxdebug->leave_sub();
1287 $query = qq|SELECT curr FROM defaults|;
1289 my ($currency) = selectrow_query($self, $dbh, $query);
1290 my ($defaultcurrency) = split m/:/, $currency;
1293 if ($curr eq $defaultcurrency) {
1294 $main::lxdebug->leave_sub();
1298 $query = qq|SELECT e.curr FROM exchangerate e
1299 WHERE e.curr = ? AND e.transdate = ?
1301 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1310 $buy = conv_i($buy, "NULL");
1311 $sell = conv_i($sell, "NULL");
1314 if ($buy != 0 && $sell != 0) {
1315 $set = "buy = $buy, sell = $sell";
1316 } elsif ($buy != 0) {
1317 $set = "buy = $buy";
1318 } elsif ($sell != 0) {
1319 $set = "sell = $sell";
1322 if ($sth->fetchrow_array) {
1323 $query = qq|UPDATE exchangerate
1329 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1330 VALUES (?, $buy, $sell, ?)|;
1333 do_query($self, $dbh, $query, $curr, $transdate);
1335 $main::lxdebug->leave_sub();
1338 sub save_exchangerate {
1339 $main::lxdebug->enter_sub();
1341 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1343 my $dbh = $self->dbconnect($myconfig);
1347 $buy = $rate if $fld eq 'buy';
1348 $sell = $rate if $fld eq 'sell';
1351 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1356 $main::lxdebug->leave_sub();
1359 sub get_exchangerate {
1360 $main::lxdebug->enter_sub();
1362 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1365 unless ($transdate) {
1366 $main::lxdebug->leave_sub();
1370 $query = qq|SELECT curr FROM defaults|;
1372 my ($currency) = selectrow_query($self, $dbh, $query);
1373 my ($defaultcurrency) = split m/:/, $currency;
1375 if ($currency eq $defaultcurrency) {
1376 $main::lxdebug->leave_sub();
1380 $query = qq|SELECT e.$fld FROM exchangerate e
1381 WHERE e.curr = ? AND e.transdate = ?|;
1382 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1386 $main::lxdebug->leave_sub();
1388 return $exchangerate;
1391 sub check_exchangerate {
1392 $main::lxdebug->enter_sub();
1394 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1396 unless ($transdate) {
1397 $main::lxdebug->leave_sub();
1401 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1403 if ($currency eq $defaultcurrency) {
1404 $main::lxdebug->leave_sub();
1408 my $dbh = $self->get_standard_dbh($myconfig);
1409 my $query = qq|SELECT e.$fld FROM exchangerate e
1410 WHERE e.curr = ? AND e.transdate = ?|;
1412 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1414 $exchangerate = 1 if ($exchangerate eq "");
1416 $main::lxdebug->leave_sub();
1418 return $exchangerate;
1421 sub get_default_currency {
1422 $main::lxdebug->enter_sub();
1424 my ($self, $myconfig) = @_;
1425 my $dbh = $self->get_standard_dbh($myconfig);
1427 my $query = qq|SELECT curr FROM defaults|;
1429 my ($curr) = selectrow_query($self, $dbh, $query);
1430 my ($defaultcurrency) = split m/:/, $curr;
1432 $main::lxdebug->leave_sub();
1434 return $defaultcurrency;
1438 sub set_payment_options {
1439 $main::lxdebug->enter_sub();
1441 my ($self, $myconfig, $transdate) = @_;
1443 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1445 my $dbh = $self->get_standard_dbh($myconfig);
1448 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1449 qq|FROM payment_terms p | .
1452 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1453 $self->{payment_terms}) =
1454 selectrow_query($self, $dbh, $query, $self->{payment_id});
1456 if ($transdate eq "") {
1457 if ($self->{invdate}) {
1458 $transdate = $self->{invdate};
1460 $transdate = $self->{transdate};
1465 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1466 qq|FROM payment_terms|;
1467 ($self->{netto_date}, $self->{skonto_date}) =
1468 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1470 my ($invtotal, $total);
1471 my (%amounts, %formatted_amounts);
1473 if ($self->{type} =~ /_order$/) {
1474 $amounts{invtotal} = $self->{ordtotal};
1475 $amounts{total} = $self->{ordtotal};
1477 } elsif ($self->{type} =~ /_quotation$/) {
1478 $amounts{invtotal} = $self->{quototal};
1479 $amounts{total} = $self->{quototal};
1482 $amounts{invtotal} = $self->{invtotal};
1483 $amounts{total} = $self->{total};
1486 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1488 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1489 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1490 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1492 foreach (keys %amounts) {
1493 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1494 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1497 if ($self->{"language_id"}) {
1499 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1500 qq|FROM translation_payment_terms t | .
1501 qq|LEFT JOIN language l ON t.language_id = l.id | .
1502 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1503 my ($description_long, $output_numberformat, $output_dateformat,
1504 $output_longdates) =
1505 selectrow_query($self, $dbh, $query,
1506 $self->{"language_id"}, $self->{"payment_id"});
1508 $self->{payment_terms} = $description_long if ($description_long);
1510 if ($output_dateformat) {
1511 foreach my $key (qw(netto_date skonto_date)) {
1513 $main::locale->reformat_date($myconfig, $self->{$key},
1519 if ($output_numberformat &&
1520 ($output_numberformat ne $myconfig->{"numberformat"})) {
1521 my $saved_numberformat = $myconfig->{"numberformat"};
1522 $myconfig->{"numberformat"} = $output_numberformat;
1523 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1524 $myconfig->{"numberformat"} = $saved_numberformat;
1528 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1529 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1530 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1531 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1532 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1533 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1534 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1536 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1538 $main::lxdebug->leave_sub();
1542 sub get_template_language {
1543 $main::lxdebug->enter_sub();
1545 my ($self, $myconfig) = @_;
1547 my $template_code = "";
1549 if ($self->{language_id}) {
1550 my $dbh = $self->get_standard_dbh($myconfig);
1551 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1552 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1555 $main::lxdebug->leave_sub();
1557 return $template_code;
1560 sub get_printer_code {
1561 $main::lxdebug->enter_sub();
1563 my ($self, $myconfig) = @_;
1565 my $template_code = "";
1567 if ($self->{printer_id}) {
1568 my $dbh = $self->get_standard_dbh($myconfig);
1569 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1570 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1573 $main::lxdebug->leave_sub();
1575 return $template_code;
1579 $main::lxdebug->enter_sub();
1581 my ($self, $myconfig) = @_;
1583 my $template_code = "";
1585 if ($self->{shipto_id}) {
1586 my $dbh = $self->get_standard_dbh($myconfig);
1587 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1588 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1589 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1592 $main::lxdebug->leave_sub();
1596 $main::lxdebug->enter_sub();
1598 my ($self, $dbh, $id, $module) = @_;
1603 foreach my $item (qw(name department_1 department_2 street zipcode city country
1604 contact phone fax email)) {
1605 if ($self->{"shipto$item"}) {
1606 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1608 push(@values, $self->{"shipto${item}"});
1612 if ($self->{shipto_id}) {
1613 my $query = qq|UPDATE shipto set
1615 shiptodepartment_1 = ?,
1616 shiptodepartment_2 = ?,
1625 WHERE shipto_id = ?|;
1626 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1628 my $query = qq|SELECT * FROM shipto
1629 WHERE shiptoname = ? AND
1630 shiptodepartment_1 = ? AND
1631 shiptodepartment_2 = ? AND
1632 shiptostreet = ? AND
1633 shiptozipcode = ? AND
1635 shiptocountry = ? AND
1636 shiptocontact = ? AND
1642 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1645 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1646 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1647 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1648 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1649 do_query($self, $dbh, $query, $id, @values, $module);
1654 $main::lxdebug->leave_sub();
1658 $main::lxdebug->enter_sub();
1660 my ($self, $dbh) = @_;
1662 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1663 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1664 $self->{"employee_id"} *= 1;
1666 $main::lxdebug->leave_sub();
1670 $main::lxdebug->enter_sub();
1672 my ($self, $myconfig, $salesman_id) = @_;
1674 $main::lxdebug->leave_sub() and return unless $salesman_id;
1676 my $dbh = $self->get_standard_dbh($myconfig);
1679 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1683 my $user = new User($main::memberfile, $login);
1684 map({ $self->{"salesman_$_"} = $user->{$_}; }
1685 qw(address businessnumber co_ustid company duns email fax name
1687 $self->{salesman_login} = $login;
1689 $self->{salesman_name} = $login
1690 if ($self->{salesman_name} eq "");
1692 map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
1695 $main::lxdebug->leave_sub();
1699 $main::lxdebug->enter_sub();
1701 my ($self, $myconfig) = @_;
1703 my $dbh = $self->get_standard_dbh($myconfig);
1704 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1705 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1707 $main::lxdebug->leave_sub();
1711 $main::lxdebug->enter_sub();
1713 my ($self, $dbh, $id, $key) = @_;
1715 $key = "all_contacts" unless ($key);
1718 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1719 qq|FROM contacts | .
1720 qq|WHERE cp_cv_id = ? | .
1721 qq|ORDER BY lower(cp_name)|;
1723 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1725 $main::lxdebug->leave_sub();
1729 $main::lxdebug->enter_sub();
1731 my ($self, $dbh, $key) = @_;
1733 my ($all, $old_id, $where, @values);
1735 if (ref($key) eq "HASH") {
1738 $key = "ALL_PROJECTS";
1740 foreach my $p (keys(%{$params})) {
1742 $all = $params->{$p};
1743 } elsif ($p eq "old_id") {
1744 $old_id = $params->{$p};
1745 } elsif ($p eq "key") {
1746 $key = $params->{$p};
1752 $where = "WHERE active ";
1754 if (ref($old_id) eq "ARRAY") {
1755 my @ids = grep({ $_ } @{$old_id});
1757 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1758 push(@values, @ids);
1761 $where .= " OR (id = ?) ";
1762 push(@values, $old_id);
1768 qq|SELECT id, projectnumber, description, active | .
1771 qq|ORDER BY lower(projectnumber)|;
1773 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1775 $main::lxdebug->leave_sub();
1779 $main::lxdebug->enter_sub();
1781 my ($self, $dbh, $vc_id, $key) = @_;
1783 $key = "all_shipto" unless ($key);
1785 # get shipping addresses
1786 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1788 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1790 $main::lxdebug->leave_sub();
1794 $main::lxdebug->enter_sub();
1796 my ($self, $dbh, $key) = @_;
1798 $key = "all_printers" unless ($key);
1800 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1802 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1804 $main::lxdebug->leave_sub();
1808 $main::lxdebug->enter_sub();
1810 my ($self, $dbh, $params) = @_;
1812 $key = $params->{key};
1813 $key = "all_charts" unless ($key);
1815 my $transdate = quote_db_date($params->{transdate});
1818 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1820 qq|LEFT JOIN taxkeys tk ON | .
1821 qq|(tk.id = (SELECT id FROM taxkeys | .
1822 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1823 qq| ORDER BY startdate DESC LIMIT 1)) | .
1824 qq|ORDER BY c.accno|;
1826 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1828 $main::lxdebug->leave_sub();
1831 sub _get_taxcharts {
1832 $main::lxdebug->enter_sub();
1834 my ($self, $dbh, $key) = @_;
1836 $key = "all_taxcharts" unless ($key);
1838 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1840 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1842 $main::lxdebug->leave_sub();
1846 $main::lxdebug->enter_sub();
1848 my ($self, $dbh, $key) = @_;
1850 $key = "all_taxzones" unless ($key);
1852 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1854 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1856 $main::lxdebug->leave_sub();
1859 sub _get_employees {
1860 $main::lxdebug->enter_sub();
1862 my ($self, $dbh, $default_key, $key) = @_;
1864 $key = $default_key unless ($key);
1865 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
1867 $main::lxdebug->leave_sub();
1870 sub _get_business_types {
1871 $main::lxdebug->enter_sub();
1873 my ($self, $dbh, $key) = @_;
1875 $key = "all_business_types" unless ($key);
1877 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1879 $main::lxdebug->leave_sub();
1882 sub _get_languages {
1883 $main::lxdebug->enter_sub();
1885 my ($self, $dbh, $key) = @_;
1887 $key = "all_languages" unless ($key);
1889 my $query = qq|SELECT * FROM language ORDER BY id|;
1891 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1893 $main::lxdebug->leave_sub();
1896 sub _get_dunning_configs {
1897 $main::lxdebug->enter_sub();
1899 my ($self, $dbh, $key) = @_;
1901 $key = "all_dunning_configs" unless ($key);
1903 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
1905 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1907 $main::lxdebug->leave_sub();
1910 sub _get_currencies {
1911 $main::lxdebug->enter_sub();
1913 my ($self, $dbh, $key) = @_;
1915 $key = "all_currencies" unless ($key);
1917 my $query = qq|SELECT curr AS currency FROM defaults|;
1919 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
1921 $main::lxdebug->leave_sub();
1925 $main::lxdebug->enter_sub();
1927 my ($self, $dbh, $key) = @_;
1929 $key = "all_payments" unless ($key);
1931 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
1933 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1935 $main::lxdebug->leave_sub();
1938 sub _get_customers {
1939 $main::lxdebug->enter_sub();
1941 my ($self, $dbh, $key, $limit) = @_;
1943 $key = "all_customers" unless ($key);
1944 $limit_clause = "LIMIT $limit" if $limit;
1946 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
1948 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1950 $main::lxdebug->leave_sub();
1954 $main::lxdebug->enter_sub();
1956 my ($self, $dbh, $key) = @_;
1958 $key = "all_vendors" unless ($key);
1960 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
1962 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1964 $main::lxdebug->leave_sub();
1967 sub _get_departments {
1968 $main::lxdebug->enter_sub();
1970 my ($self, $dbh, $key) = @_;
1972 $key = "all_departments" unless ($key);
1974 my $query = qq|SELECT * FROM department ORDER BY description|;
1976 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1978 $main::lxdebug->leave_sub();
1981 sub _get_price_factors {
1982 $main::lxdebug->enter_sub();
1984 my ($self, $dbh, $key) = @_;
1986 $key ||= "all_price_factors";
1988 my $query = qq|SELECT * FROM price_factors ORDER BY sortkey|;
1990 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1992 $main::lxdebug->leave_sub();
1996 $main::lxdebug->enter_sub();
2001 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2002 my ($sth, $query, $ref);
2004 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2005 my $vc_id = $self->{"${vc}_id"};
2007 if ($params{"contacts"}) {
2008 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2011 if ($params{"shipto"}) {
2012 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2015 if ($params{"projects"} || $params{"all_projects"}) {
2016 $self->_get_projects($dbh, $params{"all_projects"} ?
2017 $params{"all_projects"} : $params{"projects"},
2018 $params{"all_projects"} ? 1 : 0);
2021 if ($params{"printers"}) {
2022 $self->_get_printers($dbh, $params{"printers"});
2025 if ($params{"languages"}) {
2026 $self->_get_languages($dbh, $params{"languages"});
2029 if ($params{"charts"}) {
2030 $self->_get_charts($dbh, $params{"charts"});
2033 if ($params{"taxcharts"}) {
2034 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2037 if ($params{"taxzones"}) {
2038 $self->_get_taxzones($dbh, $params{"taxzones"});
2041 if ($params{"employees"}) {
2042 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2045 if ($params{"salesmen"}) {
2046 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2049 if ($params{"business_types"}) {
2050 $self->_get_business_types($dbh, $params{"business_types"});
2053 if ($params{"dunning_configs"}) {
2054 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2057 if($params{"currencies"}) {
2058 $self->_get_currencies($dbh, $params{"currencies"});
2061 if($params{"customers"}) {
2062 if (ref $params{"customers"} eq 'HASH') {
2063 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2065 $self->_get_customers($dbh, $params{"customers"});
2069 if($params{"vendors"}) {
2070 if (ref $params{"vendors"} eq 'HASH') {
2071 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2073 $self->_get_vendors($dbh, $params{"vendors"});
2077 if($params{"payments"}) {
2078 $self->_get_payments($dbh, $params{"payments"});
2081 if($params{"departments"}) {
2082 $self->_get_departments($dbh, $params{"departments"});
2085 if ($params{price_factors}) {
2086 $self->_get_price_factors($dbh, $params{price_factors});
2089 $main::lxdebug->leave_sub();
2092 # this sub gets the id and name from $table
2094 $main::lxdebug->enter_sub();
2096 my ($self, $myconfig, $table) = @_;
2098 # connect to database
2099 my $dbh = $self->get_standard_dbh($myconfig);
2101 $table = $table eq "customer" ? "customer" : "vendor";
2102 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2104 my ($query, @values);
2106 if (!$self->{openinvoices}) {
2108 if ($self->{customernumber} ne "") {
2109 $where = qq|(vc.customernumber ILIKE ?)|;
2110 push(@values, '%' . $self->{customernumber} . '%');
2112 $where = qq|(vc.name ILIKE ?)|;
2113 push(@values, '%' . $self->{$table} . '%');
2117 qq~SELECT vc.id, vc.name,
2118 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2120 WHERE $where AND (NOT vc.obsolete)
2124 qq~SELECT DISTINCT vc.id, vc.name,
2125 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2127 JOIN $table vc ON (a.${table}_id = vc.id)
2128 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2130 push(@values, '%' . $self->{$table} . '%');
2133 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2135 $main::lxdebug->leave_sub();
2137 return scalar(@{ $self->{name_list} });
2140 # the selection sub is used in the AR, AP, IS, IR and OE module
2143 $main::lxdebug->enter_sub();
2145 my ($self, $myconfig, $table, $module) = @_;
2148 my $dbh = $self->get_standard_dbh($myconfig);
2150 $table = $table eq "customer" ? "customer" : "vendor";
2152 my $query = qq|SELECT count(*) FROM $table|;
2153 my ($count) = selectrow_query($self, $dbh, $query);
2155 # build selection list
2156 if ($count < $myconfig->{vclimit}) {
2157 $query = qq|SELECT id, name, salesman_id
2158 FROM $table WHERE NOT obsolete
2160 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2164 $self->get_employee($dbh);
2166 # setup sales contacts
2167 $query = qq|SELECT e.id, e.name
2169 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2170 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2173 push(@{ $self->{all_employees} },
2174 { id => $self->{employee_id},
2175 name => $self->{employee} });
2177 # sort the whole thing
2178 @{ $self->{all_employees} } =
2179 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2181 if ($module eq 'AR') {
2183 # prepare query for departments
2184 $query = qq|SELECT id, description
2187 ORDER BY description|;
2190 $query = qq|SELECT id, description
2192 ORDER BY description|;
2195 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2198 $query = qq|SELECT id, description
2202 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2205 $query = qq|SELECT printer_description, id
2207 ORDER BY printer_description|;
2209 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2212 $query = qq|SELECT id, description
2216 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2218 $main::lxdebug->leave_sub();
2221 sub language_payment {
2222 $main::lxdebug->enter_sub();
2224 my ($self, $myconfig) = @_;
2226 my $dbh = $self->get_standard_dbh($myconfig);
2228 my $query = qq|SELECT id, description
2232 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2235 $query = qq|SELECT printer_description, id
2237 ORDER BY printer_description|;
2239 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2242 $query = qq|SELECT id, description
2246 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2248 # get buchungsgruppen
2249 $query = qq|SELECT id, description
2250 FROM buchungsgruppen|;
2252 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2254 $main::lxdebug->leave_sub();
2257 # this is only used for reports
2258 sub all_departments {
2259 $main::lxdebug->enter_sub();
2261 my ($self, $myconfig, $table) = @_;
2263 my $dbh = $self->get_standard_dbh($myconfig);
2266 if ($table eq 'customer') {
2267 $where = "WHERE role = 'P' ";
2270 my $query = qq|SELECT id, description
2273 ORDER BY description|;
2274 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2276 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2278 $main::lxdebug->leave_sub();
2282 $main::lxdebug->enter_sub();
2284 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2287 if ($table eq "customer") {
2296 $self->all_vc($myconfig, $table, $module);
2298 # get last customers or vendors
2299 my ($query, $sth, $ref);
2301 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2306 my $transdate = "current_date";
2307 if ($self->{transdate}) {
2308 $transdate = $dbh->quote($self->{transdate});
2311 # now get the account numbers
2312 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2313 FROM chart c, taxkeys tk
2314 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2315 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2318 $sth = $dbh->prepare($query);
2320 do_statement($self, $sth, $query, '%' . $module . '%');
2322 $self->{accounts} = "";
2323 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2325 foreach my $key (split(/:/, $ref->{link})) {
2326 if ($key =~ /\Q$module\E/) {
2328 # cross reference for keys
2329 $xkeyref{ $ref->{accno} } = $key;
2331 push @{ $self->{"${module}_links"}{$key} },
2332 { accno => $ref->{accno},
2333 description => $ref->{description},
2334 taxkey => $ref->{taxkey_id},
2335 tax_id => $ref->{tax_id} };
2337 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2343 # get taxkeys and description
2344 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2345 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2347 if (($module eq "AP") || ($module eq "AR")) {
2348 # get tax rates and description
2349 $query = qq|SELECT * FROM tax|;
2350 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2356 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2357 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2358 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2359 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2361 d.description AS department,
2364 JOIN $table c ON (a.${table}_id = c.id)
2365 LEFT JOIN employee e ON (e.id = a.employee_id)
2366 LEFT JOIN department d ON (d.id = a.department_id)
2368 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2370 foreach $key (keys %$ref) {
2371 $self->{$key} = $ref->{$key};
2374 my $transdate = "current_date";
2375 if ($self->{transdate}) {
2376 $transdate = $dbh->quote($self->{transdate});
2379 # now get the account numbers
2380 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2382 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2384 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2385 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2388 $sth = $dbh->prepare($query);
2389 do_statement($self, $sth, $query, "%$module%");
2391 $self->{accounts} = "";
2392 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2394 foreach my $key (split(/:/, $ref->{link})) {
2395 if ($key =~ /\Q$module\E/) {
2397 # cross reference for keys
2398 $xkeyref{ $ref->{accno} } = $key;
2400 push @{ $self->{"${module}_links"}{$key} },
2401 { accno => $ref->{accno},
2402 description => $ref->{description},
2403 taxkey => $ref->{taxkey_id},
2404 tax_id => $ref->{tax_id} };
2406 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2412 # get amounts from individual entries
2415 c.accno, c.description,
2416 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2420 LEFT JOIN chart c ON (c.id = a.chart_id)
2421 LEFT JOIN project p ON (p.id = a.project_id)
2422 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2423 WHERE (tk.taxkey_id=a.taxkey) AND
2424 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2425 THEN tk.chart_id = a.chart_id
2428 OR (c.link='%tax%')) AND
2429 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2430 WHERE a.trans_id = ?
2431 AND a.fx_transaction = '0'
2432 ORDER BY a.oid, a.transdate|;
2433 $sth = $dbh->prepare($query);
2434 do_statement($self, $sth, $query, $self->{id});
2436 # get exchangerate for currency
2437 $self->{exchangerate} =
2438 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2441 # store amounts in {acc_trans}{$key} for multiple accounts
2442 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2443 $ref->{exchangerate} =
2444 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2445 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2448 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2449 $ref->{amount} *= -1;
2451 $ref->{index} = $index;
2453 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2459 d.curr AS currencies, d.closedto, d.revtrans,
2460 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2461 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2463 $ref = selectfirst_hashref_query($self, $dbh, $query);
2464 map { $self->{$_} = $ref->{$_} } keys %$ref;
2471 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2472 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2473 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2475 $ref = selectfirst_hashref_query($self, $dbh, $query);
2476 map { $self->{$_} = $ref->{$_} } keys %$ref;
2478 if ($self->{"$self->{vc}_id"}) {
2480 # only setup currency
2481 ($self->{currency}) = split(/:/, $self->{currencies});
2485 $self->lastname_used($dbh, $myconfig, $table, $module);
2487 # get exchangerate for currency
2488 $self->{exchangerate} =
2489 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2495 $main::lxdebug->leave_sub();
2499 $main::lxdebug->enter_sub();
2501 my ($self, $dbh, $myconfig, $table, $module) = @_;
2503 my $arap = ($table eq 'customer') ? "ar" : "ap";
2504 $table = $table eq "customer" ? "customer" : "vendor";
2505 my $where = "1 = 1";
2507 if ($self->{type} =~ /_order/) {
2509 $where = "quotation = '0'";
2511 if ($self->{type} =~ /_quotation/) {
2513 $where = "quotation = '1'";
2516 my $query = qq|SELECT MAX(id) FROM $arap
2517 WHERE $where AND ${table}_id > 0|;
2518 my ($trans_id) = selectrow_query($self, $dbh, $query);
2523 a.curr, a.${table}_id, a.department_id,
2524 d.description AS department,
2525 ct.name, current_date + ct.terms AS duedate
2527 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2528 LEFT JOIN department d ON (a.department_id = d.id)
2530 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2531 $self->{department}, $self->{$table}, $self->{duedate})
2532 = selectrow_query($self, $dbh, $query, $trans_id);
2534 $main::lxdebug->leave_sub();
2538 $main::lxdebug->enter_sub();
2540 my ($self, $myconfig, $thisdate, $days) = @_;
2542 my $dbh = $self->get_standard_dbh($myconfig);
2547 my $dateformat = $myconfig->{dateformat};
2548 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2549 $thisdate = $dbh->quote($thisdate);
2550 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2552 $query = qq|SELECT current_date AS thisdate|;
2555 ($thisdate) = selectrow_query($self, $dbh, $query);
2557 $main::lxdebug->leave_sub();
2563 $main::lxdebug->enter_sub();
2565 my ($self, $string) = @_;
2567 if ($string !~ /%/) {
2568 $string = "%$string%";
2571 $string =~ s/\'/\'\'/g;
2573 $main::lxdebug->leave_sub();
2579 $main::lxdebug->enter_sub();
2581 my ($self, $flds, $new, $count, $numrows) = @_;
2585 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2590 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2592 $j = $item->{ndx} - 1;
2593 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2597 for $i ($count + 1 .. $numrows) {
2598 map { delete $self->{"${_}_$i"} } @{$flds};
2601 $main::lxdebug->leave_sub();
2605 $main::lxdebug->enter_sub();
2607 my ($self, $myconfig) = @_;
2611 my $dbh = $self->dbconnect_noauto($myconfig);
2613 my $query = qq|DELETE FROM status
2614 WHERE (formname = ?) AND (trans_id = ?)|;
2615 my $sth = prepare_query($self, $dbh, $query);
2617 if ($self->{formname} =~ /(check|receipt)/) {
2618 for $i (1 .. $self->{rowcount}) {
2619 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2622 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2626 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2627 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2629 my %queued = split / /, $self->{queued};
2632 if ($self->{formname} =~ /(check|receipt)/) {
2634 # this is a check or receipt, add one entry for each lineitem
2635 my ($accno) = split /--/, $self->{account};
2636 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2637 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2638 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2639 $sth = prepare_query($self, $dbh, $query);
2641 for $i (1 .. $self->{rowcount}) {
2642 if ($self->{"checked_$i"}) {
2643 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2649 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2650 VALUES (?, ?, ?, ?, ?)|;
2651 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2652 $queued{$self->{formname}}, $self->{formname});
2658 $main::lxdebug->leave_sub();
2662 $main::lxdebug->enter_sub();
2664 my ($self, $dbh) = @_;
2666 my ($query, $printed, $emailed);
2668 my $formnames = $self->{printed};
2669 my $emailforms = $self->{emailed};
2671 $query = qq|DELETE FROM status
2672 WHERE (formname = ?) AND (trans_id = ?)|;
2673 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2675 # this only applies to the forms
2676 # checks and receipts are posted when printed or queued
2678 if ($self->{queued}) {
2679 my %queued = split / /, $self->{queued};
2681 foreach my $formname (keys %queued) {
2682 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2683 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2685 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2686 VALUES (?, ?, ?, ?, ?)|;
2687 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2689 $formnames =~ s/\Q$self->{formname}\E//;
2690 $emailforms =~ s/\Q$self->{formname}\E//;
2695 # save printed, emailed info
2696 $formnames =~ s/^ +//g;
2697 $emailforms =~ s/^ +//g;
2700 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2701 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2703 foreach my $formname (keys %status) {
2704 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2705 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2707 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2708 VALUES (?, ?, ?, ?)|;
2709 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2712 $main::lxdebug->leave_sub();
2716 # $main::locale->text('SAVED')
2717 # $main::locale->text('DELETED')
2718 # $main::locale->text('ADDED')
2719 # $main::locale->text('PAYMENT POSTED')
2720 # $main::locale->text('POSTED')
2721 # $main::locale->text('POSTED AS NEW')
2722 # $main::locale->text('ELSE')
2723 # $main::locale->text('SAVED FOR DUNNING')
2724 # $main::locale->text('DUNNING STARTED')
2725 # $main::locale->text('PRINTED')
2726 # $main::locale->text('MAILED')
2727 # $main::locale->text('SCREENED')
2728 # $main::locale->text('CANCELED')
2729 # $main::locale->text('invoice')
2730 # $main::locale->text('proforma')
2731 # $main::locale->text('sales_order')
2732 # $main::locale->text('packing_list')
2733 # $main::locale->text('pick_list')
2734 # $main::locale->text('purchase_order')
2735 # $main::locale->text('bin_list')
2736 # $main::locale->text('sales_quotation')
2737 # $main::locale->text('request_quotation')
2740 $main::lxdebug->enter_sub();
2745 if(!exists $self->{employee_id}) {
2746 &get_employee($self, $dbh);
2750 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2751 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2752 my @values = (conv_i($self->{id}), $self->{login},
2753 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2754 do_query($self, $dbh, $query, @values);
2756 $main::lxdebug->leave_sub();
2760 $main::lxdebug->enter_sub();
2762 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2763 my ($orderBy, $desc) = split(/\-\-/, $order);
2764 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2767 if ($trans_id ne "") {
2769 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 | .
2770 qq|FROM history_erp h | .
2771 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2772 qq|WHERE trans_id = | . $trans_id
2773 . $restriction . qq| |
2776 my $sth = $dbh->prepare($query) || $self->dberror($query);
2778 $sth->execute() || $self->dberror("$query");
2780 while(my $hash_ref = $sth->fetchrow_hashref()) {
2781 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2782 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2783 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2784 $tempArray[$i++] = $hash_ref;
2786 $main::lxdebug->leave_sub() and return \@tempArray
2787 if ($i > 0 && $tempArray[0] ne "");
2789 $main::lxdebug->leave_sub();
2793 sub update_defaults {
2794 $main::lxdebug->enter_sub();
2796 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2799 if ($provided_dbh) {
2800 $dbh = $provided_dbh;
2802 $dbh = $self->dbconnect_noauto($myconfig);
2804 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2805 my $sth = $dbh->prepare($query);
2807 $sth->execute || $self->dberror($query);
2808 my ($var) = $sth->fetchrow_array;
2811 if ($var =~ m/\d+$/) {
2812 my $new_var = (substr $var, $-[0]) * 1 + 1;
2813 my $len_diff = length($var) - $-[0] - length($new_var);
2814 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2820 $query = qq|UPDATE defaults SET $fld = ?|;
2821 do_query($self, $dbh, $query, $var);
2823 if (!$provided_dbh) {
2828 $main::lxdebug->leave_sub();
2833 sub update_business {
2834 $main::lxdebug->enter_sub();
2836 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
2839 if ($provided_dbh) {
2840 $dbh = $provided_dbh;
2842 $dbh = $self->dbconnect_noauto($myconfig);
2845 qq|SELECT customernumberinit FROM business
2846 WHERE id = ? FOR UPDATE|;
2847 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
2849 if ($var =~ m/\d+$/) {
2850 my $new_var = (substr $var, $-[0]) * 1 + 1;
2851 my $len_diff = length($var) - $-[0] - length($new_var);
2852 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
2858 $query = qq|UPDATE business
2859 SET customernumberinit = ?
2861 do_query($self, $dbh, $query, $var, $business_id);
2863 if (!$provided_dbh) {
2868 $main::lxdebug->leave_sub();
2873 sub get_partsgroup {
2874 $main::lxdebug->enter_sub();
2876 my ($self, $myconfig, $p) = @_;
2878 my $dbh = $self->get_standard_dbh($myconfig);
2880 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2882 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2885 if ($p->{searchitems} eq 'part') {
2886 $query .= qq|WHERE p.inventory_accno_id > 0|;
2888 if ($p->{searchitems} eq 'service') {
2889 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
2891 if ($p->{searchitems} eq 'assembly') {
2892 $query .= qq|WHERE p.assembly = '1'|;
2894 if ($p->{searchitems} eq 'labor') {
2895 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
2898 $query .= qq|ORDER BY partsgroup|;
2901 $query = qq|SELECT id, partsgroup FROM partsgroup
2902 ORDER BY partsgroup|;
2905 if ($p->{language_code}) {
2906 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2907 t.description AS translation
2909 JOIN parts p ON (p.partsgroup_id = pg.id)
2910 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
2911 ORDER BY translation|;
2912 @values = ($p->{language_code});
2915 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
2917 $main::lxdebug->leave_sub();
2920 sub get_pricegroup {
2921 $main::lxdebug->enter_sub();
2923 my ($self, $myconfig, $p) = @_;
2925 my $dbh = $self->get_standard_dbh($myconfig);
2927 my $query = qq|SELECT p.id, p.pricegroup
2930 $query .= qq| ORDER BY pricegroup|;
2933 $query = qq|SELECT id, pricegroup FROM pricegroup
2934 ORDER BY pricegroup|;
2937 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
2939 $main::lxdebug->leave_sub();
2943 # usage $form->all_years($myconfig, [$dbh])
2944 # return list of all years where bookings found
2947 $main::lxdebug->enter_sub();
2949 my ($self, $myconfig, $dbh) = @_;
2951 $dbh ||= $self->get_standard_dbh($myconfig);
2954 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2955 (SELECT MAX(transdate) FROM acc_trans)|;
2956 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
2958 if ($myconfig->{dateformat} =~ /^yy/) {
2959 ($startdate) = split /\W/, $startdate;
2960 ($enddate) = split /\W/, $enddate;
2962 (@_) = split /\W/, $startdate;
2964 (@_) = split /\W/, $enddate;
2969 $startdate = substr($startdate,0,4);
2970 $enddate = substr($enddate,0,4);
2972 while ($enddate >= $startdate) {
2973 push @all_years, $enddate--;
2978 $main::lxdebug->leave_sub();