1 #====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #====================================================================
 
  14 use Time::HiRes qw(gettimeofday);
 
  19 use vars qw(@db_encodings %db_encoding_to_charset %charset_to_db_encoding);
 
  22   { "label" => "ASCII",          "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
 
  23   { "label" => "UTF-8 Unicode",  "dbencoding" => "UNICODE",   "charset" => "UTF-8" },
 
  24   { "label" => "ISO 8859-1",     "dbencoding" => "LATIN1",    "charset" => "ISO-8859-1" },
 
  25   { "label" => "ISO 8859-2",     "dbencoding" => "LATIN2",    "charset" => "ISO-8859-2" },
 
  26   { "label" => "ISO 8859-3",     "dbencoding" => "LATIN3",    "charset" => "ISO-8859-3" },
 
  27   { "label" => "ISO 8859-4",     "dbencoding" => "LATIN4",    "charset" => "ISO-8859-4" },
 
  28   { "label" => "ISO 8859-5",     "dbencoding" => "LATIN5",    "charset" => "ISO-8859-5" },
 
  29   { "label" => "ISO 8859-15",    "dbencoding" => "LATIN9",    "charset" => "ISO-8859-15" },
 
  30   { "label" => "KOI8-R",         "dbencoding" => "KOI8",      "charset" => "KOI8-R" },
 
  31   { "label" => "Windows CP1251", "dbencoding" => "WIN",       "charset" => "CP1251" },
 
  32   { "label" => "Windows CP866",  "dbencoding" => "ALT",       "charset" => "CP866" },
 
  35 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
 
  36 %charset_to_db_encoding = map { $_->{charset}, $_->{dbencoding} } @db_encodings;
 
  38 use constant DEFAULT_CHARSET => 'ISO-8859-15';
 
  41   my ($a, $b) = gettimeofday();
 
  42   return "${a}-${b}-${$}";
 
  46   return "/tmp/lx-office-tmp-" . unique_id();
 
  50   $main::lxdebug->enter_sub();
 
  52   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  54   my $dbh = $form->dbconnect($myconfig);
 
  56   my (@filter_values, $filter);
 
  58   foreach (qw(partnumber description ean)) {
 
  59     next unless $form->{$_};
 
  61     $filter .= qq| AND ($_ ILIKE ?)|;
 
  62     push @filter_values, '%' . $form->{$_} . '%';
 
  65   if ($form->{no_assemblies}) {
 
  66     $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
 
  68   if ($form->{assemblies}) {
 
  69     $filter .= qq| AND assembly=TRUE|;
 
  72   if ($form->{no_services}) {
 
  73     $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|; # @mb hier nochmal optimieren ... nach kurzer ruecksprache alles i.o.
 
  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, ean | .
 
  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_gender, 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_gender, 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 = ?') ! .
 
 291     qq!ORDER BY $order_by $order_dir!;
 
 292   push @filter_values, $::locale->{iconv_utf8}->convert('Händler');
 
 293   my $sth = $dbh->prepare($query);
 
 294   $sth->execute(@filter_values) ||
 
 295     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 297   while (my $ref = $sth->fetchrow_hashref()) {
 
 298     push(@{$vendors}, $ref);
 
 303   $main::lxdebug->leave_sub();
 
 308 sub mkdir_with_parents {
 
 309   $main::lxdebug->enter_sub();
 
 311   my ($full_path) = @_;
 
 315   $full_path =~ s|/+|/|;
 
 317   foreach my $part (split(m|/|, $full_path)) {
 
 318     $path .= "/" if ($path);
 
 321     die("Could not create directory '$path' because a file exists with " .
 
 322         "the same name.\n") if (-f $path);
 
 325       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
 
 330   $main::lxdebug->leave_sub();
 
 334   $main::lxdebug->enter_sub();
 
 338   return $main::lxdebug->leave_sub()
 
 339     unless ($main::webdav && $form->{id});
 
 343   $form->{WEBDAV} = [];
 
 345   if ($form->{type} eq "sales_quotation") {
 
 346     ($path, $number) = ("angebote", $form->{quonumber});
 
 347   } elsif ($form->{type} eq "sales_order") {
 
 348     ($path, $number) = ("bestellungen", $form->{ordnumber});
 
 349   } elsif ($form->{type} eq "request_quotation") {
 
 350     ($path, $number) = ("anfragen", $form->{quonumber});
 
 351   } elsif ($form->{type} eq "purchase_order") {
 
 352     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
 
 353   } elsif ($form->{type} eq "sales_delivery_order") {
 
 354     ($path, $number) = ("verkaufslieferscheine", $form->{donumber});
 
 355   } elsif ($form->{type} eq "purchase_delivery_order") {
 
 356     ($path, $number) = ("einkaufslieferscheine", $form->{donumber});
 
 357   } elsif ($form->{type} eq "credit_note") {
 
 358     ($path, $number) = ("gutschriften", $form->{invnumber});
 
 359   } elsif ($form->{vc} eq "customer") {
 
 360     ($path, $number) = ("rechnungen", $form->{invnumber});
 
 362     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
 
 365   return $main::lxdebug->leave_sub() unless ($path && $number);
 
 367   $number =~ s|[/\\]|_|g;
 
 369   $path = "webdav/${path}/${number}";
 
 372     mkdir_with_parents($path);
 
 375     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
 
 376     $base_path =~ s|[^/]+$||;
 
 377     $base_path =~ s|/$||;
 
 378     # wo kommt der wert für dir her? es wird doch gar nichts übergeben? fix für strict my $dir jb 21.2.
 
 379     if (opendir my $dir, $path) {
 
 380       foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
 
 381         next if (($file eq '.') || ($file eq '..'));
 
 386         my $is_directory = -d "$path/$file";
 
 388         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
 
 389         $file .=  '/' if ($is_directory);
 
 391         push @{ $form->{WEBDAV} }, {
 
 393           'link' => "$base_path/$file",
 
 394           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
 
 402   $main::lxdebug->leave_sub();
 
 406   $main::lxdebug->enter_sub();
 
 408   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
 
 410   $vc = $vc eq "customer" ? "customer" : "vendor";
 
 412   my $dbh = $form->dbconnect($myconfig);
 
 419          pt.description AS payment_terms,
 
 420          b.description AS business,
 
 421          l.description AS language
 
 423        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
 
 424        LEFT JOIN business b ON (vc.business_id = b.id)
 
 425        LEFT JOIN language l ON (vc.language_id = l.id)
 
 427   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
 
 431     $main::lxdebug->leave_sub();
 
 435   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 437   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
 
 439   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
 
 440   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 442   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
 
 443   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 447   $main::lxdebug->leave_sub();
 
 452 sub get_shipto_by_id {
 
 453   $main::lxdebug->enter_sub();
 
 455   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
 
 459   my $dbh = $form->dbconnect($myconfig);
 
 461   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 462   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
 
 464   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
 
 468   $main::lxdebug->leave_sub();
 
 471 sub save_email_status {
 
 472   $main::lxdebug->enter_sub();
 
 474   my ($self, $myconfig, $form) = @_;
 
 476   my ($table, $query, $dbh);
 
 478   if ($form->{script} eq 'oe.pl') {
 
 481   } elsif ($form->{script} eq 'is.pl') {
 
 484   } elsif ($form->{script} eq 'ir.pl') {
 
 489   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
 
 491   $dbh = $form->get_standard_dbh($myconfig);
 
 493   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
 
 495   $intnotes =~ s|\r||g;
 
 496   $intnotes =~ s|\n$||;
 
 498   $intnotes .= "\n\n" if ($intnotes);
 
 500   my $cc  = $main::locale->text('Cc') . ": $form->{cc}\n"   if $form->{cc};
 
 501   my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
 
 502   my $now = scalar localtime;
 
 504   $intnotes .= $main::locale->text('[email]') . "\n"
 
 505     . $main::locale->text('Date') . ": $now\n"
 
 506     . $main::locale->text('To (email)') . ": $form->{email}\n"
 
 508     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
 
 509     . $main::locale->text('Message') . ": $form->{message}";
 
 511   $intnotes =~ s|\r||g;
 
 513   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
 
 515   $form->save_status($dbh);
 
 519   $main::lxdebug->leave_sub();
 
 525   foreach my $key (@_) {
 
 526     if ((ref $key eq '') && !defined $params->{$key}) {
 
 527       my $subroutine = (caller(1))[3];
 
 528       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
 
 529       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
 
 530       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 532     } elsif (ref $key eq 'ARRAY') {
 
 534       foreach my $subkey (@{ $key }) {
 
 535         if (defined $params->{$subkey}) {
 
 542         my $subroutine = (caller(1))[3];
 
 543         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
 
 544         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
 
 545         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
 
 554   foreach my $key (@_) {
 
 555     if ((ref $key eq '') && !exists $params->{$key}) {
 
 556       my $subroutine = (caller(1))[3];
 
 557       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 559     } elsif (ref $key eq 'ARRAY') {
 
 561       foreach my $subkey (@{ $key }) {
 
 562         if (exists $params->{$subkey}) {
 
 569         my $subroutine = (caller(1))[3];
 
 570         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));