import { useState, type FormEvent } from 'react'; import { Card, Input, Select } from '../components/reusable'; import { Button } from '../components/buttons/Button'; import { SALES_REPORT_ROUTES, ROUTE_WISE_DISTRIBUTORS, SALES_REPORT_SO_NAMES } from '../api/config'; import { Download, Printer } from 'lucide-react'; import jsPDF from 'jspdf'; import autoTable from 'jspdf-autotable'; export function DailySalesReportPage() { const [date, setDate] = useState(''); const [route, setRoute] = useState(''); const [distributor, setDistributor] = useState(''); const [soName, setSoName] = useState(''); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [reportData, setReportData] = useState(null); const downloadPDF = () => { const doc = new jsPDF(); // Headers doc.setFontSize(16); doc.setFont('helvetica', 'bold'); doc.text('Krishna Flour Mills (Bangalore) Pvt. Limited', 105, 15, { align: 'center' }); doc.setFontSize(11); doc.setFont('helvetica', 'normal'); doc.text('19, Platform Road, Bengaluru - 560 020', 105, 22, { align: 'center' }); doc.setFontSize(12); doc.setFont('helvetica', 'bold'); doc.text('DAILY ORDER REPORT', 105, 30, { align: 'center' }); // Simple underline for DAILY ORDER REPORT const textWidth = doc.getTextWidth('DAILY ORDER REPORT'); doc.setLineWidth(0.5); doc.line(105 - textWidth / 2, 31, 105 + textWidth / 2, 31); // Meta Info doc.setFontSize(10); doc.text(`Distributor Name : ${distributor}`, 14, 40); doc.text(`Date : ${date}`, 196, 40, { align: 'right' }); doc.text(`SO Name : ${soName}`, 196, 45, { align: 'right' }); // Table autoTable(doc, { html: '#report-table', startY: 50, theme: 'grid', styles: { fontSize: 8, textColor: [0, 0, 0], lineColor: [0, 0, 0], lineWidth: 0.2 }, headStyles: { fillColor: [243, 244, 246], fontStyle: 'bold', halign: 'center' }, }); doc.save(`Daily_Sales_Report_${date || 'Draft'}.pdf`); }; const printReport = async () => { const doc = new jsPDF(); // Headers doc.setFontSize(16); doc.setFont('helvetica', 'bold'); doc.text('Krishna Flour Mills (Bangalore) Pvt. Limited', 105, 15, { align: 'center' }); doc.setFontSize(11); doc.setFont('helvetica', 'normal'); doc.text('19, Platform Road, Bengaluru - 560 020', 105, 22, { align: 'center' }); doc.setFontSize(12); doc.setFont('helvetica', 'bold'); doc.text('DAILY ORDER REPORT', 105, 30, { align: 'center' }); // Simple underline for DAILY ORDER REPORT const textWidth = doc.getTextWidth('DAILY ORDER REPORT'); doc.setLineWidth(0.5); doc.line(105 - textWidth / 2, 31, 105 + textWidth / 2, 31); // Meta Info doc.setFontSize(10); doc.text(`Distributor Name : ${distributor}`, 14, 40); doc.text(`Date : ${date}`, 196, 40, { align: 'right' }); doc.text(`SO Name : ${soName}`, 196, 45, { align: 'right' }); // Table autoTable(doc, { html: '#report-table', startY: 50, theme: 'grid', styles: { fontSize: 8, textColor: [0, 0, 0], lineColor: [0, 0, 0], lineWidth: 0.2 }, headStyles: { fillColor: [243, 244, 246], fontStyle: 'bold', halign: 'center' }, }); const blob = doc.output('blob'); // Fallback for PC / unsupported browsers const blobUrl = URL.createObjectURL(blob); const iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = blobUrl; document.body.appendChild(iframe); iframe.onload = () => { setTimeout(() => { iframe.contentWindow?.focus(); iframe.contentWindow?.print(); }, 500); }; }; async function submit(e: FormEvent) { e.preventDefault(); setBusy(true); setError(null); setReportData(null); try { const res = await fetch('https://sandbox.getzino.in/api/papi2/daily-sales-report', { method: 'POST', headers: { 'TemplateID': '157', 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json', }, body: JSON.stringify({ date, route, distributor, so_name: soName, }) }); if (!res.ok) { throw new Error(`Error: ${res.status} ${res.statusText}`); } const data = await res.json(); setReportData(data); } catch (err) { setError((err as Error).message ?? 'Failed to fetch report'); } finally { setBusy(false); } } return (

Daily Sales Report

setDate(e.target.value)} /> setDistributor(e.target.value)} options={[{ value: '', label: 'Select Distributor' }, ...(route ? (ROUTE_WISE_DISTRIBUTORS[route] || []) : []).map(d => ({ value: d, label: d }))]} />