<?php

namespace App;

use App\Services\LogisticService;
use App\Services\OwnerService;
use App\Traits\HasCompositePrimaryKey;
use App\Traits\ModelTimeFormat;
use Illuminate\Database\Eloquent\Model;

use App\Traits\ModelLogChanging;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class OracleDOCOrderHeader extends Model
{
    use ModelLogChanging;

    use ModelTimeFormat;
    protected $connection="oracle";
    protected $table="DOC_Order_Header";
    protected $primaryKey="ORDERNO";
    public $timestamps=false;
    function getIncrementing()
    {
        return false;
    }


    protected $appends=[
        'oracleBASCustomer_descr_c',
        'oracleBASCode_codename_c'
    ];
    protected $fillable=[
        'NOTES','ReleaseStatus','SoStatus','WaveNo'
    ];

    /*
     * column: OrderNo=>编号,SOStatus=>订单状态,WAREHOUSEID=>仓库,CustomerID=>客户,
     *         ConsigneeID=>收货人,ConsigneeName=>收货人名称,C_Tel1=>收货人电话,CarrierName=>承运人,
     *         IssuePartyName=>下单方名称,WaveNo=>波次编号,SOReference1=>客户订单号,soreference5=>快递单号,EDISENDFLAG2=>快递获取标记,
     *         EDISendTime2=>快递获取时间,Notes=>备注,ERPCANCELFLAG=>接口取消标记,
     *         Picking_Print_Flag=>拣货单打印标记,EDISENDFLAG=>接口回传标记,ReleaseStatus=>订单冻结标记,C_Address1=>收货人地址,
     * */

    public function oracleDOCOrderDetails(){
        return $this->hasMany('App\OracleDOCOrderDetail','orderno','orderno');
    }
    public function oracleBASCustomer(){
        return $this->hasOne('App\OracleBasCustomer','customerid','customerid');
    }
    public function oracleBASCode(){
        return $this->hasOne('App\OracleBasCode','code','sostatus')->where('codeid','SO_STS');
    }
    public function actAllocationDetails(){
        return $this->hasMany('App\OracleActAllocationDetails','orderno','orderno');
    }
    public function getOracleBASCustomerDescrCAttribute(){
        return $this['oracleBASCustomer']['descr_c']??'';
    }
    public function getOracleBASCodeCodenameCAttribute(){
        return $this['oracleBASCode']['codename_c'] ??'';
    }
    public function hasUn(){
        return $this->hasMany('App\OracleDOCOrderDetail','orderno','orderno');
    }
    public function orderType(){
        return $this->hasOne('App\OracleBasCode','code','ordertype')->where('codeid','SO_TYP');
    }

    public function oracleDOCWaveDetail(): BelongsTo
    {
        return $this->belongsTo(OracleDOCWaveDetails::class,'orderno','orderno');
    }
    public function getLogistic()
    {
        /** @var LogisticService $logistic_service */
        $logistic_service = app('LogisticService');
        if(!$this['carrierid'])return null;
        return $logistic_service->getLogisticByCode($this['carrierid']);
    }

    public function getOwner()
    {
        /** @var OwnerService $owner_service */
        $owner_service = app('OwnerService');
        return $owner_service->codeGetOwner($this['customerid']);
    }
}
