<?php

/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */

/**
 * This is the model class for table "v_report_item_summary_for_department".
 *
 * The followings are the available columns in table 'v_report_item_summary_for_department':
 * @property integer $id
 * @property string $department
 * @property string $date_time
 * @property string $user_name
 * @property string $item_name
 * @property integer $quantity
 * @property string $location_code
 */
class VReportItemSummaryForDepartment extends LsmsActiveRecord {

    public $from_date;
    public $to_date;
    public $department_filter;
    public $location_filter;
    public $quantity_sum;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return 'v_report_item_summary_for_department';
    }

    /**
     * @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('department,  item_name, quantity, location_code', 'required'),
	    array('id, quantity', 'numerical', 'integerOnly' => true),
	    array('department,  location_code', 'length', 'max' => 255),
	    array('item_name', 'length', 'max' => 50),
	    // The following rule is used by search().
	    // @todo Please remove those attributes that should not be searched.
	    array('quantity_sum,from_date,to_date,id, department,   item_name, quantity, location_code,department_filter,location_filter', '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',
	    'department' => yii::t('default', 'Department'),
	    'date_time' => yii::t('default', 'Date Time'),
	    'user_name' => yii::t('default', 'System User'),
	    'item_name' => yii::t('default', 'Item'),
	    'quantity' => yii::t('default', 'Quantity'),
	    'location_code' => yii::t('default', 'Location'),
	    'department_filter' => yii::t('default', 'Department'),
	    'location_filter' => yii::t('default', 'Location'),
	    'quantity_sum' => yii::t('default', 'Quantity'),
	);
    }

    /**
     * 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($is_empty = FALSE) {
	// @todo Please modify the following code to remove attributes that should not be searched.

	if ($is_empty) {
	    $criteria = new CDbCriteria;
	    $criteria->compare('id', -1);
	} else {
	    $criteria = new CDbCriteria;

	    // $criteria->compare('id', $this->id);
	    $criteria->compare('department_id', $this->department, true);
//	    $criteria->compare('date_time', $this->date_time, true);
//	    $criteria->compare('user_name', $this->user_name, true);
	    $criteria->compare('item_name', $this->item_name, true);
	    //$criteria->compare('quantity', $this->quantity);
	    $criteria->compare('location_id', $this->location_code, true);

	    $criteria->group = 'location_code,department_id,id';
	    $criteria->select = 'location_code,department,item_name,sum(quantity) as quantity_sum';

	    if (($this->from_date != null ) && ($this->to_date != null)) {
		$criteria->addCondition('Date(date_time) BETWEEN "' . $this->from_date . '" AND "' . $this->to_date . '"');
	    }

	    if ($this->location_filter != '' || $this->location_filter != null) {
		//Return Selected department name
		if (is_numeric($this->location_filter)) {
		    $criteria->compare('location_id', $this->location_filter);
		}
	    }
	    if ($this->location_filter == '' || $this->location_filter == null) {
		if (!Yii::app()->user->checkAccess('Super Admin')) {
		    $ids = User::getIdsOfDepartments();
		    if ((is_string($ids)))
			$criteria->addCondition('t.department_id in (' . $ids . ')');
		    else
			$criteria->addCondition('t.department_id =' . $ids);
		}
	    }

	    if ($this->quantity_sum != null && $this->quantity_sum != "")
		$criteria->having = 'sum(quantity) =' . $this->quantity_sum;
	    // Check if user choose All Department
	    if ($this->department_filter != '' || $this->department_filter != null) {
		//Return Selected department name
		if (is_numeric($this->department_filter)) {
		    $criteria->compare('department_id', $this->department_filter);
		}
	    }
	}

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'sort' => array(
		'attributes' => array(
		    '*',
		    'quantity_sum' => array(
			'asc' => 'quantity_sum',
			'desc' => 'quantity_sum DESC',
		    ),
		)),
	));
    }

    /**
     * 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 VReportItemSummaryForDepartment the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

}
