diff --git a/src/db/templates.js b/src/db/templates.js index f629d86..cbb31cc 100644 --- a/src/db/templates.js +++ b/src/db/templates.js @@ -98,7 +98,7 @@ const getProblemas = async (id) => { const getListaTarefas = async (id) => { let data = await db.query( - "select tarefa.descricao from tarefa,entrada,checklist where entrada.id_checklist = checklist.id_checklist and tarefa.id_checklist = checklist.id_checklist and entrada.cliente = $1", [id] + "select tarefa.descricao from tarefa,entrada,checklist where entrada.id_checklist = checklist.id_checklist and tarefa.id_checklist = checklist.id_checklist and entrada.cliente = $1", [id] ) .catch(e => console.error(e.stack)) @@ -306,6 +306,15 @@ const aprovarOrcamentoIdVeiculo = async (date, time, idCliente, idVeiculo) => { return data.rows ? data.rows : null } +//Adicioanr orcamento +const adicionarOrcamento = async (valor, idCliente, idVeiculo) => { + let data = await db.query( + "INSERT INTO orcamento (id_orcamento, valor, descricao, id_cliente, id_veiculo, aprovacao) VALUES (default, $1, 'Os problemas que nos comunicou à chegada estão a ser resolvidos neste momento', $2, $3, '0')", [valor, idCliente, idVeiculo] + ) + .catch(e => console.error(e.stack)) + + return data.rows ? data.rows : null +} // --------------------------------------------------------------------------------------------- // Admin // --------------------------------------------------------------------------------------------- @@ -383,5 +392,6 @@ module.exports = { getClientes, getChecklists, addEntrada, - addCliente + addCliente, + adicionarOrcamento } \ No newline at end of file diff --git a/src/routers/roleRoute.js b/src/routers/roleRoute.js index bcb696e..a52675f 100644 --- a/src/routers/roleRoute.js +++ b/src/routers/roleRoute.js @@ -6,10 +6,10 @@ const saltRounds = 10 const { getWorkviewInfo, getTarefasCompletas, getdetalhesOrcamento, getProblemas, getListaTarefas, getFuncionarioByUsername, - getClientByLink, getVeiculoById, getNomeCliente, aproletOrcamento, + getClientByLink, getVeiculoById, getNomeCliente, aprovarOrcamento, getVeiculosByFuncionario, getTarefasVeiculo, maxIDVeiculo, getVeiculo, getIdChecklistByEntrada, adicionarTarefa, maxIDTarefa, getListaMecanicos, - getTarefasIncompletas, markTaskAsCompleted, getListaVeiculos, adicionarVeiculo, adicionarFuncionario, getFuncionario, editFuncionario, getLogin, getClientIdByEmail, aproletOrcamentoIdVeiculo, getVeiculoIdByPlate, newChecklist, getClientes, getChecklists, addEntrada, addCliente } = require('../db/templates') + getTarefasIncompletas, markTaskAsCompleted, getListaVeiculos, adicionarVeiculo, adicionarFuncionario, getFuncionario, editFuncionario, getLogin, getClientIdByEmail, aprovarOrcamentoIdVeiculo, getVeiculoIdByPlate, newChecklist, getClientes, getChecklists, addEntrada, addCliente, adicionarOrcamento} = require('../db/templates') //-------------------------------------------------------------------- // Mecanico ---------------------------------------------------------- @@ -403,7 +403,7 @@ router.get('/getAllTarefas', async (req, res) => { }) //Gets info about cars without a mecanic and sends it to responsavel.hbs -router.post('/aproletOrcamentos', async (req, res) => { +router.post('/aprovarOrcamentos', async (req, res) => { const clientEmail = req.body.email const matriculaCarro = req.body.matricula @@ -441,7 +441,25 @@ router.post('/aproletOrcamentos', async (req, res) => { let idVeiculo = await getVeiculoIdByPlate(matriculaCarro) idVeiculo = idVeiculo.id_veiculo - const submitDetails = await aproletOrcamentoIdVeiculo(today, currentTime, idCliente, idVeiculo) + const submitDetails = await aprovarOrcamentoIdVeiculo(today, currentTime, idCliente, idVeiculo) + + res.redirect('/responsavel/' + req.body.nomeAdmin) +}) + +//adds new value to orcamento +router.post('/adicionarOrcamentos', async (req, res) => { + + const clientEmail = req.body.email2 + const matriculaCarro = req.body.matricula2 + const valor = req.body.preco + + //Get client and veiculo id + let idCliente = await getClientIdByEmail(clientEmail) + idCliente = idCliente.id_cliente + let idVeiculo = await getVeiculoIdByPlate(matriculaCarro) + idVeiculo = idVeiculo.id_veiculo + + const submitDetails = await adicionarOrcamento(valor, idCliente, idVeiculo) res.redirect('/responsavel/' + req.body.nomeAdmin) }) @@ -632,7 +650,7 @@ router.post('/orcamentoAprovado/:link', async (req, res) => { let idCliente = await getClientByLink(urlCode) idCliente = idCliente[0].cliente - const submitDetails = await aproletOrcamento(today, currentTime, idCliente) + const submitDetails = await aprovarOrcamento(today, currentTime, idCliente) res.render('aproveSuccess') }) diff --git a/views/responsavel.hbs b/views/responsavel.hbs index ede14ec..c8c3a78 100644 --- a/views/responsavel.hbs +++ b/views/responsavel.hbs @@ -32,6 +32,7 @@
{{user}}
{{matriculasEspera}}
{{matriculasTrabalho}}
@@ -394,6 +439,7 @@ let select = document.getElementById("email"), arr = jsonData1 + //Add to aceitar for (let i = 0; i < arr.length; i++) { let option = document.createElement("OPTION"), txt = document.createTextNode(arr[i].email); @@ -402,9 +448,21 @@ option.setAttribute("value", arr[i].email); select.insertBefore(option, select.lastChild); } + + //Add to adicionar + let select2 = document.getElementById("email2"), arr2 = jsonData1 + + for (let i = 0; i < arr2.length; i++) { + let option2 = document.createElement("OPTION"), + txt2 = document.createTextNode(arr2[i].email); + option2.appendChild(txt2); + option2.setAttribute("id", "botaoAddClient2" + [i + 1]) + option2.setAttribute("value", arr2[i].email); + select2.insertBefore(option2, select2.lastChild); + } } ).catch((e) => { - console.log('Couldnt get data from server') + console.log('Couldnt get data from server - cliente') }) //Fetch veiculos to show on the modal @@ -414,17 +472,33 @@ jsonData2 = await result.json(); //console.log(jsonData2) + //Add to aceitar let select = document.getElementById("matricula"), arr = jsonData2 for (let i = 0; i < arr.length; i++) { let option = document.createElement("OPTION"), txt = document.createTextNode(arr[i].matricula); option.appendChild(txt); + option.setAttribute("id", "botaoAddVeiculo" + [i + 1]) option.setAttribute("value", arr[i].matricula); select.insertBefore(option, select.lastChild); } + + //Add to adicionar + let select2 = document.getElementById("matricula2"), arr2 = jsonData2 + + for (let i = 0; i < arr2.length; i++) { + let option2 = document.createElement("OPTION"), + txt2 = document.createTextNode(arr2[i].matricula); + option2.appendChild(txt2); + option2.setAttribute("id", "botaoAddVeiculo2" + [i + 1]) + option2.setAttribute("value", arr2[i].matricula); + select2.insertBefore(option2, select2.lastChild); + } } - ) + ).catch((e) => { + console.log('Couldnt get data from server - veiculo') + })