Phase 3 complete: - Zmieniono _csrf_token -> _token w OrdersController (1x), ShipmentController (2x) - Zmieniono name="_csrf_token" -> name="_token" w orders/show.php (1x), shipments/prepare.php (2x) - Usunięto concern z .paul/codebase/CONCERNS.md Co-Authored-By: Claude <noreply@anthropic.com>
740 lines
31 KiB
PHP
740 lines
31 KiB
PHP
<?php
|
|
$orderRow = is_array($order ?? null) ? $order : [];
|
|
$itemsList = is_array($items ?? null) ? $items : [];
|
|
$receiver = is_array($receiverAddr ?? null) ? $receiverAddr : [];
|
|
$prefs = is_array($preferences ?? null) ? $preferences : [];
|
|
$comp = is_array($company ?? null) ? $company : [];
|
|
$services = is_array($deliveryServices ?? null) ? $deliveryServices : [];
|
|
$apaczkaSvcList = is_array($apaczkaServices ?? null) ? $apaczkaServices : [];
|
|
$packages = is_array($existingPackages ?? null) ? $existingPackages : [];
|
|
$servicesError = (string) ($deliveryServicesError ?? '');
|
|
$flashSuccessMsg = (string) ($flashSuccess ?? '');
|
|
$flashErrorMsg = (string) ($flashError ?? '');
|
|
$deliveryMappingDiagnostic = trim((string) ($deliveryMappingDiagnostic ?? ''));
|
|
|
|
$mapping = is_array($deliveryMapping ?? null) ? $deliveryMapping : [];
|
|
$mappedMethodId = trim((string) ($mapping['provider_service_id'] ?? ''));
|
|
$mappedCredentialsId = trim((string) ($mapping['provider_account_id'] ?? ''));
|
|
$mappedCarrierId = trim((string) ($mapping['provider_carrier_id'] ?? ''));
|
|
$mappedProvider = trim((string) ($mapping['provider'] ?? ''));
|
|
$mappedServiceName = trim((string) ($mapping['provider_service_name'] ?? ''));
|
|
$mappedCarrier = $mappedProvider === 'apaczka' ? 'apaczka' : 'allegro';
|
|
if ($mappedCarrier !== 'apaczka' && stripos($mappedCarrierId, 'inpost') !== false) {
|
|
$mappedCarrier = 'inpost';
|
|
}
|
|
$deliveryMethodId = '';
|
|
if ($mappedCarrier === 'apaczka' && $mappedMethodId !== '') {
|
|
$deliveryMethodId = $mappedMethodId;
|
|
} elseif (($mappedCarrier === 'allegro' || $mappedCarrier === 'inpost') && $mappedMethodId !== '') {
|
|
$deliveryMethodId = $mappedMethodId;
|
|
} elseif ($mappedCarrier !== 'inpost') {
|
|
$deliveryMethodId = trim((string) ($prefs['delivery_method_id'] ?? ($orderRow['external_carrier_account_id'] ?? '')));
|
|
}
|
|
$deliveryMethodName = trim((string) ($orderRow['external_carrier_id'] ?? ''));
|
|
$inpostSvcList = is_array($inpostServices ?? null) ? $inpostServices : [];
|
|
$preselectedCarrier = $mappedCarrier !== '' ? $mappedCarrier : ($mappedMethodId !== '' ? 'allegro' : '');
|
|
$pointId = trim((string) ($receiver['parcel_external_id'] ?? ''));
|
|
$pointName = trim((string) ($receiver['parcel_name'] ?? ''));
|
|
$totalWithTax = (float) ($orderRow['total_with_tax'] ?? 0);
|
|
$currency = strtoupper(trim((string) ($orderRow['currency'] ?? 'PLN')));
|
|
$isCod = strtoupper(trim((string) ($orderRow['external_payment_type_id'] ?? ''))) === 'CASH_ON_DELIVERY';
|
|
$defaultCodAmount = $isCod ? number_format($totalWithTax, 2, '.', '') : '0';
|
|
?>
|
|
|
|
<section class="card">
|
|
<div class="order-details-head">
|
|
<div>
|
|
<a href="/orders/<?= $e((string) ($orderId ?? 0)) ?>" class="order-back-link">← Powrot do zamowienia</a>
|
|
<h2 class="section-title mt-12">Przygotuj przesylke #<?= $e((string) ($orderId ?? 0)) ?></h2>
|
|
<div class="order-details-sub mt-4">
|
|
<span><?= $e(ucfirst((string) ($orderRow['source'] ?? ''))) ?> <?= $e((string) ($orderRow['external_order_id'] ?? '')) ?></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($flashSuccessMsg !== ''): ?>
|
|
<div class="flash flash--success mt-12"><?= $e($flashSuccessMsg) ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($flashErrorMsg !== ''): ?>
|
|
<div class="flash flash--error mt-12"><?= $e($flashErrorMsg) ?></div>
|
|
<?php endif; ?>
|
|
</section>
|
|
|
|
<?php if ($packages !== []): ?>
|
|
<section class="card mt-16">
|
|
<h3 class="section-title">Utworzone przesylki</h3>
|
|
<div class="table-wrap mt-12">
|
|
<table class="table table--details">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Status</th>
|
|
<th>Nr sledzenia</th>
|
|
<th>Przewoznik</th>
|
|
<th>Etykieta</th>
|
|
<th>Utworzono</th>
|
|
<th>Akcje</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($packages as $pkg): ?>
|
|
<?php
|
|
$pkgId = (int) ($pkg['id'] ?? 0);
|
|
$pkgStatus = (string) ($pkg['status'] ?? 'draft');
|
|
$pkgTracking = trim((string) ($pkg['tracking_number'] ?? ''));
|
|
$pkgCarrier = trim((string) ($pkg['carrier_id'] ?? ''));
|
|
$pkgLabelPath = trim((string) ($pkg['label_path'] ?? ''));
|
|
$pkgShipmentId = trim((string) ($pkg['shipment_id'] ?? ''));
|
|
$pkgError = trim((string) ($pkg['error_message'] ?? ''));
|
|
?>
|
|
<tr>
|
|
<td><?= $e((string) $pkgId) ?></td>
|
|
<td>
|
|
<span class="order-tag <?= $pkgStatus === 'label_ready' || $pkgStatus === 'created' ? 'is-success' : ($pkgStatus === 'error' ? 'is-danger' : 'is-warn') ?>">
|
|
<?= $e($pkgStatus) ?>
|
|
</span>
|
|
<?php if ($pkgError !== ''): ?>
|
|
<div class="muted mt-4" style="font-size:0.75rem"><?= $e($pkgError) ?></div>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><?= $e($pkgTracking !== '' ? $pkgTracking : '-') ?></td>
|
|
<td><?= $e($pkgCarrier !== '' ? $pkgCarrier : '-') ?></td>
|
|
<td>
|
|
<?php if ($pkgLabelPath !== ''): ?>
|
|
<?php if ($pkgStatus === 'error'): ?>
|
|
-
|
|
<?php else: ?>
|
|
<form method="post" action="/orders/<?= $e((string) ($orderId ?? 0)) ?>/shipment/<?= $e((string) $pkgId) ?>/label" style="display:inline">
|
|
<input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
|
|
<button type="submit" class="btn btn--sm btn--secondary">Pobierz</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<?php elseif ($pkgShipmentId !== '' && $pkgStatus === 'created'): ?>
|
|
<span class="muted">Generowanie etykiety...</span>
|
|
<?php else: ?>
|
|
-
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-nowrap"><?= $e((string) ($pkg['created_at'] ?? '')) ?></td>
|
|
<td>
|
|
<?php
|
|
$shouldCheckStatus = $pkgStatus === 'pending' || ($pkgStatus === 'created' && $pkgLabelPath === '');
|
|
?>
|
|
<?php if ($shouldCheckStatus): ?>
|
|
<button type="button"
|
|
class="btn btn--sm btn--secondary"
|
|
data-check-status="<?= $e((string) $pkgId) ?>"
|
|
data-order-id="<?= $e((string) ($orderId ?? 0)) ?>"
|
|
data-package-status="<?= $e($pkgStatus) ?>"
|
|
data-auto-check="1">Sprawdz status</button>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
<?php endif; ?>
|
|
|
|
<form method="post" action="/orders/<?= $e((string) ($orderId ?? 0)) ?>/shipment/create" novalidate>
|
|
<input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
|
|
|
|
<div class="shipment-grid mt-16">
|
|
<section class="card">
|
|
<h3 class="section-title">Przesylka</h3>
|
|
|
|
<?php if ($servicesError !== ''): ?>
|
|
<div class="flash flash--error mt-12"><?= $e($servicesError) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="form-field mt-12">
|
|
<span class="field-label">Przewoznik</span>
|
|
<select class="form-control" id="shipment-carrier-select">
|
|
<option value="">-- Wybierz --</option>
|
|
<option value="allegro"<?= $preselectedCarrier === 'allegro' ? ' selected' : '' ?>>Allegro</option>
|
|
<option value="inpost"<?= $preselectedCarrier === 'inpost' ? ' selected' : '' ?>>InPost</option>
|
|
<option value="apaczka"<?= $preselectedCarrier === 'apaczka' ? ' selected' : '' ?>>Apaczka</option>
|
|
</select>
|
|
<?php if ($deliveryMethodName !== ''): ?>
|
|
<div class="muted mt-4" style="font-size:12px">Metoda z zamowienia: <strong><?= $e($deliveryMethodName) ?></strong><?php if ($mappedServiceName !== ''): ?> → <?= $e($mappedCarrier === 'inpost' ? 'InPost' : ($mappedCarrier === 'apaczka' ? 'Apaczka' : 'Allegro')) ?>: <?= $e($mappedServiceName) ?><?php endif; ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($deliveryMappingDiagnostic !== ''): ?>
|
|
<div class="flash flash--error mt-8"><?= $e($deliveryMappingDiagnostic) ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-field mt-12">
|
|
<span class="field-label">Usluga dostawy</span>
|
|
<input type="hidden" name="delivery_method_id" id="shipment-delivery-service" value="" required>
|
|
|
|
<div id="shipment-allegro-panel" style="<?= $preselectedCarrier !== 'allegro' ? 'display:none' : '' ?>">
|
|
<?php if ($servicesError !== ''): ?>
|
|
<div class="flash flash--error mt-4"><?= $e($servicesError) ?></div>
|
|
<?php endif; ?>
|
|
<div class="searchable-select" id="shipment-service-wrapper" data-match-id="<?= $e($deliveryMethodId) ?>">
|
|
<input type="text" class="form-control" id="shipment-service-search" placeholder="Szukaj uslugi dostawy Allegro..." autocomplete="off">
|
|
<div class="searchable-select__dropdown" id="shipment-service-dropdown">
|
|
<?php foreach ($services as $svc): ?>
|
|
<?php
|
|
$svcId = is_array($svc['id'] ?? null) ? $svc['id'] : [];
|
|
$svcMethodId = trim((string) ($svcId['deliveryMethodId'] ?? ''));
|
|
$svcCredentialsId = trim((string) ($svcId['credentialsId'] ?? ''));
|
|
$svcName = trim((string) ($svc['name'] ?? ''));
|
|
$svcCarrierId = trim((string) ($svc['carrierId'] ?? ''));
|
|
$svcOwner = trim((string) ($svc['owner'] ?? ''));
|
|
?>
|
|
<div class="searchable-select__option"
|
|
data-value="<?= $e($svcMethodId) ?>"
|
|
data-credentials-id="<?= $e($svcCredentialsId) ?>"
|
|
data-carrier-id="<?= $e($svcCarrierId) ?>"
|
|
data-owner="<?= $e($svcOwner) ?>"
|
|
data-label="<?= $e($svcName) ?> (<?= $e($svcOwner) ?>)"
|
|
><?= $e($svcName) ?> <span class="muted">(<?= $e($svcOwner) ?>)</span></div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="shipment-inpost-panel" style="<?= $preselectedCarrier !== 'inpost' ? 'display:none' : '' ?>">
|
|
<?php if ($inpostSvcList === []): ?>
|
|
<div class="muted">Brak uslug InPost (sprawdz polaczenie z Allegro).</div>
|
|
<?php else: ?>
|
|
<select class="form-control" id="shipment-inpost-select">
|
|
<option value="">-- Wybierz usluge InPost --</option>
|
|
<?php foreach ($inpostSvcList as $inSvc): ?>
|
|
<?php
|
|
$inSvcId = is_array($inSvc['id'] ?? null) ? $inSvc['id'] : [];
|
|
$inSvcMethodId = trim((string) ($inSvcId['deliveryMethodId'] ?? ''));
|
|
$inSvcCredentialsId = trim((string) ($inSvcId['credentialsId'] ?? ''));
|
|
$inSvcCarrierId = trim((string) ($inSvc['carrierId'] ?? ''));
|
|
$inSvcName = trim((string) ($inSvc['name'] ?? ''));
|
|
$inSvcSelected = $mappedCarrier === 'inpost' && $mappedMethodId === $inSvcMethodId;
|
|
?>
|
|
<option
|
|
value="<?= $e($inSvcMethodId) ?>"
|
|
data-credentials-id="<?= $e($inSvcCredentialsId) ?>"
|
|
data-carrier-id="<?= $e($inSvcCarrierId) ?>"
|
|
<?= $inSvcSelected ? 'selected' : '' ?>>
|
|
<?= $e($inSvcName) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div id="shipment-apaczka-panel" style="<?= $preselectedCarrier !== 'apaczka' ? 'display:none' : '' ?>">
|
|
<?php if ($apaczkaSvcList === []): ?>
|
|
<div class="muted">Brak uslug Apaczka (sprawdz konfiguracje App ID/App Secret).</div>
|
|
<?php else: ?>
|
|
<select class="form-control" id="shipment-apaczka-select">
|
|
<option value="">-- Wybierz usluge Apaczka --</option>
|
|
<?php foreach ($apaczkaSvcList as $aSvc): ?>
|
|
<?php
|
|
if (!is_array($aSvc)) {
|
|
continue;
|
|
}
|
|
$aSvcId = trim((string) ($aSvc['service_id'] ?? $aSvc['id'] ?? ''));
|
|
$aSvcName = trim((string) ($aSvc['name'] ?? ''));
|
|
$aSvcCarrierCode = trim((string) ($aSvc['carrier_code'] ?? ''));
|
|
$aSvcSelected = $mappedCarrier === 'apaczka' && $mappedMethodId === $aSvcId;
|
|
?>
|
|
<option
|
|
value="<?= $e($aSvcId) ?>"
|
|
data-carrier-id="<?= $e($aSvcCarrierCode) ?>"
|
|
<?= $aSvcSelected ? 'selected' : '' ?>>
|
|
<?= $e($aSvcName !== '' ? $aSvcName : ('ID ' . $aSvcId)) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div id="shipment-empty-panel" class="muted" style="<?= $preselectedCarrier !== '' ? 'display:none' : '' ?>">Wybierz przewoznika</div>
|
|
</div>
|
|
|
|
<input type="hidden" name="credentials_id" id="shipment-credentials-id" value="">
|
|
<input type="hidden" name="carrier_id" id="shipment-carrier-id" value="">
|
|
<input type="hidden" name="provider_code" id="shipment-provider-code" value="<?= $e($preselectedCarrier === 'apaczka' ? 'apaczka' : 'allegro_wza') ?>">
|
|
|
|
<label class="form-field">
|
|
<span class="field-label">Typ paczki</span>
|
|
<select class="form-control" name="package_type">
|
|
<option value="PACKAGE" selected>Paczka</option>
|
|
<option value="DOX">Dokument</option>
|
|
<option value="PALLET">Paleta</option>
|
|
</select>
|
|
</label>
|
|
|
|
<div class="form-grid-4">
|
|
<label class="form-field">
|
|
<span class="field-label">Dlugosc (cm)</span>
|
|
<input class="form-control" type="number" name="length_cm" step="0.1" min="0.1" value="<?= $e((string) ($comp['default_package_length_cm'] ?? '25')) ?>">
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">Szerokosc (cm)</span>
|
|
<input class="form-control" type="number" name="width_cm" step="0.1" min="0.1" value="<?= $e((string) ($comp['default_package_width_cm'] ?? '20')) ?>">
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">Wysokosc (cm)</span>
|
|
<input class="form-control" type="number" name="height_cm" step="0.1" min="0.1" value="<?= $e((string) ($comp['default_package_height_cm'] ?? '8')) ?>">
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">Waga (kg)</span>
|
|
<input class="form-control" type="number" name="weight_kg" step="0.001" min="0.001" value="<?= $e((string) ($comp['default_package_weight_kg'] ?? '1')) ?>">
|
|
</label>
|
|
</div>
|
|
|
|
<div class="form-grid-2">
|
|
<label class="form-field">
|
|
<span class="field-label">Ubezpieczenie (<?= $e($currency) ?>)</span>
|
|
<input class="form-control" type="number" name="insurance_amount" step="0.01" min="0" value="<?= $e(number_format($totalWithTax, 2, '.', '')) ?>">
|
|
<input type="hidden" name="insurance_currency" value="<?= $e($currency) ?>">
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">Format etykiety</span>
|
|
<select class="form-control" name="label_format">
|
|
<option value="PDF"<?= ((string) ($comp['default_label_format'] ?? 'PDF')) === 'PDF' ? ' selected' : '' ?>>PDF</option>
|
|
<option value="ZPL"<?= ((string) ($comp['default_label_format'] ?? 'PDF')) === 'ZPL' ? ' selected' : '' ?>>ZPL</option>
|
|
</select>
|
|
</label>
|
|
</div>
|
|
|
|
<div class="form-grid-2">
|
|
<label class="form-field">
|
|
<span class="field-label">Pobranie (<?= $e($currency) ?>)<?= $isCod ? ' <span class="order-tag is-cod" style="font-size:0.7rem;vertical-align:middle">ZA POBRANIEM</span>' : '' ?></span>
|
|
<input class="form-control" type="number" name="cod_amount" step="0.01" min="0" value="<?= $e($defaultCodAmount) ?>">
|
|
<input type="hidden" name="cod_currency" value="<?= $e($currency) ?>">
|
|
</label>
|
|
</div>
|
|
|
|
<label class="form-field">
|
|
<span class="field-label">Punkt nadania (opcjonalnie)</span>
|
|
<input class="form-control" type="text" name="sender_point_id" maxlength="64" placeholder="np. KRA010">
|
|
</label>
|
|
</section>
|
|
|
|
<section class="card">
|
|
<h3 class="section-title">Adres odbiorcy</h3>
|
|
|
|
<label class="form-field mt-12">
|
|
<span class="field-label">Imie i nazwisko</span>
|
|
<input class="form-control" type="text" name="receiver_name" maxlength="200" value="<?= $e((string) ($receiver['name'] ?? '')) ?>" required>
|
|
</label>
|
|
|
|
<label class="form-field">
|
|
<span class="field-label">Firma (opcjonalnie)</span>
|
|
<input class="form-control" type="text" name="receiver_company" maxlength="200" value="<?= $e((string) ($receiver['company_name'] ?? '')) ?>">
|
|
</label>
|
|
|
|
<label class="form-field">
|
|
<span class="field-label">Ulica</span>
|
|
<input class="form-control" type="text" name="receiver_street" maxlength="200" value="<?= $e((string) ($receiver['street_name'] ?? '')) ?>" required>
|
|
</label>
|
|
|
|
<div class="form-grid-3">
|
|
<label class="form-field">
|
|
<span class="field-label">Kod pocztowy</span>
|
|
<input class="form-control" type="text" name="receiver_postal_code" maxlength="16" value="<?= $e((string) ($receiver['zip_code'] ?? '')) ?>" required>
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">Miasto</span>
|
|
<input class="form-control" type="text" name="receiver_city" maxlength="128" value="<?= $e((string) ($receiver['city'] ?? '')) ?>" required>
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">Kraj</span>
|
|
<input class="form-control" type="text" name="receiver_country_code" maxlength="2" value="<?= $e((string) ($receiver['country'] ?? 'PL')) ?>" required>
|
|
</label>
|
|
</div>
|
|
|
|
<div class="form-grid-2">
|
|
<label class="form-field">
|
|
<span class="field-label">Telefon</span>
|
|
<input class="form-control" type="tel" name="receiver_phone" maxlength="64" value="<?= $e((string) ($receiver['phone'] ?? '')) ?>" required>
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="field-label">E-mail</span>
|
|
<input class="form-control" type="email" name="receiver_email" maxlength="128" value="<?= $e((string) ($receiver['email'] ?? '')) ?>" required>
|
|
</label>
|
|
</div>
|
|
|
|
<?php if ($pointId !== ''): ?>
|
|
<label class="form-field">
|
|
<span class="field-label">Punkt odbioru<?= $pointName !== '' ? ' (' . $e($pointName) . ')' : '' ?></span>
|
|
<input class="form-control" type="text" name="receiver_point_id" maxlength="64" value="<?= $e($pointId) ?>">
|
|
</label>
|
|
<?php else: ?>
|
|
<label class="form-field">
|
|
<span class="field-label">Punkt odbioru (opcjonalnie)</span>
|
|
<input class="form-control" type="text" name="receiver_point_id" maxlength="64" value="">
|
|
</label>
|
|
<?php endif; ?>
|
|
</section>
|
|
</div>
|
|
|
|
<section class="card mt-16">
|
|
<h3 class="section-title">Pozycje zamowienia (<?= $e((string) count($itemsList)) ?>)</h3>
|
|
<div class="table-wrap mt-12">
|
|
<table class="table table--details">
|
|
<thead>
|
|
<tr><th>Lp.</th><th>Nazwa</th><th>Ilosc</th><th>Cena</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($itemsList as $idx => $item): ?>
|
|
<tr>
|
|
<td><?= $e((string) ($idx + 1)) ?></td>
|
|
<td><?= $e((string) ($item['original_name'] ?? '')) ?></td>
|
|
<td><?= $e((string) ($item['quantity'] ?? 0)) ?></td>
|
|
<td><?= $e($item['original_price_with_tax'] !== null ? number_format((float) $item['original_price_with_tax'], 2, '.', ' ') : '-') ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="form-actions mt-16">
|
|
<button type="submit" class="btn btn--primary">Utworz przesylke</button>
|
|
<a href="/orders/<?= $e((string) ($orderId ?? 0)) ?>" class="btn btn--secondary">Anuluj</a>
|
|
</div>
|
|
</section>
|
|
</form>
|
|
|
|
<script>
|
|
(function () {
|
|
function enhanceSelect(selectEl) {
|
|
if (!selectEl || selectEl.dataset.enhanced) return;
|
|
selectEl.dataset.enhanced = '1';
|
|
|
|
var parent = selectEl.parentNode;
|
|
var container = document.createElement('div');
|
|
container.className = 'searchable-select';
|
|
parent.insertBefore(container, selectEl);
|
|
container.appendChild(selectEl);
|
|
selectEl.style.display = 'none';
|
|
|
|
var trigger = document.createElement('div');
|
|
trigger.className = 'searchable-select__trigger form-control';
|
|
container.appendChild(trigger);
|
|
|
|
var dd = document.createElement('div');
|
|
dd.className = 'searchable-select__dropdown';
|
|
container.appendChild(dd);
|
|
|
|
var search = document.createElement('input');
|
|
search.type = 'text';
|
|
search.className = 'searchable-select__search form-control';
|
|
search.placeholder = 'Szukaj...';
|
|
search.autocomplete = 'off';
|
|
dd.appendChild(search);
|
|
|
|
var optEls = [];
|
|
Array.from(selectEl.options).forEach(function (opt) {
|
|
var div = document.createElement('div');
|
|
div.className = 'searchable-select__option';
|
|
div.setAttribute('data-value', opt.value);
|
|
div.textContent = opt.textContent.trim();
|
|
if (opt.selected) div.classList.add('is-selected');
|
|
dd.appendChild(div);
|
|
optEls.push(div);
|
|
});
|
|
|
|
function syncTrigger() {
|
|
var opt = selectEl.options[selectEl.selectedIndex];
|
|
var text = opt ? opt.textContent.trim() : '';
|
|
trigger.textContent = text;
|
|
trigger.classList.toggle('searchable-select__trigger--placeholder', !selectEl.value);
|
|
optEls.forEach(function (d) {
|
|
d.classList.toggle('is-selected', d.getAttribute('data-value') === selectEl.value);
|
|
});
|
|
}
|
|
syncTrigger();
|
|
|
|
var isOpen = false;
|
|
|
|
function open() {
|
|
dd.classList.add('is-open');
|
|
isOpen = true;
|
|
search.value = '';
|
|
filterOpts('');
|
|
setTimeout(function () { search.focus(); }, 0);
|
|
}
|
|
|
|
function close() {
|
|
dd.classList.remove('is-open');
|
|
isOpen = false;
|
|
}
|
|
|
|
function pick(div) {
|
|
selectEl.value = div.getAttribute('data-value');
|
|
syncTrigger();
|
|
close();
|
|
selectEl.dispatchEvent(new Event('change', { bubbles: true }));
|
|
}
|
|
|
|
function filterOpts(q) {
|
|
q = q.toLowerCase().trim();
|
|
optEls.forEach(function (d) {
|
|
d.style.display = q === '' || d.textContent.toLowerCase().indexOf(q) !== -1 ? '' : 'none';
|
|
});
|
|
}
|
|
|
|
trigger.addEventListener('click', function (e) {
|
|
e.stopPropagation();
|
|
isOpen ? close() : open();
|
|
});
|
|
|
|
search.addEventListener('input', function () {
|
|
filterOpts(search.value);
|
|
});
|
|
|
|
optEls.forEach(function (d) {
|
|
d.addEventListener('mousedown', function (e) {
|
|
e.preventDefault();
|
|
pick(d);
|
|
});
|
|
});
|
|
|
|
search.addEventListener('blur', function () {
|
|
setTimeout(close, 150);
|
|
});
|
|
|
|
search.addEventListener('keydown', function (e) {
|
|
if (e.key === 'Escape') close();
|
|
});
|
|
|
|
document.addEventListener('click', function (e) {
|
|
if (isOpen && !container.contains(e.target)) close();
|
|
});
|
|
|
|
selectEl._syncTrigger = syncTrigger;
|
|
}
|
|
|
|
var carrierSelect = document.getElementById('shipment-carrier-select');
|
|
var inpostSelect = document.getElementById('shipment-inpost-select');
|
|
var apaczkaSelect = document.getElementById('shipment-apaczka-select');
|
|
|
|
document.querySelectorAll('form select.form-control').forEach(function (sel) {
|
|
enhanceSelect(sel);
|
|
});
|
|
|
|
var allegroPanel = document.getElementById('shipment-allegro-panel');
|
|
var inpostPanel = document.getElementById('shipment-inpost-panel');
|
|
var apaczkaPanel = document.getElementById('shipment-apaczka-panel');
|
|
var emptyPanel = document.getElementById('shipment-empty-panel');
|
|
|
|
var wrapper = document.getElementById('shipment-service-wrapper');
|
|
var hiddenInput = document.getElementById('shipment-delivery-service');
|
|
var searchInput = document.getElementById('shipment-service-search');
|
|
var dropdown = document.getElementById('shipment-service-dropdown');
|
|
var credentialsInput = document.getElementById('shipment-credentials-id');
|
|
var carrierInput = document.getElementById('shipment-carrier-id');
|
|
var providerInput = document.getElementById('shipment-provider-code');
|
|
|
|
if (!carrierSelect || !hiddenInput) return;
|
|
|
|
var allegroOpts = dropdown ? dropdown.querySelectorAll('.searchable-select__option') : [];
|
|
|
|
function clearHiddenFields() {
|
|
hiddenInput.value = '';
|
|
credentialsInput.value = '';
|
|
carrierInput.value = '';
|
|
}
|
|
|
|
function showPanel(carrier) {
|
|
if (allegroPanel) allegroPanel.style.display = carrier === 'allegro' ? '' : 'none';
|
|
if (inpostPanel) inpostPanel.style.display = carrier === 'inpost' ? '' : 'none';
|
|
if (apaczkaPanel) apaczkaPanel.style.display = carrier === 'apaczka' ? '' : 'none';
|
|
if (emptyPanel) emptyPanel.style.display = carrier === '' ? '' : 'none';
|
|
if (providerInput) providerInput.value = carrier === 'apaczka' ? 'apaczka' : 'allegro_wza';
|
|
}
|
|
|
|
carrierSelect.addEventListener('change', function () {
|
|
clearHiddenFields();
|
|
if (searchInput) searchInput.value = '';
|
|
if (inpostSelect) {
|
|
inpostSelect.selectedIndex = 0;
|
|
if (inpostSelect._syncTrigger) inpostSelect._syncTrigger();
|
|
}
|
|
if (apaczkaSelect) {
|
|
apaczkaSelect.selectedIndex = 0;
|
|
if (apaczkaSelect._syncTrigger) apaczkaSelect._syncTrigger();
|
|
}
|
|
allegroOpts.forEach(function (o) { o.classList.remove('is-selected'); });
|
|
showPanel(carrierSelect.value);
|
|
});
|
|
|
|
if (inpostSelect) {
|
|
function syncInpostFields() {
|
|
var opt = inpostSelect.options[inpostSelect.selectedIndex];
|
|
hiddenInput.value = inpostSelect.value;
|
|
credentialsInput.value = opt ? (opt.getAttribute('data-credentials-id') || '') : '';
|
|
carrierInput.value = opt ? (opt.getAttribute('data-carrier-id') || '') : '';
|
|
if (providerInput) providerInput.value = 'allegro_wza';
|
|
}
|
|
inpostSelect.addEventListener('change', syncInpostFields);
|
|
if (carrierSelect.value === 'inpost' && inpostSelect.value !== '') {
|
|
syncInpostFields();
|
|
}
|
|
}
|
|
|
|
if (apaczkaSelect) {
|
|
function syncApaczkaFields() {
|
|
var opt = apaczkaSelect.options[apaczkaSelect.selectedIndex];
|
|
hiddenInput.value = apaczkaSelect.value;
|
|
credentialsInput.value = '';
|
|
carrierInput.value = opt ? (opt.getAttribute('data-carrier-id') || '') : '';
|
|
if (providerInput) providerInput.value = 'apaczka';
|
|
}
|
|
apaczkaSelect.addEventListener('change', syncApaczkaFields);
|
|
if (carrierSelect.value === 'apaczka' && apaczkaSelect.value !== '') {
|
|
syncApaczkaFields();
|
|
}
|
|
}
|
|
|
|
if (wrapper && searchInput && dropdown) {
|
|
var isAllegroOpen = false;
|
|
|
|
function selectAllegroOption(opt) {
|
|
hiddenInput.value = opt.getAttribute('data-value') || '';
|
|
credentialsInput.value = opt.getAttribute('data-credentials-id') || '';
|
|
carrierInput.value = opt.getAttribute('data-carrier-id') || '';
|
|
if (providerInput) providerInput.value = 'allegro_wza';
|
|
searchInput.value = opt.getAttribute('data-label') || '';
|
|
closeAllegro();
|
|
allegroOpts.forEach(function (o) { o.classList.remove('is-selected'); });
|
|
opt.classList.add('is-selected');
|
|
}
|
|
|
|
function openAllegro() {
|
|
dropdown.classList.add('is-open');
|
|
isAllegroOpen = true;
|
|
}
|
|
|
|
function closeAllegro() {
|
|
dropdown.classList.remove('is-open');
|
|
isAllegroOpen = false;
|
|
}
|
|
|
|
function filterAllegro(q) {
|
|
q = q.toLowerCase().trim();
|
|
allegroOpts.forEach(function (opt) {
|
|
var label = (opt.getAttribute('data-label') || '').toLowerCase();
|
|
opt.style.display = q === '' || label.indexOf(q) !== -1 ? '' : 'none';
|
|
});
|
|
}
|
|
|
|
searchInput.addEventListener('focus', function () {
|
|
filterAllegro(searchInput.value);
|
|
openAllegro();
|
|
});
|
|
|
|
searchInput.addEventListener('input', function () {
|
|
filterAllegro(searchInput.value);
|
|
if (!isAllegroOpen) openAllegro();
|
|
});
|
|
|
|
allegroOpts.forEach(function (opt) {
|
|
opt.addEventListener('mousedown', function (e) {
|
|
e.preventDefault();
|
|
selectAllegroOption(opt);
|
|
});
|
|
});
|
|
|
|
searchInput.addEventListener('blur', function () {
|
|
setTimeout(closeAllegro, 150);
|
|
});
|
|
|
|
searchInput.addEventListener('keydown', function (e) {
|
|
if (e.key === 'Escape') {
|
|
closeAllegro();
|
|
searchInput.blur();
|
|
}
|
|
});
|
|
|
|
if (carrierSelect.value === 'allegro') {
|
|
var matchId = (wrapper.getAttribute('data-match-id') || '').trim();
|
|
if (matchId !== '') {
|
|
allegroOpts.forEach(function (opt) {
|
|
if (opt.getAttribute('data-value') === matchId) {
|
|
selectAllegroOption(opt);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
function checkPackageStatus(pkgId, oId, btn, attempt) {
|
|
if (btn) {
|
|
btn.disabled = true;
|
|
btn.textContent = 'Sprawdzam...';
|
|
}
|
|
fetch('/orders/' + oId + '/shipment/' + pkgId + '/status')
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (data) {
|
|
if (data.status === 'label_ready') {
|
|
window.location.reload();
|
|
} else if (data.status === 'created' || data.status === 'in_progress' || data.status === 'pending') {
|
|
if (attempt < 10) {
|
|
var delayCreated = Math.min(2000 * Math.pow(1.5, attempt), 15000);
|
|
setTimeout(function () {
|
|
checkPackageStatus(pkgId, oId, btn, attempt + 1);
|
|
}, delayCreated);
|
|
if (btn) btn.textContent = 'Generuje etykiete... (' + (attempt + 1) + ')';
|
|
} else if (btn) {
|
|
btn.textContent = 'W toku... Odswiez status';
|
|
btn.disabled = false;
|
|
}
|
|
} else if (data.status === 'error') {
|
|
if (btn) {
|
|
btn.textContent = 'Blad: ' + (data.error || '');
|
|
btn.disabled = false;
|
|
}
|
|
} else {
|
|
if (btn) {
|
|
btn.textContent = 'W toku... Sprobuj ponownie';
|
|
btn.disabled = false;
|
|
}
|
|
}
|
|
})
|
|
.catch(function () {
|
|
if (btn) {
|
|
btn.textContent = 'Blad sieci';
|
|
btn.disabled = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
document.querySelectorAll('[data-check-status]').forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
checkPackageStatus(
|
|
btn.getAttribute('data-check-status'),
|
|
btn.getAttribute('data-order-id'),
|
|
btn,
|
|
0
|
|
);
|
|
});
|
|
});
|
|
|
|
var params = new URLSearchParams(window.location.search);
|
|
var autoCheckId = params.get('check');
|
|
|
|
document.querySelectorAll('[data-check-status][data-auto-check=\"1\"]').forEach(function (btn, idx) {
|
|
var pkgId = btn.getAttribute('data-check-status');
|
|
var oId = btn.getAttribute('data-order-id');
|
|
var pkgStatus = (btn.getAttribute('data-package-status') || '').toLowerCase();
|
|
if (!pkgId || !oId) return;
|
|
|
|
if (autoCheckId) {
|
|
if (pkgId !== autoCheckId) return;
|
|
} else {
|
|
if (pkgStatus !== 'pending') return;
|
|
}
|
|
|
|
setTimeout(function () {
|
|
checkPackageStatus(pkgId, oId, btn, 0);
|
|
}, idx * 400);
|
|
});
|
|
|
|
})();
|
|
</script>
|