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



}



$query = '';



$output = array();



$query .= "



SELECT m.do_id, DATE_FORMAT(m.dated,'%d-%m-%Y')as dated,m.s_id as sale_id,s.store_name,m.description



 from do m,do_detail d, store s WHERE m.do_id=d.do_id and m.store_id=s.store_id and d.store_id = '".$store_id."'



 and d.item_status = 'Not Approved'



 AND



";



if(isset($_POST["search"]["value"]))



{



	$query .= '(description LIKE "%'.$_POST["search"]["value"].'%" ';



	$query .= 'OR store_name LIKE "%'.$_POST["search"]["value"].'%")';



	



	



}



if(isset($_POST["order"]))



{



	$query .= 'group by m.do_id ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';



}



else



{



	$query .= 'group by m.do_id ORDER BY do_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['do_id'];



	$sub_array[] = $row['sale_id'];



	$sub_array[] = $row['dated'];



	$sub_array[] = $row['store_name'];



	$sub_array[] = $row['description'];



	



	 



	$sub_array[] = '<a href="do_print?print=1&id='.$row["do_id"].'"><span class="btn btn-success "><i class="fa fa-print"></i></span></a>



	<a href="sale_invoice_a4?print=1&id='.$row["sale_id"].'"><span class="btn btn-danger"><i class="fa fa-print"></i></span></a>



	<a href="sale_invoice_a5?print=1&id='.$row["sale_id"].'"><span class="btn btn-secondary"><i class="fa fa-print"></i></span></a>



	<a href="chalan?print=1&id='.$row["sale_id"].'"><span class="btn btn-primary"><i class="fa fa-print"></i></span></a>



	



	';



	$sub_array[] = '<a href="do_detail?detail=1&id='.$row["do_id"].'"><span class="btn btn-warning "><i class="fa fa-edit"></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)



{



	$sql = "SELECT * FROM do_detail";



	$statement = $connect->prepare($sql);



	$statement->execute();



	return $statement->rowCount();



}



?>