
<?php

include 'conn.php';

if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

//$link = mssql_connect('192.168.1.30', 'sa', 'sap123');
//$link = mssql_connect('10.1.0.18', 'rctech', 'rc.tech.2016');
$link = mssql_connect('10.0.0.50', 'sa', 'sap123');

$_SESSION['suc_id'] = $_GET['suc_id'];

$_SESSION['fini'] = $_GET['fini'];

$_SESSION['ffin'] = $_GET['ffin'];

$_SESSION['itemcode1'] ="";


 
if (!$link)
    die('Unable to connect!');
 
//if (!mssql_select_db('invpoca2017', $link))

if (!mssql_select_db('sbo_calzashoes_inc', $link))

    die('Unable to select database!');
    
    


if ($connection->query('truncate 171_calzashoes.tmpcomparesapr') === TRUE) {
  
} else {
	
 echo "Error: 1 truncate 171_calzashoes.tmpcomparesapr <br>" . $connection->error;
};


if ($connection->query('truncate 171_calzashoes.tmpcomparesap') === TRUE) {
  
} else {
	
 echo "Error: 2 truncate 171_calzashoes.tmpcomparesap<br>" . $connection->error;
};



$sqlinsert="insert into 171_calzashoes.tmpcomparesapr

SELECT op_id ,DATE_FORMAT(op_fecha, '%Y%m%d') dia,op_subtotalneto as totalneto,'".$_SESSION['suc_id']."' as suc_id from 171_calzashoes.operacion  where

op_fecha BETWEEN '".$_SESSION['fini'] ." 00:00:01' AND '".$_SESSION['ffin'] ." 23:59:59'  and suc_id=".$_SESSION['suc_id']." and op_tipo=2";
  
 
 if ($connection->query($sqlinsert) === TRUE) {
  
} else {
	
 echo "Error: 3 truncate 171_calzashoes.tmpcomparesap<br>" . $conn->error;
};

 
 $datei = new DateTime($_SESSION['fini']);
 
$fechai= date_format($datei, 'Y/m/d');
 
  $datef = new DateTime($_SESSION['ffin']);
 
$fechaf=  date_format($datef, 'Y/m/d');
 
 
//  $datef = new DateTime($_SESSION['ffin']);
 
 
 
$sql="SELECT top(100000) a.u_op_id as op_id,cast(a.DocDate as date) as dia,(doctotal-vatsum) as totalneto,'".$_SESSION['suc_id']."' as suc_id from oinv a where

cast(A.docdate as date) BETWEEN '". $fechai."' AND '". $fechaf."'  and left(a.u_op_id,1)='".$_SESSION['suc_id']."'  ";


//echo $sql;


$result = mssql_query($sql);

$return_arr = array();

$total=0; 

while ($row = mssql_fetch_array($result)) {
    
   


 $SQLupdate="insert into 171_calzashoes.tmpcomparesap (op_id,dia,totalneto,suc_id) values ('".$row['op_id']."','".$row['dia']."','".$row['totalneto']."','".$row['suc_id']."') ";


// echo "sql: " . $SQLupdate;



if ($connection->query($SQLupdate) === TRUE) {
    $msg = array("success" => "true", "detalle" => "Detalle ejemplo");
    
   // echo 'paso '.$row['itemcode'];
   
  $total=$total+1;

} else {
	
	$msg = array("success" => "false", "detalle" => $conn);

   // echo "Error: " . $SQLupdate . "<br>" . $conn->error;
}



}

$montoesql1='select a.dia as dia1,count(a.dia) as aperaciones1,sum(a.totalneto) totalneto1,
  b.dia,b.aperaciones,(b.totalneto) totalneto,(count(a.dia)-b.aperaciones) as diferencia from 171_calzashoes.tmpcomparesapr a

left outer join (select dia,count(*) as aperaciones,sum(totalneto) totalneto from 171_calzashoes.tmpcomparesap

group by dia) b on a.dia=b.dia group by a.dia';

//echo $montoesql1;

  $qur1 = $connection->query($montoesql1);
  
  $i=0;
  
 //  $jsonData[] ='';
 
   $jsonData      = array();
    $jsonTempData  = array();
     

while($r1 = mysqli_fetch_assoc($qur1)){
	
	 $jsonTempData = array();
       $jsonTempData['dia1']= utf8_encode($r1['dia1']);
       
        $jsonTempData['operaciones1']= utf8_encode($r1['aperaciones1']);
        
            $jsonTempData['totalneto1']= utf8_encode($r1['totalneto1']);
    
               
                $jsonTempData['operaciones2']= utf8_encode($r1['aperaciones']);
        
            $jsonTempData['totalneto2']= utf8_encode($r1['totalneto']);
    
         $jsonTempData['diferencia1']= utf8_encode($r1['diferencia']);
    
    
            
          //  echo 'entro'. utf8_encode($r1['operaciones1']);
            
      $jsonData[] = $jsonTempData;
}




//echo 'termino'.   $total ;

 header('content-type: application/json');
echo json_encode($jsonData);
//echo $json;
@mysqli_close($connection);
mssql_free_result($result);
 
?>
