<?php
////////////////////////////////////////////////////
//Convierte fecha de normal a mysql
////////////////////////////////////////////////////

function cambiaf_a_mysql($fecha){
    ereg( "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})", $fecha, $mifecha);
    $lafecha=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1];
    return $lafecha;
} 

function cambiaf_a_mysql_guion($fecha){
    ereg( "([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})", $fecha, $mifecha);
    $lafecha=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1];
    return $lafecha;
} 

//--------------------- Class CSVImporter
class CSVImporter {

var $table;
var $Csv;
var $truncate;
var $cnx;


function CSVImporter ($table, $Csv, $Repres, $truncate, $cnx)
{
	$this->table = $table;
	$this->Csv = $Csv;
	$this->Repres = $Repres;
	$this->truncate = $truncate;
	$this->cnx = $cnx;
	$this->contador = 1;
}



function result ()
{
	$sql = "SELECT * FROM ".$this->table;
	$result = mysql_query ($sql, $this->cnx);	
	return $result;
}

function buscar($rep)
{
	$client = "SELECT idclientes FROM clientes WHERE (nombre like '%".$rep."%') LIMIT 0,1";
	$results = mysql_query ($client, $this->cnx);	
	while($row = mysql_fetch_array($results)){ 
	$idclientes="'".$row["idclientes"]."'";
	} 
	return $idclientes;
}

function validar($nrofact,$idrestipo,$importe)
{
	$client = "SELECT importe,idrestipo FROM resumen WHERE nro_fact ='".$nrofact."' LIMIT 0,1";
	$resultado = mysql_query ($client, $this->cnx);	
	while($row = mysql_fetch_array($resultado)){ 
	$price= $row["importe"];
	$idr= "'".$row["idrestipo"]."'";
	} 
	if($importe < 0) {
	$importe = ($importe * -1);
	}
	if (empty($price)){
	$retorno = 1;
	}else {
	if ($price == $importe){
	    if ($idrestipo == $idr){
			$retorno = 0;
			} else {
			$retorno = 1;
			}
	} else {
	$retorno = 1;
	}
	}
	return $retorno;
}

function select_tipo($tipo,$representacion)
{
   switch ($representacion)
   {
   case 11:
	if ($tipo == 'RIC'){
		$valor = "'1'";
		}else if ($tipo == 'FA5'){
		$valor = "'2'";
		}else if ($tipo == 'FA7'){
		$valor = "'2'";
		}else if ($tipo == 'DEV'){
		$valor = "'4'";
		}
		return $valor;
		break;
	case 22:
		if ($tipo == 'COB'){
		$valor = "'1'";
		}else if ($tipo == 'FAC'){
		$valor = "'2'";
		}else if ($tipo == 'NDE'){
		$valor = "'3'";
		}else if ($tipo == 'NCR'){
		$valor = "'4'";
		}
		echo $valor;
		return $valor;
		break;
	}
}



function listFields()
{
$result = $this->result ();
$numFields = mysql_num_fields ($result);

	for ($i=0; $i < $numFields; $i++)
	{
	$fields[] = mysql_field_name ($result, $i);
	}

return $fields;
}




function InsertData ($values)
{
$Representacion = $this->Repres;
///////////////////////////
//REPRESENTACION DAKOT/////
//////////////////////////
if ($Representacion == '11'){
$fields = array("idrecibo","idrestipo","nro_fact","nro_remesa","fecha_fac","importe","imp_desc","nro_recibo","tipo_fact","idclientes", "idrepresentacion");
$result = $this->result ();
$NroFact = $values [2];
$Tipo = $values [1];
$IdTipo = $this->select_tipo($Tipo,$Representacion);
$Importe = $values [5];
$Validacion = $this->validar($NroFact,$IdTipo,$Importe);
if ($Validacion == 1){

for ($i=0; $i <= 3; $i++)
{

$sql = "INSERT INTO ".$this->table." (";

for ($i=1; $i < count($fields); $i++)
{
$sql .= $fields [$i];

	if ($i < count ($fields) - 1)
	{
	$sql .= ", ";
	}
}

$sql .= ") 
	VALUES
(";

for ($i=1; $i <= 10; $i++)
{
    switch ($i) {
    case 1:
		$tipo = $values [$i];
		$sql .= $this->select_tipo($tipo,$Representacion);
        break;
    case 2:
		$sql .= "'".$values [$i]."'";
		break;
    case 3:
        $sql .= "''";
		break;
	case 4:
        $fecha = cambiaf_a_mysql($values [$i]);
		$sql .= "'$fecha'";
		break;
	case 5:
	    $valor = $values [$i];
        if ($valor >= 0)
		{
		$sql .= "'$valor'";
		}else{
		$valor = (-1 * $valor);
		$sql .= $valor;
		}
        break;
	case 6:
        $sql .= "''";
		break;
	case 7:
        $sql .= "''";
		break;
	case 8:
	    $sql .= "'".$values [$i]."'";
		break;	
	case 9:
		$rep = $values [$i];
	    $sql.= $this->buscar($rep);
		break;	

	case 10:
	    $sql .= "'".$this->Repres."'";
		break;		
}
	


	if ($i < count ($values))
	{
	$sql .= ", ";
	}

$n++;	

}

$sql .= ")";
}
}
}
//////////////////////////////
///REPRESENTACION SEALY //////
//////////////////////////////
else if ($Representacion == '22'){
$fields = array("idrecibo","idclientes","fecha_fac","idrestipo","nro_fact","nro_remesa","nro_recibo","importe","tipo_fact","idrepresentacion");
$result = $this->result ();
$NroFact = $values [4];
$Tipo = $values [3];
$IdTipo = $this->select_tipo($Tipo,$Representacion);
$Importe = $values [7];
$Validacion = $this->validar($NroFact,$IdTipo,$Importe);
if ($Validacion == 1){
for ($i=0; $i <= 3; $i++)
{

$sql = "INSERT INTO ".$this->table." (";

for ($i=1; $i < count($fields); $i++)
{
$sql .= $fields [$i];

	if ($i < count ($fields) - 1)
	{
	$sql .= ", ";
	}
}

$sql .= ") 
	VALUES
(";

for ($i=1; $i <= 9; $i++)
{
    switch ($i) {
	case 1:
		$rep = $values [$i];
	    $sql.= $this->buscar($rep);
		break;
	case 2:
        $fecha = cambiaf_a_mysql_guion($values [$i]);
		$sql .= "'$fecha'";
		break;
    case 3:
		$tipo = $values [$i];
		$sql .= $this->select_tipo($tipo,$Representacion);
        break;
    case 4:
		$sql .= "'".$values [$i]."'";
		break;
    case 5:
        $sql .= "''";
		break;
	case 6:
        $sql .= "''";
		break;
	case 7:
	    $valor = $values [$i];
        if ($valor >= 0)
		{
		$sql .= "'$valor'";
		}else{
		$valor = (-1 * $valor);
		$sql .= $valor;
		}
        break;
	
	case 8:
        $sql .= "'".$values [$i]."'";
		break;
		
	case 9:
	    $sql .= "'".$this->Repres."'";
		break;			
}
	


	if ($i < count ($values))
	{
	$sql .= ", ";
	}

$n++;	

}

$sql .= ")";
}
}
}
//////////////////////////////
///GARDEN LIFE  //////////////
//////////////////////////////
else if ($Representacion == '25'){
$fields = array("idrecibo","fecha_fac","nro_recibo","idclientes","nro_remesa","importe","tipo_fact","nro_fact","idrestipo","idrepresentacion");
$result = $this->result ();
$NroFact = $values [7];
$Tipo = "'".$values [8]."'";
$Importe = $values [5];
$Validacion = $this->validar($NroFact,$Tipo,$Importe);
if ($Validacion == 1){

for ($i=0; $i <= 3; $i++)
{

$sql = "INSERT INTO ".$this->table." (";

for ($i=1; $i < count($fields); $i++)
{
$sql .= $fields [$i];

	if ($i < count ($fields) - 1)
	{
	$sql .= ", ";
	}
}

$sql .= ") 
	VALUES
(";

for ($i=0; $i < 9; $i++)
{
    switch ($i) {
    
    case 0:
        $fecha = cambiaf_a_mysql($values [$i]);
		$sql .= "'$fecha'";
		break;
	case 1:
		$sql .= "''";
		break;
	case 2:
		$rep = $values [$i];
	    $sql.= $this->buscar($rep);
		break;	
    case 3:
        $sql .= "''";
		break;
	case 4:
	    $valor = $values [$i];
        if ($valor >= 0)
		{
		$sql .= "'$valor'";
		}else{
		$valor = (-1 * $valor);
		$sql .= $valor;
		}
        break;
	case 5:
		$sql .= "'".$values [$i]."'";
		break;
	case 6:
		$sql .= "'".$values [$i]."'";
		break;
	case 7:
		$sql .= "'2'";
		break;
	case 8:
	    $sql .= "'".$this->Repres."'";
		break;			
}
	


	if ($i <= count ($values))
	{
	$sql .= ", ";
	}

$n++;	

}

$sql .= ")";
}
}
}
//////////////////////////////
///F Y C INTERNACIONAL ///////
//////////////////////////////
else if ($Representacion == '29'){
$fields = array("idrecibo","fecha_fac","idclientes","idrestipo","nro_remesa","nro_recibo","importe","tipo_fact","nro_fact","idrepresentacion");
$result = $this->result ();
$NroFact = $values [8];
$Tipo = "'".$values [3]."'";
$Importe = $values [6];
$Validacion = $this->validar($NroFact,$Tipo,$Importe);
if ($Validacion == 1){

for ($i=0; $i <= 3; $i++)
{

$sql = "INSERT INTO ".$this->table." (";

for ($i=1; $i < count($fields); $i++)
{
$sql .= $fields [$i];

	if ($i < count ($fields) - 1)
	{
	$sql .= ", ";
	}
}

$sql .= ") 
	VALUES
(";

for ($i=1; $i < 10; $i++)
{
    switch ($i) {
	case 1:
        $fecha = cambiaf_a_mysql($values [$i]);
		$sql .= "'$fecha'";
		break;
	case 2:
		$rep = $values [$i];
	    $sql.= $this->buscar($rep);
		break;	
    case 3:
		$tipo = $values [$i];
		if ($tipo == 'FAC'){
		$sql .= "'2'";
		}else {
		$sql .= "''";
		}
        break;
	case 4:
        $sql .= "''";
		break;
	case 5:
        $sql .= "''";
		break;
	case 6:
	    $valor = $values [$i];
        if ($valor >= 0)
		{
		$sql .= "'$valor'";
		}else{
		$valor = (-1 * $valor);
		$sql .= $valor;
		}
        break;
	case 7:
		$sql .= "'".$values [$i]."'";
		break;
	case 8:
		$sql .= "'".$values [$i]."'";
		break;
	case 9:
	    $sql .= "'".$this->Repres."'";
		break;			
}
	


	if ($i < count ($values))
	{
	$sql .= ", ";
	}

$n++;	

}

$sql .= ")";
}
}
}
mysql_query ($sql, $this->cnx);
}



function TruncateTable ()
{

$sql='TRUNCATE table '.$this->table; 

if (mysql_query ($sql, $this->cnx)) { $exito="si"; }else{ $exito="no"; }

return $exito;

}


function Upload () {

ini_set ('auto_detect_line_endings','1');

	if ($this->truncate == "yes") 
	{ 
	$this->TruncateTable ();
	}

$row=1; 

$fp = fopen ($this->Csv, "r");


while ($data = fgetcsv ($fp, 1000, ";"))
{
	$this->InsertData ($data);
} 

fclose($fp);

}

}

//--------------------- Class CSVImporter

?>