This commit is contained in:
2026-03-30 20:23:38 +02:00
parent 70662afd2c
commit 5435209b08
26 changed files with 1949 additions and 160 deletions

View File

@@ -847,6 +847,94 @@ final class OrdersRepository
], $actorType, $actorName);
}
/**
* @param array<string, mixed> $data Keys: payment_type_id, amount, payment_date, comment, currency
* @return array{id:int, payment_status:int, total_paid:float}|null
*/
/**
* @return array{source:string, integration_id:int, source_order_id:string}|null
*/
public function findOrderSourceInfo(int $orderId): ?array
{
if ($orderId <= 0) {
return null;
}
$stmt = $this->pdo->prepare('SELECT source, integration_id, source_order_id FROM orders WHERE id = :id LIMIT 1');
$stmt->execute(['id' => $orderId]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return is_array($row) ? $row : null;
}
/**
* @param array<string, mixed> $data Keys: payment_type_id, amount, payment_date, comment, currency
* @return array{id:int, payment_status:int, total_paid:float}|null
*/
public function addPayment(int $orderId, array $data): ?array
{
if ($orderId <= 0) {
return null;
}
$stmt = $this->pdo->prepare('SELECT id, total_with_tax, currency FROM orders WHERE id = :id LIMIT 1');
$stmt->execute(['id' => $orderId]);
$order = $stmt->fetch(PDO::FETCH_ASSOC);
if (!is_array($order)) {
return null;
}
$amount = round((float) ($data['amount'] ?? 0), 2);
$paymentTypeId = trim((string) ($data['payment_type_id'] ?? ''));
$paymentDate = trim((string) ($data['payment_date'] ?? ''));
$comment = trim((string) ($data['comment'] ?? ''));
$currency = trim((string) ($data['currency'] ?? $order['currency'] ?? 'PLN'));
if ($amount <= 0 || $paymentTypeId === '') {
return null;
}
$sourcePaymentId = 'manual_' . $orderId . '_' . time();
$insert = $this->pdo->prepare(
'INSERT INTO order_payments (order_id, source_payment_id, payment_type_id, payment_date, amount, currency, comment, created_at, updated_at)
VALUES (:order_id, :source_payment_id, :payment_type_id, :payment_date, :amount, :currency, :comment, NOW(), NOW())'
);
$insert->execute([
'order_id' => $orderId,
'source_payment_id' => $sourcePaymentId,
'payment_type_id' => $paymentTypeId,
'payment_date' => $paymentDate !== '' ? $paymentDate : date('Y-m-d H:i:s'),
'amount' => $amount,
'currency' => $currency,
'comment' => $comment !== '' ? $comment : null,
]);
$paymentId = (int) $this->pdo->lastInsertId();
$sumStmt = $this->pdo->prepare('SELECT COALESCE(SUM(amount), 0) FROM order_payments WHERE order_id = :order_id');
$sumStmt->execute(['order_id' => $orderId]);
$totalPaid = round((float) $sumStmt->fetchColumn(), 2);
$totalWithTax = $order['total_with_tax'] !== null ? (float) $order['total_with_tax'] : null;
$paymentStatus = 0;
if ($totalPaid > 0 && $totalWithTax !== null && $totalPaid >= $totalWithTax) {
$paymentStatus = 2;
} elseif ($totalPaid > 0) {
$paymentStatus = 1;
}
$update = $this->pdo->prepare('UPDATE orders SET payment_status = :payment_status, total_paid = :total_paid, updated_at = NOW() WHERE id = :id');
$update->execute([
'payment_status' => $paymentStatus,
'total_paid' => $totalPaid,
'id' => $orderId,
]);
return [
'id' => $paymentId,
'payment_status' => $paymentStatus,
'total_paid' => $totalPaid,
];
}
public function updateOrderStatus(int $orderId, string $newStatusCode, string $actorType = 'user', ?string $actorName = null): bool
{
try {