diff --git a/src/api/config.ts b/src/api/config.ts index 181f093..5397f6e 100644 --- a/src/api/config.ts +++ b/src/api/config.ts @@ -217,16 +217,17 @@ export const SALES_REPORT_ROUTES = [ 'West-4' ]; -export const SALES_REPORT_DISTRIBUTORS = [ - 'Banashree Multi Millet Flour', - 'Shiva nandi Enterprises', - 'BPK & Co', - 'Gaviranga', - 'Bhagwan Enterprises', - 'Shreshtaa Trading Co', - 'Rajarajeshwari Enterprises', - 'Byraveshwara Trading Co' -]; +export const ROUTE_WISE_DISTRIBUTORS: Record = { + "Krishna": ["Banashree Multi Millet Flour"], + "East-1": ["Shiva nandi Enterprises"], + "East-2": ["BPK & Co"], + "East-3": ["BPK & Co"], + "East-4": ["Gaviranga "], + "West-1": ["Bhagwan Enterprises"], + "West-2": ["Shreshtaa Trading Co "], + "West-3": ["Rajarajeshwari Enterprises "], + "West-4": ["Byraveshwara Trading Co"], +}; export const SALES_REPORT_SO_NAMES = [ 'Surya C' @@ -236,6 +237,8 @@ export const SALES_REPORT_SO_NAMES = [ export const PIPELINE = { endpoints: { nearestStores: '/api/papi2/nearest-stores', - dailySalesReport: '/api/papi2/daily-sales-report' + dailySalesReport: '/api/papi2/daily-sales-report', + productiveCallSummary: '/api/papi2/productive-call-summary', + salesOfficers: '/api/papi2/sales-officers' } }; diff --git a/src/screens/DailySalesReportPage.tsx b/src/screens/DailySalesReportPage.tsx index b5df370..5816199 100644 --- a/src/screens/DailySalesReportPage.tsx +++ b/src/screens/DailySalesReportPage.tsx @@ -1,10 +1,11 @@ -import { useState, type FormEvent } from 'react'; +import { useState, useEffect, type FormEvent } from 'react'; import { Card, Input, Select } from '../components/reusable'; import { Button } from '../components/buttons'; import { SALES_REPORT_ROUTES, - SALES_REPORT_DISTRIBUTORS, - SALES_REPORT_SO_NAMES + ROUTE_WISE_DISTRIBUTORS, + BASE_URL, + PIPELINE } from '../api/config'; import { Download, Printer } from 'lucide-react'; import jsPDF from 'jspdf'; @@ -15,12 +16,36 @@ export function DailySalesReportPage() { const [date, setDate] = useState(''); const [route, setRoute] = useState(''); const [distributor, setDistributor] = useState(''); - const [soName, setSoName] = useState(''); + const [soEmail, setSoEmail] = useState(''); + const [soOptions, setSoOptions] = useState<{value: string, label: string}[]>([]); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [reportData, setReportData] = useState(null); + useEffect(() => { + async function fetchSOs() { + try { + const res = await fetch(`${BASE_URL}${PIPELINE.endpoints.salesOfficers}`, { + headers: { + 'TemplateID': '194', + 'X-Pipeline-Version': 'latest', + 'OrgID': '57', + 'GroupID': '25', + 'Content-Type': 'application/json' + } + }); + const data = await res.json(); + if (data?.response?.options) { + setSoOptions(data.response.options); + } + } catch (err) { + console.error('Failed to fetch SOs', err); + } + } + fetchSOs(); + }, []); + const downloadPDF = () => { const doc = new jsPDF('landscape'); const pageWidth = doc.internal.pageSize.getWidth(); @@ -49,7 +74,8 @@ export function DailySalesReportPage() { doc.setFontSize(10); doc.text(`Distributor Name : ${distributor}`, 14, 40); doc.text(`Date : ${date}`, rightMargin, 40, { align: 'right' }); - doc.text(`SO Name : ${soName}`, rightMargin, 45, { align: 'right' }); + const selectedSo = soOptions.find(o => o.value === soEmail); + doc.text(`SO Name : ${selectedSo ? selectedSo.label : soEmail}`, rightMargin, 45, { align: 'right' }); // Table autoTable(doc, { @@ -93,7 +119,8 @@ export function DailySalesReportPage() { doc.setFontSize(10); doc.text(`Distributor Name : ${distributor}`, 14, 40); doc.text(`Date : ${date}`, rightMargin, 40, { align: 'right' }); - doc.text(`SO Name : ${soName}`, rightMargin, 45, { align: 'right' }); + const selectedSo = soOptions.find(o => o.value === soEmail); + doc.text(`SO Name : ${selectedSo ? selectedSo.label : soEmail}`, rightMargin, 45, { align: 'right' }); // Table autoTable(doc, { @@ -149,7 +176,7 @@ export function DailySalesReportPage() { date, route, distributor, - so_name: soName, + so_name: soEmail, }); setReportData(data); @@ -175,20 +202,23 @@ export function DailySalesReportPage() { setDistributor(e.target.value)} - options={[{ value: '', label: 'Select Distributor' }, ...SALES_REPORT_DISTRIBUTORS]} + options={[{ value: '', label: 'Select Distributor' }, ...(route ? (ROUTE_WISE_DISTRIBUTORS[route] || []) : []).map(d => ({ value: d, label: d }))]} />