/**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = MediabrokerCommission::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $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();
 }