<?php

namespace modules\cors\models;


use Yii;
use modules\cors\models\Licensecatbe;
use modules\users\models\User;
/**
 * This is the model class For table "licensedata".
 *
 * @property string $id
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $created_by
 * @property string $updated_by
 * @property string $deleted_by
 * @property integer $deleted_at
 * @property integer $deleted
 * @property string $actid
 * @property string $status
 * @property string $padno
 * @property integer $jobno
 * @property string $employee
 * @property string $name
 * @property string $licensenumber
 * @property string $dob
 * @property string $issuingbody
 * @property integer $issuedate
 * @property integer $expirydate
 * @property string $address
 */
class Licensedatabe extends \common\models\Base {
	
	public $dates = ['dob', 'expirydate', 'issuedate'];
	public $uniqueidentifiers = ['actid', 'employee'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'licensedata';
    }

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['id', 'created_by', 'updated_by', 'deleted_by', 'actid', 'status', 'padno', 'employee', 'name', 'licensenumber', 'issuingbody', 'address'], 'string'],
            [['created_at', 'updated_at', 'deleted_at', 'deleted', 'jobno', ], 'integer'],
            [['issuedate', 'expirydate', 'dob', ], 'safe'],
            [['id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'createdby.fullname' => 'Created By',
            'updatedby.fullname' => 'Updated By',
            'deleted_by' => 'Deleted By',
            'deleted_at' => 'Deleted At',
            'deleted' => 'Deleted',
            'actid' => 'Actid',
            'status' => 'Status',
            'padno' => 'Padno',
            'jobno' => 'Jobno',
            'employee' => 'Employee',
            'name' => 'Name',
            'licensenumber' => 'License number',
            'dob' => 'Dob',
            'issuingbody' => 'Issuing body',
            'issuedate' => 'Issue date',
            'expirydate' => 'Expiry date',
            'address' => 'Address',
            'activepoints' => 'Active Points',
            'user.fullname' => 'Name'
        ];
    }
    
    public function getFiles() {
		return $this->hasMany(\backend\models\Uploadsbe::class, ['relatedid' => 'id']);
	}
	public function getCats() {
		if (Yii::$app->controller->action->id == 'create') {
			$tempid = Yii::$app->session->get('ldtempid');
				return Licensecatbe::find()->where(['relatedid' => "$tempid"])->all();
		} else {
			return $this->hasMany(Licensecatbe::class, ['relatedid' => 'id']);	
		}
	}
	
	public function getEndorsements() {
		return $this->hasMany(\modules\cors\models\Licenseendorsementsbe::class, ['relatedid' => 'id'])->where(['status' => 'Active']);
	}
	
	public function getActivepoints() {
		$sum = 0;
		foreach ($this->endorsements as $key => $model) {
			$sum += $model->pointsawarded;
		}
		return $sum;
	}
	public function getUser() {
		return $this->hasOne(User::class, ['id' => 'employee']);
	}
	

}
