File: D:/HostingSpaces/SBogers10/dndin.komma.pro/app/KommaApp/Shop/Products/ProductGroup/ProductGroup.php
<?php
namespace App\KommaApp\Shop\Products\ProductGroup;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\HasImagesInterface;
use App\KommaApp\Shop\Categories\Kms\CategorizableInterface;
use App\KommaApp\Shop\Categories\Models\Category;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\ProductableInterface;
use App\KommaApp\Shop\Products\ProductComposite\ProductComposite;
use App\KommaApp\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use App\KommaApp\Shop\Properties\Models\PropertizableInterface;
use App\KommaApp\Shop\Properties\Models\Property;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Laravel\Scout\Searchable;
/**
* Groups products together (not like a product category)
* to link behaviour.
*
* Class ProductGroup
*
* @package App
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Categories\Models\Category[] $categories
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\ProductComposite\ProductComposite[] $composites
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
* @property-read \App\KommaApp\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour $productGroupBehaviour
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\Product\Product[] $products
* @property-read \App\KommaApp\Shop\Products\ProductGroup\ProductGroupTranslation $translation
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\ProductGroup\ProductGroupTranslation[] $translations
* @mixin \Eloquent
* @property int $id
* @property int $active
* @property string $title
* @property int $product_group_behaviour_id
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup whereProductGroupBehaviourId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup whereUpdatedAt($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Properties\Models\Property[] $properties
*/
class ProductGroup extends AbstractTranslatableModel implements ProductableInterface, CategorizableInterface, PropertizableInterface, HasImagesInterface
{
use Searchable;
public function searchableAs()
{
return 'products_groups_index';
}
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title,
];
}
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['title'];
public function productGroupBehaviour()
{
return $this->belongsTo(ProductGroupBehaviour::class);
}
public function products()
{
return $this->belongsToMany(Product::class);
}
public function composites()
{
return $this->belongsToMany(ProductComposite::class);
}
/**
* We belong to many categories
*
* @return MorphToMany
*/
public function categories(): MorphToMany
{
return $this->morphToMany(Category::class, 'product_categorizable');
}
/**
* Returns properties
* Example key: color
* Example value of key color: green
*
* @return MorphToMany
*/
function properties(): MorphToMany
{
return $this->morphToMany(Property::class, 'propertizable');
}
/**
* Get the images from the current user
*
* @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', '=', ProductGroup::class);
}
public function translations(): HasMany
{
return $this->hasMany(ProductGroupTranslation::class);
}
/**
* Returns the lowest price possible for the group.
* Also known as "entry price"
*
* @return float
*/
public function getPrice(): float
{
/** @var ProductGroupBehaviour $behaviour */
$behaviour = $this->productGroupBehaviour()->first();
$price = 0;
switch ($behaviour->title)
{
case 'and':
$products = $this->products()->where('active', '=', '1')->get(['products.id', 'products.price']);
$products->each(function(Product $product) use (&$price) {
$price += $product->price;
});
break;
case 'or':
case 'xor':
/** @var Product $product */
$product = $this->products()->where('active', '=', '1')->orderBy('products.price', 'asc')->first(['products.id', 'products.price']);
if($product) $price += $product->getPrice();
break;
default:
throw new \RuntimeException("The productgroup cannot handle behaviour '".$behaviour->title."' because it was not implemented");
}
return $price;
}
}