<?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('PhpExcel/Classes/PHPExcel.php');

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

		$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 && $Precio ){

        echo $Proveedor;
        echo "<br>";
        echo $Codigo;
        echo "<br>";
        echo $Precio;

        //Articulo
        $Query = "
    			SELECT
    				art.id
    			FROM articulos art
    			WHERE art.codigo = '$Codigo'
    		";
        $rQuery  = mysqli_query($mysqli, $Query);
        $Art   = mysqli_fetch_assoc($rQuery);
        if( $Art[id] ){

          //Delete Precios
          $Query = "
            DELETE FROM articulos_precios
            WHERE
      				fk_articulo = '$Art[id]'
          ";
          $Result = mysqli_query($mysqli, $Query);

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

        }else {

        }

      }

		}

  }

?>
