'pages' => ['totalCount' => (int)$pages->totalCount, 'page' => $pages->page, 'per-page' => $pages->pageSize]
]]);
}
-
public function actionUseBonuses()
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- $clientId = Yii::$app->request->getQueryParam('client_id');
- $orderId = Yii::$app->request->getQueryParam('order_id');
- $clientPhone = Yii::$app->request->getQueryParam('client_phone');
- $pointsToAdd = Yii::$app->request->getQueryParam('points_to_add');
- $comment = Yii::$app->request->getQueryParam('comment');
+ $data = json_decode(Yii::$app->request->getRawBody(), true);
+ $clientId = $data['client_id'] ?? null;
+ $orderId = $data['order_id'] ?? null;
+ $phone = $data['phone'] ?? null;
+ $pointsToUse = $data['points_to_use'] ?? null;
+ $comment = $data['comment'] ?? null;
- $sale = Sales::findOne(['number' => $orderId]);
+ if (!$clientId || !$orderId || !$phone || !$pointsToUse) {
+ return $this->asJson(['error' => ['code' => 400, 'message' => 'Недостаточно параметров']]);
+ }
+ $sale = Sales::findOne(['number' => $orderId]);
if (!$sale) {
return $this->asJson(['error' => ['code' => 404, 'message' => 'Продажа не найдена']]);
}
- $bonusAmount = $pointsToAdd !== null ? (int)$pointsToAdd : floor($sale->summ * 0.1);
+ if (UsersBonus::findOne(['user_id' => $clientId, 'check_id' => $sale->id, 'phone' => $phone, 'tip' => 'minus'])) {
+ return $this->asJson([
+ 'response' => [
+ 'code' => 200,
+ 'status' => 'success',
+ 'data' => 'Бонусы за этот заказ уже списаны'
+ ]
+ ]);
+ }
$userBonus = new UsersBonus();
- $userBonus->phone = strval($sale->phone);
- $userBonus->name = $comment ?: ("Возврат с покупки 10%. Чек " . $sale->number . " от " .
- date("d.m.Y H:i:s", strtotime($sale->date)) . ". Сумма чека " . $sale->summ);
+ $userBonus->phone = $phone;
+ $userBonus->name = $comment ?? sprintf(
+ 'Возврат с покупки 10%%. Чек %s от %s. Сумма чека %s',
+ $sale->number,
+ date('d.m.Y H:i:s', strtotime($sale->date)),
+ $sale->summ
+ );
$userBonus->date = date('Y-m-d H:i:s');
$userBonus->site_id = 1;
$userBonus->setka_id = 1;
- $userBonus->tip = 'plus';
+ $userBonus->tip = 'minus';
$userBonus->tip_sale = 'sale';
+ $userBonus->user_id = $clientId;
$userBonus->check_id = $sale->id;
$userBonus->price = $sale->summ;
- $userBonus->admin_id = Yii::$app->user->id ?? null;
$userBonus->store_id = $sale->store_id;
- $userBonus->bonus = $bonusAmount;
+ $userBonus->bonus = $pointsToUse;
$userBonus->date_start = $sale->date;
- $userBonus->date_end = date('Y-m-d H:i:s', strtotime('+365 day', strtotime($userBonus->date)));
+ $userBonus->date_end = date('Y-m-d H:i:s', strtotime('+365 day', strtotime($sale->date)));
try {
if (!$userBonus->save()) {
return $this->asJson(['error' => ['code' => 500, 'message' => 'Ошибка при сохранении бонуса: ' . $ex->getMessage()]]);
}
- $totalBonus = UsersBonus::find()
- ->select([
- new \yii\db\Expression("SUM(CASE WHEN tip = 'plus' THEN bonus ELSE 0 END) - SUM(CASE WHEN tip = 'minus' THEN bonus ELSE 0 END) AS total")
- ])
- ->where(['phone' => $clientPhone])
+ $totalBonus = (int)UsersBonus::find()
+ ->select([new \yii\db\Expression("SUM(CASE WHEN tip = 'plus' THEN bonus ELSE 0 END) - SUM(CASE WHEN tip = 'minus' THEN bonus ELSE 0 END)")])
+ ->where(['phone' => $phone])
->scalar();
return $this->asJson([
'data' => [
'client_id' => $clientId,
'order_id' => $orderId,
- 'addedPoints' => $bonusAmount,
- 'totalPoints' => (int)$totalBonus,
+ 'addedPoints' => $pointsToUse,
+ 'remainingPoints' => $totalBonus
]
]
]);
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- $clientId = Yii::$app->request->getQueryParam('client_id');
- $orderId = Yii::$app->request->getQueryParam('order_id');
- $clientPhone = Yii::$app->request->getQueryParam('client_phone');
- $pointsToAdd = Yii::$app->request->getQueryParam('points_to_add');
- $comment = Yii::$app->request->getQueryParam('comment');
+ $data = json_decode(Yii::$app->request->getRawBody(), true);
+ $clientId = $data['client_id'] ?? null;
+ $orderId = $data['order_id'] ?? null;
+ $phone = $data['phone'] ?? null;
+ $pointsToAdd = $data['points_to_add'] ?? null;
+ $comment = $data['comment'] ?? null;
+
+ if (!$clientId || !$orderId || !$phone || !$pointsToAdd) {
+ return $this->asJson(['error' => ['code' => 400, 'message' => 'Недостаточно параметров']]);
+ }
$sale = Sales::findOne(['number' => $orderId]);
-
if (!$sale) {
return $this->asJson(['error' => ['code' => 404, 'message' => 'Продажа не найдена']]);
}
- $bonusAmount = $pointsToAdd !== null ? (int)$pointsToAdd : floor($sale->summ * 0.1);
-
$userBonus = new UsersBonus();
- $userBonus->phone = strval($sale->phone);
- $userBonus->name = $comment ?: ("Возврат с покупки 10%. Чек " . $sale->number . " от " .
- date("d.m.Y H:i:s", strtotime($sale->date)) . ". Сумма чека " . $sale->summ);
+ $userBonus->phone = $phone;
+ $userBonus->name = $comment ?? sprintf(
+ 'Возврат с покупки 10%%. Чек %s от %s. Сумма чека %s',
+ $sale->number,
+ date('d.m.Y H:i:s', strtotime($sale->date)),
+ $sale->summ
+ );
$userBonus->date = date('Y-m-d H:i:s');
$userBonus->site_id = 1;
$userBonus->setka_id = 1;
$userBonus->tip = 'plus';
$userBonus->tip_sale = 'sale';
+ $userBonus->user_id = $clientId;
$userBonus->check_id = $sale->id;
$userBonus->price = $sale->summ;
- $userBonus->admin_id = Yii::$app->user->id ?? null;
$userBonus->store_id = $sale->store_id;
- $userBonus->bonus = $bonusAmount;
+ $userBonus->bonus = $pointsToAdd;
$userBonus->date_start = $sale->date;
- $userBonus->date_end = date('Y-m-d H:i:s', strtotime('+365 day', strtotime($userBonus->date)));
+ $userBonus->date_end = date('Y-m-d H:i:s', strtotime('+365 day', strtotime($sale->date)));
try {
if (!$userBonus->save()) {
return $this->asJson(['error' => ['code' => 500, 'message' => 'Ошибка при сохранении бонуса: ' . $ex->getMessage()]]);
}
- $totalBonus = UsersBonus::find()
- ->select([
- new \yii\db\Expression("SUM(CASE WHEN tip = 'plus' THEN bonus ELSE 0 END) - SUM(CASE WHEN tip = 'minus' THEN bonus ELSE 0 END) AS total")
- ])
- ->where(['phone' => $clientPhone])
+ $totalBonus = (int)UsersBonus::find()
+ ->select([new \yii\db\Expression("SUM(CASE WHEN tip = 'plus' THEN bonus ELSE 0 END) - SUM(CASE WHEN tip = 'minus' THEN bonus ELSE 0 END)")])
+ ->where(['phone' => $phone])
->scalar();
return $this->asJson([
'data' => [
'client_id' => $clientId,
'order_id' => $orderId,
- 'addedPoints' => $bonusAmount,
- 'totalPoints' => (int)$totalBonus,
+ 'addedPoints' => $pointsToAdd,
+ 'totalPoints' => $totalBonus
]
]
]);
$clientId = $data['client_id'] ?? null;
$phone = $data['phone'] ?? null;
- if (empty($clientId) || empty($phone)) {
- return $this->asJson(['error' => ['code' => 400, 'message' => 'Uncorrect params']]);
+ if (!$clientId || !$phone) {
+ return $this->asJson(['error' => ['code' => 400, 'message' => 'Недостаточно параметров']]);
}
$user = UsersBonus::find()
- ->andFilterWhere(['id' => $clientId])
- ->andFilterWhere(['phone' => $phone])
+ ->where(['id' => $clientId, 'phone' => $phone])
->one();
if (!$user) {
- return $this->asJson(['error' => ['code' => 404, 'message' => 'User not found']]);
+ return $this->asJson(['error' => ['code' => 404, 'message' => 'Пользователь не найден']]);
}
$bonusLevel = UsersBonusLevels::find()
->select([
'bl.name as bonus_level',
'bl.bonus_rate as discount_percent',
- 'bl.current_points',
+ 'bl.threshold as current_points',
'bl.id as level_id'
])
->leftJoin('bonus_levels bl', 'bl.alias = ubl.bonus_level AND bl.active = 1')
->one();
if (!$bonusLevel) {
- return $this->asJson(['error' => ['code' => 404, 'message' => 'Bonus level not found']]);
+ return $this->asJson(['error' => ['code' => 404, 'message' => 'Уровень бонусов не найден']]);
}
$nextLevel = BonusLevels::find()
- ->select(['current_points as next_points'])
+ ->select(['threshold as next_points'])
->where(['id' => $bonusLevel['level_id'] + 1, 'active' => 1])
->asArray()
->one();
- $data = [
- 'client_id' => $clientId,
- 'bonus_level' => $bonusLevel['bonus_level'],
- 'current_points' => $bonusLevel['current_points'],
- 'next_points' => $nextLevel['next_points'] ?? null,
- 'discount_percent' => $bonusLevel['discount_percent'],
- ];
-
- return $this->asJson(['response' => $data]);
+ return $this->asJson([
+ 'response' => [
+ 'client_id' => $clientId,
+ 'bonus_level' => $bonusLevel['bonus_level'],
+ 'current_points' => $bonusLevel['current_points'],
+ 'next_points' => $nextLevel['next_points'] ?? null,
+ 'discount_percent' => $bonusLevel['discount_percent']
+ ]
+ ]);
}
public function actionMemorableDates()