kivitendo/SL/Common.pm @ 5ad7e1ed
54e4131e | Moritz Bunkus | #====================================================================
|
||
# LX-Office ERP
|
||||
# Copyright (C) 2004
|
||||
# Based on SQL-Ledger Version 2.1.9
|
||||
# Web http://www.lx-office.org
|
||||
#
|
||||
#====================================================================
|
||||
package Common;
|
||||
05c6840d | Moritz Bunkus | use utf8;
|
||
use strict;
|
||||
895309b3 | Moritz Bunkus | use Carp;
|
||
0614d6da | Moritz Bunkus | use English qw(-no_match_vars);
|
||
b61db412 | Moritz Bunkus | use Time::HiRes qw(gettimeofday);
|
||
ff296f5f | Sven Schöling | use Data::Dumper;
|
||
4b17528a | Moritz Bunkus | use File::Copy ();
|
||
9a578c8b | Jan Büren | use File::stat;
|
||
use File::Slurp;
|
||||
4b17528a | Moritz Bunkus | use File::Spec;
|
||
a5c7ef23 | Moritz Bunkus | use List::MoreUtils qw(apply);
|
||
0614d6da | Moritz Bunkus | use POSIX ();
|
||
ac84f80b | Sven Schöling | use Encode qw(decode);
|
||
faef45c2 | Moritz Bunkus | |||
fb4d2ffa | Moritz Bunkus | use SL::DBUtils;
|
||
b61db412 | Moritz Bunkus | sub unique_id {
|
||
my ($a, $b) = gettimeofday();
|
||||
return "${a}-${b}-${$}";
|
||||
}
|
||||
sub tmpname {
|
||||
65867e61 | Sven Schöling | return "/tmp/kivitendo-tmp-" . unique_id();
|
||
b61db412 | Moritz Bunkus | }
|
||
6794ddd4 | Moritz Bunkus | sub truncate {
|
||
my ($text, %params) = @_;
|
||||
$params{at} //= 50;
|
||||
$params{at} = 3 if 3 > $params{at};
|
||||
$params{strip} //= '';
|
||||
$text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x;
|
||||
$text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?: newlines? | full )$/x;
|
||||
return $text if length($text) <= $params{at};
|
||||
return substr($text, 0, $params{at} - 3) . '...';
|
||||
}
|
||||
54e4131e | Moritz Bunkus | sub retrieve_parts {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my (@filter_values, $filter);
|
||||
8c7e4493 | Moritz Bunkus | |||
16c66f61 | Geoffrey Richardson | foreach (qw(partnumber description ean)) {
|
||
8c7e4493 | Moritz Bunkus | next unless $form->{$_};
|
||
$filter .= qq| AND ($_ ILIKE ?)|;
|
||||
bc40bcab | Moritz Bunkus | push @filter_values, like($form->{$_});
|
||
8c7e4493 | Moritz Bunkus | }
|
||
if ($form->{no_assemblies}) {
|
||||
71b51e70 | Geoffrey Richardson | $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
|
||
54e4131e | Moritz Bunkus | }
|
||
e807eba3 | Geoffrey Richardson | if ($form->{assemblies}) {
|
||
16c66f61 | Geoffrey Richardson | $filter .= qq| AND assembly=TRUE|;
|
||
e807eba3 | Geoffrey Richardson | }
|
||
8c7e4493 | Moritz Bunkus | |||
if ($form->{no_services}) {
|
||||
82c4717d | Jan Büren | $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|;
|
||
54e4131e | Moritz Bunkus | }
|
||
8c7e4493 | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | substr($filter, 1, 3) = "WHERE" if ($filter);
|
||
$order_by =~ s/[^a-zA-Z_]//g;
|
||||
$order_dir = $order_dir ? "ASC" : "DESC";
|
||||
c0f83f3e | Moritz Bunkus | my $query =
|
||
82c4717d | Jan Büren | qq|SELECT id, partnumber, description, ean, | .
|
||
qq| warehouse_id, bin_id | .
|
||||
c0f83f3e | Moritz Bunkus | qq|FROM parts $filter | .
|
||
qq|ORDER BY $order_by $order_dir|;
|
||||
54e4131e | Moritz Bunkus | my $sth = $dbh->prepare($query);
|
||
$sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
|
||||
my $parts = [];
|
||||
while (my $ref = $sth->fetchrow_hashref()) {
|
||||
push(@{$parts}, $ref);
|
||||
}
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $parts;
|
||||
}
|
||||
91ab1ef6 | Sven Schöling | sub retrieve_customers_or_vendors {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my (@filter_values, $filter);
|
||||
if ($form->{"name"}) {
|
||||
$filter .= " AND (TABLE.name ILIKE ?)";
|
||||
bc40bcab | Moritz Bunkus | push(@filter_values, like($form->{"name"}));
|
||
91ab1ef6 | Sven Schöling | }
|
||
if (!$form->{"obsolete"}) {
|
||||
$filter .= " AND NOT TABLE.obsolete";
|
||||
}
|
||||
substr($filter, 1, 3) = "WHERE" if ($filter);
|
||||
$order_by =~ s/[^a-zA-Z_]//g;
|
||||
$order_dir = $order_dir ? "ASC" : "DESC";
|
||||
my (@queries, @query_parameters);
|
||||
if ($allow_both || !$is_vendor) {
|
||||
my $c_filter = $filter;
|
||||
$c_filter =~ s/TABLE/c/g;
|
||||
push(@queries, qq|SELECT
|
||||
c.id, c.name, 0 AS customer_is_vendor,
|
||||
c.street, c.zipcode, c.city,
|
||||
e09347c8 | Geoffrey Richardson | ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
|
||
91ab1ef6 | Sven Schöling | FROM customer c
|
||
LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
|
||||
$c_filter|);
|
||||
push(@query_parameters, @filter_values);
|
||||
}
|
||||
if ($allow_both || $is_vendor) {
|
||||
my $v_filter = $filter;
|
||||
$v_filter =~ s/TABLE/v/g;
|
||||
push(@queries, qq|SELECT
|
||||
v.id, v.name, 1 AS customer_is_vendor,
|
||||
v.street, v.zipcode, v.city,
|
||||
e09347c8 | Geoffrey Richardson | ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
|
||
91ab1ef6 | Sven Schöling | FROM vendor v
|
||
LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
|
||||
$v_filter|);
|
||||
push(@query_parameters, @filter_values);
|
||||
}
|
||||
my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
|
||||
my $sth = $dbh->prepare($query);
|
||||
$sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
|
||||
my $customers = [];
|
||||
while (my $ref = $sth->fetchrow_hashref()) {
|
||||
push(@{$customers}, $ref);
|
||||
}
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $customers;
|
||||
}
|
||||
54e4131e | Moritz Bunkus | sub retrieve_delivery_customer {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my (@filter_values, $filter);
|
||||
if ($form->{"name"}) {
|
||||
c0f83f3e | Moritz Bunkus | $filter .= qq| (name ILIKE ?) AND|;
|
||
bc40bcab | Moritz Bunkus | push(@filter_values, like($form->{"name"}));
|
||
54e4131e | Moritz Bunkus | }
|
||
$order_by =~ s/[^a-zA-Z_]//g;
|
||||
$order_dir = $order_dir ? "ASC" : "DESC";
|
||||
c0f83f3e | Moritz Bunkus | my $query =
|
||
qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
|
||||
qq!FROM customer ! .
|
||||
qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
|
||||
qq!ORDER BY $order_by $order_dir!;
|
||||
54e4131e | Moritz Bunkus | my $sth = $dbh->prepare($query);
|
||
c0f83f3e | Moritz Bunkus | $sth->execute(@filter_values) ||
|
||
$form->dberror($query . " (" . join(", ", @filter_values) . ")");
|
||||
54e4131e | Moritz Bunkus | my $delivery_customers = [];
|
||
while (my $ref = $sth->fetchrow_hashref()) {
|
||||
push(@{$delivery_customers}, $ref);
|
||||
}
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $delivery_customers;
|
||||
}
|
||||
sub retrieve_vendor {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my (@filter_values, $filter);
|
||||
if ($form->{"name"}) {
|
||||
c0f83f3e | Moritz Bunkus | $filter .= qq| (name ILIKE ?) AND|;
|
||
bc40bcab | Moritz Bunkus | push(@filter_values, like($form->{"name"}));
|
||
54e4131e | Moritz Bunkus | }
|
||
$order_by =~ s/[^a-zA-Z_]//g;
|
||||
$order_dir = $order_dir ? "ASC" : "DESC";
|
||||
c0f83f3e | Moritz Bunkus | my $query =
|
||
qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
|
||||
05c6840d | Moritz Bunkus | qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = ?') ! .
|
||
c0f83f3e | Moritz Bunkus | qq!ORDER BY $order_by $order_dir!;
|
||
05c6840d | Moritz Bunkus | push @filter_values, $::locale->{iconv_utf8}->convert('Händler');
|
||
54e4131e | Moritz Bunkus | my $sth = $dbh->prepare($query);
|
||
c0f83f3e | Moritz Bunkus | $sth->execute(@filter_values) ||
|
||
$form->dberror($query . " (" . join(", ", @filter_values) . ")");
|
||||
54e4131e | Moritz Bunkus | my $vendors = [];
|
||
while (my $ref = $sth->fetchrow_hashref()) {
|
||||
push(@{$vendors}, $ref);
|
||||
}
|
||||
$sth->finish();
|
||||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return $vendors;
|
||||
}
|
||||
8e206587 | Moritz Bunkus | sub mkdir_with_parents {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($full_path) = @_;
|
||||
my $path = "";
|
||||
$full_path =~ s|/+|/|;
|
||||
foreach my $part (split(m|/|, $full_path)) {
|
||||
$path .= "/" if ($path);
|
||||
$path .= $part;
|
||||
die("Could not create directory '$path' because a file exists with " .
|
||||
"the same name.\n") if (-f $path);
|
||||
if (! -d $path) {
|
||||
mkdir($path, 0770) || die("Could not create the directory '$path'. " .
|
||||
"OS error: $!\n");
|
||||
}
|
||||
}
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub webdav_folder {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($form) = @_;
|
||||
return $main::lxdebug->leave_sub()
|
||||
9a578c8b | Jan Büren | unless ($::instance_conf->get_webdav && $form->{id});
|
||
8e206587 | Moritz Bunkus | |||
895309b3 | Moritz Bunkus | |||
8e206587 | Moritz Bunkus | |||
7e2bf6ca | Moritz Bunkus | $form->{WEBDAV} = [];
|
||
8e206587 | Moritz Bunkus | |||
d4dd649f | Sven Schöling | my ($path, $number) = get_webdav_folder($form);
|
||
8e206587 | Moritz Bunkus | return $main::lxdebug->leave_sub() unless ($path && $number);
|
||
if (!-d $path) {
|
||||
mkdir_with_parents($path);
|
||||
} else {
|
||||
d4ea1fa8 | Sven Schöling | my $base_path = $ENV{'SCRIPT_NAME'};
|
||
8e206587 | Moritz Bunkus | $base_path =~ s|[^/]+$||;
|
||
e807eba3 | Geoffrey Richardson | if (opendir my $dir, $path) {
|
||
ac84f80b | Sven Schöling | foreach my $file (sort { lc $a cmp lc $b } map { decode("UTF-8", $_) } readdir $dir) {
|
||
e436a6b0 | Moritz Bunkus | next if (($file eq '.') || ($file eq '..'));
|
||
my $fname = $file;
|
||||
$fname =~ s|.*/||;
|
||||
7e2bf6ca | Moritz Bunkus | my $is_directory = -d "$path/$file";
|
||
e436a6b0 | Moritz Bunkus | |||
$file = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
|
||||
7e2bf6ca | Moritz Bunkus | $file .= '/' if ($is_directory);
|
||
e436a6b0 | Moritz Bunkus | |||
7e2bf6ca | Moritz Bunkus | push @{ $form->{WEBDAV} }, {
|
||
'name' => $fname,
|
||||
d4ea1fa8 | Sven Schöling | 'link' => $base_path . $file,
|
||
7e2bf6ca | Moritz Bunkus | 'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
|
||
};
|
||||
e436a6b0 | Moritz Bunkus | }
|
||
closedir $dir;
|
||||
8e206587 | Moritz Bunkus | }
|
||
}
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
fb4d2ffa | Moritz Bunkus | sub get_vc_details {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $vc, $vc_id) = @_;
|
||||
$vc = $vc eq "customer" ? "customer" : "vendor";
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my $query;
|
||||
$query =
|
||||
qq|SELECT
|
||||
vc.*,
|
||||
pt.description AS payment_terms,
|
||||
b.description AS business,
|
||||
03d3d025 | Bernd Bleßmann | l.description AS language,
|
||
dt.description AS delivery_terms
|
||||
fb4d2ffa | Moritz Bunkus | FROM ${vc} vc
|
||
LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
|
||||
LEFT JOIN business b ON (vc.business_id = b.id)
|
||||
LEFT JOIN language l ON (vc.language_id = l.id)
|
||||
03d3d025 | Bernd Bleßmann | LEFT JOIN delivery_terms dt ON (vc.delivery_term_id = dt.id)
|
||
fb4d2ffa | Moritz Bunkus | WHERE vc.id = ?|;
|
||
my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
|
||||
if (!$ref) {
|
||||
$dbh->disconnect();
|
||||
$main::lxdebug->leave_sub();
|
||||
return 0;
|
||||
}
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
|
||||
$query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
|
||||
$form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
|
||||
$query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
|
||||
$form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
|
||||
5574a036 | Geoffrey Richardson | # Only show default pricegroup for customer, not vendor, which is why this is outside the main query
|
||
7349649b | Geoffrey Richardson | ($form->{pricegroup}) = selectrow_query($form, $dbh, qq|SELECT pricegroup FROM pricegroup WHERE id = ?|, $form->{pricegroup_id});
|
||
5574a036 | Geoffrey Richardson | |||
fb4d2ffa | Moritz Bunkus | $dbh->disconnect();
|
||
$main::lxdebug->leave_sub();
|
||||
return 1;
|
||||
}
|
||||
480c6709 | Moritz Bunkus | sub get_shipto_by_id {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
|
||||
$prefix ||= "";
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
|
||||
my $ref = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
|
||||
map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
|
||||
4493d1eb | Moritz Bunkus | my $cvars = CVar->get_custom_variables(
|
||
dbh => $dbh,
|
||||
module => 'ShipTo',
|
||||
trans_id => $shipto_id,
|
||||
);
|
||||
$form->{"${prefix}shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
|
||||
480c6709 | Moritz Bunkus | $dbh->disconnect();
|
||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
6d1df9ca | Moritz Bunkus | sub save_email_status {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
my ($table, $query, $dbh);
|
||||
if ($form->{script} eq 'oe.pl') {
|
||||
$table = 'oe';
|
||||
} elsif ($form->{script} eq 'is.pl') {
|
||||
$table = 'ar';
|
||||
} elsif ($form->{script} eq 'ir.pl') {
|
||||
$table = 'ap';
|
||||
45dbf2f1 | Jan Büren | } elsif ($form->{script} eq 'do.pl') {
|
||
$table = 'delivery_orders';
|
||||
6d1df9ca | Moritz Bunkus | }
|
||
return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
|
||||
$dbh = $form->get_standard_dbh($myconfig);
|
||||
my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
|
||||
$intnotes =~ s|\r||g;
|
||||
$intnotes =~ s|\n$||;
|
||||
$intnotes .= "\n\n" if ($intnotes);
|
||||
74fca575 | Sven Schöling | my $cc = $form->{cc} ? $main::locale->text('Cc') . ": $form->{cc}\n" : '';
|
||
my $bcc = $form->{bcc} ? $main::locale->text('Bcc') . ": $form->{bcc}\n" : '';
|
||||
6d1df9ca | Moritz Bunkus | my $now = scalar localtime;
|
||
$intnotes .= $main::locale->text('[email]') . "\n"
|
||||
. $main::locale->text('Date') . ": $now\n"
|
||||
. $main::locale->text('To (email)') . ": $form->{email}\n"
|
||||
. "${cc}${bcc}"
|
||||
. $main::locale->text('Subject') . ": $form->{subject}\n\n"
|
||||
. $main::locale->text('Message') . ": $form->{message}";
|
||||
$intnotes =~ s|\r||g;
|
||||
do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
|
||||
$form->save_status($dbh);
|
||||
$dbh->commit();
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
900dff5f | Moritz Bunkus | sub check_params {
|
||
my $params = shift;
|
||||
foreach my $key (@_) {
|
||||
4a826447 | Moritz Bunkus | if ((ref $key eq '') && !defined $params->{$key}) {
|
||
900dff5f | Moritz Bunkus | my $subroutine = (caller(1))[3];
|
||
ff296f5f | Sven Schöling | $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
|
||
$main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
|
||||
900dff5f | Moritz Bunkus | $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
|
||
4a826447 | Moritz Bunkus | |||
} elsif (ref $key eq 'ARRAY') {
|
||||
my $found = 0;
|
||||
e807eba3 | Geoffrey Richardson | foreach my $subkey (@{ $key }) {
|
||
4a826447 | Moritz Bunkus | if (defined $params->{$subkey}) {
|
||
$found = 1;
|
||||
last;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
my $subroutine = (caller(1))[3];
|
||||
ff296f5f | Sven Schöling | $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
|
||
$main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
|
||||
4a826447 | Moritz Bunkus | $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
|
||
}
|
||||
900dff5f | Moritz Bunkus | }
|
||
}
|
||||
}
|
||||
49189bfb | Moritz Bunkus | sub check_params_x {
|
||
my $params = shift;
|
||||
foreach my $key (@_) {
|
||||
4a826447 | Moritz Bunkus | if ((ref $key eq '') && !exists $params->{$key}) {
|
||
49189bfb | Moritz Bunkus | my $subroutine = (caller(1))[3];
|
||
$main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
|
||||
4a826447 | Moritz Bunkus | |||
} elsif (ref $key eq 'ARRAY') {
|
||||
my $found = 0;
|
||||
e807eba3 | Geoffrey Richardson | foreach my $subkey (@{ $key }) {
|
||
4a826447 | Moritz Bunkus | if (exists $params->{$subkey}) {
|
||
$found = 1;
|
||||
last;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
my $subroutine = (caller(1))[3];
|
||||
$main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
|
||||
}
|
||||
49189bfb | Moritz Bunkus | }
|
||
}
|
||||
}
|
||||
9a578c8b | Jan Büren | sub get_webdav_folder {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($form) = @_;
|
||||
croak "No client set in \$::auth" unless $::auth->client;
|
||||
my ($path, $number);
|
||||
# dispatch table
|
||||
if ($form->{type} eq "sales_quotation") {
|
||||
($path, $number) = ("angebote", $form->{quonumber});
|
||||
} elsif ($form->{type} eq "sales_order") {
|
||||
($path, $number) = ("bestellungen", $form->{ordnumber});
|
||||
} elsif ($form->{type} eq "request_quotation") {
|
||||
($path, $number) = ("anfragen", $form->{quonumber});
|
||||
} elsif ($form->{type} eq "purchase_order") {
|
||||
($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
|
||||
} elsif ($form->{type} eq "sales_delivery_order") {
|
||||
($path, $number) = ("verkaufslieferscheine", $form->{donumber});
|
||||
} elsif ($form->{type} eq "purchase_delivery_order") {
|
||||
($path, $number) = ("einkaufslieferscheine", $form->{donumber});
|
||||
} elsif ($form->{type} eq "credit_note") {
|
||||
($path, $number) = ("gutschriften", $form->{invnumber});
|
||||
faea2c48 | Jan Büren | } elsif ($form->{type} eq "letter") {
|
||
($path, $number) = ("briefe", $form->{letternumber} );
|
||||
9a578c8b | Jan Büren | } elsif ($form->{vc} eq "customer") {
|
||
($path, $number) = ("rechnungen", $form->{invnumber});
|
||||
} elsif ($form->{vc} eq "vendor") {
|
||||
($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
|
||||
} else {
|
||||
$main::lxdebug->leave_sub();
|
||||
return undef;
|
||||
}
|
||||
$number =~ s|[/\\]|_|g;
|
||||
$path = "webdav/" . $::auth->client->{id} . "/${path}/${number}";
|
||||
$main::lxdebug->leave_sub();
|
||||
return ($path, $number);
|
||||
}
|
||||
sub copy_file_to_webdav_folder {
|
||||
d22f89a3 | Moritz Bunkus | $::lxdebug->enter_sub();
|
||
9a578c8b | Jan Büren | |||
my ($form) = @_;
|
||||
my ($last_mod_time, $latest_file_name, $complete_path);
|
||||
# checks
|
||||
foreach my $item (qw(tmpdir tmpfile type)){
|
||||
d22f89a3 | Moritz Bunkus | next if $form->{$item};
|
||
faea2c48 | Jan Büren | $::lxdebug->message(LXDebug::WARN(), 'Missing parameter:' . $item);
|
||
58c69eb0 | Moritz Bunkus | $::form->error($::locale->text("Missing parameter for WebDAV file copy"));
|
||
9a578c8b | Jan Büren | }
|
||
my ($webdav_folder, $document_name) = get_webdav_folder($form);
|
||||
if (! $webdav_folder){
|
||||
d22f89a3 | Moritz Bunkus | $::lxdebug->leave_sub();
|
||
faea2c48 | Jan Büren | $::lxdebug->message(LXDebug::WARN(), 'Cannot check correct WebDAV folder');
|
||
58c69eb0 | Moritz Bunkus | $::form->error($::locale->text("Cannot check correct WebDAV folder"));
|
||
d4dd649f | Sven Schöling | return undef;
|
||
9a578c8b | Jan Büren | }
|
||
d4dd649f | Sven Schöling | |||
4b17528a | Moritz Bunkus | $complete_path = File::Spec->catfile($form->{cwd}, $webdav_folder);
|
||
b599ced4 | Jan Büren | |||
# maybe the path does not exist (automatic printing), see #2446
|
||||
if (!-d $complete_path) {
|
||||
faea2c48 | Jan Büren | # we need a chdir and restore old dir
|
||
b599ced4 | Jan Büren | my $current_dir = POSIX::getcwd();
|
||
chdir("$form->{cwd}");
|
||||
mkdir_with_parents($webdav_folder);
|
||||
chdir($current_dir);
|
||||
}
|
||||
9a578c8b | Jan Büren | opendir my $dh, $complete_path or die "Could not open $complete_path: $!";
|
||
my ($newest_name, $newest_time);
|
||||
while ( defined( my $file = readdir( $dh ) ) ) {
|
||||
my $path = File::Spec->catfile( $complete_path, $file );
|
||||
next if -d $path; # skip directories, or anything else you like
|
||||
d22f89a3 | Moritz Bunkus | ( $newest_name, $newest_time ) = ( $file, -M _ ) if( ! defined $newest_time or -M $path < $newest_time );
|
||
}
|
||||
9a578c8b | Jan Büren | |||
d22f89a3 | Moritz Bunkus | closedir $dh;
|
||
4b17528a | Moritz Bunkus | $latest_file_name = File::Spec->catfile($complete_path, $newest_name);
|
||
d22f89a3 | Moritz Bunkus | my $filesize = stat($latest_file_name)->size;
|
||
a5c7ef23 | Moritz Bunkus | my $current_file = File::Spec->catfile($form->{tmpdir}, apply { s:.*/:: } $form->{tmpfile});
|
||
f5b999c9 | Moritz Bunkus | my $current_filesize = -f $current_file ? stat($current_file)->size : 0;
|
||
d22f89a3 | Moritz Bunkus | |||
d4dd649f | Sven Schöling | if ($current_filesize == $filesize) {
|
||
d22f89a3 | Moritz Bunkus | $::lxdebug->leave_sub();
|
||
9a578c8b | Jan Büren | return;
|
||
}
|
||||
d4dd649f | Sven Schöling | |||
4b17528a | Moritz Bunkus | my $timestamp = get_current_formatted_time();
|
||
my $new_file = File::Spec->catfile($form->{cwd}, $webdav_folder, $form->generate_attachment_filename());
|
||||
d5959d7e | Jan Büren | $new_file =~ s{(.*)\.}{$1$timestamp\.};
|
||
9a578c8b | Jan Büren | |||
4b17528a | Moritz Bunkus | if (!File::Copy::copy($current_file, $new_file)) {
|
||
$::lxdebug->message(LXDebug::WARN(), "Copy file from $current_file to $new_file failed: $ERRNO");
|
||||
$::form->error($::locale->text("Copy file from #1 to #2 failed: #3", $current_file, $new_file, $ERRNO));
|
||||
9a578c8b | Jan Büren | }
|
||
d22f89a3 | Moritz Bunkus | $::lxdebug->leave_sub();
|
||
9a578c8b | Jan Büren | }
|
||
0614d6da | Moritz Bunkus | sub get_current_formatted_time {
|
||
return POSIX::strftime('_%Y%m%d_%H%M%S', localtime());
|
||||
9a578c8b | Jan Büren | }
|
||
54e4131e | Moritz Bunkus | 1;
|
||
6794ddd4 | Moritz Bunkus | __END__
|
||
=pod
|
||||
=encoding utf8
|
||||
=head1 NAME
|
||||
Common - Common routines used in a lot of places.
|
||||
=head1 SYNOPSIS
|
||||
my $short_text = Common::truncate($long_text, at => 10);
|
||||
=head1 FUNCTIONS
|
||||
=over 4
|
||||
=item C<truncate $text, %params>
|
||||
Truncates C<$text> at a position and insert an ellipsis if the text is
|
||||
longer. The maximum number of characters to return is given with the
|
||||
paramter C<at> which defaults to 50.
|
||||
The optional parameter C<strip> can be used to remove unwanted line
|
||||
feed/carriage return characters from the text before truncation. It
|
||||
can be set to C<1> (only strip those at the end of C<$text>) or
|
||||
C<full> (replace consecutive line feed/carriage return characters in
|
||||
the middle by a single space and remove tailing line feed/carriage
|
||||
return characters).
|
||||
=back
|
||||
=head1 BUGS
|
||||
Nothing here yet.
|
||||
=head1 AUTHOR
|
||||
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
|
||||
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
|
||||
=cut
|