gruene-seele/app/Models/Attribute.php
Kevin Adametz cff749b133 White Label WL-02: verbindliche Tiegelfarbe, Etikettposition und gemeinsamer Resolver
- products.whitelabel_variant_id, product_images.position/size_cm mit Backfill aus Produktnamen
- WhiteLabelResolver: Farbe vom Produkt, Kundenlogo je Position, Fehler statt stillem Rückfall; Altlogik für Produkte ohne Variante
- Attribute: Variantenwert-Parsing und Farbname ohne Größe
- Produkt- und Kundenformular um Variante, Position und Größe ergänzt
- Lieferschein noch nicht umgestellt (WL-03, wartet auf K6/K7)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 12:33:44 +00:00

162 lines
4.8 KiB
PHP

<?php
namespace App\Models;
use App\Services\Util;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/**
* App\Models\Attribute
*
* @property int $id
* @property int|null $parent_id
* @property string $name
* @property array|null $trans_name
* @property int|null $pos
* @property int $active
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Collection|Attribute[] $childrens
* @property-read Attribute|null $parent
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereParentId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereTransName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereUpdatedAt($value)
*
* @property string|null $slug
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attribute query()
*
* @property-read int|null $childrens_count
* @property int $attribute_type_id
* @property string|null $value
* @property-read AttributeType $attribute_type
*
* @method static \Illuminate\Database\Eloquent\Builder|Attribute whereAttributeTypeId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Attribute whereValue($value)
*
* @mixin \Eloquent
*/
class Attribute extends Model
{
protected $table = 'attributes';
protected $casts = [
'parent_id' => 'int',
'pos' => 'int',
'active' => 'bool',
'trans_name' => 'array',
];
protected $fillable = [
'parent_id',
'attribute_type_id',
'name',
'value',
'trans_name',
'pos',
'active',
'slug',
];
public function attribute_type()
{
return $this->belongsTo(AttributeType::class, 'attribute_type_id');
}
/**
* Variantenwert "Volumen#Größe" (z. B. "40#3,5") in Volumen (ml) und Etikettgröße (cm) zerlegen.
* Die Größe steht mit deutschem Komma im Bestand; ein reiner (float)-Cast würde "3,5" zu 3.0 kürzen.
*
* @return array{0: float, 1: float}
*/
public static function parseVariantValue(?string $value): array
{
$parts = explode('#', (string) $value);
return [
(float) reFormatNumber($parts[0] ?? ''),
(float) reFormatNumber($parts[1] ?? ''),
];
}
public function variantVolume(): float
{
return self::parseVariantValue($this->value)[0];
}
public function variantSize(): float
{
return self::parseVariantValue($this->value)[1];
}
/**
* Farbname ohne Größenangabe: "Schwarz 40 ml" => "Schwarz". Namen ohne Volumen bleiben unverändert.
*/
public function colorName(): string
{
return trim((string) preg_replace('/\s+\d+([,.]\d+)?\s*ml\s*$/iu', '', (string) $this->name));
}
public function parent()
{
return $this->belongsTo(Attribute::class, 'parent_id');
}
public function childrens()
{
return $this->hasMany(Attribute::class, 'parent_id', 'id');
}
public function setPosAttribute($value)
{
$this->attributes['pos'] = is_numeric($value) ? $value : null;
}
public function getFormattedValue()
{
// return isset($this->attributes['value']) ? Util::formatNumber($this->attributes['value']) : "";
}
public function getLang($key)
{
$lang = \App::getLocale();
if ($lang == 'de') {
return $this->{$key};
}
$trans = $this->getTrans($key, $lang);
if (! $trans || $trans == '') {
return $this->{$key};
}
return $trans;
}
public function getTrans($key, $lang)
{
$key = 'trans_'.$key;
if (! empty($this->{$key}[$lang])) {
return $this->{$key}[$lang];
}
}
public function getTranNames()
{
$ret = '';
foreach ((array) $this->trans_name as $value) {
$ret .= $value.', ';
}
return rtrim($ret, ', ');
}
}