import React, { useState, useEffect, useRef, useCallback } from "react"; import { Link } from "react-router-dom"; import axios from "axios"; import Button from "@material-ui/core/Button"; import { useNavigate } from "react-router-dom"; import { isAutheticated } from "src/auth"; import swal from "sweetalert"; import debounce from "lodash.debounce"; import { toast } from "react-hot-toast"; const OpeningInventoryReports = () => { const token = isAutheticated(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [productsData, setProductsData] = useState([]); const [categories, setCategories] = useState([]); const [brands, setBrands] = useState([]); const nameRef = useRef(); const categoryRef = useRef(); const brandRef = useRef(); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [totalData, setTotalData] = useState(0); const getProductsData = async () => { setLoading(true); try { const response = await axios.get(`/api/report/opening-inventory`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, name: nameRef.current?.value || "", category: categoryRef.current?.value || "", brand: brandRef.current?.value || "", }, }); // console.log(response.data.data); setProductsData(response.data?.data || []); setTotalData(response.data?.pagination?.total || 0); } catch (err) { const msg = err?.response?.data?.msg || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } finally { setLoading(false); } }; const handleDownloadReport = async () => { try { const response = await axios.get( `/api/report/opening-inventory/download`, { headers: { Authorization: `Bearer ${token}`, }, responseType: "arraybuffer", // Ensure that we handle the response as binary data } ); // 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", }) ); // Step 2: Create a download link and trigger the download const link = document.createElement("a"); link.setAttribute("href", url); link.setAttribute("download", "OpeningInventoryReport.xlsx"); document.body.appendChild(link); link.click(); // Step 3: Clean up document.body.removeChild(link); window.URL.revokeObjectURL(url); // Clean up the Blob URL toast.success("Report downloaded successfully!"); } catch (err) { const msg = err?.response?.data?.msg || "Something went wrong!"; swal({ title: "Error", text: msg, icon: "error", button: "Retry", dangerMode: true, }); } }; const getCatagories = () => { axios .get(`/api/category/getCategories`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { // console.log(res?.data?.categories); setCategories(res?.data?.categories); }); }; const getBrands = () => { axios .get(`/api/brand/getBrands`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { // console.log(res?.data?.brands); setBrands(res?.data?.brands); }); }; useEffect(() => { getCatagories(); getBrands(); }, []); useEffect(() => { getProductsData(); }, [itemPerPage, currentPage]); const debouncedSearch = useCallback( debounce(() => { setCurrentPage(1); getProductsData(); }, 500), [] ); const handleSearchChange = () => { debouncedSearch(); }; return (
SKU Code | SKU Description | Category Name | Brand Name | Total At PDs & Retailers | All PDs | All Retailers |
---|---|---|---|---|---|---|
Loading... | ||||||
{product.SKU} | {product.name} | {product.category || "Category Not selected"} | {product.brand || "Brand Not selected"} | {parseFloat(product.allPdAndRd).toFixed(2)} | {parseFloat(product.allPDs).toFixed(2)} | {parseFloat(product.allRDs).toFixed(2)} |
No Product Available... |