<?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
 */
class ReportController extends Controller {

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
	return array(
	    'accessControl', // perform access control for CRUD operations
	);
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
	return array(
	    array('allow', // allow all users to perform 'index' and 'view' actions
		'actions' => array('ticketsSummaryForDepartments',
		    'ticketsSummaryPosForSchedule',
		    'ticketsDetailsForPerson',
		    'ticketsDetailsForDepartment',
		    'ticketsDetailsForBadge',
		    'expiredBadgesWithinPeriod',
		    'personPOS',
		    'GetDepartments',
		    'ItemsSummaryForDepartment',
		),
		'roles' => array('System Admin', 'Finance', 'Facility Admin', 'Super Admin', 'Food Manager'),
	    ),
	    array('deny', // deny all users
		'users' => array('*'),
	    ),
	);
    }

    /*
     * Action for return department in selected location  in cascading dropdown list
     */

    public function actionGetDepartments() {
	if (Yii::app()->request->isAjaxRequest) {
	    $location = $_GET['selected'];
	    $departments = array();
	    $departments[] = array('label' => yii::t('default', 'All Departments'), 'value' => '');
	    if ($location != '') {
		$departments_array = Department::getDepartmentsInLocation($location);
//		$selected_location = Location::model()->findByPk($location);
//
		if ($departments_array != null) {
		    foreach ($departments_array as $id => $name) {
			$departments[] = array('label' => $name, 'value' => $id);
		    }
		}
	    }
	    echo json_encode($departments);
	    Yii::app()->end();
	} else {
	    throw new CHttpException(400, 'Invalid request.');
	}
    }

    /*
     * This action responsible for  generating information about the total number of tickets
     *  within a certain period of time
     */

    public function actionTicketsSummaryForDepartments() {
	$department = '';
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model = new VReportTicketSummaryForDepartment('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VReportTicketSummaryForDepartment'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportTicketSummaryForDepartment'];
	    if ($model->department != '')
		$department = $model->department;
	}
	if (isset($_GET['Location'])) {
	    $model->location = $_GET['Location'];
	}

	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('header' => 'Ticket Type', 'name' => 'ticket_type'),
		array('header' => 'Department', 'name' => 'department'),
		array('header' => 'Accounting Unit', 'name' => 'accounting_unit'),
		array('header' => 'Activity Code', 'name' => 'activity_code'),
		array('header' => 'System User', 'name' => 'user_name'),
		array('header' => 'Person', 'name' => 'person'),
		array('header' => 'POS', 'name' => 'pos'),
		array('header' => 'Badge', 'name' => 'badge', 'type' => 'text'),
		array('header' => 'Schedule', 'name' => 'schedule'),
		array('header' => 'Number Of Tickets', 'name' => 'number_of_tickets'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Ticket Summary For Department'), array('creator' => 'LSMS',), 'Excel5');
	}
	$this->render('ticketsSummaryForDepartments', array(
	    'model' => $model,
	    'department' => $department,
	    'is_empty' => $is_empty,
	));
    }

    /*
     * This report will present the total number of the issued tickets in each schedule for the all POS’s
     */

    public function actionTicketsSummaryPosForSchedule() {
	$model = new VReportTicketSummaryPosSchedule('search');
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VReportTicketSummaryPosSchedule'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportTicketSummaryPosSchedule'];
	}

	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('header' => 'POS', 'name' => 'pos'),
		array('header' => 'Schedule', 'name' => 'schedule'),
		array('header' => 'Number Of Tickets', 'name' => 'number_of_tickets'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Tickets Summary For POS per Schedule'), array('creator' => 'LSMS',), 'Excel5');
	}

	$this->render('ticketsSummaryPosForSchedule', array(
	    'model' => $model,
	    'is_empty' => $is_empty,
	));
    }

    /*
     * This action responsible for generating report wich getting the details of certain person meals at certain period of time.
     */

    public function actionTicketsDetailsForPerson() {
	$model = new VReportTicketsDetailsForPerson('search');
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VReportTicketsDetailsForPerson'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportTicketsDetailsForPerson'];
	}
	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('header' => 'Person', 'name' => 'person'),
		array('header' => 'Department', 'name' => 'department'),
		array('header' => 'Accounting_unit', 'name' => 'accounting_unit'),
		array('header' => 'Activity Code', 'name' => 'activity_code'),
		array('header' => 'User_name', 'name' => 'user_name'),
		array('header' => 'POS', 'name' => 'pos'),
		array('header' => 'Ticket ID', 'name' => 'id'),
		array('header' => 'Badge', 'name' => 'badge'),
		array('header' => 'Schedule', 'name' => 'schedule'),
		array('header' => 'Date Time', 'name' => 'date_time'),
		array('header' => 'Cancelled ', 'name' => 'cancelled'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Details For Person Tickets'), array('creator' => 'LSMS',), 'Excel5');
	}

	$this->render('ticketsDetailsForPerson', array(
	    'model' => $model,
	    'is_empty' => $is_empty,
	));
    }

    /*
     * This action responsible for generating report wich getting the details of certain person meals at certain period of time.
     */

    public function actionPersonPOS() {
	$model = new VReportPersonPos('search');
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VReportPersonPos'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportPersonPos'];
	}
	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('name' => 'person_name', 'header' => 'Person Name'),
		array('name' => 'person_number', 'header' => 'Person Number'),
		array('name' => 'person_alias', 'header' => 'Person Alias'),
		array('name' => 'person_type', 'header' => 'Person Type'),
		array('name' => 'person_mobile', 'header' => 'Mobile'),
		array('name' => 'person_email', 'header' => 'Email'),
		array('name' => 'person_manager', 'header' => 'Person Manager'),
		array('name' => 'person_is_ldap', 'header' => 'Is LDAP Person'),
		array('name' => 'person_is_manager', 'header' => 'Is Manager'),
		array('name' => 'person_is_food_manager', 'header' => 'Is Food Manager'),
		array('name' => 'person_status', 'header' => 'Person Status'),
		array('name' => 'person_department_name', 'header' => 'Person Department'),
		array('name' => 'person_location_code', 'header' => 'Person Location'),
		array('name' => 'person_geomarket_name', 'header' => 'Person Geomarket'),
		array('name' => 'pos_name', 'header' => 'POS Name'),
		array('name' => 'pos_location_code', 'header' => 'POS Location'),
		array('name' => 'pos_geomraket_name', 'header' => 'POS Geomraket'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Person - POS'), array('creator' => 'LSMS',), 'Excel5');
	}

	$this->render('personPOS', array(
	    'model' => $model,
	    'is_empty' => $is_empty,
	));
    }

    /*
     * This action responsible for generating report of the generated tickets for a specific department within a period of time.
     */

    public function actionTicketsDetailsForDepartment() {
	$department = '';
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model = new VReportTicketsDetailsForDepartment('search');
	$model->unsetAttributes();  // clear any default values

	if (isset($_GET['VReportTicketsDetailsForDepartment'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportTicketsDetailsForDepartment'];
	    if ($model->department != '')
		$department = $model->department;
	}
	if (isset($_GET['Location'])) {
	    $model->location = $_GET['Location'];
	}
	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('header' => 'System User', 'name' => 'user_name'),
		array('header' => 'Department', 'name' => 'department'),
		array('header' => 'Accounting Unit', 'name' => 'accounting_unit'),
		array('header' => 'Activity Code', 'name' => 'activity_code'),
		array('header' => 'POS', 'name' => 'pos'),
		array('header' => 'Schedule', 'name' => 'schedule'),
		array('header' => 'Date Time', 'name' => 'date_time'),
		array('header' => 'Reason', 'name' => 'reason'),
		array('header' => 'Cancelled ', 'name' => 'cancelled'),
		array('header' => 'Quantity', 'name' => 'quantity'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Details For Department Tickets'), array('creator' => 'LSMS',), 'Excel5');
	}

	$this->render('ticketsDetailsForDepartment', array(
	    'model' => $model,
	    'department' => $department,
	    'is_empty' => $is_empty,
	));
    }

    /*
     * This action responsible for generating report wich getting the details of certain badge meals at certain period of time.
     */

    public function actionTicketsDetailsForBadge() {
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model = new VReportTicketsDetailsForBadge('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VReportTicketsDetailsForBadge'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportTicketsDetailsForBadge'];
	}
	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('header' => 'Badge', 'name' => 'badge'),
		array('header' => 'Person', 'name' => 'person'),
		array('header' => 'Department', 'name' => 'department'),
		array('header' => 'Accounting Unit', 'name' => 'accounting_unit'),
		array('header' => 'Activity Code', 'name' => 'activity_code'),
		array('header' => 'POS', 'name' => 'pos'),
		array('header' => 'Ticket ID', 'name' => 'id'),
		array('header' => 'Schedule', 'name' => 'schedule'),
		array('header' => 'Date Time', 'name' => 'date_time'),
		array('header' => 'System User ', 'name' => 'user_name'),
		array('header' => 'Cancelled ', 'name' => 'cancelled'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Details For Badge Tickets'), array('creator' => 'LSMS',), 'Excel5');
	}
	$this->render('ticketsDetailsForBadge', array(
	    'model' => $model,
	    'is_empty' => $is_empty,
	));
    }

    /*
     * This action responsible for generating report is getting the badges that will expire within a period
     */

    public function actionExpiredBadgesWithinPeriod() {
	$department = '';
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model = new VReportExpiredBadgesWithinPeriod('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VReportExpiredBadgesWithinPeriod'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportExpiredBadgesWithinPeriod'];
	    if ($model->department != '')
		$department = $model->department;
	}
	if (isset($_GET['Location'])) {
	    $model->location = $_GET['Location'];
	}
	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		array('header' => 'Badge', 'name' => 'badge'),
		array('header' => 'Person', 'name' => 'person'),
		array('header' => 'Department', 'name' => 'department'),
		array('header' => 'Activation Start Date', 'name' => 'activation_start_date'),
		array('header' => 'Activation End Date', 'name' => 'activation_end_date'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Expired Badges Within Period'), array('creator' => 'LSMS',), 'Excel5');
	}

	$this->render('expiredBadgesWithinPeriod', array(
	    'model' => $model,
	    'department' => $department,
	    'is_empty' => $is_empty,
	));
    }

    // for excel export extension
    public function behaviors() {
	return array(
	    'eexcelview' => array(
		'class' => 'ext.eexcelview.EExcelBehavior',
	    ),
	);
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
	$model = Geomarket::model()->findByPk($id);
	if ($model === null)
	    throw new CHttpException(404, 'The requested page does not exist.');
	return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
	if (isset($_POST['ajax']) && $_POST['ajax'] === 'geomarket-form') {
	    echo CActiveForm::validate($model);
	    Yii::app()->end();
	}
    }

    /*
     * This action responsible for generating report of the generated items for a specific department within a period of time.
     */

    public function actionItemsSummaryForDepartment() {
	// this parameter passed to search method in model for return faild creteria
	$is_empty = true;
	$model = new VReportItemSummaryForDepartment('search');
	$model->unsetAttributes();  // clear any default values

	if (isset($_GET['VReportItemSummaryForDepartment'])) {
	    $is_empty = false;
	    $model->attributes = $_GET['VReportItemSummaryForDepartment'];
	}
	if (isset($_GET['Location'])) {
	    $model->location_filter = $_GET['Location'];
	}
	//Export Data to Excel File
	if ((isset($_GET['export'])) && ($_GET['export'] == Yii::t('default', 'Export'))) {
	    $columns = array(
		//array('header' => 'System User', 'name' => 'user_name'),
		array('header' => 'Location', 'name' => 'location_code'),
		array('header' => 'Department', 'name' => 'department'),
		array('header' => 'Item', 'name' => 'item_name'),
		//array('header' => 'Date Time', 'name' => 'date_time'),
		//array('header' => 'Cancelled ', 'name' => 'cancelled'),
		array('header' => 'Quantity', 'name' => 'quantity_sum'),
	    );
	    $data = $model->search();
	    $data->pagination->pageSize = $data->getTotalItemCount();
	    $this->toExcel($data, $columns, Yii::t('default', 'Items Summary For Department'), array('creator' => 'LSMS',), 'Excel5');
	}

	$this->render('itemsSummaryForDepartment', array(
	    'model' => $model,
	    'is_empty' => $is_empty,
	));
    }

}
