}
}
}
+
+ 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]);
+ }
}
--- /dev/null
+<?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);
+ }
+}
--- /dev/null
+<?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