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 #======================================================================
44 use List::Util qw(min max);
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;
380 $main::lxdebug->enter_sub(2);
382 my ($self, $str) = @_;
385 ('order' => ['&', '"', '<', '>'],
392 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
394 $main::lxdebug->leave_sub(2);
400 $main::lxdebug->enter_sub(2);
402 my ($self, $str) = @_;
417 map { $str =~ s/\Q$_\E/$replace{$_}/g; } keys %replace;
418 $str =~ s/\&/\&/g;
420 $main::lxdebug->leave_sub(2);
430 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
432 for (sort keys %$self) {
433 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
434 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
441 $main::lxdebug->enter_sub();
443 $main::lxdebug->show_backtrace();
445 my ($self, $msg) = @_;
446 if ($ENV{HTTP_USER_AGENT}) {
448 $self->show_generic_error($msg);
455 $main::lxdebug->leave_sub();
459 $main::lxdebug->enter_sub();
461 my ($self, $msg) = @_;
463 if ($ENV{HTTP_USER_AGENT}) {
466 if (!$self->{header}) {
479 if ($self->{info_function}) {
480 &{ $self->{info_function} }($msg);
486 $main::lxdebug->leave_sub();
489 # calculates the number of rows in a textarea based on the content and column number
490 # can be capped with maxrows
492 $main::lxdebug->enter_sub();
493 my ($self, $str, $cols, $maxrows, $minrows) = @_;
497 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
500 $main::lxdebug->leave_sub();
502 return max(min($rows, $maxrows), $minrows);
506 $main::lxdebug->enter_sub();
508 my ($self, $msg) = @_;
510 $self->error("$msg\n" . $DBI::errstr);
512 $main::lxdebug->leave_sub();
516 $main::lxdebug->enter_sub();
518 my ($self, $name, $msg) = @_;
521 foreach my $part (split '.', $name) {
522 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
525 $curr = $curr->{$part};
528 $main::lxdebug->leave_sub();
532 $main::lxdebug->enter_sub();
534 my ($self, $extra_code) = @_;
536 if ($self->{header}) {
537 $main::lxdebug->leave_sub();
541 my $cgi = $main::cgi;
542 $cgi ||= CGI->new('');
544 my ($stylesheet, $favicon);
546 if ($ENV{HTTP_USER_AGENT}) {
549 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
550 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
551 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
554 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
556 $stylesheets =~ s|^\s*||;
557 $stylesheets =~ s|\s*$||;
558 foreach my $file (split m/\s+/, $stylesheets) {
560 next if (! -f "css/$file");
562 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
565 $self->{favicon} = "favicon.ico" unless $self->{favicon};
567 if ($self->{favicon} && (-f "$self->{favicon}")) {
569 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
573 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
575 if ($self->{landscape}) {
576 $pagelayout = qq|<style type="text/css">
577 \@page { size:landscape; }
581 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
585 if ($self->{jsscript} == 1) {
588 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
589 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
590 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
591 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
598 ? "$self->{title} - $self->{titlebar}"
601 foreach $item (@ { $self->{AJAX} }) {
602 $ajax .= $item->show_javascript();
607 if ($ENV{HTTP_X_FORWARDED_FOR}) {
608 $base_path = $ENV{HTTP_REFERER};
609 $base_path =~ s|^.*?://.*?/|/|;
611 $base_path = $ENV{REQUEST_URI};
613 $base_path =~ s|[^/]+$||;
614 $base_path =~ s|/$||;
617 if (defined $main::auth) {
618 my $session_cookie_value = $main::auth->get_session_id();
619 $session_cookie_value ||= 'NO_SESSION';
621 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
622 '-value' => $session_cookie_value,
623 '-path' => $base_path);
626 print $cgi->header('-type' => 'text/html',
627 '-charset' => $db_charset,
628 '-cookie' => $session_cookie);
629 print qq|${doctype}<html>
631 <title>$self->{titlebar}</title>
635 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
639 <script type="text/javascript">
647 <meta name="robots" content="noindex,nofollow" />
648 <script type="text/javascript" src="js/highlight_input.js"></script>
649 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
651 <script type="text/javascript" src="js/tabcontent.js">
653 /***********************************************
654 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
655 * This notice MUST stay intact for legal use
656 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
657 ***********************************************/
668 $main::lxdebug->leave_sub();
671 sub _prepare_html_template {
672 $main::lxdebug->enter_sub();
674 my ($self, $file, $additional_params) = @_;
677 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
678 $language = $main::language;
680 $language = $main::myconfig{"countrycode"};
682 $language = "de" unless ($language);
684 if (-f "templates/webpages/${file}_${language}.html") {
685 if ((-f ".developer") &&
686 (-f "templates/webpages/${file}_master.html") &&
687 ((stat("templates/webpages/${file}_master.html"))[9] >
688 (stat("templates/webpages/${file}_${language}.html"))[9])) {
689 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
690 "Please re-run 'locales.pl' in 'locale/${language}'.";
691 print(qq|<pre>$info</pre>|);
695 $file = "templates/webpages/${file}_${language}.html";
696 } elsif (-f "templates/webpages/${file}.html") {
697 $file = "templates/webpages/${file}.html";
699 my $info = "Web page template '${file}' not found.\n" .
700 "Please re-run 'locales.pl' in 'locale/${language}'.";
701 print(qq|<pre>$info</pre>|);
705 if ($self->{"DEBUG"}) {
706 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
709 if ($additional_params->{"DEBUG"}) {
710 $additional_params->{"DEBUG"} =
711 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
714 if (%main::myconfig) {
715 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
716 my $jsc_dateformat = $main::myconfig{"dateformat"};
717 $jsc_dateformat =~ s/d+/\%d/gi;
718 $jsc_dateformat =~ s/m+/\%m/gi;
719 $jsc_dateformat =~ s/y+/\%Y/gi;
720 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
723 $additional_params->{"conf_webdav"} = $main::webdav;
724 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
725 $additional_params->{"conf_latex_templates"} = $main::latex;
726 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
728 if (%main::debug_options) {
729 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
732 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
733 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
734 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
738 $main::lxdebug->leave_sub();
743 sub parse_html_template {
744 $main::lxdebug->enter_sub();
746 my ($self, $file, $additional_params) = @_;
748 $additional_params ||= { };
750 $file = $self->_prepare_html_template($file, $additional_params);
752 my $template = Template->new({ 'INTERPOLATE' => 0,
756 'PLUGIN_BASE' => 'SL::Template::Plugin',
757 'INCLUDE_PATH' => '.:templates/webpages',
760 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
763 if (!$template->process($file, $additional_params, \$output)) {
764 print STDERR $template->error();
766 $main::lxdebug->message(0, "err " . $template->error());
768 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
770 $main::lxdebug->leave_sub();
775 sub show_generic_error {
776 my ($self, $error, %params) = @_;
779 'title_error' => $params{title},
780 'label_error' => $error,
783 if ($params{action}) {
786 map { delete($self->{$_}); } qw(action);
787 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
789 $add_params->{SHOW_BUTTON} = 1;
790 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
791 $add_params->{VARIABLES} = \@vars;
793 } elsif ($params{back_button}) {
794 $add_params->{SHOW_BACK_BUTTON} = 1;
797 $self->{title} = $title if ($title);
800 print $self->parse_html_template("generic/error", $add_params);
802 die("Error: $error\n");
805 sub show_generic_information {
806 my ($self, $text, $title) = @_;
809 'title_information' => $title,
810 'label_information' => $text,
813 $self->{title} = $title if ($title);
816 print $self->parse_html_template("generic/information", $add_params);
818 die("Information: $error\n");
821 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
822 # changed it to accept an arbitrary number of triggers - sschoeling
824 $main::lxdebug->enter_sub();
827 my $myconfig = shift;
830 # set dateform for jsscript
833 "dd.mm.yy" => "%d.%m.%Y",
834 "dd-mm-yy" => "%d-%m-%Y",
835 "dd/mm/yy" => "%d/%m/%Y",
836 "mm/dd/yy" => "%m/%d/%Y",
837 "mm-dd-yy" => "%m-%d-%Y",
838 "yyyy-mm-dd" => "%Y-%m-%d",
841 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
842 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
849 inputField : "| . (shift) . qq|",
850 ifFormat :"$ifFormat",
851 align : "| . (shift) . qq|",
852 button : "| . (shift) . qq|"
858 <script type="text/javascript">
859 <!--| . join("", @triggers) . qq|//-->
863 $main::lxdebug->leave_sub();
866 } #end sub write_trigger
869 $main::lxdebug->enter_sub();
871 my ($self, $msg) = @_;
873 if ($self->{callback}) {
875 ($script, $argv) = split(/\?/, $self->{callback}, 2);
877 $script =~ s|[^a-zA-Z0-9_\.]||g;
878 exec("perl", "$script", $argv);
886 $main::lxdebug->leave_sub();
889 # sort of columns removed - empty sub
891 $main::lxdebug->enter_sub();
893 my ($self, @columns) = @_;
895 $main::lxdebug->leave_sub();
901 $main::lxdebug->enter_sub(2);
903 my ($self, $myconfig, $amount, $places, $dash) = @_;
909 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
911 my $neg = ($amount =~ s/^-//);
912 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
914 if (defined($places) && ($places ne '')) {
920 my ($actual_places) = ($amount =~ /\.(\d+)/);
921 $actual_places = length($actual_places);
922 $places = $actual_places > $places ? $actual_places : $places;
925 $amount = $self->round_amount($amount, $places);
928 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
929 my @p = split(/\./, $amount); # split amount at decimal point
931 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
934 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
937 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
938 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
939 ($neg ? "-$amount" : "$amount" ) ;
943 $main::lxdebug->leave_sub(2);
947 sub format_amount_units {
948 $main::lxdebug->enter_sub();
953 Common::check_params(\%params, qw(amount part_unit));
955 my $myconfig = \%main::myconfig;
956 my $amount = $params{amount};
957 my $places = $params{places};
958 my $part_unit_name = $params{part_unit};
959 my $amount_unit_name = $params{amount_unit};
960 my $conv_units = $params{conv_units};
961 my $max_places = $params{max_places};
963 AM->retrieve_all_units();
964 my $all_units = $main::all_units;
966 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
967 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
970 if (!scalar @{ $conv_units }) {
971 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
972 $main::lxdebug->leave_sub();
976 my $part_unit = $all_units->{$part_unit_name};
977 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
979 $amount *= $conv_unit->{factor};
983 foreach my $unit (@$conv_units) {
984 my $last = $unit->{name} eq $part_unit->{name};
986 $num = int($amount / $unit->{factor});
987 $amount -= $num * $unit->{factor};
990 if ($last ? $amount : $num) {
991 push @values, { "unit" => $unit->{name},
992 "amount" => $last ? $amount / $unit->{factor} : $num,
993 "places" => $last ? $places : 0 };
1000 push @values, { "unit" => $part_unit_name,
1005 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
1007 $main::lxdebug->leave_sub();
1013 $main::lxdebug->enter_sub(2);
1018 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
1019 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
1020 $input =~ s/\#\#/\#/g;
1022 $main::lxdebug->leave_sub(2);
1030 $main::lxdebug->enter_sub(2);
1032 my ($self, $myconfig, $amount) = @_;
1034 if ( ($myconfig->{numberformat} eq '1.000,00')
1035 || ($myconfig->{numberformat} eq '1000,00')) {
1040 if ($myconfig->{numberformat} eq "1'000.00") {
1046 $main::lxdebug->leave_sub(2);
1048 return ($amount * 1);
1052 $main::lxdebug->enter_sub(2);
1054 my ($self, $amount, $places) = @_;
1057 # Rounding like "Kaufmannsrunden"
1058 # Descr. http://de.wikipedia.org/wiki/Rundung
1060 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
1063 $amount = $amount * (10**($places));
1064 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1066 $main::lxdebug->leave_sub(2);
1068 return $round_amount;
1072 sub parse_template {
1073 $main::lxdebug->enter_sub();
1075 my ($self, $myconfig, $userspath) = @_;
1076 my ($template, $out);
1080 $self->{"cwd"} = getcwd();
1081 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1083 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1084 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1085 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1086 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1087 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1088 } elsif (($self->{"format"} =~ /html/i) ||
1089 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1090 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1091 } elsif (($self->{"format"} =~ /xml/i) ||
1092 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1093 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1094 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
1095 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1096 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
1097 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1098 } elsif ( defined $self->{'format'}) {
1099 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1100 } elsif ( $self->{'format'} eq '' ) {
1101 $self->error("No Outputformat given: $self->{'format'}");
1102 } else { #Catch the rest
1103 $self->error("Outputformat not defined: $self->{'format'}");
1106 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1107 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1109 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
1110 qw(email tel fax name signature company address businessnumber
1111 co_ustid taxnumber duns));
1113 map({ $self->{"${_}"} = $myconfig->{$_}; }
1117 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1119 # OUT is used for the media, screen, printer, email
1120 # for postscript we store a copy in a temporary file
1122 my $prepend_userspath;
1124 if (!$self->{tmpfile}) {
1125 $self->{tmpfile} = "${fileid}.$self->{IN}";
1126 $prepend_userspath = 1;
1129 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1131 $self->{tmpfile} =~ s|.*/||;
1132 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1133 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1135 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1136 $out = $self->{OUT};
1137 $self->{OUT} = ">$self->{tmpfile}";
1141 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
1143 open(OUT, ">-") or $self->error("STDOUT : $!");
1147 if (!$template->parse(*OUT)) {
1149 $self->error("$self->{IN} : " . $template->get_error());
1154 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1156 if ($self->{media} eq 'email') {
1158 my $mail = new Mailer;
1160 map { $mail->{$_} = $self->{$_} }
1161 qw(cc bcc subject message version format);
1162 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1163 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1164 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1165 $mail->{fileid} = "$fileid.";
1166 $myconfig->{signature} =~ s/\r//g;
1168 # if we send html or plain text inline
1169 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1170 $mail->{contenttype} = "text/html";
1172 $mail->{message} =~ s/\r//g;
1173 $mail->{message} =~ s/\n/<br>\n/g;
1174 $myconfig->{signature} =~ s/\n/<br>\n/g;
1175 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1177 open(IN, $self->{tmpfile})
1178 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1180 $mail->{message} .= $_;
1187 if (!$self->{"do_not_attach"}) {
1188 @{ $mail->{attachments} } =
1189 ({ "filename" => $self->{"tmpfile"},
1190 "name" => $self->{"attachment_filename"} ?
1191 $self->{"attachment_filename"} : $self->{"tmpfile"} });
1194 $mail->{message} =~ s/\r//g;
1195 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1199 my $err = $mail->send();
1200 $self->error($self->cleanup . "$err") if ($err);
1204 $self->{OUT} = $out;
1206 my $numbytes = (-s $self->{tmpfile});
1207 open(IN, $self->{tmpfile})
1208 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1210 $self->{copies} = 1 unless $self->{media} eq 'printer';
1212 chdir("$self->{cwd}");
1213 #print(STDERR "Kopien $self->{copies}\n");
1214 #print(STDERR "OUT $self->{OUT}\n");
1215 for my $i (1 .. $self->{copies}) {
1217 open(OUT, $self->{OUT})
1218 or $self->error($self->cleanup . "$self->{OUT} : $!");
1220 $self->{attachment_filename} = ($self->{attachment_filename})
1221 ? $self->{attachment_filename}
1222 : $self->generate_attachment_filename();
1224 # launch application
1225 print qq|Content-Type: | . $template->get_mime_type() . qq|
1226 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1227 Content-Length: $numbytes
1231 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1251 chdir("$self->{cwd}");
1252 $main::lxdebug->leave_sub();
1255 sub get_formname_translation {
1256 my ($self, $formname) = @_;
1258 $formname ||= $self->{formname};
1260 my %formname_translations = (
1261 bin_list => $main::locale->text('Bin List'),
1262 credit_note => $main::locale->text('Credit Note'),
1263 invoice => $main::locale->text('Invoice'),
1264 packing_list => $main::locale->text('Packing List'),
1265 pick_list => $main::locale->text('Pick List'),
1266 proforma => $main::locale->text('Proforma Invoice'),
1267 purchase_order => $main::locale->text('Purchase Order'),
1268 request_quotation => $main::locale->text('RFQ'),
1269 sales_order => $main::locale->text('Confirmation'),
1270 sales_quotation => $main::locale->text('Quotation'),
1271 storno_invoice => $main::locale->text('Storno Invoice'),
1272 storno_packing_list => $main::locale->text('Storno Packing List'),
1273 sales_delivery_order => $main::locale->text('Delivery Order'),
1274 purchase_delivery_order => $main::locale->text('Delivery Order'),
1277 return $formname_translations{$formname}
1280 sub generate_attachment_filename {
1283 my $attachment_filename = $self->unquote_html($self->get_formname_translation());
1285 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1286 : ($self->{type} =~ /_quotation$/) ? 'quo'
1287 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1290 if ($attachment_filename && $self->{"${prefix}number"}) {
1291 $attachment_filename .= "_" . $self->{"${prefix}number"}
1292 . ( $self->{format} =~ /pdf/i ? ".pdf"
1293 : $self->{format} =~ /postscript/i ? ".ps"
1294 : $self->{format} =~ /opendocument/i ? ".odt"
1295 : $self->{format} =~ /html/i ? ".html"
1297 $attachment_filename =~ s/ /_/g;
1298 my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue",
1299 "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
1300 map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
1302 $attachment_filename = "";
1305 return $attachment_filename;
1309 $main::lxdebug->enter_sub();
1313 chdir("$self->{tmpdir}");
1316 if (-f "$self->{tmpfile}.err") {
1317 open(FH, "$self->{tmpfile}.err");
1322 if ($self->{tmpfile}) {
1323 $self->{tmpfile} =~ s|.*/||g;
1325 $self->{tmpfile} =~ s/\.\w+$//g;
1326 my $tmpfile = $self->{tmpfile};
1327 unlink(<$tmpfile.*>);
1330 chdir("$self->{cwd}");
1332 $main::lxdebug->leave_sub();
1338 $main::lxdebug->enter_sub();
1340 my ($self, $date, $myconfig) = @_;
1342 if ($date && $date =~ /\D/) {
1344 if ($myconfig->{dateformat} =~ /^yy/) {
1345 ($yy, $mm, $dd) = split /\D/, $date;
1347 if ($myconfig->{dateformat} =~ /^mm/) {
1348 ($mm, $dd, $yy) = split /\D/, $date;
1350 if ($myconfig->{dateformat} =~ /^dd/) {
1351 ($dd, $mm, $yy) = split /\D/, $date;
1356 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1357 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1359 $dd = "0$dd" if ($dd < 10);
1360 $mm = "0$mm" if ($mm < 10);
1362 $date = "$yy$mm$dd";
1365 $main::lxdebug->leave_sub();
1370 # Database routines used throughout
1373 $main::lxdebug->enter_sub(2);
1375 my ($self, $myconfig) = @_;
1377 # connect to database
1379 DBI->connect($myconfig->{dbconnect},
1380 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1384 if ($myconfig->{dboptions}) {
1385 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1388 $main::lxdebug->leave_sub(2);
1393 sub dbconnect_noauto {
1394 $main::lxdebug->enter_sub();
1396 my ($self, $myconfig) = @_;
1398 # connect to database
1400 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1401 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1405 if ($myconfig->{dboptions}) {
1406 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1409 $main::lxdebug->leave_sub();
1414 sub get_standard_dbh {
1415 $main::lxdebug->enter_sub(2);
1417 my ($self, $myconfig) = @_;
1419 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1421 $main::lxdebug->leave_sub(2);
1423 return $standard_dbh;
1426 sub update_balance {
1427 $main::lxdebug->enter_sub();
1429 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1431 # if we have a value, go do it
1434 # retrieve balance from table
1435 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1436 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1437 my ($balance) = $sth->fetchrow_array;
1443 $query = "UPDATE $table SET $field = $balance WHERE $where";
1444 do_query($self, $dbh, $query, @values);
1446 $main::lxdebug->leave_sub();
1449 sub update_exchangerate {
1450 $main::lxdebug->enter_sub();
1452 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1454 # some sanity check for currency
1456 $main::lxdebug->leave_sub();
1459 $query = qq|SELECT curr FROM defaults|;
1461 my ($currency) = selectrow_query($self, $dbh, $query);
1462 my ($defaultcurrency) = split m/:/, $currency;
1465 if ($curr eq $defaultcurrency) {
1466 $main::lxdebug->leave_sub();
1470 $query = qq|SELECT e.curr FROM exchangerate e
1471 WHERE e.curr = ? AND e.transdate = ?
1473 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1482 $buy = conv_i($buy, "NULL");
1483 $sell = conv_i($sell, "NULL");
1486 if ($buy != 0 && $sell != 0) {
1487 $set = "buy = $buy, sell = $sell";
1488 } elsif ($buy != 0) {
1489 $set = "buy = $buy";
1490 } elsif ($sell != 0) {
1491 $set = "sell = $sell";
1494 if ($sth->fetchrow_array) {
1495 $query = qq|UPDATE exchangerate
1501 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1502 VALUES (?, $buy, $sell, ?)|;
1505 do_query($self, $dbh, $query, $curr, $transdate);
1507 $main::lxdebug->leave_sub();
1510 sub save_exchangerate {
1511 $main::lxdebug->enter_sub();
1513 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1515 my $dbh = $self->dbconnect($myconfig);
1519 $buy = $rate if $fld eq 'buy';
1520 $sell = $rate if $fld eq 'sell';
1523 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1528 $main::lxdebug->leave_sub();
1531 sub get_exchangerate {
1532 $main::lxdebug->enter_sub();
1534 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1537 unless ($transdate) {
1538 $main::lxdebug->leave_sub();
1542 $query = qq|SELECT curr FROM defaults|;
1544 my ($currency) = selectrow_query($self, $dbh, $query);
1545 my ($defaultcurrency) = split m/:/, $currency;
1547 if ($currency eq $defaultcurrency) {
1548 $main::lxdebug->leave_sub();
1552 $query = qq|SELECT e.$fld FROM exchangerate e
1553 WHERE e.curr = ? AND e.transdate = ?|;
1554 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1558 $main::lxdebug->leave_sub();
1560 return $exchangerate;
1563 sub check_exchangerate {
1564 $main::lxdebug->enter_sub();
1566 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1568 unless ($transdate) {
1569 $main::lxdebug->leave_sub();
1573 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1575 if ($currency eq $defaultcurrency) {
1576 $main::lxdebug->leave_sub();
1580 my $dbh = $self->get_standard_dbh($myconfig);
1581 my $query = qq|SELECT e.$fld FROM exchangerate e
1582 WHERE e.curr = ? AND e.transdate = ?|;
1584 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1586 $exchangerate = 1 if ($exchangerate eq "");
1588 $main::lxdebug->leave_sub();
1590 return $exchangerate;
1593 sub get_default_currency {
1594 $main::lxdebug->enter_sub();
1596 my ($self, $myconfig) = @_;
1597 my $dbh = $self->get_standard_dbh($myconfig);
1599 my $query = qq|SELECT curr FROM defaults|;
1601 my ($curr) = selectrow_query($self, $dbh, $query);
1602 my ($defaultcurrency) = split m/:/, $curr;
1604 $main::lxdebug->leave_sub();
1606 return $defaultcurrency;
1610 sub set_payment_options {
1611 $main::lxdebug->enter_sub();
1613 my ($self, $myconfig, $transdate) = @_;
1615 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1617 my $dbh = $self->get_standard_dbh($myconfig);
1620 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1621 qq|FROM payment_terms p | .
1624 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1625 $self->{payment_terms}) =
1626 selectrow_query($self, $dbh, $query, $self->{payment_id});
1628 if ($transdate eq "") {
1629 if ($self->{invdate}) {
1630 $transdate = $self->{invdate};
1632 $transdate = $self->{transdate};
1637 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1638 qq|FROM payment_terms|;
1639 ($self->{netto_date}, $self->{skonto_date}) =
1640 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1642 my ($invtotal, $total);
1643 my (%amounts, %formatted_amounts);
1645 if ($self->{type} =~ /_order$/) {
1646 $amounts{invtotal} = $self->{ordtotal};
1647 $amounts{total} = $self->{ordtotal};
1649 } elsif ($self->{type} =~ /_quotation$/) {
1650 $amounts{invtotal} = $self->{quototal};
1651 $amounts{total} = $self->{quototal};
1654 $amounts{invtotal} = $self->{invtotal};
1655 $amounts{total} = $self->{total};
1658 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1660 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1661 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1662 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1664 foreach (keys %amounts) {
1665 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1666 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1669 if ($self->{"language_id"}) {
1671 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1672 qq|FROM translation_payment_terms t | .
1673 qq|LEFT JOIN language l ON t.language_id = l.id | .
1674 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1675 my ($description_long, $output_numberformat, $output_dateformat,
1676 $output_longdates) =
1677 selectrow_query($self, $dbh, $query,
1678 $self->{"language_id"}, $self->{"payment_id"});
1680 $self->{payment_terms} = $description_long if ($description_long);
1682 if ($output_dateformat) {
1683 foreach my $key (qw(netto_date skonto_date)) {
1685 $main::locale->reformat_date($myconfig, $self->{$key},
1691 if ($output_numberformat &&
1692 ($output_numberformat ne $myconfig->{"numberformat"})) {
1693 my $saved_numberformat = $myconfig->{"numberformat"};
1694 $myconfig->{"numberformat"} = $output_numberformat;
1695 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1696 $myconfig->{"numberformat"} = $saved_numberformat;
1700 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1701 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1702 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1703 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1704 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1705 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1706 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1708 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1710 $main::lxdebug->leave_sub();
1714 sub get_template_language {
1715 $main::lxdebug->enter_sub();
1717 my ($self, $myconfig) = @_;
1719 my $template_code = "";
1721 if ($self->{language_id}) {
1722 my $dbh = $self->get_standard_dbh($myconfig);
1723 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1724 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1727 $main::lxdebug->leave_sub();
1729 return $template_code;
1732 sub get_printer_code {
1733 $main::lxdebug->enter_sub();
1735 my ($self, $myconfig) = @_;
1737 my $template_code = "";
1739 if ($self->{printer_id}) {
1740 my $dbh = $self->get_standard_dbh($myconfig);
1741 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1742 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1745 $main::lxdebug->leave_sub();
1747 return $template_code;
1751 $main::lxdebug->enter_sub();
1753 my ($self, $myconfig) = @_;
1755 my $template_code = "";
1757 if ($self->{shipto_id}) {
1758 my $dbh = $self->get_standard_dbh($myconfig);
1759 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1760 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1761 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1764 $main::lxdebug->leave_sub();
1768 $main::lxdebug->enter_sub();
1770 my ($self, $dbh, $id, $module) = @_;
1775 foreach my $item (qw(name department_1 department_2 street zipcode city country
1776 contact phone fax email)) {
1777 if ($self->{"shipto$item"}) {
1778 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1780 push(@values, $self->{"shipto${item}"});
1784 if ($self->{shipto_id}) {
1785 my $query = qq|UPDATE shipto set
1787 shiptodepartment_1 = ?,
1788 shiptodepartment_2 = ?,
1797 WHERE shipto_id = ?|;
1798 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1800 my $query = qq|SELECT * FROM shipto
1801 WHERE shiptoname = ? AND
1802 shiptodepartment_1 = ? AND
1803 shiptodepartment_2 = ? AND
1804 shiptostreet = ? AND
1805 shiptozipcode = ? AND
1807 shiptocountry = ? AND
1808 shiptocontact = ? AND
1814 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1817 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1818 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1819 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1820 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1821 do_query($self, $dbh, $query, $id, @values, $module);
1826 $main::lxdebug->leave_sub();
1830 $main::lxdebug->enter_sub();
1832 my ($self, $dbh) = @_;
1834 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1835 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1836 $self->{"employee_id"} *= 1;
1838 $main::lxdebug->leave_sub();
1842 $main::lxdebug->enter_sub();
1844 my ($self, $myconfig, $salesman_id) = @_;
1846 $main::lxdebug->leave_sub() and return unless $salesman_id;
1848 my $dbh = $self->get_standard_dbh($myconfig);
1851 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1855 my $user = new User($main::memberfile, $login);
1856 map({ $self->{"salesman_$_"} = $user->{$_}; }
1857 qw(address businessnumber co_ustid company duns email fax name
1859 $self->{salesman_login} = $login;
1861 $self->{salesman_name} = $login
1862 if ($self->{salesman_name} eq "");
1865 $main::lxdebug->leave_sub();
1869 $main::lxdebug->enter_sub();
1871 my ($self, $myconfig) = @_;
1873 my $dbh = $self->get_standard_dbh($myconfig);
1874 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1875 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1877 $main::lxdebug->leave_sub();
1881 $main::lxdebug->enter_sub();
1883 my ($self, $dbh, $id, $key) = @_;
1885 $key = "all_contacts" unless ($key);
1888 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1889 qq|FROM contacts | .
1890 qq|WHERE cp_cv_id = ? | .
1891 qq|ORDER BY lower(cp_name)|;
1893 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1895 $main::lxdebug->leave_sub();
1899 $main::lxdebug->enter_sub();
1901 my ($self, $dbh, $key) = @_;
1903 my ($all, $old_id, $where, @values);
1905 if (ref($key) eq "HASH") {
1908 $key = "ALL_PROJECTS";
1910 foreach my $p (keys(%{$params})) {
1912 $all = $params->{$p};
1913 } elsif ($p eq "old_id") {
1914 $old_id = $params->{$p};
1915 } elsif ($p eq "key") {
1916 $key = $params->{$p};
1922 $where = "WHERE active ";
1924 if (ref($old_id) eq "ARRAY") {
1925 my @ids = grep({ $_ } @{$old_id});
1927 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1928 push(@values, @ids);
1931 $where .= " OR (id = ?) ";
1932 push(@values, $old_id);
1938 qq|SELECT id, projectnumber, description, active | .
1941 qq|ORDER BY lower(projectnumber)|;
1943 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1945 $main::lxdebug->leave_sub();
1949 $main::lxdebug->enter_sub();
1951 my ($self, $dbh, $vc_id, $key) = @_;
1953 $key = "all_shipto" unless ($key);
1955 # get shipping addresses
1956 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1958 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1960 $main::lxdebug->leave_sub();
1964 $main::lxdebug->enter_sub();
1966 my ($self, $dbh, $key) = @_;
1968 $key = "all_printers" unless ($key);
1970 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1972 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1974 $main::lxdebug->leave_sub();
1978 $main::lxdebug->enter_sub();
1980 my ($self, $dbh, $params) = @_;
1982 $key = $params->{key};
1983 $key = "all_charts" unless ($key);
1985 my $transdate = quote_db_date($params->{transdate});
1988 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1990 qq|LEFT JOIN taxkeys tk ON | .
1991 qq|(tk.id = (SELECT id FROM taxkeys | .
1992 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1993 qq| ORDER BY startdate DESC LIMIT 1)) | .
1994 qq|ORDER BY c.accno|;
1996 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1998 $main::lxdebug->leave_sub();
2001 sub _get_taxcharts {
2002 $main::lxdebug->enter_sub();
2004 my ($self, $dbh, $key) = @_;
2006 $key = "all_taxcharts" unless ($key);
2008 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
2010 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2012 $main::lxdebug->leave_sub();
2016 $main::lxdebug->enter_sub();
2018 my ($self, $dbh, $key) = @_;
2020 $key = "all_taxzones" unless ($key);
2022 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
2024 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2026 $main::lxdebug->leave_sub();
2029 sub _get_employees {
2030 $main::lxdebug->enter_sub();
2032 my ($self, $dbh, $default_key, $key) = @_;
2034 $key = $default_key unless ($key);
2035 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
2037 $main::lxdebug->leave_sub();
2040 sub _get_business_types {
2041 $main::lxdebug->enter_sub();
2043 my ($self, $dbh, $key) = @_;
2045 $key = "all_business_types" unless ($key);
2047 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
2049 $main::lxdebug->leave_sub();
2052 sub _get_languages {
2053 $main::lxdebug->enter_sub();
2055 my ($self, $dbh, $key) = @_;
2057 $key = "all_languages" unless ($key);
2059 my $query = qq|SELECT * FROM language ORDER BY id|;
2061 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2063 $main::lxdebug->leave_sub();
2066 sub _get_dunning_configs {
2067 $main::lxdebug->enter_sub();
2069 my ($self, $dbh, $key) = @_;
2071 $key = "all_dunning_configs" unless ($key);
2073 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2075 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2077 $main::lxdebug->leave_sub();
2080 sub _get_currencies {
2081 $main::lxdebug->enter_sub();
2083 my ($self, $dbh, $key) = @_;
2085 $key = "all_currencies" unless ($key);
2087 my $query = qq|SELECT curr AS currency FROM defaults|;
2089 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2091 $main::lxdebug->leave_sub();
2095 $main::lxdebug->enter_sub();
2097 my ($self, $dbh, $key) = @_;
2099 $key = "all_payments" unless ($key);
2101 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2103 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2105 $main::lxdebug->leave_sub();
2108 sub _get_customers {
2109 $main::lxdebug->enter_sub();
2111 my ($self, $dbh, $key, $limit) = @_;
2113 $key = "all_customers" unless ($key);
2114 $limit_clause = "LIMIT $limit" if $limit;
2116 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
2118 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2120 $main::lxdebug->leave_sub();
2124 $main::lxdebug->enter_sub();
2126 my ($self, $dbh, $key) = @_;
2128 $key = "all_vendors" unless ($key);
2130 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2132 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2134 $main::lxdebug->leave_sub();
2137 sub _get_departments {
2138 $main::lxdebug->enter_sub();
2140 my ($self, $dbh, $key) = @_;
2142 $key = "all_departments" unless ($key);
2144 my $query = qq|SELECT * FROM department ORDER BY description|;
2146 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2148 $main::lxdebug->leave_sub();
2151 sub _get_warehouses {
2152 $main::lxdebug->enter_sub();
2154 my ($self, $dbh, $param) = @_;
2156 my ($key, $bins_key, $q_access, @values);
2158 if ('' eq ref $param) {
2161 $key = $param->{key};
2162 $bins_key = $param->{bins};
2164 if ($param->{access}) {
2167 SELECT wa.employee_id
2168 FROM warehouse_access wa
2169 WHERE (wa.employee_id = (SELECT id FROM employee WHERE login = ?))
2170 AND (wa.warehouse_id = w.id)
2171 AND (wa.access IN ('ro', 'rw')))|;
2172 push @values, $param->{access};
2175 if ($param->{no_personal}) {
2176 $q_access .= qq| AND (w.personal_warehouse_of IS NULL)|;
2178 } elsif ($param->{personal}) {
2179 $q_access .= qq| AND (w.personal_warehouse_of = ?)|;
2180 push @values, conv_i($param->{personal});
2184 my $query = qq|SELECT w.* FROM warehouse w
2185 WHERE (NOT w.invalid) AND
2186 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2188 ORDER BY w.sortkey|;
2190 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2193 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2194 my $sth = prepare_query($self, $dbh, $query);
2196 foreach my $warehouse (@{ $self->{$key} }) {
2197 do_statement($self, $sth, $query, $warehouse->{id});
2198 $warehouse->{$bins_key} = [];
2200 while (my $ref = $sth->fetchrow_hashref()) {
2201 push @{ $warehouse->{$bins_key} }, $ref;
2207 $main::lxdebug->leave_sub();
2211 $main::lxdebug->enter_sub();
2213 my ($self, $dbh, $table, $key, $sortkey) = @_;
2215 my $query = qq|SELECT * FROM $table|;
2216 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2218 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2220 $main::lxdebug->leave_sub();
2224 $main::lxdebug->enter_sub();
2226 my ($self, $dbh, $key) = @_;
2228 $key ||= "all_groups";
2230 my $groups = $main::auth->read_groups();
2232 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2234 $main::lxdebug->leave_sub();
2238 $main::lxdebug->enter_sub();
2243 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2244 my ($sth, $query, $ref);
2246 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2247 my $vc_id = $self->{"${vc}_id"};
2249 if ($params{"contacts"}) {
2250 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2253 if ($params{"shipto"}) {
2254 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2257 if ($params{"projects"} || $params{"all_projects"}) {
2258 $self->_get_projects($dbh, $params{"all_projects"} ?
2259 $params{"all_projects"} : $params{"projects"},
2260 $params{"all_projects"} ? 1 : 0);
2263 if ($params{"printers"}) {
2264 $self->_get_printers($dbh, $params{"printers"});
2267 if ($params{"languages"}) {
2268 $self->_get_languages($dbh, $params{"languages"});
2271 if ($params{"charts"}) {
2272 $self->_get_charts($dbh, $params{"charts"});
2275 if ($params{"taxcharts"}) {
2276 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2279 if ($params{"taxzones"}) {
2280 $self->_get_taxzones($dbh, $params{"taxzones"});
2283 if ($params{"employees"}) {
2284 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2287 if ($params{"salesmen"}) {
2288 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2291 if ($params{"business_types"}) {
2292 $self->_get_business_types($dbh, $params{"business_types"});
2295 if ($params{"dunning_configs"}) {
2296 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2299 if($params{"currencies"}) {
2300 $self->_get_currencies($dbh, $params{"currencies"});
2303 if($params{"customers"}) {
2304 if (ref $params{"customers"} eq 'HASH') {
2305 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2307 $self->_get_customers($dbh, $params{"customers"});
2311 if($params{"vendors"}) {
2312 if (ref $params{"vendors"} eq 'HASH') {
2313 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2315 $self->_get_vendors($dbh, $params{"vendors"});
2319 if($params{"payments"}) {
2320 $self->_get_payments($dbh, $params{"payments"});
2323 if($params{"departments"}) {
2324 $self->_get_departments($dbh, $params{"departments"});
2327 if ($params{price_factors}) {
2328 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2331 if ($params{warehouses}) {
2332 $self->_get_warehouses($dbh, $params{warehouses});
2335 if ($params{groups}) {
2336 $self->_get_groups($dbh, $params{groups});
2339 $main::lxdebug->leave_sub();
2342 # this sub gets the id and name from $table
2344 $main::lxdebug->enter_sub();
2346 my ($self, $myconfig, $table) = @_;
2348 # connect to database
2349 my $dbh = $self->get_standard_dbh($myconfig);
2351 $table = $table eq "customer" ? "customer" : "vendor";
2352 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2354 my ($query, @values);
2356 if (!$self->{openinvoices}) {
2358 if ($self->{customernumber} ne "") {
2359 $where = qq|(vc.customernumber ILIKE ?)|;
2360 push(@values, '%' . $self->{customernumber} . '%');
2362 $where = qq|(vc.name ILIKE ?)|;
2363 push(@values, '%' . $self->{$table} . '%');
2367 qq~SELECT vc.id, vc.name,
2368 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2370 WHERE $where AND (NOT vc.obsolete)
2374 qq~SELECT DISTINCT vc.id, vc.name,
2375 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2377 JOIN $table vc ON (a.${table}_id = vc.id)
2378 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2380 push(@values, '%' . $self->{$table} . '%');
2383 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2385 $main::lxdebug->leave_sub();
2387 return scalar(@{ $self->{name_list} });
2390 # the selection sub is used in the AR, AP, IS, IR and OE module
2393 $main::lxdebug->enter_sub();
2395 my ($self, $myconfig, $table, $module) = @_;
2398 my $dbh = $self->get_standard_dbh($myconfig);
2400 $table = $table eq "customer" ? "customer" : "vendor";
2402 my $query = qq|SELECT count(*) FROM $table|;
2403 my ($count) = selectrow_query($self, $dbh, $query);
2405 # build selection list
2406 if ($count < $myconfig->{vclimit}) {
2407 $query = qq|SELECT id, name, salesman_id
2408 FROM $table WHERE NOT obsolete
2410 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2414 $self->get_employee($dbh);
2416 # setup sales contacts
2417 $query = qq|SELECT e.id, e.name
2419 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2420 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2423 push(@{ $self->{all_employees} },
2424 { id => $self->{employee_id},
2425 name => $self->{employee} });
2427 # sort the whole thing
2428 @{ $self->{all_employees} } =
2429 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2431 if ($module eq 'AR') {
2433 # prepare query for departments
2434 $query = qq|SELECT id, description
2437 ORDER BY description|;
2440 $query = qq|SELECT id, description
2442 ORDER BY description|;
2445 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2448 $query = qq|SELECT id, description
2452 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2455 $query = qq|SELECT printer_description, id
2457 ORDER BY printer_description|;
2459 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2462 $query = qq|SELECT id, description
2466 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2468 $main::lxdebug->leave_sub();
2471 sub language_payment {
2472 $main::lxdebug->enter_sub();
2474 my ($self, $myconfig) = @_;
2476 my $dbh = $self->get_standard_dbh($myconfig);
2478 my $query = qq|SELECT id, description
2482 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2485 $query = qq|SELECT printer_description, id
2487 ORDER BY printer_description|;
2489 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2492 $query = qq|SELECT id, description
2496 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2498 # get buchungsgruppen
2499 $query = qq|SELECT id, description
2500 FROM buchungsgruppen|;
2502 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2504 $main::lxdebug->leave_sub();
2507 # this is only used for reports
2508 sub all_departments {
2509 $main::lxdebug->enter_sub();
2511 my ($self, $myconfig, $table) = @_;
2513 my $dbh = $self->get_standard_dbh($myconfig);
2516 if ($table eq 'customer') {
2517 $where = "WHERE role = 'P' ";
2520 my $query = qq|SELECT id, description
2523 ORDER BY description|;
2524 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2526 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2528 $main::lxdebug->leave_sub();
2532 $main::lxdebug->enter_sub();
2534 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2537 if ($table eq "customer") {
2546 $self->all_vc($myconfig, $table, $module);
2548 # get last customers or vendors
2549 my ($query, $sth, $ref);
2551 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2556 my $transdate = "current_date";
2557 if ($self->{transdate}) {
2558 $transdate = $dbh->quote($self->{transdate});
2561 # now get the account numbers
2562 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2563 FROM chart c, taxkeys tk
2564 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2565 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2568 $sth = $dbh->prepare($query);
2570 do_statement($self, $sth, $query, '%' . $module . '%');
2572 $self->{accounts} = "";
2573 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2575 foreach my $key (split(/:/, $ref->{link})) {
2576 if ($key =~ /\Q$module\E/) {
2578 # cross reference for keys
2579 $xkeyref{ $ref->{accno} } = $key;
2581 push @{ $self->{"${module}_links"}{$key} },
2582 { accno => $ref->{accno},
2583 description => $ref->{description},
2584 taxkey => $ref->{taxkey_id},
2585 tax_id => $ref->{tax_id} };
2587 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2593 # get taxkeys and description
2594 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2595 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2597 if (($module eq "AP") || ($module eq "AR")) {
2598 # get tax rates and description
2599 $query = qq|SELECT * FROM tax|;
2600 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2606 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2607 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2608 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2609 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2611 d.description AS department,
2614 JOIN $table c ON (a.${table}_id = c.id)
2615 LEFT JOIN employee e ON (e.id = a.employee_id)
2616 LEFT JOIN department d ON (d.id = a.department_id)
2618 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2620 foreach $key (keys %$ref) {
2621 $self->{$key} = $ref->{$key};
2624 my $transdate = "current_date";
2625 if ($self->{transdate}) {
2626 $transdate = $dbh->quote($self->{transdate});
2629 # now get the account numbers
2630 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2632 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2634 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2635 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2638 $sth = $dbh->prepare($query);
2639 do_statement($self, $sth, $query, "%$module%");
2641 $self->{accounts} = "";
2642 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2644 foreach my $key (split(/:/, $ref->{link})) {
2645 if ($key =~ /\Q$module\E/) {
2647 # cross reference for keys
2648 $xkeyref{ $ref->{accno} } = $key;
2650 push @{ $self->{"${module}_links"}{$key} },
2651 { accno => $ref->{accno},
2652 description => $ref->{description},
2653 taxkey => $ref->{taxkey_id},
2654 tax_id => $ref->{tax_id} };
2656 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2662 # get amounts from individual entries
2665 c.accno, c.description,
2666 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2670 LEFT JOIN chart c ON (c.id = a.chart_id)
2671 LEFT JOIN project p ON (p.id = a.project_id)
2672 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2673 WHERE (tk.taxkey_id=a.taxkey) AND
2674 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2675 THEN tk.chart_id = a.chart_id
2678 OR (c.link='%tax%')) AND
2679 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2680 WHERE a.trans_id = ?
2681 AND a.fx_transaction = '0'
2682 ORDER BY a.oid, a.transdate|;
2683 $sth = $dbh->prepare($query);
2684 do_statement($self, $sth, $query, $self->{id});
2686 # get exchangerate for currency
2687 $self->{exchangerate} =
2688 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2691 # store amounts in {acc_trans}{$key} for multiple accounts
2692 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2693 $ref->{exchangerate} =
2694 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2695 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2698 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2699 $ref->{amount} *= -1;
2701 $ref->{index} = $index;
2703 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2709 d.curr AS currencies, d.closedto, d.revtrans,
2710 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2711 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2713 $ref = selectfirst_hashref_query($self, $dbh, $query);
2714 map { $self->{$_} = $ref->{$_} } keys %$ref;
2721 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2722 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2723 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2725 $ref = selectfirst_hashref_query($self, $dbh, $query);
2726 map { $self->{$_} = $ref->{$_} } keys %$ref;
2728 if ($self->{"$self->{vc}_id"}) {
2730 # only setup currency
2731 ($self->{currency}) = split(/:/, $self->{currencies});
2735 $self->lastname_used($dbh, $myconfig, $table, $module);
2737 # get exchangerate for currency
2738 $self->{exchangerate} =
2739 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2745 $main::lxdebug->leave_sub();
2749 $main::lxdebug->enter_sub();
2751 my ($self, $dbh, $myconfig, $table, $module) = @_;
2753 my $arap = ($table eq 'customer') ? "ar" : "ap";
2754 $table = $table eq "customer" ? "customer" : "vendor";
2755 my $where = "1 = 1";
2757 if ($self->{type} =~ /_order/) {
2759 $where = "quotation = '0'";
2761 if ($self->{type} =~ /_quotation/) {
2763 $where = "quotation = '1'";
2766 my $query = qq|SELECT MAX(id) FROM $arap
2767 WHERE $where AND ${table}_id > 0|;
2768 my ($trans_id) = selectrow_query($self, $dbh, $query);
2773 a.curr, a.${table}_id, a.department_id,
2774 d.description AS department,
2775 ct.name, current_date + ct.terms AS duedate
2777 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2778 LEFT JOIN department d ON (a.department_id = d.id)
2780 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2781 $self->{department}, $self->{$table}, $self->{duedate})
2782 = selectrow_query($self, $dbh, $query, $trans_id);
2784 $main::lxdebug->leave_sub();
2788 $main::lxdebug->enter_sub();
2790 my ($self, $myconfig, $thisdate, $days) = @_;
2792 my $dbh = $self->get_standard_dbh($myconfig);
2797 my $dateformat = $myconfig->{dateformat};
2798 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2799 $thisdate = $dbh->quote($thisdate);
2800 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2802 $query = qq|SELECT current_date AS thisdate|;
2805 ($thisdate) = selectrow_query($self, $dbh, $query);
2807 $main::lxdebug->leave_sub();
2813 $main::lxdebug->enter_sub();
2815 my ($self, $string) = @_;
2817 if ($string !~ /%/) {
2818 $string = "%$string%";
2821 $string =~ s/\'/\'\'/g;
2823 $main::lxdebug->leave_sub();
2829 $main::lxdebug->enter_sub();
2831 my ($self, $flds, $new, $count, $numrows) = @_;
2835 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2840 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2842 $j = $item->{ndx} - 1;
2843 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2847 for $i ($count + 1 .. $numrows) {
2848 map { delete $self->{"${_}_$i"} } @{$flds};
2851 $main::lxdebug->leave_sub();
2855 $main::lxdebug->enter_sub();
2857 my ($self, $myconfig) = @_;
2861 my $dbh = $self->dbconnect_noauto($myconfig);
2863 my $query = qq|DELETE FROM status
2864 WHERE (formname = ?) AND (trans_id = ?)|;
2865 my $sth = prepare_query($self, $dbh, $query);
2867 if ($self->{formname} =~ /(check|receipt)/) {
2868 for $i (1 .. $self->{rowcount}) {
2869 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2872 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2876 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2877 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2879 my %queued = split / /, $self->{queued};
2882 if ($self->{formname} =~ /(check|receipt)/) {
2884 # this is a check or receipt, add one entry for each lineitem
2885 my ($accno) = split /--/, $self->{account};
2886 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2887 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2888 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2889 $sth = prepare_query($self, $dbh, $query);
2891 for $i (1 .. $self->{rowcount}) {
2892 if ($self->{"checked_$i"}) {
2893 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2899 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2900 VALUES (?, ?, ?, ?, ?)|;
2901 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2902 $queued{$self->{formname}}, $self->{formname});
2908 $main::lxdebug->leave_sub();
2912 $main::lxdebug->enter_sub();
2914 my ($self, $dbh) = @_;
2916 my ($query, $printed, $emailed);
2918 my $formnames = $self->{printed};
2919 my $emailforms = $self->{emailed};
2921 $query = qq|DELETE FROM status
2922 WHERE (formname = ?) AND (trans_id = ?)|;
2923 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2925 # this only applies to the forms
2926 # checks and receipts are posted when printed or queued
2928 if ($self->{queued}) {
2929 my %queued = split / /, $self->{queued};
2931 foreach my $formname (keys %queued) {
2932 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2933 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2935 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2936 VALUES (?, ?, ?, ?, ?)|;
2937 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2939 $formnames =~ s/\Q$self->{formname}\E//;
2940 $emailforms =~ s/\Q$self->{formname}\E//;
2945 # save printed, emailed info
2946 $formnames =~ s/^ +//g;
2947 $emailforms =~ s/^ +//g;
2950 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2951 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2953 foreach my $formname (keys %status) {
2954 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2955 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2957 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2958 VALUES (?, ?, ?, ?)|;
2959 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2962 $main::lxdebug->leave_sub();
2966 # $main::locale->text('SAVED')
2967 # $main::locale->text('DELETED')
2968 # $main::locale->text('ADDED')
2969 # $main::locale->text('PAYMENT POSTED')
2970 # $main::locale->text('POSTED')
2971 # $main::locale->text('POSTED AS NEW')
2972 # $main::locale->text('ELSE')
2973 # $main::locale->text('SAVED FOR DUNNING')
2974 # $main::locale->text('DUNNING STARTED')
2975 # $main::locale->text('PRINTED')
2976 # $main::locale->text('MAILED')
2977 # $main::locale->text('SCREENED')
2978 # $main::locale->text('CANCELED')
2979 # $main::locale->text('invoice')
2980 # $main::locale->text('proforma')
2981 # $main::locale->text('sales_order')
2982 # $main::locale->text('packing_list')
2983 # $main::locale->text('pick_list')
2984 # $main::locale->text('purchase_order')
2985 # $main::locale->text('bin_list')
2986 # $main::locale->text('sales_quotation')
2987 # $main::locale->text('request_quotation')
2990 $main::lxdebug->enter_sub();
2995 if(!exists $self->{employee_id}) {
2996 &get_employee($self, $dbh);
3000 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
3001 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
3002 my @values = (conv_i($self->{id}), $self->{login},
3003 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
3004 do_query($self, $dbh, $query, @values);
3006 $main::lxdebug->leave_sub();
3010 $main::lxdebug->enter_sub();
3012 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
3013 my ($orderBy, $desc) = split(/\-\-/, $order);
3014 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
3017 if ($trans_id ne "") {
3019 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 | .
3020 qq|FROM history_erp h | .
3021 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
3022 qq|WHERE trans_id = | . $trans_id
3023 . $restriction . qq| |
3026 my $sth = $dbh->prepare($query) || $self->dberror($query);
3028 $sth->execute() || $self->dberror("$query");
3030 while(my $hash_ref = $sth->fetchrow_hashref()) {
3031 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
3032 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
3033 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
3034 $tempArray[$i++] = $hash_ref;
3036 $main::lxdebug->leave_sub() and return \@tempArray
3037 if ($i > 0 && $tempArray[0] ne "");
3039 $main::lxdebug->leave_sub();
3043 sub update_defaults {
3044 $main::lxdebug->enter_sub();
3046 my ($self, $myconfig, $fld, $provided_dbh) = @_;
3049 if ($provided_dbh) {
3050 $dbh = $provided_dbh;
3052 $dbh = $self->dbconnect_noauto($myconfig);
3054 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3055 my $sth = $dbh->prepare($query);
3057 $sth->execute || $self->dberror($query);
3058 my ($var) = $sth->fetchrow_array;
3061 if ($var =~ m/\d+$/) {
3062 my $new_var = (substr $var, $-[0]) * 1 + 1;
3063 my $len_diff = length($var) - $-[0] - length($new_var);
3064 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3070 $query = qq|UPDATE defaults SET $fld = ?|;
3071 do_query($self, $dbh, $query, $var);
3073 if (!$provided_dbh) {
3078 $main::lxdebug->leave_sub();
3083 sub update_business {
3084 $main::lxdebug->enter_sub();
3086 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3089 if ($provided_dbh) {
3090 $dbh = $provided_dbh;
3092 $dbh = $self->dbconnect_noauto($myconfig);
3095 qq|SELECT customernumberinit FROM business
3096 WHERE id = ? FOR UPDATE|;
3097 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3099 if ($var =~ m/\d+$/) {
3100 my $new_var = (substr $var, $-[0]) * 1 + 1;
3101 my $len_diff = length($var) - $-[0] - length($new_var);
3102 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3108 $query = qq|UPDATE business
3109 SET customernumberinit = ?
3111 do_query($self, $dbh, $query, $var, $business_id);
3113 if (!$provided_dbh) {
3118 $main::lxdebug->leave_sub();
3123 sub get_partsgroup {
3124 $main::lxdebug->enter_sub();
3126 my ($self, $myconfig, $p) = @_;
3128 my $dbh = $self->get_standard_dbh($myconfig);
3130 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3132 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3135 if ($p->{searchitems} eq 'part') {
3136 $query .= qq|WHERE p.inventory_accno_id > 0|;
3138 if ($p->{searchitems} eq 'service') {
3139 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3141 if ($p->{searchitems} eq 'assembly') {
3142 $query .= qq|WHERE p.assembly = '1'|;
3144 if ($p->{searchitems} eq 'labor') {
3145 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3148 $query .= qq|ORDER BY partsgroup|;
3151 $query = qq|SELECT id, partsgroup FROM partsgroup
3152 ORDER BY partsgroup|;
3155 if ($p->{language_code}) {
3156 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3157 t.description AS translation
3159 JOIN parts p ON (p.partsgroup_id = pg.id)
3160 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3161 ORDER BY translation|;
3162 @values = ($p->{language_code});
3165 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
3167 $main::lxdebug->leave_sub();
3170 sub get_pricegroup {
3171 $main::lxdebug->enter_sub();
3173 my ($self, $myconfig, $p) = @_;
3175 my $dbh = $self->get_standard_dbh($myconfig);
3177 my $query = qq|SELECT p.id, p.pricegroup
3180 $query .= qq| ORDER BY pricegroup|;
3183 $query = qq|SELECT id, pricegroup FROM pricegroup
3184 ORDER BY pricegroup|;
3187 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3189 $main::lxdebug->leave_sub();
3193 # usage $form->all_years($myconfig, [$dbh])
3194 # return list of all years where bookings found
3197 $main::lxdebug->enter_sub();
3199 my ($self, $myconfig, $dbh) = @_;
3201 $dbh ||= $self->get_standard_dbh($myconfig);
3204 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3205 (SELECT MAX(transdate) FROM acc_trans)|;
3206 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3208 if ($myconfig->{dateformat} =~ /^yy/) {
3209 ($startdate) = split /\W/, $startdate;
3210 ($enddate) = split /\W/, $enddate;
3212 (@_) = split /\W/, $startdate;
3214 (@_) = split /\W/, $enddate;
3219 $startdate = substr($startdate,0,4);
3220 $enddate = substr($enddate,0,4);
3222 while ($enddate >= $startdate) {
3223 push @all_years, $enddate--;
3228 $main::lxdebug->leave_sub();