Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
5 / 7
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WarehouseStock
71.43% covered (warning)
71.43%
5 / 7
60.00% covered (warning)
60.00%
3 / 5
6.84
0.00% covered (danger)
0.00%
0 / 1
 booted
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 warehouse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 product
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 variation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Models;
4
5use App\Models\admin\Product;
6use Illuminate\Database\Eloquent\Model;
7
8class WarehouseStock extends Model
9{
10    protected $fillable = ['item_key', 'warehouse_id', 'product_id', 'variation_id', 'quantity', 'reserved'];
11
12    protected $casts = ['quantity' => 'decimal:4', 'reserved' => 'decimal:4'];
13
14    protected static function booted(): void
15    {
16        static::saving(function (self $stock) {
17            $stock->item_key = $stock->warehouse_id . '-' . $stock->product_id . '-' . ($stock->variation_id ?: 0);
18        });
19    }
20
21    public function warehouse()
22    {
23        return $this->belongsTo(Warehouse::class);
24    }
25
26    public function product()
27    {
28        return $this->belongsTo(Product::class);
29    }
30
31    public function variation()
32    {
33        return $this->belongsTo(ProductVariation::class);
34    }
35
36    public function available(): float
37    {
38        return max(0, (float) $this->quantity - (float) $this->reserved);
39    }
40}