/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new MediabrokerCommission();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['MediabrokerCommission'])) {
         $model->attributes = $_POST['MediabrokerCommission'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->mbcommission_id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Lists all models.
  */
 public function actionIndex()
 {
     $model = new MediabrokerCommission();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['MediabrokerCommission'])) {
         $model->attributes = $_POST['MediabrokerCommission'];
         if ($model->save()) {
             $this->redirect(array('index'));
         }
     }
     if (isset($_GET['MediabrokerCommission'])) {
         $model->attributes = $_GET['MediabrokerCommission'];
     }
     $this->render('index', array('model' => $model));
 }
 function afterSave()
 {
     if ($this->isNewRecord) {
         $purchasedPlan = PurchasedPlan::model()->findByPk($this->pplan_id);
         if ($purchasedPlan == null) {
             throw new CHttpException(1234, "No plan found!");
         }
         if ($purchasedPlan->type == PlanType::ADVERTISER_PLAN && $purchasedPlan->advertiser_id != '') {
             $advertiser = Advertiser::model()->findByPk($purchasedPlan->advertiser_id);
             if ($advertiser == null) {
                 throw new CHttpException(1234, "No advertiser found!");
             }
             if ($advertiser->mbroker_id != '') {
                 // insert commission for this media broker
                 $wlAccount = WhiteLabel::model()->findByPk($purchasedPlan->wlabel_id);
                 if ($wlAccount == null) {
                     throw new CHttpException(1234, "No account found!");
                 }
                 if ($wlAccount->mb_commission != '' && $wlAccount->mb_commission != 0 && $wlAccount->mb_commission_type != '' && $wlAccount->mb_commission_type != 0) {
                     if ($wlAccount->mb_commission_type == CommissionType::TYPE_FIXED) {
                         $commissionAmount = $wlAccount->mb_commission;
                     } else {
                         // percentage
                         $commissionAmount = $purchasedPlan->price / 100 * $wlAccount->mb_commission;
                     }
                     $mbCommissions = new MediabrokerCommission();
                     $mbCommissions->mbroker_id = $advertiser->mbroker_id;
                     $mbCommissions->pppayment_id = $this->pppayment_id;
                     $mbCommissions->date_created = date("Y-m-d h:i:s");
                     $mbCommissions->amount = $commissionAmount;
                     $mbCommissions->save();
                 }
             }
         }
     }
     return parent::afterSave();
 }