]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-261] отправка в 1с
authorAlexander Smirnov <fredeom@mail.ru>
Mon, 16 Dec 2024 12:50:41 +0000 (15:50 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Mon, 16 Dec 2024 12:50:41 +0000 (15:50 +0300)
erp24/api2/controllers/DataController.php

index a54c78b55228fa98ca5d57233996fa6e8739cfb7..bbd1af1fdc69a6072c6f7a8979b52ecfd61290dc 100644 (file)
@@ -38,6 +38,8 @@ use yii_app\records\SelfCostProduct;
 //use yii_app\records\SalesProductsUpdate;
 use yii_app\records\SalesUpdate;
 use yii_app\records\Terminals;
+use yii_app\records\WaybillIncoming;
+use yii_app\records\WaybillIncomingProducts;
 use yii_app\records\WriteOffs;
 use yii_app\records\WriteOffsErp;
 use yii_app\records\WriteOffsProducts;
@@ -134,6 +136,14 @@ class DataController extends BaseController {
                 $mess['create_write_offs'] = $writeOff;
             }
 
+            $waybillIncomingArray = $this->getWaybillIncomingDoc();
+            $waybillIncoming = $waybillIncomingArray['waybillIncoming'];
+            $waybillIncomingIds = $waybillIncomingArray['waybillIncomingIds'];
+
+            if (!empty($waybillIncoming)) {
+                $mess['create_waybill_incoming'] = $waybillIncoming;
+            }
+
             $createEmployeeArray = $this->getCreateEmployee();
             if (!empty($createEmployeeArray)) {
                 $mess['create_employee'] = $createEmployeeArray;
@@ -182,6 +192,9 @@ class DataController extends BaseController {
                 if (!empty($writeOffIds) && !empty($writeOff)) {
                     $this->getWriteOffsSetStatusSend($writeOffIds);
                 }
+                if (!empty($waybillIncomingIds) && !empty($waybillIncoming)) {
+                    $this->getWaybillIncomingSetStatusSend($waybillIncomingIds);
+                }
                 $json = json_encode($mess, JSON_UNESCAPED_UNICODE);
             }
 //
@@ -219,6 +232,22 @@ class DataController extends BaseController {
         }
     }
 
+    private function getWaybillIncomingSetStatusSend($waybillIncomingIds) {
+        try {
+            $waybillIncoming = WaybillIncoming::find()->where(['in', 'id', $waybillIncomingIds])->all();
+            foreach ($waybillIncoming as $waybillIncomingElem) {
+                $waybillIncomingElem->status = 3;
+                $waybillIncomingElem->send_at = date('Y-m-d H:i:s');
+                $waybillIncomingElem->save();
+                if ($waybillIncomingElem->getErrors()) {
+                    LogService::apiErrorLog(json_encode(["error_id" => 2.5, "error" => $waybillIncomingElem->getErrors()], JSON_UNESCAPED_UNICODE));
+                }
+            }
+        } catch (Exception $e3) {
+            file_put_contents(self::OUT_DIR . '/log_created_waybill_incoming_error.txt', PHP_EOL . date("d.m.Y H:i:s",time()). $e3->getMessage(), FILE_APPEND);
+        }
+    }
+
     private function getWriteOffsDoc() : array
     {
         $writeOff = [];
@@ -306,6 +335,80 @@ class DataController extends BaseController {
         ];
     }
 
+    private function getWaybillIncomingDoc() : array {
+        $waybillIncoming = [];
+        $waybillIncomingIds = [];
+        $waybillIncomingResult = [];
+
+        try {
+            $waybillIncoming = WaybillIncoming::find()
+                ->select([
+                    'id',
+                    'guid',
+                    'store_guid',
+                    'number',
+                    'date',
+//                    'write_offs_type',
+                    'comment',
+                    'quantity',
+                    'summ'
+                ])
+                ->indexBy('id')
+                ->andWhere(['and', ['status' => 2], ['active' => 1]])
+                ->asArray()
+                ->all();
+
+            if (!empty($waybillIncoming)) {
+                $waybillIncomingIds = array_column($waybillIncoming, 'id');
+
+                $products = WaybillIncomingProducts::find()
+                    ->select(['waybill_incoming_id', 'product_id', 'product_count', 'product_price', 'summ'])
+                    ->where(['in', 'waybill_incoming_id', $waybillIncomingIds])
+//                    ->andWhere(['active_product' => 1])
+                    ->asArray()
+                    ->all();
+
+                $waybillIncomingProducts = [];
+
+                foreach ($products as $product) {
+                    $waybillIncomingProducts[$product['waybill_incoming_id']][] = [
+                        'product_id' => $product['product_id'],
+                        'quantity' => (string) $product['product_count'],
+                        'price' => $product['product_price'],
+                    ];
+                }
+
+                $waybillIncomingTemp = $waybillIncoming;
+                foreach ($waybillIncomingTemp as $key => $row) {
+                    $waybillIncomingProductsRow = [];
+                    if (array_key_exists($row['id'], $waybillIncomingProducts)) {
+                        $waybillIncomingProductsRow = $waybillIncomingProducts[$row['id']];
+                    }
+                    if (!empty($waybillIncomingProductsRow)) {
+                        $waybillIncoming[$key]['items'] = $waybillIncomingProductsRow;
+                    }
+                }
+
+                foreach ($waybillIncoming as $row) {
+                    $waybillIncomingResult[] = [
+                        'id' => $row['guid'],
+                        'store_id' => $row['store_guid'],
+//                        'type' => $row['write_offs_type'],
+//                        'cause' => 'Документ списания в ERP ' . $row['number'],
+                        'items' => $row['items'],
+                        'summ' => $row['summ'],
+                        'comment' => $row['comment'],
+                    ];
+                }
+            }
+
+        } catch (Exception $e3) {
+            file_put_contents(self::OUT_DIR . '/log_created_waybill_incoming_error.txt', PHP_EOL . date("d.m.Y H:i:s",time()). $e3->getMessage(). ' file: '. $e3->getFile() . ' line: '. $e3->getLine(), FILE_APPEND);
+        }
+
+        return compact('waybillIncoming', 'waybillIncomingIds');
+    }
+
     private function getCreateEmployee() : array {
         $employeeOnShiftArray = EmployeeOnShift::find()
             ->where([