<?php

/**
 * This is the model class for table "crm_employee".
 *
 * The followings are the available columns in table 'crm_employee':
 * @property integer $id
 * @property integer $branch_or_sponsor_id
 * @property string $employee_code
 * @property string $user_name
 * @property string $password
 * @property string $first_name
 * @property string $middle_name
 * @property string $last_name
 * @property integer $designation
 * @property integer $department
 * @property integer $subledger_account_no
 * @property string $father_husband
 * @property string $anniversary_date
 * @property string $present_address
 * @property string $permanent_address
 * @property string $phone
 * @property string $mobile
 * @property string $email
 * @property string $email2
 * @property string $emergency_contact1
 * @property string $emergency_contact2
 * @property string $emergency_contact_mob1
 * @property string $emergency_contact_mob2
 * @property string $friend1_name
 * @property string $friend1_phone
 * @property string $friend1_email
 * @property string $friend2_name
 * @property string $friend2_phone
 * @property string $friend2_email
 * @property integer $gender
 * @property string $date_of_birth
 * @property string $date_join
 * @property string $date_leaving
 * @property string $employee_type
 * @property string $probation_days
 * @property string $probation_date
 * @property integer $reporting_manager
 * @property integer $nationality
 * @property integer $have_pension
 * @property string $marital_status
 * @property string $confirmation_date
 * @property integer $currency
 * @property integer $employee_overtime
 * @property string $employee_bankname
 * @property string $employee_accountno
 * @property string $employee_bankifsc
 * @property string $employee_wpscode
 * @property string $employee_iban_no
 * @property integer $employee_shift
 * @property integer $employee_exclude_biometric
 * @property integer $employee_biometric_code
 * @property integer $employee_wps_company
 * @property string $personal_file_no
 * @property string $salary_commission
 * @property string $overtime
 * @property integer $overtime_type
 * @property integer $leave_reason
 * @property integer $weekend_off1
 * @property integer $weekend_off2
 * @property string $profile_photo
 * @property integer $created_by
 * @property string $created_date_time
 * @property integer $updated_by
 * @property string $updated_date_time
 * @property string $isActive
 */
class Employee extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public $employee_wps_company_name;
    public $shift_detail;
    public $old_branch;
    public $old_department;
    public $old_designation;
    public $effective_date;
    public $weekend_1, $weekend_2, $weekend_3;
    public $old_nationality;
    public $old_gender;
    public $old_marital_status;
    public $old_date_of_birth;
    public $old_date_of_joining;
    public $old_team;

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

    /**
     * @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('branch_or_sponsor_id, employee_code, first_name, last_name, designation, department, employee_biometric_code,currency,date_join,gender,date_of_birth,nationality,working_status', 'required'),
            array('branch_or_sponsor_id, employee_category,designation, department, subledger_account_no, gender, reporting_manager, nationality, have_pension, currency, employee_overtime, employee_shift, employee_exclude_biometric, employee_biometric_code, employee_wps_company, overtime_type, leave_reason, weekend_off1, weekend_off2,marital_status,employee_bankname, created_by, updated_by,payment_type,attendance_manager,salary_calculation_method,team_id,work_location,working_status,employee_status', 'numerical', 'integerOnly' => true),
            array('employee_code, user_name, password, first_name, middle_name, last_name, father_husband, present_address, permanent_address, friend1_name, friend2_name, employee_accountno, employee_bankifsc, employee_wpscode, employee_iban_no, profile_photo', 'length', 'max' => 500),
            array('phone, email, email2, friend1_phone, friend1_email, friend2_phone, friend2_email, employee_type, probation_days, personal_file_no', 'length', 'max' => 255),
            array('mobile,email,employee_type', 'required', 'on' => 'update'),
            //array('user_name, password', 'required', 'on' => 'password_change'),
            array('email,email2', 'email'),
            array('phone,mobile,emergency_contact_mob1,emergency_contact_mob2', 'match',
                'pattern' => '/^[0-9+-]+$/u',
                'message' => '{attribute} contains only numbers and symbols.'
            ),
            array('user_name, password', 'validateuserpassword', 'on' => 'password_change'),
            array('date_join,mobile,email,personal_file_no,nationality', 'required', 'on' => 'excelupload'),
            array('branch_or_sponsor_id', 'validatebranch', 'on' => 'excelupload'),
            array('department', 'validateDepartment', 'on' => 'excelupload'),
            array('designation', 'validateDesignation', 'on' => 'excelupload'),
            array('currency', 'validateCurrency', 'on' => 'excelupload'),
            array('gender', 'validateGender', 'on' => 'excelupload'),
            array('nationality', 'validateNationality', 'on' => 'excelupload'),
            array('date_join', 'validateJoinDate', 'on' => 'excelupload'),
            array('date_of_birth', 'validatebirthDate', 'on' => 'excelupload'),
            array('mobile', 'length', 'max' => 25),
            array('emergency_contact1, emergency_contact2, emergency_contact_mob1, emergency_contact_mob2', 'length', 'max' => 30),
            array('salary_commission', 'length', 'max' => 5),
            array('overtime, isActive,is_lead_owner,travel_allowance,family_status,insurance,family_ticket,housing_facility,car_facility,post_salary,disciplinary_action,general_request', 'length', 'max' => 1),
            array('employee_code', 'filter', 'filter' => 'trim'),
            array('employee_biometric_code', 'filter', 'filter' => 'trim'),
            array('employee_code', 'unique', 'message' => 'Employee code already exist'),
            //array('employee_biometric_code', 'unique', 'message' => 'Biometric code already exist'),
            array('employee_biometric_code', 'validatebiometriccode'),
            array('user_name', 'unique', 'message' => 'Username {value} already exist'),
            array('employee_pension_scheme', 'validateshemeselection'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, branch_or_sponsor_id, employee_code, user_name, password, first_name, middle_name, last_name, designation, department, subledger_account_no, father_husband, anniversary_date, present_address, permanent_address, phone, mobile, email, email2, emergency_contact1, emergency_contact2, emergency_contact_mob1, emergency_contact_mob2, friend1_name, friend1_phone, friend1_email, friend2_name, friend2_phone, friend2_email, gender, date_of_birth, date_join, date_leaving, employee_type, probation_days, probation_date, reporting_manager, nationality, have_pension, marital_status, confirmation_date, currency, employee_overtime, employee_bankname, employee_accountno, employee_bankifsc, employee_wpscode, employee_iban_no, employee_shift, employee_exclude_biometric, employee_biometric_code, employee_wps_company, personal_file_no, salary_commission, overtime, overtime_type, leave_reason, weekend_off1, weekend_off2, profile_photo, created_by, created_date_time, updated_by, updated_date_time, isActive', 'safe', 'on' => 'search'),
        );
    }
    /**
     * @return array relational rules.
     */
    public function validateuserpassword() {

        if ($this->password == '') {
            $this->addError('password', 'Enter password for ' . $this->user_name);
            $this->rules();
        }
        return true;
    }

    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(
            'designationdetail' => array(self::BELONGS_TO, 'MasDesignation', 'designation'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'branch_or_sponsor_id' => 'Branch Or Sponsor',
            'employee_code' => 'Employee Code',
            'user_name' => 'User Name',
            'password' => 'Password',
            'first_name' => 'First Name',
            'middle_name' => 'Middle Name',
            'last_name' => 'Last Name',
            'designation' => 'Designation',
            'department' => 'Department',
            'subledger_account_no' => 'Subledger Account No',
            'father_husband' => 'Father Husband',
            'anniversary_date' => 'Anniversary Date',
            'present_address' => 'UAE Address',
            'permanent_address' => 'Native Address',
            'phone' => 'Official Mobile',
            'mobile' => 'Personal Mobile',
            'email' => 'Office Email',
            'email2' => 'Personal Email',
            'emergency_contact1' => 'UAE Emergency Contact',
            'emergency_contact2' => 'Native Emergency Contact',
            'emergency_contact_mob1' => 'UAE Emergency Contact Mobile',
            'emergency_contact_mob2' => 'Native Emergency Contact Mobile',
            'friend1_name' => 'Friend1 Name',
            'friend1_phone' => 'Friend1 Phone',
            'friend1_email' => 'Friend1 Email',
            'friend2_name' => 'Friend2 Name',
            'friend2_phone' => 'Friend2 Phone',
            'friend2_email' => 'Friend2 Email',
            'gender' => 'Gender',
            'date_of_birth' => 'Date Of Birth',
            'date_join' => 'Date Join',
            'date_leaving' => 'Date Leaving',
            'employee_type' => 'Employee Type',
            'probation_days' => 'Probation Days',
            'probation_date' => 'Probation Date',
            'reporting_manager' => 'Reporting Manager',
            'nationality' => 'Nationality',
            'have_pension' => 'Have Pension',
            'marital_status' => 'Marital Status',
            'confirmation_date' => 'Confirmation Date',
            'currency' => 'Currency',
            'employee_overtime' => '1-Yes,2-No',
            'employee_bankname' => 'Employee Bankname',
            'employee_accountno' => 'Employee Accountno',
            'employee_bankifsc' => 'Employee Bankifsc',
            'employee_wpscode' => 'Employee Wpscode',
            'employee_iban_no' => 'Employee Iban No',
            'employee_shift' => 'Employee Shift',
            'employee_exclude_biometric' => '1-Yes,0-No',
            'employee_biometric_code' => 'Employee Biometric Code',
            'employee_wps_company' => 'Employee Wps Company',
            'personal_file_no' => 'Personal File No',
            'salary_commission' => 'Salary Commission',
            'overtime' => 'Overtime',
            'overtime_type' => 'Overtime Type',
            'leave_reason' => '1-Termination, 2- Resignation, 3- Absconding, 4- Stop Hire',
            'weekend_off1' => 'Weekend Off1',
            'weekend_off2' => 'Weekend Off2',
            'profile_photo' => 'Profile Photo',
            'created_by' => 'Created By',
            'created_date_time' => 'Created Date Time',
            'updated_by' => 'Updated By',
            'updated_date_time' => 'Updated Date Time',
            'isActive' => 'Is Active',
        );
    }

    /**
     * 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('branch_or_sponsor_id', $this->branch_or_sponsor_id);
        $criteria->compare('employee_code', $this->employee_code, true);
        $criteria->compare('user_name', $this->user_name, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('middle_name', $this->middle_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('designation', $this->designation);
        $criteria->compare('department', $this->department);
        $criteria->compare('subledger_account_no', $this->subledger_account_no);
        $criteria->compare('father_husband', $this->father_husband, true);
        $criteria->compare('anniversary_date', $this->anniversary_date, true);
        $criteria->compare('present_address', $this->present_address, true);
        $criteria->compare('permanent_address', $this->permanent_address, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('mobile', $this->mobile, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('email2', $this->email2, true);
        $criteria->compare('emergency_contact1', $this->emergency_contact1, true);
        $criteria->compare('emergency_contact2', $this->emergency_contact2, true);
        $criteria->compare('emergency_contact_mob1', $this->emergency_contact_mob1, true);
        $criteria->compare('emergency_contact_mob2', $this->emergency_contact_mob2, true);
        $criteria->compare('friend1_name', $this->friend1_name, true);
        $criteria->compare('friend1_phone', $this->friend1_phone, true);
        $criteria->compare('friend1_email', $this->friend1_email, true);
        $criteria->compare('friend2_name', $this->friend2_name, true);
        $criteria->compare('friend2_phone', $this->friend2_phone, true);
        $criteria->compare('friend2_email', $this->friend2_email, true);
        $criteria->compare('gender', $this->gender);
        $criteria->compare('date_of_birth', $this->date_of_birth, true);
        $criteria->compare('date_join', $this->date_join, true);
        $criteria->compare('date_leaving', $this->date_leaving, true);
        $criteria->compare('employee_type', $this->employee_type, true);
        $criteria->compare('probation_days', $this->probation_days, true);
        $criteria->compare('probation_date', $this->probation_date, true);
        $criteria->compare('reporting_manager', $this->reporting_manager);
        $criteria->compare('nationality', $this->nationality);
        $criteria->compare('have_pension', $this->have_pension);
        $criteria->compare('marital_status', $this->marital_status, true);
        $criteria->compare('confirmation_date', $this->confirmation_date, true);
        $criteria->compare('currency', $this->currency);
        $criteria->compare('employee_overtime', $this->employee_overtime);
        $criteria->compare('employee_bankname', $this->employee_bankname, true);
        $criteria->compare('employee_accountno', $this->employee_accountno, true);
        $criteria->compare('employee_bankifsc', $this->employee_bankifsc, true);
        $criteria->compare('employee_wpscode', $this->employee_wpscode, true);
        $criteria->compare('employee_iban_no', $this->employee_iban_no, true);
        $criteria->compare('employee_shift', $this->employee_shift);
        $criteria->compare('employee_exclude_biometric', $this->employee_exclude_biometric);
        $criteria->compare('employee_biometric_code', $this->employee_biometric_code);
        $criteria->compare('employee_wps_company', $this->employee_wps_company);
        $criteria->compare('personal_file_no', $this->personal_file_no, true);
        $criteria->compare('salary_commission', $this->salary_commission, true);
        $criteria->compare('overtime', $this->overtime, true);
        $criteria->compare('overtime_type', $this->overtime_type);
        $criteria->compare('leave_reason', $this->leave_reason);
        $criteria->compare('weekend_off1', $this->weekend_off1);
        $criteria->compare('weekend_off2', $this->weekend_off2);
        $criteria->compare('profile_photo', $this->profile_photo, true);
        $criteria->compare('created_by', $this->created_by);
        $criteria->compare('created_date_time', $this->created_date_time, true);
        $criteria->compare('updated_by', $this->updated_by);
        $criteria->compare('updated_date_time', $this->updated_date_time, true);
        $criteria->compare('isActive', $this->isActive, 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 Employee the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    public function getemployeefullname() {
        return (Yii::app()->controller->ciphertext($this->first_name, "d") . ' ' . Yii::app()->controller->ciphertext($this->middle_name, "d") . ' ' . Yii::app()->controller->ciphertext($this->last_name, "d"));
    }

    public function getemployeenamecode() {
        return (Yii::app()->controller->ciphertext($this->first_name, "d") . ' ' . Yii::app()->controller->ciphertext($this->middle_name, "d") . ' ' . Yii::app()->controller->ciphertext($this->last_name, "d")) . ' ( ' . Yii::app()->controller->ciphertext($this->employee_code, "d") . ' )';
    }

    public function getemployeecode() {
        return Yii::app()->controller->ciphertext($this->employee_code, "d");
    }

}
