Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 3x 13x 13x 13x 13x 13x 13x 11x 13x 11x 1x 1x 13x 6x 6x 6x 13x 13x 1x 22x 172x 172x 172x 2x 16x | import React, { useId, useMemo } from 'react';
import { ArrowLeft, ArrowRight, Edit, TrashCan } from '@carbon/react/icons';
import {
Button,
DataTable,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableHeader,
TableRow,
} from '@carbon/react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { showSnackbar } from '@openmrs/esm-framework';
import { type StockOperationDTO } from '../../../core/api/types/stockOperation/StockOperationDTO';
import { type StockOperationType } from '../../../core/api/types/stockOperation/StockOperationType';
import { getStockOperationUniqueId } from '../../stock-operation.utils';
import { type BaseStockOperationItemFormData, type StockOperationItemDtoSchema } from '../../validation-schema';
import useOperationTypePermisions from '../hooks/useOperationTypePermisions';
import StockItemSearch from '../input-components/stock-item-search.component';
import QuantityUomCell from './quantity-uom-cell.component';
import StockAvailability from './stock-availability-cell.component';
import StockOperationItemBatchNoCell from './stock-operation-item-batch-no-cell.component';
import StockOperationItemCell from './stock-operation-item-cell.component';
import StockoperationItemExpiryCell from './stock-operation-item-expiry-cell.component';
import { type CustomTableHeader } from '../../../core/components/table/types';
import styles from './stock-operation-items-form-step.scc.scss';
type StockOperationItemsFormStepProps = {
stockOperation?: StockOperationDTO;
stockOperationType: StockOperationType;
onNext?: () => void;
onPrevious?: () => void;
onLaunchItemsForm?: (stockOperationItem?: BaseStockOperationItemFormData) => void;
};
const StockOperationItemsFormStep: React.FC<StockOperationItemsFormStepProps> = ({
stockOperationType,
stockOperation,
onNext,
onPrevious,
onLaunchItemsForm,
}) => {
const { t } = useTranslation();
const operationTypePermision = useOperationTypePermisions(stockOperationType);
const uniqueId = useId();
const form = useFormContext<StockOperationItemDtoSchema>();
const observableOperationItems = form.watch('stockOperationItems');
const headers = useMemo(() => {
return [
{
key: 'item',
header: t('item', 'Item'),
styles: { width: '40% !important' },
},
{
key: 'itemDetails',
header: t('itemDetails', 'Item Details'),
styles: { width: '20% !important' },
},
...(operationTypePermision.requiresBatchUuid || operationTypePermision.requiresActualBatchInfo
? [
{
key: 'batch',
header: t('batchNo', 'Batch number'),
styles: { width: '15% !important' },
},
]
: []),
...(operationTypePermision.requiresActualBatchInfo
? [
{
key: 'expiry',
header: t('expiry', 'Expiry'),
},
]
: []),
...(operationTypePermision.requiresBatchUuid
? [
{
key: 'expiry',
header: t('expiry', 'Expiry'),
},
]
: []),
{
key: 'quantity',
header: t('quantity', 'Quantity'),
},
{
key: 'quantityuom',
header: t('quantityUom', 'Quantity unit of measurement'),
},
...(operationTypePermision.canCaptureQuantityPrice
? [
{
key: 'purchasePrice',
header: t('purchasePrice', 'Purchase Price'),
},
]
: []),
{ key: 'actions', header: t('actions', 'Actions') },
];
}, [operationTypePermision, t]);
const tableRows = useMemo(() => {
return observableOperationItems?.map((item, index) => {
const {
batchNo,
expiration,
quantity,
purchasePrice,
uuid,
stockItemUuid,
stockItemPackagingUOMUuid,
stockBatchUuid,
} = item;
return {
id: uuid || `${uniqueId}-${index}`,
item: stockItemUuid ? <StockOperationItemCell stockItemUuid={stockItemUuid} /> : '--',
itemDetails: stockItemUuid ? <StockAvailability stockItemUuid={stockItemUuid} /> : '--',
batch: (
<StockOperationItemBatchNoCell
operation={stockOperationType}
stockBatchUuid={stockBatchUuid}
batchNo={batchNo}
stockItemUuid={stockItemUuid}
/>
),
expiry: (
<StockoperationItemExpiryCell
operation={stockOperationType}
stockBatchUuid={stockBatchUuid}
expiration={expiration}
stockItemUuid={stockItemUuid}
/>
),
quantity: quantity?.toLocaleString(),
quantityuom: stockItemPackagingUOMUuid ? (
<QuantityUomCell stockItemPackagingUOMUuid={stockItemPackagingUOMUuid} stockItemUuid={stockItemUuid} />
) : (
'--'
),
purchasePrice: purchasePrice,
actions: (
<>
<Button
type="button"
size="sm"
className="submitButton clear-padding-margin"
iconDescription={t('edit', 'Edit')}
kind="ghost"
renderIcon={Edit}
onClick={() => {
onLaunchItemsForm?.(item);
}}
/>
<Button
type="button"
size="sm"
className="submitButton clear-padding-margin"
iconDescription={t('delete', 'Delete')}
kind="ghost"
renderIcon={TrashCan}
onClick={() => {
onLaunchItemsForm?.(item);
}}
/>
</>
),
};
});
}, [observableOperationItems, onLaunchItemsForm, stockOperationType, t, uniqueId]);
const handleNext = async () => {
const valid = await form.trigger(['stockOperationItems']);
if (valid) {
onNext();
} else E{
showSnackbar({
kind: 'error',
title: 'Validation error',
subtitle:
observableOperationItems && observableOperationItems.length > 0
? 'You must update batch infomation for items'
: 'You must add atleast one item',
});
}
};
const headerTitle = t('stockOperationItems', 'Stock operation items');
return (
<div style={{ margin: '10px' }}>
<div className={styles.tableContainer}>
<div className={styles.heading}>
<h4>{headerTitle}</h4>
</div>
<StockItemSearch
onSelectedItem={(stockItem) =>
onLaunchItemsForm({
uuid: `new-item-${getStockOperationUniqueId()}`,
stockItemUuid: stockItem.uuid,
hasExpiration: stockItem.hasExpiration,
purchasePrice: stockItem.purchasePrice,
})
}
/>
<DataTable rows={tableRows ?? []} headers={headers} isSortable={false} useZebraStyles>
{({ rows, headers, getTableProps, getHeaderProps, getRowProps }) => (
<TableContainer>
<Table {...getTableProps()}>
<TableHead>
<TableRow>
{headers.map((header) => (
<TableHeader
{...getHeaderProps({
header,
isSortable: false,
})}
style={(header as CustomTableHeader)?.styles}
key={header.key}
>
{(() => {
const customHeader = header as CustomTableHeader;
return typeof customHeader.header === 'object' &&
customHeader.header !== null &&
'content' in customHeader.header
? (customHeader.header.content as React.ReactNode)
: (customHeader.header as React.ReactNode);
})()}
</TableHeader>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow {...getRowProps({ row })} key={row.id}>
{row.cells.map((cell) => (
<TableCell key={cell.id}>{cell.value}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)}
</DataTable>
<div className={styles.btnSet}>
{typeof onNext === 'function' && (
<Button kind="primary" onClick={handleNext} renderIcon={ArrowRight}>
{t('nextButton', 'Next')}
</Button>
)}
{typeof onPrevious === 'function' && (
<Button
kind="secondary"
onClick={onPrevious}
renderIcon={ArrowLeft}
hasIconOnly
data-testid="previous-btn"
iconDescription={t('previousButton', 'Previous')}
/>
)}
</div>
</div>
</div>
);
};
export default StockOperationItemsFormStep;
|