Cómo Construir un Sistema de Gestión de Inventarios con Apps Script y HTML
function doGet(){
return HtmlService.createHtmlOutputFromFile("index");
}
function addReferencia(descripcion, color, talla, cantidad){
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Existencias");
const data = sheet. getDataRange().getValues();
const referencia = descripcion + " " + color + " " + talla;
const lastRow = sheet.getLastRow() + 1;
const consecutivo = "REF-" + lastRow;
sheet.getRange(lastRow, 1).setValue(consecutivo);
sheet.getRange(lastRow, 2).setValue(referencia);
sheet.getRange(lastRow, 3).setValue(descripcion);
sheet.getRange(lastRow, 4).setValue(color);
sheet.getRange(lastRow, 5).setValue(talla);
sheet.getRange(lastRow, 6).setValue(cantidad);
}
function getReferencias(){
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Existencias");
const referencias = sheet.getRange("B2:B" + sheet.getLastRow()).getValues();
return referencias.map(function(row){
return row[0];
});
}
function registrarMovimiento(referencia, tipoMovimiento, cantidad){
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Movimientos");
const fecha = new Date();
sheet.appendRow([fecha, referencia, tipoMovimiento, cantidad]);
const inventorSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Existencias");
const data = inventorSheet.getDataRange().getValues();
for(var i = 1; i<data.length; i++){
if(data[i][1] == referencia){
if(tipoMovimiento == "entrada"){
data[i][5] += parseInt(cantidad);
}else if(tipoMovimiento == "salida"){
data[i][5] -= parseFloat(cantidad);
}
inventorSheet.getRange(i + 1, 6).setValue(data[i][5]);
break;
}
}
}
Código HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<title>Sistema de Inventarios</title>
</head>
<body>
<h1>Sistemas de Inventarios</h1>
<!-- Formulario para agregar referencias -->
<h2>Crear Referencias</h2>
<form id="refForm">
<label for="descripcion">Descripción:</label>
<input type="text" id="descripcion" required><br>
<label for="color">Color:</label>
<input type="text" id="color" required><br>
<label for="talla">Talla:</label>
<input type="text" id="talla" required><br>
<label for="cantidad">Cantidad:</label>
<input type="number" id="cantidad" required><br>
<button type="submit">Agregar Referencia</button>
</form>
<!-- Formulario para registrar movimientos -->
<h2 class="section-title">Hacer Movimientos</h2>
<form id="movimientoForm">
<label for="referencia">Referencia:</label>
<select id="referencia" required></select><br>
<label for="tipoMovimiento">Tipo de Movimiento:</label>
<select id="tipoMovimiento" required>
<option value="entrada">Entrada</option>
<option value="salida">Salida</option>
</select><br>
<label for="cantidadMovimiento">Cantidad:</label>
<input type="number" id="cantidadMovimiento" required><br>
<button type="submit">Registrar Movimiento</button>
</form>
<script>
google.script.run.withSuccessHandler(function(referencias){
const referenciasDropdown = document.getElementById("referencia");
referencias.forEach(function(referencia){
const option = document.createElement("option");
option.value = referencia;
option.text = referencia;
referenciasDropdown.appendChild(option);
});
}).getReferencias();
document.getElementById("refForm").addEventListener("submit", function(e){
e.preventDefault();
const descripcion = document.getElementById("descripcion").value;
const color = document.getElementById("color").value;
const talla = document.getElementById("talla").value;
const cantidad = document.getElementById("cantidad").value;
const referencia = descripcion + " " + color + " " + talla;
const refereciaExistente = document.getElementById("referencia");
for(var i = 0; i<refereciaExistente.options.length; i++){
if(refereciaExistente.options[i].text === referencia){
showErrorPopup("Error: la referencia ya existe");
return;
}
}
google.script.run.withSuccessHandler(function(result){
showSuccessPopup(result);
}).addReferencia(descripcion, color, talla, cantidad);
document.getElementById("refForm").reset();
});
movimientoForm.addEventListener("submit", function(e){
e.preventDefault();
google.script.run.registrarMovimiento(
document.getElementById("referencia").value,
document.getElementById("tipoMovimiento").value,
document.getElementById("cantidadMovimiento").value
);
movimientoForm.reset();
});
</script>
</body>
</html>
Comentarios
Publicar un comentario