1 #====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #====================================================================
 
  11 use Time::HiRes qw(gettimeofday);
 
  13 use vars qw(@db_encodings %db_encoding_to_charset);
 
  16   { "label" => "ASCII",          "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
 
  17   { "label" => "UTF-8 Unicode",  "dbencoding" => "UNICODE",   "charset" => "UTF-8" },
 
  18   { "label" => "ISO 8859-1",     "dbencoding" => "LATIN1",    "charset" => "ISO-8859-1" },
 
  19   { "label" => "ISO 8859-2",     "dbencoding" => "LATIN2",    "charset" => "ISO-8859-2" },
 
  20   { "label" => "ISO 8859-3",     "dbencoding" => "LATIN3",    "charset" => "ISO-8859-3" },
 
  21   { "label" => "ISO 8859-4",     "dbencoding" => "LATIN4",    "charset" => "ISO-8859-4" },
 
  22   { "label" => "ISO 8859-5",     "dbencoding" => "LATIN5",    "charset" => "ISO-8859-5" },
 
  23   { "label" => "ISO 8859-15",    "dbencoding" => "LATIN9",    "charset" => "ISO-8859-15" },
 
  24   { "label" => "KOI8-R",         "dbencoding" => "KOI8",      "charset" => "KOI8-R" },
 
  25   { "label" => "Windows CP1251", "dbencoding" => "WIN",       "charset" => "CP1251" },
 
  26   { "label" => "Windows CP866",  "dbencoding" => "ALT",       "charset" => "CP866" },
 
  29 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
 
  31 use constant DEFAULT_CHARSET => 'ISO-8859-15';
 
  34   my ($a, $b) = gettimeofday();
 
  35   return "${a}-${b}-${$}";
 
  39   return "/tmp/lx-office-tmp-" . unique_id();
 
  43   $main::lxdebug->enter_sub();
 
  45   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  47   my $dbh = $form->dbconnect($myconfig);
 
  49   my (@filter_values, $filter);
 
  50   if ($form->{"partnumber"}) {
 
  51     $filter .= qq| AND (partnumber ILIKE ?)|;
 
  52     push(@filter_values, '%' . $form->{"partnumber"} . '%');
 
  54   if ($form->{"description"}) {
 
  55     $filter .= qq| AND (description ILIKE ?)|;
 
  56     push(@filter_values, '%' . $form->{"description"} . '%');
 
  58   substr($filter, 1, 3) = "WHERE" if ($filter);
 
  60   $order_by =~ s/[^a-zA-Z_]//g;
 
  61   $order_dir = $order_dir ? "ASC" : "DESC";
 
  64     qq|SELECT id, partnumber, description | .
 
  65     qq|FROM parts $filter | .
 
  66     qq|ORDER BY $order_by $order_dir|;
 
  67   my $sth = $dbh->prepare($query);
 
  68   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
  70   while (my $ref = $sth->fetchrow_hashref()) {
 
  71     push(@{$parts}, $ref);
 
  76   $main::lxdebug->leave_sub();
 
  81 sub retrieve_projects {
 
  82   $main::lxdebug->enter_sub();
 
  84   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  86   my $dbh = $form->dbconnect($myconfig);
 
  88   my (@filter_values, $filter);
 
  89   if ($form->{"projectnumber"}) {
 
  90     $filter .= qq| AND (projectnumber ILIKE ?)|;
 
  91     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
 
  93   if ($form->{"description"}) {
 
  94     $filter .= qq| AND (description ILIKE ?)|;
 
  95     push(@filter_values, '%' . $form->{"description"} . '%');
 
  97   substr($filter, 1, 3) = "WHERE" if ($filter);
 
  99   $order_by =~ s/[^a-zA-Z_]//g;
 
 100   $order_dir = $order_dir ? "ASC" : "DESC";
 
 103     qq|SELECT id, projectnumber, description | .
 
 104     qq|FROM project $filter | .
 
 105     qq|ORDER BY $order_by $order_dir|;
 
 106   my $sth = $dbh->prepare($query);
 
 107   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 109   while (my $ref = $sth->fetchrow_hashref()) {
 
 110     push(@{$projects}, $ref);
 
 115   $main::lxdebug->leave_sub();
 
 120 sub retrieve_employees {
 
 121   $main::lxdebug->enter_sub();
 
 123   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 125   my $dbh = $form->dbconnect($myconfig);
 
 127   my (@filter_values, $filter);
 
 128   if ($form->{"name"}) {
 
 129     $filter .= qq| AND (name ILIKE ?)|;
 
 130     push(@filter_values, '%' . $form->{"name"} . '%');
 
 132   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 134   $order_by =~ s/[^a-zA-Z_]//g;
 
 135   $order_dir = $order_dir ? "ASC" : "DESC";
 
 138     qq|SELECT id, name | .
 
 139     qq|FROM employee $filter | .
 
 140     qq|ORDER BY $order_by $order_dir|;
 
 141   my $sth = $dbh->prepare($query);
 
 142   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 144   while (my $ref = $sth->fetchrow_hashref()) {
 
 145     push(@{$employees}, $ref);
 
 150   $main::lxdebug->leave_sub();
 
 155 sub retrieve_delivery_customer {
 
 156   $main::lxdebug->enter_sub();
 
 158   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 160   my $dbh = $form->dbconnect($myconfig);
 
 162   my (@filter_values, $filter);
 
 163   if ($form->{"name"}) {
 
 164     $filter .= qq| (name ILIKE ?) AND|;
 
 165     push(@filter_values, '%' . $form->{"name"} . '%');
 
 168   $order_by =~ s/[^a-zA-Z_]//g;
 
 169   $order_dir = $order_dir ? "ASC" : "DESC";
 
 172     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
 
 174     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
 
 175     qq!ORDER BY $order_by $order_dir!;
 
 176   my $sth = $dbh->prepare($query);
 
 177   $sth->execute(@filter_values) ||
 
 178     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 179   my $delivery_customers = [];
 
 180   while (my $ref = $sth->fetchrow_hashref()) {
 
 181     push(@{$delivery_customers}, $ref);
 
 186   $main::lxdebug->leave_sub();
 
 188   return $delivery_customers;
 
 191 sub retrieve_vendor {
 
 192   $main::lxdebug->enter_sub();
 
 194   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 196   my $dbh = $form->dbconnect($myconfig);
 
 198   my (@filter_values, $filter);
 
 199   if ($form->{"name"}) {
 
 200     $filter .= qq| (name ILIKE ?) AND|;
 
 201     push(@filter_values, '%' . $form->{"name"} . '%');
 
 204   $order_by =~ s/[^a-zA-Z_]//g;
 
 205   $order_dir = $order_dir ? "ASC" : "DESC";
 
 208     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
 
 209     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
 
 210     qq!ORDER BY $order_by $order_dir!;
 
 211   my $sth = $dbh->prepare($query);
 
 212   $sth->execute(@filter_values) ||
 
 213     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 215   while (my $ref = $sth->fetchrow_hashref()) {
 
 216     push(@{$vendors}, $ref);
 
 221   $main::lxdebug->leave_sub();
 
 226 sub mkdir_with_parents {
 
 227   $main::lxdebug->enter_sub();
 
 229   my ($full_path) = @_;
 
 233   $full_path =~ s|/+|/|;
 
 235   foreach my $part (split(m|/|, $full_path)) {
 
 236     $path .= "/" if ($path);
 
 239     die("Could not create directory '$path' because a file exists with " .
 
 240         "the same name.\n") if (-f $path);
 
 243       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
 
 248   $main::lxdebug->leave_sub();
 
 252   $main::lxdebug->enter_sub();
 
 256   return $main::lxdebug->leave_sub()
 
 257     unless ($main::webdav && $form->{id});
 
 261   $form->{WEBDAV} = {};
 
 263   if ($form->{type} eq "sales_quotation") {
 
 264     ($path, $number) = ("angebote", $form->{quonumber});
 
 265   } elsif ($form->{type} eq "sales_order") {
 
 266     ($path, $number) = ("bestellungen", $form->{ordnumber});
 
 267   } elsif ($form->{type} eq "request_quotation") {
 
 268     ($path, $number) = ("anfragen", $form->{quonumber});
 
 269   } elsif ($form->{type} eq "purchase_order") {
 
 270     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
 
 271   } elsif ($form->{type} eq "credit_note") {
 
 272     ($path, $number) = ("gutschriften", $form->{invnumber});
 
 273   } elsif ($form->{vc} eq "customer") {
 
 274     ($path, $number) = ("rechnungen", $form->{invnumber});
 
 276     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
 
 279   return $main::lxdebug->leave_sub() unless ($path && $number);
 
 281   $path = "webdav/${path}/${number}";
 
 284     mkdir_with_parents($path);
 
 287     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
 
 288     $base_path =~ s|[^/]+$||;
 
 290     foreach my $file (<$path/*>) {
 
 293       $form->{WEBDAV}{$fname} =
 
 294         ($ENV{"HTTPS"} ? "https://" : "http://") .
 
 295         $ENV{'SERVER_NAME'} . "/" . $base_path . $file;
 
 299   $main::lxdebug->leave_sub();