]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-209] отображает на странице гуиды чеков, айди связанных сделок и телефоны, котор...
authorAlexander Smirnov <fredeom@mail.ru>
Mon, 7 Oct 2024 13:25:49 +0000 (16:25 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Mon, 7 Oct 2024 13:25:49 +0000 (16:25 +0300)
erp24/actions/orders/FillPhoneAction.php [new file with mode: 0644]
erp24/controllers/OrdersController.php
erp24/views/orders/fill-phone.php [new file with mode: 0644]

diff --git a/erp24/actions/orders/FillPhoneAction.php b/erp24/actions/orders/FillPhoneAction.php
new file mode 100644 (file)
index 0000000..ba135bf
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+namespace yii_app\actions\orders;
+
+use Yii;
+use yii\base\Action;
+use yii\base\DynamicModel;
+use yii\helpers\ArrayHelper;
+use yii_app\records\OrdersAmo;
+use yii_app\records\Sales;
+
+class FillPhoneAction extends Action
+{
+    public function run() {
+
+        $model = DynamicModel::validateData([
+            'date_from' => date('Y-m-d 00:00:00'),
+            'date_to' => date('Y-m-d 23:59:59')
+        ], [
+            [['date_from', 'date_to'], 'safe']
+        ]);
+
+        $action = null;
+        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
+            $action = Yii::$app->request->post('action');
+        };
+
+        $sales = [];
+        $phoneByAmoIdMap = [];
+        if ($action == 'show') {
+            $sales = Sales::find()->select(['id', 'order_id'])->where(['between', 'date', $model->date_from, $model->date_to])
+                ->andWhere(['not in', 'order_id', ['', null]])
+                ->andWhere(['phone' => 0])
+                ->all();
+            $orderIds = ArrayHelper::getColumn($sales, 'order_id');
+            $ordersAmo = OrdersAmo::find()->select(['amo_id', 'phone'])
+                ->where(['amo_id' => $orderIds])
+                ->andWhere(['not in', 'phone', ['', null]])
+                ->all();
+            $phoneByAmoIdMap = ArrayHelper::map($ordersAmo, 'amo_id', 'phone');
+        }
+
+        return $this->controller->render('fill-phone',
+            compact('model', 'action', 'sales', 'phoneByAmoIdMap'));
+    }
+}
\ No newline at end of file
index a563e2612fd74f1fbc924186dcd98be31f143d78..72785bd0a82c3a3597c10b697bf19c780ea45353 100644 (file)
@@ -6,6 +6,7 @@ ini_set("memory_limit", "256M");
 
 use Exception;
 use Yii;
+use yii\helpers\ArrayHelper;
 use yii\web\Controller;
 use yii_app\records\CreateChecks;
 use yii_app\records\ExportImportTable;
@@ -14,6 +15,13 @@ use yii_app\records\OrdersAmoSearch;
 
 class OrdersController extends Controller
 {
+    public function actions()
+    {
+        return ArrayHelper::merge(parent::actions(), [
+            'fill-phone' => \yii_app\actions\orders\FillPhoneAction::class
+        ]);
+    }
+
     public function actionCouriers() {
         return $this->render('couriers');
     }
diff --git a/erp24/views/orders/fill-phone.php b/erp24/views/orders/fill-phone.php
new file mode 100644 (file)
index 0000000..46a0ec2
--- /dev/null
@@ -0,0 +1,142 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+use yii\base\DynamicModel;
+
+use dosamigos\datepicker\DatePicker;
+
+use yii_app\records\Sales;
+
+/** @var $model DynamicModel */
+/** @var $action string */
+/** @var $sales array */
+/** @var $phoneByAmoIdMap array */
+
+?>
+
+<div class="ordersFillPhone m-5">
+
+    <?php $form = ActiveForm::begin(); ?>
+    <?= $model->date_from ?>
+    <?= $model->date_to ?>
+    <div class="row">
+        <div class="col-2">
+            <?= $form->field($model, 'date_from')->widget(DatePicker::class, [
+                'template' => '{addon}{input}',
+                'clientOptions' => [
+                    'autoclose' => true,
+                    'format' => 'yyyy-mm-dd 00:00:00',
+                    'todayBtn' => true
+                ],
+            ])->label(false) ?>
+        </div>
+        <div class="col-2">
+            <?= $form->field($model, 'date_to')->widget(DatePicker::class, [
+                'template' => '{addon}{input}',
+                'clientOptions' => [
+                    'autoclose' => true,
+                    'format' => 'yyyy-mm-dd 23:59:59',
+                    'todayBtn' => true
+                ],
+            ])->label(false) ?>
+        </div>
+    </div>
+    <div class="row">
+        <div class="col-4">
+            <?= Html::submitButton('Показать', ['class' => 'btn btn-secondary', 'name' => 'action', 'value' => 'show'])?>
+            <?php if (count($sales) > 0): ?>
+                <?= Html::a('Выгрузить в файл', null, ['onclick' => 'download_table_as_csv(\'hiddenTable\');', 'class' => 'btn btn-success', 'name' => 'action', 'value' => 'download']) ?>
+                <?= Html::submitButton('Обновить', ['class' => 'btn btn-primary', 'name' => 'action', 'value' => 'update']) ?>
+            <?php endif; ?>
+        </div>
+    </div>
+    <?php ActiveForm::end() ?>
+
+    <div class="row">
+        <div class="col-4">
+            <?= $action ?>
+            Число записей: <?= count($sales) ?>
+        </div>
+    </div>
+
+    <table id="hiddenTable" style="display:none">
+        <tbody>
+            <?php foreach($sales as $sale): /** @var $sale Sales */ ?>
+                <?php if (!empty($phoneByAmoIdMap[$sale->order_id])): ?>
+                    <tr>
+                        <td><?= $sale->id ?></td>
+                        <td><?= $sale->order_id ?></td>
+                        <td><?= $phoneByAmoIdMap[$sale->order_id] ?></td>
+                    </tr>
+                <?php endif; ?>
+            <?php endforeach; ?>
+        </tbody>
+    </table>
+
+    <div class="row">
+        <div class="col-3">
+            <table id="table" class="table table-hover table-striped">
+                <thead>
+                    <tr>
+                        <th>guid</th>
+                        <th>order_id</th>
+                        <th>phone</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <?php foreach($sales as $sale): /** @var $sale Sales */ ?>
+                        <tr>
+                            <td><?= $sale->id ?></td>
+                            <td><?= $sale->order_id ?></td>
+                            <td><?= $phoneByAmoIdMap[$sale->order_id] ?? 'missing' ?></td>
+                        </tr>
+                    <?php endforeach; ?>
+                </tbody>
+            </table>
+        </div>
+    </div>
+
+    <script>
+        $(document).ready(()=>{
+            $('#table').DataTable({
+                sorting: false,
+                info: false,
+                paging: true,
+                searching: true,
+                language: data_table_language
+            })
+        })
+
+        // Quick and simple export target #table_id into a csv
+        function download_table_as_csv(table_id, separator = ',') {
+            // Select rows from table_id
+            var rows = document.querySelectorAll('table#' + table_id + ' tr');
+            // Construct csv
+            var csv = [];
+            for (var i = 0; i < rows.length; i++) {
+                var row = [], cols = rows[i].querySelectorAll('td, th');
+                for (var j = 0; j < cols.length; j++) {
+                    // Clean innertext to remove multiple spaces and jumpline (break csv)
+                    var data = cols[j].innerText.replace(/(\r\n|\n|\r)/gm, '').replace(/(\s\s)/gm, ' ')
+                    // Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv)
+                    data = data.replace(/"/g, '""');
+                    // Push escaped string
+                    row.push('"' + data + '"');
+                }
+                csv.push(row.join(separator));
+            }
+            var csv_string = csv.join('\n');
+            // Download it
+            var filename = 'export_' + table_id + '_' + new Date().toLocaleDateString() + '.csv';
+            var link = document.createElement('a');
+            link.style.display = 'none';
+            link.setAttribute('target', '_blank');
+            link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string));
+            link.setAttribute('download', filename);
+            document.body.appendChild(link);
+            link.click();
+            document.body.removeChild(link);
+        }
+    </script>
+</div>