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 #======================================================================
 
  49   $main::lxdebug->enter_sub(2);
 
  53   my @pairs = split(/&/, $input);
 
  56     my ($name, $value) = split(/=/, $_, 2);
 
  57     $in{$name} = unescape(undef, $value);
 
  60   $main::lxdebug->leave_sub(2);
 
  65 sub _request_to_hash {
 
  66   $main::lxdebug->enter_sub(2);
 
  69   my ($i,        $loc,  $key,    $val);
 
  70   my (%ATTACH,   $f,    $header, $header_body, $len, $buf);
 
  71   my ($boundary, @list, $size,   $body, $x, $blah, $name);
 
  73   if ($ENV{'CONTENT_TYPE'}
 
  74       && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
 
  75     $boundary = quotemeta('--' . $1);
 
  76     @list     = split(/$boundary/, $input);
 
  78     # For some reason there are always 2 extra, that are empty
 
  81     for ($x = 1; $x <= $size; $x++) {
 
  82       $header_body = $list[$x];
 
  83       $header_body =~ /\r\n\r\n|\n\n/;
 
  85       # Here we split the header and body
 
  90       # Now we try to get the file name
 
  92       $name =~ /name=\"(.+)\"/;
 
  93       ($name, $blah) = split(/\"/, $1);
 
  95       # If the form name is not attach, then we need to parse this like
 
  97       if ($name ne "attach") {
 
  98         $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
  99         $ATTACH{$name} = $body;
 
 101         # Otherwise it is an attachment and we need to finish it up
 
 102       } elsif ($name eq "attach") {
 
 103         $header =~ /filename=\"(.+)\"/;
 
 104         $ATTACH{'FILE_NAME'} = $1;
 
 105         $ATTACH{'FILE_NAME'} =~ s/\"//g;
 
 106         $ATTACH{'FILE_NAME'} =~ s/\s//g;
 
 107         $ATTACH{'FILE_CONTENT'} = $body;
 
 109         for ($i = $x; $list[$i]; $i++) {
 
 110           $list[$i] =~ s/^.+name=$//;
 
 111           $list[$i] =~ /\"(\w+)\"/;
 
 117     $main::lxdebug->leave_sub(2);
 
 121     $main::lxdebug->leave_sub(2);
 
 122     return _input_to_hash($input);
 
 127   $main::lxdebug->enter_sub();
 
 133   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 135   if ($ENV{QUERY_STRING}) {
 
 136     $_ = $ENV{QUERY_STRING};
 
 143   my %parameters = _request_to_hash($_);
 
 144   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 146   $self->{action} = lc $self->{action};
 
 147   $self->{action} =~ s/( |-|,|\#)/_/g;
 
 149   $self->{version}   = "2.3.9";
 
 151   $main::lxdebug->leave_sub();
 
 157   $main::lxdebug->enter_sub();
 
 163   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 165   $main::lxdebug->leave_sub();
 
 169   $main::lxdebug->enter_sub(2);
 
 171   my ($self, $str, $beenthere) = @_;
 
 173   # for Apache 2 we escape strings twice
 
 174   #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
 
 175   #  $str = $self->escape($str, 1);
 
 178   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 180   $main::lxdebug->leave_sub(2);
 
 186   $main::lxdebug->enter_sub(2);
 
 188   my ($self, $str) = @_;
 
 193   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 195   $main::lxdebug->leave_sub(2);
 
 201   my ($self, $str) = @_;
 
 203   if ($str && !ref($str)) {
 
 204     $str =~ s/\"/"/g;
 
 212   my ($self, $str) = @_;
 
 214   if ($str && !ref($str)) {
 
 215     $str =~ s/"/\"/g;
 
 223   $main::lxdebug->enter_sub(2);
 
 225   my ($self, $str) = @_;
 
 228     ('order' => ['"', '<', '>'],
 
 234   map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 236   $main::lxdebug->leave_sub(2);
 
 246       print qq|<input type=hidden name="$_" value="|
 
 247         . $self->quote($self->{$_})
 
 251     delete $self->{header};
 
 252     for (sort keys %$self) {
 
 253       print qq|<input type=hidden name="$_" value="|
 
 254         . $self->quote($self->{$_})
 
 262   $main::lxdebug->enter_sub();
 
 264   my ($self, $msg) = @_;
 
 265   if ($ENV{HTTP_USER_AGENT}) {
 
 267     $self->show_generic_error($msg);
 
 271     if ($self->{error_function}) {
 
 272       &{ $self->{error_function} }($msg);
 
 278   $main::lxdebug->leave_sub();
 
 282   $main::lxdebug->enter_sub();
 
 284   my ($self, $msg) = @_;
 
 286   if ($ENV{HTTP_USER_AGENT}) {
 
 289     if (!$self->{header}) {
 
 302     if ($self->{info_function}) {
 
 303       &{ $self->{info_function} }($msg);
 
 309   $main::lxdebug->leave_sub();
 
 313   $main::lxdebug->enter_sub();
 
 315   my ($self, $str, $cols, $maxrows) = @_;
 
 319   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 321   $maxrows = $rows unless defined $maxrows;
 
 323   $main::lxdebug->leave_sub();
 
 325   return ($rows > $maxrows) ? $maxrows : $rows;
 
 329   $main::lxdebug->enter_sub();
 
 331   my ($self, $msg) = @_;
 
 333   $self->error("$msg\n" . $DBI::errstr);
 
 335   $main::lxdebug->leave_sub();
 
 339   $main::lxdebug->enter_sub();
 
 341   my ($self, $name, $msg) = @_;
 
 343   if ($self->{$name} =~ /^\s*$/) {
 
 346   $main::lxdebug->leave_sub();
 
 350   $main::lxdebug->enter_sub();
 
 354   if ($self->{header}) {
 
 355     $main::lxdebug->leave_sub();
 
 359   my ($stylesheet, $favicon, $charset);
 
 361   if ($ENV{HTTP_USER_AGENT}) {
 
 363     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 365         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 369     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 371         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 375     if ($self->{charset}) {
 
 377         qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
 
 380     if ($self->{landscape}) {
 
 381       $pagelayout = qq|<style type="text/css">
 
 382                         \@page { size:landscape; }
 
 385     if ($self->{fokus}) {
 
 386       $fokus = qq|<script type="text/javascript">
 
 388 function fokus(){document.$self->{fokus}.focus();}
 
 395     if ($self->{jsscript} == 1) {
 
 398         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 399         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 400         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 401         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 408       ? "$self->{title} - $self->{titlebar}"
 
 411     foreach $item (@ { $self->{AJAX} }) {
 
 412       $ajax .= $item->show_javascript();
 
 414     print qq|Content-Type: text/html
 
 418   <title>$self->{titlebar}</title>
 
 426   <script type="text/javascript" src="js/highlight_input.js"></script>
 
 427   <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
 
 429   <script type="text/javascript" src="js/tabcontent.js">
 
 431   /***********************************************
 
 432   * Tab Content script- Â© Dynamic Drive DHTML code library (www.dynamicdrive.com)
 
 433   * This notice MUST stay intact for legal use
 
 434   * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 
 435   ***********************************************/
 
 445   $main::lxdebug->leave_sub();
 
 448 sub parse_html_template {
 
 449   $main::lxdebug->enter_sub();
 
 451   my ($self, $file, $additional_params) = @_;
 
 454   if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
 
 455     $language = $main::language;
 
 457     $language = $main::myconfig{"countrycode"};
 
 460   if (-f "templates/webpages/${file}_${language}.html") {
 
 461     if ((-f ".developer") &&
 
 462         (-f "templates/webpages/${file}_master.html") &&
 
 463         ((stat("templates/webpages/${file}_master.html"))[9] >
 
 464          (stat("templates/webpages/${file}_${language}.html"))[9])) {
 
 465       my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
 
 466         "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 467       print(qq|<pre>$info</pre>|);
 
 471     $file = "templates/webpages/${file}_${language}.html";
 
 472   } elsif (-f "templates/webpages/${file}.html") {
 
 473     $file = "templates/webpages/${file}.html";
 
 475     my $info = "Web page template '${file}' not found.\n" .
 
 476       "Please re-run 'locales.pl' in 'locale/${language}'.";
 
 477     print(qq|<pre>$info</pre>|);
 
 481   my $template = HTML::Template->new("filename" => $file,
 
 482                                      "die_on_bad_params" => 0,
 
 484                                      "case_sensitive" => 1,
 
 485                                      "loop_context_vars" => 1,
 
 488   $additional_params = {} unless ($additional_params);
 
 489   if ($self->{"DEBUG"}) {
 
 490     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 493   if ($additional_params->{"DEBUG"}) {
 
 494     $additional_params->{"DEBUG"} =
 
 495       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 498   if (%main::myconfig) {
 
 499     map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
 
 500     my $jsc_dateformat = $main::myconfig{"dateformat"};
 
 501     $jsc_dateformat =~ s/d+/\%d/gi;
 
 502     $jsc_dateformat =~ s/m+/\%m/gi;
 
 503     $jsc_dateformat =~ s/y+/\%Y/gi;
 
 504     $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
 
 507   $additional_params->{"conf_jscalendar"} = $main::jscalendar;
 
 508   $additional_params->{"conf_lizenzen"} = $main::lizenzen;
 
 509   $additional_params->{"conf_latex_templates"} = $main::latex;
 
 510   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
 512   my @additional_param_names = keys(%{$additional_params});
 
 513   foreach my $key ($template->param()) {
 
 514     my $param = $self->{$key};
 
 515     $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
 
 516     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
 
 517     $template->param($key => $param);
 
 520   my $output = $template->output();
 
 522   $main::lxdebug->leave_sub();
 
 527 sub show_generic_error {
 
 528   my ($self, $error, $title, $action) = @_;
 
 531   $add_params->{"title"} = $title if ($title);
 
 532   $self->{"label_error"} = $error;
 
 536     map({ delete($self->{$_}); } qw(action));
 
 537     map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
 
 538             if (!ref($self->{$_})); }
 
 540     $add_params->{"SHOW_BUTTON"} = 1;
 
 541     $add_params->{"BUTTON_LABEL"} = $action;
 
 543   $add_params->{"VARIABLES"} = \@vars;
 
 546   print($self->parse_html_template("generic/error", $add_params));
 
 548   die("Error: $error\n");
 
 551 sub show_generic_information {
 
 552   my ($self, $error, $title) = @_;
 
 555   $add_params->{"title"} = $title if ($title);
 
 556   $self->{"label_information"} = $error;
 
 559   print($self->parse_html_template("generic/information", $add_params));
 
 561   die("Information: $error\n");
 
 564 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 565 # changed it to accept an arbitrary number of triggers - sschoeling
 
 567   $main::lxdebug->enter_sub();
 
 570   my $myconfig = shift;
 
 573   # set dateform for jsscript
 
 575   $ifFormat = "%d.%m.%Y";
 
 576   if ($myconfig->{dateformat} eq "dd.mm.yy") {
 
 577     $ifFormat = "%d.%m.%Y";
 
 579     if ($myconfig->{dateformat} eq "dd-mm-yy") {
 
 580       $ifFormat = "%d-%m-%Y";
 
 582       if ($myconfig->{dateformat} eq "dd/mm/yy") {
 
 583         $ifFormat = "%d/%m/%Y";
 
 585         if ($myconfig->{dateformat} eq "mm/dd/yy") {
 
 586           $ifFormat = "%m/%d/%Y";
 
 588           if ($myconfig->{dateformat} eq "mm-dd-yy") {
 
 589             $ifFormat = "%m-%d-%Y";
 
 591             if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
 
 592               $ifFormat = "%Y-%m-%d";
 
 604       inputField : "| . (shift) . qq|",
 
 605       ifFormat :"$ifFormat",
 
 606       align : "| .  (shift) . qq|", 
 
 607       button : "| . (shift) . qq|"
 
 613        <script type="text/javascript">
 
 614        <!--| . join("", @triggers) . qq|//-->
 
 618   $main::lxdebug->leave_sub();
 
 621 }    #end sub write_trigger
 
 624   $main::lxdebug->enter_sub();
 
 626   my ($self, $msg) = @_;
 
 628   if ($self->{callback}) {
 
 630     ($script, $argv) = split(/\?/, $self->{callback});
 
 631     exec("perl", "$script", $argv);
 
 639   $main::lxdebug->leave_sub();
 
 642 # sort of columns removed - empty sub
 
 644   $main::lxdebug->enter_sub();
 
 646   my ($self, @columns) = @_;
 
 648   $main::lxdebug->leave_sub();
 
 654   $main::lxdebug->enter_sub(2);
 
 656   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 661   my $neg = ($amount =~ s/-//);
 
 663   $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
 
 665   my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
 
 666   my @p = split /\./, $amount ; # split amount at decimal point
 
 668   $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
 
 671   $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
 
 674     ($dash =~ /-/)    ? ($neg ? "($amount)"  : "$amount" )    :
 
 675     ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
 
 676                         ($neg ? "-$amount"   : "$amount" )    ;
 
 680   $main::lxdebug->leave_sub(2);
 
 685   $main::lxdebug->enter_sub(2);
 
 687   my ($self, $myconfig, $amount) = @_;
 
 689   if ($myconfig->{in_numberformat} == 1) {
 
 690     # Extra input number format 1000.00 or 1000,00
 
 692     $amount = scalar reverse $amount;
 
 693     $amount =~ s/\./DOT/;
 
 695     $amount =~ s/DOT/\./;
 
 696     $amount = scalar reverse $amount;
 
 697     $main::lxdebug->leave_sub(2);
 
 698     return ($amount * 1);
 
 701   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 702       || ($myconfig->{numberformat} eq '1000,00')) {
 
 707   if ($myconfig->{numberformat} eq "1'000.00") {
 
 713   $main::lxdebug->leave_sub(2);
 
 715   return ($amount * 1);
 
 719   $main::lxdebug->enter_sub(2);
 
 721   my ($self, $amount, $places) = @_;
 
 724   # Rounding like "Kaufmannsrunden"
 
 725   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 727   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 730   $amount = $amount * (10**($places));
 
 731   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 733   $main::lxdebug->leave_sub(2);
 
 735   return $round_amount;
 
 740   $main::lxdebug->enter_sub();
 
 742   my ($self, $myconfig, $userspath) = @_;
 
 745   $self->{"cwd"} = getcwd();
 
 746   $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
 
 748   if ($self->{"format"} =~ /(opendocument|oasis)/i) {
 
 749     $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 750   } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
 
 751     $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
 
 752     $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 753   } elsif (($self->{"format"} =~ /html/i) ||
 
 754            (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
 
 755     $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
 
 758   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 759   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 761   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 762       qw(email tel fax name signature company address businessnumber));
 
 764   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 766   # OUT is used for the media, screen, printer, email
 
 767   # for postscript we store a copy in a temporary file
 
 769   $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
 
 770   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 772     $self->{OUT} = ">$self->{tmpfile}";
 
 776     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 778     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 782   if (!$template->parse(*OUT)) {
 
 784     $self->error("$self->{IN} : " . $template->get_error());
 
 790   #print(STDERR Dumper($self));
 
 792   if ($template->uses_temp_file() || $self->{media} eq 'email') {
 
 794     if ($self->{media} eq 'email') {
 
 798       my $mail = new Mailer;
 
 800       map { $mail->{$_} = $self->{$_} }
 
 801         qw(cc bcc subject message version format charset);
 
 802       $mail->{to}     = qq|$self->{email}|;
 
 803       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
 804       $mail->{fileid} = "$fileid.";
 
 805       $myconfig->{signature} =~ s/\\r\\n/\\n/g;
 
 807       # if we send html or plain text inline
 
 808       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
 809         $mail->{contenttype} = "text/html";
 
 811         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
 812         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
 813         $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
 
 815         open(IN, $self->{tmpfile})
 
 816           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 818           $mail->{message} .= $_;
 
 825         @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
 
 827         $mail->{message}       =~ s/\r\n/\n/g;
 
 828         $myconfig->{signature} =~ s/\\n/\n/g;
 
 829         $mail->{message} .= "\n-- \n$myconfig->{signature}";
 
 833       my $err = $mail->send($out);
 
 834       $self->error($self->cleanup . "$err") if ($err);
 
 840       my $numbytes = (-s $self->{tmpfile});
 
 841       open(IN, $self->{tmpfile})
 
 842         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
 844       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
 846       chdir("$self->{cwd}");
 
 847       #print(STDERR "Kopien $self->{copies}\n");
 
 848       #print(STDERR "OUT $self->{OUT}\n");
 
 849       for my $i (1 .. $self->{copies}) {
 
 851           open(OUT, $self->{OUT})
 
 852             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
 856           print qq|Content-Type: | . $template->get_mime_type() . qq|
 
 857 Content-Disposition: attachment; filename="$self->{tmpfile}"
 
 858 Content-Length: $numbytes
 
 862           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
 882   chdir("$self->{cwd}");
 
 883   $main::lxdebug->leave_sub();
 
 887   $main::lxdebug->enter_sub();
 
 891   chdir("$self->{tmpdir}");
 
 894   if (-f "$self->{tmpfile}.err") {
 
 895     open(FH, "$self->{tmpfile}.err");
 
 900   if ($self->{tmpfile}) {
 
 901     $self->{tmpfile} =~ s|.*/||g;
 
 903     $self->{tmpfile} =~ s/\.\w+$//g;
 
 904     my $tmpfile = $self->{tmpfile};
 
 905     unlink(<$tmpfile.*>);
 
 908   chdir("$self->{cwd}");
 
 910   $main::lxdebug->leave_sub();
 
 916   $main::lxdebug->enter_sub();
 
 918   my ($self, $date, $myconfig) = @_;
 
 920   if ($date && $date =~ /\D/) {
 
 922     if ($myconfig->{dateformat} =~ /^yy/) {
 
 923       ($yy, $mm, $dd) = split /\D/, $date;
 
 925     if ($myconfig->{dateformat} =~ /^mm/) {
 
 926       ($mm, $dd, $yy) = split /\D/, $date;
 
 928     if ($myconfig->{dateformat} =~ /^dd/) {
 
 929       ($dd, $mm, $yy) = split /\D/, $date;
 
 934     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
 935     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
 937     $dd = "0$dd" if ($dd < 10);
 
 938     $mm = "0$mm" if ($mm < 10);
 
 943   $main::lxdebug->leave_sub();
 
 948 # Database routines used throughout
 
 951   $main::lxdebug->enter_sub();
 
 953   my ($self, $myconfig) = @_;
 
 955   # connect to database
 
 957     DBI->connect($myconfig->{dbconnect},
 
 958                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
 962   if ($myconfig->{dboptions}) {
 
 963     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
 966   $main::lxdebug->leave_sub();
 
 971 sub dbconnect_noauto {
 
 972   $main::lxdebug->enter_sub();
 
 974   my ($self, $myconfig) = @_;
 
 976   # connect to database
 
 978     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
 979                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
 983   if ($myconfig->{dboptions}) {
 
 984     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
 987   $main::lxdebug->leave_sub();
 
 993   $main::lxdebug->enter_sub();
 
 995   my ($self, $dbh, $table, $field, $where, $value) = @_;
 
 997   # if we have a value, go do it
 
1000     # retrieve balance from table
 
1001     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1002     my $sth   = $dbh->prepare($query);
 
1004     $sth->execute || $self->dberror($query);
 
1005     my ($balance) = $sth->fetchrow_array;
 
1011     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1012     $dbh->do($query) || $self->dberror($query);
 
1014   $main::lxdebug->leave_sub();
 
1017 sub update_exchangerate {
 
1018   $main::lxdebug->enter_sub();
 
1020   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1022   # some sanity check for currency
 
1024     $main::lxdebug->leave_sub();
 
1028   my $query = qq|SELECT e.curr FROM exchangerate e
 
1029                  WHERE e.curr = '$curr'
 
1030                  AND e.transdate = '$transdate'
 
1032   my $sth = $dbh->prepare($query);
 
1033   $sth->execute || $self->dberror($query);
 
1036   if ($buy != 0 && $sell != 0) {
 
1037     $set = "buy = $buy, sell = $sell";
 
1038   } elsif ($buy != 0) {
 
1039     $set = "buy = $buy";
 
1040   } elsif ($sell != 0) {
 
1041     $set = "sell = $sell";
 
1044   if ($sth->fetchrow_array) {
 
1045     $query = qq|UPDATE exchangerate
 
1047                 WHERE curr = '$curr'
 
1048                 AND transdate = '$transdate'|;
 
1050     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1051                 VALUES ('$curr', $buy, $sell, '$transdate')|;
 
1054   $dbh->do($query) || $self->dberror($query);
 
1056   $main::lxdebug->leave_sub();
 
1059 sub save_exchangerate {
 
1060   $main::lxdebug->enter_sub();
 
1062   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1064   my $dbh = $self->dbconnect($myconfig);
 
1066   my ($buy, $sell) = (0, 0);
 
1067   $buy  = $rate if $fld eq 'buy';
 
1068   $sell = $rate if $fld eq 'sell';
 
1070   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1074   $main::lxdebug->leave_sub();
 
1077 sub get_exchangerate {
 
1078   $main::lxdebug->enter_sub();
 
1080   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1082   unless ($transdate) {
 
1083     $main::lxdebug->leave_sub();
 
1087   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1088                  WHERE e.curr = '$curr'
 
1089                  AND e.transdate = '$transdate'|;
 
1090   my $sth = $dbh->prepare($query);
 
1091   $sth->execute || $self->dberror($query);
 
1093   my ($exchangerate) = $sth->fetchrow_array;
 
1096   if ($exchangerate == 0) {
 
1100   $main::lxdebug->leave_sub();
 
1102   return $exchangerate;
 
1105 sub set_payment_options {
 
1106   $main::lxdebug->enter_sub();
 
1108   my ($self, $myconfig, $transdate) = @_;
 
1110   if ($self->{payment_id}) {
 
1112     my $dbh = $self->dbconnect($myconfig);
 
1115     my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
 
1116                   WHERE p.id = $self->{payment_id}|;
 
1117     my $sth = $dbh->prepare($query);
 
1118     $sth->execute || $self->dberror($query);
 
1120     ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
 
1123     my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date  FROM payment_terms
 
1125     my $sth = $dbh->prepare($query);
 
1126     $sth->execute || $self->dberror($query);    
 
1127     ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
 
1130     $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
 
1132     $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
 
1133     $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
 
1134     $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
 
1139   $main::lxdebug->leave_sub();
 
1143 sub check_exchangerate {
 
1144   $main::lxdebug->enter_sub();
 
1146   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1148   unless ($transdate) {
 
1149     $main::lxdebug->leave_sub();
 
1153   my $dbh = $self->dbconnect($myconfig);
 
1155   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1156                  WHERE e.curr = '$currency'
 
1157                  AND e.transdate = '$transdate'|;
 
1158   my $sth = $dbh->prepare($query);
 
1159   $sth->execute || $self->dberror($query);
 
1161   my ($exchangerate) = $sth->fetchrow_array;
 
1165   $main::lxdebug->leave_sub();
 
1167   return $exchangerate;
 
1170 sub get_template_language {
 
1171   $main::lxdebug->enter_sub();
 
1173   my ($self, $myconfig) = @_;
 
1175   my $template_code = "";
 
1177   if ($self->{language_id}) {
 
1179     my $dbh = $self->dbconnect($myconfig);
 
1182     my $query = qq|SELECT l.template_code FROM language l
 
1183                   WHERE l.id = $self->{language_id}|;
 
1184     my $sth = $dbh->prepare($query);
 
1185     $sth->execute || $self->dberror($query);
 
1187     ($template_code) = $sth->fetchrow_array;
 
1192   $main::lxdebug->leave_sub();
 
1194   return $template_code;
 
1197 sub get_printer_code {
 
1198   $main::lxdebug->enter_sub();
 
1200   my ($self, $myconfig) = @_;
 
1202   my $template_code = "";
 
1204   if ($self->{printer_id}) {
 
1206     my $dbh = $self->dbconnect($myconfig);
 
1209     my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
 
1210                   WHERE p.id = $self->{printer_id}|;
 
1211     my $sth = $dbh->prepare($query);
 
1212     $sth->execute || $self->dberror($query);
 
1214     ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
 
1219   $main::lxdebug->leave_sub();
 
1221   return $template_code;
 
1225   $main::lxdebug->enter_sub();
 
1227   my ($self, $myconfig) = @_;
 
1229   my $template_code = "";
 
1231   if ($self->{shipto_id}) {
 
1233     my $dbh = $self->dbconnect($myconfig);
 
1236     my $query = qq|SELECT s.* FROM shipto s
 
1237                   WHERE s.shipto_id = $self->{shipto_id}|;
 
1238     my $sth = $dbh->prepare($query);
 
1239     $sth->execute || $self->dberror($query);
 
1240     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1241     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1246   $main::lxdebug->leave_sub();
 
1251   $main::lxdebug->enter_sub();
 
1253   my ($self, $dbh, $id, $module) = @_;
 
1257     qw(name department_1 department_2 street zipcode city country contact phone fax email)
 
1259     if ($self->{"shipto$item"}) {
 
1260       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1262     $self->{"shipto$item"} =~ s/\'/\'\'/g;
 
1265     if ($self->{shipto_id}) {
 
1266       my $query = qq| UPDATE shipto set
 
1267                       shiptoname = '$self->{shiptoname}',
 
1268                       shiptodepartment_1 = '$self->{shiptodepartment_1}',
 
1269                       shiptodepartment_2 = '$self->{shiptodepartment_2}',
 
1270                       shiptostreet = '$self->{shiptostreet}',
 
1271                       shiptozipcode = '$self->{shiptozipcode}',
 
1272                       shiptocity = '$self->{shiptocity}',
 
1273                       shiptocountry = '$self->{shiptocountry}',
 
1274                       shiptocontact = '$self->{shiptocontact}',
 
1275                       shiptophone = '$self->{shiptophone}',
 
1276                       shiptofax = '$self->{shiptofax}',
 
1277                       shiptoemail = '$self->{shiptoemail}'
 
1278                       WHERE shipto_id = $self->{shipto_id}|;
 
1279       $dbh->do($query) || $self->dberror($query);
 
1282       qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
 
1283                    shiptozipcode, shiptocity, shiptocountry, shiptocontact,
 
1284                    shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
 
1285                    '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
 
1286                    '$self->{shiptozipcode}', '$self->{shiptocity}',
 
1287                    '$self->{shiptocountry}', '$self->{shiptocontact}',
 
1288                    '$self->{shiptophone}', '$self->{shiptofax}',
 
1289                    '$self->{shiptoemail}', '$module')|;
 
1290       $dbh->do($query) || $self->dberror($query);
 
1294   $main::lxdebug->leave_sub();
 
1298   $main::lxdebug->enter_sub();
 
1300   my ($self, $dbh) = @_;
 
1302   my $query = qq|SELECT e.id, e.name FROM employee e
 
1303                  WHERE e.login = '$self->{login}'|;
 
1304   my $sth = $dbh->prepare($query);
 
1305   $sth->execute || $self->dberror($query);
 
1307   ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
 
1308   $self->{employee_id} *= 1;
 
1312   $main::lxdebug->leave_sub();
 
1315 # get other contact for transaction and form - html/tex
 
1317   $main::lxdebug->enter_sub();
 
1319   my ($self, $dbh, $id) = @_;
 
1321   my $query = qq|SELECT c.*
 
1324   $sth = $dbh->prepare($query);
 
1325   $sth->execute || $self->dberror($query);
 
1327   $ref = $sth->fetchrow_hashref(NAME_lc);
 
1329   push @{ $self->{$_} }, $ref;
 
1332   $main::lxdebug->leave_sub();
 
1335 # get contacts for id, if no contact return {"","","","",""}
 
1337   $main::lxdebug->enter_sub();
 
1339   my ($self, $dbh, $id) = @_;
 
1341   my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
 
1343               WHERE cp_cv_id=$id|;
 
1344   my $sth = $dbh->prepare($query);
 
1345   $sth->execute || $self->dberror($query);
 
1348   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1349     push @{ $self->{all_contacts} }, $ref;
 
1354     push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
 
1357   $main::lxdebug->leave_sub();
 
1360 # this sub gets the id and name from $table
 
1362   $main::lxdebug->enter_sub();
 
1364   my ($self, $myconfig, $table) = @_;
 
1366   # connect to database
 
1367   my $dbh = $self->dbconnect($myconfig);
 
1369   my $name           = $self->like(lc $self->{$table});
 
1370   my $customernumber = $self->like(lc $self->{customernumber});
 
1372   if ($self->{customernumber} ne "") {
 
1373     $query = qq~SELECT c.id, c.name,
 
1374                   c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1376                   WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
 
1379     $query = qq~SELECT c.id, c.name,
 
1380                  c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1382                  WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
 
1386   if ($self->{openinvoices}) {
 
1387     $query = qq~SELECT DISTINCT c.id, c.name,
 
1388                 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1389                 FROM $self->{arap} a
 
1390                 JOIN $table c ON (a.${table}_id = c.id)
 
1391                 WHERE NOT a.amount = a.paid
 
1392                 AND lower(c.name) LIKE '$name'
 
1395   my $sth = $dbh->prepare($query);
 
1397   $sth->execute || $self->dberror($query);
 
1400   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1401     push(@{ $self->{name_list} }, $ref);
 
1407   $main::lxdebug->leave_sub();
 
1412 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1415   $main::lxdebug->enter_sub();
 
1417   my ($self, $myconfig, $table, $module) = @_;
 
1420   my $dbh = $self->dbconnect($myconfig);
 
1422   my $query = qq|SELECT count(*) FROM $table|;
 
1423   my $sth   = $dbh->prepare($query);
 
1424   $sth->execute || $self->dberror($query);
 
1425   my ($count) = $sth->fetchrow_array;
 
1428   # build selection list
 
1429   if ($count < $myconfig->{vclimit}) {
 
1430     $query = qq|SELECT id, name
 
1431                 FROM $table WHERE not obsolete
 
1433     $sth = $dbh->prepare($query);
 
1434     $sth->execute || $self->dberror($query);
 
1436     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1437       push @{ $self->{"all_$table"} }, $ref;
 
1445   $self->get_employee($dbh);
 
1447   # setup sales contacts
 
1448   $query = qq|SELECT e.id, e.name
 
1451               AND NOT e.id = $self->{employee_id}|;
 
1452   $sth = $dbh->prepare($query);
 
1453   $sth->execute || $self->dberror($query);
 
1455   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1456     push @{ $self->{all_employees} }, $ref;
 
1461   push @{ $self->{all_employees} },
 
1462     { id   => $self->{employee_id},
 
1463       name => $self->{employee} };
 
1465   # sort the whole thing
 
1466   @{ $self->{all_employees} } =
 
1467     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
1469   if ($module eq 'AR') {
 
1471     # prepare query for departments
 
1472     $query = qq|SELECT d.id, d.description
 
1478     $query = qq|SELECT d.id, d.description
 
1483   $sth = $dbh->prepare($query);
 
1484   $sth->execute || $self->dberror($query);
 
1486   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1487     push @{ $self->{all_departments} }, $ref;
 
1492   $query = qq|SELECT id, description
 
1495   $sth = $dbh->prepare($query);
 
1496   $sth->execute || $form->dberror($query);
 
1498   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1499     push @{ $self->{languages} }, $ref;
 
1504   $query = qq|SELECT printer_description, id
 
1507   $sth = $dbh->prepare($query);
 
1508   $sth->execute || $form->dberror($query);
 
1510   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1511     push @{ $self->{printers} }, $ref;
 
1517   $query = qq|SELECT id, description
 
1520   $sth = $dbh->prepare($query);
 
1521   $sth->execute || $form->dberror($query);
 
1523   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1524     push @{ $self->{payment_terms} }, $ref;
 
1528   $main::lxdebug->leave_sub();
 
1532 sub language_payment {
 
1533   $main::lxdebug->enter_sub();
 
1535   my ($self, $myconfig) = @_;
 
1536   undef $self->{languages};
 
1537   undef $self->{payment_terms};
 
1538   undef $self->{printers};
 
1541   my $dbh = $self->dbconnect($myconfig);
 
1543   my $query = qq|SELECT id, description
 
1546   my $sth = $dbh->prepare($query);
 
1547   $sth->execute || $form->dberror($query);
 
1549   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1550     push @{ $self->{languages} }, $ref;
 
1555   $query = qq|SELECT printer_description, id
 
1558   $sth = $dbh->prepare($query);
 
1559   $sth->execute || $form->dberror($query);
 
1561   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1562     push @{ $self->{printers} }, $ref;
 
1567   $query = qq|SELECT id, description
 
1570   $sth = $dbh->prepare($query);
 
1571   $sth->execute || $form->dberror($query);
 
1573   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1574     push @{ $self->{payment_terms} }, $ref;
 
1578   # get buchungsgruppen
 
1579   $query = qq|SELECT id, description
 
1580               FROM buchungsgruppen|;
 
1581   $sth = $dbh->prepare($query);
 
1582   $sth->execute || $form->dberror($query);
 
1584   $self->{BUCHUNGSGRUPPEN} = [];
 
1585   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1586     push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
 
1591   $main::lxdebug->leave_sub();
 
1594 # this is only used for reports
 
1595 sub all_departments {
 
1596   $main::lxdebug->enter_sub();
 
1598   my ($self, $myconfig, $table) = @_;
 
1600   my $dbh   = $self->dbconnect($myconfig);
 
1601   my $where = "1 = 1";
 
1603   if (defined $table) {
 
1604     if ($table eq 'customer') {
 
1605       $where = " d.role = 'P'";
 
1609   my $query = qq|SELECT d.id, d.description
 
1613   my $sth = $dbh->prepare($query);
 
1614   $sth->execute || $self->dberror($query);
 
1616   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1617     push @{ $self->{all_departments} }, $ref;
 
1623   $main::lxdebug->leave_sub();
 
1627   $main::lxdebug->enter_sub();
 
1629   my ($self, $module, $myconfig, $table) = @_;
 
1631   $self->all_vc($myconfig, $table, $module);
 
1633   # get last customers or vendors
 
1636   my $dbh = $self->dbconnect($myconfig);
 
1640   # now get the account numbers
 
1641   $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
 
1643               WHERE c.link LIKE '%$module%'
 
1646   $sth = $dbh->prepare($query);
 
1647   $sth->execute || $self->dberror($query);
 
1649   $self->{accounts} = "";
 
1650   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1652     foreach my $key (split /:/, $ref->{link}) {
 
1653       if ($key =~ /$module/) {
 
1655         # cross reference for keys
 
1656         $xkeyref{ $ref->{accno} } = $key;
 
1658         push @{ $self->{"${module}_links"}{$key} },
 
1659           { accno       => $ref->{accno},
 
1660             description => $ref->{description},
 
1661             taxkey      => $ref->{taxkey_id} };
 
1663         $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
1669   # get taxkeys and description
 
1670   $query = qq|SELECT taxkey, taxdescription
 
1672   $sth = $dbh->prepare($query);
 
1673   $sth->execute || $self->dberror($query);
 
1675   $ref = $sth->fetchrow_hashref(NAME_lc);
 
1677   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1678     push @{ $self->{TAXKEY} }, $ref;
 
1685   $query = qq|SELECT id, description
 
1687   $sth = $dbh->prepare($query);
 
1688   $sth->execute || $form->dberror($query);
 
1691   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1692     push @{ $self->{TAXZONE} }, $ref;
 
1696   if (($module eq "AP") || ($module eq "AR")) {
 
1698     # get tax rates and description
 
1699     $query = qq| SELECT * FROM tax t|;
 
1700     $sth   = $dbh->prepare($query);
 
1701     $sth->execute || $self->dberror($query);
 
1703     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1704       push @{ $self->{TAX} }, $ref;
 
1710     my $arap = ($table eq 'customer') ? 'ar' : 'ap';
 
1712     $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
 
1713                 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
 
1714                 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
 
1715                 c.name AS $table, a.department_id, d.description AS department,
 
1716                 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
 
1717                 a.employee_id, e.name AS employee, a.gldate, a.type
 
1719                 JOIN $table c ON (a.${table}_id = c.id)
 
1720                 LEFT JOIN employee e ON (e.id = a.employee_id)
 
1721                 LEFT JOIN department d ON (d.id = a.department_id)
 
1722                 WHERE a.id = $self->{id}|;
 
1723     $sth = $dbh->prepare($query);
 
1724     $sth->execute || $self->dberror($query);
 
1726     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1727     foreach $key (keys %$ref) {
 
1728       $self->{$key} = $ref->{$key};
 
1732     # get amounts from individual entries
 
1733     $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
 
1734                 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
 
1736                 JOIN chart c ON (c.id = a.chart_id)
 
1737                 LEFT JOIN project p ON (p.id = a.project_id)
 
1738                 LEFT Join tax t ON (a.taxkey = t.taxkey)
 
1739                 WHERE a.trans_id = $self->{id}
 
1740                 AND a.fx_transaction = '0'
 
1741                 ORDER BY a.oid,a.transdate|;
 
1742     $sth = $dbh->prepare($query);
 
1743     $sth->execute || $self->dberror($query);
 
1745     my $fld = ($table eq 'customer') ? 'buy' : 'sell';
 
1747     # get exchangerate for currency
 
1748     $self->{exchangerate} =
 
1749       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
 
1753     # store amounts in {acc_trans}{$key} for multiple accounts
 
1754     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1755       $ref->{exchangerate} =
 
1756         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
 
1758       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
1761       if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
 
1762         $ref->{amount} *= -1;
 
1764       $ref->{index} = $index;
 
1766       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
1770     $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
 
1771                   (SELECT c.accno FROM chart c
 
1772                    WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1773                   (SELECT c.accno FROM chart c
 
1774                    WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
1776     $sth = $dbh->prepare($query);
 
1777     $sth->execute || $self->dberror($query);
 
1779     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1780     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
1786     $query = qq|SELECT current_date AS transdate,
 
1787                 d.curr AS currencies, d.closedto, d.revtrans,
 
1788                   (SELECT c.accno FROM chart c
 
1789                    WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1790                   (SELECT c.accno FROM chart c
 
1791                    WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
1793     $sth = $dbh->prepare($query);
 
1794     $sth->execute || $self->dberror($query);
 
1796     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1797     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
1800     if ($self->{"$self->{vc}_id"}) {
 
1802       # only setup currency
 
1803       ($self->{currency}) = split /:/, $self->{currencies};
 
1807       $self->lastname_used($dbh, $myconfig, $table, $module);
 
1809       my $fld = ($table eq 'customer') ? 'buy' : 'sell';
 
1811       # get exchangerate for currency
 
1812       $self->{exchangerate} =
 
1813         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
 
1822   $main::lxdebug->leave_sub();
 
1826   $main::lxdebug->enter_sub();
 
1828   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
1830   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
1831   my $where = "1 = 1";
 
1833   if ($self->{type} =~ /_order/) {
 
1835     $where = "quotation = '0'";
 
1837   if ($self->{type} =~ /_quotation/) {
 
1839     $where = "quotation = '1'";
 
1842   my $query = qq|SELECT MAX(id) FROM $arap
 
1844                               AND ${table}_id > 0|;
 
1845   my $sth = $dbh->prepare($query);
 
1846   $sth->execute || $self->dberror($query);
 
1848   my ($trans_id) = $sth->fetchrow_array;
 
1852   $query = qq|SELECT ct.name, a.curr, a.${table}_id,
 
1853               current_date + ct.terms AS duedate, a.department_id,
 
1854               d.description AS department
 
1856               JOIN $table ct ON (a.${table}_id = ct.id)
 
1857               LEFT JOIN department d ON (a.department_id = d.id)
 
1858               WHERE a.id = $trans_id|;
 
1859   $sth = $dbh->prepare($query);
 
1860   $sth->execute || $self->dberror($query);
 
1862   ($self->{$table},  $self->{currency},      $self->{"${table}_id"},
 
1863    $self->{duedate}, $self->{department_id}, $self->{department})
 
1864     = $sth->fetchrow_array;
 
1867   $main::lxdebug->leave_sub();
 
1871   $main::lxdebug->enter_sub();
 
1873   my ($self, $myconfig, $thisdate, $days) = @_;
 
1875   my $dbh = $self->dbconnect($myconfig);
 
1880     my $dateformat = $myconfig->{dateformat};
 
1881     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
1883     $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
 
1885     $sth = $dbh->prepare($query);
 
1886     $sth->execute || $self->dberror($query);
 
1888     $query = qq|SELECT current_date AS thisdate
 
1890     $sth = $dbh->prepare($query);
 
1891     $sth->execute || $self->dberror($query);
 
1894   ($thisdate) = $sth->fetchrow_array;
 
1899   $main::lxdebug->leave_sub();
 
1905   $main::lxdebug->enter_sub();
 
1907   my ($self, $string) = @_;
 
1909   if ($string !~ /%/) {
 
1910     $string = "%$string%";
 
1913   $string =~ s/\'/\'\'/g;
 
1915   $main::lxdebug->leave_sub();
 
1921   $main::lxdebug->enter_sub();
 
1923   my ($self, $flds, $new, $count, $numrows) = @_;
 
1927   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
1933   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
1935     $j = $item->{ndx} - 1;
 
1936     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
1940   for $i ($count + 1 .. $numrows) {
 
1941     map { delete $self->{"${_}_$i"} } @{$flds};
 
1944   $main::lxdebug->leave_sub();
 
1948   $main::lxdebug->enter_sub();
 
1950   my ($self, $myconfig) = @_;
 
1954   my $dbh = $self->dbconnect_noauto($myconfig);
 
1956   my $query = qq|DELETE FROM status
 
1957                  WHERE formname = '$self->{formname}'
 
1959   my $sth = $dbh->prepare($query) || $self->dberror($query);
 
1961   if ($self->{formname} =~ /(check|receipt)/) {
 
1962     for $i (1 .. $self->{rowcount}) {
 
1963       $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
 
1967     $sth->execute($self->{id}) || $self->dberror($query);
 
1971   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
1972   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
1974   my %queued = split / /, $self->{queued};
 
1976   if ($self->{formname} =~ /(check|receipt)/) {
 
1978     # this is a check or receipt, add one entry for each lineitem
 
1979     my ($accno) = split /--/, $self->{account};
 
1980     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
 
1981                 chart_id) VALUES (?, '$printed',
 
1982                 '$queued{$self->{formname}}', '$self->{prinform}',
 
1983                 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
 
1984     $sth = $dbh->prepare($query) || $self->dberror($query);
 
1986     for $i (1 .. $self->{rowcount}) {
 
1987       if ($self->{"checked_$i"}) {
 
1988         $sth->execute($self->{"id_$i"}) || $self->dberror($query);
 
1993     $query = qq|INSERT INTO status (trans_id, printed, emailed,
 
1994                 spoolfile, formname)
 
1995                 VALUES ($self->{id}, '$printed', '$emailed',
 
1996                 '$queued{$self->{formname}}', '$self->{formname}')|;
 
1997     $dbh->do($query) || $self->dberror($query);
 
2003   $main::lxdebug->leave_sub();
 
2007   $main::lxdebug->enter_sub();
 
2009   my ($self, $dbh) = @_;
 
2011   my ($query, $printed, $emailed);
 
2013   my $formnames  = $self->{printed};
 
2014   my $emailforms = $self->{emailed};
 
2016   my $query = qq|DELETE FROM status
 
2017                  WHERE formname = '$self->{formname}'
 
2018                  AND trans_id = $self->{id}|;
 
2019   $dbh->do($query) || $self->dberror($query);
 
2021   # this only applies to the forms
 
2022   # checks and receipts are posted when printed or queued
 
2024   if ($self->{queued}) {
 
2025     my %queued = split / /, $self->{queued};
 
2027     foreach my $formname (keys %queued) {
 
2028       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2029       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2031       $query = qq|INSERT INTO status (trans_id, printed, emailed,
 
2032                   spoolfile, formname)
 
2033                   VALUES ($self->{id}, '$printed', '$emailed',
 
2034                   '$queued{$formname}', '$formname')|;
 
2035       $dbh->do($query) || $self->dberror($query);
 
2037       $formnames  =~ s/$self->{formname}//;
 
2038       $emailforms =~ s/$self->{formname}//;
 
2043   # save printed, emailed info
 
2044   $formnames  =~ s/^ +//g;
 
2045   $emailforms =~ s/^ +//g;
 
2048   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2049   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2051   foreach my $formname (keys %status) {
 
2052     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2053     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2055     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2056                 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
 
2057     $dbh->do($query) || $self->dberror($query);
 
2060   $main::lxdebug->leave_sub();
 
2063 sub update_defaults {
 
2064   $main::lxdebug->enter_sub();
 
2066   my ($self, $myconfig, $fld) = @_;
 
2068   my $dbh   = $self->dbconnect_noauto($myconfig);
 
2069   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2070   my $sth   = $dbh->prepare($query);
 
2072   $sth->execute || $self->dberror($query);
 
2073   my ($var) = $sth->fetchrow_array;
 
2078   $query = qq|UPDATE defaults
 
2080   $dbh->do($query) || $self->dberror($query);
 
2085   $main::lxdebug->leave_sub();
 
2090 sub update_business {
 
2091   $main::lxdebug->enter_sub();
 
2093   my ($self, $myconfig, $business_id) = @_;
 
2095   my $dbh   = $self->dbconnect_noauto($myconfig);
 
2097     qq|SELECT customernumberinit FROM business  WHERE id=$business_id FOR UPDATE|;
 
2098   my $sth = $dbh->prepare($query);
 
2100   $sth->execute || $self->dberror($query);
 
2101   my ($var) = $sth->fetchrow_array;
 
2106   $query = qq|UPDATE business
 
2107               SET customernumberinit = '$var' WHERE id=$business_id|;
 
2108   $dbh->do($query) || $self->dberror($query);
 
2113   $main::lxdebug->leave_sub();
 
2119   $main::lxdebug->enter_sub();
 
2121   my ($self, $myconfig, $salesman) = @_;
 
2123   my $dbh   = $self->dbconnect($myconfig);
 
2125     qq|SELECT id, name FROM customer  WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
 
2126   my $sth = $dbh->prepare($query);
 
2127   $sth->execute || $self->dberror($query);
 
2130   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2131     push(@{ $self->{salesman_list} }, $ref);
 
2135   $main::lxdebug->leave_sub();
 
2140 sub get_partsgroup {
 
2141   $main::lxdebug->enter_sub();
 
2143   my ($self, $myconfig, $p) = @_;
 
2145   my $dbh = $self->dbconnect($myconfig);
 
2147   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2149                  JOIN parts p ON (p.partsgroup_id = pg.id)|;
 
2151   if ($p->{searchitems} eq 'part') {
 
2153                  WHERE p.inventory_accno_id > 0|;
 
2155   if ($p->{searchitems} eq 'service') {
 
2157                  WHERE p.inventory_accno_id IS NULL|;
 
2159   if ($p->{searchitems} eq 'assembly') {
 
2161                  WHERE p.assembly = '1'|;
 
2163   if ($p->{searchitems} eq 'labor') {
 
2165                  WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
 
2169                  ORDER BY partsgroup|;
 
2172     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2173                 ORDER BY partsgroup|;
 
2176   if ($p->{language_code}) {
 
2177     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2178                 t.description AS translation
 
2180                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2181                 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
 
2182                 ORDER BY translation|;
 
2185   my $sth = $dbh->prepare($query);
 
2186   $sth->execute || $self->dberror($query);
 
2188   $self->{all_partsgroup} = ();
 
2189   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2190     push @{ $self->{all_partsgroup} }, $ref;
 
2194   $main::lxdebug->leave_sub();
 
2197 sub get_pricegroup {
 
2198   $main::lxdebug->enter_sub();
 
2200   my ($self, $myconfig, $p) = @_;
 
2202   my $dbh = $self->dbconnect($myconfig);
 
2204   my $query = qq|SELECT p.id, p.pricegroup
 
2208                  ORDER BY pricegroup|;
 
2211     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2212                 ORDER BY pricegroup|;
 
2215   my $sth = $dbh->prepare($query);
 
2216   $sth->execute || $self->dberror($query);
 
2218   $self->{all_pricegroup} = ();
 
2219   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2220     push @{ $self->{all_pricegroup} }, $ref;
 
2225   $main::lxdebug->leave_sub();
 
2229   my ($self, $dbh, $myconfig, $audittrail) = @_;
 
2231   # table, $reference, $formname, $action, $id, $transdate) = @_;
 
2238     $dbh        = $self->dbconnect($myconfig);
 
2242   # if we have an id add audittrail, otherwise get a new timestamp
 
2244   if ($audittrail->{id}) {
 
2246     $query = qq|SELECT audittrail FROM defaults|;
 
2248     if ($dbh->selectrow_array($query)) {
 
2249       my ($null, $employee_id) = $self->get_employee($dbh);
 
2251       if ($self->{audittrail} && !$myconfig) {
 
2252         chop $self->{audittrail};
 
2254         my @a = split /\|/, $self->{audittrail};
 
2258         my @flds = qw(tablename reference formname action transdate);
 
2260         # put into hash and remove dups
 
2262           $key = "$a[2]$a[3]";
 
2264           $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
 
2268         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2269                     formname, action, employee_id, transdate)
 
2270                     VALUES ($audittrail->{id}, ?, ?,
 
2271                     ?, ?, $employee_id, ?)|;
 
2272         my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2276             $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
 
2280           for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
 
2282           $sth->execute || $self->dberror;
 
2287       if ($audittrail->{transdate}) {
 
2288         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2289                     formname, action, employee_id, transdate) VALUES (
 
2290                     $audittrail->{id}, '$audittrail->{tablename}', |
 
2291           . $dbh->quote($audittrail->{reference}) . qq|,
 
2292                     '$audittrail->{formname}', '$audittrail->{action}',
 
2293                     $employee_id, '$audittrail->{transdate}')|;
 
2295         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2296                     formname, action, employee_id) VALUES ($audittrail->{id},
 
2297                     '$audittrail->{tablename}', |
 
2298           . $dbh->quote($audittrail->{reference}) . qq|,
 
2299                     '$audittrail->{formname}', '$audittrail->{action}',
 
2306     $query = qq|SELECT current_timestamp FROM defaults|;
 
2307     my ($timestamp) = $dbh->selectrow_array($query);
 
2310       "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
 
2313   $dbh->disconnect if $disconnect;
 
2322   $main::lxdebug->enter_sub();
 
2324   my ($type, $country, $NLS_file) = @_;
 
2327   if ($country && -d "locale/$country") {
 
2329     $self->{countrycode} = $country;
 
2330     if (open(IN, "locale/$country/$NLS_file")) {
 
2331       my $code = join("", <IN>);
 
2337   $self->{NLS_file} = $NLS_file;
 
2339   push @{ $self->{LONG_MONTH} },
 
2340     ("January",   "February", "March",    "April",
 
2341      "May ",      "June",     "July",     "August",
 
2342      "September", "October",  "November", "December");
 
2343   push @{ $self->{SHORT_MONTH} },
 
2344     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
 
2346   $main::lxdebug->leave_sub();
 
2352   my ($self, $text) = @_;
 
2354   return (exists $self->{texts}{$text}) ? $self->{texts}{$text} : $text;
 
2358   $main::lxdebug->enter_sub();
 
2360   my ($self, $text) = @_;
 
2362   if (exists $self->{subs}{$text}) {
 
2363     $text = $self->{subs}{$text};
 
2365     if ($self->{countrycode} && $self->{NLS_file}) {
 
2367          "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
 
2371   $main::lxdebug->leave_sub();
 
2377   $main::lxdebug->enter_sub();
 
2379   my ($self, $myconfig, $date, $longformat) = @_;
 
2382   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
 
2387     $spc = $myconfig->{dateformat};
 
2389     $spc = substr($spc, 1, 1);
 
2391     if ($date =~ /\D/) {
 
2392       if ($myconfig->{dateformat} =~ /^yy/) {
 
2393         ($yy, $mm, $dd) = split /\D/, $date;
 
2395       if ($myconfig->{dateformat} =~ /^mm/) {
 
2396         ($mm, $dd, $yy) = split /\D/, $date;
 
2398       if ($myconfig->{dateformat} =~ /^dd/) {
 
2399         ($dd, $mm, $yy) = split /\D/, $date;
 
2402       $date = substr($date, 2);
 
2403       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
 
2408     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
2409     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
2411     if ($myconfig->{dateformat} =~ /^dd/) {
 
2412       if (defined $longformat && $longformat == 0) {
 
2414         $dd = "0$dd" if ($dd < 10);
 
2415         $mm = "0$mm" if ($mm < 10);
 
2416         $longdate = "$dd$spc$mm$spc$yy";
 
2419         $longdate .= ($spc eq '.') ? ". " : " ";
 
2420         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
 
2422     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
 
2424       # Use German syntax with the ISO date style "yyyy-mm-dd" because
 
2425       # Lx-Office is mainly used in Germany or German speaking countries.
 
2426       if (defined $longformat && $longformat == 0) {
 
2428         $dd = "0$dd" if ($dd < 10);
 
2429         $mm = "0$mm" if ($mm < 10);
 
2430         $longdate = "$yy-$mm-$dd";
 
2432         $longdate = "$dd. ";
 
2433         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
 
2436       if (defined $longformat && $longformat == 0) {
 
2438         $dd = "0$dd" if ($dd < 10);
 
2439         $mm = "0$mm" if ($mm < 10);
 
2440         $longdate = "$mm$spc$dd$spc$yy";
 
2442         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
 
2448   $main::lxdebug->leave_sub();
 
2454   $main::lxdebug->enter_sub();
 
2456   my ($self, $myconfig, $date, $longformat) = @_;
 
2459     $main::lxdebug->leave_sub();
 
2464   $spc = $myconfig->{dateformat};
 
2466   $spc = substr($spc, 1, 1);
 
2468   if ($date =~ /\D/) {
 
2469     if ($myconfig->{dateformat} =~ /^yy/) {
 
2470       ($yy, $mm, $dd) = split /\D/, $date;
 
2471     } elsif ($myconfig->{dateformat} =~ /^mm/) {
 
2472       ($mm, $dd, $yy) = split /\D/, $date;
 
2473     } elsif ($myconfig->{dateformat} =~ /^dd/) {
 
2474       ($dd, $mm, $yy) = split /\D/, $date;
 
2477     $date = substr($date, 2);
 
2478     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
 
2483   $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
2484   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
2486   $main::lxdebug->leave_sub();
 
2487   return ($yy, $mm, $dd);