1 #====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #====================================================================
 
  15 use Time::HiRes qw(gettimeofday);
 
  20 use vars qw(@db_encodings %db_encoding_to_charset %charset_to_db_encoding);
 
  23   { "label" => "ASCII",          "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
 
  24   { "label" => "UTF-8 Unicode",  "dbencoding" => "UNICODE",   "charset" => "UTF-8" },
 
  25   { "label" => "ISO 8859-1",     "dbencoding" => "LATIN1",    "charset" => "ISO-8859-1" },
 
  26   { "label" => "ISO 8859-2",     "dbencoding" => "LATIN2",    "charset" => "ISO-8859-2" },
 
  27   { "label" => "ISO 8859-3",     "dbencoding" => "LATIN3",    "charset" => "ISO-8859-3" },
 
  28   { "label" => "ISO 8859-4",     "dbencoding" => "LATIN4",    "charset" => "ISO-8859-4" },
 
  29   { "label" => "ISO 8859-5",     "dbencoding" => "LATIN5",    "charset" => "ISO-8859-5" },
 
  30   { "label" => "ISO 8859-15",    "dbencoding" => "LATIN9",    "charset" => "ISO-8859-15" },
 
  31   { "label" => "KOI8-R",         "dbencoding" => "KOI8",      "charset" => "KOI8-R" },
 
  32   { "label" => "Windows CP1251", "dbencoding" => "WIN",       "charset" => "CP1251" },
 
  33   { "label" => "Windows CP866",  "dbencoding" => "ALT",       "charset" => "CP866" },
 
  36 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
 
  37 %charset_to_db_encoding = map { $_->{charset}, $_->{dbencoding} } @db_encodings;
 
  39 use constant DEFAULT_CHARSET => 'ISO-8859-15';
 
  42   my ($a, $b) = gettimeofday();
 
  43   return "${a}-${b}-${$}";
 
  47   return "/tmp/kivitendo-tmp-" . unique_id();
 
  51   my ($text, %params) = @_;
 
  54   $params{at}         =  3 if 3 > $params{at};
 
  56   $params{strip}    //= '';
 
  58   $text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x;
 
  59   $text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?:     newlines? | full )$/x;
 
  61   return $text if length($text) <= $params{at};
 
  62   return substr($text, 0, $params{at} - 3) . '...';
 
  66   $main::lxdebug->enter_sub();
 
  68   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
  70   my $dbh = $form->dbconnect($myconfig);
 
  72   my (@filter_values, $filter);
 
  74   foreach (qw(partnumber description ean)) {
 
  75     next unless $form->{$_};
 
  77     $filter .= qq| AND ($_ ILIKE ?)|;
 
  78     push @filter_values, '%' . $form->{$_} . '%';
 
  81   if ($form->{no_assemblies}) {
 
  82     $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
 
  84   if ($form->{assemblies}) {
 
  85     $filter .= qq| AND assembly=TRUE|;
 
  88   if ($form->{no_services}) {
 
  89     $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|;
 
  92   substr($filter, 1, 3) = "WHERE" if ($filter);
 
  94   $order_by =~ s/[^a-zA-Z_]//g;
 
  95   $order_dir = $order_dir ? "ASC" : "DESC";
 
  98     qq|SELECT id, partnumber, description, ean, | .
 
  99     qq|       warehouse_id, bin_id | .
 
 100     qq|FROM parts $filter | .
 
 101     qq|ORDER BY $order_by $order_dir|;
 
 102   my $sth = $dbh->prepare($query);
 
 103   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 105   while (my $ref = $sth->fetchrow_hashref()) {
 
 106     push(@{$parts}, $ref);
 
 111   $main::lxdebug->leave_sub();
 
 116 sub retrieve_projects {
 
 117   $main::lxdebug->enter_sub();
 
 119   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 121   my $dbh = $form->dbconnect($myconfig);
 
 123   my (@filter_values, $filter);
 
 124   if ($form->{"projectnumber"}) {
 
 125     $filter .= qq| AND (projectnumber ILIKE ?)|;
 
 126     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
 
 128   if ($form->{"description"}) {
 
 129     $filter .= qq| AND (description ILIKE ?)|;
 
 130     push(@filter_values, '%' . $form->{"description"} . '%');
 
 132   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 134   $order_by =~ s/[^a-zA-Z_]//g;
 
 135   $order_dir = $order_dir ? "ASC" : "DESC";
 
 138     qq|SELECT id, projectnumber, description | .
 
 139     qq|FROM project $filter | .
 
 140     qq|ORDER BY $order_by $order_dir|;
 
 141   my $sth = $dbh->prepare($query);
 
 142   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 144   while (my $ref = $sth->fetchrow_hashref()) {
 
 145     push(@{$projects}, $ref);
 
 150   $main::lxdebug->leave_sub();
 
 155 sub retrieve_employees {
 
 156   $main::lxdebug->enter_sub();
 
 158   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 160   my $dbh = $form->dbconnect($myconfig);
 
 162   my (@filter_values, $filter);
 
 163   if ($form->{"name"}) {
 
 164     $filter .= qq| AND (name ILIKE ?)|;
 
 165     push(@filter_values, '%' . $form->{"name"} . '%');
 
 167   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 169   $order_by =~ s/[^a-zA-Z_]//g;
 
 170   $order_dir = $order_dir ? "ASC" : "DESC";
 
 173     qq|SELECT id, name | .
 
 174     qq|FROM employee $filter | .
 
 175     qq|ORDER BY $order_by $order_dir|;
 
 176   my $sth = $dbh->prepare($query);
 
 177   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 179   while (my $ref = $sth->fetchrow_hashref()) {
 
 180     push(@{$employees}, $ref);
 
 185   $main::lxdebug->leave_sub();
 
 190 sub retrieve_customers_or_vendors {
 
 191   $main::lxdebug->enter_sub();
 
 193   my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
 
 195   my $dbh = $form->dbconnect($myconfig);
 
 197   my (@filter_values, $filter);
 
 198   if ($form->{"name"}) {
 
 199     $filter .= " AND (TABLE.name ILIKE ?)";
 
 200     push(@filter_values, '%' . $form->{"name"} . '%');
 
 202   if (!$form->{"obsolete"}) {
 
 203     $filter .= " AND NOT TABLE.obsolete";
 
 205   substr($filter, 1, 3) = "WHERE" if ($filter);
 
 207   $order_by =~ s/[^a-zA-Z_]//g;
 
 208   $order_dir = $order_dir ? "ASC" : "DESC";
 
 210   my (@queries, @query_parameters);
 
 212   if ($allow_both || !$is_vendor) {
 
 213     my $c_filter = $filter;
 
 214     $c_filter =~ s/TABLE/c/g;
 
 215     push(@queries, qq|SELECT
 
 216                         c.id, c.name, 0 AS customer_is_vendor,
 
 217                         c.street, c.zipcode, c.city,
 
 218                         ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
 
 220                       LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
 
 222     push(@query_parameters, @filter_values);
 
 225   if ($allow_both || $is_vendor) {
 
 226     my $v_filter = $filter;
 
 227     $v_filter =~ s/TABLE/v/g;
 
 228     push(@queries, qq|SELECT
 
 229                         v.id, v.name, 1 AS customer_is_vendor,
 
 230                         v.street, v.zipcode, v.city,
 
 231                         ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
 
 233                       LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
 
 235     push(@query_parameters, @filter_values);
 
 238   my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
 
 239   my $sth = $dbh->prepare($query);
 
 240   $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
 
 242   while (my $ref = $sth->fetchrow_hashref()) {
 
 243     push(@{$customers}, $ref);
 
 248   $main::lxdebug->leave_sub();
 
 253 sub retrieve_delivery_customer {
 
 254   $main::lxdebug->enter_sub();
 
 256   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 258   my $dbh = $form->dbconnect($myconfig);
 
 260   my (@filter_values, $filter);
 
 261   if ($form->{"name"}) {
 
 262     $filter .= qq| (name ILIKE ?) AND|;
 
 263     push(@filter_values, '%' . $form->{"name"} . '%');
 
 266   $order_by =~ s/[^a-zA-Z_]//g;
 
 267   $order_dir = $order_dir ? "ASC" : "DESC";
 
 270     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
 
 272     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
 
 273     qq!ORDER BY $order_by $order_dir!;
 
 274   my $sth = $dbh->prepare($query);
 
 275   $sth->execute(@filter_values) ||
 
 276     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 277   my $delivery_customers = [];
 
 278   while (my $ref = $sth->fetchrow_hashref()) {
 
 279     push(@{$delivery_customers}, $ref);
 
 284   $main::lxdebug->leave_sub();
 
 286   return $delivery_customers;
 
 289 sub retrieve_vendor {
 
 290   $main::lxdebug->enter_sub();
 
 292   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
 
 294   my $dbh = $form->dbconnect($myconfig);
 
 296   my (@filter_values, $filter);
 
 297   if ($form->{"name"}) {
 
 298     $filter .= qq| (name ILIKE ?) AND|;
 
 299     push(@filter_values, '%' . $form->{"name"} . '%');
 
 302   $order_by =~ s/[^a-zA-Z_]//g;
 
 303   $order_dir = $order_dir ? "ASC" : "DESC";
 
 306     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
 
 307     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = ?') ! .
 
 308     qq!ORDER BY $order_by $order_dir!;
 
 309   push @filter_values, $::locale->{iconv_utf8}->convert('Händler');
 
 310   my $sth = $dbh->prepare($query);
 
 311   $sth->execute(@filter_values) ||
 
 312     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
 
 314   while (my $ref = $sth->fetchrow_hashref()) {
 
 315     push(@{$vendors}, $ref);
 
 320   $main::lxdebug->leave_sub();
 
 325 sub mkdir_with_parents {
 
 326   $main::lxdebug->enter_sub();
 
 328   my ($full_path) = @_;
 
 332   $full_path =~ s|/+|/|;
 
 334   foreach my $part (split(m|/|, $full_path)) {
 
 335     $path .= "/" if ($path);
 
 338     die("Could not create directory '$path' because a file exists with " .
 
 339         "the same name.\n") if (-f $path);
 
 342       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
 
 347   $main::lxdebug->leave_sub();
 
 351   $main::lxdebug->enter_sub();
 
 355   return $main::lxdebug->leave_sub()
 
 356     unless ($::lx_office_conf{features}->{webdav} && $form->{id});
 
 358   croak "No client set in \$::auth" unless $::auth->client;
 
 362   $form->{WEBDAV} = [];
 
 364   if ($form->{type} eq "sales_quotation") {
 
 365     ($path, $number) = ("angebote", $form->{quonumber});
 
 366   } elsif ($form->{type} eq "sales_order") {
 
 367     ($path, $number) = ("bestellungen", $form->{ordnumber});
 
 368   } elsif ($form->{type} eq "request_quotation") {
 
 369     ($path, $number) = ("anfragen", $form->{quonumber});
 
 370   } elsif ($form->{type} eq "purchase_order") {
 
 371     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
 
 372   } elsif ($form->{type} eq "sales_delivery_order") {
 
 373     ($path, $number) = ("verkaufslieferscheine", $form->{donumber});
 
 374   } elsif ($form->{type} eq "purchase_delivery_order") {
 
 375     ($path, $number) = ("einkaufslieferscheine", $form->{donumber});
 
 376   } elsif ($form->{type} eq "credit_note") {
 
 377     ($path, $number) = ("gutschriften", $form->{invnumber});
 
 378   } elsif ($form->{vc} eq "customer") {
 
 379     ($path, $number) = ("rechnungen", $form->{invnumber});
 
 381     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
 
 384   return $main::lxdebug->leave_sub() unless ($path && $number);
 
 386   $number =~ s|[/\\]|_|g;
 
 388   $path = "webdav/" . $::auth->client->{id} . "/${path}/${number}";
 
 391     mkdir_with_parents($path);
 
 394     my $base_path = $ENV{'SCRIPT_NAME'};
 
 395     $base_path =~ s|[^/]+$||;
 
 396     if (opendir my $dir, $path) {
 
 397       foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
 
 398         next if (($file eq '.') || ($file eq '..'));
 
 403         my $is_directory = -d "$path/$file";
 
 405         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
 
 406         $file .=  '/' if ($is_directory);
 
 408         push @{ $form->{WEBDAV} }, {
 
 410           'link' => $base_path . $file,
 
 411           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
 
 419   $main::lxdebug->leave_sub();
 
 423   $main::lxdebug->enter_sub();
 
 425   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
 
 427   $vc = $vc eq "customer" ? "customer" : "vendor";
 
 429   my $dbh = $form->dbconnect($myconfig);
 
 436          pt.description AS payment_terms,
 
 437          b.description AS business,
 
 438          l.description AS language
 
 440        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
 
 441        LEFT JOIN business b ON (vc.business_id = b.id)
 
 442        LEFT JOIN language l ON (vc.language_id = l.id)
 
 444   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
 
 448     $main::lxdebug->leave_sub();
 
 452   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 454   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
 
 456   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
 
 457   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 459   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
 
 460   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
 462   # Only show default pricegroup for customer, not vendor, which is why this is outside the main query
 
 463   ($form->{pricegroup}) = selectrow_query($form, $dbh, qq|SELECT pricegroup FROM pricegroup WHERE id = ?|, $form->{klass});
 
 467   $main::lxdebug->leave_sub();
 
 472 sub get_shipto_by_id {
 
 473   $main::lxdebug->enter_sub();
 
 475   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
 
 479   my $dbh = $form->dbconnect($myconfig);
 
 481   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 482   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
 
 484   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
 
 488   $main::lxdebug->leave_sub();
 
 491 sub save_email_status {
 
 492   $main::lxdebug->enter_sub();
 
 494   my ($self, $myconfig, $form) = @_;
 
 496   my ($table, $query, $dbh);
 
 498   if ($form->{script} eq 'oe.pl') {
 
 501   } elsif ($form->{script} eq 'is.pl') {
 
 504   } elsif ($form->{script} eq 'ir.pl') {
 
 507   } elsif ($form->{script} eq 'do.pl') {
 
 508     $table = 'delivery_orders';
 
 511   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
 
 513   $dbh = $form->get_standard_dbh($myconfig);
 
 515   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
 
 517   $intnotes =~ s|\r||g;
 
 518   $intnotes =~ s|\n$||;
 
 520   $intnotes .= "\n\n" if ($intnotes);
 
 522   my $cc  = $form->{cc}  ? $main::locale->text('Cc') . ": $form->{cc}\n"   : '';
 
 523   my $bcc = $form->{bcc} ? $main::locale->text('Bcc') . ": $form->{bcc}\n" : '';
 
 524   my $now = scalar localtime;
 
 526   $intnotes .= $main::locale->text('[email]') . "\n"
 
 527     . $main::locale->text('Date') . ": $now\n"
 
 528     . $main::locale->text('To (email)') . ": $form->{email}\n"
 
 530     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
 
 531     . $main::locale->text('Message') . ": $form->{message}";
 
 533   $intnotes =~ s|\r||g;
 
 535   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
 
 537   $form->save_status($dbh);
 
 541   $main::lxdebug->leave_sub();
 
 547   foreach my $key (@_) {
 
 548     if ((ref $key eq '') && !defined $params->{$key}) {
 
 549       my $subroutine = (caller(1))[3];
 
 550       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
 
 551       $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
 
 552       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 554     } elsif (ref $key eq 'ARRAY') {
 
 556       foreach my $subkey (@{ $key }) {
 
 557         if (defined $params->{$subkey}) {
 
 564         my $subroutine = (caller(1))[3];
 
 565         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
 
 566         $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
 
 567         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
 
 576   foreach my $key (@_) {
 
 577     if ((ref $key eq '') && !exists $params->{$key}) {
 
 578       my $subroutine = (caller(1))[3];
 
 579       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
 
 581     } elsif (ref $key eq 'ARRAY') {
 
 583       foreach my $subkey (@{ $key }) {
 
 584         if (exists $params->{$subkey}) {
 
 591         my $subroutine = (caller(1))[3];
 
 592         $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
 
 607 Common - Common routines used in a lot of places.
 
 611   my $short_text = Common::truncate($long_text, at => 10);
 
 617 =item C<truncate $text, %params>
 
 619 Truncates C<$text> at a position and insert an ellipsis if the text is
 
 620 longer. The maximum number of characters to return is given with the
 
 621 paramter C<at> which defaults to 50.
 
 623 The optional parameter C<strip> can be used to remove unwanted line
 
 624 feed/carriage return characters from the text before truncation. It
 
 625 can be set to C<1> (only strip those at the end of C<$text>) or
 
 626 C<full> (replace consecutive line feed/carriage return characters in
 
 627 the middle by a single space and remove tailing line feed/carriage
 
 638 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
 
 639 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>