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

//$date1=$_GET['date1'];
//$date2=$_GET['date2'];
$kodecab=$_GET['kodecab'];
$item_code=$_GET['item_code'];

//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));
 
/* 
$sql = "

SELECT 'A' as grp, date_format(tgl_rec,'%d-%m-%Y') ,notrans_link,item_code,item_desc,0,0,0 FROM Trans1sub where date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and kode_cabang='".$kodecab."' group by notrans_link,item_code,item_desc,harga_jual,harga_beli 
union all
SELECT 'B' as grp, date_format(tgl_rec,'%d-%m-%Y') ,notrans_link,item_code,item_desc,0,0,0 FROM Trans1sub where date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and kode_cabang='".$kodecab."' group by notrans_link,item_code,item_desc,harga_jual,harga_beli 
union all
SELECT  'C' as grp, date_format(tgl_rec,'%d-%m-%Y') ,notrans_link,item_code,item_desc,sum(qty),harga_jual,harga_beli FROM Trans1sub where date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and kode_cabang='".$kodecab."' group by notrans_link,item_code,item_desc,harga_jual,harga_beli order by notrans_link,item_code,item_desc,grp asc" ; 

$sql2 = "SELECT sum(harga_jual*qty)-sum(disc_val) as totrp,sum(disc_val) as discount FROM Trans1sub  where date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."'  order by idno desc" ;  

$res2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_row($res2);*/







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


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


/*
if($res === false) {
  echo "error while executing mysql: " . mysqli_error($conn);
}
 
 while($row = mysqli_fetch_row($res3))
 
 {
 echo "<tr><td align='center'>".$row[0]."</td><td align='center'>".$row[7]."</td><td align='center'>".$row[6]."</td></tr>";
     
 }*/
 
 $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);
 
?>
