
<?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.0.0.50', 'sa', 'sap123');
$_SESSION['itemcode'] = $_GET['itemcode'];

$_SESSION['itemcode1'] ="";
 
if (!$link)
    die('Unable to connect!');
 
if (!mssql_select_db('sbo_calzashoes_inc', $link))
    die('Unable to select database!');
    
    

  
 
$result = mssql_query("select  itemcode,onhand, CASE 
 WHEN whscode='01' THEN 1
      WHEN whscode='02' THEN 2
      WHEN whscode='03' THEN 3 
       WHEN whscode='04' THEN 4 
  WHEN whscode='05' THEN 5
   END as dep
	
	 from oitw where onhand>1 and (whscode>=01 and whscode<09)  ; ");

$return_arr = array();

$total=0; 
while ($row = mssql_fetch_array($result)) {
    
   
  
  $row_array['itemcode'] = $row['itemcode'];
    $row_array['dep'] = $row['dep'];
    
      $row_array['onhand'] = $row['onhand'];
  

   // array_push($return_arr,$row_array);
    
   
$trig=$row['dep']."-".$row['itemcode'];
 $SQLupdate="insert into 171_calzashoes.prod_existencia (pr_id,p_existencia_dep_id,p_existencia,trig_unic) values ('".$row['itemcode']."','".$row['dep']."',".$row['onhand'].",'".$trig."') 

ON DUPLICATE KEY UPDATE pr_id='".$row['itemcode']."',p_existencia_dep_id='".$row['dep']."',p_existencia='".$row['onhand']."'";


// 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;
}



}

echo 'termino'.   $total ;

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