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/kivitendo-tmp-" . unique_id();
 
  50   my ($text, %params) = @_;
 
  53   $params{at}         =  3 if 3 > $params{at};
 
  55   $params{strip}    //= '';
 
  57   $text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x;
 
  58   $text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?:     newlines? | full )$/x;
 
  60   return $text if length($text) <= $params{at};
 
  61   return substr($text, 0, $params{at} - 3) . '...';
 
  65   $main::lxdebug->enter_sub();
 
  67   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  69   my $dbh = $form->dbconnect($myconfig);
 
  71   my (@filter_values, $filter);
 
  73   foreach (qw(partnumber description ean)) {
 
  74     next unless $form->{$_};
 
  76     $filter .= qq| AND ($_ ILIKE ?)|;
 
  77     push @filter_values, '%' . $form->{$_} . '%';
 
  80   if ($form->{no_assemblies}) {
 
  81     $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
 
  83   if ($form->{assemblies}) {
 
  84     $filter .= qq| AND assembly=TRUE|;
 
  87   if ($form->{no_services}) {
 
  88     $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|; # @mb hier nochmal optimieren ... nach kurzer ruecksprache alles i.o.
 
  91   substr($filter, 1, 3) = "WHERE" if ($filter);
 
  93   $order_by =~ s/[^a-zA-Z_]//g;
 
  94   $order_dir = $order_dir ? "ASC" : "DESC";
 
  97     qq|SELECT id, partnumber, description, ean | .
 
  98     qq|FROM parts $filter | .
 
  99     qq|ORDER BY $order_by $order_dir|;
 
 100   my $sth = $dbh->prepare($query);
 
 101   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 103   while (my $ref = $sth->fetchrow_hashref()) {
 
 104     push(@{$parts}, $ref);
 
 109   $main::lxdebug->leave_sub();
 
 114 sub retrieve_projects {
 
 115   $main::lxdebug->enter_sub();
 
 117   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 119   my $dbh = $form->dbconnect($myconfig);
 
 121   my (@filter_values, $filter);
 
 122   if ($form->{"projectnumber"}) {
 
 123     $filter .= qq| AND (projectnumber ILIKE ?)|;
 
 124     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
 
 126   if ($form->{"description"}) {
 
 127     $filter .= qq| AND (description ILIKE ?)|;
 
 128     push(@filter_values, '%' . $form->{"description"} . '%');
 
 130   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 132   $order_by =~ s/[^a-zA-Z_]//g;
 
 133   $order_dir = $order_dir ? "ASC" : "DESC";
 
 136     qq|SELECT id, projectnumber, description | .
 
 137     qq|FROM project $filter | .
 
 138     qq|ORDER BY $order_by $order_dir|;
 
 139   my $sth = $dbh->prepare($query);
 
 140   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 142   while (my $ref = $sth->fetchrow_hashref()) {
 
 143     push(@{$projects}, $ref);
 
 148   $main::lxdebug->leave_sub();
 
 153 sub retrieve_employees {
 
 154   $main::lxdebug->enter_sub();
 
 156   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 158   my $dbh = $form->dbconnect($myconfig);
 
 160   my (@filter_values, $filter);
 
 161   if ($form->{"name"}) {
 
 162     $filter .= qq| AND (name ILIKE ?)|;
 
 163     push(@filter_values, '%' . $form->{"name"} . '%');
 
 165   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 167   $order_by =~ s/[^a-zA-Z_]//g;
 
 168   $order_dir = $order_dir ? "ASC" : "DESC";
 
 171     qq|SELECT id, name | .
 
 172     qq|FROM employee $filter | .
 
 173     qq|ORDER BY $order_by $order_dir|;
 
 174   my $sth = $dbh->prepare($query);
 
 175   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 177   while (my $ref = $sth->fetchrow_hashref()) {
 
 178     push(@{$employees}, $ref);
 
 183   $main::lxdebug->leave_sub();
 
 188 sub retrieve_customers_or_vendors {
 
 189   $main::lxdebug->enter_sub();
 
 191   my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
 
 193   my $dbh = $form->dbconnect($myconfig);
 
 195   my (@filter_values, $filter);
 
 196   if ($form->{"name"}) {
 
 197     $filter .= " AND (TABLE.name ILIKE ?)";
 
 198     push(@filter_values, '%' . $form->{"name"} . '%');
 
 200   if (!$form->{"obsolete"}) {
 
 201     $filter .= " AND NOT TABLE.obsolete";
 
 203   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 205   $order_by =~ s/[^a-zA-Z_]//g;
 
 206   $order_dir = $order_dir ? "ASC" : "DESC";
 
 208   my (@queries, @query_parameters);
 
 210   if ($allow_both || !$is_vendor) {
 
 211     my $c_filter = $filter;
 
 212     $c_filter =~ s/TABLE/c/g;
 
 213     push(@queries, qq|SELECT
 
 214                         c.id, c.name, 0 AS customer_is_vendor,
 
 215                         c.street, c.zipcode, c.city,
 
 216                         ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
 
 218                       LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
 
 220     push(@query_parameters, @filter_values);
 
 223   if ($allow_both || $is_vendor) {
 
 224     my $v_filter = $filter;
 
 225     $v_filter =~ s/TABLE/v/g;
 
 226     push(@queries, qq|SELECT
 
 227                         v.id, v.name, 1 AS customer_is_vendor,
 
 228                         v.street, v.zipcode, v.city,
 
 229                         ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
 
 231                       LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
 
 233     push(@query_parameters, @filter_values);
 
 236   my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
 
 237   my $sth = $dbh->prepare($query);
 
 238   $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
 
 240   while (my $ref = $sth->fetchrow_hashref()) {
 
 241     push(@{$customers}, $ref);
 
 246   $main::lxdebug->leave_sub();
 
 251 sub retrieve_delivery_customer {
 
 252   $main::lxdebug->enter_sub();
 
 254   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 256   my $dbh = $form->dbconnect($myconfig);
 
 258   my (@filter_values, $filter);
 
 259   if ($form->{"name"}) {
 
 260     $filter .= qq| (name ILIKE ?) AND|;
 
 261     push(@filter_values, '%' . $form->{"name"} . '%');
 
 264   $order_by =~ s/[^a-zA-Z_]//g;
 
 265   $order_dir = $order_dir ? "ASC" : "DESC";
 
 268     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
 
 270     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
 
 271     qq!ORDER BY $order_by $order_dir!;
 
 272   my $sth = $dbh->prepare($query);
 
 273   $sth->execute(@filter_values) ||
 
 274     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 275   my $delivery_customers = [];
 
 276   while (my $ref = $sth->fetchrow_hashref()) {
 
 277     push(@{$delivery_customers}, $ref);
 
 282   $main::lxdebug->leave_sub();
 
 284   return $delivery_customers;
 
 287 sub retrieve_vendor {
 
 288   $main::lxdebug->enter_sub();
 
 290   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 292   my $dbh = $form->dbconnect($myconfig);
 
 294   my (@filter_values, $filter);
 
 295   if ($form->{"name"}) {
 
 296     $filter .= qq| (name ILIKE ?) AND|;
 
 297     push(@filter_values, '%' . $form->{"name"} . '%');
 
 300   $order_by =~ s/[^a-zA-Z_]//g;
 
 301   $order_dir = $order_dir ? "ASC" : "DESC";
 
 304     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
 
 305     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = ?') ! .
 
 306     qq!ORDER BY $order_by $order_dir!;
 
 307   push @filter_values, $::locale->{iconv_utf8}->convert('Händler');
 
 308   my $sth = $dbh->prepare($query);
 
 309   $sth->execute(@filter_values) ||
 
 310     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 312   while (my $ref = $sth->fetchrow_hashref()) {
 
 313     push(@{$vendors}, $ref);
 
 318   $main::lxdebug->leave_sub();
 
 323 sub mkdir_with_parents {
 
 324   $main::lxdebug->enter_sub();
 
 326   my ($full_path) = @_;
 
 330   $full_path =~ s|/+|/|;
 
 332   foreach my $part (split(m|/|, $full_path)) {
 
 333     $path .= "/" if ($path);
 
 336     die("Could not create directory '$path' because a file exists with " .
 
 337         "the same name.\n") if (-f $path);
 
 340       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
 
 345   $main::lxdebug->leave_sub();
 
 349   $main::lxdebug->enter_sub();
 
 353   return $main::lxdebug->leave_sub()
 
 354     unless ($::lx_office_conf{features}->{webdav} && $form->{id});
 
 358   $form->{WEBDAV} = [];
 
 360   if ($form->{type} eq "sales_quotation") {
 
 361     ($path, $number) = ("angebote", $form->{quonumber});
 
 362   } elsif ($form->{type} eq "sales_order") {
 
 363     ($path, $number) = ("bestellungen", $form->{ordnumber});
 
 364   } elsif ($form->{type} eq "request_quotation") {
 
 365     ($path, $number) = ("anfragen", $form->{quonumber});
 
 366   } elsif ($form->{type} eq "purchase_order") {
 
 367     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
 
 368   } elsif ($form->{type} eq "sales_delivery_order") {
 
 369     ($path, $number) = ("verkaufslieferscheine", $form->{donumber});
 
 370   } elsif ($form->{type} eq "purchase_delivery_order") {
 
 371     ($path, $number) = ("einkaufslieferscheine", $form->{donumber});
 
 372   } elsif ($form->{type} eq "credit_note") {
 
 373     ($path, $number) = ("gutschriften", $form->{invnumber});
 
 374   } elsif ($form->{vc} eq "customer") {
 
 375     ($path, $number) = ("rechnungen", $form->{invnumber});
 
 377     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
 
 380   return $main::lxdebug->leave_sub() unless ($path && $number);
 
 382   $number =~ s|[/\\]|_|g;
 
 384   $path = "webdav/${path}/${number}";
 
 387     mkdir_with_parents($path);
 
 390     my $base_path = $ENV{'SCRIPT_NAME'};
 
 391     $base_path =~ s|[^/]+$||;
 
 392     # wo kommt der wert für dir her? es wird doch gar nichts übergeben? fix für strict my $dir jb 21.2.
 
 393     if (opendir my $dir, $path) {
 
 394       foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
 
 395         next if (($file eq '.') || ($file eq '..'));
 
 400         my $is_directory = -d "$path/$file";
 
 402         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
 
 403         $file .=  '/' if ($is_directory);
 
 405         push @{ $form->{WEBDAV} }, {
 
 407           'link' => $base_path . $file,
 
 408           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
 
 416   $main::lxdebug->leave_sub();
 
 420   $main::lxdebug->enter_sub();
 
 422   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
 
 424   $vc = $vc eq "customer" ? "customer" : "vendor";
 
 426   my $dbh = $form->dbconnect($myconfig);
 
 433          pt.description AS payment_terms,
 
 434          b.description AS business,
 
 435          l.description AS language
 
 437        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
 
 438        LEFT JOIN business b ON (vc.business_id = b.id)
 
 439        LEFT JOIN language l ON (vc.language_id = l.id)
 
 441   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
 
 445     $main::lxdebug->leave_sub();
 
 449   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 451   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
 
 453   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
 
 454   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 456   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
 
 457   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 459   # Only show default pricegroup for customer, not vendor, which is why this is outside the main query
 
 460   ($form->{pricegroup}) = selectrow_query($form, $dbh, qq|SELECT pricegroup FROM pricegroup WHERE id = ?|, $form->{klass});
 
 464   $main::lxdebug->leave_sub();
 
 469 sub get_shipto_by_id {
 
 470   $main::lxdebug->enter_sub();
 
 472   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
 
 476   my $dbh = $form->dbconnect($myconfig);
 
 478   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 479   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
 
 481   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
 
 485   $main::lxdebug->leave_sub();
 
 488 sub save_email_status {
 
 489   $main::lxdebug->enter_sub();
 
 491   my ($self, $myconfig, $form) = @_;
 
 493   my ($table, $query, $dbh);
 
 495   if ($form->{script} eq 'oe.pl') {
 
 498   } elsif ($form->{script} eq 'is.pl') {
 
 501   } elsif ($form->{script} eq 'ir.pl') {
 
 506   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
 
 508   $dbh = $form->get_standard_dbh($myconfig);
 
 510   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
 
 512   $intnotes =~ s|\r||g;
 
 513   $intnotes =~ s|\n$||;
 
 515   $intnotes .= "\n\n" if ($intnotes);
 
 517   my $cc  = $form->{cc}  ? $main::locale->text('Cc') . ": $form->{cc}\n"   : '';
 
 518   my $bcc = $form->{bcc} ? $main::locale->text('Bcc') . ": $form->{bcc}\n" : '';
 
 519   my $now = scalar localtime;
 
 521   $intnotes .= $main::locale->text('[email]') . "\n"
 
 522     . $main::locale->text('Date') . ": $now\n"
 
 523     . $main::locale->text('To (email)') . ": $form->{email}\n"
 
 525     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
 
 526     . $main::locale->text('Message') . ": $form->{message}";
 
 528   $intnotes =~ s|\r||g;
 
 530   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
 
 532   $form->save_status($dbh);
 
 536   $main::lxdebug->leave_sub();
 
 542   foreach my $key (@_) {
 
 543     if ((ref $key eq '') && !defined $params->{$key}) {
 
 544       my $subroutine = (caller(1))[3];
 
 545       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
 
 546       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
 
 547       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 549     } elsif (ref $key eq 'ARRAY') {
 
 551       foreach my $subkey (@{ $key }) {
 
 552         if (defined $params->{$subkey}) {
 
 559         my $subroutine = (caller(1))[3];
 
 560         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
 
 561         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
 
 562         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
 
 571   foreach my $key (@_) {
 
 572     if ((ref $key eq '') && !exists $params->{$key}) {
 
 573       my $subroutine = (caller(1))[3];
 
 574       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 576     } elsif (ref $key eq 'ARRAY') {
 
 578       foreach my $subkey (@{ $key }) {
 
 579         if (exists $params->{$subkey}) {
 
 586         my $subroutine = (caller(1))[3];
 
 587         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
 
 602 Common - Common routines used in a lot of places.
 
 606   my $short_text = Common::truncate($long_text, at => 10);
 
 612 =item C<truncate $text, %params>
 
 614 Truncates C<$text> at a position and insert an ellipsis if the text is
 
 615 longer. The maximum number of characters to return is given with the
 
 616 paramter C<at> which defaults to 50.
 
 618 The optional parameter C<strip> can be used to remove unwanted line
 
 619 feed/carriage return characters from the text before truncation. It
 
 620 can be set to C<1> (only strip those at the end of C<$text>) or
 
 621 C<full> (replace consecutive line feed/carriage return characters in
 
 622 the middle by a single space and remove tailing line feed/carriage
 
 633 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
 
 634 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>