<?php
require('../fpdf181/fpdf.php');

class pdf extends FPDF
{
	public function header()
	{
	// Encabezado
	}

	public function MultiCellAll($yi,$xi,$ancho,$alto,$texto,$border,$alineacion,$relleno)
	{
	// Escribir varias columnas
	//MultiCell(ancho,alto,texto,border,alineacion,relleno)
		$x1 = 0;
		$y1 = $yi;
		$i  = 0;
		$maxSize = $yi;
		foreach ($texto as $posicion=>$columna)
		{
			$this->SetXY( $x1+$xi[$i], $y1);
			$this->MultiCell($ancho[$i], $alto[$i], $columna,$border[$i],$alineacion[$i],$relleno[$i]);
			$i += 1;
			if ( $maxSize < ($this->GetY()  ) )
				$maxSize = $this->GetY() ;
		}
		return ( $maxSize - $yi );
	}

	public function footer()
	{
		// pie de pagina
	}

 /* 
  x, y: top left corner of the rectangle.
  w, h: width and height.
  r: radius of the rounded corners.
  corners: numbers of the corners to be rounded: 1, 2, 3, 4 or any combination (1=top left, 2=top right, 3=bottom right, 4=bottom left).
  style: same as Rect(): F, D (default), FD or DF. 
   
  */

  function RoundedRect($x, $y, $w, $h, $r, $corners = '1234', $style = '')
	{
			$k = $this->k;
			$hp = $this->h;
			if($style=='F')
					$op='f';
			elseif($style=='FD' || $style=='DF')
					$op='B';
			else
					$op='S';
			$MyArc = 4/3 * (sqrt(2) - 1);
			$this->_out(sprintf('%.2F %.2F m',($x+$r)*$k,($hp-$y)*$k ));

			$xc = $x+$w-$r;
			$yc = $y+$r;
			$this->_out(sprintf('%.2F %.2F l', $xc*$k,($hp-$y)*$k ));
			if (strpos($corners, '2')===false)
					$this->_out(sprintf('%.2F %.2F l', ($x+$w)*$k,($hp-$y)*$k ));
			else
					$this->_Arc($xc + $r*$MyArc, $yc - $r, $xc + $r, $yc - $r*$MyArc, $xc + $r, $yc);

			$xc = $x+$w-$r;
			$yc = $y+$h-$r;
			$this->_out(sprintf('%.2F %.2F l',($x+$w)*$k,($hp-$yc)*$k));
			if (strpos($corners, '3')===false)
					$this->_out(sprintf('%.2F %.2F l',($x+$w)*$k,($hp-($y+$h))*$k));
			else
					$this->_Arc($xc + $r, $yc + $r*$MyArc, $xc + $r*$MyArc, $yc + $r, $xc, $yc + $r);

			$xc = $x+$r;
			$yc = $y+$h-$r;
			$this->_out(sprintf('%.2F %.2F l',$xc*$k,($hp-($y+$h))*$k));
			if (strpos($corners, '4')===false)
					$this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-($y+$h))*$k));
			else
					$this->_Arc($xc - $r*$MyArc, $yc + $r, $xc - $r, $yc + $r*$MyArc, $xc - $r, $yc);

			$xc = $x+$r ;
			$yc = $y+$r;
			$this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-$yc)*$k ));
			if (strpos($corners, '1')===false)
			{
					$this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-$y)*$k ));
					$this->_out(sprintf('%.2F %.2F l',($x+$r)*$k,($hp-$y)*$k ));
			}
			else
					$this->_Arc($xc - $r, $yc - $r*$MyArc, $xc - $r*$MyArc, $yc - $r, $xc, $yc - $r);
			$this->_out($op);
	}

  function _Arc($x1, $y1, $x2, $y2, $x3, $y3)
  {
		$h = $this->h;
		$this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $x1*$this->k, ($h-$y1)*$this->k,
				$x2*$this->k, ($h-$y2)*$this->k, $x3*$this->k, ($h-$y3)*$this->k));
  }


}

function Ymd_dmY(string $fecha, string $separador = "/"):string
{
	$ano=substr($fecha,0,4);
	$mes=substr($fecha, 5,2);
	$dia=substr($fecha, 8,2);
	$_fecha=$dia.$separador.$mes.$separador.$ano; 
	return $_fecha;
}

function formatoFecha($fecha)
{
	//$date   = date_format($fecha, 'F, jS Y');
	$date   =	date('d/ m/ Y');
	return $date;
}


function SetCurrency(float $valor, string $signo = '$'):string
{
	return $signo.number_format($valor,2,'.','');
}

// Se define el tamaño de hoja y margenes
$fpdf = new pdf('l','mm','letter',true);
$fpdf->AddPage('portrait', 'letter');
$fpdf->SetMargins(10,30,20,20);

// Datos de la comapañia
require_once "../modelos/Empresa.php";
$empresa				= new Empresa();
$rsptae					= $empresa->buscarempresa();
$rege						= $rsptae->fetch_object();
$licenialicore	= $rege->licenciadelicor;
$nombreem       = $rege->nombre;
$telefenoem     = $rege->telefono;
$direccionem    = $rege->direccion;
$rifem          = $rege->num_documento;


// Datos de cliente y Nota de Crédito
//obtenemos los datos de la cabecera de la venta actual
require_once "../modelos/Notacredito.php";
$notacredito	= new Notacredito();
$rsptav		    =$notacredito->notacreditocabecera($_GET["id"]);
//recorremos todos los valores que obtengamos
$regv		    =$rsptav->fetch_object();

$cliente    			=$regv->cliente;
$direccion				=$regv->direccion;
$telefono   			=$regv->telefono;
$rif        			=$regv->num_documento;
$licencia_licor			=$regv->licencia_licor;
$licencia_licord		=$regv->licencia_licord;
$tdoc					=$regv->tipo_documento;
$cliented   			=$regv->cliented;
$direcciond				=$regv->direcciond;
$telefonod  			=$regv->telefonod;
$rifd       			=$regv->num_documentod;
$tdocd					=$regv->tipo_documentod;
$vendedor				=$regv->vendedor;
$observacion			=$regv->observacion;
/* 
$conductor				=$regv->nombretransporte;
$vehiculo				=$regv->vehiculo;
$placa					=$regv->placa; 
*/
$tasa_dollar			=$regv->tasa_dollar;
$cuidad     			="Caracas";
$longitud				=strlen($regv->num_notacredito);
$serie					='0000';
$prefac					= substr($serie, 1, 4-$longitud);
$nrofactura				='NC-'.$prefac.$regv->num_notacredito;
$Fecha 					=$regv->fecha;
//$Fecha					=formatoFecha($Fecha);
$Fecha1			        =Ymd_dmY($Fecha);
$totacredito    		=$regv->total_credito;
$totacreditod  	 		=$regv->total_creditod;

//FACTURA AFECTADA
$tasafactura			= $regv->tasaventa;
$notaentrega		 	= $regv->num_comprobante1;
$factura 				= $regv->num_comprobante;
$fechafactura 			= formatoFecha($regv->fechafactura);
$total_ventadf			= $regv->total_ventadf;
$total_ventaimpuestodf	= $regv->total_ventaimpuestodf;
$ivadf					= $regv->ivadf;



if ($tdoc=="RIF"){
    $rif 	= "J-".$rif;
    $rift 	= "RIF";
}else{
    $rif 	= "CI-".$rif;
    $rift 	= "CÉDULA";
}
if ($tdocd=="RIF"){
	$rifd 	= "J-".$rifd;
	$riftd 	= "RIF";
}else{
	$rifd 	= "CI-".$rifd;
	$riftd 	= "CÉDULA";
}

//$fpdf	->Image('FacturaAlimentoDaniel1.jpg' , 17 ,8, 180 , 210,'JPG', '');
//Titulos del encabezado
$fpdf	->SetFillColor(255,255,255);
$fpdf	->SetTextColor(0,0,0); 
$fpdf	->SetLineWidth(0.3);
$fpdf	->SetDrawColor(0,0,0);

 /* 
 $fpdf	->SetFillColor(0,0,0);
$fpdf	->SetTextColor(255,255,255); 
 */


$xa		=21;
$y		=52;
$x1		=$xa+91.5;
$x1c	=130;
$x2		=50;
$fpdf	->SetXY($x2,$y);
$fpdf	->SetFont('Helvetica', 'B', 9);
if ($regv->notacredito=='NOTA DE CREDITO POR DEVOLUCION') {
	$titulo = 'NOTA DE DEVOLUCIÓN';
} else {
	$titulo = str_replace("NOTA DE CREDITO", "NOTA DE DESCUENTO", $regv->notacredito);
}

$fpdf	->Cell(90, 6, utf8_decode($titulo), '0', 0, 'L', 1);
//$fpdf	->Cell(90, 6, utf8_decode($regv->notacredito), '0', 0, 'L', 1);



$fpdf	->MultiCell(28, 6, $nrofactura, '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$fpdf	->SetXY($x1c,$y);
$fpdf	->SetFont('Helvetica', '', 9);
$fpdf	->Cell(27, 6, 'FECHA:', '0', 0, 'L', 1);
//$fpdf	->MultiCell(28, 6, $Fecha, '0', 'L', 1);
$fpdf	->MultiCell(28, 6, $Fecha1, '0', 'L', 1);
$y		=$fpdf->GetY()+4;


$yl		=$y;
$x		=$xa;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', 'B', 10);
$fpdf	->Cell(27, 5, 'CLIENTE', '0', 0, 'L', 1);
$fpdf	->SetXY($x1,$y);
$fpdf	->SetFont('Helvetica', 'B', 10);
$fpdf	->Cell(27, 5, 'DESPACHO', '0', 0, 'L', 1);
$yf		=$fpdf->GetY();

//cuadrado del encabezado
$y		=$fpdf->GetY()+6;
//$fpdf	->Rect(20,$y-6,92.5,5);
//$fpdf	->Rect(112.5,$y-6,84,5);
$yd1	=$y;

//Encabezado datos del cliente
$x		=$xa;
$fpdf	->SetXY($x,$y-1);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 5, 'NOMBRE', '0', 0, 'L', 1);
$x		=$xa+19;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 7);
$fpdf	->MultiCell(70, 4, utf8_decode($cliente), '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$x		=$xa;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 5, utf8_decode('DIRECCIÓN'), '0', 0, 'L', 1);
$x		=$xa+19;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 6.5);
$fpdf	->MultiCell(70, 4, utf8_decode($direccion), '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$x		=$xa;
$fpdf	->SetXY($x,$y-1);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 6, utf8_decode($riftd), '0', 0, 'L', 1);
$x		=$xa+19;
$fpdf	->SetXY($x,$y-1);
$fpdf	->SetFont('Helvetica', '', 7);
$fpdf	->MultiCell(70, 6, utf8_decode($rif), '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$x		=$xa;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 6, utf8_decode('TELÉFONO'), '0', 0, 'L', 1);
$x		=$xa+19;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 7);
$fpdf	->MultiCell(70, 6, utf8_decode($telefono), '0', 'L', 1);
$yd     = $fpdf->GetY();
$y		=$yd1;
$x		=$x1;
$fpdf	->SetXY($x,$y-1);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 5, utf8_decode('NOMBRE'), '0', 0, 'L', 1);
$x		=$x1+21;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 6.5);
$fpdf	->MultiCell(62, 4, utf8_decode($cliented), '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$x		=$x1;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 5, utf8_decode('DIRECCIÓN'), '0', 0, 'L', 1);
$x		=$x1+21;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 6.5);
$fpdf	->MultiCell(62, 4, utf8_decode($direcciond), '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$x		=$x1;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 6, utf8_decode($riftd), '0', 0, 'L', 1);
$x		=$x1+21;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', '', 7);
$fpdf	->MultiCell(62, 6, utf8_decode($rifd), '0', 'L', 1);
$y		=$fpdf->GetY()+1;
$x		=$x1;
$fpdf	->SetXY($x,$y-1);
$fpdf	->SetFont('Helvetica', '', 8);
$fpdf	->Cell(20, 6, utf8_decode('TELÉFONO'), '0', 0, 'L', 1);
$x		=$x1+21;
$fpdf	->SetXY($x,$y-1);
$fpdf	->SetFont('Helvetica', '', 7);
$fpdf	->MultiCell(62, 6, utf8_decode($telefonod), '0', 'L', 1);

$yi     = $fpdf->GetY();

if ($yi>$yd){
    $yd=$yi;
}

//Cuadrado del encabezado del cliente
//Derecho
$x	=$xa-1;
$fpdf->RoundedRect($x, $yl-1, 92, 6, 5, '1', '');
$fpdf->RoundedRect($x, $yl+5, 20, ($yd-$yf)-5, 5, '4', '');
$fpdf->RoundedRect($x+20, $yl+5, 72, ($yd-$yf)-5, 5, '', '');
//Izquierda
$fpdf->RoundedRect($x1-0.6, $yl-1, 85, 6, 3.3, '2', '');
$fpdf->RoundedRect($x1-0.6, $yl+5, 21, ($yd-$yf)-5, 5, '', '');
$fpdf->RoundedRect($x1+20.5, $yl+5,63.9, ($yd-$yf)-5, 5, '3', '');



/*  
 $fpdf	->SetFillColor(0,0,0);
$fpdf	->SetTextColor(255,255,255);     
*/
//Observacion

$y		=$yd+4;
$yf     =$y;
$x		=$xa;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', 'B', 8);
$fpdf	->Cell(30, 6, utf8_decode('OBSERVACIÓN'), '0', 0, 'L', 1);
$fpdf	->SetFont('Helvetica', '', 6.5);
$fpdf	->SetXY($x+30,$y);
$fpdf	->MultiCell(126, 6,utf8_decode($observacion), '0', 'L', 1);

$y		=$fpdf->GetY();

//Cuadros
$fpdf->RoundedRect($x-1, $yf-1, 30, 8, 3, '14', '');
$fpdf->RoundedRect($x+29, $yf-1, 147, 8, 3, '23', '');

// Factura Afectada
$y		=$y+8;
$yf     =$y;
$x		=$xa;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', 'B', 8);
if($factura<>""){
	$fpdf	->Cell(40, 6, utf8_decode('FACTURA AFECTADA'), '0', 0, 'C', 1);
	$fpdf	->SetXY($x+40,$y);
	$fpdf	->Cell(38, 6, utf8_decode($factura), '0', 0, 'C', 1);
	$fpdf	->SetXY($x+78,$y);
}else{
	$fpdf	->Cell(40, 6, utf8_decode('NOTA DE ENTREGA'), '0', 0, 'C', 1);
	$fpdf	->SetXY($x+40,$y);
	$fpdf	->Cell(38, 6, utf8_decode($notaentrega), '0', 0, 'C', 1);
	$fpdf	->SetXY($x+78,$y);
}
$fpdf	->Cell(18, 6, utf8_decode('TASA BCV'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+96,$y);
$fpdf	->Cell(20, 6, number_format($tasafactura, 2, ',', '.'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+116,$y);
$fpdf	->Cell(29, 6, utf8_decode('FECHA'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+145.1,$y);
$fpdf	->Cell(30, 6, utf8_decode($fechafactura), '0', 0, 'C', 1);
$y		=$fpdf->GetY();
//Cuadros
$fpdf->RoundedRect($x-1, $yf-1, 79, 8, 3, '14', '');
$fpdf->RoundedRect($x+78, $yf-1, 38, 8, 3, '', '');
$fpdf->RoundedRect($x+116, $yf-1, 60, 8, 3, '23', '');



// Encabezado del Detalle
$y		=$y+12;
$yf     =$y;
$x		=$xa;
$fpdf	->SetXY($x,$y);
$fpdf	->SetFont('Helvetica', 'B', 8);
$fpdf	->Cell(60, 6, utf8_decode('DESCRIPCIÓN'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+60,$y);
$fpdf	->Cell(18, 6, utf8_decode('CANTIDAD'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+78,$y);
$fpdf	->Cell(18, 6, utf8_decode('REF.'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+96,$y);
$fpdf	->Cell(20, 6, utf8_decode('PRECIO BS.'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+116,$y);
$fpdf	->Cell(29, 6, utf8_decode('TOTAL REF.'), '0', 0, 'C', 1);
$fpdf	->SetXY($x+145.1,$y);
$fpdf	->Cell(30, 6, utf8_decode('TOTAL BS.'), '0', 0, 'C', 1);
$y		=$fpdf->GetY();


//Cuadro del encabezado
$fpdf->RoundedRect($x-1, $yf-1, 60, 6.5, 3, '1', '');
$fpdf->RoundedRect($x+59, $yf-1, 19, 6.5, 3, '', '');
$fpdf->RoundedRect($x+78, $yf-1, 18, 6.5, 3, '', '');
$fpdf->RoundedRect($x+96, $yf-1, 20, 6.5, 3, '', '');
$fpdf->RoundedRect($x+116, $yf-1, 29, 6.5, 3, '', '');
$fpdf->RoundedRect($x+145.1, $yf-1, 30.6, 6.5, 3, '2', '');


//Cuadro del detalle
$yd1     =5.5;
$alto    =70;
$fpdf->RoundedRect($x-1, $yf+$yd1, 60, $alto, 3, '4', '');
$fpdf->RoundedRect($x+59, $yf+$yd1, 19, $alto, 3, '', '');
$fpdf->RoundedRect($x+78, $yf+$yd1, 18, $alto, 3, '', '');
$fpdf->RoundedRect($x+96, $yf+$yd1, 20, $alto, 3, '', '');
$fpdf->RoundedRect($x+116, $yf+$yd1, 29, $alto, 3, '', '');
$fpdf->RoundedRect($x+145.1, $yf+$yd1, 30.6, $alto, 3, '3', '');



// Detalle de la factura
//Buscamos los detalles de la factura

if ($regv->notacredito=='NOTA DE CREDITO POR DEVOLUCION') {
	$rsptad=$notacredito->notacreditodetallesdevolucion($_GET["id"]);
	$observacion1 = 'Devolución de: ';
} else {
	$rsptad=$notacredito-> notacreditodetallesimportefactura($_GET["id"]);
	$rsptad1=$notacredito->notacreditodetallesimporte($_GET["id"]);
	$observacion1 = '';
}




// 
//articulo, codigo, cajas, unidades, precio_venta, descuento, subtotal, idcategoriadeunidades, 
//nombre, idunidademedidad, nombre, unidadesxcajas
$fpdf	->SetY($y+6);
$fpdf	->SetFont('Helvetica', '', 7);
/* $fpdf	->SetFillColor(255,255,255);
$fpdf	->SetLineWidth(0.5);
$fpdf	->SetDrawColor(90,90,90);
$fpdf	->SetTextColor(0,0,0); */

$subtotal		=0;$subtotald	=0;
$total			=0;$totald		=0;
$iva			=0;$ivapvp		=0;
$exento			=0;$exentod		=0;
$totaldollar	=0;$imp 		= 1+(16/100);
$totalG			=0;$totalGpvp	=0;
$totalapagar	=0;$totalapagard=0;
$totalpvp		=0;$subtotalpvp	=0;
$totalart16		=0;

$y				=$fpdf->GetY()+1;
$x				=$xa;
$y11			=$y;

/*
$fpdf	->SetFillColor(0,0,0);
$fpdf	->SetTextColor(255,255,255);     
 */

while($regd=$rsptad->fetch_object()){
	$precio 		=	$regd->precio_venta;
	$preciod 		=	$regd->precio_ventad;
	$subtotal		=	$precio*$regd->cajas;
  	$subtotald		=	$preciod*$regd->cajas;
	if ($observacion1<>'') {
		$subtotal	=	$precio*$regd->cajas;
  		$subtotald	=	$preciod*$regd->cajas;
	}
	 
	$total		=	$total+$subtotal;
	$totald		=	$totald+$subtotald;
	$tesxto 	= "";
	if($regd->gradoalcohol<>0){
		$tesxto=' Grados '.utf8_decode($regd->gradoalcohol).'%';
	}
	$desc		= ' UND: '.number_format($regd->unidadesxcaja, 0, ',', '.').' DE '.utf8_decode($regd->capacidad).$tesxto;
	
	
	$fpdf	->SetFont('Helvetica', '',6);
	$fpdf	->SetXY($x,$y);
	$fpdf	->MultiCell(58, 4,utf8_decode($regd->articulo.$desc), '0', 'L', 1);
	$y1		=$fpdf->GetY();
	$fpdf	->SetXY($x+60,$y);
	$fpdf	->Cell(17, 4, number_format($regd->cajas, 2, ',', '.'), '0', 0, 'C', 1);
	$fpdf	->SetXY($x+78.5,$y);
	$fpdf	->Cell(17, 4, number_format($preciod, 2, ',', '.'), '0', 0, 'R', 1);
	$fpdf	->SetXY($x+96.5,$y);
	$fpdf	->Cell(19, 4,  number_format($precio, 2, ',', '.'), '0', 0, 'R', 1);
	$fpdf	->SetXY($x+116.5,$y);
	$fpdf	->Cell(28, 4, number_format($subtotald, 2, ',', '.'), '0', 0, 'R', 1);
	$fpdf	->SetXY($x+145.5,$y);
	$fpdf	->Cell(29, 4,  number_format($subtotal, 2, ',', '.'), '0', 0, 'R', 1);
	$y		=$fpdf->GetY();
	if ($y1>$y){
		$y = $y1;
	}
    //$y		=$fpdf->GetY()+5;

}

if ($regv->notacredito=='NOTA DE CREDITO POR DEVOLUCION') {
	$totalapagar	=	$total;
	$totalapagard	=	$totald;
	$ivadnc	 		= 	($totald*$ivadf)/$total_ventadf;
	$ivanc 			= 	$ivadnc*$tasafactura;

	$totalapagardnci= 	$totalapagard+$ivadnc;
	$totalapagarnci = 	$totalapagar+$ivanc;
}else{
	$regd1			=	$rsptad1->fetch_object();
	//$tasafactura
	$diferencia     =   abs($total - ($regd1->totalfncbase*$tasafactura));
	if($diferencia<=2){
	    $totalapagar	=	$total;
	    $totalapagard	=	$totald;
	}else{
	    $totalapagard 	= 	$regd1->totalfncbase;
	    $totalapagar 	= 	$regd1->totalfncbase*$tasafactura;
	}
	
	$por 			= 	number_format($regd1->por, 0, ',', '.');
	$totalapagardnc = 	$regd1->totalncbase;
	$totalapagarnc 	= 	$regd1->totalncbase*$tasafactura;
	$ivadnc	 		= 	$regd1->ivanc;
	$ivanc 			= 	$regd1->ivanc*$tasafactura;
	$totalapagardnci= 	$regd1->totalnc;
	$totalapagarnci = 	$regd1->totalnc*$tasafactura;
}

//Sub-total, impuesto y total ****************
//Sub-total

//$fpdf	->SetFillColor(0,0,0);
//$fpdf	->SetTextColor(255,255,255); 

$x1			=46;
//$y1			=120;
$xd     =72;
$xd1    =107;
$fpdf	->SetFont('Helvetica', '', 9);
$y		=$y11+$alto+1;
$x		=$x1;
if($regv->notacredito=='NOTA DE CREDITO POR DEVOLUCION'){
	$fpdf	->SetXY($x,$y-1);
	$fpdf	->Cell(70, 6, utf8_decode('Sub-Total'), '', 0, 'R', 1);
	$fpdf	->SetXY($x+$xd,$y);
	$fpdf	->Cell(34.5, 6, number_format($totalapagard, 2, ',', '.'), '', 0, 'R', 1);
	$fpdf	->SetXY($x+$xd1,$y);
	$fpdf	->Cell(43.5, 6, number_format($totalapagar, 2, ',', '.'), '', 0, 'R', 1);
}else{
    $fpdf	->SetFont('Helvetica', 'B', 9);
	$fpdf	->SetXY($x,$y);
	$fpdf	->Cell(70, 6, 'Base Imponible:', '', 0, 'R', 1);
	$fpdf	->SetXY($x+$xd,$y);
	$fpdf	->Cell(34.5, 6, number_format($totalapagard, 2, ',', '.'), '', 0, 'R', 1);
	$fpdf	->SetXY($x+$xd1,$y);
	$fpdf	->Cell(43.5, 6, number_format($totalapagar, 2, ',', '.'), '', 0, 'R', 1);
	//Nc sin Iva
	$fpdf	->SetFont('Helvetica', '', 9);
	$y		=$fpdf->GetY()+7;
	$fpdf	->SetXY($x,$y-1);
	$fpdf	->Cell(70, 6, utf8_decode('Descuento del '.$por.'%'), '', 0, 'R', 1);
	$fpdf	->SetXY($x+$xd,$y);
	$fpdf	->Cell(34.5, 6, number_format($totalapagardnc, 2, ',', '.'), '', 0, 'R', 1);
	$fpdf	->SetXY($x+$xd1,$y);
	$fpdf	->Cell(43.5, 6, number_format($totalapagarnc, 2, ',', '.'), '', 0, 'R', 1);
}
$fpdf	->SetFont('Helvetica', '', 9); 
//Iva
$y		=$fpdf->GetY()+7;
$fpdf	->SetXY($x,$y-1);
$fpdf	->Cell(70, 6, utf8_decode('Iva'), '', 0, 'R', 1);
$fpdf	->SetXY($x+$xd,$y-1);
$fpdf	->Cell(34.5, 6, number_format($ivadnc, 2, ',', '.'), '', 0, 'R', 1);
$fpdf	->SetXY($x+$xd1,$y-1);
$fpdf	->Cell(43.5, 6, number_format($ivanc, 2, ',', '.'), '', 0, 'R', 1);
//Total
$y		=$fpdf->GetY()+7;
$fpdf	->SetFont('Helvetica', 'B', 9);
$fpdf	->SetXY($x,$y-1);
$fpdf	->Cell(70, 6, utf8_decode('Total Nota:'), '', 0, 'R', 1);
$fpdf	->SetXY($x+$xd,$y-1);
$fpdf	->Cell(34.5, 6, number_format($totalapagardnci, 2, ',', '.'), '', 0, 'R', 1);
$fpdf	->SetXY($x+$xd1,$y-1);
$fpdf	->Cell(43.5, 6, number_format($totalapagarnci, 2, ',', '.'), '', 0, 'R', 1);
//Cuadros
$y		=$y11+$alto+1;
$x		=$x1;
if($regv->notacredito<>'NOTA DE CREDITO POR DEVOLUCION'){
	$fpdf	->RoundedRect($x,$y,71,5.3, 1, '14', '');
	$fpdf	->RoundedRect($x+71,$y,36,5.3, 3, '', '');
	$fpdf	->RoundedRect($x+107,$y,43.5,5.3, 1, '23', '');
	$y		=$y+6.5;
}
$fpdf	->RoundedRect($x,$y,71,6, 3, '1', '');
$fpdf	->RoundedRect($x+71,$y,36,6, 3, '', '');
$fpdf	->RoundedRect($x+107,$y,43.5,6, 3, '2', '');
$y		=$y+6.1;
$fpdf	->RoundedRect($x,$y,71,6, 3, '', '');
$fpdf	->RoundedRect($x+71,$y,36,6, 3, '', '');
$fpdf	->RoundedRect($x+107,$y,43.5,6, 3, '', '');
$y		=$y+6.1;
$fpdf	->RoundedRect($x,$y,71,6, 3, '4', '');
$fpdf	->RoundedRect($x+71,$y,36,6, 3, '', '');
$fpdf	->RoundedRect($x+107,$y,43.5,6, 3, '3', '');


$fpdf->OutPut('NotaDeCredito_'.$nrofactura,'I');