Mandantenabschluss: Marken, Vertragsdateien und Archive trennen
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m23s

This commit is contained in:
Lars committed 2026-10-26 17:07:11 +01:00
1 parent 86374b1659
commit 06df80e4db
45 files changed
+592 -99

No files matched your search

+55 -5
View File
@@ -2,13 +2,16 @@
namespace App\Tenancy;
use App\Models\Booking;
use App\Models\Photo;
use App\Models\Site;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use ZipArchive;
@@ -192,25 +195,39 @@ private function rowsOf(string $table, Tenant $tenant): array
}
/**
* Photos keep their files through Spatie's media library; they hang off the photo, not off the landlord,
* so they are collected through the photos of this landlord.
* Include media metadata, original files, conversions and every retained document version.
*/
private function addFiles(ZipArchive $zip, Tenant $tenant): void
{
foreach ($this->mediaOf($tenant) as $media) {
$mediaItems = $this->mediaOf($tenant);
$zip->addFromString('daten/media.json', (string) $mediaItems->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
foreach ($mediaItems as $media) {
$path = $media->getPath();
if (is_string($path) && File::exists($path)) {
$zip->addFile($path, 'dateien/'.$media->id.'-'.$media->file_name);
$directory = dirname($path);
foreach (File::allFiles($directory) as $file) {
$relative = str_replace('\\', '/', substr($file->getPathname(), strlen($directory) + 1));
$zip->addFile($file->getPathname(), 'dateien/'.$media->id.'-'.$relative);
}
}
}
foreach ($this->documentPaths($tenant) as $path) {
$zip->addFile(Storage::disk('local')->path($path), 'dateien/local/'.$path);
}
}
private function deleteFiles(Tenant $tenant): void
{
$documents = $this->documentPaths($tenant);
foreach ($this->mediaOf($tenant) as $media) {
$media->delete(); // removes the stored file as well
}
Storage::disk('local')->delete($documents);
}
/**
@@ -220,10 +237,43 @@ private function mediaOf(Tenant $tenant): Collection
{
return app(TenantContext::class)->run(
$tenant,
fn () => Photo::query()->with('media')->get()->flatMap(fn (Photo $photo) => $photo->media),
fn () => Photo::query()->with('media')->get()->flatMap(fn (Photo $photo) => $photo->media)
->concat(Site::query()->with('media')->get()->flatMap(fn (Site $site) => $site->media)),
);
}
/**
* New documents have a tenant directory, including earlier offer versions. Existing installations
* also have flat paths: accept only those explicitly referenced by this tenant and never a shared file.
*
* @return list<string>
*/
private function documentPaths(Tenant $tenant): array
{
$disk = Storage::disk('local');
$paths = $disk->allFiles('contracts/tenants/'.$tenant->id);
$bookings = Booking::withoutGlobalScope('tenant')->where('tenant_id', $tenant->id)->get(['contract_path', 'offer_path']);
foreach ($bookings as $booking) {
foreach ([$booking->contract_path, $booking->offer_path] as $path) {
if (! is_string($path) || ! preg_match('~\Acontracts/[^/\\\\]+\.pdf\z~', $path) || ! $disk->exists($path)) {
continue;
}
$shared = Booking::withoutGlobalScope('tenant')->where('tenant_id', '!=', $tenant->id)
->where(fn ($query) => $query->where('contract_path', $path)->orWhere('offer_path', $path))->exists();
if ($shared) {
throw new RuntimeException('Vertragsdatei ist mehreren Mandanten zugeordnet: '.$path);
}
$paths[] = $path;
}
}
return array_values(array_unique($paths));
}
private function hasTenantColumn(string $table): bool
{
static $known = [];
+1
View File
@@ -59,6 +59,7 @@ class TenantConfig
* @var array<string, string>
*/
private const OWNED = [
'app.name' => 'name',
'fewo.owner.name' => 'owner_name',
'fewo.owner.street' => 'owner_street',
'fewo.owner.city' => 'owner_city',