<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @property int $id
* @property int $address_id
* @property int $discount
* @property string $variation
* @property Product $product
* @property int $quantity
* @property float $shipping_cost
* @property string $shipping_type
* @property int $pickup_point
*/
class Cart extends Model {
protected $guarded = [];
protected $fillable = [
'address_id',
'price',
'tax',
'shipping_cost',
'shipping_type',
'discount',
'product_referral_code',
'coupon_code',
'coupon_applied',
'quantity',
'user_id',
'temp_user_id',
'owner_id',
'product_id',
'variation',
'pickup_point'
];
public function user() {
return $this->belongsTo(User::class);
}
public function product() {
return $this->belongsTo(Product::class, 'product_id', 'id');
}
public function address() {
return $this->belongsTo(Address::class);
}
public function shippingCosts() {
return $this->hasMany(ShippingCost::class);
}
public function cartWeight(): float {
return ($this->product->weight * $this->quantity);
}
public function deliveryEstimate(): HasMany {
return $this->hasMany(DeliveryEstimate::class);
}
public function cartVolume(): float {
$choise_options = json_decode($this->product->choice_options);
$value = $choise_options[0]->values[0];
// $replaceWord = str_contains($value, 'SOBRE') ? 'SOBRE' : 'CAJA';
// $envelope = trim(str_replace($replaceWord, '', $value));
$dimensions = explode('x', $value);
$length = $dimensions[0];
$width = $dimensions[1];
$height = $dimensions[2];
return $this->calcVolume($length, $width, $height);
// return match ($envelope) {
// 'A' => $this->calcVolume(4.7, 8.4, 2.4) * $this->quantity,
// 'B' => $this->calcVolume(5.9, 8.4, 2.4) * $this->quantity,
// 'C' => $this->calcVolume(7, 10.4, 2.4) * $this->quantity,
// 'D' => $this->calcVolume(8.6, 10.4, 2.4) * $this->quantity,
// 'E' => $this->calcVolume(9, 13.3, 2.4) * $this->quantity,
// 'F' => $this->calcVolume(9.4, 13.1, 2.4) * $this->quantity,
// 'G' => $this->calcVolume(10.6, 8.4, 14.1) * $this->quantity,
// 'H' => $this->calcVolume(11.8, 17.5, 2.4) * $this->quantity,
// 'I' => $this->calcVolume(13.7, 18.5, 2.4) * $this->quantity,
// 'J' => $this->calcVolume(7, 6.4, 2.4) * $this->quantity,
// 'K' => $this->calcVolume(19, 12, 12) * $this->quantity,
// default => 0.00,
// };
}
private function calcVolume(int|float $length, int|float $width, int|float $height): float {
$length_to_cm = $length * 2.54;
$width_to_cm = $width * 2.54;
$height_to_cm = $height * 2.54;
return $length_to_cm * $width_to_cm * $height_to_cm;
}
public function scopeOwnedByCurrentUser($query) {
return $query->where('user_id', auth()->id());
}
public function scopeOwnedByUser($query, $userId) {
return $query->where('user_id', $userId);
}
}
|