<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use App\Traits\ModelTimeFormat;
use Illuminate\Support\Facades\Date;

class UserDutyCheck extends Model
{

    use ModelTimeFormat;
    public $timestamps=false;

    protected $fillable=[
        'user_id','checked_at','confirmed_by','type','source','workgroup_id',
    ];

    public function userDetail(){
        return $this->belongsTo('App\UserDetail','user_id','user_id');
    }

    public function userLabor(){
        return $this->belongsTo('App\UserLabor','user_id','user_id');
    }
    public function userWorkgroup(){
        return $this->belongsTo('App\UserWorkgroup','workgroup_id','id');
    }
    public function laborReport(){
        return $this->hasOne('App\LaborReport');
    }

    public function laborEnterCheck(){
        $this->makeEnteringRecord();
    }
    //进场创建临时工报表信息
     function makeEnteringRecord(){
        $userDetail=UserDetail::find($this['user_id']);
        $name=$userDetail['full_name'];
        $mobile_phone=$userDetail['mobile_phone'];
        $identity_number=$userDetail['identity_number'];
        $labor_company_id=UserLabor::where('user_id',$this['user_id'])->value('labor_company_id');
        $labor_company=LaborCompany::where('id',$labor_company_id)->value('name');
        $laborReport=new LaborReport([
            'user_id'=>$this['user_id'],
            'name'=>$name,
            'mobile_phone'=>$mobile_phone,
            'identity_number'=>$identity_number,
            'labor_company'=>$labor_company,
            'user_duty_check_id'=>$this['id'],
        ]);
        $laborReport->save();
        $laborReportStatus=new LaborReportStatus([
            'labor_report_id'=>$laborReport['id'],
            'status'=>'未审核',
            'created_at'=>$this['checked_at'],
        ]);
        $laborReportStatus->save();
        $laborReport['enter_number']=$laborReport->makeOrGetEnteringNumber();
        $laborReport->update();
        return $laborReport;
    }
}
