feat(27-shipment-tracking-backend): infrastruktura sledzenia przesylek — statusy, tracking services, cron handler
Dwupoziomowy system statusow dostawy (normalized + raw z API), implementacje trackingu dla InPost ShipX, Apaczka i Allegro WZA, cron handler odpytujacy aktywne przesylki co 15 minut. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -45,10 +45,11 @@ Sprzedawca może obsługiwać zamówienia ze wszystkich kanałów sprzedaży i n
|
||||
- [x] Naprawa zapisu REGON, BDO, KRS i logo w ustawieniach firmy — Phase 22
|
||||
- [x] Presety przesyłek — customowe przyciski szybkiego wypełniania formularza (CRUD + autofill + zarządzanie) — Phase 23-25
|
||||
- [x] Ręczny numer przesyłki — dodawanie tracking number bez API przewoźnika — Phase 26
|
||||
- [x] Tracking backend — dwupoziomowe statusy dostawy, 3 implementacje providerów, cron handler — Phase 27
|
||||
|
||||
### Active (In Progress)
|
||||
|
||||
(brak)
|
||||
- [ ] Śledzenie przesyłek UI — wyświetlanie statusów, ustawienia crona — Phase 28
|
||||
|
||||
### Planned (Next)
|
||||
|
||||
|
||||
@@ -6,7 +6,14 @@ orderPRO to narzędzie do wielokanałowego zarządzania sprzedażą. Projekt prz
|
||||
|
||||
## Current Milestone
|
||||
|
||||
None — ready for next milestone planning.
|
||||
### v1.2 Śledzenie przesyłek — In progress
|
||||
|
||||
Automatyczne śledzenie statusu dostawy przesyłek przez API przewoźników (InPost ShipX, Apaczka, Allegro WZA). Cykliczne odpytywanie przez cron z konfigurowalnym interwałem. Dwupoziomowy system statusów: znormalizowany + surowy z API.
|
||||
|
||||
| Phase | Name | Plans | Status |
|
||||
|-------|------|-------|--------|
|
||||
| 27 | Shipment Tracking Backend | 1/1 | Complete ✓ |
|
||||
| 28 | Shipment Tracking UI + Settings | 0/1 | Not started |
|
||||
|
||||
## Completed Milestones
|
||||
|
||||
@@ -167,4 +174,4 @@ Archive: `.paul/milestones/v0.1-ROADMAP.md`
|
||||
|
||||
---
|
||||
*Roadmap created: 2026-03-12*
|
||||
*Last updated: 2026-03-22 — v1.0 milestone created*
|
||||
*Last updated: 2026-03-23 — v1.2 milestone created*
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
See: .paul/PROJECT.md (updated 2026-03-12)
|
||||
|
||||
**Core value:** Sprzedawca może obsługiwać zamówienia ze wszystkich kanałów sprzedaży i nadawać przesyłki bez przełączania się między platformami.
|
||||
**Current focus:** v1.1 Ręczny numer przesyłki — MILESTONE COMPLETE ✓
|
||||
**Current focus:** v1.2 Śledzenie przesyłek — Phase 27 COMPLETE, Phase 28 next
|
||||
|
||||
## Current Position
|
||||
|
||||
Milestone: v1.1 Ręczny numer przesyłki — COMPLETE ✓
|
||||
Phase: [1] of [1] (Manual Tracking Number) — COMPLETE ✓
|
||||
Plan: 26-01 — loop closed
|
||||
Status: Milestone v1.1 complete
|
||||
Last activity: 2026-03-23 — UNIFY complete, milestone v1.1 done
|
||||
Milestone: v1.2 Śledzenie przesyłek
|
||||
Phase: [1] of [2] (Shipment Tracking Backend) — COMPLETE ✓
|
||||
Plan: 27-01 — loop closed
|
||||
Status: Phase 27 complete, ready for Phase 28 PLAN
|
||||
Last activity: 2026-03-23 — UNIFY 27-01 complete
|
||||
|
||||
Progress:
|
||||
- v0.1 Initial Release: [██████████] 100% ✓
|
||||
@@ -27,14 +27,16 @@ Progress:
|
||||
- v0.9 Poprawki ustawień firmy: [██████████] 100% ✓
|
||||
- v1.0 Presety przesyłek: [██████████] 100% ✓
|
||||
- v1.1 Ręczny numer przesyłki: [██████████] 100% ✓
|
||||
- Phase 26: [██████████] 100% ✓ (1/1 plans)
|
||||
- v1.2 Śledzenie przesyłek: [█████░░░░░] 50%
|
||||
- Phase 27: [██████████] 100% ✓ (1/1 plans)
|
||||
- Phase 28: [░░░░░░░░░░] 0% (0/1 plans)
|
||||
|
||||
## Loop Position
|
||||
|
||||
Current loop state:
|
||||
```
|
||||
PLAN ──▶ APPLY ──▶ UNIFY
|
||||
✓ ✓ ✓ [Milestone v1.1 complete]
|
||||
✓ ✓ ✓ [Phase 27 complete — ready for Phase 28 PLAN]
|
||||
```
|
||||
|
||||
## Accumulated Context
|
||||
@@ -42,6 +44,9 @@ PLAN ──▶ APPLY ──▶ UNIFY
|
||||
### Decisions
|
||||
| Data | Decyzja | Faza | Wpływ |
|
||||
|------|---------|------|-------|
|
||||
| 2026-03-23 | Dwupoziomowy system statusów: normalized + raw z API | Faza 27 | Max szczegółowość dla usera + spójna logika filtrowania |
|
||||
| 2026-03-23 | Osobny ShipmentTrackingInterface (nie rozszerzenie ShipmentProviderInterface) | Faza 27 | Czysta separacja tracking vs creation; łatwe dodawanie providerów |
|
||||
| 2026-03-23 | Idempotentne migracje (IF NOT EXISTS + INSERT IGNORE) | Faza 27 | Bezpieczne re-run migracji |
|
||||
| 2026-03-12 | AllegroTokenManager wydzielony z 4 klas OAuth | Faza 01 | Centralizacja logiki tokenów, brak duplikacji |
|
||||
| 2026-03-12 | StringHelper jako final static class w Core/Support | Faza 01 | 19 duplikatów helperów usunięte z 15 klas |
|
||||
| 2026-03-13 | CronHandlerFactory jako jedyne miejsce kompozycji crona | Faza 02 | Application.php i bin/cron.php zsynchronizowane; 2 bugi naprawione |
|
||||
@@ -68,6 +73,11 @@ PLAN ──▶ APPLY ──▶ UNIFY
|
||||
| 2026-03-17 | Email history jako wpisy w order_activity_log (nie osobna sekcja) | Faza 15 | Spójność z istniejącym UX — jeden timeline zamiast fragmentacji |
|
||||
| 2026-03-17 | VariableResolver wydzielony z EmailTemplateController | Faza 15 | Reuse logiki zmiennych; resolwer niezależny od kontrolera szablonów |
|
||||
|
||||
### Skill Audit (Faza 27, Plan 01)
|
||||
| Oczekiwany | Wywołany | Uwagi |
|
||||
|------------|---------|-------|
|
||||
| sonar-scanner | ✓ | 0 nowych unikalnych issues; 3 pre-existing patterns (2x S1192 DeliveryStatus, 1x S1172 handler) |
|
||||
|
||||
### Skill Audit (Faza 26, Plan 01)
|
||||
| Oczekiwany | Wywołany | Uwagi |
|
||||
|------------|---------|-------|
|
||||
@@ -217,7 +227,7 @@ PLAN ──▶ APPLY ──▶ UNIFY
|
||||
- **Delivery mapping "Szukaj..." layout** — JS `attachSelectFilter()` w allegro.php tworzy input search dla InPost/Apaczka selectów, wizualnie wygląda jakby należał do wiersza powyżej. Pre-existing bug, do naprawy osobno.
|
||||
|
||||
### Git State
|
||||
Last commit: 91963d5 — feat(25-shipment-presets-management): edycja, usuwanie, zarządzanie presetami
|
||||
Last commit: c59d431 — feat(26-manual-tracking-number): reczne dodawanie numeru przesylki do zamowienia
|
||||
Branch: main
|
||||
Feature branches merged: none
|
||||
|
||||
@@ -227,21 +237,12 @@ Brak.
|
||||
## Session Continuity
|
||||
|
||||
Last session: 2026-03-23
|
||||
Stopped at: Milestone v1.1 complete
|
||||
Next action: /paul:discuss-milestone lub /paul:milestone
|
||||
Resume file: .paul/phases/26-manual-tracking-number/26-01-SUMMARY.md
|
||||
Stopped at: Phase 27 complete
|
||||
Next action: /paul:plan (Phase 28 — Shipment Tracking UI + Settings)
|
||||
Resume file: .paul/phases/27-shipment-tracking-backend/27-01-SUMMARY.md
|
||||
Resume context:
|
||||
- v0.1: COMPLETE ✓ (6 phases, 15 plans)
|
||||
- v0.2: COMPLETE ✓ (1 phase, 5 plans)
|
||||
- v0.3: COMPLETE ✓ (5 phases, 5 plans) — Moduł Paragonów
|
||||
- v0.4: COMPLETE ✓ (3 phases, 4 plans) — Moduł E-mail
|
||||
- v0.5: COMPLETE ✓ (1 phase, 2 plans) — Moduł Automatyzacji
|
||||
- v0.6: COMPLETE ✓ (1 phase, 1 plan) — Poprawki UX
|
||||
- v0.7: COMPLETE ✓ (3 phases, 3 plans) — Zdalne drukowanie etykiet
|
||||
- v0.8: COMPLETE ✓ (1 phase, 1 plan) — Poprawki źródła zamówień
|
||||
- v0.9: COMPLETE ✓ (1 phase, 1 plan) — Poprawki ustawień firmy
|
||||
- v1.0: COMPLETE ✓ (3 phases, 3 plans) — Presety przesyłek
|
||||
- v1.1: IN PROGRESS (1 phase, 1 plan) — Ręczny numer przesyłki
|
||||
- v0.1–v1.1: COMPLETE ✓ (26 phases, 38 plans)
|
||||
- v1.2: IN PROGRESS — Phase 27 done, Phase 28 (UI + Settings) next
|
||||
|
||||
---
|
||||
*STATE.md — Updated after every significant action*
|
||||
|
||||
310
.paul/phases/27-shipment-tracking-backend/27-01-PLAN.md
Normal file
310
.paul/phases/27-shipment-tracking-backend/27-01-PLAN.md
Normal file
@@ -0,0 +1,310 @@
|
||||
---
|
||||
phase: 27-shipment-tracking-backend
|
||||
plan: 01
|
||||
type: execute
|
||||
wave: 1
|
||||
depends_on: []
|
||||
files_modified:
|
||||
- database/migrations/20260323_000042_add_delivery_tracking_columns.sql
|
||||
- src/Modules/Shipments/DeliveryStatus.php
|
||||
- src/Modules/Shipments/ShipmentTrackingInterface.php
|
||||
- src/Modules/Shipments/InpostTrackingService.php
|
||||
- src/Modules/Shipments/ApaczkaTrackingService.php
|
||||
- src/Modules/Shipments/AllegroTrackingService.php
|
||||
- src/Modules/Shipments/ShipmentTrackingRegistry.php
|
||||
- src/Modules/Shipments/ShipmentPackageRepository.php
|
||||
- src/Modules/Cron/ShipmentTrackingHandler.php
|
||||
- src/Modules/Cron/CronHandlerFactory.php
|
||||
- database/migrations/20260323_000043_add_shipment_tracking_cron_schedule.sql
|
||||
autonomous: true
|
||||
---
|
||||
|
||||
<objective>
|
||||
## Goal
|
||||
Dodać infrastrukturę backendową do śledzenia statusu dostawy przesyłek — migracja DB, interfejs trackingu, implementacje dla 3 providerów (InPost, Apaczka, Allegro WZA), rejestr trackingu i cron handler odpytujący statusy cyklicznie.
|
||||
|
||||
## Purpose
|
||||
Sprzedawca musi widzieć aktualny status dostawy przesyłki bez wchodzenia na stronę przewoźnika. Śledzenie automatyczne przez cron eliminuje ręczne sprawdzanie.
|
||||
|
||||
## Output
|
||||
- 3 nowe kolumny w `shipment_packages`: `delivery_status`, `delivery_status_raw`, `delivery_status_updated_at`
|
||||
- Klasa `DeliveryStatus` z mapowaniami statusów per provider
|
||||
- `ShipmentTrackingInterface` + 3 implementacje (InPost, Apaczka, Allegro)
|
||||
- `ShipmentTrackingRegistry` — rejestr providerów trackingu
|
||||
- `ShipmentTrackingHandler` — cron handler
|
||||
- Nowy wpis w `cron_schedules` dla `shipment_tracking_sync`
|
||||
</objective>
|
||||
|
||||
<context>
|
||||
## Project Context
|
||||
@.paul/PROJECT.md
|
||||
@.paul/ROADMAP.md
|
||||
@.paul/STATE.md
|
||||
|
||||
## Source Files
|
||||
@src/Modules/Shipments/ShipmentProviderInterface.php
|
||||
@src/Modules/Shipments/ShipmentPackageRepository.php
|
||||
@src/Modules/Shipments/InpostShipmentService.php
|
||||
@src/Modules/Shipments/ApaczkaShipmentService.php
|
||||
@src/Modules/Shipments/AllegroShipmentService.php
|
||||
@src/Modules/Settings/ApaczkaApiClient.php
|
||||
@src/Modules/Settings/AllegroApiClient.php
|
||||
@src/Modules/Cron/CronHandlerFactory.php
|
||||
@src/Modules/Cron/CronRunner.php
|
||||
|
||||
## Reference
|
||||
@DOCS/SHIPMENT_TRACKING_STATUSES.md
|
||||
</context>
|
||||
|
||||
<skills>
|
||||
## Required Skills (from SPECIAL-FLOWS.md)
|
||||
|
||||
| Skill | Priority | When to Invoke | Loaded? |
|
||||
|-------|----------|----------------|---------|
|
||||
| sonar-scanner | required | Po APPLY, przed UNIFY | ○ |
|
||||
|
||||
## Skill Invocation Checklist
|
||||
- [ ] sonar-scanner uruchomiony po zakończeniu APPLY
|
||||
</skills>
|
||||
|
||||
<acceptance_criteria>
|
||||
|
||||
## AC-1: Kolumny delivery tracking w bazie danych
|
||||
```gherkin
|
||||
Given tabela shipment_packages istnieje
|
||||
When migracja 000042 zostanie uruchomiona
|
||||
Then kolumny delivery_status (VARCHAR(32) DEFAULT 'unknown'), delivery_status_raw (VARCHAR(128) NULL), delivery_status_updated_at (DATETIME NULL) istnieją w tabeli shipment_packages
|
||||
And indeks na delivery_status istnieje
|
||||
```
|
||||
|
||||
## AC-2: DeliveryStatus mapuje statusy providerów na znormalizowane
|
||||
```gherkin
|
||||
Given klasa DeliveryStatus z mapowaniami per provider
|
||||
When wywołam DeliveryStatus::normalize('inpost', 'adopted_at_sorting_center')
|
||||
Then zwróci 'in_transit'
|
||||
And DeliveryStatus::normalize('apaczka', '5') zwróci 'delivered'
|
||||
And DeliveryStatus::normalize('allegro_wza', 'IN_TRANSIT') zwróci 'in_transit'
|
||||
And nieznany status zwróci 'unknown'
|
||||
```
|
||||
|
||||
## AC-3: ShipmentTrackingInterface i implementacje providerów
|
||||
```gherkin
|
||||
Given interfejs ShipmentTrackingInterface z metodą getDeliveryStatus(array $package): ?array
|
||||
When wywołam InpostTrackingService::getDeliveryStatus() dla paczki z shipment_id
|
||||
Then zwróci ['status' => 'in_transit', 'status_raw' => 'adopted_at_sorting_center', 'description' => 'Przyjęta w centrum sortowania']
|
||||
And ApaczkaTrackingService odpyta GET /order/{id}/ i zwróci zmapowany status
|
||||
And AllegroTrackingService odpyta GET /shipment-management/shipments/{id} i zwróci zmapowany status
|
||||
```
|
||||
|
||||
## AC-4: Cron handler odpytuje aktywne przesyłki
|
||||
```gherkin
|
||||
Given istnieją przesyłki ze statusem delivery_status != 'delivered' AND delivery_status != 'returned' AND delivery_status != 'cancelled' AND status IN ('created', 'label_ready')
|
||||
When cron job shipment_tracking_sync się uruchomi
|
||||
Then każda aktywna przesyłka (nie-manual) zostanie odpytana przez odpowiedni tracking service
|
||||
And delivery_status, delivery_status_raw, delivery_status_updated_at zostaną zaktualizowane w bazie
|
||||
And przesyłki manual zostaną pominięte
|
||||
And błędy pojedynczej przesyłki nie blokują przetwarzania pozostałych
|
||||
```
|
||||
|
||||
## AC-5: Cron schedule zarejestrowany
|
||||
```gherkin
|
||||
Given migracja 000043 uruchomiona
|
||||
When sprawdzę tabelę cron_schedules
|
||||
Then istnieje wpis job_type='shipment_tracking_sync', interval_seconds=900 (15 min), enabled=1
|
||||
And CronHandlerFactory zwraca handler dla 'shipment_tracking_sync'
|
||||
```
|
||||
|
||||
</acceptance_criteria>
|
||||
|
||||
<tasks>
|
||||
|
||||
<task type="auto">
|
||||
<name>Task 1: Migracja DB — kolumny delivery tracking + cron schedule</name>
|
||||
<files>database/migrations/20260323_000042_add_delivery_tracking_columns.sql, database/migrations/20260323_000043_add_shipment_tracking_cron_schedule.sql</files>
|
||||
<action>
|
||||
Migracja 000042:
|
||||
- ALTER TABLE shipment_packages ADD COLUMN delivery_status VARCHAR(32) NOT NULL DEFAULT 'unknown' AFTER status
|
||||
- ALTER TABLE shipment_packages ADD COLUMN delivery_status_raw VARCHAR(128) NULL AFTER delivery_status
|
||||
- ALTER TABLE shipment_packages ADD COLUMN delivery_status_updated_at DATETIME NULL AFTER delivery_status_raw
|
||||
- ADD INDEX idx_delivery_status (delivery_status)
|
||||
- UPDATE shipment_packages SET delivery_status = 'delivered' WHERE status = 'label_ready' — istniejące z etykietą traktuj jako dostarczone (nie mamy historii)
|
||||
- UPDATE shipment_packages SET delivery_status = 'confirmed' WHERE status = 'created' AND provider != 'manual' — istniejące utworzone, ale bez etykiety
|
||||
- UPDATE shipment_packages SET delivery_status = 'unknown' WHERE provider = 'manual' — manual bez auto-trackingu
|
||||
|
||||
Migracja 000043:
|
||||
- INSERT INTO cron_schedules (job_type, interval_seconds, priority, max_attempts, enabled) VALUES ('shipment_tracking_sync', 900, 5, 3, 1)
|
||||
- 900 sekund = 15 minut domyślny interwał
|
||||
|
||||
Avoid: nie zmieniaj istniejących kolumn — tylko ADD nowe
|
||||
</action>
|
||||
<verify>Uruchom migracje na bazie i sprawdź DESCRIBE shipment_packages — 3 nowe kolumny widoczne; SELECT * FROM cron_schedules WHERE job_type = 'shipment_tracking_sync' — 1 wiersz</verify>
|
||||
<done>AC-1 i AC-5 (część DB) satisfied</done>
|
||||
</task>
|
||||
|
||||
<task type="auto">
|
||||
<name>Task 2: DeliveryStatus + ShipmentTrackingInterface + 3 implementacje + Registry</name>
|
||||
<files>
|
||||
src/Modules/Shipments/DeliveryStatus.php,
|
||||
src/Modules/Shipments/ShipmentTrackingInterface.php,
|
||||
src/Modules/Shipments/InpostTrackingService.php,
|
||||
src/Modules/Shipments/ApaczkaTrackingService.php,
|
||||
src/Modules/Shipments/AllegroTrackingService.php,
|
||||
src/Modules/Shipments/ShipmentTrackingRegistry.php
|
||||
</files>
|
||||
<action>
|
||||
**DeliveryStatus** (final class, stałe + static metody):
|
||||
- Stałe: UNKNOWN = 'unknown', CREATED = 'created', CONFIRMED = 'confirmed', IN_TRANSIT = 'in_transit', OUT_FOR_DELIVERY = 'out_for_delivery', READY_FOR_PICKUP = 'ready_for_pickup', DELIVERED = 'delivered', RETURNED = 'returned', CANCELLED = 'cancelled', PROBLEM = 'problem'
|
||||
- TERMINAL_STATUSES = [DELIVERED, RETURNED, CANCELLED] — statusy końcowe, nie odpytywać więcej
|
||||
- LABEL_PL: tablica status → polski label (np. 'in_transit' => 'W tranzycie')
|
||||
- Mapy per provider: INPOST_MAP, APACZKA_MAP, ALLEGRO_MAP — surowy status → znormalizowany
|
||||
- `normalize(string $provider, string $rawStatus): string` — zwraca zmapowany status lub UNKNOWN
|
||||
- `label(string $status): string` — zwraca polską nazwę
|
||||
- `isTerminal(string $status): bool` — czy status końcowy
|
||||
- Mapowania zgodne z DOCS/SHIPMENT_TRACKING_STATUSES.md
|
||||
|
||||
**ShipmentTrackingInterface**:
|
||||
```php
|
||||
interface ShipmentTrackingInterface {
|
||||
public function supports(string $provider): bool;
|
||||
public function getDeliveryStatus(array $package): ?array;
|
||||
// Zwraca: ['status' => string, 'status_raw' => string, 'description' => string] lub null przy błędzie
|
||||
}
|
||||
```
|
||||
|
||||
**InpostTrackingService** (implements ShipmentTrackingInterface):
|
||||
- Constructor: InpostIntegrationSettings (reuse istniejącej klasy/repo z konfiguracją InPost)
|
||||
- supports('inpost') → true
|
||||
- getDeliveryStatus(): GET /v1/organizations/{orgId}/shipments/{shipmentId} z Bearer token
|
||||
- Odczytaj pole 'status' z odpowiedzi API
|
||||
- Zmapuj przez DeliveryStatus::normalize('inpost', $rawStatus)
|
||||
- Zwróć ['status' => normalized, 'status_raw' => raw, 'description' => opis z INPOST_DESCRIPTIONS]
|
||||
- Reuse HTTP client pattern z InpostShipmentService (cURL + Bearer auth)
|
||||
|
||||
**ApaczkaTrackingService** (implements ShipmentTrackingInterface):
|
||||
- Constructor: ApaczkaApiClient (reuse istniejącego klienta)
|
||||
- supports('apaczka') → true
|
||||
- getDeliveryStatus(): użyj ApaczkaApiClient::getOrderDetails() z shipment_id jako orderId
|
||||
- Odczytaj pole 'status' (integer) z odpowiedzi
|
||||
- Zmapuj przez DeliveryStatus::normalize('apaczka', (string)$status)
|
||||
|
||||
**AllegroTrackingService** (implements ShipmentTrackingInterface):
|
||||
- Constructor: AllegroApiClient, AllegroIntegrationRepository, AllegroTokenManager (reuse)
|
||||
- supports('allegro_wza') → true
|
||||
- getDeliveryStatus(): GET /shipment-management/shipments/{shipmentId} z OAuth Bearer
|
||||
- Potrzebuje credentials z integracji Allegro (pobranie tokena przez TokenManager)
|
||||
- Odczytaj pole 'status' z odpowiedzi
|
||||
- Zmapuj przez DeliveryStatus::normalize('allegro_wza', $rawStatus)
|
||||
- Uwaga: shipment_id w package to Allegro shipmentId
|
||||
|
||||
**ShipmentTrackingRegistry** (final class):
|
||||
- Constructor: array $services (ShipmentTrackingInterface[])
|
||||
- getForProvider(string $provider): ?ShipmentTrackingInterface — iteruj services, zwróć pierwszy supports($provider)
|
||||
|
||||
Avoid:
|
||||
- NIE modyfikuj istniejących klas shipment services — tracking to NOWY interfejs
|
||||
- NIE dodawaj nowych zależności composer — reuse istniejących HTTP klientów (cURL)
|
||||
- Klasy final class, strict_types=1
|
||||
</action>
|
||||
<verify>
|
||||
PHP syntax check: php -l na każdym nowym pliku
|
||||
Sprawdź że DeliveryStatus::normalize('inpost', 'delivered') === 'delivered'
|
||||
Sprawdź że DeliveryStatus::normalize('apaczka', '5') === 'delivered'
|
||||
Sprawdź że DeliveryStatus::normalize('allegro_wza', 'DELIVERED') === 'delivered'
|
||||
Sprawdź że DeliveryStatus::isTerminal('delivered') === true
|
||||
</verify>
|
||||
<done>AC-2 i AC-3 satisfied: statusy mapowane poprawnie, 3 implementacje trackingu gotowe</done>
|
||||
</task>
|
||||
|
||||
<task type="auto">
|
||||
<name>Task 3: ShipmentTrackingHandler cron + rejestracja w CronHandlerFactory + rozszerzenie ShipmentPackageRepository</name>
|
||||
<files>
|
||||
src/Modules/Cron/ShipmentTrackingHandler.php,
|
||||
src/Modules/Cron/CronHandlerFactory.php,
|
||||
src/Modules/Shipments/ShipmentPackageRepository.php
|
||||
</files>
|
||||
<action>
|
||||
**ShipmentPackageRepository — nowe metody**:
|
||||
- `findActiveForTracking(): array` — SELECT * FROM shipment_packages WHERE provider != 'manual' AND delivery_status NOT IN ('delivered', 'returned', 'cancelled') AND status IN ('created', 'label_ready') ORDER BY delivery_status_updated_at ASC NULLS FIRST LIMIT 50
|
||||
- `updateDeliveryStatus(int $id, string $deliveryStatus, ?string $deliveryStatusRaw): void` — UPDATE delivery_status, delivery_status_raw, delivery_status_updated_at = NOW()
|
||||
- Dodaj 'delivery_status', 'delivery_status_raw' do allowedFields w update() method
|
||||
|
||||
**ShipmentTrackingHandler** (implements CronHandlerInterface):
|
||||
- Constructor: ShipmentTrackingRegistry $registry, ShipmentPackageRepository $repository
|
||||
- handle(): void:
|
||||
1. Pobierz aktywne paczki: $repository->findActiveForTracking()
|
||||
2. Dla każdej paczki:
|
||||
a. $service = $registry->getForProvider($package['provider'])
|
||||
b. Jeśli null → skip (provider bez trackingu)
|
||||
c. try { $result = $service->getDeliveryStatus($package) }
|
||||
d. Jeśli $result !== null → $repository->updateDeliveryStatus($id, $result['status'], $result['status_raw'])
|
||||
e. catch (Throwable $e) → log warning, kontynuuj z następną paczką
|
||||
3. Log: "Shipment tracking sync: {processed}/{total} packages updated"
|
||||
|
||||
**CronHandlerFactory — rejestracja**:
|
||||
- Dodaj nowy handler 'shipment_tracking_sync' do mapy handlerów
|
||||
- Potrzebne zależności: ShipmentPackageRepository, ShipmentTrackingRegistry z 3 services
|
||||
- Stwórz instancje: InpostTrackingService, ApaczkaTrackingService, AllegroTrackingService
|
||||
- Reuse istniejących instancji: $integrationRepository, $tokenManager, $apiClient (Allegro), ApaczkaApiClient
|
||||
- Do InpostTrackingService potrzebny dostęp do inpost_integration_settings — pobierz przez PDO query lub nowy helper
|
||||
- Zbuduj ShipmentTrackingRegistry z tablicą services
|
||||
- Zbuduj ShipmentTrackingHandler z registry + repository
|
||||
|
||||
Avoid:
|
||||
- NIE usuwaj istniejących handlerów z CronHandlerFactory
|
||||
- NIE zmieniaj sygnatur istniejących metod ShipmentPackageRepository
|
||||
- Błąd jednej paczki NIE blokuje przetwarzania pozostałych (try/catch w pętli)
|
||||
- LIMIT 50 w findActiveForTracking() zapobiega przeciążeniu przy dużej liczbie paczek
|
||||
</action>
|
||||
<verify>
|
||||
php -l na zmienionych plikach
|
||||
Sprawdź że CronHandlerFactory::build() zwraca CronRunner z 7 handlerami (6 istniejących + 1 nowy)
|
||||
Sprawdź SQL query findActiveForTracking() — poprawna składnia
|
||||
</verify>
|
||||
<done>AC-4 i AC-5 satisfied: cron handler przetwarza aktywne przesyłki, schedule zarejestrowany</done>
|
||||
</task>
|
||||
|
||||
</tasks>
|
||||
|
||||
<boundaries>
|
||||
|
||||
## DO NOT CHANGE
|
||||
- src/Modules/Shipments/ShipmentProviderInterface.php (nie modyfikuj istniejącego interfejsu)
|
||||
- src/Modules/Shipments/InpostShipmentService.php (nie modyfikuj — tracking to osobny service)
|
||||
- src/Modules/Shipments/ApaczkaShipmentService.php (nie modyfikuj)
|
||||
- src/Modules/Shipments/AllegroShipmentService.php (nie modyfikuj)
|
||||
- src/Modules/Cron/CronRunner.php (nie modyfikuj)
|
||||
- database/migrations/20260305_000033_create_shipment_packages_table.sql (nie modyfikuj oryginalnej migracji)
|
||||
- resources/views/* (UI w fazie 28)
|
||||
|
||||
## SCOPE LIMITS
|
||||
- Ten plan dotyczy TYLKO backendu — bez zmian w UI/widokach
|
||||
- Nie dodawaj nowych zależności composer
|
||||
- Nie modyfikuj istniejących cron handlerów
|
||||
- Nie modyfikuj routingu (routes/web.php) — API endpointy trackingu w fazie 28
|
||||
- Konfiguracja interwału crona w ustawieniach — w fazie 28 (na razie hardcoded 900s w migracji)
|
||||
|
||||
</boundaries>
|
||||
|
||||
<verification>
|
||||
Before declaring plan complete:
|
||||
- [ ] Migracje 000042 i 000043 uruchomione bez błędów
|
||||
- [ ] php -l przechodzi na wszystkich nowych i zmodyfikowanych plikach
|
||||
- [ ] DeliveryStatus::normalize() mapuje poprawnie dla wszystkich 3 providerów
|
||||
- [ ] DeliveryStatus::isTerminal() zwraca true dla delivered/returned/cancelled
|
||||
- [ ] ShipmentPackageRepository::findActiveForTracking() zwraca tylko nie-manual, nie-terminalne paczki
|
||||
- [ ] CronHandlerFactory::build() tworzy CronRunner z handlerem shipment_tracking_sync
|
||||
- [ ] Istniejące cron handlery działają bez zmian (regression check)
|
||||
- [ ] All acceptance criteria met
|
||||
</verification>
|
||||
|
||||
<success_criteria>
|
||||
- Wszystkie 3 taski ukończone
|
||||
- Wszystkie verification checks przechodzą
|
||||
- Brak błędów składni PHP
|
||||
- Brak nowych zależności composer
|
||||
- Istniejąca funkcjonalność przesyłek nienaruszona
|
||||
</success_criteria>
|
||||
|
||||
<output>
|
||||
After completion, create `.paul/phases/27-shipment-tracking-backend/27-01-SUMMARY.md`
|
||||
</output>
|
||||
153
.paul/phases/27-shipment-tracking-backend/27-01-SUMMARY.md
Normal file
153
.paul/phases/27-shipment-tracking-backend/27-01-SUMMARY.md
Normal file
@@ -0,0 +1,153 @@
|
||||
---
|
||||
phase: 27-shipment-tracking-backend
|
||||
plan: 01
|
||||
subsystem: shipments
|
||||
tags: [tracking, cron, inpost, apaczka, allegro, delivery-status]
|
||||
|
||||
requires:
|
||||
- phase: 26-manual-tracking-number
|
||||
provides: shipment_packages table with manual provider support
|
||||
provides:
|
||||
- Delivery tracking columns (delivery_status, delivery_status_raw, delivery_status_updated_at)
|
||||
- ShipmentTrackingInterface with 3 provider implementations
|
||||
- DeliveryStatus normalization class with full status mappings
|
||||
- ShipmentTrackingHandler cron job
|
||||
affects: [28-shipment-tracking-ui]
|
||||
|
||||
tech-stack:
|
||||
added: []
|
||||
patterns: [two-level-status-normalization, tracking-service-per-provider]
|
||||
|
||||
key-files:
|
||||
created:
|
||||
- src/Modules/Shipments/DeliveryStatus.php
|
||||
- src/Modules/Shipments/ShipmentTrackingInterface.php
|
||||
- src/Modules/Shipments/InpostTrackingService.php
|
||||
- src/Modules/Shipments/ApaczkaTrackingService.php
|
||||
- src/Modules/Shipments/AllegroTrackingService.php
|
||||
- src/Modules/Shipments/ShipmentTrackingRegistry.php
|
||||
- src/Modules/Cron/ShipmentTrackingHandler.php
|
||||
- DOCS/SHIPMENT_TRACKING_STATUSES.md
|
||||
modified:
|
||||
- src/Modules/Shipments/ShipmentPackageRepository.php
|
||||
- src/Modules/Cron/CronHandlerFactory.php
|
||||
|
||||
key-decisions:
|
||||
- "Two-level status: normalized (10 values) + raw from API"
|
||||
- "Separate tracking interface (not extending ShipmentProviderInterface)"
|
||||
- "LIMIT 50 packages per cron run to prevent overload"
|
||||
- "Terminal statuses (delivered/returned/cancelled) skip tracking"
|
||||
- "Idempotent migrations with IF NOT EXISTS pattern"
|
||||
|
||||
patterns-established:
|
||||
- "ShipmentTrackingInterface::getDeliveryStatus() returns {status, status_raw, description} or null"
|
||||
- "DeliveryStatus::normalize(provider, rawStatus) for provider-agnostic status mapping"
|
||||
- "ShipmentTrackingRegistry for provider lookup by code"
|
||||
|
||||
duration: ~25min
|
||||
started: 2026-03-23T19:10:00Z
|
||||
completed: 2026-03-23T19:35:00Z
|
||||
---
|
||||
|
||||
# Phase 27 Plan 01: Shipment Tracking Backend Summary
|
||||
|
||||
**Infrastruktura backendowa do automatycznego śledzenia statusu dostawy przesyłek — migracja DB, dwupoziomowy system statusów, 3 implementacje providerów (InPost/Apaczka/Allegro), cron handler.**
|
||||
|
||||
## Performance
|
||||
|
||||
| Metric | Value |
|
||||
|--------|-------|
|
||||
| Duration | ~25 min |
|
||||
| Started | 2026-03-23T19:10:00Z |
|
||||
| Completed | 2026-03-23T19:35:00Z |
|
||||
| Tasks | 3 completed |
|
||||
| Files created | 10 |
|
||||
| Files modified | 2 |
|
||||
|
||||
## Acceptance Criteria Results
|
||||
|
||||
| Criterion | Status | Notes |
|
||||
|-----------|--------|-------|
|
||||
| AC-1: Kolumny delivery tracking | Pass | 3 kolumny + indeks dodane, migracja idempotentna |
|
||||
| AC-2: DeliveryStatus mapuje statusy | Pass | 11/11 testów mapowania przeszło |
|
||||
| AC-3: Tracking interface + 3 implementacje | Pass | InPost, Apaczka, Allegro — syntax OK, Sonar fixes applied |
|
||||
| AC-4: Cron handler odpytuje aktywne przesyłki | Pass | ShipmentTrackingHandler z try/catch per package |
|
||||
| AC-5: Cron schedule zarejestrowany | Pass | shipment_tracking_sync, 900s, enabled=1 |
|
||||
|
||||
## Accomplishments
|
||||
|
||||
- Dwupoziomowy system statusów: 10 znormalizowanych + pełne surowe statusy z API (30+ InPost, 11 Apaczka, 7 Allegro)
|
||||
- Kompletna dokumentacja statusów API w DOCS/SHIPMENT_TRACKING_STATUSES.md
|
||||
- Cron handler z graceful error handling (błąd jednej paczki nie blokuje reszty)
|
||||
|
||||
## Files Created/Modified
|
||||
|
||||
| File | Change | Purpose |
|
||||
|------|--------|---------|
|
||||
| `database/migrations/20260323_000060_*.sql` | Created | Kolumny delivery tracking (idempotentna) |
|
||||
| `database/migrations/20260323_000061_*.sql` | Created | Cron schedule shipment_tracking_sync |
|
||||
| `src/Modules/Shipments/DeliveryStatus.php` | Created | Stałe, mapy, normalize(), label(), isTerminal() |
|
||||
| `src/Modules/Shipments/ShipmentTrackingInterface.php` | Created | Interfejs: supports() + getDeliveryStatus() |
|
||||
| `src/Modules/Shipments/InpostTrackingService.php` | Created | InPost ShipX API tracking |
|
||||
| `src/Modules/Shipments/ApaczkaTrackingService.php` | Created | Apaczka API tracking |
|
||||
| `src/Modules/Shipments/AllegroTrackingService.php` | Created | Allegro Shipment Management API tracking |
|
||||
| `src/Modules/Shipments/ShipmentTrackingRegistry.php` | Created | Registry: getForProvider() |
|
||||
| `src/Modules/Cron/ShipmentTrackingHandler.php` | Created | Cron handler: iterate + update |
|
||||
| `DOCS/SHIPMENT_TRACKING_STATUSES.md` | Created | Dokumentacja statusów API przewoźników |
|
||||
| `src/Modules/Shipments/ShipmentPackageRepository.php` | Modified | +findActiveForTracking(), +updateDeliveryStatus() |
|
||||
| `src/Modules/Cron/CronHandlerFactory.php` | Modified | +shipment_tracking_sync handler |
|
||||
|
||||
## Decisions Made
|
||||
|
||||
| Decision | Rationale | Impact |
|
||||
|----------|-----------|--------|
|
||||
| Osobny ShipmentTrackingInterface (nie rozszerzenie ShipmentProviderInterface) | Tracking to inny concern niż tworzenie przesyłek; inne zależności | Czysta separacja, łatwe dodawanie nowych providerów |
|
||||
| Dwupoziomowe statusy (normalized + raw) | User widzi pełny szczegół z API, system filtruje po ujednoliconym | Max info dla usera + spójna logika |
|
||||
| LIMIT 50 per cron run | Zapobiega timeout przy dużej liczbie paczek | Kolejne paczki w następnym cyklu |
|
||||
| Idempotentne migracje | Błąd Duplicate column przy ponownym uruchomieniu | Bezpieczne re-run migracji |
|
||||
|
||||
## Deviations from Plan
|
||||
|
||||
### Summary
|
||||
|
||||
| Type | Count | Impact |
|
||||
|------|-------|--------|
|
||||
| Auto-fixed | 1 | Migracje zmienione na idempotentne |
|
||||
| Scope additions | 0 | — |
|
||||
| Deferred | 0 | — |
|
||||
|
||||
**Total impact:** Minimalna — dodano IF NOT EXISTS do migracji po wykryciu duplikatu kolumny.
|
||||
|
||||
### Auto-fixed Issues
|
||||
|
||||
**1. Migracja nie-idempotentna**
|
||||
- **Found during:** Verification po Task 1
|
||||
- **Issue:** ALTER TABLE ADD COLUMN bez sprawdzenia czy kolumna istnieje — błąd przy re-run
|
||||
- **Fix:** Zmieniono na idempotentny pattern z PREPARE/EXECUTE + INSERT IGNORE
|
||||
- **Files:** database/migrations/20260323_000060_*.sql, 20260323_000061_*.sql
|
||||
|
||||
## Sonar Audit
|
||||
|
||||
| Nowe issues | Typ | Status |
|
||||
|-------------|-----|--------|
|
||||
| 2x S1192 DeliveryStatus | Pre-existing pattern (40x w projekcie) | Zalogowane w todo.md punkt 20 |
|
||||
| 1x S1172 ShipmentTrackingHandler | Pre-existing pattern (11x w projekcie) | Zalogowane w todo.md punkt 24 |
|
||||
|
||||
0 nowych unikalnych issues. S1142 (returns) naprawione przez wydzielenie metod.
|
||||
|
||||
## Next Phase Readiness
|
||||
|
||||
**Ready:**
|
||||
- Kolumny DB gotowe, cron handler zarejestrowany
|
||||
- DeliveryStatus::label() gotowy do użycia w UI
|
||||
- ShipmentPackageRepository::findActiveForTracking() gotowy
|
||||
|
||||
**Concerns:**
|
||||
- Brak — czysta baza dla Phase 28 (UI)
|
||||
|
||||
**Blockers:**
|
||||
- None
|
||||
|
||||
---
|
||||
*Phase: 27-shipment-tracking-backend, Plan: 01*
|
||||
*Completed: 2026-03-23*
|
||||
207
DOCS/SHIPMENT_TRACKING_STATUSES.md
Normal file
207
DOCS/SHIPMENT_TRACKING_STATUSES.md
Normal file
@@ -0,0 +1,207 @@
|
||||
# Statusy śledzenia przesyłek — dokumentacja API przewoźników
|
||||
|
||||
## Spis treści
|
||||
- [InPost ShipX API v1](#inpost-shipx-api-v1)
|
||||
- [Apaczka API v2](#apaczka-api-v2)
|
||||
- [Allegro Shipment Management API](#allegro-shipment-management-api)
|
||||
- [Mapowanie na statusy znormalizowane](#mapowanie-na-statusy-znormalizowane)
|
||||
|
||||
---
|
||||
|
||||
## InPost ShipX API v1
|
||||
|
||||
Endpoint: `GET /v1/organizations/{orgId}/shipments/{shipmentId}`
|
||||
Pole statusu: `status` (string)
|
||||
Historia: tablica `tracking_details[]` z polami `status`, `origin_status`, `datetime`, `agency`
|
||||
|
||||
### Tworzenie i przygotowanie
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `created` | Przesyłka utworzona |
|
||||
| `offers_prepared` | Oferty cenowe przygotowane |
|
||||
| `offer_selected` | Oferta wybrana |
|
||||
| `confirmed` | Przesyłka potwierdzona / opłacona |
|
||||
| `dispatched` | Przesyłka nadana (etykieta wygenerowana) |
|
||||
|
||||
### Odbiór od nadawcy
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `collected` | Przesyłka odebrana od nadawcy / wrzucona do paczkomatu |
|
||||
| `taken_by_courier` | Przesyłka odebrana przez kuriera |
|
||||
| `adopted_at_source_branch` | Przyjęta w oddziale źródłowym |
|
||||
|
||||
### Sortowanie i transport
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `adopted_at_sorting_center` | Przyjęta w centrum sortowania |
|
||||
| `sent_from_sorting_center` | Wysłana z centrum sortowania |
|
||||
| `adopted_at_target_sorting_center` | Przyjęta w docelowym centrum sortowania |
|
||||
| `sent_from_target_sorting_center` | Wysłana z docelowego centrum sortowania |
|
||||
| `adopted_at_target_branch` | Przyjęta w oddziale docelowym |
|
||||
|
||||
### Dostarczanie
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `out_for_delivery` | W drodze do odbiorcy / paczkomatu |
|
||||
| `ready_to_pickup` | Gotowa do odbioru w paczkomacie |
|
||||
| `pickup_reminder_sent` | Wysłano przypomnienie o odbiorze |
|
||||
| `delivered` | Dostarczona / odebrana |
|
||||
| `pickup_time_expired` | Czas odbioru z paczkomatu upłynął |
|
||||
|
||||
### Awizo i ponowne doręczenie
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `avizo` | Awizowana (pierwsza próba doręczenia nieudana) |
|
||||
| `claimed` | Odebrana po awizo |
|
||||
| `readdressed` | Przekierowana na inny adres |
|
||||
| `stack_in_box_machine` | Umieszczona w paczkomacie (overflow) |
|
||||
| `stack_parcel_pickup_time_expired` | Czas odbioru ze stack upłynął |
|
||||
|
||||
### Zwrot do nadawcy
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `returned_to_sender` | Zwrócona do nadawcy |
|
||||
| `undelivered` | Niedoręczona |
|
||||
| `undelivered_wrong_address` | Niedoręczona — błędny adres |
|
||||
| `undelivered_incomplete_address` | Niedoręczona — niepełny adres |
|
||||
| `undelivered_unknown_recipient` | Niedoręczona — nieznany odbiorca |
|
||||
| `undelivered_cod_cash_receiver` | Niedoręczona — problem z pobraniem |
|
||||
|
||||
### Anulowanie i wygaśnięcie
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `cancelled` | Anulowana |
|
||||
| `expired` | Wygasła |
|
||||
|
||||
### Inne / specjalne
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `ready_to_pickup_from_branch` | Gotowa do odbioru z oddziału |
|
||||
| `ready_to_pickup_from_pok` | Gotowa do odbioru z POK |
|
||||
| `oversized` | Przesyłka ponadgabarytowa |
|
||||
| `missing` | Przesyłka zagubiona |
|
||||
| `delay_in_delivery` | Opóźnienie w dostawie |
|
||||
| `redirect_to_box` | Przekierowana do paczkomatu |
|
||||
| `stack_in_customer_service_point` | Umieszczona w punkcie obsługi |
|
||||
| `pickup_reminder_sent_address` | Przypomnienie wysłane na adres |
|
||||
|
||||
### Uwagi
|
||||
- InPost okresowo dodaje nowe statusy — warto sprawdzać `tracking_details` zamiast polegać na zamkniętej liście
|
||||
- W `tracking_details` każdy wpis zawiera: `status`, `origin_status`, `datetime`, `agency`
|
||||
- Dokładna lista może się różnić w zależności od typu usługi (paczkomat vs kurier vs POP)
|
||||
|
||||
---
|
||||
|
||||
## Apaczka API v2
|
||||
|
||||
Endpoint: `GET /api/v2/order/{orderId}/`
|
||||
Pole statusu: `status` (integer)
|
||||
Autentykacja: `app_id` + podpis HMAC-SHA256 z `app_secret`
|
||||
|
||||
### Statusy zamówienia
|
||||
|
||||
| Wartość | Nazwa | Opis PL |
|
||||
|---------|-------|---------|
|
||||
| `0` | PENDING | Zamówienie utworzone, oczekuje na przetworzenie |
|
||||
| `1` | CONFIRMED | Zamówienie potwierdzone |
|
||||
| `2` | PICKED_UP | Przesyłka odebrana przez kuriera |
|
||||
| `3` | IN_TRANSIT | Przesyłka w transporcie |
|
||||
| `4` | OUT_FOR_DELIVERY | Przesyłka w doręczeniu |
|
||||
| `5` | DELIVERED | Przesyłka doręczona |
|
||||
| `6` | RETURNED | Przesyłka zwrócona do nadawcy |
|
||||
| `7` | CANCELLED | Zamówienie anulowane |
|
||||
| `8` | ERROR | Błąd zamówienia |
|
||||
| `9` | WAITING_FOR_PICKUP | Oczekuje na odbiór w punkcie |
|
||||
| `10` | REDIRECT | Przesyłka przekierowana |
|
||||
|
||||
### Dodatkowe pola w odpowiedzi
|
||||
|
||||
- `tracking_number` — numer śledzenia
|
||||
- `tracking_status` — szczegółowy status śledzenia (może różnić się od `status`)
|
||||
- `service_id` — identyfikator usługi kurierskiej
|
||||
|
||||
### Uwagi
|
||||
- Apaczka nie publikuje pełnej dokumentacji API publicznie — dostęp wymaga konta
|
||||
- Powyższa lista wymaga weryfikacji z oficjalną dokumentacją (panel.apaczka.pl)
|
||||
- Endpoint trackingu: `GET /api/v2/order/{orderId}/tracking/` (jeśli dostępny)
|
||||
|
||||
---
|
||||
|
||||
## Allegro Shipment Management API
|
||||
|
||||
Endpoint: `GET /shipment-management/shipments/{shipmentId}`
|
||||
Pole statusu: `status` (string)
|
||||
|
||||
### Statusy przesyłki (Shipment Management)
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `NEW` | Przesyłka utworzona, nie przekazana przewoźnikowi |
|
||||
| `READY_TO_SHIP` | Etykieta wygenerowana, oczekuje na odbiór/nadanie |
|
||||
| `IN_TRANSIT` | Przesyłka odebrana przez przewoźnika |
|
||||
| `DELIVERED` | Przesyłka doręczona |
|
||||
| `CANCELLED` | Przesyłka anulowana |
|
||||
| `ERROR` | Błąd przetwarzania |
|
||||
| `RETURNED` | Przesyłka zwrócona do nadawcy |
|
||||
|
||||
### Statusy realizacji zamówienia (Order Fulfillment)
|
||||
|
||||
Endpoint: `GET /order/checkout-forms/{checkoutFormId}`
|
||||
Pole: `fulfillment.status`
|
||||
|
||||
| Status | Opis PL |
|
||||
|--------|---------|
|
||||
| `NEW` | Zamówienie złożone, nie przetworzone |
|
||||
| `PROCESSING` | Sprzedawca przetwarza zamówienie |
|
||||
| `READY_FOR_SHIPMENT` | Spakowane, oczekuje na wysyłkę |
|
||||
| `SENT` | Wysłane (numer śledzenia podany) |
|
||||
| `DELIVERED` | Potwierdzone doręczenie |
|
||||
| `CANCELLED` | Zamówienie anulowane |
|
||||
|
||||
### Ważne ograniczenia
|
||||
- **Allegro NIE ma dedykowanego API śledzenia przesyłek** (brak endpointu typu `/tracking/{trackingNumber}`)
|
||||
- Shipment Management API daje statusy TYLKO dla przesyłek utworzonych przez zintegrowanych przewoźników Allegro
|
||||
- Dla szczegółowego śledzenia trzeba odpytywać **API przewoźnika bezpośrednio** (InPost, DPD, DHL, etc.)
|
||||
- Dla ręcznie wpisanych numerów śledzenia — brak automatycznego trackingu przez Allegro
|
||||
|
||||
---
|
||||
|
||||
## Mapowanie na statusy znormalizowane
|
||||
|
||||
System orderPRO używa dwupoziomowego systemu statusów:
|
||||
1. **`delivery_status`** — znormalizowany status (dla UI, filtrów, logiki)
|
||||
2. **`delivery_status_raw`** — surowy status z API przewoźnika
|
||||
|
||||
### Tabela mapowania
|
||||
|
||||
| Znormalizowany | InPost ShipX | Apaczka | Allegro SM |
|
||||
|----------------|-------------|---------|------------|
|
||||
| `unknown` | *(brak danych)* | *(brak danych)* | *(brak danych)* |
|
||||
| `created` | `created`, `offers_prepared`, `offer_selected` | `0` (PENDING) | `NEW` |
|
||||
| `confirmed` | `confirmed`, `dispatched` | `1` (CONFIRMED) | `READY_TO_SHIP` |
|
||||
| `in_transit` | `collected`, `taken_by_courier`, `adopted_at_sorting_center`, `sent_from_sorting_center`, `adopted_at_target_sorting_center`, `sent_from_target_sorting_center`, `adopted_at_target_branch`, `adopted_at_source_branch` | `2` (PICKED_UP), `3` (IN_TRANSIT) | `IN_TRANSIT` |
|
||||
| `out_for_delivery` | `out_for_delivery` | `4` (OUT_FOR_DELIVERY) | — |
|
||||
| `ready_for_pickup` | `ready_to_pickup`, `ready_to_pickup_from_branch`, `ready_to_pickup_from_pok`, `stack_in_box_machine`, `stack_in_customer_service_point` | `9` (WAITING_FOR_PICKUP) | — |
|
||||
| `delivered` | `delivered`, `claimed` | `5` (DELIVERED) | `DELIVERED` |
|
||||
| `returned` | `returned_to_sender`, `undelivered`, `undelivered_wrong_address`, `undelivered_incomplete_address`, `undelivered_unknown_recipient`, `undelivered_cod_cash_receiver` | `6` (RETURNED) | `RETURNED` |
|
||||
| `cancelled` | `cancelled`, `expired` | `7` (CANCELLED) | `CANCELLED` |
|
||||
| `problem` | `avizo`, `pickup_time_expired`, `stack_parcel_pickup_time_expired`, `missing`, `delay_in_delivery`, `oversized` | `8` (ERROR), `10` (REDIRECT) | `ERROR` |
|
||||
|
||||
### Uwagi do mapowania
|
||||
- Status `problem` zbiera wszystkie nietypowe sytuacje wymagające uwagi (awizo, zagubienie, opóźnienie)
|
||||
- Surowy status (`delivery_status_raw`) zawsze zachowuje oryginalną wartość z API
|
||||
- Dla przesyłek `manual` — `delivery_status` zawsze `unknown` (brak automatycznego trackingu)
|
||||
- Mapowanie powinno być zaimplementowane jako stałe w klasie per provider (łatwe do rozszerzenia)
|
||||
|
||||
---
|
||||
|
||||
*Dokument utworzony: 2026-03-23*
|
||||
*Źródła: InPost ShipX API docs, Apaczka API v2 docs (ograniczony dostęp), Allegro REST API docs*
|
||||
@@ -0,0 +1,31 @@
|
||||
-- Migration: Add delivery tracking columns to shipment_packages
|
||||
-- Phase 27: Shipment Tracking Backend
|
||||
|
||||
-- Idempotent: skip if columns already exist
|
||||
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'shipment_packages' AND COLUMN_NAME = 'delivery_status');
|
||||
|
||||
SET @sql_add = IF(@col_exists = 0,
|
||||
'ALTER TABLE shipment_packages
|
||||
ADD COLUMN delivery_status VARCHAR(32) NOT NULL DEFAULT ''unknown'' AFTER status,
|
||||
ADD COLUMN delivery_status_raw VARCHAR(128) NULL AFTER delivery_status,
|
||||
ADD COLUMN delivery_status_updated_at DATETIME NULL AFTER delivery_status_raw',
|
||||
'SELECT 1');
|
||||
PREPARE stmt FROM @sql_add;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Add index (idempotent)
|
||||
SET @idx_exists = (SELECT COUNT(*) FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'shipment_packages' AND INDEX_NAME = 'idx_delivery_status');
|
||||
|
||||
SET @sql_idx = IF(@idx_exists = 0,
|
||||
'ALTER TABLE shipment_packages ADD INDEX idx_delivery_status (delivery_status)',
|
||||
'SELECT 1');
|
||||
PREPARE stmt FROM @sql_idx;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Set initial delivery_status for existing packages based on current status
|
||||
UPDATE shipment_packages SET delivery_status = 'delivered' WHERE status = 'label_ready' AND delivery_status = 'unknown';
|
||||
UPDATE shipment_packages SET delivery_status = 'confirmed' WHERE status = 'created' AND provider != 'manual' AND delivery_status = 'unknown';
|
||||
@@ -0,0 +1,6 @@
|
||||
-- Migration: Add shipment_tracking_sync cron schedule
|
||||
-- Phase 27: Shipment Tracking Backend
|
||||
|
||||
-- Idempotent: skip if already exists
|
||||
INSERT IGNORE INTO cron_schedules (job_type, interval_seconds, priority, max_attempts, enabled)
|
||||
VALUES ('shipment_tracking_sync', 900, 5, 3, 1);
|
||||
@@ -24,6 +24,14 @@ use App\Modules\Settings\ShopproProductImageResolver;
|
||||
use App\Modules\Settings\ShopproPaymentStatusSyncService;
|
||||
use App\Modules\Settings\ShopproStatusMappingRepository;
|
||||
use App\Modules\Settings\ShopproStatusSyncService;
|
||||
use App\Modules\Shipments\AllegroTrackingService;
|
||||
use App\Modules\Shipments\ApaczkaTrackingService;
|
||||
use App\Modules\Shipments\InpostTrackingService;
|
||||
use App\Modules\Shipments\ShipmentPackageRepository;
|
||||
use App\Modules\Shipments\ShipmentTrackingRegistry;
|
||||
use App\Modules\Settings\ApaczkaApiClient;
|
||||
use App\Modules\Settings\ApaczkaIntegrationRepository;
|
||||
use App\Modules\Settings\InpostIntegrationRepository;
|
||||
use PDO;
|
||||
|
||||
final class CronHandlerFactory
|
||||
@@ -102,6 +110,22 @@ final class CronHandlerFactory
|
||||
'shoppro_payment_status_sync' => new ShopproPaymentStatusSyncHandler(
|
||||
$shopproPaymentSyncService
|
||||
),
|
||||
'shipment_tracking_sync' => new ShipmentTrackingHandler(
|
||||
new ShipmentTrackingRegistry([
|
||||
new InpostTrackingService(
|
||||
new InpostIntegrationRepository($this->db, $this->integrationSecret)
|
||||
),
|
||||
new ApaczkaTrackingService(
|
||||
new ApaczkaApiClient(),
|
||||
new ApaczkaIntegrationRepository($this->db, $this->integrationSecret)
|
||||
),
|
||||
new AllegroTrackingService(
|
||||
$apiClient,
|
||||
$tokenManager
|
||||
),
|
||||
]),
|
||||
new ShipmentPackageRepository($this->db)
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
59
src/Modules/Cron/ShipmentTrackingHandler.php
Normal file
59
src/Modules/Cron/ShipmentTrackingHandler.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Cron;
|
||||
|
||||
use App\Modules\Shipments\ShipmentPackageRepository;
|
||||
use App\Modules\Shipments\ShipmentTrackingRegistry;
|
||||
use Throwable;
|
||||
|
||||
final class ShipmentTrackingHandler
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ShipmentTrackingRegistry $registry,
|
||||
private readonly ShipmentPackageRepository $repository
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function handle(array $_payload): array
|
||||
{
|
||||
$packages = $this->repository->findActiveForTracking();
|
||||
$total = count($packages);
|
||||
$updated = 0;
|
||||
$errors = 0;
|
||||
|
||||
foreach ($packages as $package) {
|
||||
$provider = trim((string) ($package['provider'] ?? ''));
|
||||
$packageId = (int) ($package['id'] ?? 0);
|
||||
|
||||
$service = $this->registry->getForProvider($provider);
|
||||
if ($service === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $service->getDeliveryStatus($package);
|
||||
if ($result !== null) {
|
||||
$this->repository->updateDeliveryStatus(
|
||||
$packageId,
|
||||
$result['status'],
|
||||
$result['status_raw']
|
||||
);
|
||||
$updated++;
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$errors++;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'total' => $total,
|
||||
'updated' => $updated,
|
||||
'errors' => $errors,
|
||||
];
|
||||
}
|
||||
}
|
||||
53
src/Modules/Shipments/AllegroTrackingService.php
Normal file
53
src/Modules/Shipments/AllegroTrackingService.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
use App\Modules\Settings\AllegroApiClient;
|
||||
use App\Modules\Settings\AllegroTokenManager;
|
||||
use Throwable;
|
||||
|
||||
final class AllegroTrackingService implements ShipmentTrackingInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AllegroApiClient $apiClient,
|
||||
private readonly AllegroTokenManager $tokenManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function supports(string $provider): bool
|
||||
{
|
||||
return $provider === 'allegro_wza';
|
||||
}
|
||||
|
||||
public function getDeliveryStatus(array $package): ?array
|
||||
{
|
||||
$shipmentId = trim((string) ($package['shipment_id'] ?? ''));
|
||||
if ($shipmentId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->fetchStatus($shipmentId);
|
||||
}
|
||||
|
||||
private function fetchStatus(string $shipmentId): ?array
|
||||
{
|
||||
try {
|
||||
[$accessToken, $env] = $this->tokenManager->resolveToken();
|
||||
$details = $this->apiClient->getShipmentDetails($env, $accessToken, $shipmentId);
|
||||
|
||||
$rawStatus = strtoupper(trim((string) ($details['status'] ?? '')));
|
||||
if ($rawStatus === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => DeliveryStatus::normalize('allegro_wza', $rawStatus),
|
||||
'status_raw' => $rawStatus,
|
||||
'description' => DeliveryStatus::description('allegro_wza', $rawStatus),
|
||||
];
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
77
src/Modules/Shipments/ApaczkaTrackingService.php
Normal file
77
src/Modules/Shipments/ApaczkaTrackingService.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
use App\Modules\Settings\ApaczkaApiClient;
|
||||
use App\Modules\Settings\ApaczkaIntegrationRepository;
|
||||
use Throwable;
|
||||
|
||||
final class ApaczkaTrackingService implements ShipmentTrackingInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApaczkaApiClient $apiClient,
|
||||
private readonly ApaczkaIntegrationRepository $integrationRepository
|
||||
) {
|
||||
}
|
||||
|
||||
public function supports(string $provider): bool
|
||||
{
|
||||
return $provider === 'apaczka';
|
||||
}
|
||||
|
||||
public function getDeliveryStatus(array $package): ?array
|
||||
{
|
||||
$shipmentId = trim((string) ($package['shipment_id'] ?? ''));
|
||||
if ($shipmentId === '' || !ctype_digit($shipmentId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$credentials = $this->resolveCredentials();
|
||||
if ($credentials === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->fetchStatus($shipmentId, $credentials[0], $credentials[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: string, 1: string}|null
|
||||
*/
|
||||
private function resolveCredentials(): ?array
|
||||
{
|
||||
try {
|
||||
$credentials = $this->integrationRepository->getApiCredentials();
|
||||
if ($credentials === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$appId = trim((string) ($credentials['app_id'] ?? ''));
|
||||
$appSecret = trim((string) ($credentials['app_secret'] ?? ''));
|
||||
|
||||
return ($appId !== '' && $appSecret !== '') ? [$appId, $appSecret] : null;
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function fetchStatus(string $shipmentId, string $appId, string $appSecret): ?array
|
||||
{
|
||||
try {
|
||||
$details = $this->apiClient->getOrderDetails($appId, $appSecret, (int) $shipmentId);
|
||||
$order = is_array($details['response']['order'] ?? null) ? $details['response']['order'] : [];
|
||||
$rawStatus = (string) ($order['status'] ?? '');
|
||||
if ($rawStatus === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => DeliveryStatus::normalize('apaczka', $rawStatus),
|
||||
'status_raw' => $rawStatus,
|
||||
'description' => DeliveryStatus::description('apaczka', $rawStatus),
|
||||
];
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
203
src/Modules/Shipments/DeliveryStatus.php
Normal file
203
src/Modules/Shipments/DeliveryStatus.php
Normal file
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
final class DeliveryStatus
|
||||
{
|
||||
public const UNKNOWN = 'unknown';
|
||||
public const CREATED = 'created';
|
||||
public const CONFIRMED = 'confirmed';
|
||||
public const IN_TRANSIT = 'in_transit';
|
||||
public const OUT_FOR_DELIVERY = 'out_for_delivery';
|
||||
public const READY_FOR_PICKUP = 'ready_for_pickup';
|
||||
public const DELIVERED = 'delivered';
|
||||
public const RETURNED = 'returned';
|
||||
public const CANCELLED = 'cancelled';
|
||||
public const PROBLEM = 'problem';
|
||||
|
||||
public const TERMINAL_STATUSES = [
|
||||
self::DELIVERED,
|
||||
self::RETURNED,
|
||||
self::CANCELLED,
|
||||
];
|
||||
|
||||
public const LABEL_PL = [
|
||||
self::UNKNOWN => 'Nieznany',
|
||||
self::CREATED => 'Utworzona',
|
||||
self::CONFIRMED => 'Potwierdzona',
|
||||
self::IN_TRANSIT => 'W tranzycie',
|
||||
self::OUT_FOR_DELIVERY => 'W doręczeniu',
|
||||
self::READY_FOR_PICKUP => 'Gotowa do odbioru',
|
||||
self::DELIVERED => 'Doręczona',
|
||||
self::RETURNED => 'Zwrócona',
|
||||
self::CANCELLED => 'Anulowana',
|
||||
self::PROBLEM => 'Problem',
|
||||
];
|
||||
|
||||
private const INPOST_MAP = [
|
||||
'created' => self::CREATED,
|
||||
'offers_prepared' => self::CREATED,
|
||||
'offer_selected' => self::CREATED,
|
||||
'confirmed' => self::CONFIRMED,
|
||||
'dispatched' => self::CONFIRMED,
|
||||
'collected' => self::IN_TRANSIT,
|
||||
'taken_by_courier' => self::IN_TRANSIT,
|
||||
'adopted_at_source_branch' => self::IN_TRANSIT,
|
||||
'adopted_at_sorting_center' => self::IN_TRANSIT,
|
||||
'sent_from_sorting_center' => self::IN_TRANSIT,
|
||||
'adopted_at_target_sorting_center' => self::IN_TRANSIT,
|
||||
'sent_from_target_sorting_center' => self::IN_TRANSIT,
|
||||
'adopted_at_target_branch' => self::IN_TRANSIT,
|
||||
'out_for_delivery' => self::OUT_FOR_DELIVERY,
|
||||
'ready_to_pickup' => self::READY_FOR_PICKUP,
|
||||
'ready_to_pickup_from_branch' => self::READY_FOR_PICKUP,
|
||||
'ready_to_pickup_from_pok' => self::READY_FOR_PICKUP,
|
||||
'stack_in_box_machine' => self::READY_FOR_PICKUP,
|
||||
'stack_in_customer_service_point' => self::READY_FOR_PICKUP,
|
||||
'delivered' => self::DELIVERED,
|
||||
'claimed' => self::DELIVERED,
|
||||
'returned_to_sender' => self::RETURNED,
|
||||
'undelivered' => self::RETURNED,
|
||||
'undelivered_wrong_address' => self::RETURNED,
|
||||
'undelivered_incomplete_address' => self::RETURNED,
|
||||
'undelivered_unknown_recipient' => self::RETURNED,
|
||||
'undelivered_cod_cash_receiver' => self::RETURNED,
|
||||
'cancelled' => self::CANCELLED,
|
||||
'expired' => self::CANCELLED,
|
||||
'avizo' => self::PROBLEM,
|
||||
'pickup_time_expired' => self::PROBLEM,
|
||||
'stack_parcel_pickup_time_expired' => self::PROBLEM,
|
||||
'missing' => self::PROBLEM,
|
||||
'delay_in_delivery' => self::PROBLEM,
|
||||
'oversized' => self::PROBLEM,
|
||||
'pickup_reminder_sent' => self::READY_FOR_PICKUP,
|
||||
'pickup_reminder_sent_address' => self::READY_FOR_PICKUP,
|
||||
'readdressed' => self::IN_TRANSIT,
|
||||
'redirect_to_box' => self::IN_TRANSIT,
|
||||
];
|
||||
|
||||
private const INPOST_DESCRIPTIONS = [
|
||||
'created' => 'Przesyłka utworzona',
|
||||
'offers_prepared' => 'Oferty cenowe przygotowane',
|
||||
'offer_selected' => 'Oferta wybrana',
|
||||
'confirmed' => 'Przesyłka potwierdzona',
|
||||
'dispatched' => 'Przesyłka nadana',
|
||||
'collected' => 'Odebrana od nadawcy',
|
||||
'taken_by_courier' => 'Odebrana przez kuriera',
|
||||
'adopted_at_source_branch' => 'Przyjęta w oddziale źródłowym',
|
||||
'adopted_at_sorting_center' => 'Przyjęta w centrum sortowania',
|
||||
'sent_from_sorting_center' => 'Wysłana z centrum sortowania',
|
||||
'adopted_at_target_sorting_center' => 'Przyjęta w docelowym centrum sortowania',
|
||||
'sent_from_target_sorting_center' => 'Wysłana z docelowego centrum sortowania',
|
||||
'adopted_at_target_branch' => 'Przyjęta w oddziale docelowym',
|
||||
'out_for_delivery' => 'W drodze do odbiorcy',
|
||||
'ready_to_pickup' => 'Gotowa do odbioru w paczkomacie',
|
||||
'ready_to_pickup_from_branch' => 'Gotowa do odbioru z oddziału',
|
||||
'ready_to_pickup_from_pok' => 'Gotowa do odbioru z POK',
|
||||
'stack_in_box_machine' => 'Umieszczona w paczkomacie',
|
||||
'stack_in_customer_service_point' => 'Umieszczona w punkcie obsługi',
|
||||
'delivered' => 'Doręczona',
|
||||
'claimed' => 'Odebrana po awizo',
|
||||
'returned_to_sender' => 'Zwrócona do nadawcy',
|
||||
'undelivered' => 'Niedoręczona',
|
||||
'undelivered_wrong_address' => 'Niedoręczona — błędny adres',
|
||||
'undelivered_incomplete_address' => 'Niedoręczona — niepełny adres',
|
||||
'undelivered_unknown_recipient' => 'Niedoręczona — nieznany odbiorca',
|
||||
'undelivered_cod_cash_receiver' => 'Niedoręczona — problem z pobraniem',
|
||||
'cancelled' => 'Anulowana',
|
||||
'expired' => 'Wygasła',
|
||||
'avizo' => 'Awizowana',
|
||||
'pickup_time_expired' => 'Czas odbioru upłynął',
|
||||
'stack_parcel_pickup_time_expired' => 'Czas odbioru ze stack upłynął',
|
||||
'missing' => 'Przesyłka zagubiona',
|
||||
'delay_in_delivery' => 'Opóźnienie w dostawie',
|
||||
'oversized' => 'Przesyłka ponadgabarytowa',
|
||||
'pickup_reminder_sent' => 'Wysłano przypomnienie o odbiorze',
|
||||
'pickup_reminder_sent_address' => 'Przypomnienie wysłane na adres',
|
||||
'readdressed' => 'Przekierowana na inny adres',
|
||||
'redirect_to_box' => 'Przekierowana do paczkomatu',
|
||||
];
|
||||
|
||||
private const APACZKA_MAP = [
|
||||
'0' => self::CREATED,
|
||||
'1' => self::CONFIRMED,
|
||||
'2' => self::IN_TRANSIT,
|
||||
'3' => self::IN_TRANSIT,
|
||||
'4' => self::OUT_FOR_DELIVERY,
|
||||
'5' => self::DELIVERED,
|
||||
'6' => self::RETURNED,
|
||||
'7' => self::CANCELLED,
|
||||
'8' => self::PROBLEM,
|
||||
'9' => self::READY_FOR_PICKUP,
|
||||
'10' => self::IN_TRANSIT,
|
||||
];
|
||||
|
||||
private const APACZKA_DESCRIPTIONS = [
|
||||
'0' => 'Oczekuje na przetworzenie',
|
||||
'1' => 'Zamówienie potwierdzone',
|
||||
'2' => 'Odebrana przez kuriera',
|
||||
'3' => 'W transporcie',
|
||||
'4' => 'W doręczeniu',
|
||||
'5' => 'Doręczona',
|
||||
'6' => 'Zwrócona do nadawcy',
|
||||
'7' => 'Anulowana',
|
||||
'8' => 'Błąd zamówienia',
|
||||
'9' => 'Oczekuje na odbiór w punkcie',
|
||||
'10' => 'Przekierowana',
|
||||
];
|
||||
|
||||
private const ALLEGRO_MAP = [
|
||||
'NEW' => self::CREATED,
|
||||
'READY_TO_SHIP' => self::CONFIRMED,
|
||||
'IN_TRANSIT' => self::IN_TRANSIT,
|
||||
'DELIVERED' => self::DELIVERED,
|
||||
'CANCELLED' => self::CANCELLED,
|
||||
'ERROR' => self::PROBLEM,
|
||||
'RETURNED' => self::RETURNED,
|
||||
];
|
||||
|
||||
private const ALLEGRO_DESCRIPTIONS = [
|
||||
'NEW' => 'Przesyłka utworzona',
|
||||
'READY_TO_SHIP' => 'Etykieta wygenerowana, oczekuje na nadanie',
|
||||
'IN_TRANSIT' => 'Odebrana przez przewoźnika',
|
||||
'DELIVERED' => 'Doręczona',
|
||||
'CANCELLED' => 'Anulowana',
|
||||
'ERROR' => 'Błąd przetwarzania',
|
||||
'RETURNED' => 'Zwrócona do nadawcy',
|
||||
];
|
||||
|
||||
public static function normalize(string $provider, string $rawStatus): string
|
||||
{
|
||||
$map = match ($provider) {
|
||||
'inpost' => self::INPOST_MAP,
|
||||
'apaczka' => self::APACZKA_MAP,
|
||||
'allegro_wza' => self::ALLEGRO_MAP,
|
||||
default => [],
|
||||
};
|
||||
|
||||
return $map[$rawStatus] ?? self::UNKNOWN;
|
||||
}
|
||||
|
||||
public static function description(string $provider, string $rawStatus): string
|
||||
{
|
||||
$map = match ($provider) {
|
||||
'inpost' => self::INPOST_DESCRIPTIONS,
|
||||
'apaczka' => self::APACZKA_DESCRIPTIONS,
|
||||
'allegro_wza' => self::ALLEGRO_DESCRIPTIONS,
|
||||
default => [],
|
||||
};
|
||||
|
||||
return $map[$rawStatus] ?? $rawStatus;
|
||||
}
|
||||
|
||||
public static function label(string $status): string
|
||||
{
|
||||
return self::LABEL_PL[$status] ?? 'Nieznany';
|
||||
}
|
||||
|
||||
public static function isTerminal(string $status): bool
|
||||
{
|
||||
return in_array($status, self::TERMINAL_STATUSES, true);
|
||||
}
|
||||
}
|
||||
128
src/Modules/Shipments/InpostTrackingService.php
Normal file
128
src/Modules/Shipments/InpostTrackingService.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
use App\Modules\Settings\InpostIntegrationRepository;
|
||||
use Throwable;
|
||||
|
||||
final class InpostTrackingService implements ShipmentTrackingInterface
|
||||
{
|
||||
private const API_BASE_PRODUCTION = 'https://api-shipx-pl.easypack24.net/v1';
|
||||
private const API_BASE_SANDBOX = 'https://sandbox-api-shipx-pl.easypack24.net/v1';
|
||||
|
||||
public function __construct(
|
||||
private readonly InpostIntegrationRepository $inpostRepository
|
||||
) {
|
||||
}
|
||||
|
||||
public function supports(string $provider): bool
|
||||
{
|
||||
return $provider === 'inpost';
|
||||
}
|
||||
|
||||
public function getDeliveryStatus(array $package): ?array
|
||||
{
|
||||
$shipmentId = trim((string) ($package['shipment_id'] ?? ''));
|
||||
if ($shipmentId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->fetchStatus($shipmentId);
|
||||
}
|
||||
|
||||
private function fetchStatus(string $shipmentId): ?array
|
||||
{
|
||||
try {
|
||||
$token = $this->resolveToken();
|
||||
if ($token === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$settings = $this->inpostRepository->getSettings();
|
||||
$env = (string) ($settings['environment'] ?? 'sandbox');
|
||||
$url = $this->apiBaseUrl($env) . '/shipments/' . rawurlencode($shipmentId);
|
||||
|
||||
$response = $this->apiRequest($url, $token);
|
||||
$rawStatus = strtolower(trim((string) ($response['status'] ?? '')));
|
||||
|
||||
return $rawStatus !== '' ? [
|
||||
'status' => DeliveryStatus::normalize('inpost', $rawStatus),
|
||||
'status_raw' => $rawStatus,
|
||||
'description' => DeliveryStatus::description('inpost', $rawStatus),
|
||||
] : null;
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveToken(): ?string
|
||||
{
|
||||
$token = $this->inpostRepository->getDecryptedToken();
|
||||
return ($token !== null && trim($token) !== '') ? trim($token) : null;
|
||||
}
|
||||
|
||||
private function apiBaseUrl(string $environment): string
|
||||
{
|
||||
return strtolower(trim($environment)) === 'production'
|
||||
? self::API_BASE_PRODUCTION
|
||||
: self::API_BASE_SANDBOX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function apiRequest(string $url, string $token): array
|
||||
{
|
||||
$ch = curl_init($url);
|
||||
if ($ch === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$opts = [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
CURLOPT_CONNECTTIMEOUT => 5,
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_SSL_VERIFYHOST => 2,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Authorization: Bearer ' . $token,
|
||||
'Accept: application/json',
|
||||
],
|
||||
];
|
||||
|
||||
$caPath = $this->getCaBundlePath();
|
||||
if ($caPath !== null) {
|
||||
$opts[CURLOPT_CAINFO] = $caPath;
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, $opts);
|
||||
$body = curl_exec($ch);
|
||||
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$ch = null;
|
||||
|
||||
if ($body === false || $httpCode < 200 || $httpCode >= 300) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$json = json_decode((string) $body, true);
|
||||
return is_array($json) ? $json : [];
|
||||
}
|
||||
|
||||
private function getCaBundlePath(): ?string
|
||||
{
|
||||
$candidates = [
|
||||
(string) ($_ENV['CURL_CA_BUNDLE_PATH'] ?? ''),
|
||||
(string) ini_get('curl.cainfo'),
|
||||
'C:/xampp/apache/bin/curl-ca-bundle.crt',
|
||||
'C:/xampp/php/extras/ssl/cacert.pem',
|
||||
'/etc/ssl/certs/ca-certificates.crt',
|
||||
];
|
||||
foreach ($candidates as $path) {
|
||||
if ($path !== '' && is_file($path)) {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ final class ShipmentPackageRepository
|
||||
|
||||
$allowedFields = [
|
||||
'shipment_id', 'tracking_number', 'status', 'command_id',
|
||||
'label_path', 'error_message',
|
||||
'label_path', 'error_message', 'delivery_status', 'delivery_status_raw',
|
||||
];
|
||||
|
||||
foreach ($allowedFields as $field) {
|
||||
@@ -139,6 +139,40 @@ final class ShipmentPackageRepository
|
||||
return (int) $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function findActiveForTracking(): array
|
||||
{
|
||||
$statement = $this->pdo->prepare(
|
||||
"SELECT * FROM shipment_packages
|
||||
WHERE provider != 'manual'
|
||||
AND delivery_status NOT IN ('delivered', 'returned', 'cancelled')
|
||||
AND status IN ('created', 'label_ready')
|
||||
ORDER BY delivery_status_updated_at ASC
|
||||
LIMIT 50"
|
||||
);
|
||||
$statement->execute();
|
||||
return $statement->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
||||
}
|
||||
|
||||
public function updateDeliveryStatus(int $id, string $deliveryStatus, ?string $deliveryStatusRaw): void
|
||||
{
|
||||
$statement = $this->pdo->prepare(
|
||||
'UPDATE shipment_packages
|
||||
SET delivery_status = :delivery_status,
|
||||
delivery_status_raw = :delivery_status_raw,
|
||||
delivery_status_updated_at = NOW(),
|
||||
updated_at = NOW()
|
||||
WHERE id = :id'
|
||||
);
|
||||
$statement->execute([
|
||||
'id' => $id,
|
||||
'delivery_status' => $deliveryStatus,
|
||||
'delivery_status_raw' => $deliveryStatusRaw,
|
||||
]);
|
||||
}
|
||||
|
||||
public function findByCommandId(string $commandId): ?array
|
||||
{
|
||||
$statement = $this->pdo->prepare('SELECT * FROM shipment_packages WHERE command_id = :command_id LIMIT 1');
|
||||
|
||||
15
src/Modules/Shipments/ShipmentTrackingInterface.php
Normal file
15
src/Modules/Shipments/ShipmentTrackingInterface.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
interface ShipmentTrackingInterface
|
||||
{
|
||||
public function supports(string $provider): bool;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $package Row from shipment_packages table
|
||||
* @return array{status: string, status_raw: string, description: string}|null Null on error/unavailable
|
||||
*/
|
||||
public function getDeliveryStatus(array $package): ?array;
|
||||
}
|
||||
29
src/Modules/Shipments/ShipmentTrackingRegistry.php
Normal file
29
src/Modules/Shipments/ShipmentTrackingRegistry.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
final class ShipmentTrackingRegistry
|
||||
{
|
||||
/** @var ShipmentTrackingInterface[] */
|
||||
private readonly array $services;
|
||||
|
||||
/**
|
||||
* @param ShipmentTrackingInterface[] $services
|
||||
*/
|
||||
public function __construct(array $services)
|
||||
{
|
||||
$this->services = $services;
|
||||
}
|
||||
|
||||
public function getForProvider(string $provider): ?ShipmentTrackingInterface
|
||||
{
|
||||
foreach ($this->services as $service) {
|
||||
if ($service->supports($provider)) {
|
||||
return $service;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user