Files
orderPRO/resources/views/automation/form.php
Jacek Pyziak b9f639e037 feat(16-automated-tasks): moduł zadań automatycznych — CRUD + watcher/executor
Reguły automatyzacji oparte na zdarzeniach (receipt.created) z warunkami
(integracja/kanał sprzedaży, AND logic) i akcjami (wyślij e-mail z 3 trybami
odbiorcy: klient / firma / klient+firma). Trigger w ReceiptController po
utworzeniu paragonu — błąd automatyzacji nie blokuje sukcesu paragonu.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 00:39:47 +01:00

140 lines
7.2 KiB
PHP

<?php
$rule = is_array($rule ?? null) ? $rule : null;
$isEdit = $rule !== null;
$integrations = is_array($integrations ?? null) ? $integrations : [];
$emailTemplates = is_array($emailTemplates ?? null) ? $emailTemplates : [];
$conditions = $isEdit ? (is_array($rule['conditions'] ?? null) ? $rule['conditions'] : []) : [];
$actions = $isEdit ? (is_array($rule['actions'] ?? null) ? $rule['actions'] : []) : [];
$eventLabels = [
'receipt.created' => 'Utworzono paragon',
];
$recipientLabels = [
'client' => 'Klient',
'client_and_company' => 'Klient + e-mail z danych firmy',
'company' => 'E-mail z danych firmy',
];
?>
<section class="card">
<h2 class="section-title"><?= $isEdit ? 'Edytuj zadanie automatyczne' : 'Nowe zadanie automatyczne' ?></h2>
<?php if (!empty($errorMessage)): ?>
<div class="alert alert--danger mt-12" role="alert"><?= $e((string) $errorMessage) ?></div>
<?php endif; ?>
<form action="<?= $isEdit ? '/settings/automation/update' : '/settings/automation/store' ?>" method="post" novalidate class="mt-12" id="js-automation-form">
<input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
<?php if ($isEdit): ?>
<input type="hidden" name="id" value="<?= (int) ($rule['id'] ?? 0) ?>">
<?php endif; ?>
<div class="form-grid-2">
<label class="form-field">
<span class="field-label">Nazwa zadania *</span>
<input class="form-control" type="text" name="name" maxlength="128" required value="<?= $e((string) ($rule['name'] ?? '')) ?>" placeholder="np. Paragon Allegro - wyslij e-mail">
</label>
<label class="form-field">
<span class="field-label">Zdarzenie *</span>
<select class="form-control" name="event_type">
<?php foreach ($eventLabels as $key => $label): ?>
<option value="<?= $e($key) ?>"<?= ((string) ($rule['event_type'] ?? 'receipt.created')) === $key ? ' selected' : '' ?>><?= $e($label) ?></option>
<?php endforeach; ?>
</select>
</label>
</div>
<div class="form-grid-2 mt-0">
<label class="form-field" style="display:flex;align-items:center;gap:6px;flex-direction:row">
<input type="checkbox" name="is_active" value="1"<?= $isEdit ? (((int) ($rule['is_active'] ?? 0)) === 1 ? ' checked' : '') : ' checked' ?>>
<span class="field-label" style="margin:0">Aktywne</span>
</label>
</div>
<h3 class="section-title mt-16">Warunki</h3>
<p class="muted">Wszystkie warunki musza byc spelnione (AND).</p>
<div id="js-conditions-container">
<?php if (count($conditions) > 0): ?>
<?php foreach ($conditions as $idx => $cond): ?>
<div class="automation-row mt-8" data-index="<?= $idx ?>">
<div class="automation-row__fields">
<select class="form-control automation-row__type" name="conditions[<?= $idx ?>][type]" onchange="window.AutomationForm.onConditionTypeChange(this)">
<option value="integration"<?= ((string) ($cond['condition_type'] ?? '')) === 'integration' ? ' selected' : '' ?>>Integracja (kanal sprzedazy)</option>
</select>
<div class="automation-row__config">
<?php
$condValue = is_array($cond['condition_value'] ?? null) ? $cond['condition_value'] : [];
$selectedIds = is_array($condValue['integration_ids'] ?? null) ? $condValue['integration_ids'] : [];
?>
<div class="checkbox-group">
<?php foreach ($integrations as $integ): ?>
<label class="checkbox-label">
<input type="checkbox" name="conditions[<?= $idx ?>][integration_ids][]" value="<?= (int) $integ['id'] ?>"<?= in_array((int) $integ['id'], $selectedIds, true) ? ' checked' : '' ?>>
<?= $e((string) ($integ['name'] ?? '')) ?> <span class="muted">(<?= $e((string) ($integ['type'] ?? '')) ?>)</span>
</label>
<?php endforeach; ?>
</div>
</div>
</div>
<button type="button" class="btn btn--sm btn--danger automation-row__remove" onclick="window.AutomationForm.removeRow(this)">&times;</button>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<button type="button" class="btn btn--sm btn--secondary mt-8" id="js-add-condition">+ Dodaj warunek</button>
<h3 class="section-title mt-16">Akcje</h3>
<p class="muted">Wszystkie akcje zostana wykonane po kolei.</p>
<div id="js-actions-container">
<?php if (count($actions) > 0): ?>
<?php foreach ($actions as $idx => $act): ?>
<div class="automation-row mt-8" data-index="<?= $idx ?>">
<div class="automation-row__fields">
<select class="form-control automation-row__type" name="actions[<?= $idx ?>][type]" onchange="window.AutomationForm.onActionTypeChange(this)">
<option value="send_email"<?= ((string) ($act['action_type'] ?? '')) === 'send_email' ? ' selected' : '' ?>>Wyslij e-mail</option>
</select>
<div class="automation-row__config">
<?php $actConfig = is_array($act['action_config'] ?? null) ? $act['action_config'] : []; ?>
<select class="form-control" name="actions[<?= $idx ?>][template_id]">
<option value="">-- Wybierz szablon --</option>
<?php foreach ($emailTemplates as $tpl): ?>
<option value="<?= (int) $tpl['id'] ?>"<?= ((int) ($actConfig['template_id'] ?? 0)) === (int) $tpl['id'] ? ' selected' : '' ?>><?= $e((string) ($tpl['name'] ?? '')) ?></option>
<?php endforeach; ?>
</select>
<select class="form-control" name="actions[<?= $idx ?>][recipient]">
<?php foreach ($recipientLabels as $key => $label): ?>
<option value="<?= $e($key) ?>"<?= ((string) ($actConfig['recipient'] ?? '')) === $key ? ' selected' : '' ?>><?= $e($label) ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<button type="button" class="btn btn--sm btn--danger automation-row__remove" onclick="window.AutomationForm.removeRow(this)">&times;</button>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<button type="button" class="btn btn--sm btn--secondary mt-8" id="js-add-action">+ Dodaj akcje</button>
<div class="form-actions mt-16">
<button type="submit" class="btn btn--primary"><?= $isEdit ? 'Zapisz zmiany' : 'Utworz zadanie' ?></button>
<a href="/settings/automation" class="btn btn--secondary">Anuluj</a>
</div>
</form>
</section>
<script>
window.AutomationFormData = {
integrations: <?= json_encode(array_map(function($i) {
return ['id' => (int) $i['id'], 'name' => (string) ($i['name'] ?? ''), 'type' => (string) ($i['type'] ?? '')];
}, $integrations), JSON_UNESCAPED_UNICODE) ?>,
emailTemplates: <?= json_encode(array_map(function($t) {
return ['id' => (int) $t['id'], 'name' => (string) ($t['name'] ?? '')];
}, $emailTemplates), JSON_UNESCAPED_UNICODE) ?>,
recipientLabels: <?= json_encode($recipientLabels, JSON_UNESCAPED_UNICODE) ?>
};
</script>
<script src="/assets/js/modules/automation-form.js"></script>