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 #======================================================================
56 use List::Util qw(first max min sum);
62 $standard_dbh->disconnect();
68 $main::lxdebug->enter_sub(2);
76 while ($key =~ /\[\+?\]\.|\./) {
77 substr($key, 0, $+[0]) = '';
85 if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
86 push @{ $curr->{$`} }, { };
89 $curr = $curr->{$`}->[-1];
93 $curr->{$key} = $value;
95 $main::lxdebug->leave_sub(2);
97 return \$curr->{$key};
101 $main::lxdebug->enter_sub(2);
106 my @pairs = split(/&/, $input);
109 my ($key, $value) = split(/=/, $_, 2);
110 $self->_store_value($self->unescape($key), $self->unescape($value));
113 $main::lxdebug->leave_sub(2);
116 sub _request_to_hash {
117 $main::lxdebug->enter_sub(2);
122 if (!$ENV{'CONTENT_TYPE'}
123 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
125 $self->_input_to_hash($input);
127 $main::lxdebug->leave_sub(2);
131 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
133 my $boundary = '--' . $1;
135 foreach my $line (split m/\n/, $input) {
136 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
138 if (($line eq $boundary) || ($line eq "$boundary\r")) {
139 ${ $previous } =~ s|\r?\n$|| if $previous;
145 $content_type = "text/plain";
152 next unless $boundary_found;
154 if (!$headers_done) {
155 $line =~ s/[\r\n]*$//;
162 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
163 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
165 substr $line, $-[0], $+[0] - $-[0], "";
168 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
170 substr $line, $-[0], $+[0] - $-[0], "";
173 $previous = $self->_store_value($name, '');
174 $self->{FILENAME} = $filename if ($filename);
179 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
186 next unless $previous;
188 ${ $previous } .= "${line}\n";
191 ${ $previous } =~ s|\r?\n$|| if $previous;
193 $main::lxdebug->leave_sub(2);
197 $main::lxdebug->enter_sub();
203 if ($LXDebug::watch_form) {
204 require SL::Watchdog;
205 tie %{ $self }, 'SL::Watchdog';
208 read(STDIN, $_, $ENV{CONTENT_LENGTH});
210 if ($ENV{QUERY_STRING}) {
211 $_ = $ENV{QUERY_STRING};
220 $self->_request_to_hash($_);
222 $self->{action} = lc $self->{action};
223 $self->{action} =~ s/( |-|,|\#)/_/g;
225 $self->{version} = "2.4.3";
227 $main::lxdebug->leave_sub();
232 sub _flatten_variables_rec {
233 $main::lxdebug->enter_sub(2);
242 if ('' eq ref $curr->{$key}) {
243 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
245 } elsif ('HASH' eq ref $curr->{$key}) {
246 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
247 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
251 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
252 my $first_array_entry = 1;
254 foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
255 push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
256 $first_array_entry = 0;
261 $main::lxdebug->leave_sub(2);
266 sub flatten_variables {
267 $main::lxdebug->enter_sub(2);
275 push @variables, $self->_flatten_variables_rec($self, '', $_);
278 $main::lxdebug->leave_sub(2);
283 sub flatten_standard_variables {
284 $main::lxdebug->enter_sub(2);
287 my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
291 foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
292 push @variables, $self->_flatten_variables_rec($self, '', $_);
295 $main::lxdebug->leave_sub(2);
301 $main::lxdebug->enter_sub();
307 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
309 $main::lxdebug->leave_sub();
313 $main::lxdebug->enter_sub(2);
316 my $password = $self->{password};
318 $self->{password} = 'X' x 8;
320 local $Data::Dumper::Sortkeys = 1;
321 my $output = Dumper($self);
323 $self->{password} = $password;
325 $main::lxdebug->leave_sub(2);
331 $main::lxdebug->enter_sub(2);
333 my ($self, $str) = @_;
335 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
337 $main::lxdebug->leave_sub(2);
343 $main::lxdebug->enter_sub(2);
345 my ($self, $str) = @_;
350 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
352 $main::lxdebug->leave_sub(2);
358 my ($self, $str) = @_;
360 if ($str && !ref($str)) {
361 $str =~ s/\"/"/g;
369 my ($self, $str) = @_;
371 if ($str && !ref($str)) {
372 $str =~ s/"/\"/g;
383 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
385 for (sort keys %$self) {
386 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
387 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
394 $main::lxdebug->enter_sub();
396 $main::lxdebug->show_backtrace();
398 my ($self, $msg) = @_;
399 if ($ENV{HTTP_USER_AGENT}) {
401 $self->show_generic_error($msg);
408 $main::lxdebug->leave_sub();
412 $main::lxdebug->enter_sub();
414 my ($self, $msg) = @_;
416 if ($ENV{HTTP_USER_AGENT}) {
419 if (!$self->{header}) {
432 if ($self->{info_function}) {
433 &{ $self->{info_function} }($msg);
439 $main::lxdebug->leave_sub();
442 # calculates the number of rows in a textarea based on the content and column number
443 # can be capped with maxrows
445 $main::lxdebug->enter_sub();
446 my ($self, $str, $cols, $maxrows, $minrows) = @_;
450 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
453 $main::lxdebug->leave_sub();
455 return max(min($rows, $maxrows), $minrows);
459 $main::lxdebug->enter_sub();
461 my ($self, $msg) = @_;
463 $self->error("$msg\n" . $DBI::errstr);
465 $main::lxdebug->leave_sub();
469 $main::lxdebug->enter_sub();
471 my ($self, $name, $msg) = @_;
474 foreach my $part (split m/\./, $name) {
475 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
478 $curr = $curr->{$part};
481 $main::lxdebug->leave_sub();
484 sub create_http_response {
485 $main::lxdebug->enter_sub();
490 my $cgi = $main::cgi;
491 $cgi ||= CGI->new('');
495 if ($ENV{HTTP_X_FORWARDED_FOR}) {
496 $base_path = $ENV{HTTP_REFERER};
497 $base_path =~ s|^.*?://.*?/|/|;
499 $base_path = $ENV{REQUEST_URI};
501 $base_path =~ s|[^/]+$||;
502 $base_path =~ s|/$||;
505 if (defined $main::auth) {
506 my $session_cookie_value = $main::auth->get_session_id();
507 $session_cookie_value ||= 'NO_SESSION';
509 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
510 '-value' => $session_cookie_value,
511 '-path' => $base_path);
514 my %cgi_params = ('-type' => $params{content_type});
515 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
517 my $output = $cgi->header('-cookie' => $session_cookie,
520 $main::lxdebug->leave_sub();
527 $main::lxdebug->enter_sub();
529 my ($self, $extra_code) = @_;
531 if ($self->{header}) {
532 $main::lxdebug->leave_sub();
536 my ($stylesheet, $favicon);
538 if ($ENV{HTTP_USER_AGENT}) {
541 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
542 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
543 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
546 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
548 $stylesheets =~ s|^\s*||;
549 $stylesheets =~ s|\s*$||;
550 foreach my $file (split m/\s+/, $stylesheets) {
552 next if (! -f "css/$file");
554 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
557 $self->{favicon} = "favicon.ico" unless $self->{favicon};
559 if ($self->{favicon} && (-f "$self->{favicon}")) {
561 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
565 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
567 if ($self->{landscape}) {
568 $pagelayout = qq|<style type="text/css">
569 \@page { size:landscape; }
573 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
577 if ($self->{jsscript} == 1) {
580 <script type="text/javascript" src="js/common.js"></script>
581 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
582 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
583 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
584 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
591 ? "$self->{title} - $self->{titlebar}"
594 foreach $item (@ { $self->{AJAX} }) {
595 $ajax .= $item->show_javascript();
598 print $self->create_http_response('content_type' => 'text/html',
599 'charset' => $db_charset,);
600 print qq|${doctype}<html>
602 <title>$self->{titlebar}</title>
606 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
610 <script type="text/javascript">
618 <meta name="robots" content="noindex,nofollow" />
619 <script type="text/javascript" src="js/highlight_input.js"></script>
620 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
622 <script type="text/javascript" src="js/tabcontent.js">
624 /***********************************************
625 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
626 * This notice MUST stay intact for legal use
627 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
628 ***********************************************/
639 $main::lxdebug->leave_sub();
642 sub _prepare_html_template {
643 $main::lxdebug->enter_sub();
645 my ($self, $file, $additional_params) = @_;
648 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
649 $language = $main::language;
651 $language = $main::myconfig{"countrycode"};
653 $language = "de" unless ($language);
655 if (-f "templates/webpages/${file}_${language}.html") {
656 if ((-f ".developer") &&
657 (-f "templates/webpages/${file}_master.html") &&
658 ((stat("templates/webpages/${file}_master.html"))[9] >
659 (stat("templates/webpages/${file}_${language}.html"))[9])) {
660 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
661 "Please re-run 'locales.pl' in 'locale/${language}'.";
662 print(qq|<pre>$info</pre>|);
666 $file = "templates/webpages/${file}_${language}.html";
667 } elsif (-f "templates/webpages/${file}.html") {
668 $file = "templates/webpages/${file}.html";
670 my $info = "Web page template '${file}' not found.\n" .
671 "Please re-run 'locales.pl' in 'locale/${language}'.";
672 print(qq|<pre>$info</pre>|);
676 if ($self->{"DEBUG"}) {
677 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
680 if ($additional_params->{"DEBUG"}) {
681 $additional_params->{"DEBUG"} =
682 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
685 if (%main::myconfig) {
686 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
687 my $jsc_dateformat = $main::myconfig{"dateformat"};
688 $jsc_dateformat =~ s/d+/\%d/gi;
689 $jsc_dateformat =~ s/m+/\%m/gi;
690 $jsc_dateformat =~ s/y+/\%Y/gi;
691 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
694 $additional_params->{"conf_webdav"} = $main::webdav;
695 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
696 $additional_params->{"conf_latex_templates"} = $main::latex;
697 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
699 if (%main::debug_options) {
700 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
703 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
704 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
705 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
709 $main::lxdebug->leave_sub();
714 sub parse_html_template {
715 $main::lxdebug->enter_sub();
717 my ($self, $file, $additional_params) = @_;
719 $additional_params ||= { };
721 $file = $self->_prepare_html_template($file, $additional_params);
723 my $template = Template->new({ 'INTERPOLATE' => 0,
727 'PLUGIN_BASE' => 'SL::Template::Plugin',
728 'INCLUDE_PATH' => '.:templates/webpages',
731 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
733 my $in = IO::File->new($file, 'r');
736 print STDERR "Error opening template file: $!";
737 $main::lxdebug->leave_sub();
741 my $input = join('', <$in>);
745 $input = $main::locale->{iconv}->convert($input);
749 if (!$template->process(\$input, $additional_params, \$output)) {
750 print STDERR $template->error();
753 $main::lxdebug->leave_sub();
758 sub show_generic_error {
759 my ($self, $error, %params) = @_;
762 'title_error' => $params{title},
763 'label_error' => $error,
766 if ($params{action}) {
769 map { delete($self->{$_}); } qw(action);
770 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
772 $add_params->{SHOW_BUTTON} = 1;
773 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
774 $add_params->{VARIABLES} = \@vars;
776 } elsif ($params{back_button}) {
777 $add_params->{SHOW_BACK_BUTTON} = 1;
780 $self->{title} = $title if ($title);
783 print $self->parse_html_template("generic/error", $add_params);
785 die("Error: $error\n");
788 sub show_generic_information {
789 my ($self, $text, $title) = @_;
792 'title_information' => $title,
793 'label_information' => $text,
796 $self->{title} = $title if ($title);
799 print $self->parse_html_template("generic/information", $add_params);
801 die("Information: $error\n");
804 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
805 # changed it to accept an arbitrary number of triggers - sschoeling
807 $main::lxdebug->enter_sub();
810 my $myconfig = shift;
813 # set dateform for jsscript
816 "dd.mm.yy" => "%d.%m.%Y",
817 "dd-mm-yy" => "%d-%m-%Y",
818 "dd/mm/yy" => "%d/%m/%Y",
819 "mm/dd/yy" => "%m/%d/%Y",
820 "mm-dd-yy" => "%m-%d-%Y",
821 "yyyy-mm-dd" => "%Y-%m-%d",
824 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
825 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
832 inputField : "| . (shift) . qq|",
833 ifFormat :"$ifFormat",
834 align : "| . (shift) . qq|",
835 button : "| . (shift) . qq|"
841 <script type="text/javascript">
842 <!--| . join("", @triggers) . qq|//-->
846 $main::lxdebug->leave_sub();
849 } #end sub write_trigger
852 $main::lxdebug->enter_sub();
854 my ($self, $msg) = @_;
856 if ($self->{callback}) {
858 ($script, $argv) = split(/\?/, $self->{callback}, 2);
860 $script =~ s|[^a-zA-Z0-9_\.]||g;
861 exec("perl", "$script", $argv);
869 $main::lxdebug->leave_sub();
872 # sort of columns removed - empty sub
874 $main::lxdebug->enter_sub();
876 my ($self, @columns) = @_;
878 $main::lxdebug->leave_sub();
884 $main::lxdebug->enter_sub(2);
886 my ($self, $myconfig, $amount, $places, $dash) = @_;
892 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
894 my $neg = ($amount =~ s/^-//);
895 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
897 if (defined($places) && ($places ne '')) {
903 my ($actual_places) = ($amount =~ /\.(\d+)/);
904 $actual_places = length($actual_places);
905 $places = $actual_places > $places ? $actual_places : $places;
908 $amount = $self->round_amount($amount, $places);
911 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
912 my @p = split(/\./, $amount); # split amount at decimal point
914 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
917 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
920 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
921 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
922 ($neg ? "-$amount" : "$amount" ) ;
926 $main::lxdebug->leave_sub(2);
930 sub format_amount_units {
931 $main::lxdebug->enter_sub();
936 my $myconfig = \%main::myconfig;
937 my $amount = $params{amount} * 1;
938 my $places = $params{places};
939 my $part_unit_name = $params{part_unit};
940 my $amount_unit_name = $params{amount_unit};
941 my $conv_units = $params{conv_units};
942 my $max_places = $params{max_places};
944 if (!$part_unit_name) {
945 $main::lxdebug->leave_sub();
949 AM->retrieve_all_units();
950 my $all_units = $main::all_units;
952 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
953 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
956 if (!scalar @{ $conv_units }) {
957 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
958 $main::lxdebug->leave_sub();
962 my $part_unit = $all_units->{$part_unit_name};
963 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
965 $amount *= $conv_unit->{factor};
969 foreach my $unit (@$conv_units) {
970 my $last = $unit->{name} eq $part_unit->{name};
972 $num = int($amount / $unit->{factor});
973 $amount -= $num * $unit->{factor};
976 if ($last ? $amount : $num) {
977 push @values, { "unit" => $unit->{name},
978 "amount" => $last ? $amount / $unit->{factor} : $num,
979 "places" => $last ? $places : 0 };
986 push @values, { "unit" => $part_unit_name,
991 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
993 $main::lxdebug->leave_sub();
999 $main::lxdebug->enter_sub(2);
1004 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
1005 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
1006 $input =~ s/\#\#/\#/g;
1008 $main::lxdebug->leave_sub(2);
1016 $main::lxdebug->enter_sub(2);
1018 my ($self, $myconfig, $amount) = @_;
1020 if ( ($myconfig->{numberformat} eq '1.000,00')
1021 || ($myconfig->{numberformat} eq '1000,00')) {
1026 if ($myconfig->{numberformat} eq "1'000.00") {
1032 $main::lxdebug->leave_sub(2);
1034 return ($amount * 1);
1038 $main::lxdebug->enter_sub(2);
1040 my ($self, $amount, $places) = @_;
1043 # Rounding like "Kaufmannsrunden"
1044 # Descr. http://de.wikipedia.org/wiki/Rundung
1046 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
1049 $amount = $amount * (10**($places));
1050 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1052 $main::lxdebug->leave_sub(2);
1054 return $round_amount;
1058 sub parse_template {
1059 $main::lxdebug->enter_sub();
1061 my ($self, $myconfig, $userspath) = @_;
1062 my ($template, $out);
1066 $self->{"cwd"} = getcwd();
1067 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1069 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1070 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1071 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1072 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1073 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1074 } elsif (($self->{"format"} =~ /html/i) ||
1075 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1076 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1077 } elsif (($self->{"format"} =~ /xml/i) ||
1078 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1079 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1080 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
1081 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1082 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
1083 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1084 } elsif ( defined $self->{'format'}) {
1085 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1086 } elsif ( $self->{'format'} eq '' ) {
1087 $self->error("No Outputformat given: $self->{'format'}");
1088 } else { #Catch the rest
1089 $self->error("Outputformat not defined: $self->{'format'}");
1092 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1093 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1095 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
1096 qw(email tel fax name signature company address businessnumber
1097 co_ustid taxnumber duns));
1099 map({ $self->{"${_}"} = $myconfig->{$_}; }
1103 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1105 # OUT is used for the media, screen, printer, email
1106 # for postscript we store a copy in a temporary file
1108 my $prepend_userspath;
1110 if (!$self->{tmpfile}) {
1111 $self->{tmpfile} = "${fileid}.$self->{IN}";
1112 $prepend_userspath = 1;
1115 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1117 $self->{tmpfile} =~ s|.*/||;
1118 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1119 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1121 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1122 $out = $self->{OUT};
1123 $self->{OUT} = ">$self->{tmpfile}";
1127 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
1129 open(OUT, ">-") or $self->error("STDOUT : $!");
1133 if (!$template->parse(*OUT)) {
1135 $self->error("$self->{IN} : " . $template->get_error());
1140 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1142 if ($self->{media} eq 'email') {
1144 my $mail = new Mailer;
1146 map { $mail->{$_} = $self->{$_} }
1147 qw(cc bcc subject message version format);
1148 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1149 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1150 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1151 $mail->{fileid} = "$fileid.";
1152 $myconfig->{signature} =~ s/\r//g;
1154 # if we send html or plain text inline
1155 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1156 $mail->{contenttype} = "text/html";
1158 $mail->{message} =~ s/\r//g;
1159 $mail->{message} =~ s/\n/<br>\n/g;
1160 $myconfig->{signature} =~ s/\n/<br>\n/g;
1161 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1163 open(IN, $self->{tmpfile})
1164 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1166 $mail->{message} .= $_;
1173 if (!$self->{"do_not_attach"}) {
1174 @{ $mail->{attachments} } =
1175 ({ "filename" => $self->{"tmpfile"},
1176 "name" => $self->{"attachment_filename"} ?
1177 $self->{"attachment_filename"} : $self->{"tmpfile"} });
1180 $mail->{message} =~ s/\r//g;
1181 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1185 my $err = $mail->send();
1186 $self->error($self->cleanup . "$err") if ($err);
1190 $self->{OUT} = $out;
1192 my $numbytes = (-s $self->{tmpfile});
1193 open(IN, $self->{tmpfile})
1194 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1196 $self->{copies} = 1 unless $self->{media} eq 'printer';
1198 chdir("$self->{cwd}");
1199 #print(STDERR "Kopien $self->{copies}\n");
1200 #print(STDERR "OUT $self->{OUT}\n");
1201 for my $i (1 .. $self->{copies}) {
1203 open(OUT, $self->{OUT})
1204 or $self->error($self->cleanup . "$self->{OUT} : $!");
1206 $self->{attachment_filename} = ($self->{attachment_filename})
1207 ? $self->{attachment_filename}
1208 : $self->generate_attachment_filename();
1210 # launch application
1211 print qq|Content-Type: | . $template->get_mime_type() . qq|
1212 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1213 Content-Length: $numbytes
1217 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1237 chdir("$self->{cwd}");
1238 $main::lxdebug->leave_sub();
1241 sub get_formname_translation {
1242 my ($self, $formname) = @_;
1244 $formname ||= $self->{formname};
1246 my %formname_translations = (
1247 bin_list => $main::locale->text('Bin List'),
1248 credit_note => $main::locale->text('Credit Note'),
1249 invoice => $main::locale->text('Invoice'),
1250 packing_list => $main::locale->text('Packing List'),
1251 pick_list => $main::locale->text('Pick List'),
1252 proforma => $main::locale->text('Proforma Invoice'),
1253 purchase_order => $main::locale->text('Purchase Order'),
1254 request_quotation => $main::locale->text('RFQ'),
1255 sales_order => $main::locale->text('Confirmation'),
1256 sales_quotation => $main::locale->text('Quotation'),
1257 storno_invoice => $main::locale->text('Storno Invoice'),
1258 storno_packing_list => $main::locale->text('Storno Packing List'),
1259 sales_delivery_order => $main::locale->text('Delivery Order'),
1260 purchase_delivery_order => $main::locale->text('Delivery Order'),
1263 return $formname_translations{$formname}
1266 sub get_number_prefix_for_type {
1270 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1271 : ($self->{type} =~ /_quotation$/) ? 'quo'
1272 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1278 sub get_extension_for_format {
1281 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1282 : $self->{format} =~ /postscript/i ? ".ps"
1283 : $self->{format} =~ /opendocument/i ? ".odt"
1284 : $self->{format} =~ /html/i ? ".html"
1290 sub generate_attachment_filename {
1293 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1294 my $prefix = $self->get_number_prefix_for_type();
1296 if ($attachment_filename && $self->{"${prefix}number"}) {
1297 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1298 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1299 $attachment_filename =~ s|[\s/\\]+|_|g;
1301 $attachment_filename = "";
1304 return $attachment_filename;
1307 sub generate_email_subject {
1310 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1311 my $prefix = $self->get_number_prefix_for_type();
1313 if ($subject && $self->{"${prefix}number"}) {
1314 $subject .= " " . $self->{"${prefix}number"}
1321 $main::lxdebug->enter_sub();
1325 chdir("$self->{tmpdir}");
1328 if (-f "$self->{tmpfile}.err") {
1329 open(FH, "$self->{tmpfile}.err");
1334 if ($self->{tmpfile}) {
1335 $self->{tmpfile} =~ s|.*/||g;
1337 $self->{tmpfile} =~ s/\.\w+$//g;
1338 my $tmpfile = $self->{tmpfile};
1339 unlink(<$tmpfile.*>);
1342 chdir("$self->{cwd}");
1344 $main::lxdebug->leave_sub();
1350 $main::lxdebug->enter_sub();
1352 my ($self, $date, $myconfig) = @_;
1354 if ($date && $date =~ /\D/) {
1356 if ($myconfig->{dateformat} =~ /^yy/) {
1357 ($yy, $mm, $dd) = split /\D/, $date;
1359 if ($myconfig->{dateformat} =~ /^mm/) {
1360 ($mm, $dd, $yy) = split /\D/, $date;
1362 if ($myconfig->{dateformat} =~ /^dd/) {
1363 ($dd, $mm, $yy) = split /\D/, $date;
1368 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1369 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1371 $dd = "0$dd" if ($dd < 10);
1372 $mm = "0$mm" if ($mm < 10);
1374 $date = "$yy$mm$dd";
1377 $main::lxdebug->leave_sub();
1382 # Database routines used throughout
1385 $main::lxdebug->enter_sub(2);
1387 my ($self, $myconfig) = @_;
1389 # connect to database
1391 DBI->connect($myconfig->{dbconnect},
1392 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1396 if ($myconfig->{dboptions}) {
1397 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1400 $main::lxdebug->leave_sub(2);
1405 sub dbconnect_noauto {
1406 $main::lxdebug->enter_sub();
1408 my ($self, $myconfig) = @_;
1410 # connect to database
1412 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1413 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1417 if ($myconfig->{dboptions}) {
1418 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1421 $main::lxdebug->leave_sub();
1426 sub get_standard_dbh {
1427 $main::lxdebug->enter_sub(2);
1429 my ($self, $myconfig) = @_;
1431 if ($standard_dbh && !$standard_dbh->{Active}) {
1432 $main::lxdebug->message(LXDebug::INFO, "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
1433 undef $standard_dbh;
1436 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1438 $main::lxdebug->leave_sub(2);
1440 return $standard_dbh;
1444 $main::lxdebug->enter_sub();
1446 my ($self, $date, $myconfig) = @_;
1447 my $dbh = $self->dbconnect($myconfig);
1449 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1450 my $sth = prepare_execute_query($self, $dbh, $query, $date);
1451 my ($closed) = $sth->fetchrow_array;
1453 $main::lxdebug->leave_sub();
1458 sub update_balance {
1459 $main::lxdebug->enter_sub();
1461 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1463 # if we have a value, go do it
1466 # retrieve balance from table
1467 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1468 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1469 my ($balance) = $sth->fetchrow_array;
1475 $query = "UPDATE $table SET $field = $balance WHERE $where";
1476 do_query($self, $dbh, $query, @values);
1478 $main::lxdebug->leave_sub();
1481 sub update_exchangerate {
1482 $main::lxdebug->enter_sub();
1484 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1486 # some sanity check for currency
1488 $main::lxdebug->leave_sub();
1491 $query = qq|SELECT curr FROM defaults|;
1493 my ($currency) = selectrow_query($self, $dbh, $query);
1494 my ($defaultcurrency) = split m/:/, $currency;
1497 if ($curr eq $defaultcurrency) {
1498 $main::lxdebug->leave_sub();
1502 $query = qq|SELECT e.curr FROM exchangerate e
1503 WHERE e.curr = ? AND e.transdate = ?
1505 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1514 $buy = conv_i($buy, "NULL");
1515 $sell = conv_i($sell, "NULL");
1518 if ($buy != 0 && $sell != 0) {
1519 $set = "buy = $buy, sell = $sell";
1520 } elsif ($buy != 0) {
1521 $set = "buy = $buy";
1522 } elsif ($sell != 0) {
1523 $set = "sell = $sell";
1526 if ($sth->fetchrow_array) {
1527 $query = qq|UPDATE exchangerate
1533 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1534 VALUES (?, $buy, $sell, ?)|;
1537 do_query($self, $dbh, $query, $curr, $transdate);
1539 $main::lxdebug->leave_sub();
1542 sub save_exchangerate {
1543 $main::lxdebug->enter_sub();
1545 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1547 my $dbh = $self->dbconnect($myconfig);
1551 $buy = $rate if $fld eq 'buy';
1552 $sell = $rate if $fld eq 'sell';
1555 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1560 $main::lxdebug->leave_sub();
1563 sub get_exchangerate {
1564 $main::lxdebug->enter_sub();
1566 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1569 unless ($transdate) {
1570 $main::lxdebug->leave_sub();
1574 $query = qq|SELECT curr FROM defaults|;
1576 my ($currency) = selectrow_query($self, $dbh, $query);
1577 my ($defaultcurrency) = split m/:/, $currency;
1579 if ($currency eq $defaultcurrency) {
1580 $main::lxdebug->leave_sub();
1584 $query = qq|SELECT e.$fld FROM exchangerate e
1585 WHERE e.curr = ? AND e.transdate = ?|;
1586 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1590 $main::lxdebug->leave_sub();
1592 return $exchangerate;
1595 sub check_exchangerate {
1596 $main::lxdebug->enter_sub();
1598 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1600 unless ($transdate) {
1601 $main::lxdebug->leave_sub();
1605 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1607 if ($currency eq $defaultcurrency) {
1608 $main::lxdebug->leave_sub();
1612 my $dbh = $self->get_standard_dbh($myconfig);
1613 my $query = qq|SELECT e.$fld FROM exchangerate e
1614 WHERE e.curr = ? AND e.transdate = ?|;
1616 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1618 $main::lxdebug->leave_sub();
1620 return $exchangerate;
1623 sub get_default_currency {
1624 $main::lxdebug->enter_sub();
1626 my ($self, $myconfig) = @_;
1627 my $dbh = $self->get_standard_dbh($myconfig);
1629 my $query = qq|SELECT curr FROM defaults|;
1631 my ($curr) = selectrow_query($self, $dbh, $query);
1632 my ($defaultcurrency) = split m/:/, $curr;
1634 $main::lxdebug->leave_sub();
1636 return $defaultcurrency;
1640 sub set_payment_options {
1641 $main::lxdebug->enter_sub();
1643 my ($self, $myconfig, $transdate) = @_;
1645 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1647 my $dbh = $self->get_standard_dbh($myconfig);
1650 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1651 qq|FROM payment_terms p | .
1654 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1655 $self->{payment_terms}) =
1656 selectrow_query($self, $dbh, $query, $self->{payment_id});
1658 if ($transdate eq "") {
1659 if ($self->{invdate}) {
1660 $transdate = $self->{invdate};
1662 $transdate = $self->{transdate};
1667 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1668 qq|FROM payment_terms|;
1669 ($self->{netto_date}, $self->{skonto_date}) =
1670 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1672 my ($invtotal, $total);
1673 my (%amounts, %formatted_amounts);
1675 if ($self->{type} =~ /_order$/) {
1676 $amounts{invtotal} = $self->{ordtotal};
1677 $amounts{total} = $self->{ordtotal};
1679 } elsif ($self->{type} =~ /_quotation$/) {
1680 $amounts{invtotal} = $self->{quototal};
1681 $amounts{total} = $self->{quototal};
1684 $amounts{invtotal} = $self->{invtotal};
1685 $amounts{total} = $self->{total};
1688 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1690 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1691 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1692 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1694 foreach (keys %amounts) {
1695 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1696 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1699 if ($self->{"language_id"}) {
1701 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1702 qq|FROM translation_payment_terms t | .
1703 qq|LEFT JOIN language l ON t.language_id = l.id | .
1704 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1705 my ($description_long, $output_numberformat, $output_dateformat,
1706 $output_longdates) =
1707 selectrow_query($self, $dbh, $query,
1708 $self->{"language_id"}, $self->{"payment_id"});
1710 $self->{payment_terms} = $description_long if ($description_long);
1712 if ($output_dateformat) {
1713 foreach my $key (qw(netto_date skonto_date)) {
1715 $main::locale->reformat_date($myconfig, $self->{$key},
1721 if ($output_numberformat &&
1722 ($output_numberformat ne $myconfig->{"numberformat"})) {
1723 my $saved_numberformat = $myconfig->{"numberformat"};
1724 $myconfig->{"numberformat"} = $output_numberformat;
1725 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1726 $myconfig->{"numberformat"} = $saved_numberformat;
1730 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1731 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1732 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1733 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1734 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1735 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1736 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1738 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1740 $main::lxdebug->leave_sub();
1744 sub get_template_language {
1745 $main::lxdebug->enter_sub();
1747 my ($self, $myconfig) = @_;
1749 my $template_code = "";
1751 if ($self->{language_id}) {
1752 my $dbh = $self->get_standard_dbh($myconfig);
1753 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1754 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1757 $main::lxdebug->leave_sub();
1759 return $template_code;
1762 sub get_printer_code {
1763 $main::lxdebug->enter_sub();
1765 my ($self, $myconfig) = @_;
1767 my $template_code = "";
1769 if ($self->{printer_id}) {
1770 my $dbh = $self->get_standard_dbh($myconfig);
1771 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1772 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1775 $main::lxdebug->leave_sub();
1777 return $template_code;
1781 $main::lxdebug->enter_sub();
1783 my ($self, $myconfig) = @_;
1785 my $template_code = "";
1787 if ($self->{shipto_id}) {
1788 my $dbh = $self->get_standard_dbh($myconfig);
1789 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1790 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1791 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1794 $main::lxdebug->leave_sub();
1798 $main::lxdebug->enter_sub();
1800 my ($self, $dbh, $id, $module) = @_;
1805 foreach my $item (qw(name department_1 department_2 street zipcode city country
1806 contact phone fax email)) {
1807 if ($self->{"shipto$item"}) {
1808 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1810 push(@values, $self->{"shipto${item}"});
1814 if ($self->{shipto_id}) {
1815 my $query = qq|UPDATE shipto set
1817 shiptodepartment_1 = ?,
1818 shiptodepartment_2 = ?,
1827 WHERE shipto_id = ?|;
1828 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1830 my $query = qq|SELECT * FROM shipto
1831 WHERE shiptoname = ? AND
1832 shiptodepartment_1 = ? AND
1833 shiptodepartment_2 = ? AND
1834 shiptostreet = ? AND
1835 shiptozipcode = ? AND
1837 shiptocountry = ? AND
1838 shiptocontact = ? AND
1844 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1847 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1848 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1849 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1850 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1851 do_query($self, $dbh, $query, $id, @values, $module);
1856 $main::lxdebug->leave_sub();
1860 $main::lxdebug->enter_sub();
1862 my ($self, $dbh) = @_;
1864 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1865 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1866 $self->{"employee_id"} *= 1;
1868 $main::lxdebug->leave_sub();
1872 $main::lxdebug->enter_sub();
1874 my ($self, $myconfig, $salesman_id) = @_;
1876 $main::lxdebug->leave_sub() and return unless $salesman_id;
1878 my $dbh = $self->get_standard_dbh($myconfig);
1881 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1885 my $user = new User($main::memberfile, $login);
1886 map({ $self->{"salesman_$_"} = $user->{$_}; }
1887 qw(address businessnumber co_ustid company duns email fax name
1889 $self->{salesman_login} = $login;
1891 $self->{salesman_name} = $login
1892 if ($self->{salesman_name} eq "");
1895 $main::lxdebug->leave_sub();
1899 $main::lxdebug->enter_sub();
1901 my ($self, $myconfig) = @_;
1903 my $dbh = $self->get_standard_dbh($myconfig);
1904 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1905 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1907 $main::lxdebug->leave_sub();
1911 $main::lxdebug->enter_sub();
1913 my ($self, $dbh, $id, $key) = @_;
1915 $key = "all_contacts" unless ($key);
1919 $main::lxdebug->leave_sub();
1924 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1925 qq|FROM contacts | .
1926 qq|WHERE cp_cv_id = ? | .
1927 qq|ORDER BY lower(cp_name)|;
1929 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1931 $main::lxdebug->leave_sub();
1935 $main::lxdebug->enter_sub();
1937 my ($self, $dbh, $key) = @_;
1939 my ($all, $old_id, $where, @values);
1941 if (ref($key) eq "HASH") {
1944 $key = "ALL_PROJECTS";
1946 foreach my $p (keys(%{$params})) {
1948 $all = $params->{$p};
1949 } elsif ($p eq "old_id") {
1950 $old_id = $params->{$p};
1951 } elsif ($p eq "key") {
1952 $key = $params->{$p};
1958 $where = "WHERE active ";
1960 if (ref($old_id) eq "ARRAY") {
1961 my @ids = grep({ $_ } @{$old_id});
1963 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1964 push(@values, @ids);
1967 $where .= " OR (id = ?) ";
1968 push(@values, $old_id);
1974 qq|SELECT id, projectnumber, description, active | .
1977 qq|ORDER BY lower(projectnumber)|;
1979 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1981 $main::lxdebug->leave_sub();
1985 $main::lxdebug->enter_sub();
1987 my ($self, $dbh, $vc_id, $key) = @_;
1989 $key = "all_shipto" unless ($key);
1992 # get shipping addresses
1993 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1995 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
2001 $main::lxdebug->leave_sub();
2005 $main::lxdebug->enter_sub();
2007 my ($self, $dbh, $key) = @_;
2009 $key = "all_printers" unless ($key);
2011 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
2013 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2015 $main::lxdebug->leave_sub();
2019 $main::lxdebug->enter_sub();
2021 my ($self, $dbh, $params) = @_;
2023 $key = $params->{key};
2024 $key = "all_charts" unless ($key);
2026 my $transdate = quote_db_date($params->{transdate});
2029 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
2031 qq|LEFT JOIN taxkeys tk ON | .
2032 qq|(tk.id = (SELECT id FROM taxkeys | .
2033 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2034 qq| ORDER BY startdate DESC LIMIT 1)) | .
2035 qq|ORDER BY c.accno|;
2037 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2039 $main::lxdebug->leave_sub();
2042 sub _get_taxcharts {
2043 $main::lxdebug->enter_sub();
2045 my ($self, $dbh, $key) = @_;
2047 $key = "all_taxcharts" unless ($key);
2049 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
2051 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2053 $main::lxdebug->leave_sub();
2057 $main::lxdebug->enter_sub();
2059 my ($self, $dbh, $key) = @_;
2061 $key = "all_taxzones" unless ($key);
2063 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2065 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2067 $main::lxdebug->leave_sub();
2070 sub _get_employees {
2071 $main::lxdebug->enter_sub();
2073 my ($self, $dbh, $default_key, $key) = @_;
2075 $key = $default_key unless ($key);
2076 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2078 $main::lxdebug->leave_sub();
2081 sub _get_business_types {
2082 $main::lxdebug->enter_sub();
2084 my ($self, $dbh, $key) = @_;
2086 $key = "all_business_types" unless ($key);
2088 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
2090 $main::lxdebug->leave_sub();
2093 sub _get_languages {
2094 $main::lxdebug->enter_sub();
2096 my ($self, $dbh, $key) = @_;
2098 $key = "all_languages" unless ($key);
2100 my $query = qq|SELECT * FROM language ORDER BY id|;
2102 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2104 $main::lxdebug->leave_sub();
2107 sub _get_dunning_configs {
2108 $main::lxdebug->enter_sub();
2110 my ($self, $dbh, $key) = @_;
2112 $key = "all_dunning_configs" unless ($key);
2114 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2116 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2118 $main::lxdebug->leave_sub();
2121 sub _get_currencies {
2122 $main::lxdebug->enter_sub();
2124 my ($self, $dbh, $key) = @_;
2126 $key = "all_currencies" unless ($key);
2128 my $query = qq|SELECT curr AS currency FROM defaults|;
2130 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2132 $main::lxdebug->leave_sub();
2136 $main::lxdebug->enter_sub();
2138 my ($self, $dbh, $key) = @_;
2140 $key = "all_payments" unless ($key);
2142 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2144 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2146 $main::lxdebug->leave_sub();
2149 sub _get_customers {
2150 $main::lxdebug->enter_sub();
2152 my ($self, $dbh, $key, $limit) = @_;
2154 $key = "all_customers" unless ($key);
2155 $limit_clause = "LIMIT $limit" if $limit;
2157 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
2159 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2161 $main::lxdebug->leave_sub();
2165 $main::lxdebug->enter_sub();
2167 my ($self, $dbh, $key) = @_;
2169 $key = "all_vendors" unless ($key);
2171 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2173 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2175 $main::lxdebug->leave_sub();
2178 sub _get_departments {
2179 $main::lxdebug->enter_sub();
2181 my ($self, $dbh, $key) = @_;
2183 $key = "all_departments" unless ($key);
2185 my $query = qq|SELECT * FROM department ORDER BY description|;
2187 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2189 $main::lxdebug->leave_sub();
2192 sub _get_warehouses {
2193 $main::lxdebug->enter_sub();
2195 my ($self, $dbh, $param) = @_;
2197 my ($key, $bins_key);
2199 if ('' eq ref $param) {
2203 $key = $param->{key};
2204 $bins_key = $param->{bins};
2207 my $query = qq|SELECT w.* FROM warehouse w
2208 WHERE (NOT w.invalid) AND
2209 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2210 ORDER BY w.sortkey|;
2212 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2215 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2216 my $sth = prepare_query($self, $dbh, $query);
2218 foreach my $warehouse (@{ $self->{$key} }) {
2219 do_statement($self, $sth, $query, $warehouse->{id});
2220 $warehouse->{$bins_key} = [];
2222 while (my $ref = $sth->fetchrow_hashref()) {
2223 push @{ $warehouse->{$bins_key} }, $ref;
2229 $main::lxdebug->leave_sub();
2233 $main::lxdebug->enter_sub();
2235 my ($self, $dbh, $table, $key, $sortkey) = @_;
2237 my $query = qq|SELECT * FROM $table|;
2238 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2240 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2242 $main::lxdebug->leave_sub();
2246 $main::lxdebug->enter_sub();
2248 my ($self, $dbh, $key) = @_;
2250 $key ||= "all_groups";
2252 my $groups = $main::auth->read_groups();
2254 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2256 $main::lxdebug->leave_sub();
2260 $main::lxdebug->enter_sub();
2265 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2266 my ($sth, $query, $ref);
2268 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2269 my $vc_id = $self->{"${vc}_id"};
2271 if ($params{"contacts"}) {
2272 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2275 if ($params{"shipto"}) {
2276 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2279 if ($params{"projects"} || $params{"all_projects"}) {
2280 $self->_get_projects($dbh, $params{"all_projects"} ?
2281 $params{"all_projects"} : $params{"projects"},
2282 $params{"all_projects"} ? 1 : 0);
2285 if ($params{"printers"}) {
2286 $self->_get_printers($dbh, $params{"printers"});
2289 if ($params{"languages"}) {
2290 $self->_get_languages($dbh, $params{"languages"});
2293 if ($params{"charts"}) {
2294 $self->_get_charts($dbh, $params{"charts"});
2297 if ($params{"taxcharts"}) {
2298 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2301 if ($params{"taxzones"}) {
2302 $self->_get_taxzones($dbh, $params{"taxzones"});
2305 if ($params{"employees"}) {
2306 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2309 if ($params{"salesmen"}) {
2310 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2313 if ($params{"business_types"}) {
2314 $self->_get_business_types($dbh, $params{"business_types"});
2317 if ($params{"dunning_configs"}) {
2318 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2321 if($params{"currencies"}) {
2322 $self->_get_currencies($dbh, $params{"currencies"});
2325 if($params{"customers"}) {
2326 if (ref $params{"customers"} eq 'HASH') {
2327 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2329 $self->_get_customers($dbh, $params{"customers"});
2333 if($params{"vendors"}) {
2334 if (ref $params{"vendors"} eq 'HASH') {
2335 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2337 $self->_get_vendors($dbh, $params{"vendors"});
2341 if($params{"payments"}) {
2342 $self->_get_payments($dbh, $params{"payments"});
2345 if($params{"departments"}) {
2346 $self->_get_departments($dbh, $params{"departments"});
2349 if ($params{price_factors}) {
2350 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2353 if ($params{warehouses}) {
2354 $self->_get_warehouses($dbh, $params{warehouses});
2357 if ($params{groups}) {
2358 $self->_get_groups($dbh, $params{groups});
2360 if ($params{partsgroup}) {
2361 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2364 $main::lxdebug->leave_sub();
2367 # this sub gets the id and name from $table
2369 $main::lxdebug->enter_sub();
2371 my ($self, $myconfig, $table) = @_;
2373 # connect to database
2374 my $dbh = $self->get_standard_dbh($myconfig);
2376 $table = $table eq "customer" ? "customer" : "vendor";
2377 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2379 my ($query, @values);
2381 if (!$self->{openinvoices}) {
2383 if ($self->{customernumber} ne "") {
2384 $where = qq|(vc.customernumber ILIKE ?)|;
2385 push(@values, '%' . $self->{customernumber} . '%');
2387 $where = qq|(vc.name ILIKE ?)|;
2388 push(@values, '%' . $self->{$table} . '%');
2392 qq~SELECT vc.id, vc.name,
2393 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2395 WHERE $where AND (NOT vc.obsolete)
2399 qq~SELECT DISTINCT vc.id, vc.name,
2400 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2402 JOIN $table vc ON (a.${table}_id = vc.id)
2403 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2405 push(@values, '%' . $self->{$table} . '%');
2408 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2410 $main::lxdebug->leave_sub();
2412 return scalar(@{ $self->{name_list} });
2415 # the selection sub is used in the AR, AP, IS, IR and OE module
2418 $main::lxdebug->enter_sub();
2420 my ($self, $myconfig, $table, $module) = @_;
2423 my $dbh = $self->get_standard_dbh($myconfig);
2425 $table = $table eq "customer" ? "customer" : "vendor";
2427 my $query = qq|SELECT count(*) FROM $table|;
2428 my ($count) = selectrow_query($self, $dbh, $query);
2430 # build selection list
2431 if ($count < $myconfig->{vclimit}) {
2432 $query = qq|SELECT id, name, salesman_id
2433 FROM $table WHERE NOT obsolete
2435 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2439 $self->get_employee($dbh);
2441 # setup sales contacts
2442 $query = qq|SELECT e.id, e.name
2444 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2445 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2448 push(@{ $self->{all_employees} },
2449 { id => $self->{employee_id},
2450 name => $self->{employee} });
2452 # sort the whole thing
2453 @{ $self->{all_employees} } =
2454 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2456 if ($module eq 'AR') {
2458 # prepare query for departments
2459 $query = qq|SELECT id, description
2462 ORDER BY description|;
2465 $query = qq|SELECT id, description
2467 ORDER BY description|;
2470 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2473 $query = qq|SELECT id, description
2477 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2480 $query = qq|SELECT printer_description, id
2482 ORDER BY printer_description|;
2484 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2487 $query = qq|SELECT id, description
2491 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2493 $main::lxdebug->leave_sub();
2496 sub language_payment {
2497 $main::lxdebug->enter_sub();
2499 my ($self, $myconfig) = @_;
2501 my $dbh = $self->get_standard_dbh($myconfig);
2503 my $query = qq|SELECT id, description
2507 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2510 $query = qq|SELECT printer_description, id
2512 ORDER BY printer_description|;
2514 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2517 $query = qq|SELECT id, description
2521 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2523 # get buchungsgruppen
2524 $query = qq|SELECT id, description
2525 FROM buchungsgruppen|;
2527 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2529 $main::lxdebug->leave_sub();
2532 # this is only used for reports
2533 sub all_departments {
2534 $main::lxdebug->enter_sub();
2536 my ($self, $myconfig, $table) = @_;
2538 my $dbh = $self->get_standard_dbh($myconfig);
2541 if ($table eq 'customer') {
2542 $where = "WHERE role = 'P' ";
2545 my $query = qq|SELECT id, description
2548 ORDER BY description|;
2549 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2551 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2553 $main::lxdebug->leave_sub();
2557 $main::lxdebug->enter_sub();
2559 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2562 if ($table eq "customer") {
2571 $self->all_vc($myconfig, $table, $module);
2573 # get last customers or vendors
2574 my ($query, $sth, $ref);
2576 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2581 my $transdate = "current_date";
2582 if ($self->{transdate}) {
2583 $transdate = $dbh->quote($self->{transdate});
2586 # now get the account numbers
2587 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2588 FROM chart c, taxkeys tk
2589 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2590 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2593 $sth = $dbh->prepare($query);
2595 do_statement($self, $sth, $query, '%' . $module . '%');
2597 $self->{accounts} = "";
2598 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2600 foreach my $key (split(/:/, $ref->{link})) {
2601 if ($key =~ /\Q$module\E/) {
2603 # cross reference for keys
2604 $xkeyref{ $ref->{accno} } = $key;
2606 push @{ $self->{"${module}_links"}{$key} },
2607 { accno => $ref->{accno},
2608 description => $ref->{description},
2609 taxkey => $ref->{taxkey_id},
2610 tax_id => $ref->{tax_id} };
2612 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2618 # get taxkeys and description
2619 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2620 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2622 if (($module eq "AP") || ($module eq "AR")) {
2623 # get tax rates and description
2624 $query = qq|SELECT * FROM tax|;
2625 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2631 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2632 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2633 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2634 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2636 d.description AS department,
2639 JOIN $table c ON (a.${table}_id = c.id)
2640 LEFT JOIN employee e ON (e.id = a.employee_id)
2641 LEFT JOIN department d ON (d.id = a.department_id)
2643 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2645 foreach $key (keys %$ref) {
2646 $self->{$key} = $ref->{$key};
2649 my $transdate = "current_date";
2650 if ($self->{transdate}) {
2651 $transdate = $dbh->quote($self->{transdate});
2654 # now get the account numbers
2655 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2657 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2659 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2660 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2663 $sth = $dbh->prepare($query);
2664 do_statement($self, $sth, $query, "%$module%");
2666 $self->{accounts} = "";
2667 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2669 foreach my $key (split(/:/, $ref->{link})) {
2670 if ($key =~ /\Q$module\E/) {
2672 # cross reference for keys
2673 $xkeyref{ $ref->{accno} } = $key;
2675 push @{ $self->{"${module}_links"}{$key} },
2676 { accno => $ref->{accno},
2677 description => $ref->{description},
2678 taxkey => $ref->{taxkey_id},
2679 tax_id => $ref->{tax_id} };
2681 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2687 # get amounts from individual entries
2690 c.accno, c.description,
2691 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2695 LEFT JOIN chart c ON (c.id = a.chart_id)
2696 LEFT JOIN project p ON (p.id = a.project_id)
2697 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2698 WHERE (tk.taxkey_id=a.taxkey) AND
2699 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2700 THEN tk.chart_id = a.chart_id
2703 OR (c.link='%tax%')) AND
2704 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2705 WHERE a.trans_id = ?
2706 AND a.fx_transaction = '0'
2707 ORDER BY a.oid, a.transdate|;
2708 $sth = $dbh->prepare($query);
2709 do_statement($self, $sth, $query, $self->{id});
2711 # get exchangerate for currency
2712 $self->{exchangerate} =
2713 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2716 # store amounts in {acc_trans}{$key} for multiple accounts
2717 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2718 $ref->{exchangerate} =
2719 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2720 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2723 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2724 $ref->{amount} *= -1;
2726 $ref->{index} = $index;
2728 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2734 d.curr AS currencies, d.closedto, d.revtrans,
2735 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2736 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2738 $ref = selectfirst_hashref_query($self, $dbh, $query);
2739 map { $self->{$_} = $ref->{$_} } keys %$ref;
2746 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2747 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2748 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2750 $ref = selectfirst_hashref_query($self, $dbh, $query);
2751 map { $self->{$_} = $ref->{$_} } keys %$ref;
2753 if ($self->{"$self->{vc}_id"}) {
2755 # only setup currency
2756 ($self->{currency}) = split(/:/, $self->{currencies});
2760 $self->lastname_used($dbh, $myconfig, $table, $module);
2762 # get exchangerate for currency
2763 $self->{exchangerate} =
2764 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2770 $main::lxdebug->leave_sub();
2774 $main::lxdebug->enter_sub();
2776 my ($self, $dbh, $myconfig, $table, $module) = @_;
2780 $table = $table eq "customer" ? "customer" : "vendor";
2781 my %column_map = ("a.curr" => "currency",
2782 "a.${table}_id" => "${table}_id",
2783 "a.department_id" => "department_id",
2784 "d.description" => "department",
2785 "ct.name" => $table,
2786 "current_date + ct.terms" => "duedate",
2789 if ($self->{type} =~ /delivery_order/) {
2790 $arap = 'delivery_orders';
2791 delete $column_map{"a.curr"};
2793 } elsif ($self->{type} =~ /_order/) {
2795 $where = "quotation = '0'";
2797 } elsif ($self->{type} =~ /_quotation/) {
2799 $where = "quotation = '1'";
2801 } elsif ($table eq 'customer') {
2809 $where = "($where) AND" if ($where);
2810 my $query = qq|SELECT MAX(id) FROM $arap
2811 WHERE $where ${table}_id > 0|;
2812 my ($trans_id) = selectrow_query($self, $dbh, $query);
2815 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
2816 $query = qq|SELECT $column_spec
2818 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2819 LEFT JOIN department d ON (a.department_id = d.id)
2821 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
2823 map { $self->{$_} = $ref->{$_} } values %column_map;
2825 $main::lxdebug->leave_sub();
2829 $main::lxdebug->enter_sub();
2831 my ($self, $myconfig, $thisdate, $days) = @_;
2833 my $dbh = $self->get_standard_dbh($myconfig);
2838 my $dateformat = $myconfig->{dateformat};
2839 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2840 $thisdate = $dbh->quote($thisdate);
2841 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2843 $query = qq|SELECT current_date AS thisdate|;
2846 ($thisdate) = selectrow_query($self, $dbh, $query);
2848 $main::lxdebug->leave_sub();
2854 $main::lxdebug->enter_sub();
2856 my ($self, $string) = @_;
2858 if ($string !~ /%/) {
2859 $string = "%$string%";
2862 $string =~ s/\'/\'\'/g;
2864 $main::lxdebug->leave_sub();
2870 $main::lxdebug->enter_sub();
2872 my ($self, $flds, $new, $count, $numrows) = @_;
2876 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2881 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2883 $j = $item->{ndx} - 1;
2884 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2888 for $i ($count + 1 .. $numrows) {
2889 map { delete $self->{"${_}_$i"} } @{$flds};
2892 $main::lxdebug->leave_sub();
2896 $main::lxdebug->enter_sub();
2898 my ($self, $myconfig) = @_;
2902 my $dbh = $self->dbconnect_noauto($myconfig);
2904 my $query = qq|DELETE FROM status
2905 WHERE (formname = ?) AND (trans_id = ?)|;
2906 my $sth = prepare_query($self, $dbh, $query);
2908 if ($self->{formname} =~ /(check|receipt)/) {
2909 for $i (1 .. $self->{rowcount}) {
2910 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2913 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2917 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2918 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2920 my %queued = split / /, $self->{queued};
2923 if ($self->{formname} =~ /(check|receipt)/) {
2925 # this is a check or receipt, add one entry for each lineitem
2926 my ($accno) = split /--/, $self->{account};
2927 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2928 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2929 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2930 $sth = prepare_query($self, $dbh, $query);
2932 for $i (1 .. $self->{rowcount}) {
2933 if ($self->{"checked_$i"}) {
2934 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2940 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2941 VALUES (?, ?, ?, ?, ?)|;
2942 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2943 $queued{$self->{formname}}, $self->{formname});
2949 $main::lxdebug->leave_sub();
2953 $main::lxdebug->enter_sub();
2955 my ($self, $dbh) = @_;
2957 my ($query, $printed, $emailed);
2959 my $formnames = $self->{printed};
2960 my $emailforms = $self->{emailed};
2962 $query = qq|DELETE FROM status
2963 WHERE (formname = ?) AND (trans_id = ?)|;
2964 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2966 # this only applies to the forms
2967 # checks and receipts are posted when printed or queued
2969 if ($self->{queued}) {
2970 my %queued = split / /, $self->{queued};
2972 foreach my $formname (keys %queued) {
2973 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2974 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2976 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2977 VALUES (?, ?, ?, ?, ?)|;
2978 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2980 $formnames =~ s/\Q$self->{formname}\E//;
2981 $emailforms =~ s/\Q$self->{formname}\E//;
2986 # save printed, emailed info
2987 $formnames =~ s/^ +//g;
2988 $emailforms =~ s/^ +//g;
2991 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2992 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2994 foreach my $formname (keys %status) {
2995 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2996 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2998 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2999 VALUES (?, ?, ?, ?)|;
3000 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
3003 $main::lxdebug->leave_sub();
3007 # $main::locale->text('SAVED')
3008 # $main::locale->text('DELETED')
3009 # $main::locale->text('ADDED')
3010 # $main::locale->text('PAYMENT POSTED')
3011 # $main::locale->text('POSTED')
3012 # $main::locale->text('POSTED AS NEW')
3013 # $main::locale->text('ELSE')
3014 # $main::locale->text('SAVED FOR DUNNING')
3015 # $main::locale->text('DUNNING STARTED')
3016 # $main::locale->text('PRINTED')
3017 # $main::locale->text('MAILED')
3018 # $main::locale->text('SCREENED')
3019 # $main::locale->text('CANCELED')
3020 # $main::locale->text('invoice')
3021 # $main::locale->text('proforma')
3022 # $main::locale->text('sales_order')
3023 # $main::locale->text('packing_list')
3024 # $main::locale->text('pick_list')
3025 # $main::locale->text('purchase_order')
3026 # $main::locale->text('bin_list')
3027 # $main::locale->text('sales_quotation')
3028 # $main::locale->text('request_quotation')
3031 $main::lxdebug->enter_sub();
3036 if(!exists $self->{employee_id}) {
3037 &get_employee($self, $dbh);
3041 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3042 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3043 my @values = (conv_i($self->{id}), $self->{login},
3044 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3045 do_query($self, $dbh, $query, @values);
3047 $main::lxdebug->leave_sub();
3051 $main::lxdebug->enter_sub();
3053 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3054 my ($orderBy, $desc) = split(/\-\-/, $order);
3055 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3058 if ($trans_id ne "") {
3060 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 | .
3061 qq|FROM history_erp h | .
3062 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3063 qq|WHERE trans_id = | . $trans_id
3064 . $restriction . qq| |
3067 my $sth = $dbh->prepare($query) || $self->dberror($query);
3069 $sth->execute() || $self->dberror("$query");
3071 while(my $hash_ref = $sth->fetchrow_hashref()) {
3072 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3073 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3074 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3075 $tempArray[$i++] = $hash_ref;
3077 $main::lxdebug->leave_sub() and return \@tempArray
3078 if ($i > 0 && $tempArray[0] ne "");
3080 $main::lxdebug->leave_sub();
3084 sub update_defaults {
3085 $main::lxdebug->enter_sub();
3087 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3090 if ($provided_dbh) {
3091 $dbh = $provided_dbh;
3093 $dbh = $self->dbconnect_noauto($myconfig);
3095 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3096 my $sth = $dbh->prepare($query);
3098 $sth->execute || $self->dberror($query);
3099 my ($var) = $sth->fetchrow_array;
3102 if ($var =~ m/\d+$/) {
3103 my $new_var = (substr $var, $-[0]) * 1 + 1;
3104 my $len_diff = length($var) - $-[0] - length($new_var);
3105 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3111 $query = qq|UPDATE defaults SET $fld = ?|;
3112 do_query($self, $dbh, $query, $var);
3114 if (!$provided_dbh) {
3119 $main::lxdebug->leave_sub();
3124 sub update_business {
3125 $main::lxdebug->enter_sub();
3127 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3130 if ($provided_dbh) {
3131 $dbh = $provided_dbh;
3133 $dbh = $self->dbconnect_noauto($myconfig);
3136 qq|SELECT customernumberinit FROM business
3137 WHERE id = ? FOR UPDATE|;
3138 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3140 if ($var =~ m/\d+$/) {
3141 my $new_var = (substr $var, $-[0]) * 1 + 1;
3142 my $len_diff = length($var) - $-[0] - length($new_var);
3143 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3149 $query = qq|UPDATE business
3150 SET customernumberinit = ?
3152 do_query($self, $dbh, $query, $var, $business_id);
3154 if (!$provided_dbh) {
3159 $main::lxdebug->leave_sub();
3164 sub get_partsgroup {
3165 $main::lxdebug->enter_sub();
3167 my ($self, $myconfig, $p) = @_;
3168 my $target = $p->{target} || 'all_partsgroup';
3170 my $dbh = $self->get_standard_dbh($myconfig);
3172 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3174 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3177 if ($p->{searchitems} eq 'part') {
3178 $query .= qq|WHERE p.inventory_accno_id > 0|;
3180 if ($p->{searchitems} eq 'service') {
3181 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3183 if ($p->{searchitems} eq 'assembly') {
3184 $query .= qq|WHERE p.assembly = '1'|;
3186 if ($p->{searchitems} eq 'labor') {
3187 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3190 $query .= qq|ORDER BY partsgroup|;
3193 $query = qq|SELECT id, partsgroup FROM partsgroup
3194 ORDER BY partsgroup|;
3197 if ($p->{language_code}) {
3198 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3199 t.description AS translation
3201 JOIN parts p ON (p.partsgroup_id = pg.id)
3202 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3203 ORDER BY translation|;
3204 @values = ($p->{language_code});
3207 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3209 $main::lxdebug->leave_sub();
3212 sub get_pricegroup {
3213 $main::lxdebug->enter_sub();
3215 my ($self, $myconfig, $p) = @_;
3217 my $dbh = $self->get_standard_dbh($myconfig);
3219 my $query = qq|SELECT p.id, p.pricegroup
3222 $query .= qq| ORDER BY pricegroup|;
3225 $query = qq|SELECT id, pricegroup FROM pricegroup
3226 ORDER BY pricegroup|;
3229 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3231 $main::lxdebug->leave_sub();
3235 # usage $form->all_years($myconfig, [$dbh])
3236 # return list of all years where bookings found
3239 $main::lxdebug->enter_sub();
3241 my ($self, $myconfig, $dbh) = @_;
3243 $dbh ||= $self->get_standard_dbh($myconfig);
3246 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3247 (SELECT MAX(transdate) FROM acc_trans)|;
3248 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3250 if ($myconfig->{dateformat} =~ /^yy/) {
3251 ($startdate) = split /\W/, $startdate;
3252 ($enddate) = split /\W/, $enddate;
3254 (@_) = split /\W/, $startdate;
3256 (@_) = split /\W/, $enddate;
3261 $startdate = substr($startdate,0,4);
3262 $enddate = substr($enddate,0,4);
3264 while ($enddate >= $startdate) {
3265 push @all_years, $enddate--;
3270 $main::lxdebug->leave_sub();
3274 $main::lxdebug->enter_sub();
3278 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
3280 $main::lxdebug->leave_sub();
3284 $main::lxdebug->enter_sub();
3289 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
3291 $main::lxdebug->leave_sub();