wyyl/beike/Models/Product.php

265 lines
7.7 KiB
PHP

<?php
namespace Beike\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Base
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'images',
'video',
'position',
'brand_id',
'tax_class_id',
'weight',
'weight_class',
'active',
'variables',
'price_setting',
'length',
'width',
'height',
'minimum_order',
'sales_method',
'piece_to_batch',
'trade_term',
'unit'
];
protected $casts = [
'active' => 'boolean',
'variables' => 'array',
'images' => 'array',
'numPrices' => 'array',
];
public function getNumPricesByNum($num)
{
$descNumPrices = array_reverse($this->numprices->toArray());
$multiple = 1;
if($this->sales_method == 'batches') $multiple = (int)$this->piece_to_batch;
foreach($descNumPrices as $numprice){
if($num >= ($numprice['num'] * $multiple)){
return $numprice['price'];
}
}
return FALSE;
}
protected $appends = ['image'];
public function categories()
{
return $this->belongsToMany(Category::class, ProductCategory::class)->withTimestamps();
}
public function productCategories()
{
return $this->hasMany(ProductCategory::class);
}
public function description()
{
return $this->hasOne(ProductDescription::class)->where('locale', locale());
}
public function descriptions()
{
return $this->hasMany(ProductDescription::class);
}
public function skus()
{
return $this->hasMany(ProductSku::class);
}
public function numPrices()
{
return $this->hasMany(ProductNumPrice::class);
}
public function attributes(): HasMany
{
return $this->hasMany(ProductAttribute::class);
}
public function masterSku()
{
return $this->hasOne(ProductSku::class)->where('is_default', 1);
}
public function brand()
{
return $this->belongsTo(Brand::class, 'brand_id', 'id');
}
public function relations()
{
return $this->belongsToMany(self::class, ProductRelation::class, 'product_id', 'relation_id')->withTimestamps();
}
public function inCurrentWishlist()
{
$customer = current_customer();
$customerId = $customer ? $customer->id : 0;
return $this->hasOne(CustomerWishlist::class)->where('customer_id', $customerId);
}
public function getUrlAttribute()
{
$url = shop_route('products.show', ['product' => $this]);
$filters = hook_filter('model.product.url', ['url' => $url, 'product' => $this]);
return $filters['url'] ?? '';
}
public function getImageAttribute()
{
$images = $this->images ?? [];
return $images[0] ?? '';
}
public static function getUnitList(){
return [
//A
['title' => 'Acre/Acres'],
['title' => 'Ampere/Amperes'],
//B
['title' => 'Bag/Bags'],
['title' => 'Barrel/Barrels'],
['title' => 'Blade/Blades'],
['title' => 'Box/Boxes'],
['title' => 'Bushel/Bushels'],
// C
['title' => 'Carat/Carats'],
['title' => 'Carton/Cartons'],
['title' => 'Case/Cases'],
['title' => 'Centimeter/Centimeters'],
['title' => 'Chain/Chains'],
['title' => 'Combo/Combos'],
['title' => 'Cubic Centimeter/Cubic Centimeters'],
['title' => 'Cubic Foot/Cubic Feet'],
['title' => 'Cubic Inch/Cubic Inches'],
['title' => 'Cubic Meter/Cubic Meters'],
['title' => 'Cubic Yard/Cubic Yards'],
// D
['title' => 'Degrees Fahrenheit'],
['title' => 'Dozen/Dozens'],
['title' => 'Dram/Drams'],
// F
['title' => 'Fluid Ounce/Fluid Ounces'],
['title' => 'Foot/Feet'],
['title' => 'Forty-FootContainer'],
['title' => 'Furlong/Furlongs'],
// G
['title' => 'Gallon/Gallons'],
['title' => 'Gill/Gills'],
['title' => 'Grain/Grains'],
['title' => 'Gram/Grams'],
['title' => 'Gross'],
// H
['title' => 'Hectare/Hectares'],
['title' => 'Hertz'],
// I
['title' => 'Inch/Inches'],
// K
['title' => 'Kiloampere/Kiloamperes'],
['title' => 'Kilogram/Kilograms'],
['title' => 'Kilohertz'],
['title' => 'Kilometer/kilometers'],
['title' => 'Kiloohm/Kiloohms'],
['title' => 'Kilovolt/Kilovolts'],
['title' => 'Kilowatt/Kilowatts'],
// L
['title' => 'Liter/Liters'],
['title' => 'Long Ton/Long Tons'],
// M
['title' => 'Megahertz'],
['title' => 'Meter/Meters'],
['title' => 'Metric Ton/Metric Tons'],
['title' => 'Mile/Miles'],
['title' => 'Milliampere/Milliamperes'],
['title' => 'Milligram/Milligrams'],
['title' => 'Millihertz'],
['title' => 'Milliliter/Milliliters'],
['title' => 'Milliohm/Milliohms'],
['title' => 'Millivolt/Millivolts'],
['title' => 'Milliwatt/Milliwatts'],
// N
['title' => 'Nautical Mile/Nautical Miles'],
// O
['title' => 'Ohm/Ohms'],
['title' => 'Ounce/Ounces'],
// P
['title' => 'Pack/Packs'],
['title' => 'Pair/Pairs'],
['title' => 'Pallet/Pallets'],
['title' => 'Parcel/Parcels'],
['title' => 'Perch/Perches'],
['title' => 'Piece/Pieces'],
['title' => 'Pint/Pints'],
['title' => 'Plant/Plants'],
['title' => 'Pole/Poles'],
['title' => 'Pound/Pounds'],
// Q
['title' => 'Quart/Quarts'],
['title' => 'Quarter/Quarters'],
// R
['title' => 'Rod/Rods'],
['title' => 'Roll/Rolls'],
// S
['title' => 'Set/Sets'],
['title' => 'Sheet/Sheets'],
['title' => 'Short Ton/Short Tons'],
['title' => 'Square Centimeter/Square Centimeters'],
['title' => 'Square Foot/Square Feet'],
['title' => 'Square Inch/Square Inches'],
['title' => 'Square Meter/Square Meters'],
['title' => 'Square Mile/Square Miles'],
['title' => 'Square Yard/Square Yards'],
['title' => 'Stone/Stones'],
['title' => 'Strand/Strands'],
// T
['title' => 'Ton/Tons'],
['title' => 'Tonne/Tonnes'],
['title' => 'Tray/Trays'],
['title' => 'Twenty-Foot Container'],
// U
['title' => 'Unit/Units'],
// V
['title' => 'Volt/Volts'],
// W
['title' => 'Watt/Watts'],
['title' => 'Wp'],
// Y
['title' => 'Yard/Yards'],
];
}
public static function getTradeTermList(){
return [
['title'=>'EXW'],
['title'=>'FCA'],
['title'=>'FAS'],
['title'=>'FOB'],
['title'=>'CFR'],
['title'=>'CIF'],
['title'=>'CPT'],
['title'=>'CIP'],
['title'=>'DAT'],
['title'=>'DAP'],
['title'=>'DDP'],
];
}
}