<?php
 
include 'dbconfig.php';
 
// Create connection

//$date1=$_GET['date1'];
//$date2=$_G['date2'];
$item_code=$_POST['item_code'];
$kodecab=$_POST['kodecab'];
$groupho=$_POST['groupho'];
$namacab=$_POST['namacab'];

//echo $notrans;
 
$conn = mysqli_connect($servername, $username, $password, $dbname);
 
// Check connection
 
if (!$conn) {
 
    die("Connection failed: " . mysqli_connect_error());
 
}
 
$sql3="select * from toko_profile";
$res3 = mysqli_query($conn, $sql3);
$row3 = mysqli_fetch_row($res3);
$dateok=date('d-m-Y',strtotime(tgl_rec));
 







$sql0="delete from  Trans1subtemp";
$res0 = mysqli_query($conn, $sql0);


$sql2 ="

INSERT INTO Trans1subtemp (item_code,item_desc,AWALqty,INqty,TRFqty,POSqty,ADJqty,kode_cabang)


SELECT  item_code,item_desc,0,sum(qty) as Masuk,0,0,0,kode_cabang FROM Trans1sub where  kode_cabang='".$kodecab."' and typetrans='IN' and item_code='".$item_code."' group by item_code,item_desc 
union all
SELECT  item_code,item_desc,0,0,sum(qty) as Transfer,0,0,'".$kodecab."' FROM Trans1sub where typetrans='IN2' and item_code='".$item_code."' group by item_code,item_desc 
union all
SELECT  item_code,item_desc,0,0,0,sum(qty) as OT,0,kode_cabang FROM Trans1sub where  typetrans='OT' and kode_cabang='".$kodecab."' and item_code='".$item_code."' group by item_code,item_desc
union all
SELECT  item_code,item_desc,0,0,0,0,sum(qty) as ADJ,kode_cabang FROM Trans1sub where  typetrans='ADJ' and kode_cabang='".$kodecab."' and item_code='".$item_code."' group by item_code,item_desc";

$res = mysqli_query($conn, $sql2);

if($groupho=='1')
{
$sql3="select b.nama,sum(IFNULL(a.INQty,0))-sum(IFNULL(a.TRFqty,0))-sum(IFNULL(a.POSqty,0))+sum(IFNULL(a.ADJqty,0)) as stock,b.kode,b.harga as harga_jual,b.harga_beli,'".$namacab."' as namacab from tbl_barang b left join Trans1subtemp a on a.item_code=b.kode where a.kode_cabang='HO' or b.kode='".$item_code."'  group by b.kode,b.nama order by b.nama" ;
}else
{
$sql3="select b.nama,sum(IFNULL(a.INQty,0))+sum(IFNULL(a.TRFqty,0))-sum(IFNULL(a.POSqty,0))+sum(IFNULL(a.ADJqty,0)) as stock,b.kode,b.harga as harga_jual,b.harga_beli,'".$namacab."' as namacab from tbl_barang b left join Trans1subtemp a on a.item_code=b.kode where a.kode_cabang='HO' or b.kode='".$item_code."'  group by b.kode,b.nama order by b.nama" ;
}

$res3 = mysqli_query($conn, $sql3);


 
 $items = array();
 
 if (mysqli_num_rows($res3) > 0) {
 
    while($row = mysqli_fetch_object($res3)){
 
        array_push($items, $row);
 
//$result["errorcode"] = "1";
 
    $result["data"] = $items;
 
}}
else
{
 
       $result["data"] = "failed";
 
}
 
echo json_encode($result);
 
mysqli_close($conn);

   
   
 
 

 
?>
