<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use common\components\Helper;

/**
 * This is the model class for table "contract".
 *
 * @property integer $id_contract
 * @property integer $id_user
 * @property integer $id_customer
 * @property integer $status
 * @property integer $flag
 * @property integer $part_paid
 * @property integer $part_count
 * @property integer $part_required
 * @property integer $id_ticket_type
 * @property string $expired_at
 * @property string $created_at
 * @property string $updated_at
 * @property string $started_at
 */
class Contract extends \yii\db\ActiveRecord
{
	
	public static $STATUS_PAID = 10;
	public static $STATUS_NOT_PAID = 20;
	
	public static $FLAG_DELETED = 10;
	public static $FLAG_CANCELED = 20;
	public static $FLAG_ACTIVE= 30;
	public static $FLAG_EXPIRED = 40;
	public static $FLAG_CANCELED_2 = 50;
	
	
	public function behaviors()
	{
		return ArrayHelper::merge(  [
				[
						'class' => TimestampBehavior::className(),
						'value' => function(){  return  Helper::getDateTimeString(); }
				],
				], parent::behaviors());
	}
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'contract';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//             [['id_user', 'id_customer', 'status', 'flag', 'part_paid', 'part_count', 'part_required'], 'integer'],
//             [['expired_at', 'created_at', 'updated_at'], 'required'],
//             [['expired_at', 'created_at', 'updated_at'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_contract' => Yii::t('common/contract', 'Szerződés azonosító'),
            'id_user' => Yii::t('common/contract', 'Felhasználó'),
            'id_customer' => Yii::t('common/contract', 'Vendég'),
            'status' => Yii::t('common/contract', 'Státusz'),
            'flag' => Yii::t('common/contract', 'Állapot'),
            'part_count' => Yii::t('common/contract', 'Részletek száma'),
            'part_paid' => Yii::t('common/contract', 'Fizetett részletek'),
            'part_required' => Yii::t('common/contract', 'Esedékes részlet'),
            'expired_at' => Yii::t('common/contract', 'Lejárati dátum'),
            'created_at' => Yii::t('common/contract', 'Létrehozva'),
            'updated_at' => Yii::t('common/contract', 'Módosítva'),
            'started_at' => Yii::t('common/contract', 'Érvényesség kezdete'),
        ];
    }
    
    public function getTicketType(){
    	return $this->hasOne(TicketType::className(), ['id_ticket_type' => 'id_ticket_type']);
    }
    public function getTicket(){
    	return $this->hasOne(Ticket::className(), ['id_contract' => 'id_contract']);
    }
    
    public function getCustomer(){
    	return $this->hasOne(Customer::className(), ['id_customer' => 'id_customer']);
    }
    public function getCustomerName(){
    	$customer = $this->customer;
    	$result = "";
    	
    	if ( isset($customer)){
    		$result = $customer->name;
    	}
    	
    	return $result;
    }
    public function getUser(){
    	return $this->hasOne(User::className(), ['id' => 'id_user']);
    }
    public function getUserName(){
    	$user = $this->user;
    	$result = "";
    	
    	if ( isset($user)){
    		$result = $user->username;
    	}
    	
    	return $result;
    }
    
    public function getRequests(){
    	return $this->hasMany(TicketInstallmentRequest::className(), ['id_contract' => 'id_contract']);
    }
    
    public function getDiscount(){
    	return $this->hasOne(Discount::className(), ['id_discount' => 'id_discount']);
    }
    
 
    
    public static function statuses(){
    	return [
    		static::$STATUS_NOT_PAID => "Nincs fizetve",
    		static::$STATUS_PAID => "Fizetve",
    	];
    }
    
    
    public static function flags(){
    	return [
    			static::$FLAG_ACTIVE => "Aktív",
    			static::$FLAG_CANCELED => "Felbontva",
    			static::$FLAG_CANCELED_2 => "Felbontva, újrakötés lehetőségével",
    			static::$FLAG_DELETED => "Törölve",
    			static::$FLAG_EXPIRED => "Lejárt",
    	];
    }
    
    public static function toStatusName($status){
    	return Helper::getArrayValue(self::statuses(), $status, "");
    }
    
    public static function toFlangName($flag){
    	return Helper::getArrayValue(self::flags(), $flag, "");
    }
    
    public static function getFlagsDoesNotProhibitNewContract(){
    	return [
    			static::$FLAG_CANCELED_2,
    			static::$FLAG_DELETED
    	];
    }
    
    public function getStatusName() {
    	return self::toStatusName($this->status);
    }
    
    public function getFlagName() {
    	return self::toFlangName($this->flag);
    }
    
    
    public function isStatusNotPaid(){
    	return $this->status == static::$STATUS_NOT_PAID;
    }
    
    public function canCancel() {
    	 return $this->flag == static::$FLAG_ACTIVE;
    }
    public function isFlagCanceled() {
    	 return $this->flag == static::$FLAG_CANCELED;
    }
    public function isFlagDeleted() {
    	 return $this->flag == static::$FLAG_DELETED;
    }
    
    public function isFlagActive() {
    	return $this->flag == static::$FLAG_ACTIVE;
    }
    
    
    
    public function getPriceMonthly(){
    	return $this->ticket->price_brutto;
    }
    
    public function getPartsTotal(){
    	$money = $this->getPriceMonthly();
    	$money = $money * $this->part_count;
    	return $money;
    }
    
    public function getPriceTotal(){
    	$money = $this->getPartsTotal();
    	$money = $money + $this->getPriceMonthly();
    	return $money;
    }
}
