-
Notifications
You must be signed in to change notification settings - Fork 2
/
Carrinho.html
440 lines (361 loc) · 14 KB
/
Carrinho.html
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Carrinho</title>
<style>
body {
background-image: url('./IMG-Novas/EasyWalkBackgroundClaro\ .png');
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f3f3f3;
}
#cartContainer {
max-width: 800px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#cartItems, #boughtItems {
list-style-type: none;
padding: 0;
margin-bottom: 20px;
}
.cartItem, .boughtItem {
border-bottom: 1px solid #ddd;
padding: 10px 0;
display: flex;
justify-content: space-between;
align-items: center;
}
#checkoutButton {
display: block;
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #10005f;
color: #fff;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
#checkoutButton:hover {
background-color: #f0f4f0;
color: black;
}
#confirmPopup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
z-index: 9999;
display: none;
max-height: 400px;
overflow-y: auto;
}
#confirmPopup h2 {
margin-top: 0;
}
#confirmPopup img {
max-width: 100px;
max-height: 100px;
margin-right: 20px;
}
#confirmPopup p {
margin: 10px 0;
}
#confirmPopup button {
padding: 10px 20px;
background-color: #10005f;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
#confirmPopup button:hover {
background-color: #e9e7e7;
color: black;
}
#confirmPopup .remove {
color: red;
cursor: pointer;
margin-left: 10px;
}
#confirmPopup .remove:hover {
text-decoration: underline;
}
.login-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.login-message {
text-align: center;
font-size: 20px;
margin-top: 50px;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div id="cartContainer">
<h2>Meu Carrinho</h2>
<ul id="cartItems"></ul>
<button id="checkoutButton" onclick="showConfirmPopup()">Efetuar Compra</button>
<p id="cartEmptyMessage" style="display: none;">Seu carrinho está vazio.</p>
</div>
<div id="boughtContainer" style="display: none;">
<h2>Produtos Comprados</h2>
<ul id="boughtItems"></ul>
</div>
<div id="confirmPopup">
<h2>Confirmar Compra</h2>
<div id="popupContent"></div>
<button onclick="confirmPurchase()">Confirmar</button>
<button onclick="closeConfirmPopup()">Cancelar</button>
</div>
<div class="login-overlay" id="loginOverlay" style="display: none;">
<div class="login-message">
Você não está logado. Por favor, faça login ou cadastre-se para acessar esta página.
</div>
</div>
<!--Notas de desenpenho: Foram efetuados diversos testes para inplementar uma biblioteca que fosse capas de gerar um pdf de uma nota fiscal
Foram feitas diversas temtativas utilizando a biblioteca JSPDF porem o resultado foi muito ruim, ela nao funcionou e diversos erros foram apontados.
Potr fim em uma ultiva alternativa Foi utilizado a biblioteca PDFKit para gerar o pdf, e em poucas tentativas e ajustes ela conseguil atingir o melhor resultado ate agora.-->
<script src="pdfkit.standalone.js"></script>
<script>
function generateInvoiceNumber() {
return Math.floor(Math.random() * 1000000) + 1;
}
function loadCartItems() {
const cartItemsContainer = document.getElementById('cartItems');
const cartEmptyMessage = document.getElementById('cartEmptyMessage');
cartItemsContainer.innerHTML = '';
const storage = window.localStorage;
for (const key in storage) {
if (key.includes('product_')) {
const product = JSON.parse(storage.getItem(key));
const listItem = document.createElement('li');
listItem.className = 'cartItem';
listItem.innerHTML = `
<div>
<h3>${product.TITULO}</h3>
<p>Preço: R$ ${product.PREÇO}</p>
<p>Marca: ${product.MARCA}</p>
<p>Tamanho: ${product.TAMANHO}</p>
<p>Estado: ${product.ESTADO}</p>
</div>
`;
cartItemsContainer.appendChild(listItem);
}
}
if (cartItemsContainer.children.length === 0) {
cartEmptyMessage.style.display = 'block';
} else {
cartEmptyMessage.style.display = 'none';
}
}
function showConfirmPopup() {
const popup = document.getElementById('confirmPopup');
const popupContent = document.getElementById('popupContent');
popupContent.innerHTML = '';
const storage = window.localStorage;
for (const key in storage) {
if (key.includes('product_')) {
const product = JSON.parse(storage.getItem(key));
const cartItem = document.createElement('div');
cartItem.className = 'cartItem';
cartItem.innerHTML = `
<div>
<h3>${product.TITULO}</h3>
<p>Preço: R$ ${product.PREÇO}</p>
<p>Marca: ${product.MARCA}</p>
<p>Tamanho: ${product.TAMANHO}</p>
<p>Estado: ${product.ESTADO}</p>
</div>
`;
const removeButton = document.createElement('span');
removeButton.className = 'remove';
removeButton.textContent = '[X]';
removeButton.onclick = function() {
cartItem.remove();
const storageKey = `product_${product.ID}`;
window.localStorage.removeItem(storageKey);
};
cartItem.appendChild(removeButton);
popupContent.appendChild(cartItem);
}
}
popup.style.display = 'block';
}
function closeConfirmPopup() {
const popup = document.getElementById('confirmPopup');
popup.style.display = 'none';
}
function checkUser() {
const currentUser = localStorage.getItem('currentUser');
if (!currentUser) {
const loginOverlay = document.getElementById('loginOverlay');
loginOverlay.style.display = 'flex';
}
}
function confirmPurchase() {
alert('Compra efetuada com sucesso!');
const cartItemsContainer = document.getElementById('cartItems');
const boughtItemsContainer = document.getElementById('boughtItems');
const storage = window.localStorage;
const currentUserData = JSON.parse(storage.getItem('currentUser'));
const userEmail = currentUserData.email;
for (const key in storage) {
if (key.includes('product_')) {
const product = JSON.parse(storage.getItem(key));
if (product.OPERAÇÃO === 'Comprar') {
const listItem = document.createElement('li');
listItem.className = 'boughtItem';
listItem.innerHTML = `
<div>
<h3>${product.TITULO}</h3>
<p>Preço: R$ ${product.PREÇO}</p>
<p>Marca: ${product.MARCA}</p>
<p>Tamanho: ${product.TAMANHO}</p>
<p>Estado: ${product.ESTADO}</p>
</div>
`;
boughtItemsContainer.appendChild(listItem);
let purchaseHistory = JSON.parse(storage.getItem(userEmail + '_purchaseHistory')) || [];
const date = new Date().toLocaleDateString();
const purchase = { date, product: product.TITULO, price: product.PREÇO };
purchaseHistory.push(purchase);
storage.setItem(userEmail + '_purchaseHistory', JSON.stringify(purchaseHistory));
storage.removeItem(key);
saveAsPDF(storage);
checkUser();
}
}
}
document.getElementById('boughtContainer').style.display = 'block';
loadCartItems();
closeConfirmPopup();
saveAsPDF(storage);
}
function saveAsPDF(storage) {
const boughtItemsContainer = document.getElementById('boughtItems');
const invoiceContent = [];
const currentUser = JSON.parse(storage.getItem('currentUser'));
console.log(currentUser);
const cardData = JSON.parse(storage.getItem(`${currentUser.email}_cardData`));
const profile = JSON.parse(storage.getItem(`${currentUser.email}_profile`));
console.log(profile);
let subtotal = 0;
for (const listItem of boughtItemsContainer.children) {
const productTitle = listItem.querySelector('h3').textContent;
const productPrice = parseFloat(listItem.querySelector('p:nth-child(2)').textContent.replace('Preço: R$ ', ''));
const productBrand = listItem.querySelector('p:nth-child(3)').textContent;
const productSize = listItem.querySelector('p:nth-child(4)').textContent;
const productCondition = listItem.querySelector('p:nth-child(5)').textContent;
invoiceContent.push([productTitle, productPrice, productBrand, productSize, productCondition]);
subtotal += productPrice;
}
const taxRate = 0.1;
const tax = subtotal * taxRate;
const total = subtotal + tax;
const doc = new PDFDocument();
doc.fontSize(18).text('Nota Fiscal Eletrônica',285, 60).moveDown();
doc.fontSize(18).text('Empresa: Croesus LTDA',70, 120).moveDown();
doc.fontSize(16).text('Projeto StartupOne - Facens',70, 60).moveDown();
doc.fontSize(10).text(`N° da Nota: ${generateInvoiceNumber()}`,471,61);
doc.text('Data de Emissão: ' + new Date().toLocaleDateString()).moveDown();
doc.rect(464, 60, 70, 65).stroke();
doc.rect(55, 55, 225, 91).stroke();
doc.lineWidth(1);
doc.moveTo(50, 150).lineTo(550, 150).stroke();
doc.font('Helvetica').fontSize(10);
doc.text(`Nome/Razão Social: ${profile.name}`, 55, 160);
doc.text(`Email: ${currentUser.email}`, 55, 175);
doc.text(`CPF: ${profile.cpf}`, 55, 190);7
doc.text('Usuário: ' + currentUser.userType, 55, 205);
doc.text('Endereço de Entrega:', 300, 155);
doc.text(`Estado: ${profile.address.state}`, 300, 170);
doc.text(`Cidade: ${profile.address.city}`, 300, 185);
doc.text(`Bairro: ${profile.address.neighborhood}`, 300, 200);
doc.text(`CEP: ${profile.address.cep}`, 300, 215);
doc.text(`Rua: ${profile.address.street}`, 300, 230);
doc.moveTo(60, 240).lineTo(550, 240).stroke();
doc.font('Helvetica-Bold').fontSize(11);
doc.moveDown();
doc.text('Produto', 55, 260);
doc.text('Preço', 280, 260);
doc.text('Marca', 345, 260);
doc.text('Tamanho', 425, 260);
doc.text('Estado', 500, 260);
doc.font('Helvetica').fontSize(6);
let y = 280;
for (const rowData of invoiceContent) {
doc.text(rowData[0], 55, y);
doc.text(`R$ ${rowData[1].toFixed(2)}`, 280, y);
doc.text(rowData[2], 345, y);
doc.text(rowData[3], 425, y);
doc.text(rowData[4], 500, y, { width: 50 });
y += 10;
}
doc.moveTo(50, y).lineTo(550, y).stroke();
const deliveryFee = 5.00;
const siteFeeRate = 0.05;
const siteFee = subtotal * siteFeeRate;
doc.font('Helvetica-Bold').fontSize(12);
y += 20;
doc.text('Taxas:', 55, y);
doc.text(`Frete de Entrega: R$ ${deliveryFee.toFixed(2)}`, 200, y);
doc.rect(50, 50, 500, 700).stroke();
doc.text(`Taxa do Site (${(siteFeeRate * 100).toFixed(2)}%): R$ ${siteFee.toFixed(2)}`, 360, y);
const totalTaxes = deliveryFee + siteFee;
y += 20;
doc.text('Total a Pagar:', 55, y);
doc.text(`R$ ${(total + totalTaxes).toFixed(2)}`, 250, y);
const chunks = [];
doc.on('data', function(chunk) {
chunks.push(chunk);
});
doc.on('end', function() {
const blob = new Blob(chunks, { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'nota_fiscal.pdf';
a.click();
});
doc.end();
}
window.onload = function() {
loadCartItems();
};
const currentUser = localStorage.getItem('currentUser');
if (!currentUser) {
const loginOverlay = document.getElementById('loginOverlay');
loginOverlay.style.display = 'flex';
}
</script>
</body>
</html>