<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Traits\ModelTimeFormat;

class ProcessesContent extends Model
{
    use ModelTimeFormat;
    protected $fillable=[
        'process_id','bill_type','commodity_id','wms_code','amount','type'
    ];
    protected $appends=[
        'commodity_name',
        'sign_commodity_name_mark',
        'sign_commodity_sku_mark',
    ];

    public function process(){
        return $this->belongsTo('App\Process','process_id','id');
    }
    public function commodity(){
        return $this->belongsTo('App\Commodity','commodity_id','id');
    }
    public function signCommodityName(){
        return $this->hasOne('App\Sign','signable_id','id')
            ->where('signable_type','processes_contents')->where('field','commodity_name');
    }
    public function signCommoditySku(){
        return $this->hasOne('App\Sign','signable_id','id')
            ->where('signable_type','processes_contents')->where('field','commodity_sku');
    }

    public function getCommodityNameAttribute()
    {
        return $this['commodity']? $this['commodity']['name']:null;
    }
    public function getSignCommodityNameMarkAttribute()
    {
        return $this['signCommodityName']? $this['signCommodityName']['mark']:null;
    }
    public function getSignCommoditySkuMarkAttribute()
    {
        return $this['signCommodityName']? $this['signCommoditySku']['mark']:null;
    }
}
