Files
paad/service/backend/periodos.php
2023-08-02 09:12:46 -06:00

60 lines
1.6 KiB
PHP

<?
$ruta = "../../";
require_once "$ruta/include/bd_pdo.php";
header('Content-Type: application/json');
// json data from service\periodos.v1.php (input)
$data = json_decode(file_get_contents('php://input'), true);
// check if the input is empty
if (is_response_empty($data)) {
echo json_encode([
'status' => 'error',
'message' => 'No se recibieron datos',
]);
exit;
}
/*
{
"IdNivel": 1,
"IdPeriodo": 635,
"NombreNivel": "LICENCIATURA",
"NombrePeriodo": "241",
"in_db": false
inicio,
fin
}
*/
// insert into database
setlocale(LC_TIME, 'es_MX.UTF-8');
$formatter = new IntlDateFormatter('es_MX', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Mexico_City', IntlDateFormatter::GREGORIAN, 'MMMM');
$inicio = strtotime($data['inicio']);
$fin = strtotime($data['fin']);
try {
$result = $db->insert('periodo', [
'id_reference' => $data['IdPeriodo'],
'periodo_nombre' => "{$data['NombreNivel']}: {$formatter->format($inicio)} - {$formatter->format($fin)} " . date('Y', $inicio),
'nivel_id' => $data['IdNivel'],
'periodo_fecha_inicio' => $data['inicio'],
'periodo_fecha_fin' => $data['fin'],
'estado_id' => 4,
'periodo_clave' => $data['NombrePeriodo']
], ['id_reference']);
} catch (PDOException $th) {
echo json_encode([
'success' => false,
'message' => $th->getMessage()
]);
exit;
}
echo json_encode($result ? [
'success' => true,
'message' => 'Periodo agregado correctamente'
] : [
'success' => false,
'message' => 'Error al agregar el periodo'
]);