function promo_code() { $promo_code = $_POST['code']; $user_id = Auth::getUser()['id']; $date_now = date('Y-m-d H-i-s'); //echo 'promo code = '.$promo_code; $code = ReferalCodeModel::model()->where(" `code` = '" . $promo_code . "' AND `active_to_use` = 1")->findRow(); //var_dump($code); if (empty($code)) { echo 'Неверный код!'; } else { if ($code->code_type == "1") { if ($code->used_count < $code->activate_count) { //echo 'used_count bigger'; if ($code->can_activate > $date_now) { //echo '|| date good'; $Date = date('Y-m-d'); $new_date = date('Y-m-d', strtotime($Date . $code->active_to)); //echo ' || new_date = '.$new_date; $users = new UsersModel(); $users->id = $user_id; $users->promo_code = $code->code; $users->package_expire = $new_date; $users->package_id = 5; $users->save(); $model = new ReferalCodeModel(); $model->id = $code->id; $model->used_count = $code->used_count + 1; $model->save(); echo 'Промо-код засчитан!'; } else { echo 'Срок годности кода истек!'; } } else { echo 'Этот код был использован максимальное количество раз!'; } } else { if ($code->code_type == "2") { $Date = date('Y-m-d'); $new_date = date('Y-m-d', strtotime($Date . $code->active_to)); //echo ' || new_date = '.$new_date; $users = new UsersModel(); $users->id = $user_id; $users->promo_code = $code->code; $users->package_id = 5; $users->package_expire = $new_date; $users->save(); $model = new ReferalCodeModel(); $model->id = $code->id; $model->used_count = $code->used_count + 1; $model->save(); echo 'Промо-код засчитан!'; } } } }
public function actionDeactivateCode($id) { $model = new ReferalCodeModel(); $model->id = $id; $model->active_to_use = "0"; $model->save(); $this->redirect("/admin/generate_code"); }