<?php

/**
 * This is the model class for table "configuracion.empresa".
 *
 * The followings are the available columns in table 'configuracion.empresa':
 *
 * @property int    $id
 * @property string $nom_emp
 * @property string $razon_social
 * @property string $rif_emp
 * @property string $siglas_emp
 * @property int    $sector_emp
 * @property int    $nro_reg_sudeca
 * @property string $cod_reg_rora
 * @property string $telf_emp
 * @property string $telf_2_emp
 * @property string $email_emp
 * @property string $dir_emp
 * @property string $logo
 * @property string $coletilla
 */
class Empresa extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'configuracion.empresa';
    }

    /**
     * @return array validation rules for model attributes
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return [
            ['sector_emp, nro_reg_sudeca', 'numerical', 'integerOnly' => true],
            ['nom_emp, razon_social', 'length', 'max' => 500],
            ['rif_emp', 'length', 'max' => 12],
            ['siglas_emp, email_emp', 'length', 'max' => 50],
            //array('cod_reg_rora', 'length', 'max'=>10),
            ['telf_emp, telf_2_emp', 'length', 'max' => 18],
            ['logo', 'length', 'max' => 300],
            ['coletilla, coletilla_planilla', 'length', 'max' => 1000],

            ['dir_emp,ruta_tienda,host,puerto,secure, preinscripcion,email_clave, cargar_planilla_firmada, actualizar_fecha_inscripcion', 'safe'],
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            ['id, nom_emp,ruta_tienda razon_social, rif_emp, siglas_emp, sector_emp, nro_reg_sudeca, cod_reg_rora, telf_emp, telf_2_emp, email_emp, dir_emp, logo, coletilla, coletilla_planilla', 'safe', 'on' => 'search'],
        ];
    }

    /**
     * @return array relational rules
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return [];
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'nom_emp' => 'Nombre de la empresa',
            'razon_social' => 'Razon social',
            'rif_emp' => 'RIF de la empresa',
            'siglas_emp' => 'Siglas empresa',
            'sector_emp' => 'Sector ',
            'ruta_tienda' => 'Ruta Tienda Virtual ',
            'nro_reg_sudeca' => 'Nro. de registro Sudeca',
            'cod_reg_rora' => 'Cod Reg Rora',
            'telf_emp' => 'Teléfono nro. 1',
            'telf_2_emp' => 'Teléfono  nro. 2',
            'email_emp' => 'Correo electrónico de la empresa',
            'dir_emp' => 'Dirección de la empresa',
            'logo' => 'Logo',
            'coletilla' => 'Coletilla',
            'preinscripcion' => 'Permitir preinscripción de asociado por inicio de sesión',
            'cargar_planilla_firmada' => 'Cargar planilla firmada',
            'actualizar_fecha_inscripcion' => 'Actualizar fecha de inscripción al editar planilla la primera vez',
        ];
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     *                             based on the search/filter conditions
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria();

        $criteria->compare('id', $this->id);
        $criteria->compare('nom_emp', $this->nom_emp, true);
        $criteria->compare('razon_social', $this->razon_social, true);
        $criteria->compare('rif_emp', $this->rif_emp, true);
        $criteria->compare('siglas_emp', $this->siglas_emp, true);
        $criteria->compare('sector_emp', $this->sector_emp);
        $criteria->compare('nro_reg_sudeca', $this->nro_reg_sudeca);
        $criteria->compare('cod_reg_rora', $this->cod_reg_rora, true);
        $criteria->compare('telf_emp', $this->telf_emp, true);
        $criteria->compare('telf_2_emp', $this->telf_2_emp, true);
        $criteria->compare('email_emp', $this->email_emp, true);
        $criteria->compare('dir_emp', $this->dir_emp, true);
        $criteria->compare('logo', $this->logo, true);
        $criteria->compare('coletilla', $this->coletilla, true);

        return new CActiveDataProvider($this, [
            'criteria' => $criteria,
        ]);
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     *
     * @param string $className active record class name
     *
     * @return Empresa the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public static function Nombre()
    {
        return strtoupper(Empresa::model()->find()->nom_emp);
    }

    public static function RazonSocial()
    {
        return strtoupper(Empresa::model()->find()->razon_social);
    }

    public static function Rif()
    {
        return strtoupper(Empresa::model()->find()->rif_emp);
    }

    public static function Siglas()
    {
        return strtoupper(Empresa::model()->find()->siglas_emp);
    }

    public static function Sector()
    {
        switch (Empresa::model()->find()->sector_emp): case 1:
                $sector = 'PÚBLICO';
        break;
        case 2:
                $sector = 'Privado';
        break;
        default:
                $sector = '-';
        endswitch;

        return strtoupper($sector);
    }

    public function getSector()
    {
        switch ($this->sector_emp) {
            case 1:
                return 'PÚBLICO';
            case 2:
                return 'PRIVADO';
            default:
                return '-';
        }
    }

    public function getRegistroSudeca()
    {
        return strtoupper($this->nro_reg_sudeca);
    }

    public function getLogo()
    {
        if ($this->logo == '') {
            return sprintf('%s/images/logo.png', dirname(Yii::app()->getBasePath()));
        }

        return vsprintf('%s/images/%s', [
            dirname(Yii::app()->getBasePath()),
            $this->logo,
        ]);
    }

    public static function RegistroSUDECA()
    {
        return strtoupper(Empresa::model()->find()->nro_reg_sudeca);
    }

    public static function RegistroRoraima()
    {
        return strtoupper(Empresa::model()->find()->nro_reg_rora);
    }

    public static function Telef()
    {
        return strtoupper(Empresa::model()->find()->telf_emp);
    }

    public static function Telefonos()
    {
        return strtoupper(Empresa::model()->find()->telf_emp.' - '.Empresa::model()->find()->telf_2_emp);
    }

    public static function Email()
    {
        return strtoupper(Empresa::model()->find()->email_emp);
    }

    public static function Direccion()
    {
        return strtoupper(Empresa::model()->find()->dir_emp);
    }

    public static function Coletilla()
    {
        $col = Empresa::model()->find()->coletilla;
        if (! empty($col)) {
            return $col;
        }

        return $col = '';
    }

    public static function checkEmpresa()
    {
        $empresa = self::model()->find();

        return $empresa;
    }

    public static function config($key)
    {
        return static::model()->find()->getAttribute($key);
    }
}
