<?php
	include 'PDF_plantilla.php';
	require 'conexion.php';
    
	$id = $_GET['id'];
    
    date_default_timezone_set('America/Sao_Paulo');
        
    $query = "SELECT DATE_FORMAT(pedidos.enviado_oc, '%d/%m/%Y %H:%i:%s') AS xenviado_oc, pedidos.nr_pedido, ordem_corte.*,grade.*, companies.popular_name, pedidos.*, pedidos.obs AS obsPed FROM pedidos INNER JOIN companies on companies.id = pedidos.nr_company LEFT JOIN grade ON grade.id_pedido = pedidos.id INNER JOIN ordem_corte ON ordem_corte.id_pedido = pedidos.id WHERE grade.id_pedido = '2' and grade.dir = '1'";
	$resultado = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    
    
   	$pdf = new PDF();
	$pdf->AliasNbPages();
	$pdf->AddPage(); 
    
    //cabeçalho

    $pdf->SetFont('Arial','B',15);
    $pdf->Cell(30);
    $pdf->Cell(120,10,"ORDEM DE CORTE",0,0,'C');
    
    $pdf->Ln(15);
            
    //cabeçalho da primeira linha
    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"ORDEM DE CORTE :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,$row['id_oc'],0);
    
    $pdf->Ln();

    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"DATA/HORA :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,$row['xenviado_oc'],0);
    
    $pdf->Ln();

    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"CLIENTE :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,utf8_decode($row['popular_name']),0,0,'');

    $pdf->Ln();

    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"MODELAGEM :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,utf8_decode($row['modelagem']),0,0,'');    

    $pdf->Ln();
    
    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"MODELO :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,utf8_decode($row['modelo']),0,0,'');    
    $pdf->Ln();

    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"PEDIDO :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,$row['nr_pedido'],0);
    
    $pdf->Ln();

    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"TECIDO :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,utf8_decode($row['tecido']),0,0,'');    
    $pdf->Ln();

    $pdf->SetFont('arial','',12);
    $pdf->Cell(45,6,"LARGURA :",0,0,'R');
    $pdf->SetFont('arial','B',12);
    $pdf->Cell(35,6,$row['tamanho'],0);
    
    $pdf->Ln();
    $pdf->Ln();
    
    $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'  AND grade.dir = '1'";
	$resultado2 = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    

        $pdf->SetFont('arial','B',10);
        $pdf->Cell(190,8,utf8_decode("PEDIDO"),0,0,'C');        
        
        $pdf->Ln();   
        $pdf->SetDrawColor(160,160,160);    
        $pdf->SetFont('courier','',8);
		$pdf->Cell(28,7,"GRADE",1,0,'C');        
		$pdf->Cell(14,7,"32",1,0,'C');
		$pdf->Cell(14,7,"34",1,0,'C');
		$pdf->Cell(14,7,"36",1,0,'C');
		$pdf->Cell(14,7,"38",1,0,'C');
		$pdf->Cell(14,7,"40",1,0,'C');
		$pdf->Cell(14,7,"42",1,0,'C');
		$pdf->Cell(14,7,"44",1,0,'C');
		$pdf->Cell(14,7,"46",1,0,'C');
		$pdf->Cell(14,7,"48",1,0,'C');
		$pdf->Cell(14,7,"50",1,0,'C');
		$pdf->Cell(14,7,"52",1,0,'C');
		$pdf->Cell(14,7,"TOTAL",1,0,'C');
        

	while($row = $resultado2->fetch_assoc())
	{
        $pdf->Ln();           
        $pdf->SetFont('courier','',8);
        $pdf->Cell(28,7, $row['nome_risco'],1,0,'C');  
        $pdf->SetFont('courier','B',11);
        $pdf->Cell(14,7, $row['tda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['tqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['tsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['toa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qua'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['cia'],1,0,'C');                           
        $pdf->Cell(14,7, $row['cda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['subtot'],1,0,'C');  
	}

    $pdf->Ln();  


    // linha de totalização do Pedido
    $query = "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' AND grade.dir = '2' and grade.titulo = 'FORRO'";
	$resultado3 = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    

	while($row = $resultado3->fetch_assoc())
	{

        $pdf->SetFont('courier','',8);
		$pdf->Cell(28,7,utf8_decode("TOTALIZAÇÃO"),1,0,'C');
        $pdf->SetFont('courier','B',11);        
        $pdf->Cell(14,7, $row['stda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['squa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['scia'],1,0,'C');                           
        $pdf->Cell(14,7, $row['scda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['totalzao'],1,0,'C');                           
        
        $pdf->Ln();      
        $pdf->Ln();              
        
	}   
    
    $pdf->SetFont('arial','',8);
  	$pdf->Cell(35,6,utf8_decode($row['obsPed']),0,0,'R');      
 
    
    $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'  AND grade.dir = '2' and grade.titulo = 'RISCOS'";
	$resultado2 = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    

        $pdf->SetFont('arial','B',10);
        $pdf->Cell(120,8,utf8_decode("RISCOS - GRADE CORTADA"),0,0,'C');        
        
        $pdf->SetFont('arial','B',10);
        $pdf->Cell(29,6,utf8_decode($row['titulo']),0,0,'L');
		$pdf->Cell(160,6,utf8_decode($row['obs_grade']),0,0,'R');
        

        $pdf->Ln();   
        $pdf->SetDrawColor(160,160,160);    
        $pdf->SetFont('courier','',8);
		$pdf->Cell(28,7,"NOME DO RISCO",1,0,'C');        
		$pdf->Cell(14,7,"32",1,0,'C');
		$pdf->Cell(14,7,"34",1,0,'C');
		$pdf->Cell(14,7,"36",1,0,'C');
		$pdf->Cell(14,7,"38",1,0,'C');
		$pdf->Cell(14,7,"40",1,0,'C');
		$pdf->Cell(14,7,"42",1,0,'C');
		$pdf->Cell(14,7,"44",1,0,'C');
		$pdf->Cell(14,7,"46",1,0,'C');
		$pdf->Cell(14,7,"48",1,0,'C');
		$pdf->Cell(14,7,"50",1,0,'C');
		$pdf->Cell(14,7,"52",1,0,'C');
		$pdf->Cell(14,7,"TOTAL",1,0,'C');
	while($row = $resultado2->fetch_assoc())
	{
        $pdf->Ln();           
        $pdf->SetFont('courier','',8);
        $pdf->Cell(28,7, $row['nome_risco'],1,0,'C');  
        $pdf->SetFont('courier','B',11);
        $pdf->Cell(14,7, $row['tda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['tqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['tsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['toa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qua'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['cia'],1,0,'C');                           
        $pdf->Cell(14,7, $row['cda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['subtot'],1,0,'C');                           
        
        
	}
    
    $pdf->Ln();  
    // linha de totalização do Pedido
    $query = "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' AND grade.dir = '2' and grade.titulo = 'RISCOS'";
	$resultado3 = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    

	while($row = $resultado3->fetch_assoc())
	{

        $pdf->SetFont('courier','',8);
		$pdf->Cell(28,7,utf8_decode("TOTALIZAÇÃO"),1,0,'C');
        $pdf->SetFont('courier','B',11);
        $pdf->Cell(14,7, $row['stda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['squa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['scia'],1,0,'C');                           
        $pdf->Cell(14,7, $row['scda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['totalzao'],1,0,'C');                           
        
        $pdf->Ln();      
        $pdf->Ln();      
        
	}    

    $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'  AND grade.dir = '2' and grade.titulo = 'FORRO'";
	$resultado2 = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    

        $pdf->SetFont('arial','B',10);
        $pdf->Cell(190,8,utf8_decode("FORRO - GRADE CORTADA"),0,0,'C');        
        
        $pdf->SetFont('arial','B',10);
        $pdf->Cell(29,6,utf8_decode($row['titulo']),0,0,'L');
		$pdf->Cell(160,6,utf8_decode($row['obs_grade']),0,0,'R');
        

        $pdf->Ln();   
        $pdf->SetDrawColor(160,160,160);    
        $pdf->SetFont('courier','',8);
		$pdf->Cell(28,7,"NOME DO RISCO",1,0,'C');        
        $pdf->SetFont('courier','B',11);
		$pdf->Cell(14,7,"32",1,0,'C');
		$pdf->Cell(14,7,"34",1,0,'C');
		$pdf->Cell(14,7,"36",1,0,'C');
		$pdf->Cell(14,7,"38",1,0,'C');
		$pdf->Cell(14,7,"40",1,0,'C');
		$pdf->Cell(14,7,"42",1,0,'C');
		$pdf->Cell(14,7,"44",1,0,'C');
		$pdf->Cell(14,7,"46",1,0,'C');
		$pdf->Cell(14,7,"48",1,0,'C');
		$pdf->Cell(14,7,"50",1,0,'C');
		$pdf->Cell(14,7,"52",1,0,'C');
		$pdf->Cell(14,7,"TOTAL",1,0,'C');
        
	while($row = $resultado2->fetch_assoc())
	{
        $pdf->Ln();           
        $pdf->SetFont('courier','',8);
        $pdf->Cell(28,7, $row['nome_risco'],1,0,'C');  
        $pdf->SetFont('courier','B',11);        
        $pdf->Cell(14,7, $row['tda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['tqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['tsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['toa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qua'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['qoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['cia'],1,0,'C');                           
        $pdf->Cell(14,7, $row['cda'],1,0,'C');                           
        $pdf->SetFont('courier','B',8);
        $pdf->Cell(14,7, $row['subtot'],1,0,'C');                           
        $pdf->SetFont('courier','',8);
        
        
	}
    
    $pdf->Ln();  
    // linha de totalização do Pedido
    $query = "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' AND grade.dir = '2' and grade.titulo = 'FORRO'";
	$resultado3 = $mysqli->query($query);
    $row = $resultado->fetch_assoc();    

	while($row = $resultado3->fetch_assoc())
	{

        $pdf->SetFont('courier','',8);
		$pdf->Cell(28,7,utf8_decode("TOTALIZAÇÃO"),1,0,'C');
        $pdf->SetFont('courier','B',11);
        $pdf->Cell(14,7, $row['stda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['stoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['squa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqqa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqsa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['sqoa'],1,0,'C');                           
        $pdf->Cell(14,7, $row['scia'],1,0,'C');                           
        $pdf->Cell(14,7, $row['scda'],1,0,'C');                           
        $pdf->Cell(14,7, $row['totalzao'],1,0,'C');                           
        
        $pdf->Ln();      
        
	}   
    
    $pdf->Output();
?>

