Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ShippingRate | |
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| zone | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| chargeFor | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Model; |
| 6 | |
| 7 | class ShippingRate extends Model |
| 8 | { |
| 9 | protected $fillable = ['zone_id', 'label', 'charge', 'free_above', 'sort_order']; |
| 10 | |
| 11 | protected $casts = [ |
| 12 | 'charge' => 'decimal:2', |
| 13 | 'free_above' => 'decimal:2', |
| 14 | 'sort_order' => 'integer', |
| 15 | ]; |
| 16 | |
| 17 | public function zone() |
| 18 | { |
| 19 | return $this->belongsTo(ShippingZone::class, 'zone_id'); |
| 20 | } |
| 21 | |
| 22 | /** The charge for a given order subtotal — free once it clears free_above. */ |
| 23 | public function chargeFor(float $subtotal): float |
| 24 | { |
| 25 | if ($this->free_above !== null && $subtotal >= (float) $this->free_above) { |
| 26 | return 0.0; |
| 27 | } |
| 28 | |
| 29 | return (float) $this->charge; |
| 30 | } |
| 31 | } |