Revision 9a578c8b
Von Jan Büren vor mehr als 11 Jahren hinzugefügt
SL/Common.pm | ||
---|---|---|
14 | 14 |
use Carp; |
15 | 15 |
use Time::HiRes qw(gettimeofday); |
16 | 16 |
use Data::Dumper; |
17 |
use File::Copy; |
|
18 |
use File::stat; |
|
19 |
use File::Slurp; |
|
17 | 20 |
|
18 | 21 |
use SL::DBUtils; |
19 | 22 |
|
... | ... | |
326 | 329 |
$main::lxdebug->leave_sub(); |
327 | 330 |
} |
328 | 331 |
|
332 |
# |
|
333 |
# Legt ein entsprechendes Webdav-Verzeichnis an, falls |
|
334 |
# Webdav als Option konfiguriert ist. Falls schon ein |
|
335 |
# Ordner vorhanden ist, werden alle Dateien alphabetisch |
|
336 |
# sortiert ausgelesen und an der Oberfläche angezeigt |
|
337 |
# |
|
329 | 338 |
sub webdav_folder { |
330 | 339 |
$main::lxdebug->enter_sub(); |
331 | 340 |
|
332 | 341 |
my ($form) = @_; |
333 | 342 |
|
334 | 343 |
return $main::lxdebug->leave_sub() |
335 |
unless ($::lx_office_conf{features}->{webdav} && $form->{id});
|
|
344 |
unless ($::instance_conf->get_webdav && $form->{id});
|
|
336 | 345 |
|
337 |
croak "No client set in \$::auth" unless $::auth->client; |
|
338 | 346 |
|
339 |
my ($path, $number); |
|
340 | 347 |
|
341 | 348 |
$form->{WEBDAV} = []; |
342 | 349 |
|
343 |
if ($form->{type} eq "sales_quotation") { |
|
344 |
($path, $number) = ("angebote", $form->{quonumber}); |
|
345 |
} elsif ($form->{type} eq "sales_order") { |
|
346 |
($path, $number) = ("bestellungen", $form->{ordnumber}); |
|
347 |
} elsif ($form->{type} eq "request_quotation") { |
|
348 |
($path, $number) = ("anfragen", $form->{quonumber}); |
|
349 |
} elsif ($form->{type} eq "purchase_order") { |
|
350 |
($path, $number) = ("lieferantenbestellungen", $form->{ordnumber}); |
|
351 |
} elsif ($form->{type} eq "sales_delivery_order") { |
|
352 |
($path, $number) = ("verkaufslieferscheine", $form->{donumber}); |
|
353 |
} elsif ($form->{type} eq "purchase_delivery_order") { |
|
354 |
($path, $number) = ("einkaufslieferscheine", $form->{donumber}); |
|
355 |
} elsif ($form->{type} eq "credit_note") { |
|
356 |
($path, $number) = ("gutschriften", $form->{invnumber}); |
|
357 |
} elsif ($form->{vc} eq "customer") { |
|
358 |
($path, $number) = ("rechnungen", $form->{invnumber}); |
|
359 |
} else { |
|
360 |
($path, $number) = ("einkaufsrechnungen", $form->{invnumber}); |
|
361 |
} |
|
362 |
|
|
350 |
my ($path, $number) = get_webdav_folder($form); # ausgelagert |
|
363 | 351 |
return $main::lxdebug->leave_sub() unless ($path && $number); |
364 | 352 |
|
365 |
$number =~ s|[/\\]|_|g; |
|
366 |
|
|
367 |
$path = "webdav/" . $::auth->client->{id} . "/${path}/${number}"; |
|
368 |
|
|
369 | 353 |
if (!-d $path) { |
370 | 354 |
mkdir_with_parents($path); |
371 | 355 |
|
... | ... | |
373 | 357 |
my $base_path = $ENV{'SCRIPT_NAME'}; |
374 | 358 |
$base_path =~ s|[^/]+$||; |
375 | 359 |
if (opendir my $dir, $path) { |
360 |
# alphabetisch sortiert. |
|
376 | 361 |
foreach my $file (sort { lc $a cmp lc $b } readdir $dir) { |
377 | 362 |
next if (($file eq '.') || ($file eq '..')); |
378 | 363 |
|
... | ... | |
574 | 559 |
} |
575 | 560 |
} |
576 | 561 |
|
562 |
# |
|
563 |
# Diese Routine baut aus dem Masken-Typ und der |
|
564 |
# Beleg-Nummer, das entsprechende Webdav-Verzeichnis zusammen |
|
565 |
# Nimmt leider noch die ganze Form entgegen und den if-elsif-Block |
|
566 |
# sollte man schöner "dispatchen" |
|
567 |
# Ergänzung 6.5.2011, den else-Zweig defensiver gestaltet und mit |
|
568 |
# -1 als n.i.O. Rückgabewert versehen |
|
569 |
# |
|
570 |
sub get_webdav_folder { |
|
571 |
$main::lxdebug->enter_sub(); |
|
572 |
|
|
573 |
my ($form) = @_; |
|
574 |
|
|
575 |
croak "No client set in \$::auth" unless $::auth->client; |
|
576 |
|
|
577 |
my ($path, $number); |
|
578 |
|
|
579 |
# dispatch table |
|
580 |
if ($form->{type} eq "sales_quotation") { |
|
581 |
($path, $number) = ("angebote", $form->{quonumber}); |
|
582 |
} elsif ($form->{type} eq "sales_order") { |
|
583 |
($path, $number) = ("bestellungen", $form->{ordnumber}); |
|
584 |
} elsif ($form->{type} eq "request_quotation") { |
|
585 |
($path, $number) = ("anfragen", $form->{quonumber}); |
|
586 |
} elsif ($form->{type} eq "purchase_order") { |
|
587 |
($path, $number) = ("lieferantenbestellungen", $form->{ordnumber}); |
|
588 |
} elsif ($form->{type} eq "sales_delivery_order") { |
|
589 |
($path, $number) = ("verkaufslieferscheine", $form->{donumber}); |
|
590 |
} elsif ($form->{type} eq "purchase_delivery_order") { |
|
591 |
($path, $number) = ("einkaufslieferscheine", $form->{donumber}); |
|
592 |
} elsif ($form->{type} eq "credit_note") { |
|
593 |
($path, $number) = ("gutschriften", $form->{invnumber}); |
|
594 |
} elsif ($form->{vc} eq "customer") { |
|
595 |
($path, $number) = ("rechnungen", $form->{invnumber}); |
|
596 |
} elsif ($form->{vc} eq "vendor") { |
|
597 |
($path, $number) = ("einkaufsrechnungen", $form->{invnumber}); |
|
598 |
} else { |
|
599 |
# wir befinden uns nicht in einer belegmaske |
|
600 |
# scheinbar wird diese routine auch bspw. bei waren |
|
601 |
# aufgerufen - naja, steuerung über die $form halt ... |
|
602 |
$main::lxdebug->leave_sub(); |
|
603 |
return undef; |
|
604 |
} |
|
605 |
|
|
606 |
$number =~ s|[/\\]|_|g; |
|
607 |
|
|
608 |
$path = "webdav/" . $::auth->client->{id} . "/${path}/${number}"; |
|
609 |
|
|
610 |
$main::lxdebug->leave_sub(); |
|
611 |
|
|
612 |
return ($path, $number); |
|
613 |
} |
|
614 |
|
|
615 |
# |
|
616 |
# Falls Webdav aktiviert ist, auch den generierten Beleg in das |
|
617 |
# Webdav-Verzeichnis kopieren |
|
618 |
# |
|
619 |
# |
|
620 |
sub copy_file_to_webdav_folder { |
|
621 |
$main::lxdebug->enter_sub(); |
|
622 |
|
|
623 |
my ($form) = @_; |
|
624 |
my ($last_mod_time, $latest_file_name, $complete_path); |
|
625 |
|
|
626 |
# checks |
|
627 |
foreach my $item (qw(tmpdir tmpfile type)){ |
|
628 |
if (!$form->{$item}){ |
|
629 |
$main::lxdebug->message(0, 'Missing parameter'); |
|
630 |
$main::form->error($main::locale->text("Missing parameter for webdav file copy")); |
|
631 |
} |
|
632 |
} |
|
633 |
|
|
634 |
# Den Webdav-Ordner ÜBER exakt denselben Mechanismus wie beim |
|
635 |
# Anlegen des Ordners bestimmen |
|
636 |
my ($webdav_folder, $document_name) = get_webdav_folder($form); |
|
637 |
|
|
638 |
if (! $webdav_folder){ |
|
639 |
$main::lxdebug->leave_sub(); |
|
640 |
$main::form->error($main::locale->text("Cannot check correct webdav folder")); |
|
641 |
return undef; # s.o. erstmal so ... |
|
642 |
} |
|
643 |
# kompletter pfad |
|
644 |
$complete_path = join('/', $form->{cwd}, $webdav_folder); |
|
645 |
opendir my $dh, $complete_path or die "Could not open $complete_path: $!"; |
|
646 |
|
|
647 |
my ($newest_name, $newest_time); |
|
648 |
while ( defined( my $file = readdir( $dh ) ) ) { |
|
649 |
my $path = File::Spec->catfile( $complete_path, $file ); |
|
650 |
next if -d $path; # skip directories, or anything else you like |
|
651 |
( $newest_name, $newest_time ) = ( $file, -M _ ) |
|
652 |
if( ! defined $newest_time or -M $path < $newest_time ); |
|
653 |
} |
|
654 |
$latest_file_name = $complete_path .'/' . $newest_name; |
|
655 |
my $filesize = stat($latest_file_name)->size; |
|
656 |
|
|
657 |
# prüfung auf identisch oder nicht |
|
658 |
my ($ext) = $form->{tmpfile} =~ /(\.[^.]+)$/; |
|
659 |
my $current_file = join('/', $form->{tmpdir}, $form->{tmpfile}); |
|
660 |
my $current_filesize = stat($current_file)->size; |
|
661 |
if ($current_filesize == $filesize) { # bei gleicher größe copy deaktivieren |
|
662 |
$main::lxdebug->leave_sub(); |
|
663 |
return; |
|
664 |
} |
|
665 |
# zeitstempel und dateinamen holen |
|
666 |
my $timestamp = get_current_formatted_time(); |
|
667 |
my $myfilename = $form->generate_attachment_filename(); |
|
668 |
# entsprechend vor der endung hinzufügen |
|
669 |
$myfilename =~ s/\./$timestamp\./; |
|
670 |
|
|
671 |
if (!copy(join('/', $form->{tmpdir}, $form->{tmpfile}), join('/', $form->{cwd}, $webdav_folder, $myfilename))) { |
|
672 |
my $j = join('/', $form->{tmpdir}, $form->{tmpfile}); |
|
673 |
my $k = join('/', $form->{cwd}, $webdav_folder); |
|
674 |
$main::lxdebug->message(0, "Copy file from $j to $k failed"); |
|
675 |
$main::form->error($main::locale->text("Copy file from #1 to #2 failed", $j, $k)); |
|
676 |
} |
|
677 |
|
|
678 |
$main::lxdebug->leave_sub(); |
|
679 |
} |
|
680 |
sub get_current_formatted_time { |
|
681 |
$main::lxdebug->enter_sub(); |
|
682 |
|
|
683 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); |
|
684 |
my $formatted_current_time = sprintf ( "_%04d%02d%02d_%02d%02d%02d", |
|
685 |
$year+1900,$mon+1,$mday,$hour,$min,$sec); |
|
686 |
|
|
687 |
$main::lxdebug->leave_sub(); |
|
688 |
return $formatted_current_time; |
|
689 |
} |
|
690 |
|
|
577 | 691 |
1; |
578 | 692 |
__END__ |
579 | 693 |
|
Auch abrufbar als: Unified diff
Trac 2300 / 2301 zweiter Versuch
;-)