<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\components\AccountAwareBehavior;
use common\components\UserAwareBehavior;
use common\components\DiscountAwareBehavior;
use yii\helpers\ArrayHelper;
use common\components\ProductAwareBehavior;
use common\components\CurrencyAwareBehavior;

/**
 * This is the model class for table "sale".
 *
 * @property integer $id_sale
 * @property integer $id_account
 * @property integer $id_discount
 * @property integer $id_currency
 * @property integer $id_product
 * @property integer $status
 * @property integer $type
 * @property integer $item_price
 * @property integer $count
 * @property integer $money
 * @property integer $money_currency
 * @property integer $rate
 * @property integer $id_user
 * @property string $comment
 * @property string $created_at
 * @property string $updated_at
 */
class Sale extends \yii\db\ActiveRecord
{
	const TYPE_PRODUCT = 10;
	
	const STATUS_NOT_PAID = 10;
	const STATUS_PAID = 20;
	const STATUS_DELETED= 30;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sale';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
    	return ArrayHelper::merge(  [
    			[
    			'class' => TimestampBehavior::className(),
    			'value' => function(){ return date('Y-m-d H:i:s' ); }
    	],
    	[
    	'class' => AccountAwareBehavior::className(),
    	],
    	[
    	'class' => UserAwareBehavior::className(),
    	],
    	[
    	'class' => DiscountAwareBehavior::className(),
    	],
    	[
    	'class' => ProductAwareBehavior::className(),
    	],
    	[
    	'class' => CurrencyAwareBehavior::className(),
    	],
    	], parent::behaviors());
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_sale' => Yii::t('common/sale', 'Id Sale'),
            'id_discount' => Yii::t('common/sale', 'Id Discount'),
            'id_currency' => Yii::t('common/sale', 'Id Currency'),
            'id_product' => Yii::t('common/sale', 'Id Product'),
            'status' => Yii::t('common/sale', 'Status'),
            'type' => Yii::t('common/sale', 'Type'),
            'item_price' => Yii::t('common/sale', 'Item Price'),
            'count' => Yii::t('common/sale', 'Count'),
            'money' => Yii::t('common/sale', 'Money'),
            'money_currency' => Yii::t('common/sale', 'Money Currency'),
            'rate' => Yii::t('common/sale', 'Rate'),
            'id_user' => Yii::t('common/sale', 'Id User'),
            'comment' => Yii::t('common/sale', 'Comment'),
            'created_at' => Yii::t('common/sale', 'Created At'),
            'updated_at' => Yii::t('common/sale', 'Updated At'),
        ];
    }
    
    
    public static function createSale($account, $discount, $currency, $count,$product , $status = Sale::STATUS_PAID){
    	$sale = new Sale();
    	 
    	$sale->type = Sale::TYPE_PRODUCT;
    	 
    	$sale->id_product  = $product->id_product;
    	 
    	$sale->item_price = $product->sale_price;
    	$totalPrice = $sale->item_price;
    	 
    	$sale->count = $count;
    	$totalPrice = $totalPrice * $count;
    	 
    	if ( isset( $discount ) ){
    		$sale->id_discount = $discount->id_discount;
    		$totalPrice =  Discount::applyDiscount( $totalPrice, $discount);
    	}
    	 
    	$sale->money = $totalPrice;
    	 
    	if ( isset( $currency ) ){
    		$sale->rate = $currency->rate;
    		$sale->money_currency = Currency::applyCurrency($totalPrice, $currency);
    	}
    	 
    	$sale->status = $status;
    	 
    	$sale->id_account = $account->id_account;
    	 
    	return $sale;
    }
    
    
    public static function statuses( ) {
    	return [
    			Sale::STATUS_PAID => 'Aktív',
    			Sale::STATUS_DELETED => 'Törölve',
    			Sale::STATUS_NOT_PAID => 'Nincs fizetve',
    	];
    }
    
    public static function toStatusName($id_status){
    	$result = "Ismeretlen";
    	$statuses = Sale::statuses();
    	if ( array_key_exists($id_status, $statuses)){
    		$result = $statuses[$id_status];
    	}
    	return $result;
    }
    
    public function getStatusName(){
    	return static::toStatusName($this->status);
    }
    
}
