<?php

class MasWarehouseController extends Controller
{
    public function filters()
    {
        return array('accessControl');
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('index','getList','create','disable'),
                'users'=>array('@'),
            ),
            array('deny','users'=>array('*')),
        );
    }

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetList()
    {
        $model = new MasWarehouse();

        $draw   = $_POST['draw'];
        $offset = $_POST['start'];
        $limit  = $_POST['length'];

        $count = $model->count();

        $rows = $model->findAll(array(
            'limit'=>$limit,
            'offset'=>$offset,
            'order'=>'warehouse_name ASC'
        ));

        $data['draw'] = $draw;
        $data['recordsTotal'] = $count;
        $data['recordsFiltered'] = $count;

        $result = array();
        $i = $offset;

        foreach($rows as $row){

            $baseWarehouse = ($row->is_base_warehouse == 'Y')
                ? '<span class="label label-success">Yes</span>'
                : 'No';

            $result[] = array(
                ++$i,
                $row->warehouse_name,
                $baseWarehouse,
                array(
                    'id'=>$row->id,
                    'status'=>$row->isActive
                )
            );
        }

        $data['data'] = $result;

        echo json_encode($data);
        Yii::app()->end();
    }

    public function actionCreate()
    {
        $model = new MasWarehouse();
        $id = (int) Yii::app()->request->getParam('id');

        if($id > 0){
            $model = MasWarehouse::model()->findByPk($id);
        }

        if(isset($_POST['MasWarehouse']))
        {
            if((int)$_POST['MasWarehouse']['id'] > 0){
                $model = MasWarehouse::model()->findByPk($_POST['MasWarehouse']['id']);
            }

            $model->attributes = $_POST['MasWarehouse'];

            if($model->isNewRecord){
                $model->created_by = Yii::app()->user->id;
                $model->created_datetime = date('Y-m-d H:i:s');
                $model->isActive = 'Y';
            }

            $model->updated_by = Yii::app()->user->id;
            $model->updated_datetime = date('Y-m-d H:i:s');

            if($model->save()){

                if($model->is_base_warehouse == 'Y'){
                    MasWarehouse::model()->updateAll(
                        array('is_base_warehouse'=>'N'),
                        'id != :id',
                        array(':id'=>$model->id)
                    );
                }

                echo CJSON::encode(array('status'=>'success'));
                Yii::app()->end();
            }
        }

        Yii::app()->clientScript->scriptMap = array(
            '*jquery.js'=>false,
            '*jquery.min.js'=>false,
            '*jquery-ui.js'=>false,
            '*jquery-ui.min.js'=>false
        );

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

    public function actionDisable()
    {
        $id = Yii::app()->request->getParam('id');

        $model = MasWarehouse::model()->findByPk($id);

        if($model){

            if($model->is_base_warehouse == 'Y'){
                echo json_encode(array('status'=>'fail'));
                Yii::app()->end();
            }

            $model->isActive = ($model->isActive == 'Y') ? 'N' : 'Y';
            $model->updated_by = Yii::app()->user->id;
            $model->updated_datetime = date('Y-m-d H:i:s');
            $model->save();
        }

        echo json_encode(array('status'=>'success'));
        Yii::app()->end();
    }
}