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/blijegasten/blijegasten.be/app/Komma/Shop/Products/Product/Product.php
<?php

namespace App\Komma\Shop\Products\Product;

use App\Komma\Documents\DocumentsTrait;
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\Kms\Core\Entities\HasSearchableAttributesForSidebarInterface;
use App\Komma\Shop\Categories\Kms\CategorizableInterface;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Discounts\DiscountableInterface;
use App\Komma\Shop\Discounts\Discount;
use App\Komma\Shop\Products\ProductableInterface;
use App\Komma\Shop\Products\ProductGroup\ProductGroup;
use App\Komma\Shop\Properties\Models\PropertizableInterface;
use App\Komma\Shop\Properties\Models\Property;
use App\Komma\Shop\Vat\VatService;
use App\Komma\Shop\Vat\TransientVatProperties;
use App\Komma\Shop\Vat\VatServiceInterface;
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 Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;

/**
 * Represents a basic product that a user can put in a cart
 * 
 * Class Product
 *
 * @package App
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Categories\Models\Category[] $categories
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $documents
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Products\ProductGroup\ProductGroup[] $groups
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Sites\Models\Site[] $sites
 * @property-read \App\Komma\Shop\Products\Product\ProductTranslation $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\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 int $stock
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product whereStockKeepingUnit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Properties\Models\Property[] $properties
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Products\Product\Product query()
 */
class Product extends AbstractTranslatableModel implements ProductableInterface, CategorizableInterface, DocumentableInterface, DisplayNameInterface, HasSearchableAttributesForSidebarInterface
{
    use TransientVatProperties;
//    use Searchable;
    use DisplayNameTrait;
    use DocumentsTrait;
    use SoftDeletes;

//    public function searchableAs()
//    {
//        return 'products_index';
//    }
//
//    public function toSearchableArray()
//    {
//        return [
//            'id' => $this->id,
//            'stock_keeping_unit' => $this->stock_keeping_unit
//        ];
//    }

    /**
     * Possible type of deposit
     *
     * @var array
     */
    const DEPOSIT_TYPES = [
        1 => 'by_quantity',
        2 => 'free',
    ];

    /**
     * Returns an array containing model attributes with their values,
     * that will be used to search trough in the sidebar.
     *
     * @return array
     */
    public function searchableAttributesForSidebar(): array
    {
        return [
            'stock_keeping_unit' => $this->stock_keeping_unit
        ];
    }

    /** @var Discount[] $discounts  */
    private $discounts = [];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['active', 'price', 'price_excluding_vat', 'stock_keeping_unit', 'custom_vatrate', 'stock', 'deposit_type', 'shortage_price'];

    public function groups()
    {
        return $this->belongsToMany(ProductGroup::class);
    }


    /**
     * We belong to many categories
     *
     * @return MorphToMany
     */
    public function categories(): MorphToMany
    {
        return $this->morphToMany(Category::class, 'product_categorizable');
    }

    public function translations(): HasMany
    {
        return $this->hasMany(ProductTranslation::class);
    }

    /**
     * Get the site for this model
     *
     * @return BelongsToMany
     */
    public function sites(): BelongsToMany
    {
        return $this->belongsToMany(Site::class);
    }

    /**
     * Price is in cents
     *
     * @return float
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function getTotal(): float
    {
//        /** @var VatServiceInterface $vatService */
//        $vatService = app()->make(VatServiceInterface::class);
//        $this->productable = $vatService->calculateVatForProductable($this);
        return $this->price;
    }

    /**
     * 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;
    }

    /**
     * Scope a query to only include favorite products.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeFavorite($query)
    {
        return $query->where('favorite', '=', '1');
    }

    /**
     * Scope a query to only include new products.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeNew($query)
    {
        return $query->where('new', '=', '1');
    }

    /**
     * Get the documents belonging to this model
     *
     * @return MorphMany
     */
    public function downloads():MorphMany
    {
        return $this->morphMany(Document::class, 'documentable')
            ->orderBy('sort_order')
            ->where('key', 'Documents-product_documents');
    }

    /**
     * @return string
     */
    public function getSidebarName():string
    {
        // If there is no name defined generate a generic name
        if(!$sidebarName = $this->getDisplayName()){
            $translationKey = Str::camel($this->getTable()).'.entity';

            $translation = __('shop/'.$translationKey);
            if($translation !== 'shop/'.$translationKey) $sidebarName = $translation;
            else $sidebarName = __('kms/'.$translationKey);

            $sidebarName .= ' '.$this->id;
        }

        if(isset($this->categories) && $this->categories->isNotEmpty()) {
            $sidebarName .= '<br/><sub>' . $this->categories->first()->translation->name . '</sub>';
        }

        return $sidebarName;
    }
}