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 #======================================================================
 
  43   $main::lxdebug->enter_sub();
 
  47   my @pairs = split(/&/, $input);
 
  50     my ($name, $value) = split(/=/, $_, 2);
 
  51     $in{$name} = unescape(undef, $value);
 
  54   $main::lxdebug->leave_sub();
 
  59 sub _request_to_hash {
 
  60   $main::lxdebug->enter_sub();
 
  63   my ($i,        $loc,  $key,    $val);
 
  64   my (%ATTACH,   $f,    $header, $header_body, $len, $buf);
 
  65   my ($boundary, @list, $size,   $body, $x, $blah, $name);
 
  67   if ($ENV{'CONTENT_TYPE'}
 
  68       && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
 
  69     $boundary = quotemeta('--' . $1);
 
  70     @list     = split(/$boundary/, $input);
 
  72     # For some reason there are always 2 extra, that are empty
 
  75     for ($x = 1; $x <= $size; $x++) {
 
  76       $header_body = $list[$x];
 
  77       $header_body =~ /\r\n\r\n|\n\n/;
 
  79       # Here we split the header and body
 
  84       # Now we try to get the file name
 
  86       $name =~ /name=\"(.+)\"/;
 
  87       ($name, $blah) = split(/\"/, $1);
 
  89       # If the form name is not attach, then we need to parse this like
 
  91       if ($name ne "attach") {
 
  92         $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
  93         $ATTACH{$name} = $body;
 
  95         # Otherwise it is an attachment and we need to finish it up
 
  96       } elsif ($name eq "attach") {
 
  97         $header =~ /filename=\"(.+)\"/;
 
  98         $ATTACH{'FILE_NAME'} = $1;
 
  99         $ATTACH{'FILE_NAME'} =~ s/\"//g;
 
 100         $ATTACH{'FILE_NAME'} =~ s/\s//g;
 
 101         $ATTACH{'FILE_CONTENT'} = $body;
 
 103         for ($i = $x; $list[$i]; $i++) {
 
 104           $list[$i] =~ s/^.+name=$//;
 
 105           $list[$i] =~ /\"(\w+)\"/;
 
 111     $main::lxdebug->leave_sub();
 
 115     $main::lxdebug->leave_sub();
 
 116     return _input_to_hash($input);
 
 121   $main::lxdebug->enter_sub();
 
 127   read(STDIN, $_, $ENV{CONTENT_LENGTH});
 
 129   if ($ENV{QUERY_STRING}) {
 
 130     $_ = $ENV{QUERY_STRING};
 
 137   my %parameters = _request_to_hash($_);
 
 138   map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
 
 140   $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
 
 142   $self->{action} = lc $self->{action};
 
 143   $self->{action} =~ s/( |-|,|#)/_/g;
 
 145   $self->{version}   = "2.2.0";
 
 146   $self->{dbversion} = "2.2.0";
 
 148   $main::lxdebug->leave_sub();
 
 154   $main::lxdebug->enter_sub();
 
 160   map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
 
 162   $main::lxdebug->leave_sub();
 
 166   $main::lxdebug->enter_sub();
 
 168   my ($self, $str, $beenthere) = @_;
 
 170   # for Apache 2 we escape strings twice
 
 171   #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
 
 172   #  $str = $self->escape($str, 1);
 
 175   $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
 
 177   $main::lxdebug->leave_sub();
 
 183   $main::lxdebug->enter_sub();
 
 185   my ($self, $str) = @_;
 
 190   $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
 
 192   $main::lxdebug->leave_sub();
 
 198   my ($self, $str) = @_;
 
 200   if ($str && !ref($str)) {
 
 201     $str =~ s/"/"/g;
 
 209   my ($self, $str) = @_;
 
 211   if ($str && !ref($str)) {
 
 212     $str =~ s/"/"/g;
 
 224       print qq|<input type=hidden name="$_" value="|
 
 225         . $self->quote($self->{$_})
 
 229     delete $self->{header};
 
 230     for (sort keys %$self) {
 
 231       print qq|<input type=hidden name="$_" value="|
 
 232         . $self->quote($self->{$_})
 
 240   $main::lxdebug->enter_sub();
 
 242   my ($self, $msg) = @_;
 
 244   if ($ENV{HTTP_USER_AGENT}) {
 
 252     <h2 class=error>Error!</h2>
 
 264     if ($self->{error_function}) {
 
 265       &{ $self->{error_function} }($msg);
 
 271   $main::lxdebug->leave_sub();
 
 275   $main::lxdebug->enter_sub();
 
 277   my ($self, $msg) = @_;
 
 279   if ($ENV{HTTP_USER_AGENT}) {
 
 282     if (!$self->{header}) {
 
 295     if ($self->{info_function}) {
 
 296       &{ $self->{info_function} }($msg);
 
 302   $main::lxdebug->leave_sub();
 
 306   $main::lxdebug->enter_sub();
 
 308   my ($self, $str, $cols, $maxrows) = @_;
 
 312   map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
 
 314   $maxrows = $rows unless defined $maxrows;
 
 316   $main::lxdebug->leave_sub();
 
 318   return ($rows > $maxrows) ? $maxrows : $rows;
 
 322   $main::lxdebug->enter_sub();
 
 324   my ($self, $msg) = @_;
 
 326   $self->error("$msg\n" . $DBI::errstr);
 
 328   $main::lxdebug->leave_sub();
 
 332   $main::lxdebug->enter_sub();
 
 334   my ($self, $name, $msg) = @_;
 
 336   if ($self->{$name} =~ /^\s*$/) {
 
 339   $main::lxdebug->leave_sub();
 
 343   $main::lxdebug->enter_sub();
 
 347   if ($self->{header}) {
 
 348     $main::lxdebug->leave_sub();
 
 352   my ($stylesheet, $favicon, $charset);
 
 354   if ($ENV{HTTP_USER_AGENT}) {
 
 356     if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
 
 358         qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
 
 362     if ($self->{favicon} && (-f "$self->{favicon}")) {
 
 364         qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
 
 368     if ($self->{charset}) {
 
 370         qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
 
 373     if ($self->{landscape}) {
 
 374       $pagelayout = qq|<style type="text/css">
 
 375                         \@page { size:landscape; }
 
 378     if ($self->{fokus}) {
 
 379       $fokus = qq|<script type="text/javascript">
 
 381 function fokus(){document.$self->{fokus}.focus();}
 
 388     if ($self->{jsscript} == 1) {
 
 391         <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
 
 392         <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
 
 393         <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
 
 394         <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
 
 401       ? "$self->{title} - $self->{titlebar}"
 
 404     print qq|Content-Type: text/html
 
 408   <title>$self->{titlebar}</title>
 
 421   $main::lxdebug->leave_sub();
 
 425 sub parse_html_template {
 
 426   $main::lxdebug->enter_sub();
 
 428   my ($self, $file, $additional_params) = @_;
 
 430   if (-f "templates/webpages/${file}_" . $main::myconfig{"countrycode"} .
 
 432     $file = "templates/webpages/${file}_" . $main::myconfig{"countrycode"} .
 
 434   } elsif (-f "templates/webpages/${file}.html") {
 
 435     $file = "templates/webpages/${file}.html";
 
 437     $self->error("Web page template '${file}' not found.");
 
 440   my $template = HTML::Template->new("filename" => $file,
 
 441                                      "die_on_bad_params" => 0,
 
 443                                      "case_sensitive" => 1,
 
 444                                      "loop_context_vars" => 1,
 
 447   $additional_params = {} unless ($additional_params);
 
 448   if ($self->{"DEBUG"}) {
 
 449     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
 
 452   if ($additional_params->{"DEBUG"}) {
 
 453     $additional_params->{"DEBUG"} =
 
 454       "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
 
 457   my @additional_param_names = keys(%{$additional_params});
 
 459   foreach my $key ($template->param()) {
 
 460     if (grep(/^${key}$/, @additional_param_names)) {
 
 461       $template->param($key => $additional_params->{$key});
 
 463       $template->param($key => $self->{$key});
 
 467   my $output = $template->output();
 
 469   $main::lxdebug->leave_sub();
 
 474 sub show_generic_error {
 
 475   my ($self, $error, $title) = @_;
 
 478   $add_params->{"title"} = $title if ($title);
 
 479   $self->{"label_error"} = $error;
 
 481   print($self->parse_html_template("generic/error", $add_params));
 
 484 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
 
 485 # changed it to accept an arbitrary number of triggers - sschoeling
 
 487   $main::lxdebug->enter_sub();
 
 490   my $myconfig = shift;
 
 493   # set dateform for jsscript
 
 495   $ifFormat = "%d.%m.%Y";
 
 496   if ($myconfig->{dateformat} eq "dd.mm.yy") {
 
 497     $ifFormat = "%d.%m.%Y";
 
 499     if ($myconfig->{dateformat} eq "dd-mm-yy") {
 
 500       $ifFormat = "%d-%m-%Y";
 
 502       if ($myconfig->{dateformat} eq "dd/mm/yy") {
 
 503         $ifFormat = "%d/%m/%Y";
 
 505         if ($myconfig->{dateformat} eq "mm/dd/yy") {
 
 506           $ifFormat = "%m/%d/%Y";
 
 508           if ($myconfig->{dateformat} eq "mm-dd-yy") {
 
 509             $ifFormat = "%m-%d-%Y";
 
 511             if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
 
 512               $ifFormat = "%Y-%m-%d";
 
 524       inputField : "| . (shift) . qq|",
 
 525       ifFormat :"$ifFormat",
 
 526       align : "| .  (shift) . qq|", 
 
 527       button : "| . (shift) . qq|"
 
 533        <script type="text/javascript">
 
 534        <!--| . join("", @triggers) . qq|//-->
 
 538   $main::lxdebug->leave_sub();
 
 541 }    #end sub write_trigger
 
 544   $main::lxdebug->enter_sub();
 
 546   my ($self, $msg) = @_;
 
 548   if ($self->{callback}) {
 
 550     ($script, $argv) = split(/\?/, $self->{callback});
 
 551     exec("perl", "$script", $argv);
 
 559   $main::lxdebug->leave_sub();
 
 562 # sort of columns removed - empty sub
 
 564   $main::lxdebug->enter_sub();
 
 566   my ($self, @columns) = @_;
 
 568   $main::lxdebug->leave_sub();
 
 574   $main::lxdebug->enter_sub();
 
 576   my ($self, $myconfig, $amount, $places, $dash) = @_;
 
 578   #Workaround for $format_amount calls without $places
 
 579   if (!defined $places) {
 
 580     (my $dec) = ($amount =~ /\.(\d+)/);
 
 581     $places = length $dec;
 
 584   if ($places =~ /\d/) {
 
 585     $amount = $self->round_amount($amount, $places);
 
 588   # is the amount negative
 
 589   my $negative = ($amount < 0);
 
 593     if ($myconfig->{numberformat} && ($myconfig->{numberformat} ne '1000.00'))
 
 595       my ($whole, $dec) = split /\./, "$amount";
 
 597       $amount = join '', reverse split //, $whole;
 
 598       $fillup = "0" x ($places - length($dec));
 
 600       if ($myconfig->{numberformat} eq '1,000.00') {
 
 601         $amount =~ s/\d{3,}?/$&,/g;
 
 603         $amount = join '', reverse split //, $amount;
 
 604         $amount .= "\.$dec" . $fillup if ($places ne '' && $places * 1 != 0);
 
 607       if ($myconfig->{numberformat} eq '1.000,00') {
 
 608         $amount =~ s/\d{3,}?/$&./g;
 
 610         $amount = join '', reverse split //, $amount;
 
 611         $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
 
 614       if ($myconfig->{numberformat} eq '1000,00') {
 
 616         $amount .= ",$dec" . $fillup if ($places ne '' && $places * 1 != 0);
 
 620         $amount = ($negative) ? "($amount)" : "$amount";
 
 621       } elsif ($dash =~ /DRCR/) {
 
 622         $amount = ($negative) ? "$amount DR" : "$amount CR";
 
 624         $amount = ($negative) ? "-$amount" : "$amount";
 
 628     if ($dash eq "0" && $places) {
 
 629       if ($myconfig->{numberformat} eq '1.000,00') {
 
 630         $amount = "0" . "," . "0" x $places;
 
 632         $amount = "0" . "." . "0" x $places;
 
 635       $amount = ($dash ne "") ? "$dash" : "0";
 
 639   $main::lxdebug->leave_sub();
 
 645   $main::lxdebug->enter_sub();
 
 647   my ($self, $myconfig, $amount) = @_;
 
 648   $main::lxdebug->message(LXDebug::DEBUG2, "Start amount: $amount");
 
 650   if ($myconfig->{in_numberformat} == 1) {
 
 652     # Extra input number format 1000.00 or 1000,00
 
 653     $main::lxdebug->message(LXDebug::DEBUG2,
 
 654               "in_numberformat: " . $main::locale->text('1000,00 or 1000.00'));
 
 657     #$main::lxdebug->message(LXDebug::DEBUG2, "1.Parsed Number: $amount") if ($amount);
 
 658     $amount = scalar reverse $amount;
 
 660     #$main::lxdebug->message(LXDebug::DEBUG2, "2.Parsed Number: $amount") if ($amount);
 
 661     $amount =~ s/\./DOT/;
 
 663     #$main::lxdebug->message(LXDebug::DEBUG2, "3.Parsed Number: $amount") if ($amount);
 
 666     #$main::lxdebug->message(LXDebug::DEBUG2, "4.Parsed Number: $amount") if ($amount);
 
 667     $amount =~ s/DOT/\./;
 
 669     #$main::lxdebug->message(LXDebug::DEBUG2, "5.Parsed Number:" . $amount) if ($amount);
 
 670     $amount = scalar reverse $amount;
 
 671     $main::lxdebug->message(LXDebug::DEBUG2,
 
 672                             "Parsed amount:" . $amount . "\n");
 
 674     return ($amount * 1);
 
 677   $main::lxdebug->message(LXDebug::DEBUG2,
 
 678               "in_numberformat: " . $main::locale->text('equal Outputformat'));
 
 679   $main::lxdebug->message(LXDebug::DEBUG2,
 
 680                           " = numberformat: $myconfig->{numberformat}");
 
 681   if (   ($myconfig->{numberformat} eq '1.000,00')
 
 682       || ($myconfig->{numberformat} eq '1000,00')) {
 
 687   if ($myconfig->{numberformat} eq "1'000.00") {
 
 693   $main::lxdebug->message(LXDebug::DEBUG2, "Parsed amount:" . $amount . "\n")
 
 695   $main::lxdebug->leave_sub();
 
 697   return ($amount * 1);
 
 701   $main::lxdebug->enter_sub();
 
 703   my ($self, $amount, $places) = @_;
 
 706   # Rounding like "Kaufmannsrunden"
 
 707   # Descr. http://de.wikipedia.org/wiki/Rundung
 
 709   # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
 712   $amount = $amount * (10**($places));
 
 713   $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
 
 715   $main::lxdebug->leave_sub();
 
 717   return $round_amount;
 
 722   $main::lxdebug->enter_sub();
 
 724   my ($self, $myconfig, $userspath) = @_;
 
 727   # Some variables used for page breaks
 
 728   my ($chars_per_line, $lines_on_first_page, $lines_on_second_page) =
 
 730   my ($current_page, $current_line, $current_row) = (1, 1, 0);
 
 736   # Make sure that all *notes* (intnotes, partnotes_*, notes etc) are converted to markup correctly.
 
 737   $self->format_string(grep(/notes/, keys(%{$self})));
 
 739   # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
 
 740   $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
 
 742   map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
 
 743       qw(email tel fax name signature));
 
 745   open(IN, "$self->{templates}/$self->{IN}")
 
 746     or $self->error("$self->{IN} : $!");
 
 751   $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
 
 753   # OUT is used for the media, screen, printer, email
 
 754   # for postscript we store a copy in a temporary file
 
 756   $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
 
 757   if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
 
 759     $self->{OUT} = ">$self->{tmpfile}";
 
 763     open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
 
 765     open(OUT, ">-") or $self->error("STDOUT : $!");
 
 769   # Do we have to run LaTeX two times? This is needed if
 
 770   # the template contains page references.
 
 773   # first we generate a tmpfile
 
 774   # read file and replace <%variable%>
 
 780     $two_passes = 1 if (/\\pageref/);
 
 783     # detect pagebreak block and its parameters
 
 784     if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
 
 785       $chars_per_line       = $1;
 
 786       $lines_on_first_page  = $2;
 
 787       $lines_on_second_page = $3;
 
 790         last if (/\s*<%end pagebreak%>/);
 
 797     if (/\s*<%foreach /) {
 
 799       # this one we need for the count
 
 801       $var =~ s/\s*<%foreach (.+?)%>/$1/;
 
 803         last if (/\s*<%end /);
 
 809       # display contents of $self->{number}[] array
 
 810       for $i (0 .. $#{ $self->{$var} }) {
 
 813         # Try to detect whether a manual page break is necessary
 
 814         # but only if there was a <%pagebreak ...%> block before
 
 816         if ($chars_per_line) {
 
 818             int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
 
 821           my $_description = $self->{"description"}[$i];
 
 822           while ($_description =~ /\\newline/) {
 
 824             $_description =~ s/\\newline//;
 
 826           $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
 
 828           if ($current_page == 1) {
 
 829             $lpp = $lines_on_first_page;
 
 831             $lpp = $lines_on_second_page;
 
 834           # Yes we need a manual page break -- or the user has forced one
 
 836              (($current_line + $lines) > $lpp)
 
 837              || ($self->{"_forced_pagebreaks"}
 
 838                && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
 
 842             # replace the special variables <%sumcarriedforward%>
 
 845             my $psum = $self->format_amount($myconfig, $sum, 2);
 
 846             $pb =~ s/<%sumcarriedforward%>/$psum/g;
 
 847             $pb =~ s/<%lastpage%>/$current_page/g;
 
 849             # only "normal" variables are supported here
 
 850             # (no <%if, no <%foreach, no <%include)
 
 852             $pb =~ s/<%(.+?)%>/$self->{$1}/g;
 
 854             # page break block is ready to rock
 
 859           $current_line += $lines;
 
 862         $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
 
 866         # don't parse par, we need it for each line
 
 868         s/<%(.+?)%>/$self->{$1}[$i]/mg;
 
 874     # if not comes before if!
 
 875     if (/\s*<%if not /) {
 
 877       # check if it is not set and display
 
 879       s/\s*<%if not (.+?)%>/$1/;
 
 881       unless ($self->{$_}) {
 
 883           last if (/\s*<%end /);
 
 893           last if (/\s*<%end /);
 
 901       # check if it is set and display
 
 903       s/\s*<%if (.+?)%>/$1/;
 
 907           last if (/\s*<%end /);
 
 917           last if (/\s*<%end /);
 
 923     # check for <%include filename%>
 
 924     if (/\s*<%include /) {
 
 928       $var =~ s/\s*<%include (.+?)%>/$1/;
 
 931       $var =~ s/(\/|\.\.)//g;
 
 933       # prevent the infinite loop!
 
 934       next if ($self->{"$var"});
 
 936       open(INC, "$self->{templates}/$var")
 
 937         or $self->error($self->cleanup . "$self->{templates}/$var : $!");
 
 946     s/<%(.+?)%>/$self->{$1}/g;
 
 947     s/<nobr><\/nobr>/ /g;
 
 954   # Convert the tex file to postscript
 
 955   if ($self->{format} =~ /(postscript|pdf)/) {
 
 958     $self->{cwd}    = cwd();
 
 959     $self->{tmpdir} = "$self->{cwd}/$userspath";
 
 961     chdir("$userspath") or $self->error($self->cleanup . "chdir : $!");
 
 963     $self->{tmpfile} =~ s/$userspath\///g;
 
 965     if ($self->{format} eq 'postscript') {
 
 967         "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
 
 969       $self->error($self->cleanup) if ($?);
 
 972           "latex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
 
 974         $self->error($self->cleanup) if ($?);
 
 977       $self->{tmpfile} =~ s/tex$/dvi/;
 
 979       system("dvips $self->{tmpfile} -o -q > /dev/null");
 
 980       $self->error($self->cleanup . "dvips : $!") if ($?);
 
 981       $self->{tmpfile} =~ s/dvi$/ps/;
 
 983     if ($self->{format} eq 'pdf') {
 
 985         "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
 
 987       $self->error($self->cleanup) if ($?);
 
 990           "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{tmpfile}.err"
 
 992         $self->error($self->cleanup) if ($?);
 
 994       $self->{tmpfile} =~ s/tex$/pdf/;
 
 999   if ($self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email') {
 
1001     if ($self->{media} eq 'email') {
 
1005       my $mail = new Mailer;
 
1007       map { $mail->{$_} = $self->{$_} }
 
1008         qw(cc bcc subject message version format charset);
 
1009       $mail->{to}     = qq|$self->{email}|;
 
1010       $mail->{from}   = qq|"$myconfig->{name}" <$myconfig->{email}>|;
 
1011       $mail->{fileid} = "$fileid.";
 
1013       # if we send html or plain text inline
 
1014       if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
 
1015         $mail->{contenttype} = "text/html";
 
1017         $mail->{message}       =~ s/\r\n/<br>\n/g;
 
1018         $myconfig->{signature} =~ s/\\n/<br>\n/g;
 
1019         $mail->{message} .= "<br>\n--<br>\n$myconfig->{signature}\n<br>";
 
1021         open(IN, $self->{tmpfile})
 
1022           or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1024           $mail->{message} .= $_;
 
1031         @{ $mail->{attachments} } = ($self->{tmpfile});
 
1033         $myconfig->{signature} =~ s/\\n/\r\n/g;
 
1034         $mail->{message} .= "\r\n--\r\n$myconfig->{signature}";
 
1038       my $err = $mail->send($out);
 
1039       $self->error($self->cleanup . "$err") if ($err);
 
1043       $self->{OUT} = $out;
 
1045       my $numbytes = (-s $self->{tmpfile});
 
1046       open(IN, $self->{tmpfile})
 
1047         or $self->error($self->cleanup . "$self->{tmpfile} : $!");
 
1049       $self->{copies} = 1 unless $self->{media} eq 'printer';
 
1051       chdir("$self->{cwd}");
 
1053       for my $i (1 .. $self->{copies}) {
 
1055           open(OUT, $self->{OUT})
 
1056             or $self->error($self->cleanup . "$self->{OUT} : $!");
 
1059           # launch application
 
1060           print qq|Content-Type: application/$self->{format}
 
1061 Content-Disposition: attachment; filename="$self->{tmpfile}"
 
1062 Content-Length: $numbytes
 
1066           open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
 
1086   chdir("$self->{cwd}");
 
1087   $main::lxdebug->leave_sub();
 
1091   $main::lxdebug->enter_sub();
 
1095   chdir("$self->{tmpdir}");
 
1098   if (-f "$self->{tmpfile}.err") {
 
1099     open(FH, "$self->{tmpfile}.err");
 
1104   if ($self->{tmpfile}) {
 
1107     $self->{tmpfile} =~ s/\.\w+$//g;
 
1108     my $tmpfile = $self->{tmpfile};
 
1109     unlink(<$tmpfile.*>);
 
1112   chdir("$self->{cwd}");
 
1114   $main::lxdebug->leave_sub();
 
1120   $main::lxdebug->enter_sub();
 
1122   my ($self, @fields) = @_;
 
1125   %unique_fields = map({ $_ => 1 } @fields);
 
1126   @fields        = keys(%unique_fields);
 
1128   foreach my $field (@fields) {
 
1129     next unless ($self->{$field} =~ /\<pagebreak\>/);
 
1130     $self->{$field} =~ s/\<pagebreak\>//g;
 
1131     if ($field =~ /.*_(\d+)$/) {
 
1132       if (!$self->{"_forced_pagebreaks"}) {
 
1133         $self->{"_forced_pagebreaks"} = [];
 
1135       push(@{ $self->{"_forced_pagebreaks"} }, "$1");
 
1139   my $format = $self->{format};
 
1140   if ($self->{format} =~ /(postscript|pdf)/) {
 
1147         '<', '>', quotemeta('\n'), '
 
1151         '&', quotemeta('\n'), '
 
1153         '"', '\$', '%', '_', '#', quotemeta('^'),
 
1154         '{', '}',  '<', '>', '£', "\r"
 
1160       quotemeta('\n') => '<br>',
 
1171       quotemeta('^')  => '\^\\',
 
1176       quotemeta('\n') => '\newline ',
 
1183   foreach my $key (@{ $replace{order}{$format} }) {
 
1184     map { $self->{$_} =~ s/$key/$replace{$format}{$key}/g; } @fields;
 
1187   # Allow some HTML markup to be converted into the output format's
 
1188   # corresponding markup code, e.g. bold or italic.
 
1189   if ('html' eq $format) {
 
1190     my @markup_replace = ('b', 'i', 's', 'u');
 
1192     foreach my $key (@markup_replace) {
 
1193       map({ $self->{$_} =~ s/\<(\/?)${key}\>/<$1${key}>/g } @fields);
 
1196   } elsif ('tex' eq $format) {
 
1197     my %markup_replace = ('b' => 'textbf',
 
1199                           'u' => 'underline');
 
1201     foreach my $field (@fields) {
 
1202       foreach my $key (keys(%markup_replace)) {
 
1203         my $new = $markup_replace{$key};
 
1205           s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
1210   $main::lxdebug->leave_sub();
 
1214   $main::lxdebug->enter_sub();
 
1216   my ($self, $date, $myconfig) = @_;
 
1218   if ($date && $date =~ /\D/) {
 
1220     if ($myconfig->{dateformat} =~ /^yy/) {
 
1221       ($yy, $mm, $dd) = split /\D/, $date;
 
1223     if ($myconfig->{dateformat} =~ /^mm/) {
 
1224       ($mm, $dd, $yy) = split /\D/, $date;
 
1226     if ($myconfig->{dateformat} =~ /^dd/) {
 
1227       ($dd, $mm, $yy) = split /\D/, $date;
 
1232     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
1233     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
1235     $dd = "0$dd" if ($dd < 10);
 
1236     $mm = "0$mm" if ($mm < 10);
 
1238     $date = "$yy$mm$dd";
 
1241   $main::lxdebug->leave_sub();
 
1246 # Database routines used throughout
 
1249   $main::lxdebug->enter_sub();
 
1251   my ($self, $myconfig) = @_;
 
1253   # connect to database
 
1255     DBI->connect($myconfig->{dbconnect},
 
1256                  $myconfig->{dbuser}, $myconfig->{dbpasswd})
 
1260   if ($myconfig->{dboptions}) {
 
1261     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1264   $main::lxdebug->leave_sub();
 
1269 sub dbconnect_noauto {
 
1270   $main::lxdebug->enter_sub();
 
1272   my ($self, $myconfig) = @_;
 
1274   # connect to database
 
1276     DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
 
1277                  $myconfig->{dbpasswd}, { AutoCommit => 0 })
 
1281   if ($myconfig->{dboptions}) {
 
1282     $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
 
1285   $main::lxdebug->leave_sub();
 
1290 sub update_balance {
 
1291   $main::lxdebug->enter_sub();
 
1293   my ($self, $dbh, $table, $field, $where, $value) = @_;
 
1295   # if we have a value, go do it
 
1298     # retrieve balance from table
 
1299     my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
 
1300     my $sth   = $dbh->prepare($query);
 
1302     $sth->execute || $self->dberror($query);
 
1303     my ($balance) = $sth->fetchrow_array;
 
1309     $query = "UPDATE $table SET $field = $balance WHERE $where";
 
1310     $dbh->do($query) || $self->dberror($query);
 
1312   $main::lxdebug->leave_sub();
 
1315 sub update_exchangerate {
 
1316   $main::lxdebug->enter_sub();
 
1318   my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
 
1320   # some sanity check for currency
 
1322     $main::lxdebug->leave_sub();
 
1326   my $query = qq|SELECT e.curr FROM exchangerate e
 
1327                  WHERE e.curr = '$curr'
 
1328                  AND e.transdate = '$transdate'
 
1330   my $sth = $dbh->prepare($query);
 
1331   $sth->execute || $self->dberror($query);
 
1334   if ($buy != 0 && $sell != 0) {
 
1335     $set = "buy = $buy, sell = $sell";
 
1336   } elsif ($buy != 0) {
 
1337     $set = "buy = $buy";
 
1338   } elsif ($sell != 0) {
 
1339     $set = "sell = $sell";
 
1342   if ($sth->fetchrow_array) {
 
1343     $query = qq|UPDATE exchangerate
 
1345                 WHERE curr = '$curr'
 
1346                 AND transdate = '$transdate'|;
 
1348     $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
 
1349                 VALUES ('$curr', $buy, $sell, '$transdate')|;
 
1352   $dbh->do($query) || $self->dberror($query);
 
1354   $main::lxdebug->leave_sub();
 
1357 sub save_exchangerate {
 
1358   $main::lxdebug->enter_sub();
 
1360   my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
 
1362   my $dbh = $self->dbconnect($myconfig);
 
1364   my ($buy, $sell) = (0, 0);
 
1365   $buy  = $rate if $fld eq 'buy';
 
1366   $sell = $rate if $fld eq 'sell';
 
1368   $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
 
1372   $main::lxdebug->leave_sub();
 
1375 sub get_exchangerate {
 
1376   $main::lxdebug->enter_sub();
 
1378   my ($self, $dbh, $curr, $transdate, $fld) = @_;
 
1380   unless ($transdate) {
 
1381     $main::lxdebug->leave_sub();
 
1385   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1386                  WHERE e.curr = '$curr'
 
1387                  AND e.transdate = '$transdate'|;
 
1388   my $sth = $dbh->prepare($query);
 
1389   $sth->execute || $self->dberror($query);
 
1391   my ($exchangerate) = $sth->fetchrow_array;
 
1394   $main::lxdebug->leave_sub();
 
1396   return $exchangerate;
 
1399 sub check_exchangerate {
 
1400   $main::lxdebug->enter_sub();
 
1402   my ($self, $myconfig, $currency, $transdate, $fld) = @_;
 
1404   unless ($transdate) {
 
1405     $main::lxdebug->leave_sub();
 
1409   my $dbh = $self->dbconnect($myconfig);
 
1411   my $query = qq|SELECT e.$fld FROM exchangerate e
 
1412                  WHERE e.curr = '$currency'
 
1413                  AND e.transdate = '$transdate'|;
 
1414   my $sth = $dbh->prepare($query);
 
1415   $sth->execute || $self->dberror($query);
 
1417   my ($exchangerate) = $sth->fetchrow_array;
 
1421   $main::lxdebug->leave_sub();
 
1423   return $exchangerate;
 
1427   $main::lxdebug->enter_sub();
 
1429   my ($self, $dbh, $id) = @_;
 
1433     qw(name department_1 department_2 street zipcode city country contact phone fax email)
 
1435     if ($self->{"shipto$item"}) {
 
1436       $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
 
1438     $self->{"shipto$item"} =~ s/\'/\'\'/g;
 
1443       qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
 
1444                    shiptozipcode, shiptocity, shiptocountry, shiptocontact,
 
1445                    shiptophone, shiptofax, shiptoemail) VALUES ($id,
 
1446                    '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
 
1447                    '$self->{shiptozipcode}', '$self->{shiptocity}',
 
1448                    '$self->{shiptocountry}', '$self->{shiptocontact}',
 
1449                    '$self->{shiptophone}', '$self->{shiptofax}',
 
1450                    '$self->{shiptoemail}')|;
 
1451     $dbh->do($query) || $self->dberror($query);
 
1454   $main::lxdebug->leave_sub();
 
1458   $main::lxdebug->enter_sub();
 
1460   my ($self, $dbh) = @_;
 
1462   my $query = qq|SELECT e.id, e.name FROM employee e
 
1463                  WHERE e.login = '$self->{login}'|;
 
1464   my $sth = $dbh->prepare($query);
 
1465   $sth->execute || $self->dberror($query);
 
1467   ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
 
1468   $self->{employee_id} *= 1;
 
1472   $main::lxdebug->leave_sub();
 
1475 # get other contact for transaction and form - html/tex
 
1477   $main::lxdebug->enter_sub();
 
1479   my ($self, $dbh, $id) = @_;
 
1481   my $query = qq|SELECT c.*
 
1484   $sth = $dbh->prepare($query);
 
1485   $sth->execute || $self->dberror($query);
 
1487   $ref = $sth->fetchrow_hashref(NAME_lc);
 
1489   push @{ $self->{$_} }, $ref;
 
1492   $main::lxdebug->leave_sub();
 
1495 # get contacts for id, if no contact return {"","","","",""}
 
1497   $main::lxdebug->enter_sub();
 
1499   my ($self, $dbh, $id) = @_;
 
1501   my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname
 
1503               WHERE cp_cv_id=$id|;
 
1504   my $sth = $dbh->prepare($query);
 
1505   $sth->execute || $self->dberror($query);
 
1508   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1509     push @{ $self->{all_contacts} }, $ref;
 
1514     push @{ $self->{all_contacts} }, { { "", "", "", "", "" } };
 
1517   $main::lxdebug->leave_sub();
 
1520 # this sub gets the id and name from $table
 
1522   $main::lxdebug->enter_sub();
 
1524   my ($self, $myconfig, $table) = @_;
 
1526   # connect to database
 
1527   my $dbh = $self->dbconnect($myconfig);
 
1529   my $name           = $self->like(lc $self->{$table});
 
1530   my $customernumber = $self->like(lc $self->{customernumber});
 
1532   if ($self->{customernumber} ne "") {
 
1533     $query = qq~SELECT c.id, c.name,
 
1534                   c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1536                   WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
 
1539     $query = qq~SELECT c.id, c.name,
 
1540                  c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1542                  WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
 
1546   if ($self->{openinvoices}) {
 
1547     $query = qq~SELECT DISTINCT c.id, c.name,
 
1548                 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
 
1549                 FROM $self->{arap} a
 
1550                 JOIN $table c ON (a.${table}_id = c.id)
 
1551                 WHERE NOT a.amount = a.paid
 
1552                 AND lower(c.name) LIKE '$name'
 
1555   my $sth = $dbh->prepare($query);
 
1557   $sth->execute || $self->dberror($query);
 
1560   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1561     push(@{ $self->{name_list} }, $ref);
 
1567   $main::lxdebug->leave_sub();
 
1572 # the selection sub is used in the AR, AP, IS, IR and OE module
 
1575   $main::lxdebug->enter_sub();
 
1577   my ($self, $myconfig, $table, $module) = @_;
 
1580   my $dbh = $self->dbconnect($myconfig);
 
1582   my $query = qq|SELECT count(*) FROM $table|;
 
1583   my $sth   = $dbh->prepare($query);
 
1584   $sth->execute || $self->dberror($query);
 
1585   my ($count) = $sth->fetchrow_array;
 
1588   # build selection list
 
1589   if ($count < $myconfig->{vclimit}) {
 
1590     $query = qq|SELECT id, name
 
1591                 FROM $table WHERE not obsolete
 
1593     $sth = $dbh->prepare($query);
 
1594     $sth->execute || $self->dberror($query);
 
1596     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1597       push @{ $self->{"all_$table"} }, $ref;
 
1605   $self->get_employee($dbh);
 
1607   # setup sales contacts
 
1608   $query = qq|SELECT e.id, e.name
 
1611               AND NOT e.id = $self->{employee_id}|;
 
1612   $sth = $dbh->prepare($query);
 
1613   $sth->execute || $self->dberror($query);
 
1615   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1616     push @{ $self->{all_employees} }, $ref;
 
1621   push @{ $self->{all_employees} },
 
1622     { id   => $self->{employee_id},
 
1623       name => $self->{employee} };
 
1625   # sort the whole thing
 
1626   @{ $self->{all_employees} } =
 
1627     sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
 
1629   if ($module eq 'AR') {
 
1631     # prepare query for departments
 
1632     $query = qq|SELECT d.id, d.description
 
1638     $query = qq|SELECT d.id, d.description
 
1643   $sth = $dbh->prepare($query);
 
1644   $sth->execute || $self->dberror($query);
 
1646   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1647     push @{ $self->{all_departments} }, $ref;
 
1652   $main::lxdebug->leave_sub();
 
1655 # this is only used for reports
 
1656 sub all_departments {
 
1657   $main::lxdebug->enter_sub();
 
1659   my ($self, $myconfig, $table) = @_;
 
1661   my $dbh   = $self->dbconnect($myconfig);
 
1662   my $where = "1 = 1";
 
1664   if (defined $table) {
 
1665     if ($table eq 'customer') {
 
1666       $where = " d.role = 'P'";
 
1670   my $query = qq|SELECT d.id, d.description
 
1674   my $sth = $dbh->prepare($query);
 
1675   $sth->execute || $self->dberror($query);
 
1677   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1678     push @{ $self->{all_departments} }, $ref;
 
1684   $main::lxdebug->leave_sub();
 
1688   $main::lxdebug->enter_sub();
 
1690   my ($self, $module, $myconfig, $table) = @_;
 
1692   $self->all_vc($myconfig, $table, $module);
 
1694   # get last customers or vendors
 
1697   my $dbh = $self->dbconnect($myconfig);
 
1701   # now get the account numbers
 
1702   $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id
 
1704               WHERE c.link LIKE '%$module%'
 
1707   $sth = $dbh->prepare($query);
 
1708   $sth->execute || $self->dberror($query);
 
1710   $self->{accounts} = "";
 
1711   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1713     foreach my $key (split /:/, $ref->{link}) {
 
1714       if ($key =~ /$module/) {
 
1716         # cross reference for keys
 
1717         $xkeyref{ $ref->{accno} } = $key;
 
1719         push @{ $self->{"${module}_links"}{$key} },
 
1720           { accno       => $ref->{accno},
 
1721             description => $ref->{description},
 
1722             taxkey      => $ref->{taxkey_id} };
 
1724         $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
 
1730   if (($module eq "AP") || ($module eq "AR")) {
 
1732     # get tax rates and description
 
1733     $query = qq| SELECT * FROM tax t|;
 
1734     $sth   = $dbh->prepare($query);
 
1735     $sth->execute || $self->dberror($query);
 
1737     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1738       push @{ $self->{TAX} }, $ref;
 
1744     my $arap = ($table eq 'customer') ? 'ar' : 'ap';
 
1746     $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
 
1747                 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
 
1748                 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
 
1749                 c.name AS $table, a.department_id, d.description AS department,
 
1750                 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
 
1751                 a.employee_id, e.name AS employee, a.gldate
 
1753                 JOIN $table c ON (a.${table}_id = c.id)
 
1754                 LEFT JOIN employee e ON (e.id = a.employee_id)
 
1755                 LEFT JOIN department d ON (d.id = a.department_id)
 
1756                 WHERE a.id = $self->{id}|;
 
1757     $sth = $dbh->prepare($query);
 
1758     $sth->execute || $self->dberror($query);
 
1760     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1761     foreach $key (keys %$ref) {
 
1762       $self->{$key} = $ref->{$key};
 
1766     # get amounts from individual entries
 
1767     $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
 
1768                 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate
 
1770                 JOIN chart c ON (c.id = a.chart_id)
 
1771                 LEFT JOIN project p ON (p.id = a.project_id)
 
1772                 LEFT Join tax t ON (a.taxkey = t.taxkey)
 
1773                 WHERE a.trans_id = $self->{id}
 
1774                 AND a.fx_transaction = '0'
 
1775                 ORDER BY a.oid,a.transdate|;
 
1776     $sth = $dbh->prepare($query);
 
1777     $sth->execute || $self->dberror($query);
 
1779     my $fld = ($table eq 'customer') ? 'buy' : 'sell';
 
1781     # get exchangerate for currency
 
1782     $self->{exchangerate} =
 
1783       $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
 
1787     # store amounts in {acc_trans}{$key} for multiple accounts
 
1788     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1789       $ref->{exchangerate} =
 
1790         $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
 
1792       if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
 
1795       $ref->{index} = $index;
 
1797       push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
 
1801     $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
 
1802                   (SELECT c.accno FROM chart c
 
1803                    WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1804                   (SELECT c.accno FROM chart c
 
1805                    WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
1807     $sth = $dbh->prepare($query);
 
1808     $sth->execute || $self->dberror($query);
 
1810     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1811     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
1817     $query = qq|SELECT current_date AS transdate,
 
1818                 d.curr AS currencies, d.closedto, d.revtrans,
 
1819                   (SELECT c.accno FROM chart c
 
1820                    WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
 
1821                   (SELECT c.accno FROM chart c
 
1822                    WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
 
1824     $sth = $dbh->prepare($query);
 
1825     $sth->execute || $self->dberror($query);
 
1827     $ref = $sth->fetchrow_hashref(NAME_lc);
 
1828     map { $self->{$_} = $ref->{$_} } keys %$ref;
 
1831     if ($self->{"$self->{vc}_id"}) {
 
1833       # only setup currency
 
1834       ($self->{currency}) = split /:/, $self->{currencies};
 
1838       $self->lastname_used($dbh, $myconfig, $table, $module);
 
1840       my $fld = ($table eq 'customer') ? 'buy' : 'sell';
 
1842       # get exchangerate for currency
 
1843       $self->{exchangerate} =
 
1844         $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
 
1853   $main::lxdebug->leave_sub();
 
1857   $main::lxdebug->enter_sub();
 
1859   my ($self, $dbh, $myconfig, $table, $module) = @_;
 
1861   my $arap  = ($table eq 'customer') ? "ar" : "ap";
 
1862   my $where = "1 = 1";
 
1864   if ($self->{type} =~ /_order/) {
 
1866     $where = "quotation = '0'";
 
1868   if ($self->{type} =~ /_quotation/) {
 
1870     $where = "quotation = '1'";
 
1873   my $query = qq|SELECT MAX(id) FROM $arap
 
1875                               AND ${table}_id > 0|;
 
1876   my $sth = $dbh->prepare($query);
 
1877   $sth->execute || $self->dberror($query);
 
1879   my ($trans_id) = $sth->fetchrow_array;
 
1883   $query = qq|SELECT ct.name, a.curr, a.${table}_id,
 
1884               current_date + ct.terms AS duedate, a.department_id,
 
1885               d.description AS department
 
1887               JOIN $table ct ON (a.${table}_id = ct.id)
 
1888               LEFT JOIN department d ON (a.department_id = d.id)
 
1889               WHERE a.id = $trans_id|;
 
1890   $sth = $dbh->prepare($query);
 
1891   $sth->execute || $self->dberror($query);
 
1893   ($self->{$table},  $self->{currency},      $self->{"${table}_id"},
 
1894    $self->{duedate}, $self->{department_id}, $self->{department})
 
1895     = $sth->fetchrow_array;
 
1898   $main::lxdebug->leave_sub();
 
1902   $main::lxdebug->enter_sub();
 
1904   my ($self, $myconfig, $thisdate, $days) = @_;
 
1906   my $dbh = $self->dbconnect($myconfig);
 
1911     my $dateformat = $myconfig->{dateformat};
 
1912     $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
 
1914     $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
 
1916     $sth = $dbh->prepare($query);
 
1917     $sth->execute || $self->dberror($query);
 
1919     $query = qq|SELECT current_date AS thisdate
 
1921     $sth = $dbh->prepare($query);
 
1922     $sth->execute || $self->dberror($query);
 
1925   ($thisdate) = $sth->fetchrow_array;
 
1930   $main::lxdebug->leave_sub();
 
1936   $main::lxdebug->enter_sub();
 
1938   my ($self, $string) = @_;
 
1940   if ($string !~ /%/) {
 
1941     $string = "%$string%";
 
1944   $string =~ s/\'/\'\'/g;
 
1946   $main::lxdebug->leave_sub();
 
1952   $main::lxdebug->enter_sub();
 
1954   my ($self, $flds, $new, $count, $numrows) = @_;
 
1958   map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
 
1964   foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
 
1966     $j = $item->{ndx} - 1;
 
1967     map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
 
1971   for $i ($count + 1 .. $numrows) {
 
1972     map { delete $self->{"${_}_$i"} } @{$flds};
 
1975   $main::lxdebug->leave_sub();
 
1979   $main::lxdebug->enter_sub();
 
1981   my ($self, $myconfig) = @_;
 
1985   my $dbh = $self->dbconnect_noauto($myconfig);
 
1987   my $query = qq|DELETE FROM status
 
1988                  WHERE formname = '$self->{formname}'
 
1990   my $sth = $dbh->prepare($query) || $self->dberror($query);
 
1992   if ($self->{formname} =~ /(check|receipt)/) {
 
1993     for $i (1 .. $self->{rowcount}) {
 
1994       $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
 
1998     $sth->execute($self->{id}) || $self->dberror($query);
 
2002   my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2003   my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2005   my %queued = split / /, $self->{queued};
 
2007   if ($self->{formname} =~ /(check|receipt)/) {
 
2009     # this is a check or receipt, add one entry for each lineitem
 
2010     my ($accno) = split /--/, $self->{account};
 
2011     $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
 
2012                 chart_id) VALUES (?, '$printed',
 
2013                 '$queued{$self->{formname}}', '$self->{prinform}',
 
2014                 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
 
2015     $sth = $dbh->prepare($query) || $self->dberror($query);
 
2017     for $i (1 .. $self->{rowcount}) {
 
2018       if ($self->{"checked_$i"}) {
 
2019         $sth->execute($self->{"id_$i"}) || $self->dberror($query);
 
2024     $query = qq|INSERT INTO status (trans_id, printed, emailed,
 
2025                 spoolfile, formname)
 
2026                 VALUES ($self->{id}, '$printed', '$emailed',
 
2027                 '$queued{$self->{formname}}', '$self->{formname}')|;
 
2028     $dbh->do($query) || $self->dberror($query);
 
2034   $main::lxdebug->leave_sub();
 
2038   $main::lxdebug->enter_sub();
 
2040   my ($self, $dbh) = @_;
 
2042   my ($query, $printed, $emailed);
 
2044   my $formnames  = $self->{printed};
 
2045   my $emailforms = $self->{emailed};
 
2047   my $query = qq|DELETE FROM status
 
2048                  WHERE formname = '$self->{formname}'
 
2049                  AND trans_id = $self->{id}|;
 
2050   $dbh->do($query) || $self->dberror($query);
 
2052   # this only applies to the forms
 
2053   # checks and receipts are posted when printed or queued
 
2055   if ($self->{queued}) {
 
2056     my %queued = split / /, $self->{queued};
 
2058     foreach my $formname (keys %queued) {
 
2059       $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
 
2060       $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
 
2062       $query = qq|INSERT INTO status (trans_id, printed, emailed,
 
2063                   spoolfile, formname)
 
2064                   VALUES ($self->{id}, '$printed', '$emailed',
 
2065                   '$queued{$formname}', '$formname')|;
 
2066       $dbh->do($query) || $self->dberror($query);
 
2068       $formnames  =~ s/$self->{formname}//;
 
2069       $emailforms =~ s/$self->{formname}//;
 
2074   # save printed, emailed info
 
2075   $formnames  =~ s/^ +//g;
 
2076   $emailforms =~ s/^ +//g;
 
2079   map { $status{$_}{printed} = 1 } split / +/, $formnames;
 
2080   map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
 
2082   foreach my $formname (keys %status) {
 
2083     $printed = ($formnames  =~ /$self->{formname}/) ? "1" : "0";
 
2084     $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
 
2086     $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
 
2087                 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
 
2088     $dbh->do($query) || $self->dberror($query);
 
2091   $main::lxdebug->leave_sub();
 
2094 sub update_defaults {
 
2095   $main::lxdebug->enter_sub();
 
2097   my ($self, $myconfig, $fld) = @_;
 
2099   my $dbh   = $self->dbconnect_noauto($myconfig);
 
2100   my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
 
2101   my $sth   = $dbh->prepare($query);
 
2103   $sth->execute || $self->dberror($query);
 
2104   my ($var) = $sth->fetchrow_array;
 
2109   $query = qq|UPDATE defaults
 
2111   $dbh->do($query) || $form->dberror($query);
 
2116   $main::lxdebug->leave_sub();
 
2121 sub update_business {
 
2122   $main::lxdebug->enter_sub();
 
2124   my ($self, $myconfig, $business_id) = @_;
 
2126   my $dbh   = $self->dbconnect_noauto($myconfig);
 
2128     qq|SELECT customernumberinit FROM business  WHERE id=$business_id FOR UPDATE|;
 
2129   my $sth = $dbh->prepare($query);
 
2131   $sth->execute || $self->dberror($query);
 
2132   my ($var) = $sth->fetchrow_array;
 
2137   $query = qq|UPDATE business
 
2138               SET customernumberinit = '$var' WHERE id=$business_id|;
 
2139   $dbh->do($query) || $form->dberror($query);
 
2144   $main::lxdebug->leave_sub();
 
2150   $main::lxdebug->enter_sub();
 
2152   my ($self, $myconfig, $salesman) = @_;
 
2154   my $dbh   = $self->dbconnect($myconfig);
 
2156     qq|SELECT id, name FROM customer  WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
 
2157   my $sth = $dbh->prepare($query);
 
2158   $sth->execute || $self->dberror($query);
 
2161   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2162     push(@{ $self->{salesman_list} }, $ref);
 
2166   $main::lxdebug->leave_sub();
 
2171 sub get_partsgroup {
 
2172   $main::lxdebug->enter_sub();
 
2174   my ($self, $myconfig, $p) = @_;
 
2176   my $dbh = $self->dbconnect($myconfig);
 
2178   my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
 
2180                  JOIN parts p ON (p.partsgroup_id = pg.id)|;
 
2182   if ($p->{searchitems} eq 'part') {
 
2184                  WHERE p.inventory_accno_id > 0|;
 
2186   if ($p->{searchitems} eq 'service') {
 
2188                  WHERE p.inventory_accno_id IS NULL|;
 
2190   if ($p->{searchitems} eq 'assembly') {
 
2192                  WHERE p.assembly = '1'|;
 
2194   if ($p->{searchitems} eq 'labor') {
 
2196                  WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
 
2200                  ORDER BY partsgroup|;
 
2203     $query = qq|SELECT id, partsgroup FROM partsgroup
 
2204                 ORDER BY partsgroup|;
 
2207   if ($p->{language_code}) {
 
2208     $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
 
2209                 t.description AS translation
 
2211                 JOIN parts p ON (p.partsgroup_id = pg.id)
 
2212                 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
 
2213                 ORDER BY translation|;
 
2216   my $sth = $dbh->prepare($query);
 
2217   $sth->execute || $self->dberror($query);
 
2219   $self->{all_partsgroup} = ();
 
2220   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2221     push @{ $self->{all_partsgroup} }, $ref;
 
2225   $main::lxdebug->leave_sub();
 
2228 sub get_pricegroup {
 
2229   $main::lxdebug->enter_sub();
 
2231   my ($self, $myconfig, $p) = @_;
 
2233   my $dbh = $self->dbconnect($myconfig);
 
2235   my $query = qq|SELECT p.id, p.pricegroup
 
2239                  ORDER BY pricegroup|;
 
2242     $query = qq|SELECT id, pricegroup FROM pricegroup
 
2243                 ORDER BY pricegroup|;
 
2246   my $sth = $dbh->prepare($query);
 
2247   $sth->execute || $self->dberror($query);
 
2249   $self->{all_pricegroup} = ();
 
2250   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2251     push @{ $self->{all_pricegroup} }, $ref;
 
2256   $main::lxdebug->leave_sub();
 
2260   my ($self, $dbh, $myconfig, $audittrail) = @_;
 
2262   # table, $reference, $formname, $action, $id, $transdate) = @_;
 
2269     $dbh        = $self->dbconnect($myconfig);
 
2273   # if we have an id add audittrail, otherwise get a new timestamp
 
2275   if ($audittrail->{id}) {
 
2277     $query = qq|SELECT audittrail FROM defaults|;
 
2279     if ($dbh->selectrow_array($query)) {
 
2280       my ($null, $employee_id) = $self->get_employee($dbh);
 
2282       if ($self->{audittrail} && !$myconfig) {
 
2283         chop $self->{audittrail};
 
2285         my @a = split /\|/, $self->{audittrail};
 
2289         my @flds = qw(tablename reference formname action transdate);
 
2291         # put into hash and remove dups
 
2293           $key = "$a[2]$a[3]";
 
2295           $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
 
2299         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2300                     formname, action, employee_id, transdate)
 
2301                     VALUES ($audittrail->{id}, ?, ?,
 
2302                     ?, ?, $employee_id, ?)|;
 
2303         my $sth = $dbh->prepare($query) || $self->dberror($query);
 
2307             $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
 
2311           for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
 
2313           $sth->execute || $self->dberror;
 
2318       if ($audittrail->{transdate}) {
 
2319         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2320                     formname, action, employee_id, transdate) VALUES (
 
2321                     $audittrail->{id}, '$audittrail->{tablename}', |
 
2322           . $dbh->quote($audittrail->{reference}) . qq|,
 
2323                     '$audittrail->{formname}', '$audittrail->{action}',
 
2324                     $employee_id, '$audittrail->{transdate}')|;
 
2326         $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
 
2327                     formname, action, employee_id) VALUES ($audittrail->{id},
 
2328                     '$audittrail->{tablename}', |
 
2329           . $dbh->quote($audittrail->{reference}) . qq|,
 
2330                     '$audittrail->{formname}', '$audittrail->{action}',
 
2337     $query = qq|SELECT current_timestamp FROM defaults|;
 
2338     my ($timestamp) = $dbh->selectrow_array($query);
 
2341       "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
 
2344   $dbh->disconnect if $disconnect;
 
2353   $main::lxdebug->enter_sub();
 
2355   my ($type, $country, $NLS_file) = @_;
 
2359   if ($country && -d "locale/$country") {
 
2360     $self->{countrycode} = $country;
 
2361     eval { require "locale/$country/$NLS_file"; };
 
2364   $self->{NLS_file} = $NLS_file;
 
2366   push @{ $self->{LONG_MONTH} },
 
2367     ("January",   "February", "March",    "April",
 
2368      "May ",      "June",     "July",     "August",
 
2369      "September", "October",  "November", "December");
 
2370   push @{ $self->{SHORT_MONTH} },
 
2371     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
 
2373   $main::lxdebug->leave_sub();
 
2379   my ($self, $text) = @_;
 
2381   return (exists $self{texts}{$text}) ? $self{texts}{$text} : $text;
 
2385   $main::lxdebug->enter_sub();
 
2387   my ($self, $text) = @_;
 
2389   if (exists $self{subs}{$text}) {
 
2390     $text = $self{subs}{$text};
 
2392     if ($self->{countrycode} && $self->{NLS_file}) {
 
2394          "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
 
2398   $main::lxdebug->leave_sub();
 
2404   $main::lxdebug->enter_sub();
 
2406   my ($self, $myconfig, $date, $longformat) = @_;
 
2409   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
 
2414     $spc = $myconfig->{dateformat};
 
2416     $spc = substr($spc, 1, 1);
 
2418     if ($date =~ /\D/) {
 
2419       if ($myconfig->{dateformat} =~ /^yy/) {
 
2420         ($yy, $mm, $dd) = split /\D/, $date;
 
2422       if ($myconfig->{dateformat} =~ /^mm/) {
 
2423         ($mm, $dd, $yy) = split /\D/, $date;
 
2425       if ($myconfig->{dateformat} =~ /^dd/) {
 
2426         ($dd, $mm, $yy) = split /\D/, $date;
 
2429       $date = substr($date, 2);
 
2430       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
 
2435     $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
2436     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
2438     if ($myconfig->{dateformat} =~ /^dd/) {
 
2439       if (defined $longformat && $longformat == 0) {
 
2441         $dd = "0$dd" if ($dd < 10);
 
2442         $mm = "0$mm" if ($mm < 10);
 
2443         $longdate = "$dd$spc$mm$spc$yy";
 
2446         $longdate .= ($spc eq '.') ? ". " : " ";
 
2447         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
 
2449     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
 
2451       # Use German syntax with the ISO date style "yyyy-mm-dd" because
 
2452       # Lx-Office is mainly used in Germany or German speaking countries.
 
2453       if (defined $longformat && $longformat == 0) {
 
2455         $dd = "0$dd" if ($dd < 10);
 
2456         $mm = "0$mm" if ($mm < 10);
 
2457         $longdate = "$yy-$mm-$dd";
 
2459         $longdate = "$dd. ";
 
2460         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
 
2463       if (defined $longformat && $longformat == 0) {
 
2465         $dd = "0$dd" if ($dd < 10);
 
2466         $mm = "0$mm" if ($mm < 10);
 
2467         $longdate = "$mm$spc$dd$spc$yy";
 
2469         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
 
2475   $main::lxdebug->leave_sub();
 
2481   $main::lxdebug->enter_sub();
 
2483   my ($self, $myconfig, $date, $longformat) = @_;
 
2486     $main::lxdebug->leave_sub();
 
2491   $spc = $myconfig->{dateformat};
 
2493   $spc = substr($spc, 1, 1);
 
2495   if ($date =~ /\D/) {
 
2496     if ($myconfig->{dateformat} =~ /^yy/) {
 
2497       ($yy, $mm, $dd) = split /\D/, $date;
 
2498     } elsif ($myconfig->{dateformat} =~ /^mm/) {
 
2499       ($mm, $dd, $yy) = split /\D/, $date;
 
2500     } elsif ($myconfig->{dateformat} =~ /^dd/) {
 
2501       ($dd, $mm, $yy) = split /\D/, $date;
 
2504     $date = substr($date, 2);
 
2505     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
 
2510   $yy = ($yy < 70) ? $yy + 2000 : $yy;
 
2511   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
 
2513   $main::lxdebug->leave_sub();
 
2514   return ($yy, $mm, $dd);