2023-01-16 09:45:12 +00:00
|
|
|
import Skeleton from "@dashboard/components/Skeleton";
|
|
|
|
import TableCellAvatar from "@dashboard/components/TableCellAvatar";
|
|
|
|
import TableRowLink from "@dashboard/components/TableRowLink";
|
|
|
|
import { OrderFulfillLineFragment } from "@dashboard/graphql";
|
|
|
|
import { FormsetChange, FormsetData } from "@dashboard/hooks/useFormset";
|
|
|
|
import {
|
|
|
|
getAttributesCaption,
|
|
|
|
getOrderLineAvailableQuantity,
|
|
|
|
getWarehouseStock,
|
|
|
|
OrderFulfillLineFormData,
|
|
|
|
} from "@dashboard/orders/utils/data";
|
2022-10-27 10:58:17 +00:00
|
|
|
import { TableCell, TextField, Typography } from "@material-ui/core";
|
2023-04-12 13:55:42 +00:00
|
|
|
import { ChevronIcon, IconButton, WarningIcon } from "@saleor/macaw-ui";
|
|
|
|
import { Box, Tooltip } from "@saleor/macaw-ui/next";
|
2022-12-02 10:45:19 +00:00
|
|
|
import clsx from "clsx";
|
2022-04-29 09:16:58 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import { messages } from "./messages";
|
|
|
|
import { useStyles } from "./styles";
|
|
|
|
|
|
|
|
interface OrderFulfillLineProps {
|
|
|
|
line: OrderFulfillLineFragment;
|
|
|
|
lineIndex: number;
|
2022-08-02 12:57:18 +00:00
|
|
|
formsetData: FormsetData<null, OrderFulfillLineFormData[]>;
|
|
|
|
formsetChange: FormsetChange<OrderFulfillLineFormData[]>;
|
|
|
|
onWarehouseChange: () => void;
|
2022-04-29 09:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const OrderFulfillLine: React.FC<OrderFulfillLineProps> = props => {
|
2023-04-12 13:55:42 +00:00
|
|
|
const { line, lineIndex, formsetData, formsetChange, onWarehouseChange } =
|
|
|
|
props;
|
2022-04-29 09:16:58 +00:00
|
|
|
const classes = useStyles();
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const isDeletedVariant = !line?.variant;
|
|
|
|
const isPreorder = !!line.variant?.preorder;
|
|
|
|
const lineFormQuantity = isPreorder
|
|
|
|
? 0
|
2022-05-30 09:42:29 +00:00
|
|
|
: formsetData[lineIndex]?.value?.[0]?.quantity;
|
2022-08-02 12:57:18 +00:00
|
|
|
const lineFormWarehouse = formsetData[lineIndex]?.value?.[0]?.warehouse;
|
2022-04-29 09:16:58 +00:00
|
|
|
|
|
|
|
const overfulfill = lineFormQuantity > line.quantityToFulfill;
|
2022-08-02 12:57:18 +00:00
|
|
|
|
|
|
|
const warehouseStock = getWarehouseStock(
|
|
|
|
line?.variant?.stocks,
|
|
|
|
lineFormWarehouse?.id,
|
|
|
|
);
|
2022-04-29 09:16:58 +00:00
|
|
|
const availableQuantity = getOrderLineAvailableQuantity(line, warehouseStock);
|
|
|
|
|
|
|
|
const isStockExceeded = lineFormQuantity > availableQuantity;
|
|
|
|
|
2022-09-07 14:08:41 +00:00
|
|
|
if (!line) {
|
2022-04-29 09:16:58 +00:00
|
|
|
return (
|
2022-10-27 10:58:17 +00:00
|
|
|
<TableRowLink key={lineIndex}>
|
2022-04-29 09:16:58 +00:00
|
|
|
<TableCellAvatar className={classes.colName}>
|
|
|
|
<Skeleton />
|
|
|
|
</TableCellAvatar>
|
|
|
|
<TableCell className={classes.colSku}>
|
|
|
|
<Skeleton />
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colQuantity}>
|
|
|
|
<Skeleton />
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colStock}>
|
|
|
|
<Skeleton />
|
|
|
|
</TableCell>
|
2022-08-02 12:57:18 +00:00
|
|
|
<TableCell className={classes.colWarehouse}>
|
|
|
|
<Skeleton />
|
|
|
|
</TableCell>
|
2022-10-27 10:58:17 +00:00
|
|
|
</TableRowLink>
|
2022-04-29 09:16:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-10-27 10:58:17 +00:00
|
|
|
<TableRowLink key={line.id}>
|
2022-04-29 09:16:58 +00:00
|
|
|
<TableCellAvatar
|
|
|
|
className={classes.colName}
|
|
|
|
thumbnail={line?.thumbnail?.url}
|
|
|
|
badge={
|
|
|
|
isPreorder || !line?.variant ? (
|
2023-04-12 13:55:42 +00:00
|
|
|
<Tooltip>
|
|
|
|
<Tooltip.Trigger>
|
|
|
|
<div className={classes.warningIcon}>
|
|
|
|
<WarningIcon />
|
|
|
|
</div>
|
|
|
|
</Tooltip.Trigger>
|
|
|
|
<Tooltip.Content side="bottom">
|
|
|
|
<Tooltip.Arrow />
|
|
|
|
<Box __maxWidth={350}>
|
|
|
|
{intl.formatMessage(
|
|
|
|
isPreorder
|
|
|
|
? messages.preorderWarning
|
|
|
|
: messages.deletedVariantWarning,
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Tooltip.Content>
|
2022-04-29 09:16:58 +00:00
|
|
|
</Tooltip>
|
2023-04-12 13:55:42 +00:00
|
|
|
) : undefined
|
2022-04-29 09:16:58 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{line.productName}
|
|
|
|
<Typography color="textSecondary" variant="caption">
|
|
|
|
{getAttributesCaption(line.variant?.attributes)}
|
|
|
|
</Typography>
|
|
|
|
</TableCellAvatar>
|
|
|
|
<TableCell className={classes.colSku}>{line.variant?.sku}</TableCell>
|
|
|
|
{isPreorder ? (
|
|
|
|
<TableCell className={classes.colQuantity} />
|
|
|
|
) : (
|
|
|
|
<TableCell
|
|
|
|
className={classes.colQuantity}
|
|
|
|
key={warehouseStock?.id ?? "deletedVariant" + lineIndex}
|
|
|
|
>
|
|
|
|
<TextField
|
|
|
|
type="number"
|
|
|
|
inputProps={{
|
2022-12-02 10:45:19 +00:00
|
|
|
className: clsx(classes.quantityInnerInput, {
|
2023-04-12 13:55:42 +00:00
|
|
|
[classes.quantityInnerInputNoRemaining]:
|
|
|
|
!line.variant?.trackInventory,
|
2022-04-29 09:16:58 +00:00
|
|
|
}),
|
|
|
|
min: 0,
|
2022-06-21 09:36:55 +00:00
|
|
|
style: { textAlign: "right" },
|
2022-04-29 09:16:58 +00:00
|
|
|
}}
|
|
|
|
fullWidth
|
|
|
|
value={lineFormQuantity}
|
|
|
|
onChange={event =>
|
|
|
|
formsetChange(line.id, [
|
|
|
|
{
|
|
|
|
quantity: parseInt(event.target.value, 10),
|
2022-08-02 12:57:18 +00:00
|
|
|
warehouse: lineFormWarehouse,
|
2022-06-21 09:36:55 +00:00
|
|
|
},
|
2022-04-29 09:16:58 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
error={overfulfill}
|
|
|
|
variant="outlined"
|
|
|
|
InputProps={{
|
|
|
|
classes: {
|
|
|
|
...(isStockExceeded &&
|
|
|
|
!overfulfill && {
|
2022-06-21 09:36:55 +00:00
|
|
|
notchedOutline: classes.warning,
|
|
|
|
}),
|
2022-04-29 09:16:58 +00:00
|
|
|
},
|
|
|
|
endAdornment: (
|
|
|
|
<div className={classes.remainingQuantity}>
|
|
|
|
/ {line.quantityToFulfill}
|
|
|
|
</div>
|
2022-06-21 09:36:55 +00:00
|
|
|
),
|
2022-04-29 09:16:58 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
)}
|
|
|
|
<TableCell className={classes.colStock} key="total">
|
2022-09-07 14:08:41 +00:00
|
|
|
{lineFormWarehouse
|
|
|
|
? isPreorder || isDeletedVariant
|
|
|
|
? undefined
|
|
|
|
: availableQuantity
|
|
|
|
: "-"}
|
2022-04-29 09:16:58 +00:00
|
|
|
</TableCell>
|
2022-08-02 12:57:18 +00:00
|
|
|
<TableCell className={classes.colWarehouse}>
|
2022-11-25 13:59:11 +00:00
|
|
|
{isPreorder ? (
|
|
|
|
"-"
|
|
|
|
) : (
|
|
|
|
<IconButton
|
|
|
|
onClick={onWarehouseChange}
|
2022-12-02 10:45:19 +00:00
|
|
|
className={clsx(
|
2022-11-25 13:59:11 +00:00
|
|
|
classes.warehouseButton,
|
|
|
|
"MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl MuiInputBase-adornedEnd MuiOutlinedInput-adornedEnd",
|
|
|
|
)}
|
|
|
|
data-test-id="select-warehouse-button"
|
|
|
|
>
|
|
|
|
<div className={classes.warehouseButtonContent}>
|
|
|
|
<Typography className={classes.warehouseButtonContentText}>
|
|
|
|
{lineFormWarehouse?.name ??
|
|
|
|
intl.formatMessage(messages.selectWarehouse)}
|
|
|
|
</Typography>
|
|
|
|
<ChevronIcon />
|
|
|
|
</div>
|
|
|
|
</IconButton>
|
|
|
|
)}
|
2022-08-02 12:57:18 +00:00
|
|
|
</TableCell>
|
2022-10-27 10:58:17 +00:00
|
|
|
</TableRowLink>
|
2022-04-29 09:16:58 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
OrderFulfillLine.displayName = "OrderFulfillLine";
|
|
|
|
export default OrderFulfillLine;
|