Bisher ließen sich nur offene Bestellungen bearbeiten; ein Tippfehler in einem gebuchten Wareneingang war nicht korrigierbar (Anforderung 14.09.2026). - Bearbeiten/Korrigieren für gebuchte Eingänge (Admin): Bestell- und Eingangsdaten (Menge, Datum, Charge, MHD, Qualität, Preise, Lieferant, Lagerort). Menge wirkt sofort auf den Bestand; ml-Eingänge werden in ml korrigiert und über die gespeicherte Dichte in Gramm gebucht. - Schutz: Menge nicht unter den bereits verbrauchten Anteil (Rohstoff je Eingang, Verpackung je Artikel); bei Verbrauch sind Art und Rohstoff/ Artikel gesperrt (Formular + Server). - Änderungsprotokoll (stock_entry_changes): wer/wann/Grund + geänderte Felder alt → neu; Anzeige auf der Detailseite. Grund bei gebuchten Eingängen Pflicht, bei offenen optional. Speichern ohne Änderung erzeugt keine Zeile. - Tests (6 neue), In-App-Doku (Hinweise + Leitfaden 3.2) nachgezogen. Migration: 2026_09_14_110000_create_stock_entry_changes_table. Suite: 396 grün. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
163 lines
4 KiB
PHP
163 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\User;
|
|
use Database\Factories\StockEntryFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class StockEntry extends Model
|
|
{
|
|
/** @use HasFactory<StockEntryFactory> */
|
|
use HasFactory;
|
|
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'entry_type',
|
|
'ingredient_id',
|
|
'packaging_item_id',
|
|
'supplier_id',
|
|
'location_id',
|
|
'unit',
|
|
'purchase_unit',
|
|
'density_used',
|
|
'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<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'ordered_at' => 'date',
|
|
'received_at' => 'date',
|
|
'best_before' => 'date',
|
|
'ordered_quantity' => 'decimal:3',
|
|
'received_quantity' => 'decimal:3',
|
|
'density_used' => 'decimal:4',
|
|
'price_per_kg' => 'decimal:4',
|
|
'price_per_kg_gross' => 'decimal:4',
|
|
'price_total' => 'decimal:4',
|
|
'tax_rate_percent' => 'decimal:2',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Änderungsprotokoll (nachträgliche Korrekturen), neueste zuerst.
|
|
*
|
|
* @return HasMany<StockEntryChange, $this>
|
|
*/
|
|
public function changes(): HasMany
|
|
{
|
|
return $this->hasMany(StockEntryChange::class)->orderByDesc('id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Ingredient, $this>
|
|
*/
|
|
public function ingredient(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Ingredient::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<PackagingItem, $this>
|
|
*/
|
|
public function packagingItem(): BelongsTo
|
|
{
|
|
return $this->belongsTo(PackagingItem::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Supplier, $this>
|
|
*/
|
|
public function supplier(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Supplier::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Location, $this>
|
|
*/
|
|
public function location(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Location::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<TaxRate, $this>
|
|
*/
|
|
public function taxRate(): BelongsTo
|
|
{
|
|
return $this->belongsTo(TaxRate::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<MaterialQuality, $this>
|
|
*/
|
|
public function quality(): BelongsTo
|
|
{
|
|
return $this->belongsTo(MaterialQuality::class, 'quality_id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function orderedByUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'ordered_by');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
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';
|
|
}
|
|
}
|