<?php

namespace App;

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

use App\Traits\ModelLogChanging;

class ProcessesContent extends Model
{
    use ModelLogChanging;

    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',
        'sign_commodity_barcode_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 signCommodityBarcode(){
        return $this->hasOne('App\Sign','signable_id','id')
            ->where('signable_type','processes_contents')->where('field','commodity_barcode');
    }

    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['signCommoditySku']? $this['signCommoditySku']['mark']:null;
    }
    public function getSignCommodityBarcodeMarkAttribute()
    {
        return $this['signCommodityBarcode']? $this['signCommodityBarcode']['mark']:null;
    }
}
