From 4080493c1a0ee237458b32c2a9d1c699446dfbe2 Mon Sep 17 00:00:00 2001 From: fomichev Date: Fri, 18 Apr 2025 10:48:10 +0300 Subject: [PATCH] =?utf8?q?=D0=B4=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D0=BA=D0=B0?= =?utf8?q?=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5?= =?utf8?q?=20=D1=84=D0=BB=D0=B0=D1=83=D0=B2=D0=B0=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/services/MarketplaceService.php | 156 +++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) diff --git a/erp24/services/MarketplaceService.php b/erp24/services/MarketplaceService.php index f9a1bb5c..fea70698 100644 --- a/erp24/services/MarketplaceService.php +++ b/erp24/services/MarketplaceService.php @@ -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('
', $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, + ]; + } + } -- 2.39.5