Added new field in excel sheet

This commit is contained in:
Srinath 2026-03-23 19:24:43 +05:30
parent 53eecf6dc3
commit d47a486461
1 changed files with 122 additions and 89 deletions

View File

@ -149,87 +149,65 @@ const StockExcel = ({
useEffect(() => { useEffect(() => {
if (excelData && excelData.length > 0) { if (excelData && excelData.length > 0) {
let ExcelToJsConversion = []; const headers = excelData[0]; // first row is the header array
let slicedData = excelData.slice(1); // Skip header row const slicedData = excelData.slice(1); // data rows
slicedData.forEach((item, index) => { const ExcelToJsConversion = slicedData
// Skip empty rows .filter((item) => item && item.some((val) => val !== '' && val != null))
if (!item || Object.values(item).every((val) => !val)) return; .map((item, index) => {
const get = (i) => item[i];
const isSelf = String(get(1)).trim() === 'Self';
const offset = isSelf ? 1 : 0;
// Build header-to-index map from actual headers row
const headerIndexMap = {};
headers.forEach((h, i) => {
if (h) headerIndexMap[String(h).trim()] = i;
});
const getByHeader = (headerName) =>
item[headerIndexMap[headerName]];
const rowData = { const rowData = {
key: index, key: index,
InwardDate: item['0'], InwardDate: get(0),
Supplier: item['1'], Supplier: get(1),
InvoiceDeliveryChallan: item['3'], ...(isSelf ? { OwnWithPaid: get(2) } : {}),
SupplierInvoiceNumber: item['4'], InvoiceDeliveryChallan: get(2 + offset),
SupplierInvoiceDate: item['5'], SupplierInvoiceNumber: get(3 + offset),
DeliveryChallanNo: item['6'], SupplierInvoiceDate: get(4 + offset),
DeliveryInvoiceDate: item['7'], DeliveryChallanNo: get(5 + offset),
PaymentType: item['8'], DeliveryInvoiceDate: get(6 + offset),
PaymentMode: item['9'], PaymentType: get(7 + offset),
PaymentAmount: item['10'], PaymentMode: get(8 + offset),
DueDate: item['11'], PaymentAmount: get(9 + offset),
ProductName: item['12'], DueDate: get(10 + offset),
VariantName: item['13'], ProductName: get(11 + offset),
Quantity: item['14'], VariantName: get(12 + offset),
PurchaseRate: item['15'], Quantity: get(13 + offset),
Amount: item['16'], PurchaseRate: get(14 + offset),
ReceivedQty: item['17'], Amount: get(15 + offset),
AcceptedQty: item['18'], ReceivedQty: get(16 + offset),
MRP: item['19'], AcceptedQty: get(17 + offset),
SalesPrice: item['20'], MRP: get(18 + offset),
AmountPerPiece: item['21'], SalesPrice: get(19 + offset),
NumberofPieceInside: item['22'], AmountPerPiece: get(20 + offset),
NumberofPieceInside: get(21 + offset),
Uom: getByHeader('Uom'),
Tax: getByHeader('Tax (%)'),
Hsn: getByHeader('Hsn'),
IMEI: getByHeader('IMEI'),
ManufactureDate: getByHeader('Manufacture Date') ?? getByHeader('Manuf.Date'),
ExpireDate: getByHeader('Expire Date') ?? getByHeader('Exp.Date'),
BatchNo: getByHeader('Batch No') ?? getByHeader('Batch'),
ModelNo: getByHeader('Model No') ?? getByHeader('Model'),
RejectedQty: getByHeader('Rejected Qty'),
FreeQty: getByHeader('Free Qty'),
WholesalePrice: getByHeader('Wholesale Price'),
}; };
// Handle conditional 'Own / With Paid' column return rowData;
const supplierValue = item['1']; })
if (String(supplierValue).trim() === 'Self') { .filter((row) => row?.ProductName);
rowData.OwnWithPaid = item['2'];
// Shift all subsequent columns by 1
Object.keys(rowData).forEach((key) => {
if (
!['key', 'InwardDate', 'Supplier', 'OwnWithPaid'].includes(key)
) {
const currentIndex = parseInt(Object.keys(rowData).indexOf(key));
rowData[key] = item[currentIndex.toString()];
}
});
}
// Add optional fields based on column presence
let currentColIndex = 23;
// Check for optional fields in order they appear in handleDownload
const optionalFields = [
{ key: 'ManufactureDate', header: 'Manufacture Date' },
{ key: 'ExpireDate', header: 'Expire Date' },
{ key: 'BatchNo', header: 'Batch No.' },
{ key: 'ModelNo', header: 'Model No.' },
{ key: 'RejectedQty', header: 'Rejected Qty' },
{ key: 'FreeQty', header: 'Free Qty' },
{ key: 'WholesalePrice', header: 'Wholesale Price' },
];
// Get headers from first row to determine which optional fields exist
const headers = excelData[0];
optionalFields.forEach((field) => {
if (headers.includes(field.header)) {
rowData[field.key] = item[currentColIndex.toString()];
currentColIndex++;
}
});
if (
rowData?.PurchaseRate &&
rowData?.Amount &&
rowData?.MRP &&
rowData?.SalesPrice &&
rowData?.ProductName &&
rowData?.PaymentAmount
) {
ExcelToJsConversion.push(rowData);
}
});
setDatas(ExcelToJsConversion); setDatas(ExcelToJsConversion);
} }
@ -315,8 +293,7 @@ const StockExcel = ({
}); });
dispatch(uploadExcel({ file, jsonData: nonEmptyRows })); dispatch(uploadExcel({ file, jsonData: nonEmptyRows }));
setWorksheet1(worksheet1); setWorksheet1(worksheet);
setDatas(formattedData);
}; };
reader.readAsArrayBuffer(file); reader.readAsArrayBuffer(file);
@ -417,14 +394,14 @@ const StockExcel = ({
pushCol('Amount Per Piece', 'AmountPerPiece', true); pushCol('Amount Per Piece', 'AmountPerPiece', true);
pushCol('Number of Piece Inside', 'NumberofPieceInside', true); pushCol('Number of Piece Inside', 'NumberofPieceInside', true);
if (isOptionalFieldAllowed('Manufacture Date')) pushCol('Uom', 'Uom', true);
pushCol('Tax (%)', 'Tax', true);
pushCol('Hsn', 'Hsn', false);
pushCol('IMEI', 'IMEI', false);
pushCol('Manufacture Date', 'ManufDate', false); pushCol('Manufacture Date', 'ManufDate', false);
if (isOptionalFieldAllowed('Expire Date'))
pushCol('Expire Date', 'ExpDate', false); pushCol('Expire Date', 'ExpDate', false);
if (isOptionalFieldAllowed('Batch No.')) pushCol('Batch No', 'BatchNo', false);
pushCol('Batch No.', 'BatchNo', false); pushCol('Model No', 'ModelNo', false);
if (isOptionalFieldAllowed('Model No.'))
pushCol('Model No.', 'ModelNo', false);
if (isOptionalFieldAllowed('Rejected Qty')) if (isOptionalFieldAllowed('Rejected Qty'))
pushCol('Rejected Qty', 'RejectedQty', false); pushCol('Rejected Qty', 'RejectedQty', false);
if (isOptionalFieldAllowed('Free Qty')) if (isOptionalFieldAllowed('Free Qty'))
@ -543,6 +520,10 @@ const StockExcel = ({
SalesPrice: variant.SellPrice || 0, SalesPrice: variant.SellPrice || 0,
NumberofPieceInside: variant.NoOfPcs || 0, NumberofPieceInside: variant.NoOfPcs || 0,
AmountPerPiece: variant.OnePcsPrice || 0, AmountPerPiece: variant.OnePcsPrice || 0,
Uom: variant.UomName || product.UomName || '',
Tax: variant.TaxPer || product.TaxPer || 0,
Hsn: product.HsnCode || '',
IMEI: '',
}; };
if (String(selectedSupplierData?.SuppName).trim() === 'Self') { if (String(selectedSupplierData?.SuppName).trim() === 'Self') {
@ -831,6 +812,34 @@ const StockExcel = ({
}; };
expCell.protection = { locked: false }; expCell.protection = { locked: false };
} }
if (colIndex('BatchNo')) {
const cell = worksheet.getCell(r, colIndex('BatchNo'));
cell.protection = { locked: false };
}
if (colIndex('ModelNo')) {
const cell = worksheet.getCell(r, colIndex('ModelNo'));
cell.protection = { locked: false };
}
if (colIndex('Hsn')) {
const cell = worksheet.getCell(r, colIndex('Hsn'));
cell.protection = { locked: false };
}
if (colIndex('Uom')) {
const cell = worksheet.getCell(r, colIndex('Uom'));
cell.protection = { locked: true };
}
if (colIndex('Tax')) {
const cIdx = colIndex('Tax');
const letter = colLetter(cIdx);
const taxCell = worksheet.getCell(`${letter}${r}`);
taxCell.numFmt = '#,##0.00';
taxCell.protection = { locked: false };
addNumberValidation(taxCell, true, 0);
}
if (colIndex('IMEI')) {
const cell = worksheet.getCell(r, colIndex('IMEI'));
cell.protection = { locked: false };
}
if (colIndex('ReceivedQty') && colIndex('Quantity')) { if (colIndex('ReceivedQty') && colIndex('Quantity')) {
const rIdx = colIndex('ReceivedQty'); const rIdx = colIndex('ReceivedQty');
const qIdx = colIndex('Quantity'); const qIdx = colIndex('Quantity');
@ -1137,13 +1146,13 @@ const StockExcel = ({
editable: true, editable: true,
}, },
{ {
title: 'Batch No.', title: 'Batch No',
dataIndex: 'BatchNo', dataIndex: 'BatchNo',
key: 'BatchNo', key: 'BatchNo',
editable: true, editable: true,
}, },
{ {
title: 'Model No.', title: 'Model No',
dataIndex: 'ModelNo', dataIndex: 'ModelNo',
key: 'ModelNo', key: 'ModelNo',
editable: true, editable: true,
@ -1166,6 +1175,30 @@ const StockExcel = ({
key: 'WholesalePrice', key: 'WholesalePrice',
editable: true, editable: true,
}, },
{
title: 'Hsn',
dataIndex: 'Hsn',
key: 'Hsn',
editable: true,
},
{
title: 'Uom',
dataIndex: 'Uom',
key: 'Uom',
editable: true,
},
{
title: 'Tax (%)',
dataIndex: 'Tax',
key: 'Tax',
editable: true,
},
{
title: 'IMEI',
dataIndex: 'IMEI',
key: 'IMEI',
editable: true,
},
]; ];
const column = columns?.map((col) => { const column = columns?.map((col) => {
@ -1187,7 +1220,7 @@ const StockExcel = ({
dataIndex: col.dataIndex, dataIndex: col.dataIndex,
title: col.title, title: col.title,
handleSave, handleSave,
onClick: () => onClickHandler(record), onClick: onClickHandler ? () => onClickHandler(record) : undefined,
}), }),
}; };
}); });