示例#1
0
 public function actionAddPaid($id = 0, $withDate = 0)
 {
     $model = new AddToUserForm();
     $tariffs = TariffPlans::getAllTariffPlans(true, true);
     $tariffsArray = CHtml::listData($tariffs, 'id', 'name');
     $request = Yii::app()->request;
     $data = $request->getPost('AddToUserForm');
     if ($data) {
         $userId = $request->getPost('user_id');
         $withDate = $request->getPost('withDate');
         $model->attributes = $data;
         if ($model->validate()) {
             $user = User::model()->findByPk($userId);
             $tariff = TariffPlans::getFullTariffInfoById($model->tariff_id);
             if (!$tariff || !$user) {
                 throw new CException('Not valid data');
             }
             if (TariffPlans::applyToUser($userId, $tariff['id'], $model->date_end, null, true)) {
                 echo CJSON::encode(array('status' => 'ok', 'userId' => $userId, 'html' => TariffPlans::getTariffPlansHtml($withDate, true, $user)));
                 Yii::app()->end();
             }
         } else {
             echo CJSON::encode(array('status' => 'err', 'html' => $this->renderPartial('_add_to_user', array('id' => $userId, 'model' => $model, 'withDate' => $withDate, 'tariffsArray' => $tariffsArray), true)));
             Yii::app()->end();
         }
     }
     $renderData = array('id' => $id, 'model' => $model, 'withDate' => $withDate, 'tariffsArray' => $tariffsArray);
     if (Yii::app()->request->isAjaxRequest) {
         $this->renderPartial('_add_to_user', $renderData);
     } else {
         $this->render('_add_to_user', $renderData);
     }
 }
示例#2
0
 public function actionBuyTariffPlan()
 {
     $this->layout = '//layouts/usercpanel';
     $user = HUser::getModel();
     $tariffId = Yii::app()->request->getParam('tariffid');
     if (!$user || !$tariffId) {
         throw404();
     }
     $currentTariffModel = TariffPlans::model()->findByPk($tariffId);
     if (!$currentTariffModel || $currentTariffModel->active != TariffPlans::STATUS_ACTIVE) {
         throw404();
     }
     // check current user tariff plan
     $currentTariffPlanInfo = TariffPlans::getTariffInfoByUserId($user->id);
     if ($currentTariffPlanInfo['issetTariff'] && $currentTariffPlanInfo['tariffDuration']) {
         if (!$currentTariffPlanInfo['activeTariff']) {
             Yii::app()->user->setFlash('error', Yii::t("module_tariffPlans", "You can only extend the tariff plan {name}", array("{name}" => $currentTariffPlanInfo['tariffName'])));
             $this->redirect(array('choosetariffplans'));
             Yii::app()->end();
         }
     }
     // check balance
     if ($currentTariffModel->price) {
         # платный тариф
         if ($currentTariffModel->price > $user->balance) {
             Yii::app()->user->setFlash('error', tt('On your balance is not enough money to buy the chosen tariff plan', 'tariffPlans'));
             $this->redirect(array('choosetariffplans'));
             Yii::app()->end();
         }
     }
     // check object count
     if ($currentTariffModel->limit_objects) {
         $usersObjects = TariffPlans::getCountUserObjects($user->id);
         if ($usersObjects > $currentTariffModel->limit_objects) {
             Yii::app()->user->setFlash('error', tt('The number of added ads exceeds the limit of the tariff. Remove its not relevant your ads and try again.', 'tariffPlans'));
             $this->redirect(array('choosetariffplans'));
             Yii::app()->end();
         }
     }
     // apply action
     $interval = 'INTERVAL ' . $currentTariffModel->duration . ' DAY';
     $dateEnd = new CDbExpression('NOW() + ' . $interval);
     if (TariffPlans::applyToUser($user->id, $tariffId, $dateEnd, $interval)) {
         if ($currentTariffModel->price) {
             # платный тариф
             $user->deductBalance($currentTariffModel->price);
         }
         Yii::app()->user->setFlash('success', tt('Tariff plan has been successfully applied', 'tariffPlans'));
         $this->redirect(array('tariffhistory'));
     } else {
         Yii::app()->user->setFlash('error', tc('Error. Repeat attempt later'));
         $this->redirect(array('choosetariffplans'));
         Yii::app()->end();
     }
 }
示例#3
0
 public function complete()
 {
     if ($this->tariff_id) {
         # оплата за тарифный план
         $tariffInfo = TariffPlans::getFullTariffInfoById($this->tariff_id);
         if ($tariffInfo['duration']) {
             $interval = 'INTERVAL ' . $tariffInfo["duration"] . ' DAY';
         } else {
             $interval = 'INTERVAL 1460 DAY';
         }
         $dateEnd = new CDbExpression('NOW() + ' . $interval);
         TariffPlans::applyToUser($this->user_id, $this->tariff_id, $dateEnd, $interval);
     } else {
         if ($this->paid_id != PaidServices::ID_ADD_FUNDS) {
             $paidOption = $this->paidOption;
             $interval = 'INTERVAL ' . $paidOption->duration_days . ' DAY';
             $dateEnd = new CDbExpression('NOW() + ' . $interval);
             PaidServices::applyToApartment($this->apartment_id, $this->paid_id, $dateEnd, $interval);
         } else {
             $user = User::model()->findByPk($this->user_id);
             if (!$user) {
                 throw new CHttpException('Not user with ID ' . $this->user_id);
             }
             $user->addToBalance($this->amount);
         }
     }
     $this->status = Payments::STATUS_PAYMENTCOMPLETE;
     $this->update('status');
     return true;
 }