Files

341 lines
8.2 KiB
TypeScript
Raw Permalink Normal View History

2026-01-13 18:51:56 -06:00
"use client";
import { useState } from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
2026-02-23 09:47:40 -06:00
import { envConfig } from "@/app/lib/config";
2026-01-13 18:51:56 -06:00
import Selection from "../Selection/Selection";
2026-01-23 13:46:46 -06:00
import Cookies from "js-cookie";
import axios from "axios";
2026-02-11 15:09:45 -05:00
import Swal from "sweetalert2";
2026-02-23 09:47:40 -06:00
import "./Receipt.css";
2026-01-13 18:51:56 -06:00
interface ReceiptsProps {
numAcount: number | null;
2026-01-22 10:26:15 -06:00
plataformasInscritas: string[];
2026-01-13 18:51:56 -06:00
}
2026-01-22 10:53:07 -06:00
const PLATAFORMA_MAP: Record<string, number> = {
WINDOWS: 1,
MACINTOSH: 2,
PROFESORES: 5,
};
export default function Inscripcion({
numAcount,
plataformasInscritas,
}: ReceiptsProps) {
const [conPago, setConPago] = useState<"con" | "sin">("con");
const [plataformaSeleccionada, setPlataformaSeleccionada] = useState<
string | null
>(null);
2026-01-13 18:51:56 -06:00
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
2026-01-13 18:51:56 -06:00
const [folio, setFolio] = useState("");
const [amount, setAmount] = useState("");
2026-02-09 12:23:03 -06:00
const [lock, setLock] = useState<boolean>(true);
2026-02-16 19:04:18 -06:00
const todayISO = new Date().toLocaleDateString("en-CA");
2026-01-23 12:43:01 -06:00
const [date, setDate] = useState(todayISO);
2026-01-13 18:51:56 -06:00
//restrict this month//
const day = new Date();
const year = day.getFullYear();
const month = day.getMonth();
const today = day.getDate();
2026-03-02 14:17:43 -06:00
let minMount = month
if(!lock){
minMount = month-1
}
const minFecha = new Date(year, minMount, 1).toISOString().split("T")[0];
2026-01-13 18:51:56 -06:00
const maxFecha = new Date(year, month, today).toISOString().split("T")[0];
//restrict this month//
const handleSaveReceipt = async () => {
if (numAcount == null) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "busca de nuevo al estudiante!",
icon: "error",
draggable: true
});
2026-01-13 18:51:56 -06:00
return;
}
if (!folio) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Ingresa el folio del ticket!",
icon: "error",
draggable: true
});
2026-01-13 18:51:56 -06:00
return;
}
if (!amount) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Coloca el monto a depositar!",
icon: "error",
draggable: true
});
2026-01-13 18:51:56 -06:00
return;
}
if (!date) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Coloca la fecha!",
icon: "error",
draggable: true
});
2026-01-13 18:51:56 -06:00
return;
}
2026-01-23 12:35:35 -06:00
if (!plataformaSeleccionada) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Selecciona una plataforma!",
icon: "error",
draggable: true
});
2026-01-23 12:35:35 -06:00
return;
}
const id_plataforma = PLATAFORMA_MAP[plataformaSeleccionada];
2026-01-13 18:51:56 -06:00
try {
2026-01-23 13:46:46 -06:00
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-01-13 18:51:56 -06:00
2026-01-23 13:46:46 -06:00
await axios.post(
`${envConfig.apiUrl}/operations/registration`,
{
monto: Number(amount),
2026-01-23 12:35:35 -06:00
id_cuenta: numAcount,
id_plataforma,
2026-01-23 13:46:46 -06:00
folio_recibo: folio,
fecha_recibo: date,
realizo_pago: true,
},
{ headers },
);
2026-01-23 12:35:35 -06:00
Swal.fire({
title: "Alumno inscrito con pago",
icon: "success",
draggable: true
});
2026-01-13 18:51:56 -06:00
setFolio("");
setAmount("");
2026-02-11 16:30:02 -06:00
setDate(todayISO);
2026-01-13 18:51:56 -06:00
const params = new URLSearchParams(searchParams.toString());
params.delete("numAcount");
router.push(`${pathname}?${params.toString()}`);
2026-02-18 19:14:37 -06:00
} catch (error: any) {
const msg =
error.response?.data?.message ||
error.message ||
"Error desconocido al crear recibo";
Swal.fire({
title: msg,
icon: "error",
draggable: true
});
2026-01-13 18:51:56 -06:00
}
};
2026-01-22 10:53:07 -06:00
const handleInscripcionSinPago = async () => {
if (numAcount == null) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "busca de nuevo al estudiante!",
icon: "error",
draggable: true
});
2026-01-22 10:53:07 -06:00
return;
}
if (!plataformaSeleccionada) {
2026-02-20 14:33:08 -05:00
Swal.fire({
title: "Selecciona una plataforma!",
icon: "error",
draggable: true
});
2026-01-22 10:53:07 -06:00
return;
}
const id_plataforma = PLATAFORMA_MAP[plataformaSeleccionada];
try {
2026-02-26 14:38:28 -06:00
const token = Cookies.get("token");
const headers = { Authorization: `Bearer ${token}` };
2026-02-23 13:31:02 -06:00
await axios.post(
2026-02-23 12:47:08 -06:00
`${envConfig.apiUrl}/alumno-inscrito`,
{
2026-01-22 10:53:07 -06:00
id_cuenta: numAcount,
id_plataforma,
realizo_pago: false,
2026-02-26 14:38:28 -06:00
},
{ headers },
2026-02-23 12:47:08 -06:00
);
2026-02-11 15:09:45 -05:00
Swal.fire({
title: "Alumno inscrito sin pago!",
icon: "success",
2026-02-18 19:16:00 -06:00
draggable: true,
2026-02-11 15:09:45 -05:00
});
2026-02-23 12:47:08 -06:00
2026-01-22 10:53:07 -06:00
router.refresh();
2026-02-23 12:47:08 -06:00
2026-01-22 10:53:07 -06:00
} catch (err: any) {
2026-02-23 12:47:08 -06:00
const message =
err.response?.data?.message || "Error al inscribir";
Swal.fire({
2026-02-23 12:47:08 -06:00
title: message,
icon: "error",
draggable: true,
});
2026-01-22 10:53:07 -06:00
}
};
2026-01-13 18:51:56 -06:00
return (
<section>
2026-01-22 10:53:07 -06:00
<Selection
plataformasInscritas={plataformasInscritas}
onSelect={setPlataformaSeleccionada}
/>
2026-01-13 18:51:56 -06:00
<select
2026-01-22 10:53:07 -06:00
value={conPago}
onChange={(e) => setConPago(e.target.value as "con" | "sin")}
2026-01-13 18:51:56 -06:00
style={{
2026-01-22 10:53:07 -06:00
maxWidth: "120px",
minWidth: "120px",
2026-01-13 18:51:56 -06:00
}}
>
2026-01-22 10:53:07 -06:00
<option value="con">con pago</option>
<option value="sin">sin pago</option>
2026-01-13 18:51:56 -06:00
</select>
2026-01-22 10:53:07 -06:00
{conPago === "con" && (
<form
onSubmit={(e) => {
e.preventDefault();
handleSaveReceipt();
}}
>
<div className="gap">
<div className="groupInput">
<label className="label">Ticket:</label>
<input
type="text"
value={folio}
onChange={(e) => {
const value = e.target.value;
if (/^\d*$/.test(value) && value.length <= 7) {
setFolio(value);
2026-01-13 18:51:56 -06:00
}
2026-01-22 10:53:07 -06:00
}}
2026-01-23 12:43:01 -06:00
placeholder="Numero de ticket..."
inputMode="numeric"
2026-01-22 10:53:07 -06:00
/>
</div>
<div className="groupInput">
<label className="label">Monto:</label>
<input
type="text"
value={amount}
onChange={(e) => {
const value = e.target.value;
2026-02-09 12:23:03 -06:00
2026-01-22 10:53:07 -06:00
if (/^\d*\.?\d*$/.test(value)) {
const numericValue = parseFloat(value);
2026-02-09 12:23:03 -06:00
if (lock && numericValue > 1000) {
2026-02-18 19:16:00 -06:00
Swal.fire({
title:
"El monto no puede superar $1000.00 Desbloquea el candado !",
icon: "error",
draggable: true,
});
2026-02-09 12:23:03 -06:00
return;
2026-01-22 10:53:07 -06:00
}
2026-02-09 12:23:03 -06:00
if (numericValue > 10000) {
Swal.fire({
title:
"El monto no puede superar $10000.00 pesos",
icon: "error",
draggable: true,
});
return;
}
2026-02-09 12:23:03 -06:00
setAmount(value);
2026-01-22 10:53:07 -06:00
}
}}
2026-01-23 12:43:01 -06:00
placeholder="Monto recibido..."
inputMode="numeric"
2026-01-22 10:53:07 -06:00
/>
</div>
<div className="groupInput">
<label className="label">Fecha de Pago:</label>
<input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
min={minFecha}
max={maxFecha}
/>
</div>
2026-02-18 19:16:00 -06:00
<div className="containerButton" style={{ position: "relative" }}>
2026-02-23 10:01:45 -06:00
<button className="button buttonSearch" disabled={!plataformaSeleccionada}
>Inscribir</button>
2026-02-09 12:23:03 -06:00
<button
type="button"
className={`button buttonCancel ${lock ? "buttonLock" : "buttonOpenLock"
}`}
2026-02-09 12:23:03 -06:00
onClick={() => {
2026-02-18 19:16:00 -06:00
setLock((prev) => !prev);
setAmount("");
2026-02-09 12:23:03 -06:00
}}
/>
2026-01-22 10:53:07 -06:00
</div>
2026-01-13 18:51:56 -06:00
</div>
2026-01-22 10:53:07 -06:00
</form>
)}
{conPago === "sin" && (
<div className="containerButton">
<button
className="button buttonSearch"
disabled={!plataformaSeleccionada}
onClick={handleInscripcionSinPago}
>
Inscribir
</button>
2026-01-13 18:51:56 -06:00
</div>
2026-01-22 10:53:07 -06:00
)}
2026-01-13 18:51:56 -06:00
</section>
);
}
//IO