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 #======================================================================
 
  51   $main::lxdebug->enter_sub(2);
 
  55   my @pairs = split(/&/, $input);
 
  58     my ($name, $value) = split(/=/, $_, 2);
 
  59     $in{$name} = unescape(undef, $value);
 
  62   $main::lxdebug->leave_sub(2);
 
  67 sub _request_to_hash {
 
  68   $main::lxdebug->enter_sub(2);
 
  71   my ($i,        $loc,  $key,    $val);
 
  72   my (%ATTACH,   $f,    $header, $header_body, $len, $buf);
 
  73   my ($boundary, @list, $size,   $body, $x, $blah, $name);
 
  75   if ($ENV{'CONTENT_TYPE'}
 
  76       && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
 
  77     $boundary = quotemeta('--' . $1);
 
  78     @list     = split(/$boundary/, $input);
 
  80     # For some reason there are always 2 extra, that are empty
 
  83     for ($x = 1; $x <= $size; $x++) {
 
  84       $header_body = $list[$x];
 
  85       $header_body =~ /\r\n\r\n|\n\n/;
 
  87       # Here we split the header and body
 
  92       # Now we try to get the file name
 
  94       $name =~ /name=\"(.+)\"/;
 
  95       ($name, $blah) = split(/\"/, $1);
 
  97       # If the form name is not attach, then we need to parse this like
 
  99       if ($name ne "attach") {
 
 100         $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 101         $ATTACH{$name} = $body;
 
 103         # Otherwise it is an attachment and we need to finish it up
 
 104       } elsif ($name eq "attach") {
 
 105         $header =~ /filename=\"(.+)\"/;
 
 106         $ATTACH{'FILE_NAME'} = $1;
 
 107         $ATTACH{'FILE_NAME'} =~ s/\"//g;
 
 108         $ATTACH{'FILE_NAME'} =~ s/\s//g;
 
 109         $ATTACH{'FILE_CONTENT'} = $body;
 
 111         for ($i = $x; $list[$i]; $i++) {
 
 112           $list[$i] =~ s/^.+name=$//;
 
 113           $list[$i] =~ /\"(\w+)\"/;
 
 119     $main::lxdebug->leave_sub(2);
 
 123     $main::lxdebug->leave_sub(2);
 
 124     return _input_to_hash($input);
 
 129   $main::lxdebug->enter_sub();
 
 135   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 137   if ($ENV{QUERY_STRING}) {
 
 138     $_ = $ENV{QUERY_STRING};
 
 145   my %parameters = _request_to_hash($_);
 
 146   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 148   $self->{action} = lc $self->{action};
 
 149   $self->{action} =~ s/( |-|,|\#)/_/g;
 
 151   $self->{version}   = "2.4.2";
 
 153   $main::lxdebug->leave_sub();
 
 159   $main::lxdebug->enter_sub();
 
 165   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 167   $main::lxdebug->leave_sub();
 
 171   $main::lxdebug->enter_sub(2);
 
 173   my ($self, $str) = @_;
 
 175   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 177   $main::lxdebug->leave_sub(2);
 
 183   $main::lxdebug->enter_sub(2);
 
 185   my ($self, $str) = @_;
 
 190   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 192   $main::lxdebug->leave_sub(2);
 
 198   my ($self, $str) = @_;
 
 200   if ($str && !ref($str)) {
 
 201     $str =~ s/\"/"/g;
 
 209   my ($self, $str) = @_;
 
 211   if ($str && !ref($str)) {
 
 212     $str =~ s/"/\"/g;
 
 220   $main::lxdebug->enter_sub(2);
 
 222   my ($self, $str) = @_;
 
 225     ('order' => ['"', '<', '>'],
 
 231   map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 233   $main::lxdebug->leave_sub(2);
 
 242     map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 244     for (sort keys %$self) {
 
 245       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 246       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 253   $main::lxdebug->enter_sub();
 
 255   my ($self, $msg) = @_;
 
 256   if ($ENV{HTTP_USER_AGENT}) {
 
 258     $self->show_generic_error($msg);
 
 262     if ($self->{error_function}) {
 
 263       &{ $self->{error_function} }($msg);
 
 269   $main::lxdebug->leave_sub();
 
 273   $main::lxdebug->enter_sub();
 
 275   my ($self, $msg) = @_;
 
 277   if ($ENV{HTTP_USER_AGENT}) {
 
 280     if (!$self->{header}) {
 
 293     if ($self->{info_function}) {
 
 294       &{ $self->{info_function} }($msg);
 
 300   $main::lxdebug->leave_sub();
 
 304   $main::lxdebug->enter_sub();
 
 306   my ($self, $str, $cols, $maxrows) = @_;
 
 310   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 312   $maxrows = $rows unless defined $maxrows;
 
 314   $main::lxdebug->leave_sub();
 
 316   return ($rows > $maxrows) ? $maxrows : $rows;
 
 320   $main::lxdebug->enter_sub();
 
 322   my ($self, $msg) = @_;
 
 324   $self->error("$msg\n" . $DBI::errstr);
 
 326   $main::lxdebug->leave_sub();
 
 330   $main::lxdebug->enter_sub();
 
 332   my ($self, $name, $msg) = @_;
 
 334   if ($self->{$name} =~ /^\s*$/) {
 
 337   $main::lxdebug->leave_sub();
 
 341   $main::lxdebug->enter_sub();
 
 343   my ($self, $extra_code) = @_;
 
 345   if ($self->{header}) {
 
 346     $main::lxdebug->leave_sub();
 
 350   my ($stylesheet, $favicon, $charset);
 
 352   if ($ENV{HTTP_USER_AGENT}) {
 
 354     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 356         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 360     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 362     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 364         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 368     if ($self->{charset}) {
 
 370         qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
 
 373     if ($self->{landscape}) {
 
 374       $pagelayout = qq|<style type="text/css">
 
 375                         \@page { size:landscape; }
 
 378     if ($self->{fokus}) {
 
 379       $fokus = qq|<script type="text/javascript">
 
 381 function fokus(){document.$self->{fokus}.focus();}
 
 388     if ($self->{jsscript} == 1) {
 
 391         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 392         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 393         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 394         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 401       ? "$self->{title} - $self->{titlebar}"
 
 404     foreach $item (@ { $self->{AJAX} }) {
 
 405       $ajax .= $item->show_javascript();
 
 407     print qq|Content-Type: text/html
 
 411   <title>$self->{titlebar}</title>
 
 419   <meta name="robots" content="noindex,nofollow" />
 
 420   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 421   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 423   <script type="text/javascript" src="js/tabcontent.js">
 
 425   /***********************************************
 
 426   * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 427   * This notice MUST stay intact for legal use
 
 428   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 429   ***********************************************/
 
 440   $main::lxdebug->leave_sub();
 
 443 sub parse_html_template {
 
 444   $main::lxdebug->enter_sub();
 
 446   my ($self, $file, $additional_params) = @_;
 
 449   if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 450     $language = $main::language;
 
 452     $language = $main::myconfig{"countrycode"};
 
 454   $language = "de" unless ($language);
 
 456   if (-f "templates/webpages/${file}_${language}.html") {
 
 457     if ((-f ".developer") &&
 
 458         (-f "templates/webpages/${file}_master.html") &&
 
 459         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 460          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 461       my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 462         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 463       print(qq|<pre>$info</pre>|);
 
 467     $file = "templates/webpages/${file}_${language}.html";
 
 468   } elsif (-f "templates/webpages/${file}.html") {
 
 469     $file = "templates/webpages/${file}.html";
 
 471     my $info = "Web page template '${file}' not found.\n" .
 
 472       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 473     print(qq|<pre>$info</pre>|);
 
 477   my $template = HTML::Template->new("filename" => $file,
 
 478                                      "die_on_bad_params" => 0,
 
 480                                      "case_sensitive" => 1,
 
 481                                      "loop_context_vars" => 1,
 
 484   $additional_params = {} unless ($additional_params);
 
 485   if ($self->{"DEBUG"}) {
 
 486     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 489   if ($additional_params->{"DEBUG"}) {
 
 490     $additional_params->{"DEBUG"} =
 
 491       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 494   if (%main::myconfig) {
 
 495     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 496     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 497     $jsc_dateformat =~ s/d+/\%d/gi;
 
 498     $jsc_dateformat =~ s/m+/\%m/gi;
 
 499     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 500     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 503   $additional_params->{"conf_jscalendar"} = $main::jscalendar;
 
 504   $additional_params->{"conf_lizenzen"} = $main::lizenzen;
 
 505   $additional_params->{"conf_latex_templates"} = $main::latex;
 
 506   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 508   my @additional_param_names = keys(%{$additional_params});
 
 509   foreach my $key ($template->param()) {
 
 510     my $param = $self->{$key};
 
 511     $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
 
 512     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
 
 513     $template->param($key => $param);
 
 516   my $output = $template->output();
 
 518   $main::lxdebug->leave_sub();
 
 523 sub show_generic_error {
 
 524   my ($self, $error, $title, $action) = @_;
 
 527   $add_params->{"title"} = $title if ($title);
 
 528   $self->{"label_error"} = $error;
 
 532     map({ delete($self->{$_}); } qw(action));
 
 533     map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
 
 534             if (!ref($self->{$_})); }
 
 536     $add_params->{"SHOW_BUTTON"} = 1;
 
 537     $add_params->{"BUTTON_LABEL"} = $action;
 
 539   $add_params->{"VARIABLES"} = \@vars;
 
 542   print($self->parse_html_template("generic/error", $add_params));
 
 544   die("Error: $error\n");
 
 547 sub show_generic_information {
 
 548   my ($self, $error, $title) = @_;
 
 551   $add_params->{"title"} = $title if ($title);
 
 552   $self->{"label_information"} = $error;
 
 555   print($self->parse_html_template("generic/information", $add_params));
 
 557   die("Information: $error\n");
 
 560 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 561 # changed it to accept an arbitrary number of triggers - sschoeling
 
 563   $main::lxdebug->enter_sub();
 
 566   my $myconfig = shift;
 
 569   # set dateform for jsscript
 
 572     "dd.mm.yy" => "%d.%m.%Y",
 
 573     "dd-mm-yy" => "%d-%m-%Y",
 
 574     "dd/mm/yy" => "%d/%m/%Y",
 
 575     "mm/dd/yy" => "%m/%d/%Y",
 
 576     "mm-dd-yy" => "%m-%d-%Y",
 
 577     "yyyy-mm-dd" => "%Y-%m-%d",
 
 580   my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
 
 581     $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
 
 588       inputField : "| . (shift) . qq|",
 
 589       ifFormat :"$ifFormat",
 
 590       align : "| .  (shift) . qq|",
 
 591       button : "| . (shift) . qq|"
 
 597        <script type="text/javascript">
 
 598        <!--| . join("", @triggers) . qq|//-->
 
 602   $main::lxdebug->leave_sub();
 
 605 }    #end sub write_trigger
 
 608   $main::lxdebug->enter_sub();
 
 610   my ($self, $msg) = @_;
 
 612   if ($self->{callback}) {
 
 614     ($script, $argv) = split(/\?/, $self->{callback});
 
 615     exec("perl", "$script", $argv);
 
 623   $main::lxdebug->leave_sub();
 
 626 # sort of columns removed - empty sub
 
 628   $main::lxdebug->enter_sub();
 
 630   my ($self, @columns) = @_;
 
 632   $main::lxdebug->leave_sub();
 
 638   $main::lxdebug->enter_sub(2);
 
 640   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 645   my $neg = ($amount =~ s/-//);
 
 647   if (defined($places) && ($places ne '')) {
 
 652       my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 653       $actual_places = length($actual_places);
 
 654       $places = $actual_places > $places ? $actual_places : $places;
 
 657     $amount = $self->round_amount($amount, $places);
 
 660   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 661   my @p = split(/\./, $amount); # split amount at decimal point
 
 663   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 666   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 669     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 670     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 671                         ($neg ? "-$amount"   : "$amount" )    ;
 
 675   $main::lxdebug->leave_sub(2);
 
 680   $main::lxdebug->enter_sub(2);
 
 682   my ($self, $myconfig, $amount) = @_;
 
 684   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 685       || ($myconfig->{numberformat} eq '1000,00')) {
 
 690   if ($myconfig->{numberformat} eq "1'000.00") {
 
 696   $main::lxdebug->leave_sub(2);
 
 698   return ($amount * 1);
 
 702   $main::lxdebug->enter_sub(2);
 
 704   my ($self, $amount, $places) = @_;
 
 707   # Rounding like "Kaufmannsrunden"
 
 708   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 710   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 713   $amount = $amount * (10**($places));
 
 714   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 716   $main::lxdebug->leave_sub(2);
 
 718   return $round_amount;
 
 723   $main::lxdebug->enter_sub();
 
 725   my ($self, $myconfig, $userspath) = @_;
 
 728   $self->{"cwd"} = getcwd();
 
 729   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
 731   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
 732     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 733   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
 734     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
 735     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 736   } elsif (($self->{"format"} =~ /html/i) ||
 
 737            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
 738     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 739   } elsif (($self->{"format"} =~ /xml/i) ||
 
 740              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
 741     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 742   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
 743     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 744   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
 745     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 746   } elsif ( defined $self->{'format'}) {
 
 747     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
 748   } elsif ( $self->{'format'} eq '' ) {
 
 749     $self->error("No Outputformat given: $self->{'format'}");
 
 750   } else { #Catch the rest
 
 751     $self->error("Outputformat not defined: $self->{'format'}");
 
 754   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 755   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 757   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 758       qw(email tel fax name signature company address businessnumber
 
 759          co_ustid taxnumber duns));
 
 760   map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
 
 761       qw(company address signature));
 
 762   map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
 
 764   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 766   # OUT is used for the media, screen, printer, email
 
 767   # for postscript we store a copy in a temporary file
 
 769   $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
 
 770   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 772     $self->{OUT} = ">$self->{tmpfile}";
 
 776     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 778     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 782   if (!$template->parse(*OUT)) {
 
 784     $self->error("$self->{IN} : " . $template->get_error());
 
 789   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 791     if ($self->{media} eq 'email') {
 
 795       my $mail = new Mailer;
 
 797       map { $mail->{$_} = $self->{$_} }
 
 798         qw(cc bcc subject message version format charset);
 
 799       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
 800       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
 801       $mail->{fileid} = "$fileid.";
 
 802       $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
 804       # if we send html or plain text inline
 
 805       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
 806         $mail->{contenttype} = "text/html";
 
 808         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
 809         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
 810         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
 812         open(IN, $self->{tmpfile})
 
 813           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 815           $mail->{message} .= $_;
 
 822         if (!$self->{"do_not_attach"}) {
 
 823           @{ $mail->{attachments} } =
 
 824             ({ "filename" => $self->{"tmpfile"},
 
 825                "name" => $self->{"attachment_filename"} ?
 
 826                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
 829         $mail->{message}       =~ s/\r\n/\n/g;
 
 830         $myconfig->{signature} =~ s/\\n/\n/g;
 
 831         $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 835       my $err = $mail->send($out);
 
 836       $self->error($self->cleanup . "$err") if ($err);
 
 842       my $numbytes = (-s $self->{tmpfile});
 
 843       open(IN, $self->{tmpfile})
 
 844         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 846       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
 848       chdir("$self->{cwd}");
 
 849       #print(STDERR "Kopien $self->{copies}\n");
 
 850       #print(STDERR "OUT $self->{OUT}\n");
 
 851       for my $i (1 .. $self->{copies}) {
 
 853           open(OUT, $self->{OUT})
 
 854             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
 856           $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
 
 858           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
 859 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
 860 Content-Length: $numbytes
 
 864           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
 884   chdir("$self->{cwd}");
 
 885   $main::lxdebug->leave_sub();
 
 889   $main::lxdebug->enter_sub();
 
 893   chdir("$self->{tmpdir}");
 
 896   if (-f "$self->{tmpfile}.err") {
 
 897     open(FH, "$self->{tmpfile}.err");
 
 902   if ($self->{tmpfile}) {
 
 903     $self->{tmpfile} =~ s|.*/||g;
 
 905     $self->{tmpfile} =~ s/\.\w+$//g;
 
 906     my $tmpfile = $self->{tmpfile};
 
 907     unlink(<$tmpfile.*>);
 
 910   chdir("$self->{cwd}");
 
 912   $main::lxdebug->leave_sub();
 
 918   $main::lxdebug->enter_sub();
 
 920   my ($self, $date, $myconfig) = @_;
 
 922   if ($date && $date =~ /\D/) {
 
 924     if ($myconfig->{dateformat} =~ /^yy/) {
 
 925       ($yy, $mm, $dd) = split /\D/, $date;
 
 927     if ($myconfig->{dateformat} =~ /^mm/) {
 
 928       ($mm, $dd, $yy) = split /\D/, $date;
 
 930     if ($myconfig->{dateformat} =~ /^dd/) {
 
 931       ($dd, $mm, $yy) = split /\D/, $date;
 
 936     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
 937     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
 939     $dd = "0$dd" if ($dd < 10);
 
 940     $mm = "0$mm" if ($mm < 10);
 
 945   $main::lxdebug->leave_sub();
 
 950 # Database routines used throughout
 
 953   $main::lxdebug->enter_sub(2);
 
 955   my ($self, $myconfig) = @_;
 
 957   # connect to database
 
 959     DBI->connect($myconfig->{dbconnect},
 
 960                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
 964   if ($myconfig->{dboptions}) {
 
 965     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
 968   $main::lxdebug->leave_sub(2);
 
 973 sub dbconnect_noauto {
 
 974   $main::lxdebug->enter_sub();
 
 976   my ($self, $myconfig) = @_;
 
 978   # connect to database
 
 980     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
 981                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
 985   if ($myconfig->{dboptions}) {
 
 986     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
 989   $main::lxdebug->leave_sub();
 
 995   $main::lxdebug->enter_sub();
 
 997   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
 999   # if we have a value, go do it
 
1002     # retrieve balance from table
 
1003     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1004     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1005     my ($balance) = $sth->fetchrow_array;
 
1011     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1012     do_query($self, $dbh, $query, @values);
 
1014   $main::lxdebug->leave_sub();
 
1017 sub update_exchangerate {
 
1018   $main::lxdebug->enter_sub();
 
1020   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1022   # some sanity check for currency
 
1024     $main::lxdebug->leave_sub();
 
1028   my $query = qq|SELECT e.curr FROM exchangerate e
 
1029                  WHERE e.curr = ? AND e.transdate = ?
 
1031   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1034   if ($buy != 0 && $sell != 0) {
 
1035     $set = "buy = $buy, sell = $sell";
 
1036   } elsif ($buy != 0) {
 
1037     $set = "buy = $buy";
 
1038   } elsif ($sell != 0) {
 
1039     $set = "sell = $sell";
 
1042   if ($sth->fetchrow_array) {
 
1043     $query = qq|UPDATE exchangerate
 
1048     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1049                 VALUES (?, $buy, $sell, ?)|;
 
1052   do_query($self, $dbh, $query, $curr, $transdate);
 
1054   $main::lxdebug->leave_sub();
 
1057 sub save_exchangerate {
 
1058   $main::lxdebug->enter_sub();
 
1060   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1062   my $dbh = $self->dbconnect($myconfig);
 
1064   my ($buy, $sell) = (0, 0);
 
1065   $buy  = $rate if $fld eq 'buy';
 
1066   $sell = $rate if $fld eq 'sell';
 
1068   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1072   $main::lxdebug->leave_sub();
 
1075 sub get_exchangerate {
 
1076   $main::lxdebug->enter_sub();
 
1078   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1080   unless ($transdate) {
 
1081     $main::lxdebug->leave_sub();
 
1085   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1086                  WHERE e.curr = ? AND e.transdate = ?|;
 
1087   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1089   if (!$exchangerate) {
 
1093   $main::lxdebug->leave_sub();
 
1095   return $exchangerate;
 
1098 sub check_exchangerate {
 
1099   $main::lxdebug->enter_sub();
 
1101   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1103   unless ($transdate) {
 
1104     $main::lxdebug->leave_sub();
 
1108   my $dbh = $self->dbconnect($myconfig);
 
1110   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1111                  WHERE e.curr = ? AND e.transdate = ?|;
 
1112   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1115   $main::lxdebug->leave_sub();
 
1117   return $exchangerate;
 
1120 sub set_payment_options {
 
1121   $main::lxdebug->enter_sub();
 
1123   my ($self, $myconfig, $transdate) = @_;
 
1125   if ($self->{payment_id}) {
 
1127     my $dbh = $self->dbconnect($myconfig);
 
1130       qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
 
1131       qq|FROM payment_terms p | .
 
1134     ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1135      $self->{payment_terms}) =
 
1136        selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1138     if ($transdate eq "") {
 
1139       if ($self->{invdate}) {
 
1140         $transdate = $self->{invdate};
 
1142         $transdate = $self->{transdate};
 
1147       qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1148       qq|FROM payment_terms|;
 
1149     ($self->{netto_date}, $self->{skonto_date}) =
 
1150       selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1152     my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
 
1153     my $skonto_amount = $self->parse_amount($myconfig, $total) *
 
1154       $self->{percent_skonto};
 
1156     $self->{skonto_amount} =
 
1157       $self->format_amount($myconfig, $skonto_amount, 2);
 
1159     if ($self->{"language_id"}) {
 
1161         qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1162         qq|FROM translation_payment_terms t | .
 
1163         qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1164         qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1165       my ($description_long, $output_numberformat, $output_dateformat,
 
1166         $output_longdates) =
 
1167         selectrow_query($self, $dbh, $query,
 
1168                         $self->{"language_id"}, $self->{"payment_id"});
 
1170       $self->{payment_terms} = $description_long if ($description_long);
 
1172       if ($output_dateformat) {
 
1173         foreach my $key (qw(netto_date skonto_date)) {
 
1175             $main::locale->reformat_date($myconfig, $self->{$key},
 
1181       if ($output_numberformat &&
 
1182           ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1183         my $saved_numberformat = $myconfig->{"numberformat"};
 
1184         $myconfig->{"numberformat"} = $output_numberformat;
 
1185         $self->{skonto_amount} =
 
1186           $self->format_amount($myconfig, $skonto_amount, 2);
 
1187         $myconfig->{"numberformat"} = $saved_numberformat;
 
1191     $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1192     $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1193     $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
 
1194     $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
 
1195     $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
 
1196     $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1197     $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1198     $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1199     $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1200     $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1205   $main::lxdebug->leave_sub();
 
1209 sub get_template_language {
 
1210   $main::lxdebug->enter_sub();
 
1212   my ($self, $myconfig) = @_;
 
1214   my $template_code = "";
 
1216   if ($self->{language_id}) {
 
1217     my $dbh = $self->dbconnect($myconfig);
 
1218     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1219     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1223   $main::lxdebug->leave_sub();
 
1225   return $template_code;
 
1228 sub get_printer_code {
 
1229   $main::lxdebug->enter_sub();
 
1231   my ($self, $myconfig) = @_;
 
1233   my $template_code = "";
 
1235   if ($self->{printer_id}) {
 
1236     my $dbh = $self->dbconnect($myconfig);
 
1237     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1238     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1242   $main::lxdebug->leave_sub();
 
1244   return $template_code;
 
1248   $main::lxdebug->enter_sub();
 
1250   my ($self, $myconfig) = @_;
 
1252   my $template_code = "";
 
1254   if ($self->{shipto_id}) {
 
1255     my $dbh = $self->dbconnect($myconfig);
 
1256     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1257     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1258     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1262   $main::lxdebug->leave_sub();
 
1266   $main::lxdebug->enter_sub();
 
1268   my ($self, $dbh, $id, $module) = @_;
 
1272   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1273                        contact phone fax email)) {
 
1274     if ($self->{"shipto$item"}) {
 
1275       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1277     push(@values, $self->{"shipto${item}"});
 
1280     if ($self->{shipto_id}) {
 
1281       my $query = qq|UPDATE shipto set
 
1283                        shiptodepartment_1 = ?,
 
1284                        shiptodepartment_2 = ?,
 
1293                      WHERE shipto_id = ?|;
 
1294       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1296       my $query = qq|SELECT * FROM shipto
 
1297                      WHERE shiptoname = ? AND
 
1298                        shiptodepartment_1 = ? AND
 
1299                        shiptodepartment_2 = ? AND
 
1300                        shiptostreet = ? AND
 
1301                        shiptozipcode = ? AND
 
1303                        shiptocountry = ? AND
 
1304                        shiptocontact = ? AND
 
1308       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
 
1311           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1312                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1313                                  shiptocontact, shiptophone, shiptofax, shiptoemail, module)
 
1314              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1315         do_query($self, $dbh, $query, $id, @values, $module);
 
1320   $main::lxdebug->leave_sub();
 
1324   $main::lxdebug->enter_sub();
 
1326   my ($self, $dbh) = @_;
 
1328   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1329   ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1330   $self->{employee_id} *= 1;
 
1332   $main::lxdebug->leave_sub();
 
1336   $main::lxdebug->enter_sub();
 
1338   my ($self, $myconfig, $salesman_id) = @_;
 
1340   $main::lxdebug->leave_sub() and return unless $salesman_id;
 
1342   my $dbh = $self->dbconnect($myconfig);
 
1345     selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
 
1349     my $user = new User($main::memberfile, $login);
 
1350     map({ $self->{"salesman_$_"} = $user->{$_}; }
 
1351         qw(address businessnumber co_ustid company duns email fax name
 
1353     $self->{salesman_login} = $login;
 
1355     $self->{salesman_name} = $login
 
1356       if ($self->{salesman_name} eq "");
 
1358     map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
 
1363   $main::lxdebug->leave_sub();
 
1367   $main::lxdebug->enter_sub();
 
1369   my ($self, $myconfig) = @_;
 
1371   my $dbh = $self->dbconnect($myconfig);
 
1372   my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
 
1373   ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1376   $main::lxdebug->leave_sub();
 
1380   $main::lxdebug->enter_sub();
 
1382   my ($self, $dbh, $id, $key) = @_;
 
1384   $key = "all_contacts" unless ($key);
 
1387     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1388     qq|FROM contacts | .
 
1389     qq|WHERE cp_cv_id = ? | .
 
1390     qq|ORDER BY lower(cp_name)|;
 
1392   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1394   $main::lxdebug->leave_sub();
 
1398   $main::lxdebug->enter_sub();
 
1400   my ($self, $dbh, $key) = @_;
 
1402   my ($all, $old_id, $where, @values);
 
1404   if (ref($key) eq "HASH") {
 
1407     $key = "ALL_PROJECTS";
 
1409     foreach my $p (keys(%{$params})) {
 
1411         $all = $params->{$p};
 
1412       } elsif ($p eq "old_id") {
 
1413         $old_id = $params->{$p};
 
1414       } elsif ($p eq "key") {
 
1415         $key = $params->{$p};
 
1421     $where = "WHERE active ";
 
1423       if (ref($old_id) eq "ARRAY") {
 
1424         my @ids = grep({ $_ } @{$old_id});
 
1426           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1427           push(@values, @ids);
 
1430         $where .= " OR (id = ?) ";
 
1431         push(@values, $old_id);
 
1437     qq|SELECT id, projectnumber, description, active | .
 
1440     qq|ORDER BY lower(projectnumber)|;
 
1442   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
1444   $main::lxdebug->leave_sub();
 
1448   $main::lxdebug->enter_sub();
 
1450   my ($self, $dbh, $vc_id, $key) = @_;
 
1452   $key = "all_shipto" unless ($key);
 
1454   # get shipping addresses
 
1456     qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
 
1458     qq|WHERE trans_id = ?|;
 
1460   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
1462   $main::lxdebug->leave_sub();
 
1466   $main::lxdebug->enter_sub();
 
1468   my ($self, $dbh, $key) = @_;
 
1470   $key = "all_printers" unless ($key);
 
1472   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
1474   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1476   $main::lxdebug->leave_sub();
 
1480   $main::lxdebug->enter_sub();
 
1482   my ($self, $dbh, $params) = @_;
 
1484   $key = $params->{key};
 
1485   $key = "all_charts" unless ($key);
 
1487   my $transdate = quote_db_date($params->{transdate});
 
1490     qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
1492     qq|LEFT JOIN taxkeys tk ON | .
 
1493     qq|(tk.id = (SELECT id FROM taxkeys | .
 
1494     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
1495     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
1496     qq|ORDER BY c.accno|;
 
1498   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1500   $main::lxdebug->leave_sub();
 
1503 sub _get_taxcharts {
 
1504   $main::lxdebug->enter_sub();
 
1506   my ($self, $dbh, $key) = @_;
 
1508   $key = "all_taxcharts" unless ($key);
 
1510   my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
1512   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1514   $main::lxdebug->leave_sub();
 
1517 sub _get_employees {
 
1518   $main::lxdebug->enter_sub();
 
1520   my ($self, $dbh, $key) = @_;
 
1522   $key = "all_employees" unless ($key);
 
1524     selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
 
1526   $main::lxdebug->leave_sub();
 
1529 sub _get_business_types {
 
1530   $main::lxdebug->enter_sub();
 
1532   my ($self, $dbh, $key) = @_;
 
1534   $key = "all_business_types" unless ($key);
 
1536     selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
 
1538   $main::lxdebug->leave_sub();
 
1541 sub _get_languages {
 
1542   $main::lxdebug->enter_sub();
 
1544   my ($self, $dbh, $key) = @_;
 
1546   $key = "all_languages" unless ($key);
 
1548   my $query = qq|SELECT * FROM language ORDER BY id|;
 
1550   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1552   $main::lxdebug->leave_sub();
 
1556   $main::lxdebug->enter_sub();
 
1561   my $dbh = $self->dbconnect(\%main::myconfig);
 
1562   my ($sth, $query, $ref);
 
1564   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
1565   my $vc_id = $self->{"${vc}_id"};
 
1567   if ($params{"contacts"}) {
 
1568     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
1571   if ($params{"shipto"}) {
 
1572     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
1575   if ($params{"projects"} || $params{"all_projects"}) {
 
1576     $self->_get_projects($dbh, $params{"all_projects"} ?
 
1577                          $params{"all_projects"} : $params{"projects"},
 
1578                          $params{"all_projects"} ? 1 : 0);
 
1581   if ($params{"printers"}) {
 
1582     $self->_get_printers($dbh, $params{"printers"});
 
1585   if ($params{"languages"}) {
 
1586     $self->_get_languages($dbh, $params{"languages"});
 
1589   if ($params{"charts"}) {
 
1590     $self->_get_charts($dbh, $params{"charts"});
 
1593   if ($params{"taxcharts"}) {
 
1594     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
1597   if ($params{"employees"}) {
 
1598     $self->_get_employees($dbh, $params{"employees"});
 
1601   if ($params{"business_types"}) {
 
1602     $self->_get_business_types($dbh, $params{"business_types"});
 
1607   $main::lxdebug->leave_sub();
 
1610 # this sub gets the id and name from $table
 
1612   $main::lxdebug->enter_sub();
 
1614   my ($self, $myconfig, $table) = @_;
 
1616   # connect to database
 
1617   my $dbh = $self->dbconnect($myconfig);
 
1619   $table = $table eq "customer" ? "customer" : "vendor";
 
1620   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
1622   my ($query, @values);
 
1624   if (!$self->{openinvoices}) {
 
1626     if ($self->{customernumber} ne "") {
 
1627       $where = qq|(vc.customernumber ILIKE ?)|;
 
1628       push(@values, '%' . $self->{customernumber} . '%');
 
1630       $where = qq|(vc.name ILIKE ?)|;
 
1631       push(@values, '%' . $self->{$table} . '%');
 
1635       qq~SELECT vc.id, vc.name,
 
1636            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1638          WHERE $where AND (NOT vc.obsolete)
 
1642       qq~SELECT DISTINCT vc.id, vc.name,
 
1643            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1645          JOIN $table vc ON (a.${table}_id = vc.id)
 
1646          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
1648     push(@values, '%' . $self->{$table} . '%');
 
1651   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
1653   $main::lxdebug->leave_sub();
 
1655   return scalar(@{ $self->{name_list} });
 
1658 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1661   $main::lxdebug->enter_sub();
 
1663   my ($self, $myconfig, $table, $module) = @_;
 
1666   my $dbh = $self->dbconnect($myconfig);
 
1668   $table = $table eq "customer" ? "customer" : "vendor";
 
1670   my $query = qq|SELECT count(*) FROM $table|;
 
1671   my ($count) = selectrow_query($self, $dbh, $query);
 
1673   # build selection list
 
1674   if ($count < $myconfig->{vclimit}) {
 
1675     $query = qq|SELECT id, name, salesman_id
 
1676                 FROM $table WHERE NOT obsolete
 
1678     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
1682   $self->get_employee($dbh);
 
1684   # setup sales contacts
 
1685   $query = qq|SELECT e.id, e.name
 
1687               WHERE (e.sales = '1') AND (NOT e.id = ?)|;
 
1688   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
1691   push(@{ $self->{all_employees} },
 
1692        { id   => $self->{employee_id},
 
1693          name => $self->{employee} });
 
1695   # sort the whole thing
 
1696   @{ $self->{all_employees} } =
 
1697     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
1699   if ($module eq 'AR') {
 
1701     # prepare query for departments
 
1702     $query = qq|SELECT id, description
 
1705                 ORDER BY description|;
 
1708     $query = qq|SELECT id, description
 
1710                 ORDER BY description|;
 
1713   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
1716   $query = qq|SELECT id, description
 
1720   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
1723   $query = qq|SELECT printer_description, id
 
1725               ORDER BY printer_description|;
 
1727   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
1730   $query = qq|SELECT id, description
 
1734   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
1738   $main::lxdebug->leave_sub();
 
1741 sub language_payment {
 
1742   $main::lxdebug->enter_sub();
 
1744   my ($self, $myconfig) = @_;
 
1746   my $dbh = $self->dbconnect($myconfig);
 
1748   my $query = qq|SELECT id, description
 
1752   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
1755   $query = qq|SELECT printer_description, id
 
1757               ORDER BY printer_description|;
 
1759   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
1762   $query = qq|SELECT id, description
 
1766   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
1768   # get buchungsgruppen
 
1769   $query = qq|SELECT id, description
 
1770               FROM buchungsgruppen|;
 
1772   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
1775   $main::lxdebug->leave_sub();
 
1778 # this is only used for reports
 
1779 sub all_departments {
 
1780   $main::lxdebug->enter_sub();
 
1782   my ($self, $myconfig, $table) = @_;
 
1784   my $dbh = $self->dbconnect($myconfig);
 
1787   if ($table eq 'customer') {
 
1788     $where = "WHERE role = 'P' ";
 
1791   my $query = qq|SELECT id, description
 
1794                  ORDER BY description|;
 
1795   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
1797   delete($self->{all_departments}) unless (@{ $self->{all_departments} });
 
1801   $main::lxdebug->leave_sub();
 
1805   $main::lxdebug->enter_sub();
 
1807   my ($self, $module, $myconfig, $table) = @_;
 
1810   if ($table eq "customer") {
 
1819   $self->all_vc($myconfig, $table, $module);
 
1821   # get last customers or vendors
 
1822   my ($query, $sth, $ref);
 
1824   my $dbh = $self->dbconnect($myconfig);
 
1829     my $transdate = "current_date";
 
1830     if ($self->{transdate}) {
 
1831       $transdate = $dbh->quote($self->{transdate});
 
1834     # now get the account numbers
 
1835     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
1836                 FROM chart c, taxkeys tk
 
1837                 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
1838                   (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
1841     $sth = $dbh->prepare($query);
 
1843     do_statement($self, $sth, $query, '%' . $module . '%');
 
1845     $self->{accounts} = "";
 
1846     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1848       foreach my $key (split(/:/, $ref->{link})) {
 
1849         if ($key =~ /$module/) {
 
1851           # cross reference for keys
 
1852           $xkeyref{ $ref->{accno} } = $key;
 
1854           push @{ $self->{"${module}_links"}{$key} },
 
1855             { accno       => $ref->{accno},
 
1856               description => $ref->{description},
 
1857               taxkey      => $ref->{taxkey_id},
 
1858               tax_id      => $ref->{tax_id} };
 
1860           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
1866   # get taxkeys and description
 
1867   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
1868   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
1871   $query = qq|SELECT id, description FROM tax_zones|;
 
1872   $self->{TAXZONE} = selectall_hashref_query($self, $dbh, $query);
 
1874   if (($module eq "AP") || ($module eq "AR")) {
 
1875     # get tax rates and description
 
1876     $query = qq|SELECT * FROM tax|;
 
1877     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
1883            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
1884            a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
 
1885            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
1886            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
1888            d.description AS department,
 
1891          JOIN $table c ON (a.${table}_id = c.id)
 
1892          LEFT JOIN employee e ON (e.id = a.employee_id)
 
1893          LEFT JOIN department d ON (d.id = a.department_id)
 
1895     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
1897     foreach $key (keys %$ref) {
 
1898       $self->{$key} = $ref->{$key};
 
1901     my $transdate = "current_date";
 
1902     if ($self->{transdate}) {
 
1903       $transdate = $dbh->quote($self->{transdate});
 
1906     # now get the account numbers
 
1907      $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
1908                  FROM chart c, taxkeys tk
 
1910                    AND (    tk.chart_id = c.id OR     c.link LIKE '%_tax%') 
 
1911                    AND (NOT tk.chart_id = c.id OR NOT c.link LIKE '%_tax%')
 
1912                    AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
1913                      OR c.link LIKE '%_tax%')
 
1916     $sth = $dbh->prepare($query);
 
1917     do_statement($self, $sth, $query, "%$module%");
 
1919     $self->{accounts} = "";
 
1920     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1922       foreach my $key (split(/:/, $ref->{link})) {
 
1923         if ($key =~ /$module/) {
 
1925           # cross reference for keys
 
1926           $xkeyref{ $ref->{accno} } = $key;
 
1928           push @{ $self->{"${module}_links"}{$key} },
 
1929             { accno       => $ref->{accno},
 
1930               description => $ref->{description},
 
1931               taxkey      => $ref->{taxkey_id},
 
1932               tax_id      => $ref->{tax_id} };
 
1934           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
1940     # get amounts from individual entries
 
1943            c.accno, c.description,
 
1944            a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
 
1948          LEFT JOIN chart c ON (c.id = a.chart_id)
 
1949          LEFT JOIN project p ON (p.id = a.project_id)
 
1950          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
1951                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
1952                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
1953                                         THEN tk.chart_id = a.chart_id
 
1956                                        OR (c.link='%tax%')) AND
 
1957                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
1958          WHERE a.trans_id = ?
 
1959          AND a.fx_transaction = '0'
 
1960          ORDER BY a.oid, a.transdate|;
 
1961     $sth = $dbh->prepare($query);
 
1962     do_statement($self, $sth, $query, $self->{id});
 
1964     # get exchangerate for currency
 
1965     $self->{exchangerate} =
 
1966       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
1969     # store amounts in {acc_trans}{$key} for multiple accounts
 
1970     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1971       $ref->{exchangerate} =
 
1972         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
1973       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
1976       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
1977         $ref->{amount} *= -1;
 
1979       $ref->{index} = $index;
 
1981       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
1987            d.curr AS currencies, d.closedto, d.revtrans,
 
1988            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1989            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
1991     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
1992     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
1999             current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
 
2000             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2001             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2003     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2004     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2006     if ($self->{"$self->{vc}_id"}) {
 
2008       # only setup currency
 
2009       ($self->{currency}) = split(/:/, $self->{currencies});
 
2013       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2015       # get exchangerate for currency
 
2016       $self->{exchangerate} =
 
2017         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2025   $main::lxdebug->leave_sub();
 
2029   $main::lxdebug->enter_sub();
 
2031   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2033   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
2034   $table = $table eq "customer" ? "customer" : "vendor";
 
2035   my $where = "1 = 1";
 
2037   if ($self->{type} =~ /_order/) {
 
2039     $where = "quotation = '0'";
 
2041   if ($self->{type} =~ /_quotation/) {
 
2043     $where = "quotation = '1'";
 
2046   my $query = qq|SELECT MAX(id) FROM $arap
 
2047                  WHERE $where AND ${table}_id > 0|;
 
2048   my ($trans_id) = selectrow_query($self, $dbh, $query);
 
2053          a.curr, a.${table}_id, a.department_id,
 
2054          d.description AS department,
 
2055          ct.name, current_date + ct.terms AS duedate
 
2057        LEFT JOIN $table ct ON (a.${table}_id = ct.id)
 
2058        LEFT JOIN department d ON (a.department_id = d.id)
 
2060   ($self->{currency},   $self->{"${table}_id"}, $self->{department_id},
 
2061    $self->{department}, $self->{$table},        $self->{duedate})
 
2062     = selectrow_query($self, $dbh, $query, $trans_id);
 
2064   $main::lxdebug->leave_sub();
 
2068   $main::lxdebug->enter_sub();
 
2070   my ($self, $myconfig, $thisdate, $days) = @_;
 
2072   my $dbh = $self->dbconnect($myconfig);
 
2077     my $dateformat = $myconfig->{dateformat};
 
2078     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2079     $thisdate = $dbh->quote($thisdate);
 
2080     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2082     $query = qq|SELECT current_date AS thisdate|;
 
2085   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2089   $main::lxdebug->leave_sub();
 
2095   $main::lxdebug->enter_sub();
 
2097   my ($self, $string) = @_;
 
2099   if ($string !~ /%/) {
 
2100     $string = "%$string%";
 
2103   $string =~ s/\'/\'\'/g;
 
2105   $main::lxdebug->leave_sub();
 
2111   $main::lxdebug->enter_sub();
 
2113   my ($self, $flds, $new, $count, $numrows) = @_;
 
2117   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
2123   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2125     $j = $item->{ndx} - 1;
 
2126     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2130   for $i ($count + 1 .. $numrows) {
 
2131     map { delete $self->{"${_}_$i"} } @{$flds};
 
2134   $main::lxdebug->leave_sub();
 
2138   $main::lxdebug->enter_sub();
 
2140   my ($self, $myconfig) = @_;
 
2144   my $dbh = $self->dbconnect_noauto($myconfig);
 
2146   my $query = qq|DELETE FROM status
 
2147                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2148   my $sth = prepare_query($self, $dbh, $query);
 
2150   if ($self->{formname} =~ /(check|receipt)/) {
 
2151     for $i (1 .. $self->{rowcount}) {
 
2152       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
2155     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
2159   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2160   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2162   my %queued = split / /, $self->{queued};
 
2165   if ($self->{formname} =~ /(check|receipt)/) {
 
2167     # this is a check or receipt, add one entry for each lineitem
 
2168     my ($accno) = split /--/, $self->{account};
 
2169     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
2170                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
2171     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
2172     $sth = prepare_query($self, $dbh, $query);
 
2174     for $i (1 .. $self->{rowcount}) {
 
2175       if ($self->{"checked_$i"}) {
 
2176         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
2182     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2183                 VALUES (?, ?, ?, ?, ?)|;
 
2184     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
2185              $queued{$self->{formname}}, $self->{formname});
 
2191   $main::lxdebug->leave_sub();
 
2195   $main::lxdebug->enter_sub();
 
2197   my ($self, $dbh) = @_;
 
2199   my ($query, $printed, $emailed);
 
2201   my $formnames  = $self->{printed};
 
2202   my $emailforms = $self->{emailed};
 
2204   my $query = qq|DELETE FROM status
 
2205                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2206   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
2208   # this only applies to the forms
 
2209   # checks and receipts are posted when printed or queued
 
2211   if ($self->{queued}) {
 
2212     my %queued = split / /, $self->{queued};
 
2214     foreach my $formname (keys %queued) {
 
2215       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2216       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2218       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2219                   VALUES (?, ?, ?, ?, ?)|;
 
2220       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
2222       $formnames  =~ s/$self->{formname}//;
 
2223       $emailforms =~ s/$self->{formname}//;
 
2228   # save printed, emailed info
 
2229   $formnames  =~ s/^ +//g;
 
2230   $emailforms =~ s/^ +//g;
 
2233   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2234   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2236   foreach my $formname (keys %status) {
 
2237     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2238     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2240     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2241                 VALUES (?, ?, ?, ?)|;
 
2242     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
2245   $main::lxdebug->leave_sub();
 
2249 # $main::locale->text('SAVED')
 
2250 # $main::locale->text('DELETED')
 
2251 # $main::locale->text('ADDED')
 
2252 # $main::locale->text('PAYMENT POSTED')
 
2253 # $main::locale->text('POSTED')
 
2254 # $main::locale->text('POSTED AS NEW')
 
2255 # $main::locale->text('ELSE')
 
2256 # $main::locale->text('SAVED FOR DUNNING')
 
2257 # $main::locale->text('DUNNING STARTED')
 
2258 # $main::locale->text('PRINTED')
 
2259 # $main::locale->text('MAILED')
 
2260 # $main::locale->text('SCREENED')
 
2261 # $main::locale->text('invoice')
 
2262 # $main::locale->text('proforma')
 
2263 # $main::locale->text('sales_order')
 
2264 # $main::locale->text('packing_list')
 
2265 # $main::locale->text('pick_list')
 
2266 # $main::locale->text('purchase_order')
 
2267 # $main::locale->text('bin_list')
 
2268 # $main::locale->text('sales_quotation')
 
2269 # $main::locale->text('request_quotation')
 
2272   $main::lxdebug->enter_sub();
 
2277   if(!exists $self->{employee_id}) {
 
2278     &get_employee($self, $dbh);
 
2282     qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done) | .
 
2283     qq|VALUES (?, ?, ?, ?)|;
 
2284   my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
 
2285                 $self->{addition}, $self->{what_done});
 
2286   do_query($self, $dbh, $query, @values);
 
2288   $main::lxdebug->leave_sub();
 
2292   $main::lxdebug->enter_sub();
 
2296   my $trans_id = shift();
 
2297   my $restriction = shift();
 
2300   if ($trans_id ne "") {
 
2302       qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name | .
 
2303       qq|FROM history_erp h | .
 
2304       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
2305       qq|WHERE trans_id = ? |
 
2308     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2310     $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
 
2312     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
2313       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
2314       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
2315       $tempArray[$i++] = $hash_ref;
 
2317     $main::lxdebug->leave_sub() and return \@tempArray
 
2318       if ($i > 0 && $tempArray[0] ne "");
 
2320   $main::lxdebug->leave_sub();
 
2324 sub update_defaults {
 
2325   $main::lxdebug->enter_sub();
 
2327   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
2330   if ($provided_dbh) {
 
2331     $dbh = $provided_dbh;
 
2333     $dbh = $self->dbconnect_noauto($myconfig);
 
2335   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2336   my $sth   = $dbh->prepare($query);
 
2338   $sth->execute || $self->dberror($query);
 
2339   my ($var) = $sth->fetchrow_array;
 
2342   if ($var =~ /^(.*?)(\d+)$/) {
 
2343     $var = "$1" . ($2 + 1);
 
2348   $query = qq|UPDATE defaults SET $fld = ?|;
 
2349   do_query($self, $dbh, $query, $var);
 
2351   if (!$provided_dbh) {
 
2356   $main::lxdebug->leave_sub();
 
2361 sub update_business {
 
2362   $main::lxdebug->enter_sub();
 
2364   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
2367   if ($provided_dbh) {
 
2368     $dbh = $provided_dbh;
 
2370     $dbh = $self->dbconnect_noauto($myconfig);
 
2373     qq|SELECT customernumberinit FROM business
 
2374        WHERE id = ? FOR UPDATE|;
 
2375   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
2378     if ($var =~ /^(.*?)(\d+)$/) {
 
2379       $var = "$1" . ($2 + 1);
 
2384   $query = qq|UPDATE business
 
2385               SET customernumberinit = ?
 
2387   do_query($self, $dbh, $query, $var, $business_id);
 
2389   if (!$provided_dbh) {
 
2394   $main::lxdebug->leave_sub();
 
2399 sub get_partsgroup {
 
2400   $main::lxdebug->enter_sub();
 
2402   my ($self, $myconfig, $p) = @_;
 
2404   my $dbh = $self->dbconnect($myconfig);
 
2406   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2408                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
2411   if ($p->{searchitems} eq 'part') {
 
2412     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
2414   if ($p->{searchitems} eq 'service') {
 
2415     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
2417   if ($p->{searchitems} eq 'assembly') {
 
2418     $query .= qq|WHERE p.assembly = '1'|;
 
2420   if ($p->{searchitems} eq 'labor') {
 
2421     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
2424   $query .= qq|ORDER BY partsgroup|;
 
2427     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2428                 ORDER BY partsgroup|;
 
2431   if ($p->{language_code}) {
 
2432     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2433                   t.description AS translation
 
2435                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2436                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
2437                 ORDER BY translation|;
 
2438     @values = ($p->{language_code});
 
2441   $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
 
2444   $main::lxdebug->leave_sub();
 
2447 sub get_pricegroup {
 
2448   $main::lxdebug->enter_sub();
 
2450   my ($self, $myconfig, $p) = @_;
 
2452   my $dbh = $self->dbconnect($myconfig);
 
2454   my $query = qq|SELECT p.id, p.pricegroup
 
2457   $query .= qq| ORDER BY pricegroup|;
 
2460     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2461                 ORDER BY pricegroup|;
 
2464   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
2468   $main::lxdebug->leave_sub();
 
2472 # usage $form->all_years($myconfig, [$dbh])
 
2473 # return list of all years where bookings found
 
2476   $main::lxdebug->enter_sub();
 
2478   my ($self, $myconfig, $dbh) = @_;
 
2482     $dbh = $self->dbconnect($myconfig);
 
2487   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
2488                    (SELECT MAX(transdate) FROM acc_trans)|;
 
2489   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
2491   if ($myconfig->{dateformat} =~ /^yy/) {
 
2492     ($startdate) = split /\W/, $startdate;
 
2493     ($enddate) = split /\W/, $enddate;
 
2495     (@_) = split /\W/, $startdate;
 
2497     (@_) = split /\W/, $enddate;
 
2502   $startdate = substr($startdate,0,4);
 
2503   $enddate = substr($enddate,0,4);
 
2505   while ($enddate >= $startdate) {
 
2506     push @all_years, $enddate--;
 
2509   $dbh->disconnect if $disconnect;
 
2513   $main::lxdebug->leave_sub();