<?php

require_once('../../libreriasphp/html2pdf/_tcpdf_5.9.206/tcpdf.php');

session_start();

require_once ("../../config/Cado.php");
require_once ("../../app/ordentrabajo/cOrdentrabajo.php");
$oOrdentrabajo = new cOrdentrabajo();

require_once ("../../app/producto/cCatalogo.php");
$oCatalogo = new cCatalogo();

require_once ("../../app/empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();
require_once ("../../app/formatos/numletras.php");
require_once ("../../app/formatos/formato.php");

date_default_timezone_set('America/Lima');
$now =  date('d-m-Y h:i:s a');

$ordtra_id = $_GET['ordtraid'];

	$dts= $oOrdentrabajo->mostrarUno($_GET['ordtraid']);
	$dt = mysql_fetch_array($dts);
	$fec = mostrarFecha($dt['tb_ordentrabajo_fec']);

	$doc_id	=$dt['tb_documento_id'];
	$numdoc	=$dt['tb_ordentrabajo_numdoc'];
	$ordtra_des=$dt['tb_ordentrabajo_des'];
	$ordtra_res=$dt['tb_trabajador_dni'].' - '.$dt['tb_trabajador_nom'].' '.$dt['tb_trabajador_ape'];
	$dia=$dt['tb_ordentrabajo_dia'];
	$usu_nom=$dt['tb_usuario_use'];

	$monid	=$dt['tb_moneda_id'];
	$tipcam	=$dt['tb_ordentrabajo_tipcam'];

	if($tipcam==0){
		$tipcam = '1.00';
	}else{
		$tipcam = $tipcam;
	}
	
	
	$pro_id	=$dt['tb_cliente_id'];
	$prodoc	=$dt['tb_cliente_doc'];
	$pronom	=$dt['tb_cliente_nom'];
	$prodir	=$dt['tb_cliente_dir'];
	$est	=$dt['tb_ordentrabajo_est'];
	$ordtra_tot	= formato_money($dt['tb_ordentrabajo_tot']);
	mysql_free_result($dts);
	
	
	
	$ordcom_valven = $ordcom_tot / 1.18;
	$ordcom_igv = $ordcom_valven * 0.18;
	
	
	$fecven = mostrarFecha($dtven['tb_ventapago_fecven']);
	$ven_nom = $dtven['tb_usuario_use'];
	
	if($dia == 0){
		$compag = 'CONTADO';
	}else {
		$compag = $dia.' DIAS.';
	}
	
	mysql_free_result($dtsven);
	
	if($monid==1){
		$mon ="S/ ";
		$lblmon="SOLES";
	}else if($monid==2)
	{
		$lblmon="DOLARES AMERICANOS";
		$mon ="US$ ";
	}
	
$dts=$oEmpresa->mostrarUno('1');
$dt = mysql_fetch_array($dts);
	$emp_ruc=$dt['tb_empresa_ruc'];
	$emp_nomcom=$dt['tb_empresa_nomcom'];
	$emp_razsoc=$dt['tb_empresa_razsoc'];
	$emp_dir=$dt['tb_empresa_dir'];
	$emp_dir2=$dt['tb_empresa_dir2'];
	$emp_tel=$dt['tb_empresa_tel'];
	$emp_ema=$dt['tb_empresa_ema'];
	$emp_fir=$dt['tb_empresa_fir'];		
mysql_free_result($dts);
	

	
	if($est=='1'){
		$anular = '
		<tr>
		<td width="50%"></td>
	    <td width="50%" style="text-align: right">
		<strong style="color:blue;font-size:10pt;" >ABIERTO EN CURSO</strong> 
		</td>
		</tr>
		';
}else if($est=='2'){
		$anular = '
		<tr>
		<td width="50%"></td>
	    <td width="50%" style="text-align: right">
		<strong style="color:red;font-size:10pt;" >FINALIZADO</strong> 
		</td>
		</tr>
		';
}
else if($est=='3'){
		$anular = '
		<tr>
		<td width="50%"></td>
	    <td width="50%" style="text-align: right">
		<strong style="color:red;font-size:10pt;" >CANCELADO</strong> 
		</td>
		</tr>
		';
}
else if($est=='4'){
		$anular = '
		<tr>
		<td width="50%"></td>
	    <td width="50%" style="text-align: right">
		<strong style="color:red;font-size:10pt;" >FACTURADO</strong> 
		</td>
		</tr>
		';
}


	
$title = "Representacion de ".$numdoc;
$nombre_archivo = $emp_ruc.'_'.$numdoc.'_'.$prodoc.'.pdf';

// $file_name = 'FA-'.$serie.'-'.$numero.'-code.png';
// $path = 'temp/'.$file_name;
// $type = pathinfo($path, PATHINFO_EXTENSION);
// $data = file_get_contents($path);
// $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);


class MYPDF extends TCPDF
{

    public function Header() {	
      //$image_file = K_PATH_IMAGES.'logo.jpg';
      //$this->Image($image_file, 20, 10, 71, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
      // Set font
      //$this->SetFont('helvetica', 'B', 20);
      // Title
      //$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
    }

    public function Footer() 
    {
      // $style = array(
      //   'position' => 'L',
      //   'align' => 'L',
      //   'stretch' => false,
      //   'fitwidth' => true,
      //   'cellfitalign' => '',
      //   'border' => false,
      //   'padding' => 0,
      //   'fgcolor' => array(0,0,0),
      //   'bgcolor' => false,
      //   'text' => false
      // //     'font' => 'helvetica',
      // //     'fontsize' => 8,
      // //     'stretchtext' => 4
      // );

      // $this -> SetY(-24);
      // // Page number
      // $this->SetFont('helvetica', '', 9);
      // //$this->SetTextColor(0,0,0);
      // $this->Cell(0, 0, 'Página '.$this->getAliasNumPage().' de '.$this->getAliasNbPages(), 'T', 1, 'R', 0, '', 0, false, 'T', 'M');
      
      // $codigo='CAV-'.str_pad($_GET['d1'], 4, "0", STR_PAD_LEFT);
      
      // $this->write1DBarcode($codigo, 'C128', '', 273, '', 6, 0.3, $style, 'N');
      // $this->Cell(0, 0, 'www.prestamosdelnortechiclayo.com', 0, 1, 'C', 0, '', 0, false, 'T', 'M');        
    }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('https://infosisperu.com.pe');
$pdf->SetTitle($title);

$pdf->SetSubject('https://infosisperu.com.pe');
$pdf->SetKeywords('https://infosisperu.com.pe');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(12,15, 12);// left top right
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
//$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 15);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// add a page
$pdf->AddPage('P', 'A4');

//font-family: Consolas, monaco, monospace;
//helvetica, monaco, monospace;

// Introducimos HTML de prueba
$html = '
<!DOCTYPE html>
<html lang="es">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	 <link rel="shortcut icon" href="../../admin/bootstrap/images/favicon.png">
</head>
<style type="text/css">
    body {
        color: black;
        font-family: Verdana, Arial, Consolas;
        margin: 0px;
        padding-top: 0px;
        font-size: 7.5pt;
    }
    .header_row th {
		font-size:28px;
		background-color:#eee;
        border-bottom: 0.9px solid #010101;
        border-right: 0.9px solid #010101;
        border-left: 0.9px solid #010101;
        border-top: 0.9px solid #010101;
    }
    .odd_row td {
        background-color: transparent;
        border-bottom: 0.9px solid #010101;
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .even_row td {
        padding-top: 5px;
        padding-bottom: 5px;
       border-bottom: 0.9px solid #010101;
    }
    .row td{
        border-right: 0.9px solid #010101;
        border-left: 0.9px solid #010101;
    }
</style>

<style media="print">
    .oculto{
        display: none;
    }

</style>
<body>




<table width="100%" style="" > '.$anular.'
<tr>
	<td width="22%">
	<br />
	
	<img src="../../imagenes/logo.png" alt="" />
	</td>
        <td style="text-align:center;" width="50%">
		<br />
            <strong style="font-size:40px">'.$emp_razsoc.'</strong>
			<br>
            '.$emp_dir.'<br />   <strong>Telf.: '.$emp_tel.'  E-mail.: '.$emp_ema.'</strong> 
			<br />       
		</td>   
	   <td width="" style="text-align:center;border:1px solid #010101;">
         <br>
         <br>
       <strong style="font-size: 40px">ORDEN DE TRABAJO<br>
            RUC: '.$emp_ruc.'<br>
            '.$numdoc.'</strong>
			  <br>
        </td>
    </tr>
</table>

<br />
<br />
<br />

<table style="border-collapse: collapse;" >

    <tr  style="border:1px solid #010101" >
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:35px;" width="15%"><strong>SEÑOR(ES)</strong></td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:30px" width="55%">'.$pronom.'</td>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:35px;" width="15%">USUARIO</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="15%">'.$usu_nom.'</td>
    </tr>
	
    <tr  style="border:1px solid #010101" >
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:35px;" width="15%">RUC</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="55%">'.$prodoc.'</td>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:35px;" width="15%">MONEDA</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="15%">'.$lblmon.'</td>
    </tr>
	
    <tr>
        <td style="text-align: left;border:1px solid #010101;font-weight:bold;background-color:#bdbdbd;padding:10%;font-size:35px;" width="15%">DIRECCIÓN</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="55%">'.$prodir.'</td>
		
		<td style="text-align: left;border:1px solid #010101;font-weight:bold;background-color:#bdbdbd;padding:10%;font-size:35px;" width="15%">COND. PAGO</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="15%">'.$compag.'</td>
    </tr>
</table>

<br />
<br />

<table style="border-collapse: collapse;" >

    <tr  style="border:1px solid #010101" >
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:35px;" width="100%"><strong>TRABAJO/DESCRIPCION</strong></td>
    </tr>
	 
	<tr  style="border:1px solid #010101" >
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:50px" width="80%">'.$ordtra_des.'</td>
        <td style="text-align: right;border:1px solid #010101;padding:15%;font-size:50px" width="20%">'.$ordtra_tot.'</td>
    </tr>
	
	<tr  style="border:1px solid #010101" >
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:35px;" width="100%"><strong>RESPONSABLE</strong></td>
    </tr>
	 
	<tr  style="border:1px solid #010101" >
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:39px" width="100%">'.$ordtra_res.'</td>
    </tr>
	
</table>


<br />
<br />

<table width="100%;" style="border-collapse: collapse;border:0.9px solid #010101;height:400px;"  >
    <tbody >
	
		 <tr class="" style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th colspan="8" style="text-align:center;bold;border:1px solid #010101;background-color:red;color:#fff;padding:10%;" >PARA TRABAJO</th>          
        </tr>
	
        <tr class="" style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th style="text-align:center; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >CODIGO</th>
            <th style="text-align:center; width:38%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >DESCRIPCION</th>
            <th style="text-align:center; width:7%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >U.M.</th>
            <th style="text-align:center; width:5%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >CAN</th>
            <th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >VAL. UNI.</th>			
            <th style="text-align:center; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >PRE. UNI.</th>
            <th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >VAL. TOT.</th>
            <th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >PRE. TOT.</th>
        </tr>
';

$dtsdetven=$oOrdentrabajo->mostrar_ordentrabajo_detalle($_GET['ordtraid']);
$cont=1;
while($dt1 = mysql_fetch_array($dtsdetven))
	{
	
		
		$can = $dt1['tb_ordentrabajodetalle_can'];
		$valuni=$dt1['tb_ordentrabajodetalle_valuni'];
		$pre_uni=$valuni*1.18;
			
		$val_tot = $valuni * $can;
		$pre_tot = $pre_uni * $can;
		$des_tot = $dt1['tb_ordentrabajodetalle_des']*$can;
		$pre_tot_cdes = $pre_tot - $des_tot;
		
		$html.='
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1['tb_catalogo_cod'].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1['tb_producto_nom'].'|'.$dt1['tb_marca_nom'].'</td>
		
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1['tb_unidad_abr'].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.$dt1["tb_ordentrabajodetalle_can"]*1 .'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.formato_money($valuni).'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.formato_money($pre_uni).'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.formato_money($val_tot).'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.formato_money($pre_tot).'</td>
		
		</tr>
		
		';				
		
		$cont++;
		
		$ordtra_tot += $pre_tot; 
	}
	mysql_free_result($dtsdetven);	

$ordtra_valven = $ordtra_tot / 1.18;
$ordtra_igv = $ordtra_valven * 0.18;


$html.='</tbody>
</table>
<br />
<br />

<table width="100%;" style="border-collapse: collapse;border:0.9px solid #010101;height:400px;"  >
    <tbody >
	
		 <tr class="" style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th colspan="8" style="text-align:center;bold;border:1px solid #010101;background-color:red;color:#fff;padding:10%;" >PARA FACTURAR</th>          
        </tr>
	
        <tr class="" style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th style="text-align:center; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >CODIGO</th>
            <th style="text-align:center; width:38%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >DESCRIPCION</th>
            <th style="text-align:center; width:7%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >U.M.</th>
            <th style="text-align:center; width:5%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >CAN</th>
            <th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >VAL. UNI.</th>			
            <th style="text-align:center; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >PRE. UNI.</th>
            <th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >VAL. TOT.</th>
            <th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >PRE. TOT.</th>
        </tr>
';

$dtsdetvenf=$oOrdentrabajo->mostrar_ordentrabajo_detalle_factura($_GET['ordtraid']);
$contf=1;
while($dt1f = mysql_fetch_array($dtsdetvenf))
	{
	
		
		$canf = $dt1f['tb_ordentrabajodetalle_facturar_can']*1;
		$valunif=$dt1f['tb_ordentrabajodetalle_facturar_valuni'];
		$pre_unif=$valunif*1.18;
			
		$val_totf = $valunif * $canf;
		$pre_totf = $pre_unif * $canf;
		
		$pre_tot_cdesf = $pre_totf ;
		
		$html.='
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1f['tb_catalogo_cod'].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1f['tb_producto_nom'].'|'.$dt1f['tb_marca_nom'].'</td>
		
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1f['tb_unidad_abr'].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.$dt1f["tb_ordentrabajodetalle_facturar_can"]*1 .'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.formato_money($valunif).'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.formato_money($pre_unif).'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.formato_money($val_totf).'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;text-align:right;" >'.formato_money($pre_totf).'</td>
		
		</tr>
		
		';				
		
		$contf++;
		
		$ordtra_totf += $pre_totf; 
	}
	mysql_free_result($dtsdetvenf);	

$ordtra_valvenf = $ordtra_totf / 1.18;
$ordtra_igvf = $ordtra_valvenf * 0.18;


$html.='</tbody>
</table>



<br />
<br />

<table width="100%;" style="border-collapse: collapse;"  >
    <tbody >
        <tr class="" style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >OP. INAFECTA</th>
            <th style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >OP. EXONERADA</th>
            <th style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >OP. GRAVADA</th>
            <th style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >TOTAL IGV (18%)</th>
            <th style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >TOTAL DE VENTA</th>
        </tr>
		
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">		
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.'0.00</strong></td>
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.'0.00</strong></td>
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.formato_money($ordtra_valvenf).'</strong></td>
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.formato_money($ordtra_igvf).'</strong></td>
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.formato_money($ordtra_totf).'</strong></td>		
		</tr>
		
		<tr style="font-size:35px" >
		<td colspan="5" style="border:1px solid #010101;padding:10%;text-align:left;" >SON '.numtoletras($ordtra_totf).' '.$lblmon.' 
		</td>
		</tr>
	
	</tbody>
</table>

<br />
<br />

<table width="100%;" style="border-collapse: collapse;"  >
    <tbody >
       
		<tr style="">		
		<td style="" ><strong>IMPORTANTE:</strong> </td>
		</tr>
		
		<tr style="">		
		<td style="" >1. Estoy de acuerdo en que todo el trabajo se ha realizado satisfactoriamente. </td>
		</tr>
	</tbody>
</table>


';

$html.='
</body>
</html>';

$pdf->writeHTML($html, true, 0, true, true);

//$pdf->write2DBarcode($ruc_empresa.'|'.$idcomprobante.'|'.$serie.'|'.$numero.'|'.$toigv.'|'.$importetotal.'|'.fecha_mysql($fecha).'|'.$idtipodni.'|'.$ruc.'|', 'QRCODE,Q', 157, 99, 40, 40, $style, 'N');

$pdf->Output($nombre_archivo, 'I');
