<?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)){
    $pid=$row['pid'];
    $unqid=$row['unqid'];
    
    
    $pqty=$row['p_in'];
    
    $sql="SELECT avqty FROM tbl_brstock WHERE pid=$pid OR unqid='$unqid'";
    $que=mysqli_query($con,$sql)or die(mysqli_error($con));  
    $rowd=mysqli_fetch_array($que);
    
    $avqty = $rowd['avqty'];

    if($avqty < $pqty) {
        $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'){
 $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(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[]=$row['note'].'<br><strong>Created By: </strong>'.get_fild_data('tbl_user',$row['uid'],'name').' '.date("d M Y H:i:s", strtotime($row['date']));
    if(get_pagesecurity('48','prints','P')){
       $ac.='<a class="btn btn-flat bg-purple details-invoice" style="margin: 3px;" data-toggle="tooltip" title="View" href="#" id="inv_'.$row['id'].'_'.$row['supid'].'"><i class="fa fa-eye cat-child"></i></a>';
    }
    // if(!check_purinvdel($invno)){   
    // if(substr($row['refinv'],0,3)=='POR'){
    // }else{    
        if(get_pagesecurity('48','edits','P')){
            $ac.='<a class="btn btn-flat bg-purple" style="margin: 3px;" data-toggle="tooltip" title="Edit" 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="#" data-toggle="tooltip" title="Return" onclick="take_action(\'RE_'.$row['id'].'\')"><i class="fa fa-reply"></i></a>';
    if(!check_purinvdel($invno)){
        if(get_pagesecurity('48','deletes','P')){
          $ac.='<a class="btn btn-flat bg-purple" style="margin: 3px;" href="#" data-toggle="tooltip" title="Delete" 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);

?>
