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 #======================================================================
55 use List::Util qw(first max min sum);
61 $standard_dbh->disconnect();
67 $main::lxdebug->enter_sub(2);
75 while ($key =~ /\[\+?\]\.|\./) {
76 substr($key, 0, $+[0]) = '';
84 if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
85 push @{ $curr->{$`} }, { };
88 $curr = $curr->{$`}->[-1];
92 $curr->{$key} = $value;
94 $main::lxdebug->leave_sub(2);
96 return \$curr->{$key};
100 $main::lxdebug->enter_sub(2);
105 my @pairs = split(/&/, $input);
108 my ($key, $value) = split(/=/, $_, 2);
109 $self->_store_value($self->unescape($key), $self->unescape($value));
112 $main::lxdebug->leave_sub(2);
115 sub _request_to_hash {
116 $main::lxdebug->enter_sub(2);
121 if (!$ENV{'CONTENT_TYPE'}
122 || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
124 $self->_input_to_hash($input);
126 $main::lxdebug->leave_sub(2);
130 my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
132 my $boundary = '--' . $1;
134 foreach my $line (split m/\n/, $input) {
135 last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
137 if (($line eq $boundary) || ($line eq "$boundary\r")) {
138 ${ $previous } =~ s|\r?\n$|| if $previous;
144 $content_type = "text/plain";
151 next unless $boundary_found;
153 if (!$headers_done) {
154 $line =~ s/[\r\n]*$//;
161 if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
162 if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
164 substr $line, $-[0], $+[0] - $-[0], "";
167 if ($line =~ m|name\s*=\s*"(.*?)"|i) {
169 substr $line, $-[0], $+[0] - $-[0], "";
172 $previous = $self->_store_value($name, '');
173 $self->{FILENAME} = $filename if ($filename);
178 if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
185 next unless $previous;
187 ${ $previous } .= "${line}\n";
190 ${ $previous } =~ s|\r?\n$|| if $previous;
192 $main::lxdebug->leave_sub(2);
196 $main::lxdebug->enter_sub();
202 if ($LXDebug::watch_form) {
203 require SL::Watchdog;
204 tie %{ $self }, 'SL::Watchdog';
207 read(STDIN, $_, $ENV{CONTENT_LENGTH});
209 if ($ENV{QUERY_STRING}) {
210 $_ = $ENV{QUERY_STRING};
219 $self->_request_to_hash($_);
221 $self->{action} = lc $self->{action};
222 $self->{action} =~ s/( |-|,|\#)/_/g;
224 $self->{version} = "2.4.3";
226 $main::lxdebug->leave_sub();
231 sub _flatten_variables_rec {
232 $main::lxdebug->enter_sub(2);
241 if ('' eq ref $curr->{$key}) {
242 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
244 } elsif ('HASH' eq ref $curr->{$key}) {
245 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
246 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
250 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
251 my $first_array_entry = 1;
253 foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
254 push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
255 $first_array_entry = 0;
260 $main::lxdebug->leave_sub(2);
265 sub flatten_variables {
266 $main::lxdebug->enter_sub(2);
274 push @variables, $self->_flatten_variables_rec($self, '', $_);
277 $main::lxdebug->leave_sub(2);
282 sub flatten_standard_variables {
283 $main::lxdebug->enter_sub(2);
286 my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
290 foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
291 push @variables, $self->_flatten_variables_rec($self, '', $_);
294 $main::lxdebug->leave_sub(2);
300 $main::lxdebug->enter_sub();
306 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
308 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub(2);
315 my $password = $self->{password};
317 $self->{password} = 'X' x 8;
319 local $Data::Dumper::Sortkeys = 1;
320 my $output = Dumper($self);
322 $self->{password} = $password;
324 $main::lxdebug->leave_sub(2);
330 $main::lxdebug->enter_sub(2);
332 my ($self, $str) = @_;
334 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
336 $main::lxdebug->leave_sub(2);
342 $main::lxdebug->enter_sub(2);
344 my ($self, $str) = @_;
349 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
351 $main::lxdebug->leave_sub(2);
357 my ($self, $str) = @_;
359 if ($str && !ref($str)) {
360 $str =~ s/\"/"/g;
368 my ($self, $str) = @_;
370 if ($str && !ref($str)) {
371 $str =~ s/"/\"/g;
382 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
384 for (sort keys %$self) {
385 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
386 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
393 $main::lxdebug->enter_sub();
395 $main::lxdebug->show_backtrace();
397 my ($self, $msg) = @_;
398 if ($ENV{HTTP_USER_AGENT}) {
400 $self->show_generic_error($msg);
407 $main::lxdebug->leave_sub();
411 $main::lxdebug->enter_sub();
413 my ($self, $msg) = @_;
415 if ($ENV{HTTP_USER_AGENT}) {
418 if (!$self->{header}) {
431 if ($self->{info_function}) {
432 &{ $self->{info_function} }($msg);
438 $main::lxdebug->leave_sub();
441 # calculates the number of rows in a textarea based on the content and column number
442 # can be capped with maxrows
444 $main::lxdebug->enter_sub();
445 my ($self, $str, $cols, $maxrows, $minrows) = @_;
449 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
452 $main::lxdebug->leave_sub();
454 return max(min($rows, $maxrows), $minrows);
458 $main::lxdebug->enter_sub();
460 my ($self, $msg) = @_;
462 $self->error("$msg\n" . $DBI::errstr);
464 $main::lxdebug->leave_sub();
468 $main::lxdebug->enter_sub();
470 my ($self, $name, $msg) = @_;
473 foreach my $part (split /\./, $name) {
474 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
477 $curr = $curr->{$part};
480 $main::lxdebug->leave_sub();
483 sub create_http_response {
484 $main::lxdebug->enter_sub();
489 my $cgi = $main::cgi;
490 $cgi ||= CGI->new('');
494 if ($ENV{HTTP_X_FORWARDED_FOR}) {
495 $base_path = $ENV{HTTP_REFERER};
496 $base_path =~ s|^.*?://.*?/|/|;
498 $base_path = $ENV{REQUEST_URI};
500 $base_path =~ s|[^/]+$||;
501 $base_path =~ s|/$||;
504 if (defined $main::auth) {
505 my $session_cookie_value = $main::auth->get_session_id();
506 $session_cookie_value ||= 'NO_SESSION';
508 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
509 '-value' => $session_cookie_value,
510 '-path' => $base_path);
513 my %cgi_params = ('-type' => $params{content_type});
514 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
516 my $output = $cgi->header('-cookie' => $session_cookie,
519 $main::lxdebug->leave_sub();
526 $main::lxdebug->enter_sub();
528 my ($self, $extra_code) = @_;
530 if ($self->{header}) {
531 $main::lxdebug->leave_sub();
535 my ($stylesheet, $favicon);
537 if ($ENV{HTTP_USER_AGENT}) {
540 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
541 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
542 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
545 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
547 $stylesheets =~ s|^\s*||;
548 $stylesheets =~ s|\s*$||;
549 foreach my $file (split m/\s+/, $stylesheets) {
551 next if (! -f "css/$file");
553 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
556 $self->{favicon} = "favicon.ico" unless $self->{favicon};
558 if ($self->{favicon} && (-f "$self->{favicon}")) {
560 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
564 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
566 if ($self->{landscape}) {
567 $pagelayout = qq|<style type="text/css">
568 \@page { size:landscape; }
572 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
576 if ($self->{jsscript} == 1) {
579 <script type="text/javascript" src="js/common.js"></script>
580 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
581 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
582 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
583 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
590 ? "$self->{title} - $self->{titlebar}"
593 foreach $item (@ { $self->{AJAX} }) {
594 $ajax .= $item->show_javascript();
597 print $self->create_http_response('content_type' => 'text/html',
598 'charset' => $db_charset,);
599 print qq|${doctype}<html>
601 <title>$self->{titlebar}</title>
605 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
609 <script type="text/javascript">
617 <meta name="robots" content="noindex,nofollow" />
618 <script type="text/javascript" src="js/highlight_input.js"></script>
619 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
621 <script type="text/javascript" src="js/tabcontent.js">
623 /***********************************************
624 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
625 * This notice MUST stay intact for legal use
626 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
627 ***********************************************/
638 $main::lxdebug->leave_sub();
641 sub _prepare_html_template {
642 $main::lxdebug->enter_sub();
644 my ($self, $file, $additional_params) = @_;
647 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
648 $language = $main::language;
650 $language = $main::myconfig{"countrycode"};
652 $language = "de" unless ($language);
654 if (-f "templates/webpages/${file}_${language}.html") {
655 if ((-f ".developer") &&
656 (-f "templates/webpages/${file}_master.html") &&
657 ((stat("templates/webpages/${file}_master.html"))[9] >
658 (stat("templates/webpages/${file}_${language}.html"))[9])) {
659 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
660 "Please re-run 'locales.pl' in 'locale/${language}'.";
661 print(qq|<pre>$info</pre>|);
665 $file = "templates/webpages/${file}_${language}.html";
666 } elsif (-f "templates/webpages/${file}.html") {
667 $file = "templates/webpages/${file}.html";
669 my $info = "Web page template '${file}' not found.\n" .
670 "Please re-run 'locales.pl' in 'locale/${language}'.";
671 print(qq|<pre>$info</pre>|);
675 if ($self->{"DEBUG"}) {
676 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
679 if ($additional_params->{"DEBUG"}) {
680 $additional_params->{"DEBUG"} =
681 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
684 if (%main::myconfig) {
685 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
686 my $jsc_dateformat = $main::myconfig{"dateformat"};
687 $jsc_dateformat =~ s/d+/\%d/gi;
688 $jsc_dateformat =~ s/m+/\%m/gi;
689 $jsc_dateformat =~ s/y+/\%Y/gi;
690 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
693 $additional_params->{"conf_webdav"} = $main::webdav;
694 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
695 $additional_params->{"conf_latex_templates"} = $main::latex;
696 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
698 if (%main::debug_options) {
699 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
702 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
703 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
704 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
708 $main::lxdebug->leave_sub();
713 sub parse_html_template {
714 $main::lxdebug->enter_sub();
716 my ($self, $file, $additional_params) = @_;
718 $additional_params ||= { };
720 $file = $self->_prepare_html_template($file, $additional_params);
722 my $template = Template->new({ 'INTERPOLATE' => 0,
726 'PLUGIN_BASE' => 'SL::Template::Plugin',
727 'INCLUDE_PATH' => '.:templates/webpages',
730 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
733 if (!$template->process($file, $additional_params, \$output)) {
734 print STDERR $template->error();
737 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
739 $main::lxdebug->leave_sub();
744 sub show_generic_error {
745 my ($self, $error, %params) = @_;
748 'title_error' => $params{title},
749 'label_error' => $error,
752 if ($params{action}) {
755 map { delete($self->{$_}); } qw(action);
756 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
758 $add_params->{SHOW_BUTTON} = 1;
759 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
760 $add_params->{VARIABLES} = \@vars;
762 } elsif ($params{back_button}) {
763 $add_params->{SHOW_BACK_BUTTON} = 1;
766 $self->{title} = $title if ($title);
769 print $self->parse_html_template("generic/error", $add_params);
771 die("Error: $error\n");
774 sub show_generic_information {
775 my ($self, $text, $title) = @_;
778 'title_information' => $title,
779 'label_information' => $text,
782 $self->{title} = $title if ($title);
785 print $self->parse_html_template("generic/information", $add_params);
787 die("Information: $error\n");
790 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
791 # changed it to accept an arbitrary number of triggers - sschoeling
793 $main::lxdebug->enter_sub();
796 my $myconfig = shift;
799 # set dateform for jsscript
802 "dd.mm.yy" => "%d.%m.%Y",
803 "dd-mm-yy" => "%d-%m-%Y",
804 "dd/mm/yy" => "%d/%m/%Y",
805 "mm/dd/yy" => "%m/%d/%Y",
806 "mm-dd-yy" => "%m-%d-%Y",
807 "yyyy-mm-dd" => "%Y-%m-%d",
810 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
811 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
818 inputField : "| . (shift) . qq|",
819 ifFormat :"$ifFormat",
820 align : "| . (shift) . qq|",
821 button : "| . (shift) . qq|"
827 <script type="text/javascript">
828 <!--| . join("", @triggers) . qq|//-->
832 $main::lxdebug->leave_sub();
835 } #end sub write_trigger
838 $main::lxdebug->enter_sub();
840 my ($self, $msg) = @_;
842 if ($self->{callback}) {
844 ($script, $argv) = split(/\?/, $self->{callback}, 2);
846 $script =~ s|[^a-zA-Z0-9_\.]||g;
847 exec("perl", "$script", $argv);
855 $main::lxdebug->leave_sub();
858 # sort of columns removed - empty sub
860 $main::lxdebug->enter_sub();
862 my ($self, @columns) = @_;
864 $main::lxdebug->leave_sub();
870 $main::lxdebug->enter_sub(2);
872 my ($self, $myconfig, $amount, $places, $dash) = @_;
878 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
880 my $neg = ($amount =~ s/^-//);
881 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
883 if (defined($places) && ($places ne '')) {
889 my ($actual_places) = ($amount =~ /\.(\d+)/);
890 $actual_places = length($actual_places);
891 $places = $actual_places > $places ? $actual_places : $places;
894 $amount = $self->round_amount($amount, $places);
897 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
898 my @p = split(/\./, $amount); # split amount at decimal point
900 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
903 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
906 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
907 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
908 ($neg ? "-$amount" : "$amount" ) ;
912 $main::lxdebug->leave_sub(2);
916 sub format_amount_units {
917 $main::lxdebug->enter_sub();
922 Common::check_params(\%params, qw(amount part_unit));
924 my $myconfig = \%main::myconfig;
925 my $amount = $params{amount};
926 my $places = $params{places};
927 my $part_unit_name = $params{part_unit};
928 my $amount_unit_name = $params{amount_unit};
929 my $conv_units = $params{conv_units};
930 my $max_places = $params{max_places};
932 AM->retrieve_all_units();
933 my $all_units = $main::all_units;
935 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
936 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
939 if (!scalar @{ $conv_units }) {
940 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
941 $main::lxdebug->leave_sub();
945 my $part_unit = $all_units->{$part_unit_name};
946 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
948 $amount *= $conv_unit->{factor};
952 foreach my $unit (@$conv_units) {
953 my $last = $unit->{name} eq $part_unit->{name};
955 $num = int($amount / $unit->{factor});
956 $amount -= $num * $unit->{factor};
959 if ($last ? $amount : $num) {
960 push @values, { "unit" => $unit->{name},
961 "amount" => $last ? $amount / $unit->{factor} : $num,
962 "places" => $last ? $places : 0 };
969 push @values, { "unit" => $part_unit_name,
974 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
976 $main::lxdebug->leave_sub();
982 $main::lxdebug->enter_sub(2);
987 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
988 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
989 $input =~ s/\#\#/\#/g;
991 $main::lxdebug->leave_sub(2);
999 $main::lxdebug->enter_sub(2);
1001 my ($self, $myconfig, $amount) = @_;
1003 if ( ($myconfig->{numberformat} eq '1.000,00')
1004 || ($myconfig->{numberformat} eq '1000,00')) {
1009 if ($myconfig->{numberformat} eq "1'000.00") {
1015 $main::lxdebug->leave_sub(2);
1017 return ($amount * 1);
1021 $main::lxdebug->enter_sub(2);
1023 my ($self, $amount, $places) = @_;
1026 # Rounding like "Kaufmannsrunden"
1027 # Descr. http://de.wikipedia.org/wiki/Rundung
1029 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
1032 $amount = $amount * (10**($places));
1033 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1035 $main::lxdebug->leave_sub(2);
1037 return $round_amount;
1041 sub parse_template {
1042 $main::lxdebug->enter_sub();
1044 my ($self, $myconfig, $userspath) = @_;
1045 my ($template, $out);
1049 $self->{"cwd"} = getcwd();
1050 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1052 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1053 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1054 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1055 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1056 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1057 } elsif (($self->{"format"} =~ /html/i) ||
1058 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1059 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1060 } elsif (($self->{"format"} =~ /xml/i) ||
1061 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1062 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1063 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
1064 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1065 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
1066 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1067 } elsif ( defined $self->{'format'}) {
1068 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1069 } elsif ( $self->{'format'} eq '' ) {
1070 $self->error("No Outputformat given: $self->{'format'}");
1071 } else { #Catch the rest
1072 $self->error("Outputformat not defined: $self->{'format'}");
1075 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1076 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1078 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
1079 qw(email tel fax name signature company address businessnumber
1080 co_ustid taxnumber duns));
1082 map({ $self->{"${_}"} = $myconfig->{$_}; }
1086 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1088 # OUT is used for the media, screen, printer, email
1089 # for postscript we store a copy in a temporary file
1091 my $prepend_userspath;
1093 if (!$self->{tmpfile}) {
1094 $self->{tmpfile} = "${fileid}.$self->{IN}";
1095 $prepend_userspath = 1;
1098 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1100 $self->{tmpfile} =~ s|.*/||;
1101 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1102 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1104 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1105 $out = $self->{OUT};
1106 $self->{OUT} = ">$self->{tmpfile}";
1110 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
1112 open(OUT, ">-") or $self->error("STDOUT : $!");
1116 if (!$template->parse(*OUT)) {
1118 $self->error("$self->{IN} : " . $template->get_error());
1123 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1125 if ($self->{media} eq 'email') {
1127 my $mail = new Mailer;
1129 map { $mail->{$_} = $self->{$_} }
1130 qw(cc bcc subject message version format);
1131 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1132 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1133 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1134 $mail->{fileid} = "$fileid.";
1135 $myconfig->{signature} =~ s/\r//g;
1137 # if we send html or plain text inline
1138 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1139 $mail->{contenttype} = "text/html";
1141 $mail->{message} =~ s/\r//g;
1142 $mail->{message} =~ s/\n/<br>\n/g;
1143 $myconfig->{signature} =~ s/\n/<br>\n/g;
1144 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1146 open(IN, $self->{tmpfile})
1147 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1149 $mail->{message} .= $_;
1156 if (!$self->{"do_not_attach"}) {
1157 @{ $mail->{attachments} } =
1158 ({ "filename" => $self->{"tmpfile"},
1159 "name" => $self->{"attachment_filename"} ?
1160 $self->{"attachment_filename"} : $self->{"tmpfile"} });
1163 $mail->{message} =~ s/\r//g;
1164 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1168 my $err = $mail->send();
1169 $self->error($self->cleanup . "$err") if ($err);
1173 $self->{OUT} = $out;
1175 my $numbytes = (-s $self->{tmpfile});
1176 open(IN, $self->{tmpfile})
1177 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1179 $self->{copies} = 1 unless $self->{media} eq 'printer';
1181 chdir("$self->{cwd}");
1182 #print(STDERR "Kopien $self->{copies}\n");
1183 #print(STDERR "OUT $self->{OUT}\n");
1184 for my $i (1 .. $self->{copies}) {
1186 open(OUT, $self->{OUT})
1187 or $self->error($self->cleanup . "$self->{OUT} : $!");
1189 $self->{attachment_filename} = ($self->{attachment_filename})
1190 ? $self->{attachment_filename}
1191 : $self->generate_attachment_filename();
1193 # launch application
1194 print qq|Content-Type: | . $template->get_mime_type() . qq|
1195 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1196 Content-Length: $numbytes
1200 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1220 chdir("$self->{cwd}");
1221 $main::lxdebug->leave_sub();
1224 sub get_formname_translation {
1225 my ($self, $formname) = @_;
1227 $formname ||= $self->{formname};
1229 my %formname_translations = (
1230 bin_list => $main::locale->text('Bin List'),
1231 credit_note => $main::locale->text('Credit Note'),
1232 invoice => $main::locale->text('Invoice'),
1233 packing_list => $main::locale->text('Packing List'),
1234 pick_list => $main::locale->text('Pick List'),
1235 proforma => $main::locale->text('Proforma Invoice'),
1236 purchase_order => $main::locale->text('Purchase Order'),
1237 request_quotation => $main::locale->text('RFQ'),
1238 sales_order => $main::locale->text('Confirmation'),
1239 sales_quotation => $main::locale->text('Quotation'),
1240 storno_invoice => $main::locale->text('Storno Invoice'),
1241 storno_packing_list => $main::locale->text('Storno Packing List'),
1242 sales_delivery_order => $main::locale->text('Delivery Order'),
1243 purchase_delivery_order => $main::locale->text('Delivery Order'),
1246 return $formname_translations{$formname}
1249 sub get_number_prefix_for_type {
1253 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1254 : ($self->{type} =~ /_quotation$/) ? 'quo'
1255 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1261 sub get_extension_for_format {
1264 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1265 : $self->{format} =~ /postscript/i ? ".ps"
1266 : $self->{format} =~ /opendocument/i ? ".odt"
1267 : $self->{format} =~ /html/i ? ".html"
1273 sub generate_attachment_filename {
1276 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1277 my $prefix = $self->get_number_prefix_for_type();
1279 if ($attachment_filename && $self->{"${prefix}number"}) {
1280 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1281 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1282 $attachment_filename =~ s|[\s/\\]+|_|g;
1284 $attachment_filename = "";
1287 return $attachment_filename;
1290 sub generate_email_subject {
1293 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1294 my $prefix = $self->get_number_prefix_for_type();
1296 if ($subject && $self->{"${prefix}number"}) {
1297 $subject .= " " . $self->{"${prefix}number"}
1304 $main::lxdebug->enter_sub();
1308 chdir("$self->{tmpdir}");
1311 if (-f "$self->{tmpfile}.err") {
1312 open(FH, "$self->{tmpfile}.err");
1317 if ($self->{tmpfile}) {
1318 $self->{tmpfile} =~ s|.*/||g;
1320 $self->{tmpfile} =~ s/\.\w+$//g;
1321 my $tmpfile = $self->{tmpfile};
1322 unlink(<$tmpfile.*>);
1325 chdir("$self->{cwd}");
1327 $main::lxdebug->leave_sub();
1333 $main::lxdebug->enter_sub();
1335 my ($self, $date, $myconfig) = @_;
1337 if ($date && $date =~ /\D/) {
1339 if ($myconfig->{dateformat} =~ /^yy/) {
1340 ($yy, $mm, $dd) = split /\D/, $date;
1342 if ($myconfig->{dateformat} =~ /^mm/) {
1343 ($mm, $dd, $yy) = split /\D/, $date;
1345 if ($myconfig->{dateformat} =~ /^dd/) {
1346 ($dd, $mm, $yy) = split /\D/, $date;
1351 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1352 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1354 $dd = "0$dd" if ($dd < 10);
1355 $mm = "0$mm" if ($mm < 10);
1357 $date = "$yy$mm$dd";
1360 $main::lxdebug->leave_sub();
1365 # Database routines used throughout
1368 $main::lxdebug->enter_sub(2);
1370 my ($self, $myconfig) = @_;
1372 # connect to database
1374 DBI->connect($myconfig->{dbconnect},
1375 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1379 if ($myconfig->{dboptions}) {
1380 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1383 $main::lxdebug->leave_sub(2);
1388 sub dbconnect_noauto {
1389 $main::lxdebug->enter_sub();
1391 my ($self, $myconfig) = @_;
1393 # connect to database
1395 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1396 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1400 if ($myconfig->{dboptions}) {
1401 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1404 $main::lxdebug->leave_sub();
1409 sub get_standard_dbh {
1410 $main::lxdebug->enter_sub(2);
1412 my ($self, $myconfig) = @_;
1414 if ($standard_dbh && !$standard_dbh->{Active}) {
1415 $main::lxdebug->message(LXDebug::INFO, "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
1416 undef $standard_dbh;
1419 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1421 $main::lxdebug->leave_sub(2);
1423 return $standard_dbh;
1427 $main::lxdebug->enter_sub();
1429 my ($self, $date, $myconfig) = @_;
1430 my $dbh = $self->dbconnect($myconfig);
1432 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1433 my $sth = prepare_execute_query($self, $dbh, $query, $date);
1434 my ($closed) = $sth->fetchrow_array;
1436 $main::lxdebug->leave_sub();
1441 sub update_balance {
1442 $main::lxdebug->enter_sub();
1444 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1446 # if we have a value, go do it
1449 # retrieve balance from table
1450 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1451 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1452 my ($balance) = $sth->fetchrow_array;
1458 $query = "UPDATE $table SET $field = $balance WHERE $where";
1459 do_query($self, $dbh, $query, @values);
1461 $main::lxdebug->leave_sub();
1464 sub update_exchangerate {
1465 $main::lxdebug->enter_sub();
1467 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1469 # some sanity check for currency
1471 $main::lxdebug->leave_sub();
1474 $query = qq|SELECT curr FROM defaults|;
1476 my ($currency) = selectrow_query($self, $dbh, $query);
1477 my ($defaultcurrency) = split m/:/, $currency;
1480 if ($curr eq $defaultcurrency) {
1481 $main::lxdebug->leave_sub();
1485 $query = qq|SELECT e.curr FROM exchangerate e
1486 WHERE e.curr = ? AND e.transdate = ?
1488 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1497 $buy = conv_i($buy, "NULL");
1498 $sell = conv_i($sell, "NULL");
1501 if ($buy != 0 && $sell != 0) {
1502 $set = "buy = $buy, sell = $sell";
1503 } elsif ($buy != 0) {
1504 $set = "buy = $buy";
1505 } elsif ($sell != 0) {
1506 $set = "sell = $sell";
1509 if ($sth->fetchrow_array) {
1510 $query = qq|UPDATE exchangerate
1516 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1517 VALUES (?, $buy, $sell, ?)|;
1520 do_query($self, $dbh, $query, $curr, $transdate);
1522 $main::lxdebug->leave_sub();
1525 sub save_exchangerate {
1526 $main::lxdebug->enter_sub();
1528 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1530 my $dbh = $self->dbconnect($myconfig);
1534 $buy = $rate if $fld eq 'buy';
1535 $sell = $rate if $fld eq 'sell';
1538 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1543 $main::lxdebug->leave_sub();
1546 sub get_exchangerate {
1547 $main::lxdebug->enter_sub();
1549 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1552 unless ($transdate) {
1553 $main::lxdebug->leave_sub();
1557 $query = qq|SELECT curr FROM defaults|;
1559 my ($currency) = selectrow_query($self, $dbh, $query);
1560 my ($defaultcurrency) = split m/:/, $currency;
1562 if ($currency eq $defaultcurrency) {
1563 $main::lxdebug->leave_sub();
1567 $query = qq|SELECT e.$fld FROM exchangerate e
1568 WHERE e.curr = ? AND e.transdate = ?|;
1569 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1573 $main::lxdebug->leave_sub();
1575 return $exchangerate;
1578 sub check_exchangerate {
1579 $main::lxdebug->enter_sub();
1581 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1583 unless ($transdate) {
1584 $main::lxdebug->leave_sub();
1588 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1590 if ($currency eq $defaultcurrency) {
1591 $main::lxdebug->leave_sub();
1595 my $dbh = $self->get_standard_dbh($myconfig);
1596 my $query = qq|SELECT e.$fld FROM exchangerate e
1597 WHERE e.curr = ? AND e.transdate = ?|;
1599 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1601 $main::lxdebug->leave_sub();
1603 return $exchangerate;
1606 sub get_default_currency {
1607 $main::lxdebug->enter_sub();
1609 my ($self, $myconfig) = @_;
1610 my $dbh = $self->get_standard_dbh($myconfig);
1612 my $query = qq|SELECT curr FROM defaults|;
1614 my ($curr) = selectrow_query($self, $dbh, $query);
1615 my ($defaultcurrency) = split m/:/, $curr;
1617 $main::lxdebug->leave_sub();
1619 return $defaultcurrency;
1623 sub set_payment_options {
1624 $main::lxdebug->enter_sub();
1626 my ($self, $myconfig, $transdate) = @_;
1628 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1630 my $dbh = $self->get_standard_dbh($myconfig);
1633 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1634 qq|FROM payment_terms p | .
1637 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1638 $self->{payment_terms}) =
1639 selectrow_query($self, $dbh, $query, $self->{payment_id});
1641 if ($transdate eq "") {
1642 if ($self->{invdate}) {
1643 $transdate = $self->{invdate};
1645 $transdate = $self->{transdate};
1650 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1651 qq|FROM payment_terms|;
1652 ($self->{netto_date}, $self->{skonto_date}) =
1653 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1655 my ($invtotal, $total);
1656 my (%amounts, %formatted_amounts);
1658 if ($self->{type} =~ /_order$/) {
1659 $amounts{invtotal} = $self->{ordtotal};
1660 $amounts{total} = $self->{ordtotal};
1662 } elsif ($self->{type} =~ /_quotation$/) {
1663 $amounts{invtotal} = $self->{quototal};
1664 $amounts{total} = $self->{quototal};
1667 $amounts{invtotal} = $self->{invtotal};
1668 $amounts{total} = $self->{total};
1671 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1673 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1674 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1675 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1677 foreach (keys %amounts) {
1678 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1679 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1682 if ($self->{"language_id"}) {
1684 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1685 qq|FROM translation_payment_terms t | .
1686 qq|LEFT JOIN language l ON t.language_id = l.id | .
1687 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1688 my ($description_long, $output_numberformat, $output_dateformat,
1689 $output_longdates) =
1690 selectrow_query($self, $dbh, $query,
1691 $self->{"language_id"}, $self->{"payment_id"});
1693 $self->{payment_terms} = $description_long if ($description_long);
1695 if ($output_dateformat) {
1696 foreach my $key (qw(netto_date skonto_date)) {
1698 $main::locale->reformat_date($myconfig, $self->{$key},
1704 if ($output_numberformat &&
1705 ($output_numberformat ne $myconfig->{"numberformat"})) {
1706 my $saved_numberformat = $myconfig->{"numberformat"};
1707 $myconfig->{"numberformat"} = $output_numberformat;
1708 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1709 $myconfig->{"numberformat"} = $saved_numberformat;
1713 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1714 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1715 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1716 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1717 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1718 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1719 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1721 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1723 $main::lxdebug->leave_sub();
1727 sub get_template_language {
1728 $main::lxdebug->enter_sub();
1730 my ($self, $myconfig) = @_;
1732 my $template_code = "";
1734 if ($self->{language_id}) {
1735 my $dbh = $self->get_standard_dbh($myconfig);
1736 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1737 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1740 $main::lxdebug->leave_sub();
1742 return $template_code;
1745 sub get_printer_code {
1746 $main::lxdebug->enter_sub();
1748 my ($self, $myconfig) = @_;
1750 my $template_code = "";
1752 if ($self->{printer_id}) {
1753 my $dbh = $self->get_standard_dbh($myconfig);
1754 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1755 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1758 $main::lxdebug->leave_sub();
1760 return $template_code;
1764 $main::lxdebug->enter_sub();
1766 my ($self, $myconfig) = @_;
1768 my $template_code = "";
1770 if ($self->{shipto_id}) {
1771 my $dbh = $self->get_standard_dbh($myconfig);
1772 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1773 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1774 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1777 $main::lxdebug->leave_sub();
1781 $main::lxdebug->enter_sub();
1783 my ($self, $dbh, $id, $module) = @_;
1788 foreach my $item (qw(name department_1 department_2 street zipcode city country
1789 contact phone fax email)) {
1790 if ($self->{"shipto$item"}) {
1791 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1793 push(@values, $self->{"shipto${item}"});
1797 if ($self->{shipto_id}) {
1798 my $query = qq|UPDATE shipto set
1800 shiptodepartment_1 = ?,
1801 shiptodepartment_2 = ?,
1810 WHERE shipto_id = ?|;
1811 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1813 my $query = qq|SELECT * FROM shipto
1814 WHERE shiptoname = ? AND
1815 shiptodepartment_1 = ? AND
1816 shiptodepartment_2 = ? AND
1817 shiptostreet = ? AND
1818 shiptozipcode = ? AND
1820 shiptocountry = ? AND
1821 shiptocontact = ? AND
1827 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1830 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1831 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1832 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1833 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1834 do_query($self, $dbh, $query, $id, @values, $module);
1839 $main::lxdebug->leave_sub();
1843 $main::lxdebug->enter_sub();
1845 my ($self, $dbh) = @_;
1847 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1848 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1849 $self->{"employee_id"} *= 1;
1851 $main::lxdebug->leave_sub();
1855 $main::lxdebug->enter_sub();
1857 my ($self, $myconfig, $salesman_id) = @_;
1859 $main::lxdebug->leave_sub() and return unless $salesman_id;
1861 my $dbh = $self->get_standard_dbh($myconfig);
1864 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1868 my $user = new User($main::memberfile, $login);
1869 map({ $self->{"salesman_$_"} = $user->{$_}; }
1870 qw(address businessnumber co_ustid company duns email fax name
1872 $self->{salesman_login} = $login;
1874 $self->{salesman_name} = $login
1875 if ($self->{salesman_name} eq "");
1878 $main::lxdebug->leave_sub();
1882 $main::lxdebug->enter_sub();
1884 my ($self, $myconfig) = @_;
1886 my $dbh = $self->get_standard_dbh($myconfig);
1887 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1888 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1890 $main::lxdebug->leave_sub();
1894 $main::lxdebug->enter_sub();
1896 my ($self, $dbh, $id, $key) = @_;
1898 $key = "all_contacts" unless ($key);
1901 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1902 qq|FROM contacts | .
1903 qq|WHERE cp_cv_id = ? | .
1904 qq|ORDER BY lower(cp_name)|;
1906 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1908 $main::lxdebug->leave_sub();
1912 $main::lxdebug->enter_sub();
1914 my ($self, $dbh, $key) = @_;
1916 my ($all, $old_id, $where, @values);
1918 if (ref($key) eq "HASH") {
1921 $key = "ALL_PROJECTS";
1923 foreach my $p (keys(%{$params})) {
1925 $all = $params->{$p};
1926 } elsif ($p eq "old_id") {
1927 $old_id = $params->{$p};
1928 } elsif ($p eq "key") {
1929 $key = $params->{$p};
1935 $where = "WHERE active ";
1937 if (ref($old_id) eq "ARRAY") {
1938 my @ids = grep({ $_ } @{$old_id});
1940 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1941 push(@values, @ids);
1944 $where .= " OR (id = ?) ";
1945 push(@values, $old_id);
1951 qq|SELECT id, projectnumber, description, active | .
1954 qq|ORDER BY lower(projectnumber)|;
1956 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1958 $main::lxdebug->leave_sub();
1962 $main::lxdebug->enter_sub();
1964 my ($self, $dbh, $vc_id, $key) = @_;
1966 $key = "all_shipto" unless ($key);
1968 # get shipping addresses
1969 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1971 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1973 $main::lxdebug->leave_sub();
1977 $main::lxdebug->enter_sub();
1979 my ($self, $dbh, $key) = @_;
1981 $key = "all_printers" unless ($key);
1983 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1985 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1987 $main::lxdebug->leave_sub();
1991 $main::lxdebug->enter_sub();
1993 my ($self, $dbh, $params) = @_;
1995 $key = $params->{key};
1996 $key = "all_charts" unless ($key);
1998 my $transdate = quote_db_date($params->{transdate});
2001 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
2003 qq|LEFT JOIN taxkeys tk ON | .
2004 qq|(tk.id = (SELECT id FROM taxkeys | .
2005 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
2006 qq| ORDER BY startdate DESC LIMIT 1)) | .
2007 qq|ORDER BY c.accno|;
2009 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2011 $main::lxdebug->leave_sub();
2014 sub _get_taxcharts {
2015 $main::lxdebug->enter_sub();
2017 my ($self, $dbh, $key) = @_;
2019 $key = "all_taxcharts" unless ($key);
2021 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
2023 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2025 $main::lxdebug->leave_sub();
2029 $main::lxdebug->enter_sub();
2031 my ($self, $dbh, $key) = @_;
2033 $key = "all_taxzones" unless ($key);
2035 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2037 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2039 $main::lxdebug->leave_sub();
2042 sub _get_employees {
2043 $main::lxdebug->enter_sub();
2045 my ($self, $dbh, $default_key, $key) = @_;
2047 $key = $default_key unless ($key);
2048 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2050 $main::lxdebug->leave_sub();
2053 sub _get_business_types {
2054 $main::lxdebug->enter_sub();
2056 my ($self, $dbh, $key) = @_;
2058 $key = "all_business_types" unless ($key);
2060 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
2062 $main::lxdebug->leave_sub();
2065 sub _get_languages {
2066 $main::lxdebug->enter_sub();
2068 my ($self, $dbh, $key) = @_;
2070 $key = "all_languages" unless ($key);
2072 my $query = qq|SELECT * FROM language ORDER BY id|;
2074 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2076 $main::lxdebug->leave_sub();
2079 sub _get_dunning_configs {
2080 $main::lxdebug->enter_sub();
2082 my ($self, $dbh, $key) = @_;
2084 $key = "all_dunning_configs" unless ($key);
2086 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2088 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2090 $main::lxdebug->leave_sub();
2093 sub _get_currencies {
2094 $main::lxdebug->enter_sub();
2096 my ($self, $dbh, $key) = @_;
2098 $key = "all_currencies" unless ($key);
2100 my $query = qq|SELECT curr AS currency FROM defaults|;
2102 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2104 $main::lxdebug->leave_sub();
2108 $main::lxdebug->enter_sub();
2110 my ($self, $dbh, $key) = @_;
2112 $key = "all_payments" unless ($key);
2114 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2116 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2118 $main::lxdebug->leave_sub();
2121 sub _get_customers {
2122 $main::lxdebug->enter_sub();
2124 my ($self, $dbh, $key, $limit) = @_;
2126 $key = "all_customers" unless ($key);
2127 $limit_clause = "LIMIT $limit" if $limit;
2129 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
2131 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2133 $main::lxdebug->leave_sub();
2137 $main::lxdebug->enter_sub();
2139 my ($self, $dbh, $key) = @_;
2141 $key = "all_vendors" unless ($key);
2143 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2145 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2147 $main::lxdebug->leave_sub();
2150 sub _get_departments {
2151 $main::lxdebug->enter_sub();
2153 my ($self, $dbh, $key) = @_;
2155 $key = "all_departments" unless ($key);
2157 my $query = qq|SELECT * FROM department ORDER BY description|;
2159 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2161 $main::lxdebug->leave_sub();
2164 sub _get_warehouses {
2165 $main::lxdebug->enter_sub();
2167 my ($self, $dbh, $param) = @_;
2169 my ($key, $bins_key);
2171 if ('' eq ref $param) {
2175 $key = $param->{key};
2176 $bins_key = $param->{bins};
2179 my $query = qq|SELECT w.* FROM warehouse w
2180 WHERE (NOT w.invalid) AND
2181 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2182 ORDER BY w.sortkey|;
2184 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2187 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2188 my $sth = prepare_query($self, $dbh, $query);
2190 foreach my $warehouse (@{ $self->{$key} }) {
2191 do_statement($self, $sth, $query, $warehouse->{id});
2192 $warehouse->{$bins_key} = [];
2194 while (my $ref = $sth->fetchrow_hashref()) {
2195 push @{ $warehouse->{$bins_key} }, $ref;
2201 $main::lxdebug->leave_sub();
2205 $main::lxdebug->enter_sub();
2207 my ($self, $dbh, $table, $key, $sortkey) = @_;
2209 my $query = qq|SELECT * FROM $table|;
2210 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2212 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2214 $main::lxdebug->leave_sub();
2218 $main::lxdebug->enter_sub();
2220 my ($self, $dbh, $key) = @_;
2222 $key ||= "all_groups";
2224 my $groups = $main::auth->read_groups();
2226 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2228 $main::lxdebug->leave_sub();
2232 $main::lxdebug->enter_sub();
2237 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2238 my ($sth, $query, $ref);
2240 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2241 my $vc_id = $self->{"${vc}_id"};
2243 if ($params{"contacts"}) {
2244 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2247 if ($params{"shipto"}) {
2248 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2251 if ($params{"projects"} || $params{"all_projects"}) {
2252 $self->_get_projects($dbh, $params{"all_projects"} ?
2253 $params{"all_projects"} : $params{"projects"},
2254 $params{"all_projects"} ? 1 : 0);
2257 if ($params{"printers"}) {
2258 $self->_get_printers($dbh, $params{"printers"});
2261 if ($params{"languages"}) {
2262 $self->_get_languages($dbh, $params{"languages"});
2265 if ($params{"charts"}) {
2266 $self->_get_charts($dbh, $params{"charts"});
2269 if ($params{"taxcharts"}) {
2270 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2273 if ($params{"taxzones"}) {
2274 $self->_get_taxzones($dbh, $params{"taxzones"});
2277 if ($params{"employees"}) {
2278 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2281 if ($params{"salesmen"}) {
2282 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2285 if ($params{"business_types"}) {
2286 $self->_get_business_types($dbh, $params{"business_types"});
2289 if ($params{"dunning_configs"}) {
2290 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2293 if($params{"currencies"}) {
2294 $self->_get_currencies($dbh, $params{"currencies"});
2297 if($params{"customers"}) {
2298 if (ref $params{"customers"} eq 'HASH') {
2299 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2301 $self->_get_customers($dbh, $params{"customers"});
2305 if($params{"vendors"}) {
2306 if (ref $params{"vendors"} eq 'HASH') {
2307 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2309 $self->_get_vendors($dbh, $params{"vendors"});
2313 if($params{"payments"}) {
2314 $self->_get_payments($dbh, $params{"payments"});
2317 if($params{"departments"}) {
2318 $self->_get_departments($dbh, $params{"departments"});
2321 if ($params{price_factors}) {
2322 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2325 if ($params{warehouses}) {
2326 $self->_get_warehouses($dbh, $params{warehouses});
2329 if ($params{groups}) {
2330 $self->_get_groups($dbh, $params{groups});
2332 if ($params{partsgroup}) {
2333 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2336 $main::lxdebug->leave_sub();
2339 # this sub gets the id and name from $table
2341 $main::lxdebug->enter_sub();
2343 my ($self, $myconfig, $table) = @_;
2345 # connect to database
2346 my $dbh = $self->get_standard_dbh($myconfig);
2348 $table = $table eq "customer" ? "customer" : "vendor";
2349 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2351 my ($query, @values);
2353 if (!$self->{openinvoices}) {
2355 if ($self->{customernumber} ne "") {
2356 $where = qq|(vc.customernumber ILIKE ?)|;
2357 push(@values, '%' . $self->{customernumber} . '%');
2359 $where = qq|(vc.name ILIKE ?)|;
2360 push(@values, '%' . $self->{$table} . '%');
2364 qq~SELECT vc.id, vc.name,
2365 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2367 WHERE $where AND (NOT vc.obsolete)
2371 qq~SELECT DISTINCT vc.id, vc.name,
2372 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2374 JOIN $table vc ON (a.${table}_id = vc.id)
2375 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2377 push(@values, '%' . $self->{$table} . '%');
2380 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2382 $main::lxdebug->leave_sub();
2384 return scalar(@{ $self->{name_list} });
2387 # the selection sub is used in the AR, AP, IS, IR and OE module
2390 $main::lxdebug->enter_sub();
2392 my ($self, $myconfig, $table, $module) = @_;
2395 my $dbh = $self->get_standard_dbh($myconfig);
2397 $table = $table eq "customer" ? "customer" : "vendor";
2399 my $query = qq|SELECT count(*) FROM $table|;
2400 my ($count) = selectrow_query($self, $dbh, $query);
2402 # build selection list
2403 if ($count < $myconfig->{vclimit}) {
2404 $query = qq|SELECT id, name, salesman_id
2405 FROM $table WHERE NOT obsolete
2407 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2411 $self->get_employee($dbh);
2413 # setup sales contacts
2414 $query = qq|SELECT e.id, e.name
2416 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2417 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2420 push(@{ $self->{all_employees} },
2421 { id => $self->{employee_id},
2422 name => $self->{employee} });
2424 # sort the whole thing
2425 @{ $self->{all_employees} } =
2426 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2428 if ($module eq 'AR') {
2430 # prepare query for departments
2431 $query = qq|SELECT id, description
2434 ORDER BY description|;
2437 $query = qq|SELECT id, description
2439 ORDER BY description|;
2442 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2445 $query = qq|SELECT id, description
2449 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2452 $query = qq|SELECT printer_description, id
2454 ORDER BY printer_description|;
2456 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2459 $query = qq|SELECT id, description
2463 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2465 $main::lxdebug->leave_sub();
2468 sub language_payment {
2469 $main::lxdebug->enter_sub();
2471 my ($self, $myconfig) = @_;
2473 my $dbh = $self->get_standard_dbh($myconfig);
2475 my $query = qq|SELECT id, description
2479 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2482 $query = qq|SELECT printer_description, id
2484 ORDER BY printer_description|;
2486 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2489 $query = qq|SELECT id, description
2493 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2495 # get buchungsgruppen
2496 $query = qq|SELECT id, description
2497 FROM buchungsgruppen|;
2499 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2501 $main::lxdebug->leave_sub();
2504 # this is only used for reports
2505 sub all_departments {
2506 $main::lxdebug->enter_sub();
2508 my ($self, $myconfig, $table) = @_;
2510 my $dbh = $self->get_standard_dbh($myconfig);
2513 if ($table eq 'customer') {
2514 $where = "WHERE role = 'P' ";
2517 my $query = qq|SELECT id, description
2520 ORDER BY description|;
2521 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2523 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2525 $main::lxdebug->leave_sub();
2529 $main::lxdebug->enter_sub();
2531 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2534 if ($table eq "customer") {
2543 $self->all_vc($myconfig, $table, $module);
2545 # get last customers or vendors
2546 my ($query, $sth, $ref);
2548 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2553 my $transdate = "current_date";
2554 if ($self->{transdate}) {
2555 $transdate = $dbh->quote($self->{transdate});
2558 # now get the account numbers
2559 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2560 FROM chart c, taxkeys tk
2561 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2562 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2565 $sth = $dbh->prepare($query);
2567 do_statement($self, $sth, $query, '%' . $module . '%');
2569 $self->{accounts} = "";
2570 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2572 foreach my $key (split(/:/, $ref->{link})) {
2573 if ($key =~ /\Q$module\E/) {
2575 # cross reference for keys
2576 $xkeyref{ $ref->{accno} } = $key;
2578 push @{ $self->{"${module}_links"}{$key} },
2579 { accno => $ref->{accno},
2580 description => $ref->{description},
2581 taxkey => $ref->{taxkey_id},
2582 tax_id => $ref->{tax_id} };
2584 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2590 # get taxkeys and description
2591 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2592 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2594 if (($module eq "AP") || ($module eq "AR")) {
2595 # get tax rates and description
2596 $query = qq|SELECT * FROM tax|;
2597 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2603 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2604 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2605 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2606 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2608 d.description AS department,
2611 JOIN $table c ON (a.${table}_id = c.id)
2612 LEFT JOIN employee e ON (e.id = a.employee_id)
2613 LEFT JOIN department d ON (d.id = a.department_id)
2615 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2617 foreach $key (keys %$ref) {
2618 $self->{$key} = $ref->{$key};
2621 my $transdate = "current_date";
2622 if ($self->{transdate}) {
2623 $transdate = $dbh->quote($self->{transdate});
2626 # now get the account numbers
2627 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2629 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2631 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2632 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2635 $sth = $dbh->prepare($query);
2636 do_statement($self, $sth, $query, "%$module%");
2638 $self->{accounts} = "";
2639 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2641 foreach my $key (split(/:/, $ref->{link})) {
2642 if ($key =~ /\Q$module\E/) {
2644 # cross reference for keys
2645 $xkeyref{ $ref->{accno} } = $key;
2647 push @{ $self->{"${module}_links"}{$key} },
2648 { accno => $ref->{accno},
2649 description => $ref->{description},
2650 taxkey => $ref->{taxkey_id},
2651 tax_id => $ref->{tax_id} };
2653 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2659 # get amounts from individual entries
2662 c.accno, c.description,
2663 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2667 LEFT JOIN chart c ON (c.id = a.chart_id)
2668 LEFT JOIN project p ON (p.id = a.project_id)
2669 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2670 WHERE (tk.taxkey_id=a.taxkey) AND
2671 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2672 THEN tk.chart_id = a.chart_id
2675 OR (c.link='%tax%')) AND
2676 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2677 WHERE a.trans_id = ?
2678 AND a.fx_transaction = '0'
2679 ORDER BY a.oid, a.transdate|;
2680 $sth = $dbh->prepare($query);
2681 do_statement($self, $sth, $query, $self->{id});
2683 # get exchangerate for currency
2684 $self->{exchangerate} =
2685 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2688 # store amounts in {acc_trans}{$key} for multiple accounts
2689 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2690 $ref->{exchangerate} =
2691 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2692 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2695 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2696 $ref->{amount} *= -1;
2698 $ref->{index} = $index;
2700 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2706 d.curr AS currencies, d.closedto, d.revtrans,
2707 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2708 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2710 $ref = selectfirst_hashref_query($self, $dbh, $query);
2711 map { $self->{$_} = $ref->{$_} } keys %$ref;
2718 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2719 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2720 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2722 $ref = selectfirst_hashref_query($self, $dbh, $query);
2723 map { $self->{$_} = $ref->{$_} } keys %$ref;
2725 if ($self->{"$self->{vc}_id"}) {
2727 # only setup currency
2728 ($self->{currency}) = split(/:/, $self->{currencies});
2732 $self->lastname_used($dbh, $myconfig, $table, $module);
2734 # get exchangerate for currency
2735 $self->{exchangerate} =
2736 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2742 $main::lxdebug->leave_sub();
2746 $main::lxdebug->enter_sub();
2748 my ($self, $dbh, $myconfig, $table, $module) = @_;
2752 $table = $table eq "customer" ? "customer" : "vendor";
2753 my %column_map = ("a.curr" => "currency",
2754 "a.${table}_id" => "${table}_id",
2755 "a.department_id" => "department_id",
2756 "d.description" => "department",
2757 "ct.name" => $table,
2758 "current_date + ct.terms" => "duedate",
2761 if ($self->{type} =~ /delivery_order/) {
2762 $arap = 'delivery_orders';
2763 delete $column_map{"a.curr"};
2765 } elsif ($self->{type} =~ /_order/) {
2767 $where = "quotation = '0'";
2769 } elsif ($self->{type} =~ /_quotation/) {
2771 $where = "quotation = '1'";
2773 } elsif ($table eq 'customer') {
2781 $where = "($where) AND" if ($where);
2782 my $query = qq|SELECT MAX(id) FROM $arap
2783 WHERE $where ${table}_id > 0|;
2784 my ($trans_id) = selectrow_query($self, $dbh, $query);
2787 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
2788 $query = qq|SELECT $column_spec
2790 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2791 LEFT JOIN department d ON (a.department_id = d.id)
2793 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
2795 map { $self->{$_} = $ref->{$_} } values %column_map;
2797 $main::lxdebug->leave_sub();
2801 $main::lxdebug->enter_sub();
2803 my ($self, $myconfig, $thisdate, $days) = @_;
2805 my $dbh = $self->get_standard_dbh($myconfig);
2810 my $dateformat = $myconfig->{dateformat};
2811 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2812 $thisdate = $dbh->quote($thisdate);
2813 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2815 $query = qq|SELECT current_date AS thisdate|;
2818 ($thisdate) = selectrow_query($self, $dbh, $query);
2820 $main::lxdebug->leave_sub();
2826 $main::lxdebug->enter_sub();
2828 my ($self, $string) = @_;
2830 if ($string !~ /%/) {
2831 $string = "%$string%";
2834 $string =~ s/\'/\'\'/g;
2836 $main::lxdebug->leave_sub();
2842 $main::lxdebug->enter_sub();
2844 my ($self, $flds, $new, $count, $numrows) = @_;
2848 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2853 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2855 $j = $item->{ndx} - 1;
2856 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2860 for $i ($count + 1 .. $numrows) {
2861 map { delete $self->{"${_}_$i"} } @{$flds};
2864 $main::lxdebug->leave_sub();
2868 $main::lxdebug->enter_sub();
2870 my ($self, $myconfig) = @_;
2874 my $dbh = $self->dbconnect_noauto($myconfig);
2876 my $query = qq|DELETE FROM status
2877 WHERE (formname = ?) AND (trans_id = ?)|;
2878 my $sth = prepare_query($self, $dbh, $query);
2880 if ($self->{formname} =~ /(check|receipt)/) {
2881 for $i (1 .. $self->{rowcount}) {
2882 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2885 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2889 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2890 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2892 my %queued = split / /, $self->{queued};
2895 if ($self->{formname} =~ /(check|receipt)/) {
2897 # this is a check or receipt, add one entry for each lineitem
2898 my ($accno) = split /--/, $self->{account};
2899 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2900 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2901 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2902 $sth = prepare_query($self, $dbh, $query);
2904 for $i (1 .. $self->{rowcount}) {
2905 if ($self->{"checked_$i"}) {
2906 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2912 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2913 VALUES (?, ?, ?, ?, ?)|;
2914 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2915 $queued{$self->{formname}}, $self->{formname});
2921 $main::lxdebug->leave_sub();
2925 $main::lxdebug->enter_sub();
2927 my ($self, $dbh) = @_;
2929 my ($query, $printed, $emailed);
2931 my $formnames = $self->{printed};
2932 my $emailforms = $self->{emailed};
2934 $query = qq|DELETE FROM status
2935 WHERE (formname = ?) AND (trans_id = ?)|;
2936 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2938 # this only applies to the forms
2939 # checks and receipts are posted when printed or queued
2941 if ($self->{queued}) {
2942 my %queued = split / /, $self->{queued};
2944 foreach my $formname (keys %queued) {
2945 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2946 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2948 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2949 VALUES (?, ?, ?, ?, ?)|;
2950 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2952 $formnames =~ s/\Q$self->{formname}\E//;
2953 $emailforms =~ s/\Q$self->{formname}\E//;
2958 # save printed, emailed info
2959 $formnames =~ s/^ +//g;
2960 $emailforms =~ s/^ +//g;
2963 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2964 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2966 foreach my $formname (keys %status) {
2967 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2968 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2970 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2971 VALUES (?, ?, ?, ?)|;
2972 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2975 $main::lxdebug->leave_sub();
2979 # $main::locale->text('SAVED')
2980 # $main::locale->text('DELETED')
2981 # $main::locale->text('ADDED')
2982 # $main::locale->text('PAYMENT POSTED')
2983 # $main::locale->text('POSTED')
2984 # $main::locale->text('POSTED AS NEW')
2985 # $main::locale->text('ELSE')
2986 # $main::locale->text('SAVED FOR DUNNING')
2987 # $main::locale->text('DUNNING STARTED')
2988 # $main::locale->text('PRINTED')
2989 # $main::locale->text('MAILED')
2990 # $main::locale->text('SCREENED')
2991 # $main::locale->text('CANCELED')
2992 # $main::locale->text('invoice')
2993 # $main::locale->text('proforma')
2994 # $main::locale->text('sales_order')
2995 # $main::locale->text('packing_list')
2996 # $main::locale->text('pick_list')
2997 # $main::locale->text('purchase_order')
2998 # $main::locale->text('bin_list')
2999 # $main::locale->text('sales_quotation')
3000 # $main::locale->text('request_quotation')
3003 $main::lxdebug->enter_sub();
3008 if(!exists $self->{employee_id}) {
3009 &get_employee($self, $dbh);
3013 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3014 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3015 my @values = (conv_i($self->{id}), $self->{login},
3016 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3017 do_query($self, $dbh, $query, @values);
3019 $main::lxdebug->leave_sub();
3023 $main::lxdebug->enter_sub();
3025 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3026 my ($orderBy, $desc) = split(/\-\-/, $order);
3027 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3030 if ($trans_id ne "") {
3032 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 | .
3033 qq|FROM history_erp h | .
3034 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3035 qq|WHERE trans_id = | . $trans_id
3036 . $restriction . qq| |
3039 my $sth = $dbh->prepare($query) || $self->dberror($query);
3041 $sth->execute() || $self->dberror("$query");
3043 while(my $hash_ref = $sth->fetchrow_hashref()) {
3044 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3045 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3046 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3047 $tempArray[$i++] = $hash_ref;
3049 $main::lxdebug->leave_sub() and return \@tempArray
3050 if ($i > 0 && $tempArray[0] ne "");
3052 $main::lxdebug->leave_sub();
3056 sub update_defaults {
3057 $main::lxdebug->enter_sub();
3059 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3062 if ($provided_dbh) {
3063 $dbh = $provided_dbh;
3065 $dbh = $self->dbconnect_noauto($myconfig);
3067 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3068 my $sth = $dbh->prepare($query);
3070 $sth->execute || $self->dberror($query);
3071 my ($var) = $sth->fetchrow_array;
3074 if ($var =~ m/\d+$/) {
3075 my $new_var = (substr $var, $-[0]) * 1 + 1;
3076 my $len_diff = length($var) - $-[0] - length($new_var);
3077 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3083 $query = qq|UPDATE defaults SET $fld = ?|;
3084 do_query($self, $dbh, $query, $var);
3086 if (!$provided_dbh) {
3091 $main::lxdebug->leave_sub();
3096 sub update_business {
3097 $main::lxdebug->enter_sub();
3099 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3102 if ($provided_dbh) {
3103 $dbh = $provided_dbh;
3105 $dbh = $self->dbconnect_noauto($myconfig);
3108 qq|SELECT customernumberinit FROM business
3109 WHERE id = ? FOR UPDATE|;
3110 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3112 if ($var =~ m/\d+$/) {
3113 my $new_var = (substr $var, $-[0]) * 1 + 1;
3114 my $len_diff = length($var) - $-[0] - length($new_var);
3115 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3121 $query = qq|UPDATE business
3122 SET customernumberinit = ?
3124 do_query($self, $dbh, $query, $var, $business_id);
3126 if (!$provided_dbh) {
3131 $main::lxdebug->leave_sub();
3136 sub get_partsgroup {
3137 $main::lxdebug->enter_sub();
3139 my ($self, $myconfig, $p) = @_;
3140 my $target = $p->{target} || 'all_partsgroup';
3142 my $dbh = $self->get_standard_dbh($myconfig);
3144 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3146 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3149 if ($p->{searchitems} eq 'part') {
3150 $query .= qq|WHERE p.inventory_accno_id > 0|;
3152 if ($p->{searchitems} eq 'service') {
3153 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3155 if ($p->{searchitems} eq 'assembly') {
3156 $query .= qq|WHERE p.assembly = '1'|;
3158 if ($p->{searchitems} eq 'labor') {
3159 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3162 $query .= qq|ORDER BY partsgroup|;
3165 $query = qq|SELECT id, partsgroup FROM partsgroup
3166 ORDER BY partsgroup|;
3169 if ($p->{language_code}) {
3170 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3171 t.description AS translation
3173 JOIN parts p ON (p.partsgroup_id = pg.id)
3174 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3175 ORDER BY translation|;
3176 @values = ($p->{language_code});
3179 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3181 $main::lxdebug->leave_sub();
3184 sub get_pricegroup {
3185 $main::lxdebug->enter_sub();
3187 my ($self, $myconfig, $p) = @_;
3189 my $dbh = $self->get_standard_dbh($myconfig);
3191 my $query = qq|SELECT p.id, p.pricegroup
3194 $query .= qq| ORDER BY pricegroup|;
3197 $query = qq|SELECT id, pricegroup FROM pricegroup
3198 ORDER BY pricegroup|;
3201 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3203 $main::lxdebug->leave_sub();
3207 # usage $form->all_years($myconfig, [$dbh])
3208 # return list of all years where bookings found
3211 $main::lxdebug->enter_sub();
3213 my ($self, $myconfig, $dbh) = @_;
3215 $dbh ||= $self->get_standard_dbh($myconfig);
3218 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3219 (SELECT MAX(transdate) FROM acc_trans)|;
3220 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3222 if ($myconfig->{dateformat} =~ /^yy/) {
3223 ($startdate) = split /\W/, $startdate;
3224 ($enddate) = split /\W/, $enddate;
3226 (@_) = split /\W/, $startdate;
3228 (@_) = split /\W/, $enddate;
3233 $startdate = substr($startdate,0,4);
3234 $enddate = substr($enddate,0,4);
3236 while ($enddate >= $startdate) {
3237 push @all_years, $enddate--;
3242 $main::lxdebug->leave_sub();
3246 $main::lxdebug->enter_sub();
3250 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
3252 $main::lxdebug->leave_sub();
3256 $main::lxdebug->enter_sub();
3261 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
3263 $main::lxdebug->leave_sub();