<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/carrierfunctions.php');
$body='';
$foot='';
$buton='';
$s=0;
$totqty=0;

if(isset($_SESSION['axes_cardeli'])){
if(is_array($_SESSION['axes_cardeli'])){
$max=count($_SESSION['axes_cardeli']);
for($i=($max-1);$i>=0;$i=$i-1){
$unqid=$_SESSION['axes_cardeli'][$i]['unqid'];
$pid=$_SESSION['axes_cardeli'][$i]['pid'];    
$code=$_SESSION['axes_cardeli'][$i]['code'];
$name=$_SESSION['axes_cardeli'][$i]['name'];
$unit=$_SESSION['axes_cardeli'][$i]['unid'];
$col=$_SESSION['axes_cardeli'][$i]['col'];
if($col==0){$col='';}    
$siz=$_SESSION['axes_cardeli'][$i]['siz'];
if($siz==0){$siz='';}    
$imei=$_SESSION['axes_cardeli'][$i]['imei'];
$check=$_SESSION['axes_cardeli'][$i]['check'];    
$cost=$_SESSION['axes_cardeli'][$i]['cost']; 
$qty=$_SESSION['axes_cardeli'][$i]['qty'];
$price=$_SESSION['axes_cardeli'][$i]['price'];
$disp=$_SESSION['axes_cardeli'][$i]['disp'];
$disf=$_SESSION['axes_cardeli'][$i]['disf'];    
$disamo=$_SESSION['axes_cardeli'][$i]['disamo'];    
$subtot=$_SESSION['axes_cardeli'][$i]['subtot'];

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="#sitem'.$i.'" class="accordion-toggle" style="cursor: pointer;" width="214px">'.$name.'</td>';
$body.='<td width="72px"><input type="text" min="1" onkeypress="return isNumberKey(event)" class="form-control quantity" id="qty_'.$i.'" value="'.$qty.'"  size="2" style="height: 24px;"/></td>';
$body.='<td width="72px"><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==1){    
$body.='<td class="text-center" width="35px"><input type="checkbox" class="bscan" id="bscan_'.$i.'" value="'.$unqid.'" name="radio-group" checked /></td>';
}else{
$body.='<td class="text-center" width="35px"><input type="checkbox" class="bscan" id="bscan_'.$i.'" value="'.$unqid.'" name="radio-group" /></td>';    
}
$body.='<td width="77px" id="stotal_'.$i.'" class="text-right">'.getfloatval($subtot).'</td>';
$body.='<td class="text-center" width="25px"><a id="'.$i.'" class="remove"><span style="cursor: pointer;" class="fa fa-times"></span></a></td>';    
$body.='</tr>';  
}    

if($max>0){
if (!isset($_SESSION['axes_cardelide'])) {
$_SESSION['axes_cardelide'] = array();
$_SESSION['axes_cardelide'][0]['disp']=0;
$_SESSION['axes_cardelide'][0]['disamo']=0;    
$_SESSION['axes_cardelide'][0]['gtotal']=$subtot;    
}
}else{
if(isset($_SESSION['axes_cardelide'])){
unset($_SESSION['axes_cardelide']);    
}    
}
if(isset($_SESSION['axes_cardelide'])){
if(is_array($_SESSION['axes_cardelide'])){    
$disp=$_SESSION['axes_cardelide'][0]['disp'];
$gtotal=$_SESSION['axes_cardelide'][0]['gtotal'];    
}else{
$disp=0;
$gtotal=0;    
}
}else{
$disp=0;
$gtotal=0;    
}    

$foot.='<tr>';
$foot.='<td width="30px"></td>';
$foot.='<td width="214px"></td>';
$foot.='<td width="72px"></td>';
$foot.='<td width="72px"></td>';
$foot.='<td width="35px"></td>';
$foot.='<td width="77px"></td>';
$foot.='<td width="25px"></td>';
$foot.='</tr>';    
$foot.='<tr>';
$foot.='<td colspan="2" class="text-center" width="244px"><strong>-Total-</strong></td>';
$foot.='<td width="72px"><strong>'.$totqty.'</strong></td>';
$foot.='<td colspan="2" width="107px"></td>';
$foot.='<td width="77px" class="text-right"><strong>'.getfloatval(get_cardeli_total()).'</strong></td>';
$foot.='<td></td>';
$foot.='</tr>';	


$foot.='<tr>';   
$foot.='<td colspan="3" align="right"><strong>Discount (%)</strong></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"><strong>'.getfloatval(get_cardelidiscount_total($disp)-get_cardelidiscount_total()).'</strong></td>';
$foot.='<td></td>';
$foot.='</tr>';	
if(get_cardelidiscount_total()>0){
$foot.='<tr>';   
$foot.='<td colspan="3" align="right"><strong>Total Discount (%)</strong></td>';
$foot.='<td align="center" colspan="2"></td>';    
$foot.='<td id="totdisamo" class="text-right"><strong>'.getfloatval(get_cardelidiscount_total($disp)).'</strong></td>';
$foot.='<td></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></td>';    
$foot.='</tr>';    
}else{
$body.='<tr>';
$body.='<td colspan="7" class="text-center">There are no Delivery Item!</td>';
$body.='</tr>'; 
} 
}else{
$body.='<tr>';
$body.='<td colspan="7" class="text-center">There are no Delivery 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_cardeli'])){
$buton.='<div class="col-md-6">';
$buton.='<input type="button" id="emptycart" class="btn btn-flat bg-red btn-sm" value="Empty"/></div>';
$buton.='<div class="col-md-6 text-right">';   
$buton.='<input type="button" id="save_sales" 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; 