Created
December 20, 2016 16:44
Como acionar insert somente quando clicar no botão (maozinha).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function listaMatHistOS($conn, $rm){ | |
$HistMatOS = array(); | |
$where = []; | |
$where[] = "cliente.id = (select usuario.idCliente from usuario where usuario.login = '".usuarioLogado()."')"; | |
//$where[] = "EXISTS(SELECT usuario.id FROM usuario WHERE usuario.login = '".usuarioLogado()."' AND (cliente.id = usuario.idCliente OR usuario.funcionarioMatriz = 1))"; | |
if ($rm) { | |
$where[] = "itemMaterial.rm = '{$rm}'"; | |
} | |
$SQL = "select os.id as IdOS, os.dataHora, material.nome as equipamento, itemMaterial.rm, itemMaterial.patrimonio, | |
itemMaterial.nSerie, tipoOs.nome as TipoOS, itemMaterial.valorTotal | |
from material | |
inner join itemMaterial on | |
material.id = itemMaterial.idMaterial | |
inner join os on | |
os.idItemMaterial = itemMaterial.id | |
inner join cliente on | |
cliente.id = os.idCliente | |
inner join tipoOs on | |
tipoOs.id = os.idTipoOs | |
where " . implode(' AND ', $where) ; | |
//$SQL = utf8_decode($SQL); | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($RelMatOS = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){; | |
array_push($HistMatOS, $RelMatOS); | |
} | |
//echo $SQL; | |
return $HistMatOS; | |
} | |
function listaMateriaisEd($conn, $IdOS){ | |
$materiais = array(); | |
$where = []; | |
$where[] = "EXISTS(SELECT usuario.id FROM usuario WHERE usuario.login = '".usuarioLogado()."' AND (cliente.id = usuario.idCliente OR usuario.funcionarioMatriz = 1))"; | |
if($IdOS) { | |
$where[] = "os.id = '{$IdOS}'"; | |
} | |
$SQL = "select os.id as IdOS, material.id as IdMat, itemMaterial.id as IdItem, material.nome as Equipamento, | |
itemMaterial.rm, itemMaterial.patrimonio, itemMaterial.nSerie, itemMaterial.valorUnitario from os | |
inner join itemMaterial on itemMaterial.id = os.idItemMaterial | |
inner join material on material.id = itemMaterial.idMaterial | |
inner join cliente on cliente.id = os.idCliente | |
where " . implode(' AND ', $where) . ""; | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($material = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($materiais, $material); | |
} | |
return $materiais; | |
} | |
function PreparMateriaisInsert($conn){ | |
$materiaisInsert = array(); | |
$where = []; | |
$where[] = "material.tipo = 'consumo'"; | |
$SQL = "SELECT distinct material.id as idPeca, material.nome as nomePeca FROM material | |
inner join modelo on | |
modelo.idMaterial = material.id | |
inner join empresa on | |
empresa.id = modelo.idEmpresaFabricante | |
where " . implode(' AND ', $where) . " order by material.nome"; | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($materialInsert = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($materiaisInsert, $materialInsert); | |
} | |
return $materiaisInsert; | |
} | |
function PreparModeloInsert($conn, $idPeca = NULL ){ | |
$modelosInsert = array(); | |
$where = []; | |
$where[] = "material.tipo = 'consumo'"; | |
if($idPeca){ | |
$where[] = "material.id = '{$idPeca}'"; | |
} | |
$SQL = "SELECT distinct modelo.id as IdModelo, modelo.nome as Modelo FROM modelo | |
inner join material on | |
modelo.idMaterial = material.id | |
where " . implode(' AND ', $where) . " order by modelo.nome"; | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($ModeloInsert = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($modelosInsert, $ModeloInsert); | |
} | |
echo $SQL; | |
return $modelosInsert; | |
} | |
function PreparFabricanteInsert($conn, $IdPeca = NULL, $IdModelo = NULL){ | |
$FabricantesInsert = array(); | |
$where = []; | |
$where[] = "material.tipo = 'consumo'"; | |
if($IdPeca){ | |
$where[] = "material.id = '{$IdPeca}'"; | |
} | |
if($IdModelo){ | |
$where[] = "modelo.id = '{$IdModelo}'"; | |
} | |
$SQL = "select distinct empresa.id as IdFabricante, empresa.nomeFantasia as Fabricante | |
from empresa | |
inner join modelo on | |
modelo.idEmpresaFabricante = empresa.id | |
inner join material on | |
material.id = modelo.idMaterial | |
where " . implode(' AND ', $where) . " order by empresa.nomeFantasia"; | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($FabricanteInsert = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($FabricantesInsert, $FabricanteInsert); | |
} | |
echo $SQL; | |
return $FabricantesInsert; | |
} | |
function RelacaoPecasInseridas($conn, $IdOS = NULL){ | |
$RelacaoPecasInseridas = array(); | |
$where = []; | |
if($IdOS){ | |
$where[] = "historicoPecasOS.idOS = '{$IdOS}'"; | |
} | |
$SQL = "SELECT historicoPecasOs.id, historicoPecasOs.idOS, historicoPecasOs.idMaterial, material.nome as nomePeca, | |
historicoPecasOs.quantidade, historicoPecasOs.custoUnitario FROM historicoPecasOs | |
inner join material on | |
material.id = historicoPecasOs.idMaterial | |
where " . implode(' AND ', $where) . " | |
order by nomePeca"; | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($PecasInseridas = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($RelacaoPecasInseridas, $PecasInseridas); | |
} | |
echo $SQL; | |
return $RelacaoPecasInseridas ; | |
} | |
function inseriPecasOS($conn, $IdOS = NULL, $IdPeca = NULL, $Quant = NULL, $CustoUni = NULL){ | |
$PecasInsertOS = array(); | |
$SQL = "insert into historicoPecasOs (idOS, idMaterial, quantidade, custoUnitario) | |
values ($IdOS, $IdPeca, $Quant, $CustoUni)"; | |
$resultado = sqlsrv_query($conn, $SQL); | |
while ($InseriPeca = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($PecasInsertOS, $InseriPeca); | |
} | |
echo $SQL; | |
return $PecasInsertOS ; | |
} | |
function listaMatOS($conn, $nomeMat){ | |
$histMatOS = array(); | |
$resultado = sqlsrv_query($conn, "SELECT os.id as IdOS, material.id as idMat, material.nome as nomeMat, | |
itemMaterial.nSerie, itemMaterial.valorUnitario, os.motivoOs, os.status FROM os | |
inner join itemMaterial on | |
itemMaterial.id = os.id | |
inner join material on | |
material.id = itemMaterial.idMaterial | |
where material.nome like {$nomeMat}"); | |
while ($histMat = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){ | |
array_push($histMatOS, $histMat); | |
} | |
return $histMatOS; | |
} | |
function buscaMaterial($conn, $IdItem){ | |
$query = "select m.id as IdMat, I.id as IdItem, M.nome, M.tipo, I.nSerie ,I.valorUnitario from medx.dbo.material as M INNER JOIN medx.dbo.itemMaterial as I on M.id=I.idMaterial where I.Id = {$IdItem}"; | |
$resultado = sqlsrv_query($conn, $query) or die(print_r(sqlsrv_errors())); | |
return sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC); | |
} | |
function alteraMaterial($conn, $IdItem, $valorUnitario){ | |
$query = "update itemMaterial set valorUnitario = {$valorUnitario} where id = '{$IdItem}'"; | |
return sqlsrv_query($conn, $query); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php include 'cabecalho.php'; | |
include 'conect/conecta.php'; | |
include 'banco-material.php'; | |
include 'banco-inventario.php'; | |
include 'logica-usuario.php'; | |
?> | |
<script> | |
$(document).ready(function(){ | |
$('#DT_Mat_List').DataTable({ | |
"language": { | |
"lengthMenu": "Exibindo _MENU_ registros por páginas", | |
"zeroRecords": "Nenhum resultado encontrado", | |
"info": "Página _PAGE_ de _PAGES_", | |
"infoEmpty": "Nenhum resultado disponível", | |
"sSearch": "Pesquisar: ", | |
"infoFiltered": "(Número _MAX_ total registros)", | |
"oPaginate": { | |
"sFirst": "Início", | |
"sPrevious": "Anterior", | |
"sNext": "Próximo", | |
"sLast": "Último" | |
} | |
}}); | |
}); | |
</script> | |
<?php if(isset($_SESSION["success"])) { ?> | |
<p class="alert-success"><?= $_SESSION["success"]?></p> | |
<?php | |
unset($_SESSION["success"]); | |
} ?> | |
<form class="form-horizontal" method="post" action="edicao-MatOS.php"> | |
<div class="form-group"> | |
<div class="col-lg-1"> | |
<div class="form-group"><label for="IdOS">CÓD OS:</label> | |
<input name="IdOS" id="IdOS" class="form-control" placeholder="Digite o Cód. OS" | |
value="<?=isset($_POST['IdOS']) ? $_POST['IdOS'] : ''; ?>"> | |
</div> | |
</div> | |
<?php | |
if(isset($_POST['IdOS'])) : ?> | |
<div class="row"> | |
<!-- Inicio Fabricante --> | |
<div class="col-lg-2"><label for="Fabricante">FABRICANTE:</label> | |
<select class="form-control" id="Fabricante" name="Fabricante" > | |
<option></option> | |
<?php | |
$FabricantesInsert = PreparFabricanteInsert($conn ); | |
foreach ($FabricantesInsert as $FabricanteInsert) { | |
$selected = ''; | |
if ($_POST['Fabricante'] == $FabricanteInsert['IdFabricante']) { | |
$selected = 'selected'; | |
} | |
?> | |
<option value="<?=$FabricanteInsert['IdFabricante']?>" <?= $selected ?>> | |
<?= utf8_encode($FabricanteInsert['Fabricante']) ?> | |
</option> | |
<?php } ?> | |
</select> | |
</div> | |
<!-- Fim Fabricante --> | |
<!-- Inicio nomePeca --> | |
<div class="col-lg-2"><label for="nomePeca">PEÇA:</label> | |
<select class="form-control" id="nomePeca" name="nomePeca" > | |
<option></option> | |
<?php | |
$materiaisInsert = PreparMateriaisInsert($conn); | |
foreach ($materiaisInsert as $materialInsert) { | |
$selected = ''; | |
if ($_POST['nomePeca'] == $materialInsert['idPeca']) { | |
$selected = 'selected'; | |
} | |
?> | |
<option value="<?=$materialInsert['idPeca']?>" <?= $selected ?>> | |
<?= utf8_encode($materialInsert['nomePeca']) ?> | |
</option> | |
<?php } ?> | |
</select> | |
</div> | |
<!-- Fim nomePeca --> | |
<!-- Inicio Modelo --> | |
<div class="col-lg-2"><label for="Modelo">MODELO:</label> | |
<select class="form-control" id="Modelo" name="Modelo" > | |
<option></option> | |
<?php | |
$modelosInsert = PreparModeloInsert($conn, isset($_POST['nomePeca']) ? $_POST['nomePeca'] : null ); | |
foreach ($modelosInsert as $ModeloInsert) { | |
$selected = ''; | |
if ($_POST['Modelo'] == $ModeloInsert['idModelo']) { | |
$selected = 'selected'; | |
} | |
?> | |
<option value="<?=$ModeloInsert['idModelo']?>" <?= $selected ?>> | |
<?= utf8_encode($ModeloInsert['Modelo']) ?> | |
</option> | |
<?php } ?> | |
</select> | |
</div> | |
<!-- Fim Modelo --> | |
<div class="col-lg-1"><label for="Quant">QUANT:</label> | |
<input type="number" name="Quant" id="Quant" class="form-control" min="0" ></div> | |
<div class="col-lg-1"><label for="custoUnit">CUSTO:</label> | |
<input type="number" min="0" id="currency" name="custoUnit" step="0.01" class="form-control"/></div> | |
<label> </label> | |
<button alt="Inserir Peça na OS" title="Click para Inserir Peças na OS" class="btn btn-warning pull-left"><span class="glyphicon glyphicon-hand-down" ></span></button> | |
<!-- Insert de dados vindo do select --> | |
<?php | |
$IdOS = isset($_POST['IdOS']) ? $_POST['IdOS'] : null; | |
$IdPeca = isset($_POST['nomePeca']) ? $_POST['nomePeca'] : null; | |
if ($IdPeca) { | |
$Quant = $_POST['Quant']; | |
$CustoUni = $_POST['custoUnit']; | |
inseriPecasOS($conn, $IdOS, $IdPeca, $Quant, $CustoUni); | |
} | |
?> | |
</div> | |
<?php endif; ?> | |
<div class="col-lg-1 pull-right"><button class="btn btn-warning">Buscar</button></div> | |
</div> | |
<?php | |
if(isset($_POST['IdOS'])) : ?> | |
<table class="display table table-bordered table-condensed table-hover table-striped" cellspacing="0" width="100%"> | |
<thead> | |
<tr> | |
<th>NOME</th> | |
<th>RM</th> | |
<th>PATRIMÔNIO</th> | |
<th>N. SÉRIE</th> | |
<th>CUSTO</th> | |
<th>AÇÕES</th> | |
</tr> | |
</thead> | |
<?php | |
$materiais = listaMateriaisEd($conn, isset($_POST['IdOS']) ? $_POST['IdOS'] : null); | |
foreach ($materiais as $material): ?> | |
<tr> | |
<td><?=utf8_encode($material['Equipamento']); ?></td> | |
<td><?=utf8_encode($material['rm']); ?></td> | |
<td><?=utf8_encode($material['patrimonio']); ?></td> | |
<td><?=utf8_encode($material['nSerie']); ?></td> | |
<td><?="R$ ".number_format($material['valorUnitario'], 2, ',', '.') ; ?></td> | |
<td> | |
<!-- <a href="" class="btn btn-warning">Incluir Peça</a>--> | |
<a href="material-altera-formulario.php?IdItem=<?=$material['IdItem']; ?>" class="btn btn-warning">Alterar</a> | |
</td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
<?php endif ?> | |
<br /> | |
<?php | |
if(isset($_POST['IdOS']) and isset($_POST['IdMaterial']) ) : ?> | |
<table class="table table-striped table-bordered"> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>NOME</th> | |
<th>QUANT</th> | |
<th>CUSTO</th> | |
</tr> | |
</thead> | |
<?php | |
$RelacaoPecasInseridas = RelacaoPecasInseridas($conn, isset($_POST['IdOS']) ? $_POST['IdOS'] : null ); | |
foreach ($RelacaoPecasInseridas as $PecasInseridas): ?> | |
<tr> | |
<td><?=$PecasInseridas['idMaterial']?></td> | |
<td><?= utf8_encode($PecasInseridas['nomePeca'])?></td> | |
<td><?=$PecasInseridas['quantidade']?></td> | |
<td><?="R$ ".number_format($PecasInseridas['custoUnitario'],2, ',', '.')?></td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
<?php endif; ?> | |
</form> | |
<br /> | |
<br /> | |
<?php include 'rodape.php'; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment