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 #======================================================================
 
  57     $standard_dbh->disconnect();
 
  63   $main::lxdebug->enter_sub(2);
 
  67   my @pairs = split(/&/, $input);
 
  70     my ($name, $value) = split(/=/, $_, 2);
 
  71     $in{$name} = unescape(undef, $value);
 
  74   $main::lxdebug->leave_sub(2);
 
  79 sub _request_to_hash {
 
  80   $main::lxdebug->enter_sub(2);
 
  84   if (!$ENV{'CONTENT_TYPE'}
 
  85       || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
 
  86     $main::lxdebug->leave_sub(2);
 
  87     return _input_to_hash($input);
 
  90   my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr);
 
  93   my $boundary = '--' . $1;
 
  95   foreach my $line (split m/\n/, $input) {
 
  96     last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
 
  98     if (($line eq $boundary) || ($line eq "$boundary\r")) {
 
  99       $params{$name} =~ s|\r?\n$|| if $name;
 
 101       undef $name, $filename;
 
 104       $content_type   = "text/plain";
 
 111     next unless $boundary_found;
 
 113     if (!$headers_done) {
 
 114       $line =~ s/[\r\n]*$//;
 
 121       if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
 
 122         if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
 
 124           substr $line, $-[0], $+[0] - $-[0], "";
 
 127         if ($line =~ m|name\s*=\s*"(.*?)"|i) {
 
 129           substr $line, $-[0], $+[0] - $-[0], "";
 
 133         $params{FILENAME} = $filename if ($filename);
 
 138       if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
 
 147     $params{$name} .= "${line}\n";
 
 150   $params{$name} =~ s|\r?\n$|| if $name;
 
 152   $main::lxdebug->leave_sub(2);
 
 157   $main::lxdebug->enter_sub();
 
 163   if ($LXDebug::watch_form) {
 
 164     require SL::Watchdog;
 
 165     tie %{ $self }, 'SL::Watchdog';
 
 168   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 170   if ($ENV{QUERY_STRING}) {
 
 171     $_ = $ENV{QUERY_STRING};
 
 178   my %parameters = _request_to_hash($_);
 
 179   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 181   $self->{action} = lc $self->{action};
 
 182   $self->{action} =~ s/( |-|,|\#)/_/g;
 
 184   $self->{version}   = "2.4.2";
 
 186   $main::lxdebug->leave_sub();
 
 192   $main::lxdebug->enter_sub();
 
 198   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 200   $main::lxdebug->leave_sub();
 
 204   $main::lxdebug->enter_sub(2);
 
 206   my ($self, $str) = @_;
 
 208   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 210   $main::lxdebug->leave_sub(2);
 
 216   $main::lxdebug->enter_sub(2);
 
 218   my ($self, $str) = @_;
 
 223   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 225   $main::lxdebug->leave_sub(2);
 
 231   my ($self, $str) = @_;
 
 233   if ($str && !ref($str)) {
 
 234     $str =~ s/\"/"/g;
 
 242   my ($self, $str) = @_;
 
 244   if ($str && !ref($str)) {
 
 245     $str =~ s/"/\"/g;
 
 253   $main::lxdebug->enter_sub(2);
 
 255   my ($self, $str) = @_;
 
 258     ('order' => ['"', '<', '>'],
 
 264   map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 266   $main::lxdebug->leave_sub(2);
 
 275     map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 277     for (sort keys %$self) {
 
 278       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 279       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 286   $main::lxdebug->enter_sub();
 
 288   $main::lxdebug->show_backtrace();
 
 290   my ($self, $msg) = @_;
 
 291   if ($ENV{HTTP_USER_AGENT}) {
 
 293     $self->show_generic_error($msg);
 
 297     if ($self->{error_function}) {
 
 298       &{ $self->{error_function} }($msg);
 
 304   $main::lxdebug->leave_sub();
 
 308   $main::lxdebug->enter_sub();
 
 310   my ($self, $msg) = @_;
 
 312   if ($ENV{HTTP_USER_AGENT}) {
 
 315     if (!$self->{header}) {
 
 328     if ($self->{info_function}) {
 
 329       &{ $self->{info_function} }($msg);
 
 335   $main::lxdebug->leave_sub();
 
 339   $main::lxdebug->enter_sub();
 
 341   my ($self, $str, $cols, $maxrows) = @_;
 
 345   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 347   $maxrows = $rows unless defined $maxrows;
 
 349   $main::lxdebug->leave_sub();
 
 351   return ($rows > $maxrows) ? $maxrows : $rows;
 
 355   $main::lxdebug->enter_sub();
 
 357   my ($self, $msg) = @_;
 
 359   $self->error("$msg\n" . $DBI::errstr);
 
 361   $main::lxdebug->leave_sub();
 
 365   $main::lxdebug->enter_sub();
 
 367   my ($self, $name, $msg) = @_;
 
 369   if ($self->{$name} =~ /^\s*$/) {
 
 372   $main::lxdebug->leave_sub();
 
 376   $main::lxdebug->enter_sub();
 
 378   my ($self, $extra_code) = @_;
 
 380   if ($self->{header}) {
 
 381     $main::lxdebug->leave_sub();
 
 385   my ($stylesheet, $favicon);
 
 387   if ($ENV{HTTP_USER_AGENT}) {
 
 389     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 391         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 395     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 397     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 399         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 403     my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 405     if ($self->{landscape}) {
 
 406       $pagelayout = qq|<style type="text/css">
 
 407                         \@page { size:landscape; }
 
 411     my $fokus = qq|  document.$self->{fokus}.focus();| if ($self->{"fokus"});
 
 415     if ($self->{jsscript} == 1) {
 
 418         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 419         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 420         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 421         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 428       ? "$self->{title} - $self->{titlebar}"
 
 431     foreach $item (@ { $self->{AJAX} }) {
 
 432       $ajax .= $item->show_javascript();
 
 434     print qq|Content-Type: text/html; charset=${db_charset};
 
 438   <title>$self->{titlebar}</title>
 
 442   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
 
 446   <script type="text/javascript">
 
 454   <meta name="robots" content="noindex,nofollow" />
 
 455   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 456   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 458   <script type="text/javascript" src="js/tabcontent.js">
 
 460   /***********************************************
 
 461   * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 462   * This notice MUST stay intact for legal use
 
 463   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 464   ***********************************************/
 
 475   $main::lxdebug->leave_sub();
 
 478 sub _prepare_html_template {
 
 479   $main::lxdebug->enter_sub();
 
 481   my ($self, $file, $additional_params) = @_;
 
 484   if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 485     $language = $main::language;
 
 487     $language = $main::myconfig{"countrycode"};
 
 489   $language = "de" unless ($language);
 
 491   if (-f "templates/webpages/${file}_${language}.html") {
 
 492     if ((-f ".developer") &&
 
 493         (-f "templates/webpages/${file}_master.html") &&
 
 494         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 495          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 496       my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 497         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 498       print(qq|<pre>$info</pre>|);
 
 502     $file = "templates/webpages/${file}_${language}.html";
 
 503   } elsif (-f "templates/webpages/${file}.html") {
 
 504     $file = "templates/webpages/${file}.html";
 
 506     my $info = "Web page template '${file}' not found.\n" .
 
 507       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 508     print(qq|<pre>$info</pre>|);
 
 512   if ($self->{"DEBUG"}) {
 
 513     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 516   if ($additional_params->{"DEBUG"}) {
 
 517     $additional_params->{"DEBUG"} =
 
 518       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 521   if (%main::myconfig) {
 
 522     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 523     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 524     $jsc_dateformat =~ s/d+/\%d/gi;
 
 525     $jsc_dateformat =~ s/m+/\%m/gi;
 
 526     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 527     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 530   $additional_params->{"conf_webdav"}                 = $main::webdav;
 
 531   $additional_params->{"conf_lizenzen"}               = $main::lizenzen;
 
 532   $additional_params->{"conf_latex_templates"}        = $main::latex;
 
 533   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 535   if (%main::debug_options) {
 
 536     map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
 
 539   $main::lxdebug->leave_sub();
 
 544 sub parse_html_template {
 
 545   $main::lxdebug->enter_sub();
 
 547   my ($self, $file, $additional_params) = @_;
 
 549   $additional_params ||= { };
 
 551   $file = $self->_prepare_html_template($file, $additional_params);
 
 553   my $template = HTML::Template->new("filename" => $file,
 
 554                                      "die_on_bad_params" => 0,
 
 556                                      "case_sensitive" => 1,
 
 557                                      "loop_context_vars" => 1,
 
 560   foreach my $key ($template->param()) {
 
 561     my $param = $additional_params->{$key} || $self->{$key};
 
 562     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
 
 563     $template->param($key => $param);
 
 566   my $output = $template->output();
 
 568   $output = $main::locale->{iconv}->convert($output) if ($main::locale);
 
 570   $main::lxdebug->leave_sub();
 
 575 sub parse_html_template2 {
 
 576   $main::lxdebug->enter_sub();
 
 578   my ($self, $file, $additional_params) = @_;
 
 580   $additional_params ||= { };
 
 582   $file = $self->_prepare_html_template($file, $additional_params);
 
 584   my $template = Template->new({ 'INTERPOLATE' => 0,
 
 590   map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
 
 593   $template->process($file, $additional_params, \$output);
 
 595   $output = $main::locale->{iconv}->convert($output) if ($main::locale);
 
 597   $main::lxdebug->leave_sub();
 
 602 sub show_generic_error {
 
 603   my ($self, $error, $title, $action) = @_;
 
 606   $add_params->{"title"} = $title if ($title);
 
 607   $self->{"label_error"} = $error;
 
 611     map({ delete($self->{$_}); } qw(action));
 
 612     map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
 
 613             if (!ref($self->{$_})); }
 
 615     $add_params->{"SHOW_BUTTON"} = 1;
 
 616     $add_params->{"BUTTON_LABEL"} = $action;
 
 618   $add_params->{"VARIABLES"} = \@vars;
 
 621   print($self->parse_html_template("generic/error", $add_params));
 
 623   die("Error: $error\n");
 
 626 sub show_generic_information {
 
 627   my ($self, $error, $title) = @_;
 
 630   $add_params->{"title"} = $title if ($title);
 
 631   $self->{"label_information"} = $error;
 
 634   print($self->parse_html_template("generic/information", $add_params));
 
 636   die("Information: $error\n");
 
 639 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 640 # changed it to accept an arbitrary number of triggers - sschoeling
 
 642   $main::lxdebug->enter_sub();
 
 645   my $myconfig = shift;
 
 648   # set dateform for jsscript
 
 651     "dd.mm.yy" => "%d.%m.%Y",
 
 652     "dd-mm-yy" => "%d-%m-%Y",
 
 653     "dd/mm/yy" => "%d/%m/%Y",
 
 654     "mm/dd/yy" => "%m/%d/%Y",
 
 655     "mm-dd-yy" => "%m-%d-%Y",
 
 656     "yyyy-mm-dd" => "%Y-%m-%d",
 
 659   my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
 
 660     $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
 
 667       inputField : "| . (shift) . qq|",
 
 668       ifFormat :"$ifFormat",
 
 669       align : "| .  (shift) . qq|",
 
 670       button : "| . (shift) . qq|"
 
 676        <script type="text/javascript">
 
 677        <!--| . join("", @triggers) . qq|//-->
 
 681   $main::lxdebug->leave_sub();
 
 684 }    #end sub write_trigger
 
 687   $main::lxdebug->enter_sub();
 
 689   my ($self, $msg) = @_;
 
 691   if ($self->{callback}) {
 
 693     ($script, $argv) = split(/\?/, $self->{callback});
 
 694     exec("perl", "$script", $argv);
 
 702   $main::lxdebug->leave_sub();
 
 705 # sort of columns removed - empty sub
 
 707   $main::lxdebug->enter_sub();
 
 709   my ($self, @columns) = @_;
 
 711   $main::lxdebug->leave_sub();
 
 717   $main::lxdebug->enter_sub(2);
 
 719   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 724   my $neg = ($amount =~ s/-//);
 
 726   if (defined($places) && ($places ne '')) {
 
 731       my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 732       $actual_places = length($actual_places);
 
 733       $places = $actual_places > $places ? $actual_places : $places;
 
 736     $amount = $self->round_amount($amount, $places);
 
 739   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 740   my @p = split(/\./, $amount); # split amount at decimal point
 
 742   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 745   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 748     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 749     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 750                         ($neg ? "-$amount"   : "$amount" )    ;
 
 754   $main::lxdebug->leave_sub(2);
 
 759   $main::lxdebug->enter_sub(2);
 
 761   my ($self, $myconfig, $amount) = @_;
 
 763   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 764       || ($myconfig->{numberformat} eq '1000,00')) {
 
 769   if ($myconfig->{numberformat} eq "1'000.00") {
 
 775   $main::lxdebug->leave_sub(2);
 
 777   return ($amount * 1);
 
 781   $main::lxdebug->enter_sub(2);
 
 783   my ($self, $amount, $places) = @_;
 
 786   # Rounding like "Kaufmannsrunden"
 
 787   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 789   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 792   $amount = $amount * (10**($places));
 
 793   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 795   $main::lxdebug->leave_sub(2);
 
 797   return $round_amount;
 
 802   $main::lxdebug->enter_sub();
 
 804   my ($self, $myconfig, $userspath) = @_;
 
 805   my ($template, $out);
 
 809   $self->{"cwd"} = getcwd();
 
 810   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
 812   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
 813     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 814   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
 815     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
 816     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 817   } elsif (($self->{"format"} =~ /html/i) ||
 
 818            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
 819     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 820   } elsif (($self->{"format"} =~ /xml/i) ||
 
 821              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
 822     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 823   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
 824     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 825   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
 826     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 827   } elsif ( defined $self->{'format'}) {
 
 828     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
 829   } elsif ( $self->{'format'} eq '' ) {
 
 830     $self->error("No Outputformat given: $self->{'format'}");
 
 831   } else { #Catch the rest
 
 832     $self->error("Outputformat not defined: $self->{'format'}");
 
 835   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 836   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 838   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 839       qw(email tel fax name signature company address businessnumber
 
 840          co_ustid taxnumber duns));
 
 841   map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
 
 842       qw(company address signature));
 
 843   map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
 
 845   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 847   # OUT is used for the media, screen, printer, email
 
 848   # for postscript we store a copy in a temporary file
 
 850   my $prepend_userspath;
 
 852   if (!$self->{tmpfile}) {
 
 853     $self->{tmpfile}   = "${fileid}.$self->{IN}";
 
 854     $prepend_userspath = 1;
 
 857   $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
 
 859   $self->{tmpfile} =~ s|.*/||;
 
 860   $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
 
 861   $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
 
 863   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 865     $self->{OUT} = ">$self->{tmpfile}";
 
 869     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 871     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 875   if (!$template->parse(*OUT)) {
 
 877     $self->error("$self->{IN} : " . $template->get_error());
 
 882   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 884     if ($self->{media} eq 'email') {
 
 886       my $mail = new Mailer;
 
 888       map { $mail->{$_} = $self->{$_} }
 
 889         qw(cc bcc subject message version format);
 
 890       $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 891       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
 892       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
 893       $mail->{fileid} = "$fileid.";
 
 894       $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
 896       # if we send html or plain text inline
 
 897       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
 898         $mail->{contenttype} = "text/html";
 
 900         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
 901         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
 902         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
 904         open(IN, $self->{tmpfile})
 
 905           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 907           $mail->{message} .= $_;
 
 914         if (!$self->{"do_not_attach"}) {
 
 915           @{ $mail->{attachments} } =
 
 916             ({ "filename" => $self->{"tmpfile"},
 
 917                "name" => $self->{"attachment_filename"} ?
 
 918                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
 921         $mail->{message}       =~ s/\r\n/\n/g;
 
 922         $myconfig->{signature} =~ s/\\n/\n/g;
 
 923         $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 927       my $err = $mail->send();
 
 928       $self->error($self->cleanup . "$err") if ($err);
 
 934       my $numbytes = (-s $self->{tmpfile});
 
 935       open(IN, $self->{tmpfile})
 
 936         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 938       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
 940       chdir("$self->{cwd}");
 
 941       #print(STDERR "Kopien $self->{copies}\n");
 
 942       #print(STDERR "OUT $self->{OUT}\n");
 
 943       for my $i (1 .. $self->{copies}) {
 
 945           open(OUT, $self->{OUT})
 
 946             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
 948           $self->{attachment_filename} = ($self->{attachment_filename}) 
 
 949                                        ? $self->{attachment_filename}
 
 950                                        : $self->generate_attachment_filename();
 
 953           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
 954 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
 955 Content-Length: $numbytes
 
 959           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
 979   chdir("$self->{cwd}");
 
 980   $main::lxdebug->leave_sub();
 
 983 sub generate_attachment_filename {
 
 986   my %formname_translations = (
 
 987      bin_list            => $main::locale->text('Bin List'),
 
 988      credit_note         => $main::locale->text('Credit Note'),
 
 989      invoice             => $main::locale->text('Invoice'),
 
 990      packing_list        => $main::locale->text('Packing List'),
 
 991      pick_list           => $main::locale->text('Pick List'),
 
 992      proforma            => $main::locale->text('Proforma Invoice'),
 
 993      purchase_order      => $main::locale->text('Purchase Order'),
 
 994      request_quotation   => $main::locale->text('RFQ'),
 
 995      sales_order         => $main::locale->text('Confirmation'),
 
 996      sales_quotation     => $main::locale->text('Quotation'),
 
 997      storno_invoice      => $main::locale->text('Storno Invoice'),
 
 998      storno_packing_list => $main::locale->text('Storno Packing List'),
 
1001   my $attachment_filename = $formname_translations{$self->{"formname"}};
 
1003       (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
 
1004     : ($self->{"type"} =~ /_quotation$/)                       ? "quo"
 
1007   if ($attachment_filename && $self->{"${prefix}number"}) {
 
1008     $attachment_filename .= "_" . $self->{"${prefix}number"}
 
1009                             . (  $self->{format} =~ /pdf/i          ? ".pdf"
 
1010                                : $self->{format} =~ /postscript/i   ? ".ps"
 
1011                                : $self->{format} =~ /opendocument/i ? ".odt"
 
1012                                : $self->{format} =~ /html/i         ? ".html"
 
1014     $attachment_filename =~ s/ /_/g;
 
1015     my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue", 
 
1016                     "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
 
1017     map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
 
1019     $attachment_filename = "";
 
1022   return $attachment_filename;
 
1026   $main::lxdebug->enter_sub();
 
1030   chdir("$self->{tmpdir}");
 
1033   if (-f "$self->{tmpfile}.err") {
 
1034     open(FH, "$self->{tmpfile}.err");
 
1039   if ($self->{tmpfile}) {
 
1040     $self->{tmpfile} =~ s|.*/||g;
 
1042     $self->{tmpfile} =~ s/\.\w+$//g;
 
1043     my $tmpfile = $self->{tmpfile};
 
1044     unlink(<$tmpfile.*>);
 
1047   chdir("$self->{cwd}");
 
1049   $main::lxdebug->leave_sub();
 
1055   $main::lxdebug->enter_sub();
 
1057   my ($self, $date, $myconfig) = @_;
 
1059   if ($date && $date =~ /\D/) {
 
1061     if ($myconfig->{dateformat} =~ /^yy/) {
 
1062       ($yy, $mm, $dd) = split /\D/, $date;
 
1064     if ($myconfig->{dateformat} =~ /^mm/) {
 
1065       ($mm, $dd, $yy) = split /\D/, $date;
 
1067     if ($myconfig->{dateformat} =~ /^dd/) {
 
1068       ($dd, $mm, $yy) = split /\D/, $date;
 
1073     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
1074     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
1076     $dd = "0$dd" if ($dd < 10);
 
1077     $mm = "0$mm" if ($mm < 10);
 
1079     $date = "$yy$mm$dd";
 
1082   $main::lxdebug->leave_sub();
 
1087 # Database routines used throughout
 
1090   $main::lxdebug->enter_sub(2);
 
1092   my ($self, $myconfig) = @_;
 
1094   # connect to database
 
1096     DBI->connect($myconfig->{dbconnect},
 
1097                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
1101   if ($myconfig->{dboptions}) {
 
1102     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1105   $main::lxdebug->leave_sub(2);
 
1110 sub dbconnect_noauto {
 
1111   $main::lxdebug->enter_sub();
 
1113   my ($self, $myconfig) = @_;
 
1115   # connect to database
 
1117     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
1118                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
1122   if ($myconfig->{dboptions}) {
 
1123     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1126   $main::lxdebug->leave_sub();
 
1131 sub get_standard_dbh {
 
1132   $main::lxdebug->enter_sub(2);
 
1134   my ($self, $myconfig) = @_;
 
1136   $standard_dbh ||= $self->dbconnect_noauto($myconfig);
 
1138   $main::lxdebug->leave_sub(2);
 
1140   return $standard_dbh;
 
1143 sub update_balance {
 
1144   $main::lxdebug->enter_sub();
 
1146   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
1148   # if we have a value, go do it
 
1151     # retrieve balance from table
 
1152     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1153     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1154     my ($balance) = $sth->fetchrow_array;
 
1160     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1161     do_query($self, $dbh, $query, @values);
 
1163   $main::lxdebug->leave_sub();
 
1166 sub update_exchangerate {
 
1167   $main::lxdebug->enter_sub();
 
1169   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1171   # some sanity check for currency
 
1173     $main::lxdebug->leave_sub();
 
1176   my $query = qq|SELECT curr FROM defaults|;
 
1178   my ($currency) = selectrow_query($self, $dbh, $query);
 
1179   my ($defaultcurrency) = split m/:/, $currency;
 
1182   if ($curr eq $defaultcurrency) {
 
1183     $main::lxdebug->leave_sub();
 
1187   my $query = qq|SELECT e.curr FROM exchangerate e
 
1188                  WHERE e.curr = ? AND e.transdate = ?
 
1190   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1199   $buy = conv_i($buy, "NULL");
 
1200   $sell = conv_i($sell, "NULL");
 
1203   if ($buy != 0 && $sell != 0) {
 
1204     $set = "buy = $buy, sell = $sell";
 
1205   } elsif ($buy != 0) {
 
1206     $set = "buy = $buy";
 
1207   } elsif ($sell != 0) {
 
1208     $set = "sell = $sell";
 
1211   if ($sth->fetchrow_array) {
 
1212     $query = qq|UPDATE exchangerate
 
1218     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1219                 VALUES (?, $buy, $sell, ?)|;
 
1222   do_query($self, $dbh, $query, $curr, $transdate);
 
1224   $main::lxdebug->leave_sub();
 
1227 sub save_exchangerate {
 
1228   $main::lxdebug->enter_sub();
 
1230   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1232   my $dbh = $self->dbconnect($myconfig);
 
1236   $buy  = $rate if $fld eq 'buy';
 
1237   $sell = $rate if $fld eq 'sell';
 
1240   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1245   $main::lxdebug->leave_sub();
 
1248 sub get_exchangerate {
 
1249   $main::lxdebug->enter_sub();
 
1251   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1253   unless ($transdate) {
 
1254     $main::lxdebug->leave_sub();
 
1258   my $query = qq|SELECT curr FROM defaults|;
 
1260   my ($currency) = selectrow_query($self, $dbh, $query);
 
1261   my ($defaultcurrency) = split m/:/, $currency;
 
1263   if ($currency eq $defaultcurrency) {
 
1264     $main::lxdebug->leave_sub();
 
1268   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1269                  WHERE e.curr = ? AND e.transdate = ?|;
 
1270   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1274   $main::lxdebug->leave_sub();
 
1276   return $exchangerate;
 
1279 sub check_exchangerate {
 
1280   $main::lxdebug->enter_sub();
 
1282   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1284   unless ($transdate) {
 
1285     $main::lxdebug->leave_sub();
 
1289   my ($defaultcurrency) = $self->get_default_currency($myconfig);
 
1291   if ($currency eq $defaultcurrency) {
 
1292     $main::lxdebug->leave_sub();
 
1296   my $dbh   = $self->get_standard_dbh($myconfig);
 
1297   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1298                  WHERE e.curr = ? AND e.transdate = ?|;
 
1300   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1302   $exchangerate = 1 if ($exchangerate eq "");
 
1304   $main::lxdebug->leave_sub();
 
1306   return $exchangerate;
 
1309 sub get_default_currency {
 
1310   $main::lxdebug->enter_sub();
 
1312   my ($self, $myconfig) = @_;
 
1313   my $dbh = $self->get_standard_dbh($myconfig);
 
1315   my $query = qq|SELECT curr FROM defaults|;
 
1317   my ($curr)            = selectrow_query($self, $dbh, $query);
 
1318   my ($defaultcurrency) = split m/:/, $curr;
 
1320   $main::lxdebug->leave_sub();
 
1322   return $defaultcurrency;
 
1326 sub set_payment_options {
 
1327   $main::lxdebug->enter_sub();
 
1329   my ($self, $myconfig, $transdate) = @_;
 
1331   return $main::lxdebug->leave_sub() unless ($self->{payment_id});
 
1333   my $dbh = $self->get_standard_dbh($myconfig);
 
1336     qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
 
1337     qq|FROM payment_terms p | .
 
1340   ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1341    $self->{payment_terms}) =
 
1342      selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1344   if ($transdate eq "") {
 
1345     if ($self->{invdate}) {
 
1346       $transdate = $self->{invdate};
 
1348       $transdate = $self->{transdate};
 
1353     qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1354     qq|FROM payment_terms|;
 
1355   ($self->{netto_date}, $self->{skonto_date}) =
 
1356     selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1358   my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
 
1359   my $skonto_amount = $self->parse_amount($myconfig, $total) *
 
1360     $self->{percent_skonto};
 
1362   $self->{skonto_amount} =
 
1363     $self->format_amount($myconfig, $skonto_amount, 2);
 
1365   if ($self->{"language_id"}) {
 
1367       qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1368       qq|FROM translation_payment_terms t | .
 
1369       qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1370       qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1371     my ($description_long, $output_numberformat, $output_dateformat,
 
1372       $output_longdates) =
 
1373       selectrow_query($self, $dbh, $query,
 
1374                       $self->{"language_id"}, $self->{"payment_id"});
 
1376     $self->{payment_terms} = $description_long if ($description_long);
 
1378     if ($output_dateformat) {
 
1379       foreach my $key (qw(netto_date skonto_date)) {
 
1381           $main::locale->reformat_date($myconfig, $self->{$key},
 
1387     if ($output_numberformat &&
 
1388         ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1389       my $saved_numberformat = $myconfig->{"numberformat"};
 
1390       $myconfig->{"numberformat"} = $output_numberformat;
 
1391       $self->{skonto_amount} =
 
1392         $self->format_amount($myconfig, $skonto_amount, 2);
 
1393       $myconfig->{"numberformat"} = $saved_numberformat;
 
1397   $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1398   $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1399   $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
 
1400   $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
 
1401   $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
 
1402   $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1403   $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1404   $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1405   $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1406   $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1408   $main::lxdebug->leave_sub();
 
1412 sub get_template_language {
 
1413   $main::lxdebug->enter_sub();
 
1415   my ($self, $myconfig) = @_;
 
1417   my $template_code = "";
 
1419   if ($self->{language_id}) {
 
1420     my $dbh = $self->get_standard_dbh($myconfig);
 
1421     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1422     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1425   $main::lxdebug->leave_sub();
 
1427   return $template_code;
 
1430 sub get_printer_code {
 
1431   $main::lxdebug->enter_sub();
 
1433   my ($self, $myconfig) = @_;
 
1435   my $template_code = "";
 
1437   if ($self->{printer_id}) {
 
1438     my $dbh = $self->get_standard_dbh($myconfig);
 
1439     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1440     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1443   $main::lxdebug->leave_sub();
 
1445   return $template_code;
 
1449   $main::lxdebug->enter_sub();
 
1451   my ($self, $myconfig) = @_;
 
1453   my $template_code = "";
 
1455   if ($self->{shipto_id}) {
 
1456     my $dbh = $self->get_standard_dbh($myconfig);
 
1457     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1458     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1459     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1462   $main::lxdebug->leave_sub();
 
1466   $main::lxdebug->enter_sub();
 
1468   my ($self, $dbh, $id, $module) = @_;
 
1473   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1474                        contact phone fax email)) {
 
1475     if ($self->{"shipto$item"}) {
 
1476       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1478     push(@values, $self->{"shipto${item}"});
 
1482     if ($self->{shipto_id}) {
 
1483       my $query = qq|UPDATE shipto set
 
1485                        shiptodepartment_1 = ?,
 
1486                        shiptodepartment_2 = ?,
 
1495                      WHERE shipto_id = ?|;
 
1496       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1498       my $query = qq|SELECT * FROM shipto
 
1499                      WHERE shiptoname = ? AND
 
1500                        shiptodepartment_1 = ? AND
 
1501                        shiptodepartment_2 = ? AND
 
1502                        shiptostreet = ? AND
 
1503                        shiptozipcode = ? AND
 
1505                        shiptocountry = ? AND
 
1506                        shiptocontact = ? AND
 
1512       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
 
1515           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1516                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1517                                  shiptocontact, shiptophone, shiptofax, shiptoemail, module)
 
1518              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1519         do_query($self, $dbh, $query, $id, @values, $module);
 
1524   $main::lxdebug->leave_sub();
 
1528   $main::lxdebug->enter_sub();
 
1530   my ($self, $dbh) = @_;
 
1532   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1533   ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1534   $self->{"employee_id"} *= 1;
 
1536   $main::lxdebug->leave_sub();
 
1540   $main::lxdebug->enter_sub();
 
1542   my ($self, $myconfig, $salesman_id) = @_;
 
1544   $main::lxdebug->leave_sub() and return unless $salesman_id;
 
1546   my $dbh = $self->get_standard_dbh($myconfig);
 
1549     selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
 
1553     my $user = new User($main::memberfile, $login);
 
1554     map({ $self->{"salesman_$_"} = $user->{$_}; }
 
1555         qw(address businessnumber co_ustid company duns email fax name
 
1557     $self->{salesman_login} = $login;
 
1559     $self->{salesman_name} = $login
 
1560       if ($self->{salesman_name} eq "");
 
1562     map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
 
1565   $main::lxdebug->leave_sub();
 
1569   $main::lxdebug->enter_sub();
 
1571   my ($self, $myconfig) = @_;
 
1573   my $dbh = $self->get_standard_dbh($myconfig);
 
1574   my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
 
1575   ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1577   $main::lxdebug->leave_sub();
 
1581   $main::lxdebug->enter_sub();
 
1583   my ($self, $dbh, $id, $key) = @_;
 
1585   $key = "all_contacts" unless ($key);
 
1588     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1589     qq|FROM contacts | .
 
1590     qq|WHERE cp_cv_id = ? | .
 
1591     qq|ORDER BY lower(cp_name)|;
 
1593   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1595   $main::lxdebug->leave_sub();
 
1599   $main::lxdebug->enter_sub();
 
1601   my ($self, $dbh, $key) = @_;
 
1603   my ($all, $old_id, $where, @values);
 
1605   if (ref($key) eq "HASH") {
 
1608     $key = "ALL_PROJECTS";
 
1610     foreach my $p (keys(%{$params})) {
 
1612         $all = $params->{$p};
 
1613       } elsif ($p eq "old_id") {
 
1614         $old_id = $params->{$p};
 
1615       } elsif ($p eq "key") {
 
1616         $key = $params->{$p};
 
1622     $where = "WHERE active ";
 
1624       if (ref($old_id) eq "ARRAY") {
 
1625         my @ids = grep({ $_ } @{$old_id});
 
1627           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1628           push(@values, @ids);
 
1631         $where .= " OR (id = ?) ";
 
1632         push(@values, $old_id);
 
1638     qq|SELECT id, projectnumber, description, active | .
 
1641     qq|ORDER BY lower(projectnumber)|;
 
1643   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
1645   $main::lxdebug->leave_sub();
 
1649   $main::lxdebug->enter_sub();
 
1651   my ($self, $dbh, $vc_id, $key) = @_;
 
1653   $key = "all_shipto" unless ($key);
 
1655   # get shipping addresses
 
1656   my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
 
1658   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
1660   $main::lxdebug->leave_sub();
 
1664   $main::lxdebug->enter_sub();
 
1666   my ($self, $dbh, $key) = @_;
 
1668   $key = "all_printers" unless ($key);
 
1670   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
1672   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1674   $main::lxdebug->leave_sub();
 
1678   $main::lxdebug->enter_sub();
 
1680   my ($self, $dbh, $params) = @_;
 
1682   $key = $params->{key};
 
1683   $key = "all_charts" unless ($key);
 
1685   my $transdate = quote_db_date($params->{transdate});
 
1688     qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
1690     qq|LEFT JOIN taxkeys tk ON | .
 
1691     qq|(tk.id = (SELECT id FROM taxkeys | .
 
1692     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
1693     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
1694     qq|ORDER BY c.accno|;
 
1696   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1698   $main::lxdebug->leave_sub();
 
1701 sub _get_taxcharts {
 
1702   $main::lxdebug->enter_sub();
 
1704   my ($self, $dbh, $key) = @_;
 
1706   $key = "all_taxcharts" unless ($key);
 
1708   my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
1710   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1712   $main::lxdebug->leave_sub();
 
1716   $main::lxdebug->enter_sub();
 
1718   my ($self, $dbh, $key) = @_;
 
1720   $key = "all_taxzones" unless ($key);
 
1722   my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
 
1724   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1726   $main::lxdebug->leave_sub();
 
1729 sub _get_employees {
 
1730   $main::lxdebug->enter_sub();
 
1732   my ($self, $dbh, $default_key, $key) = @_;
 
1734   $key = $default_key unless ($key);
 
1735   $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY name|);
 
1737   $main::lxdebug->leave_sub();
 
1740 sub _get_business_types {
 
1741   $main::lxdebug->enter_sub();
 
1743   my ($self, $dbh, $key) = @_;
 
1745   $key = "all_business_types" unless ($key);
 
1747     selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
 
1749   $main::lxdebug->leave_sub();
 
1752 sub _get_languages {
 
1753   $main::lxdebug->enter_sub();
 
1755   my ($self, $dbh, $key) = @_;
 
1757   $key = "all_languages" unless ($key);
 
1759   my $query = qq|SELECT * FROM language ORDER BY id|;
 
1761   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1763   $main::lxdebug->leave_sub();
 
1766 sub _get_dunning_configs {
 
1767   $main::lxdebug->enter_sub();
 
1769   my ($self, $dbh, $key) = @_;
 
1771   $key = "all_dunning_configs" unless ($key);
 
1773   my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
 
1775   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1777   $main::lxdebug->leave_sub();
 
1780 sub _get_currencies {
 
1781 $main::lxdebug->enter_sub();
 
1783   my ($self, $dbh, $key) = @_;
 
1785   $key = "all_currencies" unless ($key);
 
1787   my $query = qq|SELECT curr AS currency FROM defaults|;
 
1789   $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
 
1791   $main::lxdebug->leave_sub();
 
1795 $main::lxdebug->enter_sub();
 
1797   my ($self, $dbh, $key) = @_;
 
1799   $key = "all_payments" unless ($key);
 
1801   my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
 
1803   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1805   $main::lxdebug->leave_sub();
 
1808 sub _get_customers {
 
1809   $main::lxdebug->enter_sub();
 
1811   my ($self, $dbh, $key) = @_;
 
1813   $key = "all_customers" unless ($key);
 
1815   my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name|;
 
1817   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1819   $main::lxdebug->leave_sub();
 
1823   $main::lxdebug->enter_sub();
 
1825   my ($self, $dbh, $key) = @_;
 
1827   $key = "all_vendors" unless ($key);
 
1829   my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
 
1831   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1833   $main::lxdebug->leave_sub();
 
1836 sub _get_departments {
 
1837   $main::lxdebug->enter_sub();
 
1839   my ($self, $dbh, $key) = @_;
 
1841   $key = "all_departments" unless ($key);
 
1843   my $query = qq|SELECT * FROM department ORDER BY description|;
 
1845   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1847   $main::lxdebug->leave_sub();
 
1851   $main::lxdebug->enter_sub();
 
1856   my $dbh = $self->get_standard_dbh(\%main::myconfig);
 
1857   my ($sth, $query, $ref);
 
1859   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
1860   my $vc_id = $self->{"${vc}_id"};
 
1862   if ($params{"contacts"}) {
 
1863     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
1866   if ($params{"shipto"}) {
 
1867     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
1870   if ($params{"projects"} || $params{"all_projects"}) {
 
1871     $self->_get_projects($dbh, $params{"all_projects"} ?
 
1872                          $params{"all_projects"} : $params{"projects"},
 
1873                          $params{"all_projects"} ? 1 : 0);
 
1876   if ($params{"printers"}) {
 
1877     $self->_get_printers($dbh, $params{"printers"});
 
1880   if ($params{"languages"}) {
 
1881     $self->_get_languages($dbh, $params{"languages"});
 
1884   if ($params{"charts"}) {
 
1885     $self->_get_charts($dbh, $params{"charts"});
 
1888   if ($params{"taxcharts"}) {
 
1889     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
1892   if ($params{"taxzones"}) {
 
1893     $self->_get_taxzones($dbh, $params{"taxzones"});
 
1896   if ($params{"employees"}) {
 
1897     $self->_get_employees($dbh, "all_employees", $params{"employees"});
 
1900   if ($params{"salesmen"}) {
 
1901     $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
 
1904   if ($params{"business_types"}) {
 
1905     $self->_get_business_types($dbh, $params{"business_types"});
 
1908   if ($params{"dunning_configs"}) {
 
1909     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
 
1912   if($params{"currencies"}) {
 
1913     $self->_get_currencies($dbh, $params{"currencies"});
 
1916   if($params{"customers"}) {
 
1917     $self->_get_customers($dbh, $params{"customers"});
 
1920   if($params{"vendors"}) {
 
1921     $self->_get_vendors($dbh, $params{"vendors"});
 
1924   if($params{"payments"}) {
 
1925     $self->_get_payments($dbh, $params{"payments"});
 
1928   if($params{"departments"}) {
 
1929     $self->_get_departments($dbh, $params{"departments"});
 
1932   $main::lxdebug->leave_sub();
 
1935 # this sub gets the id and name from $table
 
1937   $main::lxdebug->enter_sub();
 
1939   my ($self, $myconfig, $table) = @_;
 
1941   # connect to database
 
1942   my $dbh = $self->get_standard_dbh($myconfig);
 
1944   $table = $table eq "customer" ? "customer" : "vendor";
 
1945   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
1947   my ($query, @values);
 
1949   if (!$self->{openinvoices}) {
 
1951     if ($self->{customernumber} ne "") {
 
1952       $where = qq|(vc.customernumber ILIKE ?)|;
 
1953       push(@values, '%' . $self->{customernumber} . '%');
 
1955       $where = qq|(vc.name ILIKE ?)|;
 
1956       push(@values, '%' . $self->{$table} . '%');
 
1960       qq~SELECT vc.id, vc.name,
 
1961            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1963          WHERE $where AND (NOT vc.obsolete)
 
1967       qq~SELECT DISTINCT vc.id, vc.name,
 
1968            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1970          JOIN $table vc ON (a.${table}_id = vc.id)
 
1971          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
1973     push(@values, '%' . $self->{$table} . '%');
 
1976   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
1978   $main::lxdebug->leave_sub();
 
1980   return scalar(@{ $self->{name_list} });
 
1983 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1986   $main::lxdebug->enter_sub();
 
1988   my ($self, $myconfig, $table, $module) = @_;
 
1991   my $dbh = $self->get_standard_dbh($myconfig);
 
1993   $table = $table eq "customer" ? "customer" : "vendor";
 
1995   my $query = qq|SELECT count(*) FROM $table|;
 
1996   my ($count) = selectrow_query($self, $dbh, $query);
 
1998   # build selection list
 
1999   if ($count < $myconfig->{vclimit}) {
 
2000     $query = qq|SELECT id, name, salesman_id
 
2001                 FROM $table WHERE NOT obsolete
 
2003     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
2007   $self->get_employee($dbh);
 
2009   # setup sales contacts
 
2010   $query = qq|SELECT e.id, e.name
 
2012               WHERE (e.sales = '1') AND (NOT e.id = ?)|;
 
2013   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
2016   push(@{ $self->{all_employees} },
 
2017        { id   => $self->{employee_id},
 
2018          name => $self->{employee} });
 
2020   # sort the whole thing
 
2021   @{ $self->{all_employees} } =
 
2022     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
2024   if ($module eq 'AR') {
 
2026     # prepare query for departments
 
2027     $query = qq|SELECT id, description
 
2030                 ORDER BY description|;
 
2033     $query = qq|SELECT id, description
 
2035                 ORDER BY description|;
 
2038   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2041   $query = qq|SELECT id, description
 
2045   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2048   $query = qq|SELECT printer_description, id
 
2050               ORDER BY printer_description|;
 
2052   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2055   $query = qq|SELECT id, description
 
2059   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2061   $main::lxdebug->leave_sub();
 
2064 sub language_payment {
 
2065   $main::lxdebug->enter_sub();
 
2067   my ($self, $myconfig) = @_;
 
2069   my $dbh = $self->get_standard_dbh($myconfig);
 
2071   my $query = qq|SELECT id, description
 
2075   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2078   $query = qq|SELECT printer_description, id
 
2080               ORDER BY printer_description|;
 
2082   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2085   $query = qq|SELECT id, description
 
2089   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2091   # get buchungsgruppen
 
2092   $query = qq|SELECT id, description
 
2093               FROM buchungsgruppen|;
 
2095   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
2097   $main::lxdebug->leave_sub();
 
2100 # this is only used for reports
 
2101 sub all_departments {
 
2102   $main::lxdebug->enter_sub();
 
2104   my ($self, $myconfig, $table) = @_;
 
2106   my $dbh = $self->get_standard_dbh($myconfig);
 
2109   if ($table eq 'customer') {
 
2110     $where = "WHERE role = 'P' ";
 
2113   my $query = qq|SELECT id, description
 
2116                  ORDER BY description|;
 
2117   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2119   delete($self->{all_departments}) unless (@{ $self->{all_departments} });
 
2121   $main::lxdebug->leave_sub();
 
2125   $main::lxdebug->enter_sub();
 
2127   my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
 
2130   if ($table eq "customer") {
 
2139   $self->all_vc($myconfig, $table, $module);
 
2141   # get last customers or vendors
 
2142   my ($query, $sth, $ref);
 
2144   my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
 
2149     my $transdate = "current_date";
 
2150     if ($self->{transdate}) {
 
2151       $transdate = $dbh->quote($self->{transdate});
 
2154     # now get the account numbers
 
2155     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2156                 FROM chart c, taxkeys tk
 
2157                 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
2158                   (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
2161     $sth = $dbh->prepare($query);
 
2163     do_statement($self, $sth, $query, '%' . $module . '%');
 
2165     $self->{accounts} = "";
 
2166     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2168       foreach my $key (split(/:/, $ref->{link})) {
 
2169         if ($key =~ /$module/) {
 
2171           # cross reference for keys
 
2172           $xkeyref{ $ref->{accno} } = $key;
 
2174           push @{ $self->{"${module}_links"}{$key} },
 
2175             { accno       => $ref->{accno},
 
2176               description => $ref->{description},
 
2177               taxkey      => $ref->{taxkey_id},
 
2178               tax_id      => $ref->{tax_id} };
 
2180           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2186   # get taxkeys and description
 
2187   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
2188   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
2190   if (($module eq "AP") || ($module eq "AR")) {
 
2191     # get tax rates and description
 
2192     $query = qq|SELECT * FROM tax|;
 
2193     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
2199            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
2200            a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
 
2201            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
2202            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
2204            d.description AS department,
 
2207          JOIN $table c ON (a.${table}_id = c.id)
 
2208          LEFT JOIN employee e ON (e.id = a.employee_id)
 
2209          LEFT JOIN department d ON (d.id = a.department_id)
 
2211     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
2213     foreach $key (keys %$ref) {
 
2214       $self->{$key} = $ref->{$key};
 
2217     my $transdate = "current_date";
 
2218     if ($self->{transdate}) {
 
2219       $transdate = $dbh->quote($self->{transdate});
 
2222     # now get the account numbers
 
2223     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2225                 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
 
2227                   AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
2228                     OR c.link LIKE '%_tax%')
 
2231     $sth = $dbh->prepare($query);
 
2232     do_statement($self, $sth, $query, "%$module%");
 
2234     $self->{accounts} = "";
 
2235     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2237       foreach my $key (split(/:/, $ref->{link})) {
 
2238         if ($key =~ /$module/) {
 
2240           # cross reference for keys
 
2241           $xkeyref{ $ref->{accno} } = $key;
 
2243           push @{ $self->{"${module}_links"}{$key} },
 
2244             { accno       => $ref->{accno},
 
2245               description => $ref->{description},
 
2246               taxkey      => $ref->{taxkey_id},
 
2247               tax_id      => $ref->{tax_id} };
 
2249           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2255     # get amounts from individual entries
 
2258            c.accno, c.description,
 
2259            a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
 
2263          LEFT JOIN chart c ON (c.id = a.chart_id)
 
2264          LEFT JOIN project p ON (p.id = a.project_id)
 
2265          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
2266                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
2267                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
2268                                         THEN tk.chart_id = a.chart_id
 
2271                                        OR (c.link='%tax%')) AND
 
2272                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
2273          WHERE a.trans_id = ?
 
2274          AND a.fx_transaction = '0'
 
2275          ORDER BY a.oid, a.transdate|;
 
2276     $sth = $dbh->prepare($query);
 
2277     do_statement($self, $sth, $query, $self->{id});
 
2279     # get exchangerate for currency
 
2280     $self->{exchangerate} =
 
2281       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2284     # store amounts in {acc_trans}{$key} for multiple accounts
 
2285     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2286       $ref->{exchangerate} =
 
2287         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
2288       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2291       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2292         $ref->{amount} *= -1;
 
2294       $ref->{index} = $index;
 
2296       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2302            d.curr AS currencies, d.closedto, d.revtrans,
 
2303            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2304            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2306     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2307     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2314             current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
 
2315             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2316             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2318     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2319     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2321     if ($self->{"$self->{vc}_id"}) {
 
2323       # only setup currency
 
2324       ($self->{currency}) = split(/:/, $self->{currencies});
 
2328       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2330       # get exchangerate for currency
 
2331       $self->{exchangerate} =
 
2332         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2338   $main::lxdebug->leave_sub();
 
2342   $main::lxdebug->enter_sub();
 
2344   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2346   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
2347   $table = $table eq "customer" ? "customer" : "vendor";
 
2348   my $where = "1 = 1";
 
2350   if ($self->{type} =~ /_order/) {
 
2352     $where = "quotation = '0'";
 
2354   if ($self->{type} =~ /_quotation/) {
 
2356     $where = "quotation = '1'";
 
2359   my $query = qq|SELECT MAX(id) FROM $arap
 
2360                  WHERE $where AND ${table}_id > 0|;
 
2361   my ($trans_id) = selectrow_query($self, $dbh, $query);
 
2366          a.curr, a.${table}_id, a.department_id,
 
2367          d.description AS department,
 
2368          ct.name, current_date + ct.terms AS duedate
 
2370        LEFT JOIN $table ct ON (a.${table}_id = ct.id)
 
2371        LEFT JOIN department d ON (a.department_id = d.id)
 
2373   ($self->{currency},   $self->{"${table}_id"}, $self->{department_id},
 
2374    $self->{department}, $self->{$table},        $self->{duedate})
 
2375     = selectrow_query($self, $dbh, $query, $trans_id);
 
2377   $main::lxdebug->leave_sub();
 
2381   $main::lxdebug->enter_sub();
 
2383   my ($self, $myconfig, $thisdate, $days) = @_;
 
2385   my $dbh = $self->get_standard_dbh($myconfig);
 
2390     my $dateformat = $myconfig->{dateformat};
 
2391     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2392     $thisdate = $dbh->quote($thisdate);
 
2393     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2395     $query = qq|SELECT current_date AS thisdate|;
 
2398   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2400   $main::lxdebug->leave_sub();
 
2406   $main::lxdebug->enter_sub();
 
2408   my ($self, $string) = @_;
 
2410   if ($string !~ /%/) {
 
2411     $string = "%$string%";
 
2414   $string =~ s/\'/\'\'/g;
 
2416   $main::lxdebug->leave_sub();
 
2422   $main::lxdebug->enter_sub();
 
2424   my ($self, $flds, $new, $count, $numrows) = @_;
 
2428   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
2434   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2436     $j = $item->{ndx} - 1;
 
2437     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2441   for $i ($count + 1 .. $numrows) {
 
2442     map { delete $self->{"${_}_$i"} } @{$flds};
 
2445   $main::lxdebug->leave_sub();
 
2449   $main::lxdebug->enter_sub();
 
2451   my ($self, $myconfig) = @_;
 
2455   my $dbh = $self->dbconnect_noauto($myconfig);
 
2457   my $query = qq|DELETE FROM status
 
2458                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2459   my $sth = prepare_query($self, $dbh, $query);
 
2461   if ($self->{formname} =~ /(check|receipt)/) {
 
2462     for $i (1 .. $self->{rowcount}) {
 
2463       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
2466     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
2470   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2471   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2473   my %queued = split / /, $self->{queued};
 
2476   if ($self->{formname} =~ /(check|receipt)/) {
 
2478     # this is a check or receipt, add one entry for each lineitem
 
2479     my ($accno) = split /--/, $self->{account};
 
2480     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
2481                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
2482     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
2483     $sth = prepare_query($self, $dbh, $query);
 
2485     for $i (1 .. $self->{rowcount}) {
 
2486       if ($self->{"checked_$i"}) {
 
2487         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
2493     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2494                 VALUES (?, ?, ?, ?, ?)|;
 
2495     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
2496              $queued{$self->{formname}}, $self->{formname});
 
2502   $main::lxdebug->leave_sub();
 
2506   $main::lxdebug->enter_sub();
 
2508   my ($self, $dbh) = @_;
 
2510   my ($query, $printed, $emailed);
 
2512   my $formnames  = $self->{printed};
 
2513   my $emailforms = $self->{emailed};
 
2515   my $query = qq|DELETE FROM status
 
2516                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2517   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
2519   # this only applies to the forms
 
2520   # checks and receipts are posted when printed or queued
 
2522   if ($self->{queued}) {
 
2523     my %queued = split / /, $self->{queued};
 
2525     foreach my $formname (keys %queued) {
 
2526       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2527       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2529       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2530                   VALUES (?, ?, ?, ?, ?)|;
 
2531       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
2533       $formnames  =~ s/$self->{formname}//;
 
2534       $emailforms =~ s/$self->{formname}//;
 
2539   # save printed, emailed info
 
2540   $formnames  =~ s/^ +//g;
 
2541   $emailforms =~ s/^ +//g;
 
2544   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2545   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2547   foreach my $formname (keys %status) {
 
2548     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2549     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2551     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2552                 VALUES (?, ?, ?, ?)|;
 
2553     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
2556   $main::lxdebug->leave_sub();
 
2560 # $main::locale->text('SAVED')
 
2561 # $main::locale->text('DELETED')
 
2562 # $main::locale->text('ADDED')
 
2563 # $main::locale->text('PAYMENT POSTED')
 
2564 # $main::locale->text('POSTED')
 
2565 # $main::locale->text('POSTED AS NEW')
 
2566 # $main::locale->text('ELSE')
 
2567 # $main::locale->text('SAVED FOR DUNNING')
 
2568 # $main::locale->text('DUNNING STARTED')
 
2569 # $main::locale->text('PRINTED')
 
2570 # $main::locale->text('MAILED')
 
2571 # $main::locale->text('SCREENED')
 
2572 # $main::locale->text('CANCELED')
 
2573 # $main::locale->text('invoice')
 
2574 # $main::locale->text('proforma')
 
2575 # $main::locale->text('sales_order')
 
2576 # $main::locale->text('packing_list')
 
2577 # $main::locale->text('pick_list')
 
2578 # $main::locale->text('purchase_order')
 
2579 # $main::locale->text('bin_list')
 
2580 # $main::locale->text('sales_quotation')
 
2581 # $main::locale->text('request_quotation')
 
2584   $main::lxdebug->enter_sub();
 
2589   if(!exists $self->{employee_id}) {
 
2590     &get_employee($self, $dbh);
 
2594    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
 
2595    qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
 
2596   my @values = (conv_i($self->{id}), $self->{login},
 
2597                 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
 
2598   do_query($self, $dbh, $query, @values);
 
2600   $main::lxdebug->leave_sub();
 
2604   $main::lxdebug->enter_sub();
 
2606   my ($self, $dbh, $trans_id, $restriction, $order) = @_;
 
2607   my ($orderBy, $desc) = split(/\-\-/, $order);
 
2608   $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
 
2611   if ($trans_id ne "") {
 
2613       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 | .
 
2614       qq|FROM history_erp h | .
 
2615       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
2616       qq|WHERE trans_id = | . $trans_id
 
2617       . $restriction . qq| |
 
2620     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2622     $sth->execute() || $self->dberror("$query");
 
2624     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
2625       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
2626       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
2627       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
 
2628       $tempArray[$i++] = $hash_ref;
 
2630     $main::lxdebug->leave_sub() and return \@tempArray 
 
2631       if ($i > 0 && $tempArray[0] ne "");
 
2633   $main::lxdebug->leave_sub();
 
2637 sub update_defaults {
 
2638   $main::lxdebug->enter_sub();
 
2640   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
2643   if ($provided_dbh) {
 
2644     $dbh = $provided_dbh;
 
2646     $dbh = $self->dbconnect_noauto($myconfig);
 
2648   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2649   my $sth   = $dbh->prepare($query);
 
2651   $sth->execute || $self->dberror($query);
 
2652   my ($var) = $sth->fetchrow_array;
 
2655   if ($var =~ m/\d+$/) {
 
2656     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
2657     my $len_diff = length($var) - $-[0] - length($new_var);
 
2658     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
2664   $query = qq|UPDATE defaults SET $fld = ?|;
 
2665   do_query($self, $dbh, $query, $var);
 
2667   if (!$provided_dbh) {
 
2672   $main::lxdebug->leave_sub();
 
2677 sub update_business {
 
2678   $main::lxdebug->enter_sub();
 
2680   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
2683   if ($provided_dbh) {
 
2684     $dbh = $provided_dbh;
 
2686     $dbh = $self->dbconnect_noauto($myconfig);
 
2689     qq|SELECT customernumberinit FROM business
 
2690        WHERE id = ? FOR UPDATE|;
 
2691   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
2693   if ($var =~ m/\d+$/) {
 
2694     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
2695     my $len_diff = length($var) - $-[0] - length($new_var);
 
2696     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
2702   $query = qq|UPDATE business
 
2703               SET customernumberinit = ?
 
2705   do_query($self, $dbh, $query, $var, $business_id);
 
2707   if (!$provided_dbh) {
 
2712   $main::lxdebug->leave_sub();
 
2717 sub get_partsgroup {
 
2718   $main::lxdebug->enter_sub();
 
2720   my ($self, $myconfig, $p) = @_;
 
2722   my $dbh = $self->get_standard_dbh($myconfig);
 
2724   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2726                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
2729   if ($p->{searchitems} eq 'part') {
 
2730     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
2732   if ($p->{searchitems} eq 'service') {
 
2733     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
2735   if ($p->{searchitems} eq 'assembly') {
 
2736     $query .= qq|WHERE p.assembly = '1'|;
 
2738   if ($p->{searchitems} eq 'labor') {
 
2739     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
2742   $query .= qq|ORDER BY partsgroup|;
 
2745     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2746                 ORDER BY partsgroup|;
 
2749   if ($p->{language_code}) {
 
2750     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2751                   t.description AS translation
 
2753                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2754                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
2755                 ORDER BY translation|;
 
2756     @values = ($p->{language_code});
 
2759   $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
 
2761   $main::lxdebug->leave_sub();
 
2764 sub get_pricegroup {
 
2765   $main::lxdebug->enter_sub();
 
2767   my ($self, $myconfig, $p) = @_;
 
2769   my $dbh = $self->get_standard_dbh($myconfig);
 
2771   my $query = qq|SELECT p.id, p.pricegroup
 
2774   $query .= qq| ORDER BY pricegroup|;
 
2777     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2778                 ORDER BY pricegroup|;
 
2781   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
2783   $main::lxdebug->leave_sub();
 
2787 # usage $form->all_years($myconfig, [$dbh])
 
2788 # return list of all years where bookings found
 
2791   $main::lxdebug->enter_sub();
 
2793   my ($self, $myconfig, $dbh) = @_;
 
2795   $dbh ||= $self->get_standard_dbh($myconfig);
 
2798   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
2799                    (SELECT MAX(transdate) FROM acc_trans)|;
 
2800   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
2802   if ($myconfig->{dateformat} =~ /^yy/) {
 
2803     ($startdate) = split /\W/, $startdate;
 
2804     ($enddate) = split /\W/, $enddate;
 
2806     (@_) = split /\W/, $startdate;
 
2808     (@_) = split /\W/, $enddate;
 
2813   $startdate = substr($startdate,0,4);
 
2814   $enddate = substr($enddate,0,4);
 
2816   while ($enddate >= $startdate) {
 
2817     push @all_years, $enddate--;
 
2822   $main::lxdebug->leave_sub();