From: Alexander Smirnov Date: Mon, 10 Jun 2024 08:16:42 +0000 (+0300) Subject: fix promocode usage messages X-Git-Tag: 1.2^2~2^2~5 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=8ffb38431683ee754493da732e371cea5a559df9;p=erp24_rep%2Fyii-erp24%2F.git fix promocode usage messages --- diff --git a/erp24/api2/controllers/ClientController.php b/erp24/api2/controllers/ClientController.php index c8067f95..1d9ae8e9 100644 --- a/erp24/api2/controllers/ClientController.php +++ b/erp24/api2/controllers/ClientController.php @@ -6,6 +6,7 @@ use GuzzleHttp\Client; use Yii; use yii\data\Pagination; +use yii\db\Expression; use yii\helpers\Json; use yii_app\helpers\ClientHelper; use yii_app\helpers\UtilHelper; @@ -1071,16 +1072,21 @@ class ClientController extends BaseController { return $this->asJson(["error_id" => 1.2, "error" => "phone is required"]); } - $promoCode = Promocode::find()->where(['code' => $result['code'], 'active' => '1'])->one(); + $promoCode = Promocode::find()->where(['code' => $result['code'], 'active' => '1', 'base' => [Promocode::BASE_SHARED, Promocode::BASE_SINGLE_USE]]) + ->andWhere(['<=', new Expression("TO_CHAR(date_start, 'YYYY-MM-DD')"), date('Y-m-d')]) + ->andWhere(['>=', new Expression("TO_CHAR(date_end, 'YYYY-MM-DD')"), date('Y-m-d')]) + ->one(); /** @var $promoCode Promocode */ - if (!$promoCode) { - return $this->asJson(["error_id" => 2, "error" => "промокод не известен"]); - } $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" => "промокод уже использован"]); + return $this->asJson(["error_id" => 3, "error" => "промокод уже использован"]); + } + + if (!$promoCode) { + $promoCodeWeak = Promocode::find()->where(['code' => $result['code'], 'base' => [Promocode::BASE_SHARED, Promocode::BASE_SINGLE_USE]])->one(); + return $this->asJson(["error_id" => 2, "error" => $promoCodeWeak ? "истёк срок действия промокода" : "промокод не известен"]); } $usersBonus = new UsersBonus; @@ -1088,7 +1094,7 @@ class ClientController extends BaseController { $usersBonus->tip = 'plus'; $usersBonus->tip_sale = $tip_sale; $usersBonus->phone = $phone; - $usersBonus->name = "Бонусы по промокоду"; + $usersBonus->name = "Бонусы по промокоду " . $promoCode->code; $usersBonus->store_id = 0; $usersBonus->site_id = 0; $usersBonus->referal_id = 0; @@ -1103,7 +1109,16 @@ class ClientController extends BaseController { $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] ?? ""]); + return $this->asJson(["error_id" => 5, "error" => array_values($usersBonus->firstErrors)[0] ?? ""]); + } + + if ($promoCode->base == Promocode::BASE_SINGLE_USE) { + $promoCode->active = Promocode::ACTIVE_OFF; + $promoCode->save(); + if ($promoCode->getErrors()) { + LogService::apiErrorLog(json_encode(["error_id" => 6, "error" => $promoCode->getErrors()], JSON_UNESCAPED_UNICODE)); + return $this->asJson(["error_id" => 6, "error" => array_values($promoCode->firstErrors)[0] ?? ""]); + } } $mess = ['ok']; diff --git a/erp24/controllers/PromocodeController.php b/erp24/controllers/PromocodeController.php index 02688308..91fb9168 100644 --- a/erp24/controllers/PromocodeController.php +++ b/erp24/controllers/PromocodeController.php @@ -23,7 +23,8 @@ class PromocodeController extends Controller $model->date_end = date("Y-m-d 00:00:00", strtotime("+366 day", time())); } else { $model = Promocode::findOne($id); - $dataProvider = new ActiveDataProvider(['query' => Promocode::find()->where(['base' => Promocode::BASE_SINGLE_USE, 'parent_id' => $id])]); + $dataProvider = new ActiveDataProvider(['query' => Promocode::find()->where(['base' => Promocode::BASE_SINGLE_USE, 'parent_id' => $id]) + ->orderBy(['created_at' => SORT_DESC, 'code' => SORT_ASC])]); } /** @var $model Promocode */ if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {