|
- <?php
-
- namespace App\Models;
-
- use Framework\Database\Model;
-
- class Stock extends Model
- {
- protected string $table = 'stock';
-
- public function item(): mixed
- {
- return $this->belongsTo(Item::class, 'item_id');
- }
-
- public function location(): mixed
- {
- return $this->belongsTo(Location::class, 'location_id');
- }
-
- public static function forItemAtLocation(int $itemId, int $locationId): static
- {
- $stock = static::where('item_id', $itemId)->where('location_id', $locationId)->first();
-
- if ($stock) {
- return $stock;
- }
-
- $stock = new static();
- $stock->item_id = $itemId;
- $stock->location_id = $locationId;
- $stock->quantity = 0;
-
- return $stock;
- }
- }
|