Files
orderPRO/archive/2026-03-02_users-only-reset/src/Modules/Settings/OrderStatusMappingRepository.php
Jacek Pyziak c489891d15 Add Orders and Order Status repositories with pagination and management features
- Implemented OrdersRepository for handling order data with pagination, filtering, and sorting capabilities.
- Added methods for retrieving order status options, quick stats, and detailed order information.
- Created OrderStatusRepository for managing order status groups and statuses, including CRUD operations and sorting.
- Introduced a bootstrap file for test environment setup and autoloading.
2026-03-03 01:32:28 +01:00

126 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Settings;
use PDO;
final class OrderStatusMappingRepository
{
public function __construct(private readonly PDO $pdo)
{
}
/**
* @return array<string, array{orderpro_status_code:string, shoppro_status_name:string|null}>
*/
public function listByIntegration(int $integrationId): array
{
$stmt = $this->pdo->prepare(
'SELECT shoppro_status_code, shoppro_status_name, orderpro_status_code
FROM order_status_mappings
WHERE integration_id = :integration_id
ORDER BY shoppro_status_code ASC'
);
$stmt->execute(['integration_id' => $integrationId]);
$rows = $stmt->fetchAll();
if (!is_array($rows)) {
return [];
}
$result = [];
foreach ($rows as $row) {
if (!is_array($row)) {
continue;
}
$code = trim((string) ($row['shoppro_status_code'] ?? ''));
if ($code === '') {
continue;
}
$result[$code] = [
'orderpro_status_code' => trim((string) ($row['orderpro_status_code'] ?? '')),
'shoppro_status_name' => isset($row['shoppro_status_name']) ? trim((string) $row['shoppro_status_name']) : null,
];
}
return $result;
}
/**
* @param array<int, array{shoppro_status_code:string,shoppro_status_name:string|null,orderpro_status_code:string}> $mappings
*/
public function replaceForIntegration(int $integrationId, array $mappings): void
{
$deleteStmt = $this->pdo->prepare('DELETE FROM order_status_mappings WHERE integration_id = :integration_id');
$deleteStmt->execute(['integration_id' => $integrationId]);
if ($mappings === []) {
return;
}
$insertStmt = $this->pdo->prepare(
'INSERT INTO order_status_mappings (
integration_id, shoppro_status_code, shoppro_status_name, orderpro_status_code, created_at, updated_at
) VALUES (
:integration_id, :shoppro_status_code, :shoppro_status_name, :orderpro_status_code, :created_at, :updated_at
)'
);
$now = date('Y-m-d H:i:s');
foreach ($mappings as $mapping) {
$shopCode = trim((string) ($mapping['shoppro_status_code'] ?? ''));
$orderCode = trim((string) ($mapping['orderpro_status_code'] ?? ''));
if ($shopCode === '' || $orderCode === '') {
continue;
}
$shopNameRaw = isset($mapping['shoppro_status_name']) ? trim((string) $mapping['shoppro_status_name']) : '';
$shopName = $shopNameRaw === '' ? null : $shopNameRaw;
$insertStmt->execute([
'integration_id' => $integrationId,
'shoppro_status_code' => $shopCode,
'shoppro_status_name' => $shopName,
'orderpro_status_code' => $orderCode,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
/**
* @return array<string, string>
*/
public function listOrderProToShopProMap(int $integrationId): array
{
$rows = $this->listByIntegration($integrationId);
if ($rows === []) {
return [];
}
$result = [];
foreach ($rows as $shopCode => $mapping) {
$orderProCode = trim((string) ($mapping['orderpro_status_code'] ?? ''));
$normalizedOrderProCode = $this->normalizeCode($orderProCode);
$normalizedShopCode = $this->normalizeCode((string) $shopCode);
if ($normalizedOrderProCode === '' || $normalizedShopCode === '') {
continue;
}
if (!isset($result[$normalizedOrderProCode])) {
$result[$normalizedOrderProCode] = $normalizedShopCode;
}
}
return $result;
}
private function normalizeCode(string $value): string
{
return trim(mb_strtolower($value));
}
}