<?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
 */
 
/**
 *  TID
 *
 * Yii controller to control tid actions
 *
 * @version 1.0
 *
 * @author Amaney
 * @package application.controllers
 */
class TidController 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
	// 'postOnly + delete', // we only allow deletion via POST request
	);
    }

    /**
     * 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('create', 'update', 'delete', 'editStatus', 'renew', 'viewStatus', 'deleteStatus', 'processTiType'),
		'roles' => array(User::SYSTEM_ADMIN_ROLE, User::LOGISTICS_ADMIN_ROLE),
	    ),
	    array('allow', // allow all users to perform 'index' and 'view' actions
		'actions' => array('index', 'view', 'exportToExcelStatusInfo', 'DetailExportToExcel', 'SummaryExportToExcel'),
		'users' => array('*'),
	    ),
	    array('deny', // deny all users
		'users' => array('*'),
	    ),
	);
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
	$model = $this->loadModel($id);
	$model->document_type_ids = Tid::getSelectedDocumentTypeAsString($id);
	$this->render('view', array(
	    'model' => $model,
	));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {
	$model = new Tid;

	// Uncomment the following line if AJAX validation is needed
	// $this->performAjaxValidation($model);

	if (isset($_POST['Tid'])) {
	    $model->attributes = $_POST['Tid'];
	    $transaction = Yii::app()->db->beginTransaction();

	    if ($model->save()) {
		if ($model->createUpdateStatus(true, $error_msg)) {
		    if ($model->saveDocuments($error_msg, $transaction)) {
			$transaction->commit();
			Yii::app()->user->setFlash('success', Yii::t('default', 'The TID has been added successfully.'));
			echo $model->id;
			return;
		    } else {
			$transaction->rollback();
			Yii::app()->user->setFlash('error', $error_msg);
			echo "<div class='flash-msg'>" . "<br>";
			$this->widget('bootstrap.widgets.TbAlert');
			echo "</div>";
			return;
		    }
		} else {
		    $transaction->rollback();
		    Yii::app()->user->setFlash('error', $error_msg);
		    echo "<div class='flash-msg'>" . "<br>";
		    $this->widget('bootstrap.widgets.TbAlert');
		    echo "</div>";
		    return;
		}
	    } else {
		$transaction->rollback();
		Yii::app()->user->setFlash('error', CHtml::errorSummary($model));
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
		return;
	    }
	}

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

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id) {
	$model = $this->loadModel($id);
	$model->document_type_ids = Tid::getSelectedDocumentTypeAsString($id);
	$model->fillLastStatusInfo();
	if ($model->customs_letter_date == '0000-00-00')
	    $model->customs_letter_date = '';
	if ($model->original_documnets_delivery_date == '0000-00-00')
	    $model->original_documnets_delivery_date = '';

	// Uncomment the following line if AJAX validation is needed
	// $this->performAjaxValidation($model);

	if (isset($_POST['Tid'])) {
	    $model->attributes = $_POST['Tid'];
	    $transaction = Yii::app()->db->beginTransaction();
	    if ($model->save()) {
		if ($model->createUpdateStatus(false, $error_msg)) {
		    if ($model->saveDocuments($error_msg, $transaction)) {
			$transaction->commit();
			Yii::app()->user->setFlash('success', Yii::t('default', 'The TID has been updated successfully.'));
			echo $model->id;
			return;
		    } else {
			$transaction->rollback();
			Yii::app()->user->setFlash('error', $error_msg);
			echo "<div class='flash-msg'>" . "<br>";
			$this->widget('bootstrap.widgets.TbAlert');
			echo "</div>";
			return;
		    }
		} else {
		    $transaction->rollback();
		    Yii::app()->user->setFlash('error', $error_msg);
		    echo "<div class='flash-msg'>" . "<br>";
		    $this->widget('bootstrap.widgets.TbAlert');
		    echo "</div>";
		    return;
		}
	    } else {
		$transaction->rollback();
		Yii::app()->user->setFlash('error', CHtml::errorSummary($model));
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
		return;
	    }
	}

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

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
	$model = $this->loadModel($id);

	$success_msg = Yii::t('default', 'The TID ({n}) has been deleted successfully', $model->tid_no);
	$failer_msg = Yii::t('default', 'You cannot delete this element because it is referenced by other element');
	try {


	    $file_name = $model->id . "-";
	    if ($model->delete()) {
		$document_path = Setting::getDocumentsPath();
		$files = glob($document_path . $file_name . "*.*");
		if ($files != null) {
		    foreach ($files as $file) {
			unlink($file);
		    }
		}
	    }


	    if (!isset($_GET['ajax'])) { // Check Wether the data grid will be updated through AJAX or Not
		Yii::app()->user->setFlash('success', $success_msg);
		$this->redirect(array('index'));
	    } else {
		Yii::app()->user->setFlash('success', $success_msg);
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
	    }
	} catch (CDbException $e) { // here the item is referenced by another item
	    if (!isset($_GET['ajax'])) {
		$failer_msg = $model->setTidFailureMessage();
		Yii::app()->user->setFlash('error', $failer_msg);
		$this->redirect(array('view', 'id' => $model->id));
	    } else {
		$failer_msg = $model->setTidFailureMessage();
		Yii::app()->user->setFlash('error', $failer_msg);
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
	    }
	} catch (Exception $e) { // here the item is referenced by another item
	    if (!isset($_GET['ajax'])) {
		Yii::app()->user->setFlash('error', $e->getMessage());
		$this->redirect(array('view', 'id' => $model->id));
	    } else {
		Yii::app()->user->setFlash('error', $e->getMessage());
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
	    }
	}
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {
	$model = new VTid('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VTid']))
	    $model->attributes = $_GET['VTid'];

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

    /**
     * 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 $id the ID of the model to be loaded
     * @return Tid the loaded model
     * @throws CHttpException
     */
    public function loadModel($id) {
	$model = Tid::model()->findByPk($id);
	if ($model === null)
	    throw new CHttpException(404, 'The requested page does not exist.');
	return $model;
    }

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

    /**
     * View Statuses of TID
     */
    public function actionViewStatus($id) {
	$model = new TidStatus('search');
	$model->unsetAttributes();
	if (isset($_GET['TidStatus']))
	    $model->attributes = $_GET['TidStatus'];

	$this->renderPartial('/tidStatus/_index', array(
	    'model' => $model,
	    'tid_id' => $id
	));
    }

    /**
     * Edit Status of TID
     */
    public function actionEditStatus($id, $tid_id) {
	$model = TidStatus::model()->findByPk($id);
	if ($model->ti_expiry_date == '0000-00-00')
	    $model->ti_expiry_date = '';
	$tid = $this->loadModel($tid_id);
	if (isset($_POST['TidStatus'])) {
	    $model->attributes = $_POST['TidStatus'];
	    if ($model->under_processing == 'No' || $model->under_processing == 0) {
		$model->renewal_stage_id = null;
		$model->transferred_to_contract_id = null;
	    }
	    if ($model->renewal_stage_id == null || $model->renewal_stage_id != Setting::getUnderTransferOfPrivilege())
		$model->transferred_to_contract_id = null;
	    $model->ti_status = $model->setTidStatus($tid);
	    if ($model->ti_status == 'Exported') {
		$tid->exported_date = date("Y-m-d");
		if (!$tid->save())
		    Yii::app()->user->setFlash('error', CHtml::errorSummary($tid));
	    }
	    if ($model->save()) {
		Yii::app()->user->setFlash('success', Yii::t('default', 'The TID Status has been updated successfully'));
		$this->redirect(array('view', 'id' => $model->tid_id));
	    } else {
		Yii::app()->user->setFlash('error', CHtml::errorSummary($model));
	    }
	}

	$this->render('/tidStatus/update', array(
	    'model' => $model,
		// 'tid_id' => $id
	));
    }

    /**
     * Export Tid statuses info to excel
     */
    public function actionExportToExcelStatusInfo($tid_id = null) {
	$model = new TidStatus('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['TidStatus']))
	    $model->attributes = $_GET['TidStatus'];

	//Export Data to Excel File
	$header = array(
	    'tid_id' => Yii::t('default', 'TID'),
	    'ti_type_id' => Yii::t('default', 'TI Type'),
	    'contract_id' => Yii::t('default', 'Contract Ref'),
	    'contract_expiry_date' => Yii::t('default', 'Contract Expiry Date'),
	    'ti_issue_date' => Yii::t('default', 'Issue Date'),
	    'ti_expiry_date' => Yii::t('default', 'Expiry Date'),
	    'cca_id' => Yii::t('default', 'CCA'),
	    'ti_status' => Yii::t('default', 'Status'),
	    'comments' => Yii::t('default', 'Comments'),
	    'under_processing' => Yii::t('default', 'Under Processing'),
	    'renewal_stage_id' => Yii::t('default', 'Processing Stage'),
	    'last_edited_by' => Yii::t('default', 'TI Status Updated By'),
	    'multi_single' => Yii::t('default', 'Single Segment/Multi-Segment'),
	    'segment_name' => Yii::t('default', 'Segments'),
	);

	$column = "tid.tid_no,ti_type.name as ti_type_name,contract.reference,contract.expiry_date as contract_expiry_date,ti_issue_date,
            if(ti_expiry_date = '0000-00-00','',ti_expiry_date),
            cca.full_name,ti_status,comments,under_processing,renewal_stage.name as renewal_stage_name,user.username,
                  If( isnull( segment_name ) , '', If( LOCATE( ',', segment_name ) , 'Multi', 'Single' ) ) as multi_single,tid.segment_name";

	$excel_file_name = Yii::t('default', 'TID Statuses');
	$model->exportToExcel($excel_file_name, $model, $header, $column, $model->ModelCriteria($tid_id));
    }

    /**
     * Renew TID
     */
    public function actionRenew($id) {
	$tid = $this->loadModel($id);
	$model = new TidStatus();
	$last_status_model = $tid->getLastStatus();
	if ($model->ti_expiry_date == '0000-00-00')
	    $model->ti_expiry_date = '';
	if (isset($_POST['TidStatus'])) {
	    if ($last_status_model->ti_status == 'Expired') {
		$fine = Setting::getFine();
		$days = TidansActiveRecord::dateDiff($last_status_model->ti_expiry_date, date("Y-m-d"));
		$last_status_model->expiry_days = $days + 1;
		$last_status_model->fine = $fine * ($days + 1);
		if (!$last_status_model->save())
		    Yii::app()->user->setFlash('error', CHtml::errorSummary($last_status_model));
	    }


	    $model->attributes = $_POST['TidStatus'];
	    $model->tid_id = $id;
	    $model->ti_status = $model->setTidStatus($tid);
	    if ($model->ti_status == 'Exported') {
		$tid->exported_date = date("Y-m-d");
		if (!$tid->save())
		    Yii::app()->user->setFlash('error', CHtml::errorSummary($tid));
	    }

	    if ($model->under_processing == 'No' || $model->under_processing == 0)
		$model->renewal_stage_id = null;
	    if ($model->save()) {
		Yii::app()->user->setFlash('success', Yii::t('default', 'The TID has been renewed successfully'));
		$this->redirect(array('view', 'id' => $model->tid_id));
	    }
	    else
		Yii::app()->user->setFlash('error', CHtml::errorSummary($model));
	}

	$this->render('/tid/renew', array(
	    'model' => $model,
	    'tid_id' => $id,
	    'tid_no' => $tid->tid_no
	));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'view' page of TID
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDeleteStatus($id, $tid_id) {
	$model = TidStatus::model()->findByPk($id);

	$success_msg = Yii::t('default', 'The TID Status has been deleted successfully');
	$failer_msg = Yii::t('default', 'You cannot delete this element because  it is referenced by dashboard reports');
	try {



	    $model->delete();

	    if (!isset($_GET['ajax'])) { // Check Wether the data grid will be updated through AJAX or Not
		Yii::app()->user->setFlash('success', $success_msg);
		$tid = Tid::model()->findByPk($tid_id);
		if ($tid->tidStatuses == null || count($tid->tidStatuses) == 0)
		    $this->redirect(array('view', 'id' => $tid_id));
	    } else {
		$tid = Tid::model()->findByPk($tid_id);
		if ($tid->tidStatuses == null || count($tid->tidStatuses) == 0)
		    echo "refresh@";
		Yii::app()->user->setFlash('success', $success_msg);
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
	    }
	} catch (CDbException $e) { // here the item is referenced by another item
	    if (!isset($_GET['ajax'])) {
		Yii::app()->user->setFlash('error', $failer_msg);
	    } else {
		Yii::app()->user->setFlash('error', $failer_msg);
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
	    }
	} catch (Exception $e) { // here the item is referenced by another item
	    if (!isset($_GET['ajax'])) {
		Yii::app()->user->setFlash('error', $e->getMessage());
	    } else {
		Yii::app()->user->setFlash('error', $e->getMessage());
		echo "<div class='flash-msg'>" . "<br>";
		$this->widget('bootstrap.widgets.TbAlert');
		echo "</div>";
	    }
	}
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'view' page of TID

     */
    public function actionProcessTiType() {

	if (isset($_POST['ti_type_id'])) {
	    $ti_type = TiType::model()->findByPk($_POST['ti_type_id']);
	    if ($ti_type->validity_mode == 'Based on Validity Days') {
		echo date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d"))) . " +$ti_type->validity_days day"));
		return;
	    } else
	    if ($ti_type->validity_mode == 'Until Contract Ends') {
		if (isset($_POST['contract_id'])) {
		    $contract = Contract::model()->findByPk($_POST['contract_id']);
		    if ($contract != null)
			echo date("Y-m-d", strtotime($contract->expiry_date));
		    return;
		}
		if (isset($_POST['status_contract_id'])) {
		    $contract = Contract::model()->findByPk($_POST['status_contract_id']);
		    if ($contract != null)
			echo date("Y-m-d", strtotime($contract->expiry_date));
		    return;
		}
	    } else {
		echo "Permanent";
		return;
	    }
	}
	if (isset($_POST['status_ti_type_id'])) {
	    $ti_type = TiType::model()->findByPk($_POST['status_ti_type_id']);
	    if ($ti_type->validity_mode == 'Based on Validity Days') {
		echo date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d"))) . " +$ti_type->validity_days day"));
		return;
	    } else
	    if ($ti_type->validity_mode == 'Until Contract Ends') {
		if (isset($_POST['status_contract_id'])) {
		    $contract = Contract::model()->findByPk($_POST['status_contract_id']);
		    if ($contract != null)
			echo date("Y-m-d", strtotime($contract->expiry_date));
		    return;
		}
		if (isset($_POST['contract_id'])) {
		    $contract = Contract::model()->findByPk($_POST['contract_id']);
		    if ($contract != null)
			echo date("Y-m-d", strtotime($contract->expiry_date));
		    return;
		}
	    } else {
		echo "Permanent";
		return;
	    }
	}

	echo '';
    }

    /**
     * check renewal stage if it's of type under transfer of privilege
     */
    public function actionProcessRenewalStage() {

	if (isset($_POST['renewal_stage_id'])) {
	    if ($_POST['renewal_stage_id'] == Setting::getUnderTransferOfPrivilege())
		echo "true";
	    else
		echo "false";
	}
    }

    /**
     * Export Invoices summary info to excel
     */
    public function actionSummaryExportToExcel() {
	$model = new VTid('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VTid']))
	    $model->attributes = $_GET['VTid'];

	//Export Data to Excel File
	$header = array(
	    'number' => Yii::t('default', 'Number'),
	    'tid_no' => Yii::t('default', 'TID No'),
	    'initial_issue_date' => Yii::t('default', 'Initial Issue Date'),
	    'customs_letter_no' => Yii::t('default', 'Customs Letter No'),
	    'customs_letter_date' => Yii::t('default', 'Customs Letter Date'),
	    'shipment_ref' => Yii::t('default', 'Shipment Ref'),
	    'point_of_entry_name' => Yii::t('default', 'Point of Entry'),
	    'original_documnets_delivery_date' => Yii::t('default', 'Original Documnets Delivery Date'),
	    'value' => Yii::t('default', 'Value'),
	    'tid_last_edited_by' => Yii::t('default', 'TID Updated By'),
	    'ti_type_name' => Yii::t('default', 'TI Type'),
	    'last_status_ti_issue_date' => Yii::t('default', 'Issue Date'),
	    'last_status_ti_expiry_date' => Yii::t('default', 'Expiry Date'),
	    'cca_full_name' => Yii::t('default', 'CCA'),
	    'contract_reference' => Yii::t('default', 'Contract Ref'),
	    'last_status_ti_status' => Yii::t('default', 'Status'),
	    'last_status_comments' => Yii::t('default', 'Comments'),
	    'last_status_under_processing' => Yii::t('default', 'Under Processing'),
	    'renewal_stage_name' => Yii::t('default', 'Processing Stage'),
	    'status_last_edited_by' => Yii::t('default', 'TI Status Updated By'),
	    'multi_single' => Yii::t('default', 'Single Segment/Multi-Segment'),
	    'segment_name' => Yii::t('default', 'Segments'),
	);

	$column = "number,tid_no,initial_issue_date,customs_letter_no,
	    if(customs_letter_date = '0000-00-00','',customs_letter_date),
	    shipment_ref,point_of_entry_name,
	    if(original_documnets_delivery_date = '0000-00-00','',original_documnets_delivery_date),
	    if(isnull(value) || value = '','',CONCAT('$',value)),
            tid_last_edited_by,
	    ti_type_name,last_status_ti_issue_date,last_status_ti_expiry_date,cca_full_name,contract_reference,last_status_ti_status,last_status_comments,last_status_under_processing,renewal_stage_name,status_last_edited_by,
             If( isnull( segment_name ) , '', If( LOCATE( ',', segment_name ) , 'Multi', 'Single' ) ) as multi_single,segment_name";

	$excel_file_name = Yii::t('default', 'TIDs Summary');
	$model->exportToExcel($excel_file_name, $model, $header, $column, $model->ModelCriteria());
    }

    /**
     * Export invoices detailed info to excel
     */
    public function actionDetailExportToExcel() {
	$model = new VTid('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['VTid']))
	    $model->attributes = $_GET['VTid'];

	//Export Data to Excel File
	$header = array(
	    'number' => Yii::t('default', 'Number'),
	    'tid_no' => Yii::t('default', 'TID No'),
	    'initial_issue_date' => Yii::t('default', 'Initial Issue Date'),
	    'customs_letter_no' => Yii::t('default', 'Customs Letter No'),
	    'customs_letter_date' => Yii::t('default', 'Customs Letter Date'),
	    'shipment_ref' => Yii::t('default', 'Shipment Ref'),
	    'point_of_entry_name' => Yii::t('default', 'Point of Entry'),
	    'original_documnets_delivery_date' => Yii::t('default', 'Original Documnets Delivery Date'),
	    'value' => Yii::t('default', 'Value'),
	    'tid_last_edited_by' => Yii::t('default', 'TID Updated By'),
	    'ti_type_name' => Yii::t('default', 'TI Type'),
	    'last_status_ti_issue_date' => Yii::t('default', 'Issue Date'),
	    'last_status_ti_expiry_date' => Yii::t('default', 'Expiry Date'),
	    'cca_full_name' => Yii::t('default', 'CCA'),
	    'contract_reference' => Yii::t('default', 'Contract Ref'),
	    'last_status_ti_status' => Yii::t('default', 'Status'),
	    'last_status_comments' => Yii::t('default', 'Comments'),
	    'last_status_under_processing' => Yii::t('default', 'Under Processing'),
	    'renewal_stage_name' => Yii::t('default', 'Processing Stage'),
	    'status_last_edited_by' => Yii::t('default', 'TI Status Updated By'),
	    'invoice_number' => Yii::t('default', 'Invoice Number'),
	    'invoice_value' => Yii::t('default', 'Invoice Value'),
	    'invoice_description' => Yii::t('default', 'Invoice Description'),
	    'comments' => Yii::t('default', 'Invoice Comments'),
	    'last_edited_by' => Yii::t('default', 'Invoice Updated By'),
	    'po_number' => Yii::t('default', 'PO Number'),
	    'segment_name' => Yii::t('default', 'Segment'),
	    'serial_number' => Yii::t('default', 'Serial Number'),
	    'asset_number' => Yii::t('default', 'Asset Number'),
	    'part_number' => Yii::t('default', 'Part Number'),
	    'type_of_asset_name' => Yii::t('default', 'Type of Asset'),
	    'asset_description' => Yii::t('default', 'Asset Description'),
	    'asset_status' => Yii::t('default', 'Asset Status'),
	    'plate_number' => Yii::t('default', 'Plate Number'),
	    'chassis_number' => Yii::t('default', 'Chassis Number'),
	    'vehicle_book_expiry_date' => Yii::t('default', 'Vehicle Book Expiry Date'),
	    'vehicle_book_status' => Yii::t('default', 'Vehicle Book Status'),
	    'radioactive_license_number' => Yii::t('default', 'Radioactive License Number'),
	    'radioactive_license_expiry_date' => Yii::t('default', 'Radioactive License Expiry Date'),
	    'radioactive_license_status' => Yii::t('default', 'Radioactive License Status'),
	    'username' => Yii::t('default', 'Asset Updated By'),
	);

	$column = "t.number,t.tid_no,initial_issue_date,customs_letter_no,
	     if(customs_letter_date = '0000-00-00','',customs_letter_date),
	    shipment_ref,point_of_entry_name,
	    if(original_documnets_delivery_date = '0000-00-00','',original_documnets_delivery_date),
	    if(isnull(t.value) || t.value = '','',CONCAT('$',t.value)), tid_last_edited_by,
	    ti_type_name,last_status_ti_issue_date,last_status_ti_expiry_date,cca_full_name,contract_reference,last_status_ti_status,last_status_comments,last_status_under_processing,renewal_stage_name,status_last_edited_by,
	    invoice.number as invoice_number,
             if(isnull(invoice.value) || invoice.value = '','',CONCAT('$',invoice.value))as invoice_value,             
            invoice.description as invoice_description,invoice.comments,user.username,po_number,v_asset.segment_name,serial_number,asset_number,part_number,
	    type_of_asset_name,v_asset.description as asset_description,asset_status,plate_number,chassis_number,vehicle_book_expiry_date,vehicle_book_status,
	    radioactive_license_number,radioactive_license_expiry_date,radioactive_license_status,v_asset.username as asset_updated_by";

	$excel_file_name = Yii::t('default', 'TIDs Detailed');
	$model->exportToExcel($excel_file_name, $model, $header, $column, $model->ModelCriteria('with InvoiceAssets'));
    }

}
