1 #====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (C) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  15 # Contributors: Thomas Bayen <bayen@gmx.de>
 
  16 #               Antti Kaihola <akaihola@siba.fi>
 
  17 #               Moritz Bunkus (tex code)
 
  19 # This program is free software; you can redistribute it and/or modify
 
  20 # it under the terms of the GNU General Public License as published by
 
  21 # the Free Software Foundation; either version 2 of the License, or
 
  22 # (at your option) any later version.
 
  24 # This program is distributed in the hope that it will be useful,
 
  25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  27 # GNU General Public License for more details.
 
  28 # You should have received a copy of the GNU General Public License
 
  29 # along with this program; if not, write to the Free Software
 
  30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  31 #======================================================================
 
  32 # Utilities for parsing forms
 
  33 # and supporting routines for linking account numbers
 
  34 # used in AR, AP and IS, IR modules
 
  36 #======================================================================
 
  59 use List::Util qw(first max min sum);
 
  65     $standard_dbh->disconnect();
 
  71   $main::lxdebug->enter_sub(2);
 
  79   while ($key =~ /\[\+?\]\.|\./) {
 
  80     substr($key, 0, $+[0]) = '';
 
  88       if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
 
  89         push @{ $curr->{$`} }, { };
 
  92       $curr = $curr->{$`}->[-1];
 
  96   $curr->{$key} = $value;
 
  98   $main::lxdebug->leave_sub(2);
 
 100   return \$curr->{$key};
 
 104   $main::lxdebug->enter_sub(2);
 
 109   my @pairs = split(/&/, $input);
 
 112     my ($key, $value) = split(/=/, $_, 2);
 
 113     $self->_store_value($self->unescape($key), $self->unescape($value));
 
 116   $main::lxdebug->leave_sub(2);
 
 119 sub _request_to_hash {
 
 120   $main::lxdebug->enter_sub(2);
 
 125   if (!$ENV{'CONTENT_TYPE'}
 
 126       || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
 
 128     $self->_input_to_hash($input);
 
 130     $main::lxdebug->leave_sub(2);
 
 134   my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
 
 136   my $boundary = '--' . $1;
 
 138   foreach my $line (split m/\n/, $input) {
 
 139     last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
 
 141     if (($line eq $boundary) || ($line eq "$boundary\r")) {
 
 142       ${ $previous } =~ s|\r?\n$|| if $previous;
 
 148       $content_type   = "text/plain";
 
 155     next unless $boundary_found;
 
 157     if (!$headers_done) {
 
 158       $line =~ s/[\r\n]*$//;
 
 165       if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
 
 166         if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
 
 168           substr $line, $-[0], $+[0] - $-[0], "";
 
 171         if ($line =~ m|name\s*=\s*"(.*?)"|i) {
 
 173           substr $line, $-[0], $+[0] - $-[0], "";
 
 176         $previous         = $self->_store_value($name, '');
 
 177         $self->{FILENAME} = $filename if ($filename);
 
 182       if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
 
 189     next unless $previous;
 
 191     ${ $previous } .= "${line}\n";
 
 194   ${ $previous } =~ s|\r?\n$|| if $previous;
 
 196   $main::lxdebug->leave_sub(2);
 
 200   $main::lxdebug->enter_sub();
 
 206   if ($LXDebug::watch_form) {
 
 207     require SL::Watchdog;
 
 208     tie %{ $self }, 'SL::Watchdog';
 
 211   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 213   if ($ENV{QUERY_STRING}) {
 
 214     $_ = $ENV{QUERY_STRING};
 
 223   $self->_request_to_hash($_);
 
 225   $self->{action}  =  lc $self->{action};
 
 226   $self->{action}  =~ s/( |-|,|\#)/_/g;
 
 228   $self->{version} =  "2.6.0 beta 1";
 
 230   $main::lxdebug->leave_sub();
 
 235 sub _flatten_variables_rec {
 
 236   $main::lxdebug->enter_sub(2);
 
 245   if ('' eq ref $curr->{$key}) {
 
 246     @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
 
 248   } elsif ('HASH' eq ref $curr->{$key}) {
 
 249     foreach my $hash_key (sort keys %{ $curr->{$key} }) {
 
 250       push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
 
 254     foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
 
 255       my $first_array_entry = 1;
 
 257       foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
 
 258         push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
 
 259         $first_array_entry = 0;
 
 264   $main::lxdebug->leave_sub(2);
 
 269 sub flatten_variables {
 
 270   $main::lxdebug->enter_sub(2);
 
 278     push @variables, $self->_flatten_variables_rec($self, '', $_);
 
 281   $main::lxdebug->leave_sub(2);
 
 286 sub flatten_standard_variables {
 
 287   $main::lxdebug->enter_sub(2);
 
 290   my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
 
 294   foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
 
 295     push @variables, $self->_flatten_variables_rec($self, '', $_);
 
 298   $main::lxdebug->leave_sub(2);
 
 304   $main::lxdebug->enter_sub();
 
 310   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 312   $main::lxdebug->leave_sub();
 
 316   $main::lxdebug->enter_sub(2);
 
 319   my $password      = $self->{password};
 
 321   $self->{password} = 'X' x 8;
 
 323   local $Data::Dumper::Sortkeys = 1;
 
 324   my $output                    = Dumper($self);
 
 326   $self->{password} = $password;
 
 328   $main::lxdebug->leave_sub(2);
 
 334   $main::lxdebug->enter_sub(2);
 
 336   my ($self, $str) = @_;
 
 338   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 340   $main::lxdebug->leave_sub(2);
 
 346   $main::lxdebug->enter_sub(2);
 
 348   my ($self, $str) = @_;
 
 353   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 355   $main::lxdebug->leave_sub(2);
 
 361   my ($self, $str) = @_;
 
 363   if ($str && !ref($str)) {
 
 364     $str =~ s/\"/"/g;
 
 372   my ($self, $str) = @_;
 
 374   if ($str && !ref($str)) {
 
 375     $str =~ s/"/\"/g;
 
 386     map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 388     for (sort keys %$self) {
 
 389       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 390       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 397   $main::lxdebug->enter_sub();
 
 399   $main::lxdebug->show_backtrace();
 
 401   my ($self, $msg) = @_;
 
 402   if ($ENV{HTTP_USER_AGENT}) {
 
 404     $self->show_generic_error($msg);
 
 411   $main::lxdebug->leave_sub();
 
 415   $main::lxdebug->enter_sub();
 
 417   my ($self, $msg) = @_;
 
 419   if ($ENV{HTTP_USER_AGENT}) {
 
 422     if (!$self->{header}) {
 
 435     if ($self->{info_function}) {
 
 436       &{ $self->{info_function} }($msg);
 
 442   $main::lxdebug->leave_sub();
 
 445 # calculates the number of rows in a textarea based on the content and column number
 
 446 # can be capped with maxrows
 
 448   $main::lxdebug->enter_sub();
 
 449   my ($self, $str, $cols, $maxrows, $minrows) = @_;
 
 453   my $rows   = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
 
 456   $main::lxdebug->leave_sub();
 
 458   return max(min($rows, $maxrows), $minrows);
 
 462   $main::lxdebug->enter_sub();
 
 464   my ($self, $msg) = @_;
 
 466   $self->error("$msg\n" . $DBI::errstr);
 
 468   $main::lxdebug->leave_sub();
 
 472   $main::lxdebug->enter_sub();
 
 474   my ($self, $name, $msg) = @_;
 
 477   foreach my $part (split m/\./, $name) {
 
 478     if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
 
 481     $curr = $curr->{$part};
 
 484   $main::lxdebug->leave_sub();
 
 487 sub create_http_response {
 
 488   $main::lxdebug->enter_sub();
 
 493   my $cgi      = $main::cgi;
 
 494   $cgi       ||= CGI->new('');
 
 498   if ($ENV{HTTP_X_FORWARDED_FOR}) {
 
 499     $base_path =  $ENV{HTTP_REFERER};
 
 500     $base_path =~ s|^.*?://.*?/|/|;
 
 502     $base_path =  $ENV{REQUEST_URI};
 
 504   $base_path =~ s|[^/]+$||;
 
 505   $base_path =~ s|/$||;
 
 508   if (defined $main::auth) {
 
 509     my $session_cookie_value   = $main::auth->get_session_id();
 
 510     $session_cookie_value    ||= 'NO_SESSION';
 
 512     $session_cookie = $cgi->cookie('-name'  => $main::auth->get_session_cookie_name(),
 
 513                                    '-value' => $session_cookie_value,
 
 514                                    '-path'  => $base_path);
 
 517   my %cgi_params = ('-type' => $params{content_type});
 
 518   $cgi_params{'-charset'} = $params{charset} if ($params{charset});
 
 520   my $output = $cgi->header('-cookie' => $session_cookie,
 
 523   $main::lxdebug->leave_sub();
 
 530   $main::lxdebug->enter_sub();
 
 532   my ($self, $extra_code) = @_;
 
 534   if ($self->{header}) {
 
 535     $main::lxdebug->leave_sub();
 
 539   my ($stylesheet, $favicon, $pagelayout);
 
 541   if ($ENV{HTTP_USER_AGENT}) {
 
 544     if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
 
 545       # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
 
 546       $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
 
 549     my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
 
 551     $stylesheets =~ s|^\s*||;
 
 552     $stylesheets =~ s|\s*$||;
 
 553     foreach my $file (split m/\s+/, $stylesheets) {
 
 555       next if (! -f "css/$file");
 
 557       $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
 
 560     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 562     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 564         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 568     my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 570     if ($self->{landscape}) {
 
 571       $pagelayout = qq|<style type="text/css">
 
 572                         \@page { size:landscape; }
 
 576     my $fokus = qq|  document.$self->{fokus}.focus();| if ($self->{"fokus"});
 
 580     if ($self->{jsscript} == 1) {
 
 583         <script type="text/javascript" src="js/common.js"></script>
 
 584         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 585         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 586         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 587         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 594       ? "$self->{title} - $self->{titlebar}"
 
 597     foreach my $item (@ { $self->{AJAX} }) {
 
 598       $ajax .= $item->show_javascript();
 
 601     print $self->create_http_response('content_type' => 'text/html',
 
 602                                       'charset'      => $db_charset,);
 
 603     print qq|${doctype}<html>
 
 605   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
 
 606   <title>$self->{titlebar}</title>
 
 613   <script type="text/javascript">
 
 621   <meta name="robots" content="noindex,nofollow" />
 
 622   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 624   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 625   <script type="text/javascript" src="js/tabcontent.js">
 
 627   /***********************************************
 
 628    * Tab Content script v2.2- Â© Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 629    * This notice MUST stay intact for legal use
 
 630    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 631    ***********************************************/
 
 642   $main::lxdebug->leave_sub();
 
 645 sub ajax_response_header {
 
 646   $main::lxdebug->enter_sub();
 
 650   my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 651   my $cgi        = $main::cgi || CGI->new('');
 
 652   my $output     = $cgi->header('-charset' => $db_charset);
 
 654   $main::lxdebug->leave_sub();
 
 659 sub _prepare_html_template {
 
 660   $main::lxdebug->enter_sub();
 
 662   my ($self, $file, $additional_params) = @_;
 
 665   if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 666     $language = $main::language;
 
 668     $language = $main::myconfig{"countrycode"};
 
 670   $language = "de" unless ($language);
 
 672   if (-f "templates/webpages/${file}_${language}.html") {
 
 673     if ((-f ".developer") &&
 
 674         (-f "templates/webpages/${file}_master.html") &&
 
 675         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 676          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 677       my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 678         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 679       print(qq|<pre>$info</pre>|);
 
 683     $file = "templates/webpages/${file}_${language}.html";
 
 684   } elsif (-f "templates/webpages/${file}.html") {
 
 685     $file = "templates/webpages/${file}.html";
 
 687     my $info = "Web page template '${file}' not found.\n" .
 
 688       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 689     print(qq|<pre>$info</pre>|);
 
 693   if ($self->{"DEBUG"}) {
 
 694     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 697   if ($additional_params->{"DEBUG"}) {
 
 698     $additional_params->{"DEBUG"} =
 
 699       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 702   if (%main::myconfig) {
 
 703     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 704     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 705     $jsc_dateformat =~ s/d+/\%d/gi;
 
 706     $jsc_dateformat =~ s/m+/\%m/gi;
 
 707     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 708     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 711   $additional_params->{"conf_dbcharset"}              = $main::dbcharset;
 
 712   $additional_params->{"conf_webdav"}                 = $main::webdav;
 
 713   $additional_params->{"conf_lizenzen"}               = $main::lizenzen;
 
 714   $additional_params->{"conf_latex_templates"}        = $main::latex;
 
 715   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 717   if (%main::debug_options) {
 
 718     map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
 
 721   if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
 
 722     while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
 
 723       $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
 
 727   $main::lxdebug->leave_sub();
 
 732 sub parse_html_template {
 
 733   $main::lxdebug->enter_sub();
 
 735   my ($self, $file, $additional_params) = @_;
 
 737   $additional_params ||= { };
 
 739   $file = $self->_prepare_html_template($file, $additional_params);
 
 741   my $template = Template->new({ 'INTERPOLATE'  => 0,
 
 745                                  'PLUGIN_BASE'  => 'SL::Template::Plugin',
 
 746                                  'INCLUDE_PATH' => '.:templates/webpages',
 
 749   map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
 
 751   my $in = IO::File->new($file, 'r');
 
 754     print STDERR "Error opening template file: $!";
 
 755     $main::lxdebug->leave_sub();
 
 759   my $input = join('', <$in>);
 
 763     $input = $main::locale->{iconv}->convert($input);
 
 767   if (!$template->process(\$input, $additional_params, \$output)) {
 
 768     print STDERR $template->error();
 
 771   $main::lxdebug->leave_sub();
 
 776 sub show_generic_error {
 
 777   $main::lxdebug->enter_sub();
 
 779   my ($self, $error, %params) = @_;
 
 782     'title_error' => $params{title},
 
 783     'label_error' => $error,
 
 786   if ($params{action}) {
 
 789     map { delete($self->{$_}); } qw(action);
 
 790     map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
 
 792     $add_params->{SHOW_BUTTON}  = 1;
 
 793     $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
 
 794     $add_params->{VARIABLES}    = \@vars;
 
 796   } elsif ($params{back_button}) {
 
 797     $add_params->{SHOW_BACK_BUTTON} = 1;
 
 800   $self->{title} = $params{title} if $params{title};
 
 803   print $self->parse_html_template("generic/error", $add_params);
 
 805   $main::lxdebug->leave_sub();
 
 807   die("Error: $error\n");
 
 810 sub show_generic_information {
 
 811   $main::lxdebug->enter_sub();
 
 813   my ($self, $text, $title) = @_;
 
 816     'title_information' => $title,
 
 817     'label_information' => $text,
 
 820   $self->{title} = $title if ($title);
 
 823   print $self->parse_html_template("generic/information", $add_params);
 
 825   $main::lxdebug->leave_sub();
 
 827   die("Information: $text\n");
 
 830 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 831 # changed it to accept an arbitrary number of triggers - sschoeling
 
 833   $main::lxdebug->enter_sub();
 
 836   my $myconfig = shift;
 
 839   # set dateform for jsscript
 
 842     "dd.mm.yy" => "%d.%m.%Y",
 
 843     "dd-mm-yy" => "%d-%m-%Y",
 
 844     "dd/mm/yy" => "%d/%m/%Y",
 
 845     "mm/dd/yy" => "%m/%d/%Y",
 
 846     "mm-dd-yy" => "%m-%d-%Y",
 
 847     "yyyy-mm-dd" => "%Y-%m-%d",
 
 850   my $ifFormat = defined($dateformats{$myconfig->{"dateformat"}}) ?
 
 851     $dateformats{$myconfig->{"dateformat"}} : "%d.%m.%Y";
 
 858       inputField : "| . (shift) . qq|",
 
 859       ifFormat :"$ifFormat",
 
 860       align : "| .  (shift) . qq|",
 
 861       button : "| . (shift) . qq|"
 
 867        <script type="text/javascript">
 
 868        <!--| . join("", @triggers) . qq|//-->
 
 872   $main::lxdebug->leave_sub();
 
 875 }    #end sub write_trigger
 
 878   $main::lxdebug->enter_sub();
 
 880   my ($self, $msg) = @_;
 
 882   if ($self->{callback}) {
 
 884     my ($script, $argv) = split(/\?/, $self->{callback}, 2);
 
 886     $script =~ s|[^a-zA-Z0-9_\.]||g;
 
 887     exec("perl", "$script", $argv);
 
 895   $main::lxdebug->leave_sub();
 
 898 # sort of columns removed - empty sub
 
 900   $main::lxdebug->enter_sub();
 
 902   my ($self, @columns) = @_;
 
 904   $main::lxdebug->leave_sub();
 
 910   $main::lxdebug->enter_sub(2);
 
 912   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 918   # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
 
 920   my $neg = ($amount =~ s/^-//);
 
 921   my $exp = ($amount =~ m/[e]/) ? 1 : 0;
 
 923   if (defined($places) && ($places ne '')) {
 
 929         my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 930         $actual_places = length($actual_places);
 
 931         $places = $actual_places > $places ? $actual_places : $places;
 
 934     $amount = $self->round_amount($amount, $places);
 
 937   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 938   my @p = split(/\./, $amount); # split amount at decimal point
 
 940   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 943   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 946     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 947     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 948                         ($neg ? "-$amount"   : "$amount" )    ;
 
 952   $main::lxdebug->leave_sub(2);
 
 956 sub format_amount_units {
 
 957   $main::lxdebug->enter_sub();
 
 962   my $myconfig         = \%main::myconfig;
 
 963   my $amount           = $params{amount} * 1;
 
 964   my $places           = $params{places};
 
 965   my $part_unit_name   = $params{part_unit};
 
 966   my $amount_unit_name = $params{amount_unit};
 
 967   my $conv_units       = $params{conv_units};
 
 968   my $max_places       = $params{max_places};
 
 970   if (!$part_unit_name) {
 
 971     $main::lxdebug->leave_sub();
 
 975   AM->retrieve_all_units();
 
 976   my $all_units        = $main::all_units;
 
 978   if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
 
 979     $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
 
 982   if (!scalar @{ $conv_units }) {
 
 983     my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
 
 984     $main::lxdebug->leave_sub();
 
 988   my $part_unit  = $all_units->{$part_unit_name};
 
 989   my $conv_unit  = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
 
 991   $amount       *= $conv_unit->{factor};
 
 996   foreach my $unit (@$conv_units) {
 
 997     my $last = $unit->{name} eq $part_unit->{name};
 
 999       $num     = int($amount / $unit->{factor});
 
1000       $amount -= $num * $unit->{factor};
 
1003     if ($last ? $amount : $num) {
 
1004       push @values, { "unit"   => $unit->{name},
 
1005                       "amount" => $last ? $amount / $unit->{factor} : $num,
 
1006                       "places" => $last ? $places : 0 };
 
1013     push @values, { "unit"   => $part_unit_name,
 
1018   my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
 
1020   $main::lxdebug->leave_sub();
 
1026   $main::lxdebug->enter_sub(2);
 
1031   $input =~ s/(^|[^\#]) \#  (\d+)  /$1$_[$2 - 1]/gx;
 
1032   $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
 
1033   $input =~ s/\#\#/\#/g;
 
1035   $main::lxdebug->leave_sub(2);
 
1043   $main::lxdebug->enter_sub(2);
 
1045   my ($self, $myconfig, $amount) = @_;
 
1047   if (   ($myconfig->{numberformat} eq '1.000,00')
 
1048       || ($myconfig->{numberformat} eq '1000,00')) {
 
1053   if ($myconfig->{numberformat} eq "1'000.00") {
 
1059   $main::lxdebug->leave_sub(2);
 
1061   return ($amount * 1);
 
1065   $main::lxdebug->enter_sub(2);
 
1067   my ($self, $amount, $places) = @_;
 
1070   # Rounding like "Kaufmannsrunden"
 
1071   # Descr. http://de.wikipedia.org/wiki/Rundung
 
1073   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
1076   $amount = $amount * (10**($places));
 
1077   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
1079   $main::lxdebug->leave_sub(2);
 
1081   return $round_amount;
 
1085 sub parse_template {
 
1086   $main::lxdebug->enter_sub();
 
1088   my ($self, $myconfig, $userspath) = @_;
 
1089   my ($template, $out);
 
1093   $self->{"cwd"} = getcwd();
 
1094   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
1096   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
1097     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1098   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
1099     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
1100     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1101   } elsif (($self->{"format"} =~ /html/i) ||
 
1102            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
1103     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1104   } elsif (($self->{"format"} =~ /xml/i) ||
 
1105              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
1106     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1107   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
1108     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1109   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
1110     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1111   } elsif ( defined $self->{'format'}) {
 
1112     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
1113   } elsif ( $self->{'format'} eq '' ) {
 
1114     $self->error("No Outputformat given: $self->{'format'}");
 
1115   } else { #Catch the rest
 
1116     $self->error("Outputformat not defined: $self->{'format'}");
 
1119   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
1120   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
1122   if (!$self->{employee_id}) {
 
1123     map { $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature company address businessnumber co_ustid taxnumber duns);
 
1126   map { $self->{"${_}"} = $myconfig->{$_}; } qw(co_ustid);
 
1128   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
1130   # OUT is used for the media, screen, printer, email
 
1131   # for postscript we store a copy in a temporary file
 
1133   my $prepend_userspath;
 
1135   if (!$self->{tmpfile}) {
 
1136     $self->{tmpfile}   = "${fileid}.$self->{IN}";
 
1137     $prepend_userspath = 1;
 
1140   $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
 
1142   $self->{tmpfile} =~ s|.*/||;
 
1143   $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
 
1144   $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
 
1146   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
1147     $out = $self->{OUT};
 
1148     $self->{OUT} = ">$self->{tmpfile}";
 
1152     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
1154     open(OUT, ">-") or $self->error("STDOUT : $!");
 
1158   if (!$template->parse(*OUT)) {
 
1160     $self->error("$self->{IN} : " . $template->get_error());
 
1165   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
1167     if ($self->{media} eq 'email') {
 
1169       my $mail = new Mailer;
 
1171       map { $mail->{$_} = $self->{$_} }
 
1172         qw(cc bcc subject message version format);
 
1173       $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
1174       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
1175       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
1176       $mail->{fileid} = "$fileid.";
 
1177       $myconfig->{signature} =~ s/\r//g;
 
1179       # if we send html or plain text inline
 
1180       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
1181         $mail->{contenttype} = "text/html";
 
1183         $mail->{message}       =~ s/\r//g;
 
1184         $mail->{message}       =~ s/\n/<br>\n/g;
 
1185         $myconfig->{signature} =~ s/\n/<br>\n/g;
 
1186         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
1188         open(IN, $self->{tmpfile})
 
1189           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1191           $mail->{message} .= $_;
 
1198         if (!$self->{"do_not_attach"}) {
 
1199           @{ $mail->{attachments} } =
 
1200             ({ "filename" => $self->{"tmpfile"},
 
1201                "name" => $self->{"attachment_filename"} ?
 
1202                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
1205         $mail->{message}  =~ s/\r//g;
 
1206         $mail->{message} .=  "\n-- \n$myconfig->{signature}";
 
1210       my $err = $mail->send();
 
1211       $self->error($self->cleanup . "$err") if ($err);
 
1215       $self->{OUT} = $out;
 
1217       my $numbytes = (-s $self->{tmpfile});
 
1218       open(IN, $self->{tmpfile})
 
1219         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1221       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
1223       chdir("$self->{cwd}");
 
1224       #print(STDERR "Kopien $self->{copies}\n");
 
1225       #print(STDERR "OUT $self->{OUT}\n");
 
1226       for my $i (1 .. $self->{copies}) {
 
1228           open(OUT, $self->{OUT})
 
1229             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
1231           $self->{attachment_filename} = ($self->{attachment_filename}) 
 
1232                                        ? $self->{attachment_filename}
 
1233                                        : $self->generate_attachment_filename();
 
1235           # launch application
 
1236           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
1237 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
1238 Content-Length: $numbytes
 
1242           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
1262   chdir("$self->{cwd}");
 
1263   $main::lxdebug->leave_sub();
 
1266 sub get_formname_translation {
 
1267   my ($self, $formname) = @_;
 
1269   $formname ||= $self->{formname};
 
1271   my %formname_translations = (
 
1272     bin_list                => $main::locale->text('Bin List'),
 
1273     credit_note             => $main::locale->text('Credit Note'),
 
1274     invoice                 => $main::locale->text('Invoice'),
 
1275     packing_list            => $main::locale->text('Packing List'),
 
1276     pick_list               => $main::locale->text('Pick List'),
 
1277     proforma                => $main::locale->text('Proforma Invoice'),
 
1278     purchase_order          => $main::locale->text('Purchase Order'),
 
1279     request_quotation       => $main::locale->text('RFQ'),
 
1280     sales_order             => $main::locale->text('Confirmation'),
 
1281     sales_quotation         => $main::locale->text('Quotation'),
 
1282     storno_invoice          => $main::locale->text('Storno Invoice'),
 
1283     storno_packing_list     => $main::locale->text('Storno Packing List'),
 
1284     sales_delivery_order    => $main::locale->text('Delivery Order'),
 
1285     purchase_delivery_order => $main::locale->text('Delivery Order'),
 
1288   return $formname_translations{$formname}
 
1291 sub get_number_prefix_for_type {
 
1295       (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
 
1296     : ($self->{type} =~ /_quotation$/)                        ? 'quo'
 
1297     : ($self->{type} =~ /_delivery_order$/)                   ? 'do'
 
1303 sub get_extension_for_format {
 
1306   my $extension = $self->{format} =~ /pdf/i          ? ".pdf"
 
1307                 : $self->{format} =~ /postscript/i   ? ".ps"
 
1308                 : $self->{format} =~ /opendocument/i ? ".odt"
 
1309                 : $self->{format} =~ /html/i         ? ".html"
 
1315 sub generate_attachment_filename {
 
1318   my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
 
1319   my $prefix              = $self->get_number_prefix_for_type();
 
1321   if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
 
1322     $attachment_filename .= ' (' . $main::locale->text('Preview') . ')' . $self->get_extension_for_format();
 
1324   } elsif ($attachment_filename && $self->{"${prefix}number"}) {
 
1325     $attachment_filename .=  "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
 
1328     $attachment_filename = "";
 
1331   $attachment_filename =  $main::locale->quote_special_chars('filenames', $attachment_filename);
 
1332   $attachment_filename =~ s|[\s/\\]+|_|g;
 
1334   return $attachment_filename;
 
1337 sub generate_email_subject {
 
1340   my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
 
1341   my $prefix  = $self->get_number_prefix_for_type();
 
1343   if ($subject && $self->{"${prefix}number"}) {
 
1344     $subject .= " " . $self->{"${prefix}number"}
 
1351   $main::lxdebug->enter_sub();
 
1355   chdir("$self->{tmpdir}");
 
1358   if (-f "$self->{tmpfile}.err") {
 
1359     open(FH, "$self->{tmpfile}.err");
 
1364   if ($self->{tmpfile}) {
 
1365     $self->{tmpfile} =~ s|.*/||g;
 
1367     $self->{tmpfile} =~ s/\.\w+$//g;
 
1368     my $tmpfile = $self->{tmpfile};
 
1369     unlink(<$tmpfile.*>);
 
1372   chdir("$self->{cwd}");
 
1374   $main::lxdebug->leave_sub();
 
1380   $main::lxdebug->enter_sub();
 
1382   my ($self, $date, $myconfig) = @_;
 
1385   if ($date && $date =~ /\D/) {
 
1387     if ($myconfig->{dateformat} =~ /^yy/) {
 
1388       ($yy, $mm, $dd) = split /\D/, $date;
 
1390     if ($myconfig->{dateformat} =~ /^mm/) {
 
1391       ($mm, $dd, $yy) = split /\D/, $date;
 
1393     if ($myconfig->{dateformat} =~ /^dd/) {
 
1394       ($dd, $mm, $yy) = split /\D/, $date;
 
1399     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
1400     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
1402     $dd = "0$dd" if ($dd < 10);
 
1403     $mm = "0$mm" if ($mm < 10);
 
1405     $date = "$yy$mm$dd";
 
1408   $main::lxdebug->leave_sub();
 
1413 # Database routines used throughout
 
1416   $main::lxdebug->enter_sub(2);
 
1418   my ($self, $myconfig) = @_;
 
1420   # connect to database
 
1422     DBI->connect($myconfig->{dbconnect},
 
1423                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
1427   if ($myconfig->{dboptions}) {
 
1428     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1431   $main::lxdebug->leave_sub(2);
 
1436 sub dbconnect_noauto {
 
1437   $main::lxdebug->enter_sub();
 
1439   my ($self, $myconfig) = @_;
 
1441   # connect to database
 
1443     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
1444                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
1448   if ($myconfig->{dboptions}) {
 
1449     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1452   $main::lxdebug->leave_sub();
 
1457 sub get_standard_dbh {
 
1458   $main::lxdebug->enter_sub(2);
 
1460   my ($self, $myconfig) = @_;
 
1462   if ($standard_dbh && !$standard_dbh->{Active}) {
 
1463     $main::lxdebug->message(LXDebug::INFO, "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
 
1464     undef $standard_dbh;
 
1467   $standard_dbh ||= $self->dbconnect_noauto($myconfig);
 
1469   $main::lxdebug->leave_sub(2);
 
1471   return $standard_dbh;
 
1475   $main::lxdebug->enter_sub();
 
1477   my ($self, $date, $myconfig) = @_;
 
1478   my $dbh = $self->dbconnect($myconfig);
 
1480   my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
 
1481   my $sth = prepare_execute_query($self, $dbh, $query, $date);
 
1482   my ($closed) = $sth->fetchrow_array;
 
1484   $main::lxdebug->leave_sub();
 
1489 sub update_balance {
 
1490   $main::lxdebug->enter_sub();
 
1492   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
1494   # if we have a value, go do it
 
1497     # retrieve balance from table
 
1498     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1499     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1500     my ($balance) = $sth->fetchrow_array;
 
1506     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1507     do_query($self, $dbh, $query, @values);
 
1509   $main::lxdebug->leave_sub();
 
1512 sub update_exchangerate {
 
1513   $main::lxdebug->enter_sub();
 
1515   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1517   # some sanity check for currency
 
1519     $main::lxdebug->leave_sub();
 
1522   $query = qq|SELECT curr FROM defaults|;
 
1524   my ($currency) = selectrow_query($self, $dbh, $query);
 
1525   my ($defaultcurrency) = split m/:/, $currency;
 
1528   if ($curr eq $defaultcurrency) {
 
1529     $main::lxdebug->leave_sub();
 
1533   $query = qq|SELECT e.curr FROM exchangerate e
 
1534                  WHERE e.curr = ? AND e.transdate = ?
 
1536   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1545   $buy = conv_i($buy, "NULL");
 
1546   $sell = conv_i($sell, "NULL");
 
1549   if ($buy != 0 && $sell != 0) {
 
1550     $set = "buy = $buy, sell = $sell";
 
1551   } elsif ($buy != 0) {
 
1552     $set = "buy = $buy";
 
1553   } elsif ($sell != 0) {
 
1554     $set = "sell = $sell";
 
1557   if ($sth->fetchrow_array) {
 
1558     $query = qq|UPDATE exchangerate
 
1564     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1565                 VALUES (?, $buy, $sell, ?)|;
 
1568   do_query($self, $dbh, $query, $curr, $transdate);
 
1570   $main::lxdebug->leave_sub();
 
1573 sub save_exchangerate {
 
1574   $main::lxdebug->enter_sub();
 
1576   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1578   my $dbh = $self->dbconnect($myconfig);
 
1582   $buy  = $rate if $fld eq 'buy';
 
1583   $sell = $rate if $fld eq 'sell';
 
1586   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1591   $main::lxdebug->leave_sub();
 
1594 sub get_exchangerate {
 
1595   $main::lxdebug->enter_sub();
 
1597   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1600   unless ($transdate) {
 
1601     $main::lxdebug->leave_sub();
 
1605   $query = qq|SELECT curr FROM defaults|;
 
1607   my ($currency) = selectrow_query($self, $dbh, $query);
 
1608   my ($defaultcurrency) = split m/:/, $currency;
 
1610   if ($currency eq $defaultcurrency) {
 
1611     $main::lxdebug->leave_sub();
 
1615   $query = qq|SELECT e.$fld FROM exchangerate e
 
1616                  WHERE e.curr = ? AND e.transdate = ?|;
 
1617   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1621   $main::lxdebug->leave_sub();
 
1623   return $exchangerate;
 
1626 sub check_exchangerate {
 
1627   $main::lxdebug->enter_sub();
 
1629   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1631   unless ($transdate) {
 
1632     $main::lxdebug->leave_sub();
 
1636   my ($defaultcurrency) = $self->get_default_currency($myconfig);
 
1638   if ($currency eq $defaultcurrency) {
 
1639     $main::lxdebug->leave_sub();
 
1643   my $dbh   = $self->get_standard_dbh($myconfig);
 
1644   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1645                  WHERE e.curr = ? AND e.transdate = ?|;
 
1647   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1649   $main::lxdebug->leave_sub();
 
1651   return $exchangerate;
 
1654 sub get_default_currency {
 
1655   $main::lxdebug->enter_sub();
 
1657   my ($self, $myconfig) = @_;
 
1658   my $dbh = $self->get_standard_dbh($myconfig);
 
1660   my $query = qq|SELECT curr FROM defaults|;
 
1662   my ($curr)            = selectrow_query($self, $dbh, $query);
 
1663   my ($defaultcurrency) = split m/:/, $curr;
 
1665   $main::lxdebug->leave_sub();
 
1667   return $defaultcurrency;
 
1671 sub set_payment_options {
 
1672   $main::lxdebug->enter_sub();
 
1674   my ($self, $myconfig, $transdate) = @_;
 
1676   return $main::lxdebug->leave_sub() unless ($self->{payment_id});
 
1678   my $dbh = $self->get_standard_dbh($myconfig);
 
1681     qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
 
1682     qq|FROM payment_terms p | .
 
1685   ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1686    $self->{payment_terms}) =
 
1687      selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1689   if ($transdate eq "") {
 
1690     if ($self->{invdate}) {
 
1691       $transdate = $self->{invdate};
 
1693       $transdate = $self->{transdate};
 
1698     qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1699     qq|FROM payment_terms|;
 
1700   ($self->{netto_date}, $self->{skonto_date}) =
 
1701     selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1703   my ($invtotal, $total);
 
1704   my (%amounts, %formatted_amounts);
 
1706   if ($self->{type} =~ /_order$/) {
 
1707     $amounts{invtotal} = $self->{ordtotal};
 
1708     $amounts{total}    = $self->{ordtotal};
 
1710   } elsif ($self->{type} =~ /_quotation$/) {
 
1711     $amounts{invtotal} = $self->{quototal};
 
1712     $amounts{total}    = $self->{quototal};
 
1715     $amounts{invtotal} = $self->{invtotal};
 
1716     $amounts{total}    = $self->{total};
 
1719   map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
 
1721   $amounts{skonto_amount}      = $amounts{invtotal} * $self->{percent_skonto};
 
1722   $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
 
1723   $amounts{total_wo_skonto}    = $amounts{total}    * (1 - $self->{percent_skonto});
 
1725   foreach (keys %amounts) {
 
1726     $amounts{$_}           = $self->round_amount($amounts{$_}, 2);
 
1727     $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
 
1730   if ($self->{"language_id"}) {
 
1732       qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1733       qq|FROM translation_payment_terms t | .
 
1734       qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1735       qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1736     my ($description_long, $output_numberformat, $output_dateformat,
 
1737       $output_longdates) =
 
1738       selectrow_query($self, $dbh, $query,
 
1739                       $self->{"language_id"}, $self->{"payment_id"});
 
1741     $self->{payment_terms} = $description_long if ($description_long);
 
1743     if ($output_dateformat) {
 
1744       foreach my $key (qw(netto_date skonto_date)) {
 
1746           $main::locale->reformat_date($myconfig, $self->{$key},
 
1752     if ($output_numberformat &&
 
1753         ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1754       my $saved_numberformat = $myconfig->{"numberformat"};
 
1755       $myconfig->{"numberformat"} = $output_numberformat;
 
1756       map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
 
1757       $myconfig->{"numberformat"} = $saved_numberformat;
 
1761   $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1762   $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1763   $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1764   $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1765   $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1766   $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1767   $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1769   map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
 
1771   $main::lxdebug->leave_sub();
 
1775 sub get_template_language {
 
1776   $main::lxdebug->enter_sub();
 
1778   my ($self, $myconfig) = @_;
 
1780   my $template_code = "";
 
1782   if ($self->{language_id}) {
 
1783     my $dbh = $self->get_standard_dbh($myconfig);
 
1784     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1785     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1788   $main::lxdebug->leave_sub();
 
1790   return $template_code;
 
1793 sub get_printer_code {
 
1794   $main::lxdebug->enter_sub();
 
1796   my ($self, $myconfig) = @_;
 
1798   my $template_code = "";
 
1800   if ($self->{printer_id}) {
 
1801     my $dbh = $self->get_standard_dbh($myconfig);
 
1802     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1803     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1806   $main::lxdebug->leave_sub();
 
1808   return $template_code;
 
1812   $main::lxdebug->enter_sub();
 
1814   my ($self, $myconfig) = @_;
 
1816   my $template_code = "";
 
1818   if ($self->{shipto_id}) {
 
1819     my $dbh = $self->get_standard_dbh($myconfig);
 
1820     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1821     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1822     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1825   $main::lxdebug->leave_sub();
 
1829   $main::lxdebug->enter_sub();
 
1831   my ($self, $dbh, $id, $module) = @_;
 
1836   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1837                        contact phone fax email)) {
 
1838     if ($self->{"shipto$item"}) {
 
1839       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1841     push(@values, $self->{"shipto${item}"});
 
1845     if ($self->{shipto_id}) {
 
1846       my $query = qq|UPDATE shipto set
 
1848                        shiptodepartment_1 = ?,
 
1849                        shiptodepartment_2 = ?,
 
1858                      WHERE shipto_id = ?|;
 
1859       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1861       my $query = qq|SELECT * FROM shipto
 
1862                      WHERE shiptoname = ? AND
 
1863                        shiptodepartment_1 = ? AND
 
1864                        shiptodepartment_2 = ? AND
 
1865                        shiptostreet = ? AND
 
1866                        shiptozipcode = ? AND
 
1868                        shiptocountry = ? AND
 
1869                        shiptocontact = ? AND
 
1875       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
 
1878           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1879                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1880                                  shiptocontact, shiptophone, shiptofax, shiptoemail, module)
 
1881              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1882         do_query($self, $dbh, $query, $id, @values, $module);
 
1887   $main::lxdebug->leave_sub();
 
1891   $main::lxdebug->enter_sub();
 
1893   my ($self, $dbh) = @_;
 
1895   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1896   ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1897   $self->{"employee_id"} *= 1;
 
1899   $main::lxdebug->leave_sub();
 
1902 sub get_employee_data {
 
1903   $main::lxdebug->enter_sub();
 
1908   Common::check_params(\%params, qw(prefix));
 
1909   Common::check_params_x(\%params, qw(id));
 
1912     $main::lxdebug->leave_sub();
 
1916   my $myconfig = \%main::myconfig;
 
1917   my $dbh      = $params{dbh} || $self->get_standard_dbh($myconfig);
 
1919   my ($login)  = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
 
1922     my $user = User->new($login);
 
1923     map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
 
1925     $self->{$params{prefix} . '_login'}   = $login;
 
1926     $self->{$params{prefix} . '_name'}  ||= $login;
 
1929   $main::lxdebug->leave_sub();
 
1933   $main::lxdebug->enter_sub();
 
1935   my ($self, $myconfig) = @_;
 
1937   my $dbh = $self->get_standard_dbh($myconfig);
 
1938   my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
 
1939   ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1941   $main::lxdebug->leave_sub();
 
1945   $main::lxdebug->enter_sub();
 
1947   my ($self, $dbh, $id, $key) = @_;
 
1949   $key = "all_contacts" unless ($key);
 
1953     $main::lxdebug->leave_sub();
 
1958     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1959     qq|FROM contacts | .
 
1960     qq|WHERE cp_cv_id = ? | .
 
1961     qq|ORDER BY lower(cp_name)|;
 
1963   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1965   $main::lxdebug->leave_sub();
 
1969   $main::lxdebug->enter_sub();
 
1971   my ($self, $dbh, $key) = @_;
 
1973   my ($all, $old_id, $where, @values);
 
1975   if (ref($key) eq "HASH") {
 
1978     $key = "ALL_PROJECTS";
 
1980     foreach my $p (keys(%{$params})) {
 
1982         $all = $params->{$p};
 
1983       } elsif ($p eq "old_id") {
 
1984         $old_id = $params->{$p};
 
1985       } elsif ($p eq "key") {
 
1986         $key = $params->{$p};
 
1992     $where = "WHERE active ";
 
1994       if (ref($old_id) eq "ARRAY") {
 
1995         my @ids = grep({ $_ } @{$old_id});
 
1997           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1998           push(@values, @ids);
 
2001         $where .= " OR (id = ?) ";
 
2002         push(@values, $old_id);
 
2008     qq|SELECT id, projectnumber, description, active | .
 
2011     qq|ORDER BY lower(projectnumber)|;
 
2013   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
2015   $main::lxdebug->leave_sub();
 
2019   $main::lxdebug->enter_sub();
 
2021   my ($self, $dbh, $vc_id, $key) = @_;
 
2023   $key = "all_shipto" unless ($key);
 
2026     # get shipping addresses
 
2027     my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
 
2029     $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
2035   $main::lxdebug->leave_sub();
 
2039   $main::lxdebug->enter_sub();
 
2041   my ($self, $dbh, $key) = @_;
 
2043   $key = "all_printers" unless ($key);
 
2045   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
2047   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2049   $main::lxdebug->leave_sub();
 
2053   $main::lxdebug->enter_sub();
 
2055   my ($self, $dbh, $params) = @_;
 
2058   $key = $params->{key};
 
2059   $key = "all_charts" unless ($key);
 
2061   my $transdate = quote_db_date($params->{transdate});
 
2064     qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
2066     qq|LEFT JOIN taxkeys tk ON | .
 
2067     qq|(tk.id = (SELECT id FROM taxkeys | .
 
2068     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
2069     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
2070     qq|ORDER BY c.accno|;
 
2072   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2074   $main::lxdebug->leave_sub();
 
2077 sub _get_taxcharts {
 
2078   $main::lxdebug->enter_sub();
 
2080   my ($self, $dbh, $params) = @_;
 
2082   my $key = "all_taxcharts";
 
2085   if (ref $params eq 'HASH') {
 
2086     $key = $params->{key} if ($params->{key});
 
2087     if ($params->{module} eq 'AR') {
 
2088       push @where, 'taxkey NOT IN (8, 9, 18, 19)';
 
2090     } elsif ($params->{module} eq 'AP') {
 
2091       push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
 
2098   my $where = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (@where);
 
2100   my $query = qq|SELECT * FROM tax $where ORDER BY taxkey|;
 
2102   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2104   $main::lxdebug->leave_sub();
 
2108   $main::lxdebug->enter_sub();
 
2110   my ($self, $dbh, $key) = @_;
 
2112   $key = "all_taxzones" unless ($key);
 
2114   my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
 
2116   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2118   $main::lxdebug->leave_sub();
 
2121 sub _get_employees {
 
2122   $main::lxdebug->enter_sub();
 
2124   my ($self, $dbh, $default_key, $key) = @_;
 
2126   $key = $default_key unless ($key);
 
2127   $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
 
2129   $main::lxdebug->leave_sub();
 
2132 sub _get_business_types {
 
2133   $main::lxdebug->enter_sub();
 
2135   my ($self, $dbh, $key) = @_;
 
2137   $key = "all_business_types" unless ($key);
 
2139     selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
 
2141   $main::lxdebug->leave_sub();
 
2144 sub _get_languages {
 
2145   $main::lxdebug->enter_sub();
 
2147   my ($self, $dbh, $key) = @_;
 
2149   $key = "all_languages" unless ($key);
 
2151   my $query = qq|SELECT * FROM language ORDER BY id|;
 
2153   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2155   $main::lxdebug->leave_sub();
 
2158 sub _get_dunning_configs {
 
2159   $main::lxdebug->enter_sub();
 
2161   my ($self, $dbh, $key) = @_;
 
2163   $key = "all_dunning_configs" unless ($key);
 
2165   my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
 
2167   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2169   $main::lxdebug->leave_sub();
 
2172 sub _get_currencies {
 
2173 $main::lxdebug->enter_sub();
 
2175   my ($self, $dbh, $key) = @_;
 
2177   $key = "all_currencies" unless ($key);
 
2179   my $query = qq|SELECT curr AS currency FROM defaults|;
 
2181   $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
 
2183   $main::lxdebug->leave_sub();
 
2187 $main::lxdebug->enter_sub();
 
2189   my ($self, $dbh, $key) = @_;
 
2191   $key = "all_payments" unless ($key);
 
2193   my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
 
2195   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2197   $main::lxdebug->leave_sub();
 
2200 sub _get_customers {
 
2201   $main::lxdebug->enter_sub();
 
2203   my ($self, $dbh, $key, $limit) = @_;
 
2205   $key = "all_customers" unless ($key);
 
2206   my $limit_clause = "LIMIT $limit" if $limit;
 
2208   my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
 
2210   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2212   $main::lxdebug->leave_sub();
 
2216   $main::lxdebug->enter_sub();
 
2218   my ($self, $dbh, $key) = @_;
 
2220   $key = "all_vendors" unless ($key);
 
2222   my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
 
2224   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2226   $main::lxdebug->leave_sub();
 
2229 sub _get_departments {
 
2230   $main::lxdebug->enter_sub();
 
2232   my ($self, $dbh, $key) = @_;
 
2234   $key = "all_departments" unless ($key);
 
2236   my $query = qq|SELECT * FROM department ORDER BY description|;
 
2238   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2240   $main::lxdebug->leave_sub();
 
2243 sub _get_warehouses {
 
2244   $main::lxdebug->enter_sub();
 
2246   my ($self, $dbh, $param) = @_;
 
2248   my ($key, $bins_key);
 
2250   if ('' eq ref $param) {
 
2254     $key      = $param->{key};
 
2255     $bins_key = $param->{bins};
 
2258   my $query = qq|SELECT w.* FROM warehouse w
 
2259                  WHERE (NOT w.invalid) AND
 
2260                    ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
 
2261                  ORDER BY w.sortkey|;
 
2263   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2266     $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
 
2267     my $sth = prepare_query($self, $dbh, $query);
 
2269     foreach my $warehouse (@{ $self->{$key} }) {
 
2270       do_statement($self, $sth, $query, $warehouse->{id});
 
2271       $warehouse->{$bins_key} = [];
 
2273       while (my $ref = $sth->fetchrow_hashref()) {
 
2274         push @{ $warehouse->{$bins_key} }, $ref;
 
2280   $main::lxdebug->leave_sub();
 
2284   $main::lxdebug->enter_sub();
 
2286   my ($self, $dbh, $table, $key, $sortkey) = @_;
 
2288   my $query  = qq|SELECT * FROM $table|;
 
2289   $query    .= qq| ORDER BY $sortkey| if ($sortkey);
 
2291   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2293   $main::lxdebug->leave_sub();
 
2297 #  $main::lxdebug->enter_sub();
 
2299 #  my ($self, $dbh, $key) = @_;
 
2301 #  $key ||= "all_groups";
 
2303 #  my $groups = $main::auth->read_groups();
 
2305 #  $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2307 #  $main::lxdebug->leave_sub();
 
2311   $main::lxdebug->enter_sub();
 
2316   my $dbh = $self->get_standard_dbh(\%main::myconfig);
 
2317   my ($sth, $query, $ref);
 
2319   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
2320   my $vc_id = $self->{"${vc}_id"};
 
2322   if ($params{"contacts"}) {
 
2323     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
2326   if ($params{"shipto"}) {
 
2327     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
2330   if ($params{"projects"} || $params{"all_projects"}) {
 
2331     $self->_get_projects($dbh, $params{"all_projects"} ?
 
2332                          $params{"all_projects"} : $params{"projects"},
 
2333                          $params{"all_projects"} ? 1 : 0);
 
2336   if ($params{"printers"}) {
 
2337     $self->_get_printers($dbh, $params{"printers"});
 
2340   if ($params{"languages"}) {
 
2341     $self->_get_languages($dbh, $params{"languages"});
 
2344   if ($params{"charts"}) {
 
2345     $self->_get_charts($dbh, $params{"charts"});
 
2348   if ($params{"taxcharts"}) {
 
2349     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
2352   if ($params{"taxzones"}) {
 
2353     $self->_get_taxzones($dbh, $params{"taxzones"});
 
2356   if ($params{"employees"}) {
 
2357     $self->_get_employees($dbh, "all_employees", $params{"employees"});
 
2360   if ($params{"salesmen"}) {
 
2361     $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
 
2364   if ($params{"business_types"}) {
 
2365     $self->_get_business_types($dbh, $params{"business_types"});
 
2368   if ($params{"dunning_configs"}) {
 
2369     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
 
2372   if($params{"currencies"}) {
 
2373     $self->_get_currencies($dbh, $params{"currencies"});
 
2376   if($params{"customers"}) {
 
2377     if (ref $params{"customers"} eq 'HASH') {
 
2378       $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
 
2380       $self->_get_customers($dbh, $params{"customers"});
 
2384   if($params{"vendors"}) {
 
2385     if (ref $params{"vendors"} eq 'HASH') {
 
2386       $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
 
2388       $self->_get_vendors($dbh, $params{"vendors"});
 
2392   if($params{"payments"}) {
 
2393     $self->_get_payments($dbh, $params{"payments"});
 
2396   if($params{"departments"}) {
 
2397     $self->_get_departments($dbh, $params{"departments"});
 
2400   if ($params{price_factors}) {
 
2401     $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
 
2404   if ($params{warehouses}) {
 
2405     $self->_get_warehouses($dbh, $params{warehouses});
 
2408 #  if ($params{groups}) {
 
2409 #    $self->_get_groups($dbh, $params{groups});
 
2412   if ($params{partsgroup}) {
 
2413     $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
 
2416   $main::lxdebug->leave_sub();
 
2419 # this sub gets the id and name from $table
 
2421   $main::lxdebug->enter_sub();
 
2423   my ($self, $myconfig, $table) = @_;
 
2425   # connect to database
 
2426   my $dbh = $self->get_standard_dbh($myconfig);
 
2428   $table = $table eq "customer" ? "customer" : "vendor";
 
2429   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
2431   my ($query, @values);
 
2433   if (!$self->{openinvoices}) {
 
2435     if ($self->{customernumber} ne "") {
 
2436       $where = qq|(vc.customernumber ILIKE ?)|;
 
2437       push(@values, '%' . $self->{customernumber} . '%');
 
2439       $where = qq|(vc.name ILIKE ?)|;
 
2440       push(@values, '%' . $self->{$table} . '%');
 
2444       qq~SELECT vc.id, vc.name,
 
2445            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
2447          WHERE $where AND (NOT vc.obsolete)
 
2451       qq~SELECT DISTINCT vc.id, vc.name,
 
2452            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
2454          JOIN $table vc ON (a.${table}_id = vc.id)
 
2455          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
2457     push(@values, '%' . $self->{$table} . '%');
 
2460   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
2462   $main::lxdebug->leave_sub();
 
2464   return scalar(@{ $self->{name_list} });
 
2467 # the selection sub is used in the AR, AP, IS, IR and OE module
 
2470   $main::lxdebug->enter_sub();
 
2472   my ($self, $myconfig, $table, $module) = @_;
 
2475   my $dbh = $self->get_standard_dbh($myconfig);
 
2477   $table = $table eq "customer" ? "customer" : "vendor";
 
2479   my $query = qq|SELECT count(*) FROM $table|;
 
2480   my ($count) = selectrow_query($self, $dbh, $query);
 
2482   # build selection list
 
2483   if ($count < $myconfig->{vclimit}) {
 
2484     $query = qq|SELECT id, name, salesman_id
 
2485                 FROM $table WHERE NOT obsolete
 
2487     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
2491   $self->get_employee($dbh);
 
2493   # setup sales contacts
 
2494   $query = qq|SELECT e.id, e.name
 
2496               WHERE (e.sales = '1') AND (NOT e.id = ?)|;
 
2497   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
2500   push(@{ $self->{all_employees} },
 
2501        { id   => $self->{employee_id},
 
2502          name => $self->{employee} });
 
2504   # sort the whole thing
 
2505   @{ $self->{all_employees} } =
 
2506     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
2508   if ($module eq 'AR') {
 
2510     # prepare query for departments
 
2511     $query = qq|SELECT id, description
 
2514                 ORDER BY description|;
 
2517     $query = qq|SELECT id, description
 
2519                 ORDER BY description|;
 
2522   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2525   $query = qq|SELECT id, description
 
2529   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2532   $query = qq|SELECT printer_description, id
 
2534               ORDER BY printer_description|;
 
2536   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2539   $query = qq|SELECT id, description
 
2543   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2545   $main::lxdebug->leave_sub();
 
2548 sub language_payment {
 
2549   $main::lxdebug->enter_sub();
 
2551   my ($self, $myconfig) = @_;
 
2553   my $dbh = $self->get_standard_dbh($myconfig);
 
2555   my $query = qq|SELECT id, description
 
2559   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2562   $query = qq|SELECT printer_description, id
 
2564               ORDER BY printer_description|;
 
2566   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2569   $query = qq|SELECT id, description
 
2573   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2575   # get buchungsgruppen
 
2576   $query = qq|SELECT id, description
 
2577               FROM buchungsgruppen|;
 
2579   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
2581   $main::lxdebug->leave_sub();
 
2584 # this is only used for reports
 
2585 sub all_departments {
 
2586   $main::lxdebug->enter_sub();
 
2588   my ($self, $myconfig, $table) = @_;
 
2590   my $dbh = $self->get_standard_dbh($myconfig);
 
2593   if ($table eq 'customer') {
 
2594     $where = "WHERE role = 'P' ";
 
2597   my $query = qq|SELECT id, description
 
2600                  ORDER BY description|;
 
2601   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2603   delete($self->{all_departments}) unless (@{ $self->{all_departments} });
 
2605   $main::lxdebug->leave_sub();
 
2609   $main::lxdebug->enter_sub();
 
2611   my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
 
2614   if ($table eq "customer") {
 
2623   $self->all_vc($myconfig, $table, $module);
 
2625   # get last customers or vendors
 
2626   my ($query, $sth, $ref);
 
2628   my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
 
2633     my $transdate = "current_date";
 
2634     if ($self->{transdate}) {
 
2635       $transdate = $dbh->quote($self->{transdate});
 
2638     # now get the account numbers
 
2639     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2640                 FROM chart c, taxkeys tk
 
2641                 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
2642                   (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
2645     $sth = $dbh->prepare($query);
 
2647     do_statement($self, $sth, $query, '%' . $module . '%');
 
2649     $self->{accounts} = "";
 
2650     while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2652       foreach my $key (split(/:/, $ref->{link})) {
 
2653         if ($key =~ /\Q$module\E/) {
 
2655           # cross reference for keys
 
2656           $xkeyref{ $ref->{accno} } = $key;
 
2658           push @{ $self->{"${module}_links"}{$key} },
 
2659             { accno       => $ref->{accno},
 
2660               description => $ref->{description},
 
2661               taxkey      => $ref->{taxkey_id},
 
2662               tax_id      => $ref->{tax_id} };
 
2664           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2670   # get taxkeys and description
 
2671   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
2672   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
2674   if (($module eq "AP") || ($module eq "AR")) {
 
2675     # get tax rates and description
 
2676     $query = qq|SELECT * FROM tax|;
 
2677     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
2683            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
2684            a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
 
2685            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
2686            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
2688            d.description AS department,
 
2691          JOIN $table c ON (a.${table}_id = c.id)
 
2692          LEFT JOIN employee e ON (e.id = a.employee_id)
 
2693          LEFT JOIN department d ON (d.id = a.department_id)
 
2695     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
2697     foreach my $key (keys %$ref) {
 
2698       $self->{$key} = $ref->{$key};
 
2701     my $transdate = "current_date";
 
2702     if ($self->{transdate}) {
 
2703       $transdate = $dbh->quote($self->{transdate});
 
2706     # now get the account numbers
 
2707     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2709                 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
 
2711                   AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
2712                     OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
 
2715     $sth = $dbh->prepare($query);
 
2716     do_statement($self, $sth, $query, "%$module%");
 
2718     $self->{accounts} = "";
 
2719     while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2721       foreach my $key (split(/:/, $ref->{link})) {
 
2722         if ($key =~ /\Q$module\E/) {
 
2724           # cross reference for keys
 
2725           $xkeyref{ $ref->{accno} } = $key;
 
2727           push @{ $self->{"${module}_links"}{$key} },
 
2728             { accno       => $ref->{accno},
 
2729               description => $ref->{description},
 
2730               taxkey      => $ref->{taxkey_id},
 
2731               tax_id      => $ref->{tax_id} };
 
2733           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2739     # get amounts from individual entries
 
2742            c.accno, c.description,
 
2743            a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
 
2747          LEFT JOIN chart c ON (c.id = a.chart_id)
 
2748          LEFT JOIN project p ON (p.id = a.project_id)
 
2749          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
2750                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
2751                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
2752                                         THEN tk.chart_id = a.chart_id
 
2755                                        OR (c.link='%tax%')) AND
 
2756                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
2757          WHERE a.trans_id = ?
 
2758          AND a.fx_transaction = '0'
 
2759          ORDER BY a.oid, a.transdate|;
 
2760     $sth = $dbh->prepare($query);
 
2761     do_statement($self, $sth, $query, $self->{id});
 
2763     # get exchangerate for currency
 
2764     $self->{exchangerate} =
 
2765       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2768     # store amounts in {acc_trans}{$key} for multiple accounts
 
2769     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2770       $ref->{exchangerate} =
 
2771         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
2772       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2775       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2776         $ref->{amount} *= -1;
 
2778       $ref->{index} = $index;
 
2780       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2786            d.curr AS currencies, d.closedto, d.revtrans,
 
2787            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2788            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2790     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2791     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2798             current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
 
2799             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2800             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2802     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2803     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2805     if ($self->{"$self->{vc}_id"}) {
 
2807       # only setup currency
 
2808       ($self->{currency}) = split(/:/, $self->{currencies});
 
2812       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2814       # get exchangerate for currency
 
2815       $self->{exchangerate} =
 
2816         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2822   $main::lxdebug->leave_sub();
 
2826   $main::lxdebug->enter_sub();
 
2828   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2832   $table         = $table eq "customer" ? "customer" : "vendor";
 
2833   my %column_map = ("a.curr"                  => "currency",
 
2834                     "a.${table}_id"           => "${table}_id",
 
2835                     "a.department_id"         => "department_id",
 
2836                     "d.description"           => "department",
 
2837                     "ct.name"                 => $table,
 
2838                     "current_date + ct.terms" => "duedate",
 
2841   if ($self->{type} =~ /delivery_order/) {
 
2842     $arap  = 'delivery_orders';
 
2843     delete $column_map{"a.curr"};
 
2845   } elsif ($self->{type} =~ /_order/) {
 
2847     $where = "quotation = '0'";
 
2849   } elsif ($self->{type} =~ /_quotation/) {
 
2851     $where = "quotation = '1'";
 
2853   } elsif ($table eq 'customer') {
 
2861   $where           = "($where) AND" if ($where);
 
2862   my $query        = qq|SELECT MAX(id) FROM $arap
 
2863                         WHERE $where ${table}_id > 0|;
 
2864   my ($trans_id)   = selectrow_query($self, $dbh, $query);
 
2867   my $column_spec  = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
 
2868   $query           = qq|SELECT $column_spec
 
2870                         LEFT JOIN $table     ct ON (a.${table}_id = ct.id)
 
2871                         LEFT JOIN department d  ON (a.department_id = d.id)
 
2873   my $ref          = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
 
2875   map { $self->{$_} = $ref->{$_} } values %column_map;
 
2877   $main::lxdebug->leave_sub();
 
2881   $main::lxdebug->enter_sub();
 
2883   my ($self, $myconfig, $thisdate, $days) = @_;
 
2885   my $dbh = $self->get_standard_dbh($myconfig);
 
2890     my $dateformat = $myconfig->{dateformat};
 
2891     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2892     $thisdate = $dbh->quote($thisdate);
 
2893     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2895     $query = qq|SELECT current_date AS thisdate|;
 
2898   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2900   $main::lxdebug->leave_sub();
 
2906   $main::lxdebug->enter_sub();
 
2908   my ($self, $string) = @_;
 
2910   if ($string !~ /%/) {
 
2911     $string = "%$string%";
 
2914   $string =~ s/\'/\'\'/g;
 
2916   $main::lxdebug->leave_sub();
 
2922   $main::lxdebug->enter_sub();
 
2924   my ($self, $flds, $new, $count, $numrows) = @_;
 
2928   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
 
2933   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2935     my $j = $item->{ndx} - 1;
 
2936     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2940   for $i ($count + 1 .. $numrows) {
 
2941     map { delete $self->{"${_}_$i"} } @{$flds};
 
2944   $main::lxdebug->leave_sub();
 
2948   $main::lxdebug->enter_sub();
 
2950   my ($self, $myconfig) = @_;
 
2954   my $dbh = $self->dbconnect_noauto($myconfig);
 
2956   my $query = qq|DELETE FROM status
 
2957                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2958   my $sth = prepare_query($self, $dbh, $query);
 
2960   if ($self->{formname} =~ /(check|receipt)/) {
 
2961     for $i (1 .. $self->{rowcount}) {
 
2962       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
2965     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
2969   my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2970   my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2972   my %queued = split / /, $self->{queued};
 
2975   if ($self->{formname} =~ /(check|receipt)/) {
 
2977     # this is a check or receipt, add one entry for each lineitem
 
2978     my ($accno) = split /--/, $self->{account};
 
2979     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
2980                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
2981     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
2982     $sth = prepare_query($self, $dbh, $query);
 
2984     for $i (1 .. $self->{rowcount}) {
 
2985       if ($self->{"checked_$i"}) {
 
2986         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
2992     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2993                 VALUES (?, ?, ?, ?, ?)|;
 
2994     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
2995              $queued{$self->{formname}}, $self->{formname});
 
3001   $main::lxdebug->leave_sub();
 
3005   $main::lxdebug->enter_sub();
 
3007   my ($self, $dbh) = @_;
 
3009   my ($query, $printed, $emailed);
 
3011   my $formnames  = $self->{printed};
 
3012   my $emailforms = $self->{emailed};
 
3014   $query = qq|DELETE FROM status
 
3015                  WHERE (formname = ?) AND (trans_id = ?)|;
 
3016   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
3018   # this only applies to the forms
 
3019   # checks and receipts are posted when printed or queued
 
3021   if ($self->{queued}) {
 
3022     my %queued = split / /, $self->{queued};
 
3024     foreach my $formname (keys %queued) {
 
3025       $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3026       $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3028       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
3029                   VALUES (?, ?, ?, ?, ?)|;
 
3030       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
3032       $formnames  =~ s/\Q$self->{formname}\E//;
 
3033       $emailforms =~ s/\Q$self->{formname}\E//;
 
3038   # save printed, emailed info
 
3039   $formnames  =~ s/^ +//g;
 
3040   $emailforms =~ s/^ +//g;
 
3043   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
3044   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
3046   foreach my $formname (keys %status) {
 
3047     $printed = ($formnames  =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3048     $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3050     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
3051                 VALUES (?, ?, ?, ?)|;
 
3052     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
3055   $main::lxdebug->leave_sub();
 
3059 # $main::locale->text('SAVED')
 
3060 # $main::locale->text('DELETED')
 
3061 # $main::locale->text('ADDED')
 
3062 # $main::locale->text('PAYMENT POSTED')
 
3063 # $main::locale->text('POSTED')
 
3064 # $main::locale->text('POSTED AS NEW')
 
3065 # $main::locale->text('ELSE')
 
3066 # $main::locale->text('SAVED FOR DUNNING')
 
3067 # $main::locale->text('DUNNING STARTED')
 
3068 # $main::locale->text('PRINTED')
 
3069 # $main::locale->text('MAILED')
 
3070 # $main::locale->text('SCREENED')
 
3071 # $main::locale->text('CANCELED')
 
3072 # $main::locale->text('invoice')
 
3073 # $main::locale->text('proforma')
 
3074 # $main::locale->text('sales_order')
 
3075 # $main::locale->text('packing_list')
 
3076 # $main::locale->text('pick_list')
 
3077 # $main::locale->text('purchase_order')
 
3078 # $main::locale->text('bin_list')
 
3079 # $main::locale->text('sales_quotation')
 
3080 # $main::locale->text('request_quotation')
 
3083   $main::lxdebug->enter_sub();
 
3088   if(!exists $self->{employee_id}) {
 
3089     &get_employee($self, $dbh);
 
3093    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
 
3094    qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
 
3095   my @values = (conv_i($self->{id}), $self->{login},
 
3096                 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
 
3097   do_query($self, $dbh, $query, @values);
 
3099   $main::lxdebug->leave_sub();
 
3103   $main::lxdebug->enter_sub();
 
3105   my ($self, $dbh, $trans_id, $restriction, $order) = @_;
 
3106   my ($orderBy, $desc) = split(/\-\-/, $order);
 
3107   $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
 
3110   if ($trans_id ne "") {
 
3112       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 | .
 
3113       qq|FROM history_erp h | .
 
3114       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
3115       qq|WHERE trans_id = | . $trans_id
 
3116       . $restriction . qq| |
 
3119     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
3121     $sth->execute() || $self->dberror("$query");
 
3123     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
3124       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
3125       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
3126       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
 
3127       $tempArray[$i++] = $hash_ref;
 
3129     $main::lxdebug->leave_sub() and return \@tempArray 
 
3130       if ($i > 0 && $tempArray[0] ne "");
 
3132   $main::lxdebug->leave_sub();
 
3136 sub update_defaults {
 
3137   $main::lxdebug->enter_sub();
 
3139   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
3142   if ($provided_dbh) {
 
3143     $dbh = $provided_dbh;
 
3145     $dbh = $self->dbconnect_noauto($myconfig);
 
3147   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
3148   my $sth   = $dbh->prepare($query);
 
3150   $sth->execute || $self->dberror($query);
 
3151   my ($var) = $sth->fetchrow_array;
 
3154   if ($var =~ m/\d+$/) {
 
3155     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
3156     my $len_diff = length($var) - $-[0] - length($new_var);
 
3157     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
3163   $query = qq|UPDATE defaults SET $fld = ?|;
 
3164   do_query($self, $dbh, $query, $var);
 
3166   if (!$provided_dbh) {
 
3171   $main::lxdebug->leave_sub();
 
3176 sub update_business {
 
3177   $main::lxdebug->enter_sub();
 
3179   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
3182   if ($provided_dbh) {
 
3183     $dbh = $provided_dbh;
 
3185     $dbh = $self->dbconnect_noauto($myconfig);
 
3188     qq|SELECT customernumberinit FROM business
 
3189        WHERE id = ? FOR UPDATE|;
 
3190   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
3192   if ($var =~ m/\d+$/) {
 
3193     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
3194     my $len_diff = length($var) - $-[0] - length($new_var);
 
3195     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
3201   $query = qq|UPDATE business
 
3202               SET customernumberinit = ?
 
3204   do_query($self, $dbh, $query, $var, $business_id);
 
3206   if (!$provided_dbh) {
 
3211   $main::lxdebug->leave_sub();
 
3216 sub get_partsgroup {
 
3217   $main::lxdebug->enter_sub();
 
3219   my ($self, $myconfig, $p) = @_;
 
3220   my $target = $p->{target} || 'all_partsgroup';
 
3222   my $dbh = $self->get_standard_dbh($myconfig);
 
3224   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
3226                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
3229   if ($p->{searchitems} eq 'part') {
 
3230     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
3232   if ($p->{searchitems} eq 'service') {
 
3233     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
3235   if ($p->{searchitems} eq 'assembly') {
 
3236     $query .= qq|WHERE p.assembly = '1'|;
 
3238   if ($p->{searchitems} eq 'labor') {
 
3239     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
3242   $query .= qq|ORDER BY partsgroup|;
 
3245     $query = qq|SELECT id, partsgroup FROM partsgroup
 
3246                 ORDER BY partsgroup|;
 
3249   if ($p->{language_code}) {
 
3250     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
3251                   t.description AS translation
 
3253                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
3254                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
3255                 ORDER BY translation|;
 
3256     @values = ($p->{language_code});
 
3259   $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
 
3261   $main::lxdebug->leave_sub();
 
3264 sub get_pricegroup {
 
3265   $main::lxdebug->enter_sub();
 
3267   my ($self, $myconfig, $p) = @_;
 
3269   my $dbh = $self->get_standard_dbh($myconfig);
 
3271   my $query = qq|SELECT p.id, p.pricegroup
 
3274   $query .= qq| ORDER BY pricegroup|;
 
3277     $query = qq|SELECT id, pricegroup FROM pricegroup
 
3278                 ORDER BY pricegroup|;
 
3281   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
3283   $main::lxdebug->leave_sub();
 
3287 # usage $form->all_years($myconfig, [$dbh])
 
3288 # return list of all years where bookings found
 
3291   $main::lxdebug->enter_sub();
 
3293   my ($self, $myconfig, $dbh) = @_;
 
3295   $dbh ||= $self->get_standard_dbh($myconfig);
 
3298   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
3299                    (SELECT MAX(transdate) FROM acc_trans)|;
 
3300   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
3302   if ($myconfig->{dateformat} =~ /^yy/) {
 
3303     ($startdate) = split /\W/, $startdate;
 
3304     ($enddate) = split /\W/, $enddate;
 
3306     (@_) = split /\W/, $startdate;
 
3308     (@_) = split /\W/, $enddate;
 
3313   $startdate = substr($startdate,0,4);
 
3314   $enddate = substr($enddate,0,4);
 
3316   while ($enddate >= $startdate) {
 
3317     push @all_years, $enddate--;
 
3322   $main::lxdebug->leave_sub();
 
3326   $main::lxdebug->enter_sub();
 
3330   map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
 
3332   $main::lxdebug->leave_sub();
 
3336   $main::lxdebug->enter_sub();
 
3341   map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
 
3343   $main::lxdebug->leave_sub();