*/ use HasFactory; use SoftDeletes; protected $fillable = [ 'entry_type', 'ingredient_id', 'packaging_item_id', 'supplier_id', 'location_id', 'unit', 'ordered_by', 'ordered_at', 'ordered_quantity', 'price_per_kg', 'price_per_kg_gross', 'price_total', 'tax_rate_id', 'tax_rate_percent', 'received_by', 'received_at', 'received_quantity', 'batch_number', 'best_before', 'quality_id', 'status', ]; /** * @return array */ protected function casts(): array { return [ 'ordered_at' => 'date', 'received_at' => 'date', 'best_before' => 'date', 'ordered_quantity' => 'decimal:2', 'received_quantity' => 'decimal:2', 'price_per_kg' => 'decimal:4', 'price_per_kg_gross' => 'decimal:4', 'price_total' => 'decimal:4', 'tax_rate_percent' => 'decimal:2', ]; } /** * @return BelongsTo */ public function ingredient(): BelongsTo { return $this->belongsTo(Ingredient::class); } /** * @return BelongsTo */ public function packagingItem(): BelongsTo { return $this->belongsTo(PackagingItem::class); } /** * @return BelongsTo */ public function supplier(): BelongsTo { return $this->belongsTo(Supplier::class); } /** * @return BelongsTo */ public function location(): BelongsTo { return $this->belongsTo(Location::class); } /** * @return BelongsTo */ public function taxRate(): BelongsTo { return $this->belongsTo(TaxRate::class); } /** * @return BelongsTo */ public function quality(): BelongsTo { return $this->belongsTo(MaterialQuality::class, 'quality_id'); } /** * @return BelongsTo */ public function orderedByUser(): BelongsTo { return $this->belongsTo(User::class, 'ordered_by'); } /** * @return BelongsTo */ public function receivedByUser(): BelongsTo { return $this->belongsTo(User::class, 'received_by'); } public function isPending(): bool { return $this->status === 'pending'; } /** * AP-27: Gruppierungsschlüssel für die Chargen-Zusammenführung. * Einträge mit Chargennummer werden je Charge zusammengefasst, * Einträge ohne Chargennummer bleiben einzeln. * Audit N-1 (Kundenentscheid 15.07.2026): Chargennummern kommen von * extern und können kollidieren — der Lieferant gehört zum Schlüssel, * damit gleiche Lot-Nummern verschiedener Lieferanten getrennt bleiben. */ public function batchGroupKey(): string { $batch = trim((string) $this->batch_number); return $batch !== '' ? 'b:'.((int) $this->supplier_id).':'.$batch : 'e:'.$this->id; } public function isReceived(): bool { return $this->status === 'received'; } }