<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
include ('../inc/repfunctions.php');
include ('../inc/invfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){    
$aid=$_SESSION['uid'];
$uty=$_SESSION['utype'];    
$brid=$_SESSION['abrid'];    
$today = strftime("%Y-%m-%d", time());    
$dtnow = date("Y-m-d h:i:s", time());    
}else{
header('Location:../index.php');
exit;    
}

function check_desales($invno){
$flage=0;
global $con;
$sql="SELECT * FROM tbl_trarecord WHERE invno!='$invno' AND refinv='$invno'";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
if($query->num_rows > 0) {
$flage=1;    
}else{
$flage=0;    
}

if($flage>0){
return $flage;
exit;    
}    
    
$sql="SELECT * FROM tbl_proreturn WHERE refinv='$invno'";    
$rquery=mysqli_query($con,$sql)or die(mysqli_error($con));
if($rquery->num_rows > 0) {
$flage=1;    
}else{
$flage=0;    
}

return $flage;    
}


$request=$_REQUEST;
if($uty=='1'){
$sql ="SELECT * FROM tbl_procha";
}else{
$sql ="SELECT * FROM tbl_procha WHERE brid='$brid'";    
}

$query=mysqli_query($con,$sql);

$totalData=mysqli_num_rows($query);

$totalFilter=$totalData;

//Search
$table='tbl_procha';
if($uty=='1'){
$sql_search = "SELECT * FROM ".$table." WHERE ";
}else{
$sql_search = "SELECT * FROM ".$table." WHERE brid='$brid' AND (";    
}
$sql_search_fields = Array();
$sql = "SHOW COLUMNS FROM ".$table;
$rs = mysqli_query($con,$sql);
while($r = mysqli_fetch_array($rs)){
$colum = $r[0];
$sql_search_fields[] = $colum." LIKE('%".$request['search']['value']."%')";
}
$sql_search .= implode(" OR ", $sql_search_fields);
if($uty=='1'){
$tsql = $sql_search;    
}else{
$tsql = $sql_search.')';    
}
$querys=mysqli_query($con,$tsql);
$totalData=mysqli_num_rows($querys);

//Order
//Order
if($uty=='1'){
$sql_search.=" ORDER BY apdate DESC,id DESC"."  LIMIT ".$request['start']."  ,".$request['length']."  ";    
}else{    
$sql_search.=") ORDER BY apdate DESC,id DESC"."  LIMIT ".$request['start']."  ,".$request['length']."  ";
}
$queryi=mysqli_query($con,$sql_search);

$data=array();
$s=0;
while($row=mysqli_fetch_array($queryi)){
    $ac='';
    $s+=1;
    $invno=$row['invno'];
    $subdata=array();
    $subdata[]=$s+$request['start'];//serial
    $subdata[]=date("d M Y", strtotime($row['apdate'])); //date
    if($uty=='1'){
    if($row['brid']==0){
    $subdata[]='Main Branch';    
    }else{
    $subdata[]=get_fild_data('tbl_branch',$row['brid'],'name');    
    }    
    }
   $subdata[]=show_addres($row['prjid'],$row['name'],'','','','','PJ_'.$row['pjid']);     

    $subdata[]=$row['invno'];
    $subdata[]=numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol'));
    $subdata[]=$row['note'];
    $ac.='<a class="btn btn-flat bg-purple details-invoice" style="margin: 3px;" href="#" id="inv_'.$row['id'].'_'.$row['pjid'].'_PJ'.'"><i class="fa fa-eye cat-child"></i></a>';
    if(!check_desales($invno)){
    $ac.='<a class="btn btn-flat bg-purple" style="margin: 3px;" href="#"><i class="fa fa-edit" onclick="take_action(\'ED_'.$row['id'].'\')"></i></a>';    
    }
    $ac.='<a class="btn btn-flat bg-purple" style="margin: 3px;" href="#" onclick="take_action(\'RE_'.$row['id'].'\')"><i class="fa fa-reply"></i></a>';
    if(!check_desales($invno)){
    $ac.='<a class="btn btn-flat bg-purple" style="margin: 3px;" href="#" onclick="remove_item(\'DL_'.$row['id'].'\')"><i class="fa fa-trash"></i></a>';    
    }
    $ac.='<form action="sel_useretun.php" id="RE_'.$row['id'].'" method="post" >';
    $ac.='<input type="hidden" name="selret" value="'.$row['id'].'" />';
    $ac.='</form>';
    if(!check_desales($invno)){
    $ac.='<form action="sel_uselist.php" id="DL_'.$row['id'].'" method="post" >';
    $ac.='<input type="hidden" name="delsel" value="'.$row['id'].'" />';
    $ac.='</form>';
    $ac.='<form action="sel_useedit.php" id="ED_'.$row['id'].'" method="post" >';
    $ac.='<input type="hidden" name="editsel" value="'.$row['invno'].'" />';
    $ac.='</form>';    
    }
    $subdata[]=$ac;
    $data[]=$subdata;
    $ac='';
}

$json_data=array(
    "draw"              =>  intval($request['draw']),
    "recordsTotal"      =>  intval($totalData),
    "recordsFiltered"   =>  intval($totalFilter),
    "data"              =>  $data
);

echo json_encode($json_data);

?>
