<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "discount".
 *
 * @property integer $id_warehouse
 * @property string $name
 * @property integer $status
 * @property integer $type
 * @property integer $value
 * @property integer $product_enabled
 * @property integer $ticket_enabled
 * @property string $created_at
 * @property string $updated_at
 */
class Discount extends \yii\db\ActiveRecord
{
	const STATUS_DELETED = 0;
	const STATUS_ACTIVE = 10;
	
	const TYPE_DEFAULT = 0;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'discount';
    }
    
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
    	return [
    			[	'class' => TimestampBehavior::className(),
    					'value' => function(){ return date('Y-m-d H:i:s' ); }
    			]
    			];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name','value'], 'required'],
            [['status', 'type', 'value','product_enabled','ticket_enabled'], 'integer'],
            [['name'], 'string', 'max' => 64]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_warehouse' => Yii::t('common/discount', 'Id Warehouse'),
            'name' => Yii::t('common/discount', 'Name'),
            'status' => Yii::t('common/discount', 'Status'),
            'type' => Yii::t('common/discount', 'Type'),
            'value' => Yii::t('common/discount', 'Value'),
            'created_at' => Yii::t('common/discount', 'Created At'),
            'updated_at' => Yii::t('common/discount', 'Updated At'),
            'product_enabled' => "Termékre adható",
            'ticket_enabled' => "Bérletre adható",
        ];
    }
    
    
    static function statuses() {
    	return [
    			self::STATUS_ACTIVE => Yii::t('common/discount', 'Active'),
    			self::STATUS_DELETED => Yii::t('common/discount', 'Inactive'),
    	];
    }
    
    public function getStatusHuman(){
    	$result = null;
    	$s = self::statuses($this->status);
    	if ( array_key_exists($this->status, $s)){
    		$result = $s[$this->status];
    	}
    	return $result;
    }
    
    static function types() {
    	return [
    			self::TYPE_DEFAULT => Yii::t('common/discount', 'Discount'),
    	];
    }
    
    public function getTypeHuman(){
    	$result = null;
    	$s = self::types($this->type);
    	if ( array_key_exists($this->type, $s)){
    		$result = $s[$this->type];
    	}
    	return $result;
    }
    
    public function getProcutEnabledHuman(){
    	return $this->product_enabled == 1 ? 'Igen' : 'Nem';
    }
    
    public function getTicketEnabledHuman(){
    	return $this->ticket_enabled == 1 ? 'Igen' : 'Nem';
    }
    
    public static  function read(){
    	return Discount::find()->andWhere([ 'status' => self::STATUS_ACTIVE ])->all();
    }
    
    
    public static  function readProductDiscounts(){
    	return Discount::find()->andWhere([ 'status' => self::STATUS_ACTIVE , 'product_enabled' => 1 ])->all();
    }
    

    public static  function readTicketDiscounts(){
    	return Discount::find()->andWhere([ 'status' => self::STATUS_ACTIVE , 'ticket_enabled' => 1 ])->all();
    }
    
    /**
     * @param integer $money
     * @param common\models\Discount $discount
     * */
    public static function applyDiscount($money,$discount){
    	$result  = $money;
    	$valueOfDiscount =  floor( $money  * $discount->value / 100 );
    	$result = $money - $valueOfDiscount;
    	return $result;
    }
    
    
    public static function modelsToArray($discounts,$default = []){
    
    	if ( $discounts == null ){
    		return $default;
    	}
    
    	return ArrayHelper::toArray($discounts, [
    			'common\models\Discount' => [
    			'id_discount',
    			'value',
    			],
    			]);
    }
    
}
