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

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


$filename = "datamasterstk.xls"; // File Name
// Download file
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
//$user_query = mysqli_query($con,'select kode_cabang,kode,nama,harga,harga_beli from tbl_barang');


$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,'' as StockOpname from Tbstockopname group by item_code,item_desc";
$res40 = mysqli_query($conn, $sql40);

$flag = false;
while ($row = mysqli_fetch_assoc($res40)) {
    if (!$flag) {
        // display field/column names as first row
        echo implode("\t", array_keys($row)) . "\r\n";
        $flag = true;
    }
    echo implode("\t", array_values($row)) . "\r\n";
}




   
   
 
 

 
mysqli_close($conn);
 
?>
