]> gitweb.erp-flowers.ru Git - yii-erp24/.git/commitdiff
fix integration api
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 27 Mar 2024 16:41:01 +0000 (19:41 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 27 Mar 2024 16:41:01 +0000 (19:41 +0300)
erp24/controllers/ApiController.php
erp24/views/api/integrations_index.php

index fec55e363cd8a644e4dcd3ac1c15f737962f85fc..690428695a279190e5dd30d6f28e1f4a55e1bb47 100644 (file)
@@ -216,7 +216,9 @@ class ApiController extends Controller
                 'label' => 'GUID или ID',
                 'format' => 'raw',
                 'value' => function ($model) use ($inSore) {
-                    return Html::input('string', '', $inSore[$model['id']] ?? '', ['class' => 'form-control guid_input']);
+                    return Html::input('string', '', $inSore[$model['id']] ?? '', [
+                        'data-id' => $model['id'],
+                        'class' => 'form-control guid_input']);
                 }
             ],
             [
@@ -232,7 +234,7 @@ class ApiController extends Controller
                                 'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
                             ],
                             'ajax' => [
-                                'url' => 'poisk-item',
+                                'url' => 'poisk-items?entity=' . Yii::$app->request->get('entity') ?? 'admin',
                                 'dataType' => 'json',
                                 'data' => new JsExpression('function(params) { return {q:params.term}; }')
                             ],
@@ -240,6 +242,9 @@ class ApiController extends Controller
                             'templateResult' => new JsExpression('function(city) { return city.text; }'),
                             'templateSelection' => new JsExpression('function (city) { return city.text; }'),
                         ],
+                        'pluginEvents' => [
+                            'change' => 'function() { $(".guid_input[data-id=' . $model['id'] . ']").val($(this).val()); }'
+                        ]
                     ]);
                 }
             ],
@@ -263,27 +268,93 @@ class ApiController extends Controller
             $export_id = Yii::$app->request->post('export_id');
             $value = Yii::$app->request->post('val');
             $inid = Yii::$app->request->post('id');
-            $expold = Yii::$app->request->post('expold');
 
-            if ($entity == "admin" && $export_id == 1) {
+            if ($entity == "admin") {
                 $admin = Admin::findOne(['id' => $inid]);
                 $admin->guid = $value;
-                $admin->save();
-                return "Обновили admin id=$inid guid= $value ";
+                $admin->save(false);
             }
 
-            if ($expold != $value or $expold == '') {
+            $exportImportTable = ExportImportTable::find()->where(['export_id' => 1, 'entity' => $entity, 'entity_id' => $inid])->one();
+            if (!$exportImportTable) {
+                $exportImportTable = new ExportImportTable;
+                $exportImportTable->export_id = 1;
+                $exportImportTable->entity = $entity;
+                $exportImportTable->entity_id = $inid;
+            }
+            $expold = $exportImportTable->export_val;
 
-                $exportImportTableItem = new ExportImportTable(['export_id' => $export_id, 'entity' => $entity, 'entity_id' => $inid, 'export_val' => $value]);
-                $exportImportTableItem->save();
+            $exportImportTable->export_val = $value;
+            $exportImportTable->save(false);
 
-                $apiIntegrationLogs = new ApiIntegrationLogs(['admin_id' => Yii::$app->user->id, 'date' => time(), 'export_id' => $export_id, 'entity' => $entity, 'entity_id' => $inid, 'export_val' => $value, 'export_val_old' => $expold]);
-                $apiIntegrationLogs->save();
-                return "Данные сохранены";
-            }
+            $apiIntegrationLogs = new ApiIntegrationLogs([
+                'admin_id' => Yii::$app->user->id,
+                'date' => date("Y-m-d H:i:s"),
+                'export_id' => $export_id,
+                'entity' => $entity,
+                'entity_id' => $inid,
+                'export_val' => $value,
+                'export_val_old' => $expold ?? ''
+            ]);
+            $apiIntegrationLogs->save(false);
+
+            return "Данные сохранены";
         }
     }
 
+    public function actionPoiskItems($q) {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+        $out = ['results' => ['id' => '', 'text' => '']];
+
+        $tip = "admin";
+        $tip = Yii::$app->request->get('entity') ?? $tip;
+
+        if ($tip == "products") {
+            $data = Products1c::find()
+                ->select([
+                    'products_1c.id',
+                    'products_1c.parent_id',
+                    'products_1c.name as text',
+                    'products_1c.code',
+                    'products_1c.articule',
+                    'prices.price'
+                ])
+                ->innerJoin('prices', 'products_1c.id = prices.product_id')
+                ->where([
+                    'AND',
+                    ['products_1c.tip' => 'products'],
+                    ['LIKE', 'products_1c.name', '%' . $q . '%', false],
+                ])
+                ->orderBy(['prices.price' => SORT_DESC])
+                ->limit(20)
+                ->asArray()
+                ->all();
+
+            $out['results'] = array_values($data);
+
+        } else if ($tip == 'admin' || $tip == 'city_store') {
+            $data = Products1c::find()
+                ->select([
+                    'products_1c.id',
+                    'products_1c.name as text',
+                ])
+                ->where([
+                    'AND',
+                    ['products_1c.tip' => $tip],
+                    ['LIKE', 'products_1c.name', '%' . $q . '%', false],
+                ])
+                ->orderBy(['products_1c.name' => SORT_ASC])
+                ->limit(20)
+                ->asArray()
+                ->all();
+
+            $out['results'] = array_values($data);
+
+        }
+
+        return $out;
+    }
+
     public function actionPoiskItem()
     {
         $tip = "admin";
index e416e82b870dd62c3cb9e2a61ba9872fe7884da6..e4807e9c714bf2c2d2af35dd5a41130b64e3db7c 100644 (file)
@@ -18,11 +18,10 @@ use yii\web\View;
 
 $this->registerJs(<<<JS
     function ajaxLogSave(id,val) {
-        var old=$('#expold_'+id).val();    
         $.ajax({
             url: '/api/integration-save/',
             method: 'POST',
-            data: '&id='+id+'&val='+val+'&entity=" . $entity . "&export_id=" . $export_id . "&expold='+old,
+            data: '&id='+id+'&val='+val+'&entity=$entity&export_id=$export_id',
             success: function(data){
                 alert(data);
             }
@@ -40,7 +39,7 @@ $this->registerJs(<<<JS
 
 
     $('.guid_input').change(function() {
-        var id=$(this).attr('id-data');
+        var id=$(this).attr('data-id');
         var val=$(this).val();
         ajaxLogSave(id,val);
     });