const React = window.React;
const SALES_API_URL = (window.ENV_CONFIG?.SALES_API_URL || 'http://127.0.0.1:9000') + '/api/sales';
function GstCalculator(props) {
const transactions = props.transactions || [];
const [view, setView] = React.useState('supplier'); // 'supplier' or 'sales'
const [salesData, setSalesData] = React.useState([]);
const [loading, setLoading] = React.useState(false);
const [error, setError] = React.useState('');
const [startDate, setStartDate] = React.useState('');
const [endDate, setEndDate] = React.useState('');
const [showZeroGst, setShowZeroGst] = React.useState(false);
React.useEffect(() => {
if (view === 'sales') {
setLoading(true);
// Get token from props or localStorage
const token = props.token || localStorage.getItem('sales_token') || localStorage.getItem('branch_token') || '';
// Build query params for date filter
let url = SALES_API_URL;
const params = [];
if (startDate) params.push(`startDate=${encodeURIComponent(startDate)}`);
if (endDate) params.push(`endDate=${encodeURIComponent(endDate)}`);
if (params.length) url += '?' + params.join('&');
fetch(url, {
headers: {
'Authorization': token ? `Bearer ${token}` : '',
},
})
.then(res => {
if (res.status === 401) throw new Error('Unauthorized: Invalid or missing token');
return res.json();
})
.then(data => {
// Flatten sales items for table display
const flat = [];
if (Array.isArray(data.sales)) {
data.sales.forEach(sale => {
(sale.items || []).forEach(item => {
flat.push({
productNo: item.productNo,
qty: item.qty,
subTotal: sale.subTotal,
cgstAmount: sale.cgstAmount,
sgstAmount: sale.sgstAmount,
igstAmount: sale.igstAmount,
paymentMethod: sale.paymentMethod,
createdAt: sale.createdAt,
cgst: sale.cgst !== undefined ? sale.cgst : 9,
sgst: sale.sgst !== undefined ? sale.sgst : 9,
igst: sale.igst !== undefined ? sale.igst : 0,
});
});
});
}
setSalesData(flat);
setLoading(false);
})
.catch(e => {
setError(e.message || 'Failed to fetch sales data');
setLoading(false);
});
}
}, [view, props.token, startDate, endDate]);
// Supplier GST filter by date
const filteredTransactions = transactions.filter(t => {
if (startDate && (!t.createdAt || new Date(t.createdAt) < new Date(startDate))) return false;
if (endDate && (!t.createdAt || new Date(t.createdAt) > new Date(endDate))) return false;
return true;
});
// Show only rows with meaningful data: require at least two of
// (supplier, bank, branch, supplierAmount>0, gstAmount>0).
// If showZeroGst is true, preserve the previous behavior (show all filtered rows).
const displayedTransactions = filteredTransactions.filter(t => {
if (showZeroGst) return true;
function hasText(v) {
if (v == null) return false;
const s = String(v).trim();
if (!s) return false;
if (s === '-') return false;
return true;
}
const checks = [];
checks.push(hasText(t.supplierName || t.supplier || t.supplier_id));
checks.push(hasText(t.bankName || t.bank));
checks.push(hasText(t.branchName || t.branch_name || t.branch));
checks.push((Number(t.supplierAmount) || 0) > 0);
checks.push((Number(t.gstAmount) || 0) > 0);
const truthyCount = checks.reduce((s, v) => s + (v ? 1 : 0), 0);
return truthyCount >= 2;
});
const totalGst = displayedTransactions.reduce((sum, t) => sum + (Number(t.gstAmount) || 0), 0);
// Compute per-branch GST subtotals
const branchTotals = displayedTransactions.reduce((map, t) => {
const branch = (t.branchName || t.branch_name || '').toString() || 'Main';
const v = Number(t.gstAmount) || 0;
map[branch] = (map[branch] || 0) + v;
return map;
}, {});
// Sales GST calculation
const totalSalesCgst = salesData.reduce((sum, s) => sum + (Number(s.cgstAmount) || 0), 0);
const totalSalesSgst = salesData.reduce((sum, s) => sum + (Number(s.sgstAmount) || 0), 0);
const totalSalesIgst = salesData.reduce((sum, s) => sum + (Number(s.igstAmount) || 0), 0);
const totalSalesGst = totalSalesCgst + totalSalesSgst + totalSalesIgst;
return (
GST Calculator
{view === 'supplier' ? (
<>
Total GST Amount: {totalGst}
{/* Branch subtotals */}
{Object.keys(branchTotals).length === 0 ? null : Object.keys(branchTotals).map((b) => (
{b || 'Main'}: {branchTotals[b]}
))}
| Supplier |
Bank |
Branch |
Supplier Amount |
GST Amount |
Date |
{displayedTransactions.map((t, idx) => (
| {t.supplierName || '-'} |
{t.bankName || '-'} |
{t.branchName || '-'} |
{t.supplierAmount} |
{t.gstAmount} |
{t.createdAt ? new Date(t.createdAt).toLocaleString() : '-'} |
))}
>
) : (
<>
{loading ? (
Loading sales GST data...
) : error ? (
{error}
) : (
<>
Total Sales GST Amount: {totalSalesGst.toFixed(2)}
CGST: {totalSalesCgst.toFixed(2)} | SGST: {totalSalesSgst.toFixed(2)} | IGST: {totalSalesIgst.toFixed(2)}
| Product No |
Qty |
Sub Total |
CGST |
SGST |
IGST |
Payment Method |
GST (%) |
{salesData.map((s, idx) => (
| {s.productNo || '-'} |
{s.qty || '-'} |
{s.subTotal || '-'} |
{s.cgstAmount || '-'} |
{s.sgstAmount || '-'} |
{s.igstAmount || '-'} |
{s.paymentMethod || '-'} |
{`CGST: ${s.cgst}% | SGST: ${s.sgst}% | IGST: ${s.igst}%`}
|
))}
>
)}
>
)}
);
}
window.GstCalculator = GstCalculator;