update
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user