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;
383 map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
385 for (sort keys %$self) {
386 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
387 print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
394 $main::lxdebug->enter_sub();
396 $main::lxdebug->show_backtrace();
398 my ($self, $msg) = @_;
399 if ($ENV{HTTP_USER_AGENT}) {
401 $self->show_generic_error($msg);
408 $main::lxdebug->leave_sub();
412 $main::lxdebug->enter_sub();
414 my ($self, $msg) = @_;
416 if ($ENV{HTTP_USER_AGENT}) {
419 if (!$self->{header}) {
432 if ($self->{info_function}) {
433 &{ $self->{info_function} }($msg);
439 $main::lxdebug->leave_sub();
442 # calculates the number of rows in a textarea based on the content and column number
443 # can be capped with maxrows
445 $main::lxdebug->enter_sub();
446 my ($self, $str, $cols, $maxrows, $minrows) = @_;
450 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
453 $main::lxdebug->leave_sub();
455 return max(min($rows, $maxrows), $minrows);
459 $main::lxdebug->enter_sub();
461 my ($self, $msg) = @_;
463 $self->error("$msg\n" . $DBI::errstr);
465 $main::lxdebug->leave_sub();
469 $main::lxdebug->enter_sub();
471 my ($self, $name, $msg) = @_;
474 foreach my $part (split '.', $name) {
475 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
478 $curr = $curr->{$part};
481 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my ($self, $extra_code) = @_;
489 if ($self->{header}) {
490 $main::lxdebug->leave_sub();
494 my $cgi = $main::cgi;
495 $cgi ||= CGI->new('');
497 my ($stylesheet, $favicon);
499 if ($ENV{HTTP_USER_AGENT}) {
502 if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
503 # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
504 $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
507 my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
509 $stylesheets =~ s|^\s*||;
510 $stylesheets =~ s|\s*$||;
511 foreach my $file (split m/\s+/, $stylesheets) {
513 next if (! -f "css/$file");
515 $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
518 $self->{favicon} = "favicon.ico" unless $self->{favicon};
520 if ($self->{favicon} && (-f "$self->{favicon}")) {
522 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
526 my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
528 if ($self->{landscape}) {
529 $pagelayout = qq|<style type="text/css">
530 \@page { size:landscape; }
534 my $fokus = qq| document.$self->{fokus}.focus();| if ($self->{"fokus"});
538 if ($self->{jsscript} == 1) {
541 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
542 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
543 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
544 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
551 ? "$self->{title} - $self->{titlebar}"
554 foreach $item (@ { $self->{AJAX} }) {
555 $ajax .= $item->show_javascript();
560 if ($ENV{HTTP_X_FORWARDED_FOR}) {
561 $base_path = $ENV{HTTP_REFERER};
562 $base_path =~ s|^.*?://.*?/|/|;
564 $base_path = $ENV{REQUEST_URI};
566 $base_path =~ s|[^/]+$||;
567 $base_path =~ s|/$||;
570 if (defined $main::auth) {
571 my $session_cookie_value = $main::auth->get_session_id();
572 $session_cookie_value ||= 'NO_SESSION';
574 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
575 '-value' => $session_cookie_value,
576 '-path' => $base_path);
579 print $cgi->header('-type' => 'text/html',
580 '-charset' => $db_charset,
581 '-cookie' => $session_cookie);
582 print qq|${doctype}<html>
584 <title>$self->{titlebar}</title>
588 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
592 <script type="text/javascript">
600 <meta name="robots" content="noindex,nofollow" />
601 <script type="text/javascript" src="js/highlight_input.js"></script>
602 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
604 <script type="text/javascript" src="js/tabcontent.js">
606 /***********************************************
607 * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
608 * This notice MUST stay intact for legal use
609 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
610 ***********************************************/
621 $main::lxdebug->leave_sub();
624 sub _prepare_html_template {
625 $main::lxdebug->enter_sub();
627 my ($self, $file, $additional_params) = @_;
630 if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
631 $language = $main::language;
633 $language = $main::myconfig{"countrycode"};
635 $language = "de" unless ($language);
637 if (-f "templates/webpages/${file}_${language}.html") {
638 if ((-f ".developer") &&
639 (-f "templates/webpages/${file}_master.html") &&
640 ((stat("templates/webpages/${file}_master.html"))[9] >
641 (stat("templates/webpages/${file}_${language}.html"))[9])) {
642 my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
643 "Please re-run 'locales.pl' in 'locale/${language}'.";
644 print(qq|<pre>$info</pre>|);
648 $file = "templates/webpages/${file}_${language}.html";
649 } elsif (-f "templates/webpages/${file}.html") {
650 $file = "templates/webpages/${file}.html";
652 my $info = "Web page template '${file}' not found.\n" .
653 "Please re-run 'locales.pl' in 'locale/${language}'.";
654 print(qq|<pre>$info</pre>|);
658 if ($self->{"DEBUG"}) {
659 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
662 if ($additional_params->{"DEBUG"}) {
663 $additional_params->{"DEBUG"} =
664 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
667 if (%main::myconfig) {
668 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
669 my $jsc_dateformat = $main::myconfig{"dateformat"};
670 $jsc_dateformat =~ s/d+/\%d/gi;
671 $jsc_dateformat =~ s/m+/\%m/gi;
672 $jsc_dateformat =~ s/y+/\%Y/gi;
673 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
676 $additional_params->{"conf_webdav"} = $main::webdav;
677 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
678 $additional_params->{"conf_latex_templates"} = $main::latex;
679 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
681 if (%main::debug_options) {
682 map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
685 if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
686 while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
687 $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
691 $main::lxdebug->leave_sub();
696 sub parse_html_template {
697 $main::lxdebug->enter_sub();
699 my ($self, $file, $additional_params) = @_;
701 $additional_params ||= { };
703 $file = $self->_prepare_html_template($file, $additional_params);
705 my $template = Template->new({ 'INTERPOLATE' => 0,
709 'PLUGIN_BASE' => 'SL::Template::Plugin',
710 'INCLUDE_PATH' => '.:templates/webpages',
713 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
716 if (!$template->process($file, $additional_params, \$output)) {
717 print STDERR $template->error();
720 $output = $main::locale->{iconv}->convert($output) if ($main::locale);
722 $main::lxdebug->leave_sub();
727 sub show_generic_error {
728 my ($self, $error, %params) = @_;
731 'title_error' => $params{title},
732 'label_error' => $error,
735 if ($params{action}) {
738 map { delete($self->{$_}); } qw(action);
739 map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
741 $add_params->{SHOW_BUTTON} = 1;
742 $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
743 $add_params->{VARIABLES} = \@vars;
745 } elsif ($params{back_button}) {
746 $add_params->{SHOW_BACK_BUTTON} = 1;
749 $self->{title} = $title if ($title);
752 print $self->parse_html_template("generic/error", $add_params);
754 die("Error: $error\n");
757 sub show_generic_information {
758 my ($self, $text, $title) = @_;
761 'title_information' => $title,
762 'label_information' => $text,
765 $self->{title} = $title if ($title);
768 print $self->parse_html_template("generic/information", $add_params);
770 die("Information: $error\n");
773 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
774 # changed it to accept an arbitrary number of triggers - sschoeling
776 $main::lxdebug->enter_sub();
779 my $myconfig = shift;
782 # set dateform for jsscript
785 "dd.mm.yy" => "%d.%m.%Y",
786 "dd-mm-yy" => "%d-%m-%Y",
787 "dd/mm/yy" => "%d/%m/%Y",
788 "mm/dd/yy" => "%m/%d/%Y",
789 "mm-dd-yy" => "%m-%d-%Y",
790 "yyyy-mm-dd" => "%Y-%m-%d",
793 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
794 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
801 inputField : "| . (shift) . qq|",
802 ifFormat :"$ifFormat",
803 align : "| . (shift) . qq|",
804 button : "| . (shift) . qq|"
810 <script type="text/javascript">
811 <!--| . join("", @triggers) . qq|//-->
815 $main::lxdebug->leave_sub();
818 } #end sub write_trigger
821 $main::lxdebug->enter_sub();
823 my ($self, $msg) = @_;
825 if ($self->{callback}) {
827 ($script, $argv) = split(/\?/, $self->{callback}, 2);
829 $script =~ s|[^a-zA-Z0-9_\.]||g;
830 exec("perl", "$script", $argv);
838 $main::lxdebug->leave_sub();
841 # sort of columns removed - empty sub
843 $main::lxdebug->enter_sub();
845 my ($self, @columns) = @_;
847 $main::lxdebug->leave_sub();
853 $main::lxdebug->enter_sub(2);
855 my ($self, $myconfig, $amount, $places, $dash) = @_;
861 # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
863 my $neg = ($amount =~ s/^-//);
864 my $exp = ($amount =~ m/[e]/) ? 1 : 0;
866 if (defined($places) && ($places ne '')) {
872 my ($actual_places) = ($amount =~ /\.(\d+)/);
873 $actual_places = length($actual_places);
874 $places = $actual_places > $places ? $actual_places : $places;
877 $amount = $self->round_amount($amount, $places);
880 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
881 my @p = split(/\./, $amount); # split amount at decimal point
883 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
886 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
889 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
890 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
891 ($neg ? "-$amount" : "$amount" ) ;
895 $main::lxdebug->leave_sub(2);
899 sub format_amount_units {
900 $main::lxdebug->enter_sub();
905 Common::check_params(\%params, qw(amount part_unit));
907 my $myconfig = \%main::myconfig;
908 my $amount = $params{amount};
909 my $places = $params{places};
910 my $part_unit_name = $params{part_unit};
911 my $amount_unit_name = $params{amount_unit};
912 my $conv_units = $params{conv_units};
913 my $max_places = $params{max_places};
915 AM->retrieve_all_units();
916 my $all_units = $main::all_units;
918 if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
919 $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
922 if (!scalar @{ $conv_units }) {
923 my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
924 $main::lxdebug->leave_sub();
928 my $part_unit = $all_units->{$part_unit_name};
929 my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
931 $amount *= $conv_unit->{factor};
935 foreach my $unit (@$conv_units) {
936 my $last = $unit->{name} eq $part_unit->{name};
938 $num = int($amount / $unit->{factor});
939 $amount -= $num * $unit->{factor};
942 if ($last ? $amount : $num) {
943 push @values, { "unit" => $unit->{name},
944 "amount" => $last ? $amount / $unit->{factor} : $num,
945 "places" => $last ? $places : 0 };
952 push @values, { "unit" => $part_unit_name,
957 my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
959 $main::lxdebug->leave_sub();
965 $main::lxdebug->enter_sub(2);
970 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
971 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
972 $input =~ s/\#\#/\#/g;
974 $main::lxdebug->leave_sub(2);
982 $main::lxdebug->enter_sub(2);
984 my ($self, $myconfig, $amount) = @_;
986 if ( ($myconfig->{numberformat} eq '1.000,00')
987 || ($myconfig->{numberformat} eq '1000,00')) {
992 if ($myconfig->{numberformat} eq "1'000.00") {
998 $main::lxdebug->leave_sub(2);
1000 return ($amount * 1);
1004 $main::lxdebug->enter_sub(2);
1006 my ($self, $amount, $places) = @_;
1009 # Rounding like "Kaufmannsrunden"
1010 # Descr. http://de.wikipedia.org/wiki/Rundung
1012 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
1015 $amount = $amount * (10**($places));
1016 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
1018 $main::lxdebug->leave_sub(2);
1020 return $round_amount;
1024 sub parse_template {
1025 $main::lxdebug->enter_sub();
1027 my ($self, $myconfig, $userspath) = @_;
1028 my ($template, $out);
1032 $self->{"cwd"} = getcwd();
1033 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
1035 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
1036 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1037 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1038 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1039 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1040 } elsif (($self->{"format"} =~ /html/i) ||
1041 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
1042 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1043 } elsif (($self->{"format"} =~ /xml/i) ||
1044 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
1045 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1046 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
1047 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1048 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
1049 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
1050 } elsif ( defined $self->{'format'}) {
1051 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
1052 } elsif ( $self->{'format'} eq '' ) {
1053 $self->error("No Outputformat given: $self->{'format'}");
1054 } else { #Catch the rest
1055 $self->error("Outputformat not defined: $self->{'format'}");
1058 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
1059 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
1061 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
1062 qw(email tel fax name signature company address businessnumber
1063 co_ustid taxnumber duns));
1065 map({ $self->{"${_}"} = $myconfig->{$_}; }
1069 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
1071 # OUT is used for the media, screen, printer, email
1072 # for postscript we store a copy in a temporary file
1074 my $prepend_userspath;
1076 if (!$self->{tmpfile}) {
1077 $self->{tmpfile} = "${fileid}.$self->{IN}";
1078 $prepend_userspath = 1;
1081 $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
1083 $self->{tmpfile} =~ s|.*/||;
1084 $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
1085 $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
1087 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1088 $out = $self->{OUT};
1089 $self->{OUT} = ">$self->{tmpfile}";
1093 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
1095 open(OUT, ">-") or $self->error("STDOUT : $!");
1099 if (!$template->parse(*OUT)) {
1101 $self->error("$self->{IN} : " . $template->get_error());
1106 if ($template->uses_temp_file() || $self->{media} eq 'email') {
1108 if ($self->{media} eq 'email') {
1110 my $mail = new Mailer;
1112 map { $mail->{$_} = $self->{$_} }
1113 qw(cc bcc subject message version format);
1114 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
1115 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
1116 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1117 $mail->{fileid} = "$fileid.";
1118 $myconfig->{signature} =~ s/\r//g;
1120 # if we send html or plain text inline
1121 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
1122 $mail->{contenttype} = "text/html";
1124 $mail->{message} =~ s/\r//g;
1125 $mail->{message} =~ s/\n/<br>\n/g;
1126 $myconfig->{signature} =~ s/\n/<br>\n/g;
1127 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
1129 open(IN, $self->{tmpfile})
1130 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1132 $mail->{message} .= $_;
1139 if (!$self->{"do_not_attach"}) {
1140 @{ $mail->{attachments} } =
1141 ({ "filename" => $self->{"tmpfile"},
1142 "name" => $self->{"attachment_filename"} ?
1143 $self->{"attachment_filename"} : $self->{"tmpfile"} });
1146 $mail->{message} =~ s/\r//g;
1147 $mail->{message} .= "\n-- \n$myconfig->{signature}";
1151 my $err = $mail->send();
1152 $self->error($self->cleanup . "$err") if ($err);
1156 $self->{OUT} = $out;
1158 my $numbytes = (-s $self->{tmpfile});
1159 open(IN, $self->{tmpfile})
1160 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1162 $self->{copies} = 1 unless $self->{media} eq 'printer';
1164 chdir("$self->{cwd}");
1165 #print(STDERR "Kopien $self->{copies}\n");
1166 #print(STDERR "OUT $self->{OUT}\n");
1167 for my $i (1 .. $self->{copies}) {
1169 open(OUT, $self->{OUT})
1170 or $self->error($self->cleanup . "$self->{OUT} : $!");
1172 $self->{attachment_filename} = ($self->{attachment_filename})
1173 ? $self->{attachment_filename}
1174 : $self->generate_attachment_filename();
1176 # launch application
1177 print qq|Content-Type: | . $template->get_mime_type() . qq|
1178 Content-Disposition: attachment; filename="$self->{attachment_filename}"
1179 Content-Length: $numbytes
1183 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
1203 chdir("$self->{cwd}");
1204 $main::lxdebug->leave_sub();
1207 sub get_formname_translation {
1208 my ($self, $formname) = @_;
1210 $formname ||= $self->{formname};
1212 my %formname_translations = (
1213 bin_list => $main::locale->text('Bin List'),
1214 credit_note => $main::locale->text('Credit Note'),
1215 invoice => $main::locale->text('Invoice'),
1216 packing_list => $main::locale->text('Packing List'),
1217 pick_list => $main::locale->text('Pick List'),
1218 proforma => $main::locale->text('Proforma Invoice'),
1219 purchase_order => $main::locale->text('Purchase Order'),
1220 request_quotation => $main::locale->text('RFQ'),
1221 sales_order => $main::locale->text('Confirmation'),
1222 sales_quotation => $main::locale->text('Quotation'),
1223 storno_invoice => $main::locale->text('Storno Invoice'),
1224 storno_packing_list => $main::locale->text('Storno Packing List'),
1225 sales_delivery_order => $main::locale->text('Delivery Order'),
1226 purchase_delivery_order => $main::locale->text('Delivery Order'),
1229 return $formname_translations{$formname}
1232 sub generate_attachment_filename {
1235 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1237 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1238 : ($self->{type} =~ /_quotation$/) ? 'quo'
1239 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1242 if ($attachment_filename && $self->{"${prefix}number"}) {
1243 $attachment_filename .= "_" . $self->{"${prefix}number"}
1244 . ( $self->{format} =~ /pdf/i ? ".pdf"
1245 : $self->{format} =~ /postscript/i ? ".ps"
1246 : $self->{format} =~ /opendocument/i ? ".odt"
1247 : $self->{format} =~ /html/i ? ".html"
1249 $attachment_filename = lc $main::locale->quote_special_chars('filenames', $attachment_filename);
1250 $attachment_filename =~ s/ /_/g;
1252 $attachment_filename = "";
1255 return $attachment_filename;
1259 $main::lxdebug->enter_sub();
1263 chdir("$self->{tmpdir}");
1266 if (-f "$self->{tmpfile}.err") {
1267 open(FH, "$self->{tmpfile}.err");
1272 if ($self->{tmpfile}) {
1273 $self->{tmpfile} =~ s|.*/||g;
1275 $self->{tmpfile} =~ s/\.\w+$//g;
1276 my $tmpfile = $self->{tmpfile};
1277 unlink(<$tmpfile.*>);
1280 chdir("$self->{cwd}");
1282 $main::lxdebug->leave_sub();
1288 $main::lxdebug->enter_sub();
1290 my ($self, $date, $myconfig) = @_;
1292 if ($date && $date =~ /\D/) {
1294 if ($myconfig->{dateformat} =~ /^yy/) {
1295 ($yy, $mm, $dd) = split /\D/, $date;
1297 if ($myconfig->{dateformat} =~ /^mm/) {
1298 ($mm, $dd, $yy) = split /\D/, $date;
1300 if ($myconfig->{dateformat} =~ /^dd/) {
1301 ($dd, $mm, $yy) = split /\D/, $date;
1306 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1307 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1309 $dd = "0$dd" if ($dd < 10);
1310 $mm = "0$mm" if ($mm < 10);
1312 $date = "$yy$mm$dd";
1315 $main::lxdebug->leave_sub();
1320 # Database routines used throughout
1323 $main::lxdebug->enter_sub(2);
1325 my ($self, $myconfig) = @_;
1327 # connect to database
1329 DBI->connect($myconfig->{dbconnect},
1330 $myconfig->{dbuser}, $myconfig->{dbpasswd})
1334 if ($myconfig->{dboptions}) {
1335 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1338 $main::lxdebug->leave_sub(2);
1343 sub dbconnect_noauto {
1344 $main::lxdebug->enter_sub();
1346 my ($self, $myconfig) = @_;
1348 # connect to database
1350 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
1351 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1355 if ($myconfig->{dboptions}) {
1356 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1359 $main::lxdebug->leave_sub();
1364 sub get_standard_dbh {
1365 $main::lxdebug->enter_sub(2);
1367 my ($self, $myconfig) = @_;
1369 $standard_dbh ||= $self->dbconnect_noauto($myconfig);
1371 $main::lxdebug->leave_sub(2);
1373 return $standard_dbh;
1376 sub update_balance {
1377 $main::lxdebug->enter_sub();
1379 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1381 # if we have a value, go do it
1384 # retrieve balance from table
1385 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1386 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1387 my ($balance) = $sth->fetchrow_array;
1393 $query = "UPDATE $table SET $field = $balance WHERE $where";
1394 do_query($self, $dbh, $query, @values);
1396 $main::lxdebug->leave_sub();
1399 sub update_exchangerate {
1400 $main::lxdebug->enter_sub();
1402 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1404 # some sanity check for currency
1406 $main::lxdebug->leave_sub();
1409 $query = qq|SELECT curr FROM defaults|;
1411 my ($currency) = selectrow_query($self, $dbh, $query);
1412 my ($defaultcurrency) = split m/:/, $currency;
1415 if ($curr eq $defaultcurrency) {
1416 $main::lxdebug->leave_sub();
1420 $query = qq|SELECT e.curr FROM exchangerate e
1421 WHERE e.curr = ? AND e.transdate = ?
1423 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1432 $buy = conv_i($buy, "NULL");
1433 $sell = conv_i($sell, "NULL");
1436 if ($buy != 0 && $sell != 0) {
1437 $set = "buy = $buy, sell = $sell";
1438 } elsif ($buy != 0) {
1439 $set = "buy = $buy";
1440 } elsif ($sell != 0) {
1441 $set = "sell = $sell";
1444 if ($sth->fetchrow_array) {
1445 $query = qq|UPDATE exchangerate
1451 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1452 VALUES (?, $buy, $sell, ?)|;
1455 do_query($self, $dbh, $query, $curr, $transdate);
1457 $main::lxdebug->leave_sub();
1460 sub save_exchangerate {
1461 $main::lxdebug->enter_sub();
1463 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1465 my $dbh = $self->dbconnect($myconfig);
1469 $buy = $rate if $fld eq 'buy';
1470 $sell = $rate if $fld eq 'sell';
1473 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1478 $main::lxdebug->leave_sub();
1481 sub get_exchangerate {
1482 $main::lxdebug->enter_sub();
1484 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1487 unless ($transdate) {
1488 $main::lxdebug->leave_sub();
1492 $query = qq|SELECT curr FROM defaults|;
1494 my ($currency) = selectrow_query($self, $dbh, $query);
1495 my ($defaultcurrency) = split m/:/, $currency;
1497 if ($currency eq $defaultcurrency) {
1498 $main::lxdebug->leave_sub();
1502 $query = qq|SELECT e.$fld FROM exchangerate e
1503 WHERE e.curr = ? AND e.transdate = ?|;
1504 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1508 $main::lxdebug->leave_sub();
1510 return $exchangerate;
1513 sub check_exchangerate {
1514 $main::lxdebug->enter_sub();
1516 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1518 unless ($transdate) {
1519 $main::lxdebug->leave_sub();
1523 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1525 if ($currency eq $defaultcurrency) {
1526 $main::lxdebug->leave_sub();
1530 my $dbh = $self->get_standard_dbh($myconfig);
1531 my $query = qq|SELECT e.$fld FROM exchangerate e
1532 WHERE e.curr = ? AND e.transdate = ?|;
1534 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1536 $exchangerate = 1 if ($exchangerate eq "");
1538 $main::lxdebug->leave_sub();
1540 return $exchangerate;
1543 sub get_default_currency {
1544 $main::lxdebug->enter_sub();
1546 my ($self, $myconfig) = @_;
1547 my $dbh = $self->get_standard_dbh($myconfig);
1549 my $query = qq|SELECT curr FROM defaults|;
1551 my ($curr) = selectrow_query($self, $dbh, $query);
1552 my ($defaultcurrency) = split m/:/, $curr;
1554 $main::lxdebug->leave_sub();
1556 return $defaultcurrency;
1560 sub set_payment_options {
1561 $main::lxdebug->enter_sub();
1563 my ($self, $myconfig, $transdate) = @_;
1565 return $main::lxdebug->leave_sub() unless ($self->{payment_id});
1567 my $dbh = $self->get_standard_dbh($myconfig);
1570 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1571 qq|FROM payment_terms p | .
1574 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1575 $self->{payment_terms}) =
1576 selectrow_query($self, $dbh, $query, $self->{payment_id});
1578 if ($transdate eq "") {
1579 if ($self->{invdate}) {
1580 $transdate = $self->{invdate};
1582 $transdate = $self->{transdate};
1587 qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1588 qq|FROM payment_terms|;
1589 ($self->{netto_date}, $self->{skonto_date}) =
1590 selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1592 my ($invtotal, $total);
1593 my (%amounts, %formatted_amounts);
1595 if ($self->{type} =~ /_order$/) {
1596 $amounts{invtotal} = $self->{ordtotal};
1597 $amounts{total} = $self->{ordtotal};
1599 } elsif ($self->{type} =~ /_quotation$/) {
1600 $amounts{invtotal} = $self->{quototal};
1601 $amounts{total} = $self->{quototal};
1604 $amounts{invtotal} = $self->{invtotal};
1605 $amounts{total} = $self->{total};
1608 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1610 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1611 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1612 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1614 foreach (keys %amounts) {
1615 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1616 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1619 if ($self->{"language_id"}) {
1621 qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1622 qq|FROM translation_payment_terms t | .
1623 qq|LEFT JOIN language l ON t.language_id = l.id | .
1624 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1625 my ($description_long, $output_numberformat, $output_dateformat,
1626 $output_longdates) =
1627 selectrow_query($self, $dbh, $query,
1628 $self->{"language_id"}, $self->{"payment_id"});
1630 $self->{payment_terms} = $description_long if ($description_long);
1632 if ($output_dateformat) {
1633 foreach my $key (qw(netto_date skonto_date)) {
1635 $main::locale->reformat_date($myconfig, $self->{$key},
1641 if ($output_numberformat &&
1642 ($output_numberformat ne $myconfig->{"numberformat"})) {
1643 my $saved_numberformat = $myconfig->{"numberformat"};
1644 $myconfig->{"numberformat"} = $output_numberformat;
1645 map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
1646 $myconfig->{"numberformat"} = $saved_numberformat;
1650 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1651 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1652 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1653 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1654 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1655 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1656 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1658 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1660 $main::lxdebug->leave_sub();
1664 sub get_template_language {
1665 $main::lxdebug->enter_sub();
1667 my ($self, $myconfig) = @_;
1669 my $template_code = "";
1671 if ($self->{language_id}) {
1672 my $dbh = $self->get_standard_dbh($myconfig);
1673 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1674 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1677 $main::lxdebug->leave_sub();
1679 return $template_code;
1682 sub get_printer_code {
1683 $main::lxdebug->enter_sub();
1685 my ($self, $myconfig) = @_;
1687 my $template_code = "";
1689 if ($self->{printer_id}) {
1690 my $dbh = $self->get_standard_dbh($myconfig);
1691 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1692 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1695 $main::lxdebug->leave_sub();
1697 return $template_code;
1701 $main::lxdebug->enter_sub();
1703 my ($self, $myconfig) = @_;
1705 my $template_code = "";
1707 if ($self->{shipto_id}) {
1708 my $dbh = $self->get_standard_dbh($myconfig);
1709 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1710 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1711 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1714 $main::lxdebug->leave_sub();
1718 $main::lxdebug->enter_sub();
1720 my ($self, $dbh, $id, $module) = @_;
1725 foreach my $item (qw(name department_1 department_2 street zipcode city country
1726 contact phone fax email)) {
1727 if ($self->{"shipto$item"}) {
1728 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1730 push(@values, $self->{"shipto${item}"});
1734 if ($self->{shipto_id}) {
1735 my $query = qq|UPDATE shipto set
1737 shiptodepartment_1 = ?,
1738 shiptodepartment_2 = ?,
1747 WHERE shipto_id = ?|;
1748 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1750 my $query = qq|SELECT * FROM shipto
1751 WHERE shiptoname = ? AND
1752 shiptodepartment_1 = ? AND
1753 shiptodepartment_2 = ? AND
1754 shiptostreet = ? AND
1755 shiptozipcode = ? AND
1757 shiptocountry = ? AND
1758 shiptocontact = ? AND
1764 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1767 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1768 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1769 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1770 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1771 do_query($self, $dbh, $query, $id, @values, $module);
1776 $main::lxdebug->leave_sub();
1780 $main::lxdebug->enter_sub();
1782 my ($self, $dbh) = @_;
1784 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1785 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1786 $self->{"employee_id"} *= 1;
1788 $main::lxdebug->leave_sub();
1792 $main::lxdebug->enter_sub();
1794 my ($self, $myconfig, $salesman_id) = @_;
1796 $main::lxdebug->leave_sub() and return unless $salesman_id;
1798 my $dbh = $self->get_standard_dbh($myconfig);
1801 selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
1805 my $user = new User($main::memberfile, $login);
1806 map({ $self->{"salesman_$_"} = $user->{$_}; }
1807 qw(address businessnumber co_ustid company duns email fax name
1809 $self->{salesman_login} = $login;
1811 $self->{salesman_name} = $login
1812 if ($self->{salesman_name} eq "");
1815 $main::lxdebug->leave_sub();
1819 $main::lxdebug->enter_sub();
1821 my ($self, $myconfig) = @_;
1823 my $dbh = $self->get_standard_dbh($myconfig);
1824 my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1825 ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1827 $main::lxdebug->leave_sub();
1831 $main::lxdebug->enter_sub();
1833 my ($self, $dbh, $id, $key) = @_;
1835 $key = "all_contacts" unless ($key);
1838 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1839 qq|FROM contacts | .
1840 qq|WHERE cp_cv_id = ? | .
1841 qq|ORDER BY lower(cp_name)|;
1843 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1845 $main::lxdebug->leave_sub();
1849 $main::lxdebug->enter_sub();
1851 my ($self, $dbh, $key) = @_;
1853 my ($all, $old_id, $where, @values);
1855 if (ref($key) eq "HASH") {
1858 $key = "ALL_PROJECTS";
1860 foreach my $p (keys(%{$params})) {
1862 $all = $params->{$p};
1863 } elsif ($p eq "old_id") {
1864 $old_id = $params->{$p};
1865 } elsif ($p eq "key") {
1866 $key = $params->{$p};
1872 $where = "WHERE active ";
1874 if (ref($old_id) eq "ARRAY") {
1875 my @ids = grep({ $_ } @{$old_id});
1877 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1878 push(@values, @ids);
1881 $where .= " OR (id = ?) ";
1882 push(@values, $old_id);
1888 qq|SELECT id, projectnumber, description, active | .
1891 qq|ORDER BY lower(projectnumber)|;
1893 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1895 $main::lxdebug->leave_sub();
1899 $main::lxdebug->enter_sub();
1901 my ($self, $dbh, $vc_id, $key) = @_;
1903 $key = "all_shipto" unless ($key);
1905 # get shipping addresses
1906 my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
1908 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1910 $main::lxdebug->leave_sub();
1914 $main::lxdebug->enter_sub();
1916 my ($self, $dbh, $key) = @_;
1918 $key = "all_printers" unless ($key);
1920 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1922 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1924 $main::lxdebug->leave_sub();
1928 $main::lxdebug->enter_sub();
1930 my ($self, $dbh, $params) = @_;
1932 $key = $params->{key};
1933 $key = "all_charts" unless ($key);
1935 my $transdate = quote_db_date($params->{transdate});
1938 qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
1940 qq|LEFT JOIN taxkeys tk ON | .
1941 qq|(tk.id = (SELECT id FROM taxkeys | .
1942 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1943 qq| ORDER BY startdate DESC LIMIT 1)) | .
1944 qq|ORDER BY c.accno|;
1946 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1948 $main::lxdebug->leave_sub();
1951 sub _get_taxcharts {
1952 $main::lxdebug->enter_sub();
1954 my ($self, $dbh, $key) = @_;
1956 $key = "all_taxcharts" unless ($key);
1958 my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1960 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1962 $main::lxdebug->leave_sub();
1966 $main::lxdebug->enter_sub();
1968 my ($self, $dbh, $key) = @_;
1970 $key = "all_taxzones" unless ($key);
1972 my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
1974 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1976 $main::lxdebug->leave_sub();
1979 sub _get_employees {
1980 $main::lxdebug->enter_sub();
1982 my ($self, $dbh, $default_key, $key) = @_;
1984 $key = $default_key unless ($key);
1985 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
1987 $main::lxdebug->leave_sub();
1990 sub _get_business_types {
1991 $main::lxdebug->enter_sub();
1993 my ($self, $dbh, $key) = @_;
1995 $key = "all_business_types" unless ($key);
1997 selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
1999 $main::lxdebug->leave_sub();
2002 sub _get_languages {
2003 $main::lxdebug->enter_sub();
2005 my ($self, $dbh, $key) = @_;
2007 $key = "all_languages" unless ($key);
2009 my $query = qq|SELECT * FROM language ORDER BY id|;
2011 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2013 $main::lxdebug->leave_sub();
2016 sub _get_dunning_configs {
2017 $main::lxdebug->enter_sub();
2019 my ($self, $dbh, $key) = @_;
2021 $key = "all_dunning_configs" unless ($key);
2023 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2025 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2027 $main::lxdebug->leave_sub();
2030 sub _get_currencies {
2031 $main::lxdebug->enter_sub();
2033 my ($self, $dbh, $key) = @_;
2035 $key = "all_currencies" unless ($key);
2037 my $query = qq|SELECT curr AS currency FROM defaults|;
2039 $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
2041 $main::lxdebug->leave_sub();
2045 $main::lxdebug->enter_sub();
2047 my ($self, $dbh, $key) = @_;
2049 $key = "all_payments" unless ($key);
2051 my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
2053 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2055 $main::lxdebug->leave_sub();
2058 sub _get_customers {
2059 $main::lxdebug->enter_sub();
2061 my ($self, $dbh, $key, $limit) = @_;
2063 $key = "all_customers" unless ($key);
2064 $limit_clause = "LIMIT $limit" if $limit;
2066 my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
2068 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2070 $main::lxdebug->leave_sub();
2074 $main::lxdebug->enter_sub();
2076 my ($self, $dbh, $key) = @_;
2078 $key = "all_vendors" unless ($key);
2080 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2082 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2084 $main::lxdebug->leave_sub();
2087 sub _get_departments {
2088 $main::lxdebug->enter_sub();
2090 my ($self, $dbh, $key) = @_;
2092 $key = "all_departments" unless ($key);
2094 my $query = qq|SELECT * FROM department ORDER BY description|;
2096 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2098 $main::lxdebug->leave_sub();
2101 sub _get_warehouses {
2102 $main::lxdebug->enter_sub();
2104 my ($self, $dbh, $param) = @_;
2106 my ($key, $bins_key, $q_access, @values);
2108 if ('' eq ref $param) {
2111 $key = $param->{key};
2112 $bins_key = $param->{bins};
2114 if ($param->{access}) {
2117 SELECT wa.employee_id
2118 FROM warehouse_access wa
2119 WHERE (wa.employee_id = (SELECT id FROM employee WHERE login = ?))
2120 AND (wa.warehouse_id = w.id)
2121 AND (wa.access IN ('ro', 'rw')))|;
2122 push @values, $param->{access};
2125 if ($param->{no_personal}) {
2126 $q_access .= qq| AND (w.personal_warehouse_of IS NULL)|;
2128 } elsif ($param->{personal}) {
2129 $q_access .= qq| AND (w.personal_warehouse_of = ?)|;
2130 push @values, conv_i($param->{personal});
2134 my $query = qq|SELECT w.* FROM warehouse w
2135 WHERE (NOT w.invalid) AND
2136 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2138 ORDER BY w.sortkey|;
2140 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
2143 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
2144 my $sth = prepare_query($self, $dbh, $query);
2146 foreach my $warehouse (@{ $self->{$key} }) {
2147 do_statement($self, $sth, $query, $warehouse->{id});
2148 $warehouse->{$bins_key} = [];
2150 while (my $ref = $sth->fetchrow_hashref()) {
2151 push @{ $warehouse->{$bins_key} }, $ref;
2157 $main::lxdebug->leave_sub();
2161 $main::lxdebug->enter_sub();
2163 my ($self, $dbh, $table, $key, $sortkey) = @_;
2165 my $query = qq|SELECT * FROM $table|;
2166 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2168 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2170 $main::lxdebug->leave_sub();
2174 $main::lxdebug->enter_sub();
2176 my ($self, $dbh, $key) = @_;
2178 $key ||= "all_groups";
2180 my $groups = $main::auth->read_groups();
2182 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2184 $main::lxdebug->leave_sub();
2188 $main::lxdebug->enter_sub();
2193 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2194 my ($sth, $query, $ref);
2196 my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
2197 my $vc_id = $self->{"${vc}_id"};
2199 if ($params{"contacts"}) {
2200 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2203 if ($params{"shipto"}) {
2204 $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
2207 if ($params{"projects"} || $params{"all_projects"}) {
2208 $self->_get_projects($dbh, $params{"all_projects"} ?
2209 $params{"all_projects"} : $params{"projects"},
2210 $params{"all_projects"} ? 1 : 0);
2213 if ($params{"printers"}) {
2214 $self->_get_printers($dbh, $params{"printers"});
2217 if ($params{"languages"}) {
2218 $self->_get_languages($dbh, $params{"languages"});
2221 if ($params{"charts"}) {
2222 $self->_get_charts($dbh, $params{"charts"});
2225 if ($params{"taxcharts"}) {
2226 $self->_get_taxcharts($dbh, $params{"taxcharts"});
2229 if ($params{"taxzones"}) {
2230 $self->_get_taxzones($dbh, $params{"taxzones"});
2233 if ($params{"employees"}) {
2234 $self->_get_employees($dbh, "all_employees", $params{"employees"});
2237 if ($params{"salesmen"}) {
2238 $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
2241 if ($params{"business_types"}) {
2242 $self->_get_business_types($dbh, $params{"business_types"});
2245 if ($params{"dunning_configs"}) {
2246 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2249 if($params{"currencies"}) {
2250 $self->_get_currencies($dbh, $params{"currencies"});
2253 if($params{"customers"}) {
2254 if (ref $params{"customers"} eq 'HASH') {
2255 $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
2257 $self->_get_customers($dbh, $params{"customers"});
2261 if($params{"vendors"}) {
2262 if (ref $params{"vendors"} eq 'HASH') {
2263 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2265 $self->_get_vendors($dbh, $params{"vendors"});
2269 if($params{"payments"}) {
2270 $self->_get_payments($dbh, $params{"payments"});
2273 if($params{"departments"}) {
2274 $self->_get_departments($dbh, $params{"departments"});
2277 if ($params{price_factors}) {
2278 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2281 if ($params{warehouses}) {
2282 $self->_get_warehouses($dbh, $params{warehouses});
2285 if ($params{groups}) {
2286 $self->_get_groups($dbh, $params{groups});
2289 $main::lxdebug->leave_sub();
2292 # this sub gets the id and name from $table
2294 $main::lxdebug->enter_sub();
2296 my ($self, $myconfig, $table) = @_;
2298 # connect to database
2299 my $dbh = $self->get_standard_dbh($myconfig);
2301 $table = $table eq "customer" ? "customer" : "vendor";
2302 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2304 my ($query, @values);
2306 if (!$self->{openinvoices}) {
2308 if ($self->{customernumber} ne "") {
2309 $where = qq|(vc.customernumber ILIKE ?)|;
2310 push(@values, '%' . $self->{customernumber} . '%');
2312 $where = qq|(vc.name ILIKE ?)|;
2313 push(@values, '%' . $self->{$table} . '%');
2317 qq~SELECT vc.id, vc.name,
2318 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2320 WHERE $where AND (NOT vc.obsolete)
2324 qq~SELECT DISTINCT vc.id, vc.name,
2325 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2327 JOIN $table vc ON (a.${table}_id = vc.id)
2328 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2330 push(@values, '%' . $self->{$table} . '%');
2333 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2335 $main::lxdebug->leave_sub();
2337 return scalar(@{ $self->{name_list} });
2340 # the selection sub is used in the AR, AP, IS, IR and OE module
2343 $main::lxdebug->enter_sub();
2345 my ($self, $myconfig, $table, $module) = @_;
2348 my $dbh = $self->get_standard_dbh($myconfig);
2350 $table = $table eq "customer" ? "customer" : "vendor";
2352 my $query = qq|SELECT count(*) FROM $table|;
2353 my ($count) = selectrow_query($self, $dbh, $query);
2355 # build selection list
2356 if ($count < $myconfig->{vclimit}) {
2357 $query = qq|SELECT id, name, salesman_id
2358 FROM $table WHERE NOT obsolete
2360 $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
2364 $self->get_employee($dbh);
2366 # setup sales contacts
2367 $query = qq|SELECT e.id, e.name
2369 WHERE (e.sales = '1') AND (NOT e.id = ?)|;
2370 $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
2373 push(@{ $self->{all_employees} },
2374 { id => $self->{employee_id},
2375 name => $self->{employee} });
2377 # sort the whole thing
2378 @{ $self->{all_employees} } =
2379 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
2381 if ($module eq 'AR') {
2383 # prepare query for departments
2384 $query = qq|SELECT id, description
2387 ORDER BY description|;
2390 $query = qq|SELECT id, description
2392 ORDER BY description|;
2395 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2398 $query = qq|SELECT id, description
2402 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2405 $query = qq|SELECT printer_description, id
2407 ORDER BY printer_description|;
2409 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2412 $query = qq|SELECT id, description
2416 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2418 $main::lxdebug->leave_sub();
2421 sub language_payment {
2422 $main::lxdebug->enter_sub();
2424 my ($self, $myconfig) = @_;
2426 my $dbh = $self->get_standard_dbh($myconfig);
2428 my $query = qq|SELECT id, description
2432 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2435 $query = qq|SELECT printer_description, id
2437 ORDER BY printer_description|;
2439 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2442 $query = qq|SELECT id, description
2446 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
2448 # get buchungsgruppen
2449 $query = qq|SELECT id, description
2450 FROM buchungsgruppen|;
2452 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2454 $main::lxdebug->leave_sub();
2457 # this is only used for reports
2458 sub all_departments {
2459 $main::lxdebug->enter_sub();
2461 my ($self, $myconfig, $table) = @_;
2463 my $dbh = $self->get_standard_dbh($myconfig);
2466 if ($table eq 'customer') {
2467 $where = "WHERE role = 'P' ";
2470 my $query = qq|SELECT id, description
2473 ORDER BY description|;
2474 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2476 delete($self->{all_departments}) unless (@{ $self->{all_departments} });
2478 $main::lxdebug->leave_sub();
2482 $main::lxdebug->enter_sub();
2484 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2487 if ($table eq "customer") {
2496 $self->all_vc($myconfig, $table, $module);
2498 # get last customers or vendors
2499 my ($query, $sth, $ref);
2501 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2506 my $transdate = "current_date";
2507 if ($self->{transdate}) {
2508 $transdate = $dbh->quote($self->{transdate});
2511 # now get the account numbers
2512 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2513 FROM chart c, taxkeys tk
2514 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
2515 (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
2518 $sth = $dbh->prepare($query);
2520 do_statement($self, $sth, $query, '%' . $module . '%');
2522 $self->{accounts} = "";
2523 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2525 foreach my $key (split(/:/, $ref->{link})) {
2526 if ($key =~ /\Q$module\E/) {
2528 # cross reference for keys
2529 $xkeyref{ $ref->{accno} } = $key;
2531 push @{ $self->{"${module}_links"}{$key} },
2532 { accno => $ref->{accno},
2533 description => $ref->{description},
2534 taxkey => $ref->{taxkey_id},
2535 tax_id => $ref->{tax_id} };
2537 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2543 # get taxkeys and description
2544 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2545 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2547 if (($module eq "AP") || ($module eq "AR")) {
2548 # get tax rates and description
2549 $query = qq|SELECT * FROM tax|;
2550 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2556 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
2557 a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
2558 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2559 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2561 d.description AS department,
2564 JOIN $table c ON (a.${table}_id = c.id)
2565 LEFT JOIN employee e ON (e.id = a.employee_id)
2566 LEFT JOIN department d ON (d.id = a.department_id)
2568 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2570 foreach $key (keys %$ref) {
2571 $self->{$key} = $ref->{$key};
2574 my $transdate = "current_date";
2575 if ($self->{transdate}) {
2576 $transdate = $dbh->quote($self->{transdate});
2579 # now get the account numbers
2580 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
2582 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2584 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2585 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2588 $sth = $dbh->prepare($query);
2589 do_statement($self, $sth, $query, "%$module%");
2591 $self->{accounts} = "";
2592 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2594 foreach my $key (split(/:/, $ref->{link})) {
2595 if ($key =~ /\Q$module\E/) {
2597 # cross reference for keys
2598 $xkeyref{ $ref->{accno} } = $key;
2600 push @{ $self->{"${module}_links"}{$key} },
2601 { accno => $ref->{accno},
2602 description => $ref->{description},
2603 taxkey => $ref->{taxkey_id},
2604 tax_id => $ref->{tax_id} };
2606 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2612 # get amounts from individual entries
2615 c.accno, c.description,
2616 a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
2620 LEFT JOIN chart c ON (c.id = a.chart_id)
2621 LEFT JOIN project p ON (p.id = a.project_id)
2622 LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
2623 WHERE (tk.taxkey_id=a.taxkey) AND
2624 ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
2625 THEN tk.chart_id = a.chart_id
2628 OR (c.link='%tax%')) AND
2629 (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
2630 WHERE a.trans_id = ?
2631 AND a.fx_transaction = '0'
2632 ORDER BY a.oid, a.transdate|;
2633 $sth = $dbh->prepare($query);
2634 do_statement($self, $sth, $query, $self->{id});
2636 # get exchangerate for currency
2637 $self->{exchangerate} =
2638 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2641 # store amounts in {acc_trans}{$key} for multiple accounts
2642 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2643 $ref->{exchangerate} =
2644 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2645 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2648 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2649 $ref->{amount} *= -1;
2651 $ref->{index} = $index;
2653 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2659 d.curr AS currencies, d.closedto, d.revtrans,
2660 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2661 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2663 $ref = selectfirst_hashref_query($self, $dbh, $query);
2664 map { $self->{$_} = $ref->{$_} } keys %$ref;
2671 current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
2672 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2673 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2675 $ref = selectfirst_hashref_query($self, $dbh, $query);
2676 map { $self->{$_} = $ref->{$_} } keys %$ref;
2678 if ($self->{"$self->{vc}_id"}) {
2680 # only setup currency
2681 ($self->{currency}) = split(/:/, $self->{currencies});
2685 $self->lastname_used($dbh, $myconfig, $table, $module);
2687 # get exchangerate for currency
2688 $self->{exchangerate} =
2689 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2695 $main::lxdebug->leave_sub();
2699 $main::lxdebug->enter_sub();
2701 my ($self, $dbh, $myconfig, $table, $module) = @_;
2703 my $arap = ($table eq 'customer') ? "ar" : "ap";
2704 $table = $table eq "customer" ? "customer" : "vendor";
2705 my $where = "1 = 1";
2707 if ($self->{type} =~ /_order/) {
2709 $where = "quotation = '0'";
2711 if ($self->{type} =~ /_quotation/) {
2713 $where = "quotation = '1'";
2716 my $query = qq|SELECT MAX(id) FROM $arap
2717 WHERE $where AND ${table}_id > 0|;
2718 my ($trans_id) = selectrow_query($self, $dbh, $query);
2723 a.curr, a.${table}_id, a.department_id,
2724 d.description AS department,
2725 ct.name, current_date + ct.terms AS duedate
2727 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2728 LEFT JOIN department d ON (a.department_id = d.id)
2730 ($self->{currency}, $self->{"${table}_id"}, $self->{department_id},
2731 $self->{department}, $self->{$table}, $self->{duedate})
2732 = selectrow_query($self, $dbh, $query, $trans_id);
2734 $main::lxdebug->leave_sub();
2738 $main::lxdebug->enter_sub();
2740 my ($self, $myconfig, $thisdate, $days) = @_;
2742 my $dbh = $self->get_standard_dbh($myconfig);
2747 my $dateformat = $myconfig->{dateformat};
2748 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2749 $thisdate = $dbh->quote($thisdate);
2750 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2752 $query = qq|SELECT current_date AS thisdate|;
2755 ($thisdate) = selectrow_query($self, $dbh, $query);
2757 $main::lxdebug->leave_sub();
2763 $main::lxdebug->enter_sub();
2765 my ($self, $string) = @_;
2767 if ($string !~ /%/) {
2768 $string = "%$string%";
2771 $string =~ s/\'/\'\'/g;
2773 $main::lxdebug->leave_sub();
2779 $main::lxdebug->enter_sub();
2781 my ($self, $flds, $new, $count, $numrows) = @_;
2785 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2790 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2792 $j = $item->{ndx} - 1;
2793 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2797 for $i ($count + 1 .. $numrows) {
2798 map { delete $self->{"${_}_$i"} } @{$flds};
2801 $main::lxdebug->leave_sub();
2805 $main::lxdebug->enter_sub();
2807 my ($self, $myconfig) = @_;
2811 my $dbh = $self->dbconnect_noauto($myconfig);
2813 my $query = qq|DELETE FROM status
2814 WHERE (formname = ?) AND (trans_id = ?)|;
2815 my $sth = prepare_query($self, $dbh, $query);
2817 if ($self->{formname} =~ /(check|receipt)/) {
2818 for $i (1 .. $self->{rowcount}) {
2819 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2822 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2826 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2827 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2829 my %queued = split / /, $self->{queued};
2832 if ($self->{formname} =~ /(check|receipt)/) {
2834 # this is a check or receipt, add one entry for each lineitem
2835 my ($accno) = split /--/, $self->{account};
2836 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2837 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2838 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2839 $sth = prepare_query($self, $dbh, $query);
2841 for $i (1 .. $self->{rowcount}) {
2842 if ($self->{"checked_$i"}) {
2843 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2849 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2850 VALUES (?, ?, ?, ?, ?)|;
2851 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2852 $queued{$self->{formname}}, $self->{formname});
2858 $main::lxdebug->leave_sub();
2862 $main::lxdebug->enter_sub();
2864 my ($self, $dbh) = @_;
2866 my ($query, $printed, $emailed);
2868 my $formnames = $self->{printed};
2869 my $emailforms = $self->{emailed};
2871 $query = qq|DELETE FROM status
2872 WHERE (formname = ?) AND (trans_id = ?)|;
2873 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2875 # this only applies to the forms
2876 # checks and receipts are posted when printed or queued
2878 if ($self->{queued}) {
2879 my %queued = split / /, $self->{queued};
2881 foreach my $formname (keys %queued) {
2882 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2883 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2885 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2886 VALUES (?, ?, ?, ?, ?)|;
2887 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2889 $formnames =~ s/\Q$self->{formname}\E//;
2890 $emailforms =~ s/\Q$self->{formname}\E//;
2895 # save printed, emailed info
2896 $formnames =~ s/^ +//g;
2897 $emailforms =~ s/^ +//g;
2900 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2901 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2903 foreach my $formname (keys %status) {
2904 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2905 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2907 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2908 VALUES (?, ?, ?, ?)|;
2909 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2912 $main::lxdebug->leave_sub();
2916 # $main::locale->text('SAVED')
2917 # $main::locale->text('DELETED')
2918 # $main::locale->text('ADDED')
2919 # $main::locale->text('PAYMENT POSTED')
2920 # $main::locale->text('POSTED')
2921 # $main::locale->text('POSTED AS NEW')
2922 # $main::locale->text('ELSE')
2923 # $main::locale->text('SAVED FOR DUNNING')
2924 # $main::locale->text('DUNNING STARTED')
2925 # $main::locale->text('PRINTED')
2926 # $main::locale->text('MAILED')
2927 # $main::locale->text('SCREENED')
2928 # $main::locale->text('CANCELED')
2929 # $main::locale->text('invoice')
2930 # $main::locale->text('proforma')
2931 # $main::locale->text('sales_order')
2932 # $main::locale->text('packing_list')
2933 # $main::locale->text('pick_list')
2934 # $main::locale->text('purchase_order')
2935 # $main::locale->text('bin_list')
2936 # $main::locale->text('sales_quotation')
2937 # $main::locale->text('request_quotation')
2940 $main::lxdebug->enter_sub();
2945 if(!exists $self->{employee_id}) {
2946 &get_employee($self, $dbh);
2950 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2951 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2952 my @values = (conv_i($self->{id}), $self->{login},
2953 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2954 do_query($self, $dbh, $query, @values);
2956 $main::lxdebug->leave_sub();
2960 $main::lxdebug->enter_sub();
2962 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2963 my ($orderBy, $desc) = split(/\-\-/, $order);
2964 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2967 if ($trans_id ne "") {
2969 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 | .
2970 qq|FROM history_erp h | .
2971 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2972 qq|WHERE trans_id = | . $trans_id
2973 . $restriction . qq| |
2976 my $sth = $dbh->prepare($query) || $self->dberror($query);
2978 $sth->execute() || $self->dberror("$query");
2980 while(my $hash_ref = $sth->fetchrow_hashref()) {
2981 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2982 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2983 $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
2984 $tempArray[$i++] = $hash_ref;
2986 $main::lxdebug->leave_sub() and return \@tempArray
2987 if ($i > 0 && $tempArray[0] ne "");
2989 $main::lxdebug->leave_sub();
2993 sub update_defaults {
2994 $main::lxdebug->enter_sub();
2996 my ($self, $myconfig, $fld, $provided_dbh) = @_;
2999 if ($provided_dbh) {
3000 $dbh = $provided_dbh;
3002 $dbh = $self->dbconnect_noauto($myconfig);
3004 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
3005 my $sth = $dbh->prepare($query);
3007 $sth->execute || $self->dberror($query);
3008 my ($var) = $sth->fetchrow_array;
3011 if ($var =~ m/\d+$/) {
3012 my $new_var = (substr $var, $-[0]) * 1 + 1;
3013 my $len_diff = length($var) - $-[0] - length($new_var);
3014 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3020 $query = qq|UPDATE defaults SET $fld = ?|;
3021 do_query($self, $dbh, $query, $var);
3023 if (!$provided_dbh) {
3028 $main::lxdebug->leave_sub();
3033 sub update_business {
3034 $main::lxdebug->enter_sub();
3036 my ($self, $myconfig, $business_id, $provided_dbh) = @_;
3039 if ($provided_dbh) {
3040 $dbh = $provided_dbh;
3042 $dbh = $self->dbconnect_noauto($myconfig);
3045 qq|SELECT customernumberinit FROM business
3046 WHERE id = ? FOR UPDATE|;
3047 my ($var) = selectrow_query($self, $dbh, $query, $business_id);
3049 if ($var =~ m/\d+$/) {
3050 my $new_var = (substr $var, $-[0]) * 1 + 1;
3051 my $len_diff = length($var) - $-[0] - length($new_var);
3052 $var = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
3058 $query = qq|UPDATE business
3059 SET customernumberinit = ?
3061 do_query($self, $dbh, $query, $var, $business_id);
3063 if (!$provided_dbh) {
3068 $main::lxdebug->leave_sub();
3073 sub get_partsgroup {
3074 $main::lxdebug->enter_sub();
3076 my ($self, $myconfig, $p) = @_;
3078 my $dbh = $self->get_standard_dbh($myconfig);
3080 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
3082 JOIN parts p ON (p.partsgroup_id = pg.id) |;
3085 if ($p->{searchitems} eq 'part') {
3086 $query .= qq|WHERE p.inventory_accno_id > 0|;
3088 if ($p->{searchitems} eq 'service') {
3089 $query .= qq|WHERE p.inventory_accno_id IS NULL|;
3091 if ($p->{searchitems} eq 'assembly') {
3092 $query .= qq|WHERE p.assembly = '1'|;
3094 if ($p->{searchitems} eq 'labor') {
3095 $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
3098 $query .= qq|ORDER BY partsgroup|;
3101 $query = qq|SELECT id, partsgroup FROM partsgroup
3102 ORDER BY partsgroup|;
3105 if ($p->{language_code}) {
3106 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3107 t.description AS translation
3109 JOIN parts p ON (p.partsgroup_id = pg.id)
3110 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3111 ORDER BY translation|;
3112 @values = ($p->{language_code});
3115 $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
3117 $main::lxdebug->leave_sub();
3120 sub get_pricegroup {
3121 $main::lxdebug->enter_sub();
3123 my ($self, $myconfig, $p) = @_;
3125 my $dbh = $self->get_standard_dbh($myconfig);
3127 my $query = qq|SELECT p.id, p.pricegroup
3130 $query .= qq| ORDER BY pricegroup|;
3133 $query = qq|SELECT id, pricegroup FROM pricegroup
3134 ORDER BY pricegroup|;
3137 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3139 $main::lxdebug->leave_sub();
3143 # usage $form->all_years($myconfig, [$dbh])
3144 # return list of all years where bookings found
3147 $main::lxdebug->enter_sub();
3149 my ($self, $myconfig, $dbh) = @_;
3151 $dbh ||= $self->get_standard_dbh($myconfig);
3154 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3155 (SELECT MAX(transdate) FROM acc_trans)|;
3156 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3158 if ($myconfig->{dateformat} =~ /^yy/) {
3159 ($startdate) = split /\W/, $startdate;
3160 ($enddate) = split /\W/, $enddate;
3162 (@_) = split /\W/, $startdate;
3164 (@_) = split /\W/, $enddate;
3169 $startdate = substr($startdate,0,4);
3170 $enddate = substr($enddate,0,4);
3172 while ($enddate >= $startdate) {
3173 push @all_years, $enddate--;
3178 $main::lxdebug->leave_sub();