]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-438 Доработка эндпойнтов
authormarina <m.zozirova@gmail.com>
Tue, 22 Jul 2025 11:53:41 +0000 (14:53 +0300)
committermarina <m.zozirova@gmail.com>
Tue, 22 Jul 2025 11:53:41 +0000 (14:53 +0300)
erp24/api2/controllers/ClientController.php

index d557ef6904317c4849fa8ef75846007e682f33b0..84d76791136e0f2d10d27c7c30f8708b7f802676 100644 (file)
@@ -700,16 +700,20 @@ class ClientController extends BaseController
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
 
         $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;
 
-        if (!$clientId || !$orderId || !$phone || !$pointsToUse) {
+        if (!$orderId || !$phone || !$pointsToUse) {
             return $this->asJson(['error' => ['code' => 400, 'message' => 'Недостаточно параметров']]);
         }
 
+        $clientId = Users::findOne(['phone' => $phone])->id ?? null;
+        if (!$clientId) {
+            return $this->asJson(['error' => ['code' => 404, 'message' => 'Пользователь не найден']]);
+        }
+
         $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])
@@ -721,7 +725,7 @@ class ClientController extends BaseController
 
         $sale = Sales::find()
             ->andWhere(['number' => $orderId])
-            ->andWhere(['phone' => $clientId])
+            ->andWhere(['phone' => $phone])
             ->andWhere(['operation' => 'Продажа'])
             ->one();
 
@@ -732,6 +736,7 @@ class ClientController extends BaseController
         if (UsersBonus::find()
             ->andFilterWhere(['phone' => $phone])
             ->andFilterWhere(['user_id' => $clientId])
+            ->andFilterWhere(['phone' => $phone])
             ->andFilterWhere(['tip' => 'minus'])
             ->one()) {
             return $this->asJson([
@@ -747,6 +752,7 @@ class ClientController extends BaseController
             ->alias('ubl')
             ->leftJoin('bonus_levels bl', 'bl.alias = ubl.bonus_level')
             ->andFilterWhere(['user_id' => $clientId])
+            ->andFilterWhere(['phone' => $phone])
             ->andFilterWhere(['check_id' => $sale->id])
             ->andFilterWhere(['ubl.active' => 1])
             ->select('cashback_rate')
@@ -806,19 +812,24 @@ class ClientController extends BaseController
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
 
         $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) {
+        if (!$orderId || !$phone || !$pointsToAdd) {
             return $this->asJson(['error' => ['code' => 400, 'message' => 'Недостаточно параметров']]);
         }
 
+
+        $clientId = Users::findOne(['phone' => $phone])->id ?? null;
+        if (!$clientId) {
+            return $this->asJson(['error' => ['code' => 404, 'message' => 'Пользователь не найден']]);
+        }
+
         $sale = Sales::find()
             ->andWhere(['number' => $orderId])
-            ->andWhere(['phone' => $clientId])
+            ->andWhere(['phone' => $phone])
             ->andWhere(['operation' => 'Продажа'])
             ->one();
 
@@ -881,7 +892,7 @@ class ClientController extends BaseController
                 'code' => 200,
                 'status' => 'success',
                 'data' => [
-                    'client_id' => $clientId,
+                    'phone' => $phone,
                     'order_id' => $orderId,
                     'addedPoints' => $pointsToAdd,
                     'totalPoints' => $totalBonus
@@ -895,16 +906,15 @@ class ClientController extends BaseController
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
 
         $data = json_decode(Yii::$app->request->getRawBody(), true);
-        $clientId = $data['client_id'] ?? null;
         $phone = $data['phone'] ?? null;
 
-        if (!$clientId || !$phone) {
+        if (!$phone) {
             return $this->asJson(['error' => ['code' => 400, 'message' => 'Недостаточно параметров']]);
         }
 
+
         $user = Users::find()
             ->andFilterWhere(['phone' => $phone])
-            ->andFilterWhere(['id' => $clientId])
             ->one();
 
         if (!$user) {
@@ -921,7 +931,7 @@ class ClientController extends BaseController
             ])
             ->leftJoin('bonus_levels bl', 'bl.alias = ubl.bonus_level AND bl.active = 1')
             ->andFilterWhere(['ubl.phone' => $phone])
-            ->andFilterWhere(['ubl.user_id' => $clientId])
+            ->andFilterWhere(['ubl.user_id' => $user->id])
             ->andFilterWhere(['ubl.active' => 1])
             ->asArray()
             ->one();
@@ -938,7 +948,7 @@ class ClientController extends BaseController
 
         return $this->asJson([
             'response' => [
-                'client_id' => $clientId,
+                'phone' => $phone,
                 'bonus_level' => $bonusLevel['bonus_level'],
                 'current_points' => $bonusLevel['current_points'],
                 'next_points' => $nextLevel['next_points'] ?? null,