Automate Shopify reconciliation in daily cron

This commit is contained in:
gitea_admin committed 2026-09-06 15:31:57 +02:00
1 parent e24394866f
commit bf875f6dea
4 files changed
+85 -3

No files matched your search

@@ -33,6 +33,51 @@ function shopify_delta_start_run(PDO $pdo, string $cutoff, bool $dryRun): int
return (int) $stmt->fetchColumn();
}
function shopify_delta_reconciliation_cutoff(PDO $pdo): string
{
$stateStmt = $pdo->prepare(
'SELECT last_successful_cutoff
FROM shopify_delta_sync_state
WHERE process_name = :process_name'
);
$stateStmt->execute([':process_name' => 'erp.import-integration.shopify_delta_order_import']);
$state = $stateStmt->fetchColumn();
if ($state === false || trim((string) $state) === '') {
throw new RuntimeException('Shopify-Delta-Wasserstand fehlt');
}
$pendingStmt = $pdo->query(
"SELECT MIN(received_at)
FROM shopify_webhook_event
WHERE status IN ('received', 'processing', 'failed')
AND shopify_order_gid IS NOT NULL"
);
$pending = $pendingStmt->fetchColumn();
if ($pending === false || $pending === null) {
return (string) $state;
}
$stateDate = new DateTimeImmutable((string) $state);
$pendingDate = new DateTimeImmutable((string) $pending);
return ($pendingDate < $stateDate ? $pendingDate : $stateDate)->format(DATE_ATOM);
}
function shopify_delta_store_watermark(PDO $pdo, string $cutoff): void
{
$stmt = $pdo->prepare(
'UPDATE shopify_delta_sync_state
SET last_successful_cutoff = :cutoff, updated_at = NOW()
WHERE process_name = :process_name'
);
$stmt->execute([
':cutoff' => $cutoff,
':process_name' => 'erp.import-integration.shopify_delta_order_import',
]);
if ($stmt->rowCount() !== 1) {
throw new RuntimeException('Shopify-Delta-Wasserstand konnte nicht gespeichert werden');
}
}
function shopify_delta_finish_run(PDO $pdo, int $runId, string $status, array $result, ?Throwable $error = null): void
{
$stmt = $pdo->prepare(