import { useCallback } from 'react'; // @mui import { styled } from '@mui/material/styles'; import Box from '@mui/material/Box'; import Card from '@mui/material/Card'; import Table from '@mui/material/Table'; import Stack from '@mui/material/Stack'; import Divider from '@mui/material/Divider'; import TableRow from '@mui/material/TableRow'; import TableHead from '@mui/material/TableHead'; import TableCell from '@mui/material/TableCell'; import TableBody from '@mui/material/TableBody'; import Grid from '@mui/material/Unstable_Grid2'; import Typography from '@mui/material/Typography'; import TableContainer from '@mui/material/TableContainer'; // utils import { fDate } from 'src/utils/format-time'; import { fCurrency } from 'src/utils/format-number'; // types import { Invoice } from 'src/schemas'; // components import Label from 'src/components/label'; import Scrollbar from 'src/components/scrollbar'; // import { mutate } from 'swr'; import { collections, updateDocument } from 'src/lib/firestore'; import { createFullAddress } from 'src/utils/create-full-address'; import { getQuantityType } from 'src/utils/get-invoice-quantity-type'; import InvoiceToolbar from './invoice-toolbar'; import { updateInvoice } from 'src/api/invoice'; // ---------------------------------------------------------------------- const StyledTableRow = styled(TableRow)(({ theme }) => ({ '& td': { textAlign: 'right', borderBottom: 'none', paddingTop: theme.spacing(1), paddingBottom: theme.spacing(1), }, })); // ---------------------------------------------------------------------- type Props = { invoice: Invoice; }; export const INVOICE_STATUS_OPTIONS = [ { value: 'draft', label: 'Draft' }, // { value: 'processing', label: 'Processing' }, { value: 'paid', label: 'Paid' }, { value: 'pending', label: 'Pending' }, { value: 'overdue', label: 'Overdue' }, ]; export default function InvoiceDetails({ invoice }: Props) { const currentStatus = invoice.status; const handleChangeStatus = useCallback( async (event: React.ChangeEvent) => { // await updateDocument(collections.invoice, invoice.id, { status: event.target.value }); await updateInvoice(invoice.id, { status: event.target.value as 'draft' | 'processing' | 'pending' | 'overdue' | 'paid', }); mutate([collections.invoice, invoice.id]); }, [invoice] ); const renderTotal = ( <> Subtotal {fCurrency(invoice.subTotal, invoice.currency)} {!!invoice.discount && ( Discount {fCurrency(-invoice.discount, invoice.currency)} )} {/* Taxes {fCurrency(invoice.taxes)} */} Total {fCurrency(invoice.totalAmount, invoice.currency)} ); const renderFooter = ( NOTES VAT is not calculated according to article 14, paragraph 3, point 5 from the VAT act. Have a Question? {invoice.invoiceFrom.email} ); const renderList = ( # Description {getQuantityType(invoice)} Currency Unit price Total {invoice.items.map((row, index) => ( {index + 1} {row.title} {row.description} {row.quantity} {invoice.currency} {fCurrency(row.price, invoice.currency)} {fCurrency(row.price * row.quantity, invoice.currency)} ))} {renderTotal}
); return ( <> {invoice.invoiceNumber} Invoice From {invoice.invoiceFrom.name}
{createFullAddress(invoice.invoiceFrom.address)}

{invoice.invoiceFrom.email}
Invoice To {invoice.invoiceTo.name}
{!!invoice.invoiceTo.companyNumber && ( <> Company ID: {invoice.invoiceTo.companyNumber}
)} {createFullAddress(invoice.invoiceTo.address)}

{invoice.invoiceTo.email}
Date Issued {fDate(invoice.issueDate)} Due Date {fDate(invoice.dueDate)}
{renderList} {renderFooter}
); }