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;
 
  32 %charset_to_db_encoding = map { $_->{charset}, $_->{dbencoding} } @db_encodings;
 
  34 use constant DEFAULT_CHARSET => 'ISO-8859-15';
 
  37   my ($a, $b) = gettimeofday();
 
  38   return "${a}-${b}-${$}";
 
  42   return "/tmp/lx-office-tmp-" . unique_id();
 
  46   $main::lxdebug->enter_sub();
 
  48   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  50   my $dbh = $form->dbconnect($myconfig);
 
  52   my (@filter_values, $filter);
 
  54   foreach (qw(partnumber description)) {
 
  55     next unless $form->{$_};
 
  57     $filter .= qq| AND ($_ ILIKE ?)|;
 
  58     push @filter_values, '%' . $form->{$_} . '%';
 
  61   if ($form->{no_assemblies}) {
 
  62     $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
 
  64   if ($form->{assemblies}) {
 
  65     $filter .= qq| AND assembly=TRUE|;          # alles was assembly ist rausgeben erweiterung für bin/mozilla/wh.pl -> transfer_assembly_update_part 
 
  66 # eigentlich möchte ich diesen filter abbilden: 
 
  67 # select distinct partnumber  from parts inner join assembly on (parts.id = assembly.id) where assembly='t';
 
  68 # und so common ist die anweisung gar nicht. wie wäre es mit auslagern in WH.pm? -> get_all_working_assemblies? jb 21.2.2009
 
  71   if ($form->{no_services}) {
 
  72     #$filter .= qq| AND (COALESCE(inventory_accno_id, 0) > 0) AND (COALESCE (assembly, FALSE))|;
 
  73     $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|; # @mb hier nochmal optimieren ...
 
  76   substr($filter, 1, 3) = "WHERE" if ($filter);
 
  78   $order_by =~ s/[^a-zA-Z_]//g;
 
  79   $order_dir = $order_dir ? "ASC" : "DESC";
 
  82     qq|SELECT id, partnumber, description | .
 
  83     qq|FROM parts $filter | .
 
  84     qq|ORDER BY $order_by $order_dir|;
 
  85   my $sth = $dbh->prepare($query);
 
  86   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
  88   while (my $ref = $sth->fetchrow_hashref()) {
 
  89     push(@{$parts}, $ref);
 
  94   $main::lxdebug->leave_sub();
 
  99 sub retrieve_projects {
 
 100   $main::lxdebug->enter_sub();
 
 102   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 104   my $dbh = $form->dbconnect($myconfig);
 
 106   my (@filter_values, $filter);
 
 107   if ($form->{"projectnumber"}) {
 
 108     $filter .= qq| AND (projectnumber ILIKE ?)|;
 
 109     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
 
 111   if ($form->{"description"}) {
 
 112     $filter .= qq| AND (description ILIKE ?)|;
 
 113     push(@filter_values, '%' . $form->{"description"} . '%');
 
 115   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 117   $order_by =~ s/[^a-zA-Z_]//g;
 
 118   $order_dir = $order_dir ? "ASC" : "DESC";
 
 121     qq|SELECT id, projectnumber, description | .
 
 122     qq|FROM project $filter | .
 
 123     qq|ORDER BY $order_by $order_dir|;
 
 124   my $sth = $dbh->prepare($query);
 
 125   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 127   while (my $ref = $sth->fetchrow_hashref()) {
 
 128     push(@{$projects}, $ref);
 
 133   $main::lxdebug->leave_sub();
 
 138 sub retrieve_employees {
 
 139   $main::lxdebug->enter_sub();
 
 141   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 143   my $dbh = $form->dbconnect($myconfig);
 
 145   my (@filter_values, $filter);
 
 146   if ($form->{"name"}) {
 
 147     $filter .= qq| AND (name ILIKE ?)|;
 
 148     push(@filter_values, '%' . $form->{"name"} . '%');
 
 150   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 152   $order_by =~ s/[^a-zA-Z_]//g;
 
 153   $order_dir = $order_dir ? "ASC" : "DESC";
 
 156     qq|SELECT id, name | .
 
 157     qq|FROM employee $filter | .
 
 158     qq|ORDER BY $order_by $order_dir|;
 
 159   my $sth = $dbh->prepare($query);
 
 160   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 162   while (my $ref = $sth->fetchrow_hashref()) {
 
 163     push(@{$employees}, $ref);
 
 168   $main::lxdebug->leave_sub();
 
 173 sub retrieve_customers_or_vendors {
 
 174   $main::lxdebug->enter_sub();
 
 176   my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
 
 178   my $dbh = $form->dbconnect($myconfig);
 
 180   my (@filter_values, $filter);
 
 181   if ($form->{"name"}) {
 
 182     $filter .= " AND (TABLE.name ILIKE ?)";
 
 183     push(@filter_values, '%' . $form->{"name"} . '%');
 
 185   if (!$form->{"obsolete"}) {
 
 186     $filter .= " AND NOT TABLE.obsolete";
 
 188   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 190   $order_by =~ s/[^a-zA-Z_]//g;
 
 191   $order_dir = $order_dir ? "ASC" : "DESC";
 
 193   my (@queries, @query_parameters);
 
 195   if ($allow_both || !$is_vendor) {
 
 196     my $c_filter = $filter;
 
 197     $c_filter =~ s/TABLE/c/g;
 
 198     push(@queries, qq|SELECT
 
 199                         c.id, c.name, 0 AS customer_is_vendor,
 
 200                         c.street, c.zipcode, c.city,
 
 201                         ct.cp_greeting, ct.cp_title, ct.cp_givenname, ct.cp_name
 
 203                       LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
 
 205     push(@query_parameters, @filter_values);
 
 208   if ($allow_both || $is_vendor) {
 
 209     my $v_filter = $filter;
 
 210     $v_filter =~ s/TABLE/v/g;
 
 211     push(@queries, qq|SELECT
 
 212                         v.id, v.name, 1 AS customer_is_vendor,
 
 213                         v.street, v.zipcode, v.city,
 
 214                         ct.cp_greeting, ct.cp_title, ct.cp_givenname, ct.cp_name
 
 216                       LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
 
 218     push(@query_parameters, @filter_values);
 
 221   my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
 
 222   my $sth = $dbh->prepare($query);
 
 223   $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
 
 225   while (my $ref = $sth->fetchrow_hashref()) {
 
 226     push(@{$customers}, $ref);
 
 231   $main::lxdebug->leave_sub();
 
 236 sub retrieve_delivery_customer {
 
 237   $main::lxdebug->enter_sub();
 
 239   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 241   my $dbh = $form->dbconnect($myconfig);
 
 243   my (@filter_values, $filter);
 
 244   if ($form->{"name"}) {
 
 245     $filter .= qq| (name ILIKE ?) AND|;
 
 246     push(@filter_values, '%' . $form->{"name"} . '%');
 
 249   $order_by =~ s/[^a-zA-Z_]//g;
 
 250   $order_dir = $order_dir ? "ASC" : "DESC";
 
 253     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
 
 255     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
 
 256     qq!ORDER BY $order_by $order_dir!;
 
 257   my $sth = $dbh->prepare($query);
 
 258   $sth->execute(@filter_values) ||
 
 259     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 260   my $delivery_customers = [];
 
 261   while (my $ref = $sth->fetchrow_hashref()) {
 
 262     push(@{$delivery_customers}, $ref);
 
 267   $main::lxdebug->leave_sub();
 
 269   return $delivery_customers;
 
 272 sub retrieve_vendor {
 
 273   $main::lxdebug->enter_sub();
 
 275   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 277   my $dbh = $form->dbconnect($myconfig);
 
 279   my (@filter_values, $filter);
 
 280   if ($form->{"name"}) {
 
 281     $filter .= qq| (name ILIKE ?) AND|;
 
 282     push(@filter_values, '%' . $form->{"name"} . '%');
 
 285   $order_by =~ s/[^a-zA-Z_]//g;
 
 286   $order_dir = $order_dir ? "ASC" : "DESC";
 
 289     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
 
 290     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
 
 291     qq!ORDER BY $order_by $order_dir!;
 
 292   my $sth = $dbh->prepare($query);
 
 293   $sth->execute(@filter_values) ||
 
 294     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 296   while (my $ref = $sth->fetchrow_hashref()) {
 
 297     push(@{$vendors}, $ref);
 
 302   $main::lxdebug->leave_sub();
 
 307 sub mkdir_with_parents {
 
 308   $main::lxdebug->enter_sub();
 
 310   my ($full_path) = @_;
 
 314   $full_path =~ s|/+|/|;
 
 316   foreach my $part (split(m|/|, $full_path)) {
 
 317     $path .= "/" if ($path);
 
 320     die("Could not create directory '$path' because a file exists with " .
 
 321         "the same name.\n") if (-f $path);
 
 324       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
 
 329   $main::lxdebug->leave_sub();
 
 333   $main::lxdebug->enter_sub();
 
 337   return $main::lxdebug->leave_sub()
 
 338     unless ($main::webdav && $form->{id});
 
 342   $form->{WEBDAV} = [];
 
 344   if ($form->{type} eq "sales_quotation") {
 
 345     ($path, $number) = ("angebote", $form->{quonumber});
 
 346   } elsif ($form->{type} eq "sales_order") {
 
 347     ($path, $number) = ("bestellungen", $form->{ordnumber});
 
 348   } elsif ($form->{type} eq "request_quotation") {
 
 349     ($path, $number) = ("anfragen", $form->{quonumber});
 
 350   } elsif ($form->{type} eq "purchase_order") {
 
 351     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
 
 352   } elsif ($form->{type} eq "credit_note") {
 
 353     ($path, $number) = ("gutschriften", $form->{invnumber});
 
 354   } elsif ($form->{vc} eq "customer") {
 
 355     ($path, $number) = ("rechnungen", $form->{invnumber});
 
 357     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
 
 360   return $main::lxdebug->leave_sub() unless ($path && $number);
 
 362   $number =~ s|[/\\]|_|g;
 
 364   $path = "webdav/${path}/${number}";
 
 367     mkdir_with_parents($path);
 
 370     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
 
 371     $base_path =~ s|[^/]+$||;
 
 372     $base_path =~ s|/$||;
 
 373     # wo kommt der wert für dir her? es wird doch gar nichts übergeben? fix für strict my $dir jb 21.2.
 
 374     if (opendir my $dir, $path) {
 
 375       foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
 
 376         next if (($file eq '.') || ($file eq '..'));
 
 381         my $is_directory = -d "$path/$file";
 
 383         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
 
 384         $file .=  '/' if ($is_directory);
 
 386         push @{ $form->{WEBDAV} }, {
 
 388           'link' => ($ENV{"HTTPS"} ? "https://" : "http://") . $ENV{'SERVER_NAME'} . "/$base_path/$file",
 
 389           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
 
 397   $main::lxdebug->leave_sub();
 
 401   $main::lxdebug->enter_sub();
 
 403   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
 
 405   $vc = $vc eq "customer" ? "customer" : "vendor";
 
 407   my $dbh = $form->dbconnect($myconfig);
 
 414          pt.description AS payment_terms,
 
 415          b.description AS business,
 
 416          l.description AS language
 
 418        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
 
 419        LEFT JOIN business b ON (vc.business_id = b.id)
 
 420        LEFT JOIN language l ON (vc.language_id = l.id)
 
 422   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
 
 426     $main::lxdebug->leave_sub();
 
 430   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 432   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
 
 434   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
 
 435   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 437   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
 
 438   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 442   $main::lxdebug->leave_sub();
 
 447 sub get_shipto_by_id {
 
 448   $main::lxdebug->enter_sub();
 
 450   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
 
 454   my $dbh = $form->dbconnect($myconfig);
 
 456   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 457   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
 
 459   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
 
 463   $main::lxdebug->leave_sub();
 
 466 sub save_email_status {
 
 467   $main::lxdebug->enter_sub();
 
 469   my ($self, $myconfig, $form) = @_;
 
 471   my ($table, $query, $dbh);
 
 473   if ($form->{script} eq 'oe.pl') {
 
 476   } elsif ($form->{script} eq 'is.pl') {
 
 479   } elsif ($form->{script} eq 'ir.pl') {
 
 484   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
 
 486   $dbh = $form->get_standard_dbh($myconfig);
 
 488   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
 
 490   $intnotes =~ s|\r||g;
 
 491   $intnotes =~ s|\n$||;
 
 493   $intnotes .= "\n\n" if ($intnotes);
 
 495   my $cc  = $main::locale->text('Cc') . ": $form->{cc}\n"   if $form->{cc};
 
 496   my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
 
 497   my $now = scalar localtime;
 
 499   $intnotes .= $main::locale->text('[email]') . "\n"
 
 500     . $main::locale->text('Date') . ": $now\n"
 
 501     . $main::locale->text('To (email)') . ": $form->{email}\n"
 
 503     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
 
 504     . $main::locale->text('Message') . ": $form->{message}";
 
 506   $intnotes =~ s|\r||g;
 
 508   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
 
 510   $form->save_status($dbh);
 
 514   $main::lxdebug->leave_sub();
 
 520   foreach my $key (@_) {
 
 521     if ((ref $key eq '') && !defined $params->{$key}) {
 
 522       my $subroutine = (caller(1))[3];
 
 523       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 525     } elsif (ref $key eq 'ARRAY') {
 
 527       foreach my $subkey (@{ $key }) {
 
 528         if (defined $params->{$subkey}) {
 
 535         my $subroutine = (caller(1))[3];
 
 536         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
 
 545   foreach my $key (@_) {
 
 546     if ((ref $key eq '') && !exists $params->{$key}) {
 
 547       my $subroutine = (caller(1))[3];
 
 548       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 550     } elsif (ref $key eq 'ARRAY') {
 
 552       foreach my $subkey (@{ $key }) {
 
 553         if (exists $params->{$subkey}) {
 
 560         my $subroutine = (caller(1))[3];
 
 561         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));