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

date_default_timezone_set('America/Mazatlan');

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'];

$sucbus = $_GET['sucbus'];
  $fechai = date('Y-m-d H:i:s');
  $fechap = date('Y-m-d');

if($suc == 1){
    $sucn = 'SUCURSAL CULIACAN';
}else if ($suc == 2){
    $sucn = 'SUCURSAL MAZATLAN';
}else if($suc == 3){
    $sucn = 'SUCURSAL HERMOSILLO';
}
if($sucbus == 0){
    //$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
    $registro = mysqli_query($conexion,"SELECT SUM(IF(V.IdSucursal = 1, D.Cantidad, 0)) AS 'CUL',SUM(IF(V.IdSucursal=2, D.Cantidad, 0)) AS 'MAZ',SUM(IF(V.IdSucursal=3, D.Cantidad, 0)) AS 'HMO', D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', CASE WHEN D.CrisN = 1 THEN 'SENCILLO' WHEN D.CrisN = 2 THEN 'DOBLE' WHEN D.CrisN = 4 THEN 'ESPEJO' WHEN D.CrisA = 1 THEN 'ANTIRREFLEJANTE' END AS 'Cristal' FROM VentasDetalle D
    JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
    LEFT JOIN Productos P ON D.CodProd = P.Codigo
    WHERE D.CodProd != '' AND V.Serie != 'COT-' and (D.CrisN != 3 OR D.CrisA != 3) and V.ImpCris = 0 AND V.Activo = 1 AND D.Activo = 1 
  GROUP BY D.Largo, D.Ancho, Umed, Cristal
    Order by Cristal,D.Largo, D.Ancho ASC ");
    /* if($desde != '' && $hasta != ''){
        
        $registro = mysqli_query($conexion,"SELECT D.Cantidad, D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', D.CodProd as 'MOLDURA', V.FechaBusq, V.Serie, V.FolioVenta,CASE WHEN D.TiraC = 1 THEN 'SI' WHEN D.TiraC != 1 THEN 'NO' END AS 'TiraCompleta' FROM VentasDetalle D
        JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
        JOIN Productos P ON D.CodProd = P.Codigo
        WHERE V.FechaBusq BETWEEN '$desde' AND '$hasta' AND D.CodProd != '' AND V.Serie != 'COT-' and P.IdCategoria = '3' and V.ImpresoProduccion = 0
        Order by D.CodProd");
    } */

}else if($sucbus != 0){
    $registro = mysqli_query($conexion,"SELECT SUM(IF(V.IdSucursal = 1, D.Cantidad, 0)) AS 'CUL',SUM(IF(V.IdSucursal=2, D.Cantidad, 0)) AS 'MAZ',SUM(IF(V.IdSucursal=3, D.Cantidad, 0)) AS 'HMO', D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', CASE WHEN D.CrisN = 1 THEN 'SENCILLO' WHEN D.CrisN = 2 THEN 'DOBLE' WHEN D.CrisN = 4 THEN 'ESPEJO' WHEN D.CrisA = 1 THEN 'ANTIRREFLEJANTE' END AS 'Cristal' FROM VentasDetalle D
    JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
    LEFT JOIN Productos P ON D.CodProd = P.Codigo
    WHERE D.CodProd != '' AND V.Serie != 'COT-' and (D.CrisN != 3 OR D.CrisA != 3) and V.ImpCris = 0 AND V.Activo = 1 AND D.Activo = 1 AND V.IdSucursal = '$sucbus'
  GROUP BY D.Largo, D.Ancho, Umed, Cristal
    Order by Cristal,D.Largo, D.Ancho ASC");
    /* if($desde != '' && $hasta != ''){
        
        $registro = mysqli_query($conexion,"SELECT D.Cantidad, D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', D.CodProd as 'MOLDURA', V.FechaBusq, V.Serie, V.FolioVenta,CASE WHEN D.TiraC = 1 THEN 'SI' WHEN D.TiraC != 1 THEN 'NO' END AS 'TiraCompleta' FROM VentasDetalle D
        JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
        JOIN Productos P ON D.CodProd = P.Codigo
        WHERE V.FechaBusq BETWEEN '$desde' AND '$hasta' AND D.CodProd != '' AND V.Serie != 'COT-' and P.IdCategoria = '3' AND V.IdSucursal = '$sucbus' and V.ImpresoProduccion = 0
        Order by D.CodProd");
    } */
}
  //CREAMOS NUESTRA VISTA Y LA DEVOLVEMOS AL AJAX
  $contador = mysqli_num_rows($registro);
  if($sucbus == 0){
   $sn = 'GLOBAL';
 }else if($sucbus == 1){
   $sn = 'CUL';
 }else if($sucbus == 2){
   $sn = 'MAZ';
 }else if($sucbus == 3){
  $sn = 'HMO';
}
  
  /* 
  $desde = date('d-m-Y H:i:s'); */
$pdf = new PDF_Code39('P');
$pdf->AddPage();

// PARTE DE INFORMACION
$pdf->SetFont('Arial', '', 9);
$pdf->Image('imagenes/logo.jpg' , 10 ,10, 50 , 30,'JPG');

/* 
$newDate = date("d-m-Y", strtotime($desde)); */
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY(19);
$pdf->Cell(200, 8, utf8_decode($sn), 0,0,C);
$pdf->Ln(6);
$pdf->Cell(200, 8, utf8_decode('PRODUCCION - CRISTAL'), 0,0,C);
$pdf->Ln(8);
$pdf->SetFont('Arial', 'U', 10);
$pdf->Cell(190, 8, 'Fecha de impresion: '.$fechai, 0,0,R);
$pdf->SetFont('Arial', 'B', 10);
$pdf->Ln(8);
$pdf->SetY(50);
$pdf->Cell(60, 8, $sn, 1,0,C);
$pdf->Cell(30, 8, 'Largo', 1,0,C);
$pdf->Cell(30, 8, 'Ancho', 1,0,C);
$pdf->Cell(30, 8, 'U. Med.', 1,0,C);
$pdf->Cell(40, 8, 'Cristal', 1,0,C);
$pdf->SetFont('Arial', 'B', 8);
$pdf->Ln(8);
$y = $pdf->GetY();


while($r = mysqli_fetch_array($registro)){

   if($sucbus == 1){
      $cantidad = $r['CUL'];
   }else if($sucbus == 2){
      $cantidad = $r['MAZ'];
   }else if($sucbus == 3){
      $cantidad = $r['HMO'];
   }
  $largo = $r['Largo'];
  $ancho = $r['Ancho'];
  $umed = $r['Umed'];
  $mold = $r['Cristal'];
  
  $width = 30;
  $pdf->Cell(60,6,$cantidad,1,0,C);
  $pdf->Cell($width,6,$largo,1,0,C);
  $pdf->Cell($width,6,$ancho,1,0,C);
  $pdf->Cell($width,6,$umed,1,0,C);
  $width = 40;
  $pdf->Cell($width,6,getSubString(utf8_decode($mold),54),1,0,C);
  $x = $pdf->GetX();

  $pdf->Ln(6);
}


$act = mysqli_query($conexion,"UPDATE Ventas V 
JOIN VentasDetalle D ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
SET V.ImpCris = 1  
WHERE  V.Serie != 'COT-' and (D.CrisN != 3 OR D.CrisA != 3) and V.ImpCris = 0 AND V.Activo = 1 AND D.Activo = 1 AND V.IdSucursal = '$sucbus'");

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

?>