up
This commit is contained in:
@@ -18,6 +18,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
$row->execute([$id]);
|
||||
$data = $row->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$data) jsonError('Lugar no encontrado', 404);
|
||||
// Incluir formularios de consentimiento vinculados
|
||||
$stmtFc = db()->prepare('SELECT formulario_id FROM turnero_lugar_consentimientos WHERE lugar_id = ?');
|
||||
$stmtFc->execute([$id]);
|
||||
$data['formulario_ids'] = $stmtFc->fetchAll(PDO::FETCH_COLUMN);
|
||||
jsonOk(['data' => $data]);
|
||||
}
|
||||
|
||||
@@ -48,20 +52,44 @@ $sortOrder = (int)($input['sort_order'] ?? 99);
|
||||
$activo = (int)($input['activo'] ?? 1);
|
||||
$tipo = in_array($input['tipo'] ?? '', ['recepcion', 'muestras'], true)
|
||||
? $input['tipo'] : 'muestras';
|
||||
$formularioIds = isset($input['formulario_ids']) && is_array($input['formulario_ids'])
|
||||
? array_filter(array_map('intval', $input['formulario_ids']), fn($v) => $v > 0)
|
||||
: [];
|
||||
|
||||
if ($nombre === '') jsonError('El nombre es requerido');
|
||||
if ($sortOrder < 1) jsonError('El orden debe ser mayor a 0');
|
||||
|
||||
if ($id) {
|
||||
$stmt = db()->prepare(
|
||||
'UPDATE turnero_lugares SET nombre=?, tipo=?, descripcion=?, sort_order=?, activo=? WHERE id=?'
|
||||
);
|
||||
$stmt->execute([$nombre, $tipo, $desc ?: null, $sortOrder, $activo, $id]);
|
||||
jsonOk(['id' => $id], 'Lugar actualizado');
|
||||
} else {
|
||||
$stmt = db()->prepare(
|
||||
'INSERT INTO turnero_lugares (nombre, tipo, descripcion, sort_order, activo) VALUES (?, ?, ?, ?, ?)'
|
||||
);
|
||||
$stmt->execute([$nombre, $tipo, $desc ?: null, $sortOrder, $activo]);
|
||||
jsonOk(['id' => (int)db()->lastInsertId()], 'Lugar creado');
|
||||
$pdo = db();
|
||||
$pdo->beginTransaction();
|
||||
try {
|
||||
if ($id) {
|
||||
$stmt = $pdo->prepare(
|
||||
'UPDATE turnero_lugares SET nombre=?, tipo=?, descripcion=?, sort_order=?, activo=? WHERE id=?'
|
||||
);
|
||||
$stmt->execute([$nombre, $tipo, $desc ?: null, $sortOrder, $activo, $id]);
|
||||
$lugarId = $id;
|
||||
} else {
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO turnero_lugares (nombre, tipo, descripcion, sort_order, activo) VALUES (?, ?, ?, ?, ?)'
|
||||
);
|
||||
$stmt->execute([$nombre, $tipo, $desc ?: null, $sortOrder, $activo]);
|
||||
$lugarId = (int) $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
// Sincronizar formularios de consentimiento
|
||||
$pdo->prepare('DELETE FROM turnero_lugar_consentimientos WHERE lugar_id = ?')->execute([$lugarId]);
|
||||
if (!empty($formularioIds)) {
|
||||
$stmtIns = $pdo->prepare(
|
||||
'INSERT IGNORE INTO turnero_lugar_consentimientos (lugar_id, formulario_id) VALUES (?, ?)'
|
||||
);
|
||||
foreach ($formularioIds as $fid) {
|
||||
$stmtIns->execute([$lugarId, $fid]);
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
jsonOk(['id' => $lugarId], $id ? 'Lugar actualizado' : 'Lugar creado');
|
||||
} catch (\Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
jsonError('Error al guardar lugar: ' . $e->getMessage(), 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user