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 #======================================================================
 
  52   $main::lxdebug->enter_sub(2);
 
  56   my @pairs = split(/&/, $input);
 
  59     my ($name, $value) = split(/=/, $_, 2);
 
  60     $in{$name} = unescape(undef, $value);
 
  63   $main::lxdebug->leave_sub(2);
 
  68 sub _request_to_hash {
 
  69   $main::lxdebug->enter_sub(2);
 
  72   my ($i,        $loc,  $key,    $val);
 
  73   my (%ATTACH,   $f,    $header, $header_body, $len, $buf);
 
  74   my ($boundary, @list, $size,   $body, $x, $blah, $name);
 
  76   if ($ENV{'CONTENT_TYPE'}
 
  77       && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
 
  78     $boundary = quotemeta('--' . $1);
 
  79     @list     = split(/$boundary/, $input);
 
  81     # For some reason there are always 2 extra, that are empty
 
  84     for ($x = 1; $x <= $size; $x++) {
 
  85       $header_body = $list[$x];
 
  86       $header_body =~ /\r\n\r\n|\n\n/;
 
  88       # Here we split the header and body
 
  93       # Now we try to get the file name
 
  95       $name =~ /name=\"(.+)\"/;
 
  96       ($name, $blah) = split(/\"/, $1);
 
  98       # If the form name is not attach, then we need to parse this like
 
 100       if ($name ne "attach") {
 
 101         $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 102         $ATTACH{$name} = $body;
 
 104         # Otherwise it is an attachment and we need to finish it up
 
 105       } elsif ($name eq "attach") {
 
 106         $header =~ /filename=\"(.+)\"/;
 
 107         $ATTACH{'FILE_NAME'} = $1;
 
 108         $ATTACH{'FILE_NAME'} =~ s/\"//g;
 
 109         $ATTACH{'FILE_NAME'} =~ s/\s//g;
 
 110         $ATTACH{'FILE_CONTENT'} = $body;
 
 112         for ($i = $x; $list[$i]; $i++) {
 
 113           $list[$i] =~ s/^.+name=$//;
 
 114           $list[$i] =~ /\"(\w+)\"/;
 
 120     $main::lxdebug->leave_sub(2);
 
 124     $main::lxdebug->leave_sub(2);
 
 125     return _input_to_hash($input);
 
 130   $main::lxdebug->enter_sub();
 
 136   if ($LXDebug::watch_form) {
 
 137     require SL::Watchdog;
 
 138     tie %{ $self }, 'SL::Watchdog';
 
 141   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 143   if ($ENV{QUERY_STRING}) {
 
 144     $_ = $ENV{QUERY_STRING};
 
 151   my %parameters = _request_to_hash($_);
 
 152   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 154   $self->{action} = lc $self->{action};
 
 155   $self->{action} =~ s/( |-|,|\#)/_/g;
 
 157   $self->{version}   = "2.4.2";
 
 159   $main::lxdebug->leave_sub();
 
 165   $main::lxdebug->enter_sub();
 
 171   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 173   $main::lxdebug->leave_sub();
 
 177   $main::lxdebug->enter_sub(2);
 
 179   my ($self, $str) = @_;
 
 181   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 183   $main::lxdebug->leave_sub(2);
 
 189   $main::lxdebug->enter_sub(2);
 
 191   my ($self, $str) = @_;
 
 196   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 198   $main::lxdebug->leave_sub(2);
 
 204   my ($self, $str) = @_;
 
 206   if ($str && !ref($str)) {
 
 207     $str =~ s/\"/"/g;
 
 215   my ($self, $str) = @_;
 
 217   if ($str && !ref($str)) {
 
 218     $str =~ s/"/\"/g;
 
 226   $main::lxdebug->enter_sub(2);
 
 228   my ($self, $str) = @_;
 
 231     ('order' => ['"', '<', '>'],
 
 237   map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 239   $main::lxdebug->leave_sub(2);
 
 248     map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 250     for (sort keys %$self) {
 
 251       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 252       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 259   $main::lxdebug->enter_sub();
 
 261   $main::lxdebug->show_backtrace();
 
 263   my ($self, $msg) = @_;
 
 264   if ($ENV{HTTP_USER_AGENT}) {
 
 266     $self->show_generic_error($msg);
 
 270     if ($self->{error_function}) {
 
 271       &{ $self->{error_function} }($msg);
 
 277   $main::lxdebug->leave_sub();
 
 281   $main::lxdebug->enter_sub();
 
 283   my ($self, $msg) = @_;
 
 285   if ($ENV{HTTP_USER_AGENT}) {
 
 288     if (!$self->{header}) {
 
 301     if ($self->{info_function}) {
 
 302       &{ $self->{info_function} }($msg);
 
 308   $main::lxdebug->leave_sub();
 
 312   $main::lxdebug->enter_sub();
 
 314   my ($self, $str, $cols, $maxrows) = @_;
 
 318   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 320   $maxrows = $rows unless defined $maxrows;
 
 322   $main::lxdebug->leave_sub();
 
 324   return ($rows > $maxrows) ? $maxrows : $rows;
 
 328   $main::lxdebug->enter_sub();
 
 330   my ($self, $msg) = @_;
 
 332   $self->error("$msg\n" . $DBI::errstr);
 
 334   $main::lxdebug->leave_sub();
 
 338   $main::lxdebug->enter_sub();
 
 340   my ($self, $name, $msg) = @_;
 
 342   if ($self->{$name} =~ /^\s*$/) {
 
 345   $main::lxdebug->leave_sub();
 
 349   $main::lxdebug->enter_sub();
 
 351   my ($self, $extra_code) = @_;
 
 353   if ($self->{header}) {
 
 354     $main::lxdebug->leave_sub();
 
 358   my ($stylesheet, $favicon);
 
 360   if ($ENV{HTTP_USER_AGENT}) {
 
 362     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 364         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 368     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 370     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 372         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 376     my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 378     if ($self->{landscape}) {
 
 379       $pagelayout = qq|<style type="text/css">
 
 380                         \@page { size:landscape; }
 
 384     my $fokus = qq|  document.$self->{fokus}.focus();| if ($self->{"fokus"});
 
 388     if ($self->{jsscript} == 1) {
 
 391         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 392         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 393         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 394         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 401       ? "$self->{title} - $self->{titlebar}"
 
 404     foreach $item (@ { $self->{AJAX} }) {
 
 405       $ajax .= $item->show_javascript();
 
 407     print qq|Content-Type: text/html; charset=${db_charset};
 
 411   <title>$self->{titlebar}</title>
 
 415   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
 
 419   <script type="text/javascript">
 
 427   <meta name="robots" content="noindex,nofollow" />
 
 428   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 429   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 431   <script type="text/javascript" src="js/tabcontent.js">
 
 433   /***********************************************
 
 434   * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 435   * This notice MUST stay intact for legal use
 
 436   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 437   ***********************************************/
 
 448   $main::lxdebug->leave_sub();
 
 451 sub parse_html_template {
 
 452   $main::lxdebug->enter_sub();
 
 454   my ($self, $file, $additional_params) = @_;
 
 457   if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 458     $language = $main::language;
 
 460     $language = $main::myconfig{"countrycode"};
 
 462   $language = "de" unless ($language);
 
 464   if (-f "templates/webpages/${file}_${language}.html") {
 
 465     if ((-f ".developer") &&
 
 466         (-f "templates/webpages/${file}_master.html") &&
 
 467         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 468          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 469       my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 470         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 471       print(qq|<pre>$info</pre>|);
 
 475     $file = "templates/webpages/${file}_${language}.html";
 
 476   } elsif (-f "templates/webpages/${file}.html") {
 
 477     $file = "templates/webpages/${file}.html";
 
 479     my $info = "Web page template '${file}' not found.\n" .
 
 480       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 481     print(qq|<pre>$info</pre>|);
 
 485   my $template = HTML::Template->new("filename" => $file,
 
 486                                      "die_on_bad_params" => 0,
 
 488                                      "case_sensitive" => 1,
 
 489                                      "loop_context_vars" => 1,
 
 492   $additional_params = {} unless ($additional_params);
 
 493   if ($self->{"DEBUG"}) {
 
 494     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 497   if ($additional_params->{"DEBUG"}) {
 
 498     $additional_params->{"DEBUG"} =
 
 499       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 502   if (%main::myconfig) {
 
 503     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 504     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 505     $jsc_dateformat =~ s/d+/\%d/gi;
 
 506     $jsc_dateformat =~ s/m+/\%m/gi;
 
 507     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 508     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 511   $additional_params->{"conf_jscalendar"} = $main::jscalendar;
 
 512   $additional_params->{"conf_lizenzen"} = $main::lizenzen;
 
 513   $additional_params->{"conf_latex_templates"} = $main::latex;
 
 514   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 516   my @additional_param_names = keys(%{$additional_params});
 
 517   foreach my $key ($template->param()) {
 
 518     my $param = $self->{$key};
 
 519     $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
 
 520     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
 
 521     $template->param($key => $param);
 
 524   my $output = $template->output();
 
 526   $main::lxdebug->leave_sub();
 
 531 sub show_generic_error {
 
 532   my ($self, $error, $title, $action) = @_;
 
 535   $add_params->{"title"} = $title if ($title);
 
 536   $self->{"label_error"} = $error;
 
 540     map({ delete($self->{$_}); } qw(action));
 
 541     map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
 
 542             if (!ref($self->{$_})); }
 
 544     $add_params->{"SHOW_BUTTON"} = 1;
 
 545     $add_params->{"BUTTON_LABEL"} = $action;
 
 547   $add_params->{"VARIABLES"} = \@vars;
 
 550   print($self->parse_html_template("generic/error", $add_params));
 
 552   die("Error: $error\n");
 
 555 sub show_generic_information {
 
 556   my ($self, $error, $title) = @_;
 
 559   $add_params->{"title"} = $title if ($title);
 
 560   $self->{"label_information"} = $error;
 
 563   print($self->parse_html_template("generic/information", $add_params));
 
 565   die("Information: $error\n");
 
 568 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 569 # changed it to accept an arbitrary number of triggers - sschoeling
 
 571   $main::lxdebug->enter_sub();
 
 574   my $myconfig = shift;
 
 577   # set dateform for jsscript
 
 580     "dd.mm.yy" => "%d.%m.%Y",
 
 581     "dd-mm-yy" => "%d-%m-%Y",
 
 582     "dd/mm/yy" => "%d/%m/%Y",
 
 583     "mm/dd/yy" => "%m/%d/%Y",
 
 584     "mm-dd-yy" => "%m-%d-%Y",
 
 585     "yyyy-mm-dd" => "%Y-%m-%d",
 
 588   my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
 
 589     $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
 
 596       inputField : "| . (shift) . qq|",
 
 597       ifFormat :"$ifFormat",
 
 598       align : "| .  (shift) . qq|",
 
 599       button : "| . (shift) . qq|"
 
 605        <script type="text/javascript">
 
 606        <!--| . join("", @triggers) . qq|//-->
 
 610   $main::lxdebug->leave_sub();
 
 613 }    #end sub write_trigger
 
 616   $main::lxdebug->enter_sub();
 
 618   my ($self, $msg) = @_;
 
 620   if ($self->{callback}) {
 
 622     ($script, $argv) = split(/\?/, $self->{callback});
 
 623     exec("perl", "$script", $argv);
 
 631   $main::lxdebug->leave_sub();
 
 634 # sort of columns removed - empty sub
 
 636   $main::lxdebug->enter_sub();
 
 638   my ($self, @columns) = @_;
 
 640   $main::lxdebug->leave_sub();
 
 646   $main::lxdebug->enter_sub(2);
 
 648   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 653   my $neg = ($amount =~ s/-//);
 
 655   if (defined($places) && ($places ne '')) {
 
 660       my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 661       $actual_places = length($actual_places);
 
 662       $places = $actual_places > $places ? $actual_places : $places;
 
 665     $amount = $self->round_amount($amount, $places);
 
 668   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 669   my @p = split(/\./, $amount); # split amount at decimal point
 
 671   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 674   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 677     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 678     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 679                         ($neg ? "-$amount"   : "$amount" )    ;
 
 683   $main::lxdebug->leave_sub(2);
 
 688   $main::lxdebug->enter_sub(2);
 
 690   my ($self, $myconfig, $amount) = @_;
 
 692   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 693       || ($myconfig->{numberformat} eq '1000,00')) {
 
 698   if ($myconfig->{numberformat} eq "1'000.00") {
 
 704   $main::lxdebug->leave_sub(2);
 
 706   return ($amount * 1);
 
 710   $main::lxdebug->enter_sub(2);
 
 712   my ($self, $amount, $places) = @_;
 
 715   # Rounding like "Kaufmannsrunden"
 
 716   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 718   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 721   $amount = $amount * (10**($places));
 
 722   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 724   $main::lxdebug->leave_sub(2);
 
 726   return $round_amount;
 
 731   $main::lxdebug->enter_sub();
 
 733   my ($self, $myconfig, $userspath) = @_;
 
 734   my ($template, $out);
 
 738   $self->{"cwd"} = getcwd();
 
 739   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
 741   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
 742     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 743   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
 744     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
 745     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 746   } elsif (($self->{"format"} =~ /html/i) ||
 
 747            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
 748     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 749   } elsif (($self->{"format"} =~ /xml/i) ||
 
 750              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
 751     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 752   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
 753     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 754   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
 755     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 756   } elsif ( defined $self->{'format'}) {
 
 757     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
 758   } elsif ( $self->{'format'} eq '' ) {
 
 759     $self->error("No Outputformat given: $self->{'format'}");
 
 760   } else { #Catch the rest
 
 761     $self->error("Outputformat not defined: $self->{'format'}");
 
 764   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 765   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 767   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 768       qw(email tel fax name signature company address businessnumber
 
 769          co_ustid taxnumber duns));
 
 770   map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
 
 771       qw(company address signature));
 
 772   map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
 
 774   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 776   # OUT is used for the media, screen, printer, email
 
 777   # for postscript we store a copy in a temporary file
 
 779   $self->{tmpfile} ||= "$userspath/${fileid}.$self->{IN}";
 
 781   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 783     $self->{OUT} = ">$self->{tmpfile}";
 
 787     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 789     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 793   if (!$template->parse(*OUT)) {
 
 795     $self->error("$self->{IN} : " . $template->get_error());
 
 800   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 802     if ($self->{media} eq 'email') {
 
 806       my $mail = new Mailer;
 
 808       map { $mail->{$_} = $self->{$_} }
 
 809         qw(cc bcc subject message version format);
 
 810       $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 811       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
 812       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
 813       $mail->{fileid} = "$fileid.";
 
 814       $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
 816       # if we send html or plain text inline
 
 817       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
 818         $mail->{contenttype} = "text/html";
 
 820         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
 821         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
 822         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
 824         open(IN, $self->{tmpfile})
 
 825           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 827           $mail->{message} .= $_;
 
 834         if (!$self->{"do_not_attach"}) {
 
 835           @{ $mail->{attachments} } =
 
 836             ({ "filename" => $self->{"tmpfile"},
 
 837                "name" => $self->{"attachment_filename"} ?
 
 838                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
 841         $mail->{message}       =~ s/\r\n/\n/g;
 
 842         $myconfig->{signature} =~ s/\\n/\n/g;
 
 843         $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 847       my $err = $mail->send();
 
 848       $self->error($self->cleanup . "$err") if ($err);
 
 854       my $numbytes = (-s $self->{tmpfile});
 
 855       open(IN, $self->{tmpfile})
 
 856         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 858       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
 860       chdir("$self->{cwd}");
 
 861       #print(STDERR "Kopien $self->{copies}\n");
 
 862       #print(STDERR "OUT $self->{OUT}\n");
 
 863       for my $i (1 .. $self->{copies}) {
 
 865           open(OUT, $self->{OUT})
 
 866             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
 868           $self->{attachment_filename} = ($self->{attachment_filename}) 
 
 869                                        ? $self->{attachment_filename}
 
 870                                        : $self->generate_attachment_filename();
 
 873           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
 874 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
 875 Content-Length: $numbytes
 
 879           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
 899   chdir("$self->{cwd}");
 
 900   $main::lxdebug->leave_sub();
 
 903 sub generate_attachment_filename {
 
 906   my %formname_translations = (
 
 907      bin_list            => $main::locale->text('Bin List'),
 
 908      credit_note         => $main::locale->text('Credit Note'),
 
 909      invoice             => $main::locale->text('Invoice'),
 
 910      packing_list        => $main::locale->text('Packing List'),
 
 911      pick_list           => $main::locale->text('Pick List'),
 
 912      proforma            => $main::locale->text('Proforma Invoice'),
 
 913      purchase_order      => $main::locale->text('Purchase Order'),
 
 914      request_quotation   => $main::locale->text('RFQ'),
 
 915      sales_order         => $main::locale->text('Confirmation'),
 
 916      sales_quotation     => $main::locale->text('Quotation'),
 
 917      storno_invoice      => $main::locale->text('Storno Invoice'),
 
 918      storno_packing_list => $main::locale->text('Storno Packing List'),
 
 921   my $attachment_filename = $formname_translations{$self->{"formname"}};
 
 923       (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
 
 924     : ($self->{"type"} =~ /_quotation$/)                       ? "quo"
 
 927   if ($attachment_filename && $self->{"${prefix}number"}) {
 
 928     $attachment_filename .= "_" . $self->{"${prefix}number"}
 
 929                             . (  $self->{format} =~ /pdf/i          ? ".pdf"
 
 930                                : $self->{format} =~ /postscript/i   ? ".ps"
 
 931                                : $self->{format} =~ /opendocument/i ? ".odt"
 
 932                                : $self->{format} =~ /html/i         ? ".html"
 
 934     $attachment_filename =~ s/ /_/g;
 
 935     my %umlaute = ( "ä" => "ae", "ö" => "oe", "ü" => "ue", 
 
 936                     "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue", "ß" => "ss");
 
 937     map { $attachment_filename =~ s/$_/$umlaute{$_}/g } keys %umlaute;
 
 939     $attachment_filename = "";
 
 942   return $attachment_filename;
 
 946   $main::lxdebug->enter_sub();
 
 950   chdir("$self->{tmpdir}");
 
 953   if (-f "$self->{tmpfile}.err") {
 
 954     open(FH, "$self->{tmpfile}.err");
 
 959   if ($self->{tmpfile}) {
 
 960     $self->{tmpfile} =~ s|.*/||g;
 
 962     $self->{tmpfile} =~ s/\.\w+$//g;
 
 963     my $tmpfile = $self->{tmpfile};
 
 964     unlink(<$tmpfile.*>);
 
 967   chdir("$self->{cwd}");
 
 969   $main::lxdebug->leave_sub();
 
 975   $main::lxdebug->enter_sub();
 
 977   my ($self, $date, $myconfig) = @_;
 
 979   if ($date && $date =~ /\D/) {
 
 981     if ($myconfig->{dateformat} =~ /^yy/) {
 
 982       ($yy, $mm, $dd) = split /\D/, $date;
 
 984     if ($myconfig->{dateformat} =~ /^mm/) {
 
 985       ($mm, $dd, $yy) = split /\D/, $date;
 
 987     if ($myconfig->{dateformat} =~ /^dd/) {
 
 988       ($dd, $mm, $yy) = split /\D/, $date;
 
 993     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
 994     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
 996     $dd = "0$dd" if ($dd < 10);
 
 997     $mm = "0$mm" if ($mm < 10);
 
1002   $main::lxdebug->leave_sub();
 
1007 # Database routines used throughout
 
1010   $main::lxdebug->enter_sub(2);
 
1012   my ($self, $myconfig) = @_;
 
1014   # connect to database
 
1016     DBI->connect($myconfig->{dbconnect},
 
1017                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
1021   if ($myconfig->{dboptions}) {
 
1022     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1025   $main::lxdebug->leave_sub(2);
 
1030 sub dbconnect_noauto {
 
1031   $main::lxdebug->enter_sub();
 
1033   my ($self, $myconfig) = @_;
 
1035   # connect to database
 
1037     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
1038                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
1042   if ($myconfig->{dboptions}) {
 
1043     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1046   $main::lxdebug->leave_sub();
 
1051 sub update_balance {
 
1052   $main::lxdebug->enter_sub();
 
1054   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
1056   # if we have a value, go do it
 
1059     # retrieve balance from table
 
1060     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1061     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1062     my ($balance) = $sth->fetchrow_array;
 
1068     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1069     do_query($self, $dbh, $query, @values);
 
1071   $main::lxdebug->leave_sub();
 
1074 sub update_exchangerate {
 
1075   $main::lxdebug->enter_sub();
 
1077   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1079   # some sanity check for currency
 
1081     $main::lxdebug->leave_sub();
 
1085   my $query = qq|SELECT e.curr FROM exchangerate e
 
1086                  WHERE e.curr = ? AND e.transdate = ?
 
1088   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1091   if ($buy != 0 && $sell != 0) {
 
1092     $set = "buy = $buy, sell = $sell";
 
1093   } elsif ($buy != 0) {
 
1094     $set = "buy = $buy";
 
1095   } elsif ($sell != 0) {
 
1096     $set = "sell = $sell";
 
1099   if ($sth->fetchrow_array) {
 
1100     $query = qq|UPDATE exchangerate
 
1105     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1106                 VALUES (?, $buy, $sell, ?)|;
 
1109   do_query($self, $dbh, $query, $curr, $transdate);
 
1111   $main::lxdebug->leave_sub();
 
1114 sub save_exchangerate {
 
1115   $main::lxdebug->enter_sub();
 
1117   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1119   my $dbh = $self->dbconnect($myconfig);
 
1121   my ($buy, $sell) = (0, 0);
 
1122   $buy  = $rate if $fld eq 'buy';
 
1123   $sell = $rate if $fld eq 'sell';
 
1125   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1129   $main::lxdebug->leave_sub();
 
1132 sub get_exchangerate {
 
1133   $main::lxdebug->enter_sub();
 
1135   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1137   unless ($transdate) {
 
1138     $main::lxdebug->leave_sub();
 
1142   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1143                  WHERE e.curr = ? AND e.transdate = ?|;
 
1144   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1146   if (!$exchangerate) {
 
1150   $main::lxdebug->leave_sub();
 
1152   return $exchangerate;
 
1155 sub check_exchangerate {
 
1156   $main::lxdebug->enter_sub();
 
1158   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1160   unless ($transdate) {
 
1161     $main::lxdebug->leave_sub();
 
1165   my $dbh = $self->dbconnect($myconfig);
 
1167   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1168                  WHERE e.curr = ? AND e.transdate = ?|;
 
1169   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1172   $main::lxdebug->leave_sub();
 
1174   return $exchangerate;
 
1177 sub set_payment_options {
 
1178   $main::lxdebug->enter_sub();
 
1180   my ($self, $myconfig, $transdate) = @_;
 
1182   if ($self->{payment_id}) {
 
1184     my $dbh = $self->dbconnect($myconfig);
 
1187       qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
 
1188       qq|FROM payment_terms p | .
 
1191     ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1192      $self->{payment_terms}) =
 
1193        selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1195     if ($transdate eq "") {
 
1196       if ($self->{invdate}) {
 
1197         $transdate = $self->{invdate};
 
1199         $transdate = $self->{transdate};
 
1204       qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1205       qq|FROM payment_terms|;
 
1206     ($self->{netto_date}, $self->{skonto_date}) =
 
1207       selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1209     my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
 
1210     my $skonto_amount = $self->parse_amount($myconfig, $total) *
 
1211       $self->{percent_skonto};
 
1213     $self->{skonto_amount} =
 
1214       $self->format_amount($myconfig, $skonto_amount, 2);
 
1216     if ($self->{"language_id"}) {
 
1218         qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1219         qq|FROM translation_payment_terms t | .
 
1220         qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1221         qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1222       my ($description_long, $output_numberformat, $output_dateformat,
 
1223         $output_longdates) =
 
1224         selectrow_query($self, $dbh, $query,
 
1225                         $self->{"language_id"}, $self->{"payment_id"});
 
1227       $self->{payment_terms} = $description_long if ($description_long);
 
1229       if ($output_dateformat) {
 
1230         foreach my $key (qw(netto_date skonto_date)) {
 
1232             $main::locale->reformat_date($myconfig, $self->{$key},
 
1238       if ($output_numberformat &&
 
1239           ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1240         my $saved_numberformat = $myconfig->{"numberformat"};
 
1241         $myconfig->{"numberformat"} = $output_numberformat;
 
1242         $self->{skonto_amount} =
 
1243           $self->format_amount($myconfig, $skonto_amount, 2);
 
1244         $myconfig->{"numberformat"} = $saved_numberformat;
 
1248     $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1249     $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1250     $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
 
1251     $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
 
1252     $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
 
1253     $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1254     $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1255     $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1256     $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1257     $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1262   $main::lxdebug->leave_sub();
 
1266 sub get_template_language {
 
1267   $main::lxdebug->enter_sub();
 
1269   my ($self, $myconfig) = @_;
 
1271   my $template_code = "";
 
1273   if ($self->{language_id}) {
 
1274     my $dbh = $self->dbconnect($myconfig);
 
1275     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1276     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1280   $main::lxdebug->leave_sub();
 
1282   return $template_code;
 
1285 sub get_printer_code {
 
1286   $main::lxdebug->enter_sub();
 
1288   my ($self, $myconfig) = @_;
 
1290   my $template_code = "";
 
1292   if ($self->{printer_id}) {
 
1293     my $dbh = $self->dbconnect($myconfig);
 
1294     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1295     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1299   $main::lxdebug->leave_sub();
 
1301   return $template_code;
 
1305   $main::lxdebug->enter_sub();
 
1307   my ($self, $myconfig) = @_;
 
1309   my $template_code = "";
 
1311   if ($self->{shipto_id}) {
 
1312     my $dbh = $self->dbconnect($myconfig);
 
1313     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1314     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1315     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1319   $main::lxdebug->leave_sub();
 
1323   $main::lxdebug->enter_sub();
 
1325   my ($self, $dbh, $id, $module) = @_;
 
1329   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1330                        contact phone fax email)) {
 
1331     if ($self->{"shipto$item"}) {
 
1332       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1334     push(@values, $self->{"shipto${item}"});
 
1337     if ($self->{shipto_id}) {
 
1338       my $query = qq|UPDATE shipto set
 
1340                        shiptodepartment_1 = ?,
 
1341                        shiptodepartment_2 = ?,
 
1350                      WHERE shipto_id = ?|;
 
1351       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1353       my $query = qq|SELECT * FROM shipto
 
1354                      WHERE shiptoname = ? AND
 
1355                        shiptodepartment_1 = ? AND
 
1356                        shiptodepartment_2 = ? AND
 
1357                        shiptostreet = ? AND
 
1358                        shiptozipcode = ? AND
 
1360                        shiptocountry = ? AND
 
1361                        shiptocontact = ? AND
 
1365       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
 
1368           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1369                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1370                                  shiptocontact, shiptophone, shiptofax, shiptoemail, module)
 
1371              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1372         do_query($self, $dbh, $query, $id, @values, $module);
 
1377   $main::lxdebug->leave_sub();
 
1381   $main::lxdebug->enter_sub();
 
1383   my ($self, $dbh) = @_;
 
1385   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1386   ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1387   $self->{employee_id} *= 1;
 
1389   $main::lxdebug->leave_sub();
 
1393   $main::lxdebug->enter_sub();
 
1395   my ($self, $myconfig, $salesman_id) = @_;
 
1397   $main::lxdebug->leave_sub() and return unless $salesman_id;
 
1399   my $dbh = $self->dbconnect($myconfig);
 
1402     selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
 
1406     my $user = new User($main::memberfile, $login);
 
1407     map({ $self->{"salesman_$_"} = $user->{$_}; }
 
1408         qw(address businessnumber co_ustid company duns email fax name
 
1410     $self->{salesman_login} = $login;
 
1412     $self->{salesman_name} = $login
 
1413       if ($self->{salesman_name} eq "");
 
1415     map({ $self->{"salesman_$_"} =~ s/\\n/\n/g; } qw(address company));
 
1420   $main::lxdebug->leave_sub();
 
1424   $main::lxdebug->enter_sub();
 
1426   my ($self, $myconfig) = @_;
 
1428   my $dbh = $self->dbconnect($myconfig);
 
1429   my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
 
1430   ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1433   $main::lxdebug->leave_sub();
 
1437   $main::lxdebug->enter_sub();
 
1439   my ($self, $dbh, $id, $key) = @_;
 
1441   $key = "all_contacts" unless ($key);
 
1444     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1445     qq|FROM contacts | .
 
1446     qq|WHERE cp_cv_id = ? | .
 
1447     qq|ORDER BY lower(cp_name)|;
 
1449   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1451   $main::lxdebug->leave_sub();
 
1455   $main::lxdebug->enter_sub();
 
1457   my ($self, $dbh, $key) = @_;
 
1459   my ($all, $old_id, $where, @values);
 
1461   if (ref($key) eq "HASH") {
 
1464     $key = "ALL_PROJECTS";
 
1466     foreach my $p (keys(%{$params})) {
 
1468         $all = $params->{$p};
 
1469       } elsif ($p eq "old_id") {
 
1470         $old_id = $params->{$p};
 
1471       } elsif ($p eq "key") {
 
1472         $key = $params->{$p};
 
1478     $where = "WHERE active ";
 
1480       if (ref($old_id) eq "ARRAY") {
 
1481         my @ids = grep({ $_ } @{$old_id});
 
1483           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1484           push(@values, @ids);
 
1487         $where .= " OR (id = ?) ";
 
1488         push(@values, $old_id);
 
1494     qq|SELECT id, projectnumber, description, active | .
 
1497     qq|ORDER BY lower(projectnumber)|;
 
1499   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
1501   $main::lxdebug->leave_sub();
 
1505   $main::lxdebug->enter_sub();
 
1507   my ($self, $dbh, $vc_id, $key) = @_;
 
1509   $key = "all_shipto" unless ($key);
 
1511   # get shipping addresses
 
1513     qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
 
1515     qq|WHERE trans_id = ?|;
 
1517   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
1519   $main::lxdebug->leave_sub();
 
1523   $main::lxdebug->enter_sub();
 
1525   my ($self, $dbh, $key) = @_;
 
1527   $key = "all_printers" unless ($key);
 
1529   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
1531   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1533   $main::lxdebug->leave_sub();
 
1537   $main::lxdebug->enter_sub();
 
1539   my ($self, $dbh, $params) = @_;
 
1541   $key = $params->{key};
 
1542   $key = "all_charts" unless ($key);
 
1544   my $transdate = quote_db_date($params->{transdate});
 
1547     qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
1549     qq|LEFT JOIN taxkeys tk ON | .
 
1550     qq|(tk.id = (SELECT id FROM taxkeys | .
 
1551     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
1552     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
1553     qq|ORDER BY c.accno|;
 
1555   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1557   $main::lxdebug->leave_sub();
 
1560 sub _get_taxcharts {
 
1561   $main::lxdebug->enter_sub();
 
1563   my ($self, $dbh, $key) = @_;
 
1565   $key = "all_taxcharts" unless ($key);
 
1567   my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
1569   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1571   $main::lxdebug->leave_sub();
 
1575   $main::lxdebug->enter_sub();
 
1577   my ($self, $dbh, $key) = @_;
 
1579   $key = "all_taxzones" unless ($key);
 
1581   my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
 
1583   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1585   $main::lxdebug->leave_sub();
 
1588 sub _get_employees {
 
1589   $main::lxdebug->enter_sub();
 
1591   my ($self, $dbh, $key) = @_;
 
1593   $key = "all_employees" unless ($key);
 
1595     selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee|);
 
1597   $main::lxdebug->leave_sub();
 
1600 sub _get_business_types {
 
1601   $main::lxdebug->enter_sub();
 
1603   my ($self, $dbh, $key) = @_;
 
1605   $key = "all_business_types" unless ($key);
 
1607     selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
 
1609   $main::lxdebug->leave_sub();
 
1612 sub _get_languages {
 
1613   $main::lxdebug->enter_sub();
 
1615   my ($self, $dbh, $key) = @_;
 
1617   $key = "all_languages" unless ($key);
 
1619   my $query = qq|SELECT * FROM language ORDER BY id|;
 
1621   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1623   $main::lxdebug->leave_sub();
 
1626 sub _get_dunning_configs {
 
1627   $main::lxdebug->enter_sub();
 
1629   my ($self, $dbh, $key) = @_;
 
1631   $key = "all_dunning_configs" unless ($key);
 
1633   my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
 
1635   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1637   $main::lxdebug->leave_sub();
 
1640 sub _get_currencies {
 
1641 $main::lxdebug->enter_sub();
 
1643   my ($self, $dbh, $key) = @_;
 
1645   $key = "all_currencies" unless ($key);
 
1647   my $query = qq|SELECT curr AS currency FROM defaults|;
 
1649   $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
 
1651   $main::lxdebug->leave_sub();
 
1655 $main::lxdebug->enter_sub();
 
1657   my ($self, $dbh, $key) = @_;
 
1659   $key = "all_payments" unless ($key);
 
1661   my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
 
1663   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1665   $main::lxdebug->leave_sub();
 
1668 sub _get_customers {
 
1669   $main::lxdebug->enter_sub();
 
1671   my ($self, $dbh, $key) = @_;
 
1673   $key = "all_customers" unless ($key);
 
1675   my $query = qq|SELECT * FROM customer|;
 
1677   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1679   $main::lxdebug->leave_sub();
 
1683   $main::lxdebug->enter_sub();
 
1685   my ($self, $dbh, $key) = @_;
 
1687   $key = "all_vendors" unless ($key);
 
1689   my $query = qq|SELECT * FROM vendor|;
 
1691   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1693   $main::lxdebug->leave_sub();
 
1697   $main::lxdebug->enter_sub();
 
1702   my $dbh = $self->dbconnect(\%main::myconfig);
 
1703   my ($sth, $query, $ref);
 
1705   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
1706   my $vc_id = $self->{"${vc}_id"};
 
1708   if ($params{"contacts"}) {
 
1709     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
1712   if ($params{"shipto"}) {
 
1713     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
1716   if ($params{"projects"} || $params{"all_projects"}) {
 
1717     $self->_get_projects($dbh, $params{"all_projects"} ?
 
1718                          $params{"all_projects"} : $params{"projects"},
 
1719                          $params{"all_projects"} ? 1 : 0);
 
1722   if ($params{"printers"}) {
 
1723     $self->_get_printers($dbh, $params{"printers"});
 
1726   if ($params{"languages"}) {
 
1727     $self->_get_languages($dbh, $params{"languages"});
 
1730   if ($params{"charts"}) {
 
1731     $self->_get_charts($dbh, $params{"charts"});
 
1734   if ($params{"taxcharts"}) {
 
1735     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
1738   if ($params{"taxzones"}) {
 
1739     $self->_get_taxzones($dbh, $params{"taxzones"});
 
1742   if ($params{"employees"}) {
 
1743     $self->_get_employees($dbh, $params{"employees"});
 
1746   if ($params{"business_types"}) {
 
1747     $self->_get_business_types($dbh, $params{"business_types"});
 
1750   if ($params{"dunning_configs"}) {
 
1751     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
 
1754   if($params{"currencies"}) {
 
1755     $self->_get_currencies($dbh, $params{"currencies"});
 
1758   if($params{"customers"}) {
 
1759     $self->_get_customers($dbh, $params{"customers"});
 
1762   if($params{"vendors"}) {
 
1763     $self->_get_vendors($dbh, $params{"vendors"});
 
1766   if($params{"payments"}) {
 
1767     $self->_get_payments($dbh, $params{"payments"});
 
1772   $main::lxdebug->leave_sub();
 
1775 # this sub gets the id and name from $table
 
1777   $main::lxdebug->enter_sub();
 
1779   my ($self, $myconfig, $table) = @_;
 
1781   # connect to database
 
1782   my $dbh = $self->dbconnect($myconfig);
 
1784   $table = $table eq "customer" ? "customer" : "vendor";
 
1785   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
1787   my ($query, @values);
 
1789   if (!$self->{openinvoices}) {
 
1791     if ($self->{customernumber} ne "") {
 
1792       $where = qq|(vc.customernumber ILIKE ?)|;
 
1793       push(@values, '%' . $self->{customernumber} . '%');
 
1795       $where = qq|(vc.name ILIKE ?)|;
 
1796       push(@values, '%' . $self->{$table} . '%');
 
1800       qq~SELECT vc.id, vc.name,
 
1801            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1803          WHERE $where AND (NOT vc.obsolete)
 
1807       qq~SELECT DISTINCT vc.id, vc.name,
 
1808            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
1810          JOIN $table vc ON (a.${table}_id = vc.id)
 
1811          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
1813     push(@values, '%' . $self->{$table} . '%');
 
1816   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
1818   $main::lxdebug->leave_sub();
 
1820   return scalar(@{ $self->{name_list} });
 
1823 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1826   $main::lxdebug->enter_sub();
 
1828   my ($self, $myconfig, $table, $module) = @_;
 
1831   my $dbh = $self->dbconnect($myconfig);
 
1833   $table = $table eq "customer" ? "customer" : "vendor";
 
1835   my $query = qq|SELECT count(*) FROM $table|;
 
1836   my ($count) = selectrow_query($self, $dbh, $query);
 
1838   # build selection list
 
1839   if ($count < $myconfig->{vclimit}) {
 
1840     $query = qq|SELECT id, name, salesman_id
 
1841                 FROM $table WHERE NOT obsolete
 
1843     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
1847   $self->get_employee($dbh);
 
1849   # setup sales contacts
 
1850   $query = qq|SELECT e.id, e.name
 
1852               WHERE (e.sales = '1') AND (NOT e.id = ?)|;
 
1853   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
1856   push(@{ $self->{all_employees} },
 
1857        { id   => $self->{employee_id},
 
1858          name => $self->{employee} });
 
1860   # sort the whole thing
 
1861   @{ $self->{all_employees} } =
 
1862     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
1864   if ($module eq 'AR') {
 
1866     # prepare query for departments
 
1867     $query = qq|SELECT id, description
 
1870                 ORDER BY description|;
 
1873     $query = qq|SELECT id, description
 
1875                 ORDER BY description|;
 
1878   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
1881   $query = qq|SELECT id, description
 
1885   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
1888   $query = qq|SELECT printer_description, id
 
1890               ORDER BY printer_description|;
 
1892   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
1895   $query = qq|SELECT id, description
 
1899   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
1903   $main::lxdebug->leave_sub();
 
1906 sub language_payment {
 
1907   $main::lxdebug->enter_sub();
 
1909   my ($self, $myconfig) = @_;
 
1911   my $dbh = $self->dbconnect($myconfig);
 
1913   my $query = qq|SELECT id, description
 
1917   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
1920   $query = qq|SELECT printer_description, id
 
1922               ORDER BY printer_description|;
 
1924   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
1927   $query = qq|SELECT id, description
 
1931   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
1933   # get buchungsgruppen
 
1934   $query = qq|SELECT id, description
 
1935               FROM buchungsgruppen|;
 
1937   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
1940   $main::lxdebug->leave_sub();
 
1943 # this is only used for reports
 
1944 sub all_departments {
 
1945   $main::lxdebug->enter_sub();
 
1947   my ($self, $myconfig, $table) = @_;
 
1949   my $dbh = $self->dbconnect($myconfig);
 
1952   if ($table eq 'customer') {
 
1953     $where = "WHERE role = 'P' ";
 
1956   my $query = qq|SELECT id, description
 
1959                  ORDER BY description|;
 
1960   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
1962   delete($self->{all_departments}) unless (@{ $self->{all_departments} });
 
1966   $main::lxdebug->leave_sub();
 
1970   $main::lxdebug->enter_sub();
 
1972   my ($self, $module, $myconfig, $table) = @_;
 
1975   if ($table eq "customer") {
 
1984   $self->all_vc($myconfig, $table, $module);
 
1986   # get last customers or vendors
 
1987   my ($query, $sth, $ref);
 
1989   my $dbh = $self->dbconnect($myconfig);
 
1994     my $transdate = "current_date";
 
1995     if ($self->{transdate}) {
 
1996       $transdate = $dbh->quote($self->{transdate});
 
1999     # now get the account numbers
 
2000     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2001                 FROM chart c, taxkeys tk
 
2002                 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
2003                   (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
2006     $sth = $dbh->prepare($query);
 
2008     do_statement($self, $sth, $query, '%' . $module . '%');
 
2010     $self->{accounts} = "";
 
2011     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2013       foreach my $key (split(/:/, $ref->{link})) {
 
2014         if ($key =~ /$module/) {
 
2016           # cross reference for keys
 
2017           $xkeyref{ $ref->{accno} } = $key;
 
2019           push @{ $self->{"${module}_links"}{$key} },
 
2020             { accno       => $ref->{accno},
 
2021               description => $ref->{description},
 
2022               taxkey      => $ref->{taxkey_id},
 
2023               tax_id      => $ref->{tax_id} };
 
2025           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2031   # get taxkeys and description
 
2032   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
2033   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
2035   if (($module eq "AP") || ($module eq "AR")) {
 
2036     # get tax rates and description
 
2037     $query = qq|SELECT * FROM tax|;
 
2038     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
2044            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
2045            a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
 
2046            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
2047            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
2049            d.description AS department,
 
2052          JOIN $table c ON (a.${table}_id = c.id)
 
2053          LEFT JOIN employee e ON (e.id = a.employee_id)
 
2054          LEFT JOIN department d ON (d.id = a.department_id)
 
2056     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
2058     foreach $key (keys %$ref) {
 
2059       $self->{$key} = $ref->{$key};
 
2062     my $transdate = "current_date";
 
2063     if ($self->{transdate}) {
 
2064       $transdate = $dbh->quote($self->{transdate});
 
2067     # now get the account numbers
 
2068     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2070                 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
 
2072                   AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
2073                     OR c.link LIKE '%_tax%')
 
2076     $sth = $dbh->prepare($query);
 
2077     do_statement($self, $sth, $query, "%$module%");
 
2079     $self->{accounts} = "";
 
2080     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2082       foreach my $key (split(/:/, $ref->{link})) {
 
2083         if ($key =~ /$module/) {
 
2085           # cross reference for keys
 
2086           $xkeyref{ $ref->{accno} } = $key;
 
2088           push @{ $self->{"${module}_links"}{$key} },
 
2089             { accno       => $ref->{accno},
 
2090               description => $ref->{description},
 
2091               taxkey      => $ref->{taxkey_id},
 
2092               tax_id      => $ref->{tax_id} };
 
2094           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2100     # get amounts from individual entries
 
2103            c.accno, c.description,
 
2104            a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
 
2108          LEFT JOIN chart c ON (c.id = a.chart_id)
 
2109          LEFT JOIN project p ON (p.id = a.project_id)
 
2110          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
2111                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
2112                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
2113                                         THEN tk.chart_id = a.chart_id
 
2116                                        OR (c.link='%tax%')) AND
 
2117                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
2118          WHERE a.trans_id = ?
 
2119          AND a.fx_transaction = '0'
 
2120          ORDER BY a.oid, a.transdate|;
 
2121     $sth = $dbh->prepare($query);
 
2122     do_statement($self, $sth, $query, $self->{id});
 
2124     # get exchangerate for currency
 
2125     $self->{exchangerate} =
 
2126       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2129     # store amounts in {acc_trans}{$key} for multiple accounts
 
2130     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2131       $ref->{exchangerate} =
 
2132         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
2133       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2136       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2137         $ref->{amount} *= -1;
 
2139       $ref->{index} = $index;
 
2141       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2147            d.curr AS currencies, d.closedto, d.revtrans,
 
2148            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2149            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2151     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2152     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2159             current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
 
2160             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2161             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2163     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2164     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2166     if ($self->{"$self->{vc}_id"}) {
 
2168       # only setup currency
 
2169       ($self->{currency}) = split(/:/, $self->{currencies});
 
2173       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2175       # get exchangerate for currency
 
2176       $self->{exchangerate} =
 
2177         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2185   $main::lxdebug->leave_sub();
 
2189   $main::lxdebug->enter_sub();
 
2191   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2193   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
2194   $table = $table eq "customer" ? "customer" : "vendor";
 
2195   my $where = "1 = 1";
 
2197   if ($self->{type} =~ /_order/) {
 
2199     $where = "quotation = '0'";
 
2201   if ($self->{type} =~ /_quotation/) {
 
2203     $where = "quotation = '1'";
 
2206   my $query = qq|SELECT MAX(id) FROM $arap
 
2207                  WHERE $where AND ${table}_id > 0|;
 
2208   my ($trans_id) = selectrow_query($self, $dbh, $query);
 
2213          a.curr, a.${table}_id, a.department_id,
 
2214          d.description AS department,
 
2215          ct.name, current_date + ct.terms AS duedate
 
2217        LEFT JOIN $table ct ON (a.${table}_id = ct.id)
 
2218        LEFT JOIN department d ON (a.department_id = d.id)
 
2220   ($self->{currency},   $self->{"${table}_id"}, $self->{department_id},
 
2221    $self->{department}, $self->{$table},        $self->{duedate})
 
2222     = selectrow_query($self, $dbh, $query, $trans_id);
 
2224   $main::lxdebug->leave_sub();
 
2228   $main::lxdebug->enter_sub();
 
2230   my ($self, $myconfig, $thisdate, $days) = @_;
 
2232   my $dbh = $self->dbconnect($myconfig);
 
2237     my $dateformat = $myconfig->{dateformat};
 
2238     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2239     $thisdate = $dbh->quote($thisdate);
 
2240     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2242     $query = qq|SELECT current_date AS thisdate|;
 
2245   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2249   $main::lxdebug->leave_sub();
 
2255   $main::lxdebug->enter_sub();
 
2257   my ($self, $string) = @_;
 
2259   if ($string !~ /%/) {
 
2260     $string = "%$string%";
 
2263   $string =~ s/\'/\'\'/g;
 
2265   $main::lxdebug->leave_sub();
 
2271   $main::lxdebug->enter_sub();
 
2273   my ($self, $flds, $new, $count, $numrows) = @_;
 
2277   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
2283   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2285     $j = $item->{ndx} - 1;
 
2286     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2290   for $i ($count + 1 .. $numrows) {
 
2291     map { delete $self->{"${_}_$i"} } @{$flds};
 
2294   $main::lxdebug->leave_sub();
 
2298   $main::lxdebug->enter_sub();
 
2300   my ($self, $myconfig) = @_;
 
2304   my $dbh = $self->dbconnect_noauto($myconfig);
 
2306   my $query = qq|DELETE FROM status
 
2307                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2308   my $sth = prepare_query($self, $dbh, $query);
 
2310   if ($self->{formname} =~ /(check|receipt)/) {
 
2311     for $i (1 .. $self->{rowcount}) {
 
2312       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
2315     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
2319   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2320   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2322   my %queued = split / /, $self->{queued};
 
2325   if ($self->{formname} =~ /(check|receipt)/) {
 
2327     # this is a check or receipt, add one entry for each lineitem
 
2328     my ($accno) = split /--/, $self->{account};
 
2329     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
2330                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
2331     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
2332     $sth = prepare_query($self, $dbh, $query);
 
2334     for $i (1 .. $self->{rowcount}) {
 
2335       if ($self->{"checked_$i"}) {
 
2336         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
2342     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2343                 VALUES (?, ?, ?, ?, ?)|;
 
2344     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
2345              $queued{$self->{formname}}, $self->{formname});
 
2351   $main::lxdebug->leave_sub();
 
2355   $main::lxdebug->enter_sub();
 
2357   my ($self, $dbh) = @_;
 
2359   my ($query, $printed, $emailed);
 
2361   my $formnames  = $self->{printed};
 
2362   my $emailforms = $self->{emailed};
 
2364   my $query = qq|DELETE FROM status
 
2365                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2366   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
2368   # this only applies to the forms
 
2369   # checks and receipts are posted when printed or queued
 
2371   if ($self->{queued}) {
 
2372     my %queued = split / /, $self->{queued};
 
2374     foreach my $formname (keys %queued) {
 
2375       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2376       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2378       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2379                   VALUES (?, ?, ?, ?, ?)|;
 
2380       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
2382       $formnames  =~ s/$self->{formname}//;
 
2383       $emailforms =~ s/$self->{formname}//;
 
2388   # save printed, emailed info
 
2389   $formnames  =~ s/^ +//g;
 
2390   $emailforms =~ s/^ +//g;
 
2393   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2394   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2396   foreach my $formname (keys %status) {
 
2397     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2398     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2400     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2401                 VALUES (?, ?, ?, ?)|;
 
2402     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
2405   $main::lxdebug->leave_sub();
 
2409 # $main::locale->text('SAVED')
 
2410 # $main::locale->text('DELETED')
 
2411 # $main::locale->text('ADDED')
 
2412 # $main::locale->text('PAYMENT POSTED')
 
2413 # $main::locale->text('POSTED')
 
2414 # $main::locale->text('POSTED AS NEW')
 
2415 # $main::locale->text('ELSE')
 
2416 # $main::locale->text('SAVED FOR DUNNING')
 
2417 # $main::locale->text('DUNNING STARTED')
 
2418 # $main::locale->text('PRINTED')
 
2419 # $main::locale->text('MAILED')
 
2420 # $main::locale->text('SCREENED')
 
2421 # $main::locale->text('CANCELED')
 
2422 # $main::locale->text('invoice')
 
2423 # $main::locale->text('proforma')
 
2424 # $main::locale->text('sales_order')
 
2425 # $main::locale->text('packing_list')
 
2426 # $main::locale->text('pick_list')
 
2427 # $main::locale->text('purchase_order')
 
2428 # $main::locale->text('bin_list')
 
2429 # $main::locale->text('sales_quotation')
 
2430 # $main::locale->text('request_quotation')
 
2433   $main::lxdebug->enter_sub();
 
2438   if(!exists $self->{employee_id}) {
 
2439     &get_employee($self, $dbh);
 
2443     qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
 
2444     qq|VALUES (?, ?, ?, ?, ?)|;
 
2445   my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
 
2446                 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
 
2447   do_query($self, $dbh, $query, @values);
 
2449   $main::lxdebug->leave_sub();
 
2453   $main::lxdebug->enter_sub();
 
2457   my $trans_id = shift();
 
2458   my $restriction = shift();
 
2461   if ($trans_id ne "") {
 
2463       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 | .
 
2464       qq|FROM history_erp h | .
 
2465       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
2466       qq|WHERE trans_id = ? |
 
2469     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2471     $sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
 
2473     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
2474       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
2475       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
2476       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
 
2477       $tempArray[$i++] = $hash_ref;
 
2479     $main::lxdebug->leave_sub() and return \@tempArray 
 
2480       if ($i > 0 && $tempArray[0] ne "");
 
2482   $main::lxdebug->leave_sub();
 
2486 sub update_defaults {
 
2487   $main::lxdebug->enter_sub();
 
2489   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
2492   if ($provided_dbh) {
 
2493     $dbh = $provided_dbh;
 
2495     $dbh = $self->dbconnect_noauto($myconfig);
 
2497   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2498   my $sth   = $dbh->prepare($query);
 
2500   $sth->execute || $self->dberror($query);
 
2501   my ($var) = $sth->fetchrow_array;
 
2504   $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
 
2507   $query = qq|UPDATE defaults SET $fld = ?|;
 
2508   do_query($self, $dbh, $query, $var);
 
2510   if (!$provided_dbh) {
 
2515   $main::lxdebug->leave_sub();
 
2520 sub update_business {
 
2521   $main::lxdebug->enter_sub();
 
2523   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
2526   if ($provided_dbh) {
 
2527     $dbh = $provided_dbh;
 
2529     $dbh = $self->dbconnect_noauto($myconfig);
 
2532     qq|SELECT customernumberinit FROM business
 
2533        WHERE id = ? FOR UPDATE|;
 
2534   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
2536   $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
 
2538   $query = qq|UPDATE business
 
2539               SET customernumberinit = ?
 
2541   do_query($self, $dbh, $query, $var, $business_id);
 
2543   if (!$provided_dbh) {
 
2548   $main::lxdebug->leave_sub();
 
2553 sub get_partsgroup {
 
2554   $main::lxdebug->enter_sub();
 
2556   my ($self, $myconfig, $p) = @_;
 
2558   my $dbh = $self->dbconnect($myconfig);
 
2560   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2562                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
2565   if ($p->{searchitems} eq 'part') {
 
2566     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
2568   if ($p->{searchitems} eq 'service') {
 
2569     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
2571   if ($p->{searchitems} eq 'assembly') {
 
2572     $query .= qq|WHERE p.assembly = '1'|;
 
2574   if ($p->{searchitems} eq 'labor') {
 
2575     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
2578   $query .= qq|ORDER BY partsgroup|;
 
2581     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2582                 ORDER BY partsgroup|;
 
2585   if ($p->{language_code}) {
 
2586     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2587                   t.description AS translation
 
2589                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2590                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
2591                 ORDER BY translation|;
 
2592     @values = ($p->{language_code});
 
2595   $self->{all_partsgroup} = selectall_hashref_query($self, $dbh, $query, @values);
 
2598   $main::lxdebug->leave_sub();
 
2601 sub get_pricegroup {
 
2602   $main::lxdebug->enter_sub();
 
2604   my ($self, $myconfig, $p) = @_;
 
2606   my $dbh = $self->dbconnect($myconfig);
 
2608   my $query = qq|SELECT p.id, p.pricegroup
 
2611   $query .= qq| ORDER BY pricegroup|;
 
2614     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2615                 ORDER BY pricegroup|;
 
2618   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
2622   $main::lxdebug->leave_sub();
 
2626 # usage $form->all_years($myconfig, [$dbh])
 
2627 # return list of all years where bookings found
 
2630   $main::lxdebug->enter_sub();
 
2632   my ($self, $myconfig, $dbh) = @_;
 
2636     $dbh = $self->dbconnect($myconfig);
 
2641   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
2642                    (SELECT MAX(transdate) FROM acc_trans)|;
 
2643   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
2645   if ($myconfig->{dateformat} =~ /^yy/) {
 
2646     ($startdate) = split /\W/, $startdate;
 
2647     ($enddate) = split /\W/, $enddate;
 
2649     (@_) = split /\W/, $startdate;
 
2651     (@_) = split /\W/, $enddate;
 
2656   $startdate = substr($startdate,0,4);
 
2657   $enddate = substr($enddate,0,4);
 
2659   while ($enddate >= $startdate) {
 
2660     push @all_years, $enddate--;
 
2663   $dbh->disconnect if $disconnect;
 
2667   $main::lxdebug->leave_sub();