Files
front-AT/app/Components/Receipt/Receipt.tsx
T

153 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-09-19 17:20:05 -06:00
"use client";
2025-09-22 11:57:52 -06:00
2025-10-01 15:29:23 -06:00
import toast from "react-hot-toast";
2025-09-19 17:20:05 -06:00
import { useState } from "react";
import { PostReceipt } from "@/app/lib/postReceipt";
2025-10-06 17:26:14 -06:00
import { useRouter } from "next/navigation";
2025-09-19 17:20:05 -06:00
import "./Receipt.css";
interface ReceiptsProps {
2025-09-22 12:15:32 -04:00
numAcount: number | null;
2025-09-19 17:20:05 -06:00
}
2025-09-29 15:37:56 -06:00
function Receipt({ numAcount }: ReceiptsProps) {
2025-09-22 11:57:52 -06:00
const router = useRouter();
2025-09-19 17:20:05 -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-01-23 14:57:17 -06:00
const todayISO = new Date().toISOString().split("T")[0];
const [date, setDate] = useState(todayISO);
2025-09-19 17:20:05 -06:00
//restrict this month//
const day = new Date();
const year = day.getFullYear();
const month = day.getMonth();
const today = day.getDate();
const minFecha = new Date(year, month, 1).toISOString().split("T")[0];
const maxFecha = new Date(year, month, today).toISOString().split("T")[0];
//restrict this month//
2025-09-22 11:57:52 -06:00
const handleSaveReceipt = async () => {
2025-09-22 12:15:32 -04:00
if (!numAcount) {
2025-10-01 15:29:23 -06:00
toast.error("busca de nuevo al estudiante");
return;
2025-09-17 16:47:19 -06:00
}
2025-09-19 17:20:05 -06:00
if (!folio) {
2025-10-01 15:29:23 -06:00
toast.error("Ingresa el folio del ticket");
return;
2025-09-19 17:20:05 -06:00
}
if (!amount) {
2025-10-01 15:29:23 -06:00
toast.error("Coloca el monto a depositar");
return;
2025-09-19 17:20:05 -06:00
}
if (!date) {
2025-10-01 15:29:23 -06:00
toast.error("Coloca la fecha");
return;
2025-09-19 17:20:05 -06:00
}
2025-09-22 11:57:52 -06:00
try {
await PostReceipt({
id_cuenta: numAcount,
folio_recibo: folio,
monto: Number(amount),
fecha_recibo: date,
});
setFolio("");
setAmount("");
setDate("");
2025-10-01 15:29:23 -06:00
toast.success("Recibo guardado");
2025-10-03 17:29:53 -06:00
router.refresh();
2025-09-22 11:57:52 -06:00
} catch (err: any) {
2025-10-01 15:29:23 -06:00
toast.error(String(err));
2025-09-22 11:57:52 -06:00
}
2025-09-19 17:20:05 -06:00
};
return (
<form
onSubmit={(e) => {
e.preventDefault();
handleSaveReceipt();
}}
>
<div className="gap">
<div className="groupInput">
<label className="label">Ticket:</label>
2025-10-08 12:01:52 -06:00
<input
type="text"
value={folio}
onChange={(error) => {
const value = error.target.value;
if (/^\d*$/.test(value) && value.length <= 7) {
setFolio(value);
}
}}
placeholder="Numero de tiket..."
inputMode="numeric"
pattern="[0-9]*"
/>
2025-09-19 17:20:05 -06:00
</div>
<div className="groupInput">
<label className="label">Monto:</label>
<input
type="text"
value={amount}
onChange={(e) => {
const value = e.target.value;
if (/^\d*\.?\d*$/.test(value)) {
const numericValue = parseFloat(value);
2026-02-09 12:23:03 -06:00
if (lock && numericValue > 1000) {
2025-10-01 15:29:23 -06:00
toast.error("El monto no puede superar $1000.00");
2026-02-09 12:23:03 -06:00
return;
2025-09-19 17:20:05 -06:00
}
2026-02-09 12:23:03 -06:00
setAmount(value);
2025-09-19 17:20:05 -06:00
}
2025-09-02 19:50:17 -04:00
}}
2025-09-19 17:20:05 -06:00
placeholder="Monto recibido..."
inputMode="numeric"
pattern="^\d*\.?\d+$"
/>
</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-09 12:23:03 -06:00
<div className="containerButton" style={{ position: 'relative' }}>
2026-01-13 18:51:56 -06:00
<button className="button buttonSearch" type="submit">Guardar</button>
2026-02-09 12:23:03 -06:00
<button
type="button"
className={`button buttonCancel ${lock ? "buttonLock" : "buttonOpenLock"
}`}
onClick={() => {
setLock((prev) => !prev)
setAmount('');
}}
/>
2025-09-19 17:20:05 -06:00
</div>
</div>
</form>
);
2025-09-02 19:50:17 -04:00
}
2025-09-19 17:20:05 -06:00
export default Receipt;
//IO