This commit is contained in:
2026-04-07 20:32:43 +02:00
parent 1933c74395
commit 8fa9ca6439
45 changed files with 2974 additions and 3382 deletions

View File

@@ -38,6 +38,17 @@ final class OrderImportRepository
$sourceOrderId = trim((string) ($orderData['source_order_id'] ?? ''));
$existingOrderId = $this->findOrderIdBySource($source, $sourceOrderId);
$created = $existingOrderId === null;
$paymentTransition = false;
if (!$created) {
$currentStatus = $this->getCurrentStatus($existingOrderId);
$newPaymentStatus = (int) ($orderData['payment_status'] ?? 0);
$paymentTransition = $currentStatus === 'nieoplacone' && $newPaymentStatus === 2;
if (!$paymentTransition) {
$orderData['external_status_id'] = $currentStatus;
}
}
$orderId = $created
? $this->insertOrder($orderData)
: $this->updateOrder($existingOrderId, $orderData);
@@ -50,6 +61,8 @@ final class OrderImportRepository
$this->replacePayments($orderId, $payments);
$this->replaceShipments($orderId, $shipments);
$this->replaceStatusHistory($orderId, $statusHistory);
} elseif ($paymentTransition) {
$this->replacePayments($orderId, $payments);
}
$this->pdo->commit();
@@ -63,6 +76,7 @@ final class OrderImportRepository
return [
'order_id' => $orderId,
'created' => $created,
'payment_transition' => $paymentTransition,
];
}
@@ -87,6 +101,17 @@ final class OrderImportRepository
return $id > 0 ? $id : null;
}
private function getCurrentStatus(int $orderId): string
{
$statement = $this->pdo->prepare(
'SELECT external_status_id FROM orders WHERE id = :id LIMIT 1'
);
$statement->execute(['id' => $orderId]);
$value = $statement->fetchColumn();
return strtolower(trim((string) ($value ?: '')));
}
/**
* @param array<string, mixed> $orderData
*/

View File

@@ -340,7 +340,7 @@ final class OrdersController
$totalWithTax = $row['total_with_tax'] !== null ? number_format((float) $row['total_with_tax'], 2, '.', ' ') : '-';
$totalPaid = $row['total_paid'] !== null ? number_format((float) $row['total_paid'], 2, '.', ' ') : '-';
$paymentType = strtoupper(trim((string) ($row['external_payment_type_id'] ?? '')));
$isCod = $paymentType === 'CASH_ON_DELIVERY';
$isCod = StringHelper::isCodPayment($paymentType);
$paymentStatus = isset($row['payment_status']) ? (int) $row['payment_status'] : null;
$isUnpaid = !$isCod && $paymentStatus === 0;
$itemsCount = max(0, (int) ($row['items_count'] ?? 0));
@@ -661,7 +661,7 @@ final class OrdersController
$html .= '<div class="orders-product">'
. $thumb
. '<div class="orders-product__txt">'
. '<div class="orders-product__name">' . htmlspecialchars($name !== '' ? $name : '-', ENT_QUOTES, 'UTF-8') . '</div>'
. '<div class="orders-product__name"' . ($name !== '' ? ' title="' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '"' : '') . '>' . htmlspecialchars($name !== '' ? $name : '-', ENT_QUOTES, 'UTF-8') . '</div>'
. '<div class="orders-product__qty">' . htmlspecialchars($qty, ENT_QUOTES, 'UTF-8') . ' szt.</div>'
. '</div>'
. '</div>';
@@ -914,4 +914,17 @@ final class OrdersController
}
}
public function quickSearch(Request $request): Response
{
$query = trim((string) $request->input('q', ''));
if ($query === '' || mb_strlen($query) < 2) {
return Response::json(['results' => []]);
}
$limit = min((int) $request->input('limit', 10), 20);
$results = $this->orders->quickSearch($query, $limit);
return Response::json(['results' => $results]);
}
}

View File

@@ -987,4 +987,60 @@ final class OrdersRepository
return $code;
}
/**
* @return array<int, array{id:int, order_number:string, buyer_name:string, buyer_email:string, buyer_phone:string}>
*/
public function quickSearch(string $query, int $limit = 10): array
{
$query = trim($query);
if ($query === '' || mb_strlen($query) < 2) {
return [];
}
$limit = max(1, min($limit, 20));
$searchVal = '%' . $query . '%';
$sql = 'SELECT o.id, o.source_order_id, o.external_order_id, '
. 'a.name AS buyer_name, a.email AS buyer_email, a.phone AS buyer_phone '
. 'FROM orders o '
. 'LEFT JOIN order_addresses a ON a.order_id = o.id AND a.address_type = "customer" '
. 'WHERE (o.source_order_id LIKE :s1 OR o.external_order_id LIKE :s2 '
. 'OR a.name LIKE :s3 OR a.email LIKE :s4 OR a.phone LIKE :s5 '
. 'OR EXISTS (SELECT 1 FROM order_items oi WHERE oi.order_id = o.id AND oi.original_name LIKE :s6)) '
. 'ORDER BY o.ordered_at DESC LIMIT :lim';
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':s1', $searchVal);
$stmt->bindValue(':s2', $searchVal);
$stmt->bindValue(':s3', $searchVal);
$stmt->bindValue(':s4', $searchVal);
$stmt->bindValue(':s5', $searchVal);
$stmt->bindValue(':s6', $searchVal);
$stmt->bindValue(':lim', $limit, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll();
if (!is_array($rows)) {
return [];
}
return array_map(static function (array $row): array {
$orderNumber = ((string) ($row['source_order_id'] ?? '')) !== ''
? (string) $row['source_order_id']
: (string) ($row['external_order_id'] ?? '');
return [
'id' => (int) ($row['id'] ?? 0),
'order_number' => $orderNumber,
'buyer_name' => (string) ($row['buyer_name'] ?? ''),
'buyer_email' => (string) ($row['buyer_email'] ?? ''),
'buyer_phone' => (string) ($row['buyer_phone'] ?? ''),
];
}, $rows);
} catch (Throwable) {
return [];
}
}
}