import { LstCard } from "@/components/extendedUI/LstCard"; import { Button } from "@/components/ui/button"; import { CardHeader } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { getStockSilo } from "@/utils/querys/logistics/siloAdjustments/getStockSilo"; import { useForm } from "@tanstack/react-form"; import { useQuery } from "@tanstack/react-query"; import { Link } from "@tanstack/react-router"; import axios from "axios"; import { format } from "date-fns"; import { CircleAlert } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; import ChartData from "./ChartData"; import { AttachSilo } from "./AttachSilo"; import { DetachSilo } from "./DetachSilo"; import { useSessionStore } from "@/lib/store/sessionStore"; import { useModuleStore } from "@/lib/store/useModuleStore"; import { useGetUserRoles } from "@/lib/store/useGetRoles"; export default function SiloCard(data: any) { const token = localStorage.getItem("auth_token"); const [submitting, setSubmitting] = useState(false); const { refetch } = useQuery(getStockSilo()); const { user } = useSessionStore(); const { userRoles } = useGetUserRoles(); const { modules } = useModuleStore(); const silo = data.silo; // roles that can do the silo adjustments const roles = ["systemAdmin", "technician", "admin", "manager"]; const module = modules.filter((n) => n.name === "logistics"); const accessRoles = userRoles.filter( (n) => n.module_id === module[0]?.module_id ) as any; const form = useForm({ defaultValues: { newLevel: "", }, onSubmit: async ({ value }) => { setSubmitting(true); const dataToSubmit = { quantity: parseFloat(value.newLevel), warehouseId: silo.WarehouseID, laneId: silo.LocationID, }; try { const res = await axios.post( "/api/logistics/createsiloadjustment", dataToSubmit, { headers: { Authorization: `Bearer ${token}` } } ); //console.log(res.data); if (res.data.success) { toast.success(res.data.message); refetch(); form.reset(); } if (!res.data.success && res.data.data?.status === 400) { if (res.data.data.status === 400) { toast.error(res.data.data.data.errors[0].message); } } else if (!res.data.success) { toast.error(res.data.message); } setSubmitting(false); } catch (error: any) { //console.log(error); if (error.status === 401) { toast.error(error.response.statusText); setSubmitting(false); } } }, }); console.log(accessRoles); return ( {silo.Description} Current Stock: {silo.Stock_Total} Last date adjusted {format(silo.LastAdjustment, "M/dd/yyyy")} {silo.Stock_Total === 0 ? ( The silo is currently empty you will not be able to do an adjustment until you have received material in. -Someone click "Take inventory on a empty location" in stock. -Silo virtualy ran empty due to production over consumption. -Someone forgot to move a railcar compartment over to this location. ) : ( <> {user && roles.includes(accessRoles[0]?.role) && ( { e.preventDefault(); e.stopPropagation(); }} > value.length > 1 ? undefined : "You must enter a value greate than 1", }} children={(field) => { return ( New level field.handleChange( e .target .value ) } /> {submitting ? ( Submitting... ) : ( Submit )} {field.state.meta .errors .length ? ( {field.state.meta.errors.join( "," )} ) : null} ); }} /> )} > )} Historical Data ); } const Disclaimer = () => { return ( If you have had this page open for a period of time before submitting your data, there is a chance that the stock levels will be different from the ones you see above ); };
If you have had this page open for a period of time before submitting your data, there is a chance that the stock levels will be different from the ones you see above