<?php

class ConceptoAjusteController extends Controller
{
    public $layout = '//layouts/column2';

    public function filters()
    {
        return ['accessControl', ['CrugeAccessControlFilter'], 'postOnly + delete'];
    }

    public function accessRules()
    {
        return [
            ['allow', 'actions' => ['index', 'view'], 'users' => ['*']],
            ['allow', 'actions' => ['create', 'update', 'admin', 'delete'], 'users' => ['@']],
            ['deny', 'users' => ['*']],
        ];
    }

    public function actionView($id)
    {
        $this->render('view', array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model = new ConceptoAjuste();

        if (isset($_POST['ConceptoAjuste'])) {
            $model->attributes=$_POST['ConceptoAjuste'];
            $model->setCuentaContable();
            if ($model->save()) {
                $this->redirect(array('admin'));
            }
        }

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

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $model->getCuentaContable();

        if (isset($_POST['ConceptoAjuste'])) {
            $model->attributes=$_POST['ConceptoAjuste'];
            $model->setCuentaContable();
            if ($model->save()) {
                $this->redirect(array('admin'));
            }
        }

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

    public function actionDelete($id)
    {
        $this->loadModel($id)->delete();

        if (!isset($_GET['ajax'])) {
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
    }

    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('ConceptoAjuste');
        $this->render('index', array(
            'dataProvider'=>$dataProvider,
        ));
    }

    public function actionAdmin()
    {
        $model = new ConceptoAjuste('search');
        $model->unsetAttributes();

        if (isset($_GET['ConceptoAjuste'])) {
            $model->attributes = $_GET['ConceptoAjuste'];
        }

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

    protected function loadModel($id)
    {
        $model=ConceptoAjuste::model()->findByPk($id);
        if ($model===null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax']==='concepto-ajuste-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
