%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/self/root/var/www/html/app/Models/
Upload File :
Create Path :
Current File : //proc/self/root/var/www/html/app/Models/Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'name_prod',
        'prod_type',
        'ref_prod',
        'prixachat',
        'prixvente',
        'date_debut',
        'date_fin',
        'remarques',
        'marque_id',
        'facture_id',
        'color_id',
        'category_id',
        'situation_id',
        'parent_id',
    ];

    protected $appends = [
        'in_stock',
        'count'
    ];

    public function color(){
        return $this->hasOne(Color::class, 'id', 'color_id');
    }

    public function situation(){
        return $this->hasOne(Situation::class, 'id', 'situation_id');
    }

    public function marque(){
        return $this->belongsTo(Marque::class, 'marque_id');
        
    }
    
    public function parent(){
        return $this->hasOne(static::class, 'id', 'parent_id');
    }

    public function references(){
        return $this->hasMany(static::class,'parent_id', 'id');
    }
    
    public function facture(){
        return $this->hasOne(Facture::class, 'id', 'facture_id');
    }

    public function category(){
        return $this->hasOne(Category::class, 'id', 'category_id');
    }

    public function commande() {
        return $this->hasOne(Commande_product::class, 'product_id', 'id')->whereHas('commande', function($query) {
            return $query->whereNotIn('status', ['cancelled', 'returned']);
        });
    }

    public function getInStockAttribute() {
        
        if( !$this->commande ) {
            return true;
        }

        return false;
    }

    public function getCountAttribute() {
        if( $this->prod_type != 'Produit' ) {
            return '-';
        }
        if( $this->parent_id ) {
            return $this->in_stock ? 1 : 0;
        } else {
            $count = 0;
            foreach ($this->references as $ref) {
                $count = $count + $ref->count;
            }
            return $count;
        }
    }
}

Zerion Mini Shell 1.0