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