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

if(isset($_SESSION['axes_carorder'])){
if(is_array($_SESSION['axes_carorder'])){
$max=count($_SESSION['axes_carorder']);
for($i=($max-1);$i>=0;$i=$i-1){
$pid=$_SESSION['axes_carorder'][$i]['pid'];    
$name=$_SESSION['axes_carorder'][$i]['name'];
$qty=$_SESSION['axes_carorder'][$i]['qty'];
$cost=$_SESSION['axes_carorder'][$i]['cost'];
$subtot=$_SESSION['axes_carorder'][$i]['subtot'];
$price=$_SESSION['axes_carorder'][$i]['price'];
$idisp=$_SESSION['axes_carorder'][$i]['disp'];
$idisf=$_SESSION['axes_carorder'][$i]['disf'];
$disamo=$_SESSION['axes_carorder'][$i]['disamo'];
$sdisp=$_SESSION['axes_carorder'][$i]['sdisp'];
$sdisf=$_SESSION['axes_carorder'][$i]['sdisf'];
$wday=$_SESSION['axes_carorder'][$i]['wday'];
$eqty=$_SESSION['axes_carorder'][$i]['eqty'];
$col=$_SESSION['axes_carorder'][$i]['col'];
$siz=$_SESSION['axes_carorder'][$i]['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;
$toteqty+=$eqty;    
$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="225px">'.$name.'</td>';  
$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>';
$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>';

$body.='<td width="65px" 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>';
    
$body.='<tr>';
$body.='<td colspan="6" 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">Order Discount</th>';
$body.='<th rowspan="2" class="text-center">Warranty Days</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.='</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.='</tr>';    
$body.='</tbody></table></div></td></tr>';    
}
if($max>0){
if (!isset($_SESSION['axes_carorderde'])) {
$_SESSION['axes_carorderde'] = array();
$_SESSION['axes_carorderde'][0]['disp']=0;
$_SESSION['axes_carorderde'][0]['disamo']=0;    
$_SESSION['axes_carorderde'][0]['gtotal']=$subtot;    
}
}else{
if(isset($_SESSION['axes_carorderde'])){
unset($_SESSION['axes_carorderde']);    
}    
}

if(isset($_SESSION['axes_carorderde'])){
if(is_array($_SESSION['axes_carorderde'])){    
$disp=$_SESSION['axes_carorderde'][0]['disp'];
$disamoe=$_SESSION['axes_carorderde'][0]['disamo'];
$gtotal=$_SESSION['axes_carorderde'][0]['gtotal'];    
}else{
$disp=0;
$disamoe=0;
$gtotal=0;    
}
}else{
$disp=0;
$disamoe=0;
$gtotal=0;    
}

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

$foot.='<tr>';
$foot.='<td colspan="2" class="text-center" width="255px">-Total-</td>';
$foot.='<td width="60px">'.$totqty.'</td>';
$foot.='<td width="60px"></td>';
$foot.='<td width="65px" class="text-right">'.getfloatval(get_porder_total()).'</td>';
$foot.='<td width="25px"></td>';
$foot.='</tr>';	
if(get_orddiscount_total()>0){
$foot.='<tr>';   
$foot.='<td colspan="2" align="right">Discount on Item:</td>';
$foot.='<td align="center" colspan="2"></td>';    
$foot.='<td id="disitem" class="text-right">'.getfloatval(get_orddiscount_total()).'</td>';
$foot.='<td></td>';    
$foot.='</tr>';	
}
$foot.='<tr>';   
$foot.='<td colspan="2" 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_orddiscount_total($disp)-get_orddiscount_total()).'</td>';
$foot.='<td width="25px"></td>';
$foot.='</tr>';	
if(get_orddiscount_total()>0){
$foot.='<tr>';   
$foot.='<td colspan="2" align="right">Total Discount (%)</td>';
$foot.='<td align="center" colspan="2"></td>';    
$foot.='<td id="totdisamo" class="text-right">'.getfloatval(get_orddiscount_total($disp)).'</td>';
$foot.='<td></td>';     
$foot.='</tr>';	
}
$foot.='<tr>';   
$foot.='<td colspan="2" 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>';
if(get_fild_data('tbl_setting','1','sval')==0){
$body.='<td colspan="6" class="text-center">There are no Purchase Order Item!</td>';    
}else{
$body.='<td colspan="6" class="text-center">ক্রয় আদেশ সম্পর্কিত তথ্য পাওয়া যায়নি!</td>';    
}    
$body.='</tr>'; 
} 
}else{
$body.='<tr>';
if(get_fild_data('tbl_setting','1','sval')==0){
$body.='<td colspan="6" class="text-center">There are no Purchase Order Item!</td>';    
}else{
$body.='<td colspan="6" class="text-center">ক্রয় আদেশ সম্পর্কিত তথ্য পাওয়া যায়নি!</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_carorder'])){
$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_purorder" 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;