-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (154 loc) · 6.17 KB
/
script.js
File metadata and controls
181 lines (154 loc) · 6.17 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
let count = 1, total = 0;
function generateInvoiceDetails() {
const invoiceNo = 'INV-' + String(Math.floor(Math.random() * 100000)).padStart(5, '0');
const today = new Date().toLocaleDateString();
document.getElementById('invoiceNumber').value = invoiceNo;
document.getElementById('invoiceDate').value = today;
}
function addItem() {
const name = document.getElementById('customerName').value.trim();
const address = document.getElementById('customerAddress').value.trim();
const phone = document.getElementById('customerPhone').value.trim();
const nameRegex = /^[a-zA-Z ]{2,}$/;
const phoneRegex = /^[6-9][0-9]{9}$/;
if (!nameRegex.test(name)) {
alert('Please enter a valid name (letters only, min 2 characters).');
return;
}
if (address.length < 5) {
alert('Please enter a valid address (min 5 characters).');
return;
}
if (!phoneRegex.test(phone)) {
alert('Please enter a valid 10-digit Indian phone number.');
return;
}
const product = document.getElementById('productName').value.trim();
const qty = parseInt(document.getElementById('productQty').value);
const price = parseFloat(document.getElementById('productPrice').value);
const gst = parseFloat(document.getElementById('productGST').value) || 0;
if (!product || isNaN(qty) || isNaN(price)) {
alert('Enter valid product details.');
return;
}
const subtotal = price * qty + (price * qty * gst / 100);
total += subtotal;
const row = document.createElement('tr');
row.innerHTML = `
<td>${count}</td>
<td>${product}</td>
<td>${qty}</td>
<td>₹${price.toFixed(2)}</td>
<td>${gst}%</td>
<td>₹${subtotal.toFixed(2)}</td>
<td><button class="btn btn-danger btn-sm" onclick="deleteRow(this, ${subtotal})">🗑️</button></td>
`;
document.getElementById('invoiceTable').appendChild(row);
document.getElementById('invoiceTotal').innerText = total.toFixed(2);
count++;
['productName', 'productQty', 'productPrice', 'productGST'].forEach(id => document.getElementById(id).value = '');
}
function deleteRow(btn, sub) {
btn.closest('tr').remove();
total -= sub;
document.getElementById('invoiceTotal').innerText = total.toFixed(2);
}
function resetInvoice() {
document.getElementById('invoiceTable').innerHTML = '';
document.getElementById('invoiceTotal').innerText = '0.00';
document.getElementById('customerInfoPrint').innerHTML = '';
document.getElementById('invoiceTablePrint').innerHTML = '';
count = 1;
total = 0;
generateInvoiceDetails();
}
function copyToPrintArea() {
const name = document.getElementById('customerName').value.trim();
const address = document.getElementById('customerAddress').value.trim();
const phone = document.getElementById('customerPhone').value.trim();
const invoiceNo = document.getElementById('invoiceNumber').value;
const invoiceDate = document.getElementById('invoiceDate').value;
document.getElementById('customerInfoPrint').innerHTML = `
<p><strong>Invoice No:</strong> ${invoiceNo}</p>
<p><strong>Date:</strong> ${invoiceDate}</p>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Address:</strong> ${address}</p>
<p><strong>Phone:</strong> ${phone}</p>`;
const originalRows = document.querySelectorAll('#invoiceTable tr');
const printTable = document.getElementById('invoiceTablePrint');
printTable.innerHTML = '';
originalRows.forEach(row => {
const cells = row.querySelectorAll('td');
if (cells.length === 7) {
const tr = document.createElement('tr');
for (let i = 0; i < 6; i++) {
tr.innerHTML += `<td>${cells[i].innerText}</td>`;
}
printTable.appendChild(tr);
}
});
document.getElementById('invoiceTotalPrint').innerText = total.toFixed(2);
}
function generatePDF() {
copyToPrintArea();
const el = document.getElementById('printArea');
el.style.display = 'block';
html2pdf().from(el).save('invoice.pdf').then(() => {
el.style.display = 'none';
});
}
function generateInvoice() {
copyToPrintArea();
const content = document.getElementById("printArea").innerHTML;
const win = window.open('', '', 'width=800,height=600');
win.document.write(`
<html>
<head>
<title>Invoice Print</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { font-family: 'Inter', sans-serif; padding: 20px; }
.table { width: 100%; border-collapse: collapse; }
.table, .table th, .table td { border: 1px solid #dee2e6; }
.table th { background-color: #343a40; color: white; }
.text-end { text-align: right; }
</style>
</head>
<body onload="window.print(); window.close();">
${content}
</body>
</html>
`);
win.document.close();
setTimeout(() => window.location.href = 'success.html', 1000);
}
// Initialize invoice details on load
window.onload = function() {
generateInvoiceDetails();
};
// function generateInvoice() {
// copyToPrintArea();
// const printContent = document.getElementById("printArea").innerHTML;
// const originalTitle = document.title;
// const win = window.open('', '', 'width=800,height=600');
// win.document.write(`
// <html>
// <head>
// <title>Invoice Print</title>
// <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
// <style>
// body { font-family: 'Inter', sans-serif; padding: 20px; }
// .table { width: 100%; border-collapse: collapse; }
// .table, .table th, .table td { border: 1px solid #dee2e6; }
// .table th { background-color: #343a40; color: white; }
// .text-end { text-align: right; }
// </style>
// </head>
// <body onload="window.print(); window.close();">
// ${printContent}
// </body>
// </html>
// `);
// win.document.close();
// setTimeout(() => window.location.href = 'success.html', 1000);
// }