]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
доставка сохранение флаувау
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 18 Apr 2025 07:48:10 +0000 (10:48 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 18 Apr 2025 07:48:10 +0000 (10:48 +0300)
erp24/services/MarketplaceService.php

index f9a1bb5c4439bf2f655a75d881e1519fb60d28cf..fea70698e503a5f19a05feeced9e4d1e2a11ae9f 100644 (file)
@@ -1725,7 +1725,7 @@ class MarketplaceService
             }
 
 
-            $deliveryBlock = $main->findOne('p:contains("Доставить")');
+            /*$deliveryBlock = $main->findOne('p:contains("Доставить")');
             $pickupBlock = $main->findOne('p:contains("Самовывоз")');
 
             if ($deliveryBlock && $deliveryBlock->nextNonWhitespaceSibling()) {
@@ -1741,6 +1741,46 @@ class MarketplaceService
             if ($deliveryText) {
                 Yii::warning('Текст заказа: ' . $deliveryText, __METHOD__);
                 $orderDetails['delivery'] = trim($deliveryText);
+            }*/
+
+
+            $deliveryLabel = $main->findOne('p:contains("Доставить")');
+            $pickupLabel = $main->findOne('p:contains("Самовывоз")');
+
+            $targetLabel = $deliveryLabel ?? $pickupLabel;
+            $labelPrefix = $deliveryLabel ? 'Доставка:' : ($pickupLabel ? 'Самовывоз:' : null);
+
+            if ($targetLabel && $targetLabel->nextNonWhitespaceSibling()) {
+                $infoBlock = $targetLabel->nextNonWhitespaceSibling();
+                $textParts = [];
+
+                $lines = explode('<br>', $infoBlock->innerHtml());
+
+                foreach ($lines as $line) {
+                    $clean = trim(strip_tags($line));
+                    if ($clean !== '') {
+                        $textParts[] = $clean;
+                    }
+                }
+
+                if (!empty($textParts)) {
+                    $deliveryText = $labelPrefix . ' ' . $textParts[0];
+
+                    if (isset($textParts[1])) {
+                        $deliveryText .= ' ' . $textParts[1];
+                    }
+
+                    if (isset($textParts[2])) {
+                        $deliveryText .= ', ' . $textParts[2];
+                    }
+                }
+            }
+
+            if (!empty($deliveryText)) {
+                $deliveryText = preg_replace('/\s+/', ' ', $deliveryText);
+                $deliveryText = trim($deliveryText);
+                Yii::warning('Текст заказа: ' . $deliveryText, __METHOD__);
+                $orderDetails['delivery'] = $deliveryText;
             }
 
             $commentBlock = $main->findOne('p:contains("Комментарий")');
@@ -1911,9 +1951,16 @@ class MarketplaceService
                 //заказ принят
                 if ($marketplaceOrder->raw_data !== json_encode($orderDetails, JSON_UNESCAPED_UNICODE)) {
                     $marketplaceOrder->raw_data = json_encode($orderDetails, JSON_UNESCAPED_UNICODE);
+
                 }
                 if ($marketplaceOrder->save()) {
                     self::createOrUpdateStatusHistory($marketplaceOrder->id, $statusId, $substatusId, $orderDetails);
+                    if (isset($orderDetails['delivery'])) {
+                        $deliveryRecord = self::saveFromDeliveryText($marketplaceOrder->id, $orderDetails['delivery']);
+                        if (!$deliveryRecord) {
+                            Yii::error('Не удалось сохранить доставку' . json_encode($marketplaceOrder->getErrors(), JSON_UNESCAPED_UNICODE));
+                        }
+                    }
                 } else {
                     Yii::error('Не удалось обновить заказ' . json_encode($marketplaceOrder->getErrors(), JSON_UNESCAPED_UNICODE));
                 }
@@ -1941,6 +1988,12 @@ class MarketplaceService
                     $marketplaceOrder->raw_data = json_encode($oldRawData, JSON_UNESCAPED_UNICODE);
                     if ($marketplaceOrder->save()) {
                         self::createOrUpdateStatusHistory($marketplaceOrder->id, $statusId, $substatusId, $orderDetails);
+                        if (isset($orderDetails['delivery'])) {
+                            $deliveryRecord = self::saveFromDeliveryText($marketplaceOrder->id, $orderDetails['delivery']);
+                            if (!$deliveryRecord) {
+                                Yii::error('Не удалось сохранить доставку' . json_encode($marketplaceOrder->getErrors(), JSON_UNESCAPED_UNICODE));
+                            }
+                        }
                     } else {
                         Yii::error('Не удалось обновить заказ' . json_encode($marketplaceOrder->getErrors(), JSON_UNESCAPED_UNICODE));
                     }
@@ -2098,5 +2151,106 @@ class MarketplaceService
         }
     }
 
+    public static function saveFromDeliveryText(int $orderId, string $deliveryText): bool
+    {
+        $model = new MarketplaceOrderDelivery();
+        $model->order_id = $orderId;
+        $model->country = 'Россия';
+        $model->postcode = null;
+        $model->apartment = null;
+        $model->courier_full_name = null;
+        $model->courier_phone = null;
+        $model->courier_extension = null;
+        $model->courier_vehicle_number = null;
+        $model->courier_vehicle_description = null;
+
+        if (stripos($deliveryText, 'Доставка') !== false) {
+            $model->type = 'DELIVERY';
+            $model->service_name = 'FLOWWOW';
+            $model->partner_type = 'FLOWWOW';
+        } elseif (stripos($deliveryText, 'Самовывоз') !== false) {
+            $model->type = 'PICKUP';
+            $model->service_name = 'UNKNOWN';
+            $model->partner_type = 'UNKNOWN';
+        } else {
+            return false;
+        }
+
+        $date = null;
+        if (preg_match('/(сегодня|завтра|послезавтра),?\s*(\d{1,2})\s+([а-яА-Я]+)\s+(\d{4})/u', $deliveryText, $match)) {
+            [$_, $label, $day, $monthName, $year] = $match;
+        } elseif (preg_match('/(\d{1,2})\s+([а-яА-Я]+)\s+(\d{4})/u', $deliveryText, $match)) {
+            [$_, $day, $monthName, $year] = $match;
+        }
+
+        if (isset($monthName)) {
+            $months = [
+                'января' => '01', 'февраля' => '02', 'марта' => '03',
+                'апреля' => '04', 'мая' => '05', 'июня' => '06',
+                'июля' => '07', 'августа' => '08', 'сентября' => '09',
+                'октября' => '10', 'ноября' => '11', 'декабря' => '12',
+            ];
+            $month = $months[mb_strtolower($monthName)] ?? null;
+            if ($month) {
+                $date = sprintf('%04d-%02d-%02d', $year, $month, $day);
+            }
+        }
+
+        if ($date && preg_match('/в\s+(\d{1,2}:\d{2})—(\d{1,2}:\d{2})/u', $deliveryText, $timeMatch)) {
+            $model->delivery_start = $date . ' ' . $timeMatch[1] . ':00';
+            $model->delivery_end = $date . ' ' . $timeMatch[2] . ':00';
+        } elseif ($date && preg_match('/с\s+(\d{1,2}:\d{2})/u', $deliveryText, $timeMatch)) {
+            $model->delivery_start = $date . ' ' . $timeMatch[1] . ':00';
+            $model->delivery_end = null;
+        }
+
+        $address = self::parseAddressFromDeliveryText($deliveryText);
+        $model->city = $address['city'];
+        $model->street = $address['street'];
+        $model->house = $address['house'];
+        $model->latitude = $address['latitude'];
+        $model->longitude = $address['longitude'];
+
+        return $model->save();
+    }
+
+    public static function parseAddressFromDeliveryText(string $text): array
+    {
+        $city = 'Уточняется';
+        $street = 'Уточняется';
+        $house = 'Уточняется';
+        $latitude = 0.0;
+        $longitude = 0.0;
+
+        $parts = explode(',', $text);
+        $parts = array_map('trim', $parts);
+        $count = count($parts);
+
+        if ($count >= 3) {
+            $city = $parts[$count - 3];
+            $street = $parts[$count - 2];
+            $house = $parts[$count - 1];
+        }
+
+        switch (mb_strtolower($city)) {
+            case 'нижний новгород':
+                $latitude = 56.3269;
+                $longitude = 44.0042;
+                break;
+            case 'москва':
+                $latitude = 55.7400;
+                $longitude = 37.6100;
+                break;
+        }
+
+        return [
+            'city' => $city,
+            'street' => $street,
+            'house' => $house,
+            'latitude' => $latitude,
+            'longitude' => $longitude,
+        ];
+    }
+
 
 }