/**
  * Displays a particular model.
  * @param integer $id the ID of the model to be displayed
  */
 public function actionView($id)
 {
     $this->title = 'Rincian UP';
     //UP model detail view
     $model = $this->loadModel($id);
     $userId = Yii::app()->user->id;
     $user = User::model()->findByPk($userId);
     //UPDetail model Gridview
     $modelDetail = new UpDetail('searchDetail');
     $modelDetail->unsetAttributes();
     if (isset($_GET['UpDetail'])) {
         $modelDetail->attributes = $_GET['UpDetail'];
     }
     //Create new UPDetail
     $newDetail = new UpDetail();
     $this->performAjaxValidation($newDetail);
     if (isset($_POST['UpDetail'])) {
         if ($_POST['UpDetail']['limit'] == NULL && $_POST['UpDetail']['package_code'] == NULL) {
             Yii::app()->user->setFlash('error', 'Mohon isikan data pada form.');
         } else {
             $detailLimit = $_POST['UpDetail']['limit'];
             $packageCode = $_POST['UpDetail']['package_code'];
             $checkDetail = UpDetail::model()->findByAttributes(array('up_number_of_letter' => "{$model->number_of_letter}", 'package_code' => "{$packageCode}"));
             $package = Package::model()->findByAttributes(array('code' => $packageCode));
             $detailLists = UpDetail::model()->findAllByAttributes(array('up_number_of_letter' => $model->number_of_letter));
             $total = 0;
             if ($detailLists) {
                 foreach ($detailLists as $detailList) {
                     $total += $detailList->limit;
                 }
             }
             $totalNewLimit = $total + $detailLimit;
             if ($checkDetail) {
                 Yii::app()->user->setFlash('error', 'Paket sudah digunakan dalam UP');
             } elseif ($detailLimit > $package->limit) {
                 Yii::app()->user->setFlash('error', 'Pagu UP Detail tidak boleh melebihi pagu paket');
             } elseif ($totalNewLimit > $model->total_up) {
                 Yii::app()->user->setFlash('error', 'Pagu UP Detail tidak boleh melebihi pagu UP');
             } else {
                 $newDetail->attributes = $_POST['UpDetail'];
                 $newDetail->up_number_of_letter = $model->number_of_letter;
                 if ($newDetail->save()) {
                     //Update package account UP status from "LS" to "UP"
                     $packagesAccounts = PackageAccount::model()->findAllByAttributes(array('package_code' => $newDetail->package_code));
                     foreach ($packagesAccounts as $packagesAccount) {
                         $packagesAccount->up = "UP";
                         $packagesAccount->update();
                     }
                     Yii::app()->user->setFlash('success', 'Detail berhasil ditambahkan');
                     $this->redirect(array('view', 'id' => $model->id));
                 }
             }
         }
     }
     $this->render('view', array('model' => $model, 'modelDetail' => $modelDetail, 'addDetail' => $newDetail, 'user' => $user));
 }
 /**
  * Manages all models.
  */
 public function actionAdmin()
 {
     $model = new UpDetail('search');
     $model->unsetAttributes();
     // clear any default values
     if (isset($_GET['UpDetail'])) {
         $model->attributes = $_GET['UpDetail'];
     }
     $this->render('admin', array('model' => $model));
 }