<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
$body='';
$foot='';
$buton='';
$s=0;
$totqty=0;
function check_purinvdel($invno,$pid,$pqty){
    $flage=0;
    global $con;
    
    
    
    $sql="SELECT avqty FROM tbl_brstock WHERE pid=$pid OR unqid='$invno'";
    $que=mysqli_query($con,$sql)or die(mysqli_error($con));  
    $rowd=mysqli_fetch_array($que);
    
    $avqty = $rowd['avqty'];

    if($avqty < $pqty) {
        $flage=1;
    }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;    
    }       
}

if(isset($_SESSION['axes_edpurchase'])){
if(is_array($_SESSION['axes_edpurchase'])){
$max=count($_SESSION['axes_edpurchase']);
for($i=($max-1);$i>=0;$i=$i-1){
$pid=$_SESSION['axes_edpurchase'][$i]['pid'];   
$name=$_SESSION['axes_edpurchase'][$i]['name'];
$qty=$_SESSION['axes_edpurchase'][$i]['qty'];
$cost=$_SESSION['axes_edpurchase'][$i]['cost'];
$subtot=$_SESSION['axes_edpurchase'][$i]['subtot'];
$price=$_SESSION['axes_edpurchase'][$i]['price'];
$idisp=$_SESSION['axes_edpurchase'][$i]['disp'];
$idisf=$_SESSION['axes_edpurchase'][$i]['disf'];
$disamo=$_SESSION['axes_edpurchase'][$i]['disamo'];
$sdisp=$_SESSION['axes_edpurchase'][$i]['sdisp'];
$sdisf=$_SESSION['axes_edpurchase'][$i]['sdisf'];
$wday=$_SESSION['axes_edpurchase'][$i]['wday'];
$check=$_SESSION['axes_edpurchase'][$i]['check'];
$col=$_SESSION['axes_edpurchase'][$i]['col'];
$siz=$_SESSION['axes_edpurchase'][$i]['siz'];
$invno=$_SESSION['axes_edpurchase'][$i]['unqid'];
if($col==0){$col='';}
if($siz==0){$siz='';}    
if($col=='' && $siz==''){
$name=$name;    
}elseif($col!='' && $siz==''){
$name= $name.' '.get_fild_data('tbl_color',$col,'name');    
}elseif($col=='' && $siz!=''){
$name= $name.' '.get_fild_data('tbl_size',$siz,'sval');    
}elseif($col!='' && $siz!=''){    
$name= $name.' '.get_fild_data('tbl_color',$col,'name').' '.get_fild_data('tbl_size',$siz,'sval');    
}    
$s+=1;
$totqty+=$qty;    
$body.='<tr>';
$body.='<td class="text-center" width="30px">'.$s.'</td>';
$body.='<td data-toggle="collapse" data-target="#pitem'.$i.'" class="accordion-toggle" style="cursor: pointer;" width="190px">'.$name.'</td>';
if(!check_purinvdel($invno,$pid,$qty)){
$body.='<td width="60px"><input type="text" min="1" onkeypress="return isNumberKey(event)" class="form-control quantity" id="qty_'.$i.'" value="'.$qty.'"  size="2" style="height: 24px;" /></td>';
}else{
    $body.='<td width="60px"><input type="text" min="1" onkeypress="return isNumberKey(event)" class="form-control quantity" id="qty_'.$i.'" value="'.$qty.'"  size="2" style="height: 24px;" readonly/></td>';
}
$body.='<td width="60px"><input type="text" min="1" onkeypress="return isNumberKey(event)" class="form-control cost" id="cost_'.$i.'" value="'.$cost.'"  size="2" style="height: 24px;"/></td>';

if($check==1){    
$body.='<td class="text-center" width="35px"><input type="checkbox" class="bscan" id="bscan_'.$i.'" value="'.$pid.'" name="radio-group" checked /></td>';
}else{
$body.='<td class="text-center" width="35px"><input type="checkbox" class="bscan" id="bscan_'.$i.'" value="'.$pid.'" name="radio-group" /></td>';    
}

$body.='<td width="65px" id="stotal_'.$i.'" class="text-right">'.getfloatval($subtot).'</td>';
$body.='<td width="60px"><input type="text" min="1" onkeypress="return isNumberKey(event)" class="form-control price" id="price_'.$i.'" value="'.$price.'"  size="2" style="height: 24px;"/></td>';
if(!check_purinvdel($invno,$pid,$qty)){
$body.='<td class="text-center" width="25px"><a id="'.$i.'" class="remove"><span style="cursor: pointer;" class="fa fa-times"></span></a></td>';  
}else{
$body.='<td class="text-center" width="25px"></td>';     
}  
$body.='</tr>';
    
$body.='<tr>';
$body.='<td colspan="8" class="hiddenRow"><div class="accordian-body collapse" id="pitem'.$i.'">';
$body.='<table class="table table-bordered table-striped" style="margin-bottom: 0;">';
$body.='<thead>';
$body.='<tr>';
$body.='<th colspan="3" class="text-center">Purchase Discount</th>';
$body.='<th rowspan="2" class="text-center">Warranty Days</th>';    
$body.='<th colspan="2" class="text-center">Sales Discount</th>';    
$body.='</tr>';
$body.='<tr>';
$body.='<th class="text-center">Percent(%)</th>';
$body.='<th class="text-center">Fixed</th>';
$body.='<th class="text-center">Total</th>';
$body.='<th class="text-center">Percent(%)</th>';
$body.='<th class="text-center">Fixed</th>';    
$body.='</tr>';
$body.='</thead>';
$body.='<tbody>';
$body.='<tr>';
$body.='<td><input type="text" min="0" onkeypress="return isNumberKey(event)" class="form-control disp" id="disp_'.$i.'" value="'.$idisp.'"  size="2" style="height: 24px;"/></td>';
$body.='<td><input type="text" min="0" onkeypress="return isNumberKey(event)" class="form-control disf" id="disf_'.$i.'" value="'.$idisf.'"  size="2" style="height: 24px;"/></td>';
$body.='<td id="disamo_'.$i.'" class="text-right">'.$disamo.'</td>';
$body.='<td><input type="text" min="1" onkeypress="return isNumberKey(event)" class="form-control wdays" id="wdays_'.$i.'" value="'.$wday.'"  size="2" style="height: 24px;"/></td>';    
$body.='<td><input type="text" min="0" onkeypress="return isNumberKey(event)" class="form-control sdisp" id="sdisp_'.$i.'" value="'.$sdisp.'"  size="2" style="height: 24px;"/></td>';
$body.='<td><input type="text" min="0" onkeypress="return isNumberKey(event)" class="form-control sdisf" id="sdisf_'.$i.'" value="'.$sdisf.'"  size="2" style="height: 24px;"/></td>';    
$body.='</tr>';    
$body.='</tbody></table></div></td></tr>';    
}
    
if($max>0){
if (!isset($_SESSION['axes_edpurde'])) {
$_SESSION['axes_edpurde'] = array();
$_SESSION['axes_edpurde'][0]['disp']=0;
$_SESSION['axes_edpurde'][0]['disamo']=0;    
$_SESSION['axes_edpurde'][0]['vatp']=0;
$_SESSION['axes_edpurde'][0]['vatamo']=0;
$_SESSION['axes_edpurde'][0]['taxp']=0;
$_SESSION['axes_edpurde'][0]['taxamo']=0;
$_SESSION['axes_edpurde'][0]['freight']=0;
$_SESSION['axes_edpurde'][0]['spmony']=0;
$_SESSION['axes_edpurde'][0]['name']='Others';
$_SESSION['axes_edpurde'][0]['others']=0;
$_SESSION['axes_edpurde'][0]['less']=0;
$_SESSION['axes_edpurde'][0]['gtotal']=$subtot;    
}
}else{
if(isset($_SESSION['axes_edpurde'])){
unset($_SESSION['axes_edpurde']);    
}    
}
    
if(isset($_SESSION['axes_edpurde'])){
if(is_array($_SESSION['axes_edpurde'])){    
$disp=$_SESSION['axes_edpurde'][0]['disp'];
$disamoe=$_SESSION['axes_edpurde'][0]['disamo'];
$vatp=$_SESSION['axes_edpurde'][0]['vatp'];
$vatamo=$_SESSION['axes_edpurde'][0]['vatamo'];
$taxp=$_SESSION['axes_edpurde'][0]['taxp'];
$taxamo=$_SESSION['axes_edpurde'][0]['taxamo'];
$freight=$_SESSION['axes_edpurde'][0]['freight'];
$spmoney=$_SESSION['axes_edpurde'][0]['spmony'];	
$otname=$_SESSION['axes_edpurde'][0]['name'];
$others=$_SESSION['axes_edpurde'][0]['others'];
$less=$_SESSION['axes_edpurde'][0]['less'];
$gtotal=$_SESSION['axes_edpurde'][0]['gtotal'];    
}else{
$disp=0;
$disamoe=0;
$vatp=0;
$vatamo=0;
$taxp=0;
$taxamo=0;
$freight=0;
$spmoney=0;
$otname='';
$others=0;	
$less=0;
$gtotal=0;    
}
}else{
$disp=0;
$disamoe=0;
$vatp=0;
$vatamo=0;
$taxp=0;
$taxamo=0;
$freight=0;
$spmoney=0;
$otname=0;
$others=0;	
$less=0;
$gtotal=0;    
}

$foot.='<tr>';
$foot.='<td width="30px"></td>';
$foot.='<td width="190px"></td>';
$foot.='<td width="60px"></td>';
$foot.='<td width="60px"></td>';
$foot.='<td width="35px"></td>';
$foot.='<td width="65px"></td>';
$foot.='<td width="60px"></td>';
$foot.='<td width="25px"></td>';
$foot.='</tr>';

$foot.='<tr>';
$foot.='<td colspan="2" class="text-center" width="220px">-Total-</td>';
$foot.='<td width="60px">'.$totqty.'</td>';
$foot.='<td colspan="2" width="95px"></td>';
$foot.='<td width="65px" class="text-right">'.getfloatval(get_edpurchase_total()).'</td>';
$foot.='<td colspan="2" width="85px"></td>';
$foot.='</tr>';	
if(get_edpurdiscount_total()>0){
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">Discount on Item:</td>';
$foot.='<td align="center" colspan="2"></td>';    
$foot.='<td id="disitem" class="text-right">'.getfloatval(get_edpurdiscount_total()).'</td>';
$foot.='<td colspan="2"></td>';    
$foot.='</tr>';	
}
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">Discount (%)</td>';
$foot.='<td colspan="2"><input type="text" maxlength="5" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="discount" value="'.getfloatval($disp).'"  size="2" style="height: 24px;"/></td>';    
$foot.='<td id="disitems" class="text-right">'.getfloatval(get_edpurdiscount_total($disp)-get_edpurdiscount_total()).'</td>';
$foot.='<td colspan="2" width="85px"></td>';
$foot.='</tr>';	
if(get_edpurdiscount_total()>0){
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">Total Discount (%)</td>';
$foot.='<td align="center" colspan="2"></td>';    
$foot.='<td id="totdisamo" class="text-right">'.getfloatval(get_edpurdiscount_total($disp)).'</td>';
$foot.='<td colspan="2"></td>';     
$foot.='</tr>';	
}
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">VAT (%)</td>';
$foot.='<td colspan="2"><input type="text" maxlength="5" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="vatp" value="'.getfloatval($vatp).'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td id="vatamo" class="text-right">'.getfloatval($vatamo).'</td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">TAX (%)</td>';
$foot.='<td colspan="2"><input type="text" maxlength="5" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="taxp" value="'.getfloatval($taxp).'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td id="taxamo" class="text-right">'.getfloatval($taxamo).'</td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';    
$foot.='<tr>';   
$foot.='<td colspan="3" align="right"><div style="display: inline;"><i class="fa fa-angle-right cart-icon" id="icon"></i></div><span id="otdname">'.$otname.':</span></td>';
$foot.='<td colspan="2"><input type="text" maxlength="7" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="others" value="'.getfloatval($others).'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td id="othersamo" class="text-right">'.getfloatval($others).'</td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';     
$foot.='<tr class="dshow" style="display: none;">';   
$foot.='<td colspan="3" align="right">Others Name:</td>';
$foot.='<td colspan="4"><input type="text" maxlength="20" min="0" class="form-control" id="otname" value="'.$otname.'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">CNF:</td>';
$foot.='<td colspan="2"><input type="text" maxlength="6" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="speed" value="'.getfloatval($spmoney).'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td id="spmoney" class="text-right">'.getfloatval($spmoney).'</td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">Freight:</td>';
$foot.='<td colspan="2"><input type="text" maxlength="6" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="freight" value="'.getfloatval($freight).'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td id="freightd" class="text-right">'.getfloatval($freight).'</td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';
$foot.='<tr>';   
$foot.='<td colspan="3" align="right">Fractional Discount:</td>';
$foot.='<td colspan="2"><input type="text" maxlength="6" min="0" onkeypress="return isNumberKey(event)" class="form-control" id="less" value="'.getfloatval($less).'"  size="2" style="height: 24px;"/></td>'; 
$foot.='<td id="lessd" class="text-right">'.getfloatval($less).'</td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';
$foot.='<tr>';   
$foot.='<td colspan="3" align="right"><strong>Grand Total:</strong></td>';
$foot.='<td colspan="2"></td>'; 
$foot.='<td id="grtotal" class="text-right"><strong>'.getfloatval($gtotal).'</strong></td>';
$foot.='<td  colspan="2"></td>';    
$foot.='</tr>';    
}else{
$body.='<tr>';
$body.='<td colspan="8" class="text-center">There are no Purchase Item!</td>';
$body.='</tr>'; 
} 
}else{
$body.='<tr>';
$body.='<td colspan="8" class="text-center">There are no Purchase Item!</td>';
$body.='</tr>';
}
$body.="<script>";
$body.="$('.accordian-body').on('show.bs.collapse', function () {";
$body.="$(this).closest('table')";
$body.=".find('.collapse.in')";
$body.=".not(this)";
$body.=".collapse('toggle')";
$body.="})";
$body.="</script>";

if(isset($_SESSION['axes_edpurchase'])){
$buton.='<div class="col-md-6">';
$buton.='<input type="button" id="emptycart" class="btn btn-flat bg-red btn-sm" value="Empty"/>';
$buton.='&nbsp;<input type="button" id="canceledit" class="btn btn-flat bg-red btn-sm" value="Cancel"/></div>';
$buton.='<div class="col-md-6 text-right">';   
$buton.='<input type="button" id="save_purchase" class="btn btn-flat bg-purple btn-sm" value="Checkout"/>'; 
$buton.='</div>';
}

if(isset($_POST['body'])){
echo $body;
}elseif(isset($_POST['foot'])){
echo $foot;
}elseif(isset($_POST['buton'])){
echo $buton;    
}
exit;     