fix(injector-protocols): derive protocol prefix from tenant slug (not hardcoded PS)
'PS' was baked in — wrong for multi-tenant. Now:
1. Honor $tenant->settings['protocol_prefix'] if set
2. Else use uppercase first 2 chars of tenant slug
('psauto' → 'PS', 'autoplus' → 'AP', 'plusrepair' → 'PL')
3. Fallback 'PR' if no tenant resolvable
Format changed from 'PS-{companyId}-{year}-{seq}' to '{PREFIX}-{year}-{seq}'
— dropped the redundant companyId since prefix already identifies the
tenant contextually.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -86,10 +86,18 @@ class InjectorProtocol extends Model
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** PS-{tenantId}-{YYYY}-{seq6} — per-tenant, per-year sequence. */
|
/**
|
||||||
|
* {PREFIX}-{YYYY}-{seq6} — per-tenant, per-year sequence.
|
||||||
|
*
|
||||||
|
* PREFIX is derived from the tenant (settings.protocol_prefix override,
|
||||||
|
* else uppercase of the first 2–3 letters of the tenant slug —
|
||||||
|
* 'psauto' → 'PS', 'autoplus' → 'AP', etc.). Falls back to 'PR' when
|
||||||
|
* no tenant is resolvable.
|
||||||
|
*/
|
||||||
public static function generateNumber(): string
|
public static function generateNumber(): string
|
||||||
{
|
{
|
||||||
$companyId = app(TenantManager::class)->current()?->id
|
$tenant = app(TenantManager::class)->current();
|
||||||
|
$companyId = $tenant?->id
|
||||||
?? auth()->user()?->company_id
|
?? auth()->user()?->company_id
|
||||||
?? 0;
|
?? 0;
|
||||||
$year = now()->year;
|
$year = now()->year;
|
||||||
@@ -97,6 +105,26 @@ class InjectorProtocol extends Model
|
|||||||
->where('company_id', $companyId)
|
->where('company_id', $companyId)
|
||||||
->whereYear('created_at', $year)
|
->whereYear('created_at', $year)
|
||||||
->count() + 1;
|
->count() + 1;
|
||||||
return sprintf('PS-%d-%d-%06d', $companyId, $year, $seq);
|
|
||||||
|
$prefix = self::resolvePrefix($tenant);
|
||||||
|
|
||||||
|
return sprintf('%s-%d-%06d', $prefix, $year, $seq);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Explicit tenant setting → slug initials → 'PR' fallback. */
|
||||||
|
protected static function resolvePrefix($tenant): string
|
||||||
|
{
|
||||||
|
if ($tenant) {
|
||||||
|
$explicit = trim((string) data_get($tenant->settings ?? [], 'protocol_prefix'));
|
||||||
|
if ($explicit !== '') {
|
||||||
|
return mb_strtoupper($explicit);
|
||||||
|
}
|
||||||
|
$slug = (string) ($tenant->slug ?? '');
|
||||||
|
if ($slug !== '') {
|
||||||
|
// Take first 2 characters of the slug — 'psauto' → 'PS'
|
||||||
|
return mb_strtoupper(mb_substr($slug, 0, 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 'PR';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user