import React, { useEffect, useState } from 'react'; import DateRangeFilter from '../../components/Filters/DateRangeFilter'; import './HotelPL.css'; import { useContext } from 'react'; import { langContext } from '../../context/LenguageContext'; export default function HotelPL() { const { lang } = useContext(langContext); const [dateRange, setDateRange] = useState({ from: '', to: '' }); const [revenue, setRevenue] = useState(0); const [cogs, setCogs] = useState(0); const [employeeShare, setEmployeeShare] = useState(0); const [tips, setTips] = useState(0); const [grossProfit, setGrossProfit] = useState(0); const [weightedCategoriesCost, setWeightedCategoriesCost] = useState([]); const [totalExpenses, setTotalExpenses] = useState(0); const [ebitda, setEbitda] = useState(0); const [loading, setLoading] = useState(false); const formatDate = (dateStr) => { return dateStr; }; const formatCurrency = (value) => { const num = parseFloat(value || 0); return num.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); }; const calculatePercentage = (value, total) => { if (!total || total === 0) return '0.00'; return ((parseFloat(value) / parseFloat(total)) * 100).toFixed(2); }; const formatPercentageForDisplay = (percentageValue) => { const numValue = parseFloat(percentageValue); return (numValue * 100).toFixed(2); }; const loadRevenue = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/totalrevenue`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); setRevenue(parseFloat(json.data || 0)); } catch (err) { console.error('Error loading revenue:', err); setRevenue(0); } }; const loadCogs = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/cogs`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); setCogs(parseFloat(json.data || 0)); } catch (err) { console.error('Error loading COGS:', err); setCogs(0); } }; const loadEmployeeShare = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/employeeshare`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); setEmployeeShare(parseFloat(json.data || 0)); } catch (err) { console.error('Error loading employee share:', err); setEmployeeShare(0); } }; const loadTips = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/tips`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); setTips(parseFloat(json.data || 0)); } catch (err) { console.error('Error loading tips:', err); setTips(0); } }; const loadGrossProfit = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/grossprofit`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); setGrossProfit(parseFloat(json.data || 0)); } catch (err) { console.error('Error loading gross profit:', err); setGrossProfit(0); } }; const loadWeightedCategoriesCost = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/weightedcategoriescost`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); const mapped = json.data.map(item => ({ id: item.id_expense_cat, category: item.category_name, es_category: item.spanish_name, total: item.total, participation: parseFloat(item.participation).toFixed(2) + '%' })); setWeightedCategoriesCost(mapped); } catch (err) { console.error('Error loading weigted categories:', err); } }; const loadEbidta = async () => { const start_date = formatDate(dateRange.from); const end_date = formatDate(dateRange.to); try { const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/hotelpl/ebitda`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_date, end_date }) }); const json = await res.json(); const data = json.data[0]; setTotalExpenses(parseFloat(data.expenses_total || 0)); setEbitda(parseFloat(data.ebitda || 0)); } catch (err) { console.error('Error loading ebitda:', err); setTotalExpenses(0); setEbitda(0); } }; useEffect(() => { const now = new Date(); const year = now.getFullYear(); const month = now.getMonth(); const firstDay = new Date(year, month, 1); const lastDay = new Date(year, month + 1, 0); const pad = (n) => n.toString().padStart(2, '0'); const from = `${year}-${pad(month + 1)}-${pad(firstDay.getDate())}`; const to = `${year}-${pad(month + 1)}-${pad(lastDay.getDate())}`; setDateRange({ from, to }); }, []); useEffect(() => { if (!dateRange.from || !dateRange.to) return; setLoading(true); setRevenue(0); setCogs(0); setEmployeeShare(0); setGrossProfit(0); setWeightedCategoriesCost([]); setTotalExpenses(0); setEbitda(0); Promise.all([ loadRevenue(), loadCogs(), loadEmployeeShare(), loadTips(), loadGrossProfit(), loadWeightedCategoriesCost(), loadEbidta() ]).finally(() => { setLoading(false); }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [dateRange]); const totalNetRevenue = revenue; return (