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 #======================================================================
 
  50   $main::lxdebug->enter_sub(2);
 
  54   my @pairs = split(/&/, $input);
 
  57     my ($name, $value) = split(/=/, $_, 2);
 
  58     $in{$name} = unescape(undef, $value);
 
  61   $main::lxdebug->leave_sub(2);
 
  66 sub _request_to_hash {
 
  67   $main::lxdebug->enter_sub(2);
 
  70   my ($i,        $loc,  $key,    $val);
 
  71   my (%ATTACH,   $f,    $header, $header_body, $len, $buf);
 
  72   my ($boundary, @list, $size,   $body, $x, $blah, $name);
 
  74   if ($ENV{'CONTENT_TYPE'}
 
  75       && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
 
  76     $boundary = quotemeta('--' . $1);
 
  77     @list     = split(/$boundary/, $input);
 
  79     # For some reason there are always 2 extra, that are empty
 
  82     for ($x = 1; $x <= $size; $x++) {
 
  83       $header_body = $list[$x];
 
  84       $header_body =~ /\r\n\r\n|\n\n/;
 
  86       # Here we split the header and body
 
  91       # Now we try to get the file name
 
  93       $name =~ /name=\"(.+)\"/;
 
  94       ($name, $blah) = split(/\"/, $1);
 
  96       # If the form name is not attach, then we need to parse this like
 
  98       if ($name ne "attach") {
 
  99         $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 100         $ATTACH{$name} = $body;
 
 102         # Otherwise it is an attachment and we need to finish it up
 
 103       } elsif ($name eq "attach") {
 
 104         $header =~ /filename=\"(.+)\"/;
 
 105         $ATTACH{'FILE_NAME'} = $1;
 
 106         $ATTACH{'FILE_NAME'} =~ s/\"//g;
 
 107         $ATTACH{'FILE_NAME'} =~ s/\s//g;
 
 108         $ATTACH{'FILE_CONTENT'} = $body;
 
 110         for ($i = $x; $list[$i]; $i++) {
 
 111           $list[$i] =~ s/^.+name=$//;
 
 112           $list[$i] =~ /\"(\w+)\"/;
 
 118     $main::lxdebug->leave_sub(2);
 
 122     $main::lxdebug->leave_sub(2);
 
 123     return _input_to_hash($input);
 
 128   $main::lxdebug->enter_sub();
 
 134   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 136   if ($ENV{QUERY_STRING}) {
 
 137     $_ = $ENV{QUERY_STRING};
 
 144   my %parameters = _request_to_hash($_);
 
 145   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 147   $self->{action} = lc $self->{action};
 
 148   $self->{action} =~ s/( |-|,|\#)/_/g;
 
 150   $self->{version}   = "2.4.2";
 
 152   $main::lxdebug->leave_sub();
 
 158   $main::lxdebug->enter_sub();
 
 164   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 166   $main::lxdebug->leave_sub();
 
 170   $main::lxdebug->enter_sub(2);
 
 172   my ($self, $str, $beenthere) = @_;
 
 174   # for Apache 2 we escape strings twice
 
 175   #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
 
 176   #  $str = $self->escape($str, 1);
 
 179   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 181   $main::lxdebug->leave_sub(2);
 
 187   $main::lxdebug->enter_sub(2);
 
 189   my ($self, $str) = @_;
 
 194   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 196   $main::lxdebug->leave_sub(2);
 
 202   my ($self, $str) = @_;
 
 204   if ($str && !ref($str)) {
 
 205     $str =~ s/\"/"/g;
 
 213   my ($self, $str) = @_;
 
 215   if ($str && !ref($str)) {
 
 216     $str =~ s/"/\"/g;
 
 224   $main::lxdebug->enter_sub(2);
 
 226   my ($self, $str) = @_;
 
 229     ('order' => ['"', '<', '>'],
 
 235   map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 237   $main::lxdebug->leave_sub(2);
 
 247       print qq|<input type=hidden name="$_" value="|
 
 248         . $self->quote($self->{$_})
 
 252     delete $self->{header};
 
 253     for (sort keys %$self) {
 
 254       print qq|<input type=hidden name="$_" value="|
 
 255         . $self->quote($self->{$_})
 
 263   $main::lxdebug->enter_sub();
 
 265   my ($self, $msg) = @_;
 
 266   if ($ENV{HTTP_USER_AGENT}) {
 
 268     $self->show_generic_error($msg);
 
 272     if ($self->{error_function}) {
 
 273       &{ $self->{error_function} }($msg);
 
 279   $main::lxdebug->leave_sub();
 
 283   $main::lxdebug->enter_sub();
 
 285   my ($self, $msg) = @_;
 
 287   if ($ENV{HTTP_USER_AGENT}) {
 
 290     if (!$self->{header}) {
 
 303     if ($self->{info_function}) {
 
 304       &{ $self->{info_function} }($msg);
 
 310   $main::lxdebug->leave_sub();
 
 314   $main::lxdebug->enter_sub();
 
 316   my ($self, $str, $cols, $maxrows) = @_;
 
 320   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 322   $maxrows = $rows unless defined $maxrows;
 
 324   $main::lxdebug->leave_sub();
 
 326   return ($rows > $maxrows) ? $maxrows : $rows;
 
 330   $main::lxdebug->enter_sub();
 
 332   my ($self, $msg) = @_;
 
 334   $self->error("$msg\n" . $DBI::errstr);
 
 336   $main::lxdebug->leave_sub();
 
 340   $main::lxdebug->enter_sub();
 
 342   my ($self, $name, $msg) = @_;
 
 344   if ($self->{$name} =~ /^\s*$/) {
 
 347   $main::lxdebug->leave_sub();
 
 351   $main::lxdebug->enter_sub();
 
 353   my ($self, $extra_code) = @_;
 
 355   if ($self->{header}) {
 
 356     $main::lxdebug->leave_sub();
 
 360   my ($stylesheet, $favicon, $charset);
 
 362   if ($ENV{HTTP_USER_AGENT}) {
 
 364     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 366         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 370     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 372     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 374         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 378     if ($self->{charset}) {
 
 380         qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
 
 383     if ($self->{landscape}) {
 
 384       $pagelayout = qq|<style type="text/css">
 
 385                         \@page { size:landscape; }
 
 388     if ($self->{fokus}) {
 
 389       $fokus = qq|<script type="text/javascript">
 
 391 function fokus(){document.$self->{fokus}.focus();}
 
 398     if ($self->{jsscript} == 1) {
 
 401         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 402         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 403         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 404         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 411       ? "$self->{title} - $self->{titlebar}"
 
 414     foreach $item (@ { $self->{AJAX} }) {
 
 415       $ajax .= $item->show_javascript();
 
 417     print qq|Content-Type: text/html
 
 421   <title>$self->{titlebar}</title>
 
 429   <meta name="robots" content="noindex,nofollow" />
 
 430   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 431   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 433   <script type="text/javascript" src="js/tabcontent.js">
 
 435   /***********************************************
 
 436   * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 437   * This notice MUST stay intact for legal use
 
 438   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 439   ***********************************************/
 
 450   $main::lxdebug->leave_sub();
 
 453 sub parse_html_template {
 
 454   $main::lxdebug->enter_sub();
 
 456   my ($self, $file, $additional_params) = @_;
 
 459   if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 460     $language = $main::language;
 
 462     $language = $main::myconfig{"countrycode"};
 
 465   if (-f "templates/webpages/${file}_${language}.html") {
 
 466     if ((-f ".developer") &&
 
 467         (-f "templates/webpages/${file}_master.html") &&
 
 468         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 469          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 470       my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 471         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 472       print(qq|<pre>$info</pre>|);
 
 476     $file = "templates/webpages/${file}_${language}.html";
 
 477   } elsif (-f "templates/webpages/${file}.html") {
 
 478     $file = "templates/webpages/${file}.html";
 
 480     my $info = "Web page template '${file}' not found.\n" .
 
 481       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 482     print(qq|<pre>$info</pre>|);
 
 486   my $template = HTML::Template->new("filename" => $file,
 
 487                                      "die_on_bad_params" => 0,
 
 489                                      "case_sensitive" => 1,
 
 490                                      "loop_context_vars" => 1,
 
 493   $additional_params = {} unless ($additional_params);
 
 494   if ($self->{"DEBUG"}) {
 
 495     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 498   if ($additional_params->{"DEBUG"}) {
 
 499     $additional_params->{"DEBUG"} =
 
 500       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 503   if (%main::myconfig) {
 
 504     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 505     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 506     $jsc_dateformat =~ s/d+/\%d/gi;
 
 507     $jsc_dateformat =~ s/m+/\%m/gi;
 
 508     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 509     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 512   $additional_params->{"conf_jscalendar"} = $main::jscalendar;
 
 513   $additional_params->{"conf_lizenzen"} = $main::lizenzen;
 
 514   $additional_params->{"conf_latex_templates"} = $main::latex;
 
 515   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 517   my @additional_param_names = keys(%{$additional_params});
 
 518   foreach my $key ($template->param()) {
 
 519     my $param = $self->{$key};
 
 520     $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
 
 521     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
 
 522     $template->param($key => $param);
 
 525   my $output = $template->output();
 
 527   $main::lxdebug->leave_sub();
 
 532 sub show_generic_error {
 
 533   my ($self, $error, $title, $action) = @_;
 
 536   $add_params->{"title"} = $title if ($title);
 
 537   $self->{"label_error"} = $error;
 
 541     map({ delete($self->{$_}); } qw(action));
 
 542     map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
 
 543             if (!ref($self->{$_})); }
 
 545     $add_params->{"SHOW_BUTTON"} = 1;
 
 546     $add_params->{"BUTTON_LABEL"} = $action;
 
 548   $add_params->{"VARIABLES"} = \@vars;
 
 551   print($self->parse_html_template("generic/error", $add_params));
 
 553   die("Error: $error\n");
 
 556 sub show_generic_information {
 
 557   my ($self, $error, $title) = @_;
 
 560   $add_params->{"title"} = $title if ($title);
 
 561   $self->{"label_information"} = $error;
 
 564   print($self->parse_html_template("generic/information", $add_params));
 
 566   die("Information: $error\n");
 
 569 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 570 # changed it to accept an arbitrary number of triggers - sschoeling
 
 572   $main::lxdebug->enter_sub();
 
 575   my $myconfig = shift;
 
 578   # set dateform for jsscript
 
 581     "dd.mm.yy" => "%d.%m.%Y",
 
 582     "dd-mm-yy" => "%d-%m-%Y",
 
 583     "dd/mm/yy" => "%d/%m/%Y",
 
 584     "mm/dd/yy" => "%m/%d/%Y",
 
 585     "mm-dd-yy" => "%m-%d-%Y",
 
 586     "yyyy-mm-dd" => "%Y-%m-%d",
 
 589   my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
 
 590     $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
 
 597       inputField : "| . (shift) . qq|",
 
 598       ifFormat :"$ifFormat",
 
 599       align : "| .  (shift) . qq|", 
 
 600       button : "| . (shift) . qq|"
 
 606        <script type="text/javascript">
 
 607        <!--| . join("", @triggers) . qq|//-->
 
 611   $main::lxdebug->leave_sub();
 
 614 }    #end sub write_trigger
 
 617   $main::lxdebug->enter_sub();
 
 619   my ($self, $msg) = @_;
 
 621   if ($self->{callback}) {
 
 623     ($script, $argv) = split(/\?/, $self->{callback});
 
 624     exec("perl", "$script", $argv);
 
 632   $main::lxdebug->leave_sub();
 
 635 # sort of columns removed - empty sub
 
 637   $main::lxdebug->enter_sub();
 
 639   my ($self, @columns) = @_;
 
 641   $main::lxdebug->leave_sub();
 
 647   $main::lxdebug->enter_sub(2);
 
 649   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 654   my $neg = ($amount =~ s/-//);
 
 656   if (defined($places) && ($places ne '')) {
 
 661       my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 662       $actual_places = length($actual_places);
 
 663       $places = $actual_places > $places ? $actual_places : $places;
 
 666     $amount = $self->round_amount($amount, $places);
 
 669   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 670   my @p = split(/\./, $amount); # split amount at decimal point
 
 672   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 675   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 678     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 679     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 680                         ($neg ? "-$amount"   : "$amount" )    ;
 
 684   $main::lxdebug->leave_sub(2);
 
 689   $main::lxdebug->enter_sub(2);
 
 691   my ($self, $myconfig, $amount) = @_;
 
 693   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 694       || ($myconfig->{numberformat} eq '1000,00')) {
 
 699   if ($myconfig->{numberformat} eq "1'000.00") {
 
 705   $main::lxdebug->leave_sub(2);
 
 707   return ($amount * 1);
 
 711   $main::lxdebug->enter_sub(2);
 
 713   my ($self, $amount, $places) = @_;
 
 716   # Rounding like "Kaufmannsrunden"
 
 717   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 719   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 722   $amount = $amount * (10**($places));
 
 723   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 725   $main::lxdebug->leave_sub(2);
 
 727   return $round_amount;
 
 732   $main::lxdebug->enter_sub();
 
 734   my ($self, $myconfig, $userspath) = @_;
 
 737   $self->{"cwd"} = getcwd();
 
 738   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
 740   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
 741     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 742   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
 743     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
 744     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 745   } elsif (($self->{"format"} =~ /html/i) ||
 
 746            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
 747     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 748   } elsif (($self->{"format"} =~ /xml/i) ||
 
 749              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
 750     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 751   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
 752     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);  
 
 753   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
 754     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 755   } elsif ( defined $self->{'format'}) {
 
 756     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
 757   } elsif ( $self->{'format'} eq '' ) {
 
 758     $self->error("No Outputformat given: $self->{'format'}");
 
 759   } else { #Catch the rest
 
 760     $self->error("Outputformat not defined: $self->{'format'}");  
 
 763   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 764   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 766   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 767       qw(email tel fax name signature company address businessnumber
 
 768          co_ustid taxnumber duns));
 
 769   map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
 
 770       qw(company address signature));
 
 771   map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
 
 773   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 775   # OUT is used for the media, screen, printer, email
 
 776   # for postscript we store a copy in a temporary file
 
 778   $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
 
 779   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 781     $self->{OUT} = ">$self->{tmpfile}";
 
 785     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 787     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 791   if (!$template->parse(*OUT)) {
 
 793     $self->error("$self->{IN} : " . $template->get_error());
 
 798   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 800     if ($self->{media} eq 'email') {
 
 804       my $mail = new Mailer;
 
 806       map { $mail->{$_} = $self->{$_} }
 
 807         qw(cc bcc subject message version format charset);
 
 808       $mail->{to}     = qq|$self->{email}|;
 
 809       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
 810       $mail->{fileid} = "$fileid.";
 
 811       $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
 813       # if we send html or plain text inline
 
 814       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
 815         $mail->{contenttype} = "text/html";
 
 817         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
 818         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
 819         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
 821         open(IN, $self->{tmpfile})
 
 822           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 824           $mail->{message} .= $_;
 
 831         if (!$self->{"do_not_attach"}) {
 
 832           @{ $mail->{attachments} } =
 
 833             ({ "filename" => $self->{"tmpfile"},
 
 834                "name" => $self->{"attachment_filename"} ?
 
 835                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
 838         $mail->{message}       =~ s/\r\n/\n/g;
 
 839         $myconfig->{signature} =~ s/\\n/\n/g;
 
 840         $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 844       my $err = $mail->send($out);
 
 845       $self->error($self->cleanup . "$err") if ($err);
 
 851       my $numbytes = (-s $self->{tmpfile});
 
 852       open(IN, $self->{tmpfile})
 
 853         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 855       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
 857       chdir("$self->{cwd}");
 
 858       #print(STDERR "Kopien $self->{copies}\n");
 
 859       #print(STDERR "OUT $self->{OUT}\n");
 
 860       for my $i (1 .. $self->{copies}) {
 
 862           open(OUT, $self->{OUT})
 
 863             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
 865           $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
 
 867           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
 868 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
 869 Content-Length: $numbytes
 
 873           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
 893   chdir("$self->{cwd}");
 
 894   $main::lxdebug->leave_sub();
 
 898   $main::lxdebug->enter_sub();
 
 902   chdir("$self->{tmpdir}");
 
 905   if (-f "$self->{tmpfile}.err") {
 
 906     open(FH, "$self->{tmpfile}.err");
 
 911   if ($self->{tmpfile}) {
 
 912     $self->{tmpfile} =~ s|.*/||g;
 
 914     $self->{tmpfile} =~ s/\.\w+$//g;
 
 915     my $tmpfile = $self->{tmpfile};
 
 916     unlink(<$tmpfile.*>);
 
 919   chdir("$self->{cwd}");
 
 921   $main::lxdebug->leave_sub();
 
 927   $main::lxdebug->enter_sub();
 
 929   my ($self, $date, $myconfig) = @_;
 
 931   if ($date && $date =~ /\D/) {
 
 933     if ($myconfig->{dateformat} =~ /^yy/) {
 
 934       ($yy, $mm, $dd) = split /\D/, $date;
 
 936     if ($myconfig->{dateformat} =~ /^mm/) {
 
 937       ($mm, $dd, $yy) = split /\D/, $date;
 
 939     if ($myconfig->{dateformat} =~ /^dd/) {
 
 940       ($dd, $mm, $yy) = split /\D/, $date;
 
 945     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
 946     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
 948     $dd = "0$dd" if ($dd < 10);
 
 949     $mm = "0$mm" if ($mm < 10);
 
 954   $main::lxdebug->leave_sub();
 
 959 # Database routines used throughout
 
 962   $main::lxdebug->enter_sub();
 
 964   my ($self, $myconfig) = @_;
 
 966   # connect to database
 
 968     DBI->connect($myconfig->{dbconnect},
 
 969                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
 973   if ($myconfig->{dboptions}) {
 
 974     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
 977   $main::lxdebug->leave_sub();
 
 982 sub dbconnect_noauto {
 
 983   $main::lxdebug->enter_sub();
 
 985   my ($self, $myconfig) = @_;
 
 987   # connect to database
 
 989     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
 990                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
 994   if ($myconfig->{dboptions}) {
 
 995     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
 998   $main::lxdebug->leave_sub();
 
1003 sub update_balance {
 
1004   $main::lxdebug->enter_sub();
 
1006   my ($self, $dbh, $table, $field, $where, $value) = @_;
 
1008   # if we have a value, go do it
 
1011     # retrieve balance from table
 
1012     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1013     my $sth   = $dbh->prepare($query);
 
1015     $sth->execute || $self->dberror($query);
 
1016     my ($balance) = $sth->fetchrow_array;
 
1022     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1023     $dbh->do($query) || $self->dberror($query);
 
1025   $main::lxdebug->leave_sub();
 
1028 sub update_exchangerate {
 
1029   $main::lxdebug->enter_sub();
 
1031   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1033   # some sanity check for currency
 
1035     $main::lxdebug->leave_sub();
 
1039   my $query = qq|SELECT e.curr FROM exchangerate e
 
1040                  WHERE e.curr = '$curr'
 
1041                  AND e.transdate = '$transdate'
 
1043   my $sth = $dbh->prepare($query);
 
1044   $sth->execute || $self->dberror($query);
 
1047   if ($buy != 0 && $sell != 0) {
 
1048     $set = "buy = $buy, sell = $sell";
 
1049   } elsif ($buy != 0) {
 
1050     $set = "buy = $buy";
 
1051   } elsif ($sell != 0) {
 
1052     $set = "sell = $sell";
 
1055   if ($sth->fetchrow_array) {
 
1056     $query = qq|UPDATE exchangerate
 
1058                 WHERE curr = '$curr'
 
1059                 AND transdate = '$transdate'|;
 
1061     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1062                 VALUES ('$curr', $buy, $sell, '$transdate')|;
 
1065   $dbh->do($query) || $self->dberror($query);
 
1067   $main::lxdebug->leave_sub();
 
1070 sub save_exchangerate {
 
1071   $main::lxdebug->enter_sub();
 
1073   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1075   my $dbh = $self->dbconnect($myconfig);
 
1077   my ($buy, $sell) = (0, 0);
 
1078   $buy  = $rate if $fld eq 'buy';
 
1079   $sell = $rate if $fld eq 'sell';
 
1081   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1085   $main::lxdebug->leave_sub();
 
1088 sub get_exchangerate {
 
1089   $main::lxdebug->enter_sub();
 
1091   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1093   unless ($transdate) {
 
1094     $main::lxdebug->leave_sub();
 
1098   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1099                  WHERE e.curr = '$curr'
 
1100                  AND e.transdate = '$transdate'|;
 
1101   my $sth = $dbh->prepare($query);
 
1102   $sth->execute || $self->dberror($query);
 
1104   my ($exchangerate) = $sth->fetchrow_array;
 
1107   if (!$exchangerate) {
 
1111   $main::lxdebug->leave_sub();
 
1113   return $exchangerate;
 
1116 sub set_payment_options {
 
1117   $main::lxdebug->enter_sub();
 
1119   my ($self, $myconfig, $transdate) = @_;
 
1121   if ($self->{payment_id}) {
 
1123     my $dbh = $self->dbconnect($myconfig);
 
1126       qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, | .
 
1127       qq|p.description_long | .
 
1128       qq|FROM payment_terms p | .
 
1131     ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1132      $self->{payment_terms}) =
 
1133        selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1135     if ($transdate eq "") {
 
1136       if ($self->{invdate}) {
 
1137         $transdate = $self->{invdate};
 
1139         $transdate = $self->{transdate};
 
1144       qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date, | .
 
1145       qq|date '$transdate' + $self->{terms_skonto} AS skonto_date | .
 
1146       qq|FROM payment_terms LIMIT 1|;
 
1147     ($self->{netto_date}, $self->{skonto_date}) =
 
1148       selectrow_query($self, $dbh, $query);
 
1150     my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
 
1151     my $skonto_amount = $self->parse_amount($myconfig, $total) *
 
1152       $self->{percent_skonto};
 
1154     $self->{skonto_amount} =
 
1155       $self->format_amount($myconfig, $skonto_amount, 2);
 
1157     if ($self->{"language_id"}) {
 
1159         qq|SELECT t.description_long, | .
 
1160         qq|l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1161         qq|FROM translation_payment_terms t | .
 
1162         qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1163         qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1164       my ($description_long, $output_numberformat, $output_dateformat,
 
1165         $output_longdates) =
 
1166         selectrow_query($self, $dbh, $query,
 
1167                         $self->{"language_id"}, $self->{"payment_id"});
 
1169       $self->{payment_terms} = $description_long if ($description_long);
 
1171       if ($output_dateformat) {
 
1172         foreach my $key (qw(netto_date skonto_date)) {
 
1174             $main::locale->reformat_date($myconfig, $self->{$key},
 
1180       if ($output_numberformat &&
 
1181           ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1182         my $saved_numberformat = $myconfig->{"numberformat"};
 
1183         $myconfig->{"numberformat"} = $output_numberformat;
 
1184         $self->{skonto_amount} =
 
1185           $self->format_amount($myconfig, $skonto_amount, 2);
 
1186         $myconfig->{"numberformat"} = $saved_numberformat;
 
1190     $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1191     $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1192     $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
 
1193     $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
 
1194     $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
 
1195     $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1196     $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1197     $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1198     $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1199     $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1204   $main::lxdebug->leave_sub();
 
1208 sub check_exchangerate {
 
1209   $main::lxdebug->enter_sub();
 
1211   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1213   unless ($transdate) {
 
1214     $main::lxdebug->leave_sub();
 
1218   my $dbh = $self->dbconnect($myconfig);
 
1220   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1221                  WHERE e.curr = '$currency'
 
1222                  AND e.transdate = '$transdate'|;
 
1223   my $sth = $dbh->prepare($query);
 
1224   $sth->execute || $self->dberror($query);
 
1226   my ($exchangerate) = $sth->fetchrow_array;
 
1230   $main::lxdebug->leave_sub();
 
1232   return $exchangerate;
 
1235 sub get_template_language {
 
1236   $main::lxdebug->enter_sub();
 
1238   my ($self, $myconfig) = @_;
 
1240   my $template_code = "";
 
1242   if ($self->{language_id}) {
 
1244     my $dbh = $self->dbconnect($myconfig);
 
1247     my $query = qq|SELECT l.template_code FROM language l
 
1248                   WHERE l.id = $self->{language_id}|;
 
1249     my $sth = $dbh->prepare($query);
 
1250     $sth->execute || $self->dberror($query);
 
1252     ($template_code) = $sth->fetchrow_array;
 
1257   $main::lxdebug->leave_sub();
 
1259   return $template_code;
 
1262 sub get_printer_code {
 
1263   $main::lxdebug->enter_sub();
 
1265   my ($self, $myconfig) = @_;
 
1267   my $template_code = "";
 
1269   if ($self->{printer_id}) {
 
1271     my $dbh = $self->dbconnect($myconfig);
 
1274     my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
 
1275                   WHERE p.id = $self->{printer_id}|;
 
1276     my $sth = $dbh->prepare($query);
 
1277     $sth->execute || $self->dberror($query);
 
1279     ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
 
1284   $main::lxdebug->leave_sub();
 
1286   return $template_code;
 
1290   $main::lxdebug->enter_sub();
 
1292   my ($self, $myconfig) = @_;
 
1294   my $template_code = "";
 
1296   if ($self->{shipto_id}) {
 
1298     my $dbh = $self->dbconnect($myconfig);
 
1301     my $query = qq|SELECT s.* FROM shipto s
 
1302                   WHERE s.shipto_id = $self->{shipto_id}|;
 
1303     my $sth = $dbh->prepare($query);
 
1304     $sth->execute || $self->dberror($query);
 
1305     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1306     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
1311   $main::lxdebug->leave_sub();
 
1316   $main::lxdebug->enter_sub();
 
1318   my ($self, $dbh, $id, $module) = @_;
 
1322     qw(name department_1 department_2 street zipcode city country contact phone fax email)
 
1324     if ($self->{"shipto$item"}) {
 
1325       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1327     $self->{"shipto$item"} =~ s/\'/\'\'/g;
 
1330     if ($self->{shipto_id}) {
 
1331       my $query = qq| UPDATE shipto set
 
1332                       shiptoname = '$self->{shiptoname}',
 
1333                       shiptodepartment_1 = '$self->{shiptodepartment_1}',
 
1334                       shiptodepartment_2 = '$self->{shiptodepartment_2}',
 
1335                       shiptostreet = '$self->{shiptostreet}',
 
1336                       shiptozipcode = '$self->{shiptozipcode}',
 
1337                       shiptocity = '$self->{shiptocity}',
 
1338                       shiptocountry = '$self->{shiptocountry}',
 
1339                       shiptocontact = '$self->{shiptocontact}',
 
1340                       shiptophone = '$self->{shiptophone}',
 
1341                       shiptofax = '$self->{shiptofax}',
 
1342                       shiptoemail = '$self->{shiptoemail}'
 
1343                       WHERE shipto_id = $self->{shipto_id}|;
 
1344       $dbh->do($query) || $self->dberror($query);
 
1347       qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
 
1348                    shiptozipcode, shiptocity, shiptocountry, shiptocontact,
 
1349                    shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
 
1350                    '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
 
1351                    '$self->{shiptozipcode}', '$self->{shiptocity}',
 
1352                    '$self->{shiptocountry}', '$self->{shiptocontact}',
 
1353                    '$self->{shiptophone}', '$self->{shiptofax}',
 
1354                    '$self->{shiptoemail}', '$module')|;
 
1355       $dbh->do($query) || $self->dberror($query);
 
1359   $main::lxdebug->leave_sub();
 
1363   $main::lxdebug->enter_sub();
 
1365   my ($self, $dbh) = @_;
 
1367   my $query = qq|SELECT e.id, e.name FROM employee e
 
1368                  WHERE e.login = '$self->{login}'|;
 
1369   my $sth = $dbh->prepare($query);
 
1370   $sth->execute || $self->dberror($query);
 
1372   ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
 
1373   $self->{employee_id} *= 1;
 
1377   $main::lxdebug->leave_sub();
 
1381   $main::lxdebug->enter_sub();
 
1383   my ($self, $myconfig) = @_;
 
1385   my $dbh = $self->dbconnect($myconfig);
 
1386   my $query = qq|SELECT current_date+terms_netto FROM payment_terms
 
1387                  WHERE id = '$self->{payment_id}'|;
 
1388   my $sth = $dbh->prepare($query);
 
1389   $sth->execute || $self->dberror($query);
 
1391   ($self->{duedate}) = $sth->fetchrow_array;
 
1395   $main::lxdebug->leave_sub();
 
1398 # get contacts for id, if no contact return {"","","","",""}
 
1400   $main::lxdebug->enter_sub();
 
1402   my ($self, $dbh, $id, $key) = @_;
 
1404   $key = "all_contacts" unless ($key);
 
1408     qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung | .
 
1409     qq|FROM contacts c | .
 
1410     qq|WHERE cp_cv_id = ? | .
 
1411     qq|ORDER BY lower(c.cp_name)|;
 
1412   my $sth = $dbh->prepare($query);
 
1413   $sth->execute($id) || $self->dberror($query . " ($id)");
 
1416   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1417     push @{ $self->{$key} }, $ref;
 
1422     push @{ $self->{$key} }, { { "", "", "", "", "", "" } };
 
1425   $main::lxdebug->leave_sub();
 
1429   $main::lxdebug->enter_sub();
 
1431   my ($self, $dbh, $key) = @_;
 
1433   my ($all, $old_id, $where, @values);
 
1435   if (ref($key) eq "HASH") {
 
1438     $key = "ALL_PROJECTS";
 
1440     foreach my $p (keys(%{$params})) {
 
1442         $all = $params->{$p};
 
1443       } elsif ($p eq "old_id") {
 
1444         $old_id = $params->{$p};
 
1445       } elsif ($p eq "key") {
 
1446         $key = $params->{$p};
 
1452     $where = "WHERE active ";
 
1454       if (ref($old_id) eq "ARRAY") {
 
1455         my @ids = grep({ $_ } @{$old_id});
 
1457           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1458           push(@values, @ids);
 
1461         $where .= " OR (id = ?) ";
 
1462         push(@values, $old_id);
 
1468     qq|SELECT id, projectnumber, description, active | .
 
1471     qq|ORDER BY lower(projectnumber)|;
 
1472   my $sth = $dbh->prepare($query);
 
1473   $sth->execute(@values) ||
 
1474     $self->dberror($query . " (" . join(", ", @values) . ")");
 
1477   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1478     push(@{ $self->{$key} }, $ref);
 
1482   $main::lxdebug->leave_sub();
 
1486   $main::lxdebug->enter_sub();
 
1488   my ($self, $dbh, $vc_id, $key) = @_;
 
1490   $key = "all_shipto" unless ($key);
 
1493   # get shipping addresses
 
1495     qq|SELECT s.shipto_id,s.shiptoname,s.shiptodepartment_1 | .
 
1496     qq|FROM shipto s | .
 
1497     qq|WHERE s.trans_id = ?|;
 
1498   my $sth = $dbh->prepare($query);
 
1499   $sth->execute($vc_id) || $self->dberror($query . " ($vc_id)");
 
1501   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1502     push(@{ $self->{$key} }, $ref);
 
1506   $main::lxdebug->leave_sub();
 
1510   $main::lxdebug->enter_sub();
 
1512   my ($self, $dbh, $key) = @_;
 
1514   $key = "all_printers" unless ($key);
 
1517   my $query = qq|SELECT id, printer_description, printer_command FROM printers|;
 
1518   my $sth = $dbh->prepare($query);
 
1519   $sth->execute() || $self->dberror($query);
 
1521   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1522     push(@{ $self->{$key} }, $ref);
 
1526   $main::lxdebug->leave_sub();
 
1530   $main::lxdebug->enter_sub();
 
1532   my ($self, $dbh, $params) = @_;
 
1534   $key = $params->{key};
 
1535   $key = "all_charts" unless ($key);
 
1538   my $transdate = quote_db_date($params->{transdate});
 
1541     qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
1543     qq|LEFT JOIN taxkeys tk ON | .
 
1544     qq|(tk.id = (SELECT id FROM taxkeys | .
 
1545     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
1546     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
1547     qq|ORDER BY c.accno|;
 
1549   my $sth = $dbh->prepare($query);
 
1550   $sth->execute() || $self->dberror($query);
 
1552   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1553     push(@{ $self->{$key} }, $ref);
 
1557   $main::lxdebug->leave_sub();
 
1560 sub _get_taxcharts {
 
1561   $main::lxdebug->enter_sub();
 
1563   my ($self, $dbh, $key) = @_;
 
1565   $key = "all_taxcharts" unless ($key);
 
1568   my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
1570   my $sth = $dbh->prepare($query);
 
1571   $sth->execute() || $self->dberror($query);
 
1573   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1574     push(@{ $self->{$key} }, $ref);
 
1578   $main::lxdebug->leave_sub();
 
1582   $main::lxdebug->enter_sub();
 
1587   my $dbh = $self->dbconnect(\%main::myconfig);
 
1588   my ($sth, $query, $ref);
 
1590   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
1591   my $vc_id = $self->{"${vc}_id"};
 
1593   if ($params{"contacts"}) {
 
1594     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
1597   if ($params{"shipto"}) {
 
1598     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
1601   if ($params{"projects"} || $params{"all_projects"}) {
 
1602     $self->_get_projects($dbh, $params{"all_projects"} ?
 
1603                          $params{"all_projects"} : $params{"projects"},
 
1604                          $params{"all_projects"} ? 1 : 0);
 
1607   if ($params{"printers"}) {
 
1608     $self->_get_printers($dbh, $params{"printers"});
 
1611   if ($params{"charts"}) {
 
1612     $self->_get_charts($dbh, $params{"charts"});
 
1615   if ($params{"taxcharts"}) {
 
1616     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
1621   $main::lxdebug->leave_sub();
 
1624 # this sub gets the id and name from $table
 
1626   $main::lxdebug->enter_sub();
 
1628   my ($self, $myconfig, $table) = @_;
 
1630   # connect to database
 
1631   my $dbh = $self->dbconnect($myconfig);
 
1633   my $name           = $self->like(lc $self->{$table});
 
1634   my $customernumber = $self->like(lc $self->{customernumber});
 
1636   if ($self->{customernumber} ne "") {
 
1637     $query = qq~SELECT c.id, c.name,
 
1638                   c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1640                   WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
 
1643     $query = qq~SELECT c.id, c.name,
 
1644                  c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1646                  WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
 
1650   if ($self->{openinvoices}) {
 
1651     $query = qq~SELECT DISTINCT c.id, c.name,
 
1652                 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1653                 FROM $self->{arap} a
 
1654                 JOIN $table c ON (a.${table}_id = c.id)
 
1655                 WHERE NOT a.amount = a.paid
 
1656                 AND lower(c.name) LIKE '$name'
 
1659   my $sth = $dbh->prepare($query);
 
1661   $sth->execute || $self->dberror($query);
 
1664   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1665     push(@{ $self->{name_list} }, $ref);
 
1671   $main::lxdebug->leave_sub();
 
1676 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1679   $main::lxdebug->enter_sub();
 
1681   my ($self, $myconfig, $table, $module) = @_;
 
1684   my $dbh = $self->dbconnect($myconfig);
 
1686   my $query = qq|SELECT count(*) FROM $table|;
 
1687   my $sth   = $dbh->prepare($query);
 
1688   $sth->execute || $self->dberror($query);
 
1689   my ($count) = $sth->fetchrow_array;
 
1692   # build selection list
 
1693   if ($count < $myconfig->{vclimit}) {
 
1694     $query = qq|SELECT id, name
 
1695                 FROM $table WHERE not obsolete
 
1697     $sth = $dbh->prepare($query);
 
1698     $sth->execute || $self->dberror($query);
 
1700     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1701       push @{ $self->{"all_$table"} }, $ref;
 
1709   $self->get_employee($dbh);
 
1711   # setup sales contacts
 
1712   $query = qq|SELECT e.id, e.name
 
1715               AND NOT e.id = $self->{employee_id}|;
 
1716   $sth = $dbh->prepare($query);
 
1717   $sth->execute || $self->dberror($query);
 
1719   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1720     push @{ $self->{all_employees} }, $ref;
 
1725   push @{ $self->{all_employees} },
 
1726     { id   => $self->{employee_id},
 
1727       name => $self->{employee} };
 
1729   # sort the whole thing
 
1730   @{ $self->{all_employees} } =
 
1731     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
1733   if ($module eq 'AR') {
 
1735     # prepare query for departments
 
1736     $query = qq|SELECT d.id, d.description
 
1742     $query = qq|SELECT d.id, d.description
 
1747   $sth = $dbh->prepare($query);
 
1748   $sth->execute || $self->dberror($query);
 
1750   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1751     push @{ $self->{all_departments} }, $ref;
 
1756   $query = qq|SELECT id, description
 
1759   $sth = $dbh->prepare($query);
 
1760   $sth->execute || $self->dberror($query);
 
1762   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1763     push @{ $self->{languages} }, $ref;
 
1768   $query = qq|SELECT printer_description, id
 
1771   $sth = $dbh->prepare($query);
 
1772   $sth->execute || $self->dberror($query);
 
1774   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1775     push @{ $self->{printers} }, $ref;
 
1781   $query = qq|SELECT id, description
 
1784   $sth = $dbh->prepare($query);
 
1785   $sth->execute || $self->dberror($query);
 
1787   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1788     push @{ $self->{payment_terms} }, $ref;
 
1792   $main::lxdebug->leave_sub();
 
1795 sub language_payment {
 
1796   $main::lxdebug->enter_sub();
 
1798   my ($self, $myconfig) = @_;
 
1799   undef $self->{languages};
 
1800   undef $self->{payment_terms};
 
1801   undef $self->{printers};
 
1804   my $dbh = $self->dbconnect($myconfig);
 
1806   my $query = qq|SELECT id, description
 
1809   my $sth = $dbh->prepare($query);
 
1810   $sth->execute || $self->dberror($query);
 
1812   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1813     push @{ $self->{languages} }, $ref;
 
1818   $query = qq|SELECT printer_description, id
 
1821   $sth = $dbh->prepare($query);
 
1822   $sth->execute || $self->dberror($query);
 
1824   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1825     push @{ $self->{printers} }, $ref;
 
1830   $query = qq|SELECT id, description
 
1833   $sth = $dbh->prepare($query);
 
1834   $sth->execute || $self->dberror($query);
 
1836   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1837     push @{ $self->{payment_terms} }, $ref;
 
1841   # get buchungsgruppen
 
1842   $query = qq|SELECT id, description
 
1843               FROM buchungsgruppen|;
 
1844   $sth = $dbh->prepare($query);
 
1845   $sth->execute || $self->dberror($query);
 
1847   $self->{BUCHUNGSGRUPPEN} = [];
 
1848   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1849     push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
 
1854   $main::lxdebug->leave_sub();
 
1857 # this is only used for reports
 
1858 sub all_departments {
 
1859   $main::lxdebug->enter_sub();
 
1861   my ($self, $myconfig, $table) = @_;
 
1863   my $dbh   = $self->dbconnect($myconfig);
 
1864   my $where = "1 = 1";
 
1866   if (defined $table) {
 
1867     if ($table eq 'customer') {
 
1868       $where = " d.role = 'P'";
 
1872   my $query = qq|SELECT d.id, d.description
 
1876   my $sth = $dbh->prepare($query);
 
1877   $sth->execute || $self->dberror($query);
 
1879   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1880     push @{ $self->{all_departments} }, $ref;
 
1886   $main::lxdebug->leave_sub();
 
1890   $main::lxdebug->enter_sub();
 
1892   my ($self, $module, $myconfig, $table) = @_;
 
1894   $self->all_vc($myconfig, $table, $module);
 
1896   # get last customers or vendors
 
1899   my $dbh = $self->dbconnect($myconfig);
 
1904     my $transdate = "current_date";
 
1905     if ($self->{transdate}) {
 
1906       $transdate = qq|'$self->{transdate}'|;
 
1909     # now get the account numbers
 
1910     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
1911                 FROM chart c, taxkeys tk
 
1912                 WHERE c.link LIKE '%$module%' AND c.id=tk.chart_id AND tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
 
1915     $sth = $dbh->prepare($query);
 
1916     $sth->execute || $self->dberror($query);
 
1918     $self->{accounts} = "";
 
1919     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1921       foreach my $key (split(/:/, $ref->{link})) {
 
1922         if ($key =~ /$module/) {
 
1924           # cross reference for keys
 
1925           $xkeyref{ $ref->{accno} } = $key;
 
1927           push @{ $self->{"${module}_links"}{$key} },
 
1928             { accno       => $ref->{accno},
 
1929               description => $ref->{description},
 
1930               taxkey      => $ref->{taxkey_id},
 
1931               tax_id      => $ref->{tax_id} };
 
1933           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
1939   # get taxkeys and description
 
1940   $query = qq|SELECT id, taxkey, taxdescription
 
1942   $sth = $dbh->prepare($query);
 
1943   $sth->execute || $self->dberror($query);
 
1945   $ref = $sth->fetchrow_hashref(NAME_lc);
 
1947   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1948     push @{ $self->{TAXKEY} }, $ref;
 
1955   $query = qq|SELECT id, description
 
1957   $sth = $dbh->prepare($query);
 
1958   $sth->execute || $self->dberror($query);
 
1961   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1962     push @{ $self->{TAXZONE} }, $ref;
 
1966   if (($module eq "AP") || ($module eq "AR")) {
 
1968     # get tax rates and description
 
1969     $query = qq| SELECT * FROM tax t|;
 
1970     $sth   = $dbh->prepare($query);
 
1971     $sth->execute || $self->dberror($query);
 
1973     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1974       push @{ $self->{TAX} }, $ref;
 
1980     my $arap = ($table eq 'customer') ? 'ar' : 'ap';
 
1982     $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
 
1983                 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
 
1984                 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
 
1985                 c.name AS $table, a.department_id, d.description AS department,
 
1986                 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
 
1987                 a.employee_id, e.name AS employee, a.gldate, a.type
 
1989                 JOIN $table c ON (a.${table}_id = c.id)
 
1990                 LEFT JOIN employee e ON (e.id = a.employee_id)
 
1991                 LEFT JOIN department d ON (d.id = a.department_id)
 
1992                 WHERE a.id = $self->{id}|;
 
1993     $sth = $dbh->prepare($query);
 
1994     $sth->execute || $self->dberror($query);
 
1996     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1997     foreach $key (keys %$ref) {
 
1998       $self->{$key} = $ref->{$key};
 
2003     my $transdate = "current_date";
 
2004     if ($self->{transdate}) {
 
2005       $transdate = qq|'$self->{transdate}'|;
 
2008     # now get the account numbers
 
2009     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2010                 FROM chart c, taxkeys tk
 
2011                 WHERE c.link LIKE '%$module%' AND (((tk.chart_id=c.id) AND NOT(c.link like '%_tax%')) OR (NOT(tk.chart_id=c.id) AND (c.link like '%_tax%'))) AND (((tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)) AND NOT(c.link like '%_tax%')) OR (c.link like '%_tax%'))
 
2014     $sth = $dbh->prepare($query);
 
2015     $sth->execute || $self->dberror($query);
 
2017     $self->{accounts} = "";
 
2018     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2020       foreach my $key (split(/:/, $ref->{link})) {
 
2021         if ($key =~ /$module/) {
 
2023           # cross reference for keys
 
2024           $xkeyref{ $ref->{accno} } = $key;
 
2026           push @{ $self->{"${module}_links"}{$key} },
 
2027             { accno       => $ref->{accno},
 
2028               description => $ref->{description},
 
2029               taxkey      => $ref->{taxkey_id},
 
2030               tax_id      => $ref->{tax_id} };
 
2032           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2038     # get amounts from individual entries
 
2039     $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
 
2040                 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
 
2042                 JOIN chart c ON (c.id = a.chart_id)
 
2043                 LEFT JOIN project p ON (p.id = a.project_id)
 
2044                 LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link='%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1)) 
 
2045                 WHERE a.trans_id = $self->{id}
 
2046                 AND a.fx_transaction = '0'
 
2047                 ORDER BY a.oid,a.transdate|;
 
2048     $sth = $dbh->prepare($query);
 
2049     $sth->execute || $self->dberror($query);
 
2051     my $fld = ($table eq 'customer') ? 'buy' : 'sell';
 
2053     # get exchangerate for currency
 
2054     $self->{exchangerate} =
 
2055       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
 
2059     # store amounts in {acc_trans}{$key} for multiple accounts
 
2060     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2061       $ref->{exchangerate} =
 
2062         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
 
2064       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2067       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2068         $ref->{amount} *= -1;
 
2070       $ref->{index} = $index;
 
2072       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2076     $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
 
2077                   (SELECT c.accno FROM chart c
 
2078                    WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2079                   (SELECT c.accno FROM chart c
 
2080                    WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2082     $sth = $dbh->prepare($query);
 
2083     $sth->execute || $self->dberror($query);
 
2085     $ref = $sth->fetchrow_hashref(NAME_lc);
 
2086     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2092     $query = qq|SELECT current_date AS transdate,
 
2093                 d.curr AS currencies, d.closedto, d.revtrans,
 
2094                   (SELECT c.accno FROM chart c
 
2095                    WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2096                   (SELECT c.accno FROM chart c
 
2097                    WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2099     $sth = $dbh->prepare($query);
 
2100     $sth->execute || $self->dberror($query);
 
2102     $ref = $sth->fetchrow_hashref(NAME_lc);
 
2103     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2106     if ($self->{"$self->{vc}_id"}) {
 
2108       # only setup currency
 
2109       ($self->{currency}) = split(/:/, $self->{currencies});
 
2113       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2115       my $fld = ($table eq 'customer') ? 'buy' : 'sell';
 
2117       # get exchangerate for currency
 
2118       $self->{exchangerate} =
 
2119         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
 
2130   $main::lxdebug->leave_sub();
 
2134   $main::lxdebug->enter_sub();
 
2136   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2138   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
2139   my $where = "1 = 1";
 
2141   if ($self->{type} =~ /_order/) {
 
2143     $where = "quotation = '0'";
 
2145   if ($self->{type} =~ /_quotation/) {
 
2147     $where = "quotation = '1'";
 
2150   my $query = qq|SELECT MAX(id) FROM $arap
 
2152                               AND ${table}_id > 0|;
 
2153   my $sth = $dbh->prepare($query);
 
2154   $sth->execute || $self->dberror($query);
 
2156   my ($trans_id) = $sth->fetchrow_array;
 
2160   $query = qq|SELECT ct.name, a.curr, a.${table}_id,
 
2161               current_date + ct.terms AS duedate, a.department_id,
 
2162               d.description AS department
 
2164               JOIN $table ct ON (a.${table}_id = ct.id)
 
2165               LEFT JOIN department d ON (a.department_id = d.id)
 
2166               WHERE a.id = $trans_id|;
 
2167   $sth = $dbh->prepare($query);
 
2168   $sth->execute || $self->dberror($query);
 
2170   ($self->{$table},  $self->{currency},      $self->{"${table}_id"},
 
2171    $self->{duedate}, $self->{department_id}, $self->{department})
 
2172     = $sth->fetchrow_array;
 
2175   $main::lxdebug->leave_sub();
 
2179   $main::lxdebug->enter_sub();
 
2181   my ($self, $myconfig, $thisdate, $days) = @_;
 
2183   my $dbh = $self->dbconnect($myconfig);
 
2188     my $dateformat = $myconfig->{dateformat};
 
2189     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2191     $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
 
2193     $sth = $dbh->prepare($query);
 
2194     $sth->execute || $self->dberror($query);
 
2196     $query = qq|SELECT current_date AS thisdate
 
2198     $sth = $dbh->prepare($query);
 
2199     $sth->execute || $self->dberror($query);
 
2202   ($thisdate) = $sth->fetchrow_array;
 
2207   $main::lxdebug->leave_sub();
 
2213   $main::lxdebug->enter_sub();
 
2215   my ($self, $string) = @_;
 
2217   if ($string !~ /%/) {
 
2218     $string = "%$string%";
 
2221   $string =~ s/\'/\'\'/g;
 
2223   $main::lxdebug->leave_sub();
 
2229   $main::lxdebug->enter_sub();
 
2231   my ($self, $flds, $new, $count, $numrows) = @_;
 
2235   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
2241   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2243     $j = $item->{ndx} - 1;
 
2244     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2248   for $i ($count + 1 .. $numrows) {
 
2249     map { delete $self->{"${_}_$i"} } @{$flds};
 
2252   $main::lxdebug->leave_sub();
 
2256   $main::lxdebug->enter_sub();
 
2258   my ($self, $myconfig) = @_;
 
2262   my $dbh = $self->dbconnect_noauto($myconfig);
 
2264   my $query = qq|DELETE FROM status
 
2265                  WHERE formname = '$self->{formname}'
 
2267   my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2269   if ($self->{formname} =~ /(check|receipt)/) {
 
2270     for $i (1 .. $self->{rowcount}) {
 
2271       $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
 
2275     $sth->execute($self->{id}) || $self->dberror($query);
 
2279   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2280   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2282   my %queued = split / /, $self->{queued};
 
2284   if ($self->{formname} =~ /(check|receipt)/) {
 
2286     # this is a check or receipt, add one entry for each lineitem
 
2287     my ($accno) = split /--/, $self->{account};
 
2288     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
 
2289                 chart_id) VALUES (?, '$printed',
 
2290                 '$queued{$self->{formname}}', '$self->{prinform}',
 
2291                 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
 
2292     $sth = $dbh->prepare($query) || $self->dberror($query);
 
2294     for $i (1 .. $self->{rowcount}) {
 
2295       if ($self->{"checked_$i"}) {
 
2296         $sth->execute($self->{"id_$i"}) || $self->dberror($query);
 
2301     $query = qq|INSERT INTO status (trans_id, printed, emailed,
 
2302                 spoolfile, formname)
 
2303                 VALUES ($self->{id}, '$printed', '$emailed',
 
2304                 '$queued{$self->{formname}}', '$self->{formname}')|;
 
2305     $dbh->do($query) || $self->dberror($query);
 
2311   $main::lxdebug->leave_sub();
 
2315 # $main::locale->text('SAVED') 
 
2316 # $main::locale->text('DELETED') 
 
2317 # $main::locale->text('ADDED')
 
2318 # $main::locale->text('PAYMENT POSTED')
 
2319 # $main::locale->text('POSTED')
 
2320 # $main::locale->text('POSTED AS NEW')
 
2321 # $main::locale->text('ELSE')
 
2322 # $main::locale->text('SAVED FOR DUNNING')
 
2323 # $main::locale->text('DUNNING STARTED')
 
2324 # $main::locale->text('PRINTED')
 
2325 # $main::locale->text('MAILED')
 
2326 # $main::locale->text('SCREENED')
 
2327 # $main::locale->text('invoice')
 
2328 # $main::locale->text('proforma')
 
2329 # $main::locale->text('sales_order')
 
2330 # $main::locale->text('packing_list')
 
2331 # $main::locale->text('pick_list')
 
2332 # $main::locale->text('purchase_order')
 
2333 # $main::locale->text('bin_list')
 
2334 # $main::locale->text('sales_quotation')
 
2335 # $main::locale->text('request_quotation')
 
2338         $main::lxdebug->enter_sub();
 
2343         if(!exists $self->{employee_id}) {
 
2344                 &get_employee($self, $dbh);
 
2348     qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done) | .
 
2349     qq|VALUES (?, ?, ?, ?)|;
 
2350   my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
 
2351                 $self->{addition}, $self->{what_done});
 
2352   do_query($self, $dbh, $query, @values);
 
2354         $main::lxdebug->leave_sub();
 
2358         $main::lxdebug->enter_sub();
 
2362         my $trans_id = shift();
 
2363         my $restriction = shift();
 
2366         if ($trans_id ne "") {
 
2368       qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name | .
 
2369       qq|FROM history_erp h | .
 
2370       qq|LEFT JOIN employee emp | .
 
2371       qq|ON emp.id = h.employee_id | .
 
2372       qq|WHERE trans_id = ? |
 
2375                 my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2377                 $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
 
2379                 while(my $hash_ref = $sth->fetchrow_hashref()) {
 
2380                         $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
2381                         $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
2382                         $tempArray[$i++] = $hash_ref; 
 
2384     $main::lxdebug->leave_sub() and return \@tempArray
 
2385       if ($i > 0 && $tempArray[0] ne "");
 
2387         $main::lxdebug->leave_sub();
 
2392   $main::lxdebug->enter_sub();
 
2394   my ($self, $dbh) = @_;
 
2396   my ($query, $printed, $emailed);
 
2398   my $formnames  = $self->{printed};
 
2399   my $emailforms = $self->{emailed};
 
2401   $query = qq|DELETE FROM status
 
2402               WHERE formname = '$self->{formname}'
 
2403                       AND trans_id = $self->{id}|;
 
2404   $dbh->do($query) || $self->dberror($query);
 
2406   # this only applies to the forms
 
2407   # checks and receipts are posted when printed or queued
 
2409   if ($self->{queued}) {
 
2410     my %queued = split / /, $self->{queued};
 
2412     foreach my $formname (keys %queued) {
 
2413       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2414       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2416       $query = qq|INSERT INTO status (trans_id, printed, emailed,
 
2417                   spoolfile, formname)
 
2418                   VALUES ($self->{id}, '$printed', '$emailed',
 
2419                   '$queued{$formname}', '$formname')|;
 
2420       $dbh->do($query) || $self->dberror($query);
 
2422       $formnames  =~ s/$self->{formname}//;
 
2423       $emailforms =~ s/$self->{formname}//;
 
2428   # save printed, emailed info
 
2429   $formnames  =~ s/^ +//g;
 
2430   $emailforms =~ s/^ +//g;
 
2433   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2434   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2436   foreach my $formname (keys %status) {
 
2437     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2438     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2440     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2441                 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
 
2442     $dbh->do($query) || $self->dberror($query);
 
2445   $main::lxdebug->leave_sub();
 
2448 sub update_defaults {
 
2449   $main::lxdebug->enter_sub();
 
2451   my ($self, $myconfig, $fld) = @_;
 
2453   my $dbh   = $self->dbconnect_noauto($myconfig);
 
2454   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2455   my $sth   = $dbh->prepare($query);
 
2457   $sth->execute || $self->dberror($query);
 
2458   my ($var) = $sth->fetchrow_array;
 
2463   $query = qq|UPDATE defaults
 
2465   $dbh->do($query) || $self->dberror($query);
 
2470   $main::lxdebug->leave_sub();
 
2475 sub update_business {
 
2476   $main::lxdebug->enter_sub();
 
2478   my ($self, $myconfig, $business_id) = @_;
 
2480   my $dbh   = $self->dbconnect_noauto($myconfig);
 
2482     qq|SELECT customernumberinit FROM business  WHERE id=$business_id FOR UPDATE|;
 
2483   my $sth = $dbh->prepare($query);
 
2485   $sth->execute || $self->dberror($query);
 
2486   my ($var) = $sth->fetchrow_array;
 
2491   $query = qq|UPDATE business
 
2492               SET customernumberinit = '$var' WHERE id=$business_id|;
 
2493   $dbh->do($query) || $self->dberror($query);
 
2498   $main::lxdebug->leave_sub();
 
2504   $main::lxdebug->enter_sub();
 
2506   my ($self, $myconfig, $salesman) = @_;
 
2508   my $dbh   = $self->dbconnect($myconfig);
 
2510     qq|SELECT id, name FROM customer  WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
 
2511   my $sth = $dbh->prepare($query);
 
2512   $sth->execute || $self->dberror($query);
 
2515   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2516     push(@{ $self->{salesman_list} }, $ref);
 
2520   $main::lxdebug->leave_sub();
 
2525 sub get_partsgroup {
 
2526   $main::lxdebug->enter_sub();
 
2528   my ($self, $myconfig, $p) = @_;
 
2530   my $dbh = $self->dbconnect($myconfig);
 
2532   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2534                  JOIN parts p ON (p.partsgroup_id = pg.id)|;
 
2536   if ($p->{searchitems} eq 'part') {
 
2538                  WHERE p.inventory_accno_id > 0|;
 
2540   if ($p->{searchitems} eq 'service') {
 
2542                  WHERE p.inventory_accno_id IS NULL|;
 
2544   if ($p->{searchitems} eq 'assembly') {
 
2546                  WHERE p.assembly = '1'|;
 
2548   if ($p->{searchitems} eq 'labor') {
 
2550                  WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
 
2554                  ORDER BY partsgroup|;
 
2557     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2558                 ORDER BY partsgroup|;
 
2561   if ($p->{language_code}) {
 
2562     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2563                 t.description AS translation
 
2565                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2566                 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
 
2567                 ORDER BY translation|;
 
2570   my $sth = $dbh->prepare($query);
 
2571   $sth->execute || $self->dberror($query);
 
2573   $self->{all_partsgroup} = ();
 
2574   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2575     push @{ $self->{all_partsgroup} }, $ref;
 
2579   $main::lxdebug->leave_sub();
 
2582 sub get_pricegroup {
 
2583   $main::lxdebug->enter_sub();
 
2585   my ($self, $myconfig, $p) = @_;
 
2587   my $dbh = $self->dbconnect($myconfig);
 
2589   my $query = qq|SELECT p.id, p.pricegroup
 
2593                  ORDER BY pricegroup|;
 
2596     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2597                 ORDER BY pricegroup|;
 
2600   my $sth = $dbh->prepare($query);
 
2601   $sth->execute || $self->dberror($query);
 
2603   $self->{all_pricegroup} = ();
 
2604   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2605     push @{ $self->{all_pricegroup} }, $ref;
 
2610   $main::lxdebug->leave_sub();
 
2614   my ($self, $dbh, $myconfig, $audittrail) = @_;
 
2616   # table, $reference, $formname, $action, $id, $transdate) = @_;
 
2623     $dbh        = $self->dbconnect($myconfig);
 
2627   # if we have an id add audittrail, otherwise get a new timestamp
 
2629   if ($audittrail->{id}) {
 
2631     $query = qq|SELECT audittrail FROM defaults|;
 
2633     if ($dbh->selectrow_array($query)) {
 
2634       my ($null, $employee_id) = $self->get_employee($dbh);
 
2636       if ($self->{audittrail} && !$myconfig) {
 
2637         chop $self->{audittrail};
 
2639         my @a = split /\|/, $self->{audittrail};
 
2643         my @flds = qw(tablename reference formname action transdate);
 
2645         # put into hash and remove dups
 
2647           $key = "$a[2]$a[3]";
 
2649           $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
 
2653         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2654                     formname, action, employee_id, transdate)
 
2655                     VALUES ($audittrail->{id}, ?, ?,
 
2656                     ?, ?, $employee_id, ?)|;
 
2657         my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2661             $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
 
2665           for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
 
2667           $sth->execute || $self->dberror;
 
2672       if ($audittrail->{transdate}) {
 
2673         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2674                     formname, action, employee_id, transdate) VALUES (
 
2675                     $audittrail->{id}, '$audittrail->{tablename}', |
 
2676           . $dbh->quote($audittrail->{reference}) . qq|,
 
2677                     '$audittrail->{formname}', '$audittrail->{action}',
 
2678                     $employee_id, '$audittrail->{transdate}')|;
 
2680         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2681                     formname, action, employee_id) VALUES ($audittrail->{id},
 
2682                     '$audittrail->{tablename}', |
 
2683           . $dbh->quote($audittrail->{reference}) . qq|,
 
2684                     '$audittrail->{formname}', '$audittrail->{action}',
 
2691     $query = qq|SELECT current_timestamp FROM defaults|;
 
2692     my ($timestamp) = $dbh->selectrow_array($query);
 
2695       "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
 
2698   $dbh->disconnect if $disconnect;
 
2706 # usage $form->all_years($myconfig, [$dbh])
 
2707 # return list of all years where bookings found
 
2710   $main::lxdebug->enter_sub();
 
2712   my ($self, $myconfig, $dbh) = @_;
 
2716     $dbh = $self->dbconnect($myconfig);
 
2721   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
2722                      (SELECT MAX(transdate) FROM acc_trans)
 
2724   my ($startdate, $enddate) = $dbh->selectrow_array($query);
 
2726   if ($myconfig->{dateformat} =~ /^yy/) {
 
2727     ($startdate) = split /\W/, $startdate;
 
2728     ($enddate) = split /\W/, $enddate;
 
2730     (@_) = split /\W/, $startdate;
 
2732     (@_) = split /\W/, $enddate;
 
2737   $startdate = substr($startdate,0,4);
 
2738   $enddate = substr($enddate,0,4);
 
2740   while ($enddate >= $startdate) {
 
2741     push @all_years, $enddate--;
 
2744   $dbh->disconnect if $disconnect;
 
2748   $main::lxdebug->leave_sub();