HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Shop/Products/ProductGroup/ProductGroup.php
<?php

namespace App\Komma\Shop\Products\ProductGroup;

use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Documents\Models\Document;
use App\Komma\Kms\Core\AbstractTranslatableModel;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Shop\Categories\Kms\CategorizableInterface;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\ProductableInterface;
use App\Komma\Shop\Products\ProductComposite\ProductComposite;
use App\Komma\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use App\Komma\Shop\Properties\Models\PropertizableInterface;
use App\Komma\Shop\Properties\Models\Property;
use App\Komma\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;

/**
 * Groups products together (not like a product category)
 * to link behaviour.
 *
 * Class ProductGroup
 *
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Categories\Models\Category[] $categories
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Products\ProductComposite\ProductComposite[] $composites
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\ImageProcessing\Models\Image[] $images
 * @property-read \App\Komma\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour $productGroupBehaviour
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Products\Product\Product[] $products
 * @property-read \App\Komma\Shop\Products\ProductGroup\ProductGroupTranslation $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\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\Komma\Shop\Products\ProductGroup\ProductGroup whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup whereProductGroupBehaviourId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Properties\Models\Property[] $properties
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Sites\Models\Site[] $sites
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $documents
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\ProductGroup\ProductGroup query()
 */
class ProductGroup extends AbstractTranslatableModel implements ProductableInterface, CategorizableInterface, DocumentableInterface, PropertizableInterface, DisplayNameInterface
{
    use Searchable;
    use DisplayNameTrait;

    public function searchableAs()
    {
        return 'products_groups_index';
    }

    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];

    public function productGroupBehaviour()
    {
        return $this->belongsTo(ProductGroupBehaviour::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }

    public function composites()
    {
        return $this->belongsToMany(ProductComposite::class);
    }

    /**
     * Get the documents from the current model
     *
     * @return 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');
    }

    /**
     * Returns properties
     * Example key: color
     * Example value of key color: green
     *
     * @return MorphToMany
     */
    public function properties(): MorphToMany
    {
        return $this->morphToMany(Property::class, 'propertizable');
    }

    /**
     * Get the site for this model
     *
     * @return BelongsToMany
     */
    public function sites(): BelongsToMany
    {
        return $this->belongsToMany(Site::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->name) {
            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;
    }
}