
<?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!');
    
    
        
  $resultcodigo = mssql_query("  select top(1) oitm.itemcode, codebars, suppcatnum,itemname ,picturname,a.price as costo,b.price as precio from oitm 
  
LEFT OUTER JOIN itm1 a ON oitm.itemcode=a.itemcode AND a.PriceList=2
LEFT OUTER JOIN itm1 b ON oitm.itemcode=b.itemcode AND b.PriceList=10
  
  where oitm.itemcode='".$_SESSION['itemcode']."' or codebars='".$_SESSION['itemcode']."' or suppcatnum='".$_SESSION['itemcode']."' order by oitm.itemcode ");
    
    while ($row = mssql_fetch_array($resultcodigo)) {
    	
    	$_SESSION['itemcode1']=$row['itemcode'];
    	
    	$_SESSION['itemcode'] =$row['itemcode'];
    	
    		$_SESSION['codebars'] =$row['codebars'];
    			$_SESSION['suppcatnum'] =$row['suppcatnum'];
    			
    					$_SESSION['itemname'] =$row['itemname'];
    						$_SESSION['picturname'] =$row['picturname'];
    							$_SESSION['costo'] =$row['costo'];
    								$_SESSION['precio'] =$row['precio'];
    
  }
  
 
$result = mssql_query("select top(1)  'Tienda Megamall' as whsname ,oitw.itemcode,'26/07/2016' as fecha, '96' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'01') as whscode

,round(((b.cant/(b.cant+onhand))*100),2) as porc  from oitw left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='2016-07-26 00:00:00.000' and itemcode='sd43672' and whscode='01' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='sd43672'


union
select top(1)  'Tienda Albrook' as whsname ,oitw.itemcode,'28/09/2016' as fecha, '96' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'02') as whscode

,round(((b.cant/(b.cant+onhand))*100),2) as porc  from oitw left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='2016-07-26 00:00:00.000' and itemcode='sd43672' and whscode='01' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='sd43672'

");

$return_arr = array();

//echo $result;

//echo hola;
while ($row = mssql_fetch_array($result)) {
    
   
    $row_array['itemcode'] = $row['itemcode'];
  $row_array['codebars'] = $_SESSION['codebars'];
  
//echo $row['codebars'];
  $row_array['suppcatnum'] = $_SESSION['suppcatnum'];

  $row_array['itemname'] =utf8_encode(	$_SESSION['itemname']);
  
  $row_array['picturname'] =utf8_encode(	"http://www.dkfashionpa.com/olk/agent/pic.aspx?ID=59&Original=Y&filename=".$_SESSION['picturname']);

    $row_array['whscode'] = $row['whscode'];
    $row_array['whsname'] = $row['whsname'];
       $row_array['onhand'] = $row['onhand'];
    $row_array['Price'] = 	$_SESSION['costo'];
    $row_array['venta'] = 	$_SESSION['precio'];

    $row_array['fecha'] = $row['fecha'];
    $row_array['cant'] = $row['entrada'];
    $row_array['vendidos'] = $row['vendidos'];

$row_array['porc'] = $row['porc'];

$row_array['dias'] = "126";

$row_array['cantr'] = "1";



    array_push($return_arr,$row_array);

//$msg = array("itemcode" => $row['itemcode'], "whscode" => $row['whscode'],"whsname" => $row['whsname'],"onhand" => $row['onhand']);
}

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