This commit is contained in:
2026-04-13 22:31:06 +02:00
parent 38259bc706
commit e15b4ccf45
24 changed files with 1580 additions and 3858 deletions

View File

@@ -264,6 +264,67 @@ final class OrdersController
return Response::html($html);
}
public function updateDetails(Request $request): Response
{
$orderId = max(0, (int) $request->input('id', 0));
if ($orderId <= 0) {
return Response::html('Not found', 404);
}
$csrfToken = (string) $request->input('_token', '');
if (!Csrf::validate($csrfToken)) {
Flash::set('order.error', $this->translator->get('auth.errors.csrf_expired'));
return Response::redirect('/orders/' . $orderId);
}
$details = $this->orders->findDetails($orderId);
if (!is_array($details['order'] ?? null)) {
return Response::html('Not found', 404);
}
$deliveryMethodRaw = trim((string) $request->input('delivery_method', ''));
$paymentMethodRaw = trim((string) $request->input('payment_method', ''));
$isCod = (string) $request->input('is_cod', '') === '1';
if ($deliveryMethodRaw === '' && $paymentMethodRaw === '') {
Flash::set('order.error', 'Podaj formę dostawy lub formę płatności.');
return Response::redirect('/orders/' . $orderId);
}
$deliveryMethod = $deliveryMethodRaw !== '' ? $deliveryMethodRaw : null;
$paymentMethod = $paymentMethodRaw !== '' ? $paymentMethodRaw : null;
$externalPaymentTypeId = null;
$currentOrder = (array) $details['order'];
$currentExternal = strtoupper(trim((string) ($currentOrder['external_payment_type_id'] ?? '')));
$currentlyCod = StringHelper::isCodPayment($currentExternal);
if ($isCod && !$currentlyCod) {
$externalPaymentTypeId = 'CASH_ON_DELIVERY';
} elseif (!$isCod && $currentlyCod) {
$externalPaymentTypeId = '';
}
$user = $this->auth->user();
$actorName = is_array($user) ? trim((string) ($user['name'] ?? $user['email'] ?? '')) : null;
$changed = $this->orders->updateDeliveryAndPayment(
$orderId,
$deliveryMethod,
$paymentMethod,
$externalPaymentTypeId,
'user',
$actorName !== '' ? $actorName : null
);
if ($changed) {
Flash::set('order.success', 'Dane zamówienia zostały zaktualizowane.');
} else {
Flash::set('order.error', 'Brak zmian do zapisania.');
}
return Response::redirect('/orders/' . $orderId);
}
public function updateStatus(Request $request): Response
{
$isAjax = strtolower($request->header('X-Requested-With')) === 'xmlhttprequest';
@@ -405,9 +466,28 @@ final class OrdersController
$documents
),
'ordered_at' => (string) ($row['ordered_at'] ?? ''),
'_row_class' => $this->agedRowClass((string) ($row['ordered_at'] ?? '')),
];
}
private function agedRowClass(string $orderedAt): string
{
if ($orderedAt === '') {
return '';
}
$ts = strtotime($orderedAt);
if ($ts === false) {
return '';
}
$ageDays = (int) floor((time() - $ts) / 86400);
if ($ageDays < 4) {
return '';
}
$level = $ageDays >= 7 ? 7 : $ageDays;
return 'order-row-aged order-row-aged-' . $level;
}
private function statusBadge(string $statusCode, string $statusLabel, string $colorHex = ''): string
{
$label = $statusLabel !== '' ? $statusLabel : '-';

View File

@@ -784,6 +784,93 @@ final class OrdersRepository
/**
* @param array<string, mixed>|null $details
*/
/**
* Aktualizuje formę dostawy i/lub formę płatności zamówienia. Zapisuje wpis do activity log.
*
* @return bool true gdy faktycznie nastąpiła zmiana
*/
public function updateDeliveryAndPayment(
int $orderId,
?string $deliveryMethod,
?string $paymentMethod,
?string $externalPaymentTypeId,
string $actorType = 'user',
?string $actorName = null
): bool {
if ($orderId <= 0) {
return false;
}
$stmt = $this->pdo->prepare(
'SELECT id, delivery_method, payment_method, external_payment_type_id FROM orders WHERE id = :id LIMIT 1'
);
$stmt->execute(['id' => $orderId]);
$before = $stmt->fetch(PDO::FETCH_ASSOC);
if (!is_array($before)) {
return false;
}
$updates = [];
$params = ['id' => $orderId];
$changed = [];
if ($deliveryMethod !== null) {
$oldValue = (string) ($before['delivery_method'] ?? '');
if ($oldValue !== $deliveryMethod) {
$updates[] = 'delivery_method = :delivery_method';
$params['delivery_method'] = $deliveryMethod;
$changed['delivery_method'] = ['before' => $oldValue, 'after' => $deliveryMethod];
}
}
if ($paymentMethod !== null) {
$oldValue = (string) ($before['payment_method'] ?? '');
if ($oldValue !== $paymentMethod) {
$updates[] = 'payment_method = :payment_method';
$params['payment_method'] = $paymentMethod;
$changed['payment_method'] = ['before' => $oldValue, 'after' => $paymentMethod];
}
}
if ($externalPaymentTypeId !== null) {
$oldValue = (string) ($before['external_payment_type_id'] ?? '');
if ($oldValue !== $externalPaymentTypeId) {
$updates[] = 'external_payment_type_id = :external_payment_type_id';
$params['external_payment_type_id'] = $externalPaymentTypeId;
$changed['external_payment_type_id'] = ['before' => $oldValue, 'after' => $externalPaymentTypeId];
}
}
if ($updates === []) {
return false;
}
$updates[] = 'updated_at = NOW()';
$sql = 'UPDATE orders SET ' . implode(', ', $updates) . ' WHERE id = :id';
$update = $this->pdo->prepare($sql);
$update->execute($params);
$summaryParts = [];
if (isset($changed['delivery_method'])) {
$summaryParts[] = 'forma dostawy';
}
if (isset($changed['payment_method']) || isset($changed['external_payment_type_id'])) {
$summaryParts[] = 'forma płatności';
}
$summary = 'Zmiana danych zamówienia: ' . implode(', ', $summaryParts);
$this->recordActivity(
$orderId,
'details_change',
$summary,
$changed,
$actorType,
$actorName
);
return true;
}
public function recordActivity(
int $orderId,
string $eventType,