1 | 'use client';
|
---|
2 |
|
---|
3 | import { useState, useCallback, useEffect, useMemo } from 'react';
|
---|
4 | // @mui
|
---|
5 | import { useTheme, alpha } from '@mui/material/styles';
|
---|
6 | import Tab from '@mui/material/Tab';
|
---|
7 | import Tabs from '@mui/material/Tabs';
|
---|
8 | import Card from '@mui/material/Card';
|
---|
9 | import Table from '@mui/material/Table';
|
---|
10 | import Stack from '@mui/material/Stack';
|
---|
11 | import Button from '@mui/material/Button';
|
---|
12 | import Divider from '@mui/material/Divider';
|
---|
13 | import Container from '@mui/material/Container';
|
---|
14 | import TableBody from '@mui/material/TableBody';
|
---|
15 | import TableContainer from '@mui/material/TableContainer';
|
---|
16 | // routes
|
---|
17 | import { paths } from 'src/routes/paths';
|
---|
18 | import { useRouter } from 'src/routes/hooks';
|
---|
19 | import { RouterLink } from 'src/routes/components';
|
---|
20 | // utils
|
---|
21 | import { fTimestamp } from 'src/utils/format-time';
|
---|
22 | // components
|
---|
23 | import Label from 'src/components/label';
|
---|
24 | import Iconify from 'src/components/iconify';
|
---|
25 | import Scrollbar from 'src/components/scrollbar';
|
---|
26 | import { useSettingsContext } from 'src/components/settings';
|
---|
27 | import CustomBreadcrumbs from 'src/components/custom-breadcrumbs';
|
---|
28 | import {
|
---|
29 | useTable,
|
---|
30 | getComparator,
|
---|
31 | emptyRows,
|
---|
32 | TableNoData,
|
---|
33 | TableEmptyRows,
|
---|
34 | TableHeadCustom,
|
---|
35 | TablePaginationCustom,
|
---|
36 | } from 'src/components/table';
|
---|
37 | // types
|
---|
38 | import {
|
---|
39 | Invoice,
|
---|
40 | InvoiceStatus,
|
---|
41 | InvoiceTableFilters,
|
---|
42 | InvoiceTableFilterValue,
|
---|
43 | } from 'mvpmasters-shared';
|
---|
44 | //
|
---|
45 | import deleteFromFirebaseStorage from 'src/utils/delete-from-firebase-storage';
|
---|
46 | // fetch
|
---|
47 | import { useGetInvoices } from 'src/api/invoice';
|
---|
48 | import { collections, removeDocument } from 'src/lib/firestore';
|
---|
49 | import { mutate } from 'swr';
|
---|
50 | import { useBoolean } from 'src/hooks/use-boolean';
|
---|
51 | import InvoiceAnalytic from '../invoice-analytic';
|
---|
52 | import InvoiceTableRow from '../invoice-table-row';
|
---|
53 | import InvoiceTableFiltersResult from '../invoice-table-filters-result';
|
---|
54 | import InvoiceTableToolbar from '../invoice-table-toolbar';
|
---|
55 | import MailCompose from '../mail-compose';
|
---|
56 |
|
---|
57 | // ----------------------------------------------------------------------
|
---|
58 |
|
---|
59 | const TABLE_HEAD = [
|
---|
60 | { id: 'invoiceNumber', label: 'Customer' },
|
---|
61 | { id: 'price', label: 'Amount' },
|
---|
62 | { id: 'currency', label: 'Currency' },
|
---|
63 | { id: 'invoicePeriod', label: 'Invoice Period' },
|
---|
64 | { id: 'createDate', label: 'Create' },
|
---|
65 | { id: 'dueDate', label: 'Due' },
|
---|
66 | { id: 'sent', label: 'Sent', align: 'center' },
|
---|
67 | { id: 'status', label: 'Status' },
|
---|
68 | { id: '' },
|
---|
69 | ];
|
---|
70 |
|
---|
71 | const defaultFilters: InvoiceTableFilters = {
|
---|
72 | name: '',
|
---|
73 | service: [],
|
---|
74 | status: 'all',
|
---|
75 | startDate: new Date('2024-01-01T00:00:00Z'),
|
---|
76 | endDate: null,
|
---|
77 | };
|
---|
78 |
|
---|
79 | // ----------------------------------------------------------------------
|
---|
80 |
|
---|
81 | interface StatusTotals {
|
---|
82 | EUR: number;
|
---|
83 | USD: number;
|
---|
84 | }
|
---|
85 |
|
---|
86 | type ResultsType = {
|
---|
87 | [key in 'total' | InvoiceStatus]: StatusTotals;
|
---|
88 | };
|
---|
89 |
|
---|
90 | const getEURtoUSDExchangeRate = async () => {
|
---|
91 | try {
|
---|
92 | // const response = await fetch(
|
---|
93 | // 'http://api.exchangeratesapi.io/v1/latest?access_key=248cc5d9f103fcb183ed4987f2d85b3b&base=EUR&symbols=USD'
|
---|
94 | // );
|
---|
95 | // const data = await response.json();
|
---|
96 | // return data.rates.USD;
|
---|
97 | return 1.05;
|
---|
98 | } catch (error) {
|
---|
99 | console.error('Error fetching the exchange rate:', error);
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 | };
|
---|
103 |
|
---|
104 | const getTotalAmountForAllStatuses = async (invoices: Invoice[]) => {
|
---|
105 | const eurToUsdRate = await getEURtoUSDExchangeRate();
|
---|
106 | if (!eurToUsdRate) {
|
---|
107 | throw new Error("Couldn't fetch the exchange rate.");
|
---|
108 | }
|
---|
109 |
|
---|
110 | const results: ResultsType = {
|
---|
111 | total: { EUR: 0, USD: 0 },
|
---|
112 | // excluded
|
---|
113 | processing: { EUR: 0, USD: 0 },
|
---|
114 | paid: { EUR: 0, USD: 0 },
|
---|
115 | pending: { EUR: 0, USD: 0 },
|
---|
116 | overdue: { EUR: 0, USD: 0 },
|
---|
117 | draft: { EUR: 0, USD: 0 },
|
---|
118 | };
|
---|
119 |
|
---|
120 | invoices.forEach((invoice) => {
|
---|
121 | const updateAmounts = (key: 'total' | InvoiceStatus) => {
|
---|
122 | const excludeProcessing = key === 'processing' ? 'pending' : key;
|
---|
123 | if (invoice.currency === 'USD') {
|
---|
124 | results[excludeProcessing].USD += invoice.totalAmount;
|
---|
125 | results[excludeProcessing].EUR += invoice.totalAmount / eurToUsdRate; // Convert USD to EUR
|
---|
126 | } else if (invoice.currency === 'EUR') {
|
---|
127 | results[excludeProcessing].EUR += invoice.totalAmount;
|
---|
128 | results[excludeProcessing].USD += invoice.totalAmount * eurToUsdRate; // Convert EUR to USD
|
---|
129 | }
|
---|
130 | };
|
---|
131 |
|
---|
132 | updateAmounts('total');
|
---|
133 | updateAmounts(invoice.status as InvoiceStatus);
|
---|
134 | });
|
---|
135 |
|
---|
136 | return results;
|
---|
137 | };
|
---|
138 |
|
---|
139 | export default function InvoiceListView() {
|
---|
140 | const theme = useTheme();
|
---|
141 |
|
---|
142 | const settings = useSettingsContext();
|
---|
143 |
|
---|
144 | const [analytics, setAnalytics] = useState({
|
---|
145 | total: { EUR: 0, USD: 0 },
|
---|
146 | paid: { EUR: 0, USD: 0 },
|
---|
147 | pending: { EUR: 0, USD: 0 },
|
---|
148 | overdue: { EUR: 0, USD: 0 },
|
---|
149 | draft: { EUR: 0, USD: 0 },
|
---|
150 | });
|
---|
151 |
|
---|
152 | const [sendingInvoice, setSendingInvoice] = useState<Invoice | null>(null);
|
---|
153 |
|
---|
154 | const router = useRouter();
|
---|
155 |
|
---|
156 | const table = useTable({
|
---|
157 | defaultOrderBy: 'createDate',
|
---|
158 | defaultOrder: 'desc',
|
---|
159 | defaultDense: true,
|
---|
160 | defaultRowsPerPage: 25,
|
---|
161 | });
|
---|
162 |
|
---|
163 | const [filters, setFilters] = useState(defaultFilters);
|
---|
164 |
|
---|
165 | const { invoices: tableData } = useGetInvoices({
|
---|
166 | where: [['createDate', '>=', filters.startDate]],
|
---|
167 | orderBy: 'createDate',
|
---|
168 | direction: 'desc',
|
---|
169 | });
|
---|
170 |
|
---|
171 | const invoiceMutationKey = useMemo(
|
---|
172 | () => [
|
---|
173 | collections.invoice,
|
---|
174 | JSON.stringify({
|
---|
175 | where: [['createDate', '>=', filters.startDate]],
|
---|
176 | orderBy: 'createDate',
|
---|
177 | direction: 'desc',
|
---|
178 | }),
|
---|
179 | ],
|
---|
180 | [filters]
|
---|
181 | );
|
---|
182 |
|
---|
183 | const dateError =
|
---|
184 | filters.startDate && filters.endDate
|
---|
185 | ? filters.startDate.getTime() > filters.endDate.getTime()
|
---|
186 | : false;
|
---|
187 |
|
---|
188 | const dataFiltered = applyFilter({
|
---|
189 | inputData: tableData,
|
---|
190 | comparator: getComparator(table.order, table.orderBy),
|
---|
191 | filters,
|
---|
192 | dateError,
|
---|
193 | });
|
---|
194 |
|
---|
195 | // const dataInPage = dataFiltered.slice(
|
---|
196 | // table.page * table.rowsPerPage,
|
---|
197 | // table.page * table.rowsPerPage + table.rowsPerPage
|
---|
198 | // );
|
---|
199 |
|
---|
200 | const denseHeight = table.dense ? 56 : 76;
|
---|
201 |
|
---|
202 | const canReset =
|
---|
203 | !!filters.name ||
|
---|
204 | !!filters.service.length ||
|
---|
205 | filters.status !== 'all' ||
|
---|
206 | (!!filters.startDate && !!filters.endDate);
|
---|
207 |
|
---|
208 | const notFound = (!dataFiltered.length && canReset) || !dataFiltered.length;
|
---|
209 |
|
---|
210 | const getInvoiceLength = (status: string) =>
|
---|
211 | tableData.filter((item) => item.status === status).length;
|
---|
212 |
|
---|
213 | // const getTotalAmount = (status: string) =>
|
---|
214 | // sumBy(
|
---|
215 | // tableData.filter((item) => item.status === status),
|
---|
216 | // 'totalAmount'
|
---|
217 | // );
|
---|
218 |
|
---|
219 | useEffect(() => {
|
---|
220 | if (tableData) {
|
---|
221 | const getAnalytics = async () => {
|
---|
222 | const analyticsStats = await getTotalAmountForAllStatuses(tableData);
|
---|
223 | setAnalytics(analyticsStats);
|
---|
224 | };
|
---|
225 | getAnalytics();
|
---|
226 | }
|
---|
227 | }, [tableData]);
|
---|
228 |
|
---|
229 | const getPercentByStatus = (status: string) =>
|
---|
230 | (getInvoiceLength(status) / tableData.length) * 100;
|
---|
231 |
|
---|
232 | const TABS = [
|
---|
233 | { value: 'all', label: 'All', color: 'default', count: tableData.length },
|
---|
234 | {
|
---|
235 | value: 'paid',
|
---|
236 | label: 'Paid',
|
---|
237 | color: 'success',
|
---|
238 | count: getInvoiceLength('paid'),
|
---|
239 | },
|
---|
240 | {
|
---|
241 | value: 'pending',
|
---|
242 | label: 'Pending',
|
---|
243 | color: 'warning',
|
---|
244 | count: getInvoiceLength('pending'),
|
---|
245 | },
|
---|
246 | {
|
---|
247 | value: 'overdue',
|
---|
248 | label: 'Overdue',
|
---|
249 | color: 'error',
|
---|
250 | count: getInvoiceLength('overdue'),
|
---|
251 | },
|
---|
252 | {
|
---|
253 | value: 'draft',
|
---|
254 | label: 'Draft',
|
---|
255 | color: 'default',
|
---|
256 | count: getInvoiceLength('draft'),
|
---|
257 | },
|
---|
258 | ] as const;
|
---|
259 |
|
---|
260 | const handleFilters = useCallback(
|
---|
261 | (name: string, value: InvoiceTableFilterValue) => {
|
---|
262 | table.onResetPage();
|
---|
263 | setFilters((prevState) => ({
|
---|
264 | ...prevState,
|
---|
265 | [name]: value,
|
---|
266 | }));
|
---|
267 | },
|
---|
268 | [table]
|
---|
269 | );
|
---|
270 |
|
---|
271 | const handleDeleteRow = useCallback(
|
---|
272 | async (invoice: Invoice) => {
|
---|
273 | const serializedParams = JSON.stringify({
|
---|
274 | where: [['createDate', '>=', filters.startDate]],
|
---|
275 | orderBy: 'createDate',
|
---|
276 | direction: 'desc',
|
---|
277 | }); // Get the same params as used in useGetInvoices
|
---|
278 |
|
---|
279 | // Optimistically update the cache before the deletion
|
---|
280 | // mutate(
|
---|
281 | // [collections.invoice, serializedParams],
|
---|
282 | // (invoices: Invoice[] = []) => invoices.filter((row) => row.id !== invoice.id),
|
---|
283 | // false
|
---|
284 | // );
|
---|
285 |
|
---|
286 | await removeDocument(collections.invoice, invoice.id);
|
---|
287 | await deleteFromFirebaseStorage(
|
---|
288 | `invoices/${invoice.invoiceTo.name}/${invoice.id}-${invoice.invoiceNumber}.pdf`
|
---|
289 | );
|
---|
290 |
|
---|
291 | // Optionally, rollback optimistic update or refetch data
|
---|
292 | mutate(invoiceMutationKey);
|
---|
293 | },
|
---|
294 | [filters.startDate, invoiceMutationKey]
|
---|
295 | );
|
---|
296 |
|
---|
297 | const openCompose = useBoolean();
|
---|
298 |
|
---|
299 | const handleToggleCompose = useCallback(
|
---|
300 | async (invoice: Invoice) => {
|
---|
301 | setSendingInvoice(invoice);
|
---|
302 | openCompose.onToggle();
|
---|
303 | },
|
---|
304 | [openCompose]
|
---|
305 | );
|
---|
306 |
|
---|
307 | // const handleDeleteRows = useCallback(() => {
|
---|
308 | // const deleteRows = tableData.filter((row) => !table.selected.includes(row.id));
|
---|
309 | // // setTableData(deleteRows);
|
---|
310 |
|
---|
311 | // table.onUpdatePageDeleteRows({
|
---|
312 | // totalRows: tableData.length,
|
---|
313 | // totalRowsInPage: dataInPage.length,
|
---|
314 | // totalRowsFiltered: dataFiltered.length,
|
---|
315 | // });
|
---|
316 | // }, [dataFiltered.length, dataInPage.length, table, tableData]);
|
---|
317 |
|
---|
318 | const handleEditRow = useCallback(
|
---|
319 | (id: string) => {
|
---|
320 | router.push(paths.dashboard.invoice.edit(id));
|
---|
321 | },
|
---|
322 | [router]
|
---|
323 | );
|
---|
324 |
|
---|
325 | const handleCopyRow = useCallback(
|
---|
326 | (id: string) => {
|
---|
327 | router.push(paths.dashboard.invoice.copy(id));
|
---|
328 | },
|
---|
329 | [router]
|
---|
330 | );
|
---|
331 |
|
---|
332 | const handleViewRow = useCallback(
|
---|
333 | (id: string) => {
|
---|
334 | router.push(paths.dashboard.invoice.details(id));
|
---|
335 | },
|
---|
336 | [router]
|
---|
337 | );
|
---|
338 |
|
---|
339 | const handleFilterStatus = useCallback(
|
---|
340 | (event: React.SyntheticEvent, newValue: string) => {
|
---|
341 | handleFilters('status', newValue);
|
---|
342 | },
|
---|
343 | [handleFilters]
|
---|
344 | );
|
---|
345 |
|
---|
346 | const handleResetFilters = useCallback(() => {
|
---|
347 | setFilters(defaultFilters);
|
---|
348 | }, []);
|
---|
349 |
|
---|
350 | return (
|
---|
351 | <>
|
---|
352 | <Container maxWidth={settings.themeStretch ? false : 'lg'}>
|
---|
353 | <CustomBreadcrumbs
|
---|
354 | heading="List"
|
---|
355 | links={[
|
---|
356 | {
|
---|
357 | name: 'Dashboard',
|
---|
358 | href: paths.dashboard.root,
|
---|
359 | },
|
---|
360 | {
|
---|
361 | name: 'Invoice',
|
---|
362 | href: paths.dashboard.invoice.root,
|
---|
363 | },
|
---|
364 | {
|
---|
365 | name: 'List',
|
---|
366 | },
|
---|
367 | ]}
|
---|
368 | action={
|
---|
369 | <Button
|
---|
370 | component={RouterLink}
|
---|
371 | href={paths.dashboard.invoice.new}
|
---|
372 | variant="contained"
|
---|
373 | startIcon={<Iconify icon="mingcute:add-line" />}
|
---|
374 | >
|
---|
375 | New Invoice
|
---|
376 | </Button>
|
---|
377 | }
|
---|
378 | sx={{
|
---|
379 | mb: { xs: 3, md: 5 },
|
---|
380 | }}
|
---|
381 | />
|
---|
382 |
|
---|
383 | <Card
|
---|
384 | sx={{
|
---|
385 | mb: { xs: 3, md: 5 },
|
---|
386 | }}
|
---|
387 | >
|
---|
388 | <Scrollbar>
|
---|
389 | <Stack
|
---|
390 | direction="row"
|
---|
391 | divider={<Divider orientation="vertical" flexItem sx={{ borderStyle: 'dashed' }} />}
|
---|
392 | sx={{ py: 2 }}
|
---|
393 | >
|
---|
394 | <InvoiceAnalytic
|
---|
395 | title="Total"
|
---|
396 | total={tableData.length}
|
---|
397 | percent={100}
|
---|
398 | price={analytics.total}
|
---|
399 | icon="solar:bill-list-bold-duotone"
|
---|
400 | color={theme.palette.info.main}
|
---|
401 | />
|
---|
402 |
|
---|
403 | <InvoiceAnalytic
|
---|
404 | title="Paid"
|
---|
405 | total={getInvoiceLength('paid')}
|
---|
406 | percent={getPercentByStatus('paid')}
|
---|
407 | price={analytics.paid}
|
---|
408 | icon="solar:file-check-bold-duotone"
|
---|
409 | color={theme.palette.success.main}
|
---|
410 | />
|
---|
411 |
|
---|
412 | <InvoiceAnalytic
|
---|
413 | title="Pending"
|
---|
414 | total={getInvoiceLength('pending')}
|
---|
415 | percent={getPercentByStatus('pending')}
|
---|
416 | price={analytics.pending}
|
---|
417 | icon="solar:sort-by-time-bold-duotone"
|
---|
418 | color={theme.palette.warning.main}
|
---|
419 | />
|
---|
420 |
|
---|
421 | <InvoiceAnalytic
|
---|
422 | title="Overdue"
|
---|
423 | total={getInvoiceLength('overdue')}
|
---|
424 | percent={getPercentByStatus('overdue')}
|
---|
425 | price={analytics.overdue}
|
---|
426 | icon="solar:bell-bing-bold-duotone"
|
---|
427 | color={theme.palette.error.main}
|
---|
428 | />
|
---|
429 |
|
---|
430 | <InvoiceAnalytic
|
---|
431 | title="Draft"
|
---|
432 | total={getInvoiceLength('draft')}
|
---|
433 | percent={getPercentByStatus('draft')}
|
---|
434 | price={analytics.draft}
|
---|
435 | icon="solar:file-corrupted-bold-duotone"
|
---|
436 | color={theme.palette.text.secondary}
|
---|
437 | />
|
---|
438 | </Stack>
|
---|
439 | </Scrollbar>
|
---|
440 | </Card>
|
---|
441 |
|
---|
442 | <Card>
|
---|
443 | <Tabs
|
---|
444 | value={filters.status}
|
---|
445 | onChange={handleFilterStatus}
|
---|
446 | sx={{
|
---|
447 | px: 2.5,
|
---|
448 | boxShadow: `inset 0 -2px 0 0 ${alpha(theme.palette.grey[500], 0.08)}`,
|
---|
449 | }}
|
---|
450 | >
|
---|
451 | {TABS.map((tab) => (
|
---|
452 | <Tab
|
---|
453 | key={tab.value}
|
---|
454 | value={tab.value}
|
---|
455 | label={tab.label}
|
---|
456 | iconPosition="end"
|
---|
457 | icon={
|
---|
458 | <Label
|
---|
459 | variant={
|
---|
460 | ((tab.value === 'all' || tab.value === filters.status) && 'filled') || 'soft'
|
---|
461 | }
|
---|
462 | color={tab.color}
|
---|
463 | >
|
---|
464 | {tab.count}
|
---|
465 | </Label>
|
---|
466 | }
|
---|
467 | />
|
---|
468 | ))}
|
---|
469 | </Tabs>
|
---|
470 |
|
---|
471 | <InvoiceTableToolbar
|
---|
472 | filters={filters}
|
---|
473 | onFilters={handleFilters}
|
---|
474 | //
|
---|
475 | dateError={dateError}
|
---|
476 | serviceOptions={[]}
|
---|
477 | />
|
---|
478 |
|
---|
479 | {canReset && (
|
---|
480 | <InvoiceTableFiltersResult
|
---|
481 | filters={filters}
|
---|
482 | onFilters={handleFilters}
|
---|
483 | //
|
---|
484 | onResetFilters={handleResetFilters}
|
---|
485 | //
|
---|
486 | results={dataFiltered.length}
|
---|
487 | sx={{ p: 2.5, pt: 0 }}
|
---|
488 | />
|
---|
489 | )}
|
---|
490 |
|
---|
491 | <TableContainer sx={{ position: 'relative', overflow: 'unset' }}>
|
---|
492 | {/* <TableSelectedAction
|
---|
493 | dense={table.dense}
|
---|
494 | numSelected={table.selected.length}
|
---|
495 | rowCount={tableData.length}
|
---|
496 | onSelectAllRows={(checked) =>
|
---|
497 | table.onSelectAllRows(
|
---|
498 | checked,
|
---|
499 | tableData.map((row) => row.id)
|
---|
500 | )
|
---|
501 | }
|
---|
502 | action={
|
---|
503 | <Stack direction="row">
|
---|
504 | <Tooltip title="Sent">
|
---|
505 | <IconButton color="primary">
|
---|
506 | <Iconify icon="iconamoon:send-fill" />
|
---|
507 | </IconButton>
|
---|
508 | </Tooltip>
|
---|
509 |
|
---|
510 | <Tooltip title="Download">
|
---|
511 | <IconButton color="primary">
|
---|
512 | <Iconify icon="eva:download-outline" />
|
---|
513 | </IconButton>
|
---|
514 | </Tooltip>
|
---|
515 |
|
---|
516 | <Tooltip title="Print">
|
---|
517 | <IconButton color="primary">
|
---|
518 | <Iconify icon="solar:printer-minimalistic-bold" />
|
---|
519 | </IconButton>
|
---|
520 | </Tooltip>
|
---|
521 |
|
---|
522 | <Tooltip title="Delete">
|
---|
523 | <IconButton color="primary" onClick={confirm.onTrue}>
|
---|
524 | <Iconify icon="solar:trash-bin-trash-bold" />
|
---|
525 | </IconButton>
|
---|
526 | </Tooltip>
|
---|
527 | </Stack>
|
---|
528 | }
|
---|
529 | /> */}
|
---|
530 |
|
---|
531 | <Scrollbar>
|
---|
532 | <Table size={table.dense ? 'small' : 'medium'} sx={{ minWidth: 800 }}>
|
---|
533 | <TableHeadCustom
|
---|
534 | order={table.order}
|
---|
535 | orderBy={table.orderBy}
|
---|
536 | headLabel={TABLE_HEAD}
|
---|
537 | rowCount={tableData.length}
|
---|
538 | numSelected={table.selected.length}
|
---|
539 | onSort={table.onSort}
|
---|
540 | onSelectAllRows={(checked) =>
|
---|
541 | table.onSelectAllRows(
|
---|
542 | checked,
|
---|
543 | tableData.map((row) => row.id)
|
---|
544 | )
|
---|
545 | }
|
---|
546 | />
|
---|
547 |
|
---|
548 | <TableBody>
|
---|
549 | {dataFiltered
|
---|
550 | .slice(
|
---|
551 | table.page * table.rowsPerPage,
|
---|
552 | table.page * table.rowsPerPage + table.rowsPerPage
|
---|
553 | )
|
---|
554 | .map((row) => (
|
---|
555 | <InvoiceTableRow
|
---|
556 | key={row.id}
|
---|
557 | row={row}
|
---|
558 | selected={table.selected.includes(row.id)}
|
---|
559 | onSelectRow={() => table.onSelectRow(row.id)}
|
---|
560 | onViewRow={() => handleViewRow(row.id)}
|
---|
561 | onEditRow={() => handleEditRow(row.id)}
|
---|
562 | onCopyRow={() => handleCopyRow(row.id)}
|
---|
563 | onDeleteRow={async () => handleDeleteRow(row)}
|
---|
564 | onSendRow={() => {}}
|
---|
565 | onToggleCompose={() => handleToggleCompose(row)}
|
---|
566 | />
|
---|
567 | ))}
|
---|
568 |
|
---|
569 | <TableEmptyRows
|
---|
570 | height={denseHeight}
|
---|
571 | emptyRows={emptyRows(table.page, table.rowsPerPage, tableData.length)}
|
---|
572 | />
|
---|
573 |
|
---|
574 | <TableNoData notFound={notFound} />
|
---|
575 | </TableBody>
|
---|
576 | </Table>
|
---|
577 | </Scrollbar>
|
---|
578 | </TableContainer>
|
---|
579 |
|
---|
580 | <TablePaginationCustom
|
---|
581 | count={dataFiltered.length}
|
---|
582 | page={table.page}
|
---|
583 | rowsPerPage={table.rowsPerPage}
|
---|
584 | onPageChange={table.onChangePage}
|
---|
585 | onRowsPerPageChange={table.onChangeRowsPerPage}
|
---|
586 | //
|
---|
587 | dense={table.dense}
|
---|
588 | onChangeDense={table.onChangeDense}
|
---|
589 | />
|
---|
590 | </Card>
|
---|
591 | </Container>
|
---|
592 |
|
---|
593 | {openCompose.value && (
|
---|
594 | <MailCompose
|
---|
595 | invoice={sendingInvoice}
|
---|
596 | onCloseCompose={openCompose.onFalse}
|
---|
597 | invoiceMutationKey={invoiceMutationKey}
|
---|
598 | />
|
---|
599 | )}
|
---|
600 |
|
---|
601 | {/* <ConfirmDialog
|
---|
602 | open={confirm.value}
|
---|
603 | onClose={confirm.onFalse}
|
---|
604 | title="Delete"
|
---|
605 | content={
|
---|
606 | <>
|
---|
607 | Are you sure want to delete <strong> {table.selected.length} </strong> items?
|
---|
608 | </>
|
---|
609 | }
|
---|
610 | action={
|
---|
611 | <Button
|
---|
612 | variant="contained"
|
---|
613 | color="error"
|
---|
614 | onClick={() => {
|
---|
615 | handleDeleteRows();
|
---|
616 | confirm.onFalse();
|
---|
617 | }}
|
---|
618 | >
|
---|
619 | Delete
|
---|
620 | </Button>
|
---|
621 | }
|
---|
622 | /> */}
|
---|
623 | </>
|
---|
624 | );
|
---|
625 | }
|
---|
626 |
|
---|
627 | // ----------------------------------------------------------------------
|
---|
628 |
|
---|
629 | function applyFilter({
|
---|
630 | inputData,
|
---|
631 | comparator,
|
---|
632 | filters,
|
---|
633 | dateError,
|
---|
634 | }: {
|
---|
635 | inputData: Invoice[];
|
---|
636 | comparator: (a: any, b: any) => number;
|
---|
637 | filters: InvoiceTableFilters;
|
---|
638 | dateError: boolean;
|
---|
639 | }) {
|
---|
640 | const { name, status, service, startDate, endDate } = filters;
|
---|
641 |
|
---|
642 | const stabilizedThis = inputData.map((el, index) => [el, index] as const);
|
---|
643 |
|
---|
644 | stabilizedThis.sort((a, b) => {
|
---|
645 | const order = comparator(a[0], b[0]);
|
---|
646 | if (order !== 0) return order;
|
---|
647 | return a[1] - b[1];
|
---|
648 | });
|
---|
649 |
|
---|
650 | inputData = stabilizedThis.map((el) => el[0]);
|
---|
651 |
|
---|
652 | if (name) {
|
---|
653 | inputData = inputData.filter(
|
---|
654 | (invoice) =>
|
---|
655 | invoice.invoiceNumber.toLowerCase().indexOf(name.toLowerCase()) !== -1 ||
|
---|
656 | invoice.invoiceTo.name.toLowerCase().indexOf(name.toLowerCase()) !== -1
|
---|
657 | );
|
---|
658 | }
|
---|
659 |
|
---|
660 | if (status !== 'all') {
|
---|
661 | inputData = inputData.filter((invoice) => invoice.status === status);
|
---|
662 | }
|
---|
663 |
|
---|
664 | if (service.length) {
|
---|
665 | inputData = inputData.filter((invoice) =>
|
---|
666 | invoice.items.some((filterItem) => service.includes(filterItem.service.id))
|
---|
667 | );
|
---|
668 | }
|
---|
669 |
|
---|
670 | if (!dateError) {
|
---|
671 | if (startDate && endDate) {
|
---|
672 | inputData = inputData.filter(
|
---|
673 | (invoice) =>
|
---|
674 | fTimestamp(invoice.createDate.toMillis()) >= fTimestamp(startDate) &&
|
---|
675 | fTimestamp(invoice.createDate.toMillis()) <= fTimestamp(endDate)
|
---|
676 | );
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | return inputData;
|
---|
681 | }
|
---|