<?php

class MasexpensetypeController extends Controller
{
    public function accessRules() {

        return array(

                array('allow', // allow all users to perform 'index' and 'view' actions

                  'actions' => array('login', 'error', 'logout','newlogin'),

                  'users' => array('*'),

                  ),

                array('allow', // allow authenticated user to perform 'create' and 'update' actions

                  'actions' => array('Index','GetList','Create','SaveExpenseType','Disable'),

                  'expression' => '$user->isMember()',

                  ),

                array('deny', // deny all users

                  'users' => array('*'),

                  ),

                );

    }
    
	public function actionIndex()
	{
        
        $user_id   = (int) Yii::app()->user->id; 
        $modeluser = User::model()->findByPk($user_id);

        if ($modeluser->user_group_id != 1) { 
            $this->redirect(Yii::app()->baseUrl . '/index.php');
            Yii::app()->end();
        }
		$this->render('index');
	}

	public function actionGetList() {
        $model = new MasExpenseType();
        $draw = $_POST['draw'];
        $offset = $_POST['start'];
        $limit = $_POST['length'];

        $condition = '1="1"';
        
        if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
            $searchString = $_POST['search']['value'];
            $condition .= " AND (id LIKE '%" . $searchString . "%' 
            OR expense_type LIKE '%" . $searchString . "%' 
            OR account_no LIKE '%" . $searchString . "%')";
        }

        $count = $model->count(array('condition' => $condition));

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

        $recordsTotal = $count;
        $recordsFiltered = $count;

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

        $rowData = array();
        $m = $offset;

        foreach ($model as $row) {
            $rowData[] = array(
            ++$m,
            $row['expense_type'], 
            $row['account_no'],          
            array(                      
                'id' => $row['id'],
                'code' => $row['expense_type'],
                'status' => $row->isActive
            )
        );
        }

        $data['data'] = $rowData;

        echo json_encode($data);
    }

    public function actionCreate()
    {
        $model=new MasExpenseType();
        $id = (int) Yii::app()->request->getParam('id');
        if($id>0){
            $model=$model->findByPk($id);
        }
     Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false,'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false , 'jquery-2.1.4.min.js' => false);  
    $this->render('create', array('model' => $model,'from' =>$from), false, true);
    }

    public function actionSaveExpenseType()
    {
    $model = new MasExpenseType();
        if (isset($_POST['MasExpenseType']))
        {
            $model->created_by = Yii::app()->user->id;
            $model->created_datetime =date('Y-m-d h:i:s');
            if ((int) @$_POST['MasExpenseType']['id'] > 0)
            {   $model->updated_by = Yii::app()->user->id;
                $model->updated_datetime =date('Y-m-d h:i:s');
                $id= (int) @$_POST['MasExpenseType']['id'];
                $model=$model->findByPk($id);
                $audit_type = 'UPDATED';
            }else{
                $audit_type = 'CREATED';
            }
            // if($_POST['MasStatus']['isDefault'] == "Y"){
            //     MasStatus::model()->updateAll(array( 'isDefault' => 'N' ), 'isDefault = "Y"' );
            // }
            // $model->isDefault = $_POST['MasExpenseType']['isDefault'];
            $model->attributes=$_POST['MasExpenseType'];
            if($model->validate())
               
            {    
                 $model->save();
                  // inserting to audit log
                //   AuditLog::saveAuditLogs(
                //     'STATUS',
                //     Yii::app()->user->id,
                //     $audit_type,
                //     " with name: - {$model->status_name}  "
                // );
                // end audit log
                $jsonData['status'] = 'success';
                $jsonData['frm'] = $_POST['expensetype-form'];
                $jsonData['from'] = $_POST['from'];
                $jsonData['id'] = $model->id;
                echo CJSON::encode($jsonData);
            }
            else
            {
                $error = CActiveForm::validate($model, '', false);
                if ($error != '[]')
            echo $error;
            }
        }
    }
    public function actionDisable() {
        $id = Yii::app()->request->getparam('id');
        $status = Yii::app()->request->getparam('status');
        $model = new MasExpenseType();
        $model = $model->findByPk($id);
        $model->isActive = ($status == "Y" ) ? "N" : "Y";
        $model->save();
        echo json_encode(array("status" => "success"));
    }
 
}