
<?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['tabla'] = "WTR1";

 $_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) 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'];
    
  }
  
  $sql="select * from (
select top(1)  'bodega DK' as whsname ,oitw.itemcode,isnull(b.fecha,'2000-01-01') as fecha,isnull(b.cant,0) as cant,isnull(b.whscode,'01') as whscode ,onhand from oitw


left outer join (
select top (1) itemcode,max(actdeldate) as fecha,sum(pdn1.quantity*numpermsr) as cant,whscode from pdn1 

where docdate>'2000-01-01' and itemcode='".$_SESSION['itemcode1']."'  and whscode='01'
group by itemcode,actdeldate,whscode order by actdeldate desc ) b on  b.itemcode=oitw.itemcode 

where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and oitw.whscode='01'

union

select top(1)  'Sampson 1' as whsname ,oitw.itemcode,isnull(b.fecha,'2000-01-01') as fecha,isnull(b.cant,0) as cant,isnull(b.whscode,'06') as whscode ,onhand from oitw


left outer join (
select top (1) itemcode,max(shipdate) as fecha,sum(".$_SESSION['tabla'] .".quantity*numpermsr) as cant,whscode from ".$_SESSION['tabla'] ." 

where docdate>'2000-01-01' and itemcode='".$_SESSION['itemcode1']."'  and whscode='06'
group by itemcode,shipdate,whscode order by shipdate desc) b on  b.itemcode=oitw.itemcode 

where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and oitw.whscode='06'



union

select top(1)  'Sampson 2' as whsname ,oitw.itemcode,isnull(b.fecha,'2000-01-01') as fecha,isnull(b.cant,0) as cant,isnull(b.whscode,'07') as whscode ,onhand from oitw


left outer join (
select top (1) itemcode,max(shipdate) as fecha,sum(".$_SESSION['tabla'] .".quantity*numpermsr) as cant,whscode from ".$_SESSION['tabla'] ." 

where docdate>'2000-01-01' and itemcode='".$_SESSION['itemcode1']."'  and whscode='07'
group by itemcode,shipdate,whscode order by shipdate desc) b on  b.itemcode=oitw.itemcode 

where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and oitw.whscode='07'


union

select top(1)  'Sampson 3' as whsname ,oitw.itemcode,isnull(b.fecha,'2000-01-01') as fecha,isnull(b.cant,0) as cant,isnull(b.whscode,'09') as whscode ,onhand from oitw


left outer join (
select top (1) itemcode,max(shipdate) as fecha,sum(".$_SESSION['tabla'] .".quantity*numpermsr) as cant,whscode from ".$_SESSION['tabla'] ." 

where docdate>'2000-01-01' and itemcode='".$_SESSION['itemcode1']."'  and whscode='09'
group by itemcode,shipdate,whscode order by shipdate desc) b on  b.itemcode=oitw.itemcode 

where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and oitw.whscode='09'


union

select top(1)  'Marcas' as whsname ,oitw.itemcode,isnull(b.fecha,'2000-01-01') as fecha,isnull(b.cant,0) as cant,isnull(b.whscode,'5') as whscode ,onhand from oitw


left outer join (
select top (1) itemcode,max(actdeldate) as fecha,sum(pdn1.quantity*numpermsr) as cant,whscode from pdn1

where docdate>'2000-01-01' and itemcode='".$_SESSION['itemcode1']."'  and whscode='5'
group by itemcode,actdeldate,whscode order by actdeldate desc) b on  b.itemcode=oitw.itemcode 

where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and oitw.whscode='5'




) m order by m.whscode asc";

//echo "paso".$sql;
  $result = mssql_query($sql);

$return_arr = array();

$dep="01";

$fecha1="2000-01-01";
$fecha2="2000-01-01";
$fecha3="2000-01-01";
$fecha4="2000-01-01";
$fecha5="2000-01-01";
$fecha6="2000-01-01";

$cant1="0";
$cant2="0";
$cant3="0";
$cant4="0";
$cant5="0";
$cant6="0";
	$date1="0";


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

   
    $dep= $row['whscode'];
    
    
         if($dep=='01') {$fecha1=$row['fecha'];$cant1=$row['cant'];
         
      //     echo "dep = ".$dep;
         
       //   echo "Fecha= ".$fecha1;
       $date = date_create($row['fecha']);
    
    	$date1= date_format( $date, 'd/m/y');
    
    
         
         };
    
         if($dep=='06') {$fecha2=$row['fecha'];$cant2=$row['cant'];
         
        $date = date_create($row['fecha']);
    
    	$date2= date_format( $date, 'd/m/y');
    
      };
    
         if($dep=='07') {$fecha3=$row['fecha'];$cant3=$row['cant'];
           $date = date_create($row['fecha']);
    
    	$date3= date_format( $date, 'd/m/y');
    };
     
         if($dep=='04') {$fecha4=$row['fecha'];$cant4=$row['cant'];
           $date = date_create($row['fecha']);
    
    	$date4= date_format( $date, 'd/m/y');
    };

         if($dep=='5') {$fecha5=$row['fecha'];$cant5=$row['cant'];
           $date = date_create($row['fecha']);
    
    	$date5= date_format( $date, 'd/m/y');
    };
    
       if($dep=='09') {$fecha6=$row['fecha'];$cant6=$row['cant'];
           $date = date_create($row['fecha']);
    
    	$date6= date_format( $date, 'd/m/y');
    };
    
}
  
  $sql2="select * from ( select top(1)  ' Bodega dk' as whsname ,oitw.itemcode,'".	$date1."' as fecha, '".$cant1."' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'01') as whscode

,isnull(round(((b.cant/(b.cant+onhand))*100),2),0) as porc, DATEDIFF(day, '".$fecha1."', getdate()) dias
 from oitw


left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='".$fecha1."' and itemcode='".$_SESSION['itemcode1']."' and whscode='01' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and   oitw.whscode='01'

union 

select top(1)  ' Sampson 1' as whsname ,oitw.itemcode,'".$date2."' as fecha, '".$cant2."' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'06') as whscode

,isnull(round(((b.cant/(b.cant+onhand))*100),2),0) as porc, DATEDIFF(day, '".$fecha2."', getdate()) dias
 from oitw


left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='".$fecha2."' and itemcode='".$_SESSION['itemcode1']."' and whscode='06' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and  oitw.whscode='06'  

union

select top(1)  ' Sampson 2' as whsname ,oitw.itemcode,'".$date3."' as fecha, '".$cant3."' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'07') as whscode

,isnull(round(((b.cant/(b.cant+onhand))*100),2),0) as porc, DATEDIFF(day, '".$fecha3."', getdate()) dias
 from oitw


left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='".$fecha3."' and itemcode='".$_SESSION['itemcode1']."' and whscode='07' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and  oitw.whscode='07'  

union

select top(1)  ' Sampson 3' as whsname ,oitw.itemcode,'".$date6."' as fecha, '".$cant6."' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'09') as whscode

,isnull(round(((b.cant/(b.cant+onhand))*100),2),0) as porc, DATEDIFF(day, '".$fecha6."', getdate()) dias
 from oitw


left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='".$fecha6."' and itemcode='".$_SESSION['itemcode1']."' and whscode='09' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and  oitw.whscode='09'  


union

select top(1)  ' Marcas' as whsname ,oitw.itemcode,'".$date5."' as fecha, '".$cant5."' as entrada ,isnull(b.cant,0) as vendidos,onhand , isnull(b.whscode,'5') as whscode

,isnull(round(((b.cant/(b.cant+onhand))*100),2),0) as porc, DATEDIFF(day, '".$fecha5."', getdate()) dias
 from oitw


left outer join (select itemcode,sum(quantity) as cant,whscode from dbo.inv1 

where dbo.inv1.docdate>='".$fecha5."' and itemcode='".$_SESSION['itemcode1']."' and whscode='5' group by itemcode,whscode) b on b.itemcode=oitw.itemcode 


where dbo.oitw.itemcode='".$_SESSION['itemcode1']."' and  oitw.whscode='5'  





 ) m order by m.whscode     ";
$result = mssql_query($sql2);

//echo $sql2;

$return_arr = array();

//echo $result;

//echo hola;
$dias=0;
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'];

$dias=$row['dias'];
if($dias>6000) {
$row_array['dias'] = "0";
}else {
$row_array['dias'] = $dias;
}
$row_array['cantr'] = "0";



    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);
 
?>