<?php
// IC= item count PC=price count SP=sale price	
function check_price($key){
    $res=0;
    $max=0;    
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    $price=floatval($_SESSION['axes_cardeli'][$i]['price']);
    if($price<=0){
    $res+=1;    
    }    
    }    
    }else{
    $max=0;    
    $res=0;    
    }    
    if($key=='I'){
    return $max;    
    }elseif($key=='P'){
    return $res;    
    }    
    }
    
    function check_serial($key){
        $res=0;
        $max=0;    
        if(isset($_SESSION['axes_cardelise'])){
        $max=count($_SESSION['axes_cardelise']);
        for($i=0;$i<$max;$i++){
        $serial=$_SESSION['axes_cardelise'][$i]['imei'];
        if(strlen($serial)<2 || $serial==''){
        $res+=1;    
        }    
        }    
        }else{
        $max=0;    
        $res=0;    
        }    
        if($key=='I'){
        return $max;    
        }elseif($key=='S'){
        return $res;    
        }    
        }


    function remove_selimei($pid){
        $pid=intval($pid);
        if(isset($_SESSION['axes_cardeli'])){
        $max=count($_SESSION['axes_cardeli']);
        for($i=0;$i<$max;$i++){
        if($pid==$i){
        unset($_SESSION['axes_cardeli'][$i]);
        break;
        }
        }
        $_SESSION['axes_cardeli']=array_values($_SESSION['axes_cardeli']);
        }
        }
  
        

function get_salesinfo($unqid,$key){
    global $con;
    $sql="SELECT ".$key." AS rdata FROM tbl_cartraproduct WHERE unqid='$unqid' AND mods='CR' LIMIT 1";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));    
    $row=mysqli_fetch_array($result);
    return $row['rdata'];    
    }
            
function get_pucartitemid($pid){
    $id='';    
    if(isset($_SESSION['axes_carorder'])){
    $max=count($_SESSION['axes_carorder']);
    for($i=0;$i<$max;$i++){
    $epid=$_SESSION['axes_carorder'][$i]['pid'];    
    if($pid==$epid){
    $id=$i;    
    break;    
    }    
    }
    }else{
    $id='';   
    }
    return $id;     
    }

/*--------------------------------------------------------------*/
/* Function for Carrier Order
/*--------------------------------------------------------------*/
function get_ordeqty($pid){
global $con;
$sql="SELECT COALESCE ((SELECT (avqty) FROM tbl_comstock WHERE pid='$pid'),0) AS eqty";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['eqty'];    
}

function addpurorder($pid,$qty,$col='',$siz=''){
if($qty<=0) return;

if ( !isset($_SESSION['axes_carorder']) ) {
$_SESSION['axes_carorder'] = array();
}

if(is_array($_SESSION['axes_carorder'])){
if(purord_exists($pid,$col,$siz)) return;
$max=count($_SESSION['axes_carorder']);
$_SESSION['axes_carorder'][$max]['pid']=$pid;
$_SESSION['axes_carorder'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_carorder'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_carorder'][$max]['unid']=get_fild_data('tbl_item',$pid,'unid');
$_SESSION['axes_carorder'][$max]['col']=$col;
$_SESSION['axes_carorder'][$max]['siz']=$siz;    
$_SESSION['axes_carorder'][$max]['imei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_carorder'][$max]['eqty']=get_ordeqty($pid);    
$_SESSION['axes_carorder'][$max]['cost']=get_proval($pid,'C'); 
$_SESSION['axes_carorder'][$max]['qty']=$qty;
$_SESSION['axes_carorder'][$max]['check']=0;  
$_SESSION['axes_carorder'][$max]['price']=0;
$_SESSION['axes_carorder'][$max]['disp']=0;
$_SESSION['axes_carorder'][$max]['disf']=0;
$_SESSION['axes_carorder'][$max]['disamo']=0;
$_SESSION['axes_carorder'][$max]['sdisp']=0;
$_SESSION['axes_carorder'][$max]['sdisf']=0;
$_SESSION['axes_carorder'][$max]['wday']=0;    
$_SESSION['axes_carorder'][$max]['subtot']=(get_proval($pid,'C')*$qty);    
}else{
$_SESSION['axes_carorder']=array();    
$_SESSION['axes_carorder'][0]['pid']=$pid;
$_SESSION['axes_carorder'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_carorder'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_carorder'][0]['unid']=get_fild_data('tbl_item',$pid,'unid');
$_SESSION['axes_carorder'][0]['col']=$col;
$_SESSION['axes_carorder'][0]['siz']=$siz;    
$_SESSION['axes_carorder'][0]['imei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_carorder'][0]['eqty']=get_ordeqty($pid);    
$_SESSION['axes_carorder'][0]['cost']=get_proval($pid,'C'); 
$_SESSION['axes_carorder'][0]['qty']=$qty;
$_SESSION['axes_carorder'][0]['price']=0;
$_SESSION['axes_carorder'][0]['check']=0;  
$_SESSION['axes_carorder'][0]['disp']=0;
$_SESSION['axes_carorder'][0]['disf']=0;
$_SESSION['axes_carorder'][0]['disamo']=0;
$_SESSION['axes_carorder'][0]['sdisp']=0;
$_SESSION['axes_carorder'][0]['sdisf']=0;
$_SESSION['axes_carorder'][0]['wday']=0;    
$_SESSION['axes_carorder'][0]['subtot']=(get_proval($pid,'C')*$qty);    
}
}

function purord_exists($pid,$col='',$siz=''){
$pid=intval($pid);
$max=count($_SESSION['axes_carorder']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($col=='' && $siz==''){
if($pid==$_SESSION['axes_carorder'][$i]['pid'] && $_SESSION['axes_carorder'][$i]['col']=='' && $_SESSION['axes_carorder'][$i]['siz']==''){
$flag=1;
$qy=$_SESSION['axes_carorder'][$i]['qty'];    
$_SESSION['axes_carorder'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_carorder'][$i]['sdisf'];
$cost=$_SESSION['axes_carorder'][$i]['cost'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($cost*($qy+1))-$disamo);     
$_SESSION['axes_carorder'][$i]['disamo']=$disamo;
$_SESSION['axes_carorder'][$i]['subtot']=$subtot;    
break;    
}    
}elseif($col!='' && $siz==''){
if($pid==$_SESSION['axes_carorder'][$i]['pid'] && $col==$_SESSION['axes_carorder'][$i]['col'] && $_SESSION['axes_carorder'][$i]['siz']==''){
$flag=1;
$qy=$_SESSION['axes_carorder'][$i]['qty'];    
$_SESSION['axes_carorder'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_carorder'][$i]['sdisf'];
$cost=$_SESSION['axes_carorder'][$i]['cost'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($cost*($qy+1))-$disamo);    
$_SESSION['axes_carorder'][$i]['disamo']=$disamo;
$_SESSION['axes_carorder'][$i]['subtot']=$subtot;    
break;    
}
}elseif($col=='' && $siz!=''){
if($pid==$_SESSION['axes_carorder'][$i]['pid'] && $_SESSION['axes_carorder'][$i]['col']=='' && $siz==$_SESSION['axes_carorder'][$i]['siz']){
$flag=1;
$qy=$_SESSION['axes_carorder'][$i]['qty'];    
$_SESSION['axes_carorder'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_carorder'][$i]['sdisf'];
$cost=$_SESSION['axes_carorder'][$i]['cost'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($cost*($qy+1))-$disamo);    
$_SESSION['axes_carorder'][$i]['disamo']=$disamo;
$_SESSION['axes_carorder'][$i]['subtot']=$subtot;    
break;    
}    
}    
}   
return $flag;    
}

function get_porder_count($pid){
if(isset($_SESSION['axes_carorder'])){
$max=count($_SESSION['axes_carorder']);
$sum=0;
$qty=0;
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_carorder'][$i]['qty'];
$proid=intval($_SESSION['axes_carorder'][$i]['pid']);
if($pid==$proid){
$sum+=$qty;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}	
}

function get_or_mobile_count(){
    if(isset($_SESSION['axes_carorder'])){
    $max=count($_SESSION['axes_carorder']);
    $sum=0;
    $qty=0;
    for($i=0;$i<$max;$i++){
    $qty=$_SESSION['axes_carorder'][$i]['qty'];
    $reimei=intval($_SESSION['axes_carorder'][$i]['imei']);
    if($reimei>=1){
    $sum+=$qty;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }        
}

function remove_purorder($pid){
$pid=intval($pid);
if(isset($_SESSION['axes_carorder'])){
$max=count($_SESSION['axes_carorder']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_carorder'][$i]);
break;
}
}
$_SESSION['axes_carorder']=array_values($_SESSION['axes_carorder']);
}
}

function get_porder_total(){
if(isset($_SESSION['axes_carorder'])){
$max=count($_SESSION['axes_carorder']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_carorder'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_orddiscount_total($edp=''){
if(isset($_SESSION['axes_carorder'])){
$max=count($_SESSION['axes_carorder']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_carorder'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_porder_total();     
$rdisc=0;    
if($edp>0){
$rdisc=(($bota*$edp)*0.01);    
}
$acdis=($sum+$rdisc);     
if($acdis>0){     
return ($acdis);
}else{
return 0;    
}
}else{
return 0;   
}
}

function add_orserial($pid,$data,$man=''){
		
    if(!isset($_SESSION['axes_orcarse'])) {
    $_SESSION['axes_orcarse'] = array();
    };	
            
    $excount = get_or_mobile_count();		
    if($excount <= 0){unset($_SESSION['axes_orcarse']);}
            
    if(is_array($_SESSION['axes_orcarse'])){
    if($man==''){
    if(imei_or_exists($data)) return;	
    }
        
    $max=count($_SESSION['axes_orcarse']);
    $_SESSION['axes_orcarse'][$max]['pid']=$pid;
    $_SESSION['axes_orcarse'][$max]['cartid']=get_pucartitemid($pid);    
    $_SESSION['axes_orcarse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
    $_SESSION['axes_orcarse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
    $_SESSION['axes_orcarse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
    $_SESSION['axes_orcarse'][$max]['imei']=strtoupper($data);
    }else{
    $_SESSION['axes_orcarse']=array();
    $_SESSION['axes_orcarse'][0]['pid']=$pid;
    $_SESSION['axes_orcarse'][0]['cartid']=get_pucartitemid($pid);    
    $_SESSION['axes_orcarse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
    $_SESSION['axes_orcarse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
    $_SESSION['axes_orcarse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
    $_SESSION['axes_orcarse'][0]['imei']=strtoupper($data);
    }
    }
    
    function imei_or_exists($data){
    $data=strtoupper($data);
    if (isset($_SESSION['axes_orcarse']) ){
    $max=count($_SESSION['axes_orcarse']);
    $flag=0;
    for($i=0;$i<$max;$i++){
    if($data==$_SESSION['axes_orcarse'][$i]['imei']){
    $flag=1;
    break;
    }
    }
    }else{
    $flag=0;	
    }
    return $flag;
    }
    
    function pid_or_exists_wos($pid){
    $spid='';    
    if(isset($_SESSION['axes_orcarse'])){
    $max=count($_SESSION['axes_orcarse']);
    for($i=0;$i<$max;$i++){
    $epid=$_SESSION['axes_orcarse'][$i]['pid'];
    $imei=$_SESSION['axes_orcarse'][$i]['imei'];
    if($pid==$epid && $imei==''){
    $spid=$i+1;
    break;
    }    
    }   
    }
    return $spid; 
    }
        
    function get_or_serial_count($pid){
    if(isset($_SESSION['axes_orcarse'])){
    $max=count($_SESSION['axes_orcarse']);
    $sum=0;
    for($i=0;$i<$max;$i++){
    $proid=intval($_SESSION['axes_orcarse'][$i]['pid']);
    if($pid==$proid){
    $sum+=1;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }			
    }
        
    function remove_or_imei($pid){
    $pid=intval($pid);
    if(isset($_SESSION['axes_orcarse'])){
    $max=count($_SESSION['axes_orcarse']);
    for($i=0;$i<$max;$i++){
    if($pid==$i){
    unset($_SESSION['axes_orcarse'][$i]);
    break;
    }
    }
    $_SESSION['axes_orcarse']=array_values($_SESSION['axes_orcarse']);
    }
    }
    
    function remove_or_puimei($pid){ 
    $max=count($_SESSION['axes_orcarse']);
    for($i=0;$i<$max;$i++){   
    if($pid==$_SESSION['axes_orcarse'][$i]['pid']){   
    unset($_SESSION['axes_orcarse'][$i]);    
    }    
    }
    $_SESSION['axes_orcarse']=array_values($_SESSION['axes_orcarse']);    
    }
    
    function check_or_imei($data){
    $flag=0;
    if(isset($_SESSION['axes_orcarse'])){
    $max=count($_SESSION['axes_orcarse']);
    for($i=0;$i<$max;$i++){
    $imei=$_SESSION['axes_orcarse'][$i]['imei'];
    if($data==$imei){
    $flag=1;
    break;	
    }else{
    $flag=0;	
    }
    }
    }else{
    return $flag;	
    }
    return $flag;
    }
    
    function check_or_eximei($data){
    global $con;
    $flag=0;
    $sql="SELECT COUNT(*) AS exdata FROM (SELECT serial FROM tbl_carserial UNION ALL SELECT rep_serial AS serial FROM tbl_waranty WHERE LENGTH(rep_serial)>1) srl WHERE srl.serial='$data'";    
    //$sql="SELECT COUNT(*) AS exdata FROM tbl_carserial WHERE serial='$data'";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));
    $row=mysqli_fetch_array($result);
    if($row['exdata']>0){
    $flag=1;	
    }else{
    $flag=0;	
    }
    return $flag;	
    }
        
    function check_or_empty(){
    $flag=0;
    if(isset($_SESSION['axes_orcarse'])){
    $max=count($_SESSION['axes_orcarse']);
    for($i=0;$i<$max;$i++){
    $imei=$_SESSION['axes_orcarse'][$i]['imei'];
    if($imei!=''){
    $flag=0;	
    }else{
    $flag=1;	
    }	
    }	
    }else{
    $flag=0;	
    }
    return $flag;
    }
        
    function get_or_imei_count(){
    if(isset($_SESSION['axes_orcarse'])){
    $max=count($_SESSION['axes_orcarse']);
    $sum=0;
    for($i=0;$i<$max;$i++){
    $imei=strtoupper($_SESSION['axes_orcarse'][$i]['imei']);
    if($imei!=''){
    $sum+=1;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }        
    }
    

// IC= item count PC=price count
function getcheck_ordprice($key){
$cpri=0;
$max=0;
if(isset($_SESSION['axes_carorder'])) {
$max=count($_SESSION['axes_carorder']);
for($i=0;$i<$max;$i++){
$cost=$_SESSION['axes_carorder'][$i]['cost'];
if($cost>0){
$cpri+=1;	
}
}
}
if($key=='IC'){
return $max;
}elseif($key=='PC'){
return $cpri;	
}else{
return 0;
}
}

/*--------------------------------------------------------------*/
/* Function for Sales
/*--------------------------------------------------------------*/

function get_carprostinfo($unqid,$key){
    global $con;
    $sql="SELECT ".$key." AS rdata FROM tbl_stock WHERE unqid='$unqid' LIMIT 1";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));    
    $row=mysqli_fetch_array($result);
    return $row['rdata'];    
}

function get_cardeliinfo($unqid,$key){
    global $con;
    $sql="SELECT ".$key." AS rdata FROM tbl_cartraproduct WHERE unqid='$unqid' AND mods='CR' LIMIT 1";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));    
    $row=mysqli_fetch_array($result);
    return $row['rdata'];    
    }
    
    
    
    
    
    function get_carstockinfo($brid,$unqid,$key){
    global $con;
    $sql="SELECT ".$key." AS rdata FROM tbl_carbrstock WHERE brid='$brid' AND unqid='$unqid' LIMIT 1";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));    
    $row=mysqli_fetch_array($result);
    return $row['rdata'];    
    }
    
    
function check_validqty($brid){
    $flag=0;
    if(isset($_SESSION['axes_cardeli'])){    
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    $unqid=$_SESSION['axes_cardeli'][$i]['unqid'];    
    $eqty=(get_carstockinfo($brid,$unqid,'avqty'));
    $qty=$_SESSION['axes_cardeli'][$i]['qty'];
    if($qty>$eqty){
    $flag=1;
    break;    
    }    
    }    
    }else{
    $flag=0;    
    }
    return $flag;    
    }    

    function addtocardeli($brid,$unqid,$qty,$check='M'){
    if($qty<=0) return;
    
    if ( !isset($_SESSION['axes_cardeli']) ) {
    $_SESSION['axes_cardeli'] = array();
    }
    
    if(get_carprostinfo($unqid,'disf')>0){
    $disamo=getfloatval((get_carprostinfo($unqid,'disf')*$qty));    
    }else{
    if(get_carprostinfo($unqid,'disp')>0){
    $disamo=getfloatval(((get_carprostinfo($unqid,'disp')/100)*get_carprostinfo($unqid,'price'))*$qty);    
    }else{
    $disamo=0;    
    }    
    }    
        
    if(is_array($_SESSION['axes_cardeli'])){
    if(carpro_exists($brid,$unqid,$check)) return;
    $max=count($_SESSION['axes_cardeli']);
    $_SESSION['axes_cardeli'][$max]['unqid']=$unqid;
    $_SESSION['axes_cardeli'][$max]['pid']=get_cardeliinfo($unqid,'pid');    
    $_SESSION['axes_cardeli'][$max]['code']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'code');
    $_SESSION['axes_cardeli'][$max]['name']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'name');
    $_SESSION['axes_cardeli'][$max]['unid']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'unid');
    $_SESSION['axes_cardeli'][$max]['col']=get_cardeliinfo($unqid,'colid');
    $_SESSION['axes_cardeli'][$max]['siz']=get_cardeliinfo($unqid,'sizid');    
    $_SESSION['axes_cardeli'][$max]['imei']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'pmod');
    $_SESSION['axes_cardeli'][$max]['check']=$check;
    $_SESSION['axes_cardeli'][$max]['icdis']=get_carprostinfo($unqid,'icdis');
    $_SESSION['axes_cardeli'][$max]['icinvdis']=get_carprostinfo($unqid,'icinvdis');    
    $_SESSION['axes_cardeli'][$max]['cost']=get_carprostinfo($unqid,'cost'); 
    $_SESSION['axes_cardeli'][$max]['qty']=$qty;
  
    $price=get_carprostinfo($unqid,'price');

    $_SESSION['axes_cardeli'][$max]['price']=$price;
    $_SESSION['axes_cardeli'][$max]['disp']=get_carprostinfo($unqid,'disp');
    $_SESSION['axes_cardeli'][$max]['disf']=get_carprostinfo($unqid,'disf');    
    $_SESSION['axes_cardeli'][$max]['disamo']=$disamo;
    $_SESSION['axes_cardeli'][$max]['wday']=get_carprostinfo($unqid,'wday');
    $_SESSION['axes_cardeli'][$max]['pnote']='';    
    $_SESSION['axes_cardeli'][$max]['subtot']=(($price*$qty)-$disamo);     
    }else{
    $_SESSION['axes_cardeli']=array();    
    $_SESSION['axes_cardeli'][0]['unqid']=$unqid;
    $_SESSION['axes_cardeli'][0]['pid']=get_cardeliinfo($unqid,'pid');    
    $_SESSION['axes_cardeli'][0]['code']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'code');
    $_SESSION['axes_cardeli'][0]['name']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'name');
    $_SESSION['axes_cardeli'][0]['unid']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'unid');
    $_SESSION['axes_cardeli'][0]['col']=get_cardeliinfo($unqid,'colid');
    $_SESSION['axes_cardeli'][0]['siz']=get_cardeliinfo($unqid,'sizid');    
    $_SESSION['axes_cardeli'][0]['imei']=get_fild_data('tbl_item',get_cardeliinfo($unqid,'pid'),'pmod');
    $_SESSION['axes_cardeli'][0]['check']=$check;
    $_SESSION['axes_cardeli'][0]['icdis']=get_carprostinfo($unqid,'icdis');
    $_SESSION['axes_cardeli'][0]['icinvdis']=get_carprostinfo($unqid,'icinvdis');    
    $_SESSION['axes_cardeli'][0]['cost']=get_carprostinfo($unqid,'cost');    
    $_SESSION['axes_cardeli'][0]['cost']=get_carprostinfo($unqid,'cost'); 
    $_SESSION['axes_cardeli'][0]['qty']=$qty;
    $price=get_carprostinfo($unqid,'price');
    $_SESSION['axes_cardeli'][0]['price']=$price;
    $_SESSION['axes_cardeli'][0]['disp']=get_carprostinfo($unqid,'disp');
    $_SESSION['axes_cardeli'][0]['disf']=get_carprostinfo($unqid,'disf');    
    $_SESSION['axes_cardeli'][0]['disamo']=$disamo;
    $_SESSION['axes_cardeli'][0]['wday']=get_carprostinfo($unqid,'wday');
    $_SESSION['axes_cardeli'][0]['pnote']='';    
    $_SESSION['axes_cardeli'][0]['subtot']=(($price*$qty)-$disamo);    
    }
    }
    
    
    function carpro_exists($brid,$unqid,$check){
    $max=count($_SESSION['axes_cardeli']);
    $flag=0;    
        
    for($i=0;$i<$max;$i++){   
    if($unqid==$_SESSION['axes_cardeli'][$i]['unqid']){
    if($check=='M'){
    $_SESSION['axes_cardeli'][$i]['check']='M';    
    }
    $flag=1;
    $eqty=(get_carstockinfo($brid,$unqid,'avqty'));
    $cqty=get_carexproduct_count($unqid);
    if($cqty<$eqty){    
    $qy=$_SESSION['axes_cardeli'][$i]['qty'];    
    $_SESSION['axes_cardeli'][$i]['qty']=$qy+1;
    $dis=$_SESSION['axes_cardeli'][$i]['disf'];
    $price=$_SESSION['axes_cardeli'][$i]['price'];
    $disamo=getfloatval(($dis*($qy+1)));
    $subtot=(($price*($qy+1))-$disamo);     
    $_SESSION['axes_cardeli'][$i]['disamo']=$disamo;
    $_SESSION['axes_cardeli'][$i]['subtot']=$subtot; 
    }
    break;    
    }        
    }       
    return $flag;    
    }
    
    function get_carexproduct_count($unqid){
    $cqty=0;
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    $qty=$_SESSION['axes_cardeli'][$i]['qty'];
    $itid=$_SESSION['axes_cardeli'][$i]['unqid'];
    if($unqid==$itid){
    $cqty+=$qty;
    }
    }
    if($cqty>0){
    return $cqty;	
    }else{
    return 0;	
    }
    }else{
    return 0;	
    }
    }
    
    function get_selproduct_count($unqid){
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    $sum=0;
    $qty=0;
    for($i=0;$i<$max;$i++){
    $qty=$_SESSION['axes_cardeli'][$i]['qty'];
    $exunqid=$_SESSION['axes_cardeli'][$i]['unqid'];
    if($unqid==$exunqid){
    $sum+=$qty;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }	
    }
    
    function get_cardelimobile_count(){
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    $sum=0;
    $qty=0;
    for($i=0;$i<$max;$i++){
    $qty=$_SESSION['axes_cardeli'][$i]['qty'];
    $reimei=intval($_SESSION['axes_cardeli'][$i]['imei']);
    if($reimei>=1){
    $sum+=$qty;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }        
    }
    
    function remove_sales($pid){
    $pid=intval($pid);
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    if($pid==$i){
    unset($_SESSION['axes_cardeli'][$i]);
    break;
    }
    }
    $_SESSION['axes_cardeli']=array_values($_SESSION['axes_cardeli']);
    }
    }
    
    function get_cartitemid($unqid){
    $id='';    
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    $eunqid=$_SESSION['axes_cardeli'][$i]['unqid'];    
    if($unqid==$eunqid){
    $id=$i;    
    break;    
    }    
    }
    }else{
    $id='';   
    }
    return $id;     
    }
    
    function get_cardeli_total(){
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    $sum=0;
    $stotal=0;
    for($i=0;$i<$max;$i++){
    $stotal=$_SESSION['axes_cardeli'][$i]['subtot'];
    $sum+=$stotal;
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }       
    }
        
    function get_cardelidiscount_total($edp=''){
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    $sum=0;
    for($i=0;$i<$max;$i++){
    $idis=$_SESSION['axes_cardeli'][$i]['disamo'];
    $sum+=$idis;
    }	
    $bota=get_cardeli_total();     
    $rdisc=0;    
    if($edp>0){
    $rdisc=(($bota*$edp)*0.01);    
    }
    $acdis=($sum+$rdisc);     
    if($acdis>0){     
    return ($acdis);
    }else{
    return 0;    
    }
    }else{
    return 0;   
    }
    }
    
    function add_cardeliserial($pid,$unqid,$data,$man=''){
            
    if ( !isset($_SESSION['axes_cardelise']) ) {
    $_SESSION['axes_cardelise'] = array();
    };	
            
    $excount = get_cardelimobile_count();
            
    if($excount <= 0){unset($_SESSION['axes_cardelise']);}
            
    if(is_array($_SESSION['axes_cardelise'])){
    if($man==''){
    if(cardeliimei_exists($data)) return;	
    }
        
    $max=count($_SESSION['axes_cardelise']);
    $_SESSION['axes_cardelise'][$max]['pid']=$pid;
    $_SESSION['axes_cardelise'][$max]['unqid']=$unqid;
    $_SESSION['axes_cardelise'][$max]['cartid']=get_cartitemid($unqid);    
    $_SESSION['axes_cardelise'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
    $_SESSION['axes_cardelise'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
    $_SESSION['axes_cardelise'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
    $_SESSION['axes_cardelise'][$max]['imei']=strtoupper($data);
    }else{
    $_SESSION['axes_cardelise']=array();
    $_SESSION['axes_cardelise'][0]['pid']=$pid;
    $_SESSION['axes_cardelise'][0]['unqid']=$unqid;
    $_SESSION['axes_cardelise'][0]['cartid']=get_cartitemid($unqid);     
    $_SESSION['axes_cardelise'][0]['code']=get_fild_data('tbl_item',$pid,'code');
    $_SESSION['axes_cardelise'][0]['name']=get_fild_data('tbl_item',$pid,'name');
    $_SESSION['axes_cardelise'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
    $_SESSION['axes_cardelise'][0]['imei']=strtoupper($data);
    }
    }
    
    function cardeliimei_exists($pid,$data){
    global $con;
    $flag=0;    
    $data=strtoupper($data);
    $sql="SELECT * FROM tbl_carserial WHERE pid='$pid' AND serial='$data' AND status='0' LIMIT 1";
    $query=mysqli_query($con,$sql)or die(mysqli_error($con));    
    if($query->num_rows > 0) {
    if (isset($_SESSION['axes_cardelise']) ){
    $max=count($_SESSION['axes_cardelise']);
    for($i=0;$i<$max;$i++){
    if($data==$_SESSION['axes_cardelise'][$i]['imei']){
    $flag=1;
    break;
    }
    }	
    }    
    }else{
    $flag=1;
    }
    return $flag;
    }
    
    function cardeliimei_pidewos($unqid){
    $flag=0;
    if (isset($_SESSION['axes_cardelise']) ){
    $max=count($_SESSION['axes_cardelise']);
    for($i=0;$i<$max;$i++){
    if($unqid==$_SESSION['axes_cardelise'][$i]['unqid'] && $_SESSION['axes_cardelise'][$i]['imei']==''){
    $flag=$i+1;
    break;
    }
    }
    }
    return $flag;    
    }
    
    function get_cardeliserial_count($pid){
    if(isset($_SESSION['axes_cardelise'])){
    $max=count($_SESSION['axes_cardelise']);
    $sum=0;
    for($i=0;$i<$max;$i++){
    $proid=intval($_SESSION['axes_cardelise'][$i]['pid']);
    if($pid==$proid){
    $sum+=1;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }			
    }
        
    function remove_cardeliimei($pid){
    $pid=intval($pid);
    if(isset($_SESSION['axes_cardelise'])){
    $max=count($_SESSION['axes_cardelise']);
    for($i=0;$i<$max;$i++){
    if($pid==$i){
    unset($_SESSION['axes_cardelise'][$i]);
    break;
    }
    }
    $_SESSION['axes_cardelise']=array_values($_SESSION['axes_cardelise']);
    }
    }
    
    function remove_deliimei($unqid){ 
    $max=count($_SESSION['axes_cardelise']);
    for($i=0;$i<$max;$i++){   
    if($unqid==$_SESSION['axes_cardelise'][$i]['unqid']){   
    unset($_SESSION['axes_cardelise'][$i]);    
    }    
    }
    $_SESSION['axes_cardelise']=array_values($_SESSION['axes_cardelise']);    
    }
    
    function check_cardelieximei($data){
    global $con;
    $flag=0;
    $sql="SELECT COUNT(*) AS exdata FROM tbl_carserial WHERE serial='$data'";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));
    $row=mysqli_fetch_array($result);
    if($row['exdata']>0){
    $flag=1;	
    }else{
    $flag=0;	
    }
    return $flag;	
    }
            
    function get_cardeliimei_count(){
    if(isset($_SESSION['axes_cardelise'])){
    $max=count($_SESSION['axes_cardelise']);
    $sum=0;
    for($i=0;$i<$max;$i++){
    $imei=strtoupper($_SESSION['axes_cardelise'][$i]['imei']);
    if($imei!=''){
    $sum+=1;
    }
    }
    if($sum > 0){
    return $sum;
    }else{
    return 0;    
    }
    }else{
    return 0;        
    }        
    }
    
    function check_cardelivalidqty($brid){
    $flag=0;
    if(isset($_SESSION['axes_cardeli'])){    
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    $unqid=$_SESSION['axes_cardeli'][$i]['unqid'];    
    $eqty=(get_carstockinfo($brid,$unqid,'avqty'));
    $qty=$_SESSION['axes_cardeli'][$i]['qty'];
    if($qty>$eqty){
    $flag=1;
    break;    
    }    
    }    
    }else{
    $flag=0;    
    }
    return $flag;    
    }
    
    function check_cardeliprice($key){
    $res=0;
    $max=0;    
    if(isset($_SESSION['axes_cardeli'])){
    $max=count($_SESSION['axes_cardeli']);
    for($i=0;$i<$max;$i++){
    $price=floatval($_SESSION['axes_cardeli'][$i]['price']);
    if($price<=0){
    $res+=1;    
    }    
    }    
    }else{
    $max=0;    
    $res=0;    
    }    
    if($key=='I'){
    return $max;    
    }elseif($key=='P'){
    return $res;    
    }    
    }
    
    function check_cardeliserial($key){
    $res=0;
    $max=0;    
    if(isset($_SESSION['axes_cardelise'])){
    $max=count($_SESSION['axes_cardelise']);
    for($i=0;$i<$max;$i++){
    $serial=$_SESSION['axes_cardelise'][$i]['imei'];
    if(strlen($serial)<2 || $serial==''){
    $res+=1;    
    }    
    }    
    }else{
    $max=0;    
    $res=0;    
    }    
    if($key=='I'){
    return $max;    
    }elseif($key=='S'){
    return $res;    
    }    
    }
    
?>