<?php
/*
Hay productos que se desactivan en la web pero en cambio sí que están marcardos como producto web en Navision. Pasamos este proceso a diario por si se dan nuevos casos
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
date_default_timezone_set('Europe/Madrid');
include("../conexion.php");

$discordchannel="935430617842196601";


function obtenerIDProducto($sku, $conn){
	$stmt = $conn->prepare("SELECT id_product FROM fcproduct WHERE reference='$sku'");
	$stmt->execute();
	$result = $stmt->get_result();
	if($row_m = $result->fetch_assoc()) return $row_m['id_product'];
		else return -1;
}


function mssql_escape($str)
{
    if(get_magic_quotes_gpc())
    {
        $str= stripslashes($str);
    }
    return str_replace("'", "''", $str);
}


$connPrestashop = new mysqli($bdd_servidor, $bdd_usuario, $bdd_contrasena, $bdd_base_datos);
	if( $connPrestashop ) {
			 echo "Conexion establecida con Prestashop <br>";
	}else{
			 echo "Conexion no se pudo establecer con Prestashop.<br>";
			 die( print_r( sqlsrv_errors(), true));
}
$serverName = "localhost";
$connectionInfo = array( "Database"=>"ICF", "UID"=>"VAIO", "PWD"=>"VAIO");

$connNavision=false;
$retries=0;
while(!$connNavision && $retries<3){
	$connNavision = sqlsrv_connect( $serverName, $connectionInfo);
	$retries++;
}


	if( $connNavision ) {
		 //echo "Conexion establecida con Navision.\r\n";
	}else{
		 //echo "Conexion no se pudo establecer con Navision.\r\n";
		 die( print_r( sqlsrv_errors(), true));
	}
		
		$arrayProductosPrestashop=array();
		
	
		$sql = "SELECT p.id_product, p.reference
				FROM fcproduct p
				LEFT JOIN fcproduct_shop ps
				ON ps.id_product=p.id_product
				LEFT JOIN fcstock_available s
				ON s.id_product=p.id_product
				WHERE ps.active=0  AND p.id_manufacturer!=538 AND p.id_manufacturer!=533
				AND s.out_of_stock!=0
				GROUP BY p.id_product";
		  
		$stmtPrestashop = $connPrestashop->prepare($sql);
		$stmtPrestashop->execute();
		$resultPrestashop = $stmtPrestashop->get_result();
		while($rowPrestashop = $resultPrestashop->fetch_assoc()){
			$arrayProductosPrestashop[$rowPrestashop["reference"]]=$rowPrestashop;
		}
		
		$cont=0;
			
		foreach($arrayProductosPrestashop as $productoPrestashop) {
			
			/*if($productoPrestashop['reference']=="20388118")
			{*/
				$sku=($productoPrestashop['reference']);
				
				$sql = 'SELECT *
					FROM dbo.Item	
					WHERE [No.]=\''.$sku.'\' and [ProductoWEB]=1';				
				
				
				$stmt = sqlsrv_query( $connNavision, $sql );
				if( $stmt !== false) {
					if($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){		
						
						
						$sql = "UPDATE `fcproduct` SET active=1 WHERE id_product=".$productoPrestashop['id_product'];
						$stmtPrestashop = $connPrestashop->prepare($sql);
						$stmtPrestashop->execute();	

						$sql = "UPDATE `fcproduct_shop` SET active=1 WHERE id_product=".$productoPrestashop['id_product'];
						$stmtPrestashop = $connPrestashop->prepare($sql);
						$stmtPrestashop->execute();		
						$cont++;					
									
					}					
				}				
			/*}*/
		}
		
		if($cont>0) file_get_contents("https://icfbot.ferreteriacampollano.com/message.php?channel=".$discordchannel."&msg=".urlencode(":warning:"."SCRIPT ".basename(__FILE__, '.php').": Se han encontrado ".$cont." productos desactivados en PS y marcados como ProductoWEB en Nav"));
		
		$connPrestashop->close();
		echo " \r\n \r\Finalizado \r\n"; 
?>
