
<?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('dkfadbnew', $link))
    die('Unable to select database!');
    
    
  $resultcodigo = mssql_query("  select top(1) itemcode from oitm where itemcode='".$_SESSION['itemcode']."' or codebars='".$_SESSION['itemcode']."' or suppcatnum='".$_SESSION['itemcode']."' order by itemcode ");
    
    while ($row = mssql_fetch_array($resultcodigo)) {
    	
    	$_SESSION['itemcode1']=$row['itemcode'];
    	
    	$_SESSION['itemcode'] =$row['itemcode'];
    	
  }
  
 
$result = mssql_query("
 
select top(1)  'Bodega DK' as whsname ,oitm.itemcode,isnull(b.cant,0) as cant7,isnull(c.cant,0) as cant30,isnull(d.cant,0) as cant60,isnull(e.cant,0) as cant90 from oitm

left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-7,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='01'  group by itemcode) b on b.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-30,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='01'  group by itemcode) c on c.itemcode=oitm.itemcode 
 
 
 left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-60,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='01'  group by itemcode) d on d.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-90,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='01'  group by itemcode) e on e.itemcode=oitm.itemcode 
 
 where oitm.itemcode='".$_SESSION['itemcode']."'
 
 union 
 
select top(1)   'Sampson 1' as whsname ,oitm.itemcode,isnull(b.cant,0) as cant7,isnull(c.cant,0) as cant30,isnull(d.cant,0) as cant60,isnull(e.cant,0) as cant90 from oitm

left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-7,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='06'  group by itemcode) b on b.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-30,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='06'  group by itemcode) c on c.itemcode=oitm.itemcode 
 
 
 left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-60,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='06'  group by itemcode) d on d.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-90,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='06'  group by itemcode) e on e.itemcode=oitm.itemcode 
 
  where oitm.itemcode='".$_SESSION['itemcode']."'
  
  
  
 union 
 
select top(1)   'Sampson 2' as whsname ,oitm.itemcode,isnull(b.cant,0) as cant7,isnull(c.cant,0) as cant30,isnull(d.cant,0) as cant60,isnull(e.cant,0) as cant90 from oitm

left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-7,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='07'  group by itemcode) b on b.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-30,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='07'  group by itemcode) c on c.itemcode=oitm.itemcode 
 
 
 left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-60,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='07'  group by itemcode) d on d.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-90,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='07'  group by itemcode) e on e.itemcode=oitm.itemcode 
 
  where oitm.itemcode='".$_SESSION['itemcode']."'
 
 union 
 
select top(1)   'Sampson 3' as whsname ,oitm.itemcode,isnull(b.cant,0) as cant7,isnull(c.cant,0) as cant30,isnull(d.cant,0) as cant60,isnull(e.cant,0) as cant90 from oitm

left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-7,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='09'  group by itemcode) b on b.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-30,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='09'  group by itemcode) c on c.itemcode=oitm.itemcode 
 
 
 left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-60,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='09'  group by itemcode) d on d.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-90,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='09'  group by itemcode) e on e.itemcode=oitm.itemcode 
 
  where oitm.itemcode='".$_SESSION['itemcode']."'
 
 union 
 
select top(1)   'Marcas' as whsname ,oitm.itemcode,isnull(b.cant,0) as cant7,isnull(c.cant,0) as cant30,isnull(d.cant,0) as cant60,isnull(e.cant,0) as cant90 from oitm

left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-7,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='5'  group by itemcode) b on b.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-30,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='5'  group by itemcode) c on c.itemcode=oitm.itemcode 
 
 
 left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-60,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='5'  group by itemcode) d on d.itemcode=oitm.itemcode 
 
 
left outer join (select top(1)  itemcode,sum(inv1.quantity) as cant

 from dbo.inv1
 where  dbo.INV1.DocDate>=DATEADD(day,-90,getdate()) 

 and itemcode='".$_SESSION['itemcode']."' and whscode='5'  group by itemcode) e on e.itemcode=oitm.itemcode 
 
  where oitm.itemcode='".$_SESSION['itemcode']."'
 
 
");

$return_arr = array();

while ($row = mssql_fetch_array($result)) {
    
   
    $row_array['whsname'] = $row['whsname'];
  $row_array['itemcode'] = $row['itemcode'];
  
//echo $row['codebars'];
  $row_array['cant7'] = $row['cant7'];

  $row_array['cant30'] =utf8_encode($row['cant30']);

    $row_array['cant60'] = $row['cant60'];
    $row_array['cant90'] = $row['cant90'];
      


    array_push($return_arr,$row_array);


}


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