<?php

 //ini_set('display_errors',1);

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

session_start();

require_once ("../../config/Cado.php");

require_once ("../../app/cotizacion/cCotizacion.php");

$oCotizacion = new cCotizacion();

require_once ("../../app/producto/cCatalogo.php");

$oCatalogo = new cCatalogo();

require_once ("../../app/empresa/cEmpresa.php");

$oEmpresa = new cEmpresa();

require_once ("../../app/caja/cCaja.php");

$oCaja = new cCaja();

require_once ("../../app/formatos/numletras.php");

require_once ("../../app/formatos/formato.php");



$hash = $_GET['cotid'];



$cont = strlen($hash);



if($cont>20){

     $dts = $oCotizacion->convertir_hash($hash);

    while($dt = mysqli_fetch_array($dts)){

    $cotid = $dt["tb_cotizacion_id"];

    }

}else {

   $cotid = $hash;

}



	$dtscot= $oCotizacion->mostrarUno($cotid);

	$dtcot= mysqli_fetch_array($dtscot);

	//VENTA

	$cot_numDoc	=$dtcot['tb_cotizacion_numdoc'];

	$cot_docNom=$dtcot['tb_documento_nom'];

	$cot_docAbr=$dtcot['tb_documento_abr'];	

	$cot_venNom=$dtcot['tb_usuario_nom'].' '.$dtcot['tb_usuario_ape'];

	$cot_fecReg=$dtcot['tb_cotizacion_reg'];

	$cot_fec= mostrarFecha($dtcot['tb_cotizacion_fec']);

	$cot_est=$dtcot['tb_cotizacion_est'];

	$cot_valven=$dtcot['tb_cotizacion_valven'];

	$cot_igv=$dtcot['tb_cotizacion_igv'];

	$cot_des=$dtcot['tb_cotizacion_des'];

	$cot_tot=$dtcot['tb_cotizacion_tot'];

	$cot_dia=$dtcot['tb_cotizacion_dia'];

	if($dtcot['tb_mecanico_id']>0){

	$cot_mec=$dtcot['tb_mecanico_tallnom'].' | '.$dtcot['tb_mecanico_alias'].' | '.$dtcot['tb_mecanico_nom'];	

	}

	else{

		$cot_mec='';

	}

	$empid=$dtcot['emp'];

	//CLIENTE

	$cliNom=$dtcot['tb_cliente_nom'];

	$cliDoc=$dtcot['tb_cliente_doc'];

	$cliDir=$dtcot['tb_cliente_dir'];

	$cliPla=$dtcot['tb_cliente_pla'];

	$monid=$dtcot['tb_moneda_id'];

	

	$docabr=$dtcot['tb_documento_abr'];

	

	mysqli_free_result($dtscot);

	

	if($cot_dia>0){

		$forpag = $cot_dia.' DIAS';

	}

	else if($cot_dia==0){

		$forpag ='CONTADO';

	}

	

	

	if($monid==1){

		$lblmon = 'SOLES';

	}else if($monid==2){

		$lblmon = 'DOLARES AMERICANOS';

	}



	if($empid==0){

	$cts = $oCaja->filtro('','',1);	

	$cts1 = $oCaja->filtro('','',3);	

		

$dts=$oEmpresa->mostrarUno(1);

$dt = mysqli_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'];		

mysqli_free_result($dts);



$tit1= $emp_razsoc;



$dtsid=$oEmpresa->mostrarUno(3);

$dtid = mysqli_fetch_array($dtsid);

	$emp_rucid=$dtid['tb_empresa_ruc'];

	$emp_nomcomid=$dtid['tb_empresa_nomcom'];

	$emp_razsocid=$dtid['tb_empresa_razsoc'];

	$emp_dirid=$dtid['tb_empresa_dir'];

	$emp_dir2id=$dtid['tb_empresa_dir2'];

	$emp_telid='Telf. : '.$dtid['tb_empresa_tel'];

	$emp_emaid='E-mail.: '.$dtid['tb_empresa_ema'];

	$emp_firid=$dtid['tb_empresa_fir'];		

mysqli_free_result($dtsid);



$tit3= $emp_razsocid;	

	

	}else{

		

	$cts = $oCaja->filtro('','',$empid);

		

$dts=$oEmpresa->mostrarUno($empid);

$dt = mysqli_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'];		

mysqli_free_result($dts);

	

	$tit1= $emp_razsoc;	

	}



$title = "COTIZACION Nº ".$cot_numDoc;

$nombre_archivo = 'pdf/'.$emp_ruc.'_'.$cot_numDoc.'_'.$cliDoc.'.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('www.infosisperu.com.pe');

$pdf->SetTitle($title);



$pdf->SetSubject('www.infosisperu.com.pe');

$pdf->SetKeywords('www.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;

        background-color: #fff;

        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 style="width: 100%; margin-bottom: 10px">

	<tr>

		<td width="20%">

			<img src="../../imagenes/logo1.png" alt="" />

		</td>

        <td style="text-align:center;" width="50%">

            <strong style="font-size:35px">'.$emp_razsoc.'</strong>

			<br>

            '.$emp_dir.'<br />   <strong>Telf.: '.$emp_tel.'  E-mail.: '.$emp_ema.'</strong> 

			<br />

            <strong style="font-size:35px">'.$emp_razsocid.'</strong>

			<br>

            '.$emp_dirid.'<br />   <strong>'.$emp_telid.'  '.$emp_emaid.'</strong> 

        

		</td>   

	   <td width="" style="text-align:center;border:1px solid #010101;">

        <br>

        <br>

        <br>

          <strong style="font-size: 40px">'.$title.'<br>

            RUC: '.$emp_ruc.'<br></strong>

        </td>

	   

    </tr>

</table>





<br />

<br />



<table width="100%" style="border-collapse: collapse;"  >

    <tr>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">SEÑOR(ES)</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="59%">'.$cliNom.'</td>



        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="10%">FECHA</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="19%">'.$cot_fec.'</td>

    </tr>

    <tr>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">RUC</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="30%">'.$cliDoc.'</td>



        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="10%">FORMA P.</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="19%">'.$forpag.'</td>

		

		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="10%">MONEDA</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="19%">'.$lblmon.'</td>

    </tr>

	

    <tr>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">DIRECCIÓN</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="88%">'.$cliDir.'</td>

    </tr>

	<tr>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">VEHICULO</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="88%">'.$cliPla.'</td>

    </tr>

	<tr>

        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">MEC/TALL</td>

        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="88%">'.$cot_mec.'</td>

    </tr>

	

</table>



<br />

<br />



<table width="100%;" style="border-collapse: collapse;border:0.9px solid #010101;height:400px;" >

    <tbody >

        <tr class="" style="font-size:30px;" >

				<th style="text-align:center; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >CÓDIGO</th>

				<th style="text-align:center; width:43%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >ARTÍCULO</th>

				<th style="text-align:center; width:17%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;">MARCA</th>

				<th style="text-align:right; width:5%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >U.M.</th>

				<th style="text-align:right; 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%;" >PRE. UNI.</th>	

				<th style="text-align:right; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >PRE. TOT.</th>

        </tr>

		

';



$dtsdetcot=$oCotizacion->mostrarCotizaciondetalle($cotid);



while($dtc = mysqli_fetch_array($dtsdetcot))

	{

		

		$dts1=$oCatalogo->mostrarUno($dtc['tb_catalogo_id'],$dtc['tb_marca_id']);

		$dt1 = mysqli_fetch_array($dts1);

		

			$can=$dtc['tb_cotizaciondetalle_can'];		

			$valor_uni=$dtc['tb_cotizaciondetalle_valuni'];

			

			$pre_uni= $valor_uni * 1.18;

			

			$val_tot = $valor_uni * $can;

			$igv_tot = ($valor_uni * 0.18) * $can;

			$pre_tot = ($valor_uni * 1.18) * $can;

			

			$total_valorventa +=$val_tot;

			$total_igv	+=$igv_tot;

			$total	+=$pre_tot;

			

			$cod = $dtc['tb_catalogo_cod'];

		

		$html.='

		<tr style="font-size:30px;" >	

		  <td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  >'.$cod.'</td>

          <td align="left" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  > <strong>'.$dtc['tb_cotizaciondetalle_nom'].'</strong></td>

          <td align="left" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  >'.$dt1['tb_marca_nom'].'</td>

          <td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  >'.$dt1['tb_unidad_abr'].'</td>

          <td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  >'. $can*1 .'</td>

          <td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  >'.formato_money($pre_uni).'</td>

          <td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;"  >'.formato_money($pre_tot).'</td> 

		</tr>

		

		';				

		

		// $cot_valven += $val_tot;

		// $cot_igv += $igv_tot;

		// $cot_des += $des_uni;

		// $cot_tot += $pre_tot;

	}

	mysqli_free_result($dtsdetcot);	









$html.='</tbody>

</table>



<br />

<br />

<br />





<table width="100%;" style="border-collapse: collapse;font-size:30px;">

<tr>

<td style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;"> VALOR VENTA</td>

<td style="text-align: right;border:1px solid #010101;font-size:30px" width="13%" >'.$mon . formato_money($cot_valven).'</td>

<td style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;"> IGV(18%)</td>

<td style="text-align: right;border:1px solid #010101;font-size:30px" width="13%" >'.$mon . formato_money($cot_igv).'</td>

<td style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;"> TOTAL VENTA</td>

<td style="text-align: right;border:1px solid #010101;font-size:30px" width="14%" >'.$mon . formato_money($cot_tot).'</td>

</tr>



<tr>

<td colspan="6" style="border:1px solid #010101;">

son '.numtoletras($cot_tot).'

</td>

</tr>



</table>



<br />

<br />



';



if($empid==0){

	

$html.='<table width="100%;" style="border-collapse: collapse">

<tr>

<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" >'.$tit1.'</td>

</tr>

';



while($ct = mysqli_fetch_array($cts)){

$html.='

<tr>

<td width="50%;" style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" >'.$ct['tb_moneda_nom'].' <strong>'.$ct['tb_banco_nom'].'</strong> </td>

<td width="50%;" style="text-align: left;border:1px solid #010101;font-size:30px" >'.$ct['tb_caja_nom'].' | CCI. '.$ct['tb_caja_cci'].'</td>

</tr>

';

}



$html.='

</table >



<br />

<br />



<table width="100%;"  style="border-collapse: collapse">

<tr>

<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" >'.$tit3.'</td>

</tr>

';



while($ct1 = mysqli_fetch_array($cts1)){

$html.='

<tr>

<td width="50%;" style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" >'.$ct1['tb_moneda_nom'].' <strong>'.$ct1['tb_banco_nom'].'</strong></td>

<td width="50%;" style="text-align: left;border:1px solid #010101;font-size:30px" >'.$ct1['tb_caja_nom'].' | CCI. '.$ct1['tb_caja_cci'].'</td>

</tr>

';

}



$html.='

</table> 

';

}

else{



$html.='<table width="100%;" style="border-collapse: collapse">

<tr >

<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" >'.$tit1.'</td>

</tr>

';



while($ct = mysqli_fetch_array($cts)){

$html.='

<tr >

<td width="50%;" style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" >'.$ct['tb_moneda_nom'].' <strong>'.$ct['tb_banco_nom'].'</strong></td>

<td width="50%;" style="text-align: left;border:1px solid #010101;font-size:30px" >'.$ct['tb_caja_nom'].' | CCI. '.$ct['tb_caja_cci'].'</td>

</tr> ';

}



$html.='

</table>';

	

	

}



if($empid==3){

	$html.='

	<br />

	<br />

	<br />

	<br />

	<br />

	<br />

	<br />

	<table>

	<tr>

	<td width="70%"></td>

	<td width="30%">

			<img src="firma3.jpeg" alt="" />

	</td>

	</tr>

	</table>

	';

}



if($empid==1){

	$html.='

	<br />

	<br />

	<br />

	<br />

	<br />

	<br />

	<br />

	<table>

	<tr>

	<td width="70%"></td>

	<td width="30%">

			<img src="firma1.jpeg" alt="" />

	</td>

	</tr>

	</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, 'F');



?>

