<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "linea".
 *
 * @property string $id
 * @property string $producto_id
 * @property string $numero
 * @property string $icc
 * @property string $folio
 * @property string $monto_id
 * @property string $folior
 * @property string $created
 * @property string $modified
 * @property string $created_by
 * @property integer $modified_by
 *
 * @property Productos $producto
 * @property Recargas $monto
 * @property User $createdBy
 */
class Linea extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'linea';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['producto_id', 'monto_id', 'numero', 'icc', 'folio', 'folior'], 'required'],
            [['numero'], 'string', 'length' => [12, 13], 'message' => 'Telefono debe ser al menos de 10 dígitos'],
            [['icc', 'folio', 'folior'], 'filter', 'filter' => 'strtoupper'],
            [['producto_id', 'monto_id', 'created_by', 'modified_by'], 'integer'],
            [['created', 'modified'], 'safe'],
            [['numero', 'icc', 'folio', 'folior'], 'string', 'max' => 255],
            //[['producto_id'], 'exist', 'skipOnError' => true, 'targetClass' => Productos::className(), 'targetAttribute' => ['producto_id' => 'id']],
            [['monto_id'], 'exist', 'skipOnError' => true, 'targetClass' => Recargas::className(), 'targetAttribute' => ['monto_id' => 'id']],
                //[['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => \common\models\User::className(), 'targetAttribute' => ['created_by' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'producto_id' => 'Producto',
            'numero' => 'Telefono',
            'icc' => 'ICC',
            'folio' => 'Folio de Registro',
            'monto_id' => 'Monto de Recarga',
            'folior' => 'Folio de Recarga',
            'pehua' => 'Orden Pehua',
            'created' => 'Fecha creación',
            'modified' => 'Fecha modificación',
            'created_by' => 'Creado por',
            'modified_by' => 'Modificado por',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProducto() {
        return $this->hasOne(Productos::className(), ['id' => 'producto_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMonto() {
        return $this->hasOne(Recargas::className(), ['id' => 'monto_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCreatedBy() {
        return $this->hasOne(\common\models\User::className(), ['id' => 'created_by']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getModifiedBy() {
        return $this->hasOne(\common\models\User::className(), ['id' => 'modified_by']);
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->created = date_create()->format('Y-m-d H:i:s');
                $this->modified = date_create()->format('Y-m-d H:i:s');
                $this->created_by = Yii::$app->user->identity->id;
                $this->modified_by = Yii::$app->user->identity->id;
            } else {
                $this->modified = date_create()->format('Y-m-d H:i:s');
                $this->modified_by = Yii::$app->user->identity->id;
            }
            return true;
        }
        return false;
    }

}
