<?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_purinvdel($invno){
$flage=0;
global $con;

$sql="SELECT * FROM tbl_traproduct WHERE invno='$invno'";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$unqid=$row['unqid'];
$sql="SELECT * FROM tbl_traproduct WHERE mods IN ('SE','TR','WR') AND unqid='$unqid'";
$que=mysqli_query($con,$sql)or die(mysqli_error($con));    
if($que->num_rows > 0) {
$flage=1;
break;    
}else{
$flage=0;    
}    
}    

if($flage>0){
return $flage;
exit;    
}    
    
$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_preturn WHERE 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;    
}       
}


$request=$_REQUEST;
if($uty=='1'){
$sql ="SELECT * FROM tbl_purchase";    
}else{
$sql ="SELECT * FROM tbl_purchase WHERE brid='$brid'";    
}
$query=mysqli_query($con,$sql);

$totalData=mysqli_num_rows($query);

$totalFilter=$totalData;

//Search
$table='tbl_purchase';
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.')';    
} 
$query=mysqli_query($con,$tsql);
$totalData=mysqli_num_rows($query);

//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']."  ";
}
$query=mysqli_query($con,$sql_search);

$data=array();
$s=0;
while($row=mysqli_fetch_array($query)){
    $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');    
    }    
    }
    if($row['type']=='SU'){
   if($row['supid']!=0){ $subdata[]=show_addres(get_fild_data('tbl_supplier',$row['supid'],'name'),get_fild_data('tbl_supplier',$row['supid'],'cnumber'),get_fild_data('tbl_supplier',$row['supid'],'cemail'),'',get_fild_data('tbl_supplier',$row['supid'],'code'),$row['type'].'_'.$row['supid']);
   }else{
  $subdata[]=show_addres('Local Purchase','','','','ABA/SU/000',$row['type'].'_0');    
   }
    }else{
 $subdata[]=show_addres(get_fild_data('tbl_customer',$row['supid'],'name'),get_fild_data('tbl_customer',$row['supid'],'cnumber'),get_fild_data('tbl_customer',$row['supid'],'cemail'),'',get_fild_data('tbl_customer',$row['supid'],'code'),$row['type'].'_'.$row['supid']);     
    }
    $subdata[]=$row['invno'];
    $subdata[]=numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol'));
    $subdata[]=get_fild_data('tbl_project',$row['prjno'],'prjid');
    // $subdata[]=$row['note'];
    $ac.='<a class="btn btn-flat bg-purple details-invoice" style="margin: 3px;" href="#" id="inv_'.$row['id'].'_'.$row['supid'].'"><i class="fa fa-eye cat-child"></i></a>';
    if($row['total']-$row['rawcash']){
     $ac.='<a class="btn btn-flat bg-purple details-due" style="margin: 3px;" href="#" id="inv_'.$row['id'].'"><i class="fa fa-money"></i></a>';
    }
   
    if(!check_purinvdel($invno)){   
    if(substr($row['refinv'],0,3)=='POR'){
    }else{    
    $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_purinvdel($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>';    
    }
    if(!check_purinvdel($invno)){
    $ac.='<form action="pur_pinvedit.php" id="ED_'.$row['id'].'" method="post" >';
    $ac.='<input type="hidden" name="pured" value="'.$row['invno'].'" />';
    $ac.='</form>';        
    }
    $ac.='<form action="pur_preturncteate.php" id="RE_'.$row['id'].'" method="post" >';
    $ac.='<input type="hidden" name="puret" value="'.$row['id'].'" />';
    $ac.='</form>';
    $ac.='<form action="pur_pinvlist.php" id="DL_'.$row['id'].'" method="post" >';
    $ac.='<input type="hidden" name="delpur" value="'.$row['id'].'" />';
    $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);

?>
