<?php
namespace app\controllers;
+
+use voku\helper\HtmlDomParser;
use Yii;
use yii\web\Controller;
use yii_app\services\MarketplaceService;
}
//$inboxInfo = imap_mailboxmsginfo($inbox);
//Yii::warning(' Состояние ящика: ' . print_r($inboxInfo, true), __METHOD__);
- $emails = imap_search($inbox, 'SINCE "01-Feb-2025" FROM "info@flowwow.com" ');
+ $emails = imap_search($inbox, 'ON "14-Feb-2025" FROM "info@flowwow.com" ');
$count = $emails ? count($emails) : 0;
+ $orders = [];
if ($emails) {
foreach ($emails as $email_number) {
$overview = imap_fetch_overview($inbox, $email_number, 0);
+
$structure = imap_fetchstructure($inbox, $email_number);
$htmlMessage = '';
$subject = mb_decode_mimeheader($overview[0]->subject);
- Yii::warning('Тема: ' . $subject, __METHOD__);
- if ($subject == 'Новый_оплаченный_заказ') {
- $message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 1.1));
-
- Yii::warning('Тема: ' . print_r($overview[0]->subject, true), __METHOD__);
- Yii::warning('Cообщение: ' . print_r($message, true), __METHOD__);
+ $date = mb_decode_mimeheader($overview[0]->date);
+ $date = date('Y-m-d H:i:s', strtotime($date));
+ if ($subject == 'Новый_оплаченный_заказ') {
+ if (isset($structure->parts) && count($structure->parts)) {
+ foreach ($structure->parts as $partNum => $part) {
+ if ($part->subtype == 'HTML') {
+ $htmlMessage = imap_fetchbody($inbox, $email_number, $partNum + 1);
+ $htmlMessage = quoted_printable_decode($htmlMessage);
+ break;
+ }
+ }
+ }
+
+ // Инициализация переменных для избежания undefined
+ $orderNumber = null;
+ $items = [];
+ $totalSum = 0;
+
+ try {
+ if (!empty($htmlMessage)) {
+ $htmlMessage = html_entity_decode($htmlMessage, ENT_QUOTES | ENT_HTML5, 'UTF-8');
+
+ $doc = new HtmlDomParser($htmlMessage);
+
+ $main = $doc->findOneOrFalse("body");
+
+ if ($main !== false) {
+ $orderTitleNode = $main->findOne("h1");
+
+ if ($orderTitleNode && preg_match('/№(\d+)/', $orderTitleNode->innertext, $matches)) {
+ $orderNumber = (int)$matches[1];
+ }
+
+ // Извлечение товаров
+ $elements = $main->findOne("p:contains('Детали заказа')")->parentNode();
+ if ($elements) {
+ foreach ($elements->findMultiOrFalse("tr") as $itemRow) {
+ $img = $itemRow->findOne("td img");
+ $nameNode = $itemRow->findOne("td:nth-child(2) p");
+ if (!$img || !trim($nameNode->plaintext)) continue;
+
+ $name = trim($nameNode->plaintext);
+
+ // Количество
+ $countNode = $itemRow->findOne("td:nth-child(2) p[style*='#8C8C8C']");
+ $countText = $countNode ? $countNode->plaintext : '1';
+ $count = (int) filter_var($countText, FILTER_SANITIZE_NUMBER_INT);
+
+ // Сумма
+ $sumNode = $itemRow->findOne("td:nth-child(3) p");
+ $sumText = $sumNode ? $sumNode->plaintext : '0';
+ $sum = (float) str_replace([' ', '₽'], '', $sumText);
+
+ $items[] = ['name' => $name, 'count' => $count, 'sum' => $sum];
+ }
+ }
+
+ // Общая сумма
+ $totalSumNodes = $elements->parentNode()->getElementsByTagName('table');
+ $totalSumNode = ($totalSumNodes[3])->findOne("tr:contains('Итого оплачено')");
+ if ($totalSumNode) {
+ $totalSum = (float) str_replace([' ', '₽'], '', trim(($totalSumNode->findMulti("td"))[1]->plaintext));
+ }
+ }
+ }
+ } catch (\Exception $e) {
+ Yii::error('Ошибка: ' . json_encode($e->getMessage(), JSON_UNESCAPED_UNICODE) . " " . $e->getLine() . " " . json_encode($e->getTrace(), JSON_UNESCAPED_UNICODE), __METHOD__);
+ }
+
+ if ($orderNumber !== null) {
+ $orders[] = [
+ $orderNumber => [
+ 'items' => $items,
+ 'totalSum' => $totalSum,
+ 'number' => $orderNumber,
+ 'date' => $date,
+ ]
+ ];
+ }
+
+ Yii::warning('HTML Тело письма: ' . json_encode($orders, JSON_UNESCAPED_UNICODE), __METHOD__);
+ break;
}
// imap_setflag_full($inbox, $email_number, "\\Seen");