}
}
}
+ public static function extractArticleCode($productName)
+ {
+ if (preg_match('/\(([^()]+)\)\s*$/u', $productName, $matches)) {
+ return $matches[1];
+ }
+ return null;
+ }
+ 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)) {
+ [$__, $day, $monthName, $year] = $match;
+
+ $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,
+ ];
+ }
+
+
}