PageRenderTime 375ms CodeModel.GetById 134ms app.highlight 134ms RepoModel.GetById 18ms app.codeStats 0ms

/mcmis/protected/controllers/PtbhadarateController.php

http://mcmis.googlecode.com/
PHP | 347 lines | 243 code | 40 blank | 64 comment | 28 complexity | c1f5c749b37af0292639428363508f36 MD5 | raw file
Possible License(s): LGPL-2.1
  1<?php
  2
  3class PtbhadarateController extends Controller {
  4
  5    /**
  6     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  7     * using two-column layout. See 'protected/views/layouts/column2.php'.
  8     */
  9//	public $layout='//layouts/column2';
 10    public $layout = '//layouts/eastsouthwest';
 11
 12    /**
 13     * @return array action filters
 14     */
 15    public function filters() {
 16        return array(
 17            'accessControl', // perform access control for CRUD operations
 18        );
 19    }
 20
 21    /**
 22     * Specifies the access control rules.
 23     * This method is used by the 'accessControl' filter.
 24     * @return array access control rules
 25     */
 26    public function accessRules() {
 27        return array(
 28            array('allow', // allow all users to perform 'index' and 'view' actions
 29                'actions' => array('index', 'view', 'jsonmessage'),
 30                'users' => array('*'),
 31            ),
 32            array('allow', // allow authenticated user to perform 'create' and 'update' actions
 33                'actions' => array('create', 'update', 'gridupdate', 'generate'),
 34                'users' => array('@'),
 35            ),
 36            array('allow', // allow admin user to perform 'admin' and 'delete' actions
 37                'actions' => array('popupview', 'admin', 'delete'),
 38                'users' => array('admin'),
 39            ),
 40            array('deny', // deny all users
 41                'users' => array('*'),
 42            ),
 43        );
 44    }
 45
 46    /**
 47     * Displays a particular model.
 48     * @param integer $id the ID of the model to be displayed
 49     */
 50    public function actionView($id) {
 51        $this->render('view', array(
 52            'model' => $this->loadModel($id),
 53        ));
 54    }
 55
 56    /**
 57     * Creates a new model.
 58     * If creation is successful, the browser will be redirected to the 'view' page.
 59     */
 60    public function actionCreate() {
 61        $model = new Ptbhadarate;
 62
 63        // Uncomment the following line if AJAX validation is needed
 64        $this->performAjaxValidation($model);
 65
 66        if (isset($_POST['Ptbhadarate'])) {
 67            $model->attributes = $_POST['Ptbhadarate'];
 68            if ($model->save())
 69                $this->redirect(array('create', 'id' => $model->idptbhadarate));
 70//				$this->redirect(array('view','id'=>$model->idptbhadarate));
 71        }
 72
 73        $this->render('create', array(
 74            'model' => $model,
 75        ));
 76    }
 77
 78    /**
 79     * Updates a particular model.
 80     * If update is successful, the browser will be redirected to the 'view' page.
 81     * @param integer $id the ID of the model to be updated
 82     */
 83    public function actionUpdate($id) {
 84        $model = $this->loadModel($id);
 85
 86        // Uncomment the following line if AJAX validation is needed
 87        $this->performAjaxValidation($model);
 88
 89        if (isset($_POST['Ptbhadarate'])) {
 90            $model->attributes = $_POST['Ptbhadarate'];
 91            if ($model->save())
 92                $this->redirect(array('admin'));
 93//				$this->redirect(array('view','id'=>$model->idptbhadarate));
 94        }
 95
 96        $this->render('update', array(
 97            'model' => $model,
 98        ));
 99    }
100
101    /**
102     * Deletes a particular model.
103     * If deletion is successful, the browser will be redirected to the 'admin' page.
104     * @param integer $id the ID of the model to be deleted
105     */
106    public function actionDelete($id) {
107        if (Yii::app()->request->isPostRequest) {
108            // we only allow deletion via POST request
109            $this->loadModel($id)->delete();
110
111            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
112            if (!isset($_GET['ajax']))
113                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
114        }
115        else
116            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
117    }
118
119    /**
120     * Lists all models.
121     */
122    public function actionIndex() {
123        $dataProvider = new CActiveDataProvider('Ptbhadarate');
124        $this->render('index', array(
125            'dataProvider' => $dataProvider,
126        ));
127    }
128
129    /**
130     * Manages all models.
131     */
132    public function actionAdmin() {
133        $model = new Ptbhadarate('search');
134        $model->unsetAttributes();  // clear any default values
135        if (isset($_GET['Ptbhadarate']))
136            $model->attributes = $_GET['Ptbhadarate'];
137
138        $this->render('admin', array(
139            'model' => $model,
140        ));
141    }
142
143    /**
144     * Manages popupview.
145     */
146    public function actionPopupview() {
147        $model = new Ptbhadarate('search');
148        $model->unsetAttributes();  // clear any default values
149        if (isset($_GET['Ptbhadarate']))
150            $model->attributes = $_GET['Ptbhadarate'];
151
152        if (isset($_REQUEST['isAjaxRequest']) && $_REQUEST['isAjaxRequest'] == '1') {
153            Yii::app()->clientscript->scriptMap['jquery.js'] = false;
154            echo CJSON::encode(array(
155                'status' => 'failure',
156                'div' => $this->renderPartial('popupview', array('model' => $model, 'id' => $_REQUEST['id']), true, true)));
157            exit;
158        }
159
160        $this->render('popupview', array(
161            'model' => $model, 'id' => ''
162        ));
163    }
164
165    /**
166     * Returns the data model based on the primary key given in the GET variable.
167     * If the data model is not found, an HTTP exception will be raised.
168     * @param integer the ID of the model to be loaded
169     */
170    public function loadModel($id) {
171        $model = Ptbhadarate::model()->findByPk((int) $id);
172        if ($model === null)
173            throw new CHttpException(404, 'The requested page does not exist.');
174        return $model;
175    }
176
177    /**
178     * Performs the AJAX validation.
179     * @param CModel the model to be validated
180     */
181    protected function performAjaxValidation($model) {
182        if (isset($_POST['ajax']) && $_POST['ajax'] === 'ptbhadarate-form') {
183            echo CActiveForm::validate($model);
184            Yii::app()->end();
185        }
186    }
187
188    public function actionJsonmessage() {
189        $jsonmessage = array();
190        $jsonmessage['status code'] = 200;
191        $jsonmessage['message'] = ' - ';
192        if (isset($_REQUEST['cid'])) {
193            $model = Ptbhadarate::model()->findByPk($_REQUEST['cid']);
194            if ($model) {
195                $jsonmessage['message'] = $model->aresidential;
196            }
197        }
198
199        header('Content-type: application/json');
200        echo CJSON::encode($jsonmessage);
201        Yii::app()->end();
202    }
203
204    public function actionGridupdate() {
205
206        $ptbhadarates = array();
207        $ptbhadarates = $_POST['Ptbhadarate'];
208        $valid = true;
209        $errors = array();
210        foreach ($ptbhadarates as $key => $value) {
211
212            $model = $this->loadModel($key);
213            $model->attributes = $value;
214//            if(!$model->validate(NULL, false)){
215            if (!$model->save()) {
216                $valid = false;
217
218                $model_errors = $model->getErrors();
219                foreach ($model_errors as $mekey => $mevalue) {
220                    foreach ($mevalue as $key => $value) {
221                        $errors[$model->idptbhadarate . '_' . $mekey] = $value . " (" . $model->idccward0->idcczone0->zonename . " - " . $model->idccward0->wardname . ")";
222                    }
223                }
224            }
225        }
226
227
228        $model_new = new Ptbhadarate('search');
229        $model_new->unsetAttributes();  // clear any default values
230        if (isset($_GET['Ptbhadarate']))
231            $model->attributes = $_POST['Ptbhadarate'];
232
233        $model_new->addErrors($errors);
234
235        $this->render('admin', array(
236            'model' => $model_new,
237        ));
238    }
239
240    public function actionGridupdate_original() {
241        //update aresidential column
242        $rates = $_REQUEST['aresidential'];
243        foreach ($rates as $key => $value) {
244            $sql = "Update {{ptbhadarate}} set aresidential=$value WHERE idptbhadarate=$key";
245            $connection = Yii::app()->db;
246            $connection->createCommand($sql)->execute();
247        }
248        //update acommercial column
249        $rates = $_REQUEST['acommercial'];
250        foreach ($rates as $key => $value) {
251            $sql = "Update {{ptbhadarate}} set acommercial=$value WHERE idptbhadarate=$key";
252            $connection = Yii::app()->db;
253            $connection->createCommand($sql)->execute();
254        }
255        //update bresidential column
256        $rates = $_REQUEST['bresidential'];
257        foreach ($rates as $key => $value) {
258            $sql = "Update {{ptbhadarate}} set bresidential=$value WHERE idptbhadarate=$key";
259            $connection = Yii::app()->db;
260            $connection->createCommand($sql)->execute();
261        }
262        //update bcommercial column
263        $rates = $_REQUEST['bcommercial'];
264        foreach ($rates as $key => $value) {
265            $sql = "Update {{ptbhadarate}} set bcommercial=$value WHERE idptbhadarate=$key";
266            $connection = Yii::app()->db;
267            $connection->createCommand($sql)->execute();
268        }
269        //update cresidential column
270        $rates = $_REQUEST['cresidential'];
271        foreach ($rates as $key => $value) {
272            $sql = "Update {{ptbhadarate}} set cresidential=$value WHERE idptbhadarate=$key";
273            $connection = Yii::app()->db;
274            $connection->createCommand($sql)->execute();
275        }
276        //update ccommercial column
277        $rates = $_REQUEST['ccommercial'];
278        foreach ($rates as $key => $value) {
279            $sql = "Update {{ptbhadarate}} set ccommercial=$value WHERE idptbhadarate=$key";
280            $connection = Yii::app()->db;
281            $connection->createCommand($sql)->execute();
282        }
283        //update dresidential column
284        $rates = $_REQUEST['dresidential'];
285        foreach ($rates as $key => $value) {
286            $sql = "Update {{ptbhadarate}} set dresidential=$value WHERE idptbhadarate=$key";
287            $connection = Yii::app()->db;
288            $connection->createCommand($sql)->execute();
289        }
290        //update dcommercial column
291        $rates = $_REQUEST['dcommercial'];
292        foreach ($rates as $key => $value) {
293            $sql = "Update {{ptbhadarate}} set dcommercial=$value WHERE idptbhadarate=$key";
294            $connection = Yii::app()->db;
295            $connection->createCommand($sql)->execute();
296        }
297
298        $this->redirect(array('ptbhadarate/admin'));
299    }
300
301    public function actionGenerate() {
302        if (isset(Yii::app()->session['ccfyear'])) {
303            $idccfyear = Yii::app()->session['ccfyear']->idccfyear;
304            $ccfyear_previous = Ccfyear::model()->findbypk((int) $idccfyear - 1);
305        }
306
307        $criteria = new CDbCriteria;
308        $criteria->compare('idccfyear', $idccfyear, true);
309        $ptbhadarates = Ptbhadarate::model()->findAll($criteria);
310        if (count($ptbhadarates) == 0) {
311
312            if (isset($ccfyear_previous) && $ccfyear_previous) {
313                $criteria = new CDbCriteria;
314                $criteria->compare('idccfyear', $ccfyear_previous->idccfyear, true);
315                $ptbhadarates = Ptbhadarate::model()->findAll($criteria);
316            }
317
318            $ccwards = Ccward::model()->findAll();
319            foreach ($ccwards as $ccward) {
320                $ptbhadarate = new Ptbhadarate();
321                $ptbhadarate->idccfyear = $idccfyear;
322                $ptbhadarate->idccward = $ccward->idccward;
323                if (isset($ptbhadarates) && count($ptbhadarates) > 0) {
324                    foreach ($ptbhadarates as $ptbhadaratevalue) {
325                        if ($ccward->idccward == $ptbhadaratevalue->idccward) {
326                            $ptbhadarate->aresidential = $ptbhadaratevalue->aresidential;
327                            $ptbhadarate->acommercial = $ptbhadaratevalue->acommercial;
328                            $ptbhadarate->bresidential = $ptbhadaratevalue->bresidential;
329                            $ptbhadarate->bcommercial = $ptbhadaratevalue->bcommercial;
330                            $ptbhadarate->cresidential = $ptbhadaratevalue->cresidential;
331                            $ptbhadarate->ccommercial = $ptbhadaratevalue->ccommercial;
332                            $ptbhadarate->dresidential = $ptbhadaratevalue->dresidential;
333                            $ptbhadarate->dcommercial = $ptbhadaratevalue->dcommercial;
334                        }
335                    }
336                }
337                echo $ptbhadarate->idccward . ' ' . $ptbhadarate->aresidential . '</br>';
338                $ptbhadarate->save();
339            }
340            Yii::app()->user->setFlash('success', Yii::t('application', 'Ptbhadarate for current finanical year has been generated successfully.'));
341        } else {
342            Yii::app()->user->setFlash('notice', Yii::t('application', 'Ptbhadarate for current finanical year already exist.'));
343        }
344        $this->redirect(array('ptbhadarate/admin'));
345    }
346
347}