<?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 = 'purchase_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_edit = $statement->fetchColumn();
     
              $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase_print'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_print = $statement->fetchColumn();
             $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_delete = $statement->fetchColumn();
$query = '';
$output = array();
$query .= "
select m.pr_id,m.inv_no,m.dated,v.acc_name,m.description, u.user_name,FORMAT(sum(d.qty*d.rate - d.rate * d.dis_pr / 100 * d.qty) + sum(d.qty*d.rate - d.rate * d.dis_pr / 100 * d.qty) * m.gst_pr / 100 + sum(d.qty*d.rate - d.rate * d.dis_pr / 100 * d.qty) * m.fst_pr / 100 + sum(d.qty*d.rate - d.rate * d.dis_pr / 100 * d.qty) * m.ast_pr / 100 + m.freight - m.discount,2)  amount from  pr m,pr_detail d,accounts v, user_detail u where m.pr_id = d.pr_id and m.v_id=v.acc_no AND m.user_id=u.user_id AND
";
if(isset($_POST["search"]["value"]))
{
	$query .= '(m.pr_id LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR m.dated LIKE "%'.$_POST["search"]["value"].'%" ';
	
	$query .= 'OR user_name LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR acc_name LIKE "%'.$_POST["search"]["value"].'%")';
	
	
}
if(isset($_POST["order"]))
{
	$query .= 'group by pr_id ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'group by pr_id ORDER BY pr_id DESC ';
}
$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['pr_id'];
	
	$sub_array[] = $row['dated'];
	$sub_array[] = $row['inv_no'];
	$sub_array[] = $row['acc_name'];
	
	
	$sub_array[] = $row['user_name'];
	
	$sub_array[] = $row['amount'];
	
	 
	
	
	if($purchase_edit=='ON'){
	$sub_array[] = '<a style=""href="purchase_return_edit?update=1&id='.$row["pr_id"].'" class="btn btn-warning"><i class="fa fa-edit"></i></a>';
	}
	$sub_array[] = '
	<a href="purchase_return_invoice_a4?print=1&id='.$row["pr_id"].'"><span class="btn btn-success"><i class="fa fa-print"></i></span></a>
	
	';
	if($purchase_delete=='ON'){
	$sub_array[] = '<button type="button" name="delete" id="'.$row["pr_id"].'" class="btn btn-danger delete" ><i class="fa fa-close"></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 pr");
	$statement->execute();
	return $statement->rowCount();
}
?>