update
This commit is contained in:
@@ -15,9 +15,10 @@ 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_ACTION_TYPES = ['send_email', 'issue_receipt', 'update_shipment_status'];
|
||||
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'];
|
||||
private const ALLOWED_RECEIPT_DUPLICATE_POLICIES = ['skip_if_exists', 'allow_duplicates'];
|
||||
@@ -36,6 +37,7 @@ final class AutomationController
|
||||
private readonly Translator $translator,
|
||||
private readonly AuthService $auth,
|
||||
private readonly AutomationRepository $repository,
|
||||
private readonly AutomationExecutionLogRepository $executionLogs,
|
||||
private readonly ReceiptConfigRepository $receiptConfigs
|
||||
) {
|
||||
}
|
||||
@@ -43,6 +45,15 @@ final class AutomationController
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$rules = $this->repository->findAll();
|
||||
$historyFilters = $this->extractHistoryFilters($request);
|
||||
$historyPage = max(1, (int) $request->input('history_page', 1));
|
||||
$historyTotal = $this->executionLogs->count($historyFilters);
|
||||
$historyTotalPages = max(1, (int) ceil($historyTotal / self::HISTORY_PER_PAGE));
|
||||
if ($historyPage > $historyTotalPages) {
|
||||
$historyPage = $historyTotalPages;
|
||||
}
|
||||
$historyEntries = $this->executionLogs->paginate($historyFilters, $historyPage, self::HISTORY_PER_PAGE);
|
||||
$activeTab = $this->resolveActiveTab($request, $historyFilters);
|
||||
|
||||
$html = $this->template->render('automation/index', [
|
||||
'title' => 'Zadania automatyczne',
|
||||
@@ -51,6 +62,17 @@ final class AutomationController
|
||||
'user' => $this->auth->user(),
|
||||
'csrfToken' => Csrf::token(),
|
||||
'rules' => $rules,
|
||||
'activeTab' => $activeTab,
|
||||
'historyEntries' => $historyEntries,
|
||||
'historyFilters' => $historyFilters,
|
||||
'historyEventTypes' => array_values(array_unique(array_merge(self::ALLOWED_EVENTS, $this->executionLogs->listEventTypes()))),
|
||||
'historyRuleOptions' => $this->repository->listRuleOptions(),
|
||||
'historyPagination' => [
|
||||
'page' => $historyPage,
|
||||
'per_page' => self::HISTORY_PER_PAGE,
|
||||
'total' => $historyTotal,
|
||||
'total_pages' => $historyTotalPages,
|
||||
],
|
||||
'successMessage' => Flash::get('settings.automation.success', ''),
|
||||
'errorMessage' => Flash::get('settings.automation.error', ''),
|
||||
], 'layouts/app');
|
||||
@@ -225,6 +247,7 @@ final class AutomationController
|
||||
'receiptIssueDateModes' => self::ALLOWED_RECEIPT_ISSUE_DATE_MODES,
|
||||
'receiptDuplicatePolicies' => self::ALLOWED_RECEIPT_DUPLICATE_POLICIES,
|
||||
'shipmentStatusOptions' => self::SHIPMENT_STATUS_OPTIONS,
|
||||
'orderStatusOptions' => $this->repository->listActiveOrderStatuses(),
|
||||
'errorMessage' => Flash::get('settings.automation.error', ''),
|
||||
], 'layouts/app');
|
||||
|
||||
@@ -425,6 +448,24 @@ final class AutomationController
|
||||
return ['status_key' => $statusKey];
|
||||
}
|
||||
|
||||
if ($type === 'update_order_status') {
|
||||
$statusCode = trim((string) ($action['order_status_code'] ?? ''));
|
||||
if ($statusCode === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$availableCodes = array_map(
|
||||
static fn (array $row): string => trim((string) ($row['code'] ?? '')),
|
||||
$this->repository->listActiveOrderStatuses()
|
||||
);
|
||||
|
||||
if (!in_array($statusCode, $availableCodes, true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ['status_code' => $statusCode];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -452,4 +493,53 @@ final class AutomationController
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{event_type:string,execution_status:string,rule_id:int,order_id:int,date_from:string,date_to:string}
|
||||
*/
|
||||
private function extractHistoryFilters(Request $request): array
|
||||
{
|
||||
return [
|
||||
'event_type' => trim((string) $request->input('history_event_type', '')),
|
||||
'execution_status' => trim((string) $request->input('history_status', '')),
|
||||
'rule_id' => max(0, (int) $request->input('history_rule_id', 0)),
|
||||
'order_id' => max(0, (int) $request->input('history_order_id', 0)),
|
||||
'date_from' => trim((string) $request->input('history_date_from', '')),
|
||||
'date_to' => trim((string) $request->input('history_date_to', '')),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{event_type:string,execution_status:string,rule_id:int,order_id:int,date_from:string,date_to:string} $historyFilters
|
||||
*/
|
||||
private function resolveActiveTab(Request $request, array $historyFilters): string
|
||||
{
|
||||
$activeTab = trim((string) $request->input('tab', 'settings'));
|
||||
if ($activeTab === 'history') {
|
||||
return 'history';
|
||||
}
|
||||
|
||||
if ((int) $request->input('history_page', 0) > 1) {
|
||||
return 'history';
|
||||
}
|
||||
|
||||
if ($this->hasHistoryFilters($historyFilters)) {
|
||||
return 'history';
|
||||
}
|
||||
|
||||
return 'settings';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{event_type:string,execution_status:string,rule_id:int,order_id:int,date_from:string,date_to:string} $historyFilters
|
||||
*/
|
||||
private function hasHistoryFilters(array $historyFilters): bool
|
||||
{
|
||||
return $historyFilters['event_type'] !== ''
|
||||
|| $historyFilters['execution_status'] !== ''
|
||||
|| $historyFilters['rule_id'] > 0
|
||||
|| $historyFilters['order_id'] > 0
|
||||
|| $historyFilters['date_from'] !== ''
|
||||
|| $historyFilters['date_to'] !== '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user