<?php

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

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

		if(isset($_POST['RelatorioCancelamentos']))
		{
			$model->attributes=$_POST['RelatorioCancelamentos'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->RCC_ID));
		}

		$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, 'RelatorioCancelamentos');

		if(isset($_POST['RelatorioCancelamentos']))
		{
			$model->attributes=$_POST['RelatorioCancelamentos'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->RCC_ID));
		}

		$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)
	{
		$this->loadModel($id, 'RelatorioCancelamentos')->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

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

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

	public function actionExcel()
    {
    	$model=new RelatorioCancelamentos('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['RelatorioCancelamentos']))
			$model->attributes=$_GET['RelatorioCancelamentos'];

        $dataProvider = $model->search();
        $dataProvider->setPagination(false);
        Yii::app()->request->sendFile(
            'relatorio-pos-vendas-'. date('Ymd-His') . '.xlsx',
            $this->renderPartial('clientes_cancelar', array(
                'data' => $dataProvider->getData(),
            ), true),
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', true
        );
    }

}
