<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "ticket_type".
 *
 * @property integer $id_ticket_type
 * @property string $name
 * @property integer $type
 * @property integer $max_usage_count
 * @property integer $time_unit_type
 * @property integer $time_unit_count
 * @property integer $price_brutto
 * @property integer $id_account
 * @property integer $flag_student
 * @property integer $status
 * @property integer installment_enabled
 * @property integer installment_count
 * @property integer installment_money
 * @property string $created_at
 * @property string $updated_at
 */
class TicketType extends \common\models\BaseFitnessActiveRecord {
	const STATUS_DELETED = 0;
	const STATUS_ACTIVE = 10;
	CONST TIME_UNIT_DAY = 10;//nap
	CONST TIME_UNIT_MONTH = 20;//hónap
	CONST TIME_UNIT_MONTH_REFERENCE = 30; //tárgy hónap
	const TYPE_NORMAL = 10;
	const TYPE_DEFAULT = self::TYPE_NORMAL;
	
	const FLAG_STUDENT_OFF = 0;
	const FLAG_STUDENT_ON = 1;
	
	const INSTALLMENT_OFF = 0;
	const INSTALLMENT_ON = 1;
	
	/**
	 * @inheritdoc
	 */
	public static function tableName() {
		return 'ticket_type';
	}
	
	/**
     * @formatter:off
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'id_account','time_unit_count','type' ,'time_unit_type' ,'max_usage_count','price_brutto'], 'required'],
            ////////////////
            //price brutto
            ////////////////
            [[  'price_brutto',     ], 'integer'],
            ////////////////
            //time_unit_type
            ////////////////
            [['time_unit_type',], 'integer'],
            [['time_unit_type',], 'in', 'range' => [ self::TIME_UNIT_DAY,self::TIME_UNIT_MONTH,self::TIME_UNIT_MONTH_REFERENCE] ],
            ////////////////
            //time_unit_count
            ////////////////
            [['time_unit_count',], 'integer','min' => 1 , 'max' => 100],
            ////////////////
            //max_usage_count
            ////////////////
            [['max_usage_count',], 'integer','min' => 0 , 'max' => 10000],
            ////////////////
            //flag_student
            ////////////////
            [['flag_student',], 'integer'],
            [['flag_student',], 'in', 'range' => [  self::FLAG_STUDENT_OFF, self::FLAG_STUDENT_ON  ]],
            ////////////////
            //status
            ////////////////
            [['status',], 'integer'],
            [['status',], 'in', 'range' => [  self::STATUS_ACTIVE, self::STATUS_DELETED  ]],
            ////////////////
            //type
            ////////////////
            [['type',], 'integer'],
            [['type',], 'in', 'range' => [  self::TYPE_NORMAL  ]],
            ////////////////
            //name
            ////////////////
            [['name'], 'string', 'max' => 64],
            ////////////////
            //id_account
            ////////////////
        	[['id_account',], 'integer'],
        	[['id_account',], 'validateIdAccount'],
        		
        	/////////////////////
        	//INSTALLMENT ENABLED
        	/////////////////////
        	[['installment_enabled',], 'integer'],
        	[['installment_enabled',], 'in', 'range' => [  self::INSTALLMENT_ON, self::INSTALLMENT_OFF  ]],
        		
        	[['installment_money',], 'integer'],
        	[['installment_count',], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_ticket_type' => Yii::t('common/ticket_type', 'Id Ticket Type'),
            'name' => Yii::t('common/ticket_type', 'Name'),
            'type' => Yii::t('common/ticket_type', 'Type'),
            'max_usage_count' => Yii::t('common/ticket_type', 'Max Usage Count'),
            'time_unit_type' => Yii::t('common/ticket_type', 'Time Unit Type'),
            'time_unit_count' => Yii::t('common/ticket_type', 'Time Unit Count'),
            'price_brutto' => Yii::t('common/ticket_type', 'Price Brutto'),
            'id_account' => Yii::t('common/ticket_type', 'Id Account'),
            'flag_student' => Yii::t('common/ticket_type', 'Flag Student'),
            'status' => Yii::t('common/ticket_type', 'Status'),
            'created_at' => Yii::t('common/ticket_type', 'Created At'),
            'updated_at' => Yii::t('common/ticket_type', 'Updated At'),
            'installment_enabled' => Yii::t('common/ticket_type', 'Részlet fizetés a brutto áron felül'),
            'installment_count' => Yii::t('common/ticket_type', 'Havi részletek száma'),
            'installment_money' => Yii::t('common/ticket_type', 'Havi részlet összege'),
        ];
    }
    
    /**
	 * @formatter:on
	 */
	static function statuses() {
		return [ 
				self::STATUS_ACTIVE => Yii::t ( 'common/ticket_type', 'Active' ),
				self::STATUS_DELETED => Yii::t ( 'common/ticket_type', 'Inactive' ) 
		];
	}
	public function getStatusHuman() {
		$result = null;
		$s = self::statuses (  );
		if (array_key_exists ( $this->status, $s )) {
			$result = $s [$this->status];
		}
		return $result;
	}
	static function timeUnitTypes() {
		return [ 
				self::TIME_UNIT_DAY => Yii::t ( 'common/ticket_type', 'Day' ),
				self::TIME_UNIT_MONTH => Yii::t ( 'common/ticket_type', 'Month' ),
				self::TIME_UNIT_MONTH_REFERENCE => Yii::t ( 'common/ticket_type', 'Reference month' ) 
		];
	}
	public function getTimeUnitHuman() {
		$result = null;
		$s = self::timeUnitTypes ( $this->time_unit_type );
		if (array_key_exists ( $this->time_unit_type, $s )) {
			$result = $s [$this->time_unit_type];
		}
		return $result;
	}
	static function ticketTypes() {
		return [ 
				self::TYPE_NORMAL => Yii::t ( 'common/ticket_type', 'Normal' ) 
		];
	}
	public function getTypeHuman() {
		$result = null;
		$s = self::ticketTypes ();
		if (array_key_exists ( $this->type, $s )) {
			$result = $s [$this->type];
		}
		return $result;
	}
	public function getAccount() {
		return $this->hasOne ( Account::className (), [ 
				'id_account' => 'id_account' 
		] );
	}
	public function getAccountName() {
		return $this->account->name;
	}
	
	public function isStudent(){
		return  $this->flag_student == (  self::FLAG_STUDENT_ON);
	}
	public function isInstallment(){
		return  $this->installment_enabled == (  self::INSTALLMENT_ON);
	}
	
	
	public function validateIdAccount($attribute,$params){
		$account = null;
		if ( !$this->hasErrors("id_account")){
			$account = Account::findOne($this->$attribute);
				
			if ( !isset($account)){
				$this->addError($attribute,Yii::t('common/ticket_type','Invalid account!'));
			}else{

				//on update
				if ( !$this->isNewRecord ){
					//if selected account is inactive ...
					if ( $account->isInactive() ){
						//... and changed
						 if ( $this->isAttributeChanged('id_account')){
						 	$this->addError($attribute,Yii::t('common/ticket_type','Invalid account (inactive)!'));
						 }
					}
				}
			}
		}
	}

	/**
	 * $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive
	 * */
	public static  function read($forceIncludeObjectWithId = null, $account = null){
		$ticketTypes = null;
	
		if ( $forceIncludeObjectWithId  == null){
			$ticketTypes = TicketType::find()->andWhere(['status' => self::STATUS_ACTIVE  ])
			->andWhere([ '<>', 'installment_enabled' , '1'])	->andFilterWhere(['ticket_type.id_account' => $account])->all();
		}else{
			$ticketTypes = TicketType::find()->andWhere( ['or', ['status' => self::STATUS_ACTIVE], ['id_ticket_type' =>  $forceIncludeObjectWithId ] ])
			->andWhere([ '<>', 'installment_enabled' , '1'])->andFilterWhere(['ticket_type.id_account' => $account])->all();
		}
	
		return $ticketTypes;
	}
	
	
	public static function modelsToArray($models,$default = []){
	
		if ( $models == null ){
			return $default;
		}
	
		return ArrayHelper::toArray($models, [
				'common\models\TicketType' => [
						'name',
						'id_ticket_type',
						'max_usage_count',
						'time_unit_type',
						'time_unit_count',
						'id_account',
						'price_brutto',
						],
						]);
	}
	
	
	
}
