<?php

/**
 * This is the model class for table "{{mas_company}}".
 *
 * The followings are the available columns in table '{{mas_company}}':
 * @property string $id
 * @property string $company_code
 * @property string $company_baseurl
 * @property string $company_logo
 * @property integer $crm_user_id
 * @property string $company_name
 * @property string $billing_address
 * @property string $billing_city
 * @property string $billing_state
 * @property string $billing_country
 * @property string $billing_pinzip
 * @property string $billing_postalcode
 * @property string $genral_phno
 * @property string $genral_email
 * @property string $genral_location
 * @property string $genral_web
 * @property string $isActive
 * @property string $app_version
 * @property string $ip_address
 */
class MasCompany extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	 
	public function tableName()
	{
		return '{{mas_company}}';
	}

	/**
	 * @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 array(
			array('company_code, company_baseurl, company_logo, company_name, billing_address, billing_city, billing_state, billing_country, billing_pinzip, billing_postalcode, genral_phno, genral_email, genral_location, genral_web, app_version, ip_address', 'required'),
			array('crm_user_id', 'numerical', 'integerOnly'=>true),
			array('company_code', 'length', 'max'=>10),
			array('company_logo, company_name, billing_city, billing_state, billing_country, billing_pinzip, billing_postalcode, genral_phno, genral_email, genral_location, genral_web, app_version', 'length', 'max'=>255),
			array('isActive', 'length', 'max'=>1),
			array('ip_address', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, company_code, company_baseurl, company_logo, crm_user_id, company_name, billing_address, billing_city, billing_state, billing_country, billing_pinzip, billing_postalcode, genral_phno, genral_email, genral_location, genral_web, isActive, app_version, ip_address', '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 array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'company_code' => 'Company Code',
			'company_baseurl' => 'Company Baseurl',
			'company_logo' => 'Company Logo',
			'crm_user_id' => 'Crm User',
			'company_name' => 'Company Name',
			'billing_address' => 'Billing Address',
			'billing_city' => 'Billing City',
			'billing_state' => 'Billing State',
			'billing_country' => 'Billing Country',
			'billing_pinzip' => 'Billing Pinzip',
			'billing_postalcode' => 'Billing Postalcode',
			'genral_phno' => 'Genral Phno',
			'genral_email' => 'Genral Email',
			'genral_location' => 'Genral Location',
			'genral_web' => 'Genral Web',
			'isActive' => 'Is Active',
			'app_version' => 'App Version',
			'ip_address' => 'Ip Address',
		);
	}

	/**
	 * 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,true);
		$criteria->compare('company_code',$this->company_code,true);
		$criteria->compare('company_baseurl',$this->company_baseurl,true);
		$criteria->compare('company_logo',$this->company_logo,true);
		$criteria->compare('crm_user_id',$this->crm_user_id);
		$criteria->compare('company_name',$this->company_name,true);
		$criteria->compare('billing_address',$this->billing_address,true);
		$criteria->compare('billing_city',$this->billing_city,true);
		$criteria->compare('billing_state',$this->billing_state,true);
		$criteria->compare('billing_country',$this->billing_country,true);
		$criteria->compare('billing_pinzip',$this->billing_pinzip,true);
		$criteria->compare('billing_postalcode',$this->billing_postalcode,true);
		$criteria->compare('genral_phno',$this->genral_phno,true);
		$criteria->compare('genral_email',$this->genral_email,true);
		$criteria->compare('genral_location',$this->genral_location,true);
		$criteria->compare('genral_web',$this->genral_web,true);
		$criteria->compare('isActive',$this->isActive,true);
		$criteria->compare('app_version',$this->app_version,true);
		$criteria->compare('ip_address',$this->ip_address,true);

		return new CActiveDataProvider($this, array(
			'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 MasCompany the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	 public function getAddress() {
        $address = array();
        $mas_company = MasCompany::model()->findByPK(Yii::app()->user->master_user_company_id);
        $address[] = $mas_company->company_address;
        $address[] = $mas_company->company_city;
        return (implode(', ', array_filter($address)));
    }

    /**
     * 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 MasCompany the static model class
     */
    public static function getCompanyName() {

        $condition = 'isActive =  "Y"';

        $model = self::model()->findAll(array('condition' => $condition));

        return CHtml::listData($model, 'id', 'company_name');
    }

    public static function getMasCompanyDetails($master_user_company_id = 0) {
        if ($master_user_company_id > 0) {
            $arr_mas_company = MasCompany::model()->findByPK($master_user_company_id);
        } else {
            $arr_mas_company = MasCompany::model()->find();
        }
        return $arr_mas_company;
    }

    public static function getCompanyId($company_code) {
        if ($company_code != '')
            return self::model()->find(array('condition' => 'company_code = "' . $company_code . '"'))->id;
        else
            return 0;
    }
}
