<?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 doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'cash_invoices_edit'";



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



            $statement->execute(); // no need to add `$sql` here, you can take that out



            $cash_invoices_edit =$statement->fetchColumn();



           



		             



	    	 $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'cash_invoices_delete'";



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



		     $statement->execute(); // no need to add `$sql` here, you can take that out



		     $cash_invoices_delete =$statement->fetchColumn();



		     	 $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'cash_invoices_print'";



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



		     $statement->execute(); // no need to add `$sql` here, you can take that out



		     $cash_invoices_print =$statement->fetchColumn();



$query = '';



$output = array();



$query .= "



select m.s_id,m.inv_no,m.dated,m.cust_name,m.description,m.contact,u.user_name,FORMAT(sum(d.qty*d.rate - d.qty*d.dis_rate - d.rate * d.dis_pr / 100 * d.qty)   + sum(d.qty*d.rate - d.qty*d.dis_rate - d.rate * d.dis_pr / 100 * d.qty)  * gst_pr / 100 -m.discount + m.freight,2) as amount from  sale m,sale_detail d,user_detail u where m.s_id = d.s_id and m.user_id=u.user_id and m.inv_of='Retail'  AND m.branch_id = '".$branch_id."' AND



";



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



{



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



	$query .= 'OR m.dated LIKE "%'.$_POST["search"]["value"].'%" ';



	$query .= 'OR m.contact LIKE "%'.$_POST["search"]["value"].'%" ';



	$query .= 'OR m.s_id LIKE "%'.$_POST["search"]["value"].'%" ';



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



	



	



}



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



{



	$query .= 'group by s_id,inv_no,dated,m.cust_name,m.description ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';



}



else



{



	$query .= 'group by s_id,inv_no,dated,m.cust_name,m.description ORDER BY s_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['s_id'];



	



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



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



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



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



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



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



	



	



	



	 



	 if($cash_invoices_print=='ON'){



	$sub_array[] = '

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



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



	';



	}



	if($cash_invoices_edit=='ON'){



	$sub_array[] = '<a style="margin:5px"href="csale_edit?edit=1&id='.$row["s_id"].'"><span class="btn btn-warning"><i class="fa fa-edit"></i></span></a>';



	}



	if($cash_invoices_delete=='ON'){



	$sub_array[] = '<button type="button" name="delete" id="'.$row["s_id"].'" class="btn btn-danger btn-xs delete" ><i class="fa fa-remove"></i></button>';



	}



	$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 sale where inv_of ='Retail'");



	$statement->execute();



	return $statement->rowCount();



}



?>