-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (119 loc) · 4.65 KB
/
script.js
File metadata and controls
139 lines (119 loc) · 4.65 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
function formatNumber(num) {
return parseFloat(num).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
}
let itemIndex = 0;
async function updateExchangeRate() {
try {
const res = await fetch("https://open.er-api.com/v6/latest/USD");
const data = await res.json();
if (data.result === "success") {
document.getElementById("exchangeRate").value = data.rates.EUR.toFixed(4);
renderPreview();
}
} catch (e) {
alert("Could not fetch exchange rate. Using manual value.");
}
}
function addItem(desc = "", amount = "", currency = "EUR") {
const container = document.getElementById("items");
const html = `
<div class="item-box" id="item-${itemIndex}">
<label>Description</label>
<input value="${desc}" id="desc-${itemIndex}">
<label>Amount</label>
<input type="number" step="0.01" value="${amount}" id="amount-${itemIndex}">
<label>Currency</label>
<select id="currency-${itemIndex}">
<option value="USD" ${currency==="USD"?'selected':''}>USD</option>
<option value="EUR" ${currency==="EUR"?'selected':''}>EUR</option>
</select>
<button class="btn-delete" onclick="removeItem(${itemIndex})">Delete</button>
</div>`;
container.insertAdjacentHTML("beforeend", html);
itemIndex++;
}
function removeItem(i) { document.getElementById(`item-${i}`).remove(); }
function renderPreview() {
const title = document.getElementById("title").value || "INVOICE";
const date = document.getElementById("date").value;
const paidBy = document.getElementById("paidBy").value.replace(/\n/g, "<br>");
const paidTo = document.getElementById("paidTo").value.replace(/\n/g, "<br>");
const extraTitle = document.getElementById("extraTitle").value;
const extraDesc = document.getElementById("extraDescription").value.replace(/\n/g, "<br>");
const columnLabel = document.getElementById("columnLabel").value || "Amount";
const items = [];
document.querySelectorAll("[id^=item-]").forEach(div => {
const i = div.id.split("-")[1];
const amount = parseFloat(document.getElementById(`amount-${i}`).value) || 0;
const desc = document.getElementById(`desc-${i}`).value.trim();
if (amount > 0 || desc) {
items.push({
desc: desc || "—",
amount: amount,
currency: document.getElementById(`currency-${i}`).value
});
}
});
const baseCurrency = document.getElementById("baseCurrency").value;
const rate = parseFloat(document.getElementById("exchangeRate").value) || 1;
let totalBase = 0;
items.forEach(item => {
if (item.currency === baseCurrency) {
totalBase += item.amount;
} else if (baseCurrency === "EUR") {
totalBase += item.amount * rate;
} else {
totalBase += item.amount / rate;
}
});
const rows = items.map(item => `
<tr>
<td>${item.desc}</td>
<td class="amount">${formatNumber(item.amount)} ${item.currency}</td>
</tr>
`).join("");
const note = items.some(i => i.currency !== baseCurrency) ? `
<div class="note">
Exchange rate used: 1 USD = ${rate.toFixed(4)} EUR
</div>` : "";
const html = `
<div class="title">${title}</div>
<div class="date">${date}</div>
<div class="info-grid">
<div class="info-box">
<div class="section-title">Bill From</div>
${paidBy}
</div>
<div class="info-box">
<div class="section-title">Bill To</div>
${paidTo}
</div>
</div>
${extraTitle ? `<div class="section-title">${extraTitle}</div>` : ""}
${extraDesc ? `<div style="margin-bottom:30px;line-height:1.7;">${extraDesc}</div>` : ""}
<table>
<thead>
<tr>
<th>Description</th>
<th>${columnLabel}</th>
</tr>
</thead>
<tbody>
${rows}
<tr class="total-row">
<td><strong>Total</strong></td>
<td class="amount"><strong>${formatNumber(totalBase)} ${baseCurrency}</strong></td>
</tr>
</tbody>
</table>
${note}
`;
document.getElementById("invoice-preview").innerHTML = html;
}
addItem("Server 1", "15.00", "EUR");
addItem("Server 2", "15.00", "EUR");
updateExchangeRate();
renderPreview();