<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\BrandDb;

class Transfer extends Model
{
    protected $fillable = [
        'tp', 'transfer_from', 'transfer_to',
        'tpe', 'date_tpe', 'user_tpe', 'tpr',
        'date_tpr', 'status', 'commits', 'logs', 'user_create', 'vendor', 'total', 'brand', 'usernav','TrackingCode','PdfGuia','PdfRotulo'
    ];

    public function detail()
    {
        return $this->hasMany(TransferDetail::class)->orderBy('id', 'ASC');
    }

    public function scopeTp($query, $tp)
    {
        if ($tp) {
            return  $query->where('tp', $tp)
                ->orWhere('commits', 'ilike', "%{$tp}%");
        }
    }

    public function brandName()
    {
        return BrandDb::where('Code',$this->brand)->first();
    }
    
    public function scopeStatus($query, $status)
    {
        if ($status) {
            return $query->where('status', $status);
        }
    }
    public function scopeType($query, $type, $store)
    {
        if ($type) {
            switch ($type) {
                case 'torigen':
                    return $query->where('transfer_from', $store);
                    break;
                case 'tdestino':
                    return $query->where('transfer_to', $store);
                    break;
                case 'tod':
                    return $query->where('transfer_from', '=', $store)
                        ->where('transfer_to', '=', $store);
                    break;
            }
        }
    }

    public function scopeBrand($query, $brand)
    {
        if ($brand) {
            return $query->where('brand', $brand);
        }
    }
    public function scopeVendor($query, $vendor)
    {
        if ($vendor) {
            return $query->where('vendor', $vendor);
        }
    }
}
