File: D:/HostingSpaces/SBogers10/zipwire.komma.pro/app/KommaApp/Shop/Products/Product/Product.php
<?php
namespace App\KommaApp\Shop\Products\Product;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Documents\Models\Document;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Shop\Categories\Kms\CategorizableInterface;
use App\KommaApp\Shop\Categories\Models\Category;
use App\KommaApp\Shop\Discounts\DiscountableInterface;
use App\KommaApp\Shop\Discounts\Discount;
use App\KommaApp\Shop\Products\ProductableInterface;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroup;
use App\KommaApp\Shop\Properties\Models\PropertizableInterface;
use App\KommaApp\Shop\Properties\Models\Property;
use App\KommaApp\Sites\Models\Site;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Laravel\Scout\Searchable;
use App\KommaApp\Kms\Core\HasImagesInterface;
/**
* Represents a basic product that a user can put in a cart
*
* Class Product
*
* @package App
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Categories\Models\Category[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Documents\Models\Document[] $documents
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup[] $groups
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Sites\Models\Site[] $sites
* @property-read \App\KommaApp\Shop\Products\Product\ProductTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\Product\ProductTranslation[] $translations
* @mixin \Eloquent
* @property int $id
* @property int $active
* @property string $title
* @property int $price
* @property string $stock_keeping_unit
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product wherePrice($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product whereStockKeepingUnit($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\Product\Product whereUpdatedAt($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Properties\Models\Property[] $properties
*/
class Product extends AbstractTranslatableModel implements ProductableInterface, PropertizableInterface, CategorizableInterface, DocumentableInterface, HasImagesInterface
{
use Searchable;
public function searchableAs()
{
return 'products_index';
}
public function toSearchableArray()
{
return [
'id' => $this->id,
'active' => $this->active,
'title' => $this->title,
'price' => $this->price,
'stock_keeping_unit' => $this->stock_keeping_unit
];
}
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
/** @var Discount[] $discounts */
private $discounts = [];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['title, active, price, stock_keeping_unit'];
public function groups()
{
return $this->belongsToMany(ProductGroup::class);
}
/**
* Get the images from the current product
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function images():HasMany
{
/**
*
* On the Image model is an MorphTo relation
* By using a hasMany relation:
* where the imageable_type is filled in with the KmsClass
* And the imageable_id is set as the foreign_id,
* we can collect the images of the given model directly.
*
*/
return $this->hasMany(Image::class, 'imageable_id')
->where('imageable_type', '=', Product::class);
}
/**
* Get the documents from the current model
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function documents():MorphMany
{
return $this->morphMany(Document::class, 'documentable');
}
/**
* We belong to many categories
*
* @return MorphToMany
*/
public function categories(): MorphToMany
{
return $this->morphToMany(Category::class, 'product_categorizable');
}
/**
* We belong to many sites
*
* @return BelongsToMany
*/
public function sites(): BelongsToMany
{
return $this->belongsToMany(Site::class);
}
public function translations(): HasMany
{
return $this->hasMany(ProductTranslation::class);
}
/**
* Returns properties
* Example key: color
* Example value of key color: green
*
* @return MorphToMany
*/
function properties(): MorphToMany
{
return $this->morphToMany(Property::class, 'propertizable');
}
/**
* Price is in cents
*
* @return float
*/
public function getTotal(): float
{
return $this->price;
}
public function getName(): string
{
return $this->title;
}
/**
* Applies the discount to itself
*
* @param Discount $discount
* @return DiscountableInterface
*/
public function applyDiscount(Discount $discount): DiscountableInterface
{
$this->discounts = [];
}
/**
* Returns the price of the product
*
* @return float
*/
public function getPrice(): float
{
return $this->price;
}
}