<?php
//user_fetch.php
include('db_con.php');
if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
  header('Location: login');
  die();
}
$sql = "SELECT IFNULL(doc_status,'ON') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_adjust_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_adjust_edit = $statement->fetchColumn();
            $sql = "SELECT IFNULL(doc_status,'ON') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_adjust_print'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_adjust_print = $statement->fetchColumn();
            $sql = "SELECT IFNULL(doc_status,'ON') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_adjust_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_adjust_delete = $statement->fetchColumn();
    
$query = '';
$output = array();
$query .= "
select a.tr_id,a.dated,a.item_no,CONCAT(t.type_name, ' ',i.item_name) item_name,s.store_name,u.user_name,a.qty_in,a.qty_out
from adjusted_stock a,items i,store s,item_type t, user_detail u WHERE i.item_id=a.item_no and i.type_id=t.type_id and a.store_id=s.store_id and u.user_id=a.user_id and";
if(isset($_POST["search"]["value"]))
{
	$query .= '(item_name LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR a.tr_id LIKE "%'.$_POST["search"]["value"].'%"';
	$query .= 'OR item_name LIKE "%'.$_POST["search"]["value"].'%"';
	$query .= 'OR user_name LIKE "%'.$_POST["search"]["value"].'%"';
	
	
	$query .= 'OR store_name LIKE "%'.$_POST["search"]["value"].'%" )';
	
	
}
if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY tr_id DESC ';
}
if($_POST["length"] != -1)
{
	$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
	
	$sub_array = array();
	$sub_array[] = $row['tr_id'];	
	$sub_array[] = $row['dated'];
	$sub_array[] = $row['item_name'];	
	$sub_array[] = $row['store_name'];
	$sub_array[] = $row['user_name'];
	
	$sub_array[] = $row['qty_in'];
	$sub_array[] = $row['qty_out'];
	
	
	
	if($items_adjust_edit=='ON')
	{
	$sub_array[] = '<a href="item_adjust_edit.php?update=1&id='.$row["tr_id"].'"><span class="btn btn-warning"><i class="fa fa-edit"></i></span></a>';
	}
	
		
	if($items_adjust_delete=='ON')
	{
	$sub_array[] = '<button type="button" name="delete" id="'.$row["tr_id"].'" class="btn btn-danger delete" ><i class="fa fa-close"></i></button>';
	}
	$sub_array[] = '<a href="item_adjust_print?print=1&id='.$row["tr_id"].'"><span class="btn btn-success"><i class="fa fa-print"></i></span></a>';

	$data[] = $sub_array;
}
$output = array(
	"draw"				=>	intval($_POST["draw"]),
	"recordsTotal"  	=>  $filtered_rows,
	"recordsFiltered" 	=> 	get_total_all_records($connect),
	"data"    			=> 	$data
);
echo json_encode($output);
function get_total_all_records($connect)
{
	$statement = $connect->prepare("SELECT * FROM adjusted_stock");
	$statement->execute();
	return $statement->rowCount();
}
?>