<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'];
$nopos=$_POS['nopos'];
$kodecab='HO';

//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='GR' 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' 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."' 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."' group by item_code,item_desc";

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


$sql30="delete from Tbstockopname";
$res30 = mysqli_query($conn, $sql30);

$sql3="
insert into Tbstockopname (item_code,item_desc,Qtystock,Qtystk,Qtyadj,kode_cabang)
select a.item_code,a.item_desc,sum(a.INqty)-sum(a.POSqty)+sum(a.ADJqty),0,sum(a.ADJqty),'".$kodecab."'  from Trans1subtemp a where  a.kode_cabang='".$kodecab."' group by item_code,item_desc
union all
select item_code,item_desc,0,sum(qty),0,'".$kodecab."' from Trans1sub where  kode_cabang='".$kodecab."' and typetrans='STK' and notrans_link like '%$nopos%' group by item_code,item_desc

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

$sql40="select item_code, item_desc,sum(Qtystock),sum(Qtystk),sum(Qtyadj),
(case when sum(Qtystk)=0 then 0 else (sum(Qtystock)-sum(Qtystk)) end )
,kode_cabang from Tbstockopname group by item_code,item_desc";
$res40 = mysqli_query($conn, $sql40);

echo "<table>";
echo "<tr><td id='tdh'>Stock Opname HO ".$kodecab." By Date</td></tr>";
echo "<tr><td id='tdh'>Date : ".date('d-M-Y')."</td></tr>";
echo "</table>";
echo "<br>";
echo "<table border='1'>";
echo "<th>Item Desc</th><th>Stock Akhir</th><th>Stock Opname</th><th>Adjustment</th><th>Selisih</th>";


if($res === false) {
  echo "error while executing mysql: " . mysqli_error($conn);
}
 
 while($row = mysqli_fetch_row($res40))
 
 {
 echo "<tr><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></tr>";
     
 }
 

   
   
 
 

 
mysqli_close($conn);
 
?>
