From 7b83843057277c1582cdccea601983d9b43c0143 Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 19 Mar 2025 11:21:24 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=8F=20email=20=D0=B2=20marke?= =?utf8?q?tplace=5Fstore?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/media/controllers/FlowwowController.php | 65 ++++---- ...count_email_to_marketplace_store_table.php | 55 +++++++ erp24/records/MarketplaceStore.php | 19 ++- erp24/services/MarketplaceService.php | 155 +++++++++++++++++- erp24/views/marketplace-store/_form.php | 1 + erp24/views/marketplace-store/index.php | 1 + erp24/views/marketplace-store/view.php | 4 + 7 files changed, 260 insertions(+), 40 deletions(-) create mode 100644 erp24/migrations/m250319_072505_add_column_account_email_to_marketplace_store_table.php diff --git a/erp24/media/controllers/FlowwowController.php b/erp24/media/controllers/FlowwowController.php index b4e365b3..49539eb7 100644 --- a/erp24/media/controllers/FlowwowController.php +++ b/erp24/media/controllers/FlowwowController.php @@ -410,46 +410,49 @@ class FlowwowController extends Controller } - $itemsBlock = $main->findOneOrFalse('h2:contains("Детали заказа")') ? $main->findOne('h2:contains("Детали заказа")') : $main->findOne('p:contains("Детали заказа")'); - $itemsTable = $itemsBlock->parentNode()->find('table', 2); - - $itemsRows = $itemsTable->find('tr'); - foreach ($itemsRows as $itemsRow) { - Yii::warning('Строка заказа: ' . json_encode($itemsRow->innerText(), JSON_UNESCAPED_UNICODE), __METHOD__); - $itemData = [ - 'name' => '', - 'count' => '', - 'price' => '', - ]; - - - // Извлекаем название и количество из второго - $tds = $itemsRow->find('td'); - if (count($tds) >= 2) { - $itemData['name'] = trim(str_replace("\u{00A0}", ' ', strip_tags(preg_replace('/\s+/', ' ',$tds[1]->find('p', 0)->innerText())))); - $itemData['count'] = trim(str_replace(["\u{00A0}", 'шт.'], '', strip_tags(preg_replace('/\s+/', '', $tds[1]->find('p', 1)->innerText())))); - } + $itemsBlock = $main->findOneOrFalse('h2:contains("Детали заказа")') ? $main->findOne('h2:contains("Детали заказа")') : $main->findOneOrFalse('p:contains("Детали заказа")'); + if ($itemsBlock) { + $itemsTable = $itemsBlock->parentNode()->find('table', 2); + + $itemsRows = $itemsTable->find('tr'); + foreach ($itemsRows as $itemsRow) { + Yii::warning('Строка заказа: ' . json_encode($itemsRow->innerText(), JSON_UNESCAPED_UNICODE), __METHOD__); + $itemData = [ + 'name' => '', + 'count' => '', + 'price' => '', + ]; + + + // Извлекаем название и количество из второго + $tds = $itemsRow->find('td'); + if (count($tds) >= 2) { + $itemData['name'] = trim(str_replace("\u{00A0}", ' ', strip_tags(preg_replace('/\s+/', ' ',$tds[1]->find('p', 0)->innerText())))); + $itemData['count'] = trim(str_replace(["\u{00A0}", 'шт.'], '', strip_tags(preg_replace('/\s+/', '', $tds[1]->find('p', 1)->innerText())))); + } - // Извлекаем цену из третьего - if (count($tds) >= 3) { - $itemData['price'] = (float)trim(str_replace(["\u{00A0}", '₽' , ' '], '', strip_tags(preg_replace('/\s+/', ' ', $tds[2]->find('p', 0)->innerText())))); - } + // Извлекаем цену из третьего + if (count($tds) >= 3) { + $itemData['price'] = (float)trim(str_replace(["\u{00A0}", '₽' , ' '], '', strip_tags(preg_replace('/\s+/', ' ', $tds[2]->find('p', 0)->innerText())))); + } - // Добавляем данные в массив - $orderItems[] = $itemData; - } - $totalSum = 0; - $sumBlock = $itemsBlock->parentNode()->find('table', 3); - $sumRow = $sumBlock->find('tr'); + // Добавляем данные в массив + $orderItems[] = $itemData; + } + $totalSum = 0; + $sumBlock = $itemsBlock->parentNode()->find('table', 3); + $sumRow = $sumBlock->find('tr'); $sumTds = $sumRow->find('td'); if (count($sumTds) >= 2) { $totalSum = (float)trim(str_replace(["\u{00A0}", '₽' , ' '], '', strip_tags(preg_replace('/\s+/', ' ', $sumTds[1]->innerText())))); } - $orderDetails['items'] = $orderItems; - $orderDetails['totalSum'] = $totalSum; + $orderDetails['items'] = $orderItems; + $orderDetails['totalSum'] = $totalSum; + } + } $order[$orderNumber] = $orderDetails; return [ diff --git a/erp24/migrations/m250319_072505_add_column_account_email_to_marketplace_store_table.php b/erp24/migrations/m250319_072505_add_column_account_email_to_marketplace_store_table.php new file mode 100644 index 00000000..ca68198e --- /dev/null +++ b/erp24/migrations/m250319_072505_add_column_account_email_to_marketplace_store_table.php @@ -0,0 +1,55 @@ +db->schema->getTableSchema(self::TABLE_NAME); + if ($table === null) { + return; + } + + if ($table->getColumn('account_email') === null) { + $this->addColumn( + self::TABLE_NAME, + 'account_email', + $this->string()->null()->comment('Почта привязанная к магазину') + ); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + if ($this->db->schema->getTableSchema(self::TABLE_NAME) === null) { + return; + } + + if ($this->db->schema->getTableSchema(self::TABLE_NAME)->getColumn('account_email') !== null) { + $this->dropColumn(self::TABLE_NAME, 'account_email'); + } + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m250319_072505_add_column_account_email_to_marketplace_store_table cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/erp24/records/MarketplaceStore.php b/erp24/records/MarketplaceStore.php index 36884a4b..7137cfd3 100644 --- a/erp24/records/MarketplaceStore.php +++ b/erp24/records/MarketplaceStore.php @@ -20,6 +20,7 @@ use yii\db\Expression; * @property int $created_by * @property int $updated_at * @property int $updated_by + * @property string $account_email Почта привязанная к магазину */ class MarketplaceStore extends \yii\db\ActiveRecord { @@ -39,9 +40,10 @@ class MarketplaceStore extends \yii\db\ActiveRecord return [ [['store_id', 'guid', 'warehouse_id', 'warehouse_guid', 'firm'], 'required'], [['store_id'], 'exist', 'targetClass' => CityStore::class, 'targetAttribute' => 'id'], - [['name', 'guid', 'firm', 'warehouse_guid',], 'string'], + [['name', 'guid', 'firm', 'warehouse_guid', 'account_email'], 'string'], [['created_at', 'updated_at'], 'datetime', 'format' => 'php:Y-m-d H:i:s'], [['store_id', 'created_by', 'updated_by', 'warehouse_id',], 'integer'], + ['account_email', 'unique', 'message' => 'Этот email уже используется.'], ]; } @@ -79,7 +81,8 @@ class MarketplaceStore extends \yii\db\ActiveRecord 'created_at' => 'Дата создания записи', 'created_by' => 'Автор создания записи', 'updated_at' => 'Дата изменения записи', - 'updated_by' => 'Автор изменения записи' + 'updated_by' => 'Автор изменения записи', + 'account_email' => 'Почта привязанная к магазину' ]; } @@ -90,6 +93,18 @@ class MarketplaceStore extends \yii\db\ActiveRecord ); } + /** + * Получает warehouse_guid по account_email. + * + * @param string $accountEmail + * @return string|null + */ + public static function getWarehouseGuidByAccountEmail($accountEmail) + { + $model = self::findOne(['account_email' => $accountEmail]); + return $model ? $model->warehouse_guid : null; + } + public function getStore() { return $this->hasOne(CityStore::class, ['id' => 'store_id']); } diff --git a/erp24/services/MarketplaceService.php b/erp24/services/MarketplaceService.php index 6c6dded6..8b3dbfb7 100644 --- a/erp24/services/MarketplaceService.php +++ b/erp24/services/MarketplaceService.php @@ -1410,29 +1410,172 @@ class MarketplaceService public static function processMessages(array $messages) { + $count = 0; if ($messages) { foreach ($messages as $message) { if ($message['subject_index'] == 1) { + $order = self::getOrdersDataFromMessage($message); + $count += self::processFlowwowOrders($order); + } elseif ($message['subject_index'] == 2) { + $order = self::getOrdersDataFromMessage($message); + $count += self::processFlowwowOrders($order); + } elseif ($message['subject_index'] == 3) { + $order = self::getOrdersDataFromMessage($message); + } elseif ($message['subject_index'] == 4) { + $order = self::getOrdersDataFromMessage($message); + } elseif ($message['subject_index'] == 5) { + $order = self::getOrdersDataFromMessage($message); + + } + + + + } + } + return $count; + } + + + public static function getOrdersDataFromMessage($message) { + $html = $message['body']; + $orderDetails = null; + $order = null; + if (!empty($html)) { + $html = html_entity_decode($html, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + + $doc = new HtmlDomParser($html); + + $main = $doc->findOneOrFalse("body"); + + if ($main !== false) { + $orderTitleNode = $main->findOne("h1"); + + if ($orderTitleNode && preg_match('/№(\d+)/', $orderTitleNode->innertext, $matches)) { + $orderNumber = (int)$matches[1]; } + } + $orderDetails['number'] = $orderNumber; + $deliveryText = ''; + $commentText = ''; + $clientText = ''; + $recipientText = ''; + $orderItems = []; + + + $deliveryBlock = $main->findOne('p:contains("Доставить")'); + $pickupBlock = $main->findOne('p:contains("Самовывоз")'); + + if ($deliveryBlock && $deliveryBlock->nextNonWhitespaceSibling()) { + $deliveryBlock = $deliveryBlock->nextNonWhitespaceSibling(); + $deliveryText = "Доставка: " . strip_tags($deliveryBlock->innerText()); + $deliveryText = preg_replace('/\s+/', ' ', $deliveryText); + } elseif ($pickupBlock && $pickupBlock->nextNonWhitespaceSibling()) { + $pickupBlock = $pickupBlock->nextNonWhitespaceSibling(); + $deliveryText = "Самовывоз: " . strip_tags($pickupBlock->innerText()); + $deliveryText = preg_replace('/\s+/', ' ', $deliveryText); + } + + if ($deliveryText) { + Yii::warning('Текст заказа: ' . $deliveryText, __METHOD__); + $orderDetails['delivery'] = trim($deliveryText); + } + $commentBlock = $main->findOne('p:contains("Комментарий")'); + if ($commentBlock && $commentBlock->nextNonWhitespaceSibling()) { + $commentBlock = $commentBlock->nextNonWhitespaceSibling(); + $commentText = preg_replace('/\s+/', ' ', $commentBlock->innerText()); + $orderDetails['comment'] = trim($commentText); + } + + $clientBlock = $main->findOne('p:contains("Клиент")'); + $senderBlock = $main->findOne('p:contains("Отправитель")'); - $html = $message['body']; - $orderNumber = null; - $items = []; + if ($clientBlock && $clientBlock->nextNonWhitespaceSibling()) { + $clientBlock = $clientBlock->nextNonWhitespaceSibling(); + $clientText = "Клиент: " . strip_tags($clientBlock->innerText()); + $phoneLink = $clientBlock->find('a', 0); + if ($phoneLink) { + $clientText .= ' ' . preg_replace('/tel:/', ' ', $phoneLink->getAttribute('href')); + } + } elseif ($senderBlock && $senderBlock->nextNonWhitespaceSibling()) { + $senderBlock = $senderBlock->nextNonWhitespaceSibling(); + $clientText = "Отправитель: " . strip_tags($senderBlock->innerText()); + $phoneLink = $senderBlock->find('a', 0); + if ($phoneLink) { + $clientText .= ' ' . preg_replace('/tel:/', ' ', $phoneLink->getAttribute('href')); + } + } + + if ($clientText) { + $orderDetails['client'] = str_replace('Позвонить', '', $clientText); + } + + $recipientBlock = $main->findOne('p:contains("Получатель")'); + if ($recipientBlock && $recipientBlock->nextNonWhitespaceSibling()) { + $recipientBlock = $recipientBlock->nextNonWhitespaceSibling(); + $recipientText = strip_tags( + str_replace('Позвонить', '', $recipientBlock->innerText()) + ) . ' ' . preg_replace('/tel:/', ' ', $recipientBlock->find('a', 0)->getAttribute('href')); + $orderDetails['recipient'] = $recipientText; + } + + + $itemsBlock = $main->findOneOrFalse('h2:contains("Детали заказа")') ? $main->findOne('h2:contains("Детали заказа")') : $main->findOneOrFalse('p:contains("Детали заказа")'); + if ($itemsBlock) { + $itemsTable = $itemsBlock->parentNode()->find('table', 2); + + $itemsRows = $itemsTable->find('tr'); + foreach ($itemsRows as $itemsRow) { + Yii::warning('Строка заказа: ' . json_encode($itemsRow->innerText(), JSON_UNESCAPED_UNICODE), __METHOD__); + $itemData = [ + 'name' => '', + 'count' => '', + 'price' => '', + ]; + + + // Извлекаем название и количество из второго + $tds = $itemsRow->find('td'); + if (count($tds) >= 2) { + $itemData['name'] = trim(str_replace("\u{00A0}", ' ', strip_tags(preg_replace('/\s+/', ' ',$tds[1]->find('p', 0)->innerText())))); + $itemData['count'] = trim(str_replace(["\u{00A0}", 'шт.'], '', strip_tags(preg_replace('/\s+/', '', $tds[1]->find('p', 1)->innerText())))); + } + + // Извлекаем цену из третьего + if (count($tds) >= 3) { + $itemData['price'] = (float)trim(str_replace(["\u{00A0}", '₽' , ' '], '', strip_tags(preg_replace('/\s+/', ' ', $tds[2]->find('p', 0)->innerText())))); + } + + + // Добавляем данные в массив + $orderItems[] = $itemData; + } $totalSum = 0; - if (!empty($html)) { - $html = html_entity_decode($html, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + $sumBlock = $itemsBlock->parentNode()->find('table', 3); + $sumRow = $sumBlock->find('tr'); + + $sumTds = $sumRow->find('td'); + if (count($sumTds) >= 2) { + $totalSum = (float)trim(str_replace(["\u{00A0}", '₽' , ' '], '', strip_tags(preg_replace('/\s+/', ' ', $sumTds[1]->innerText())))); + } + $orderDetails['items'] = $orderItems; + $orderDetails['totalSum'] = $totalSum; } + + $order[$orderNumber] = $orderDetails; + return $order; } + return []; } + public static function processFlowwowOrders(array $allOrders) { $statuses = MarketplaceOrderStatusTypes::find() @@ -1452,7 +1595,6 @@ class MarketplaceService $statusId = self::getOrCreateStatus($statusCode, $statuses, $statusCodes); $substatusId = self::getOrCreateStatus($substatusCode, $statuses, $statusCodes); - $marketplaceOrder = MarketplaceOrders::find() ->where(['marketplace_order_id' => (string)$number]) ->one(); @@ -1567,7 +1709,6 @@ class MarketplaceService 'Ошибка сохранения элеиента: ' . json_encode($orderItem->getErrors(), JSON_UNESCAPED_UNICODE) ); } - } } diff --git a/erp24/views/marketplace-store/_form.php b/erp24/views/marketplace-store/_form.php index d10ffe70..91e3a012 100644 --- a/erp24/views/marketplace-store/_form.php +++ b/erp24/views/marketplace-store/_form.php @@ -29,6 +29,7 @@ use yii\widgets\ActiveForm; ]) ?> field($model, 'warehouse_guid')->textInput(['type' => 'integer']) ?> + field($model, 'account_email')->textInput() ?> field($model, 'firm')->dropDownList($firms, ['prompt' => 'Выберите юр лицо']) ?>
diff --git a/erp24/views/marketplace-store/index.php b/erp24/views/marketplace-store/index.php index 5284725a..55f28de2 100644 --- a/erp24/views/marketplace-store/index.php +++ b/erp24/views/marketplace-store/index.php @@ -36,6 +36,7 @@ $this->params['breadcrumbs'][] = $this->title; } ], 'warehouse_guid', + 'account_email', [ 'attribute' => 'firm', 'value' => function ($model) { diff --git a/erp24/views/marketplace-store/view.php b/erp24/views/marketplace-store/view.php index 27bb02d9..8d7c5424 100644 --- a/erp24/views/marketplace-store/view.php +++ b/erp24/views/marketplace-store/view.php @@ -43,6 +43,10 @@ $this->params['breadcrumbs'][] = $this->title; GUID склада в данном маркетплейсе warehouse_guid) ?> + + Адрес электронной почты уведомлений для магазина + account_email) ?> + Юридическое Лицо firm]) ?> -- 2.39.5