<?php

namespace modules\cors\models;


use Yii;
use modules\users\models\User;
/**
 * This is the model class For table "vehicle".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $created_by2
 * @property integer $updated_at
 * @property integer $updated_by2
 * @property string $reg
 * @property string $equipmentid
 * @property string $created_by
 * @property string $updated_by
 * @property integer $deleted
 * @property integer $deleted_at
 * @property string $weight
 * @property string $category
 * @property string $fueltanksize
 * @property string $name
 * @property string $description
 * @property integer $firstd
 * @property integer $mileage
 * @property string $make
 * @property string $model
 * @property string $loadedweight
 */
class Vehiclebe extends \common\models\Base {
	public $dates = ['firstd'];
	public $uniqueidentifiers = [];
    public $securefields = [];/**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'vehicle';
    }

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['id', 'reg', 'equipmentid', 'created_by', 'updated_by', 'category', 'name', 'description', 'make', 'model', 'status','offsidefl', 'nearsidefl', 'offsiderl','nearsiderl','offsidefu', 'nearsidefu', 'offsideru','nearsideru','spare'], 'string'],
            [['mileage'], 'integer'],
            [['created_at', 'updated_at', 'deleted_at',],'safe'],
            [['weight', 'fueltanksize', 'loadedweight'], 'number'],
            [['firstd', ], 'safe'],
            [['id', 'reg'], 'unique'],
            [['deleted', ], 'boolean'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'created_by2' => 'Created By2',
            'updated_at' => 'Updated At',
            'updated_by2' => 'Updated By2',
            'reg' => 'Reg',
            'equipmentid' => 'Equipmentid',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'deleted' => 'Deleted',
            'deleted_at' => 'Deleted At',
            'weight' => 'Weight',
            'category' => 'Category',
            'fueltanksize' => 'Fueltanksize',
            'name' => 'Name',
            'description' => 'Description',
            'firstd' => 'Firstd',
            'mileage' => 'Mileage',
            'make' => 'Make',
            'model' => 'Model',
            'loadedweight' => 'Loadedweight',
        ];
    }
    public function getLookup() {
		return $this->hasMany(\backend\models\Lookuptablebe::class, ['sourceid' => 'id']);	
	}
	public function getReminders() {
		return $this->hasMany(\modules\cors\models\Vehicleremindersbe::class, ['id' => 'relatedid'])
		->via('lookup');
	}
	public function getTravel() {
		return $this->hasOne(\modules\cors\models\Travelbe::class, ['vehicle' => 'id'])->orderby(['finishmileage' => SORT_DESC]);
		
	}
	public function getDefects() {
        return $this->hasMany(\modules\forms\models\Forms::class, ['relatedid' => 'id'])
        ->onCondition(['form' => 'dt134']);
	}    
    public function getFiles() {
		return $this->hasMany(\backend\models\Uploadsbe::class, ['relatedid' => 'id'])
		->onCondition(['status' => 'Active']);
	}
	public function getFueldata() {
		return $this->hasMany(\modules\cors\models\Fueldatabe::class, ['relatedid' => 'id']);
	}
	public function getKeys() {
		return $this->hasMany(\modules\cors\models\Vehiclekeysbe::class, ['vehicle' => 'id']);
	}
	public function getServicehistory() {
		return $this->hasMany(\modules\cors\models\Vehicleservicehistorybe::class, ['relatedid' => 'id']);
	}
	public function getServiceintervals() {
		if (Yii::$app->controller->action->id == 'create') {
			$tempid = Yii::$app->session->get('shtempid');
				return \modules\cors\models\Vehicleservicingbe::find()->where(['relatedid' => "$tempid"])->all();
		} else {
			return $this->hasMany(\modules\cors\models\Vehicleservicingbe::class, ['relatedid' => 'id']);	
		}
	}	
	
    

}
