]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-222] интерфейс и апи для подписки на отчёт
authorAlexander Smirnov <fredeom@mail.ru>
Fri, 11 Oct 2024 09:43:54 +0000 (12:43 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Fri, 11 Oct 2024 09:43:54 +0000 (12:43 +0300)
erp24/actions/weeklyReport/SubscriptionAction.php [new file with mode: 0644]
erp24/api3/modules/v1/controllers/WeeklyReportController.php [new file with mode: 0644]
erp24/controllers/WeeklyReportController.php [new file with mode: 0644]
erp24/migrations/m241010_150710_create_table_weekly_report_subscribtion.php [new file with mode: 0755]
erp24/records/WeeklyReportSubscription.php [new file with mode: 0644]
erp24/views/weekly-report/subscription.php [new file with mode: 0644]
erp24/web/js/weekly-report/subscription.js [new file with mode: 0644]

diff --git a/erp24/actions/weeklyReport/SubscriptionAction.php b/erp24/actions/weeklyReport/SubscriptionAction.php
new file mode 100644 (file)
index 0000000..a960a50
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+namespace yii_app\actions\weeklyReport;
+
+use Yii;
+use yii\base\Action;
+use yii\helpers\Json;
+use yii_app\records\WeeklyReportSubscription;
+
+class SubscriptionAction extends Action
+{
+    public function run() {
+
+        if (Yii::$app->request->isPost) {
+            $action = Yii::$app->request->post('action');
+            if ($action == "addNew") {
+                $name = Yii::$app->request->post('name');
+                $chatId = Yii::$app->request->post('chat_id');
+
+                $sub = new WeeklyReportSubscription;
+                $sub->name = $name;
+                $sub->chat_id = $chatId;
+                $sub->active = 0;
+
+                $sub->save();
+                if ($sub->getErrors()) {
+                    throw new \Exception(Json::encode($sub->getErrors()));
+                }
+                return $this->controller->redirect('subscription');
+            }
+            if ($action == "changeSub") {
+                $subId = Yii::$app->request->post('id');
+                $name = Yii::$app->request->post('name');
+                $chatId = Yii::$app->request->post('chatId');
+                $active = Yii::$app->request->post('active');
+
+                $sub = WeeklyReportSubscription::findOne($subId);
+                $sub->name = $name;
+                $sub->chat_id = $chatId;
+                $sub->active = $active;
+                $sub->save();
+                if ($sub->getErrors()) {
+                    throw new \Exception(Json::encode($sub->getErrors()));
+                }
+
+                return 'ok';
+            }
+        }
+
+        $subscriptions = WeeklyReportSubscription::find()->orderBy(['id' => SORT_DESC])->all();
+
+        return $this->controller->render('subscription', compact('subscriptions'));
+    }
+}
\ No newline at end of file
diff --git a/erp24/api3/modules/v1/controllers/WeeklyReportController.php b/erp24/api3/modules/v1/controllers/WeeklyReportController.php
new file mode 100644 (file)
index 0000000..d114d78
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace yii_app\api3\modules\v1\controllers;
+
+use yii_app\records\WeeklyReportSubscription;
+
+class WeeklyReportController extends \yii_app\api3\controllers\NoActiveController
+{
+    public function actionSubscription() {
+        return WeeklyReportSubscription::find()->where(['active' => 1])->all();
+    }
+}
\ No newline at end of file
diff --git a/erp24/controllers/WeeklyReportController.php b/erp24/controllers/WeeklyReportController.php
new file mode 100644 (file)
index 0000000..377ee09
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+namespace app\controllers;
+
+use yii\web\Controller;
+
+class WeeklyReportController extends Controller
+{
+    public function actions()
+    {
+        return [
+            'subscription' => \yii_app\actions\weeklyReport\SubscriptionAction::class,
+        ];
+    }
+}
\ No newline at end of file
diff --git a/erp24/migrations/m241010_150710_create_table_weekly_report_subscribtion.php b/erp24/migrations/m241010_150710_create_table_weekly_report_subscribtion.php
new file mode 100755 (executable)
index 0000000..eadf901
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m241010_150710_create_table_weekly_report_subscribtion
+ */
+class m241010_150710_create_table_weekly_report_subscribtion extends Migration
+{
+    const TABLE_NAME = 'erp24.weekly_report_subscription';
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable(self::TABLE_NAME, [
+            'id' => $this->primaryKey(),
+            'name' => $this->string(36)->notNull()->comment('Имя пользователя'),
+            'chat_id' => $this->integer()->notNull()->comment('chat_id пользователя'),
+            'active' => $this->tinyInteger()->notNull()->defaultValue(0)->comment('0 - не активен, 1 - активен')
+        ]);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropTable(self::TABLE_NAME);
+    }
+}
diff --git a/erp24/records/WeeklyReportSubscription.php b/erp24/records/WeeklyReportSubscription.php
new file mode 100644 (file)
index 0000000..a4522d0
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "weekly_report_subscription".
+ *
+ * @property int $id
+ * @property string $name Имя пользователя
+ * @property int $chat_id chat_id пользователя
+ * @property int $active 0 - не активен, 1 - активен
+ */
+class WeeklyReportSubscription extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'weekly_report_subscription';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['name', 'chat_id'], 'required'],
+            [['chat_id', 'active'], 'default', 'value' => null],
+            [['chat_id', 'active'], 'integer'],
+            [['name'], 'string', 'max' => 36],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'name' => 'Name',
+            'chat_id' => 'Chat ID',
+            'active' => 'Active',
+        ];
+    }
+}
diff --git a/erp24/views/weekly-report/subscription.php b/erp24/views/weekly-report/subscription.php
new file mode 100644 (file)
index 0000000..64cc061
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+use yii_app\records\WeeklyReportSubscription;
+
+/** @var $subscriptions []WeeklyReportSubscription */
+
+$this->registerJsFile('/js/weekly-report/subscription.js', ['position' => \yii\web\View::POS_END]);
+
+?>
+
+<div class="weeklyReportSubscription m-5">
+
+    <?php ActiveForm::begin() ?>
+
+    <div class="row">
+        <div class="col-3">
+            Имя: <?= Html::textInput('name') ?>
+            chat_id: <?= Html::textInput('chat_id', null, ['type' => 'number'])?>
+        </div>
+        <div class="col-1">
+            <?= Html::submitButton('Добавить', ['class' => 'btn btn-success btn-sm', 'name' => 'action', 'value' => 'addNew'])?>
+        </div>
+    </div>
+
+    <?php ActiveForm::end() ?>
+
+    <div class="row mt-2">
+        <div class="col-4">
+            <table id="subscriptionTable">
+                <thead>
+                    <tr>
+                        <th>Имя</th>
+                        <th>chat_id</th>
+                        <th>Активен</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <?php foreach($subscriptions as $sub): /** @var $sub WeeklyReportSubscription */ ?>
+                        <tr data-id="<?= $sub->id ?>">
+                            <td><input type="text" value="<?= $sub->name ?>" onchange="saveChanges(this)" /></td>
+                            <td><input type="number" value="<?= $sub->chat_id ?>" onchange="saveChanges(this)" /></td>
+                            <td><input type="checkbox" <?= $sub->active == 1 ? 'checked="checked"' : '' ?> onclick="saveChanges(this)" /> </td>
+                        </tr>
+                    <?php endforeach; ?>
+                </tbody>
+            </table>
+        </div>
+    </div>
+
+</div>
diff --git a/erp24/web/js/weekly-report/subscription.js b/erp24/web/js/weekly-report/subscription.js
new file mode 100644 (file)
index 0000000..185a438
--- /dev/null
@@ -0,0 +1,38 @@
+/* jshint esversion: 6 */
+
+const param23 = $('meta[name=csrf-param]').attr('content');
+const token23 = $('meta[name=csrf-token]').attr('content');
+
+$(document).ready(() => {
+    $("#subscriptionTable").DataTable({
+        sorting: false,
+        info: false,
+        paging: true,
+        searching: true,
+        language: data_table_language
+    })
+})
+
+function saveChanges(self) {
+    const root = $(self).parent().parent();
+    const id = root.data('id');
+    const name = root.find('input[type=text]');
+    const chatId = root.find('input[type=number]');
+    const active = root.find('input[type=checkbox]');
+    $.ajax({
+        url: window.location.href,
+        method: 'post',
+        data: {
+            action: 'changeSub',
+            id,
+            name: name.val(),
+            chatId: chatId.val(),
+            active: (active.is(':checked') ? 1 : 0),
+            [param23]: token23
+        },
+        dataType: 'text',
+        success: (data) => {
+            console.log(data);
+        }
+    })
+}
\ No newline at end of file