]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
draft. promocode migration
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 5 Jun 2024 08:36:12 +0000 (11:36 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 5 Jun 2024 08:36:12 +0000 (11:36 +0300)
erp24/api2/controllers/ClientController.php
erp24/migrations/m240605_072755_create_table_promocode.php [new file with mode: 0755]
erp24/records/Promocode.php [new file with mode: 0644]

index 0f960210eed49acff976f392757ce683568c967c..37e4d6e94d09a5a0f8b4e01db98c9114a387c394 100644 (file)
@@ -1045,4 +1045,68 @@ class ClientController extends BaseController {
             }
         }
     }
+
+    public function actionApplyPromoCode() {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+
+        $request = Yii::$app->request->getRawBody();
+
+        try {
+            $result = Json::decode($request);
+        } catch (\Exception $ex) {
+            return $this->asJson(['error' => ['code' => 400, 'message' => 'Json body invalid']]);
+        }
+
+        if (!isset($result['phone'])) {
+            return $this->asJson(["error_id" => 1, "error" => "phone is required"]);
+        }
+
+        if (!isset($result['code'])) {
+            return $this->asJson(["error_id" => 1.3, "error" => "code is required"]);
+        }
+
+        $phone = ClientHelper::phoneClear($result['phone']);
+        if (!ClientHelper::phoneVerify($phone)) {
+            return $this->asJson(["error_id" => 1.2, "error" => "phone is required"]);
+        }
+
+        if (!in_array($result['code'], ['tulpan10', 'rose8'])) {
+            return $this->asJson(["error_id" => 2, "error" => "unknown promo code"]);
+        }
+
+        $tip_sale = 'p_' . $result['code'];
+        $userBonus = UsersBonus::find()->where(['phone' => $phone, 'tip_sale' => $tip_sale])->one();
+        if ($userBonus) {
+            return $this->asJson(["error_id" => 2, "error" => "промокод уже использован"]);
+        }
+
+        $usersBonus = new UsersBonus;
+        $usersBonus->date = date('Y-m-d H:i:s');
+        $usersBonus->tip = 'plus';
+        $usersBonus->tip_sale = $tip_sale;
+        $usersBonus->phone = $phone;
+        $usersBonus->name = "Бонусы по промокоду";
+        $usersBonus->store_id = 0;
+        $usersBonus->site_id = 0;
+        $usersBonus->referal_id = 0;
+        $usersBonus->admin_id = 0;
+        $usersBonus->price = 0;
+        $usersBonus->price_skidka = 0;
+        $usersBonus->bonus = 500;
+        $usersBonus->store_id_1c = '';
+        $usersBonus->seller_id_1c = '';
+        $usersBonus->date_start = $usersBonus->date;
+        $usersBonus->date_end = date('Y-m-d H:i:s', strtotime('+366 days', strtotime($usersBonus->date_start)));
+        $usersBonus->save();
+        if ($usersBonus->getErrors()) {
+            LogService::apiErrorLog(json_encode(["error_id" => 5, "error" => $usersBonus->getErrors()], JSON_UNESCAPED_UNICODE));
+            return $this->asJson(["error_id" => 2, "error" => array_values($usersBonus->firstErrors)[0] ?? ""]);
+        }
+
+        $mess = ['ok'];
+
+        LogService::apiLogs(1, json_encode($mess, JSON_UNESCAPED_UNICODE));
+
+        return $this->asJson(['response' => $mess]);
+    }
 }
diff --git a/erp24/migrations/m240605_072755_create_table_promocode.php b/erp24/migrations/m240605_072755_create_table_promocode.php
new file mode 100755 (executable)
index 0000000..6c7329e
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240605_072755_create_table_promocode
+ */
+class m240605_072755_create_table_promocode extends Migration
+{
+    const TABLE_NAME = 'promocode';
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable(self::TABLE_NAME, [
+            'id' => $this->primaryKey(),
+            'code' => $this->string(13)->notNull()->comment("Промокод"),
+            'bonus' => $this->integer()->notNull()->comment('Количество бонусов получаемых по промокоду'),
+            'active' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('0 - не активный, 1 - активный'),
+            'date_start' => $this->timestamp()->notNull()->comment('Дата начала действия промокода'),
+            'date_end'   => $this->timestamp()->notNull()->comment('Дата окончания действия промокода'),
+            'created_by' => $this->integer()->notNull()->comment('ID создателя записи'),
+            'updated_by' => $this->integer()->null()->comment('ID редактора записи'),
+            'created_at' => $this->timestamp()->notNull()->comment('Дата создания'),
+            'updated_at' => $this->timestamp()->null()->comment('Дата изменения'),
+        ]);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropTable(self::TABLE_NAME);
+    }
+}
diff --git a/erp24/records/Promocode.php b/erp24/records/Promocode.php
new file mode 100644 (file)
index 0000000..048a899
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "promocode".
+ *
+ * @property int $id
+ * @property string $code Промокод
+ * @property int $bonus Количество бонусов получаемых по промокоду
+ * @property int $active 0 - не активный, 1 - активный
+ * @property string $date_start Дата начала действия промокода
+ * @property string $date_end Дата окончания действия промокода
+ * @property int $created_by ID создателя записи
+ * @property int|null $updated_by ID редактора записи
+ * @property string $created_at Дата создания
+ * @property string|null $updated_at Дата изменения
+ */
+class Promocode extends \yii\db\ActiveRecord
+{
+    public static function tableName() { return 'promocode'; }
+
+    public function rules() {
+        return [];
+    }
+
+    public function attributeLabels() {
+        return [
+            'id' => 'ID',
+        ];
+    }
+
+
+}
\ No newline at end of file