-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbill.js
More file actions
88 lines (77 loc) · 3.29 KB
/
bill.js
File metadata and controls
88 lines (77 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
document.addEventListener('DOMContentLoaded', function() {
// Try to get data from both localStorage and URL
let billData;
// Method 1: Check for message data
window.addEventListener('message', function(event) {
if (event.data.type === 'billData') {
billData = event.data.data;
displayBill(billData);
}
});
// Method 2: Check localStorage
const storedBill = localStorage.getItem('currentBill');
if (storedBill) {
try {
billData = JSON.parse(storedBill);
displayBill(billData);
return;
} catch (e) {
console.error('Error parsing stored bill data', e);
}
}
// Method 3: Check URL parameters
const urlParams = new URLSearchParams(window.location.search);
const encodedData = urlParams.get('data');
if (encodedData) {
try {
billData = JSON.parse(decodeURIComponent(encodedData));
displayBill(billData);
return;
} catch (e) {
console.error('Error parsing URL bill data', e);
}
}
// If no data found
if (!billData) {
document.body.innerHTML = '<div class="container"><h2>No bill data found</h2><p>Please scan the QR code again or ask the shopkeeper to resend the bill.</p></div>';
return;
}
function displayBill(billData) {
// Set shop and bill info
document.getElementById('shopName').textContent = billData.shop;
document.getElementById('billDate').textContent = billData.date;
document.getElementById('billNumber').textContent = billData.billNumber;
// Set customer info
document.getElementById('customerName').textContent = billData.customer.name;
document.getElementById('customerPhone').textContent = billData.customer.phone;
// Add items to table
const billItemsTable = document.getElementById('billItems');
billItemsTable.innerHTML = '';
billData.items.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.name}</td>
<td>${item.quantity}</td>
<td>$${parseFloat(item.price).toFixed(2)}</td>
<td>$${item.total}</td>
`;
billItemsTable.appendChild(row);
});
// Set totals
document.getElementById('subtotal').textContent = '$' + billData.subtotal.toFixed(2);
document.getElementById('tax').textContent = '$' + billData.tax.toFixed(2);
document.getElementById('total').textContent = '$' + billData.total.toFixed(2);
// Generate payment QR code (example)
new QRCode(document.getElementById('paymentQR'), {
text: `upi://pay?pa=shop@upi&am=${billData.total}&tn=Bill ${billData.billNumber}`,
width: 150,
height: 150
});
// Add print button
const printBtn = document.createElement('button');
printBtn.id = 'printBtn';
printBtn.textContent = 'Print Bill';
printBtn.onclick = window.print;
document.querySelector('.bill-view').appendChild(printBtn);
}
});