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

+58
View File
@@ -0,0 +1,58 @@
<?php
namespace App\Console\Commands;
use App\Models\Site;
use App\Tenancy\TenantContext;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('sites:import-stommer-brand {site : ID der bestehenden Stommer-Website}')]
#[Description('Copy the legacy Stommer brand into its own website without replacing uploaded graphics')]
class ImportSiteBrand extends Command
{
/**
* Execute the console command.
*/
public function handle(): int
{
$site = Site::withoutGlobalScope('tenant')->with('tenant')->find($this->argument('site'));
if ($site === null || $site->tenant?->slug !== 'stommer') {
$this->error('Nur die bestehende Stommer-Website darf diese Marke übernehmen.');
return self::FAILURE;
}
$seebad = $site->theme === 'seebad';
$sources = [
Site::BRAND_LOGO => $seebad ? resource_path('themes/seebad/img/logo-bildmarke-720.webp') : public_path('brand/logo.svg'),
Site::BRAND_FAVICON => $seebad ? resource_path('themes/seebad/img/favicon.svg') : public_path('brand/favicon.svg'),
Site::BRAND_DOCUMENT_LOGO => public_path('brand/logo-600.png'),
];
foreach ($sources as $collection => $path) {
if (! $site->hasMedia($collection) && ! is_file($path)) {
$this->error('Markenquelle fehlt: '.$path);
return self::FAILURE;
}
}
app(TenantContext::class)->run($site->tenant, function () use ($site, $sources): void {
foreach ($sources as $collection => $path) {
if ($site->hasMedia($collection)) {
$this->line($collection.': vorhandene Grafik beibehalten.');
continue;
}
$site->addMedia($path)->preservingOriginal()->toMediaCollection($collection);
$this->info($collection.': übernommen.');
}
});
return self::SUCCESS;
}
}
+2 -1
View File
@@ -19,9 +19,10 @@ class RentalContract
public function generate(Booking $booking, bool $draft = false): string
{
$booking->loadMissing(['guest', 'property']);
$path = $draft
$filename = $draft
? "contracts/Angebot-{$booking->reference}-v{$booking->offer_version}.pdf"
: "contracts/Mietvertrag-{$booking->reference}.pdf";
$path = "contracts/tenants/{$booking->tenant_id}/bookings/{$booking->id}/".basename($filename);
// The logo is embedded as a file: the PDF is rendered on the server and must not depend on
// fetching anything over the network. Each website has its own (A14).
@@ -68,12 +68,12 @@ public static function configure(Schema $schema): Schema
SpatieMediaLibraryFileUpload::make('logo')
->label('Logo')
->collection(Site::BRAND_LOGO)
->acceptedFileTypes(['image/svg+xml', 'image/png', 'image/webp'])
->helperText('SVG bevorzugt – bleibt in jeder Größe scharf.'),
->acceptedFileTypes(['image/png', 'image/webp'])
->helperText('PNG oder WebP. SVG-Dateien werden nur aus geprüften Quellen übernommen.'),
SpatieMediaLibraryFileUpload::make('favicon')
->label('Favicon')
->collection(Site::BRAND_FAVICON)
->acceptedFileTypes(['image/svg+xml', 'image/png', 'image/x-icon'])
->acceptedFileTypes(['image/png', 'image/x-icon'])
->helperText('Das kleine Zeichen im Browser-Tab.'),
SpatieMediaLibraryFileUpload::make('document_logo')
->label('Logo für E-Mail und Vertrag')
+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',