update
This commit is contained in:
@@ -96,6 +96,7 @@ final class AllegroIntegrationController
|
||||
'statusSyncIntervalMinutes' => $statusSyncIntervalMinutes,
|
||||
'statusMappings' => $this->statusMappings->listMappings(),
|
||||
'orderproStatuses' => $this->orderStatuses->listStatuses(),
|
||||
'allegroStatuses' => $this->statusMappings->listExternalStatuses(),
|
||||
'defaultRedirectUri' => $defaultRedirectUri,
|
||||
'errorMessage' => (string) Flash::get('settings_error', ''),
|
||||
'successMessage' => (string) Flash::get('settings_success', ''),
|
||||
|
||||
@@ -23,37 +23,6 @@ final class AllegroStatusMappingController
|
||||
|
||||
public function saveStatusMapping(Request $request): Response
|
||||
{
|
||||
$csrfError = $this->validateCsrf((string) $request->input('_token', ''));
|
||||
if ($csrfError !== null) {
|
||||
return $csrfError;
|
||||
}
|
||||
|
||||
$allegroStatusCode = strtolower(trim((string) $request->input('allegro_status_code', '')));
|
||||
$orderproStatusCode = strtolower(trim((string) $request->input('orderpro_status_code', '')));
|
||||
$allegroStatusName = trim((string) $request->input('allegro_status_name', ''));
|
||||
|
||||
if ($allegroStatusCode === '') {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.allegro_status_required'));
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
if ($orderproStatusCode === '') {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.orderpro_status_required'));
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
if (!$this->orderStatusCodeExists($orderproStatusCode)) {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.orderpro_status_not_found'));
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->statusMappings->upsertMapping($allegroStatusCode, $allegroStatusName !== '' ? $allegroStatusName : null, $orderproStatusCode);
|
||||
Flash::set('settings_success', $this->translator->get('settings.allegro.statuses.flash.saved'));
|
||||
} catch (Throwable $exception) {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.save_failed') . ' ' . $exception->getMessage());
|
||||
}
|
||||
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
@@ -64,37 +33,39 @@ final class AllegroStatusMappingController
|
||||
return $csrfError;
|
||||
}
|
||||
|
||||
$codes = $request->input('allegro_status_code', []);
|
||||
$names = $request->input('allegro_status_name', []);
|
||||
$selectedOrderproCodes = $request->input('orderpro_status_code', []);
|
||||
if (!is_array($codes) || !is_array($names) || !is_array($selectedOrderproCodes)) {
|
||||
$orderproCodes = $request->input('orderpro_status_code', []);
|
||||
$allegroCodes = $request->input('allegro_status_code', []);
|
||||
$allegroNames = $request->input('allegro_status_name', []);
|
||||
if (!is_array($orderproCodes) || !is_array($allegroCodes) || !is_array($allegroNames)) {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.save_failed'));
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
try {
|
||||
foreach ($codes as $index => $rawCode) {
|
||||
$allegroStatusCode = strtolower(trim((string) $rawCode));
|
||||
if ($allegroStatusCode === '') {
|
||||
continue;
|
||||
}
|
||||
$allowedOrderpro = $this->resolveAllowedOrderproStatusCodes();
|
||||
$mappings = [];
|
||||
|
||||
$allegroStatusName = trim((string) ($names[$index] ?? ''));
|
||||
$orderproStatusCodeRaw = strtolower(trim((string) ($selectedOrderproCodes[$index] ?? '')));
|
||||
$orderproStatusCode = $orderproStatusCodeRaw !== '' ? $orderproStatusCodeRaw : null;
|
||||
|
||||
if ($orderproStatusCode !== null && !$this->orderStatusCodeExists($orderproStatusCode)) {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.orderpro_status_not_found'));
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
$this->statusMappings->upsertMapping(
|
||||
$allegroStatusCode,
|
||||
$allegroStatusName !== '' ? $allegroStatusName : null,
|
||||
$orderproStatusCode
|
||||
);
|
||||
foreach ($orderproCodes as $index => $rawOrderproCode) {
|
||||
$orderproCode = strtolower(trim((string) $rawOrderproCode));
|
||||
if ($orderproCode === '' || !isset($allowedOrderpro[$orderproCode])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$allegroCode = strtolower(trim((string) ($allegroCodes[$index] ?? '')));
|
||||
if ($allegroCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$allegroName = trim((string) ($allegroNames[$index] ?? ''));
|
||||
|
||||
$mappings[] = [
|
||||
'orderpro_status_code' => $orderproCode,
|
||||
'allegro_status_code' => $allegroCode,
|
||||
'allegro_status_name' => $allegroName,
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
$this->statusMappings->replaceAllMappings($mappings);
|
||||
Flash::set('settings_success', $this->translator->get('settings.allegro.statuses.flash.saved_bulk'));
|
||||
} catch (Throwable $exception) {
|
||||
Flash::set('settings_error', $this->translator->get('settings.allegro.statuses.flash.save_failed') . ' ' . $exception->getMessage());
|
||||
@@ -149,21 +120,20 @@ final class AllegroStatusMappingController
|
||||
return Response::redirect(RedirectPaths::ALLEGRO_STATUSES_TAB);
|
||||
}
|
||||
|
||||
private function orderStatusCodeExists(string $code): bool
|
||||
/**
|
||||
* @return array<string, true>
|
||||
*/
|
||||
private function resolveAllowedOrderproStatusCodes(): array
|
||||
{
|
||||
$needle = strtolower(trim($code));
|
||||
if ($needle === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$allowed = [];
|
||||
foreach ($this->orderStatuses->listStatuses() as $row) {
|
||||
$statusCode = strtolower(trim((string) ($row['code'] ?? '')));
|
||||
if ($statusCode === $needle) {
|
||||
return true;
|
||||
$code = strtolower(trim((string) ($row['code'] ?? '')));
|
||||
if ($code !== '') {
|
||||
$allowed[$code] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return $allowed;
|
||||
}
|
||||
|
||||
private function validateCsrf(string $token): ?Response
|
||||
|
||||
@@ -20,7 +20,7 @@ final class AllegroStatusMappingRepository
|
||||
$statement = $this->pdo->query(
|
||||
'SELECT id, allegro_status_code, allegro_status_name, orderpro_status_code, created_at, updated_at
|
||||
FROM allegro_order_status_mappings
|
||||
ORDER BY allegro_status_code ASC'
|
||||
ORDER BY orderpro_status_code ASC, allegro_status_code ASC'
|
||||
);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (!is_array($rows)) {
|
||||
@@ -39,29 +39,67 @@ final class AllegroStatusMappingRepository
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
public function upsertMapping(string $allegroStatusCode, ?string $allegroStatusName, ?string $orderproStatusCode): void
|
||||
/**
|
||||
* @return array<int, array{code:string,name:string}>
|
||||
*/
|
||||
public function listExternalStatuses(): array
|
||||
{
|
||||
$code = strtolower(trim($allegroStatusCode));
|
||||
$orderproCode = $orderproStatusCode !== null ? strtolower(trim($orderproStatusCode)) : null;
|
||||
if ($code === '') {
|
||||
$statement = $this->pdo->query(
|
||||
'SELECT DISTINCT allegro_status_code, allegro_status_name
|
||||
FROM allegro_order_status_mappings
|
||||
WHERE allegro_status_code IS NOT NULL
|
||||
AND allegro_status_code <> ""
|
||||
ORDER BY allegro_status_code ASC'
|
||||
);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$code = strtolower(trim((string) ($row['allegro_status_code'] ?? '')));
|
||||
if ($code === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'code' => $code,
|
||||
'name' => trim((string) ($row['allegro_status_name'] ?? $code)),
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function upsertMapping(string $orderproStatusCode, ?string $allegroStatusCode, ?string $allegroStatusName): void
|
||||
{
|
||||
$orderproCode = strtolower(trim($orderproStatusCode));
|
||||
if ($orderproCode === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$allegroCode = $allegroStatusCode !== null ? strtolower(trim($allegroStatusCode)) : null;
|
||||
|
||||
$statement = $this->pdo->prepare(
|
||||
'INSERT INTO allegro_order_status_mappings (
|
||||
allegro_status_code, allegro_status_name, orderpro_status_code, created_at, updated_at
|
||||
orderpro_status_code, allegro_status_code, allegro_status_name, created_at, updated_at
|
||||
) VALUES (
|
||||
:allegro_status_code, :allegro_status_name, :orderpro_status_code, NOW(), NOW()
|
||||
:orderpro_status_code, :allegro_status_code, :allegro_status_name, NOW(), NOW()
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
allegro_status_code = VALUES(allegro_status_code),
|
||||
allegro_status_name = VALUES(allegro_status_name),
|
||||
orderpro_status_code = VALUES(orderpro_status_code),
|
||||
updated_at = VALUES(updated_at)'
|
||||
);
|
||||
$statement->execute([
|
||||
'allegro_status_code' => $code,
|
||||
'orderpro_status_code' => $orderproCode,
|
||||
'allegro_status_code' => $allegroCode !== null && $allegroCode !== '' ? $allegroCode : null,
|
||||
'allegro_status_name' => StringHelper::nullableString((string) $allegroStatusName),
|
||||
'orderpro_status_code' => $orderproCode !== null && $orderproCode !== '' ? $orderproCode : null,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -72,20 +110,41 @@ final class AllegroStatusMappingRepository
|
||||
return;
|
||||
}
|
||||
|
||||
$statement = $this->pdo->prepare(
|
||||
$existing = $this->pdo->prepare(
|
||||
'SELECT id FROM allegro_order_status_mappings
|
||||
WHERE allegro_status_code = :allegro_status_code
|
||||
LIMIT 1'
|
||||
);
|
||||
$existing->execute(['allegro_status_code' => $code]);
|
||||
|
||||
if ($existing->fetchColumn() !== false) {
|
||||
$update = $this->pdo->prepare(
|
||||
'UPDATE allegro_order_status_mappings
|
||||
SET allegro_status_name = CASE
|
||||
WHEN :allegro_status_name IS NULL OR :allegro_status_name2 = "" THEN allegro_status_name
|
||||
ELSE :allegro_status_name3
|
||||
END,
|
||||
updated_at = NOW()
|
||||
WHERE allegro_status_code = :allegro_status_code'
|
||||
);
|
||||
$name = StringHelper::nullableString((string) $allegroStatusName);
|
||||
$update->execute([
|
||||
'allegro_status_name' => $name,
|
||||
'allegro_status_name2' => (string) $name,
|
||||
'allegro_status_name3' => $name,
|
||||
'allegro_status_code' => $code,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$insert = $this->pdo->prepare(
|
||||
'INSERT INTO allegro_order_status_mappings (
|
||||
allegro_status_code, allegro_status_name, orderpro_status_code, created_at, updated_at
|
||||
) VALUES (
|
||||
:allegro_status_code, :allegro_status_name, NULL, NOW(), NOW()
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
allegro_status_name = CASE
|
||||
WHEN VALUES(allegro_status_name) IS NULL OR VALUES(allegro_status_name) = "" THEN allegro_status_name
|
||||
ELSE VALUES(allegro_status_name)
|
||||
END,
|
||||
updated_at = VALUES(updated_at)'
|
||||
)'
|
||||
);
|
||||
$statement->execute([
|
||||
$insert->execute([
|
||||
'allegro_status_code' => $code,
|
||||
'allegro_status_name' => StringHelper::nullableString((string) $allegroStatusName),
|
||||
]);
|
||||
@@ -112,6 +171,8 @@ final class AllegroStatusMappingRepository
|
||||
'SELECT orderpro_status_code
|
||||
FROM allegro_order_status_mappings
|
||||
WHERE allegro_status_code = :allegro_status_code
|
||||
AND orderpro_status_code IS NOT NULL
|
||||
AND orderpro_status_code <> ""
|
||||
LIMIT 1'
|
||||
);
|
||||
$statement->execute(['allegro_status_code' => $code]);
|
||||
@@ -130,10 +191,12 @@ final class AllegroStatusMappingRepository
|
||||
public function buildOrderproToAllegroMap(): array
|
||||
{
|
||||
$statement = $this->pdo->query(
|
||||
'SELECT allegro_status_code, orderpro_status_code
|
||||
'SELECT orderpro_status_code, allegro_status_code
|
||||
FROM allegro_order_status_mappings
|
||||
WHERE orderpro_status_code IS NOT NULL
|
||||
AND orderpro_status_code <> ""
|
||||
AND allegro_status_code IS NOT NULL
|
||||
AND allegro_status_code <> ""
|
||||
ORDER BY id ASC'
|
||||
);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
@@ -157,4 +220,74 @@ final class AllegroStatusMappingRepository
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string> allegro_status_code => orderpro_status_code
|
||||
*/
|
||||
public function buildAllegroToOrderproMap(): array
|
||||
{
|
||||
$statement = $this->pdo->query(
|
||||
'SELECT allegro_status_code, orderpro_status_code
|
||||
FROM allegro_order_status_mappings
|
||||
WHERE allegro_status_code IS NOT NULL
|
||||
AND allegro_status_code <> ""
|
||||
AND orderpro_status_code IS NOT NULL
|
||||
AND orderpro_status_code <> ""
|
||||
ORDER BY id ASC'
|
||||
);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$map = [];
|
||||
foreach ($rows as $row) {
|
||||
$allegroCode = strtolower(trim((string) ($row['allegro_status_code'] ?? '')));
|
||||
$orderproCode = strtolower(trim((string) ($row['orderpro_status_code'] ?? '')));
|
||||
if ($allegroCode === '' || $orderproCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($map[$allegroCode])) {
|
||||
$map[$allegroCode] = $orderproCode;
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{orderpro_status_code:string,allegro_status_code:string,allegro_status_name:string}> $mappings
|
||||
*/
|
||||
public function replaceAllMappings(array $mappings): void
|
||||
{
|
||||
$this->pdo->exec(
|
||||
'DELETE FROM allegro_order_status_mappings WHERE orderpro_status_code IS NOT NULL AND orderpro_status_code <> ""'
|
||||
);
|
||||
|
||||
if ($mappings === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$insertStatement = $this->pdo->prepare(
|
||||
'INSERT INTO allegro_order_status_mappings (
|
||||
orderpro_status_code, allegro_status_code, allegro_status_name, created_at, updated_at
|
||||
) VALUES (
|
||||
:orderpro_status_code, :allegro_status_code, :allegro_status_name, NOW(), NOW()
|
||||
)'
|
||||
);
|
||||
|
||||
foreach ($mappings as $mapping) {
|
||||
$orderproCode = strtolower(trim((string) ($mapping['orderpro_status_code'] ?? '')));
|
||||
$allegroCode = strtolower(trim((string) ($mapping['allegro_status_code'] ?? '')));
|
||||
if ($orderproCode === '' || $allegroCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$insertStatement->execute([
|
||||
'orderpro_status_code' => $orderproCode,
|
||||
'allegro_status_code' => $allegroCode,
|
||||
'allegro_status_name' => trim((string) ($mapping['allegro_status_name'] ?? '')) ?: null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,9 +66,13 @@ final class ShopproIntegrationsController
|
||||
$this->ensureStatusSyncScheduleExists();
|
||||
$this->ensurePaymentSyncScheduleExists();
|
||||
$activeTab = $this->resolveTab((string) $request->input('tab', 'integration'));
|
||||
$integrationId = $selectedIntegration !== null ? (int) ($selectedIntegration['id'] ?? 0) : 0;
|
||||
$discoveredStatuses = $this->readDiscoveredStatuses();
|
||||
$statusRows = $selectedIntegration !== null
|
||||
? $this->buildStatusRows((int) ($selectedIntegration['id'] ?? 0), $discoveredStatuses)
|
||||
$mappingIndex = $integrationId > 0
|
||||
? $this->buildMappingIndex($integrationId)
|
||||
: [];
|
||||
$shopproStatuses = $integrationId > 0
|
||||
? $this->buildExternalStatusOptions($integrationId, $discoveredStatuses)
|
||||
: [];
|
||||
$deliveryServicesData = $activeTab === 'delivery'
|
||||
? $this->loadDeliveryServices()
|
||||
@@ -93,8 +97,9 @@ final class ShopproIntegrationsController
|
||||
'ordersImportIntervalMinutes' => $this->currentImportIntervalMinutes(),
|
||||
'statusSyncIntervalMinutes' => $this->currentStatusSyncIntervalMinutes(),
|
||||
'paymentSyncIntervalMinutes' => $this->currentPaymentSyncIntervalMinutes(),
|
||||
'statusRows' => $statusRows,
|
||||
'mappingIndex' => $mappingIndex,
|
||||
'orderproStatuses' => $this->orderStatuses->listStatuses(),
|
||||
'shopproStatuses' => $shopproStatuses,
|
||||
'deliveryMappings' => $deliveryMappings,
|
||||
'orderDeliveryMethods' => $orderDeliveryMethods,
|
||||
'allegroDeliveryServices' => $deliveryServicesData[0],
|
||||
@@ -224,38 +229,34 @@ final class ShopproIntegrationsController
|
||||
return $accessError;
|
||||
}
|
||||
|
||||
$orderCodes = $request->input('orderpro_status_code', []);
|
||||
$shopCodes = $request->input('shoppro_status_code', []);
|
||||
$shopNames = $request->input('shoppro_status_name', []);
|
||||
$orderCodes = $request->input('orderpro_status_code', []);
|
||||
if (!is_array($shopCodes) || !is_array($shopNames) || !is_array($orderCodes)) {
|
||||
if (!is_array($orderCodes) || !is_array($shopCodes) || !is_array($shopNames)) {
|
||||
Flash::set('settings_error', $this->translator->get('settings.integrations.statuses.flash.invalid_payload'));
|
||||
return Response::redirect($redirectTo);
|
||||
}
|
||||
|
||||
$allowedOrderpro = $this->resolveAllowedOrderproStatusCodes();
|
||||
$rowsCount = min(count($shopCodes), count($shopNames), count($orderCodes));
|
||||
$rowsCount = min(count($orderCodes), count($shopCodes), count($shopNames));
|
||||
$mappings = [];
|
||||
for ($index = 0; $index < $rowsCount; $index++) {
|
||||
$orderCode = strtolower(trim((string) ($orderCodes[$index] ?? '')));
|
||||
$shopCode = trim((string) ($shopCodes[$index] ?? ''));
|
||||
$shopName = trim((string) ($shopNames[$index] ?? ''));
|
||||
$orderCode = strtolower(trim((string) ($orderCodes[$index] ?? '')));
|
||||
|
||||
if ($orderCode === '' || !isset($allowedOrderpro[$orderCode])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($shopCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($orderCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($allowedOrderpro[$orderCode])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mappings[] = [
|
||||
'orderpro_status_code' => $orderCode,
|
||||
'shoppro_status_code' => $shopCode,
|
||||
'shoppro_status_name' => $shopName,
|
||||
'orderpro_status_code' => $orderCode,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -494,55 +495,68 @@ final class ShopproIntegrationsController
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{shoppro_status_code:string,shoppro_status_name:string,orderpro_status_code:string}>
|
||||
* @return array<string, array{shoppro_status_code:string,shoppro_status_name:string}>
|
||||
*/
|
||||
private function buildStatusRows(int $integrationId, array $discoveredStatuses): array
|
||||
private function buildMappingIndex(int $integrationId): array
|
||||
{
|
||||
$mappedRows = $this->statusMappings->listByIntegration($integrationId);
|
||||
$result = [];
|
||||
$rows = $this->statusMappings->listByIntegration($integrationId);
|
||||
$index = [];
|
||||
|
||||
foreach ($mappedRows as $row) {
|
||||
$code = trim((string) ($row['shoppro_status_code'] ?? ''));
|
||||
if ($code === '') {
|
||||
foreach ($rows as $row) {
|
||||
$orderproCode = strtolower(trim((string) ($row['orderpro_status_code'] ?? '')));
|
||||
if ($orderproCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = mb_strtolower($code);
|
||||
$result[$key] = [
|
||||
'shoppro_status_code' => $code,
|
||||
$index[$orderproCode] = [
|
||||
'shoppro_status_code' => trim((string) ($row['shoppro_status_code'] ?? '')),
|
||||
'shoppro_status_name' => trim((string) ($row['shoppro_status_name'] ?? '')),
|
||||
'orderpro_status_code' => strtolower(trim((string) ($row['orderpro_status_code'] ?? ''))),
|
||||
];
|
||||
}
|
||||
|
||||
return $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{code:string,name:string}> $discoveredStatuses
|
||||
* @return array<int, array{code:string,name:string}>
|
||||
*/
|
||||
private function buildExternalStatusOptions(int $integrationId, array $discoveredStatuses): array
|
||||
{
|
||||
$existing = $this->statusMappings->listExternalStatuses($integrationId);
|
||||
$seen = [];
|
||||
$result = [];
|
||||
|
||||
foreach ($existing as $status) {
|
||||
$code = strtolower(trim((string) ($status['code'] ?? '')));
|
||||
if ($code === '' || isset($seen[$code])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$code] = true;
|
||||
$result[] = $status;
|
||||
}
|
||||
|
||||
foreach ($discoveredStatuses as $status) {
|
||||
if (!is_array($status)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$code = trim((string) ($status['code'] ?? ''));
|
||||
if ($code === '') {
|
||||
$code = strtolower(trim((string) ($status['code'] ?? '')));
|
||||
if ($code === '' || isset($seen[$code])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = mb_strtolower($code);
|
||||
if (!isset($result[$key])) {
|
||||
$result[$key] = [
|
||||
'shoppro_status_code' => $code,
|
||||
'shoppro_status_name' => trim((string) ($status['name'] ?? '')),
|
||||
'orderpro_status_code' => '',
|
||||
];
|
||||
}
|
||||
$seen[$code] = true;
|
||||
$result[] = [
|
||||
'code' => $code,
|
||||
'name' => trim((string) ($status['name'] ?? $code)),
|
||||
];
|
||||
}
|
||||
|
||||
uasort($result, static function (array $left, array $right): int {
|
||||
return strcmp(
|
||||
strtolower((string) ($left['shoppro_status_code'] ?? '')),
|
||||
strtolower((string) ($right['shoppro_status_code'] ?? ''))
|
||||
);
|
||||
});
|
||||
usort($result, static fn (array $a, array $b): int => strcmp(
|
||||
strtolower((string) ($a['code'] ?? '')),
|
||||
strtolower((string) ($b['code'] ?? ''))
|
||||
));
|
||||
|
||||
return array_values($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -297,7 +297,7 @@ final class ShopproOrdersSyncService
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
* @return array<string, string> shoppro_status_code => orderpro_status_code (reverse of DB direction)
|
||||
*/
|
||||
private function buildStatusMap(int $integrationId): array
|
||||
{
|
||||
@@ -309,7 +309,9 @@ final class ShopproOrdersSyncService
|
||||
if ($shopCode === '' || $orderCode === '') {
|
||||
continue;
|
||||
}
|
||||
$map[$shopCode] = $orderCode;
|
||||
if (!isset($map[$shopCode])) {
|
||||
$map[$shopCode] = $orderCode;
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
|
||||
@@ -12,7 +12,7 @@ final class ShopproStatusMappingRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{shoppro_status_code:string,shoppro_status_name:string,orderpro_status_code:string}>
|
||||
* @return array<int, array{orderpro_status_code:string,shoppro_status_code:string,shoppro_status_name:string}>
|
||||
*/
|
||||
public function listByIntegration(int $integrationId): array
|
||||
{
|
||||
@@ -21,9 +21,53 @@ final class ShopproStatusMappingRepository
|
||||
}
|
||||
|
||||
$statement = $this->pdo->prepare(
|
||||
'SELECT shoppro_status_code, shoppro_status_name, orderpro_status_code
|
||||
'SELECT orderpro_status_code, shoppro_status_code, shoppro_status_name
|
||||
FROM order_status_mappings
|
||||
WHERE integration_id = :integration_id
|
||||
ORDER BY orderpro_status_code ASC'
|
||||
);
|
||||
$statement->execute(['integration_id' => $integrationId]);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($rows as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$orderproCode = strtolower(trim((string) ($row['orderpro_status_code'] ?? '')));
|
||||
if ($orderproCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'orderpro_status_code' => $orderproCode,
|
||||
'shoppro_status_code' => trim((string) ($row['shoppro_status_code'] ?? '')),
|
||||
'shoppro_status_name' => trim((string) ($row['shoppro_status_name'] ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{code:string,name:string}>
|
||||
*/
|
||||
public function listExternalStatuses(int $integrationId): array
|
||||
{
|
||||
if ($integrationId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$statement = $this->pdo->prepare(
|
||||
'SELECT DISTINCT shoppro_status_code, shoppro_status_name
|
||||
FROM order_status_mappings
|
||||
WHERE integration_id = :integration_id
|
||||
AND shoppro_status_code <> ""
|
||||
ORDER BY shoppro_status_code ASC'
|
||||
);
|
||||
$statement->execute(['integration_id' => $integrationId]);
|
||||
@@ -39,15 +83,14 @@ final class ShopproStatusMappingRepository
|
||||
continue;
|
||||
}
|
||||
|
||||
$shopproCode = trim((string) ($row['shoppro_status_code'] ?? ''));
|
||||
if ($shopproCode === '') {
|
||||
$code = trim((string) ($row['shoppro_status_code'] ?? ''));
|
||||
if ($code === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'shoppro_status_code' => $shopproCode,
|
||||
'shoppro_status_name' => trim((string) ($row['shoppro_status_name'] ?? '')),
|
||||
'orderpro_status_code' => trim((string) ($row['orderpro_status_code'] ?? '')),
|
||||
'code' => $code,
|
||||
'name' => trim((string) ($row['shoppro_status_name'] ?? $code)),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -55,7 +98,7 @@ final class ShopproStatusMappingRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{shoppro_status_code:string,shoppro_status_name:string,orderpro_status_code:string}> $mappings
|
||||
* @param array<int, array{orderpro_status_code:string,shoppro_status_code:string,shoppro_status_name:string}> $mappings
|
||||
*/
|
||||
public function replaceForIntegration(int $integrationId, array $mappings): void
|
||||
{
|
||||
@@ -74,25 +117,25 @@ final class ShopproStatusMappingRepository
|
||||
|
||||
$insertStatement = $this->pdo->prepare(
|
||||
'INSERT INTO order_status_mappings (
|
||||
integration_id, shoppro_status_code, shoppro_status_name, orderpro_status_code, created_at, updated_at
|
||||
integration_id, orderpro_status_code, shoppro_status_code, shoppro_status_name, created_at, updated_at
|
||||
) VALUES (
|
||||
:integration_id, :shoppro_status_code, :shoppro_status_name, :orderpro_status_code, NOW(), NOW()
|
||||
:integration_id, :orderpro_status_code, :shoppro_status_code, :shoppro_status_name, NOW(), NOW()
|
||||
)'
|
||||
);
|
||||
|
||||
foreach ($mappings as $mapping) {
|
||||
$orderproCode = strtolower(trim((string) ($mapping['orderpro_status_code'] ?? '')));
|
||||
$shopproCode = trim((string) ($mapping['shoppro_status_code'] ?? ''));
|
||||
$orderproCode = trim((string) ($mapping['orderpro_status_code'] ?? ''));
|
||||
if ($shopproCode === '' || $orderproCode === '') {
|
||||
if ($orderproCode === '' || $shopproCode === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$shopproName = trim((string) ($mapping['shoppro_status_name'] ?? ''));
|
||||
$insertStatement->execute([
|
||||
'integration_id' => $integrationId,
|
||||
'orderpro_status_code' => $orderproCode,
|
||||
'shoppro_status_code' => $shopproCode,
|
||||
'shoppro_status_name' => $shopproName !== '' ? $shopproName : null,
|
||||
'orderpro_status_code' => $orderproCode,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user