public function purchasePlan($planType, $advertiserId = '') { $transaction = Yii::app()->db->beginTransaction(); try { // load plan and its plan limits if ($planType == PlanType::ADVERTISER_PLAN) { $plan = Plan::model()->findByAttributes(array('plan_id' => $this->plan_id, 'wlabel_id' => Yii::app()->user->getWhiteLabelId(), 'type' => $planType)); } else { $plan = Plan::model()->findByAttributes(array('plan_id' => $this->plan_id, 'type' => $planType)); } if ($plan == null) { throw new Exception("Plan doesn't exist"); } $planLimits = PlanLimit::model()->findAllByAttributes(array('plan_id' => $plan->plan_id)); if ($planLimits == null) { // throw new Exception("Plan Limits don't exist"); } // create new purchased_plans record $purchasedPlan = new PurchasedPlan(); $purchasedPlan->plan_id = $plan->plan_id; if ($planType == PlanType::ADVERTISER_PLAN) { $purchasedPlan->wlabel_id = $plan->wlabel_id; } else { $purchasedPlan->wlabel_id = $this->wlabel_id; } $purchasedPlan->type = $plan->type; if ($planType == PlanType::ADVERTISER_PLAN) { if (Yii::app()->user->isAdvertiser()) { $purchasedPlan->advertiser_id = Yii::app()->user->getAdvertiserId(); } else { $purchasedPlan->advertiser_id = $advertiserId; } } else { } $purchasedPlan->method = 0; $purchasedPlan->price = $plan->price; $purchasedPlan->date_created = date("Y-m-d h:i:s"); if (!$purchasedPlan->validate()) { throw new Exception("Plan purchase cannot be saved: " . $this->putErrorsToString($purchasedPlan->getErrors())); } $purchasedPlan->save(); // create new purchased_plans payments record $purchasedPlanPayment = new PurchasedPlanPayment(); $purchasedPlanPayment->pplan_id = $purchasedPlan->pplan_id; $purchasedPlanPayment->date_paid = $purchasedPlan->date_created; $purchasedPlanPayment->date_expire = Date('y-m-d', strtotime("+{$plan->duration} day")); $purchasedPlanPayment->transaction_id = 'TEST_PAYMENT'; if (!$purchasedPlanPayment->validate()) { throw new Exception("Plan payment cannot be saved: " . $this->putErrorsToString($purchasedPlanPayment->getErrors())); } $purchasedPlanPayment->save(); } catch (Exception $e) { $transaction->rollBack(); throw new CHttpException(400, 'DB Exception: ' . $e->getMessage()); } $transaction->commit(); return false; }
/** * Displays a particular model. * @param integer $id the ID of the model to be displayed */ public function actionView($id) { $model = PurchasedPlan::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } $modelPayments = PurchasedPlanPayment::model()->findByAttributes(array('pplan_id' => $id)); if ($modelPayments === null) { throw new CHttpException(404, 'The requested page does not exist.'); } $modelLimits = PlanLimit::model()->findByAttributes(array('plan_id' => $model->plan_id)); $this->render('view', array('model' => $model, 'modelPayments' => $modelPayments, 'modelLimits' => $modelLimits)); }
public static function getPlanLimitsForResource($resourceType, $advertiserId) { if ($resourceType == '') { return array(); } $planType = PlanResourceType::getPlanType($resourceType); if ($planType == PlanType::ADVERTISER_PLAN) { if ($advertiserId == '') { $advertiserId = Yii::app()->user->getAdvertiserId(); } } // get some payment plan $sql = "SELECT plan_id, type, duration, expired FROM p_purchased_plans" . " WHERE wlabel_id=" . Yii::app()->user->getWhiteLabelId() . " AND expired=0"; if ($planType == PlanType::ADVERTISER_PLAN && $advertiserId != '') { $sql .= " AND advertiser_id=" . $advertiserId; } $sql .= " AND pplan_id in (" . " SELECT pplan_id from p_purchased_plans_payments" . " WHERE date_expire>='" . date("Y-m-d 00:00:00") . "'" . " )"; $dbCommand = Yii::app()->db->createCommand($sql); $results = $dbCommand->queryAll(); if ($results == null) { // it is still possible that the plan is paid by authorize.net and lastpayment expired, check its status $results = PlanRestriction::checkAuthorizeNetPlans($planType, $advertiserId); } if ($results == null) { return 0; } $planIds = array(); foreach ($results as $result) { $planIds[] = $result['plan_id']; } // now check for planlimits with this resource type $criteria = new CDbCriteria(); $criteria->compare('t.plan_id', $planIds); $criteria->compare('t.resource_type', $resourceType); $result = PlanLimit::model()->find($criteria); if ($result == false) { return -1; } return $result->limit_amount; }
public function verifyDelete() { if (PlanLimit::model()->findByAttributes(array('plan_id' => $this->plan_id))) { throw new CHttpException(400, 'You cannot delete plan that has some limits, delete limits first!'); } if (PurchasedPlan::model()->findByAttributes(array('plan_id' => $this->plan_id))) { throw new CHttpException(400, 'You cannot delete a plan that was already purchased!'); } return true; }
public function loadPlanLimitModel($id) { $model = PlanLimit::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
private function _makeLimit($input) { if ($input['plan_type'] == 1) { if ($input['limit_type'] == 0) { $input['data_limit'] = NULL; $input['data_unit'] = NULL; } if ($input['limit_type'] == 1) { $input['time_limit'] = NULL; $input['time_unit'] = NULL; } if (!isset($input['aq_access'])) { $input['aq_access'] = NULL; $input['aq_policy'] = NULL; } } if (isset($input['limit_id'])) { $limit = PlanLimit::find($input['limit_id']); $limit->fill($input); } else { $limit = new PlanLimit($input); } return $limit; }