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

$sql = "SELECT ifnull(doc_status,'OFF') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'accounts_receivable_payments_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $accounts_receivable_payments_edit = $statement->fetchColumn();
            $sql = "SELECT ifnull(doc_status,'OFF') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'accounts_receivable_payments_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $accounts_receivable_payments_delete = $statement->fetchColumn();
$query .= "
SELECT r.rec_id,r.dated,a.acc_name name,r.description, u.user_name,r.amount FROM rec_voucher r, accounts a, user_detail u WHERE u.user_id=r.user_id and
r.cust_id=a.acc_no AND r.paid_by='Cash' AND";
if(isset($_POST["search"]["value"]))
{
	$query .= '(rec_id LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR description LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR a.acc_name LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR r.dated LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR u.user_name LIKE "%'.$_POST["search"]["value"].'%" ';
	$query .= 'OR amount LIKE "%'.$_POST["search"]["value"].'%")';
	
	
}
if(isset($_POST["order"]))
{
	$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= 'ORDER BY rec_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['rec_id'];
	$sub_array[] = $row['dated'];
	$sub_array[] = $row['name'];
	
	$sub_array[] = $row['description'];
	$sub_array[] = $row['user_name'];
	$sub_array[] = $row['amount'];

	if($accounts_receivable_payments_edit=='ON')
	{


	
	
$sub_array[] = '<a href="ar_cash_receipt_edit.php?update=1&id='.$row["rec_id"].'"><span class="btn btn-warning"><i class="fa fa-edit"></i></span></a>';
}

	if($accounts_receivable_payments_delete=='ON')
	{

	$sub_array[] = '<button type="button" name="delete" id="'.$row["rec_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 rec_voucher");
	$statement->execute();
	return $statement->rowCount();
}
?>