1 #====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #====================================================================
 
  11 use Time::HiRes qw(gettimeofday);
 
  15 use vars qw(@db_encodings %db_encoding_to_charset);
 
  18   { "label" => "ASCII",          "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
 
  19   { "label" => "UTF-8 Unicode",  "dbencoding" => "UNICODE",   "charset" => "UTF-8" },
 
  20   { "label" => "ISO 8859-1",     "dbencoding" => "LATIN1",    "charset" => "ISO-8859-1" },
 
  21   { "label" => "ISO 8859-2",     "dbencoding" => "LATIN2",    "charset" => "ISO-8859-2" },
 
  22   { "label" => "ISO 8859-3",     "dbencoding" => "LATIN3",    "charset" => "ISO-8859-3" },
 
  23   { "label" => "ISO 8859-4",     "dbencoding" => "LATIN4",    "charset" => "ISO-8859-4" },
 
  24   { "label" => "ISO 8859-5",     "dbencoding" => "LATIN5",    "charset" => "ISO-8859-5" },
 
  25   { "label" => "ISO 8859-15",    "dbencoding" => "LATIN9",    "charset" => "ISO-8859-15" },
 
  26   { "label" => "KOI8-R",         "dbencoding" => "KOI8",      "charset" => "KOI8-R" },
 
  27   { "label" => "Windows CP1251", "dbencoding" => "WIN",       "charset" => "CP1251" },
 
  28   { "label" => "Windows CP866",  "dbencoding" => "ALT",       "charset" => "CP866" },
 
  31 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
 
  33 use constant DEFAULT_CHARSET => 'ISO-8859-15';
 
  36   my ($a, $b) = gettimeofday();
 
  37   return "${a}-${b}-${$}";
 
  41   return "/tmp/lx-office-tmp-" . unique_id();
 
  45   $main::lxdebug->enter_sub();
 
  47   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  49   my $dbh = $form->dbconnect($myconfig);
 
  51   my (@filter_values, $filter);
 
  52   if ($form->{"partnumber"}) {
 
  53     $filter .= qq| AND (partnumber ILIKE ?)|;
 
  54     push(@filter_values, '%' . $form->{"partnumber"} . '%');
 
  56   if ($form->{"description"}) {
 
  57     $filter .= qq| AND (description ILIKE ?)|;
 
  58     push(@filter_values, '%' . $form->{"description"} . '%');
 
  60   substr($filter, 1, 3) = "WHERE" if ($filter);
 
  62   $order_by =~ s/[^a-zA-Z_]//g;
 
  63   $order_dir = $order_dir ? "ASC" : "DESC";
 
  66     qq|SELECT id, partnumber, description | .
 
  67     qq|FROM parts $filter | .
 
  68     qq|ORDER BY $order_by $order_dir|;
 
  69   my $sth = $dbh->prepare($query);
 
  70   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
  72   while (my $ref = $sth->fetchrow_hashref()) {
 
  73     push(@{$parts}, $ref);
 
  78   $main::lxdebug->leave_sub();
 
  83 sub retrieve_projects {
 
  84   $main::lxdebug->enter_sub();
 
  86   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  88   my $dbh = $form->dbconnect($myconfig);
 
  90   my (@filter_values, $filter);
 
  91   if ($form->{"projectnumber"}) {
 
  92     $filter .= qq| AND (projectnumber ILIKE ?)|;
 
  93     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
 
  95   if ($form->{"description"}) {
 
  96     $filter .= qq| AND (description ILIKE ?)|;
 
  97     push(@filter_values, '%' . $form->{"description"} . '%');
 
  99   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 101   $order_by =~ s/[^a-zA-Z_]//g;
 
 102   $order_dir = $order_dir ? "ASC" : "DESC";
 
 105     qq|SELECT id, projectnumber, description | .
 
 106     qq|FROM project $filter | .
 
 107     qq|ORDER BY $order_by $order_dir|;
 
 108   my $sth = $dbh->prepare($query);
 
 109   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 111   while (my $ref = $sth->fetchrow_hashref()) {
 
 112     push(@{$projects}, $ref);
 
 117   $main::lxdebug->leave_sub();
 
 122 sub retrieve_employees {
 
 123   $main::lxdebug->enter_sub();
 
 125   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 127   my $dbh = $form->dbconnect($myconfig);
 
 129   my (@filter_values, $filter);
 
 130   if ($form->{"name"}) {
 
 131     $filter .= qq| AND (name ILIKE ?)|;
 
 132     push(@filter_values, '%' . $form->{"name"} . '%');
 
 134   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 136   $order_by =~ s/[^a-zA-Z_]//g;
 
 137   $order_dir = $order_dir ? "ASC" : "DESC";
 
 140     qq|SELECT id, name | .
 
 141     qq|FROM employee $filter | .
 
 142     qq|ORDER BY $order_by $order_dir|;
 
 143   my $sth = $dbh->prepare($query);
 
 144   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 146   while (my $ref = $sth->fetchrow_hashref()) {
 
 147     push(@{$employees}, $ref);
 
 152   $main::lxdebug->leave_sub();
 
 157 sub retrieve_delivery_customer {
 
 158   $main::lxdebug->enter_sub();
 
 160   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 162   my $dbh = $form->dbconnect($myconfig);
 
 164   my (@filter_values, $filter);
 
 165   if ($form->{"name"}) {
 
 166     $filter .= qq| (name ILIKE ?) AND|;
 
 167     push(@filter_values, '%' . $form->{"name"} . '%');
 
 170   $order_by =~ s/[^a-zA-Z_]//g;
 
 171   $order_dir = $order_dir ? "ASC" : "DESC";
 
 174     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
 
 176     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
 
 177     qq!ORDER BY $order_by $order_dir!;
 
 178   my $sth = $dbh->prepare($query);
 
 179   $sth->execute(@filter_values) ||
 
 180     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 181   my $delivery_customers = [];
 
 182   while (my $ref = $sth->fetchrow_hashref()) {
 
 183     push(@{$delivery_customers}, $ref);
 
 188   $main::lxdebug->leave_sub();
 
 190   return $delivery_customers;
 
 193 sub retrieve_vendor {
 
 194   $main::lxdebug->enter_sub();
 
 196   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 198   my $dbh = $form->dbconnect($myconfig);
 
 200   my (@filter_values, $filter);
 
 201   if ($form->{"name"}) {
 
 202     $filter .= qq| (name ILIKE ?) AND|;
 
 203     push(@filter_values, '%' . $form->{"name"} . '%');
 
 206   $order_by =~ s/[^a-zA-Z_]//g;
 
 207   $order_dir = $order_dir ? "ASC" : "DESC";
 
 210     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
 
 211     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
 
 212     qq!ORDER BY $order_by $order_dir!;
 
 213   my $sth = $dbh->prepare($query);
 
 214   $sth->execute(@filter_values) ||
 
 215     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 217   while (my $ref = $sth->fetchrow_hashref()) {
 
 218     push(@{$vendors}, $ref);
 
 223   $main::lxdebug->leave_sub();
 
 228 sub mkdir_with_parents {
 
 229   $main::lxdebug->enter_sub();
 
 231   my ($full_path) = @_;
 
 235   $full_path =~ s|/+|/|;
 
 237   foreach my $part (split(m|/|, $full_path)) {
 
 238     $path .= "/" if ($path);
 
 241     die("Could not create directory '$path' because a file exists with " .
 
 242         "the same name.\n") if (-f $path);
 
 245       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
 
 250   $main::lxdebug->leave_sub();
 
 254   $main::lxdebug->enter_sub();
 
 258   return $main::lxdebug->leave_sub()
 
 259     unless ($main::webdav && $form->{id});
 
 263   $form->{WEBDAV} = {};
 
 265   if ($form->{type} eq "sales_quotation") {
 
 266     ($path, $number) = ("angebote", $form->{quonumber});
 
 267   } elsif ($form->{type} eq "sales_order") {
 
 268     ($path, $number) = ("bestellungen", $form->{ordnumber});
 
 269   } elsif ($form->{type} eq "request_quotation") {
 
 270     ($path, $number) = ("anfragen", $form->{quonumber});
 
 271   } elsif ($form->{type} eq "purchase_order") {
 
 272     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
 
 273   } elsif ($form->{type} eq "credit_note") {
 
 274     ($path, $number) = ("gutschriften", $form->{invnumber});
 
 275   } elsif ($form->{vc} eq "customer") {
 
 276     ($path, $number) = ("rechnungen", $form->{invnumber});
 
 278     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
 
 281   return $main::lxdebug->leave_sub() unless ($path && $number);
 
 283   $path = "webdav/${path}/${number}";
 
 286     mkdir_with_parents($path);
 
 289     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
 
 290     $base_path =~ s|[^/]+$||;
 
 292     foreach my $file (<$path/*>) {
 
 295       $form->{WEBDAV}{$fname} =
 
 296         ($ENV{"HTTPS"} ? "https://" : "http://") .
 
 297         $ENV{'SERVER_NAME'} . "/" . $base_path . $file;
 
 301   $main::lxdebug->leave_sub();
 
 305   $main::lxdebug->enter_sub();
 
 307   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
 
 309   $vc = $vc eq "customer" ? "customer" : "vendor";
 
 311   my $dbh = $form->dbconnect($myconfig);
 
 318          pt.description AS payment_terms,
 
 319          b.description AS business,
 
 320          l.description AS language
 
 322        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
 
 323        LEFT JOIN business b ON (vc.business_id = b.id)
 
 324        LEFT JOIN language l ON (vc.language_id = l.id)
 
 326   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
 
 330     $main::lxdebug->leave_sub();
 
 334   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 336   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
 
 338   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
 
 339   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 341   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
 
 342   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 346   $main::lxdebug->leave_sub();