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 #======================================================================
 
  44 use List::Util qw(min max);
 
  56 use List::Util qw(first max min sum);
 
  62     $standard_dbh->disconnect();
 
  68   $main::lxdebug->enter_sub(2);
 
  76   while ($key =~ /\[\+?\]\.|\./) {
 
  77     substr($key, 0, $+[0]) = '';
 
  85       if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
 
  86         push @{ $curr->{$`} }, { };
 
  89       $curr = $curr->{$`}->[-1];
 
  93   $curr->{$key} = $value;
 
  95   $main::lxdebug->leave_sub(2);
 
  97   return \$curr->{$key};
 
 101   $main::lxdebug->enter_sub(2);
 
 106   my @pairs = split(/&/, $input);
 
 109     my ($key, $value) = split(/=/, $_, 2);
 
 110     $self->_store_value($self->unescape($key), $self->unescape($value));
 
 113   $main::lxdebug->leave_sub(2);
 
 116 sub _request_to_hash {
 
 117   $main::lxdebug->enter_sub(2);
 
 122   if (!$ENV{'CONTENT_TYPE'}
 
 123       || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
 
 125     $self->_input_to_hash($input);
 
 127     $main::lxdebug->leave_sub(2);
 
 131   my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
 
 133   my $boundary = '--' . $1;
 
 135   foreach my $line (split m/\n/, $input) {
 
 136     last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
 
 138     if (($line eq $boundary) || ($line eq "$boundary\r")) {
 
 139       ${ $previous } =~ s|\r?\n$|| if $previous;
 
 145       $content_type   = "text/plain";
 
 152     next unless $boundary_found;
 
 154     if (!$headers_done) {
 
 155       $line =~ s/[\r\n]*$//;
 
 162       if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
 
 163         if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
 
 165           substr $line, $-[0], $+[0] - $-[0], "";
 
 168         if ($line =~ m|name\s*=\s*"(.*?)"|i) {
 
 170           substr $line, $-[0], $+[0] - $-[0], "";
 
 173         $previous         = $self->_store_value($name, '');
 
 174         $self->{FILENAME} = $filename if ($filename);
 
 179       if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
 
 186     next unless $previous;
 
 188     ${ $previous } .= "${line}\n";
 
 191   ${ $previous } =~ s|\r?\n$|| if $previous;
 
 193   $main::lxdebug->leave_sub(2);
 
 197   $main::lxdebug->enter_sub();
 
 203   if ($LXDebug::watch_form) {
 
 204     require SL::Watchdog;
 
 205     tie %{ $self }, 'SL::Watchdog';
 
 208   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 210   if ($ENV{QUERY_STRING}) {
 
 211     $_ = $ENV{QUERY_STRING};
 
 220   $self->_request_to_hash($_);
 
 222   $self->{action}  =  lc $self->{action};
 
 223   $self->{action}  =~ s/( |-|,|\#)/_/g;
 
 225   $self->{version} =  "2.4.3";
 
 227   $main::lxdebug->leave_sub();
 
 232 sub _flatten_variables_rec {
 
 233   $main::lxdebug->enter_sub(2);
 
 242   if ('' eq ref $curr->{$key}) {
 
 243     @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
 
 245   } elsif ('HASH' eq ref $curr->{$key}) {
 
 246     foreach my $hash_key (sort keys %{ $curr->{$key} }) {
 
 247       push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
 
 251     foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
 
 252       my $first_array_entry = 1;
 
 254       foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) {
 
 255         push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
 
 256         $first_array_entry = 0;
 
 261   $main::lxdebug->leave_sub(2);
 
 266 sub flatten_variables {
 
 267   $main::lxdebug->enter_sub(2);
 
 275     push @variables, $self->_flatten_variables_rec($self, '', $_);
 
 278   $main::lxdebug->leave_sub(2);
 
 283 sub flatten_standard_variables {
 
 284   $main::lxdebug->enter_sub(2);
 
 287   my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
 
 291   foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
 
 292     push @variables, $self->_flatten_variables_rec($self, '', $_);
 
 295   $main::lxdebug->leave_sub(2);
 
 301   $main::lxdebug->enter_sub();
 
 307   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 309   $main::lxdebug->leave_sub();
 
 313   $main::lxdebug->enter_sub(2);
 
 316   my $password      = $self->{password};
 
 318   $self->{password} = 'X' x 8;
 
 320   local $Data::Dumper::Sortkeys = 1;
 
 321   my $output                    = Dumper($self);
 
 323   $self->{password} = $password;
 
 325   $main::lxdebug->leave_sub(2);
 
 331   $main::lxdebug->enter_sub(2);
 
 333   my ($self, $str) = @_;
 
 335   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 337   $main::lxdebug->leave_sub(2);
 
 343   $main::lxdebug->enter_sub(2);
 
 345   my ($self, $str) = @_;
 
 350   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 352   $main::lxdebug->leave_sub(2);
 
 358   my ($self, $str) = @_;
 
 360   if ($str && !ref($str)) {
 
 361     $str =~ s/\"/"/g;
 
 369   my ($self, $str) = @_;
 
 371   if ($str && !ref($str)) {
 
 372     $str =~ s/"/\"/g;
 
 383     map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 385     for (sort keys %$self) {
 
 386       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 387       print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 394   $main::lxdebug->enter_sub();
 
 396   $main::lxdebug->show_backtrace();
 
 398   my ($self, $msg) = @_;
 
 399   if ($ENV{HTTP_USER_AGENT}) {
 
 401     $self->show_generic_error($msg);
 
 408   $main::lxdebug->leave_sub();
 
 412   $main::lxdebug->enter_sub();
 
 414   my ($self, $msg) = @_;
 
 416   if ($ENV{HTTP_USER_AGENT}) {
 
 419     if (!$self->{header}) {
 
 432     if ($self->{info_function}) {
 
 433       &{ $self->{info_function} }($msg);
 
 439   $main::lxdebug->leave_sub();
 
 442 # calculates the number of rows in a textarea based on the content and column number
 
 443 # can be capped with maxrows
 
 445   $main::lxdebug->enter_sub();
 
 446   my ($self, $str, $cols, $maxrows, $minrows) = @_;
 
 450   my $rows   = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
 
 453   $main::lxdebug->leave_sub();
 
 455   return max(min($rows, $maxrows), $minrows);
 
 459   $main::lxdebug->enter_sub();
 
 461   my ($self, $msg) = @_;
 
 463   $self->error("$msg\n" . $DBI::errstr);
 
 465   $main::lxdebug->leave_sub();
 
 469   $main::lxdebug->enter_sub();
 
 471   my ($self, $name, $msg) = @_;
 
 474   foreach my $part (split /\./, $name) {
 
 475     if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
 
 478     $curr = $curr->{$part};
 
 481   $main::lxdebug->leave_sub();
 
 484 sub create_http_response {
 
 485   $main::lxdebug->enter_sub();
 
 490   my $cgi      = $main::cgi;
 
 491   $cgi       ||= CGI->new('');
 
 495   if ($ENV{HTTP_X_FORWARDED_FOR}) {
 
 496     $base_path =  $ENV{HTTP_REFERER};
 
 497     $base_path =~ s|^.*?://.*?/|/|;
 
 499     $base_path =  $ENV{REQUEST_URI};
 
 501   $base_path =~ s|[^/]+$||;
 
 502   $base_path =~ s|/$||;
 
 505   if (defined $main::auth) {
 
 506     my $session_cookie_value   = $main::auth->get_session_id();
 
 507     $session_cookie_value    ||= 'NO_SESSION';
 
 509     $session_cookie = $cgi->cookie('-name'  => $main::auth->get_session_cookie_name(),
 
 510                                    '-value' => $session_cookie_value,
 
 511                                    '-path'  => $base_path);
 
 514   my %cgi_params = ('-type' => $params{content_type});
 
 515   $cgi_params{'-charset'} = $params{charset} if ($parmas{charset});
 
 517   my $output = $cgi->header('-cookie' => $session_cookie,
 
 520   $main::lxdebug->leave_sub();
 
 527   $main::lxdebug->enter_sub();
 
 529   my ($self, $extra_code) = @_;
 
 531   if ($self->{header}) {
 
 532     $main::lxdebug->leave_sub();
 
 536   my ($stylesheet, $favicon);
 
 538   if ($ENV{HTTP_USER_AGENT}) {
 
 541     if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) {
 
 542       # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise.
 
 543       $doctype = qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n|;
 
 546     my $stylesheets = "$self->{stylesheet} $self->{stylesheets}";
 
 548     $stylesheets =~ s|^\s*||;
 
 549     $stylesheets =~ s|\s*$||;
 
 550     foreach my $file (split m/\s+/, $stylesheets) {
 
 552       next if (! -f "css/$file");
 
 554       $stylesheet .= qq|<link rel="stylesheet" href="css/$file" TYPE="text/css" TITLE="Lx-Office stylesheet">\n|;
 
 557     $self->{favicon}    = "favicon.ico" unless $self->{favicon};
 
 559     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 561         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 565     my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
 567     if ($self->{landscape}) {
 
 568       $pagelayout = qq|<style type="text/css">
 
 569                         \@page { size:landscape; }
 
 573     my $fokus = qq|  document.$self->{fokus}.focus();| if ($self->{"fokus"});
 
 577     if ($self->{jsscript} == 1) {
 
 580         <script type="text/javascript" src="js/common.js"></script>
 
 581         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 582         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 583         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 584         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 591       ? "$self->{title} - $self->{titlebar}"
 
 594     foreach $item (@ { $self->{AJAX} }) {
 
 595       $ajax .= $item->show_javascript();
 
 598     print $self->create_http_response('content_type' => 'text/html',
 
 599                                       'charset'      => $db_charset,);
 
 600     print qq|${doctype}<html>
 
 602   <title>$self->{titlebar}</title>
 
 606   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=${db_charset}">
 
 610   <script type="text/javascript">
 
 618   <meta name="robots" content="noindex,nofollow" />
 
 619   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 620   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 622   <script type="text/javascript" src="js/tabcontent.js">
 
 624   /***********************************************
 
 625   * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 626   * This notice MUST stay intact for legal use
 
 627   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 628   ***********************************************/
 
 639   $main::lxdebug->leave_sub();
 
 642 sub _prepare_html_template {
 
 643   $main::lxdebug->enter_sub();
 
 645   my ($self, $file, $additional_params) = @_;
 
 648   if (!defined(%main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 649     $language = $main::language;
 
 651     $language = $main::myconfig{"countrycode"};
 
 653   $language = "de" unless ($language);
 
 655   if (-f "templates/webpages/${file}_${language}.html") {
 
 656     if ((-f ".developer") &&
 
 657         (-f "templates/webpages/${file}_master.html") &&
 
 658         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 659          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 660       my $info = "Developer information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 661         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 662       print(qq|<pre>$info</pre>|);
 
 666     $file = "templates/webpages/${file}_${language}.html";
 
 667   } elsif (-f "templates/webpages/${file}.html") {
 
 668     $file = "templates/webpages/${file}.html";
 
 670     my $info = "Web page template '${file}' not found.\n" .
 
 671       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 672     print(qq|<pre>$info</pre>|);
 
 676   if ($self->{"DEBUG"}) {
 
 677     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 680   if ($additional_params->{"DEBUG"}) {
 
 681     $additional_params->{"DEBUG"} =
 
 682       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 685   if (%main::myconfig) {
 
 686     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 687     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 688     $jsc_dateformat =~ s/d+/\%d/gi;
 
 689     $jsc_dateformat =~ s/m+/\%m/gi;
 
 690     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 691     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 694   $additional_params->{"conf_webdav"}                 = $main::webdav;
 
 695   $additional_params->{"conf_lizenzen"}               = $main::lizenzen;
 
 696   $additional_params->{"conf_latex_templates"}        = $main::latex;
 
 697   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 699   if (%main::debug_options) {
 
 700     map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
 
 703   if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
 
 704     while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
 
 705       $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
 
 709   $main::lxdebug->leave_sub();
 
 714 sub parse_html_template {
 
 715   $main::lxdebug->enter_sub();
 
 717   my ($self, $file, $additional_params) = @_;
 
 719   $additional_params ||= { };
 
 721   $file = $self->_prepare_html_template($file, $additional_params);
 
 723   my $template = Template->new({ 'INTERPOLATE'  => 0,
 
 727                                  'PLUGIN_BASE'  => 'SL::Template::Plugin',
 
 728                                  'INCLUDE_PATH' => '.:templates/webpages',
 
 731   map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
 
 734   if (!$template->process($file, $additional_params, \$output)) {
 
 735     print STDERR $template->error();
 
 738   $output = $main::locale->{iconv}->convert($output) if ($main::locale);
 
 740   $main::lxdebug->leave_sub();
 
 745 sub show_generic_error {
 
 746   my ($self, $error, %params) = @_;
 
 749     'title_error' => $params{title},
 
 750     'label_error' => $error,
 
 753   if ($params{action}) {
 
 756     map { delete($self->{$_}); } qw(action);
 
 757     map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
 
 759     $add_params->{SHOW_BUTTON}  = 1;
 
 760     $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
 
 761     $add_params->{VARIABLES}    = \@vars;
 
 763   } elsif ($params{back_button}) {
 
 764     $add_params->{SHOW_BACK_BUTTON} = 1;
 
 767   $self->{title} = $title if ($title);
 
 770   print $self->parse_html_template("generic/error", $add_params);
 
 772   die("Error: $error\n");
 
 775 sub show_generic_information {
 
 776   my ($self, $text, $title) = @_;
 
 779     'title_information' => $title,
 
 780     'label_information' => $text,
 
 783   $self->{title} = $title if ($title);
 
 786   print $self->parse_html_template("generic/information", $add_params);
 
 788   die("Information: $error\n");
 
 791 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 792 # changed it to accept an arbitrary number of triggers - sschoeling
 
 794   $main::lxdebug->enter_sub();
 
 797   my $myconfig = shift;
 
 800   # set dateform for jsscript
 
 803     "dd.mm.yy" => "%d.%m.%Y",
 
 804     "dd-mm-yy" => "%d-%m-%Y",
 
 805     "dd/mm/yy" => "%d/%m/%Y",
 
 806     "mm/dd/yy" => "%m/%d/%Y",
 
 807     "mm-dd-yy" => "%m-%d-%Y",
 
 808     "yyyy-mm-dd" => "%Y-%m-%d",
 
 811   my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
 
 812     $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
 
 819       inputField : "| . (shift) . qq|",
 
 820       ifFormat :"$ifFormat",
 
 821       align : "| .  (shift) . qq|",
 
 822       button : "| . (shift) . qq|"
 
 828        <script type="text/javascript">
 
 829        <!--| . join("", @triggers) . qq|//-->
 
 833   $main::lxdebug->leave_sub();
 
 836 }    #end sub write_trigger
 
 839   $main::lxdebug->enter_sub();
 
 841   my ($self, $msg) = @_;
 
 843   if ($self->{callback}) {
 
 845     ($script, $argv) = split(/\?/, $self->{callback}, 2);
 
 847     $script =~ s|[^a-zA-Z0-9_\.]||g;
 
 848     exec("perl", "$script", $argv);
 
 856   $main::lxdebug->leave_sub();
 
 859 # sort of columns removed - empty sub
 
 861   $main::lxdebug->enter_sub();
 
 863   my ($self, @columns) = @_;
 
 865   $main::lxdebug->leave_sub();
 
 871   $main::lxdebug->enter_sub(2);
 
 873   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 879   # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13
 
 881   my $neg = ($amount =~ s/^-//);
 
 882   my $exp = ($amount =~ m/[e]/) ? 1 : 0;
 
 884   if (defined($places) && ($places ne '')) {
 
 890         my ($actual_places) = ($amount =~ /\.(\d+)/);
 
 891         $actual_places = length($actual_places);
 
 892         $places = $actual_places > $places ? $actual_places : $places;
 
 895     $amount = $self->round_amount($amount, $places);
 
 898   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 899   my @p = split(/\./, $amount); # split amount at decimal point
 
 901   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 904   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 907     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 908     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 909                         ($neg ? "-$amount"   : "$amount" )    ;
 
 913   $main::lxdebug->leave_sub(2);
 
 917 sub format_amount_units {
 
 918   $main::lxdebug->enter_sub();
 
 923   Common::check_params(\%params, qw(amount part_unit));
 
 925   my $myconfig         = \%main::myconfig;
 
 926   my $amount           = $params{amount};
 
 927   my $places           = $params{places};
 
 928   my $part_unit_name   = $params{part_unit};
 
 929   my $amount_unit_name = $params{amount_unit};
 
 930   my $conv_units       = $params{conv_units};
 
 931   my $max_places       = $params{max_places};
 
 933   AM->retrieve_all_units();
 
 934   my $all_units        = $main::all_units;
 
 936   if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
 
 937     $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
 
 940   if (!scalar @{ $conv_units }) {
 
 941     my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
 
 942     $main::lxdebug->leave_sub();
 
 946   my $part_unit  = $all_units->{$part_unit_name};
 
 947   my $conv_unit  = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
 
 949   $amount       *= $conv_unit->{factor};
 
 953   foreach my $unit (@$conv_units) {
 
 954     my $last = $unit->{name} eq $part_unit->{name};
 
 956       $num     = int($amount / $unit->{factor});
 
 957       $amount -= $num * $unit->{factor};
 
 960     if ($last ? $amount : $num) {
 
 961       push @values, { "unit"   => $unit->{name},
 
 962                       "amount" => $last ? $amount / $unit->{factor} : $num,
 
 963                       "places" => $last ? $places : 0 };
 
 970     push @values, { "unit"   => $part_unit_name,
 
 975   my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
 
 977   $main::lxdebug->leave_sub();
 
 983   $main::lxdebug->enter_sub(2);
 
 988   $input =~ s/(^|[^\#]) \#  (\d+)  /$1$_[$2 - 1]/gx;
 
 989   $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
 
 990   $input =~ s/\#\#/\#/g;
 
 992   $main::lxdebug->leave_sub(2);
 
1000   $main::lxdebug->enter_sub(2);
 
1002   my ($self, $myconfig, $amount) = @_;
 
1004   if (   ($myconfig->{numberformat} eq '1.000,00')
 
1005       || ($myconfig->{numberformat} eq '1000,00')) {
 
1010   if ($myconfig->{numberformat} eq "1'000.00") {
 
1016   $main::lxdebug->leave_sub(2);
 
1018   return ($amount * 1);
 
1022   $main::lxdebug->enter_sub(2);
 
1024   my ($self, $amount, $places) = @_;
 
1027   # Rounding like "Kaufmannsrunden"
 
1028   # Descr. http://de.wikipedia.org/wiki/Rundung
 
1030   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
1033   $amount = $amount * (10**($places));
 
1034   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
1036   $main::lxdebug->leave_sub(2);
 
1038   return $round_amount;
 
1042 sub parse_template {
 
1043   $main::lxdebug->enter_sub();
 
1045   my ($self, $myconfig, $userspath) = @_;
 
1046   my ($template, $out);
 
1050   $self->{"cwd"} = getcwd();
 
1051   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
1053   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
1054     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1055   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
1056     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
1057     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1058   } elsif (($self->{"format"} =~ /html/i) ||
 
1059            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
1060     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1061   } elsif (($self->{"format"} =~ /xml/i) ||
 
1062              (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
1063     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1064   } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
 
1065     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1066   } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
 
1067     $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
1068   } elsif ( defined $self->{'format'}) {
 
1069     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
1070   } elsif ( $self->{'format'} eq '' ) {
 
1071     $self->error("No Outputformat given: $self->{'format'}");
 
1072   } else { #Catch the rest
 
1073     $self->error("Outputformat not defined: $self->{'format'}");
 
1076   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
1077   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
1079   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
1080       qw(email tel fax name signature company address businessnumber
 
1081          co_ustid taxnumber duns));
 
1083   map({ $self->{"${_}"} = $myconfig->{$_}; }
 
1087   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
1089   # OUT is used for the media, screen, printer, email
 
1090   # for postscript we store a copy in a temporary file
 
1092   my $prepend_userspath;
 
1094   if (!$self->{tmpfile}) {
 
1095     $self->{tmpfile}   = "${fileid}.$self->{IN}";
 
1096     $prepend_userspath = 1;
 
1099   $prepend_userspath = 1 if substr($self->{tmpfile}, 0, length $userspath) eq $userspath;
 
1101   $self->{tmpfile} =~ s|.*/||;
 
1102   $self->{tmpfile} =~ s/[^a-zA-Z0-9\._\ \-]//g;
 
1103   $self->{tmpfile} = "$userspath/$self->{tmpfile}" if $prepend_userspath;
 
1105   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
1106     $out = $self->{OUT};
 
1107     $self->{OUT} = ">$self->{tmpfile}";
 
1111     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
1113     open(OUT, ">-") or $self->error("STDOUT : $!");
 
1117   if (!$template->parse(*OUT)) {
 
1119     $self->error("$self->{IN} : " . $template->get_error());
 
1124   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
1126     if ($self->{media} eq 'email') {
 
1128       my $mail = new Mailer;
 
1130       map { $mail->{$_} = $self->{$_} }
 
1131         qw(cc bcc subject message version format);
 
1132       $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
 
1133       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
1134       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
1135       $mail->{fileid} = "$fileid.";
 
1136       $myconfig->{signature} =~ s/\r//g;
 
1138       # if we send html or plain text inline
 
1139       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
1140         $mail->{contenttype} = "text/html";
 
1142         $mail->{message}       =~ s/\r//g;
 
1143         $mail->{message}       =~ s/\n/<br>\n/g;
 
1144         $myconfig->{signature} =~ s/\n/<br>\n/g;
 
1145         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
1147         open(IN, $self->{tmpfile})
 
1148           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1150           $mail->{message} .= $_;
 
1157         if (!$self->{"do_not_attach"}) {
 
1158           @{ $mail->{attachments} } =
 
1159             ({ "filename" => $self->{"tmpfile"},
 
1160                "name" => $self->{"attachment_filename"} ?
 
1161                  $self->{"attachment_filename"} : $self->{"tmpfile"} });
 
1164         $mail->{message}  =~ s/\r//g;
 
1165         $mail->{message} .=  "\n-- \n$myconfig->{signature}";
 
1169       my $err = $mail->send();
 
1170       $self->error($self->cleanup . "$err") if ($err);
 
1174       $self->{OUT} = $out;
 
1176       my $numbytes = (-s $self->{tmpfile});
 
1177       open(IN, $self->{tmpfile})
 
1178         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1180       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
1182       chdir("$self->{cwd}");
 
1183       #print(STDERR "Kopien $self->{copies}\n");
 
1184       #print(STDERR "OUT $self->{OUT}\n");
 
1185       for my $i (1 .. $self->{copies}) {
 
1187           open(OUT, $self->{OUT})
 
1188             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
1190           $self->{attachment_filename} = ($self->{attachment_filename}) 
 
1191                                        ? $self->{attachment_filename}
 
1192                                        : $self->generate_attachment_filename();
 
1194           # launch application
 
1195           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
1196 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
1197 Content-Length: $numbytes
 
1201           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
1221   chdir("$self->{cwd}");
 
1222   $main::lxdebug->leave_sub();
 
1225 sub get_formname_translation {
 
1226   my ($self, $formname) = @_;
 
1228   $formname ||= $self->{formname};
 
1230   my %formname_translations = (
 
1231     bin_list                => $main::locale->text('Bin List'),
 
1232     credit_note             => $main::locale->text('Credit Note'),
 
1233     invoice                 => $main::locale->text('Invoice'),
 
1234     packing_list            => $main::locale->text('Packing List'),
 
1235     pick_list               => $main::locale->text('Pick List'),
 
1236     proforma                => $main::locale->text('Proforma Invoice'),
 
1237     purchase_order          => $main::locale->text('Purchase Order'),
 
1238     request_quotation       => $main::locale->text('RFQ'),
 
1239     sales_order             => $main::locale->text('Confirmation'),
 
1240     sales_quotation         => $main::locale->text('Quotation'),
 
1241     storno_invoice          => $main::locale->text('Storno Invoice'),
 
1242     storno_packing_list     => $main::locale->text('Storno Packing List'),
 
1243     sales_delivery_order    => $main::locale->text('Delivery Order'),
 
1244     purchase_delivery_order => $main::locale->text('Delivery Order'),
 
1247   return $formname_translations{$formname}
 
1250 sub get_number_prefix_for_type {
 
1254       (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
 
1255     : ($self->{type} =~ /_quotation$/)                        ? 'quo'
 
1256     : ($self->{type} =~ /_delivery_order$/)                   ? 'do'
 
1262 sub get_extension_for_format {
 
1265   my $extension = $self->{format} =~ /pdf/i          ? ".pdf"
 
1266                 : $self->{format} =~ /postscript/i   ? ".ps"
 
1267                 : $self->{format} =~ /opendocument/i ? ".odt"
 
1268                 : $self->{format} =~ /html/i         ? ".html"
 
1274 sub generate_attachment_filename {
 
1277   my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
 
1278   my $prefix              = $self->get_number_prefix_for_type();
 
1280   if ($attachment_filename && $self->{"${prefix}number"}) {
 
1281     $attachment_filename .=  "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
 
1282     $attachment_filename  =  $main::locale->quote_special_chars('filenames', $attachment_filename);
 
1283     $attachment_filename  =~ s|[\s/\\]+|_|g;
 
1285     $attachment_filename = "";
 
1288   return $attachment_filename;
 
1291 sub generate_email_subject {
 
1294   my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
 
1295   my $prefix  = $self->get_number_prefix_for_type();
 
1297   if ($subject && $self->{"${prefix}number"}) {
 
1298     $subject .= " " . $self->{"${prefix}number"}
 
1305   $main::lxdebug->enter_sub();
 
1309   chdir("$self->{tmpdir}");
 
1312   if (-f "$self->{tmpfile}.err") {
 
1313     open(FH, "$self->{tmpfile}.err");
 
1318   if ($self->{tmpfile}) {
 
1319     $self->{tmpfile} =~ s|.*/||g;
 
1321     $self->{tmpfile} =~ s/\.\w+$//g;
 
1322     my $tmpfile = $self->{tmpfile};
 
1323     unlink(<$tmpfile.*>);
 
1326   chdir("$self->{cwd}");
 
1328   $main::lxdebug->leave_sub();
 
1334   $main::lxdebug->enter_sub();
 
1336   my ($self, $date, $myconfig) = @_;
 
1338   if ($date && $date =~ /\D/) {
 
1340     if ($myconfig->{dateformat} =~ /^yy/) {
 
1341       ($yy, $mm, $dd) = split /\D/, $date;
 
1343     if ($myconfig->{dateformat} =~ /^mm/) {
 
1344       ($mm, $dd, $yy) = split /\D/, $date;
 
1346     if ($myconfig->{dateformat} =~ /^dd/) {
 
1347       ($dd, $mm, $yy) = split /\D/, $date;
 
1352     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
1353     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
1355     $dd = "0$dd" if ($dd < 10);
 
1356     $mm = "0$mm" if ($mm < 10);
 
1358     $date = "$yy$mm$dd";
 
1361   $main::lxdebug->leave_sub();
 
1366 # Database routines used throughout
 
1369   $main::lxdebug->enter_sub(2);
 
1371   my ($self, $myconfig) = @_;
 
1373   # connect to database
 
1375     DBI->connect($myconfig->{dbconnect},
 
1376                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
1380   if ($myconfig->{dboptions}) {
 
1381     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1384   $main::lxdebug->leave_sub(2);
 
1389 sub dbconnect_noauto {
 
1390   $main::lxdebug->enter_sub();
 
1392   my ($self, $myconfig) = @_;
 
1394   # connect to database
 
1396     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
1397                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
1401   if ($myconfig->{dboptions}) {
 
1402     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1405   $main::lxdebug->leave_sub();
 
1410 sub get_standard_dbh {
 
1411   $main::lxdebug->enter_sub(2);
 
1413   my ($self, $myconfig) = @_;
 
1415   if ($standard_dbh && !$standard_dbh->{Active}) {
 
1416     $main::lxdebug->message(LXDebug::INFO, "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
 
1417     undef $standard_dbh;
 
1420   $standard_dbh ||= $self->dbconnect_noauto($myconfig);
 
1422   $main::lxdebug->leave_sub(2);
 
1424   return $standard_dbh;
 
1428   $main::lxdebug->enter_sub();
 
1430   my ($self, $date, $myconfig) = @_;
 
1431   my $dbh = $self->dbconnect($myconfig);
 
1433   my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
 
1434   my $sth = prepare_execute_query($self, $dbh, $query, $date);
 
1435   my ($closed) = $sth->fetchrow_array;
 
1437   $main::lxdebug->leave_sub();
 
1442 sub update_balance {
 
1443   $main::lxdebug->enter_sub();
 
1445   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
1447   # if we have a value, go do it
 
1450     # retrieve balance from table
 
1451     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1452     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1453     my ($balance) = $sth->fetchrow_array;
 
1459     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1460     do_query($self, $dbh, $query, @values);
 
1462   $main::lxdebug->leave_sub();
 
1465 sub update_exchangerate {
 
1466   $main::lxdebug->enter_sub();
 
1468   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1470   # some sanity check for currency
 
1472     $main::lxdebug->leave_sub();
 
1475   $query = qq|SELECT curr FROM defaults|;
 
1477   my ($currency) = selectrow_query($self, $dbh, $query);
 
1478   my ($defaultcurrency) = split m/:/, $currency;
 
1481   if ($curr eq $defaultcurrency) {
 
1482     $main::lxdebug->leave_sub();
 
1486   $query = qq|SELECT e.curr FROM exchangerate e
 
1487                  WHERE e.curr = ? AND e.transdate = ?
 
1489   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1498   $buy = conv_i($buy, "NULL");
 
1499   $sell = conv_i($sell, "NULL");
 
1502   if ($buy != 0 && $sell != 0) {
 
1503     $set = "buy = $buy, sell = $sell";
 
1504   } elsif ($buy != 0) {
 
1505     $set = "buy = $buy";
 
1506   } elsif ($sell != 0) {
 
1507     $set = "sell = $sell";
 
1510   if ($sth->fetchrow_array) {
 
1511     $query = qq|UPDATE exchangerate
 
1517     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1518                 VALUES (?, $buy, $sell, ?)|;
 
1521   do_query($self, $dbh, $query, $curr, $transdate);
 
1523   $main::lxdebug->leave_sub();
 
1526 sub save_exchangerate {
 
1527   $main::lxdebug->enter_sub();
 
1529   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1531   my $dbh = $self->dbconnect($myconfig);
 
1535   $buy  = $rate if $fld eq 'buy';
 
1536   $sell = $rate if $fld eq 'sell';
 
1539   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1544   $main::lxdebug->leave_sub();
 
1547 sub get_exchangerate {
 
1548   $main::lxdebug->enter_sub();
 
1550   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1553   unless ($transdate) {
 
1554     $main::lxdebug->leave_sub();
 
1558   $query = qq|SELECT curr FROM defaults|;
 
1560   my ($currency) = selectrow_query($self, $dbh, $query);
 
1561   my ($defaultcurrency) = split m/:/, $currency;
 
1563   if ($currency eq $defaultcurrency) {
 
1564     $main::lxdebug->leave_sub();
 
1568   $query = qq|SELECT e.$fld FROM exchangerate e
 
1569                  WHERE e.curr = ? AND e.transdate = ?|;
 
1570   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1574   $main::lxdebug->leave_sub();
 
1576   return $exchangerate;
 
1579 sub check_exchangerate {
 
1580   $main::lxdebug->enter_sub();
 
1582   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1584   unless ($transdate) {
 
1585     $main::lxdebug->leave_sub();
 
1589   my ($defaultcurrency) = $self->get_default_currency($myconfig);
 
1591   if ($currency eq $defaultcurrency) {
 
1592     $main::lxdebug->leave_sub();
 
1596   my $dbh   = $self->get_standard_dbh($myconfig);
 
1597   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1598                  WHERE e.curr = ? AND e.transdate = ?|;
 
1600   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1602   $main::lxdebug->leave_sub();
 
1604   return $exchangerate;
 
1607 sub get_default_currency {
 
1608   $main::lxdebug->enter_sub();
 
1610   my ($self, $myconfig) = @_;
 
1611   my $dbh = $self->get_standard_dbh($myconfig);
 
1613   my $query = qq|SELECT curr FROM defaults|;
 
1615   my ($curr)            = selectrow_query($self, $dbh, $query);
 
1616   my ($defaultcurrency) = split m/:/, $curr;
 
1618   $main::lxdebug->leave_sub();
 
1620   return $defaultcurrency;
 
1624 sub set_payment_options {
 
1625   $main::lxdebug->enter_sub();
 
1627   my ($self, $myconfig, $transdate) = @_;
 
1629   return $main::lxdebug->leave_sub() unless ($self->{payment_id});
 
1631   my $dbh = $self->get_standard_dbh($myconfig);
 
1634     qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
 
1635     qq|FROM payment_terms p | .
 
1638   ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1639    $self->{payment_terms}) =
 
1640      selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1642   if ($transdate eq "") {
 
1643     if ($self->{invdate}) {
 
1644       $transdate = $self->{invdate};
 
1646       $transdate = $self->{transdate};
 
1651     qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1652     qq|FROM payment_terms|;
 
1653   ($self->{netto_date}, $self->{skonto_date}) =
 
1654     selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1656   my ($invtotal, $total);
 
1657   my (%amounts, %formatted_amounts);
 
1659   if ($self->{type} =~ /_order$/) {
 
1660     $amounts{invtotal} = $self->{ordtotal};
 
1661     $amounts{total}    = $self->{ordtotal};
 
1663   } elsif ($self->{type} =~ /_quotation$/) {
 
1664     $amounts{invtotal} = $self->{quototal};
 
1665     $amounts{total}    = $self->{quototal};
 
1668     $amounts{invtotal} = $self->{invtotal};
 
1669     $amounts{total}    = $self->{total};
 
1672   map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
 
1674   $amounts{skonto_amount}      = $amounts{invtotal} * $self->{percent_skonto};
 
1675   $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
 
1676   $amounts{total_wo_skonto}    = $amounts{total}    * (1 - $self->{percent_skonto});
 
1678   foreach (keys %amounts) {
 
1679     $amounts{$_}           = $self->round_amount($amounts{$_}, 2);
 
1680     $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
 
1683   if ($self->{"language_id"}) {
 
1685       qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1686       qq|FROM translation_payment_terms t | .
 
1687       qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1688       qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
 
1689     my ($description_long, $output_numberformat, $output_dateformat,
 
1690       $output_longdates) =
 
1691       selectrow_query($self, $dbh, $query,
 
1692                       $self->{"language_id"}, $self->{"payment_id"});
 
1694     $self->{payment_terms} = $description_long if ($description_long);
 
1696     if ($output_dateformat) {
 
1697       foreach my $key (qw(netto_date skonto_date)) {
 
1699           $main::locale->reformat_date($myconfig, $self->{$key},
 
1705     if ($output_numberformat &&
 
1706         ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1707       my $saved_numberformat = $myconfig->{"numberformat"};
 
1708       $myconfig->{"numberformat"} = $output_numberformat;
 
1709       map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
 
1710       $myconfig->{"numberformat"} = $saved_numberformat;
 
1714   $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1715   $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1716   $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1717   $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1718   $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1719   $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1720   $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1722   map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
 
1724   $main::lxdebug->leave_sub();
 
1728 sub get_template_language {
 
1729   $main::lxdebug->enter_sub();
 
1731   my ($self, $myconfig) = @_;
 
1733   my $template_code = "";
 
1735   if ($self->{language_id}) {
 
1736     my $dbh = $self->get_standard_dbh($myconfig);
 
1737     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1738     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1741   $main::lxdebug->leave_sub();
 
1743   return $template_code;
 
1746 sub get_printer_code {
 
1747   $main::lxdebug->enter_sub();
 
1749   my ($self, $myconfig) = @_;
 
1751   my $template_code = "";
 
1753   if ($self->{printer_id}) {
 
1754     my $dbh = $self->get_standard_dbh($myconfig);
 
1755     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1756     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1759   $main::lxdebug->leave_sub();
 
1761   return $template_code;
 
1765   $main::lxdebug->enter_sub();
 
1767   my ($self, $myconfig) = @_;
 
1769   my $template_code = "";
 
1771   if ($self->{shipto_id}) {
 
1772     my $dbh = $self->get_standard_dbh($myconfig);
 
1773     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1774     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1775     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1778   $main::lxdebug->leave_sub();
 
1782   $main::lxdebug->enter_sub();
 
1784   my ($self, $dbh, $id, $module) = @_;
 
1789   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1790                        contact phone fax email)) {
 
1791     if ($self->{"shipto$item"}) {
 
1792       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1794     push(@values, $self->{"shipto${item}"});
 
1798     if ($self->{shipto_id}) {
 
1799       my $query = qq|UPDATE shipto set
 
1801                        shiptodepartment_1 = ?,
 
1802                        shiptodepartment_2 = ?,
 
1811                      WHERE shipto_id = ?|;
 
1812       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1814       my $query = qq|SELECT * FROM shipto
 
1815                      WHERE shiptoname = ? AND
 
1816                        shiptodepartment_1 = ? AND
 
1817                        shiptodepartment_2 = ? AND
 
1818                        shiptostreet = ? AND
 
1819                        shiptozipcode = ? AND
 
1821                        shiptocountry = ? AND
 
1822                        shiptocontact = ? AND
 
1828       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
 
1831           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1832                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1833                                  shiptocontact, shiptophone, shiptofax, shiptoemail, module)
 
1834              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1835         do_query($self, $dbh, $query, $id, @values, $module);
 
1840   $main::lxdebug->leave_sub();
 
1844   $main::lxdebug->enter_sub();
 
1846   my ($self, $dbh) = @_;
 
1848   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1849   ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1850   $self->{"employee_id"} *= 1;
 
1852   $main::lxdebug->leave_sub();
 
1856   $main::lxdebug->enter_sub();
 
1858   my ($self, $myconfig, $salesman_id) = @_;
 
1860   $main::lxdebug->leave_sub() and return unless $salesman_id;
 
1862   my $dbh = $self->get_standard_dbh($myconfig);
 
1865     selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|,
 
1869     my $user = new User($main::memberfile, $login);
 
1870     map({ $self->{"salesman_$_"} = $user->{$_}; }
 
1871         qw(address businessnumber co_ustid company duns email fax name
 
1873     $self->{salesman_login} = $login;
 
1875     $self->{salesman_name} = $login
 
1876       if ($self->{salesman_name} eq "");
 
1879   $main::lxdebug->leave_sub();
 
1883   $main::lxdebug->enter_sub();
 
1885   my ($self, $myconfig) = @_;
 
1887   my $dbh = $self->get_standard_dbh($myconfig);
 
1888   my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
 
1889   ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1891   $main::lxdebug->leave_sub();
 
1895   $main::lxdebug->enter_sub();
 
1897   my ($self, $dbh, $id, $key) = @_;
 
1899   $key = "all_contacts" unless ($key);
 
1902     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1903     qq|FROM contacts | .
 
1904     qq|WHERE cp_cv_id = ? | .
 
1905     qq|ORDER BY lower(cp_name)|;
 
1907   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1909   $main::lxdebug->leave_sub();
 
1913   $main::lxdebug->enter_sub();
 
1915   my ($self, $dbh, $key) = @_;
 
1917   my ($all, $old_id, $where, @values);
 
1919   if (ref($key) eq "HASH") {
 
1922     $key = "ALL_PROJECTS";
 
1924     foreach my $p (keys(%{$params})) {
 
1926         $all = $params->{$p};
 
1927       } elsif ($p eq "old_id") {
 
1928         $old_id = $params->{$p};
 
1929       } elsif ($p eq "key") {
 
1930         $key = $params->{$p};
 
1936     $where = "WHERE active ";
 
1938       if (ref($old_id) eq "ARRAY") {
 
1939         my @ids = grep({ $_ } @{$old_id});
 
1941           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
1942           push(@values, @ids);
 
1945         $where .= " OR (id = ?) ";
 
1946         push(@values, $old_id);
 
1952     qq|SELECT id, projectnumber, description, active | .
 
1955     qq|ORDER BY lower(projectnumber)|;
 
1957   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
1959   $main::lxdebug->leave_sub();
 
1963   $main::lxdebug->enter_sub();
 
1965   my ($self, $dbh, $vc_id, $key) = @_;
 
1967   $key = "all_shipto" unless ($key);
 
1969   # get shipping addresses
 
1970   my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
 
1972   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
1974   $main::lxdebug->leave_sub();
 
1978   $main::lxdebug->enter_sub();
 
1980   my ($self, $dbh, $key) = @_;
 
1982   $key = "all_printers" unless ($key);
 
1984   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
1986   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
1988   $main::lxdebug->leave_sub();
 
1992   $main::lxdebug->enter_sub();
 
1994   my ($self, $dbh, $params) = @_;
 
1996   $key = $params->{key};
 
1997   $key = "all_charts" unless ($key);
 
1999   my $transdate = quote_db_date($params->{transdate});
 
2002     qq|SELECT c.id, c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id | .
 
2004     qq|LEFT JOIN taxkeys tk ON | .
 
2005     qq|(tk.id = (SELECT id FROM taxkeys | .
 
2006     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
2007     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
2008     qq|ORDER BY c.accno|;
 
2010   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2012   $main::lxdebug->leave_sub();
 
2015 sub _get_taxcharts {
 
2016   $main::lxdebug->enter_sub();
 
2018   my ($self, $dbh, $key) = @_;
 
2020   $key = "all_taxcharts" unless ($key);
 
2022   my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
2024   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2026   $main::lxdebug->leave_sub();
 
2030   $main::lxdebug->enter_sub();
 
2032   my ($self, $dbh, $key) = @_;
 
2034   $key = "all_taxzones" unless ($key);
 
2036   my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
 
2038   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2040   $main::lxdebug->leave_sub();
 
2043 sub _get_employees {
 
2044   $main::lxdebug->enter_sub();
 
2046   my ($self, $dbh, $default_key, $key) = @_;
 
2048   $key = $default_key unless ($key);
 
2049   $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
 
2051   $main::lxdebug->leave_sub();
 
2054 sub _get_business_types {
 
2055   $main::lxdebug->enter_sub();
 
2057   my ($self, $dbh, $key) = @_;
 
2059   $key = "all_business_types" unless ($key);
 
2061     selectall_hashref_query($self, $dbh, qq|SELECT * FROM business|);
 
2063   $main::lxdebug->leave_sub();
 
2066 sub _get_languages {
 
2067   $main::lxdebug->enter_sub();
 
2069   my ($self, $dbh, $key) = @_;
 
2071   $key = "all_languages" unless ($key);
 
2073   my $query = qq|SELECT * FROM language ORDER BY id|;
 
2075   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2077   $main::lxdebug->leave_sub();
 
2080 sub _get_dunning_configs {
 
2081   $main::lxdebug->enter_sub();
 
2083   my ($self, $dbh, $key) = @_;
 
2085   $key = "all_dunning_configs" unless ($key);
 
2087   my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
 
2089   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2091   $main::lxdebug->leave_sub();
 
2094 sub _get_currencies {
 
2095 $main::lxdebug->enter_sub();
 
2097   my ($self, $dbh, $key) = @_;
 
2099   $key = "all_currencies" unless ($key);
 
2101   my $query = qq|SELECT curr AS currency FROM defaults|;
 
2103   $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})];
 
2105   $main::lxdebug->leave_sub();
 
2109 $main::lxdebug->enter_sub();
 
2111   my ($self, $dbh, $key) = @_;
 
2113   $key = "all_payments" unless ($key);
 
2115   my $query = qq|SELECT * FROM payment_terms ORDER BY id|;
 
2117   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2119   $main::lxdebug->leave_sub();
 
2122 sub _get_customers {
 
2123   $main::lxdebug->enter_sub();
 
2125   my ($self, $dbh, $key, $limit) = @_;
 
2127   $key = "all_customers" unless ($key);
 
2128   $limit_clause = "LIMIT $limit" if $limit;
 
2130   my $query = qq|SELECT * FROM customer WHERE NOT obsolete ORDER BY name $limit_clause|;
 
2132   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2134   $main::lxdebug->leave_sub();
 
2138   $main::lxdebug->enter_sub();
 
2140   my ($self, $dbh, $key) = @_;
 
2142   $key = "all_vendors" unless ($key);
 
2144   my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
 
2146   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2148   $main::lxdebug->leave_sub();
 
2151 sub _get_departments {
 
2152   $main::lxdebug->enter_sub();
 
2154   my ($self, $dbh, $key) = @_;
 
2156   $key = "all_departments" unless ($key);
 
2158   my $query = qq|SELECT * FROM department ORDER BY description|;
 
2160   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2162   $main::lxdebug->leave_sub();
 
2165 sub _get_warehouses {
 
2166   $main::lxdebug->enter_sub();
 
2168   my ($self, $dbh, $param) = @_;
 
2170   my ($key, $bins_key);
 
2172   if ('' eq ref $param) {
 
2176     $key      = $param->{key};
 
2177     $bins_key = $param->{bins};
 
2180   my $query = qq|SELECT w.* FROM warehouse w
 
2181                  WHERE (NOT w.invalid) AND
 
2182                    ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
 
2183                  ORDER BY w.sortkey|;
 
2185   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2188     $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?|;
 
2189     my $sth = prepare_query($self, $dbh, $query);
 
2191     foreach my $warehouse (@{ $self->{$key} }) {
 
2192       do_statement($self, $sth, $query, $warehouse->{id});
 
2193       $warehouse->{$bins_key} = [];
 
2195       while (my $ref = $sth->fetchrow_hashref()) {
 
2196         push @{ $warehouse->{$bins_key} }, $ref;
 
2202   $main::lxdebug->leave_sub();
 
2206   $main::lxdebug->enter_sub();
 
2208   my ($self, $dbh, $table, $key, $sortkey) = @_;
 
2210   my $query  = qq|SELECT * FROM $table|;
 
2211   $query    .= qq| ORDER BY $sortkey| if ($sortkey);
 
2213   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2215   $main::lxdebug->leave_sub();
 
2219   $main::lxdebug->enter_sub();
 
2221   my ($self, $dbh, $key) = @_;
 
2223   $key ||= "all_groups";
 
2225   my $groups = $main::auth->read_groups();
 
2227   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2229   $main::lxdebug->leave_sub();
 
2233   $main::lxdebug->enter_sub();
 
2238   my $dbh = $self->get_standard_dbh(\%main::myconfig);
 
2239   my ($sth, $query, $ref);
 
2241   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
2242   my $vc_id = $self->{"${vc}_id"};
 
2244   if ($params{"contacts"}) {
 
2245     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
2248   if ($params{"shipto"}) {
 
2249     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
2252   if ($params{"projects"} || $params{"all_projects"}) {
 
2253     $self->_get_projects($dbh, $params{"all_projects"} ?
 
2254                          $params{"all_projects"} : $params{"projects"},
 
2255                          $params{"all_projects"} ? 1 : 0);
 
2258   if ($params{"printers"}) {
 
2259     $self->_get_printers($dbh, $params{"printers"});
 
2262   if ($params{"languages"}) {
 
2263     $self->_get_languages($dbh, $params{"languages"});
 
2266   if ($params{"charts"}) {
 
2267     $self->_get_charts($dbh, $params{"charts"});
 
2270   if ($params{"taxcharts"}) {
 
2271     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
2274   if ($params{"taxzones"}) {
 
2275     $self->_get_taxzones($dbh, $params{"taxzones"});
 
2278   if ($params{"employees"}) {
 
2279     $self->_get_employees($dbh, "all_employees", $params{"employees"});
 
2282   if ($params{"salesmen"}) {
 
2283     $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
 
2286   if ($params{"business_types"}) {
 
2287     $self->_get_business_types($dbh, $params{"business_types"});
 
2290   if ($params{"dunning_configs"}) {
 
2291     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
 
2294   if($params{"currencies"}) {
 
2295     $self->_get_currencies($dbh, $params{"currencies"});
 
2298   if($params{"customers"}) {
 
2299     if (ref $params{"customers"} eq 'HASH') {
 
2300       $self->_get_customers($dbh, $params{"customers"}{key}, $params{"customers"}{limit});
 
2302       $self->_get_customers($dbh, $params{"customers"});
 
2306   if($params{"vendors"}) {
 
2307     if (ref $params{"vendors"} eq 'HASH') {
 
2308       $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
 
2310       $self->_get_vendors($dbh, $params{"vendors"});
 
2314   if($params{"payments"}) {
 
2315     $self->_get_payments($dbh, $params{"payments"});
 
2318   if($params{"departments"}) {
 
2319     $self->_get_departments($dbh, $params{"departments"});
 
2322   if ($params{price_factors}) {
 
2323     $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
 
2326   if ($params{warehouses}) {
 
2327     $self->_get_warehouses($dbh, $params{warehouses});
 
2330   if ($params{groups}) {
 
2331     $self->_get_groups($dbh, $params{groups});
 
2333   if ($params{partsgroup}) {
 
2334     $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
 
2337   $main::lxdebug->leave_sub();
 
2340 # this sub gets the id and name from $table
 
2342   $main::lxdebug->enter_sub();
 
2344   my ($self, $myconfig, $table) = @_;
 
2346   # connect to database
 
2347   my $dbh = $self->get_standard_dbh($myconfig);
 
2349   $table = $table eq "customer" ? "customer" : "vendor";
 
2350   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
2352   my ($query, @values);
 
2354   if (!$self->{openinvoices}) {
 
2356     if ($self->{customernumber} ne "") {
 
2357       $where = qq|(vc.customernumber ILIKE ?)|;
 
2358       push(@values, '%' . $self->{customernumber} . '%');
 
2360       $where = qq|(vc.name ILIKE ?)|;
 
2361       push(@values, '%' . $self->{$table} . '%');
 
2365       qq~SELECT vc.id, vc.name,
 
2366            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
2368          WHERE $where AND (NOT vc.obsolete)
 
2372       qq~SELECT DISTINCT vc.id, vc.name,
 
2373            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
2375          JOIN $table vc ON (a.${table}_id = vc.id)
 
2376          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
2378     push(@values, '%' . $self->{$table} . '%');
 
2381   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
2383   $main::lxdebug->leave_sub();
 
2385   return scalar(@{ $self->{name_list} });
 
2388 # the selection sub is used in the AR, AP, IS, IR and OE module
 
2391   $main::lxdebug->enter_sub();
 
2393   my ($self, $myconfig, $table, $module) = @_;
 
2396   my $dbh = $self->get_standard_dbh($myconfig);
 
2398   $table = $table eq "customer" ? "customer" : "vendor";
 
2400   my $query = qq|SELECT count(*) FROM $table|;
 
2401   my ($count) = selectrow_query($self, $dbh, $query);
 
2403   # build selection list
 
2404   if ($count < $myconfig->{vclimit}) {
 
2405     $query = qq|SELECT id, name, salesman_id
 
2406                 FROM $table WHERE NOT obsolete
 
2408     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
2412   $self->get_employee($dbh);
 
2414   # setup sales contacts
 
2415   $query = qq|SELECT e.id, e.name
 
2417               WHERE (e.sales = '1') AND (NOT e.id = ?)|;
 
2418   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
2421   push(@{ $self->{all_employees} },
 
2422        { id   => $self->{employee_id},
 
2423          name => $self->{employee} });
 
2425   # sort the whole thing
 
2426   @{ $self->{all_employees} } =
 
2427     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
2429   if ($module eq 'AR') {
 
2431     # prepare query for departments
 
2432     $query = qq|SELECT id, description
 
2435                 ORDER BY description|;
 
2438     $query = qq|SELECT id, description
 
2440                 ORDER BY description|;
 
2443   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2446   $query = qq|SELECT id, description
 
2450   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2453   $query = qq|SELECT printer_description, id
 
2455               ORDER BY printer_description|;
 
2457   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2460   $query = qq|SELECT id, description
 
2464   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2466   $main::lxdebug->leave_sub();
 
2469 sub language_payment {
 
2470   $main::lxdebug->enter_sub();
 
2472   my ($self, $myconfig) = @_;
 
2474   my $dbh = $self->get_standard_dbh($myconfig);
 
2476   my $query = qq|SELECT id, description
 
2480   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2483   $query = qq|SELECT printer_description, id
 
2485               ORDER BY printer_description|;
 
2487   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2490   $query = qq|SELECT id, description
 
2494   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2496   # get buchungsgruppen
 
2497   $query = qq|SELECT id, description
 
2498               FROM buchungsgruppen|;
 
2500   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
2502   $main::lxdebug->leave_sub();
 
2505 # this is only used for reports
 
2506 sub all_departments {
 
2507   $main::lxdebug->enter_sub();
 
2509   my ($self, $myconfig, $table) = @_;
 
2511   my $dbh = $self->get_standard_dbh($myconfig);
 
2514   if ($table eq 'customer') {
 
2515     $where = "WHERE role = 'P' ";
 
2518   my $query = qq|SELECT id, description
 
2521                  ORDER BY description|;
 
2522   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2524   delete($self->{all_departments}) unless (@{ $self->{all_departments} });
 
2526   $main::lxdebug->leave_sub();
 
2530   $main::lxdebug->enter_sub();
 
2532   my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
 
2535   if ($table eq "customer") {
 
2544   $self->all_vc($myconfig, $table, $module);
 
2546   # get last customers or vendors
 
2547   my ($query, $sth, $ref);
 
2549   my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
 
2554     my $transdate = "current_date";
 
2555     if ($self->{transdate}) {
 
2556       $transdate = $dbh->quote($self->{transdate});
 
2559     # now get the account numbers
 
2560     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2561                 FROM chart c, taxkeys tk
 
2562                 WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
2563                   (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
2566     $sth = $dbh->prepare($query);
 
2568     do_statement($self, $sth, $query, '%' . $module . '%');
 
2570     $self->{accounts} = "";
 
2571     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2573       foreach my $key (split(/:/, $ref->{link})) {
 
2574         if ($key =~ /\Q$module\E/) {
 
2576           # cross reference for keys
 
2577           $xkeyref{ $ref->{accno} } = $key;
 
2579           push @{ $self->{"${module}_links"}{$key} },
 
2580             { accno       => $ref->{accno},
 
2581               description => $ref->{description},
 
2582               taxkey      => $ref->{taxkey_id},
 
2583               tax_id      => $ref->{tax_id} };
 
2585           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2591   # get taxkeys and description
 
2592   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
2593   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
2595   if (($module eq "AP") || ($module eq "AR")) {
 
2596     # get tax rates and description
 
2597     $query = qq|SELECT * FROM tax|;
 
2598     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
2604            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
2605            a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
 
2606            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
2607            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
2609            d.description AS department,
 
2612          JOIN $table c ON (a.${table}_id = c.id)
 
2613          LEFT JOIN employee e ON (e.id = a.employee_id)
 
2614          LEFT JOIN department d ON (d.id = a.department_id)
 
2616     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
2618     foreach $key (keys %$ref) {
 
2619       $self->{$key} = $ref->{$key};
 
2622     my $transdate = "current_date";
 
2623     if ($self->{transdate}) {
 
2624       $transdate = $dbh->quote($self->{transdate});
 
2627     # now get the account numbers
 
2628     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2630                 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
 
2632                   AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
2633                     OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
 
2636     $sth = $dbh->prepare($query);
 
2637     do_statement($self, $sth, $query, "%$module%");
 
2639     $self->{accounts} = "";
 
2640     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2642       foreach my $key (split(/:/, $ref->{link})) {
 
2643         if ($key =~ /\Q$module\E/) {
 
2645           # cross reference for keys
 
2646           $xkeyref{ $ref->{accno} } = $key;
 
2648           push @{ $self->{"${module}_links"}{$key} },
 
2649             { accno       => $ref->{accno},
 
2650               description => $ref->{description},
 
2651               taxkey      => $ref->{taxkey_id},
 
2652               tax_id      => $ref->{tax_id} };
 
2654           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2660     # get amounts from individual entries
 
2663            c.accno, c.description,
 
2664            a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
 
2668          LEFT JOIN chart c ON (c.id = a.chart_id)
 
2669          LEFT JOIN project p ON (p.id = a.project_id)
 
2670          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
2671                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
2672                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
2673                                         THEN tk.chart_id = a.chart_id
 
2676                                        OR (c.link='%tax%')) AND
 
2677                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
2678          WHERE a.trans_id = ?
 
2679          AND a.fx_transaction = '0'
 
2680          ORDER BY a.oid, a.transdate|;
 
2681     $sth = $dbh->prepare($query);
 
2682     do_statement($self, $sth, $query, $self->{id});
 
2684     # get exchangerate for currency
 
2685     $self->{exchangerate} =
 
2686       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2689     # store amounts in {acc_trans}{$key} for multiple accounts
 
2690     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2691       $ref->{exchangerate} =
 
2692         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
2693       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2696       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2697         $ref->{amount} *= -1;
 
2699       $ref->{index} = $index;
 
2701       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2707            d.curr AS currencies, d.closedto, d.revtrans,
 
2708            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2709            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2711     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2712     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2719             current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
 
2720             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2721             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2723     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2724     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2726     if ($self->{"$self->{vc}_id"}) {
 
2728       # only setup currency
 
2729       ($self->{currency}) = split(/:/, $self->{currencies});
 
2733       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2735       # get exchangerate for currency
 
2736       $self->{exchangerate} =
 
2737         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2743   $main::lxdebug->leave_sub();
 
2747   $main::lxdebug->enter_sub();
 
2749   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2753   $table         = $table eq "customer" ? "customer" : "vendor";
 
2754   my %column_map = ("a.curr"                  => "currency",
 
2755                     "a.${table}_id"           => "${table}_id",
 
2756                     "a.department_id"         => "department_id",
 
2757                     "d.description"           => "department",
 
2758                     "ct.name"                 => $table,
 
2759                     "current_date + ct.terms" => "duedate",
 
2762   if ($self->{type} =~ /delivery_order/) {
 
2763     $arap  = 'delivery_orders';
 
2764     delete $column_map{"a.curr"};
 
2766   } elsif ($self->{type} =~ /_order/) {
 
2768     $where = "quotation = '0'";
 
2770   } elsif ($self->{type} =~ /_quotation/) {
 
2772     $where = "quotation = '1'";
 
2774   } elsif ($table eq 'customer') {
 
2782   $where           = "($where) AND" if ($where);
 
2783   my $query        = qq|SELECT MAX(id) FROM $arap
 
2784                         WHERE $where ${table}_id > 0|;
 
2785   my ($trans_id)   = selectrow_query($self, $dbh, $query);
 
2788   my $column_spec  = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
 
2789   $query           = qq|SELECT $column_spec
 
2791                         LEFT JOIN $table     ct ON (a.${table}_id = ct.id)
 
2792                         LEFT JOIN department d  ON (a.department_id = d.id)
 
2794   my $ref          = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
 
2796   map { $self->{$_} = $ref->{$_} } values %column_map;
 
2798   $main::lxdebug->leave_sub();
 
2802   $main::lxdebug->enter_sub();
 
2804   my ($self, $myconfig, $thisdate, $days) = @_;
 
2806   my $dbh = $self->get_standard_dbh($myconfig);
 
2811     my $dateformat = $myconfig->{dateformat};
 
2812     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2813     $thisdate = $dbh->quote($thisdate);
 
2814     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2816     $query = qq|SELECT current_date AS thisdate|;
 
2819   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2821   $main::lxdebug->leave_sub();
 
2827   $main::lxdebug->enter_sub();
 
2829   my ($self, $string) = @_;
 
2831   if ($string !~ /%/) {
 
2832     $string = "%$string%";
 
2835   $string =~ s/\'/\'\'/g;
 
2837   $main::lxdebug->leave_sub();
 
2843   $main::lxdebug->enter_sub();
 
2845   my ($self, $flds, $new, $count, $numrows) = @_;
 
2849   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
 
2854   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2856     $j = $item->{ndx} - 1;
 
2857     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2861   for $i ($count + 1 .. $numrows) {
 
2862     map { delete $self->{"${_}_$i"} } @{$flds};
 
2865   $main::lxdebug->leave_sub();
 
2869   $main::lxdebug->enter_sub();
 
2871   my ($self, $myconfig) = @_;
 
2875   my $dbh = $self->dbconnect_noauto($myconfig);
 
2877   my $query = qq|DELETE FROM status
 
2878                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2879   my $sth = prepare_query($self, $dbh, $query);
 
2881   if ($self->{formname} =~ /(check|receipt)/) {
 
2882     for $i (1 .. $self->{rowcount}) {
 
2883       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
2886     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
2890   my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2891   my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2893   my %queued = split / /, $self->{queued};
 
2896   if ($self->{formname} =~ /(check|receipt)/) {
 
2898     # this is a check or receipt, add one entry for each lineitem
 
2899     my ($accno) = split /--/, $self->{account};
 
2900     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
2901                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
2902     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
2903     $sth = prepare_query($self, $dbh, $query);
 
2905     for $i (1 .. $self->{rowcount}) {
 
2906       if ($self->{"checked_$i"}) {
 
2907         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
2913     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2914                 VALUES (?, ?, ?, ?, ?)|;
 
2915     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
2916              $queued{$self->{formname}}, $self->{formname});
 
2922   $main::lxdebug->leave_sub();
 
2926   $main::lxdebug->enter_sub();
 
2928   my ($self, $dbh) = @_;
 
2930   my ($query, $printed, $emailed);
 
2932   my $formnames  = $self->{printed};
 
2933   my $emailforms = $self->{emailed};
 
2935   $query = qq|DELETE FROM status
 
2936                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2937   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
2939   # this only applies to the forms
 
2940   # checks and receipts are posted when printed or queued
 
2942   if ($self->{queued}) {
 
2943     my %queued = split / /, $self->{queued};
 
2945     foreach my $formname (keys %queued) {
 
2946       $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2947       $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2949       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
2950                   VALUES (?, ?, ?, ?, ?)|;
 
2951       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
2953       $formnames  =~ s/\Q$self->{formname}\E//;
 
2954       $emailforms =~ s/\Q$self->{formname}\E//;
 
2959   # save printed, emailed info
 
2960   $formnames  =~ s/^ +//g;
 
2961   $emailforms =~ s/^ +//g;
 
2964   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2965   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2967   foreach my $formname (keys %status) {
 
2968     $printed = ($formnames  =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2969     $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
2971     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2972                 VALUES (?, ?, ?, ?)|;
 
2973     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
2976   $main::lxdebug->leave_sub();
 
2980 # $main::locale->text('SAVED')
 
2981 # $main::locale->text('DELETED')
 
2982 # $main::locale->text('ADDED')
 
2983 # $main::locale->text('PAYMENT POSTED')
 
2984 # $main::locale->text('POSTED')
 
2985 # $main::locale->text('POSTED AS NEW')
 
2986 # $main::locale->text('ELSE')
 
2987 # $main::locale->text('SAVED FOR DUNNING')
 
2988 # $main::locale->text('DUNNING STARTED')
 
2989 # $main::locale->text('PRINTED')
 
2990 # $main::locale->text('MAILED')
 
2991 # $main::locale->text('SCREENED')
 
2992 # $main::locale->text('CANCELED')
 
2993 # $main::locale->text('invoice')
 
2994 # $main::locale->text('proforma')
 
2995 # $main::locale->text('sales_order')
 
2996 # $main::locale->text('packing_list')
 
2997 # $main::locale->text('pick_list')
 
2998 # $main::locale->text('purchase_order')
 
2999 # $main::locale->text('bin_list')
 
3000 # $main::locale->text('sales_quotation')
 
3001 # $main::locale->text('request_quotation')
 
3004   $main::lxdebug->enter_sub();
 
3009   if(!exists $self->{employee_id}) {
 
3010     &get_employee($self, $dbh);
 
3014    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
 
3015    qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
 
3016   my @values = (conv_i($self->{id}), $self->{login},
 
3017                 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
 
3018   do_query($self, $dbh, $query, @values);
 
3020   $main::lxdebug->leave_sub();
 
3024   $main::lxdebug->enter_sub();
 
3026   my ($self, $dbh, $trans_id, $restriction, $order) = @_;
 
3027   my ($orderBy, $desc) = split(/\-\-/, $order);
 
3028   $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
 
3031   if ($trans_id ne "") {
 
3033       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 | .
 
3034       qq|FROM history_erp h | .
 
3035       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
3036       qq|WHERE trans_id = | . $trans_id
 
3037       . $restriction . qq| |
 
3040     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
3042     $sth->execute() || $self->dberror("$query");
 
3044     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
3045       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
3046       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
3047       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
 
3048       $tempArray[$i++] = $hash_ref;
 
3050     $main::lxdebug->leave_sub() and return \@tempArray 
 
3051       if ($i > 0 && $tempArray[0] ne "");
 
3053   $main::lxdebug->leave_sub();
 
3057 sub update_defaults {
 
3058   $main::lxdebug->enter_sub();
 
3060   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
3063   if ($provided_dbh) {
 
3064     $dbh = $provided_dbh;
 
3066     $dbh = $self->dbconnect_noauto($myconfig);
 
3068   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
3069   my $sth   = $dbh->prepare($query);
 
3071   $sth->execute || $self->dberror($query);
 
3072   my ($var) = $sth->fetchrow_array;
 
3075   if ($var =~ m/\d+$/) {
 
3076     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
3077     my $len_diff = length($var) - $-[0] - length($new_var);
 
3078     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
3084   $query = qq|UPDATE defaults SET $fld = ?|;
 
3085   do_query($self, $dbh, $query, $var);
 
3087   if (!$provided_dbh) {
 
3092   $main::lxdebug->leave_sub();
 
3097 sub update_business {
 
3098   $main::lxdebug->enter_sub();
 
3100   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
3103   if ($provided_dbh) {
 
3104     $dbh = $provided_dbh;
 
3106     $dbh = $self->dbconnect_noauto($myconfig);
 
3109     qq|SELECT customernumberinit FROM business
 
3110        WHERE id = ? FOR UPDATE|;
 
3111   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
3113   if ($var =~ m/\d+$/) {
 
3114     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
3115     my $len_diff = length($var) - $-[0] - length($new_var);
 
3116     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
3122   $query = qq|UPDATE business
 
3123               SET customernumberinit = ?
 
3125   do_query($self, $dbh, $query, $var, $business_id);
 
3127   if (!$provided_dbh) {
 
3132   $main::lxdebug->leave_sub();
 
3137 sub get_partsgroup {
 
3138   $main::lxdebug->enter_sub();
 
3140   my ($self, $myconfig, $p) = @_;
 
3141   my $target = $p->{target} || 'all_partsgroup';
 
3143   my $dbh = $self->get_standard_dbh($myconfig);
 
3145   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
3147                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
3150   if ($p->{searchitems} eq 'part') {
 
3151     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
3153   if ($p->{searchitems} eq 'service') {
 
3154     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
3156   if ($p->{searchitems} eq 'assembly') {
 
3157     $query .= qq|WHERE p.assembly = '1'|;
 
3159   if ($p->{searchitems} eq 'labor') {
 
3160     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
3163   $query .= qq|ORDER BY partsgroup|;
 
3166     $query = qq|SELECT id, partsgroup FROM partsgroup
 
3167                 ORDER BY partsgroup|;
 
3170   if ($p->{language_code}) {
 
3171     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
3172                   t.description AS translation
 
3174                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
3175                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
3176                 ORDER BY translation|;
 
3177     @values = ($p->{language_code});
 
3180   $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
 
3182   $main::lxdebug->leave_sub();
 
3185 sub get_pricegroup {
 
3186   $main::lxdebug->enter_sub();
 
3188   my ($self, $myconfig, $p) = @_;
 
3190   my $dbh = $self->get_standard_dbh($myconfig);
 
3192   my $query = qq|SELECT p.id, p.pricegroup
 
3195   $query .= qq| ORDER BY pricegroup|;
 
3198     $query = qq|SELECT id, pricegroup FROM pricegroup
 
3199                 ORDER BY pricegroup|;
 
3202   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
3204   $main::lxdebug->leave_sub();
 
3208 # usage $form->all_years($myconfig, [$dbh])
 
3209 # return list of all years where bookings found
 
3212   $main::lxdebug->enter_sub();
 
3214   my ($self, $myconfig, $dbh) = @_;
 
3216   $dbh ||= $self->get_standard_dbh($myconfig);
 
3219   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
3220                    (SELECT MAX(transdate) FROM acc_trans)|;
 
3221   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
3223   if ($myconfig->{dateformat} =~ /^yy/) {
 
3224     ($startdate) = split /\W/, $startdate;
 
3225     ($enddate) = split /\W/, $enddate;
 
3227     (@_) = split /\W/, $startdate;
 
3229     (@_) = split /\W/, $enddate;
 
3234   $startdate = substr($startdate,0,4);
 
3235   $enddate = substr($enddate,0,4);
 
3237   while ($enddate >= $startdate) {
 
3238     push @all_years, $enddate--;
 
3243   $main::lxdebug->leave_sub();
 
3247   $main::lxdebug->enter_sub();
 
3251   map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if $self->{$_} } @vars;
 
3253   $main::lxdebug->leave_sub();
 
3257   $main::lxdebug->enter_sub();
 
3262   map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if $self->{_VAR_BACKUP}->{$_} } @vars;
 
3264   $main::lxdebug->leave_sub();