forked from Pranav00076/Invoice-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (56 loc) · 2.72 KB
/
script.js
File metadata and controls
78 lines (56 loc) · 2.72 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
document.getElementById("in-date").valueAsDate = new Date();
addEditorRow();
function addEditorRow() {
const tbody = document.getElementById("editor-rows");
const tr = document.createElement("tr");
tr.innerHTML = `
<td><input type="text" class="item-desc" placeholder="Description" oninput="updatePreview()"></td>
<td><input type="number" class="item-qty" value="1" oninput="updatePreview()"></td>
<td><input type="number" class="item-price" value="0" oninput="updatePreview()"></td>
<td><button class="btn btn-delete" onclick="removeRow(this)">X</button></td>
`;
tbody.appendChild(tr);
updatePreview();
}
function removeRow(btn) {
const row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
updatePreview();
}
function updatePreview() {
document.getElementById("out-header").innerText =document.getElementById("in-header").value;
document.getElementById("out-num").innerText =document.getElementById("in-num").value;
document.getElementById("out-date").innerText =document.getElementById("in-date").value;
document.getElementById("out-from-name").innerText =
document.getElementById("in-from-name").value || "Company";
document.getElementById("out-from-address").innerText =
document.getElementById("in-from-address").value;
document.getElementById("out-to-name").innerText =
document.getElementById("in-to-name").value || "Client Name";
document.getElementById("out-to-address").innerText =
document.getElementById("in-to-address").value;
const editorRows = document.querySelectorAll("#editor-rows tr");
const previewBody = document.getElementById("preview-rows");
previewBody.innerHTML = "";
let grandTotal = 0;
editorRows.forEach((row) => {
const desc = row.querySelector(".item-desc").value;
const qty = parseFloat(row.querySelector(".item-qty").value) || 0;
const price = parseFloat(row.querySelector(".item-price").value) || 0;
const total = qty * price;
grandTotal += total;
const newRow = document.createElement("tr");
newRow.innerHTML = `
<td style="padding: 10px; border-bottom: 1px solid #eee;">${desc}</td>
<td style="padding: 10px; border-bottom: 1px solid #eee; text-align: center;"> ${qty}</td>
<td style="padding: 10px; border-bottom: 1px solid #eee; text-align: right;">₹${price.toFixed(
2
)}</td>
<td style="padding: 10px; border-bottom: 1px solid #eee; text-align: right;"><strong>₹${total.toFixed(
2
)}</strong></td>
`;
previewBody.appendChild(newRow);
});
document.getElementById("out-total").innerText = grandTotal.toFixed(2);
}