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 #======================================================================
 
  59 use SL::Layout::Dispatcher;
 
  63 use SL::MoreCommon qw(uri_encode uri_decode);
 
  65 use SL::PrefixedNumber;
 
  72 use List::Util qw(first max min sum);
 
  73 use List::MoreUtils qw(all any apply);
 
  80   disconnect_standard_dbh();
 
  83 sub disconnect_standard_dbh {
 
  84   return unless $standard_dbh;
 
  85   $standard_dbh->disconnect();
 
  90   $main::lxdebug->enter_sub();
 
  97   if ($LXDebug::watch_form) {
 
  99     tie %{ $self }, 'SL::Watchdog';
 
 104   open VERSION_FILE, "VERSION";                 # New but flexible code reads version from VERSION-file
 
 105   $self->{version} =  <VERSION_FILE>;
 
 107   $self->{version}  =~ s/[^0-9A-Za-z\.\_\-]//g; # only allow numbers, letters, points, underscores and dashes. Prevents injecting of malicious code.
 
 109   $main::lxdebug->leave_sub();
 
 116   SL::Request::read_cgi_input($self);
 
 119 sub _flatten_variables_rec {
 
 120   $main::lxdebug->enter_sub(2);
 
 129   if ('' eq ref $curr->{$key}) {
 
 130     @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
 
 132   } elsif ('HASH' eq ref $curr->{$key}) {
 
 133     foreach my $hash_key (sort keys %{ $curr->{$key} }) {
 
 134       push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
 
 138     foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
 
 139       my $first_array_entry = 1;
 
 141       my $element = $curr->{$key}[$idx];
 
 143       if ('HASH' eq ref $element) {
 
 144         foreach my $hash_key (sort keys %{ $element }) {
 
 145           push @result, $self->_flatten_variables_rec($element, $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
 
 146           $first_array_entry = 0;
 
 149         @result = ({ 'key' => $prefix . $key . ($first_array_entry ? '[+]' : '[]'), 'value' => $element });
 
 154   $main::lxdebug->leave_sub(2);
 
 159 sub flatten_variables {
 
 160   $main::lxdebug->enter_sub(2);
 
 168     push @variables, $self->_flatten_variables_rec($self, '', $_);
 
 171   $main::lxdebug->leave_sub(2);
 
 176 sub flatten_standard_variables {
 
 177   $main::lxdebug->enter_sub(2);
 
 180   my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_);
 
 184   foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
 
 185     push @variables, $self->_flatten_variables_rec($self, '', $_);
 
 188   $main::lxdebug->leave_sub(2);
 
 194   $main::lxdebug->enter_sub();
 
 200   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 202   $main::lxdebug->leave_sub();
 
 206   $main::lxdebug->enter_sub(2);
 
 209   my $password      = $self->{password};
 
 211   $self->{password} = 'X' x 8;
 
 213   local $Data::Dumper::Sortkeys = 1;
 
 214   my $output                    = Dumper($self);
 
 216   $self->{password} = $password;
 
 218   $main::lxdebug->leave_sub(2);
 
 224   my ($self, $str) = @_;
 
 226   return uri_encode($str);
 
 230   my ($self, $str) = @_;
 
 232   return uri_decode($str);
 
 236   $main::lxdebug->enter_sub();
 
 237   my ($self, $str) = @_;
 
 239   if ($str && !ref($str)) {
 
 240     $str =~ s/\"/"/g;
 
 243   $main::lxdebug->leave_sub();
 
 249   $main::lxdebug->enter_sub();
 
 250   my ($self, $str) = @_;
 
 252   if ($str && !ref($str)) {
 
 253     $str =~ s/"/\"/g;
 
 256   $main::lxdebug->leave_sub();
 
 262   $main::lxdebug->enter_sub();
 
 266     map({ print($::request->{cgi}->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
 
 268     for (sort keys %$self) {
 
 269       next if (($_ eq "header") || (ref($self->{$_}) ne ""));
 
 270       print($::request->{cgi}->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
 
 273   $main::lxdebug->leave_sub();
 
 277   my ($self, $code) = @_;
 
 278   local $self->{__ERROR_HANDLER} = sub { die SL::X::FormError->new($_[0]) };
 
 283   $main::lxdebug->enter_sub();
 
 285   $main::lxdebug->show_backtrace();
 
 287   my ($self, $msg) = @_;
 
 289   if ($self->{__ERROR_HANDLER}) {
 
 290     $self->{__ERROR_HANDLER}->($msg);
 
 292   } elsif ($ENV{HTTP_USER_AGENT}) {
 
 294     $self->show_generic_error($msg);
 
 297     print STDERR "Error: $msg\n";
 
 301   $main::lxdebug->leave_sub();
 
 305   $main::lxdebug->enter_sub();
 
 307   my ($self, $msg) = @_;
 
 309   if ($ENV{HTTP_USER_AGENT}) {
 
 312     if (!$self->{header}) {
 
 318     <p class="message_ok"><b>$msg</b></p>
 
 320     <script type="text/javascript">
 
 322     // If JavaScript is enabled, the whole thing will be reloaded.
 
 323     // The reason is: When one changes his menu setup (HTML / CSS ...)
 
 324     // it now loads the correct code into the browser instead of do nothing.
 
 325     setTimeout("top.frames.location.href='login.pl'",500);
 
 334     if ($self->{info_function}) {
 
 335       &{ $self->{info_function} }($msg);
 
 341   $main::lxdebug->leave_sub();
 
 344 # calculates the number of rows in a textarea based on the content and column number
 
 345 # can be capped with maxrows
 
 347   $main::lxdebug->enter_sub();
 
 348   my ($self, $str, $cols, $maxrows, $minrows) = @_;
 
 352   my $rows   = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
 
 355   $main::lxdebug->leave_sub();
 
 357   return max(min($rows, $maxrows), $minrows);
 
 361   $main::lxdebug->enter_sub();
 
 363   my ($self, $msg) = @_;
 
 365   $self->error("$msg\n" . $DBI::errstr);
 
 367   $main::lxdebug->leave_sub();
 
 371   $main::lxdebug->enter_sub();
 
 373   my ($self, $name, $msg) = @_;
 
 376   foreach my $part (split m/\./, $name) {
 
 377     if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
 
 380     $curr = $curr->{$part};
 
 383   $main::lxdebug->leave_sub();
 
 386 sub _get_request_uri {
 
 389   return URI->new($ENV{HTTP_REFERER})->canonical() if $ENV{HTTP_X_FORWARDED_FOR};
 
 390   return URI->new                                  if !$ENV{REQUEST_URI}; # for testing
 
 392   my $scheme =  $ENV{HTTPS} && (lc $ENV{HTTPS} eq 'on') ? 'https' : 'http';
 
 393   my $port   =  $ENV{SERVER_PORT};
 
 394   $port      =  undef if (($scheme eq 'http' ) && ($port == 80))
 
 395                       || (($scheme eq 'https') && ($port == 443));
 
 397   my $uri    =  URI->new("${scheme}://");
 
 398   $uri->scheme($scheme);
 
 400   $uri->host($ENV{HTTP_HOST} || $ENV{SERVER_ADDR});
 
 401   $uri->path_query($ENV{REQUEST_URI});
 
 407 sub _add_to_request_uri {
 
 410   my $relative_new_path = shift;
 
 411   my $request_uri       = shift || $self->_get_request_uri;
 
 412   my $relative_new_uri  = URI->new($relative_new_path);
 
 413   my @request_segments  = $request_uri->path_segments;
 
 415   my $new_uri           = $request_uri->clone;
 
 416   $new_uri->path_segments(@request_segments[0..scalar(@request_segments) - 2], $relative_new_uri->path_segments);
 
 421 sub create_http_response {
 
 422   $main::lxdebug->enter_sub();
 
 427   my $cgi      = $::request->{cgi};
 
 430   if (defined $main::auth) {
 
 431     my $uri      = $self->_get_request_uri;
 
 432     my @segments = $uri->path_segments;
 
 434     $uri->path_segments(@segments);
 
 436     my $session_cookie_value = $main::auth->get_session_id();
 
 438     if ($session_cookie_value) {
 
 439       $session_cookie = $cgi->cookie('-name'   => $main::auth->get_session_cookie_name(),
 
 440                                      '-value'  => $session_cookie_value,
 
 441                                      '-path'   => $uri->path,
 
 442                                      '-secure' => $ENV{HTTPS});
 
 446   my %cgi_params = ('-type' => $params{content_type});
 
 447   $cgi_params{'-charset'} = $params{charset} if ($params{charset});
 
 448   $cgi_params{'-cookie'}  = $session_cookie  if ($session_cookie);
 
 450   map { $cgi_params{'-' . $_} = $params{$_} if exists $params{$_} } qw(content_disposition content_length);
 
 452   my $output = $cgi->header(%cgi_params);
 
 454   $main::lxdebug->leave_sub();
 
 460   $::lxdebug->enter_sub;
 
 462   my ($self, %params) = @_;
 
 463   my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
 
 466   $::lxdebug->leave_sub and return if !$ENV{HTTP_USER_AGENT} || $self->{header}++;
 
 468   if ($params{no_layout}) {
 
 469     $::request->{layout} = SL::Layout::Dispatcher->new(style => 'none');
 
 472   my $layout = $::request->{layout};
 
 474   # standard css for all
 
 475   # this should gradually move to the layouts that need it
 
 476   $layout->use_stylesheet("$_.css") for qw(
 
 477     main menu list_accounts jquery.autocomplete
 
 478     jquery.multiselect2side frame_header/header
 
 479     ui-lightness/jquery-ui
 
 480     jquery-ui.custom jqModal
 
 483   $layout->use_javascript("$_.js") for (qw(
 
 484     jquery jquery-ui jquery.cookie jqModal jquery.checkall jquery.download
 
 485     common part_selection switchmenuframe
 
 486   ), "jquery/ui/i18n/jquery.ui.datepicker-$::myconfig{countrycode}");
 
 488   $self->{favicon} ||= "favicon.ico";
 
 489   $self->{titlebar} = join ' - ', grep $_, $self->{title}, $self->{login}, $::myconfig{dbname}, $self->{version} if $self->{title} || !$self->{titlebar};
 
 492   if ($self->{refresh_url} || $self->{refresh_time}) {
 
 493     my $refresh_time = $self->{refresh_time} || 3;
 
 494     my $refresh_url  = $self->{refresh_url}  || $ENV{REFERER};
 
 495     push @header, "<meta http-equiv='refresh' content='$refresh_time;$refresh_url'>";
 
 498   my $auto_reload_resources_param = $layout->auto_reload_resources_param;
 
 500   push @header, map { qq|<link rel="stylesheet" href="${_}${auto_reload_resources_param}" type="text/css" title="Stylesheet">| } $layout->stylesheets;
 
 501   push @header, "<style type='text/css'>\@page { size:landscape; }</style> "                     if $self->{landscape};
 
 502   push @header, "<link rel='shortcut icon' href='$self->{favicon}' type='image/x-icon'>"         if -f $self->{favicon};
 
 503   push @header, map { qq|<script type="text/javascript" src="${_}${auto_reload_resources_param}"></script>| }                    $layout->javascripts;
 
 504   push @header, $self->{javascript} if $self->{javascript};
 
 505   push @header, map { $_->show_javascript } @{ $self->{AJAX} || [] };
 
 508     strict       => qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">|,
 
 509     transitional => qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">|,
 
 510     frameset     => qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">|,
 
 511     html5        => qq|<!DOCTYPE html>|,
 
 515   print $self->create_http_response(content_type => 'text/html', charset => $db_charset);
 
 516   print $doctypes{$params{doctype} || 'transitional'}, $/;
 
 520   <meta http-equiv="Content-Type" content="text/html; charset=$db_charset">
 
 521   <title>$self->{titlebar}</title>
 
 523   print "  $_\n" for @header;
 
 525   <meta name="robots" content="noindex,nofollow">
 
 530   print $::request->{layout}->pre_content;
 
 531   print $::request->{layout}->start_content;
 
 533   $layout->header_done;
 
 535   $::lxdebug->leave_sub;
 
 539   return unless $::request->{layout}->need_footer;
 
 541   print $::request->{layout}->end_content;
 
 542   print $::request->{layout}->post_content;
 
 544   if (my @inline_scripts = $::request->{layout}->javascripts_inline) {
 
 545     print "<script type='text/javascript'>@inline_scripts</script>\n";
 
 554 sub ajax_response_header {
 
 555   $main::lxdebug->enter_sub();
 
 559   my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
 
 560   my $output     = $::request->{cgi}->header('-charset' => $db_charset);
 
 562   $main::lxdebug->leave_sub();
 
 567 sub redirect_header {
 
 571   my $base_uri = $self->_get_request_uri;
 
 572   my $new_uri  = URI->new_abs($new_url, $base_uri);
 
 574   die "Headers already sent" if $self->{header};
 
 577   return $::request->{cgi}->redirect($new_uri);
 
 580 sub set_standard_title {
 
 581   $::lxdebug->enter_sub;
 
 584   $self->{titlebar}  = "kivitendo " . $::locale->text('Version') . " $self->{version}";
 
 585   $self->{titlebar} .= "- $::myconfig{name}"   if $::myconfig{name};
 
 586   $self->{titlebar} .= "- $::myconfig{dbname}" if $::myconfig{name};
 
 588   $::lxdebug->leave_sub;
 
 591 sub _prepare_html_template {
 
 592   $main::lxdebug->enter_sub();
 
 594   my ($self, $file, $additional_params) = @_;
 
 597   if (!%::myconfig || !$::myconfig{"countrycode"}) {
 
 598     $language = $::lx_office_conf{system}->{language};
 
 600     $language = $main::myconfig{"countrycode"};
 
 602   $language = "de" unless ($language);
 
 604   if (-f "templates/webpages/${file}.html") {
 
 605     $file = "templates/webpages/${file}.html";
 
 608     my $info = "Web page template '${file}' not found.\n";
 
 609     print qq|<pre>$info</pre>|;
 
 613   if ($self->{"DEBUG"}) {
 
 614     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 617   if ($additional_params->{"DEBUG"}) {
 
 618     $additional_params->{"DEBUG"} =
 
 619       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 622   if (%main::myconfig) {
 
 623     $::myconfig{jsc_dateformat} = apply {
 
 627     } $::myconfig{"dateformat"};
 
 628     $additional_params->{"myconfig"} ||= \%::myconfig;
 
 629     map { $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys %::myconfig;
 
 632   $additional_params->{"conf_dbcharset"}              = $::lx_office_conf{system}->{dbcharset};
 
 633   $additional_params->{"conf_webdav"}                 = $::lx_office_conf{features}->{webdav};
 
 634   $additional_params->{"conf_latex_templates"}        = $::lx_office_conf{print_templates}->{latex};
 
 635   $additional_params->{"conf_opendocument_templates"} = $::lx_office_conf{print_templates}->{opendocument};
 
 636   $additional_params->{"conf_vertreter"}              = $::lx_office_conf{features}->{vertreter};
 
 637   $additional_params->{"conf_parts_image_css"}        = $::lx_office_conf{features}->{parts_image_css};
 
 638   $additional_params->{"conf_parts_listing_images"}   = $::lx_office_conf{features}->{parts_listing_images};
 
 639   $additional_params->{"conf_parts_show_image"}       = $::lx_office_conf{features}->{parts_show_image};
 
 640   $additional_params->{"INSTANCE_CONF"}               = $::instance_conf;
 
 642   if (my $debug_options = $::lx_office_conf{debug}{options}) {
 
 643     map { $additional_params->{'DEBUG_' . uc($_)} = $debug_options->{$_} } keys %$debug_options;
 
 646   if ($main::auth && $main::auth->{RIGHTS} && $main::auth->{RIGHTS}->{$self->{login}}) {
 
 647     while (my ($key, $value) = each %{ $main::auth->{RIGHTS}->{$self->{login}} }) {
 
 648       $additional_params->{"AUTH_RIGHTS_" . uc($key)} = $value;
 
 652   $main::lxdebug->leave_sub();
 
 657 sub parse_html_template {
 
 658   $main::lxdebug->enter_sub();
 
 660   my ($self, $file, $additional_params) = @_;
 
 662   $additional_params ||= { };
 
 664   my $real_file = $self->_prepare_html_template($file, $additional_params);
 
 665   my $template  = $self->template || $self->init_template;
 
 667   map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
 
 670   $template->process($real_file, $additional_params, \$output) || die $template->error;
 
 672   $main::lxdebug->leave_sub();
 
 680   return $self->template if $self->template;
 
 682   # Force scripts/locales.pl to pick up the exception handling template.
 
 683   # parse_html_template('generic/exception')
 
 684   return $self->template(Template->new({
 
 689      'PLUGIN_BASE'  => 'SL::Template::Plugin',
 
 690      'INCLUDE_PATH' => '.:templates/webpages',
 
 691      'COMPILE_EXT'  => '.tcc',
 
 692      'COMPILE_DIR'  => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
 
 693      'ERROR'        => 'templates/webpages/generic/exception.html',
 
 699   $self->{template_object} = shift if @_;
 
 700   return $self->{template_object};
 
 703 sub show_generic_error {
 
 704   $main::lxdebug->enter_sub();
 
 706   my ($self, $error, %params) = @_;
 
 708   if ($self->{__ERROR_HANDLER}) {
 
 709     $self->{__ERROR_HANDLER}->($error);
 
 710     $main::lxdebug->leave_sub();
 
 714   if ($::request->is_ajax) {
 
 715     $::lxdebug->message(0, "trying to render AJAX response...");
 
 718       ->render(SL::Controller::Base->new);
 
 723     'title_error' => $params{title},
 
 724     'label_error' => $error,
 
 727   if ($params{action}) {
 
 730     map { delete($self->{$_}); } qw(action);
 
 731     map { push @vars, { "name" => $_, "value" => $self->{$_} } if (!ref($self->{$_})); } keys %{ $self };
 
 733     $add_params->{SHOW_BUTTON}  = 1;
 
 734     $add_params->{BUTTON_LABEL} = $params{label} || $params{action};
 
 735     $add_params->{VARIABLES}    = \@vars;
 
 737   } elsif ($params{back_button}) {
 
 738     $add_params->{SHOW_BACK_BUTTON} = 1;
 
 741   $self->{title} = $params{title} if $params{title};
 
 744   print $self->parse_html_template("generic/error", $add_params);
 
 746   print STDERR "Error: $error\n";
 
 748   $main::lxdebug->leave_sub();
 
 753 sub show_generic_information {
 
 754   $main::lxdebug->enter_sub();
 
 756   my ($self, $text, $title) = @_;
 
 759     'title_information' => $title,
 
 760     'label_information' => $text,
 
 763   $self->{title} = $title if ($title);
 
 766   print $self->parse_html_template("generic/information", $add_params);
 
 768   $main::lxdebug->leave_sub();
 
 773 sub _store_redirect_info_in_session {
 
 776   return unless $self->{callback} =~ m:^ ( [^\?/]+ \.pl ) \? (.+) :x;
 
 778   my ($controller, $params) = ($1, $2);
 
 779   my $form                  = { map { map { $self->unescape($_) } split /=/, $_, 2 } split m/\&/, $params };
 
 780   $self->{callback}         = "${controller}?RESTORE_FORM_FROM_SESSION_ID=" . $::auth->save_form_in_session(form => $form);
 
 784   $main::lxdebug->enter_sub();
 
 786   my ($self, $msg) = @_;
 
 788   if (!$self->{callback}) {
 
 792     $self->_store_redirect_info_in_session;
 
 793     print $::form->redirect_header($self->{callback});
 
 798   $main::lxdebug->leave_sub();
 
 801 # sort of columns removed - empty sub
 
 803   $main::lxdebug->enter_sub();
 
 805   my ($self, @columns) = @_;
 
 807   $main::lxdebug->leave_sub();
 
 813   $main::lxdebug->enter_sub(2);
 
 815   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 818   my $neg = $amount < 0;
 
 819   my $force_places = defined $places && $places >= 0;
 
 821   $amount = $self->round_amount($amount, abs $places) if $force_places;
 
 822   $amount = sprintf "%.*f", ($force_places ? $places : 10), abs $amount; # 6 is default for %fa
 
 824   # before the sprintf amount was a number, afterwards it's a string. because of the dynamic nature of perl
 
 825   # this is easy to confuse, so keep in mind: before this comment no s///, m//, concat or other strong ops on
 
 826   # $amount. after this comment no +,-,*,/,abs. it will only introduce subtle bugs.
 
 828   $amount =~ s/0*$// unless defined $places && $places == 0;             # cull trailing 0s
 
 830   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 831   my @p = split(/\./, $amount);                                          # split amount at decimal point
 
 833   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1];                             # add 1,000 delimiters
 
 835   if ($places || $p[1]) {
 
 838             .  (0 x (abs($places || 0) - length ($p[1]||'')));           # pad the fraction
 
 842     ($dash =~ /-/)    ? ($neg ? "($amount)"                            : "$amount" )                              :
 
 843     ($dash =~ /DRCR/) ? ($neg ? "$amount " . $main::locale->text('DR') : "$amount " . $main::locale->text('CR') ) :
 
 844                         ($neg ? "-$amount"                             : "$amount" )                              ;
 
 847   $main::lxdebug->leave_sub(2);
 
 851 sub format_amount_units {
 
 852   $main::lxdebug->enter_sub();
 
 857   my $myconfig         = \%main::myconfig;
 
 858   my $amount           = $params{amount} * 1;
 
 859   my $places           = $params{places};
 
 860   my $part_unit_name   = $params{part_unit};
 
 861   my $amount_unit_name = $params{amount_unit};
 
 862   my $conv_units       = $params{conv_units};
 
 863   my $max_places       = $params{max_places};
 
 865   if (!$part_unit_name) {
 
 866     $main::lxdebug->leave_sub();
 
 870   my $all_units        = AM->retrieve_all_units;
 
 872   if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
 
 873     $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
 
 876   if (!scalar @{ $conv_units }) {
 
 877     my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
 
 878     $main::lxdebug->leave_sub();
 
 882   my $part_unit  = $all_units->{$part_unit_name};
 
 883   my $conv_unit  = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
 
 885   $amount       *= $conv_unit->{factor};
 
 890   foreach my $unit (@$conv_units) {
 
 891     my $last = $unit->{name} eq $part_unit->{name};
 
 893       $num     = int($amount / $unit->{factor});
 
 894       $amount -= $num * $unit->{factor};
 
 897     if ($last ? $amount : $num) {
 
 898       push @values, { "unit"   => $unit->{name},
 
 899                       "amount" => $last ? $amount / $unit->{factor} : $num,
 
 900                       "places" => $last ? $places : 0 };
 
 907     push @values, { "unit"   => $part_unit_name,
 
 912   my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
 
 914   $main::lxdebug->leave_sub();
 
 920   $main::lxdebug->enter_sub(2);
 
 925   $input =~ s/(^|[^\#]) \#  (\d+)  /$1$_[$2 - 1]/gx;
 
 926   $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
 
 927   $input =~ s/\#\#/\#/g;
 
 929   $main::lxdebug->leave_sub(2);
 
 937   $main::lxdebug->enter_sub(2);
 
 939   my ($self, $myconfig, $amount) = @_;
 
 941   if (!defined($amount) || ($amount eq '')) {
 
 942     $main::lxdebug->leave_sub(2);
 
 946   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 947       || ($myconfig->{numberformat} eq '1000,00')) {
 
 952   if ($myconfig->{numberformat} eq "1'000.00") {
 
 958   $main::lxdebug->leave_sub(2);
 
 960   # Make sure no code wich is not a math expression ends up in eval().
 
 961   return 0 unless $amount =~ /^ [\s \d \( \) \- \+ \* \/ \. ]* $/x;
 
 962   return scalar(eval($amount)) * 1 ;
 
 966   $main::lxdebug->enter_sub(2);
 
 968   my ($self, $amount, $places) = @_;
 
 971   # Rounding like "Kaufmannsrunden" (see http://de.wikipedia.org/wiki/Rundung )
 
 973   # Round amounts to eight places before rounding to the requested
 
 974   # number of places. This gets rid of errors due to internal floating
 
 975   # point representation.
 
 976   $amount       = $self->round_amount($amount, 8) if $places < 8;
 
 977   $amount       = $amount * (10**($places));
 
 978   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 980   $main::lxdebug->leave_sub(2);
 
 982   return $round_amount;
 
 987   $main::lxdebug->enter_sub();
 
 989   my ($self, $myconfig) = @_;
 
 990   my ($out, $out_mode);
 
 994   my $userspath = $::lx_office_conf{paths}->{userspath};
 
 996   $self->{"cwd"} = getcwd();
 
 997   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
1002   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
1003     $template_type  = 'OpenDocument';
 
1004     $ext_for_format = $self->{"format"} =~ m/pdf/ ? 'pdf' : 'odt';
 
1006   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
1007     $template_type    = 'LaTeX';
 
1008     $ext_for_format   = 'pdf';
 
1010   } elsif (($self->{"format"} =~ /html/i) || (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
1011     $template_type  = 'HTML';
 
1012     $ext_for_format = 'html';
 
1014   } elsif (($self->{"format"} =~ /xml/i) || (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
 
1015     $template_type  = 'XML';
 
1016     $ext_for_format = 'xml';
 
1018   } elsif ( $self->{"format"} =~ /elster(?:winston|taxbird)/i ) {
 
1019     $template_type = 'XML';
 
1021   } elsif ( $self->{"format"} =~ /excel/i ) {
 
1022     $template_type  = 'Excel';
 
1023     $ext_for_format = 'xls';
 
1025   } elsif ( defined $self->{'format'}) {
 
1026     $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
 
1028   } elsif ( $self->{'format'} eq '' ) {
 
1029     $self->error("No Outputformat given: $self->{'format'}");
 
1031   } else { #Catch the rest
 
1032     $self->error("Outputformat not defined: $self->{'format'}");
 
1035   my $template = SL::Template::create(type      => $template_type,
 
1036                                       file_name => $self->{IN},
 
1038                                       myconfig  => $myconfig,
 
1039                                       userspath => $userspath);
 
1041   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
1042   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
1044   if (!$self->{employee_id}) {
 
1045     map { $self->{"employee_${_}"} = $myconfig->{$_}; } qw(email tel fax name signature company address businessnumber co_ustid taxnumber duns);
 
1048   map { $self->{"${_}"} = $myconfig->{$_}; } qw(co_ustid);
 
1049   map { $self->{"myconfig_${_}"} = $myconfig->{$_} } grep { $_ ne 'dbpasswd' } keys %{ $myconfig };
 
1051   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
1053   # OUT is used for the media, screen, printer, email
 
1054   # for postscript we store a copy in a temporary file
 
1055   my ($temp_fh, $suffix);
 
1056   $suffix =  $self->{IN};
 
1057   $suffix =~ s/.*\.//;
 
1058   ($temp_fh, $self->{tmpfile}) = File::Temp::tempfile(
 
1059     'kivitendo-printXXXXXX',
 
1060     SUFFIX => '.' . ($suffix || 'tex'),
 
1062     UNLINK => ($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})? 0 : 1,
 
1065   (undef, undef, $self->{template_meta}{tmpfile}) = File::Spec->splitpath( $self->{tmpfile} );
 
1067   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
1068     $out              = $self->{OUT};
 
1069     $out_mode         = $self->{OUT_MODE} || '>';
 
1070     $self->{OUT}      = "$self->{tmpfile}";
 
1071     $self->{OUT_MODE} = '>';
 
1075   my $command_formatter = sub {
 
1076     my ($out_mode, $out) = @_;
 
1077     return $out_mode eq '|-' ? SL::Template::create(type => 'ShellCommand', form => $self)->parse($out) : $out;
 
1081     $self->{OUT} = $command_formatter->($self->{OUT_MODE}, $self->{OUT});
 
1082     open(OUT, $self->{OUT_MODE}, $self->{OUT}) or $self->error("error on opening $self->{OUT} with mode $self->{OUT_MODE} : $!");
 
1084     *OUT = ($::dispatcher->get_standard_filehandles)[1];
 
1088   if (!$template->parse(*OUT)) {
 
1090     $self->error("$self->{IN} : " . $template->get_error());
 
1093   close OUT if $self->{OUT};
 
1095   if ($self->{media} eq 'file') {
 
1096     copy(join('/', $self->{cwd}, $userspath, $self->{tmpfile}), $out =~ m|^/| ? $out : join('/', $self->{cwd}, $out)) if $template->uses_temp_file;
 
1098     chdir("$self->{cwd}");
 
1100     $::lxdebug->leave_sub();
 
1105   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
1107     if ($self->{media} eq 'email') {
 
1109       my $mail = new Mailer;
 
1111       map { $mail->{$_} = $self->{$_} }
 
1112         qw(cc bcc subject message version format);
 
1113       $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
 
1114       $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
 
1115       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
1116       $mail->{fileid} = time() . '.' . $$ . '.';
 
1117       $myconfig->{signature} =~ s/\r//g;
 
1119       # if we send html or plain text inline
 
1120       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
1121         $mail->{contenttype}    =  "text/html";
 
1122         $mail->{message}        =~ s/\r//g;
 
1123         $mail->{message}        =~ s/\n/<br>\n/g;
 
1124         $myconfig->{signature}  =~ s/\n/<br>\n/g;
 
1125         $mail->{message}       .=  "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
1127         open(IN, "<", $self->{tmpfile})
 
1128           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1129         $mail->{message} .= $_ while <IN>;
 
1134         if (!$self->{"do_not_attach"}) {
 
1135           my $attachment_name  =  $self->{attachment_filename} || $self->{tmpfile};
 
1136           $attachment_name     =~ s/\.(.+?)$/.${ext_for_format}/ if ($ext_for_format);
 
1137           $mail->{attachments} =  [{ "filename" => $self->{tmpfile},
 
1138                                      "name"     => $attachment_name }];
 
1141         $mail->{message}  =~ s/\r//g;
 
1142         $mail->{message} .=  "\n-- \n$myconfig->{signature}";
 
1146       my $err = $mail->send();
 
1147       $self->error($self->cleanup . "$err") if ($err);
 
1151       $self->{OUT}      = $out;
 
1152       $self->{OUT_MODE} = $out_mode;
 
1154       my $numbytes = (-s $self->{tmpfile});
 
1155       open(IN, "<", $self->{tmpfile})
 
1156         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1159       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
1161       chdir("$self->{cwd}");
 
1162       #print(STDERR "Kopien $self->{copies}\n");
 
1163       #print(STDERR "OUT $self->{OUT}\n");
 
1164       for my $i (1 .. $self->{copies}) {
 
1166           $self->{OUT} = $command_formatter->($self->{OUT_MODE}, $self->{OUT});
 
1168           open  OUT, $self->{OUT_MODE}, $self->{OUT} or $self->error($self->cleanup . "$self->{OUT} : $!");
 
1169           print OUT $_ while <IN>;
 
1174           $self->{attachment_filename} = ($self->{attachment_filename})
 
1175                                        ? $self->{attachment_filename}
 
1176                                        : $self->generate_attachment_filename();
 
1178           # launch application
 
1179           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
1180 Content-Disposition: attachment; filename="$self->{attachment_filename}"
 
1181 Content-Length: $numbytes
 
1185           $::locale->with_raw_io(\*STDOUT, sub { print while <IN> });
 
1196   chdir("$self->{cwd}");
 
1197   $main::lxdebug->leave_sub();
 
1200 sub get_formname_translation {
 
1201   $main::lxdebug->enter_sub();
 
1202   my ($self, $formname) = @_;
 
1204   $formname ||= $self->{formname};
 
1206   $self->{recipient_locale} ||=  Locale->lang_to_locale($self->{language});
 
1207   local $::locale = Locale->new($self->{recipient_locale});
 
1209   my %formname_translations = (
 
1210     bin_list                => $main::locale->text('Bin List'),
 
1211     credit_note             => $main::locale->text('Credit Note'),
 
1212     invoice                 => $main::locale->text('Invoice'),
 
1213     pick_list               => $main::locale->text('Pick List'),
 
1214     proforma                => $main::locale->text('Proforma Invoice'),
 
1215     purchase_order          => $main::locale->text('Purchase Order'),
 
1216     request_quotation       => $main::locale->text('RFQ'),
 
1217     sales_order             => $main::locale->text('Confirmation'),
 
1218     sales_quotation         => $main::locale->text('Quotation'),
 
1219     storno_invoice          => $main::locale->text('Storno Invoice'),
 
1220     sales_delivery_order    => $main::locale->text('Delivery Order'),
 
1221     purchase_delivery_order => $main::locale->text('Delivery Order'),
 
1222     dunning                 => $main::locale->text('Dunning'),
 
1225   $main::lxdebug->leave_sub();
 
1226   return $formname_translations{$formname};
 
1229 sub get_number_prefix_for_type {
 
1230   $main::lxdebug->enter_sub();
 
1234       (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
 
1235     : ($self->{type} =~ /_quotation$/)                        ? 'quo'
 
1236     : ($self->{type} =~ /_delivery_order$/)                   ? 'do'
 
1239   $main::lxdebug->leave_sub();
 
1243 sub get_extension_for_format {
 
1244   $main::lxdebug->enter_sub();
 
1247   my $extension = $self->{format} =~ /pdf/i          ? ".pdf"
 
1248                 : $self->{format} =~ /postscript/i   ? ".ps"
 
1249                 : $self->{format} =~ /opendocument/i ? ".odt"
 
1250                 : $self->{format} =~ /excel/i        ? ".xls"
 
1251                 : $self->{format} =~ /html/i         ? ".html"
 
1254   $main::lxdebug->leave_sub();
 
1258 sub generate_attachment_filename {
 
1259   $main::lxdebug->enter_sub();
 
1262   $self->{recipient_locale} ||=  Locale->lang_to_locale($self->{language});
 
1263   my $recipient_locale = Locale->new($self->{recipient_locale});
 
1265   my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
 
1266   my $prefix              = $self->get_number_prefix_for_type();
 
1268   if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
 
1269     $attachment_filename .= ' (' . $recipient_locale->text('Preview') . ')' . $self->get_extension_for_format();
 
1271   } elsif ($attachment_filename && $self->{"${prefix}number"}) {
 
1272     $attachment_filename .=  "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
 
1275     $attachment_filename = "";
 
1278   $attachment_filename =  $main::locale->quote_special_chars('filenames', $attachment_filename);
 
1279   $attachment_filename =~ s|[\s/\\]+|_|g;
 
1281   $main::lxdebug->leave_sub();
 
1282   return $attachment_filename;
 
1285 sub generate_email_subject {
 
1286   $main::lxdebug->enter_sub();
 
1289   my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
 
1290   my $prefix  = $self->get_number_prefix_for_type();
 
1292   if ($subject && $self->{"${prefix}number"}) {
 
1293     $subject .= " " . $self->{"${prefix}number"}
 
1296   $main::lxdebug->leave_sub();
 
1301   $main::lxdebug->enter_sub();
 
1303   my ($self, $application) = @_;
 
1305   my $error_code = $?;
 
1307   chdir("$self->{tmpdir}");
 
1310   if ((-1 == $error_code) || (127 == (($error_code) >> 8))) {
 
1311     push @err, $::locale->text('The application "#1" was not found on the system.', $application || 'pdflatex') . ' ' . $::locale->text('Please contact your administrator.');
 
1313   } elsif (-f "$self->{tmpfile}.err") {
 
1314     open(FH, "$self->{tmpfile}.err");
 
1319   if ($self->{tmpfile} && !($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})) {
 
1320     $self->{tmpfile} =~ s|.*/||g;
 
1322     $self->{tmpfile} =~ s/\.\w+$//g;
 
1323     my $tmpfile = $self->{tmpfile};
 
1324     unlink(<$tmpfile.*>);
 
1327   chdir("$self->{cwd}");
 
1329   $main::lxdebug->leave_sub();
 
1335   $main::lxdebug->enter_sub();
 
1337   my ($self, $date, $myconfig) = @_;
 
1340   if ($date && $date =~ /\D/) {
 
1342     if ($myconfig->{dateformat} =~ /^yy/) {
 
1343       ($yy, $mm, $dd) = split /\D/, $date;
 
1345     if ($myconfig->{dateformat} =~ /^mm/) {
 
1346       ($mm, $dd, $yy) = split /\D/, $date;
 
1348     if ($myconfig->{dateformat} =~ /^dd/) {
 
1349       ($dd, $mm, $yy) = split /\D/, $date;
 
1354     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
1355     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
1357     $dd = "0$dd" if ($dd < 10);
 
1358     $mm = "0$mm" if ($mm < 10);
 
1360     $date = "$yy$mm$dd";
 
1363   $main::lxdebug->leave_sub();
 
1368 # Database routines used throughout
 
1371   $main::lxdebug->enter_sub(2);
 
1373   my ($self, $myconfig) = @_;
 
1375   # connect to database
 
1376   my $dbh = SL::DBConnect->connect or $self->dberror;
 
1379   if ($myconfig->{dboptions}) {
 
1380     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1383   $main::lxdebug->leave_sub(2);
 
1388 sub dbconnect_noauto {
 
1389   $main::lxdebug->enter_sub();
 
1391   my ($self, $myconfig) = @_;
 
1393   # connect to database
 
1394   my $dbh = SL::DBConnect->connect(SL::DBConnect->get_connect_args(AutoCommit => 0)) or $self->dberror;
 
1397   if ($myconfig->{dboptions}) {
 
1398     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1401   $main::lxdebug->leave_sub();
 
1406 sub get_standard_dbh {
 
1407   $main::lxdebug->enter_sub(2);
 
1410   my $myconfig = shift || \%::myconfig;
 
1412   if ($standard_dbh && !$standard_dbh->{Active}) {
 
1413     $main::lxdebug->message(LXDebug->INFO(), "get_standard_dbh: \$standard_dbh is defined but not Active anymore");
 
1414     undef $standard_dbh;
 
1417   $standard_dbh ||= $self->dbconnect_noauto($myconfig);
 
1419   $main::lxdebug->leave_sub(2);
 
1421   return $standard_dbh;
 
1425   $main::lxdebug->enter_sub();
 
1427   my ($self, $date, $myconfig) = @_;
 
1428   my $dbh = $self->dbconnect($myconfig);
 
1430   my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
 
1431   my $sth = prepare_execute_query($self, $dbh, $query, conv_date($date));
 
1433   # Falls $date = '' - Fehlermeldung aus der Datenbank. Ich denke,
 
1434   # es ist sicher ein conv_date vorher IMMER auszuführen.
 
1435   # Testfälle ohne definiertes closedto:
 
1436   #   Leere Datumseingabe i.O.
 
1437   #     SELECT 1 FROM defaults WHERE '' < closedto
 
1438   #   normale Zahlungsbuchung Ã¼ber Rechnungsmaske i.O.
 
1439   #     SELECT 1 FROM defaults WHERE '10.05.2011' < closedto
 
1440   # Testfälle mit definiertem closedto (30.04.2011):
 
1441   #  Leere Datumseingabe i.O.
 
1442   #   SELECT 1 FROM defaults WHERE '' < closedto
 
1443   # normale Buchung im geschloßenem Zeitraum i.O.
 
1444   #   SELECT 1 FROM defaults WHERE '21.04.2011' < closedto
 
1445   #     Fehlermeldung: Es können keine Zahlungen für abgeschlossene Bücher gebucht werden!
 
1446   # normale Buchung in aktiver Buchungsperiode i.O.
 
1447   #   SELECT 1 FROM defaults WHERE '01.05.2011' < closedto
 
1449   my ($closed) = $sth->fetchrow_array;
 
1451   $main::lxdebug->leave_sub();
 
1456 # prevents bookings to the to far away future
 
1457 sub date_max_future {
 
1458   $main::lxdebug->enter_sub();
 
1460   my ($self, $date, $myconfig) = @_;
 
1461   my $dbh = $self->dbconnect($myconfig);
 
1463   my $query = "SELECT 1 FROM defaults WHERE ? - current_date > max_future_booking_interval";
 
1464   my $sth = prepare_execute_query($self, $dbh, $query, conv_date($date));
 
1466   my ($max_future_booking_interval) = $sth->fetchrow_array;
 
1468   $main::lxdebug->leave_sub();
 
1470   return $max_future_booking_interval;
 
1474 sub update_balance {
 
1475   $main::lxdebug->enter_sub();
 
1477   my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
 
1479   # if we have a value, go do it
 
1482     # retrieve balance from table
 
1483     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1484     my $sth = prepare_execute_query($self, $dbh, $query, @values);
 
1485     my ($balance) = $sth->fetchrow_array;
 
1491     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1492     do_query($self, $dbh, $query, @values);
 
1494   $main::lxdebug->leave_sub();
 
1497 sub update_exchangerate {
 
1498   $main::lxdebug->enter_sub();
 
1500   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1502   # some sanity check for currency
 
1504     $main::lxdebug->leave_sub();
 
1507   $query = qq|SELECT name AS curr FROM currencies WHERE id=(SELECT currency_id FROM defaults)|;
 
1509   my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
 
1511   if ($curr eq $defaultcurrency) {
 
1512     $main::lxdebug->leave_sub();
 
1516   $query = qq|SELECT e.currency_id FROM exchangerate e
 
1517                  WHERE e.currency_id = (SELECT cu.id FROM currencies cu WHERE cu.name=?) AND e.transdate = ?
 
1519   my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
 
1528   $buy = conv_i($buy, "NULL");
 
1529   $sell = conv_i($sell, "NULL");
 
1532   if ($buy != 0 && $sell != 0) {
 
1533     $set = "buy = $buy, sell = $sell";
 
1534   } elsif ($buy != 0) {
 
1535     $set = "buy = $buy";
 
1536   } elsif ($sell != 0) {
 
1537     $set = "sell = $sell";
 
1540   if ($sth->fetchrow_array) {
 
1541     $query = qq|UPDATE exchangerate
 
1543                 WHERE currency_id = (SELECT id FROM currencies WHERE name = ?)
 
1547     $query = qq|INSERT INTO exchangerate (currency_id, buy, sell, transdate)
 
1548                 VALUES ((SELECT id FROM currencies WHERE name = ?), $buy, $sell, ?)|;
 
1551   do_query($self, $dbh, $query, $curr, $transdate);
 
1553   $main::lxdebug->leave_sub();
 
1556 sub save_exchangerate {
 
1557   $main::lxdebug->enter_sub();
 
1559   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1561   my $dbh = $self->dbconnect($myconfig);
 
1565   $buy  = $rate if $fld eq 'buy';
 
1566   $sell = $rate if $fld eq 'sell';
 
1569   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1574   $main::lxdebug->leave_sub();
 
1577 sub get_exchangerate {
 
1578   $main::lxdebug->enter_sub();
 
1580   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1583   unless ($transdate && $curr) {
 
1584     $main::lxdebug->leave_sub();
 
1588   $query = qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|;
 
1590   my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
 
1592   if ($curr eq $defaultcurrency) {
 
1593     $main::lxdebug->leave_sub();
 
1597   $query = qq|SELECT e.$fld FROM exchangerate e
 
1598                  WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|;
 
1599   my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
 
1603   $main::lxdebug->leave_sub();
 
1605   return $exchangerate;
 
1608 sub check_exchangerate {
 
1609   $main::lxdebug->enter_sub();
 
1611   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1613   if ($fld !~/^buy|sell$/) {
 
1614     $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
 
1617   unless ($transdate) {
 
1618     $main::lxdebug->leave_sub();
 
1622   my ($defaultcurrency) = $self->get_default_currency($myconfig);
 
1624   if ($currency eq $defaultcurrency) {
 
1625     $main::lxdebug->leave_sub();
 
1629   my $dbh   = $self->get_standard_dbh($myconfig);
 
1630   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1631                  WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|;
 
1633   my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
 
1635   $main::lxdebug->leave_sub();
 
1637   return $exchangerate;
 
1640 sub get_all_currencies {
 
1641   $main::lxdebug->enter_sub();
 
1644   my $myconfig = shift || \%::myconfig;
 
1645   my $dbh      = $self->get_standard_dbh($myconfig);
 
1648   my $query = qq|SELECT name FROM currencies|;
 
1649   my @currencies = map { $_->{name} } selectall_hashref_query($self, $dbh, $query);
 
1651   $main::lxdebug->leave_sub();
 
1656 sub get_default_currency {
 
1657   $main::lxdebug->enter_sub();
 
1659   my ($self, $myconfig) = @_;
 
1660   my $dbh      = $self->get_standard_dbh($myconfig);
 
1661   my $query = qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|;
 
1663   my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
 
1665   $main::lxdebug->leave_sub();
 
1667   return $defaultcurrency;
 
1670 sub set_payment_options {
 
1671   $main::lxdebug->enter_sub();
 
1673   my ($self, $myconfig, $transdate) = @_;
 
1675   return $main::lxdebug->leave_sub() unless ($self->{payment_id});
 
1677   my $dbh = $self->get_standard_dbh($myconfig);
 
1680     qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long , p.description | .
 
1681     qq|FROM payment_terms p | .
 
1684   ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
 
1685    $self->{payment_terms}, $self->{payment_description}) =
 
1686      selectrow_query($self, $dbh, $query, $self->{payment_id});
 
1688   if ($transdate eq "") {
 
1689     if ($self->{invdate}) {
 
1690       $transdate = $self->{invdate};
 
1692       $transdate = $self->{transdate};
 
1697     qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
 
1698     qq|FROM payment_terms|;
 
1699   ($self->{netto_date}, $self->{skonto_date}) =
 
1700     selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
 
1702   my ($invtotal, $total);
 
1703   my (%amounts, %formatted_amounts);
 
1705   if ($self->{type} =~ /_order$/) {
 
1706     $amounts{invtotal} = $self->{ordtotal};
 
1707     $amounts{total}    = $self->{ordtotal};
 
1709   } elsif ($self->{type} =~ /_quotation$/) {
 
1710     $amounts{invtotal} = $self->{quototal};
 
1711     $amounts{total}    = $self->{quototal};
 
1714     $amounts{invtotal} = $self->{invtotal};
 
1715     $amounts{total}    = $self->{total};
 
1717   map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
 
1719   $amounts{skonto_in_percent}  = 100.0 * $self->{percent_skonto};
 
1720   $amounts{skonto_amount}      = $amounts{invtotal} * $self->{percent_skonto};
 
1721   $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
 
1722   $amounts{total_wo_skonto}    = $amounts{total}    * (1 - $self->{percent_skonto});
 
1724   foreach (keys %amounts) {
 
1725     $amounts{$_}           = $self->round_amount($amounts{$_}, 2);
 
1726     $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
 
1729   if ($self->{"language_id"}) {
 
1731       qq|SELECT t.translation, l.output_numberformat, l.output_dateformat, l.output_longdates | .
 
1732       qq|FROM generic_translations t | .
 
1733       qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1734       qq|WHERE (t.language_id = ?)
 
1735            AND (t.translation_id = ?)
 
1736            AND (t.translation_type = 'SL::DB::PaymentTerm/description_long')|;
 
1737     my ($description_long, $output_numberformat, $output_dateformat,
 
1738       $output_longdates) =
 
1739       selectrow_query($self, $dbh, $query,
 
1740                       $self->{"language_id"}, $self->{"payment_id"});
 
1742     $self->{payment_terms} = $description_long if ($description_long);
 
1744     if ($output_dateformat) {
 
1745       foreach my $key (qw(netto_date skonto_date)) {
 
1747           $main::locale->reformat_date($myconfig, $self->{$key},
 
1753     if ($output_numberformat &&
 
1754         ($output_numberformat ne $myconfig->{"numberformat"})) {
 
1755       my $saved_numberformat = $myconfig->{"numberformat"};
 
1756       $myconfig->{"numberformat"} = $output_numberformat;
 
1757       map { $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) } keys %amounts;
 
1758       $myconfig->{"numberformat"} = $saved_numberformat;
 
1762   $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1763   $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1764   $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
 
1765   $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
 
1766   $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
 
1767   $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
 
1768   $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
 
1770   map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
 
1772   $self->{skonto_in_percent} = $formatted_amounts{skonto_in_percent};
 
1774   $main::lxdebug->leave_sub();
 
1778 sub get_template_language {
 
1779   $main::lxdebug->enter_sub();
 
1781   my ($self, $myconfig) = @_;
 
1783   my $template_code = "";
 
1785   if ($self->{language_id}) {
 
1786     my $dbh = $self->get_standard_dbh($myconfig);
 
1787     my $query = qq|SELECT template_code FROM language WHERE id = ?|;
 
1788     ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
 
1791   $main::lxdebug->leave_sub();
 
1793   return $template_code;
 
1796 sub get_printer_code {
 
1797   $main::lxdebug->enter_sub();
 
1799   my ($self, $myconfig) = @_;
 
1801   my $template_code = "";
 
1803   if ($self->{printer_id}) {
 
1804     my $dbh = $self->get_standard_dbh($myconfig);
 
1805     my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
 
1806     ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
 
1809   $main::lxdebug->leave_sub();
 
1811   return $template_code;
 
1815   $main::lxdebug->enter_sub();
 
1817   my ($self, $myconfig) = @_;
 
1819   my $template_code = "";
 
1821   if ($self->{shipto_id}) {
 
1822     my $dbh = $self->get_standard_dbh($myconfig);
 
1823     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
1824     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
 
1825     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
 
1828   $main::lxdebug->leave_sub();
 
1832   $main::lxdebug->enter_sub();
 
1834   my ($self, $dbh, $id, $module) = @_;
 
1839   foreach my $item (qw(name department_1 department_2 street zipcode city country
 
1840                        contact cp_gender phone fax email)) {
 
1841     if ($self->{"shipto$item"}) {
 
1842       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1844     push(@values, $self->{"shipto${item}"});
 
1848     if ($self->{shipto_id}) {
 
1849       my $query = qq|UPDATE shipto set
 
1851                        shiptodepartment_1 = ?,
 
1852                        shiptodepartment_2 = ?,
 
1858                        shiptocp_gender = ?,
 
1862                      WHERE shipto_id = ?|;
 
1863       do_query($self, $dbh, $query, @values, $self->{shipto_id});
 
1865       my $query = qq|SELECT * FROM shipto
 
1866                      WHERE shiptoname = ? AND
 
1867                        shiptodepartment_1 = ? AND
 
1868                        shiptodepartment_2 = ? AND
 
1869                        shiptostreet = ? AND
 
1870                        shiptozipcode = ? AND
 
1872                        shiptocountry = ? AND
 
1873                        shiptocontact = ? AND
 
1874                        shiptocp_gender = ? AND
 
1880       my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
 
1883           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
 
1884                                  shiptostreet, shiptozipcode, shiptocity, shiptocountry,
 
1885                                  shiptocontact, shiptocp_gender, shiptophone, shiptofax, shiptoemail, module)
 
1886              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1887         do_query($self, $dbh, $query, $id, @values, $module);
 
1892   $main::lxdebug->leave_sub();
 
1896   $main::lxdebug->enter_sub();
 
1898   my ($self, $dbh) = @_;
 
1900   $dbh ||= $self->get_standard_dbh(\%main::myconfig);
 
1902   my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
 
1903   ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
 
1904   $self->{"employee_id"} *= 1;
 
1906   $main::lxdebug->leave_sub();
 
1909 sub get_employee_data {
 
1910   $main::lxdebug->enter_sub();
 
1915   Common::check_params(\%params, qw(prefix));
 
1916   Common::check_params_x(\%params, qw(id));
 
1919     $main::lxdebug->leave_sub();
 
1923   my $myconfig = \%main::myconfig;
 
1924   my $dbh      = $params{dbh} || $self->get_standard_dbh($myconfig);
 
1926   my ($login)  = selectrow_query($self, $dbh, qq|SELECT login FROM employee WHERE id = ?|, conv_i($params{id}));
 
1929     my $user = User->new(login => $login);
 
1930     map { $self->{$params{prefix} . "_${_}"} = $user->{$_}; } qw(address businessnumber co_ustid company duns email fax name signature taxnumber tel);
 
1932     $self->{$params{prefix} . '_login'}   = $login;
 
1933     $self->{$params{prefix} . '_name'}  ||= $login;
 
1936   $main::lxdebug->leave_sub();
 
1940   $main::lxdebug->enter_sub();
 
1942   my ($self, $myconfig, $reference_date) = @_;
 
1944   $reference_date = $reference_date ? conv_dateq($reference_date) . '::DATE' : 'current_date';
 
1946   my $dbh         = $self->get_standard_dbh($myconfig);
 
1947   my ($payment_id, $duedate);
 
1949   if($self->{payment_id}) {
 
1950     $payment_id = $self->{payment_id};
 
1951   } elsif($self->{vendor_id}) {
 
1952     my $query = 'SELECT payment_id FROM vendor WHERE id = ?';
 
1953     ($payment_id) = selectrow_query($self, $dbh, $query, $self->{vendor_id});
 
1957     my $query  = qq|SELECT ${reference_date} + terms_netto FROM payment_terms WHERE id = ?|;
 
1958     ($duedate) = selectrow_query($self, $dbh, $query, $payment_id);
 
1961   $main::lxdebug->leave_sub();
 
1967   $main::lxdebug->enter_sub();
 
1969   my ($self, $dbh, $id, $key) = @_;
 
1971   $key = "all_contacts" unless ($key);
 
1975     $main::lxdebug->leave_sub();
 
1980     qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
 
1981     qq|FROM contacts | .
 
1982     qq|WHERE cp_cv_id = ? | .
 
1983     qq|ORDER BY lower(cp_name)|;
 
1985   $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
 
1987   $main::lxdebug->leave_sub();
 
1991   $main::lxdebug->enter_sub();
 
1993   my ($self, $dbh, $key) = @_;
 
1995   my ($all, $old_id, $where, @values);
 
1997   if (ref($key) eq "HASH") {
 
2000     $key = "ALL_PROJECTS";
 
2002     foreach my $p (keys(%{$params})) {
 
2004         $all = $params->{$p};
 
2005       } elsif ($p eq "old_id") {
 
2006         $old_id = $params->{$p};
 
2007       } elsif ($p eq "key") {
 
2008         $key = $params->{$p};
 
2014     $where = "WHERE active ";
 
2016       if (ref($old_id) eq "ARRAY") {
 
2017         my @ids = grep({ $_ } @{$old_id});
 
2019           $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
 
2020           push(@values, @ids);
 
2023         $where .= " OR (id = ?) ";
 
2024         push(@values, $old_id);
 
2030     qq|SELECT id, projectnumber, description, active | .
 
2033     qq|ORDER BY lower(projectnumber)|;
 
2035   $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
 
2037   $main::lxdebug->leave_sub();
 
2041   $main::lxdebug->enter_sub();
 
2043   my ($self, $dbh, $vc_id, $key) = @_;
 
2045   $key = "all_shipto" unless ($key);
 
2048     # get shipping addresses
 
2049     my $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
 
2051     $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
 
2057   $main::lxdebug->leave_sub();
 
2061   $main::lxdebug->enter_sub();
 
2063   my ($self, $dbh, $key) = @_;
 
2065   $key = "all_printers" unless ($key);
 
2067   my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
 
2069   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2071   $main::lxdebug->leave_sub();
 
2075   $main::lxdebug->enter_sub();
 
2077   my ($self, $dbh, $params) = @_;
 
2080   $key = $params->{key};
 
2081   $key = "all_charts" unless ($key);
 
2083   my $transdate = quote_db_date($params->{transdate});
 
2086     qq|SELECT c.id, c.accno, c.description, c.link, c.charttype, tk.taxkey_id, tk.tax_id | .
 
2088     qq|LEFT JOIN taxkeys tk ON | .
 
2089     qq|(tk.id = (SELECT id FROM taxkeys | .
 
2090     qq|          WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
 
2091     qq|          ORDER BY startdate DESC LIMIT 1)) | .
 
2092     qq|ORDER BY c.accno|;
 
2094   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2096   $main::lxdebug->leave_sub();
 
2099 sub _get_taxcharts {
 
2100   $main::lxdebug->enter_sub();
 
2102   my ($self, $dbh, $params) = @_;
 
2104   my $key = "all_taxcharts";
 
2107   if (ref $params eq 'HASH') {
 
2108     $key = $params->{key} if ($params->{key});
 
2109     if ($params->{module} eq 'AR') {
 
2110       push @where, 'taxkey NOT IN (8, 9, 18, 19)';
 
2112     } elsif ($params->{module} eq 'AP') {
 
2113       push @where, 'taxkey NOT IN (1, 2, 3, 12, 13)';
 
2120   my $where = @where ? ' WHERE ' . join(' AND ', map { "($_)" } @where) : '';
 
2122   my $query = qq|SELECT * FROM tax $where ORDER BY taxkey, rate|;
 
2124   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2126   $main::lxdebug->leave_sub();
 
2130   $main::lxdebug->enter_sub();
 
2132   my ($self, $dbh, $key) = @_;
 
2134   $key = "all_taxzones" unless ($key);
 
2136   my $query = qq|SELECT * FROM tax_zones ORDER BY id|;
 
2138   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2140   $main::lxdebug->leave_sub();
 
2143 sub _get_employees {
 
2144   $main::lxdebug->enter_sub();
 
2146   my ($self, $dbh, $default_key, $key) = @_;
 
2148   $key = $default_key unless ($key);
 
2149   $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee ORDER BY lower(name)|);
 
2151   $main::lxdebug->leave_sub();
 
2154 sub _get_business_types {
 
2155   $main::lxdebug->enter_sub();
 
2157   my ($self, $dbh, $key) = @_;
 
2159   my $options       = ref $key eq 'HASH' ? $key : { key => $key };
 
2160   $options->{key} ||= "all_business_types";
 
2163   if (exists $options->{salesman}) {
 
2164     $where = 'WHERE ' . ($options->{salesman} ? '' : 'NOT ') . 'COALESCE(salesman)';
 
2167   $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, qq|SELECT * FROM business $where ORDER BY lower(description)|);
 
2169   $main::lxdebug->leave_sub();
 
2172 sub _get_languages {
 
2173   $main::lxdebug->enter_sub();
 
2175   my ($self, $dbh, $key) = @_;
 
2177   $key = "all_languages" unless ($key);
 
2179   my $query = qq|SELECT * FROM language ORDER BY id|;
 
2181   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2183   $main::lxdebug->leave_sub();
 
2186 sub _get_dunning_configs {
 
2187   $main::lxdebug->enter_sub();
 
2189   my ($self, $dbh, $key) = @_;
 
2191   $key = "all_dunning_configs" unless ($key);
 
2193   my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
 
2195   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2197   $main::lxdebug->leave_sub();
 
2200 sub _get_currencies {
 
2201 $main::lxdebug->enter_sub();
 
2203   my ($self, $dbh, $key) = @_;
 
2205   $key = "all_currencies" unless ($key);
 
2207   $self->{$key} = [$self->get_all_currencies()];
 
2209   $main::lxdebug->leave_sub();
 
2213 $main::lxdebug->enter_sub();
 
2215   my ($self, $dbh, $key) = @_;
 
2217   $key = "all_payments" unless ($key);
 
2219   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
 
2221   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2223   $main::lxdebug->leave_sub();
 
2226 sub _get_customers {
 
2227   $main::lxdebug->enter_sub();
 
2229   my ($self, $dbh, $key) = @_;
 
2231   my $options        = ref $key eq 'HASH' ? $key : { key => $key };
 
2232   $options->{key}  ||= "all_customers";
 
2233   my $limit_clause   = $options->{limit} ? "LIMIT $options->{limit}" : '';
 
2236   push @where, qq|business_id IN (SELECT id FROM business WHERE salesman)| if  $options->{business_is_salesman};
 
2237   push @where, qq|NOT obsolete|                                            if !$options->{with_obsolete};
 
2238   my $where_str = @where ? "WHERE " . join(" AND ", map { "($_)" } @where) : '';
 
2240   my $query = qq|SELECT * FROM customer $where_str ORDER BY name $limit_clause|;
 
2241   $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, $query);
 
2243   $main::lxdebug->leave_sub();
 
2247   $main::lxdebug->enter_sub();
 
2249   my ($self, $dbh, $key) = @_;
 
2251   $key = "all_vendors" unless ($key);
 
2253   my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
 
2255   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2257   $main::lxdebug->leave_sub();
 
2260 sub _get_departments {
 
2261   $main::lxdebug->enter_sub();
 
2263   my ($self, $dbh, $key) = @_;
 
2265   $key = "all_departments" unless ($key);
 
2267   my $query = qq|SELECT * FROM department ORDER BY description|;
 
2269   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2271   $main::lxdebug->leave_sub();
 
2274 sub _get_warehouses {
 
2275   $main::lxdebug->enter_sub();
 
2277   my ($self, $dbh, $param) = @_;
 
2279   my ($key, $bins_key);
 
2281   if ('' eq ref $param) {
 
2285     $key      = $param->{key};
 
2286     $bins_key = $param->{bins};
 
2289   my $query = qq|SELECT w.* FROM warehouse w
 
2290                  WHERE (NOT w.invalid) AND
 
2291                    ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
 
2292                  ORDER BY w.sortkey|;
 
2294   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2297     $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?
 
2298                 ORDER BY description|;
 
2299     my $sth = prepare_query($self, $dbh, $query);
 
2301     foreach my $warehouse (@{ $self->{$key} }) {
 
2302       do_statement($self, $sth, $query, $warehouse->{id});
 
2303       $warehouse->{$bins_key} = [];
 
2305       while (my $ref = $sth->fetchrow_hashref()) {
 
2306         push @{ $warehouse->{$bins_key} }, $ref;
 
2312   $main::lxdebug->leave_sub();
 
2316   $main::lxdebug->enter_sub();
 
2318   my ($self, $dbh, $table, $key, $sortkey) = @_;
 
2320   my $query  = qq|SELECT * FROM $table|;
 
2321   $query    .= qq| ORDER BY $sortkey| if ($sortkey);
 
2323   $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2325   $main::lxdebug->leave_sub();
 
2329 #  $main::lxdebug->enter_sub();
 
2331 #  my ($self, $dbh, $key) = @_;
 
2333 #  $key ||= "all_groups";
 
2335 #  my $groups = $main::auth->read_groups();
 
2337 #  $self->{$key} = selectall_hashref_query($self, $dbh, $query);
 
2339 #  $main::lxdebug->leave_sub();
 
2343   $main::lxdebug->enter_sub();
 
2348   my $dbh = $self->get_standard_dbh(\%main::myconfig);
 
2349   my ($sth, $query, $ref);
 
2351   my $vc = $self->{"vc"} eq "customer" ? "customer" : "vendor";
 
2352   my $vc_id = $self->{"${vc}_id"};
 
2354   if ($params{"contacts"}) {
 
2355     $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
 
2358   if ($params{"shipto"}) {
 
2359     $self->_get_shipto($dbh, $vc_id, $params{"shipto"});
 
2362   if ($params{"projects"} || $params{"all_projects"}) {
 
2363     $self->_get_projects($dbh, $params{"all_projects"} ?
 
2364                          $params{"all_projects"} : $params{"projects"},
 
2365                          $params{"all_projects"} ? 1 : 0);
 
2368   if ($params{"printers"}) {
 
2369     $self->_get_printers($dbh, $params{"printers"});
 
2372   if ($params{"languages"}) {
 
2373     $self->_get_languages($dbh, $params{"languages"});
 
2376   if ($params{"charts"}) {
 
2377     $self->_get_charts($dbh, $params{"charts"});
 
2380   if ($params{"taxcharts"}) {
 
2381     $self->_get_taxcharts($dbh, $params{"taxcharts"});
 
2384   if ($params{"taxzones"}) {
 
2385     $self->_get_taxzones($dbh, $params{"taxzones"});
 
2388   if ($params{"employees"}) {
 
2389     $self->_get_employees($dbh, "all_employees", $params{"employees"});
 
2392   if ($params{"salesmen"}) {
 
2393     $self->_get_employees($dbh, "all_salesmen", $params{"salesmen"});
 
2396   if ($params{"business_types"}) {
 
2397     $self->_get_business_types($dbh, $params{"business_types"});
 
2400   if ($params{"dunning_configs"}) {
 
2401     $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
 
2404   if($params{"currencies"}) {
 
2405     $self->_get_currencies($dbh, $params{"currencies"});
 
2408   if($params{"customers"}) {
 
2409     $self->_get_customers($dbh, $params{"customers"});
 
2412   if($params{"vendors"}) {
 
2413     if (ref $params{"vendors"} eq 'HASH') {
 
2414       $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
 
2416       $self->_get_vendors($dbh, $params{"vendors"});
 
2420   if($params{"payments"}) {
 
2421     $self->_get_payments($dbh, $params{"payments"});
 
2424   if($params{"departments"}) {
 
2425     $self->_get_departments($dbh, $params{"departments"});
 
2428   if ($params{price_factors}) {
 
2429     $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
 
2432   if ($params{warehouses}) {
 
2433     $self->_get_warehouses($dbh, $params{warehouses});
 
2436 #  if ($params{groups}) {
 
2437 #    $self->_get_groups($dbh, $params{groups});
 
2440   if ($params{partsgroup}) {
 
2441     $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
 
2444   $main::lxdebug->leave_sub();
 
2447 # this sub gets the id and name from $table
 
2449   $main::lxdebug->enter_sub();
 
2451   my ($self, $myconfig, $table) = @_;
 
2453   # connect to database
 
2454   my $dbh = $self->get_standard_dbh($myconfig);
 
2456   $table = $table eq "customer" ? "customer" : "vendor";
 
2457   my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
 
2459   my ($query, @values);
 
2461   if (!$self->{openinvoices}) {
 
2463     if ($self->{customernumber} ne "") {
 
2464       $where = qq|(vc.customernumber ILIKE ?)|;
 
2465       push(@values, '%' . $self->{customernumber} . '%');
 
2467       $where = qq|(vc.name ILIKE ?)|;
 
2468       push(@values, '%' . $self->{$table} . '%');
 
2472       qq~SELECT vc.id, vc.name,
 
2473            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
2475          WHERE $where AND (NOT vc.obsolete)
 
2479       qq~SELECT DISTINCT vc.id, vc.name,
 
2480            vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
 
2482          JOIN $table vc ON (a.${table}_id = vc.id)
 
2483          WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
 
2485     push(@values, '%' . $self->{$table} . '%');
 
2488   $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
 
2490   $main::lxdebug->leave_sub();
 
2492   return scalar(@{ $self->{name_list} });
 
2495 # the selection sub is used in the AR, AP, IS, IR, DO and OE module
 
2498   $main::lxdebug->enter_sub();
 
2500   my ($self, $myconfig, $table, $module) = @_;
 
2503   my $dbh = $self->get_standard_dbh;
 
2505   $table = $table eq "customer" ? "customer" : "vendor";
 
2507   # build selection list
 
2508   # Hotfix für Bug 1837 - Besser wäre es alte Buchungsbelege
 
2509   # OHNE Auswahlliste (reines Textfeld) zu laden. Hilft aber auch
 
2510   # nicht für veränderbare Belege (oe, do, ...)
 
2511   my $obsolete = $self->{id} ? '' : "WHERE NOT obsolete";
 
2512   my $query = qq|SELECT count(*) FROM $table $obsolete|;
 
2513   my ($count) = selectrow_query($self, $dbh, $query);
 
2515   if ($count <= $myconfig->{vclimit}) {
 
2516     $query = qq|SELECT id, name, salesman_id
 
2517                 FROM $table $obsolete
 
2519     $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
 
2523   $self->get_employee($dbh);
 
2525   # setup sales contacts
 
2526   $query = qq|SELECT e.id, e.name
 
2528               WHERE (e.sales = '1') AND (NOT e.id = ?)
 
2530   $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
 
2533   push(@{ $self->{all_employees} },
 
2534        { id   => $self->{employee_id},
 
2535          name => $self->{employee} });
 
2537     # prepare query for departments
 
2538     $query = qq|SELECT id, description
 
2540                 ORDER BY description|;
 
2542   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2545   $query = qq|SELECT id, description
 
2549   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2552   $query = qq|SELECT printer_description, id
 
2554               ORDER BY printer_description|;
 
2556   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2559   $query = qq|SELECT id, description
 
2563   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2565   $main::lxdebug->leave_sub();
 
2568 sub language_payment {
 
2569   $main::lxdebug->enter_sub();
 
2571   my ($self, $myconfig) = @_;
 
2573   my $dbh = $self->get_standard_dbh($myconfig);
 
2575   my $query = qq|SELECT id, description
 
2579   $self->{languages} = selectall_hashref_query($self, $dbh, $query);
 
2582   $query = qq|SELECT printer_description, id
 
2584               ORDER BY printer_description|;
 
2586   $self->{printers} = selectall_hashref_query($self, $dbh, $query);
 
2589   $query = qq|SELECT id, description
 
2593   $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
 
2595   # get buchungsgruppen
 
2596   $query = qq|SELECT id, description
 
2597               FROM buchungsgruppen|;
 
2599   $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
 
2601   $main::lxdebug->leave_sub();
 
2604 # this is only used for reports
 
2605 sub all_departments {
 
2606   $main::lxdebug->enter_sub();
 
2608   my ($self, $myconfig, $table) = @_;
 
2610   my $dbh = $self->get_standard_dbh($myconfig);
 
2612   my $query = qq|SELECT id, description
 
2614                  ORDER BY description|;
 
2615   $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
 
2617   delete($self->{all_departments}) unless (@{ $self->{all_departments} || [] });
 
2619   $main::lxdebug->leave_sub();
 
2623   $main::lxdebug->enter_sub();
 
2625   my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
 
2628   if ($table eq "customer") {
 
2637   $self->all_vc($myconfig, $table, $module);
 
2639   # get last customers or vendors
 
2640   my ($query, $sth, $ref);
 
2642   my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
 
2647     my $transdate = "current_date";
 
2648     if ($self->{transdate}) {
 
2649       $transdate = $dbh->quote($self->{transdate});
 
2652     # now get the account numbers
 
2653 #    $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2654 #                FROM chart c, taxkeys tk
 
2655 #                WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
 
2656 #                  (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
 
2657 #                ORDER BY c.accno|;
 
2659 #  same query as above, but without expensive subquery for each row. about 80% faster
 
2661       SELECT c.accno, c.description, c.link, c.taxkey_id, tk2.tax_id
 
2663         -- find newest entries in taxkeys
 
2665           SELECT chart_id, MAX(startdate) AS startdate
 
2667           WHERE (startdate <= $transdate)
 
2669         ) tk ON (c.id = tk.chart_id)
 
2670         -- and load all of those entries
 
2671         INNER JOIN taxkeys tk2
 
2672            ON (tk.chart_id = tk2.chart_id AND tk.startdate = tk2.startdate)
 
2673        WHERE (c.link LIKE ?)
 
2676     $sth = $dbh->prepare($query);
 
2678     do_statement($self, $sth, $query, '%' . $module . '%');
 
2680     $self->{accounts} = "";
 
2681     while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2683       foreach my $key (split(/:/, $ref->{link})) {
 
2684         if ($key =~ /\Q$module\E/) {
 
2686           # cross reference for keys
 
2687           $xkeyref{ $ref->{accno} } = $key;
 
2689           push @{ $self->{"${module}_links"}{$key} },
 
2690             { accno       => $ref->{accno},
 
2691               description => $ref->{description},
 
2692               taxkey      => $ref->{taxkey_id},
 
2693               tax_id      => $ref->{tax_id} };
 
2695           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2701   # get taxkeys and description
 
2702   $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
 
2703   $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
 
2705   if (($module eq "AP") || ($module eq "AR")) {
 
2706     # get tax rates and description
 
2707     $query = qq|SELECT * FROM tax|;
 
2708     $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
 
2711   my $extra_columns = '';
 
2712   $extra_columns   .= 'a.direct_debit, ' if ($module eq 'AR') || ($module eq 'AP');
 
2717            a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
 
2718            a.duedate, a.ordnumber, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.notes,
 
2719            a.intnotes, a.department_id, a.amount AS oldinvtotal,
 
2720            a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
 
2721            a.globalproject_id, ${extra_columns}
 
2723            d.description AS department,
 
2726          JOIN $table c ON (a.${table}_id = c.id)
 
2727          LEFT JOIN employee e ON (e.id = a.employee_id)
 
2728          LEFT JOIN department d ON (d.id = a.department_id)
 
2730     $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
 
2732     foreach my $key (keys %$ref) {
 
2733       $self->{$key} = $ref->{$key};
 
2736     my $transdate = "current_date";
 
2737     if ($self->{transdate}) {
 
2738       $transdate = $dbh->quote($self->{transdate});
 
2741     # now get the account numbers
 
2742     $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
 
2744                 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
 
2746                   AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
 
2747                     OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
 
2750     $sth = $dbh->prepare($query);
 
2751     do_statement($self, $sth, $query, "%$module%");
 
2753     $self->{accounts} = "";
 
2754     while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2756       foreach my $key (split(/:/, $ref->{link})) {
 
2757         if ($key =~ /\Q$module\E/) {
 
2759           # cross reference for keys
 
2760           $xkeyref{ $ref->{accno} } = $key;
 
2762           push @{ $self->{"${module}_links"}{$key} },
 
2763             { accno       => $ref->{accno},
 
2764               description => $ref->{description},
 
2765               taxkey      => $ref->{taxkey_id},
 
2766               tax_id      => $ref->{tax_id} };
 
2768           $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
2774     # get amounts from individual entries
 
2777            c.accno, c.description,
 
2778            a.acc_trans_id, a.source, a.amount, a.memo, a.transdate, a.gldate, a.cleared, a.project_id, a.taxkey,
 
2782          LEFT JOIN chart c ON (c.id = a.chart_id)
 
2783          LEFT JOIN project p ON (p.id = a.project_id)
 
2784          LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
 
2785                                     WHERE (tk.taxkey_id=a.taxkey) AND
 
2786                                       ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
2787                                         THEN tk.chart_id = a.chart_id
 
2790                                        OR (c.link='%tax%')) AND
 
2791                                       (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
 
2792          WHERE a.trans_id = ?
 
2793          AND a.fx_transaction = '0'
 
2794          ORDER BY a.acc_trans_id, a.transdate|;
 
2795     $sth = $dbh->prepare($query);
 
2796     do_statement($self, $sth, $query, $self->{id});
 
2798     # get exchangerate for currency
 
2799     $self->{exchangerate} =
 
2800       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2803     # store amounts in {acc_trans}{$key} for multiple accounts
 
2804     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2805       $ref->{exchangerate} =
 
2806         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
 
2807       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
2810       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
2811         $ref->{amount} *= -1;
 
2813       $ref->{index} = $index;
 
2815       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
2822            d.closedto, d.revtrans,
 
2823            (SELECT cu.name FROM currencies cu WHERE cu.id=d.currency_id) AS defaultcurrency,
 
2824            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2825            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2827     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2828     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2835             current_date AS transdate, d.closedto, d.revtrans,
 
2836             (SELECT cu.name FROM currencies cu WHERE cu.id=d.currency_id) AS defaultcurrency,
 
2837             (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
2838             (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
2840     $ref = selectfirst_hashref_query($self, $dbh, $query);
 
2841     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
2843     if ($self->{"$self->{vc}_id"}) {
 
2845       # only setup currency
 
2846       ($self->{currency}) = $self->{defaultcurrency} if !$self->{currency};
 
2850       $self->lastname_used($dbh, $myconfig, $table, $module);
 
2852       # get exchangerate for currency
 
2853       $self->{exchangerate} =
 
2854         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
 
2860   $main::lxdebug->leave_sub();
 
2864   $main::lxdebug->enter_sub();
 
2866   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
2870   $table         = $table eq "customer" ? "customer" : "vendor";
 
2871   my %column_map = ("a.${table}_id"           => "${table}_id",
 
2872                     "a.department_id"         => "department_id",
 
2873                     "d.description"           => "department",
 
2874                     "ct.name"                 => $table,
 
2875                     "cu.name"                 => "currency",
 
2876                     "current_date + ct.terms" => "duedate",
 
2879   if ($self->{type} =~ /delivery_order/) {
 
2880     $arap  = 'delivery_orders';
 
2881     delete $column_map{"cu.currency"};
 
2883   } elsif ($self->{type} =~ /_order/) {
 
2885     $where = "quotation = '0'";
 
2887   } elsif ($self->{type} =~ /_quotation/) {
 
2889     $where = "quotation = '1'";
 
2891   } elsif ($table eq 'customer') {
 
2899   $where           = "($where) AND" if ($where);
 
2900   my $query        = qq|SELECT MAX(id) FROM $arap
 
2901                         WHERE $where ${table}_id > 0|;
 
2902   my ($trans_id)   = selectrow_query($self, $dbh, $query);
 
2905   my $column_spec  = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
 
2906   $query           = qq|SELECT $column_spec
 
2908                         LEFT JOIN $table     ct ON (a.${table}_id = ct.id)
 
2909                         LEFT JOIN department d  ON (a.department_id = d.id)
 
2910                         LEFT JOIN currencies cu ON (cu.id=ct.currency_id)
 
2912   my $ref          = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
 
2914   map { $self->{$_} = $ref->{$_} } values %column_map;
 
2916   $main::lxdebug->leave_sub();
 
2920   $main::lxdebug->enter_sub();
 
2923   my $myconfig = shift || \%::myconfig;
 
2924   my ($thisdate, $days) = @_;
 
2926   my $dbh = $self->get_standard_dbh($myconfig);
 
2931     my $dateformat = $myconfig->{dateformat};
 
2932     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
2933     $thisdate = $dbh->quote($thisdate);
 
2934     $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
 
2936     $query = qq|SELECT current_date AS thisdate|;
 
2939   ($thisdate) = selectrow_query($self, $dbh, $query);
 
2941   $main::lxdebug->leave_sub();
 
2947   $main::lxdebug->enter_sub();
 
2949   my ($self, $string) = @_;
 
2951   if ($string !~ /%/) {
 
2952     $string = "%$string%";
 
2955   $string =~ s/\'/\'\'/g;
 
2957   $main::lxdebug->leave_sub();
 
2963   $main::lxdebug->enter_sub();
 
2965   my ($self, $flds, $new, $count, $numrows) = @_;
 
2969   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
 
2974   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
2976     my $j = $item->{ndx} - 1;
 
2977     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
2981   for $i ($count + 1 .. $numrows) {
 
2982     map { delete $self->{"${_}_$i"} } @{$flds};
 
2985   $main::lxdebug->leave_sub();
 
2989   $main::lxdebug->enter_sub();
 
2991   my ($self, $myconfig) = @_;
 
2995   my $dbh = $self->dbconnect_noauto($myconfig);
 
2997   my $query = qq|DELETE FROM status
 
2998                  WHERE (formname = ?) AND (trans_id = ?)|;
 
2999   my $sth = prepare_query($self, $dbh, $query);
 
3001   if ($self->{formname} =~ /(check|receipt)/) {
 
3002     for $i (1 .. $self->{rowcount}) {
 
3003       do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
 
3006     do_statement($self, $sth, $query, $self->{formname}, $self->{id});
 
3010   my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3011   my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3013   my %queued = split / /, $self->{queued};
 
3016   if ($self->{formname} =~ /(check|receipt)/) {
 
3018     # this is a check or receipt, add one entry for each lineitem
 
3019     my ($accno) = split /--/, $self->{account};
 
3020     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
 
3021                 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
 
3022     @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
 
3023     $sth = prepare_query($self, $dbh, $query);
 
3025     for $i (1 .. $self->{rowcount}) {
 
3026       if ($self->{"checked_$i"}) {
 
3027         do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
 
3033     $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
3034                 VALUES (?, ?, ?, ?, ?)|;
 
3035     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
 
3036              $queued{$self->{formname}}, $self->{formname});
 
3042   $main::lxdebug->leave_sub();
 
3046   $main::lxdebug->enter_sub();
 
3048   my ($self, $dbh) = @_;
 
3050   my ($query, $printed, $emailed);
 
3052   my $formnames  = $self->{printed};
 
3053   my $emailforms = $self->{emailed};
 
3055   $query = qq|DELETE FROM status
 
3056                  WHERE (formname = ?) AND (trans_id = ?)|;
 
3057   do_query($self, $dbh, $query, $self->{formname}, $self->{id});
 
3059   # this only applies to the forms
 
3060   # checks and receipts are posted when printed or queued
 
3062   if ($self->{queued}) {
 
3063     my %queued = split / /, $self->{queued};
 
3065     foreach my $formname (keys %queued) {
 
3066       $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3067       $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3069       $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
 
3070                   VALUES (?, ?, ?, ?, ?)|;
 
3071       do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
 
3073       $formnames  =~ s/\Q$self->{formname}\E//;
 
3074       $emailforms =~ s/\Q$self->{formname}\E//;
 
3079   # save printed, emailed info
 
3080   $formnames  =~ s/^ +//g;
 
3081   $emailforms =~ s/^ +//g;
 
3084   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
3085   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
3087   foreach my $formname (keys %status) {
 
3088     $printed = ($formnames  =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3089     $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
 
3091     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
3092                 VALUES (?, ?, ?, ?)|;
 
3093     do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
 
3096   $main::lxdebug->leave_sub();
 
3100 # $main::locale->text('SAVED')
 
3101 # $main::locale->text('DELETED')
 
3102 # $main::locale->text('ADDED')
 
3103 # $main::locale->text('PAYMENT POSTED')
 
3104 # $main::locale->text('POSTED')
 
3105 # $main::locale->text('POSTED AS NEW')
 
3106 # $main::locale->text('ELSE')
 
3107 # $main::locale->text('SAVED FOR DUNNING')
 
3108 # $main::locale->text('DUNNING STARTED')
 
3109 # $main::locale->text('PRINTED')
 
3110 # $main::locale->text('MAILED')
 
3111 # $main::locale->text('SCREENED')
 
3112 # $main::locale->text('CANCELED')
 
3113 # $main::locale->text('invoice')
 
3114 # $main::locale->text('proforma')
 
3115 # $main::locale->text('sales_order')
 
3116 # $main::locale->text('pick_list')
 
3117 # $main::locale->text('purchase_order')
 
3118 # $main::locale->text('bin_list')
 
3119 # $main::locale->text('sales_quotation')
 
3120 # $main::locale->text('request_quotation')
 
3123   $main::lxdebug->enter_sub();
 
3126   my $dbh  = shift || $self->get_standard_dbh;
 
3128   if(!exists $self->{employee_id}) {
 
3129     &get_employee($self, $dbh);
 
3133    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
 
3134    qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
 
3135   my @values = (conv_i($self->{id}), $self->{login},
 
3136                 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
 
3137   do_query($self, $dbh, $query, @values);
 
3141   $main::lxdebug->leave_sub();
 
3145   $main::lxdebug->enter_sub();
 
3147   my ($self, $dbh, $trans_id, $restriction, $order) = @_;
 
3148   my ($orderBy, $desc) = split(/\-\-/, $order);
 
3149   $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
 
3152   if ($trans_id ne "") {
 
3154       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 | .
 
3155       qq|FROM history_erp h | .
 
3156       qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
 
3157       qq|WHERE (trans_id = | . $trans_id . qq|) $restriction | .
 
3160     my $sth = $dbh->prepare($query) || $self->dberror($query);
 
3162     $sth->execute() || $self->dberror("$query");
 
3164     while(my $hash_ref = $sth->fetchrow_hashref()) {
 
3165       $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
 
3166       $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
 
3167       $hash_ref->{snumbers} =~ s/^.+_(.*)$/$1/g;
 
3168       $tempArray[$i++] = $hash_ref;
 
3170     $main::lxdebug->leave_sub() and return \@tempArray
 
3171       if ($i > 0 && $tempArray[0] ne "");
 
3173   $main::lxdebug->leave_sub();
 
3177 sub update_defaults {
 
3178   $main::lxdebug->enter_sub();
 
3180   my ($self, $myconfig, $fld, $provided_dbh) = @_;
 
3183   if ($provided_dbh) {
 
3184     $dbh = $provided_dbh;
 
3186     $dbh = $self->dbconnect_noauto($myconfig);
 
3188   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
3189   my $sth   = $dbh->prepare($query);
 
3191   $sth->execute || $self->dberror($query);
 
3192   my ($var) = $sth->fetchrow_array;
 
3195   $var   = 0 if !defined($var) || ($var eq '');
 
3196   $var   = SL::PrefixedNumber->new(number => $var)->get_next;
 
3197   $query = qq|UPDATE defaults SET $fld = ?|;
 
3198   do_query($self, $dbh, $query, $var);
 
3200   if (!$provided_dbh) {
 
3205   $main::lxdebug->leave_sub();
 
3210 sub update_business {
 
3211   $main::lxdebug->enter_sub();
 
3213   my ($self, $myconfig, $business_id, $provided_dbh) = @_;
 
3216   if ($provided_dbh) {
 
3217     $dbh = $provided_dbh;
 
3219     $dbh = $self->dbconnect_noauto($myconfig);
 
3222     qq|SELECT customernumberinit FROM business
 
3223        WHERE id = ? FOR UPDATE|;
 
3224   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
3226   return undef unless $var;
 
3228   if ($var =~ m/\d+$/) {
 
3229     my $new_var  = (substr $var, $-[0]) * 1 + 1;
 
3230     my $len_diff = length($var) - $-[0] - length($new_var);
 
3231     $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
 
3237   $query = qq|UPDATE business
 
3238               SET customernumberinit = ?
 
3240   do_query($self, $dbh, $query, $var, $business_id);
 
3242   if (!$provided_dbh) {
 
3247   $main::lxdebug->leave_sub();
 
3252 sub get_partsgroup {
 
3253   $main::lxdebug->enter_sub();
 
3255   my ($self, $myconfig, $p) = @_;
 
3256   my $target = $p->{target} || 'all_partsgroup';
 
3258   my $dbh = $self->get_standard_dbh($myconfig);
 
3260   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
3262                  JOIN parts p ON (p.partsgroup_id = pg.id) |;
 
3265   if ($p->{searchitems} eq 'part') {
 
3266     $query .= qq|WHERE p.inventory_accno_id > 0|;
 
3268   if ($p->{searchitems} eq 'service') {
 
3269     $query .= qq|WHERE p.inventory_accno_id IS NULL|;
 
3271   if ($p->{searchitems} eq 'assembly') {
 
3272     $query .= qq|WHERE p.assembly = '1'|;
 
3274   if ($p->{searchitems} eq 'labor') {
 
3275     $query .= qq|WHERE (p.inventory_accno_id > 0) AND (p.income_accno_id IS NULL)|;
 
3278   $query .= qq|ORDER BY partsgroup|;
 
3281     $query = qq|SELECT id, partsgroup FROM partsgroup
 
3282                 ORDER BY partsgroup|;
 
3285   if ($p->{language_code}) {
 
3286     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
3287                   t.description AS translation
 
3289                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
3290                 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
 
3291                 ORDER BY translation|;
 
3292     @values = ($p->{language_code});
 
3295   $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
 
3297   $main::lxdebug->leave_sub();
 
3300 sub get_pricegroup {
 
3301   $main::lxdebug->enter_sub();
 
3303   my ($self, $myconfig, $p) = @_;
 
3305   my $dbh = $self->get_standard_dbh($myconfig);
 
3307   my $query = qq|SELECT p.id, p.pricegroup
 
3310   $query .= qq| ORDER BY pricegroup|;
 
3313     $query = qq|SELECT id, pricegroup FROM pricegroup
 
3314                 ORDER BY pricegroup|;
 
3317   $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
 
3319   $main::lxdebug->leave_sub();
 
3323 # usage $form->all_years($myconfig, [$dbh])
 
3324 # return list of all years where bookings found
 
3327   $main::lxdebug->enter_sub();
 
3329   my ($self, $myconfig, $dbh) = @_;
 
3331   $dbh ||= $self->get_standard_dbh($myconfig);
 
3334   my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
 
3335                    (SELECT MAX(transdate) FROM acc_trans)|;
 
3336   my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
 
3338   if ($myconfig->{dateformat} =~ /^yy/) {
 
3339     ($startdate) = split /\W/, $startdate;
 
3340     ($enddate) = split /\W/, $enddate;
 
3342     (@_) = split /\W/, $startdate;
 
3344     (@_) = split /\W/, $enddate;
 
3349   $startdate = substr($startdate,0,4);
 
3350   $enddate = substr($enddate,0,4);
 
3352   while ($enddate >= $startdate) {
 
3353     push @all_years, $enddate--;
 
3358   $main::lxdebug->leave_sub();
 
3362   $main::lxdebug->enter_sub();
 
3366   map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if exists $self->{$_} } @vars;
 
3368   $main::lxdebug->leave_sub();
 
3372   $main::lxdebug->enter_sub();
 
3377   map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if exists $self->{_VAR_BACKUP}->{$_} } @vars;
 
3379   $main::lxdebug->leave_sub();
 
3382 sub prepare_for_printing {
 
3385   $self->{templates} ||= $::myconfig{templates};
 
3386   $self->{formname}  ||= $self->{type};
 
3387   $self->{media}     ||= 'email';
 
3389   die "'media' other than 'email', 'file', 'printer' is not supported yet" unless $self->{media} =~ m/^(?:email|file|printer)$/;
 
3391   # set shipto from billto unless set
 
3392   my $has_shipto = any { $self->{"shipto$_"} } qw(name street zipcode city country contact);
 
3393   if (!$has_shipto && ($self->{type} =~ m/^(?:purchase_order|request_quotation)$/)) {
 
3394     $self->{shiptoname}   = $::myconfig{company};
 
3395     $self->{shiptostreet} = $::myconfig{address};
 
3398   my $language = $self->{language} ? '_' . $self->{language} : '';
 
3400   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
 
3401   if ($self->{language_id}) {
 
3402     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) = AM->get_language_details(\%::myconfig, $self, $self->{language_id});
 
3404     $output_dateformat   = $::myconfig{dateformat};
 
3405     $output_numberformat = $::myconfig{numberformat};
 
3406     $output_longdates    = 1;
 
3409   # Retrieve accounts for tax calculation.
 
3410   IC->retrieve_accounts(\%::myconfig, $self, map { $_ => $self->{"id_$_"} } 1 .. $self->{rowcount});
 
3412   if ($self->{type} =~ /_delivery_order$/) {
 
3413     DO->order_details(\%::myconfig, $self);
 
3414   } elsif ($self->{type} =~ /sales_order|sales_quotation|request_quotation|purchase_order/) {
 
3415     OE->order_details(\%::myconfig, $self);
 
3417     IS->invoice_details(\%::myconfig, $self, $::locale);
 
3420   # Chose extension & set source file name
 
3421   my $extension = 'html';
 
3422   if ($self->{format} eq 'postscript') {
 
3423     $self->{postscript}   = 1;
 
3425   } elsif ($self->{"format"} =~ /pdf/) {
 
3427     $extension            = $self->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
 
3428   } elsif ($self->{"format"} =~ /opendocument/) {
 
3429     $self->{opendocument} = 1;
 
3431   } elsif ($self->{"format"} =~ /excel/) {
 
3436   my $printer_code    = $self->{printer_code} ? '_' . $self->{printer_code} : '';
 
3437   my $email_extension = -f "$::myconfig{templates}/$self->{formname}_email${language}.${extension}" ? '_email' : '';
 
3438   $self->{IN}         = "$self->{formname}${email_extension}${language}${printer_code}.${extension}";
 
3441   $self->format_dates($output_dateformat, $output_longdates,
 
3442                       qw(invdate orddate quodate pldate duedate reqdate transdate shippingdate deliverydate validitydate paymentdate datepaid
 
3443                          transdate_oe deliverydate_oe employee_startdate employee_enddate),
 
3444                       grep({ /^(?:datepaid|transdate_oe|reqdate|deliverydate|deliverydate_oe|transdate)_\d+$/ } keys(%{$self})));
 
3446   $self->reformat_numbers($output_numberformat, 2,
 
3447                           qw(invtotal ordtotal quototal subtotal linetotal listprice sellprice netprice discount tax taxbase total paid),
 
3448                           grep({ /^(?:linetotal|listprice|sellprice|netprice|taxbase|discount|paid|subtotal|total|tax)_\d+$/ } keys(%{$self})));
 
3450   $self->reformat_numbers($output_numberformat, undef, qw(qty price_factor), grep({ /^qty_\d+$/} keys(%{$self})));
 
3452   my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
 
3454   if (scalar @{ $cvar_date_fields }) {
 
3455     $self->format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
 
3458   while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
 
3459     $self->reformat_numbers($output_numberformat, $precision, @{ $field_list });
 
3466   my ($self, $dateformat, $longformat, @indices) = @_;
 
3468   $dateformat ||= $::myconfig{dateformat};
 
3470   foreach my $idx (@indices) {
 
3471     if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
 
3472       for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
 
3473         $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $::locale->reformat_date(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i], $dateformat, $longformat);
 
3477     next unless defined $self->{$idx};
 
3479     if (!ref($self->{$idx})) {
 
3480       $self->{$idx} = $::locale->reformat_date(\%::myconfig, $self->{$idx}, $dateformat, $longformat);
 
3482     } elsif (ref($self->{$idx}) eq "ARRAY") {
 
3483       for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
 
3484         $self->{$idx}->[$i] = $::locale->reformat_date(\%::myconfig, $self->{$idx}->[$i], $dateformat, $longformat);
 
3490 sub reformat_numbers {
 
3491   my ($self, $numberformat, $places, @indices) = @_;
 
3493   return if !$numberformat || ($numberformat eq $::myconfig{numberformat});
 
3495   foreach my $idx (@indices) {
 
3496     if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
 
3497       for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
 
3498         $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $self->parse_amount(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
 
3502     next unless defined $self->{$idx};
 
3504     if (!ref($self->{$idx})) {
 
3505       $self->{$idx} = $self->parse_amount(\%::myconfig, $self->{$idx});
 
3507     } elsif (ref($self->{$idx}) eq "ARRAY") {
 
3508       for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
 
3509         $self->{$idx}->[$i] = $self->parse_amount(\%::myconfig, $self->{$idx}->[$i]);
 
3514   my $saved_numberformat    = $::myconfig{numberformat};
 
3515   $::myconfig{numberformat} = $numberformat;
 
3517   foreach my $idx (@indices) {
 
3518     if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
 
3519       for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
 
3520         $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $self->format_amount(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
 
3524     next unless defined $self->{$idx};
 
3526     if (!ref($self->{$idx})) {
 
3527       $self->{$idx} = $self->format_amount(\%::myconfig, $self->{$idx}, $places);
 
3529     } elsif (ref($self->{$idx}) eq "ARRAY") {
 
3530       for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
 
3531         $self->{$idx}->[$i] = $self->format_amount(\%::myconfig, $self->{$idx}->[$i], $places);
 
3536   $::myconfig{numberformat} = $saved_numberformat;
 
3541   $::lxdebug->enter_sub;
 
3543   my %style_to_script_map = (
 
3548   my $menu_script = $style_to_script_map{$::myconfig{menustyle}} || '';
 
3551   require "bin/mozilla/menu$menu_script.pl";
 
3553   require SL::Controller::FrameHeader;
 
3556   my $layout = SL::Controller::FrameHeader->new->action_header . ::render();
 
3558   $::lxdebug->leave_sub;
 
3568 SL::Form.pm - main data object.
 
3572 This is the main data object of kivitendo.
 
3573 Unfortunately it also acts as a god object for certain data retrieval procedures used in the entry points.
 
3574 Points of interest for a beginner are:
 
3576  - $form->error            - renders a generic error in html. accepts an error message
 
3577  - $form->get_standard_dbh - returns a database connection for the
 
3579 =head1 SPECIAL FUNCTIONS
 
3581 =head2 C<update_business> PARAMS
 
3584  \%config,     - config hashref
 
3585  $business_id, - business id
 
3586  $dbh          - optional database handle
 
3588 handles business (thats customer/vendor types) sequences.
 
3590 special behaviour for empty strings in customerinitnumber field:
 
3591 will in this case not increase the value, and return undef.
 
3593 =head2 C<redirect_header> $url
 
3595 Generates a HTTP redirection header for the new C<$url>. Constructs an
 
3596 absolute URL including scheme, host name and port. If C<$url> is a
 
3597 relative URL then it is considered relative to kivitendo base URL.
 
3599 This function C<die>s if headers have already been created with
 
3600 C<$::form-E<gt>header>.
 
3604   print $::form->redirect_header('oe.pl?action=edit&id=1234');
 
3605   print $::form->redirect_header('http://www.lx-office.org/');
 
3609 Generates a general purpose http/html header and includes most of the scripts
 
3610 and stylesheets needed. Stylesheets can be added with L<use_stylesheet>.
 
3612 Only one header will be generated. If the method was already called in this
 
3613 request it will not output anything and return undef. Also if no
 
3614 HTTP_USER_AGENT is found, no header is generated.
 
3616 Although header does not accept parameters itself, it will honor special
 
3617 hashkeys of its Form instance:
 
3625 If one of these is set, a http-equiv refresh is generated. Missing parameters
 
3626 default to 3 seconds and the refering url.
 
3630 Either a scalar or an array ref. Will be inlined into the header. Add
 
3631 stylesheets with the L<use_stylesheet> function.
 
3635 If true, a css snippet will be generated that sets the page in landscape mode.
 
3639 Used to override the default favicon.
 
3643 A html page title will be generated from this