<?php

  $position   = '../../';

  require_once($position.'../switch/access.php');
  require_once($position.'assets/global.php');
  require_once($position.'assets/session.php');
  require_once($position.'assets/funciones.php');

  require_once($Position.'PhpExcel/Classes/PHPExcel.php');

  echo "Aqui";

	$Go = $_GET['Go'];
	if( $Go == 'ok' ){

		echo $archivo = "articulos.xlsx";
		$inputFileType = PHPExcel_IOFactory::identify($archivo);
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objPHPExcel = $objReader->load($archivo);
		$sheet = $objPHPExcel->getSheet(0);
		$highestRow = $sheet->getHighestRow();
		$highestColumn = $sheet->getHighestColumn();

	   for ($row = 2; $row <= $highestRow; $row++){

			$Proveedor 		  = $sheet->getCell("A".$row)->getValue();
			$Marca 			    = $sheet->getCell("B".$row)->getValue();
			$Codigo 	      = $sheet->getCell("C".$row)->getValue();
			$Descripcion 		= $sheet->getCell("D".$row)->getValue();
			$Precio 		    = $sheet->getCell("E".$row)->getValue();



			if( $Codigo ){

        echo $Proveedor;
        echo "<br>";
        echo $Marca;
        echo "<br>";
        echo "Codigo: ".$Codigo;
        echo "<br>";
        echo $Descripcion;
        echo "<br>";
        echo $Precio;
        echo "<br>";

        //Valida Productos
        $Query = "
    			SELECT
    				pro.id
    			FROM articulos pro
    			WHERE pro.codigo = '$Codigo'
    		";
        $rQuery  = mysqli_query($mysqli, $Query);
        $Art   = mysqli_fetch_assoc($rQuery);
        if( $Art[id] ){
          echo "Id: ".$Art[id];
          echo "<br>";

          $Query = "
            UPDATE articulos
            SET
              activo = '1',
              principal = '1'
            WHERE
              id = '$Art[id]'
          ";
          $Result = mysqli_query($mysqli, $Query);

          // echo "<br>";

          //Verifica Precio
          $Query = "
      			SELECT
      				pre.id
      			FROM articulos_precios pre
      			WHERE pre.fk_articulo = '$Art[id]'
      		";
          $rQuery  = mysqli_query($mysqli, $Query);
          $Pre   = mysqli_fetch_assoc($rQuery);
          if( $Pre[id] ){

            //Actualiza Precio
            $Query = "
            	UPDATE articulos_precios
      				SET
      					precio = '$Precio'
      				WHERE
      					id = '$Pre[id]'
      			";
            $Result = mysqli_query($mysqli, $Query);

          }else{

            //Inserta Precio
            $Query = "
              INSERT INTO articulos_precios
              (
                fk_articulo, precio,
                rango_a, rango_b, borrar
               )
            	VALUES
              (
                '$Art[id]', '$Precio',
                '1', '99999', '0'
              )
            ";
            $Result = mysqli_query($mysqli, $Query);

          }

        }else{
          echo "<span style='color:#f00;'>No hay</span>";

          //Id de Proveedor
          $Query = "
      			SELECT
      				pro.id
      			FROM proveedores pro
      			WHERE pro.nombre = '$Proveedor'
      		";
          $rQuery  = mysqli_query($mysqli, $Query);
          $Pro   = mysqli_fetch_assoc($rQuery);
          if( $Pro[id] ){

            //Id de Marca
            $Query = "
        			SELECT
        				mar.id
        			FROM marcas mar
        			WHERE mar.nombre = '$Marca'
        		";
            $rQuery  = mysqli_query($mysqli, $Query);
            $Mar   = mysqli_fetch_assoc($rQuery);
            if( $Mar[id] ){

              echo "<br>";

              //Inserta articulo
              echo $Query = "
                INSERT INTO articulos
                (
                  fk_proveedor, fk_marca,
                  codigo, nombre, descripcion,
                  activo, principal
                 )
              	VALUES
                (
                  '$Pro[id]', '$Mar[id]',
                  '$Codigo', '$Descripcion', '$Descripcion',
                  '1', '1'
                )
              ";
              $Result = mysqli_query($mysqli, $Query);
              echo "<br>Id: ";
              echo $idArticulo = mysqli_insert_id($mysqli);


            }

          }


        }

				echo "<hr>";

			}

		}

  }

?>
