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   $number =~ s|[/\\]|_|g;
 
 285   $path = "webdav/${path}/${number}";
 
 288     mkdir_with_parents($path);
 
 291     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
 
 292     $base_path =~ s|[^/]+$||;
 
 293     $base_path =~ s|/$||;
 
 295     if (opendir $dir, $path) {
 
 296       foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
 
 297         next if (($file eq '.') || ($file eq '..'));
 
 302         my $is_directory = -d "$path/$file";
 
 304         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
 
 305         $file .=  '/' if ($is_directory);
 
 307         push @{ $form->{WEBDAV} }, {
 
 309           'link' => ($ENV{"HTTPS"} ? "https://" : "http://") . $ENV{'SERVER_NAME'} . "/$base_path/$file",
 
 310           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
 
 318   $main::lxdebug->leave_sub();
 
 322   $main::lxdebug->enter_sub();
 
 324   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
 
 326   $vc = $vc eq "customer" ? "customer" : "vendor";
 
 328   my $dbh = $form->dbconnect($myconfig);
 
 335          pt.description AS payment_terms,
 
 336          b.description AS business,
 
 337          l.description AS language
 
 339        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
 
 340        LEFT JOIN business b ON (vc.business_id = b.id)
 
 341        LEFT JOIN language l ON (vc.language_id = l.id)
 
 343   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
 
 347     $main::lxdebug->leave_sub();
 
 351   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 353   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
 
 355   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
 
 356   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 358   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
 
 359   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 363   $main::lxdebug->leave_sub();
 
 368 sub get_shipto_by_id {
 
 369   $main::lxdebug->enter_sub();
 
 371   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
 
 375   my $dbh = $form->dbconnect($myconfig);
 
 377   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 378   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
 
 380   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
 
 384   $main::lxdebug->leave_sub();
 
 387 sub save_email_status {
 
 388   $main::lxdebug->enter_sub();
 
 390   my ($self, $myconfig, $form) = @_;
 
 392   my ($table, $query, $dbh);
 
 394   if ($form->{script} eq 'oe.pl') {
 
 397   } elsif ($form->{script} eq 'is.pl') {
 
 400   } elsif ($form->{script} eq 'ir.pl') {
 
 405   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
 
 407   $dbh = $form->get_standard_dbh($myconfig);
 
 409   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
 
 411   $intnotes =~ s|\r||g;
 
 412   $intnotes =~ s|\n$||;
 
 414   $intnotes .= "\n\n" if ($intnotes);
 
 416   my $cc  = $main::locale->text('Cc') . ": $form->{cc}\n"   if $form->{cc};
 
 417   my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
 
 418   my $now = scalar localtime;
 
 420   $intnotes .= $main::locale->text('[email]') . "\n"
 
 421     . $main::locale->text('Date') . ": $now\n"
 
 422     . $main::locale->text('To (email)') . ": $form->{email}\n"
 
 424     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
 
 425     . $main::locale->text('Message') . ": $form->{message}";
 
 427   $intnotes =~ s|\r||g;
 
 429   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
 
 431   $form->save_status($dbh);
 
 435   $main::lxdebug->leave_sub();