]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-85 Реализовать обмен данных с 1С Бухгалтерия
authormarina <m.zozirova@gmail.com>
Tue, 6 Aug 2024 06:53:32 +0000 (09:53 +0300)
committermarina <m.zozirova@gmail.com>
Tue, 6 Aug 2024 06:53:32 +0000 (09:53 +0300)
контроллер и модель

erp24/api2/controllers/DataBuhTestController.php [new file with mode: 0644]
erp24/records/ApiCronBuhTest.php [new file with mode: 0644]

diff --git a/erp24/api2/controllers/DataBuhTestController.php b/erp24/api2/controllers/DataBuhTestController.php
new file mode 100644 (file)
index 0000000..d9a7729
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+
+namespace app\api2\controllers;
+
+use app\controllers\BaseController;
+use Exception;
+use Yii;
+use yii\helpers\Json;
+use yii_app\records\ApiCronBuhTest;
+
+class DataBuhTestController extends BaseController
+{
+    public function actionRequest() {
+        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+
+        $request = \Yii::$app->request->getRawBody();
+
+        try {
+            $result = Json::decode($request);
+        } catch (\Exception $ex) {
+            return $this->asJson(['error' => ['code' => 400, 'message' => 'Json body invalid']]);
+        }
+
+        $result['ver'] = $result['ver'] ?? '';
+
+
+        if (empty($result['ver']) || round($result['ver'], 2) < 1.1) {
+            return $this->asJson(["error_id" => 1, "error" => "minimal version is 1.1"]);
+        }
+
+        $json = '';
+
+        try {
+            $apiCronTest = ApiCronBuhTest::find()
+                ->where(['status' => 0])
+                ->andWhere(['direct_id' => 1])
+                ->orderBy(['date' => SORT_ASC])
+                ->one();
+            if ($apiCronTest) {
+                $json = $apiCronTest->json_post;
+
+                $apiCronTest->status = 1;
+                $apiCronTest->date_up = date("Y-m-d H:i:s");
+
+                $validate = $apiCronTest->validate();
+                if ($validate) {
+                    $apiCronTest->save();
+                }
+            }
+        } catch (Exception $e) {
+
+        }
+
+        return $this->asJson(json_decode($json, true));
+    }
+
+
+    public function actionUpload() {
+        set_time_limit(600);
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+
+        $request = Yii::$app->request->getRawBody();
+
+        try {
+            $result = Json::decode($request);
+        } catch (Exception $ex) {
+            return $this->asJson(['error' => ['code' => 400, 'message' => 'Json body invalid']]);
+        }
+
+        $requestId = '';
+        if (!empty($result["request_id"])) {
+            $requestId = $result["request_id"];
+        }
+
+        try {
+
+
+            $apiCronTest = new ApiCronBuhTest();
+            $apiCronTest->date = date("Y-m-d H:i:s");
+            $apiCronTest->date_up = date("Y-m-d H:i:s");
+            $apiCronTest->status = 2;
+            $apiCronTest->json_post = $request;
+            $apiCronTest->request_id = $requestId;
+            $apiCronTest->direct_id = 2;
+            $validate = $apiCronTest->validate();
+            if ($validate) {
+                $apiCronTest->save();
+            }
+
+        } catch (Exception $e) {
+
+        }
+
+        return $this->asJson(['response' => true]);
+    }
+
+}
\ No newline at end of file
diff --git a/erp24/records/ApiCronBuhTest.php b/erp24/records/ApiCronBuhTest.php
new file mode 100644 (file)
index 0000000..48eb250
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "api_cron_buh_test".
+ *
+ * @property int $id ID
+ * @property string $date
+ * @property string $date_up
+ * @property int $status Статус
+ * @property string|null $json_post Тело запроса
+ * @property string $request_id id запроса
+ */
+class ApiCronBuhTest extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'api_cron_buh_test';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['date', 'date_up', 'request_id'], 'required'],
+            [['date', 'date_up'], 'safe'],
+            [['status'], 'integer'],
+            [['json_post'], 'string'],
+            [['request_id'], 'string', 'max' => 36],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'date' => 'Date',
+            'date_up' => 'Date Up',
+            'status' => 'Status',
+            'json_post' => 'Json Post',
+            'request_id' => 'Request ID',
+        ];
+    }
+}