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(() => {
if (excelData && excelData.length > 0) {
let ExcelToJsConversion = [];
let slicedData = excelData.slice(1); // Skip header row
const headers = excelData[0]; // first row is the header array
const slicedData = excelData.slice(1); // data rows
slicedData.forEach((item, index) => {
// Skip empty rows
if (!item || Object.values(item).every((val) => !val)) return;
const ExcelToJsConversion = slicedData
.filter((item) => item && item.some((val) => val !== '' && val != null))
.map((item, index) => {
const get = (i) => item[i];
const isSelf = String(get(1)).trim() === 'Self';
const offset = isSelf ? 1 : 0;
const rowData = {
key: index,
InwardDate: item['0'],
Supplier: item['1'],
InvoiceDeliveryChallan: item['3'],
SupplierInvoiceNumber: item['4'],
SupplierInvoiceDate: item['5'],
DeliveryChallanNo: item['6'],
DeliveryInvoiceDate: item['7'],
PaymentType: item['8'],
PaymentMode: item['9'],
PaymentAmount: item['10'],
DueDate: item['11'],
ProductName: item['12'],
VariantName: item['13'],
Quantity: item['14'],
PurchaseRate: item['15'],
Amount: item['16'],
ReceivedQty: item['17'],
AcceptedQty: item['18'],
MRP: item['19'],
SalesPrice: item['20'],
AmountPerPiece: item['21'],
NumberofPieceInside: item['22'],
};
// Handle conditional 'Own / With Paid' column
const supplierValue = item['1'];
if (String(supplierValue).trim() === 'Self') {
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()];
}
// 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]];
// Add optional fields based on column presence
let currentColIndex = 23;
const rowData = {
key: index,
InwardDate: get(0),
Supplier: get(1),
...(isSelf ? { OwnWithPaid: get(2) } : {}),
InvoiceDeliveryChallan: get(2 + offset),
SupplierInvoiceNumber: get(3 + offset),
SupplierInvoiceDate: get(4 + offset),
DeliveryChallanNo: get(5 + offset),
DeliveryInvoiceDate: get(6 + offset),
PaymentType: get(7 + offset),
PaymentMode: get(8 + offset),
PaymentAmount: get(9 + offset),
DueDate: get(10 + offset),
ProductName: get(11 + offset),
VariantName: get(12 + offset),
Quantity: get(13 + offset),
PurchaseRate: get(14 + offset),
Amount: get(15 + offset),
ReceivedQty: get(16 + offset),
AcceptedQty: get(17 + offset),
MRP: get(18 + offset),
SalesPrice: get(19 + offset),
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'),
};
// 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);
}
});
return rowData;
})
.filter((row) => row?.ProductName);
setDatas(ExcelToJsConversion);
}
@ -315,8 +293,7 @@ const StockExcel = ({
});
dispatch(uploadExcel({ file, jsonData: nonEmptyRows }));
setWorksheet1(worksheet1);
setDatas(formattedData);
setWorksheet1(worksheet);
};
reader.readAsArrayBuffer(file);
@ -417,14 +394,14 @@ const StockExcel = ({
pushCol('Amount Per Piece', 'AmountPerPiece', true);
pushCol('Number of Piece Inside', 'NumberofPieceInside', true);
if (isOptionalFieldAllowed('Manufacture Date'))
pushCol('Manufacture Date', 'ManufDate', false);
if (isOptionalFieldAllowed('Expire Date'))
pushCol('Expire Date', 'ExpDate', false);
if (isOptionalFieldAllowed('Batch No.'))
pushCol('Batch No.', 'BatchNo', false);
if (isOptionalFieldAllowed('Model No.'))
pushCol('Model No.', 'ModelNo', false);
pushCol('Uom', 'Uom', true);
pushCol('Tax (%)', 'Tax', true);
pushCol('Hsn', 'Hsn', false);
pushCol('IMEI', 'IMEI', false);
pushCol('Manufacture Date', 'ManufDate', false);
pushCol('Expire Date', 'ExpDate', false);
pushCol('Batch No', 'BatchNo', false);
pushCol('Model No', 'ModelNo', false);
if (isOptionalFieldAllowed('Rejected Qty'))
pushCol('Rejected Qty', 'RejectedQty', false);
if (isOptionalFieldAllowed('Free Qty'))
@ -543,6 +520,10 @@ const StockExcel = ({
SalesPrice: variant.SellPrice || 0,
NumberofPieceInside: variant.NoOfPcs || 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') {
@ -831,6 +812,34 @@ const StockExcel = ({
};
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')) {
const rIdx = colIndex('ReceivedQty');
const qIdx = colIndex('Quantity');
@ -1137,13 +1146,13 @@ const StockExcel = ({
editable: true,
},
{
title: 'Batch No.',
title: 'Batch No',
dataIndex: 'BatchNo',
key: 'BatchNo',
editable: true,
},
{
title: 'Model No.',
title: 'Model No',
dataIndex: 'ModelNo',
key: 'ModelNo',
editable: true,
@ -1166,6 +1175,30 @@ const StockExcel = ({
key: 'WholesalePrice',
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) => {
@ -1187,7 +1220,7 @@ const StockExcel = ({
dataIndex: col.dataIndex,
title: col.title,
handleSave,
onClick: () => onClickHandler(record),
onClick: onClickHandler ? () => onClickHandler(record) : undefined,
}),
};
});