<?php

namespace modules\forms\models;

use modules\users\models\User;
use Yii;
use modules\activities\models\Activities;
use modules\custom\models\Accounts;

use yii\base\Model;
/**
 * This is the model class For table "dt122".
 *
 * @property integer $id
 * @property integer $injobno
 * @property string $weldid
 * @property string $dateinspected
 * @property string $site
 * @property string $elr
 * @property string $trackid
 * @property string $mileage
 * @property string $raillhrh
 * @property integer $company
 * @property string $weldersdepot
 * @property string $stampno
 * @property string $weldprocess
 * @property string $railprofilelh
 * @property string $steelgradelh
 * @property string $railwearlh
 * @property string $mouldused
 * @property string $portionno
 * @property string $batchno
 * @property string $railprofilerh
 * @property string $steelgraderh
 * @property string $railwearrh
 * @property string $widegapbatchno
 * @property string $widegapportionno
 * @property string $rt1
 * @property string $rt2
 * @property string $rt3
 * @property string $rt4
 * @property string $rt5
 * @property string $rt6
 * @property string $rt7
 * @property string $rt8
 * @property string $rt9
 * @property string $re1
 * @property string $re2
 * @property string $re3
 * @property string $re4
 * @property string $re5
 * @property string $re6
 * @property string $re7
 * @property string $re8
 * @property string $re9
 * @property string $rc1
 * @property string $rc2
 * @property string $rc3
 * @property string $rc4
 * @property string $rc5
 * @property string $rc6
 * @property string $rc7
 * @property string $rc8
 * @property string $rc9
 * @property string $rc10
 * @property string $rc11
 * @property string $rc12
 * @property string $rc13
 * @property string $rc14
 * @property string $rc15
 * @property string $rc16
 * @property string $rc17
 * @property string $rc18
 * @property string $rc19
 * @property string $rc20
 * @property string $rc21
 * @property string $rc22
 * @property string $rc23
 * @property string $rc24
 * @property string $rc25
 * @property string $rc26
 * @property string $rc27
 * @property string $rc28
 * @property string $rc29
 * @property string $rc30
 * @property string $rc31
 * @property string $rc32
 * @property string $rc33
 * @property string $rc34
 * @property string $rc35
 * @property string $rc36
 * @property string $rc37
 * @property string $rc38
 * @property string $rc39
 * @property string $rc40
 * @property string $rc41
 * @property string $rc42
 * @property string $weldsmadeatlineside
 * @property string $betweentensors
 * @property string $lowambtemp
 * @property string $steelsleepers
 * @property string $welderscomments
 * @property string $result
 * @property string $actionrequired
 * @property string $minactioncode
 * @property string $removalmethod
 * @property string $removedeadline
 * @property string $inspectorcomments
 * @property string $inspectiondate
 * @property string $insepectorid
 * @property string $inspectorsignature
 * @property string $uiccode
 * @property string $createdby
 * @property string $createdon
 * @property string $ModifiedBy
 *
 * @property Accounts $company0
 * @property Activities $injobno0
 */
class Dt122 extends Model {
	public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['insepectorid', 'actid'];
    public $securefields = [];
    
    public $id;
    public $actid;
    public $injobno;
    public $weldid;
    public $dateinspected;
    public $site;
    public $elr;
    public $trackid;
    public $mileage;
    public $raillhrh;
    public $company;
    public $weldersdepot;
    public $stampno;
    public $weldprocess;
    public $railprofilelh;
    public $steelgradelh;
    public $railwearlh;
    public $mouldused;
    public $portionno;
    public $batchno;
    public $railprofilerh;
    public $steelgraderh;
    public $railwearrh;
    public $widegapbatchno;
    public $widegapportionno;
    public $rt1;
    public $rt2;
    public $rt3;
    public $rt4;
    public $rt5;
    public $rt6;
    public $rt7;
    public $rt8;
    public $rt9;
    public $re1;
    public $re2;
    public $re3;
    public $re4;
    public $re5;
    public $re6;
    public $re7;
    public $re8;
    public $re9;
    public $rc1;
    public $rc2;
    public $rc3;
    public $rc4;
    public $rc5;
    public $rc6;
    public $rc7;
    public $rc8;
    public $rc9;
    public $rc10;
    public $rc11;
    public $rc12;
    public $rc13;
    public $rc14;
    public $rc15;
    public $rc16;
    public $rc17;
    public $rc18;
    public $rc19;
    public $rc20;
    public $rc21;
    public $rc22;
    public $rc23;
    public $rc24;
    public $rc25;
    public $rc26;
    public $rc27;
    public $rc28;
    public $rc29;
    public $rc30;
    public $rc31;
    public $rc32;
    public $rc33;
    public $rc34;
    public $rc35;
    public $rc36;
    public $rc37;
    public $rc38;
    public $rc39;
    public $rc40;
    public $rc41;
    public $rc42;
    public $weldsmadeatlineside;
    public $betweentensors;
    public $lowambtemp;
    public $steelsleepers;
    public $welderscomments;
    public $result;
    public $actionrequired;
    public $minactioncode;
    public $removalmethod;
    public $removedeadline;
    public $inspectorcomments;
    public $inspectiondate;
    public $insepectorid;
    public $inspectorsignature;
    public $uiccode;
    public $created_by;
    public $created_at;
    public $modifiedBy;
    public $signatureloc;

    public $rtpf;
    public $repf;
    public $wsignatureloc;
    public $weldersname;
    public $padno;
    public $jobno;
    public $deleted;
    public $updated_by;
    public $updated_at;
    public $deleted_by;
    public $deleted_at;
    public $created_by2;
    public $updated_by2;
    public $tracktrolley;
    
    public $printlayout = 'Portrait';
    public $form = 'DT122';
    public $formname = 'Thermic Weld Inspection Form (DT122)';
    public $createpath = '/forms/dt122/create';
    
    public $formLayout = '/dt122/_form.php';
    public $viewLayout = '@modules/forms/views/dt122/_view.php';


    /**
	 * @inheritdoc
	 */
    public function rules()
    {
        return [
            [['injobno', ], 'integer'],
            [['company', ], 'safe'],
            [['signatureloc', 'wsignatureloc','weldersname', 'insepectorid', 'rtpf','repf'], 'string'],
            [['weldid', 'dateinspected', 'site', 'elr', 'trackid', 'mileage', 'raillhrh', 'weldersdepot', 'stampno', 'weldprocess', 'railprofilelh', 'steelgradelh', 'railwearlh', 'mouldused', 'portionno', 'batchno', 'railprofilerh', 'steelgraderh', 'railwearrh', 'widegapbatchno', 'widegapportionno', 'rt1', 'rt2', 'rt3', 'rt4', 'rt5', 'rt6', 'rt7', 'rt8', 'rt9', 're1', 're2', 're3', 're4', 're5', 're6', 're7', 're8', 're9', 'rc1', 'rc2', 'rc3', 'rc4', 'rc5', 'rc6', 'rc7', 'rc8', 'rc9', 'rc10', 'rc11', 'rc12', 'rc13', 'rc14', 'rc15', 'rc16', 'rc17', 'rc18', 'rc19', 'rc20', 'rc21', 'rc22', 'rc23', 'rc24', 'rc25', 'rc26', 'rc27', 'rc28', 'rc29', 'rc30', 'rc31', 'rc32', 'rc33', 'rc34', 'rc35', 'rc36', 'rc37', 'rc38', 'rc39', 'rc40', 'rc41', 'rc42', 'weldsmadeatlineside', 'betweentensors', 'lowambtemp', 'steelsleepers', 'welderscomments', 'result', 'actionrequired', 'minactioncode', 'removalmethod', 'removedeadline', 'inspectorcomments', 'inspectiondate', 'inspectorsignature', 'uiccode', 'actid','padno' ], 'string', 'max' => 100],
			//Defaults
			[['deleted'], 'boolean'],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by','id'],'safe'],
			//Redundant
			[['created_by2', 'updated_by2'], 'integer'],
            
        ];
    }

    /**
	 * @inheritdoc
	 */


    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'injobno' => 'Job No',
            'weldid' => 'Weldid',
            'dateinspected' => 'Dateinspected',
            'site' => 'Site',
            'elr' => 'Elr',
            'trackid' => 'Trackid',
            'mileage' => 'Mileage',
            'raillhrh' => 'Raillhrh',
            'company' => 'Company',
            'weldersdepot' => 'Weldersdepot',
            'stampno' => 'Stampno',
            'weldprocess' => 'Weldprocess',
            'railprofilelh' => 'Railprofilelh',
            'steelgradelh' => 'Steelgradelh',
            'railwearlh' => 'Railwearlh',
            'mouldused' => 'Mouldused',
            'portionno' => 'Portionno',
            'batchno' => 'Batchno',
            'railprofilerh' => 'Railprofilerh',
            'steelgraderh' => 'Steelgraderh',
            'railwearrh' => 'Railwearrh',
            'widegapbatchno' => 'Widegapbatchno',
            'widegapportionno' => 'Widegapportionno',
            'rt1' => 'Rt1',
            'rt2' => 'Rt2',
            'rt3' => 'Rt3',
            'rt4' => 'Rt4',
            'rt5' => 'Rt5',
            'rt6' => 'Rt6',
            'rt7' => 'Rt7',
            'rt8' => 'Rt8',
            'rt9' => 'Rt9',
            're1' => 'Re1',
            're2' => 'Re2',
            're3' => 'Re3',
            're4' => 'Re4',
            're5' => 'Re5',
            're6' => 'Re6',
            're7' => 'Re7',
            're8' => 'Re8',
            're9' => 'Re9',
            'rc1' => "4701A||Incorrect preheat - distortion, discolouration",
            'rc2' => '4702||Use of incorrect moulds',
            'rc3' => '4703||Excessive flashing',
            'rc4' => '4704||Sand burns / paste scars',
            'rc5' => '4705||Metal splashes',
            'rc6' => '4706||Damage (Hammer marks)',
            'rc7' => '4707A||Weld location',
            'rc8' => '4707B||Bolt or bond holes',
            'rc9' => '4707C||Bond attachment or Bond scars',
            'rc10' => '4707D||Gall',
            'rc11' => '4707E||Direct fastenings or Longitudinal Timbers',
            'rc12' => '4707F||Heavy GCC',
            'rc13' => '4707G||Severe GCC',
            'rc14' => '4708||Weld not stamped or identified',
            'rc15' => '4709||Datum marks',
            'rc16' => '4711A||Group of gas pours (Ground head & collar)',
            'rc17' => '4711B||Isolated gas pour or surface cavity (Head of rail)',
            'rc18' => '4711C||Slag inclusion in ground head',
            'rc19' => '4712||Surface cavities or inclusions (Weld collar)',
            'rc20' => '474||Black holes',
            'rc21' => '4751||Vertical alignment - running surface',
            'rc22' => '4753A||Lateral alignment - running edge & gauge corner',
            'rc23' => '4753B||Alignment of gauge corner on new to worn rail',
            'rc24' => '4754||Lateral alignment - rail foot',
            'rc25' => '4761||Verticality',
            'rc26' => '4762||Sqareness across the foot width',
            'rc27' => '4763||Misalignment of mould joint - 2 piece mould',
            'rc28' => '4764||Misalignment of bottom slab and moulds - 3 piece',
            'rc29' => '4765||Incomplete weld collar formation / lack of fushion',
            'rc30' => '4766||Weld collar damage at vent riser positions',
            'rc31' => '4767||Trimming damage',
            'rc32' => '4771A||Cracking in ground head',
            'rc33' => '4771B||Hot tear in ground head',
            'rc34' => '4772B||Hot tear in weld collar',
            'rc35' => '4781||Abnormal or violent reaction',
            'rc36' => '4783||Delay between preheating & portion ignition',
            'rc37' => '4784||Abnormal tap time or outside the specific time band',
            'rc38' => '4785||Incorrect portion',
            'rc39' => '4786A||Tensor slip',
            'rc40' => '4786B||Rail movement',
            'rc41' => '4787||Insulating muffle not correctly applied',
            'rc42' => '4788||Weld made unprotected in wet weather',
            'weldsmadeatlineside' => 'Weldsmadeatlineside',
            'betweentensors' => 'Betweentensors',
            'lowambtemp' => 'Lowambtemp',
            'steelsleepers' => 'Steelsleepers',
            'welderscomments' => 'Welderscomments',
            'result' => 'Result',
            'actionrequired' => 'Actionrequired',
            'minactioncode' => 'Minactioncode',
            'removalmethod' => 'Removalmethod',
            'removedeadline' => 'Removedeadline',
            'inspectorcomments' => 'Inspectorcomments',
            'inspectiondate' => 'Inspectiondate',
            'insepectorid' => 'Insepectorid',
            'inspectorsignature' => 'Inspectorsignature',
            'uiccode' => 'Uiccode',
            'created_by' => 'Created By',
            'created_at' => 'Created At',
            'ModifiedBy' => 'Modified By',
            'signatureloc' => 'Signature Location',
        ];
    }

    public function getAccount()
    {
        return Accounts::findOne(['id' => $this->company]);
    }
    public function getActivity()
    {
		return Activities::findOne(['id' => $this->actid]);
	}
    public function getUser()
    {
        return User::findOne(['id' => $this->created_by]);
    }
    public function beforeValidate(){      
        if (Yii::$app->name !== 'app-console' ) {
            $this->padno = $this->padno == null ? User::Padno() : $this->padno;
        }
        return parent::beforeValidate();
    }

    
    public function setData()
    {
        if(Yii::$app->controller->action->id == 'create' && $actid = (Yii::$app->request->get('actid', null)) !== null ){
            $act = Activities::find()->select(['jobno', 'relatedid', 'location', 'sdate', 'elr', 'weekno', 'company'])->where(['id' => $actid])->asArray()->one();
            $this->injobno = $act['jobno'];
            $this->site = $act['location'];
            $this->elr = $act['elr'];
            $this->company = $act['relatedid'];
           // $this->weekno = $act['weekno'];
           // $this->date = date('d/m/Y H:i', strtotime($act['sdate']));
            $this->dateinspected = date('d/m/Y', strtotime($act['sdate']));
        }
    }
    public static function adminFormButtons($buttons, $model){
      //  $buttons['Inspection']['label'] = 'Inspection Forms';
        $_this = new self();
        $buttons['Inspection']['items'][] = [
            'label' => $_this->formname,
            'url' => [$_this->createpath, 'actid' => $model->id],
            'linkOptions' =>  ['target'=>'_blank'],
        ];
        return $buttons;
    }
}
