<?php
session_start();
require('code39.php');
require('fpdi/fpdi.php');
require('conexion.php');

function getSubString($string, $length=NULL)
{
    //Si no se especifica la longitud por defecto es 50
    if ($length == NULL)
        $length = 30;
    //Primero eliminamos las etiquetas html y luego cortamos el string
    $stringDisplay = substr(strip_tags($string), 0, $length);
    //Si el texto es mayor que la longitud se agrega puntos suspensivos
    if (strlen(strip_tags($string)) > $length)
        $stringDisplay .= ' ...';
    return $stringDisplay;
}

function num2letras($num, $fem = false, $dec = true) { 
   $matuni[2]  = "dos"; 
   $matuni[3]  = "tres"; 
   $matuni[4]  = "cuatro"; 
   $matuni[5]  = "cinco"; 
   $matuni[6]  = "seis"; 
   $matuni[7]  = "siete"; 
   $matuni[8]  = "ocho"; 
   $matuni[9]  = "nueve"; 
   $matuni[10] = "diez"; 
   $matuni[11] = "once"; 
   $matuni[12] = "doce"; 
   $matuni[13] = "trece"; 
   $matuni[14] = "catorce"; 
   $matuni[15] = "quince"; 
   $matuni[16] = "dieciseis"; 
   $matuni[17] = "diecisiete"; 
   $matuni[18] = "dieciocho"; 
   $matuni[19] = "diecinueve"; 
   $matuni[20] = "veinte"; 
   $matunisub[2] = "dos"; 
   $matunisub[3] = "tres"; 
   $matunisub[4] = "cuatro"; 
   $matunisub[5] = "quin"; 
   $matunisub[6] = "seis"; 
   $matunisub[7] = "sete"; 
   $matunisub[8] = "ocho"; 
   $matunisub[9] = "nove"; 

   $matdec[2] = "veint"; 
   $matdec[3] = "treinta"; 
   $matdec[4] = "cuarenta"; 
   $matdec[5] = "cincuenta"; 
   $matdec[6] = "sesenta"; 
   $matdec[7] = "setenta"; 
   $matdec[8] = "ochenta"; 
   $matdec[9] = "noventa"; 
   $matsub[3]  = 'mill'; 
   $matsub[5]  = 'bill'; 
   $matsub[7]  = 'mill'; 
   $matsub[9]  = 'trill'; 
   $matsub[11] = 'mill'; 
   $matsub[13] = 'bill'; 
   $matsub[15] = 'mill'; 
   $matmil[4]  = 'millones'; 
   $matmil[6]  = 'billones'; 
   $matmil[7]  = 'de billones'; 
   $matmil[8]  = 'millones de billones'; 
   $matmil[10] = 'trillones'; 
   $matmil[11] = 'de trillones'; 
   $matmil[12] = 'millones de trillones'; 
   $matmil[13] = 'de trillones'; 
   $matmil[14] = 'billones de trillones'; 
   $matmil[15] = 'de billones de trillones'; 
   $matmil[16] = 'millones de billones de trillones'; 
   
   //Zi hack
   $float=explode('.',$num);
   $num=$float[0];

   $num = trim((string)@$num); 
   if ($num[0] == '-') { 
      $neg = 'menos '; 
      $num = substr($num, 1); 
   }else 
      $neg = ''; 
   while ($num[0] == '0') $num = substr($num, 1); 
   if ($num[0] < '1' or $num[0] > 9) $num = '0' . $num; 
   $zeros = true; 
   $punt = false; 
   $ent = ''; 
   $fra = ''; 
   for ($c = 0; $c < strlen($num); $c++) { 
      $n = $num[$c]; 
      if (! (strpos(".,'''", $n) === false)) { 
         if ($punt) break; 
         else{ 
            $punt = true; 
            continue; 
         } 

      }elseif (! (strpos('0123456789', $n) === false)) { 
         if ($punt) { 
            if ($n != '0') $zeros = false; 
            $fra .= $n; 
         }else 

            $ent .= $n; 
      }else 

         break; 

   } 
   $ent = '     ' . $ent; 
   if ($dec and $fra and ! $zeros) { 
      $fin = ' coma'; 
      for ($n = 0; $n < strlen($fra); $n++) { 
         if (($s = $fra[$n]) == '0') 
            $fin .= ' cero'; 
         elseif ($s == '1') 
            $fin .= $fem ? ' una' : ' un'; 
         else 
            $fin .= ' ' . $matuni[$s]; 
      } 
   }else 
      $fin = ''; 
   if ((int)$ent === 0) return 'Cero ' . $fin; 
   $tex = ''; 
   $sub = 0; 
   $mils = 0; 
   $neutro = false; 
   while ( ($num = substr($ent, -3)) != '   ') { 
      $ent = substr($ent, 0, -3); 
      if (++$sub < 3 and $fem) { 
         $matuni[1] = 'una'; 
         $subcent = 'as'; 
      }else{ 
         $matuni[1] = $neutro ? 'un' : 'uno'; 
         $subcent = 'os'; 
      } 
      $t = ''; 
      $n2 = substr($num, 1); 
      if ($n2 == '00') { 
      }elseif ($n2 < 21) 
         $t = ' ' . $matuni[(int)$n2]; 
      elseif ($n2 < 30) { 
         $n3 = $num[2]; 
         if ($n3 != 0) $t = 'i' . $matuni[$n3]; 
         $n2 = $num[1]; 
         $t = ' ' . $matdec[$n2] . $t; 
      }else{ 
         $n3 = $num[2]; 
         if ($n3 != 0) $t = ' y ' . $matuni[$n3]; 
         $n2 = $num[1]; 
         $t = ' ' . $matdec[$n2] . $t; 
      } 
      $n = $num[0]; 
      if ($n == 1) { 
         $t = ' ciento' . $t; 
      }elseif ($n == 5){ 
         $t = ' ' . $matunisub[$n] . 'ient' . $subcent . $t; 
      }elseif ($n != 0){ 
         $t = ' ' . $matunisub[$n] . 'cient' . $subcent . $t; 
      } 
      if ($sub == 1) { 
      }elseif (! isset($matsub[$sub])) { 
         if ($num == 1) { 
            $t = ' mil'; 
         }elseif ($num > 1){ 
            $t .= ' mil'; 
         } 
      }elseif ($num == 1) { 
         $t .= ' ' . $matsub[$sub] . '?n'; 
      }elseif ($num > 1){ 
         $t .= ' ' . $matsub[$sub] . 'ones'; 
      }   
      if ($num == '000') $mils ++; 
      elseif ($mils != 0) { 
         if (isset($matmil[$sub])) $t .= ' ' . $matmil[$sub]; 
         $mils = 0; 
      } 
      $neutro = true; 
      $tex = $t . $tex; 
   } 
   $tex = $neg . substr($tex, 1) . $fin; 
   //Zi hack --> return ucfirst($tex);
   $end_num=ucfirst($tex).' pesos '.$float[1].'/100 M.N.';
   return $end_num; 
}

$nom = $_SESSION['nom_admin'];
$iduser = $_SESSION['id_user'];
$suc = $_SESSION['IdSuc'];
$tipou= $_SESSION['tipouser'];



if($suc == 1){
    $sucn = 'SUCURSAL CULIACAN';
}else if ($suc == 2){
    $sucn = 'SUCURSAL MAZATLAN';
}else if($suc == 3){
    $sucn = 'SUCURSAL HERMOSILLO';
}

$serie = $_GET['serie'];
$folio = $_GET['folio'];

$ff = $serie.$folio;



$gasto = mysqli_query($conexion,"SELECT * FROM Ventas WHERE FolioVenta = '$folio' AND Serie = '$serie' AND Activo = 1");
$v = mysqli_fetch_array($gasto);




$tipoord = $v['TipoOrden'];
if($tipoord == 1){
  $tip = 'Orden';
}else if($tipoord == 2){
  $tip = 'Cotizacion';
}
$idu = $v['IdVendedor'];
$nombrea = mysqli_query($conexion,"SELECT * FROM Usuarios WHERE IdUsuario = '$idu' ");
$usu = mysqli_fetch_array($nombrea);

$usuario = $usu['Nombre'].' '.$usu['Apellidos'];
$tipofolio = $v['TipoFolio'];
$fechaventa = $v['FechaVenta'];
$idsuc = $v['IdSucursal'];
if($idsuc == 1){
    $sucn = 'SUCURSAL CULIACAN';
}else if ($idsuc == 2){
    $sucn = 'SUCURSAL MAZATLAN';
}else if($idsuc == 3){
    $sucn = 'SUCURSAL HERMOSILLO';
}
$idc = $v['IdCliente'];
$cl = mysqli_query($conexion,"SELECT * FROM Clientes WHERE IdCliente = '$idc'");
$c = mysqli_fetch_array($cl);

$nomC = $c['Nombre'];
$domC = $c['Domicilio'];
$rfcC = $c['RFC'];
$celC = $c['Celular'];
$telC = $c['Telefono'];
$ciuC = $c['Ciudad'];
$sucC = $c['IdSucursal'];
$tipoC = $c['TipoCliente'];
if($tipoC == 1){
  $tipoCliente = 'Mostrador';
}else if($tipoC == 2){
  $tipoCliente = 'Fotografo';
}

$subt = $v['Subtotal'];
$dcto = $v['Descuento'];
$total = $v['Total'];
$anti = $v['Anticipo'];
$saldo = $v['Saldo'];
$obs = $v['Obs'];
$hrent = $v['HoraEntrega'];
$fechaent = $v['FechaEntrega'];

$pdf = new PDF_Code39('L','mm',array(396,311));
$pdf->AddPage();

// PARTE DE INFORMACION
$pdf->SetFont('Arial', '', 9);
$pdf->Image('imagenes/logo.jpg' , 10 ,10, 70 , 45,'JPG');
$pdf->SetDrawColor(0,0,0);

$pdf->SetFont('Arial', 'B', 16);
$pdf->SetY(25);
$pdf->Cell(220, 8, utf8_decode($sucn), 0,0,R);
$pdf->SetFont('Arial', 'U', 20);
$pdf->Cell(130, 8, 'FOLIO: '.$serie.' '.$folio, 0,0,R);
$pdf->SetFont('Arial', 'B', 16);
$pdf->Ln(8);
$pdf->Cell(350, 8, 'Vendedor: '.utf8_decode($usuario), 0,0,R);
$pdf->Ln(6);
$pdf->Cell(350, 8, $fechaventa, 0,0,R);
$pdf->Ln(11);
$pdf->Cell(350, 8, $tipoCliente, 0,0,R);
$pdf->Line(10,60,360,60);
$pdf->Line(10,62,360,62);
$pdf->SetY(62);
$pdf->Cell(350, 8, 'Cliente:', 1,0,L);
$pdf->Ln(13);
$pdf->Cell(30, 8, 'Nombre:', 0,0,L);
$pdf->Cell(230, 8, utf8_decode($nomC), 0,0,L);
$pdf->Cell(25, 8, 'Tel. / Cel. : ', 0,0,R);
$pdf->Cell(60, 8, $telC.'/'.$celC, 0,0,R);

$pdf->Ln(13);
$pdf->Cell(30, 8, 'Domicilio: ', 0,0,L);
$pdf->Cell(170, 8, utf8_decode($domC), 0,0,L);
$pdf->Cell(25, 8, 'RFC : ', 0,0,R);
$pdf->Cell(60, 8, utf8_decode($rfcC), 0,0,R);

$pdf->SetY(100);
$pdf->Cell(20, 8, '#', 1,0,C);
$pdf->Cell(40, 8, 'MEDIDAS', 1,0,C);
$pdf->Cell(145, 8, 'PRODUCTO', 1,0,C);
$pdf->Cell(100, 8, 'OBSERVACIONES', 1,0,C);
$pdf->Cell(45, 8, 'PRECIO', 1,0,C);
$pdf->Ln(8);


$ga  = mysqli_query($conexion,"SELECT * FROM VentasDetalle WHERE IdVenta = '$folio' AND Serie = '$serie' AND Activo = 1");
while($vd = mysqli_fetch_array($ga)){
  $cant = $vd['Cantidad'];
  $largo = $vd['Largo'];
  $ancho = $vd['Ancho'];
  $umed = $vd['Umed'];
  if($umed == 1){
    $med = 'CM';
  }else if($umed == 2){
    $med = 'IN';
  }
  $prod = $vd['CodProd'];
  $cm = $vd['CrisN'];
  $ca = $vd['CrisA'];
  $ml = $vd['Ml'];
  $res = $vd['Res'];
  $pap = $vd['PapF'];
  $text = $vd['Text'];
  $canva = $vd['Canva'];
  $acri = $vd['Acrilico'];
  $dobcar = $vd['DobleCara'];
  $obspro = $vd['ObsProd'];
  $preciomold = $vd['PrecioMold'];
  $preciocrisn = $vd['PrecioCrisN'];
  $preciocrisa = $vd['PrecioCrisA'];
  $precioml = $vd['PrecioMl'];
  $preciores = $vd['PrecioRes'];
  $preciopap = $vd['PrecioPapel'];
  $preciotext = $vd['PrecioText'];
  $preciomaq = $vd['PrecioMaq'];
  $preciocanva = $vd['PrecioCanva'];
  $precioacr = $vd['PrecioAcrilico'];
  $preciodobc = $vd['PrecioDobleCara'];
  $preciotot = $vd['PrecioTotal'];

  if($cm ==1){
    $cris = 'CNS';
  }else if($cm == 2){
    $cris = 'CND';
  }
  if($ca == 1){
    $crisa = 'CA';
  }
  if($ml == 1){
    $mlu = 'MLS';
  }else if($ml== 2){
    $mlu = 'MLD';
  }
  if($res == 1){
    $resp = 'MACO3MM';
  }else if($res == 2){
    $resp = 'FB5MM';
  }else if($res == 3){
    $resp = 'PVC';
  }else if($res == 4){
    $resp = 'ALU';
  }else if($res == 5){
    $resp = 'MACO6MM';
  }else if($res == 6){
    $resp = 'MACO9MM';
  }

  if($pap == 1){
    $pape = 'EVE';
  }else if($pap == 2){
    $pape = 'MATE';
  }else if($pap == 3){
    $pape = 'PREM';
  }else if($pap == 4){
    $pape = 'ART';
  }else if($pap == 5){
    $pape = 'PERL';
  }

  if($text == 1){
    $te = 'PIEL';
  }else if($text == 2){
    $te = 'LINO';
  }else if($text == 3){
    $te = 'SAND';
  }

  if($canva == 1){
    $can = 'CANVA';
  }

  if($acri == 1){
    $ac = 'ACR';
  }

  if($dobcar == 1){
    $do = 'DC';
  }
  $n = mysqli_query($conexion,"SELECT * FROM Productos Where Codigo = '$prod'");
  $q = mysqli_fetch_array($n);
  $nom = $q['Nombre'];
  $p = $nom." ".$m." ".$cris." ".$crisa." ".$mlu." ".$resp." ".$pape." ".$te." ".$can." ".$ac." ".$do;
  $pdf->SetFontSize(16);
  $pdf->Cell(20, 8, $cant, 1,0,C);
  $pdf->Cell(40, 8, $largo.'*'.$ancho.' '.$med, 1,0,C);
  $pdf->SetFontSize(14);
  $pdf->Cell(145, 8, $p, 1,0,C);
 
  $pdf->Cell(100, 8, getSubString(utf8_decode($obspro),28), 1,0,C);
   $pdf->SetFontSize(16);
  $preciotot = number_format($preciotot,2,'.',',');
  $pdf->Cell(45, 8, '$ '.$preciotot, 1,0,R);
  $pdf->Ln(8);
}
$pdf->SetY(242);
$pdf->Cell(285, 8, utf8_decode('Observaciones: '.$obs), 1,0,L);

$pdf->SetY(250);
$pdf->Line(10,250,295,250);
$pdf->Cell(100, 8, 'Firma del cliente', 0,0,C);
$pdf->Cell(100, 8, 'Fecha de entrega: '.$fechaent, 0,0,C);
$subt = number_format($subt,2,'.',',');
$pdf->Cell(40, 8, 'Subtotal: ', 1,0,R);
$pdf->Cell(45, 8, '$ '.$subt, 1,0,R);
$pdf->Ln(8);
if($dcto <= 0){
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, 'Hora de entrega: '.$hrent, 0,0,C);
  $total = number_format($total,2,'.',',');
  $pdf->Cell(40, 8, 'Total: ', 1,0,R);
  $pdf->Cell(45, 8, '$ '.$total, 1,0,R);
  $pdf->Ln(8);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $anti = number_format($anti,2,'.',',');
  $pdf->Cell(40, 8, 'Anticipo: ', 1,0,R);
  $pdf->Cell(45, 8, '$ '.$anti, 1,0,R);
  $pdf->Ln(8);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $saldo = number_format($saldo,2,'.',',');
  $pdf->Cell(40, 8, 'Saldo: ', 1,0,R);
  $pdf->Cell(45, 8, '$ '.$saldo, 1,0,R);
}else if($dcto >0){
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, 'Hora de entrega: '.$hrent, 0,0,C);
  $dc = number_format($dcto);
  $pdf->Cell(40, 8, 'Descuento: ', 1,0,R);
  $pdf->Cell(45, 8, '% '.$dc, 1,0,R);
  $pdf->Ln(8);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $total = number_format($total,2,'.',',');
  $pdf->Cell(40, 8, 'Total: ', 1,0,R);
  $pdf->Cell(45, 8, '$ '.$total, 1,0,R);
  $pdf->Ln(8);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $anti = number_format($anti,2,'.',',');
  $pdf->Cell(40, 8, 'Anticipo: ', 1,0,R);
  $pdf->Cell(45, 8, '$ '.$anti, 1,0,R);
  $pdf->Ln(8);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $pdf->Cell(100, 8, ' ', 0,0,C);
  $saldo = number_format($saldo,2,'.',',');
  $pdf->Cell(40, 8, 'Saldo: ', 1,0,R);
  $pdf->Cell(45, 8, '$ '.$saldo, 1,0,R);

}
$pdf->Line(10,290,295,290);
$pdf->Line(100,250,100,290);
$pdf->Line(210,250,210,290);
$pdf->SetY(315);
$pdf->SetFontSize(12);
if($suc == 1){
$pdf->Cell(295, 8, 'Expedido en: Juan Jose Rios No. 37 Ote. Col. Jorge Almada', 0,0,C);
}else if($suc == 2){
$pdf->Cell(295, 8, 'Expedido en: Av. Rafael Buelna 204-D, Hacienda las Cruces, 82126', 0,0,C);
}
$pdf->Ln(8);
if($suc == 1){
$pdf->Cell(295, 8, utf8_decode('Culiacan, Sinaloa. Tel. 712-50-05'), 0,0,C);
}else if($suc == 2){
$pdf->Cell(295, 8, utf8_decode('Mazatlán, Sinaloa. Tel. 669-112-1438'), 0,0,C);
}
$pdf->Ln(16);
$pdf->Cell(295, 8, utf8_decode('Eduardo Lee Cañedo R.F.C. LECE671223-V83'), 0,0,C);
$pdf->Ln(8);
$pdf->Cell(295, 8, utf8_decode('Juan José Rios No. 37 Ote. Col. Jorge Almada Culiacán, Sinaloa. Tel. (667) 712-50-05'), 0,0,C);
$pdf->Line(10,360,295,360);


$pdf->Output($ff.'.pdf','I');

?>