update
This commit is contained in:
@@ -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 : '-';
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -145,6 +145,33 @@ final class PrintApiController
|
||||
);
|
||||
}
|
||||
|
||||
public function status(Request $request): Response
|
||||
{
|
||||
$raw = trim((string) $request->input('package_ids', ''));
|
||||
if ($raw === '') {
|
||||
return Response::json(['pending' => []]);
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
foreach (explode(',', $raw) as $part) {
|
||||
$id = (int) trim($part);
|
||||
if ($id > 0) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
if (count($ids) >= 100) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($ids === []) {
|
||||
return Response::json(['pending' => []]);
|
||||
}
|
||||
|
||||
$pending = $this->printJobs->filterPendingPackageIds($ids);
|
||||
|
||||
return Response::json(['pending' => $pending]);
|
||||
}
|
||||
|
||||
public function markComplete(Request $request): Response
|
||||
{
|
||||
$jobId = (int) $request->input('id', 0);
|
||||
|
||||
@@ -107,6 +107,34 @@ final class PrintJobRepository
|
||||
return is_array($rows) ? array_map('intval', $rows) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $packageIds
|
||||
* @return list<int>
|
||||
*/
|
||||
public function filterPendingPackageIds(array $packageIds): array
|
||||
{
|
||||
if ($packageIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$placeholders = [];
|
||||
$bindings = [];
|
||||
foreach (array_values($packageIds) as $index => $id) {
|
||||
$key = ':pkg_' . $index;
|
||||
$placeholders[] = $key;
|
||||
$bindings[$key] = (int) $id;
|
||||
}
|
||||
|
||||
$sql = 'SELECT DISTINCT package_id FROM print_jobs '
|
||||
. "WHERE status = 'pending' AND package_id IN (" . implode(', ', $placeholders) . ')';
|
||||
|
||||
$statement = $this->pdo->prepare($sql);
|
||||
$statement->execute($bindings);
|
||||
$rows = $statement->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
return is_array($rows) ? array_map('intval', $rows) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
|
||||
@@ -218,7 +218,7 @@ final class ShipmentController
|
||||
|
||||
$this->triggerShipmentCreatedAutomation($orderId, $packageId, $providerCode);
|
||||
Flash::set('order.success', 'Przesylka utworzona. Sprawdz status w zakladce Przesylki.');
|
||||
return Response::redirect('/orders/' . $orderId);
|
||||
return Response::redirect('/orders/' . $orderId . '?printLast=1');
|
||||
} catch (Throwable $exception) {
|
||||
$this->ordersRepository->recordActivity(
|
||||
$orderId,
|
||||
|
||||
Reference in New Issue
Block a user