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 #======================================================================
 
  56     $standard_dbh->disconnect();
 
  62   $main::lxdebug->enter_sub(2);
 
  66   my @pairs = split(/&/, $input);
 
  69     my ($name, $value) = split(/=/, $_, 2);
 
  70     $in{$name} = unescape(undef, $value);
 
  73   $main::lxdebug->leave_sub(2);
 
  78 sub _request_to_hash {
 
  79   $main::lxdebug->enter_sub(2);
 
  83   if (!$ENV{'CONTENT_TYPE'}
 
  84       || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
 
  85     $main::lxdebug->leave_sub(2);
 
  86     return _input_to_hash($input);
 
  89   my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr);
 
  92   my $boundary = '--' . $1;
 
  94   foreach my $line (split m/\n/, $input) {
 
  95     last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
 
  97     if (($line eq $boundary) || ($line eq "$boundary\r")) {
 
  98       $params{$name} =~ s|\r?\n$|| if $name;
 
 100       undef $name, $filename;
 
 103       $content_type   = "text/plain";
 
 110     next unless $boundary_found;
 
 112     if (!$headers_done) {
 
 113       $line =~ s/[\r\n]*$//;
 
 120       if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
 
 121         if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
 
 123           substr $line, $-[0], $+[0] - $-[0], "";
 
 126         if ($line =~ m|name\s*=\s*"(.*?)"|i) {
 
 128           substr $line, $-[0], $+[0] - $-[0], "";
 
 132         $params{FILENAME} = $filename if ($filename);
 
 137       if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
 
 146     $params{$name} .= "${line}\n";
 
 149   $params{$name} =~ s|\r?\n$|| if $name;
 
 151   $main::lxdebug->leave_sub(2);
 
 156   $main::lxdebug->enter_sub();
 
 162   if ($LXDebug::watch_form) {
 
 163     require SL::Watchdog;
 
 164     tie %{ $self }, 'SL::Watchdog';
 
 167   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 169   if ($ENV{QUERY_STRING}) {
 
 170     $_ = $ENV{QUERY_STRING};
 
 177   my %parameters = _request_to_hash($_);
 
 178   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 180   $self->{action} = lc $self->{action};
 
 181   $self->{action} =~ s/( |-|,|\#)/_/g;
 
 183   $self->{version}   = "2.4.2";
 
 185   $main::lxdebug->leave_sub();
 
 191   $main::lxdebug->enter_sub();
 
 197   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 199   $main::lxdebug->leave_sub();
 
 203   $main::lxdebug->enter_sub(2);
 
 205   my ($self, $str) = @_;
 
 207   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 209   $main::lxdebug->leave_sub(2);
 
 215   $main::lxdebug->enter_sub(2);
 
 217   my ($self, $str) = @_;
 
 222   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 224   $main::lxdebug->leave_sub(2);
 
 230   my ($self, $str) = @_;
 
 232   if ($str && !ref($str)) {
 
 233     $str =~ s/\"/"/g;
 
 241   my ($self, $str) = @_;
 
 243   if ($str && !ref($str)) {
 
 244     $str =~ s/"/\"/g;
 
 252   $main::lxdebug->enter_sub(2);
 
 254   my ($self, $str) = @_;
 
 257     ('order' => ['"', '<', '>'],
 
 263   map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 265   $main::lxdebug->leave_sub(2);
 
 274     map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 276     for (sort keys %$self) {
 
 277       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 278       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 285   $main::lxdebug->enter_sub();
 
 287   $main::lxdebug->show_backtrace();
 
 289   my ($self, $msg) = @_;
 
 290   if ($ENV{HTTP_USER_AGENT}) {
 
 292     $self->show_generic_error($msg);
 
 296     if ($self->{error_function}) {
 
 297       &{ $self->{error_function} }($msg);
 
 303   $main::lxdebug->leave_sub();
 
 307   $main::lxdebug->enter_sub();
 
 309   my ($self, $msg) = @_;
 
 311   if ($ENV{HTTP_USER_AGENT}) {
 
 314     if (!$self->{header}) {
 
 327     if ($self->{info_function}) {
 
 328       &{ $self->{info_function} }($msg);
 
 334   $main::lxdebug->leave_sub();
 
 338   $main::lxdebug->enter_sub();
 
 340   my ($self, $str, $cols, $maxrows) = @_;
 
 344   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 346   $maxrows = $rows unless defined $maxrows;
 
 348   $main::lxdebug->leave_sub();
 
 350   return ($rows > $maxrows) ? $maxrows : $rows;
 
 354   $main::lxdebug->enter_sub();
 
 356   my ($self, $msg) = @_;
 
 358   $self->error("$msg\n" . $DBI::errstr);
 
 360   $main::lxdebug->leave_sub();
 
 364   $main::lxdebug->enter_sub();
 
 366   my ($self, $name, $msg) = @_;
 
 368   if ($self->{$name} =~ /^\s*$/) {
 
 371   $main::lxdebug->leave_sub();
 
 375   $main::lxdebug->enter_sub();
 
 377   my ($self, $extra_code) = @_;
 
 379   if ($self->{header}) {
 
 380     $main::lxdebug->leave_sub();
 
 384   my ($stylesheet, $favicon);
 
 386   if ($ENV{HTTP_USER_AGENT}) {
 
 388     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 390         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 394     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 396     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 398         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 402     my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 404     if ($self->{landscape}) {
 
 405       $pagelayout = qq|<style type="text/css">
 
 406                         \@page { size:landscape; }
 
 410     my $fokus = qq|  document.$self->{fokus}.focus();| if ($self->{"fokus"});
 
 414     if ($self->{jsscript} == 1) {
 
 417         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 418         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 419         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 420         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 427       ? "$self->{title} - $self->{titlebar}"
 
 430     foreach $item (@ { $self->{AJAX} }) {
 
 431       $ajax .= $item->show_javascript();
 
 433     print qq|Content-Type: text/html; charset=${db_charset};
 
 437   <title>$self->{titlebar}</title>
 
 441   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
 
 445   <script type="text/javascript">
 
 453   <meta name="robots" content="noindex,nofollow" />
 
 454   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 455   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 457   <script type="text/javascript" src="js/tabcontent.js">
 
 459   /***********************************************
 
 460   * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 461   * This notice MUST stay intact for legal use
 
 462   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 463   ***********************************************/
 
 474   $main::lxdebug->leave_sub();
 
 477 sub parse_html_template {
 
 478   $main::lxdebug->enter_sub();
 
 480   my ($self, $file, $additional_params) = @_;
 
 483   if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 484     $language = $main::language;
 
 486     $language = $main::myconfig{"countrycode"};
 
 488   $language = "de" unless ($language);
 
 490   if (-f "templates/webpages/${file}_${language}.html") {
 
 491     if ((-f ".developer") &&
 
 492         (-f "templates/webpages/${file}_master.html") &&
 
 493         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 494          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 495       my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 496         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 497       print(qq|<pre>$info</pre>|);
 
 501     $file = "templates/webpages/${file}_${language}.html";
 
 502   } elsif (-f "templates/webpages/${file}.html") {
 
 503     $file = "templates/webpages/${file}.html";
 
 505     my $info = "Web page template '${file}' not found.\n" .
 
 506       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 507     print(qq|<pre>$info</pre>|);
 
 511   my $template = HTML::Template->new("filename" => $file,
 
 512                                      "die_on_bad_params" => 0,
 
 514                                      "case_sensitive" => 1,
 
 515                                      "loop_context_vars" => 1,
 
 518   $additional_params = {} unless ($additional_params);
 
 519   if ($self->{"DEBUG"}) {
 
 520     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 523   if ($additional_params->{"DEBUG"}) {
 
 524     $additional_params->{"DEBUG"} =
 
 525       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 528   if (%main::myconfig) {
 
 529     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 530     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 531     $jsc_dateformat =~ s/d+/\%d/gi;
 
 532     $jsc_dateformat =~ s/m+/\%m/gi;
 
 533     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 534     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 537   $additional_params->{"conf_webdav"}                 = $main::webdav;
 
 538   $additional_params->{"conf_lizenzen"}               = $main::lizenzen;
 
 539   $additional_params->{"conf_latex_templates"}        = $main::latex;
 
 540   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 542   my @additional_param_names = keys(%{$additional_params});
 
 543   foreach my $key ($template->param()) {
 
 544     my $param = $self->{$key};
 
 545     $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
 
 546     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
 
 547     $template->param($key => $param);
 
 550   my $output = $template->output();
 
 552   $main::lxdebug->leave_sub();
 
 557 sub show_generic_error {
 
 558   my ($self, $error, $title, $action) = @_;
 
 561   $add_params->{"title"} = $title if ($title);
 
 562   $self->{"label_error"} = $error;
 
 566     map({ delete($self->{$_}); } qw(action));
 
 567     map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
 
 568             if (!ref($self->{$_})); }
 
 570     $add_params->{"SHOW_BUTTON"} = 1;
 
 571     $add_params->{"BUTTON_LABEL"} = $action;
 
 573   $add_params->{"VARIABLES"} = \@vars;
 
 576   print($self->parse_html_template("generic/error", $add_params));
 
 578   die("Error: $error\n");
 
 581 sub show_generic_information {
 
 582   my ($self, $error, $title) = @_;
 
 585   $add_params->{"title"} = $title if ($title);
 
 586   $self->{"label_information"} = $error;
 
 589   print($self->parse_html_template("generic/information", $add_params));
 
 591   die("Information: $error\n");
 
 594 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 595 # changed it to accept an arbitrary number of triggers - sschoeling
 
 597   $main::lxdebug->enter_sub();
 
 600   my $myconfig = shift;
 
 603   # set dateform for jsscript
 
 606     "dd.mm.yy" => "%d.%m.%Y",
 
 607     "dd-mm-yy" => "%d-%m-%Y",
 
 608     "dd/mm/yy" => "%d/%m/%Y",
 
 609     "mm/dd/yy" => "%m/%d/%Y",
 
 610     "mm-dd-yy" => "%m-%d-%Y",
 
 611     "yyyy-mm-dd" => "%Y-%m-%d",
 
 614   my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
 
 615     $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
 
 622       inputField : "| . (shift) . qq|",
 
 623       ifFormat :"$ifFormat",
 
 624       align : "| .  (shift) . qq|",
 
 625       button : "| . (shift) . qq|"
 
 631        <script type="text/javascript">
 
 632        <!--| . join("", @triggers) . qq|//-->
 
 636   $main::lxdebug->leave_sub();
 
 639 }    #end sub write_trigger
 
 642   $main::lxdebug->enter_sub();
 
 644   my ($self, $msg) = @_;
 
 646   if ($self->{callback}) {
 
 648     ($script, $argv) = split(/\?/, $self->{callback});
 
 649     exec("perl", "$script", $argv);
 
 657   $main::lxdebug->leave_sub();
 
 660 # sort of columns removed - empty sub
 
 662   $main::lxdebug->enter_sub();
 
 664   my ($self, @columns) = @_;
 
 666   $main::lxdebug->leave_sub();
 
 672   $main::lxdebug->enter_sub(2);
 
 674   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 679   my $neg = ($amount =~ s/-//);
 
 681   if (defined($places) && ($places ne '')) {
 
 686       my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 687       $actual_places = length($actual_places);
 
 688       $places = $actual_places > $places ? $actual_places : $places;
 
 691     $amount = $self->round_amount($amount, $places);
 
 694   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 695   my @p = split(/\./, $amount); # split amount at decimal point
 
 697   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 700   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 703     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 704     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 705                         ($neg ? "-$amount"   : "$amount" )    ;
 
 709   $main::lxdebug->leave_sub(2);
 
 714   $main::lxdebug->enter_sub(2);
 
 716   my ($self, $myconfig, $amount) = @_;
 
 718   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 719       || ($myconfig->{numberformat} eq '1000,00')) {
 
 724   if ($myconfig->{numberformat} eq "1'000.00") {
 
 730   $main::lxdebug->leave_sub(2);
 
 732   return ($amount * 1);
 
 736   $main::lxdebug->enter_sub(2);
 
 738   my ($self, $amount, $places) = @_;
 
 741   # Rounding like "Kaufmannsrunden"
 
 742   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 744   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 747   $amount = $amount * (10**($places));
 
 748   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 750   $main::lxdebug->leave_sub(2);
 
 752   return $round_amount;
 
 757   $main::lxdebug->enter_sub();
 
 759   my ($self, $myconfig, $userspath) = @_;
 
 760   my ($template, $out);
 
 764   $self->{"cwd"} = getcwd();
 
 765   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
 767   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
 768     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 769   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
 770     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
 771     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 772   } elsif (($self->{"format"} =~ /html/i) ||
 
 773            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
 774     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 775   } elsif (($self->{"format"} =~ /xml/i) ||
 
 776              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
 777     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 778   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
 779     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 780   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
 781     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 782   } elsif ( defined $self->{'format'}) {
 
 783     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
 784   } elsif ( $self->{'format'} eq '' ) {
 
 785     $self->error("No Outputformat given: $self->{'format'}");
 
 786   } else { #Catch the rest
 
 787     $self->error("Outputformat not defined: $self->{'format'}");
 
 790   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 791   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 793   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 794       qw(email tel fax name signature company address businessnumber
 
 795          co_ustid taxnumber duns));
 
 796   map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
 
 797       qw(company address signature));
 
 798   map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
 
 800   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 802   # OUT is used for the media, screen, printer, email
 
 803   # for postscript we store a copy in a temporary file
 
 805   my $prepend_userspath;
 
 807   if (!$self->{tmpfile}) {
 
 808     $self->{tmpfile}   = "${fileid}.$self->{IN}";
 
 809     $prepend_userspath = 1;
 
 812   $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
 
 814   $self->{tmpfile} =~ s|.*/||;
 
 815   $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
 
 816   $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
 
 818   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 820     $self->{OUT} = ">$self->{tmpfile}";
 
 824     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 826     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 830   if (!$template->parse(*OUT)) {
 
 832     $self->error("$self->{IN} : " . $template->get_error());
 
 837   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 839     if ($self->{media} eq 'email') {
 
 841       my $mail = new Mailer;
 
 843       map { $mail->{$_} = $self->{$_} }
 
 844         qw(cc bcc subject message version format);
 
 845       $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 846       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
 847       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
 848       $mail->{fileid} = "$fileid.";
 
 849       $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
 851       # if we send html or plain text inline
 
 852       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
 853         $mail->{contenttype} = "text/html";
 
 855         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
 856         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
 857         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
 859         open(IN, $self->{tmpfile})
 
 860           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 862           $mail->{message} .= $_;
 
 869         if (!$self->{"do_not_attach"}) {
 
 870           @{ $mail->{attachments} } =
 
 871             ({ "filename" => $self->{"tmpfile"},
 
 872                "name" => $self->{"attachment_filename"} ?
 
 873                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
 876         $mail->{message}       =~ s/\r\n/\n/g;
 
 877         $myconfig->{signature} =~ s/\\n/\n/g;
 
 878         $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 882       my $err = $mail->send();
 
 883       $self->error($self->cleanup . "$err") if ($err);
 
 889       my $numbytes = (-s $self->{tmpfile});
 
 890       open(IN, $self->{tmpfile})
 
 891         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 893       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
 895       chdir("$self->{cwd}");
 
 896       #print(STDERR "Kopien $self->{copies}\n");
 
 897       #print(STDERR "OUT $self->{OUT}\n");
 
 898       for my $i (1 .. $self->{copies}) {
 
 900           open(OUT, $self->{OUT})
 
 901             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
 903           $self->{attachment_filename} = ($self->{attachment_filename}) 
 
 904                                        ? $self->{attachment_filename}
 
 905                                        : $self->generate_attachment_filename();
 
 908           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
 909 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
 910 Content-Length: $numbytes
 
 914           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
 934   chdir("$self->{cwd}");
 
 935   $main::lxdebug->leave_sub();
 
 938 sub generate_attachment_filename {
 
 941   my %formname_translations = (
 
 942      bin_list            => $main::locale->text('Bin List'),
 
 943      credit_note         => $main::locale->text('Credit Note'),
 
 944      invoice             => $main::locale->text('Invoice'),
 
 945      packing_list        => $main::locale->text('Packing List'),
 
 946      pick_list           => $main::locale->text('Pick List'),
 
 947      proforma            => $main::locale->text('Proforma Invoice'),
 
 948      purchase_order      => $main::locale->text('Purchase Order'),
 
 949      request_quotation   => $main::locale->text('RFQ'),
 
 950      sales_order         => $main::locale->text('Confirmation'),
 
 951      sales_quotation     => $main::locale->text('Quotation'),
 
 952      storno_invoice      => $main::locale->text('Storno Invoice'),
 
 953      storno_packing_list => $main::locale->text('Storno Packing List'),
 
 956   my $attachment_filename = $formname_translations{$self->{"formname"}};
 
 958       (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
 
 959     : ($self->{"type"} =~ /_quotation$/)                       ? "quo"
 
 962   if ($attachment_filename && $self->{"${prefix}number"}) {
 
 963     $attachment_filename .= "_" . $self->{"${prefix}number"}
 
 964                             . (  $self->{format} =~ /pdf/i          ? ".pdf"
 
 965                                : $self->{format} =~ /postscript/i   ? ".ps"
 
 966                                : $self->{format} =~ /opendocument/i ? ".odt"
 
 967                                : $self->{format} =~ /html/i         ? ".html"
 
 969     $attachment_filename =~ s/ /_/g;
 
 970     my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue", 
 
 971                     "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
 
 972     map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
 
 974     $attachment_filename = "";
 
 977   return $attachment_filename;
 
 981   $main::lxdebug->enter_sub();
 
 985   chdir("$self->{tmpdir}");
 
 988   if (-f "$self->{tmpfile}.err") {
 
 989     open(FH, "$self->{tmpfile}.err");
 
 994   if ($self->{tmpfile}) {
 
 995     $self->{tmpfile} =~ s|.*/||g;
 
 997     $self->{tmpfile} =~ s/\.\w+$//g;
 
 998     my $tmpfile = $self->{tmpfile};
 
 999     unlink(<$tmpfile.*>);
 
1002   chdir("$self->{cwd}");
 
1004   $main::lxdebug->leave_sub();
 
1010   $main::lxdebug->enter_sub();
 
1012   my ($self, $date, $myconfig) = @_;
 
1014   if ($date && $date =~ /\D/) {
 
1016     if ($myconfig->{dateformat} =~ /^yy/) {
 
1017       ($yy, $mm, $dd) = split /\D/, $date;
 
1019     if ($myconfig->{dateformat} =~ /^mm/) {
 
1020       ($mm, $dd, $yy) = split /\D/, $date;
 
1022     if ($myconfig->{dateformat} =~ /^dd/) {
 
1023       ($dd, $mm, $yy) = split /\D/, $date;
 
1028     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
1029     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
1031     $dd = "0$dd" if ($dd < 10);
 
1032     $mm = "0$mm" if ($mm < 10);
 
1034     $date = "$yy$mm$dd";
 
1037   $main::lxdebug->leave_sub();
 
1042 # Database routines used throughout
 
1045   $main::lxdebug->enter_sub(2);
 
1047   my ($self, $myconfig) = @_;
 
1049   # connect to database
 
1051     DBI->connect($myconfig->{dbconnect},
 
1052                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
1056   if ($myconfig->{dboptions}) {
 
1057     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1060   $main::lxdebug->leave_sub(2);
 
1065 sub dbconnect_noauto {
 
1066   $main::lxdebug->enter_sub();
 
1068   my ($self, $myconfig) = @_;
 
1070   # connect to database
 
1072     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
1073                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
1077   if ($myconfig->{dboptions}) {
 
1078     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1081   $main::lxdebug->leave_sub();
 
1086 sub get_standard_dbh {
 
1087   $main::lxdebug->enter_sub(2);
 
1089   my ($self, $myconfig) = @_;
 
1091   $standard_dbh ||= $self->dbconnect_noauto($myconfig);
 
1093   $main::lxdebug->leave_sub(2);
 
1095   return $standard_dbh;
 
1098 sub update_balance {
 
1099   $main::lxdebug->enter_sub();
 
1101   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
1103   # if we have a value, go do it
 
1106     # retrieve balance from table
 
1107     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1108     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1109     my ($balance) = $sth->fetchrow_array;
 
1115     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1116     do_query($self, $dbh, $query, @values);
 
1118   $main::lxdebug->leave_sub();
 
1121 sub update_exchangerate {
 
1122   $main::lxdebug->enter_sub();
 
1124   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1126   # some sanity check for currency
 
1128     $main::lxdebug->leave_sub();
 
1132   my $query = qq|SELECT e.curr FROM exchangerate e
 
1133                  WHERE e.curr = ? AND e.transdate = ?
 
1135   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1138   if ($buy != 0 && $sell != 0) {
 
1139     $set = "buy = $buy, sell = $sell";
 
1140   } elsif ($buy != 0) {
 
1141     $set = "buy = $buy";
 
1142   } elsif ($sell != 0) {
 
1143     $set = "sell = $sell";
 
1146   if ($sth->fetchrow_array) {
 
1147     $query = qq|UPDATE exchangerate
 
1152     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1153                 VALUES (?, $buy, $sell, ?)|;
 
1156   do_query($self, $dbh, $query, $curr, $transdate);
 
1158   $main::lxdebug->leave_sub();
 
1161 sub save_exchangerate {
 
1162   $main::lxdebug->enter_sub();
 
1164   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1166   my $dbh = $self->dbconnect($myconfig);
 
1168   my ($buy, $sell) = (0, 0);
 
1169   $buy  = $rate if $fld eq 'buy';
 
1170   $sell = $rate if $fld eq 'sell';
 
1172   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1176   $main::lxdebug->leave_sub();
 
1179 sub get_exchangerate {
 
1180   $main::lxdebug->enter_sub();
 
1182   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1184   unless ($transdate) {
 
1185     $main::lxdebug->leave_sub();
 
1189   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1190                  WHERE e.curr = ? AND e.transdate = ?|;
 
1191   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1193   if (!$exchangerate) {
 
1197   $main::lxdebug->leave_sub();
 
1199   return $exchangerate;
 
1202 sub check_exchangerate {
 
1203   $main::lxdebug->enter_sub();
 
1205   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1207   unless ($transdate) {
 
1208     $main::lxdebug->leave_sub();
 
1212   my $dbh = $self->dbconnect($myconfig);
 
1214   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1215                  WHERE e.curr = ? AND e.transdate = ?|;
 
1216   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1219   $exchangerate = 1 if ($exchangerate == 0);
 
1220   $exchangerate = 1 if ($exchangerate eq "");
 
1222   $main::lxdebug->leave_sub();
 
1224   return $exchangerate;
 
1227 sub set_payment_options {
 
1228   $main::lxdebug->enter_sub();
 
1230   my ($self, $myconfig, $transdate) = @_;
 
1232   return $main::lxdebug->leave_sub() unless ($self->{payment_id});
 
1234   my $dbh = $self->get_standard_dbh($myconfig);
 
1237     qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
 
1238     qq|FROM payment_terms p | .
 
1241   ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1242    $self->{payment_terms}) =
 
1243      selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1245   if ($transdate eq "") {
 
1246     if ($self->{invdate}) {
 
1247       $transdate = $self->{invdate};
 
1249       $transdate = $self->{transdate};
 
1254     qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1255     qq|FROM payment_terms|;
 
1256   ($self->{netto_date}, $self->{skonto_date}) =
 
1257     selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1259   my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
 
1260   my $skonto_amount = $self->parse_amount($myconfig, $total) *
 
1261     $self->{percent_skonto};
 
1263   $self->{skonto_amount} =
 
1264     $self->format_amount($myconfig, $skonto_amount, 2);
 
1266   if ($self->{"language_id"}) {
 
1268       qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1269       qq|FROM translation_payment_terms t | .
 
1270       qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1271       qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1272     my ($description_long, $output_numberformat, $output_dateformat,
 
1273       $output_longdates) =
 
1274       selectrow_query($self, $dbh, $query,
 
1275                       $self->{"language_id"}, $self->{"payment_id"});
 
1277     $self->{payment_terms} = $description_long if ($description_long);
 
1279     if ($output_dateformat) {
 
1280       foreach my $key (qw(netto_date skonto_date)) {
 
1282           $main::locale->reformat_date($myconfig, $self->{$key},
 
1288     if ($output_numberformat &&
 
1289         ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1290       my $saved_numberformat = $myconfig->{"numberformat"};
 
1291       $myconfig->{"numberformat"} = $output_numberformat;
 
1292       $self->{skonto_amount} =
 
1293         $self->format_amount($myconfig, $skonto_amount, 2);
 
1294       $myconfig->{"numberformat"} = $saved_numberformat;
 
1298   $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1299   $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1300   $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
 
1301   $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
 
1302   $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
 
1303   $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1304   $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1305   $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1306   $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1307   $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1309   $main::lxdebug->leave_sub();
 
1313 sub get_template_language {
 
1314   $main::lxdebug->enter_sub();
 
1316   my ($self, $myconfig) = @_;
 
1318   my $template_code = "";
 
1320   if ($self->{language_id}) {
 
1321     my $dbh = $self->get_standard_dbh($myconfig);
 
1322     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1323     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1326   $main::lxdebug->leave_sub();
 
1328   return $template_code;
 
1331 sub get_printer_code {
 
1332   $main::lxdebug->enter_sub();
 
1334   my ($self, $myconfig) = @_;
 
1336   my $template_code = "";
 
1338   if ($self->{printer_id}) {
 
1339     my $dbh = $self->get_standard_dbh($myconfig);
 
1340     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1341     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1344   $main::lxdebug->leave_sub();
 
1346   return $template_code;
 
1350   $main::lxdebug->enter_sub();
 
1352   my ($self, $myconfig) = @_;
 
1354   my $template_code = "";
 
1356   if ($self->{shipto_id}) {
 
1357     my $dbh = $self->get_standard_dbh($myconfig);
 
1358     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1359     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1360     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1363   $main::lxdebug->leave_sub();
 
1367   $main::lxdebug->enter_sub();
 
1369   my ($self, $dbh, $id, $module) = @_;
 
1373   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1374                        contact phone fax email)) {
 
1375     if ($self->{"shipto$item"}) {
 
1376       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1378     push(@values, $self->{"shipto${item}"});
 
1381     if ($self->{shipto_id}) {
 
1382       my $query = qq|UPDATE shipto set
 
1384                        shiptodepartment_1 = ?,
 
1385                        shiptodepartment_2 = ?,
 
1394                      WHERE shipto_id = ?|;
 
1395       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1397       my $query = qq|SELECT * FROM shipto
 
1398                      WHERE shiptoname = ? AND
 
1399                        shiptodepartment_1 = ? AND
 
1400                        shiptodepartment_2 = ? AND
 
1401                        shiptostreet = ? AND
 
1402                        shiptozipcode = ? AND
 
1404                        shiptocountry = ? AND
 
1405                        shiptocontact = ? AND
 
1409       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
 
1412           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1413                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1414                                  shiptocontact, shiptophone, shiptofax, shiptoemail, module)
 
1415              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1416         do_query($self, $dbh, $query, $id, @values, $module);
 
1421   $main::lxdebug->leave_sub();
 
1425   $main::lxdebug->enter_sub();
 
1427   my ($self, $dbh) = @_;
 
1429   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1430   ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1431   $self->{"employee_id"} *= 1;
 
1433   $main::lxdebug->leave_sub();
 
1437   $main::lxdebug->enter_sub();
 
1439   my ($self, $myconfig, $salesman_id) = @_;
 
1441   $main::lxdebug->leave_sub() and return unless $salesman_id;
 
1443   my $dbh = $self->get_standard_dbh($myconfig);
 
1446     selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
 
1450     my $user = new User($main::memberfile, $login);
 
1451     map({ $self->{"salesman_$_"} = $user->{$_}; }
 
1452         qw(address businessnumber co_ustid company duns email fax name
 
1454     $self->{salesman_login} = $login;
 
1456     $self->{salesman_name} = $login
 
1457       if ($self->{salesman_name} eq "");
 
1459     map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
 
1462   $main::lxdebug->leave_sub();
 
1466   $main::lxdebug->enter_sub();
 
1468   my ($self, $myconfig) = @_;
 
1470   my $dbh = $self->get_standard_dbh($myconfig);
 
1471   my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
 
1472   ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1474   $main::lxdebug->leave_sub();
 
1478   $main::lxdebug->enter_sub();
 
1480   my ($self, $dbh, $id, $key) = @_;
 
1482   $key = "all_contacts" unless ($key);
 
1485     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1486     qq|FROM contacts | .
 
1487     qq|WHERE cp_cv_id = ? | .
 
1488     qq|ORDER BY lower(cp_name)|;
 
1490   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1492   $main::lxdebug->leave_sub();
 
1496   $main::lxdebug->enter_sub();
 
1498   my ($self, $dbh, $key) = @_;
 
1500   my ($all, $old_id, $where, @values);
 
1502   if (ref($key) eq "HASH") {
 
1505     $key = "ALL_PROJECTS";
 
1507     foreach my $p (keys(%{$params})) {
 
1509         $all = $params->{$p};
 
1510       } elsif ($p eq "old_id") {
 
1511         $old_id = $params->{$p};
 
1512       } elsif ($p eq "key") {
 
1513         $key = $params->{$p};
 
1519     $where = "WHERE active ";
 
1521       if (ref($old_id) eq "ARRAY") {
 
1522         my @ids = grep({ $_ } @{$old_id});
 
1524           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1525           push(@values, @ids);
 
1528         $where .= " OR (id = ?) ";
 
1529         push(@values, $old_id);
 
1535     qq|SELECT id, projectnumber, description, active | .
 
1538     qq|ORDER BY lower(projectnumber)|;
 
1540   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
1542   $main::lxdebug->leave_sub();
 
1546   $main::lxdebug->enter_sub();
 
1548   my ($self, $dbh, $vc_id, $key) = @_;
 
1550   $key = "all_shipto" unless ($key);
 
1552   # get shipping addresses
 
1553   my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
 
1555   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
1557   $main::lxdebug->leave_sub();
 
1561   $main::lxdebug->enter_sub();
 
1563   my ($self, $dbh, $key) = @_;
 
1565   $key = "all_printers" unless ($key);
 
1567   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
1569   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1571   $main::lxdebug->leave_sub();
 
1575   $main::lxdebug->enter_sub();
 
1577   my ($self, $dbh, $params) = @_;
 
1579   $key = $params->{key};
 
1580   $key = "all_charts" unless ($key);
 
1582   my $transdate = quote_db_date($params->{transdate});
 
1585     qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
1587     qq|LEFT JOIN taxkeys tk ON | .
 
1588     qq|(tk.id = (SELECT id FROM taxkeys | .
 
1589     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
1590     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
1591     qq|ORDER BY c.accno|;
 
1593   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1595   $main::lxdebug->leave_sub();
 
1598 sub _get_taxcharts {
 
1599   $main::lxdebug->enter_sub();
 
1601   my ($self, $dbh, $key) = @_;
 
1603   $key = "all_taxcharts" unless ($key);
 
1605   my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
1607   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1609   $main::lxdebug->leave_sub();
 
1613   $main::lxdebug->enter_sub();
 
1615   my ($self, $dbh, $key) = @_;
 
1617   $key = "all_taxzones" unless ($key);
 
1619   my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
 
1621   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1623   $main::lxdebug->leave_sub();
 
1626 sub _get_employees {
 
1627   $main::lxdebug->enter_sub();
 
1629   my ($self, $dbh, $default_key, $key) = @_;
 
1631   $key = $default_key unless ($key);
 
1632   $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
 
1634   $main::lxdebug->leave_sub();
 
1637 sub _get_business_types {
 
1638   $main::lxdebug->enter_sub();
 
1640   my ($self, $dbh, $key) = @_;
 
1642   $key = "all_business_types" unless ($key);
 
1644     selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
 
1646   $main::lxdebug->leave_sub();
 
1649 sub _get_languages {
 
1650   $main::lxdebug->enter_sub();
 
1652   my ($self, $dbh, $key) = @_;
 
1654   $key = "all_languages" unless ($key);
 
1656   my $query = qq|SELECT * FROM language ORDER BY id|;
 
1658   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1660   $main::lxdebug->leave_sub();
 
1663 sub _get_dunning_configs {
 
1664   $main::lxdebug->enter_sub();
 
1666   my ($self, $dbh, $key) = @_;
 
1668   $key = "all_dunning_configs" unless ($key);
 
1670   my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
 
1672   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1674   $main::lxdebug->leave_sub();
 
1677 sub _get_currencies {
 
1678 $main::lxdebug->enter_sub();
 
1680   my ($self, $dbh, $key) = @_;
 
1682   $key = "all_currencies" unless ($key);
 
1684   my $query = qq|SELECT curr AS currency FROM defaults|;
 
1686   $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
 
1688   $main::lxdebug->leave_sub();
 
1692 $main::lxdebug->enter_sub();
 
1694   my ($self, $dbh, $key) = @_;
 
1696   $key = "all_payments" unless ($key);
 
1698   my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
 
1700   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1702   $main::lxdebug->leave_sub();
 
1705 sub _get_customers {
 
1706   $main::lxdebug->enter_sub();
 
1708   my ($self, $dbh, $key) = @_;
 
1710   $key = "all_customers" unless ($key);
 
1712   my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name|;
 
1714   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1716   $main::lxdebug->leave_sub();
 
1720   $main::lxdebug->enter_sub();
 
1722   my ($self, $dbh, $key) = @_;
 
1724   $key = "all_vendors" unless ($key);
 
1726   my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
 
1728   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1730   $main::lxdebug->leave_sub();
 
1733 sub _get_departments {
 
1734   $main::lxdebug->enter_sub();
 
1736   my ($self, $dbh, $key) = @_;
 
1738   $key = "all_departments" unless ($key);
 
1740   my $query = qq|SELECT * FROM department ORDER BY description|;
 
1742   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1744   $main::lxdebug->leave_sub();
 
1748   $main::lxdebug->enter_sub();
 
1753   my $dbh = $self->get_standard_dbh(\%main::myconfig);
 
1754   my ($sth, $query, $ref);
 
1756   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
1757   my $vc_id = $self->{"${vc}_id"};
 
1759   if ($params{"contacts"}) {
 
1760     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
1763   if ($params{"shipto"}) {
 
1764     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
1767   if ($params{"projects"} || $params{"all_projects"}) {
 
1768     $self->_get_projects($dbh, $params{"all_projects"} ?
 
1769                          $params{"all_projects"} : $params{"projects"},
 
1770                          $params{"all_projects"} ? 1 : 0);
 
1773   if ($params{"printers"}) {
 
1774     $self->_get_printers($dbh, $params{"printers"});
 
1777   if ($params{"languages"}) {
 
1778     $self->_get_languages($dbh, $params{"languages"});
 
1781   if ($params{"charts"}) {
 
1782     $self->_get_charts($dbh, $params{"charts"});
 
1785   if ($params{"taxcharts"}) {
 
1786     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
1789   if ($params{"taxzones"}) {
 
1790     $self->_get_taxzones($dbh, $params{"taxzones"});
 
1793   if ($params{"employees"}) {
 
1794     $self->_get_employees($dbh, "all_employees", $params{"employees"});
 
1797   if ($params{"salesmen"}) {
 
1798     $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
 
1801   if ($params{"business_types"}) {
 
1802     $self->_get_business_types($dbh, $params{"business_types"});
 
1805   if ($params{"dunning_configs"}) {
 
1806     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
 
1809   if($params{"currencies"}) {
 
1810     $self->_get_currencies($dbh, $params{"currencies"});
 
1813   if($params{"customers"}) {
 
1814     $self->_get_customers($dbh, $params{"customers"});
 
1817   if($params{"vendors"}) {
 
1818     $self->_get_vendors($dbh, $params{"vendors"});
 
1821   if($params{"payments"}) {
 
1822     $self->_get_payments($dbh, $params{"payments"});
 
1825   if($params{"departments"}) {
 
1826     $self->_get_departments($dbh, $params{"departments"});
 
1829   $main::lxdebug->leave_sub();
 
1832 # this sub gets the id and name from $table
 
1834   $main::lxdebug->enter_sub();
 
1836   my ($self, $myconfig, $table) = @_;
 
1838   # connect to database
 
1839   my $dbh = $self->get_standard_dbh($myconfig);
 
1841   $table = $table eq "customer" ? "customer" : "vendor";
 
1842   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
1844   my ($query, @values);
 
1846   if (!$self->{openinvoices}) {
 
1848     if ($self->{customernumber} ne "") {
 
1849       $where = qq|(vc.customernumber ILIKE ?)|;
 
1850       push(@values, '%' . $self->{customernumber} . '%');
 
1852       $where = qq|(vc.name ILIKE ?)|;
 
1853       push(@values, '%' . $self->{$table} . '%');
 
1857       qq~SELECT vc.id, vc.name,
 
1858            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1860          WHERE $where AND (NOT vc.obsolete)
 
1864       qq~SELECT DISTINCT vc.id, vc.name,
 
1865            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1867          JOIN $table vc ON (a.${table}_id = vc.id)
 
1868          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
1870     push(@values, '%' . $self->{$table} . '%');
 
1873   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
1875   $main::lxdebug->leave_sub();
 
1877   return scalar(@{ $self->{name_list} });
 
1880 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1883   $main::lxdebug->enter_sub();
 
1885   my ($self, $myconfig, $table, $module) = @_;
 
1888   my $dbh = $self->get_standard_dbh($myconfig);
 
1890   $table = $table eq "customer" ? "customer" : "vendor";
 
1892   my $query = qq|SELECT count(*) FROM $table|;
 
1893   my ($count) = selectrow_query($self, $dbh, $query);
 
1895   # build selection list
 
1896   if ($count < $myconfig->{vclimit}) {
 
1897     $query = qq|SELECT id, name, salesman_id
 
1898                 FROM $table WHERE NOT obsolete
 
1900     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
1904   $self->get_employee($dbh);
 
1906   # setup sales contacts
 
1907   $query = qq|SELECT e.id, e.name
 
1909               WHERE (e.sales = '1') AND (NOT e.id = ?)|;
 
1910   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
1913   push(@{ $self->{all_employees} },
 
1914        { id   => $self->{employee_id},
 
1915          name => $self->{employee} });
 
1917   # sort the whole thing
 
1918   @{ $self->{all_employees} } =
 
1919     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
1921   if ($module eq 'AR') {
 
1923     # prepare query for departments
 
1924     $query = qq|SELECT id, description
 
1927                 ORDER BY description|;
 
1930     $query = qq|SELECT id, description
 
1932                 ORDER BY description|;
 
1935   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
1938   $query = qq|SELECT id, description
 
1942   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
1945   $query = qq|SELECT printer_description, id
 
1947               ORDER BY printer_description|;
 
1949   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
1952   $query = qq|SELECT id, description
 
1956   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
1958   $main::lxdebug->leave_sub();
 
1961 sub language_payment {
 
1962   $main::lxdebug->enter_sub();
 
1964   my ($self, $myconfig) = @_;
 
1966   my $dbh = $self->get_standard_dbh($myconfig);
 
1968   my $query = qq|SELECT id, description
 
1972   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
1975   $query = qq|SELECT printer_description, id
 
1977               ORDER BY printer_description|;
 
1979   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
1982   $query = qq|SELECT id, description
 
1986   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
1988   # get buchungsgruppen
 
1989   $query = qq|SELECT id, description
 
1990               FROM buchungsgruppen|;
 
1992   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
1994   $main::lxdebug->leave_sub();
 
1997 # this is only used for reports
 
1998 sub all_departments {
 
1999   $main::lxdebug->enter_sub();
 
2001   my ($self, $myconfig, $table) = @_;
 
2003   my $dbh = $self->get_standard_dbh($myconfig);
 
2006   if ($table eq 'customer') {
 
2007     $where = "WHERE role = 'P' ";
 
2010   my $query = qq|SELECT id, description
 
2013                  ORDER BY description|;
 
2014   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2016   delete($self->{all_departments}) unless (@{ $self->{all_departments} });
 
2018   $main::lxdebug->leave_sub();
 
2022   $main::lxdebug->enter_sub();
 
2024   my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
 
2027   if ($table eq "customer") {
 
2036   $self->all_vc($myconfig, $table, $module);
 
2038   # get last customers or vendors
 
2039   my ($query, $sth, $ref);
 
2041   my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
 
2046     my $transdate = "current_date";
 
2047     if ($self->{transdate}) {
 
2048       $transdate = $dbh->quote($self->{transdate});
 
2051     # now get the account numbers
 
2052     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2053                 FROM chart c, taxkeys tk
 
2054                 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
2055                   (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
2058     $sth = $dbh->prepare($query);
 
2060     do_statement($self, $sth, $query, '%' . $module . '%');
 
2062     $self->{accounts} = "";
 
2063     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2065       foreach my $key (split(/:/, $ref->{link})) {
 
2066         if ($key =~ /$module/) {
 
2068           # cross reference for keys
 
2069           $xkeyref{ $ref->{accno} } = $key;
 
2071           push @{ $self->{"${module}_links"}{$key} },
 
2072             { accno       => $ref->{accno},
 
2073               description => $ref->{description},
 
2074               taxkey      => $ref->{taxkey_id},
 
2075               tax_id      => $ref->{tax_id} };
 
2077           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2083   # get taxkeys and description
 
2084   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
2085   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
2087   if (($module eq "AP") || ($module eq "AR")) {
 
2088     # get tax rates and description
 
2089     $query = qq|SELECT * FROM tax|;
 
2090     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
2096            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
2097            a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
 
2098            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
2099            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
2101            d.description AS department,
 
2104          JOIN $table c ON (a.${table}_id = c.id)
 
2105          LEFT JOIN employee e ON (e.id = a.employee_id)
 
2106          LEFT JOIN department d ON (d.id = a.department_id)
 
2108     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
2110     foreach $key (keys %$ref) {
 
2111       $self->{$key} = $ref->{$key};
 
2114     my $transdate = "current_date";
 
2115     if ($self->{transdate}) {
 
2116       $transdate = $dbh->quote($self->{transdate});
 
2119     # now get the account numbers
 
2120     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2122                 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
 
2124                   AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
2125                     OR c.link LIKE '%_tax%')
 
2128     $sth = $dbh->prepare($query);
 
2129     do_statement($self, $sth, $query, "%$module%");
 
2131     $self->{accounts} = "";
 
2132     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2134       foreach my $key (split(/:/, $ref->{link})) {
 
2135         if ($key =~ /$module/) {
 
2137           # cross reference for keys
 
2138           $xkeyref{ $ref->{accno} } = $key;
 
2140           push @{ $self->{"${module}_links"}{$key} },
 
2141             { accno       => $ref->{accno},
 
2142               description => $ref->{description},
 
2143               taxkey      => $ref->{taxkey_id},
 
2144               tax_id      => $ref->{tax_id} };
 
2146           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2152     # get amounts from individual entries
 
2155            c.accno, c.description,
 
2156            a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
 
2160          LEFT JOIN chart c ON (c.id = a.chart_id)
 
2161          LEFT JOIN project p ON (p.id = a.project_id)
 
2162          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
2163                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
2164                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
2165                                         THEN tk.chart_id = a.chart_id
 
2168                                        OR (c.link='%tax%')) AND
 
2169                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
2170          WHERE a.trans_id = ?
 
2171          AND a.fx_transaction = '0'
 
2172          ORDER BY a.oid, a.transdate|;
 
2173     $sth = $dbh->prepare($query);
 
2174     do_statement($self, $sth, $query, $self->{id});
 
2176     # get exchangerate for currency
 
2177     $self->{exchangerate} =
 
2178       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2181     # store amounts in {acc_trans}{$key} for multiple accounts
 
2182     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2183       $ref->{exchangerate} =
 
2184         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
2185       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2188       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2189         $ref->{amount} *= -1;
 
2191       $ref->{index} = $index;
 
2193       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2199            d.curr AS currencies, d.closedto, d.revtrans,
 
2200            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2201            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2203     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2204     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2211             current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
 
2212             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2213             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2215     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2216     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2218     if ($self->{"$self->{vc}_id"}) {
 
2220       # only setup currency
 
2221       ($self->{currency}) = split(/:/, $self->{currencies});
 
2225       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2227       # get exchangerate for currency
 
2228       $self->{exchangerate} =
 
2229         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2235   $main::lxdebug->leave_sub();
 
2239   $main::lxdebug->enter_sub();
 
2241   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2243   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
2244   $table = $table eq "customer" ? "customer" : "vendor";
 
2245   my $where = "1 = 1";
 
2247   if ($self->{type} =~ /_order/) {
 
2249     $where = "quotation = '0'";
 
2251   if ($self->{type} =~ /_quotation/) {
 
2253     $where = "quotation = '1'";
 
2256   my $query = qq|SELECT MAX(id) FROM $arap
 
2257                  WHERE $where AND ${table}_id > 0|;
 
2258   my ($trans_id) = selectrow_query($self, $dbh, $query);
 
2263          a.curr, a.${table}_id, a.department_id,
 
2264          d.description AS department,
 
2265          ct.name, current_date + ct.terms AS duedate
 
2267        LEFT JOIN $table ct ON (a.${table}_id = ct.id)
 
2268        LEFT JOIN department d ON (a.department_id = d.id)
 
2270   ($self->{currency},   $self->{"${table}_id"}, $self->{department_id},
 
2271    $self->{department}, $self->{$table},        $self->{duedate})
 
2272     = selectrow_query($self, $dbh, $query, $trans_id);
 
2274   $main::lxdebug->leave_sub();
 
2278   $main::lxdebug->enter_sub();
 
2280   my ($self, $myconfig, $thisdate, $days) = @_;
 
2282   my $dbh = $self->get_standard_dbh($myconfig);
 
2287     my $dateformat = $myconfig->{dateformat};
 
2288     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2289     $thisdate = $dbh->quote($thisdate);
 
2290     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2292     $query = qq|SELECT current_date AS thisdate|;
 
2295   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2297   $main::lxdebug->leave_sub();
 
2303   $main::lxdebug->enter_sub();
 
2305   my ($self, $string) = @_;
 
2307   if ($string !~ /%/) {
 
2308     $string = "%$string%";
 
2311   $string =~ s/\'/\'\'/g;
 
2313   $main::lxdebug->leave_sub();
 
2319   $main::lxdebug->enter_sub();
 
2321   my ($self, $flds, $new, $count, $numrows) = @_;
 
2325   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
2331   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2333     $j = $item->{ndx} - 1;
 
2334     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2338   for $i ($count + 1 .. $numrows) {
 
2339     map { delete $self->{"${_}_$i"} } @{$flds};
 
2342   $main::lxdebug->leave_sub();
 
2346   $main::lxdebug->enter_sub();
 
2348   my ($self, $myconfig) = @_;
 
2352   my $dbh = $self->dbconnect_noauto($myconfig);
 
2354   my $query = qq|DELETE FROM status
 
2355                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2356   my $sth = prepare_query($self, $dbh, $query);
 
2358   if ($self->{formname} =~ /(check|receipt)/) {
 
2359     for $i (1 .. $self->{rowcount}) {
 
2360       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
2363     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
2367   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2368   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2370   my %queued = split / /, $self->{queued};
 
2373   if ($self->{formname} =~ /(check|receipt)/) {
 
2375     # this is a check or receipt, add one entry for each lineitem
 
2376     my ($accno) = split /--/, $self->{account};
 
2377     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
2378                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
2379     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
2380     $sth = prepare_query($self, $dbh, $query);
 
2382     for $i (1 .. $self->{rowcount}) {
 
2383       if ($self->{"checked_$i"}) {
 
2384         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
2390     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2391                 VALUES (?, ?, ?, ?, ?)|;
 
2392     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
2393              $queued{$self->{formname}}, $self->{formname});
 
2399   $main::lxdebug->leave_sub();
 
2403   $main::lxdebug->enter_sub();
 
2405   my ($self, $dbh) = @_;
 
2407   my ($query, $printed, $emailed);
 
2409   my $formnames  = $self->{printed};
 
2410   my $emailforms = $self->{emailed};
 
2412   my $query = qq|DELETE FROM status
 
2413                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2414   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
2416   # this only applies to the forms
 
2417   # checks and receipts are posted when printed or queued
 
2419   if ($self->{queued}) {
 
2420     my %queued = split / /, $self->{queued};
 
2422     foreach my $formname (keys %queued) {
 
2423       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2424       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2426       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2427                   VALUES (?, ?, ?, ?, ?)|;
 
2428       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
2430       $formnames  =~ s/$self->{formname}//;
 
2431       $emailforms =~ s/$self->{formname}//;
 
2436   # save printed, emailed info
 
2437   $formnames  =~ s/^ +//g;
 
2438   $emailforms =~ s/^ +//g;
 
2441   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2442   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2444   foreach my $formname (keys %status) {
 
2445     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2446     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2448     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2449                 VALUES (?, ?, ?, ?)|;
 
2450     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
2453   $main::lxdebug->leave_sub();
 
2457 # $main::locale->text('SAVED')
 
2458 # $main::locale->text('DELETED')
 
2459 # $main::locale->text('ADDED')
 
2460 # $main::locale->text('PAYMENT POSTED')
 
2461 # $main::locale->text('POSTED')
 
2462 # $main::locale->text('POSTED AS NEW')
 
2463 # $main::locale->text('ELSE')
 
2464 # $main::locale->text('SAVED FOR DUNNING')
 
2465 # $main::locale->text('DUNNING STARTED')
 
2466 # $main::locale->text('PRINTED')
 
2467 # $main::locale->text('MAILED')
 
2468 # $main::locale->text('SCREENED')
 
2469 # $main::locale->text('CANCELED')
 
2470 # $main::locale->text('invoice')
 
2471 # $main::locale->text('proforma')
 
2472 # $main::locale->text('sales_order')
 
2473 # $main::locale->text('packing_list')
 
2474 # $main::locale->text('pick_list')
 
2475 # $main::locale->text('purchase_order')
 
2476 # $main::locale->text('bin_list')
 
2477 # $main::locale->text('sales_quotation')
 
2478 # $main::locale->text('request_quotation')
 
2481   $main::lxdebug->enter_sub();
 
2486   if(!exists $self->{employee_id}) {
 
2487     &get_employee($self, $dbh);
 
2491    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
 
2492    qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
 
2493   my @values = (conv_i($self->{id}), $self->{login},
 
2494                 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
 
2495   do_query($self, $dbh, $query, @values);
 
2497   $main::lxdebug->leave_sub();
 
2501   $main::lxdebug->enter_sub();
 
2503   my ($self, $dbh, $trans_id, $restriction, $order) = @_;
 
2504   my ($orderBy, $desc) = split(/\-\-/, $order);
 
2505   $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
 
2508   if ($trans_id ne "") {
 
2510       qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name, h.snumbers, h.trans_id AS id | .
 
2511       qq|FROM history_erp h | .
 
2512       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
2513       qq|WHERE trans_id = | . $trans_id
 
2514       . $restriction . qq| |
 
2517     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2519     $sth->execute() || $self->dberror("$query");
 
2521     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
2522       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
2523       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
2524       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
 
2525       $tempArray[$i++] = $hash_ref;
 
2527     $main::lxdebug->leave_sub() and return \@tempArray 
 
2528       if ($i > 0 && $tempArray[0] ne "");
 
2530   $main::lxdebug->leave_sub();
 
2534 sub update_defaults {
 
2535   $main::lxdebug->enter_sub();
 
2537   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
2540   if ($provided_dbh) {
 
2541     $dbh = $provided_dbh;
 
2543     $dbh = $self->dbconnect_noauto($myconfig);
 
2545   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2546   my $sth   = $dbh->prepare($query);
 
2548   $sth->execute || $self->dberror($query);
 
2549   my ($var) = $sth->fetchrow_array;
 
2552   $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
 
2555   $query = qq|UPDATE defaults SET $fld = ?|;
 
2556   do_query($self, $dbh, $query, $var);
 
2558   if (!$provided_dbh) {
 
2563   $main::lxdebug->leave_sub();
 
2568 sub update_business {
 
2569   $main::lxdebug->enter_sub();
 
2571   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
2574   if ($provided_dbh) {
 
2575     $dbh = $provided_dbh;
 
2577     $dbh = $self->dbconnect_noauto($myconfig);
 
2580     qq|SELECT customernumberinit FROM business
 
2581        WHERE id = ? FOR UPDATE|;
 
2582   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
2584   $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
 
2586   $query = qq|UPDATE business
 
2587               SET customernumberinit = ?
 
2589   do_query($self, $dbh, $query, $var, $business_id);
 
2591   if (!$provided_dbh) {
 
2596   $main::lxdebug->leave_sub();
 
2601 sub get_partsgroup {
 
2602   $main::lxdebug->enter_sub();
 
2604   my ($self, $myconfig, $p) = @_;
 
2606   my $dbh = $self->get_standard_dbh($myconfig);
 
2608   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2610                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
2613   if ($p->{searchitems} eq 'part') {
 
2614     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
2616   if ($p->{searchitems} eq 'service') {
 
2617     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
2619   if ($p->{searchitems} eq 'assembly') {
 
2620     $query .= qq|WHERE p.assembly = '1'|;
 
2622   if ($p->{searchitems} eq 'labor') {
 
2623     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
2626   $query .= qq|ORDER BY partsgroup|;
 
2629     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2630                 ORDER BY partsgroup|;
 
2633   if ($p->{language_code}) {
 
2634     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2635                   t.description AS translation
 
2637                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2638                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
2639                 ORDER BY translation|;
 
2640     @values = ($p->{language_code});
 
2643   $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
 
2645   $main::lxdebug->leave_sub();
 
2648 sub get_pricegroup {
 
2649   $main::lxdebug->enter_sub();
 
2651   my ($self, $myconfig, $p) = @_;
 
2653   my $dbh = $self->get_standard_dbh($myconfig);
 
2655   my $query = qq|SELECT p.id, p.pricegroup
 
2658   $query .= qq| ORDER BY pricegroup|;
 
2661     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2662                 ORDER BY pricegroup|;
 
2665   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
2667   $main::lxdebug->leave_sub();
 
2671 # usage $form->all_years($myconfig, [$dbh])
 
2672 # return list of all years where bookings found
 
2675   $main::lxdebug->enter_sub();
 
2677   my ($self, $myconfig, $dbh) = @_;
 
2679   $dbh ||= $self->get_standard_dbh($myconfig);
 
2682   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
2683                    (SELECT MAX(transdate) FROM acc_trans)|;
 
2684   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
2686   if ($myconfig->{dateformat} =~ /^yy/) {
 
2687     ($startdate) = split /\W/, $startdate;
 
2688     ($enddate) = split /\W/, $enddate;
 
2690     (@_) = split /\W/, $startdate;
 
2692     (@_) = split /\W/, $enddate;
 
2697   $startdate = substr($startdate,0,4);
 
2698   $enddate = substr($enddate,0,4);
 
2700   while ($enddate >= $startdate) {
 
2701     push @all_years, $enddate--;
 
2706   $main::lxdebug->leave_sub();