fix: dohvat mjesecnih racuna za troskove
Uklanja ovisnost o workOrders.invoices i dohvaća račune direktno kroz API filtriran po mjesecu. Dodaje i display code putnog naloga u serializer kako bi prikaz u izvještaju ostao potpun. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { getStatusLabel } from '../../stores/taskStore';
|
||||
import {
|
||||
downloadMonthlyCostsReport,
|
||||
downloadMonthlyServiserReport,
|
||||
fetchMonthlyCostInvoices,
|
||||
fetchMonthlyServicerEntries,
|
||||
upsertMonthlyServicerEntry,
|
||||
} from '../../stores/fleetDashboardStore';
|
||||
@@ -65,7 +66,9 @@ export default function TaskCalendarWidget({ tasks = [], notes = [], workOrders
|
||||
const [reportType, setReportType] = useState(null);
|
||||
const [downloading, setDownloading] = useState(false);
|
||||
const [manualEntries, setManualEntries] = useState([]);
|
||||
const [costInvoices, setCostInvoices] = useState([]);
|
||||
const [loadingManualEntries, setLoadingManualEntries] = useState(false);
|
||||
const [loadingCostInvoices, setLoadingCostInvoices] = useState(false);
|
||||
const [manualEntryTarget, setManualEntryTarget] = useState(null);
|
||||
const [savingManualEntry, setSavingManualEntry] = useState(false);
|
||||
|
||||
@@ -148,6 +151,28 @@ export default function TaskCalendarWidget({ tasks = [], notes = [], workOrders
|
||||
};
|
||||
}, [reportOpen, reportType, viewYear, viewMonth]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!reportOpen || reportType !== 'costs') return undefined;
|
||||
let active = true;
|
||||
setLoadingCostInvoices(true);
|
||||
fetchMonthlyCostInvoices(viewYear, viewMonth + 1)
|
||||
.then((rows) => {
|
||||
if (!active) return;
|
||||
setCostInvoices(Array.isArray(rows) ? rows : []);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!active) return;
|
||||
setCostInvoices([]);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!active) return;
|
||||
setLoadingCostInvoices(false);
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [reportOpen, reportType, viewYear, viewMonth]);
|
||||
|
||||
const servicerRows = useMemo(() => {
|
||||
if (reportType !== 'servicer') return [];
|
||||
const rows = [];
|
||||
@@ -248,28 +273,14 @@ export default function TaskCalendarWidget({ tasks = [], notes = [], workOrders
|
||||
|
||||
const costsRows = useMemo(() => {
|
||||
if (reportType !== 'costs') return [];
|
||||
const monthPrefix = `${viewYear}-${String(viewMonth + 1).padStart(2, '0')}`;
|
||||
const workOrderIds = new Set(
|
||||
tasks
|
||||
.filter((task) => {
|
||||
const key = toDateKey(task.scheduled_date);
|
||||
return Boolean(key && key.startsWith(monthPrefix) && task.work_order);
|
||||
})
|
||||
.map((task) => String(task.work_order))
|
||||
);
|
||||
|
||||
return workOrders
|
||||
.filter((workOrder) => workOrderIds.has(String(workOrder.id)) && Array.isArray(workOrder.invoices))
|
||||
.flatMap((workOrder) => (
|
||||
workOrder.invoices.map((invoice) => ([
|
||||
invoice?.datum ? new Date(invoice.datum).toLocaleDateString('hr-HR') : '-',
|
||||
invoice?.naziv_racuna || '-',
|
||||
invoice?.lokacija || '-',
|
||||
invoice?.opis || '-',
|
||||
workOrder.display_code || '-',
|
||||
]))
|
||||
));
|
||||
}, [reportType, tasks, viewYear, viewMonth, workOrders]);
|
||||
return costInvoices.map((invoice) => ([
|
||||
invoice?.datum ? new Date(invoice.datum).toLocaleDateString('hr-HR') : '-',
|
||||
invoice?.naziv_racuna || '-',
|
||||
invoice?.lokacija || '-',
|
||||
invoice?.opis || '-',
|
||||
invoice?.work_order_display_code || '-',
|
||||
]));
|
||||
}, [costInvoices, reportType]);
|
||||
|
||||
const selectedDayKey = selectedDay
|
||||
? `${viewYear}-${String(viewMonth + 1).padStart(2, '0')}-${String(selectedDay).padStart(2, '0')}`
|
||||
@@ -628,7 +639,9 @@ export default function TaskCalendarWidget({ tasks = [], notes = [], workOrders
|
||||
)}
|
||||
|
||||
{reportType === 'costs' && (
|
||||
costsRows.length === 0 ? (
|
||||
loadingCostInvoices ? (
|
||||
<p className="px-4 py-8 text-center text-sm text-text-muted">Ucitam troskove...</p>
|
||||
) : costsRows.length === 0 ? (
|
||||
<p className="px-4 py-8 text-center text-sm text-text-muted">
|
||||
Nema troskova za {MONTH_NAMES[viewMonth]} {viewYear}.
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user