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 #======================================================================
59 use List::Util qw(first max min sum);
65 $standard_dbh->disconnect();
71 $main::lxdebug->enter_sub(2);
79 while ($key =~ /\[\+?\]\.|\./) {
80 substr($key, 0, $+[0]) = '';
88 if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
89 push @{ $curr->{$`} }, { };
92 $curr = $curr->{$`}->[-1];
96 $curr->{$key} = $value;
98 $main::lxdebug->leave_sub(2);
100 return \$curr->{$key};
104 $main::lxdebug->enter_sub(2);
109 my @pairs = split(/&/, $input);
112 my ($key, $value) = split(/=/, $_, 2);
113 $self->_store_value($self->unescape($key), $self->unescape($value));
116 $main::lxdebug->leave_sub(2);
119 sub _request_to_hash {
120 $main::lxdebug->enter_sub(2);
125 if (!$ENV{'CONTENT_TYPE'}
126 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
128 $self->_input_to_hash($input);
130 $main::lxdebug->leave_sub(2);
134 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
136 my $boundary = '--' . $1;
138 foreach my $line (split m/\n/, $input) {
139 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
141 if (($line eq $boundary) || ($line eq "$boundary\r")) {
142 ${ $previous } =~ s|\r?\n$|| if $previous;
148 $content_type = "text/plain";
155 next unless $boundary_found;
157 if (!$headers_done) {
158 $line =~ s/[\r\n]*$//;
165 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
166 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
168 substr $line, $-[0], $+[0] - $-[0], "";
171 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
173 substr $line, $-[0], $+[0] - $-[0], "";
176 $previous = $self->_store_value($name, '');
177 $self->{FILENAME} = $filename if ($filename);
182 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
189 next unless $previous;
191 ${ $previous } .= "${line}\n";
194 ${ $previous } =~ s|\r?\n$|| if $previous;
196 $main::lxdebug->leave_sub(2);
200 $main::lxdebug->enter_sub();
206 if ($LXDebug::watch_form) {
207 require SL::Watchdog;
208 tie %{ $self }, 'SL::Watchdog';
211 read(STDIN, $_, $ENV{CONTENT_LENGTH});
213 if ($ENV{QUERY_STRING}) {
214 $_ = $ENV{QUERY_STRING};
223 $self->_request_to_hash($_);
225 $self->{action} = lc $self->{action};
226 $self->{action} =~ s/( |-|,|\#)/_/g;
228 $self->{version} = "2.6.0 beta 1";
230 $main::lxdebug->leave_sub();
235 sub _flatten_variables_rec {
236 $main::lxdebug->enter_sub(2);
245 if ('' eq ref $curr->{$key}) {
246 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
248 } elsif ('HASH' eq ref $curr->{$key}) {
249 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
250 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
254 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
255 my $first_array_entry = 1;
257 foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
258 push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
259 $first_array_entry = 0;
264 $main::lxdebug->leave_sub(2);
269 sub flatten_variables {
270 $main::lxdebug->enter_sub(2);
278 push @variables, $self->_flatten_variables_rec($self, '', $_);
281 $main::lxdebug->leave_sub(2);
286 sub flatten_standard_variables {
287 $main::lxdebug->enter_sub(2);
290 my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
294 foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
295 push @variables, $self->_flatten_variables_rec($self, '', $_);
298 $main::lxdebug->leave_sub(2);
304 $main::lxdebug->enter_sub();
310 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
312 $main::lxdebug->leave_sub();
316 $main::lxdebug->enter_sub(2);
319 my $password = $self->{password};
321 $self->{password} = 'X' x 8;
323 local $Data::Dumper::Sortkeys = 1;
324 my $output = Dumper($self);
326 $self->{password} = $password;
328 $main::lxdebug->leave_sub(2);
334 $main::lxdebug->enter_sub(2);
336 my ($self, $str) = @_;
338 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
340 $main::lxdebug->leave_sub(2);
346 $main::lxdebug->enter_sub(2);
348 my ($self, $str) = @_;
353 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
355 $main::lxdebug->leave_sub(2);
361 my ($self, $str) = @_;
363 if ($str && !ref($str)) {
364 $str =~ s/\"/"/g;
372 my ($self, $str) = @_;
374 if ($str && !ref($str)) {
375 $str =~ s/"/\"/g;
386 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
388 for (sort keys %$self) {
389 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
390 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
397 $main::lxdebug->enter_sub();
399 $main::lxdebug->show_backtrace();
401 my ($self, $msg) = @_;
402 if ($ENV{HTTP_USER_AGENT}) {
404 $self->show_generic_error($msg);
411 $main::lxdebug->leave_sub();
415 $main::lxdebug->enter_sub();
417 my ($self, $msg) = @_;
419 if ($ENV{HTTP_USER_AGENT}) {
422 if (!$self->{header}) {
435 if ($self->{info_function}) {
436 &{ $self->{info_function} }($msg);
442 $main::lxdebug->leave_sub();
445 # calculates the number of rows in a textarea based on the content and column number
446 # can be capped with maxrows
448 $main::lxdebug->enter_sub();
449 my ($self, $str, $cols, $maxrows, $minrows) = @_;
453 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
456 $main::lxdebug->leave_sub();
458 return max(min($rows, $maxrows), $minrows);
462 $main::lxdebug->enter_sub();
464 my ($self, $msg) = @_;
466 $self->error("$msg\n" . $DBI::errstr);
468 $main::lxdebug->leave_sub();
472 $main::lxdebug->enter_sub();
474 my ($self, $name, $msg) = @_;
477 foreach my $part (split m/\./, $name) {
478 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
481 $curr = $curr->{$part};
484 $main::lxdebug->leave_sub();
487 sub create_http_response {
488 $main::lxdebug->enter_sub();
493 my $cgi = $main::cgi;
494 $cgi ||= CGI->new('');
498 if ($ENV{HTTP_X_FORWARDED_FOR}) {
499 $base_path = $ENV{HTTP_REFERER};
500 $base_path =~ s|^.*?://.*?/|/|;
502 $base_path = $ENV{REQUEST_URI};
504 $base_path =~ s|[^/]+$||;
505 $base_path =~ s|/$||;
508 if (defined $main::auth) {
509 my $session_cookie_value = $main::auth->get_session_id();
510 $session_cookie_value ||= 'NO_SESSION';
512 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
513 '-value' => $session_cookie_value,
514 '-path' => $base_path);
517 my %cgi_params = ('-type' => $params{content_type});
518 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
520 my $output = $cgi->header('-cookie' => $session_cookie,
523 $main::lxdebug->leave_sub();
530 $main::lxdebug->enter_sub();
532 my ($self, $extra_code) = @_;
534 if ($self->{header}) {
535 $main::lxdebug->leave_sub();
539 my ($stylesheet, $favicon, $pagelayout);
541 if ($ENV{HTTP_USER_AGENT}) {
544 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
545 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
546 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
549 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
551 $stylesheets =~ s|^\s*||;
552 $stylesheets =~ s|\s*$||;
553 foreach my $file (split m/\s+/, $stylesheets) {
555 next if (! -f "css/$file");
557 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
560 $self->{favicon} = "favicon.ico" unless $self->{favicon};
562 if ($self->{favicon} && (-f "$self->{favicon}")) {
564 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
568 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
570 if ($self->{landscape}) {
571 $pagelayout = qq|<style type="text/css">
572 \@page { size:landscape; }
576 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
580 if ($self->{jsscript} == 1) {
583 <script type="text/javascript" src="js/common.js"></script>
584 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
585 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
586 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
587 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
594 ? "$self->{title} - $self->{titlebar}"
597 foreach my $item (@ { $self->{AJAX} }) {
598 $ajax .= $item->show_javascript();
601 print $self->create_http_response('content_type' => 'text/html',
602 'charset' => $db_charset,);
603 print qq|${doctype}<html>
605 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
606 <title>$self->{titlebar}</title>
613 <script type="text/javascript">
621 <meta name="robots" content="noindex,nofollow" />
622 <script type="text/javascript" src="js/highlight_input.js"></script>
624 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
625 <script type="text/javascript" src="js/tabcontent.js">
627 /***********************************************
628 * Tab Content script v2.2- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
629 * This notice MUST stay intact for legal use
630 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
631 ***********************************************/
642 $main::lxdebug->leave_sub();
645 sub ajax_response_header {
646 $main::lxdebug->enter_sub();
650 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
651 my $cgi = $main::cgi || CGI->new('');
652 my $output = $cgi->header('-charset' => $db_charset);
654 $main::lxdebug->leave_sub();
659 sub _prepare_html_template {
660 $main::lxdebug->enter_sub();
662 my ($self, $file, $additional_params) = @_;
665 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
666 $language = $main::language;
668 $language = $main::myconfig{"countrycode"};
670 $language = "de" unless ($language);
672 if (-f "templates/webpages/${file}_${language}.html") {
673 if ((-f ".developer") &&
674 (-f "templates/webpages/${file}_master.html") &&
675 ((stat("templates/webpages/${file}_master.html"))[9] >
676 (stat("templates/webpages/${file}_${language}.html"))[9])) {
677 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
678 "Please re-run 'locales.pl' in 'locale/${language}'.";
679 print(qq|<pre>$info</pre>|);
683 $file = "templates/webpages/${file}_${language}.html";
684 } elsif (-f "templates/webpages/${file}.html") {
685 $file = "templates/webpages/${file}.html";
687 my $info = "Web page template '${file}' not found.\n" .
688 "Please re-run 'locales.pl' in 'locale/${language}'.";
689 print(qq|<pre>$info</pre>|);
693 if ($self->{"DEBUG"}) {
694 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
697 if ($additional_params->{"DEBUG"}) {
698 $additional_params->{"DEBUG"} =
699 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
702 if (%main::myconfig) {
703 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
704 my $jsc_dateformat = $main::myconfig{"dateformat"};
705 $jsc_dateformat =~ s/d+/\%d/gi;
706 $jsc_dateformat =~ s/m+/\%m/gi;
707 $jsc_dateformat =~ s/y+/\%Y/gi;
708 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
711 $additional_params->{"conf_dbcharset"} = $main::dbcharset;
712 $additional_params->{"conf_webdav"} = $main::webdav;
713 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
714 $additional_params->{"conf_latex_templates"} = $main::latex;
715 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
717 if (%main::debug_options) {
718 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
721 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
722 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
723 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
727 $main::lxdebug->leave_sub();
732 sub parse_html_template {
733 $main::lxdebug->enter_sub();
735 my ($self, $file, $additional_params) = @_;
737 $additional_params ||= { };
739 $file = $self->_prepare_html_template($file, $additional_params);
741 my $template = Template->new({ 'INTERPOLATE' => 0,
745 'PLUGIN_BASE' => 'SL::Template::Plugin',
746 'INCLUDE_PATH' => '.:templates/webpages',
749 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
751 my $in = IO::File->new($file, 'r');
754 print STDERR "Error opening template file: $!";
755 $main::lxdebug->leave_sub();
759 my $input = join('', <$in>);
763 $input = $main::locale->{iconv}->convert($input);
767 if (!$template->process(\$input, $additional_params, \$output)) {
768 print STDERR $template->error();
771 $main::lxdebug->leave_sub();
776 sub show_generic_error {
777 $main::lxdebug->enter_sub();
779 my ($self, $error, %params) = @_;
782 'title_error' => $params{title},
783 'label_error' => $error,
786 if ($params{action}) {
789 map { delete($self->{$_}); } qw(action);
790 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
792 $add_params->{SHOW_BUTTON} = 1;
793 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
794 $add_params->{VARIABLES} = \@vars;
796 } elsif ($params{back_button}) {
797 $add_params->{SHOW_BACK_BUTTON} = 1;
800 $self->{title} = $params{title} if $params{title};
803 print $self->parse_html_template("generic/error", $add_params);
805 $main::lxdebug->leave_sub();
807 die("Error: $error\n");
810 sub show_generic_information {
811 $main::lxdebug->enter_sub();
813 my ($self, $text, $title) = @_;
816 'title_information' => $title,
817 'label_information' => $text,
820 $self->{title} = $title if ($title);
823 print $self->parse_html_template("generic/information", $add_params);
825 $main::lxdebug->leave_sub();
827 die("Information: $text\n");
830 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
831 # changed it to accept an arbitrary number of triggers - sschoeling
833 $main::lxdebug->enter_sub();
836 my $myconfig = shift;
839 # set dateform for jsscript
842 "dd.mm.yy" => "%d.%m.%Y",
843 "dd-mm-yy" => "%d-%m-%Y",
844 "dd/mm/yy" => "%d/%m/%Y",
845 "mm/dd/yy" => "%m/%d/%Y",
846 "mm-dd-yy" => "%m-%d-%Y",
847 "yyyy-mm-dd" => "%Y-%m-%d",
850 my $ifFormat = defined($dateformats{$myconfig->{"dateformat"}}) ?
851 $dateformats{$myconfig->{"dateformat"}} : "%d.%m.%Y";
858 inputField : "| . (shift) . qq|",
859 ifFormat :"$ifFormat",
860 align : "| . (shift) . qq|",
861 button : "| . (shift) . qq|"
867 <script type="text/javascript">
868 <!--| . join("", @triggers) . qq|//-->
872 $main::lxdebug->leave_sub();
875 } #end sub write_trigger
878 $main::lxdebug->enter_sub();
880 my ($self, $msg) = @_;
882 if ($self->{callback}) {
884 my ($script, $argv) = split(/\?/, $self->{callback}, 2);
886 $script =~ s|[^a-zA-Z0-9_\.]||g;
887 exec("perl", "$script", $argv);
895 $main::lxdebug->leave_sub();
898 # sort of columns removed - empty sub
900 $main::lxdebug->enter_sub();
902 my ($self, @columns) = @_;
904 $main::lxdebug->leave_sub();
910 $main::lxdebug->enter_sub(2);
912 my ($self, $myconfig, $amount, $places, $dash) = @_;
918 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
920 my $neg = ($amount =~ s/^-//);
921 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
923 if (defined($places) && ($places ne '')) {
929 my ($actual_places) = ($amount =~ /\.(\d+)/);
930 $actual_places = length($actual_places);
931 $places = $actual_places > $places ? $actual_places : $places;
934 $amount = $self->round_amount($amount, $places);
937 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
938 my @p = split(/\./, $amount); # split amount at decimal point
940 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
943 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
946 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
947 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
948 ($neg ? "-$amount" : "$amount" ) ;
952 $main::lxdebug->leave_sub(2);
956 sub format_amount_units {
957 $main::lxdebug->enter_sub();
962 my $myconfig = \%main::myconfig;
963 my $amount = $params{amount} * 1;
964 my $places = $params{places};
965 my $part_unit_name = $params{part_unit};
966 my $amount_unit_name = $params{amount_unit};
967 my $conv_units = $params{conv_units};
968 my $max_places = $params{max_places};
970 if (!$part_unit_name) {
971 $main::lxdebug->leave_sub();
975 AM->retrieve_all_units();
976 my $all_units = $main::all_units;
978 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
979 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
982 if (!scalar @{ $conv_units }) {
983 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
984 $main::lxdebug->leave_sub();
988 my $part_unit = $all_units->{$part_unit_name};
989 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
991 $amount *= $conv_unit->{factor};
996 foreach my $unit (@$conv_units) {
997 my $last = $unit->{name} eq $part_unit->{name};
999 $num = int($amount / $unit->{factor});
1000 $amount -= $num * $unit->{factor};
1003 if ($last ? $amount : $num) {
1004 push @values, { "unit" => $unit->{name},
1005 "amount" => $last ? $amount / $unit->{factor} : $num,
1006 "places" => $last ? $places : 0 };
1013 push @values, { "unit" => $part_unit_name,
1018 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
1020 $main::lxdebug->leave_sub();
1026 $main::lxdebug->enter_sub(2);
1031 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
1032 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
1033 $input =~ s/\#\#/\#/g;
1035 $main::lxdebug->leave_sub(2);
1043 $main::lxdebug->enter_sub(2);
1045 my ($self, $myconfig, $amount) = @_;
1047 if ( ($myconfig->{numberformat} eq '1.000,00')
1048 || ($myconfig->{numberformat} eq '1000,00')) {
1053 if ($myconfig->{numberformat} eq "1'000.00") {
1059 $main::lxdebug->leave_sub(2);
1061 return ($amount * 1);
1065 $main::lxdebug->enter_sub(2);
1067 my ($self, $amount, $places) = @_;
1070 # Rounding like "Kaufmannsrunden"
1071 # Descr. http://de.wikipedia.org/wiki/Rundung
1073 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
1076 $amount = $amount * (10**($places));
1077 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1079 $main::lxdebug->leave_sub(2);
1081 return $round_amount;
1085 sub parse_template {
1086 $main::lxdebug->enter_sub();
1088 my ($self, $myconfig, $userspath) = @_;
1089 my ($template, $out);
1093 $self->{"cwd"} = getcwd();
1094 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1096 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1097 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1098 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1099 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1100 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1101 } elsif (($self->{"format"} =~ /html/i) ||
1102 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1103 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1104 } elsif (($self->{"format"} =~ /xml/i) ||
1105 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1106 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1107 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
1108 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1109 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
1110 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1111 } elsif ( defined $self->{'format'}) {
1112 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1113 } elsif ( $self->{'format'} eq '' ) {
1114 $self->error("No Outputformat given: $self->{'format'}");
1115 } else { #Catch the rest
1116 $self->error("Outputformat not defined: $self->{'format'}");
1119 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1120 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1122 if (!$self->{employee_id}) {
1123 map { $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature company address businessnumber co_ustid taxnumber duns);
1126 map { $self->{"${_}"} = $myconfig->{$_}; } qw(co_ustid);
1128 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1130 # OUT is used for the media, screen, printer, email
1131 # for postscript we store a copy in a temporary file
1133 my $prepend_userspath;
1135 if (!$self->{tmpfile}) {
1136 $self->{tmpfile} = "${fileid}.$self->{IN}";
1137 $prepend_userspath = 1;
1140 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1142 $self->{tmpfile} =~ s|.*/||;
1143 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1144 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1146 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1147 $out = $self->{OUT};
1148 $self->{OUT} = ">$self->{tmpfile}";
1152 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
1154 open(OUT, ">-") or $self->error("STDOUT : $!");
1158 if (!$template->parse(*OUT)) {
1160 $self->error("$self->{IN} : " . $template->get_error());
1165 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1167 if ($self->{media} eq 'email') {
1169 my $mail = new Mailer;
1171 map { $mail->{$_} = $self->{$_} }
1172 qw(cc bcc subject message version format);
1173 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1174 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1175 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1176 $mail->{fileid} = "$fileid.";
1177 $myconfig->{signature} =~ s/\r//g;
1179 # if we send html or plain text inline
1180 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1181 $mail->{contenttype} = "text/html";
1183 $mail->{message} =~ s/\r//g;
1184 $mail->{message} =~ s/\n/<br>\n/g;
1185 $myconfig->{signature} =~ s/\n/<br>\n/g;
1186 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1188 open(IN, $self->{tmpfile})
1189 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1191 $mail->{message} .= $_;
1198 if (!$self->{"do_not_attach"}) {
1199 @{ $mail->{attachments} } =
1200 ({ "filename" => $self->{"tmpfile"},
1201 "name" => $self->{"attachment_filename"} ?
1202 $self->{"attachment_filename"} : $self->{"tmpfile"} });
1205 $mail->{message} =~ s/\r//g;
1206 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1210 my $err = $mail->send();
1211 $self->error($self->cleanup . "$err") if ($err);
1215 $self->{OUT} = $out;
1217 my $numbytes = (-s $self->{tmpfile});
1218 open(IN, $self->{tmpfile})
1219 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1221 $self->{copies} = 1 unless $self->{media} eq 'printer';
1223 chdir("$self->{cwd}");
1224 #print(STDERR "Kopien $self->{copies}\n");
1225 #print(STDERR "OUT $self->{OUT}\n");
1226 for my $i (1 .. $self->{copies}) {
1228 open(OUT, $self->{OUT})
1229 or $self->error($self->cleanup . "$self->{OUT} : $!");
1231 $self->{attachment_filename} = ($self->{attachment_filename})
1232 ? $self->{attachment_filename}
1233 : $self->generate_attachment_filename();
1235 # launch application
1236 print qq|Content-Type: | . $template->get_mime_type() . qq|
1237 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1238 Content-Length: $numbytes
1242 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1262 chdir("$self->{cwd}");
1263 $main::lxdebug->leave_sub();
1266 sub get_formname_translation {
1267 my ($self, $formname) = @_;
1269 $formname ||= $self->{formname};
1271 my %formname_translations = (
1272 bin_list => $main::locale->text('Bin List'),
1273 credit_note => $main::locale->text('Credit Note'),
1274 invoice => $main::locale->text('Invoice'),
1275 packing_list => $main::locale->text('Packing List'),
1276 pick_list => $main::locale->text('Pick List'),
1277 proforma => $main::locale->text('Proforma Invoice'),
1278 purchase_order => $main::locale->text('Purchase Order'),
1279 request_quotation => $main::locale->text('RFQ'),
1280 sales_order => $main::locale->text('Confirmation'),
1281 sales_quotation => $main::locale->text('Quotation'),
1282 storno_invoice => $main::locale->text('Storno Invoice'),
1283 storno_packing_list => $main::locale->text('Storno Packing List'),
1284 sales_delivery_order => $main::locale->text('Delivery Order'),
1285 purchase_delivery_order => $main::locale->text('Delivery Order'),
1288 return $formname_translations{$formname}
1291 sub get_number_prefix_for_type {
1295 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1296 : ($self->{type} =~ /_quotation$/) ? 'quo'
1297 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1303 sub get_extension_for_format {
1306 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1307 : $self->{format} =~ /postscript/i ? ".ps"
1308 : $self->{format} =~ /opendocument/i ? ".odt"
1309 : $self->{format} =~ /html/i ? ".html"
1315 sub generate_attachment_filename {
1318 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1319 my $prefix = $self->get_number_prefix_for_type();
1321 if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
1322 $attachment_filename .= ' (' . $main::locale->text('Preview') . ')' . $self->get_extension_for_format();
1324 } elsif ($attachment_filename && $self->{"${prefix}number"}) {
1325 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1328 $attachment_filename = "";
1331 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1332 $attachment_filename =~ s|[\s/\\]+|_|g;
1334 return $attachment_filename;
1337 sub generate_email_subject {
1340 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1341 my $prefix = $self->get_number_prefix_for_type();
1343 if ($subject && $self->{"${prefix}number"}) {
1344 $subject .= " " . $self->{"${prefix}number"}
1351 $main::lxdebug->enter_sub();
1355 chdir("$self->{tmpdir}");
1358 if (-f "$self->{tmpfile}.err") {
1359 open(FH, "$self->{tmpfile}.err");
1364 if ($self->{tmpfile}) {
1365 $self->{tmpfile} =~ s|.*/||g;
1367 $self->{tmpfile} =~ s/\.\w+$//g;
1368 my $tmpfile = $self->{tmpfile};
1369 unlink(<$tmpfile.*>);
1372 chdir("$self->{cwd}");
1374 $main::lxdebug->leave_sub();
1380 $main::lxdebug->enter_sub();
1382 my ($self, $date, $myconfig) = @_;
1385 if ($date && $date =~ /\D/) {
1387 if ($myconfig->{dateformat} =~ /^yy/) {
1388 ($yy, $mm, $dd) = split /\D/, $date;
1390 if ($myconfig->{dateformat} =~ /^mm/) {
1391 ($mm, $dd, $yy) = split /\D/, $date;
1393 if ($myconfig->{dateformat} =~ /^dd/) {
1394 ($dd, $mm, $yy) = split /\D/, $date;
1399 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1400 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1402 $dd = "0$dd" if ($dd < 10);
1403 $mm = "0$mm" if ($mm < 10);
1405 $date = "$yy$mm$dd";
1408 $main::lxdebug->leave_sub();
1413 # Database routines used throughout
1416 $main::lxdebug->enter_sub(2);
1418 my ($self, $myconfig) = @_;
1420 # connect to database
1422 DBI->connect($myconfig->{dbconnect},
1423 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1427 if ($myconfig->{dboptions}) {
1428 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1431 $main::lxdebug->leave_sub(2);
1436 sub dbconnect_noauto {
1437 $main::lxdebug->enter_sub();
1439 my ($self, $myconfig) = @_;
1441 # connect to database
1443 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1444 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1448 if ($myconfig->{dboptions}) {
1449 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1452 $main::lxdebug->leave_sub();
1457 sub get_standard_dbh {
1458 $main::lxdebug->enter_sub(2);
1460 my ($self, $myconfig) = @_;
1462 if ($standard_dbh && !$standard_dbh->{Active}) {
1463 $main::lxdebug->message(LXDebug::INFO, "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
1464 undef $standard_dbh;
1467 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1469 $main::lxdebug->leave_sub(2);
1471 return $standard_dbh;
1475 $main::lxdebug->enter_sub();
1477 my ($self, $date, $myconfig) = @_;
1478 my $dbh = $self->dbconnect($myconfig);
1480 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1481 my $sth = prepare_execute_query($self, $dbh, $query, $date);
1482 my ($closed) = $sth->fetchrow_array;
1484 $main::lxdebug->leave_sub();
1489 sub update_balance {
1490 $main::lxdebug->enter_sub();
1492 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1494 # if we have a value, go do it
1497 # retrieve balance from table
1498 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1499 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1500 my ($balance) = $sth->fetchrow_array;
1506 $query = "UPDATE $table SET $field = $balance WHERE $where";
1507 do_query($self, $dbh, $query, @values);
1509 $main::lxdebug->leave_sub();
1512 sub update_exchangerate {
1513 $main::lxdebug->enter_sub();
1515 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1517 # some sanity check for currency
1519 $main::lxdebug->leave_sub();
1522 $query = qq|SELECT curr FROM defaults|;
1524 my ($currency) = selectrow_query($self, $dbh, $query);
1525 my ($defaultcurrency) = split m/:/, $currency;
1528 if ($curr eq $defaultcurrency) {
1529 $main::lxdebug->leave_sub();
1533 $query = qq|SELECT e.curr FROM exchangerate e
1534 WHERE e.curr = ? AND e.transdate = ?
1536 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1545 $buy = conv_i($buy, "NULL");
1546 $sell = conv_i($sell, "NULL");
1549 if ($buy != 0 && $sell != 0) {
1550 $set = "buy = $buy, sell = $sell";
1551 } elsif ($buy != 0) {
1552 $set = "buy = $buy";
1553 } elsif ($sell != 0) {
1554 $set = "sell = $sell";
1557 if ($sth->fetchrow_array) {
1558 $query = qq|UPDATE exchangerate
1564 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1565 VALUES (?, $buy, $sell, ?)|;
1568 do_query($self, $dbh, $query, $curr, $transdate);
1570 $main::lxdebug->leave_sub();
1573 sub save_exchangerate {
1574 $main::lxdebug->enter_sub();
1576 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1578 my $dbh = $self->dbconnect($myconfig);
1582 $buy = $rate if $fld eq 'buy';
1583 $sell = $rate if $fld eq 'sell';
1586 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1591 $main::lxdebug->leave_sub();
1594 sub get_exchangerate {
1595 $main::lxdebug->enter_sub();
1597 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1600 unless ($transdate) {
1601 $main::lxdebug->leave_sub();
1605 $query = qq|SELECT curr FROM defaults|;
1607 my ($currency) = selectrow_query($self, $dbh, $query);
1608 my ($defaultcurrency) = split m/:/, $currency;
1610 if ($currency eq $defaultcurrency) {
1611 $main::lxdebug->leave_sub();
1615 $query = qq|SELECT e.$fld FROM exchangerate e
1616 WHERE e.curr = ? AND e.transdate = ?|;
1617 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1621 $main::lxdebug->leave_sub();
1623 return $exchangerate;
1626 sub check_exchangerate {
1627 $main::lxdebug->enter_sub();
1629 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1631 if ($fld !~/^buy|sell$/) {
1632 $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
1635 unless ($transdate) {
1636 $main::lxdebug->leave_sub();
1640 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1642 if ($currency eq $defaultcurrency) {
1643 $main::lxdebug->leave_sub();
1647 my $dbh = $self->get_standard_dbh($myconfig);
1648 my $query = qq|SELECT e.$fld FROM exchangerate e
1649 WHERE e.curr = ? AND e.transdate = ?|;
1651 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1653 $main::lxdebug->leave_sub();
1655 return $exchangerate;
1658 sub get_default_currency {
1659 $main::lxdebug->enter_sub();
1661 my ($self, $myconfig) = @_;
1662 my $dbh = $self->get_standard_dbh($myconfig);
1664 my $query = qq|SELECT curr FROM defaults|;
1666 my ($curr) = selectrow_query($self, $dbh, $query);
1667 my ($defaultcurrency) = split m/:/, $curr;
1669 $main::lxdebug->leave_sub();
1671 return $defaultcurrency;
1675 sub set_payment_options {
1676 $main::lxdebug->enter_sub();
1678 my ($self, $myconfig, $transdate) = @_;
1680 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1682 my $dbh = $self->get_standard_dbh($myconfig);
1685 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1686 qq|FROM payment_terms p | .
1689 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1690 $self->{payment_terms}) =
1691 selectrow_query($self, $dbh, $query, $self->{payment_id});
1693 if ($transdate eq "") {
1694 if ($self->{invdate}) {
1695 $transdate = $self->{invdate};
1697 $transdate = $self->{transdate};
1702 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1703 qq|FROM payment_terms|;
1704 ($self->{netto_date}, $self->{skonto_date}) =
1705 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1707 my ($invtotal, $total);
1708 my (%amounts, %formatted_amounts);
1710 if ($self->{type} =~ /_order$/) {
1711 $amounts{invtotal} = $self->{ordtotal};
1712 $amounts{total} = $self->{ordtotal};
1714 } elsif ($self->{type} =~ /_quotation$/) {
1715 $amounts{invtotal} = $self->{quototal};
1716 $amounts{total} = $self->{quototal};
1719 $amounts{invtotal} = $self->{invtotal};
1720 $amounts{total} = $self->{total};
1723 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1725 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1726 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1727 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1729 foreach (keys %amounts) {
1730 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1731 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1734 if ($self->{"language_id"}) {
1736 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1737 qq|FROM translation_payment_terms t | .
1738 qq|LEFT JOIN language l ON t.language_id = l.id | .
1739 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1740 my ($description_long, $output_numberformat, $output_dateformat,
1741 $output_longdates) =
1742 selectrow_query($self, $dbh, $query,
1743 $self->{"language_id"}, $self->{"payment_id"});
1745 $self->{payment_terms} = $description_long if ($description_long);
1747 if ($output_dateformat) {
1748 foreach my $key (qw(netto_date skonto_date)) {
1750 $main::locale->reformat_date($myconfig, $self->{$key},
1756 if ($output_numberformat &&
1757 ($output_numberformat ne $myconfig->{"numberformat"})) {
1758 my $saved_numberformat = $myconfig->{"numberformat"};
1759 $myconfig->{"numberformat"} = $output_numberformat;
1760 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1761 $myconfig->{"numberformat"} = $saved_numberformat;
1765 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1766 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1767 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1768 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1769 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1770 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1771 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1773 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1775 $main::lxdebug->leave_sub();
1779 sub get_template_language {
1780 $main::lxdebug->enter_sub();
1782 my ($self, $myconfig) = @_;
1784 my $template_code = "";
1786 if ($self->{language_id}) {
1787 my $dbh = $self->get_standard_dbh($myconfig);
1788 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1789 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1792 $main::lxdebug->leave_sub();
1794 return $template_code;
1797 sub get_printer_code {
1798 $main::lxdebug->enter_sub();
1800 my ($self, $myconfig) = @_;
1802 my $template_code = "";
1804 if ($self->{printer_id}) {
1805 my $dbh = $self->get_standard_dbh($myconfig);
1806 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1807 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1810 $main::lxdebug->leave_sub();
1812 return $template_code;
1816 $main::lxdebug->enter_sub();
1818 my ($self, $myconfig) = @_;
1820 my $template_code = "";
1822 if ($self->{shipto_id}) {
1823 my $dbh = $self->get_standard_dbh($myconfig);
1824 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1825 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1826 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1829 $main::lxdebug->leave_sub();
1833 $main::lxdebug->enter_sub();
1835 my ($self, $dbh, $id, $module) = @_;
1840 foreach my $item (qw(name department_1 department_2 street zipcode city country
1841 contact phone fax email)) {
1842 if ($self->{"shipto$item"}) {
1843 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1845 push(@values, $self->{"shipto${item}"});
1849 if ($self->{shipto_id}) {
1850 my $query = qq|UPDATE shipto set
1852 shiptodepartment_1 = ?,
1853 shiptodepartment_2 = ?,
1862 WHERE shipto_id = ?|;
1863 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1865 my $query = qq|SELECT * FROM shipto
1866 WHERE shiptoname = ? AND
1867 shiptodepartment_1 = ? AND
1868 shiptodepartment_2 = ? AND
1869 shiptostreet = ? AND
1870 shiptozipcode = ? AND
1872 shiptocountry = ? AND
1873 shiptocontact = ? AND
1879 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1882 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1883 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1884 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1885 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1886 do_query($self, $dbh, $query, $id, @values, $module);
1891 $main::lxdebug->leave_sub();
1895 $main::lxdebug->enter_sub();
1897 my ($self, $dbh) = @_;
1899 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1900 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1901 $self->{"employee_id"} *= 1;
1903 $main::lxdebug->leave_sub();
1906 sub get_employee_data {
1907 $main::lxdebug->enter_sub();
1912 Common::check_params(\%params, qw(prefix));
1913 Common::check_params_x(\%params, qw(id));
1916 $main::lxdebug->leave_sub();
1920 my $myconfig = \%main::myconfig;
1921 my $dbh = $params{dbh} || $self->get_standard_dbh($myconfig);
1923 my ($login) = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
1926 my $user = User->new($login);
1927 map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
1929 $self->{$params{prefix} . '_login'} = $login;
1930 $self->{$params{prefix} . '_name'} ||= $login;
1933 $main::lxdebug->leave_sub();
1937 $main::lxdebug->enter_sub();
1939 my ($self, $myconfig) = @_;
1941 my $dbh = $self->get_standard_dbh($myconfig);
1942 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1943 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1945 $main::lxdebug->leave_sub();
1949 $main::lxdebug->enter_sub();
1951 my ($self, $dbh, $id, $key) = @_;
1953 $key = "all_contacts" unless ($key);
1957 $main::lxdebug->leave_sub();
1962 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1963 qq|FROM contacts | .
1964 qq|WHERE cp_cv_id = ? | .
1965 qq|ORDER BY lower(cp_name)|;
1967 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1969 $main::lxdebug->leave_sub();
1973 $main::lxdebug->enter_sub();
1975 my ($self, $dbh, $key) = @_;
1977 my ($all, $old_id, $where, @values);
1979 if (ref($key) eq "HASH") {
1982 $key = "ALL_PROJECTS";
1984 foreach my $p (keys(%{$params})) {
1986 $all = $params->{$p};
1987 } elsif ($p eq "old_id") {
1988 $old_id = $params->{$p};
1989 } elsif ($p eq "key") {
1990 $key = $params->{$p};
1996 $where = "WHERE active ";
1998 if (ref($old_id) eq "ARRAY") {
1999 my @ids = grep({ $_ } @{$old_id});
2001 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
2002 push(@values, @ids);
2005 $where .= " OR (id = ?) ";
2006 push(@values, $old_id);
2012 qq|SELECT id, projectnumber, description, active | .
2015 qq|ORDER BY lower(projectnumber)|;
2017 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2019 $main::lxdebug->leave_sub();
2023 $main::lxdebug->enter_sub();
2025 my ($self, $dbh, $vc_id, $key) = @_;
2027 $key = "all_shipto" unless ($key);
2030 # get shipping addresses
2031 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
2033 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
2039 $main::lxdebug->leave_sub();
2043 $main::lxdebug->enter_sub();
2045 my ($self, $dbh, $key) = @_;
2047 $key = "all_printers" unless ($key);
2049 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
2051 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2053 $main::lxdebug->leave_sub();
2057 $main::lxdebug->enter_sub();
2059 my ($self, $dbh, $params) = @_;
2062 $key = $params->{key};
2063 $key = "all_charts" unless ($key);
2065 my $transdate = quote_db_date($params->{transdate});
2068 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
2070 qq|LEFT JOIN taxkeys tk ON | .
2071 qq|(tk.id = (SELECT id FROM taxkeys | .
2072 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2073 qq| ORDER BY startdate DESC LIMIT 1)) | .
2074 qq|ORDER BY c.accno|;
2076 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2078 $main::lxdebug->leave_sub();
2081 sub _get_taxcharts {
2082 $main::lxdebug->enter_sub();
2084 my ($self, $dbh, $params) = @_;
2086 my $key = "all_taxcharts";
2089 if (ref $params eq 'HASH') {
2090 $key = $params->{key} if ($params->{key});
2091 if ($params->{module} eq 'AR') {
2092 push @where, 'taxkey NOT IN (8, 9, 18, 19)';
2094 } elsif ($params->{module} eq 'AP') {
2095 push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
2102 my $where = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (@where);
2104 my $query = qq|SELECT * FROM tax $where ORDER BY taxkey|;
2106 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2108 $main::lxdebug->leave_sub();
2112 $main::lxdebug->enter_sub();
2114 my ($self, $dbh, $key) = @_;
2116 $key = "all_taxzones" unless ($key);
2118 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2120 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2122 $main::lxdebug->leave_sub();
2125 sub _get_employees {
2126 $main::lxdebug->enter_sub();
2128 my ($self, $dbh, $default_key, $key) = @_;
2130 $key = $default_key unless ($key);
2131 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2133 $main::lxdebug->leave_sub();
2136 sub _get_business_types {
2137 $main::lxdebug->enter_sub();
2139 my ($self, $dbh, $key) = @_;
2141 $key = "all_business_types" unless ($key);
2143 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
2145 $main::lxdebug->leave_sub();
2148 sub _get_languages {
2149 $main::lxdebug->enter_sub();
2151 my ($self, $dbh, $key) = @_;
2153 $key = "all_languages" unless ($key);
2155 my $query = qq|SELECT * FROM language ORDER BY id|;
2157 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2159 $main::lxdebug->leave_sub();
2162 sub _get_dunning_configs {
2163 $main::lxdebug->enter_sub();
2165 my ($self, $dbh, $key) = @_;
2167 $key = "all_dunning_configs" unless ($key);
2169 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2171 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2173 $main::lxdebug->leave_sub();
2176 sub _get_currencies {
2177 $main::lxdebug->enter_sub();
2179 my ($self, $dbh, $key) = @_;
2181 $key = "all_currencies" unless ($key);
2183 my $query = qq|SELECT curr AS currency FROM defaults|;
2185 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2187 $main::lxdebug->leave_sub();
2191 $main::lxdebug->enter_sub();
2193 my ($self, $dbh, $key) = @_;
2195 $key = "all_payments" unless ($key);
2197 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2199 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2201 $main::lxdebug->leave_sub();
2204 sub _get_customers {
2205 $main::lxdebug->enter_sub();
2207 my ($self, $dbh, $key, $limit) = @_;
2209 $key = "all_customers" unless ($key);
2210 my $limit_clause = "LIMIT $limit" if $limit;
2212 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
2214 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2216 $main::lxdebug->leave_sub();
2220 $main::lxdebug->enter_sub();
2222 my ($self, $dbh, $key) = @_;
2224 $key = "all_vendors" unless ($key);
2226 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2228 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2230 $main::lxdebug->leave_sub();
2233 sub _get_departments {
2234 $main::lxdebug->enter_sub();
2236 my ($self, $dbh, $key) = @_;
2238 $key = "all_departments" unless ($key);
2240 my $query = qq|SELECT * FROM department ORDER BY description|;
2242 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2244 $main::lxdebug->leave_sub();
2247 sub _get_warehouses {
2248 $main::lxdebug->enter_sub();
2250 my ($self, $dbh, $param) = @_;
2252 my ($key, $bins_key);
2254 if ('' eq ref $param) {
2258 $key = $param->{key};
2259 $bins_key = $param->{bins};
2262 my $query = qq|SELECT w.* FROM warehouse w
2263 WHERE (NOT w.invalid) AND
2264 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2265 ORDER BY w.sortkey|;
2267 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2270 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2271 my $sth = prepare_query($self, $dbh, $query);
2273 foreach my $warehouse (@{ $self->{$key} }) {
2274 do_statement($self, $sth, $query, $warehouse->{id});
2275 $warehouse->{$bins_key} = [];
2277 while (my $ref = $sth->fetchrow_hashref()) {
2278 push @{ $warehouse->{$bins_key} }, $ref;
2284 $main::lxdebug->leave_sub();
2288 $main::lxdebug->enter_sub();
2290 my ($self, $dbh, $table, $key, $sortkey) = @_;
2292 my $query = qq|SELECT * FROM $table|;
2293 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2295 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2297 $main::lxdebug->leave_sub();
2301 # $main::lxdebug->enter_sub();
2303 # my ($self, $dbh, $key) = @_;
2305 # $key ||= "all_groups";
2307 # my $groups = $main::auth->read_groups();
2309 # $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2311 # $main::lxdebug->leave_sub();
2315 $main::lxdebug->enter_sub();
2320 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2321 my ($sth, $query, $ref);
2323 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2324 my $vc_id = $self->{"${vc}_id"};
2326 if ($params{"contacts"}) {
2327 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2330 if ($params{"shipto"}) {
2331 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2334 if ($params{"projects"} || $params{"all_projects"}) {
2335 $self->_get_projects($dbh, $params{"all_projects"} ?
2336 $params{"all_projects"} : $params{"projects"},
2337 $params{"all_projects"} ? 1 : 0);
2340 if ($params{"printers"}) {
2341 $self->_get_printers($dbh, $params{"printers"});
2344 if ($params{"languages"}) {
2345 $self->_get_languages($dbh, $params{"languages"});
2348 if ($params{"charts"}) {
2349 $self->_get_charts($dbh, $params{"charts"});
2352 if ($params{"taxcharts"}) {
2353 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2356 if ($params{"taxzones"}) {
2357 $self->_get_taxzones($dbh, $params{"taxzones"});
2360 if ($params{"employees"}) {
2361 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2364 if ($params{"salesmen"}) {
2365 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2368 if ($params{"business_types"}) {
2369 $self->_get_business_types($dbh, $params{"business_types"});
2372 if ($params{"dunning_configs"}) {
2373 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2376 if($params{"currencies"}) {
2377 $self->_get_currencies($dbh, $params{"currencies"});
2380 if($params{"customers"}) {
2381 if (ref $params{"customers"} eq 'HASH') {
2382 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2384 $self->_get_customers($dbh, $params{"customers"});
2388 if($params{"vendors"}) {
2389 if (ref $params{"vendors"} eq 'HASH') {
2390 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2392 $self->_get_vendors($dbh, $params{"vendors"});
2396 if($params{"payments"}) {
2397 $self->_get_payments($dbh, $params{"payments"});
2400 if($params{"departments"}) {
2401 $self->_get_departments($dbh, $params{"departments"});
2404 if ($params{price_factors}) {
2405 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2408 if ($params{warehouses}) {
2409 $self->_get_warehouses($dbh, $params{warehouses});
2412 # if ($params{groups}) {
2413 # $self->_get_groups($dbh, $params{groups});
2416 if ($params{partsgroup}) {
2417 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2420 $main::lxdebug->leave_sub();
2423 # this sub gets the id and name from $table
2425 $main::lxdebug->enter_sub();
2427 my ($self, $myconfig, $table) = @_;
2429 # connect to database
2430 my $dbh = $self->get_standard_dbh($myconfig);
2432 $table = $table eq "customer" ? "customer" : "vendor";
2433 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2435 my ($query, @values);
2437 if (!$self->{openinvoices}) {
2439 if ($self->{customernumber} ne "") {
2440 $where = qq|(vc.customernumber ILIKE ?)|;
2441 push(@values, '%' . $self->{customernumber} . '%');
2443 $where = qq|(vc.name ILIKE ?)|;
2444 push(@values, '%' . $self->{$table} . '%');
2448 qq~SELECT vc.id, vc.name,
2449 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2451 WHERE $where AND (NOT vc.obsolete)
2455 qq~SELECT DISTINCT vc.id, vc.name,
2456 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2458 JOIN $table vc ON (a.${table}_id = vc.id)
2459 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2461 push(@values, '%' . $self->{$table} . '%');
2464 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2466 $main::lxdebug->leave_sub();
2468 return scalar(@{ $self->{name_list} });
2471 # the selection sub is used in the AR, AP, IS, IR and OE module
2474 $main::lxdebug->enter_sub();
2476 my ($self, $myconfig, $table, $module) = @_;
2479 my $dbh = $self->get_standard_dbh($myconfig);
2481 $table = $table eq "customer" ? "customer" : "vendor";
2483 my $query = qq|SELECT count(*) FROM $table|;
2484 my ($count) = selectrow_query($self, $dbh, $query);
2486 # build selection list
2487 if ($count < $myconfig->{vclimit}) {
2488 $query = qq|SELECT id, name, salesman_id
2489 FROM $table WHERE NOT obsolete
2491 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2495 $self->get_employee($dbh);
2497 # setup sales contacts
2498 $query = qq|SELECT e.id, e.name
2500 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2501 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2504 push(@{ $self->{all_employees} },
2505 { id => $self->{employee_id},
2506 name => $self->{employee} });
2508 # sort the whole thing
2509 @{ $self->{all_employees} } =
2510 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2512 if ($module eq 'AR') {
2514 # prepare query for departments
2515 $query = qq|SELECT id, description
2518 ORDER BY description|;
2521 $query = qq|SELECT id, description
2523 ORDER BY description|;
2526 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2529 $query = qq|SELECT id, description
2533 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2536 $query = qq|SELECT printer_description, id
2538 ORDER BY printer_description|;
2540 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2543 $query = qq|SELECT id, description
2547 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2549 $main::lxdebug->leave_sub();
2552 sub language_payment {
2553 $main::lxdebug->enter_sub();
2555 my ($self, $myconfig) = @_;
2557 my $dbh = $self->get_standard_dbh($myconfig);
2559 my $query = qq|SELECT id, description
2563 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2566 $query = qq|SELECT printer_description, id
2568 ORDER BY printer_description|;
2570 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2573 $query = qq|SELECT id, description
2577 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2579 # get buchungsgruppen
2580 $query = qq|SELECT id, description
2581 FROM buchungsgruppen|;
2583 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2585 $main::lxdebug->leave_sub();
2588 # this is only used for reports
2589 sub all_departments {
2590 $main::lxdebug->enter_sub();
2592 my ($self, $myconfig, $table) = @_;
2594 my $dbh = $self->get_standard_dbh($myconfig);
2597 if ($table eq 'customer') {
2598 $where = "WHERE role = 'P' ";
2601 my $query = qq|SELECT id, description
2604 ORDER BY description|;
2605 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2607 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2609 $main::lxdebug->leave_sub();
2613 $main::lxdebug->enter_sub();
2615 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2618 if ($table eq "customer") {
2627 $self->all_vc($myconfig, $table, $module);
2629 # get last customers or vendors
2630 my ($query, $sth, $ref);
2632 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2637 my $transdate = "current_date";
2638 if ($self->{transdate}) {
2639 $transdate = $dbh->quote($self->{transdate});
2642 # now get the account numbers
2643 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2644 FROM chart c, taxkeys tk
2645 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2646 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2649 $sth = $dbh->prepare($query);
2651 do_statement($self, $sth, $query, '%' . $module . '%');
2653 $self->{accounts} = "";
2654 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2656 foreach my $key (split(/:/, $ref->{link})) {
2657 if ($key =~ /\Q$module\E/) {
2659 # cross reference for keys
2660 $xkeyref{ $ref->{accno} } = $key;
2662 push @{ $self->{"${module}_links"}{$key} },
2663 { accno => $ref->{accno},
2664 description => $ref->{description},
2665 taxkey => $ref->{taxkey_id},
2666 tax_id => $ref->{tax_id} };
2668 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2674 # get taxkeys and description
2675 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2676 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2678 if (($module eq "AP") || ($module eq "AR")) {
2679 # get tax rates and description
2680 $query = qq|SELECT * FROM tax|;
2681 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2687 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2688 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2689 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2690 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2692 d.description AS department,
2695 JOIN $table c ON (a.${table}_id = c.id)
2696 LEFT JOIN employee e ON (e.id = a.employee_id)
2697 LEFT JOIN department d ON (d.id = a.department_id)
2699 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2701 foreach my $key (keys %$ref) {
2702 $self->{$key} = $ref->{$key};
2705 my $transdate = "current_date";
2706 if ($self->{transdate}) {
2707 $transdate = $dbh->quote($self->{transdate});
2710 # now get the account numbers
2711 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2713 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2715 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2716 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2719 $sth = $dbh->prepare($query);
2720 do_statement($self, $sth, $query, "%$module%");
2722 $self->{accounts} = "";
2723 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2725 foreach my $key (split(/:/, $ref->{link})) {
2726 if ($key =~ /\Q$module\E/) {
2728 # cross reference for keys
2729 $xkeyref{ $ref->{accno} } = $key;
2731 push @{ $self->{"${module}_links"}{$key} },
2732 { accno => $ref->{accno},
2733 description => $ref->{description},
2734 taxkey => $ref->{taxkey_id},
2735 tax_id => $ref->{tax_id} };
2737 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2743 # get amounts from individual entries
2746 c.accno, c.description,
2747 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2751 LEFT JOIN chart c ON (c.id = a.chart_id)
2752 LEFT JOIN project p ON (p.id = a.project_id)
2753 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2754 WHERE (tk.taxkey_id=a.taxkey) AND
2755 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2756 THEN tk.chart_id = a.chart_id
2759 OR (c.link='%tax%')) AND
2760 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2761 WHERE a.trans_id = ?
2762 AND a.fx_transaction = '0'
2763 ORDER BY a.oid, a.transdate|;
2764 $sth = $dbh->prepare($query);
2765 do_statement($self, $sth, $query, $self->{id});
2767 # get exchangerate for currency
2768 $self->{exchangerate} =
2769 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2772 # store amounts in {acc_trans}{$key} for multiple accounts
2773 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2774 $ref->{exchangerate} =
2775 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2776 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2779 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2780 $ref->{amount} *= -1;
2782 $ref->{index} = $index;
2784 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2790 d.curr AS currencies, d.closedto, d.revtrans,
2791 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2792 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2794 $ref = selectfirst_hashref_query($self, $dbh, $query);
2795 map { $self->{$_} = $ref->{$_} } keys %$ref;
2802 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2803 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2804 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2806 $ref = selectfirst_hashref_query($self, $dbh, $query);
2807 map { $self->{$_} = $ref->{$_} } keys %$ref;
2809 if ($self->{"$self->{vc}_id"}) {
2811 # only setup currency
2812 ($self->{currency}) = split(/:/, $self->{currencies});
2816 $self->lastname_used($dbh, $myconfig, $table, $module);
2818 # get exchangerate for currency
2819 $self->{exchangerate} =
2820 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2826 $main::lxdebug->leave_sub();
2830 $main::lxdebug->enter_sub();
2832 my ($self, $dbh, $myconfig, $table, $module) = @_;
2836 $table = $table eq "customer" ? "customer" : "vendor";
2837 my %column_map = ("a.curr" => "currency",
2838 "a.${table}_id" => "${table}_id",
2839 "a.department_id" => "department_id",
2840 "d.description" => "department",
2841 "ct.name" => $table,
2842 "current_date + ct.terms" => "duedate",
2845 if ($self->{type} =~ /delivery_order/) {
2846 $arap = 'delivery_orders';
2847 delete $column_map{"a.curr"};
2849 } elsif ($self->{type} =~ /_order/) {
2851 $where = "quotation = '0'";
2853 } elsif ($self->{type} =~ /_quotation/) {
2855 $where = "quotation = '1'";
2857 } elsif ($table eq 'customer') {
2865 $where = "($where) AND" if ($where);
2866 my $query = qq|SELECT MAX(id) FROM $arap
2867 WHERE $where ${table}_id > 0|;
2868 my ($trans_id) = selectrow_query($self, $dbh, $query);
2871 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
2872 $query = qq|SELECT $column_spec
2874 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2875 LEFT JOIN department d ON (a.department_id = d.id)
2877 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
2879 map { $self->{$_} = $ref->{$_} } values %column_map;
2881 $main::lxdebug->leave_sub();
2885 $main::lxdebug->enter_sub();
2887 my ($self, $myconfig, $thisdate, $days) = @_;
2889 my $dbh = $self->get_standard_dbh($myconfig);
2894 my $dateformat = $myconfig->{dateformat};
2895 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2896 $thisdate = $dbh->quote($thisdate);
2897 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2899 $query = qq|SELECT current_date AS thisdate|;
2902 ($thisdate) = selectrow_query($self, $dbh, $query);
2904 $main::lxdebug->leave_sub();
2910 $main::lxdebug->enter_sub();
2912 my ($self, $string) = @_;
2914 if ($string !~ /%/) {
2915 $string = "%$string%";
2918 $string =~ s/\'/\'\'/g;
2920 $main::lxdebug->leave_sub();
2926 $main::lxdebug->enter_sub();
2928 my ($self, $flds, $new, $count, $numrows) = @_;
2932 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2937 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2939 my $j = $item->{ndx} - 1;
2940 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2944 for $i ($count + 1 .. $numrows) {
2945 map { delete $self->{"${_}_$i"} } @{$flds};
2948 $main::lxdebug->leave_sub();
2952 $main::lxdebug->enter_sub();
2954 my ($self, $myconfig) = @_;
2958 my $dbh = $self->dbconnect_noauto($myconfig);
2960 my $query = qq|DELETE FROM status
2961 WHERE (formname = ?) AND (trans_id = ?)|;
2962 my $sth = prepare_query($self, $dbh, $query);
2964 if ($self->{formname} =~ /(check|receipt)/) {
2965 for $i (1 .. $self->{rowcount}) {
2966 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2969 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2973 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2974 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2976 my %queued = split / /, $self->{queued};
2979 if ($self->{formname} =~ /(check|receipt)/) {
2981 # this is a check or receipt, add one entry for each lineitem
2982 my ($accno) = split /--/, $self->{account};
2983 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2984 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2985 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2986 $sth = prepare_query($self, $dbh, $query);
2988 for $i (1 .. $self->{rowcount}) {
2989 if ($self->{"checked_$i"}) {
2990 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2996 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2997 VALUES (?, ?, ?, ?, ?)|;
2998 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2999 $queued{$self->{formname}}, $self->{formname});
3005 $main::lxdebug->leave_sub();
3009 $main::lxdebug->enter_sub();
3011 my ($self, $dbh) = @_;
3013 my ($query, $printed, $emailed);
3015 my $formnames = $self->{printed};
3016 my $emailforms = $self->{emailed};
3018 $query = qq|DELETE FROM status
3019 WHERE (formname = ?) AND (trans_id = ?)|;
3020 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
3022 # this only applies to the forms
3023 # checks and receipts are posted when printed or queued
3025 if ($self->{queued}) {
3026 my %queued = split / /, $self->{queued};
3028 foreach my $formname (keys %queued) {
3029 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3030 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3032 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3033 VALUES (?, ?, ?, ?, ?)|;
3034 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
3036 $formnames =~ s/\Q$self->{formname}\E//;
3037 $emailforms =~ s/\Q$self->{formname}\E//;
3042 # save printed, emailed info
3043 $formnames =~ s/^ +//g;
3044 $emailforms =~ s/^ +//g;
3047 map { $status{$_}{printed} = 1 } split / +/, $formnames;
3048 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
3050 foreach my $formname (keys %status) {
3051 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
3052 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
3054 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
3055 VALUES (?, ?, ?, ?)|;
3056 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
3059 $main::lxdebug->leave_sub();
3063 # $main::locale->text('SAVED')
3064 # $main::locale->text('DELETED')
3065 # $main::locale->text('ADDED')
3066 # $main::locale->text('PAYMENT POSTED')
3067 # $main::locale->text('POSTED')
3068 # $main::locale->text('POSTED AS NEW')
3069 # $main::locale->text('ELSE')
3070 # $main::locale->text('SAVED FOR DUNNING')
3071 # $main::locale->text('DUNNING STARTED')
3072 # $main::locale->text('PRINTED')
3073 # $main::locale->text('MAILED')
3074 # $main::locale->text('SCREENED')
3075 # $main::locale->text('CANCELED')
3076 # $main::locale->text('invoice')
3077 # $main::locale->text('proforma')
3078 # $main::locale->text('sales_order')
3079 # $main::locale->text('packing_list')
3080 # $main::locale->text('pick_list')
3081 # $main::locale->text('purchase_order')
3082 # $main::locale->text('bin_list')
3083 # $main::locale->text('sales_quotation')
3084 # $main::locale->text('request_quotation')
3087 $main::lxdebug->enter_sub();
3092 if(!exists $self->{employee_id}) {
3093 &get_employee($self, $dbh);
3097 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3098 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3099 my @values = (conv_i($self->{id}), $self->{login},
3100 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3101 do_query($self, $dbh, $query, @values);
3103 $main::lxdebug->leave_sub();
3107 $main::lxdebug->enter_sub();
3109 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3110 my ($orderBy, $desc) = split(/\-\-/, $order);
3111 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3114 if ($trans_id ne "") {
3116 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 | .
3117 qq|FROM history_erp h | .
3118 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3119 qq|WHERE trans_id = | . $trans_id
3120 . $restriction . qq| |
3123 my $sth = $dbh->prepare($query) || $self->dberror($query);
3125 $sth->execute() || $self->dberror("$query");
3127 while(my $hash_ref = $sth->fetchrow_hashref()) {
3128 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3129 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3130 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3131 $tempArray[$i++] = $hash_ref;
3133 $main::lxdebug->leave_sub() and return \@tempArray
3134 if ($i > 0 && $tempArray[0] ne "");
3136 $main::lxdebug->leave_sub();
3140 sub update_defaults {
3141 $main::lxdebug->enter_sub();
3143 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3146 if ($provided_dbh) {
3147 $dbh = $provided_dbh;
3149 $dbh = $self->dbconnect_noauto($myconfig);
3151 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3152 my $sth = $dbh->prepare($query);
3154 $sth->execute || $self->dberror($query);
3155 my ($var) = $sth->fetchrow_array;
3158 if ($var =~ m/\d+$/) {
3159 my $new_var = (substr $var, $-[0]) * 1 + 1;
3160 my $len_diff = length($var) - $-[0] - length($new_var);
3161 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3167 $query = qq|UPDATE defaults SET $fld = ?|;
3168 do_query($self, $dbh, $query, $var);
3170 if (!$provided_dbh) {
3175 $main::lxdebug->leave_sub();
3180 sub update_business {
3181 $main::lxdebug->enter_sub();
3183 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3186 if ($provided_dbh) {
3187 $dbh = $provided_dbh;
3189 $dbh = $self->dbconnect_noauto($myconfig);
3192 qq|SELECT customernumberinit FROM business
3193 WHERE id = ? FOR UPDATE|;
3194 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3196 if ($var =~ m/\d+$/) {
3197 my $new_var = (substr $var, $-[0]) * 1 + 1;
3198 my $len_diff = length($var) - $-[0] - length($new_var);
3199 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3205 $query = qq|UPDATE business
3206 SET customernumberinit = ?
3208 do_query($self, $dbh, $query, $var, $business_id);
3210 if (!$provided_dbh) {
3215 $main::lxdebug->leave_sub();
3220 sub get_partsgroup {
3221 $main::lxdebug->enter_sub();
3223 my ($self, $myconfig, $p) = @_;
3224 my $target = $p->{target} || 'all_partsgroup';
3226 my $dbh = $self->get_standard_dbh($myconfig);
3228 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3230 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3233 if ($p->{searchitems} eq 'part') {
3234 $query .= qq|WHERE p.inventory_accno_id > 0|;
3236 if ($p->{searchitems} eq 'service') {
3237 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3239 if ($p->{searchitems} eq 'assembly') {
3240 $query .= qq|WHERE p.assembly = '1'|;
3242 if ($p->{searchitems} eq 'labor') {
3243 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3246 $query .= qq|ORDER BY partsgroup|;
3249 $query = qq|SELECT id, partsgroup FROM partsgroup
3250 ORDER BY partsgroup|;
3253 if ($p->{language_code}) {
3254 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3255 t.description AS translation
3257 JOIN parts p ON (p.partsgroup_id = pg.id)
3258 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3259 ORDER BY translation|;
3260 @values = ($p->{language_code});
3263 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3265 $main::lxdebug->leave_sub();
3268 sub get_pricegroup {
3269 $main::lxdebug->enter_sub();
3271 my ($self, $myconfig, $p) = @_;
3273 my $dbh = $self->get_standard_dbh($myconfig);
3275 my $query = qq|SELECT p.id, p.pricegroup
3278 $query .= qq| ORDER BY pricegroup|;
3281 $query = qq|SELECT id, pricegroup FROM pricegroup
3282 ORDER BY pricegroup|;
3285 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3287 $main::lxdebug->leave_sub();
3291 # usage $form->all_years($myconfig, [$dbh])
3292 # return list of all years where bookings found
3295 $main::lxdebug->enter_sub();
3297 my ($self, $myconfig, $dbh) = @_;
3299 $dbh ||= $self->get_standard_dbh($myconfig);
3302 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3303 (SELECT MAX(transdate) FROM acc_trans)|;
3304 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3306 if ($myconfig->{dateformat} =~ /^yy/) {
3307 ($startdate) = split /\W/, $startdate;
3308 ($enddate) = split /\W/, $enddate;
3310 (@_) = split /\W/, $startdate;
3312 (@_) = split /\W/, $enddate;
3317 $startdate = substr($startdate,0,4);
3318 $enddate = substr($enddate,0,4);
3320 while ($enddate >= $startdate) {
3321 push @all_years, $enddate--;
3326 $main::lxdebug->leave_sub();
3330 $main::lxdebug->enter_sub();
3334 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
3336 $main::lxdebug->leave_sub();
3340 $main::lxdebug->enter_sub();
3345 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
3347 $main::lxdebug->leave_sub();