Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aylmerbolzan committed Mar 11, 2024
0 parents commit 39d52d2
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
106 changes: 106 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<!DOCTYPE html>
<html lang="pt-br">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fracionador de Frete</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body {
padding: 20px;
}
</style>
</head>

<body>
<div class="container">
<h1>Fracionador de Frete</h1>
<fieldset class="border p-2">
<legend class="w-auto">Pacote</legend>
<div class="form-row">
<div class="col">
<label for="altura">Altura (cm):</label>
<input type="number" id="altura" class="form-control">
</div>
<div class="col">
<label for="largura">Largura (cm):</label>
<input type="number" id="largura" class="form-control">
</div>
<div class="col">
<label for="comprimento">Comprimento (cm):</label>
<input type="number" id="comprimento" class="form-control">
</div>
<div class="col">
<label for="peso">Peso (kg):</label>
<input type="number" id="peso" class="form-control">
</div>
<div class="col">
<label for="valor_pacote">Valor (R$):</label>
<input type="number" id="valor_pacote" class="form-control">
</div>
</div>
</fieldset>

<fieldset class="border p-2 mt-3">
<legend class="w-auto">Item 1</legend>
<div class="form-row">
<div class="col">
<label for="altura_item1">Altura (cm):</label>
<input type="number" id="altura_item1" class="form-control">
</div>
<div class="col">
<label for="largura_item1">Largura (cm):</label>
<input type="number" id="largura_item1" class="form-control">
</div>
<div class="col">
<label for="comprimento_item1">Comprimento (cm):</label>
<input type="number" id="comprimento_item1" class="form-control">
</div>
<div class="col">
<label for="peso_item1">Peso (kg):</label>
<input type="number" id="peso_item1" class="form-control">
</div>
<div class="col">
<label for="frete_item1">Valor (R$):</label>
<input type="text" id="frete_item1" class="form-control frete-item" readonly>
</div>
</div>
</fieldset>

<fieldset class="border p-2 mt-3">
<legend class="w-auto">Item 2</legend>
<div class="form-row">
<div class="col">
<label for="altura_item2">Altura (cm):</label>
<input type="number" id="altura_item2" class="form-control">
</div>
<div class="col">
<label for="largura_item2">Largura (cm):</label>
<input type="number" id="largura_item2" class="form-control">
</div>
<div class="col">
<label for="comprimento_item2">Comprimento (cm):</label>
<input type="number" id="comprimento_item2" class="form-control">
</div>
<div class="col">
<label for="peso_item2">Peso (kg):</label>
<input type="number" id="peso_item2" class="form-control">
</div>
<div class="col">
<label for="frete_item2">Valor (R$):</label>
<input type="text" id="frete_item2" class="form-control frete-item" readonly>
</div>
</div>
</fieldset>

<div class="form-group mt-3">
<button onclick="adicionarNovoItem()" class="btn btn-primary">Adicionar novo Item</button>
<button onclick="calcular()" class="btn btn-success">Calcular</button>
</div>
</div>

<script src="script.js"></script>
</body>

</html>
85 changes: 85 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
let itemCounter = 2;

function adicionarNovoItem() {
itemCounter++;
const novoItemHTML = `
<fieldset class="border p-2 mt-3">
<legend class="w-auto">Item ${itemCounter}</legend>
<div class="form-row">
<div class="col">
<label for="altura_item${itemCounter}">Altura (cm):</label>
<input type="number" id="altura_item${itemCounter}" class="form-control">
</div>
<div class="col">
<label for="largura_item${itemCounter}">Largura (cm):</label>
<input type="number" id="largura_item${itemCounter}" class="form-control">
</div>
<div class="col">
<label for="comprimento_item${itemCounter}">Comprimento (cm):</label>
<input type="number" id="comprimento_item${itemCounter}" class="form-control">
</div>
<div class="col">
<label for="peso_item${itemCounter}">Peso (kg):</label>
<input type="number" id="peso_item${itemCounter}" class="form-control">
</div>
<div class="col">
<label for="frete_item${itemCounter}">Valor (R$):</label>
<input type="text" id="frete_item${itemCounter}" class="form-control frete-item" readonly>
</div>
</div>
</fieldset>
`;
const divNovoItem = document.createElement('div');
divNovoItem.innerHTML = novoItemHTML;
const formGroup = document.querySelector('.form-group');
formGroup.parentNode.insertBefore(divNovoItem, formGroup);
}

function calcular() {
var alturaPacote = parseFloat(document.getElementById('altura').value);
var larguraPacote = parseFloat(document.getElementById('largura').value);
var comprimentoPacote = parseFloat(document.getElementById('comprimento').value);
var pesoPacote = parseFloat(document.getElementById('peso').value);
var valorPacote = parseFloat(document.getElementById('valor_pacote').value);

var volumePacote = alturaPacote * larguraPacote * comprimentoPacote;

var volumeItens = 0;
var pesoItens = 0;
for (var i = 1; i <= itemCounter; i++) {
var alturaItem = parseFloat(document.getElementById('altura_item' + i).value);
var larguraItem = parseFloat(document.getElementById('largura_item' + i).value);
var comprimentoItem = parseFloat(document.getElementById('comprimento_item' + i).value);
var pesoItem = parseFloat(document.getElementById('peso_item' + i).value);
volumeItens += alturaItem * larguraItem * comprimentoItem;
pesoItens += pesoItem;
}

if (volumeItens > volumePacote) {
alert("Revise as medidas dos itens");
return;
}

if (pesoItens > pesoPacote) {
alert("Revise o peso dos itens");
return;
}

for (var i = 1; i <= itemCounter; i++) {
var alturaItem = parseFloat(document.getElementById('altura_item' + i).value);
var larguraItem = parseFloat(document.getElementById('largura_item' + i).value);
var comprimentoItem = parseFloat(document.getElementById('comprimento_item' + i).value);
var pesoItem = parseFloat(document.getElementById('peso_item' + i).value);

var volumeItem = alturaItem * larguraItem * comprimentoItem;
var proporcaoCubagemItem = volumeItem / volumeItens;
var proporcaoPesoItem = pesoItem / pesoItens;

var freteItem = (proporcaoCubagemItem * volumePacote + proporcaoPesoItem * pesoPacote) / (volumePacote + pesoPacote) * valorPacote;

document.getElementById('frete_item' + i).value = freteItem.toLocaleString('pt-BR', {
style: 'currency',
currency: 'BRL'
});
}
}

0 comments on commit 39d52d2

Please sign in to comment.