Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player subscription cancellation #940

Merged
merged 12 commits into from
Mar 23, 2023
10 changes: 10 additions & 0 deletions backend/modules/sales/commands/CronController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ public function actionStripeImportProducts()
echo "Importing Products\n";
Product::FetchStripe();
}

/**
* Delete Expired subscriptions
*/
public function actionDeleteInactive()
{
echo "Deleting expired subscriptions\n";
PlayerSubscription::DeleteInactive();
}

}
259 changes: 136 additions & 123 deletions backend/modules/sales/controllers/PlayerSubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use app\modules\sales\models\Product;
use app\modules\frontend\models\Player;
use app\modules\gameplay\models\NetworkPlayer;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;

/**
Expand All @@ -21,137 +19,152 @@ class PlayerSubscriptionController extends \app\components\BaseController
/**
* {@inheritdoc}
*/
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(),[]);
}

/**
* Lists all PlayerSubscription models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PlayerSubscriptionSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), []);
}

/**
* Displays a single PlayerSubscription model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Lists all PlayerSubscription models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PlayerSubscriptionSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

/**
* Creates a new PlayerSubscription model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new PlayerSubscription();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->player_id]);
}

return $this->render('create', [
'model' => $model,
]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

/**
* Displays a single PlayerSubscription model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Creates a new PlayerSubscription model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new PlayerSubscription();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->player_id]);
}

/**
* Updates an existing PlayerSubscription model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
if($model->active==0)
{
$network_ids=ArrayHelper::getColumn($model->price->product->productNetworks,'network_id');
NetworkPlayer::deleteAll([
'AND', 'player_id = :player_id', [
'IN', 'network_id',
$network_ids
]
], [
':player_id' => $model->player_id
]);
}

return $this->redirect(['view', 'id' => $model->player_id]);
}

return $this->render('update', [
'model' => $model,
return $this->render('create', [
'model' => $model,
]);
}

/**
* Updates an existing PlayerSubscription model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->active == 0) {
$network_ids = ArrayHelper::getColumn($model->price->product->productNetworks, 'network_id');
NetworkPlayer::deleteAll([
'AND', 'player_id = :player_id', [
'IN', 'network_id',
$network_ids
]
], [
':player_id' => $model->player_id
]);
}

return $this->redirect(['view', 'id' => $model->player_id]);
}

/**
* Deletes an existing PlayerSubscription model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
return $this->render('update', [
'model' => $model,
]);
}

/**
* Deletes an existing PlayerSubscription model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Deletes all expired PlayerSubscription models.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDeleteInactive()
{
try {
PlayerSubscription::DeleteInactive();
Yii::$app->session->setFlash('success', 'Inactive subscriptions deleted.');
} catch (\Exception $e) {
Yii::$app->session->setFlash('error', 'Failed to delete inactive subscriptions.[<code>' . \yii\helpers\Html::encode($e->getMessage()) . '</code>]');
}
return $this->redirect(['index']);
}

/**
* Gets all Product from Stripe and merges with existing ones (if any).
* @return mixed
*/
public function actionFetchStripe()
{
if(intval(Product::find()->count())<1)
{
\Yii::$app->session->addFlash('warning','There are no products on the system. First fetch the stripe products and then import the subscriptions.');
return $this->redirect(['/sales/product/index']);
}
if(intval(Player::find()->where(['IS NOT', 'stripe_customer_id', null])->count())<1)
{
\Yii::$app->session->addFlash('warning','There are no customers on the system. First fetch the stripe customers and then import the subscriptions.');
return $this->redirect(['/sales/player-customer/index']);
}
PlayerSubscription::FetchStripe();
return $this->redirect(['index']);
/**
* Gets all Product from Stripe and merges with existing ones (if any).
* @return mixed
*/
public function actionFetchStripe()
{
if (intval(Product::find()->count()) < 1) {
\Yii::$app->session->addFlash('warning', 'There are no products on the system. First fetch the stripe products and then import the subscriptions.');
return $this->redirect(['/sales/product/index']);
}
if (intval(Player::find()->where(['IS NOT', 'stripe_customer_id', null])->count()) < 1) {
\Yii::$app->session->addFlash('warning', 'There are no customers on the system. First fetch the stripe customers and then import the subscriptions.');
return $this->redirect(['/sales/player-customer/index']);
}
PlayerSubscription::FetchStripe();
return $this->redirect(['index']);
}

/**
* Finds the PlayerSubscription model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return PlayerSubscription the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = PlayerSubscription::findOne($id)) !== null) {
return $model;
}

throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
/**
* Finds the PlayerSubscription model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return PlayerSubscription the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = PlayerSubscription::findOne($id)) !== null) {
return $model;
}

throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
33 changes: 31 additions & 2 deletions backend/modules/sales/models/PlayerSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ public static function find()
return new PlayerSubscriptionQuery(get_called_class());
}

/**
* Deletes inactive subscriptions
* @return int — the number of rows deleted
* @throws NotSupportedException — if not overridden.
*/
public static function DeleteInactive()
{
foreach (PlayerSubscription::find()->active(0)->all() as $sub) {
if ($sub->product)
$metadata = json_decode($sub->product->metadata);
if (isset($metadata->network_ids)) {
NetworkPlayer::deleteAll([
'and',
['player_id' => $sub->player_id],
['in', 'network_id', explode(',', $metadata->network_ids)]
]);
}
$sub->delete();
}
}

/**
* Gets all Product from Stripe and merges with existing ones (if any).
* @return mixed
Expand Down Expand Up @@ -156,9 +177,9 @@ public static function FetchStripe()

public function afterSave($insert, $changedAttributes)
{
if ($this->product)
$metadata = json_decode($this->product->metadata);
if ($this->active == 1) {
if ($this->product)
$metadata = json_decode($this->product->metadata);
if (isset($metadata->spins) && intval($metadata->spins) > 0) {
$this->player->playerSpin->updateAttributes(['perday' => intval($metadata->spins), 'counter' => 0]);
} else {
Expand Down Expand Up @@ -187,6 +208,14 @@ public function afterSave($insert, $changedAttributes)
}
}
}
} else {
if (isset($metadata->network_ids)) {
foreach (explode(',', $metadata->network_ids) as $val) {
if (($np = NetworkPlayer::findOne(['network_id' => $val, 'player_id' => $this->player_id])) !== null) {
$np->delete();
}
}
}
}

return parent::afterSave($insert, $changedAttributes);
Expand Down
5 changes: 5 additions & 0 deletions backend/modules/sales/models/PlayerSubscriptionQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class PlayerSubscriptionQuery extends \yii\db\ActiveQuery
{
public function vip()
{
return $this->andWhere("[[subscription_id]]='sub_vip'");
}

public function active($active=1)
{
return $this->andWhere("[[active]]=".intval($active));
Expand Down
2 changes: 1 addition & 1 deletion backend/modules/sales/views/default/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'icon' => 'fab fa-cc-stripe',
'color' => 'primary',
'modelClass' => "app\modules\sales\models\PlayerSubscription",
'total' => PlayerSubscription::find()->count() . ' ' . '<small class="text-muted"><abbr title="Active">' . PlayerSubscription::find()->active()->count() . '</abbr></small>',
'total' => PlayerSubscription::find()->count() . ' ' . '<small class="text-muted"><abbr title="Active">' . PlayerSubscription::find()->active()->count() . '</abbr>/</small><sub class="text-muted fs-6">'.PlayerSubscription::find()->vip()->active()->count().' VIP</sub>',
'title' => Html::a('Subscriptions', ['/sales/player-subscription/index'])
]); ?>
</div>
Expand Down
7 changes: 7 additions & 0 deletions backend/modules/sales/views/player-subscription/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
<p>
<?= Html::a(Yii::t('app', 'Create Player Subscription'), ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Fetch from Stripe'), ['fetch-stripe'], ['class' => 'btn btn-warning']) ?>
<?= Html::a(Yii::t('app', 'Delete Inactive'), ['delete-inactive'], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete the inactive subscriptions? The subscription networks will also be removed.'),
'method' => 'post',
],
]) ?>
</p>

<?php Pjax::begin(); ?>
Expand Down
5 changes: 5 additions & 0 deletions docs/console-commands/Sales.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Usage: `./backend/yii sales/import-stripe`
Import Stripe products and their prices.

Usage: `./backend/yii sales/stripe-import-products`

## Delete Inactive
Delete inactive subscriptions

Usage: `./backend/yii sales/delete-inactive`
Loading