From 53c5cb7c660d5627be1fe7f15f3b0b26a11e904a Mon Sep 17 00:00:00 2001 From: Marina Zozirova Date: Thu, 20 Jun 2024 06:12:31 +0000 Subject: [PATCH] =?utf8?q?ERP-36=20=D0=A1=D0=BE=D0=B1=D1=80=D0=B0=D1=82?= =?utf8?q?=D1=8C=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8?= =?utf8?q?=D1=8E=20=D0=BE=D0=B1=D0=BE=20=D0=B2=D1=81=D0=B5=D1=85=20=D0=BA?= =?utf8?q?=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0=D1=85,=20=D0=BA=D0=BE=D0=BC?= =?utf8?q?=D1=83=20=D0=BD=D0=B5=20=D0=BD=D0=B0=D1=87=D0=B8=D1=81=D0=BB?= =?utf8?q?=D0=B8=D0=BB=D0=B8=D1=81=D1=8C=20=D0=B1=D0=BE=D0=BD=D1=83=D1=81?= =?utf8?q?=D1=8B=20=D0=B7=D0=B0=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=D0=B4?= =?utf8?q?=D0=BD=D0=B8=D0=B9=20=D0=BC=D0=B5=D1=81=D1=8F=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/actions/bonus/AddBonuses.php | 110 ++++++++++++++++++++++++++ erp24/controllers/BonusController.php | 1 + erp24/records/Sales.php | 1 + erp24/views/bonus/add-bonuses.php | 77 ++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 erp24/actions/bonus/AddBonuses.php create mode 100644 erp24/views/bonus/add-bonuses.php diff --git a/erp24/actions/bonus/AddBonuses.php b/erp24/actions/bonus/AddBonuses.php new file mode 100644 index 00000000..e5c56b78 --- /dev/null +++ b/erp24/actions/bonus/AddBonuses.php @@ -0,0 +1,110 @@ +request->post('action'); + + $query = Sales::find() + ->leftJoin('users_bonus us', 'us.check_id = sales.id') + ->leftJoin('users u', 'u.phone::bigint = sales.phone') + ->andWhere(['operation' => Sales::OPERATION_SALE]) + ->andWhere(['>=', 'sales.date', '2024-04-27']) + ->andWhere(['!=', 'sales.phone', '0']) + ->andWhere(['is not', 'u.id', null]) + ->andWhere(['not in', 'sales.id', ArrayHelper::getColumn(Sales::find()->andWhere(['operation' => Sales::OPERATION_RETURN])->all(), 'sales_check')]) + ->andWhere(['us.check_id' => null]); + + $query->addSelect(['sales.*', + new \yii\db\Expression('CASE WHEN ( + SELECT true + FROM sales s2 + WHERE s2.phone = sales.phone + AND s2.date < sales.date + order by s2.date asc + limit 1 + ) THEN false ELSE true END AS is_first') + ]); + + + if ($action == 'showPlus') { + + $query->addSelect(['sales.*', + new \yii\db\Expression('CASE WHEN EXISTS ( + SELECT 1 + FROM users_bonus us + WHERE us.phone::int = sales.phone + AND us.check_id = sales.id + ) THEN TRUE ELSE FALSE END AS is_success') + ]); + } + + if ($action == 'applyPlus') { + + $sales = $query->all(); + + foreach ($sales as $sale) { + if ($sale->is_first) { + $twentyPercent = new UsersBonus(); + $twentyPercent->phone = strval($sale->phone); + $twentyPercent->date = date('Y-m-d H:i:s'); + $twentyPercent->site_id = 1; + $twentyPercent->setka_id = 1; + $twentyPercent->tip = 'plus'; + $twentyPercent->tip_sale = 'sale'; + $twentyPercent->check_id = $sale->id; + $twentyPercent->price = $sale->summ; + $twentyPercent->admin_id = 1294; + $twentyPercent->store_id = $sale->store_id; + $twentyPercent->date_start = $sale->date; + $twentyPercent->name = "Возврат с покупки 20%. Чек " . $sale->number . " от " + . date("d.m.Y H:i:s", strtotime($sale->date)) . ". Сумма чека " . $sale->summ; + $sale->date = date('Y-m-d H:i:s'); + $twentyPercent->bonus = floor($sale->summ * 0.2); + $twentyPercent->date_end = date('Y-m-d H:i:s', strtotime('+90 day', strtotime($sale->date))); + $twentyPercent->save(); + } + + $userBonus = new UsersBonus(); + $userBonus->phone = strval($sale->phone); + $userBonus->name = "Возврат с покупки 10%. Чек " . $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->check_id = $sale->id; + $userBonus->price = $sale->summ; + $userBonus->admin_id = 1294; + $userBonus->store_id = $sale->store_id; + $userBonus->bonus = floor($sale->summ * 0.1); + $userBonus->date_start = $sale->date; + $userBonus->date_end = date('Y-m-d H:i:s', strtotime('+365 day', strtotime($userBonus->date))); + $userBonus->save(); + } + } + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + 'pagination' => false, + ]); + + return $this->controller->render('add-bonuses', [ + 'action' => $action, + 'dataProvider' => $dataProvider + ]); + } +} + + + diff --git a/erp24/controllers/BonusController.php b/erp24/controllers/BonusController.php index 5eb05a3b..a0c7197c 100644 --- a/erp24/controllers/BonusController.php +++ b/erp24/controllers/BonusController.php @@ -22,6 +22,7 @@ class BonusController extends \yii\web\Controller 'ajax-bonus-remove' => \yii_app\actions\bonus\AjaxBonusRemoveAction::class, 'sex' => \yii_app\actions\bonus\SexAction::class, 'add-bonus-if-written-off' => \yii_app\actions\bonus\AddBonusIfWrittenOffAction::class, + 'add-bonuses' => \yii_app\actions\bonus\AddBonuses::class, ]; } public function actionBonusUsers() { return $this->render('bonus-users'); } diff --git a/erp24/records/Sales.php b/erp24/records/Sales.php index 09956648..626d7db8 100755 --- a/erp24/records/Sales.php +++ b/erp24/records/Sales.php @@ -38,6 +38,7 @@ use yii\db\Expression; */ class Sales extends \yii\db\ActiveRecord { + public $is_first; public $sum; const OPERATION_SALE = "Продажа"; const OPERATION_RETURN = "Возврат"; diff --git a/erp24/views/bonus/add-bonuses.php b/erp24/views/bonus/add-bonuses.php new file mode 100644 index 00000000..9f5bdbaf --- /dev/null +++ b/erp24/views/bonus/add-bonuses.php @@ -0,0 +1,77 @@ + + + 'yii\grid\SerialColumn'], + + [ + 'attribute' => 'date', + 'format' => 'date', + 'label' => 'Дата', + ], + [ + 'attribute' => 'number', + 'format' => 'raw', + 'label' => 'Номер чека', + ], + [ + 'attribute' => 'phone', + 'label' => 'Пользователь', + ], + [ + 'attribute' => 'summ', + 'label' => 'Сумма', + ], + [ + 'attribute' => 'is_first', + 'label' => 'Это первая покупка?', + 'value' => function ($data) { + return $data['is_first'] ? 'Да' : 'Нет'; + } + ], + [ + 'attribute' => 'bonus_type', + 'label' => 'Сколько процентов?', + 'value' => function ($data) { + return $data['is_first'] ? '10 + 20' : '10'; + } + ], + [ + 'attribute' => 'bonus_type', + 'label' => 'Сколько начисляем?', + 'value' => function ($data) { + return $data['is_first'] ? $data->summ * 0.1 . " + " . $data->summ * 0.2 : $data->summ * 0.1; + } + ] +); + +?> + + +
+ + + + 'btn btn-success btn-lg', 'name' => 'action', 'value' => 'showPlus']) ?> + + 'btn btn-warning btn-lg', 'name' => 'action', 'value' => 'applyPlus']) ?> + + +
+
+count"); ?>
+
+         $dataProvider,
+            'columns' => $columns,
+        ]);; ?>
+        
+
-- 2.39.5