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 unless ($transdate) {
1632 $main::lxdebug->leave_sub();
1636 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1638 if ($currency eq $defaultcurrency) {
1639 $main::lxdebug->leave_sub();
1643 my $dbh = $self->get_standard_dbh($myconfig);
1644 my $query = qq|SELECT e.$fld FROM exchangerate e
1645 WHERE e.curr = ? AND e.transdate = ?|;
1647 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1649 $main::lxdebug->leave_sub();
1651 return $exchangerate;
1654 sub get_default_currency {
1655 $main::lxdebug->enter_sub();
1657 my ($self, $myconfig) = @_;
1658 my $dbh = $self->get_standard_dbh($myconfig);
1660 my $query = qq|SELECT curr FROM defaults|;
1662 my ($curr) = selectrow_query($self, $dbh, $query);
1663 my ($defaultcurrency) = split m/:/, $curr;
1665 $main::lxdebug->leave_sub();
1667 return $defaultcurrency;
1671 sub set_payment_options {
1672 $main::lxdebug->enter_sub();
1674 my ($self, $myconfig, $transdate) = @_;
1676 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1678 my $dbh = $self->get_standard_dbh($myconfig);
1681 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1682 qq|FROM payment_terms p | .
1685 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1686 $self->{payment_terms}) =
1687 selectrow_query($self, $dbh, $query, $self->{payment_id});
1689 if ($transdate eq "") {
1690 if ($self->{invdate}) {
1691 $transdate = $self->{invdate};
1693 $transdate = $self->{transdate};
1698 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1699 qq|FROM payment_terms|;
1700 ($self->{netto_date}, $self->{skonto_date}) =
1701 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1703 my ($invtotal, $total);
1704 my (%amounts, %formatted_amounts);
1706 if ($self->{type} =~ /_order$/) {
1707 $amounts{invtotal} = $self->{ordtotal};
1708 $amounts{total} = $self->{ordtotal};
1710 } elsif ($self->{type} =~ /_quotation$/) {
1711 $amounts{invtotal} = $self->{quototal};
1712 $amounts{total} = $self->{quototal};
1715 $amounts{invtotal} = $self->{invtotal};
1716 $amounts{total} = $self->{total};
1719 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1721 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1722 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1723 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1725 foreach (keys %amounts) {
1726 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1727 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1730 if ($self->{"language_id"}) {
1732 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1733 qq|FROM translation_payment_terms t | .
1734 qq|LEFT JOIN language l ON t.language_id = l.id | .
1735 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1736 my ($description_long, $output_numberformat, $output_dateformat,
1737 $output_longdates) =
1738 selectrow_query($self, $dbh, $query,
1739 $self->{"language_id"}, $self->{"payment_id"});
1741 $self->{payment_terms} = $description_long if ($description_long);
1743 if ($output_dateformat) {
1744 foreach my $key (qw(netto_date skonto_date)) {
1746 $main::locale->reformat_date($myconfig, $self->{$key},
1752 if ($output_numberformat &&
1753 ($output_numberformat ne $myconfig->{"numberformat"})) {
1754 my $saved_numberformat = $myconfig->{"numberformat"};
1755 $myconfig->{"numberformat"} = $output_numberformat;
1756 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1757 $myconfig->{"numberformat"} = $saved_numberformat;
1761 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1762 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1763 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1764 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1765 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1766 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1767 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1769 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1771 $main::lxdebug->leave_sub();
1775 sub get_template_language {
1776 $main::lxdebug->enter_sub();
1778 my ($self, $myconfig) = @_;
1780 my $template_code = "";
1782 if ($self->{language_id}) {
1783 my $dbh = $self->get_standard_dbh($myconfig);
1784 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1785 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1788 $main::lxdebug->leave_sub();
1790 return $template_code;
1793 sub get_printer_code {
1794 $main::lxdebug->enter_sub();
1796 my ($self, $myconfig) = @_;
1798 my $template_code = "";
1800 if ($self->{printer_id}) {
1801 my $dbh = $self->get_standard_dbh($myconfig);
1802 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1803 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1806 $main::lxdebug->leave_sub();
1808 return $template_code;
1812 $main::lxdebug->enter_sub();
1814 my ($self, $myconfig) = @_;
1816 my $template_code = "";
1818 if ($self->{shipto_id}) {
1819 my $dbh = $self->get_standard_dbh($myconfig);
1820 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1821 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1822 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1825 $main::lxdebug->leave_sub();
1829 $main::lxdebug->enter_sub();
1831 my ($self, $dbh, $id, $module) = @_;
1836 foreach my $item (qw(name department_1 department_2 street zipcode city country
1837 contact phone fax email)) {
1838 if ($self->{"shipto$item"}) {
1839 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1841 push(@values, $self->{"shipto${item}"});
1845 if ($self->{shipto_id}) {
1846 my $query = qq|UPDATE shipto set
1848 shiptodepartment_1 = ?,
1849 shiptodepartment_2 = ?,
1858 WHERE shipto_id = ?|;
1859 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1861 my $query = qq|SELECT * FROM shipto
1862 WHERE shiptoname = ? AND
1863 shiptodepartment_1 = ? AND
1864 shiptodepartment_2 = ? AND
1865 shiptostreet = ? AND
1866 shiptozipcode = ? AND
1868 shiptocountry = ? AND
1869 shiptocontact = ? AND
1875 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1878 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1879 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1880 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1881 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1882 do_query($self, $dbh, $query, $id, @values, $module);
1887 $main::lxdebug->leave_sub();
1891 $main::lxdebug->enter_sub();
1893 my ($self, $dbh) = @_;
1895 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1896 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1897 $self->{"employee_id"} *= 1;
1899 $main::lxdebug->leave_sub();
1902 sub get_employee_data {
1903 $main::lxdebug->enter_sub();
1908 Common::check_params(\%params, qw(prefix));
1909 Common::check_params_x(\%params, qw(id));
1912 $main::lxdebug->leave_sub();
1916 my $myconfig = \%main::myconfig;
1917 my $dbh = $params{dbh} || $self->get_standard_dbh($myconfig);
1919 my ($login) = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
1922 my $user = User->new($login);
1923 map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
1925 $self->{$params{prefix} . '_login'} = $login;
1926 $self->{$params{prefix} . '_name'} ||= $login;
1929 $main::lxdebug->leave_sub();
1933 $main::lxdebug->enter_sub();
1935 my ($self, $myconfig) = @_;
1937 my $dbh = $self->get_standard_dbh($myconfig);
1938 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1939 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1941 $main::lxdebug->leave_sub();
1945 $main::lxdebug->enter_sub();
1947 my ($self, $dbh, $id, $key) = @_;
1949 $key = "all_contacts" unless ($key);
1953 $main::lxdebug->leave_sub();
1958 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1959 qq|FROM contacts | .
1960 qq|WHERE cp_cv_id = ? | .
1961 qq|ORDER BY lower(cp_name)|;
1963 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1965 $main::lxdebug->leave_sub();
1969 $main::lxdebug->enter_sub();
1971 my ($self, $dbh, $key) = @_;
1973 my ($all, $old_id, $where, @values);
1975 if (ref($key) eq "HASH") {
1978 $key = "ALL_PROJECTS";
1980 foreach my $p (keys(%{$params})) {
1982 $all = $params->{$p};
1983 } elsif ($p eq "old_id") {
1984 $old_id = $params->{$p};
1985 } elsif ($p eq "key") {
1986 $key = $params->{$p};
1992 $where = "WHERE active ";
1994 if (ref($old_id) eq "ARRAY") {
1995 my @ids = grep({ $_ } @{$old_id});
1997 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1998 push(@values, @ids);
2001 $where .= " OR (id = ?) ";
2002 push(@values, $old_id);
2008 qq|SELECT id, projectnumber, description, active | .
2011 qq|ORDER BY lower(projectnumber)|;
2013 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2015 $main::lxdebug->leave_sub();
2019 $main::lxdebug->enter_sub();
2021 my ($self, $dbh, $vc_id, $key) = @_;
2023 $key = "all_shipto" unless ($key);
2026 # get shipping addresses
2027 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
2029 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
2035 $main::lxdebug->leave_sub();
2039 $main::lxdebug->enter_sub();
2041 my ($self, $dbh, $key) = @_;
2043 $key = "all_printers" unless ($key);
2045 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
2047 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2049 $main::lxdebug->leave_sub();
2053 $main::lxdebug->enter_sub();
2055 my ($self, $dbh, $params) = @_;
2058 $key = $params->{key};
2059 $key = "all_charts" unless ($key);
2061 my $transdate = quote_db_date($params->{transdate});
2064 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
2066 qq|LEFT JOIN taxkeys tk ON | .
2067 qq|(tk.id = (SELECT id FROM taxkeys | .
2068 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2069 qq| ORDER BY startdate DESC LIMIT 1)) | .
2070 qq|ORDER BY c.accno|;
2072 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2074 $main::lxdebug->leave_sub();
2077 sub _get_taxcharts {
2078 $main::lxdebug->enter_sub();
2080 my ($self, $dbh, $key) = @_;
2082 $key = "all_taxcharts" unless ($key);
2084 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
2086 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2088 $main::lxdebug->leave_sub();
2092 $main::lxdebug->enter_sub();
2094 my ($self, $dbh, $key) = @_;
2096 $key = "all_taxzones" unless ($key);
2098 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2100 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2102 $main::lxdebug->leave_sub();
2105 sub _get_employees {
2106 $main::lxdebug->enter_sub();
2108 my ($self, $dbh, $default_key, $key) = @_;
2110 $key = $default_key unless ($key);
2111 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2113 $main::lxdebug->leave_sub();
2116 sub _get_business_types {
2117 $main::lxdebug->enter_sub();
2119 my ($self, $dbh, $key) = @_;
2121 $key = "all_business_types" unless ($key);
2123 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
2125 $main::lxdebug->leave_sub();
2128 sub _get_languages {
2129 $main::lxdebug->enter_sub();
2131 my ($self, $dbh, $key) = @_;
2133 $key = "all_languages" unless ($key);
2135 my $query = qq|SELECT * FROM language ORDER BY id|;
2137 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2139 $main::lxdebug->leave_sub();
2142 sub _get_dunning_configs {
2143 $main::lxdebug->enter_sub();
2145 my ($self, $dbh, $key) = @_;
2147 $key = "all_dunning_configs" unless ($key);
2149 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2151 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2153 $main::lxdebug->leave_sub();
2156 sub _get_currencies {
2157 $main::lxdebug->enter_sub();
2159 my ($self, $dbh, $key) = @_;
2161 $key = "all_currencies" unless ($key);
2163 my $query = qq|SELECT curr AS currency FROM defaults|;
2165 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2167 $main::lxdebug->leave_sub();
2171 $main::lxdebug->enter_sub();
2173 my ($self, $dbh, $key) = @_;
2175 $key = "all_payments" unless ($key);
2177 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2179 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2181 $main::lxdebug->leave_sub();
2184 sub _get_customers {
2185 $main::lxdebug->enter_sub();
2187 my ($self, $dbh, $key, $limit) = @_;
2189 $key = "all_customers" unless ($key);
2190 my $limit_clause = "LIMIT $limit" if $limit;
2192 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
2194 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2196 $main::lxdebug->leave_sub();
2200 $main::lxdebug->enter_sub();
2202 my ($self, $dbh, $key) = @_;
2204 $key = "all_vendors" unless ($key);
2206 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2208 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2210 $main::lxdebug->leave_sub();
2213 sub _get_departments {
2214 $main::lxdebug->enter_sub();
2216 my ($self, $dbh, $key) = @_;
2218 $key = "all_departments" unless ($key);
2220 my $query = qq|SELECT * FROM department ORDER BY description|;
2222 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2224 $main::lxdebug->leave_sub();
2227 sub _get_warehouses {
2228 $main::lxdebug->enter_sub();
2230 my ($self, $dbh, $param) = @_;
2232 my ($key, $bins_key);
2234 if ('' eq ref $param) {
2238 $key = $param->{key};
2239 $bins_key = $param->{bins};
2242 my $query = qq|SELECT w.* FROM warehouse w
2243 WHERE (NOT w.invalid) AND
2244 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2245 ORDER BY w.sortkey|;
2247 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2250 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2251 my $sth = prepare_query($self, $dbh, $query);
2253 foreach my $warehouse (@{ $self->{$key} }) {
2254 do_statement($self, $sth, $query, $warehouse->{id});
2255 $warehouse->{$bins_key} = [];
2257 while (my $ref = $sth->fetchrow_hashref()) {
2258 push @{ $warehouse->{$bins_key} }, $ref;
2264 $main::lxdebug->leave_sub();
2268 $main::lxdebug->enter_sub();
2270 my ($self, $dbh, $table, $key, $sortkey) = @_;
2272 my $query = qq|SELECT * FROM $table|;
2273 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2275 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2277 $main::lxdebug->leave_sub();
2281 # $main::lxdebug->enter_sub();
2283 # my ($self, $dbh, $key) = @_;
2285 # $key ||= "all_groups";
2287 # my $groups = $main::auth->read_groups();
2289 # $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2291 # $main::lxdebug->leave_sub();
2295 $main::lxdebug->enter_sub();
2300 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2301 my ($sth, $query, $ref);
2303 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2304 my $vc_id = $self->{"${vc}_id"};
2306 if ($params{"contacts"}) {
2307 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2310 if ($params{"shipto"}) {
2311 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2314 if ($params{"projects"} || $params{"all_projects"}) {
2315 $self->_get_projects($dbh, $params{"all_projects"} ?
2316 $params{"all_projects"} : $params{"projects"},
2317 $params{"all_projects"} ? 1 : 0);
2320 if ($params{"printers"}) {
2321 $self->_get_printers($dbh, $params{"printers"});
2324 if ($params{"languages"}) {
2325 $self->_get_languages($dbh, $params{"languages"});
2328 if ($params{"charts"}) {
2329 $self->_get_charts($dbh, $params{"charts"});
2332 if ($params{"taxcharts"}) {
2333 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2336 if ($params{"taxzones"}) {
2337 $self->_get_taxzones($dbh, $params{"taxzones"});
2340 if ($params{"employees"}) {
2341 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2344 if ($params{"salesmen"}) {
2345 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2348 if ($params{"business_types"}) {
2349 $self->_get_business_types($dbh, $params{"business_types"});
2352 if ($params{"dunning_configs"}) {
2353 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2356 if($params{"currencies"}) {
2357 $self->_get_currencies($dbh, $params{"currencies"});
2360 if($params{"customers"}) {
2361 if (ref $params{"customers"} eq 'HASH') {
2362 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2364 $self->_get_customers($dbh, $params{"customers"});
2368 if($params{"vendors"}) {
2369 if (ref $params{"vendors"} eq 'HASH') {
2370 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2372 $self->_get_vendors($dbh, $params{"vendors"});
2376 if($params{"payments"}) {
2377 $self->_get_payments($dbh, $params{"payments"});
2380 if($params{"departments"}) {
2381 $self->_get_departments($dbh, $params{"departments"});
2384 if ($params{price_factors}) {
2385 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2388 if ($params{warehouses}) {
2389 $self->_get_warehouses($dbh, $params{warehouses});
2392 # if ($params{groups}) {
2393 # $self->_get_groups($dbh, $params{groups});
2396 if ($params{partsgroup}) {
2397 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2400 $main::lxdebug->leave_sub();
2403 # this sub gets the id and name from $table
2405 $main::lxdebug->enter_sub();
2407 my ($self, $myconfig, $table) = @_;
2409 # connect to database
2410 my $dbh = $self->get_standard_dbh($myconfig);
2412 $table = $table eq "customer" ? "customer" : "vendor";
2413 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2415 my ($query, @values);
2417 if (!$self->{openinvoices}) {
2419 if ($self->{customernumber} ne "") {
2420 $where = qq|(vc.customernumber ILIKE ?)|;
2421 push(@values, '%' . $self->{customernumber} . '%');
2423 $where = qq|(vc.name ILIKE ?)|;
2424 push(@values, '%' . $self->{$table} . '%');
2428 qq~SELECT vc.id, vc.name,
2429 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2431 WHERE $where AND (NOT vc.obsolete)
2435 qq~SELECT DISTINCT vc.id, vc.name,
2436 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2438 JOIN $table vc ON (a.${table}_id = vc.id)
2439 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2441 push(@values, '%' . $self->{$table} . '%');
2444 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2446 $main::lxdebug->leave_sub();
2448 return scalar(@{ $self->{name_list} });
2451 # the selection sub is used in the AR, AP, IS, IR and OE module
2454 $main::lxdebug->enter_sub();
2456 my ($self, $myconfig, $table, $module) = @_;
2459 my $dbh = $self->get_standard_dbh($myconfig);
2461 $table = $table eq "customer" ? "customer" : "vendor";
2463 my $query = qq|SELECT count(*) FROM $table|;
2464 my ($count) = selectrow_query($self, $dbh, $query);
2466 # build selection list
2467 if ($count < $myconfig->{vclimit}) {
2468 $query = qq|SELECT id, name, salesman_id
2469 FROM $table WHERE NOT obsolete
2471 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2475 $self->get_employee($dbh);
2477 # setup sales contacts
2478 $query = qq|SELECT e.id, e.name
2480 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2481 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2484 push(@{ $self->{all_employees} },
2485 { id => $self->{employee_id},
2486 name => $self->{employee} });
2488 # sort the whole thing
2489 @{ $self->{all_employees} } =
2490 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2492 if ($module eq 'AR') {
2494 # prepare query for departments
2495 $query = qq|SELECT id, description
2498 ORDER BY description|;
2501 $query = qq|SELECT id, description
2503 ORDER BY description|;
2506 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2509 $query = qq|SELECT id, description
2513 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2516 $query = qq|SELECT printer_description, id
2518 ORDER BY printer_description|;
2520 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2523 $query = qq|SELECT id, description
2527 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2529 $main::lxdebug->leave_sub();
2532 sub language_payment {
2533 $main::lxdebug->enter_sub();
2535 my ($self, $myconfig) = @_;
2537 my $dbh = $self->get_standard_dbh($myconfig);
2539 my $query = qq|SELECT id, description
2543 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2546 $query = qq|SELECT printer_description, id
2548 ORDER BY printer_description|;
2550 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2553 $query = qq|SELECT id, description
2557 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2559 # get buchungsgruppen
2560 $query = qq|SELECT id, description
2561 FROM buchungsgruppen|;
2563 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2565 $main::lxdebug->leave_sub();
2568 # this is only used for reports
2569 sub all_departments {
2570 $main::lxdebug->enter_sub();
2572 my ($self, $myconfig, $table) = @_;
2574 my $dbh = $self->get_standard_dbh($myconfig);
2577 if ($table eq 'customer') {
2578 $where = "WHERE role = 'P' ";
2581 my $query = qq|SELECT id, description
2584 ORDER BY description|;
2585 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2587 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2589 $main::lxdebug->leave_sub();
2593 $main::lxdebug->enter_sub();
2595 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2598 if ($table eq "customer") {
2607 $self->all_vc($myconfig, $table, $module);
2609 # get last customers or vendors
2610 my ($query, $sth, $ref);
2612 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2617 my $transdate = "current_date";
2618 if ($self->{transdate}) {
2619 $transdate = $dbh->quote($self->{transdate});
2622 # now get the account numbers
2623 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2624 FROM chart c, taxkeys tk
2625 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2626 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2629 $sth = $dbh->prepare($query);
2631 do_statement($self, $sth, $query, '%' . $module . '%');
2633 $self->{accounts} = "";
2634 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2636 foreach my $key (split(/:/, $ref->{link})) {
2637 if ($key =~ /\Q$module\E/) {
2639 # cross reference for keys
2640 $xkeyref{ $ref->{accno} } = $key;
2642 push @{ $self->{"${module}_links"}{$key} },
2643 { accno => $ref->{accno},
2644 description => $ref->{description},
2645 taxkey => $ref->{taxkey_id},
2646 tax_id => $ref->{tax_id} };
2648 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2654 # get taxkeys and description
2655 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2656 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2658 if (($module eq "AP") || ($module eq "AR")) {
2659 # get tax rates and description
2660 $query = qq|SELECT * FROM tax|;
2661 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2667 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2668 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2669 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2670 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2672 d.description AS department,
2675 JOIN $table c ON (a.${table}_id = c.id)
2676 LEFT JOIN employee e ON (e.id = a.employee_id)
2677 LEFT JOIN department d ON (d.id = a.department_id)
2679 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2681 foreach my $key (keys %$ref) {
2682 $self->{$key} = $ref->{$key};
2685 my $transdate = "current_date";
2686 if ($self->{transdate}) {
2687 $transdate = $dbh->quote($self->{transdate});
2690 # now get the account numbers
2691 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2693 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2695 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2696 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2699 $sth = $dbh->prepare($query);
2700 do_statement($self, $sth, $query, "%$module%");
2702 $self->{accounts} = "";
2703 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2705 foreach my $key (split(/:/, $ref->{link})) {
2706 if ($key =~ /\Q$module\E/) {
2708 # cross reference for keys
2709 $xkeyref{ $ref->{accno} } = $key;
2711 push @{ $self->{"${module}_links"}{$key} },
2712 { accno => $ref->{accno},
2713 description => $ref->{description},
2714 taxkey => $ref->{taxkey_id},
2715 tax_id => $ref->{tax_id} };
2717 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2723 # get amounts from individual entries
2726 c.accno, c.description,
2727 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2731 LEFT JOIN chart c ON (c.id = a.chart_id)
2732 LEFT JOIN project p ON (p.id = a.project_id)
2733 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2734 WHERE (tk.taxkey_id=a.taxkey) AND
2735 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2736 THEN tk.chart_id = a.chart_id
2739 OR (c.link='%tax%')) AND
2740 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2741 WHERE a.trans_id = ?
2742 AND a.fx_transaction = '0'
2743 ORDER BY a.oid, a.transdate|;
2744 $sth = $dbh->prepare($query);
2745 do_statement($self, $sth, $query, $self->{id});
2747 # get exchangerate for currency
2748 $self->{exchangerate} =
2749 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2752 # store amounts in {acc_trans}{$key} for multiple accounts
2753 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2754 $ref->{exchangerate} =
2755 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2756 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2759 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2760 $ref->{amount} *= -1;
2762 $ref->{index} = $index;
2764 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2770 d.curr AS currencies, d.closedto, d.revtrans,
2771 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2772 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2774 $ref = selectfirst_hashref_query($self, $dbh, $query);
2775 map { $self->{$_} = $ref->{$_} } keys %$ref;
2782 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2783 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2784 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2786 $ref = selectfirst_hashref_query($self, $dbh, $query);
2787 map { $self->{$_} = $ref->{$_} } keys %$ref;
2789 if ($self->{"$self->{vc}_id"}) {
2791 # only setup currency
2792 ($self->{currency}) = split(/:/, $self->{currencies});
2796 $self->lastname_used($dbh, $myconfig, $table, $module);
2798 # get exchangerate for currency
2799 $self->{exchangerate} =
2800 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2806 $main::lxdebug->leave_sub();
2810 $main::lxdebug->enter_sub();
2812 my ($self, $dbh, $myconfig, $table, $module) = @_;
2816 $table = $table eq "customer" ? "customer" : "vendor";
2817 my %column_map = ("a.curr" => "currency",
2818 "a.${table}_id" => "${table}_id",
2819 "a.department_id" => "department_id",
2820 "d.description" => "department",
2821 "ct.name" => $table,
2822 "current_date + ct.terms" => "duedate",
2825 if ($self->{type} =~ /delivery_order/) {
2826 $arap = 'delivery_orders';
2827 delete $column_map{"a.curr"};
2829 } elsif ($self->{type} =~ /_order/) {
2831 $where = "quotation = '0'";
2833 } elsif ($self->{type} =~ /_quotation/) {
2835 $where = "quotation = '1'";
2837 } elsif ($table eq 'customer') {
2845 $where = "($where) AND" if ($where);
2846 my $query = qq|SELECT MAX(id) FROM $arap
2847 WHERE $where ${table}_id > 0|;
2848 my ($trans_id) = selectrow_query($self, $dbh, $query);
2851 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
2852 $query = qq|SELECT $column_spec
2854 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2855 LEFT JOIN department d ON (a.department_id = d.id)
2857 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
2859 map { $self->{$_} = $ref->{$_} } values %column_map;
2861 $main::lxdebug->leave_sub();
2865 $main::lxdebug->enter_sub();
2867 my ($self, $myconfig, $thisdate, $days) = @_;
2869 my $dbh = $self->get_standard_dbh($myconfig);
2874 my $dateformat = $myconfig->{dateformat};
2875 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2876 $thisdate = $dbh->quote($thisdate);
2877 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2879 $query = qq|SELECT current_date AS thisdate|;
2882 ($thisdate) = selectrow_query($self, $dbh, $query);
2884 $main::lxdebug->leave_sub();
2890 $main::lxdebug->enter_sub();
2892 my ($self, $string) = @_;
2894 if ($string !~ /%/) {
2895 $string = "%$string%";
2898 $string =~ s/\'/\'\'/g;
2900 $main::lxdebug->leave_sub();
2906 $main::lxdebug->enter_sub();
2908 my ($self, $flds, $new, $count, $numrows) = @_;
2912 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2917 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2919 my $j = $item->{ndx} - 1;
2920 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2924 for $i ($count + 1 .. $numrows) {
2925 map { delete $self->{"${_}_$i"} } @{$flds};
2928 $main::lxdebug->leave_sub();
2932 $main::lxdebug->enter_sub();
2934 my ($self, $myconfig) = @_;
2938 my $dbh = $self->dbconnect_noauto($myconfig);
2940 my $query = qq|DELETE FROM status
2941 WHERE (formname = ?) AND (trans_id = ?)|;
2942 my $sth = prepare_query($self, $dbh, $query);
2944 if ($self->{formname} =~ /(check|receipt)/) {
2945 for $i (1 .. $self->{rowcount}) {
2946 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2949 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2953 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2954 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2956 my %queued = split / /, $self->{queued};
2959 if ($self->{formname} =~ /(check|receipt)/) {
2961 # this is a check or receipt, add one entry for each lineitem
2962 my ($accno) = split /--/, $self->{account};
2963 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2964 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2965 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2966 $sth = prepare_query($self, $dbh, $query);
2968 for $i (1 .. $self->{rowcount}) {
2969 if ($self->{"checked_$i"}) {
2970 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2976 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2977 VALUES (?, ?, ?, ?, ?)|;
2978 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2979 $queued{$self->{formname}}, $self->{formname});
2985 $main::lxdebug->leave_sub();
2989 $main::lxdebug->enter_sub();
2991 my ($self, $dbh) = @_;
2993 my ($query, $printed, $emailed);
2995 my $formnames = $self->{printed};
2996 my $emailforms = $self->{emailed};
2998 $query = qq|DELETE FROM status
2999 WHERE (formname = ?) AND (trans_id = ?)|;
3000 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
3002 # this only applies to the forms
3003 # checks and receipts are posted when printed or queued
3005 if ($self->{queued}) {
3006 my %queued = split / /, $self->{queued};
3008 foreach my $formname (keys %queued) {
3009 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3010 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
3012 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
3013 VALUES (?, ?, ?, ?, ?)|;
3014 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
3016 $formnames =~ s/\Q$self->{formname}\E//;
3017 $emailforms =~ s/\Q$self->{formname}\E//;
3022 # save printed, emailed info
3023 $formnames =~ s/^ +//g;
3024 $emailforms =~ s/^ +//g;
3027 map { $status{$_}{printed} = 1 } split / +/, $formnames;
3028 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
3030 foreach my $formname (keys %status) {
3031 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
3032 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
3034 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
3035 VALUES (?, ?, ?, ?)|;
3036 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
3039 $main::lxdebug->leave_sub();
3043 # $main::locale->text('SAVED')
3044 # $main::locale->text('DELETED')
3045 # $main::locale->text('ADDED')
3046 # $main::locale->text('PAYMENT POSTED')
3047 # $main::locale->text('POSTED')
3048 # $main::locale->text('POSTED AS NEW')
3049 # $main::locale->text('ELSE')
3050 # $main::locale->text('SAVED FOR DUNNING')
3051 # $main::locale->text('DUNNING STARTED')
3052 # $main::locale->text('PRINTED')
3053 # $main::locale->text('MAILED')
3054 # $main::locale->text('SCREENED')
3055 # $main::locale->text('CANCELED')
3056 # $main::locale->text('invoice')
3057 # $main::locale->text('proforma')
3058 # $main::locale->text('sales_order')
3059 # $main::locale->text('packing_list')
3060 # $main::locale->text('pick_list')
3061 # $main::locale->text('purchase_order')
3062 # $main::locale->text('bin_list')
3063 # $main::locale->text('sales_quotation')
3064 # $main::locale->text('request_quotation')
3067 $main::lxdebug->enter_sub();
3072 if(!exists $self->{employee_id}) {
3073 &get_employee($self, $dbh);
3077 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3078 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3079 my @values = (conv_i($self->{id}), $self->{login},
3080 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3081 do_query($self, $dbh, $query, @values);
3083 $main::lxdebug->leave_sub();
3087 $main::lxdebug->enter_sub();
3089 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3090 my ($orderBy, $desc) = split(/\-\-/, $order);
3091 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3094 if ($trans_id ne "") {
3096 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 | .
3097 qq|FROM history_erp h | .
3098 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3099 qq|WHERE trans_id = | . $trans_id
3100 . $restriction . qq| |
3103 my $sth = $dbh->prepare($query) || $self->dberror($query);
3105 $sth->execute() || $self->dberror("$query");
3107 while(my $hash_ref = $sth->fetchrow_hashref()) {
3108 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3109 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3110 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3111 $tempArray[$i++] = $hash_ref;
3113 $main::lxdebug->leave_sub() and return \@tempArray
3114 if ($i > 0 && $tempArray[0] ne "");
3116 $main::lxdebug->leave_sub();
3120 sub update_defaults {
3121 $main::lxdebug->enter_sub();
3123 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3126 if ($provided_dbh) {
3127 $dbh = $provided_dbh;
3129 $dbh = $self->dbconnect_noauto($myconfig);
3131 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3132 my $sth = $dbh->prepare($query);
3134 $sth->execute || $self->dberror($query);
3135 my ($var) = $sth->fetchrow_array;
3138 if ($var =~ m/\d+$/) {
3139 my $new_var = (substr $var, $-[0]) * 1 + 1;
3140 my $len_diff = length($var) - $-[0] - length($new_var);
3141 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3147 $query = qq|UPDATE defaults SET $fld = ?|;
3148 do_query($self, $dbh, $query, $var);
3150 if (!$provided_dbh) {
3155 $main::lxdebug->leave_sub();
3160 sub update_business {
3161 $main::lxdebug->enter_sub();
3163 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3166 if ($provided_dbh) {
3167 $dbh = $provided_dbh;
3169 $dbh = $self->dbconnect_noauto($myconfig);
3172 qq|SELECT customernumberinit FROM business
3173 WHERE id = ? FOR UPDATE|;
3174 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3176 if ($var =~ m/\d+$/) {
3177 my $new_var = (substr $var, $-[0]) * 1 + 1;
3178 my $len_diff = length($var) - $-[0] - length($new_var);
3179 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3185 $query = qq|UPDATE business
3186 SET customernumberinit = ?
3188 do_query($self, $dbh, $query, $var, $business_id);
3190 if (!$provided_dbh) {
3195 $main::lxdebug->leave_sub();
3200 sub get_partsgroup {
3201 $main::lxdebug->enter_sub();
3203 my ($self, $myconfig, $p) = @_;
3204 my $target = $p->{target} || 'all_partsgroup';
3206 my $dbh = $self->get_standard_dbh($myconfig);
3208 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3210 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3213 if ($p->{searchitems} eq 'part') {
3214 $query .= qq|WHERE p.inventory_accno_id > 0|;
3216 if ($p->{searchitems} eq 'service') {
3217 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3219 if ($p->{searchitems} eq 'assembly') {
3220 $query .= qq|WHERE p.assembly = '1'|;
3222 if ($p->{searchitems} eq 'labor') {
3223 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3226 $query .= qq|ORDER BY partsgroup|;
3229 $query = qq|SELECT id, partsgroup FROM partsgroup
3230 ORDER BY partsgroup|;
3233 if ($p->{language_code}) {
3234 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3235 t.description AS translation
3237 JOIN parts p ON (p.partsgroup_id = pg.id)
3238 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3239 ORDER BY translation|;
3240 @values = ($p->{language_code});
3243 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3245 $main::lxdebug->leave_sub();
3248 sub get_pricegroup {
3249 $main::lxdebug->enter_sub();
3251 my ($self, $myconfig, $p) = @_;
3253 my $dbh = $self->get_standard_dbh($myconfig);
3255 my $query = qq|SELECT p.id, p.pricegroup
3258 $query .= qq| ORDER BY pricegroup|;
3261 $query = qq|SELECT id, pricegroup FROM pricegroup
3262 ORDER BY pricegroup|;
3265 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3267 $main::lxdebug->leave_sub();
3271 # usage $form->all_years($myconfig, [$dbh])
3272 # return list of all years where bookings found
3275 $main::lxdebug->enter_sub();
3277 my ($self, $myconfig, $dbh) = @_;
3279 $dbh ||= $self->get_standard_dbh($myconfig);
3282 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3283 (SELECT MAX(transdate) FROM acc_trans)|;
3284 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3286 if ($myconfig->{dateformat} =~ /^yy/) {
3287 ($startdate) = split /\W/, $startdate;
3288 ($enddate) = split /\W/, $enddate;
3290 (@_) = split /\W/, $startdate;
3292 (@_) = split /\W/, $enddate;
3297 $startdate = substr($startdate,0,4);
3298 $enddate = substr($enddate,0,4);
3300 while ($enddate >= $startdate) {
3301 push @all_years, $enddate--;
3306 $main::lxdebug->leave_sub();
3310 $main::lxdebug->enter_sub();
3314 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
3316 $main::lxdebug->leave_sub();
3320 $main::lxdebug->enter_sub();
3325 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
3327 $main::lxdebug->leave_sub();