105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* Bulk "renew": set a common completed_date on the selected training records
|
|
*/
|
|
require_once(__DIR__ . '/../hr_auth_check.php');
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'message' => 'Metodo non consentito.']);
|
|
exit;
|
|
}
|
|
|
|
// $pdo and $currentUserId from hr_auth_check.php
|
|
|
|
$completedDate = trim($_POST['completed_date'] ?? '');
|
|
$ids = $_POST['training_ids'] ?? [];
|
|
|
|
if (!is_array($ids)) {
|
|
$ids = [];
|
|
}
|
|
$ids = array_values(array_unique(array_filter(array_map('intval', $ids), fn($v) => $v > 0)));
|
|
|
|
if ($completedDate === '' || !DateTime::createFromFormat('Y-m-d', $completedDate)) {
|
|
echo json_encode(['success' => false, 'message' => 'Indicare una data valida.']);
|
|
exit;
|
|
}
|
|
if (empty($ids)) {
|
|
echo json_encode(['success' => false, 'message' => 'Selezionare almeno un record.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Load each record with its topic default frequency
|
|
$rowStmt = $pdo->prepare("
|
|
SELECT et.id, et.employee_id, et.completed_date, et.next_due_date,
|
|
et.update_frequency_months, tt.default_frequency_months
|
|
FROM employee_trainings et
|
|
JOIN training_topics tt ON tt.id = et.training_topic_id
|
|
WHERE et.id = :id
|
|
");
|
|
$upd = $pdo->prepare("
|
|
UPDATE employee_trainings
|
|
SET completed_date = :cd, next_due_date = :nd, updated_at = NOW()
|
|
WHERE id = :id
|
|
");
|
|
$logStmt = $pdo->prepare("
|
|
INSERT INTO employee_training_log
|
|
(employee_id, training_id, action, field, old_value, new_value, changed_by, changed_at)
|
|
VALUES
|
|
(:eid, :tid, 'updated', :field, :old_v, :new_v, :cb, NOW())
|
|
");
|
|
|
|
$updated = 0;
|
|
foreach ($ids as $id) {
|
|
$rowStmt->execute(['id' => $id]);
|
|
$row = $rowStmt->fetch(PDO::FETCH_ASSOC);
|
|
if (!$row) {
|
|
continue;
|
|
}
|
|
|
|
// Effective frequency: per-record override, else topic default
|
|
$effFreq = $row['update_frequency_months'] !== null
|
|
? (int)$row['update_frequency_months']
|
|
: ($row['default_frequency_months'] !== null ? (int)$row['default_frequency_months'] : null);
|
|
|
|
$nextDue = null;
|
|
if ($effFreq !== null && $effFreq > 0) {
|
|
$d = DateTime::createFromFormat('Y-m-d', $completedDate);
|
|
if ($d) {
|
|
$d->modify('+' . $effFreq . ' months');
|
|
$nextDue = $d->format('Y-m-d');
|
|
}
|
|
}
|
|
|
|
$upd->execute(['cd' => $completedDate, 'nd' => $nextDue, 'id' => $id]);
|
|
|
|
if ((string)$row['completed_date'] !== (string)$completedDate) {
|
|
$logStmt->execute([
|
|
'eid' => $row['employee_id'], 'tid' => $id, 'field' => 'completed_date',
|
|
'old_v' => $row['completed_date'], 'new_v' => $completedDate, 'cb' => $currentUserId,
|
|
]);
|
|
}
|
|
if ((string)$row['next_due_date'] !== (string)$nextDue) {
|
|
$logStmt->execute([
|
|
'eid' => $row['employee_id'], 'tid' => $id, 'field' => 'next_due_date',
|
|
'old_v' => $row['next_due_date'], 'new_v' => $nextDue, 'cb' => $currentUserId,
|
|
]);
|
|
}
|
|
$updated++;
|
|
}
|
|
|
|
$pdo->commit();
|
|
echo json_encode([
|
|
'success' => true,
|
|
'updated' => $updated,
|
|
'message' => $updated . ' record aggiornat' . ($updated === 1 ? 'o' : 'i') . '.',
|
|
]);
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) $pdo->rollBack();
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|