[5d6f37a] | 1 | import { useFormContext } from 'react-hook-form';
|
---|
| 2 | // @mui
|
---|
| 3 | import Stack from '@mui/material/Stack';
|
---|
| 4 | import Button from '@mui/material/Button';
|
---|
| 5 | import Divider from '@mui/material/Divider';
|
---|
| 6 | import IconButton from '@mui/material/IconButton';
|
---|
| 7 | import Typography from '@mui/material/Typography';
|
---|
| 8 | // hooks
|
---|
[057453c] | 9 | import { useGetTenant } from 'src/api/tenant';
|
---|
[5d6f37a] | 10 | import { useBoolean } from 'src/hooks/use-boolean';
|
---|
| 11 | import { useResponsive } from 'src/hooks/use-responsive';
|
---|
| 12 | // components
|
---|
| 13 | import Iconify from 'src/components/iconify';
|
---|
| 14 | //
|
---|
| 15 | import { createFullAddress } from 'src/utils/create-full-address';
|
---|
| 16 | import { useGetCustomers } from 'src/api/customer';
|
---|
| 17 | import { CompanyListDialog } from '../company';
|
---|
| 18 |
|
---|
| 19 | // ----------------------------------------------------------------------
|
---|
| 20 |
|
---|
| 21 | export default function InvoiceNewEditAddress() {
|
---|
| 22 | const {
|
---|
| 23 | watch,
|
---|
| 24 | setValue,
|
---|
| 25 | formState: { errors },
|
---|
| 26 | } = useFormContext();
|
---|
| 27 |
|
---|
| 28 | const mdUp = useResponsive('up', 'md');
|
---|
| 29 |
|
---|
| 30 | const values = watch();
|
---|
| 31 |
|
---|
| 32 | const { invoiceFrom, invoiceTo } = values;
|
---|
| 33 |
|
---|
[057453c] | 34 | const { settings: tenant } = useGetTenant();
|
---|
[5d6f37a] | 35 | const { customers } = useGetCustomers();
|
---|
| 36 |
|
---|
| 37 | const from = useBoolean();
|
---|
| 38 | const to = useBoolean();
|
---|
| 39 |
|
---|
| 40 | return (
|
---|
| 41 | <>
|
---|
| 42 | <Stack
|
---|
| 43 | spacing={{ xs: 3, md: 5 }}
|
---|
| 44 | direction={{ xs: 'column', md: 'row' }}
|
---|
| 45 | divider={
|
---|
| 46 | <Divider
|
---|
| 47 | flexItem
|
---|
| 48 | orientation={mdUp ? 'vertical' : 'horizontal'}
|
---|
| 49 | sx={{ borderStyle: 'dashed' }}
|
---|
| 50 | />
|
---|
| 51 | }
|
---|
| 52 | sx={{ p: 3 }}
|
---|
| 53 | >
|
---|
| 54 | <Stack sx={{ width: 1 }}>
|
---|
| 55 | <Stack direction="row" alignItems="center" sx={{ mb: 1 }}>
|
---|
| 56 | <Typography variant="h6" sx={{ color: 'text.disabled', flexGrow: 1 }}>
|
---|
| 57 | From:
|
---|
| 58 | </Typography>
|
---|
| 59 | <IconButton onClick={from.onTrue}>
|
---|
| 60 | <Iconify icon="solar:pen-bold" />
|
---|
| 61 | </IconButton>
|
---|
| 62 | </Stack>
|
---|
| 63 |
|
---|
| 64 | {invoiceFrom ? (
|
---|
| 65 | <Stack spacing={1}>
|
---|
| 66 | <Typography variant="subtitle2">{invoiceFrom.name}</Typography>
|
---|
| 67 | <Typography variant="body2">{createFullAddress(invoiceFrom.address)}</Typography>
|
---|
| 68 | <Typography variant="body2"> {invoiceFrom.phoneNumber}</Typography>
|
---|
| 69 | </Stack>
|
---|
| 70 | ) : (
|
---|
| 71 | <Typography typography="caption" sx={{ color: 'error.main' }}>
|
---|
| 72 | {(errors.invoiceFrom as any)?.message}
|
---|
| 73 | </Typography>
|
---|
| 74 | )}
|
---|
| 75 | </Stack>
|
---|
| 76 |
|
---|
| 77 | <Stack sx={{ width: 1 }}>
|
---|
| 78 | <Stack direction="row" alignItems="center" sx={{ mb: 1 }}>
|
---|
| 79 | <Typography variant="h6" sx={{ color: 'text.disabled', flexGrow: 1 }}>
|
---|
| 80 | To:
|
---|
| 81 | </Typography>
|
---|
| 82 |
|
---|
| 83 | <IconButton onClick={to.onTrue}>
|
---|
| 84 | <Iconify icon={invoiceTo ? 'solar:pen-bold' : 'mingcute:add-line'} />
|
---|
| 85 | </IconButton>
|
---|
| 86 | </Stack>
|
---|
| 87 |
|
---|
| 88 | {invoiceTo ? (
|
---|
| 89 | <Stack spacing={1}>
|
---|
| 90 | <Typography variant="subtitle2">{invoiceTo.name}</Typography>
|
---|
| 91 | <Typography variant="body2">{createFullAddress(invoiceTo.address)}</Typography>
|
---|
| 92 | <Typography variant="body2"> {invoiceTo.phoneNumber}</Typography>
|
---|
| 93 | </Stack>
|
---|
| 94 | ) : (
|
---|
| 95 | <Typography typography="caption" sx={{ color: 'error.main' }}>
|
---|
| 96 | {(errors.invoiceTo as any)?.message}
|
---|
| 97 | </Typography>
|
---|
| 98 | )}
|
---|
| 99 | </Stack>
|
---|
| 100 | </Stack>
|
---|
| 101 |
|
---|
[057453c] | 102 | {tenant && (
|
---|
[5d6f37a] | 103 | <CompanyListDialog
|
---|
| 104 | title="Companies"
|
---|
| 105 | open={from.value}
|
---|
| 106 | onClose={from.onFalse}
|
---|
| 107 | selected={(selectedId: string) => invoiceFrom?.id === selectedId}
|
---|
[057453c] | 108 | onSelect={(tenant) => setValue('invoiceFrom', tenant)}
|
---|
| 109 | tenant={tenant}
|
---|
[5d6f37a] | 110 | action={
|
---|
| 111 | <Button
|
---|
| 112 | size="small"
|
---|
| 113 | startIcon={<Iconify icon="mingcute:add-line" />}
|
---|
| 114 | sx={{ alignSelf: 'flex-end' }}
|
---|
| 115 | >
|
---|
| 116 | New
|
---|
| 117 | </Button>
|
---|
| 118 | }
|
---|
| 119 | />
|
---|
| 120 | )}
|
---|
| 121 |
|
---|
| 122 | <CompanyListDialog
|
---|
| 123 | title="Customers"
|
---|
| 124 | open={to.value}
|
---|
| 125 | onClose={to.onFalse}
|
---|
| 126 | selected={(selectedId: string) => invoiceTo?.id === selectedId}
|
---|
| 127 | onSelect={(company) => setValue('invoiceTo', company)}
|
---|
| 128 | list={customers}
|
---|
| 129 | action={
|
---|
| 130 | <Button
|
---|
| 131 | size="small"
|
---|
| 132 | startIcon={<Iconify icon="mingcute:add-line" />}
|
---|
| 133 | sx={{ alignSelf: 'flex-end' }}
|
---|
| 134 | >
|
---|
| 135 | New
|
---|
| 136 | </Button>
|
---|
| 137 | }
|
---|
| 138 | />
|
---|
| 139 | </>
|
---|
| 140 | );
|
---|
| 141 | }
|
---|