This commit is contained in:
2026-03-31 00:30:50 +02:00
parent 5435209b08
commit af48e84449
30 changed files with 2706 additions and 111 deletions

View File

@@ -16,8 +16,13 @@ use Throwable;
final class AutomationController
{
private const HISTORY_PER_PAGE = 25;
private const ALLOWED_EVENTS = ['receipt.created', 'shipment.created', 'shipment.status_changed'];
private const ALLOWED_CONDITION_TYPES = ['integration', 'shipment_status'];
private const ALLOWED_EVENTS = ['receipt.created', 'shipment.created', 'shipment.status_changed', 'payment.status_changed', 'order.status_changed', 'order.status_aged'];
private const ALLOWED_CONDITION_TYPES = ['integration', 'shipment_status', 'payment_status', 'order_status', 'days_in_status'];
private const PAYMENT_STATUS_OPTIONS = [
'0' => 'Nieopłacone',
'1' => 'Częściowo opłacone',
'2' => 'Opłacone',
];
private const ALLOWED_ACTION_TYPES = ['send_email', 'issue_receipt', 'update_shipment_status', 'update_order_status'];
private const ALLOWED_RECIPIENTS = ['client', 'client_and_company', 'company'];
private const ALLOWED_RECEIPT_ISSUE_DATE_MODES = ['today', 'order_date', 'payment_date'];
@@ -107,8 +112,7 @@ final class AutomationController
$validationError = $this->validateInput($request);
if ($validationError !== null) {
Flash::set('settings.automation.error', $validationError);
return Response::redirect('/settings/automation/create');
return $this->renderForm($this->buildRuleFromRequest($request), $validationError);
}
try {
@@ -119,7 +123,7 @@ final class AutomationController
);
Flash::set('settings.automation.success', 'Zadanie automatyczne zostalo utworzone');
} catch (Throwable) {
Flash::set('settings.automation.error', 'Blad zapisu zadania automatycznego');
return $this->renderForm($this->buildRuleFromRequest($request), 'Blad zapisu zadania automatycznego');
}
return Response::redirect('/settings/automation');
@@ -140,8 +144,7 @@ final class AutomationController
$validationError = $this->validateInput($request);
if ($validationError !== null) {
Flash::set('settings.automation.error', $validationError);
return Response::redirect('/settings/automation/edit?id=' . $id);
return $this->renderForm($this->buildRuleFromRequest($request, $id), $validationError);
}
try {
@@ -153,7 +156,7 @@ final class AutomationController
);
Flash::set('settings.automation.success', 'Zadanie automatyczne zostalo zaktualizowane');
} catch (Throwable) {
Flash::set('settings.automation.error', 'Blad aktualizacji zadania automatycznego');
return $this->renderForm($this->buildRuleFromRequest($request, $id), 'Blad aktualizacji zadania automatycznego');
}
return Response::redirect('/settings/automation');
@@ -228,10 +231,10 @@ final class AutomationController
return Response::redirect('/settings/automation');
}
private function renderForm(?array $rule): Response
private function renderForm(?array $rule, string $errorMessage = ''): Response
{
$html = $this->template->render('automation/form', [
'title' => $rule !== null ? 'Edytuj zadanie automatyczne' : 'Nowe zadanie automatyczne',
'title' => $rule !== null && isset($rule['id']) ? 'Edytuj zadanie automatyczne' : 'Nowe zadanie automatyczne',
'activeMenu' => 'settings',
'activeSettings' => 'automation',
'user' => $this->auth->user(),
@@ -247,13 +250,69 @@ final class AutomationController
'receiptIssueDateModes' => self::ALLOWED_RECEIPT_ISSUE_DATE_MODES,
'receiptDuplicatePolicies' => self::ALLOWED_RECEIPT_DUPLICATE_POLICIES,
'shipmentStatusOptions' => self::SHIPMENT_STATUS_OPTIONS,
'paymentStatusOptions' => self::PAYMENT_STATUS_OPTIONS,
'orderStatusOptions' => $this->repository->listActiveOrderStatuses(),
'errorMessage' => Flash::get('settings.automation.error', ''),
'errorMessage' => $errorMessage !== '' ? $errorMessage : Flash::get('settings.automation.error', ''),
], 'layouts/app');
return Response::html($html);
}
private function buildRuleFromRequest(Request $request, ?int $id = null): array
{
$raw = $request->input('conditions', []);
$conditions = [];
if (is_array($raw)) {
foreach ($raw as $cond) {
if (!is_array($cond)) {
continue;
}
$type = (string) ($cond['type'] ?? '');
$value = [];
if ($type === 'integration') {
$value = ['integration_ids' => is_array($cond['integration_ids'] ?? null) ? $cond['integration_ids'] : []];
} elseif ($type === 'shipment_status') {
$value = ['status_keys' => is_array($cond['shipment_status_keys'] ?? null) ? $cond['shipment_status_keys'] : []];
} elseif ($type === 'payment_status') {
$value = ['status_keys' => is_array($cond['payment_status_keys'] ?? null) ? $cond['payment_status_keys'] : []];
} elseif ($type === 'order_status') {
$value = ['order_status_codes' => is_array($cond['order_status_codes'] ?? null) ? $cond['order_status_codes'] : []];
} elseif ($type === 'days_in_status') {
$value = ['days' => max(1, (int) ($cond['days'] ?? 0))];
}
$conditions[] = ['condition_type' => $type, 'condition_value' => $value];
}
}
$rawActions = $request->input('actions', []);
$actions = [];
if (is_array($rawActions)) {
foreach ($rawActions as $act) {
if (!is_array($act)) {
continue;
}
$type = (string) ($act['type'] ?? '');
$config = $act;
unset($config['type']);
$actions[] = ['action_type' => $type, 'action_config' => $config];
}
}
$rule = [
'name' => trim((string) $request->input('name', '')),
'event_type' => (string) $request->input('event_type', ''),
'is_active' => $request->input('is_active', null) !== null ? 1 : 0,
'conditions' => $conditions,
'actions' => $actions,
];
if ($id !== null) {
$rule['id'] = $id;
}
return $rule;
}
private function validateCsrf(Request $request): ?Response
{
if (!Csrf::validate((string) $request->input('_token', ''))) {
@@ -367,6 +426,46 @@ final class AutomationController
return count($statusKeys) > 0 ? ['status_keys' => array_values(array_unique($statusKeys))] : null;
}
if ($type === 'payment_status') {
$keys = $condition['payment_status_keys'] ?? [];
if (!is_array($keys)) {
$keys = [];
}
$allowedKeys = array_map('strval', array_keys(self::PAYMENT_STATUS_OPTIONS));
$statusKeys = array_values(array_filter(
array_map(static fn (mixed $key): string => trim((string) $key), $keys),
static fn (string $key): bool => $key !== '' && in_array($key, $allowedKeys, true)
));
return count($statusKeys) > 0 ? ['status_keys' => array_values(array_unique($statusKeys))] : null;
}
if ($type === 'order_status') {
$codes = $condition['order_status_codes'] ?? [];
if (!is_array($codes)) {
$codes = [];
}
$availableCodes = array_map(
static fn (array $row): string => strtolower(trim((string) ($row['code'] ?? ''))),
$this->repository->listActiveOrderStatuses()
);
$statusCodes = array_values(array_filter(
array_map(static fn (mixed $code): string => strtolower(trim((string) $code)), $codes),
static fn (string $code): bool => $code !== '' && in_array($code, $availableCodes, true)
));
return count($statusCodes) > 0 ? ['order_status_codes' => array_values(array_unique($statusCodes))] : null;
}
if ($type === 'days_in_status') {
$days = (int) ($condition['days'] ?? 0);
return $days >= 1 ? ['days' => $days] : null;
}
return null;
}

View File

@@ -128,6 +128,15 @@ final class AutomationService
if ($type === 'shipment_status') {
return $this->evaluateShipmentStatusCondition($value, $context);
}
if ($type === 'payment_status') {
return $this->evaluatePaymentStatusCondition($value, $context);
}
if ($type === 'order_status') {
return $this->evaluateOrderStatusCondition($value, $context);
}
if ($type === 'days_in_status') {
return $this->evaluateDaysInStatusCondition($value, $context);
}
return false;
}
@@ -185,6 +194,65 @@ final class AutomationService
return isset($allowedStatuses[$deliveryStatus]);
}
/**
* @param array<string, mixed> $value
* @param array<string, mixed> $context
*/
private function evaluatePaymentStatusCondition(array $value, array $context): bool
{
$statusKeys = is_array($value['status_keys'] ?? null) ? $value['status_keys'] : [];
if ($statusKeys === []) {
return false;
}
$newPaymentStatus = trim((string) ($context['new_payment_status'] ?? ''));
if ($newPaymentStatus === '') {
return false;
}
return in_array($newPaymentStatus, array_map(static fn (mixed $k): string => trim((string) $k), $statusKeys), true);
}
/**
* @param array<string, mixed> $value
* @param array<string, mixed> $context
*/
private function evaluateOrderStatusCondition(array $value, array $context): bool
{
$orderStatusCodes = is_array($value['order_status_codes'] ?? null) ? $value['order_status_codes'] : [];
if ($orderStatusCodes === []) {
return false;
}
$newStatus = strtolower(trim((string) ($context['new_status'] ?? '')));
if ($newStatus === '') {
return false;
}
$normalizedCodes = array_map(
static fn (mixed $code): string => strtolower(trim((string) $code)),
$orderStatusCodes
);
return in_array($newStatus, $normalizedCodes, true);
}
/**
* @param array<string, mixed> $value
* @param array<string, mixed> $context
*/
private function evaluateDaysInStatusCondition(array $value, array $context): bool
{
$requiredDays = (int) ($value['days'] ?? 0);
if ($requiredDays < 1) {
return false;
}
$actualDays = (int) ($context['days_in_status'] ?? 0);
return $actualDays >= $requiredDays;
}
/**
* @param list<array<string, mixed>> $actions
* @param array<string, mixed> $context
@@ -211,7 +279,7 @@ final class AutomationService
}
if ($type === 'update_order_status') {
$this->handleUpdateOrderStatus($config, $orderId, $ruleName);
$this->handleUpdateOrderStatus($config, $orderId, $ruleName, $context);
}
}
}
@@ -446,28 +514,47 @@ final class AutomationService
/**
* @param array<string, mixed> $config
* @param array<string, mixed> $context
*/
private function handleUpdateOrderStatus(array $config, int $orderId, string $ruleName): void
private function handleUpdateOrderStatus(array $config, int $orderId, string $ruleName, array $context): void
{
$statusCode = trim((string) ($config['status_code'] ?? ''));
if ($statusCode === '') {
return;
}
$details = $this->orders->findDetails($orderId);
$order = is_array($details['order'] ?? null) ? $details['order'] : [];
$oldStatus = strtolower(trim((string) ($order['external_status_id'] ?? '')));
$actorName = 'Automatyzacja: ' . $ruleName;
$updated = $this->orders->updateOrderStatus($orderId, $statusCode, 'system', $actorName);
if ($updated) {
if (!$updated) {
$this->orders->recordActivity(
$orderId,
'automation_order_status_failed',
$actorName . ' - nie udalo sie zmienic statusu zamowienia',
['target_status_code' => $statusCode],
'system',
$actorName
);
return;
}
$this->orders->recordActivity(
$orderId,
'automation_order_status_failed',
$actorName . ' - nie udalo sie zmienic statusu zamowienia',
['target_status_code' => $statusCode],
'system',
$actorName
);
$newStatus = strtolower(trim($statusCode));
if ($oldStatus !== $newStatus) {
$this->emitEvent(
'order.status_changed',
$orderId,
$context,
[
'old_status' => $oldStatus,
'new_status' => $newStatus,
'automation_source' => 'update_order_status',
'automation_rule' => $ruleName,
]
);
}
}
private function resolveStatusFromActionKey(string $statusKey): ?string

View File

@@ -0,0 +1,164 @@
<?php
declare(strict_types=1);
namespace App\Modules\Automation;
use PDO;
use Throwable;
final class OrderStatusAgedService
{
private const MAX_ORDERS_PER_RULE = 100;
public function __construct(
private readonly AutomationRepository $repository,
private readonly AutomationService $automation,
private readonly PDO $db
) {
}
public function scan(): int
{
$rules = $this->repository->findActiveByEvent('order.status_aged');
if ($rules === []) {
return 0;
}
$totalTriggered = 0;
foreach ($rules as $rule) {
try {
$totalTriggered += $this->processRule($rule);
} catch (Throwable) {
// Blad jednej reguly nie blokuje kolejnych
}
}
return $totalTriggered;
}
/**
* @param array<string, mixed> $rule
*/
private function processRule(array $rule): int
{
$conditions = is_array($rule['conditions'] ?? null) ? $rule['conditions'] : [];
$statusCodes = $this->extractStatusCodes($conditions);
$days = $this->extractDays($conditions);
if ($statusCodes === [] || $days < 1) {
return 0;
}
$orders = $this->findAgedOrders($statusCodes, $days);
$triggered = 0;
foreach ($orders as $order) {
$orderId = (int) ($order['id'] ?? 0);
if ($orderId <= 0) {
continue;
}
try {
$currentStatus = strtolower(trim((string) ($order['external_status_id'] ?? '')));
$lastChanged = (string) ($order['last_changed'] ?? '');
$actualDays = $lastChanged !== '' ? $this->daysSince($lastChanged) : $days;
$this->automation->trigger('order.status_aged', $orderId, [
'current_status' => $currentStatus,
'days_in_status' => $actualDays,
'status_changed_at' => $lastChanged,
]);
$triggered++;
} catch (Throwable) {
// Blad jednego zamowienia nie blokuje kolejnych
}
}
return $triggered;
}
/**
* @param list<array<string, mixed>> $conditions
* @return list<string>
*/
private function extractStatusCodes(array $conditions): array
{
foreach ($conditions as $condition) {
$type = (string) ($condition['condition_type'] ?? '');
$value = is_array($condition['condition_value'] ?? null) ? $condition['condition_value'] : [];
if ($type === 'order_status') {
$codes = is_array($value['order_status_codes'] ?? null) ? $value['order_status_codes'] : [];
return array_values(array_filter(
array_map(static fn (mixed $c): string => strtolower(trim((string) $c)), $codes),
static fn (string $c): bool => $c !== ''
));
}
}
return [];
}
/**
* @param list<array<string, mixed>> $conditions
*/
private function extractDays(array $conditions): int
{
foreach ($conditions as $condition) {
$type = (string) ($condition['condition_type'] ?? '');
$value = is_array($condition['condition_value'] ?? null) ? $condition['condition_value'] : [];
if ($type === 'days_in_status') {
return max(0, (int) ($value['days'] ?? 0));
}
}
return 0;
}
/**
* @param list<string> $statusCodes
* @return list<array<string, mixed>>
*/
private function findAgedOrders(array $statusCodes, int $days): array
{
if ($statusCodes === [] || $days < 1) {
return [];
}
$placeholders = implode(', ', array_fill(0, count($statusCodes), '?'));
$sql = "SELECT o.id, o.external_status_id, MAX(h.changed_at) AS last_changed
FROM orders o
INNER JOIN order_status_history h ON h.order_id = o.id
AND LOWER(h.to_status_id) = LOWER(o.external_status_id)
WHERE LOWER(COALESCE(o.external_status_id, '')) IN ({$placeholders})
GROUP BY o.id, o.external_status_id
HAVING MAX(h.changed_at) <= DATE_SUB(NOW(), INTERVAL ? DAY)
LIMIT " . self::MAX_ORDERS_PER_RULE;
try {
$stmt = $this->db->prepare($sql);
$params = $statusCodes;
$params[] = $days;
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return is_array($rows) ? $rows : [];
} catch (Throwable) {
return [];
}
}
private function daysSince(string $datetime): int
{
$timestamp = strtotime($datetime);
if ($timestamp === false) {
return 0;
}
$diff = time() - $timestamp;
return max(0, (int) floor($diff / 86400));
}
}