
<?php
    require 'conexion.php';
    require_once('functions.php');
	include(HEADER_TEMPLATE); 
    
	$id_pedido = $_GET['id'];
	$id_grade = $_GET['id_grade'];
    
    // seleciona todos os registros da base com id_grade igual ao do post que veio do formulario
    
?>

            <div class="row">
				<h3 style="text-align:center">Adiciona Grade do PEDIDO</h3>
			</div>

			<br>
            <?php 
                $sql = "SELECT * FROM grade WHERE id_grade = '$id_grade'";
                $resultado4 = $mysqli->query($sql);
                $row = $resultado4->fetch_array(MYSQLI_ASSOC);    
            ?>  
			

			<form name="gradePedido" method="POST" action="grade_update.php" autocomplete="off">

				<input type="hidden" id="id_gradez" name="id_gradez" value="<?php echo $row['id_grade']; ?>" />
				<input type="hidden" id="id_pedidoz" name="id_pedidoz" value="<?php echo $row['id_pedido']; ?>" />
                <input type="hidden" id="type" name="type" value="O" />
                <input type="hidden" id="ativo" name="ativo" value="1" />
                <input type="hidden" id="dir" name="dir" value="1" />


                <div class="container">
                    <div class="form-group col-sm-2">
                        <label for="xtda">ID Pedido</label>
                        <input type="text" class="form-control" id="id_pedido" name="id_pedido" value="<?php echo $id_pedido; ?>" readonly="readonly">
                    </div>
                    <div class="form-group col-md-2">
                        <label for="titulo">Titulo Grade</label>
                        <input type="text" class="form-control" id="titulo" name="titulo" value="<?php echo $row['titulo'] ?>" required autofocus style='text-transform:uppercase' >
                    </div>
                    <div class="form-group col-md-7">
                        <label for="obs_grade">Observações da grade</label>
                        <input type="text" class="form-control" id="obs_grade" name="obs_grade" value="<?php echo $row['obs_grade'] ?>" style='text-transform:uppercase' >
                    </div>
                    <div class="form-group col-sm-1">
                        <label for="ajuste">% <a href="javascript:window.location.reload(true)">reload</a></label>
                        <input type="number" class="form-control" id="ajuste" name="ajuste" value="<?php echo $row['ajuste'] ?>" onBlur="porcent();" >

                    </div>
                    
                </div>
                
                <div class="container">  
                    <br />  
                    <div class="table-responsive">  
                        <table id="gradePedidoz" class="table table-striped table-condensed table-bordered">  
                              <thead>  
                                   <tr style="background-color: #d3d3d3" align="center">  
                                        <td width="10%">GRADE</td>  
                                        <td>32</td>  
                                        <td>34</td>  
                                        <td>36</td>  
                                        <td>38</td>  
                                        <td>40</td>  
                                        <td>42</td>  
                                        <td>44</td>  
                                        <td>46</td>  
                                        <td>48</td>  
                                        <td>50</td>  
                                        <td>52</td>  
                                        <td>SUBTOTAL</td>  
                                    </tr>  
                              </thead>  
                              <?php 
                              $query = "SELECT (tda+tqa+tsa+toa+qua+qda+qqa+qsa+qoa+cia+cda) as subtot, SUBSTRING(grade.obs_grade,16,100) as nome_risco, companies.popular_name, grade.*, pedidos.id, pedidos.modelo, pedidos.modelagem, pedidos.nr_company, pedidos.id_piloto, pedidos.revista, pedidos.dtcadastro, pedidos.data_pre, pedidos.data_pedido,pedidos.data_entrega,pedidos.nr_pedido, pedidos.referencia, pedidos.descricao, pedidos.descricao, pedidos.quantidade, pedidos.tecido, pedidos.dt_baixado, pedidos.obs AS obsPed, pedidos.ativo FROM grade RIGHT OUTER JOIN pedidos ON pedidos.id = grade.id_pedido LEFT JOIN companies on companies.id = pedidos.nr_company WHERE pedidos.id = '$id_pedido'  AND grade.dir = '1'";
                              $resultado = $mysqli->query($query);                          
                              while($row = mysqli_fetch_array($resultado))  
                              {  
                                   echo '  
                                   <tr align="center">  
                                        <td width="10%">'.$row["titulo"].'</td>  
                                        <td>'.$row["tda"].'</td>  
                                        <td>'.$row["tqa"].'</td>  
                                        <td>'.$row["tsa"].'</td>  
                                        <td>'.$row["toa"].'</td>  
                                        <td>'.$row["qua"].'</td>  
                                        <td>'.$row["qda"].'</td>  
                                        <td>'.$row["qqa"].'</td>  
                                        <td>'.$row["qsa"].'</td>  
                                        <td>'.$row["qoa"].'</td>  
                                        <td>'.$row["cia"].'</td>  
                                        <td>'.$row["cda"].'</td>  
                                        <td>'.$row["subtot"].'</td>  
                                   </tr>  
                                   ';  
                              }  
                              ?>  
                              <?php 
                              $query2 = "SELECT SUM(tda) as stda, SUM(tqa) as stqa, SUM(tsa) as stsa, SUM(toa) as stoa, SUM(qua) as squa, SUM(qda) as sqda, SUM(qqa) as sqqa, SUM(qsa) as sqsa, SUM(qoa) as sqoa,SUM(cia) as scia,SUM(cda) as scda, (SUM(tda) + SUM(tqa) + SUM(tsa) + SUM(toa) + SUM(qua) + SUM(qda) + SUM(qqa) + SUM(qsa) + SUM(qoa) + SUM(cia) + SUM(cda) ) as totalzao FROM grade WHERE grade.id_pedido = '$id_pedido' AND grade.dir = '1'";
                              $resultado2 = $mysqli->query($query2);
                              	$row2 = $resultado2->fetch_array(MYSQLI_ASSOC);
                              {  
                                   echo '  
                                   <tr style="background-color: #d3d3d3" align="center">  
                                        <td width="10%">TOTAL GRADE</td>  
                                        <td>'.$row2["stda"].'</td>  
                                        <td>'.$row2["stqa"].'</td>  
                                        <td>'.$row2["stsa"].'</td>  
                                        <td>'.$row2["stoa"].'</td>  
                                        <td>'.$row2["squa"].'</td>  
                                        <td>'.$row2["sqda"].'</td>  
                                        <td>'.$row2["sqqa"].'</td>  
                                        <td>'.$row2["sqsa"].'</td>  
                                        <td>'.$row2["sqoa"].'</td>  
                                        <td>'.$row2["scia"].'</td>  
                                        <td>'.$row2["scda"].'</td>  
                                        <td>'.$row2["totalzao"].'</td>  
                                   </tr>  
                                   ';  
                              }  
                              ?>  
                            <?php 
                                $resultado4 = $mysqli->query($sql);
                                $row = $resultado4->fetch_array(MYSQLI_ASSOC);    
                            ?>  
                            <form name="gradePedido" method="POST" action="grade_update.php" autocomplete="off">
                            
                               <tr align="center" style="font-weight:bold">  
                                   <td width="10%">
                                        <input type="text"  style="text-align:center;" id="titulo" name="titulo" value="<?php echo $row['titulo'] ?>" onkeypress='return SomenteNumero(event)' readonly="readonly">  
                                   </td>  
                                   <td> 
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="tda" name="tda" value="<?php echo $row['tda'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td> 
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="tqa" name="tqa" value="<?php echo $row['tqa'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="tsa" name="tsa" value="<?php echo $row['tsa'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="toa" name="toa" value="<?php echo $row['toa'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="qua" name="qua" value="<?php echo $row['qua'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="qda" name="qda" value="<?php echo $row['qda'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="qqa" name="qqa" value="<?php echo $row['qqa'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="qsa" name="qsa" value="<?php echo $row['qsa'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="qoa" name="qoa" value="<?php echo $row['qoa'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="cia" name="cia" value="<?php echo $row['cia'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="cda" name="cda" value="<?php echo $row['cda'] ?>" onkeypress='return SomenteNumero(event)' onBlur="soma();">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3" style="text-align:center;" id="subtotal" name="subtotal" value="<?php echo $row['subtotal'] ?>" readonly="readonly">  
                                   </td>     
                               </tr>  

                               <tr align="center" style="background-color: #d3d3d3">  
                                   <td width="10%">
                                    Soma Futura
                                   </td>  
                                   <td> 
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somatda" name="somatda" readonly="readonly">  
                                   </td>  
                                   <td> 
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somatqa" name="somatqa" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somatsa" name="somatsa" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somatoa" name="somatoa" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somaqua" name="somaqua" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somaqda" name="somaqda" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somaqqa" name="somaqqa" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somaqsa" name="somaqsa" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somaqoa" name="somaqoa" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somacia" name="somacia" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somacda" name="somacda" readonly="readonly">  
                                   </td>  
                                   <td>  
                                        <input type="text" size ="2" width="1" maxlength="3"  style="text-align:center;" id="somageral" name="somageral" readonly="readonly">  
                                   </td>     
                               </tr>  
                                   
                                   
                            </form>
                        </table>
                    </div>  
                    
                        <div class="row">                
                            <div class="form-group">
                                <div class="col-xs-6 col-xs-offset-3 col-sm-4 col-sm-offset-0">
                                    <a href="pedido_index.php" class="btn btn-primary">Voltar</a>
                                    <button type="submit" class="btn btn-danger">ATUALIZA GRADE</button>
                                </div>
                            </div>
                        </div>

                </div>            
            </form>  

 
<script language='JavaScript'>
function soma() 
{
gradePedido.subtotal.value = (gradePedido.tda.value*1) + (gradePedido.tqa.value*1) + (gradePedido.tsa.value*1) + (gradePedido.toa.value*1) + (gradePedido.qua.value*1) + (gradePedido.qda.value*1) + (gradePedido.qqa.value*1) + (gradePedido.qsa.value*1) + (gradePedido.qoa.value*1) + (gradePedido.cia.value*1) + (gradePedido.cda.value*1)

var stda = "<?php echo $row2["stda"]; ?>";
var tda = "<?php echo $row["tda"]; ?>";

var stqa = "<?php echo $row2["stqa"]; ?>";
var tqa = "<?php echo $row["tqa"]; ?>";

var stsa = "<?php echo $row2["stsa"]; ?>";
var tsa = "<?php echo $row["tsa"]; ?>";

var stoa = "<?php echo $row2["stoa"]; ?>";
var toa = "<?php echo $row["toa"]; ?>";

var squa = "<?php echo $row2["squa"]; ?>";
var qua = "<?php echo $row["qua"]; ?>";

var sqda = "<?php echo $row2["sqda"]; ?>";
var qda = "<?php echo $row["qda"]; ?>";

var sqqa = "<?php echo $row2["sqqa"]; ?>";
var qqa = "<?php echo $row["qqa"]; ?>";

var sqsa = "<?php echo $row2["sqsa"]; ?>";
var qsa = "<?php echo $row["qsa"]; ?>";

var sqoa = "<?php echo $row2["sqoa"]; ?>";
var qoa = "<?php echo $row["qoa"]; ?>";

var scia = "<?php echo $row2["scia"]; ?>";
var cia = "<?php echo $row["cia"]; ?>";

var scda = "<?php echo $row2["scda"]; ?>";
var cda = "<?php echo $row["cda"]; ?>";

gradePedido.somatda.value =  (eval(stda) - eval(tda)) + eval((gradePedido.tda.value*1)) 
gradePedido.somatqa.value =  (eval(stqa) - eval(tqa)) + eval((gradePedido.tqa.value*1)) 
gradePedido.somatsa.value =  (eval(stsa) - eval(tsa)) + eval((gradePedido.tsa.value*1))
gradePedido.somatoa.value =  (eval(stoa) - eval(toa)) + eval((gradePedido.toa.value*1))
gradePedido.somaqua.value =  (eval(squa) - eval(qua)) + eval((gradePedido.qua.value*1))
gradePedido.somaqda.value =  (eval(sqda) - eval(qda)) + eval((gradePedido.qda.value*1))
gradePedido.somaqqa.value =  (eval(sqqa) - eval(qqa)) + eval((gradePedido.qqa.value*1))
gradePedido.somaqsa.value =  (eval(sqsa) - eval(qsa)) + eval((gradePedido.qsa.value*1))
gradePedido.somaqoa.value =  (eval(sqoa) - eval(qoa)) + eval((gradePedido.qoa.value*1))
gradePedido.somacia.value =  (eval(scia) - eval(cia)) + eval((gradePedido.cia.value*1))
gradePedido.somacda.value =  (eval(scda) - eval(cda)) + eval((gradePedido.cda.value*1))

gradePedido.somageral.value = eval(gradePedido.somatda.value) + eval(gradePedido.somatqa.value) + eval(gradePedido.somatsa.value) 
+ eval(gradePedido.somatoa.value) + eval(gradePedido.somaqua.value) + eval(gradePedido.somaqda.value) + eval(gradePedido.somaqqa.value) 
+ eval(gradePedido.somaqsa.value) + eval(gradePedido.somaqoa.value) + eval(gradePedido.somacia.value) + eval(gradePedido.somacda.value) 
}

</script>

<script language='JavaScript'>
function porcent() 
{

gradePedido.tda.value = Math.round((gradePedido.tda.value*1) + ((gradePedido.tda.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.tqa.value = Math.round((gradePedido.tqa.value*1) + ((gradePedido.tqa.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.tsa.value = Math.round((gradePedido.tsa.value*1) + ((gradePedido.tsa.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.toa.value = Math.round((gradePedido.toa.value*1) + ((gradePedido.toa.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.qua.value = Math.round((gradePedido.qua.value*1) + ((gradePedido.qua.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.qda.value = Math.round((gradePedido.qda.value*1) + ((gradePedido.qda.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.qqa.value = Math.round((gradePedido.qqa.value*1) + ((gradePedido.qqa.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.qsa.value = Math.round((gradePedido.qsa.value*1) + ((gradePedido.qsa.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.qoa.value = Math.round((gradePedido.qoa.value*1) + ((gradePedido.qoa.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.cia.value = Math.round((gradePedido.cia.value*1) + ((gradePedido.cia.value*1) * (gradePedido.ajuste.value*1) /100 ))
gradePedido.cda.value = Math.round((gradePedido.cda.value*1) + ((gradePedido.cda.value*1) * (gradePedido.ajuste.value*1) /100 ))

gradePedido.subtotal.value = (gradePedido.tda.value*1) + (gradePedido.tqa.value*1) + (gradePedido.tsa.value*1) + (gradePedido.toa.value*1) + (gradePedido.qua.value*1) + (gradePedido.qda.value*1) + (gradePedido.qqa.value*1) + (gradePedido.qsa.value*1) + (gradePedido.qoa.value*1) + (gradePedido.cia.value*1) + (gradePedido.cda.value*1)

var stda = "<?php echo $row2["stda"]; ?>";
var tda = "<?php echo $row["tda"]; ?>";
gradePedido.somatda.value =  (eval(stda) - eval(tda)) + eval((gradePedido.tda.value*1)) 

var stqa = "<?php echo $row2["stqa"]; ?>";
var tqa = "<?php echo $row["tqa"]; ?>";
gradePedido.somatqa.value =  (eval(stqa) - eval(tqa)) + eval((gradePedido.tqa.value*1)) 

var stsa = "<?php echo $row2["stsa"]; ?>";
var tsa = "<?php echo $row["tsa"]; ?>";
gradePedido.somatsa.value =  (eval(stsa) - eval(tsa)) + eval((gradePedido.tsa.value*1))

var stoa = "<?php echo $row2["stoa"]; ?>";
var toa = "<?php echo $row["toa"]; ?>";
gradePedido.somatoa.value =  (eval(stoa) - eval(toa)) + eval((gradePedido.toa.value*1))

var squa = "<?php echo $row2["squa"]; ?>";
var qua = "<?php echo $row["qua"]; ?>";
gradePedido.somaqua.value =  (eval(squa) - eval(qua)) + eval((gradePedido.qua.value*1))

var sqda = "<?php echo $row2["sqda"]; ?>";
var qda = "<?php echo $row["qda"]; ?>";
gradePedido.somaqda.value =  (eval(sqda) - eval(qda)) + eval((gradePedido.qda.value*1))

var sqqa = "<?php echo $row2["sqqa"]; ?>";
var qqa = "<?php echo $row["qqa"]; ?>";
gradePedido.somaqqa.value =  (eval(sqqa) - eval(qqa)) + eval((gradePedido.qqa.value*1))

var sqsa = "<?php echo $row2["sqsa"]; ?>";
var qsa = "<?php echo $row["qsa"]; ?>";
gradePedido.somaqsa.value =  (eval(sqsa) - eval(qsa)) + eval((gradePedido.qsa.value*1))

var sqoa = "<?php echo $row2["sqoa"]; ?>";
var qoa = "<?php echo $row["qoa"]; ?>";
gradePedido.somaqoa.value =  (eval(sqoa) - eval(qoa)) + eval((gradePedido.qoa.value*1))

var scia = "<?php echo $row2["scia"]; ?>";
var cia = "<?php echo $row["cia"]; ?>";
gradePedido.somacia.value =  (eval(scia) - eval(cia)) + eval((gradePedido.cia.value*1))

var scda = "<?php echo $row2["scda"]; ?>";
var cda = "<?php echo $row["cda"]; ?>";
gradePedido.somacda.value =  (eval(scda) - eval(cda)) + eval((gradePedido.cda.value*1))

}


</script>

<script language='JavaScript'>
function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;   
    if((tecla>47 && tecla<58)) return true;
    else{
    	if (tecla==8 || tecla==0) return true;
	else  return false;
    }
}
</script>



<?php include(FOOTER_TEMPLATE); ?>       
  
