<?php

/**
 * This is the model base class for the table "modulos_documentos_fiscais".
 * DO NOT MODIFY THIS FILE! It is automatically generated by giix.
 * If any changes are necessary, you must set or override the required
 * property or method in class "ClientesDocumentosFiscais".
 *
 * Columns in table "modulos_documentos_fiscais" available as properties of the model,
 * followed by relations of table "modulos_documentos_fiscais" available as properties of the model.
 *
 * @property integer $MDF_ID
 * @property integer $MDF_IDMODULO
 * @property integer $MDF_IDDOCUMENTO
 *
 * @property DocumentosFiscais $DocumentoFiscal
 * @property Clientes $Cliente
 */
class ModulosDocumentosFiscais extends GxActiveRecord {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'modulos_documentos_fiscais';
    }

    public static function label($n = 1) {
        return Yii::t('app', 'Módulo X Documento Fiscal|Módulos X Documentos Fiscais', $n);
    }

    public static function representingColumn() {
        return 'MDF_ID';
    }

    public function behaviors() {
        return array('datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior'));
    }

    public function rules() {
        return array(
            array('MDF_IDMODULO, MDF_IDDOCUMENTO', 'required'),
            array('MDF_IDMODULO, MDF_IDDOCUMENTO', 'numerical', 'integerOnly' => true),
            array('MDF_ID, MDF_IDMODULO, MDF_IDDOCUMENTO', 'safe', 'on' => 'search'),
        );
    }

    public function relations() {
        return array(
            'DocumentoFiscal'   => array(self::BELONGS_TO, 'DocumentosFiscais', 'MDF_IDDOCUMENTO'),
            'Modulo'           => array(self::BELONGS_TO, 'Modulos', 'MDF_IDMODULO'),
        );
    }

    public function pivotModels() {
        return array(
        );
    }

    public function attributeLabels() {
        return array(
            'MDF_ID'            => 'Código',
            'MDF_IDMODULO'      => 'Módulo',
            'MDF_IDDOCUMENTO'   => 'Documento Fiscal',
            'Modulo'            => 'Módulo',
            'DocumentoFiscal'   => 'Documento Fiscal',
        );
    }

    public function search() {
        $criteria = new CDbCriteria;

        $criteria->compare('MDF_ID', $this->MDF_ID);
        $criteria->compare('MDF_IDMODULO', $this->MDF_IDMODULO);
        $criteria->compare('MDF_IDDOCUMENTO', $this->MDF_IDDOCUMENTO);

        return new CActiveDataProvider($this, array(
                'criteria' => $criteria,
                'sort' => array(
                    'defaultOrder' => 'MDF_ID ASC',
                ),
                'pagination' => array(
                    'pageSize' => 20,
                )
            )
        );
    }

    public function findByModulos(array $modulos) {
        if(empty($modulos)) {
            return array();
        }
        $docs = $this->with('DocumentoFiscal')->findAll($this->getCriteria($modulos));
        return GxHtml::listDataEx($docs, 'DocumentoFiscal.DFS_IDDOCUMENTO', 'DocumentoFiscal.DFS_DESCRICAO');
    }

    public function countDocumentos(array $modulos) {
        if(empty($modulos)) {
            return array();
        }
        return $this->with('DocumentoFiscal')->count($this->getCriteria($modulos));
    }

    private function getCriteria(array $modulos) {
        if(empty($modulos)) {
            return false;
        }
        $criteria = new CDbCriteria;
        $criteria->addInCondition('MDF_IDMODULO', $modulos);
        $criteria->compare('DocumentoFiscal.DFS_SITUACAO', "A");
        $criteria->with = array('DocumentoFiscal' => array('joinType' => 'LEFT JOIN', 'together' => true, 'select' => false));
        return $criteria;
    }
}
