This commit is contained in:
Sibunnayak 2024-11-06 17:55:00 +05:30
parent f8a9673e24
commit d197cb2c43
2 changed files with 36 additions and 21 deletions

View File

@ -54,29 +54,36 @@ const OpeningInventoryReports = () => {
setLoading(false); setLoading(false);
} }
}; };
const handleDownloadReport = async () => { const handleDownloadReport = async () => {
try { try {
const response = await axios.get(`/api/report/opening-inventory/download`, { const response = await axios.get(
headers: { `/api/report/opening-inventory/download`,
Authorization: `Bearer ${token}`, {
}, headers: {
responseType: 'arraybuffer', // Ensure that we handle the response as binary data Authorization: `Bearer ${token}`,
}); },
responseType: "arraybuffer", // Ensure that we handle the response as binary data
}
);
// Step 1: Convert the response data into a Blob // Step 1: Convert the response data into a Blob
const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })); const url = window.URL.createObjectURL(
new Blob([response.data], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
})
);
// Step 2: Create a download link and trigger the download // Step 2: Create a download link and trigger the download
const link = document.createElement("a"); const link = document.createElement("a");
link.setAttribute("href", url); link.setAttribute("href", url);
link.setAttribute("download", "OpeningInventoryReport.xlsx"); link.setAttribute("download", "OpeningInventoryReport.xlsx");
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
// Step 3: Clean up // Step 3: Clean up
document.body.removeChild(link); document.body.removeChild(link);
window.URL.revokeObjectURL(url); // Clean up the Blob URL window.URL.revokeObjectURL(url); // Clean up the Blob URL
} catch (err) { } catch (err) {
const msg = err?.response?.data?.msg || "Something went wrong!"; const msg = err?.response?.data?.msg || "Something went wrong!";
swal({ swal({
@ -232,7 +239,7 @@ const OpeningInventoryReports = () => {
className="mt-4" className="mt-4"
variant="contained" variant="contained"
color="primary" color="primary"
size="small" size="small"
onClick={handleDownloadReport} onClick={handleDownloadReport}
disabled={loading} disabled={loading}
> >
@ -264,7 +271,7 @@ const OpeningInventoryReports = () => {
Brand Name Brand Name
</th> </th>
<th className="text-start" style={{ width: "15%" }}> <th className="text-start" style={{ width: "15%" }}>
Total At PDs & Retailers Total At PDs & Retailers
</th> </th>
<th className="text-start" style={{ width: "10%" }}> <th className="text-start" style={{ width: "10%" }}>
All PDs All PDs
@ -326,10 +333,14 @@ const OpeningInventoryReports = () => {
{product.brand || "Brand Not selected"} {product.brand || "Brand Not selected"}
</td> </td>
<td className="text-start"> <td className="text-start">
{product.allPdAndRd} {parseFloat(product.allPdAndRd).toFixed(2)}
</td>
<td className="text-start">
{parseFloat(product.allPDs).toFixed(2)}
</td>
<td className="text-start">
{parseFloat(product.allRDs).toFixed(2)}
</td> </td>
<td className="text-start">{product.allPDs}</td>
<td className="text-start">{product.allRDs}</td>
</tr> </tr>
)) ))
) : ( ) : (

View File

@ -234,7 +234,7 @@ const StockReports = () => {
className="mt-4" className="mt-4"
variant="contained" variant="contained"
color="primary" color="primary"
size="small" size="small"
onClick={handleDownloadReport} onClick={handleDownloadReport}
disabled={loading} disabled={loading}
> >
@ -328,10 +328,14 @@ const StockReports = () => {
{product.brand || "Brand Not selected"} {product.brand || "Brand Not selected"}
</td> </td>
<td className="text-start"> <td className="text-start">
{product.allPdAndRd} {parseFloat(product.allPdAndRd).toFixed(2)}
</td>
<td className="text-start">
{parseFloat(product.allPDs).toFixed(2)}
</td>
<td className="text-start">
{parseFloat(product.allRDs).toFixed(2)}
</td> </td>
<td className="text-start">{product.allPDs}</td>
<td className="text-start">{product.allRDs}</td>
</tr> </tr>
)) ))
) : ( ) : (