<?php

namespace modules\cors\models;


use Yii;

/**
 * This is the model class For table "fueldata".
 *
 * @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 $relatedid
 * @property string $driver
 * @property integer $date
 * @property string $cardno
 * @property string $type
 * @property string $quantity
 * @property integer $odometer
 * @property string $ppl
 * @property string $price
 * @property string $vat
 * @property string $total
 */
class Fueldatabe extends \common\models\Base
{
	public $dates = ['date'];
	public $uniqueidentifiers = ['relatedid','actid','vehicle'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fueldata';
    }

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['id', 'created_by', 'updated_by', 'deleted_by', 'actid', 'status', 'padno', 'relatedid', 'driver', 'cardno', 'type', 'vehicle'], 'string'],
            [['created_at', 'updated_at', 'deleted_at', 'deleted', 'jobno', 'odometer'], 'integer'],
            [['quantity', 'ppl', 'price', 'vat', 'total'], 'number'],
            [['date'], 'safe'],
            [['id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'deleted_by' => 'Deleted By',
            'deleted_at' => 'Deleted At',
            'deleted' => 'Deleted',
            'actid' => 'Actid',
            'status' => 'Status',
            'padno' => 'Padno',
            'jobno' => 'Jobno',
            'relatedid' => 'Relatedid',
            'driver' => 'Driver',
            'date' => 'Date',
            'cardno' => 'Cardno',
            'type' => 'Type',
            'quantity' => 'Quantity',
            'odometer' => 'Odometer',
            'ppl' => 'Ppl',
            'price' => 'Price',
            'vat' => 'Vat',
            'total' => 'Total',
        ];
    }
    public function getPreviousfill() {
		return Self::find()->where(['relatedid' => $this->relatedid])->andWhere(['not', ['=','id', $this->id]])->andWhere(['<','date', strtotime(str_replace('/','-',$this->date))])->orderby(['date' => SORT_DESC])->one();
		
	}
	public function getPreviousmileage() {
		return isset($this->previousfill) ? $this->previousfill->odometer : null ;
	}
	public function getMpg() {
		$tot = $this->odometer - $this->previousmileage;
		$gal = $this->quantity / 4.54609;
		return number_format($tot/$gal,2);
	}
	
	public function getLifetime() {
		$miles = 0;
		$litres = 0;	
		$data =  Self::find()->where(['relatedid' => $this->relatedid])->orderby(['date' => SORT_DESC])->all();
		foreach ($data as $a => $fuel) {
			$miles += ($this->odometer - $this->previousmileage);
			$litres += $this->quantity;
		}
		return ['miles' => $miles, 'litres' => $litres, 'mpg' =>number_format(($miles / ($litres/4.54609)),2)];
	}
	
    public function getVehicle() {
		return $this->hasOne(\modules\cors\models\Vehiclebe::class, ['id' => 'relatedid']);
		
	}
    
    public function getRows() {
    if (Yii::$app->controller->action->id == 'create') {
			$tempid = Yii::$app->session->get('fueldatatempid');
				return SELF::find()->where(['relatedid' => "$tempid"])->all();
		} else {
			return $this->hasMany(SELF::class, ['relatedid' => 'id']);	
		}
	}

}
