<style type='text/css'>
#th1{
background-color:blue;    
color:white;
font-weight:bold;
padding:5px;
font-size:8px;
    
}   

#td1{
background-color:black;    
color:white;
font-weight:bold;
padding:5px;
}   
   
#td2{
color:black;
padding:5px;
}   

#td12{
color:white;
padding:5px;
background-color:blue;
}  

#tdh{
color:black;
font-weight:bold;
 
} 
</style>

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

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

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



$sql01="delete from  Trans1subtempawal";
$res01 = mysqli_query($conn, $sql01);

$sql1 ="

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

SELECT  item_code,item_desc,0,sum(qty) as Masuk,0,0,0,$kodecab FROM Trans1sub where date(tgl_rec)<'".$date1."' and kode_cabang='".$kodecab."' and typetrans='IN' 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 date(tgl_rec)<'".$date1."' and typetrans='IN2' group by item_code,item_desc 
union all
SELECT  item_code,item_desc,0,0,0,sum(qty) as OT,0,$kodecab FROM Trans1sub where date(tgl_rec)<'".$date1."' and typetrans='OT' and kode_cabang='".$kodecab."' group by item_code,item_desc
union all
SELECT  item_code,item_desc,0,0,0,0,sum(qty) as ADJ,$kodecab FROM Trans1sub where date(tgl_rec)<'".$date1."' and typetrans='ADJ' and kode_cabang='".$kodecab."' group by item_code,item_desc";

$res1 = mysqli_query($conn, $sql1);





$sql2 ="

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

SELECT  item_code,item_desc,sum(AWALqty)+sum(INQty)-sum(TRFqty)-sum(POSqty)+sum(ADJqty),0,0,0,0,'".$kodecab."' from Trans1subtempawal where kode_cabang='".$kodecab."' group by item_code,item_desc 
union all
SELECT  item_code,item_desc,0,sum(qty) as Masuk,0,0,0,kode_cabang FROM Trans1sub where date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and kode_cabang='".$kodecab."' and typetrans='IN' 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 date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and typetrans='IN2' 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 date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and typetrans='OT' and kode_cabang='".$kodecab."' 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 date(tgl_rec)>='".$date1."'"." and date(tgl_rec)<='".$date2."' and typetrans='ADJ' and kode_cabang='".$kodecab."' group by item_code,item_desc";

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


$sql3="select item_desc,sum(AWALqty),sum(INQty),sum(TRFqty),sum(POSqty),sum(ADJqty),sum(AWALqty)+sum(INQty)+sum(TRFqty)-sum(POSqty)+sum(ADJqty),item_code from Trans1subtemp where kode_cabang='".$kodecab."' group by item_code,item_desc order by item_desc ";
$res3 = mysqli_query($conn, $sql3);

echo "<table>";
echo "<tr><td id='tdh'>Stock Mutasi Cabang ".$kodecab." By Date</td></tr>";
echo "<tr><td id='tdh'>Date : ".date('d-M-Y',strtotime($date1))." To ".date('d-M-Y',strtotime($date2))."</td></tr>";
echo "</table>";
echo "<br>";
echo "<table border='1'>";
echo "<th id='th1'>Item Desc</th><th id='th1'>Stock Awal</th><th id='th1'>Stock Masuk</th><th id='th1'>Transfer Stock</th><th id='th1'>POS</th><th id='th1'>ADJ</th><th id='th1'>Stock Akhir</th>";


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[1]."</td><td align='center'>".$row[2]."</td><td align='center'>".$row[3]."</td><td align='center'>".$row[4]."</td><td align='center'>".$row[5]."</td><td align='center'>".$row[6]."</td></tr>";
     
 }
 

   
   
 
 

 
mysqli_close($conn);
 
?>
