<?php

class ReportController extends GxController
{

    public $layout = 'application.views.layouts.column1';

    public function filters()
    {
        return array(
            'accessControl',
        );
    }


    public function accessRules()
    {
        return array(
            array('allow',
                'expression' => 'Yii::app()->user->checkAccess(Yii::app()->controller->module->id.":".Yii::app()->controller->id.":".Yii::app()->controller->action->id)'),
            array('deny', 'users' => array('*'))
        );
    }


    public function actionView($id)
    {
        $model = $this->loadModel($id, 'Report');

        if (isset($_GET['Report']))
            $model->setAttributes($_GET['Report']);


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


    /**
     *   Excel JSON
     */
    public function actionExcel($id)
    {
        /**
         * "Godina":"2016",
         * "Mesec":"01",
         * "oranizaciona_jedinica":"Komercijala",
         * "naziv_artikla":"c kesa bez rucke 21x12x31",
         * "broj_fakture":"ifr-18-2016",
         * "jedinica_mere":"kg",
         * "nabavna_cena":"189.0000",
         * "prosecna_nabavna_cena":"189.0000",
         * "prodajna_cena":"245.7000000000",
         * "fakturisana_kolicina":"24",
         * "nabavna_vrednost":"4536",
         * "prodajna_vrednost":"5896.8000",
         * "razlika":"1360.8000",
         * "NVPR":"0.76923077"},
         */

        $model = $this->loadModel($id, 'Report');

        $this->layout = false;
        header('Content-type: application/json');
        // echo $niz;
        echo $model->convertToJSON(1000);
        die();
        //Yii::app()->end();


    }

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


        if ($model->pivot_config == null) {
            $model->pivot_config = $model->defaultPivotConfig();
        }


        if (isset($_POST['Report'])) {
            $model->setAttributes($_POST['Report']);

            if ($model->save()) {
                if (Yii::app()->getRequest()->getIsAjaxRequest()) Yii::app()->end(); else
                    $this->redirect(array('view', 'id' => $model->report_id));
            }
        }

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

    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Report');


        if (isset($_POST['Report'])) {
            $model->setAttributes($_POST['Report']);

            if ($model->isValidSQL()) {
                if ($model->save()) {
                    $this->redirect(array('view', 'id' => $model->report_id));
                }
            }
        }

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

    public function actionLocale()
    {
        $model = new Report();

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

    public function actionDelete($id)
    {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Report')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex()
    {
        $model = new Report('search');
        $model->unsetAttributes();

        if (isset($_GET['Report']))
            $model->setAttributes($_GET['Report']);

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

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

        if (isset($_GET['Report']))
            $model->setAttributes($_GET['Report']);

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

}