<?php
/*--------------------------------------------------------------*/
/* Function for Generate PI
/*--------------------------------------------------------------*/
function read_item($id,$fild){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_item WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
return $adm['rdata'];    
}

function read_piinfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_proinv WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_piinfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_pidata'])){
$_SESSION['axes_pidata']= array();
$_SESSION['axes_pidata']['pino']='';
$_SESSION['axes_pidata']['date']='';
$_SESSION['axes_pidata']['curid']='';
$_SESSION['axes_pidata']['cusid']='';
$_SESSION['axes_pidata']['supid']='';
$_SESSION['axes_pidata']['terms']='';
$_SESSION['axes_pidata']['dldate']='';
$_SESSION['axes_pidata']['dldays']='';
$_SESSION['axes_pidata']['pofemb']=''; 
$_SESSION['axes_pidata']['pofdis']='';
$_SESSION['axes_pidata']['bfbank']='';
$_SESSION['axes_pidata']['bfac']='';    
}
if(isset($_SESSION['axes_pidata'])){
if($key=='pino'){
$_SESSION['axes_pidata']['pino']=strtoupper(read_piinfo('pino',$sval,$id));
}elseif($key=='date'){
$_SESSION['axes_pidata']['date']=read_piinfo('apdate',$sval,$id);
}elseif($key=='curid'){
$_SESSION['axes_pidata']['curid']=read_piinfo('curid',$sval,$id);
}elseif($key=='cusid'){
$_SESSION['axes_pidata']['cusid']=read_piinfo('cusid',$sval,$id);
}elseif($key=='supid'){
$_SESSION['axes_pidata']['supid']=read_piinfo('supid',$sval,$id);
}elseif($key=='terms'){
$_SESSION['axes_pidata']['terms']=read_piinfo('terms',$sval,$id);
}elseif($key=='dldate'){
$_SESSION['axes_pidata']['dldate']=read_piinfo('dldate',$sval,$id);
}elseif($key=='dldays'){
$_SESSION['axes_pidata']['dldays']=read_piinfo('dldays',$sval,$id);
}elseif($key=='pofemb'){
$_SESSION['axes_pidata']['pofemb']=ucwords(read_piinfo('pofemb',$sval,$id));
}elseif($key=='pofdis'){
$_SESSION['axes_pidata']['pofdis']=ucwords(read_piinfo('pofdis',$sval,$id));
}elseif($key=='bfbank'){
$_SESSION['axes_pidata']['bfbank']=ucwords(read_piinfo('bfbank',$sval,$id));
}elseif($key=='bfac'){
$_SESSION['axes_pidata']['bfac']=strtoupper(read_piinfo('bfac',$sval,$id));    
}    
}    
}

function read_piitem($fild,$sval,$seid=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_proinvde WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function addtopi($itemid,$qty){
if($qty<=0) return;    
if (!isset($_SESSION['axes_piitem']) ) {
$_SESSION['axes_piitem'] = array();
}

if(is_array($_SESSION['axes_piitem'])){
if(piitem_exists($itemid)) return;
$max=count($_SESSION['axes_piitem']);
$_SESSION['axes_piitem'][$max]['itemid']=$itemid;
$_SESSION['axes_piitem'][$max]['color']=read_item($itemid,'colid'); 
$_SESSION['axes_piitem'][$max]['itemname']= read_item($itemid,'name'); 
$_SESSION['axes_piitem'][$max]['qty']=$qty; 
$_SESSION['axes_piitem'][$max]['unit']=read_item($itemid,'unid'); 
$_SESSION['axes_piitem'][$max]['price']=0; 
$_SESSION['axes_piitem'][$max]['subtot']=0; 
$_SESSION['axes_piitem'][$max]['cfamo']=0; 
$_SESSION['axes_piitem'][$max]['cp']=0; 
$_SESSION['axes_piitem'][$max]['tcamo']=0;     
}else{
$_SESSION['axes_piitem']=array();
$_SESSION['axes_piitem'][0]['itemid']=$itemid;
$_SESSION['axes_piitem'][0]['color']=read_item($itemid,'colid'); 
$_SESSION['axes_piitem'][0]['itemname']=read_item($itemid,'name'); 
$_SESSION['axes_piitem'][0]['qty']=$qty; 
$_SESSION['axes_piitem'][0]['unit']=read_item($itemid,'unid'); 
$_SESSION['axes_piitem'][0]['price']=0; 
$_SESSION['axes_piitem'][0]['subtot']=0; 
$_SESSION['axes_piitem'][0]['cfamo']=0; 
$_SESSION['axes_piitem'][0]['cp']=0; 
$_SESSION['axes_piitem'][0]['tcamo']=0;     
}    
}

function piitem_exists($pid){
$pid=intval($pid);
$max=count($_SESSION['axes_piitem']);
$flag=0;
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['axes_piitem'][$i]['itemid']){
$flag=1;
$qty = $_SESSION['axes_piitem'][$i]['qty'];
$_SESSION['axes_piitem'][$i]['qty']=$qty+1;
$price = $_SESSION['axes_piitem'][$i]['price'];
$cfamo = $_SESSION['axes_piitem'][$i]['cfamo'];
$_SESSION['axes_piitem'][$i]['subtot']=($price*($qty+1));
$_SESSION['axes_piitem'][$i]['tcamo']=($cfamo*($qty+1));
break;    
}    
}
return $flag;    
}

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

function total_pivalue($key){
if(isset($_SESSION['axes_piitem'])){
$max=count($_SESSION['axes_piitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
if($key=='Q'){
$tot=$_SESSION['axes_piitem'][$i]['qty'];     
}elseif($key=='V'){
$tot=$_SESSION['axes_piitem'][$i]['subtot'];    
}else{
$tot=$_SESSION['axes_piitem'][$i]['tcamo'];    
}    
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Invoice
/*--------------------------------------------------------------*/

function read_invinfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_invoice WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_invinfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_invdata'])){
$_SESSION['axes_invdata']= array();
$_SESSION['axes_invdata']['invno']='';
$_SESSION['axes_invdata']['pino']='';
$_SESSION['axes_invdata']['date']='';
$_SESSION['axes_invdata']['curid']='';
$_SESSION['axes_invdata']['cusid']='';
$_SESSION['axes_invdata']['supid']='';    
}
if(isset($_SESSION['axes_invdata'])){
if($key=='invno'){
$_SESSION['axes_invdata']['invno']=strtoupper(read_invinfo('invno',$sval,$id));
}elseif($key=='pino'){
$_SESSION['axes_invdata']['pino']=strtoupper(read_invinfo('pino',$sval,$id));    
}elseif($key=='date'){
$_SESSION['axes_invdata']['date']=read_invinfo('apdate',$sval,$id);
}elseif($key=='curid'){
$_SESSION['axes_invdata']['curid']=read_invinfo('curid',$sval,$id);
}elseif($key=='cusid'){
$_SESSION['axes_invdata']['cusid']=$sval;
}elseif($key=='supid'){
$_SESSION['axes_invdata']['supid']=$sval;   
}    
}    
}

function addtoinv($id,$itemid,$qty,$price,$camo){
if($qty<=0) return;    
if (!isset($_SESSION['axes_invitem']) ) {
$_SESSION['axes_invitem'] = array();
}

if(is_array($_SESSION['axes_invitem'])){
if(invitem_exists($itemid)) return;
$max=count($_SESSION['axes_invitem']);
$_SESSION['axes_invitem'][$max]['itemid']=$itemid;
$_SESSION['axes_invitem'][$max]['pisid']=$id;    
$_SESSION['axes_invitem'][$max]['color']=read_item($itemid,'colid'); 
$_SESSION['axes_invitem'][$max]['itemname']= get_name($itemid); 
$_SESSION['axes_invitem'][$max]['qty']=$qty; 
$_SESSION['axes_invitem'][$max]['unit']=read_item($itemid,'unid'); 
$_SESSION['axes_invitem'][$max]['price']=$price;
$_SESSION['axes_invitem'][$max]['cfamo']=$camo; 
$_SESSION['axes_invitem'][$max]['subtot']=($price*$qty);
$_SESSION['axes_invitem'][$max]['tcamo']=($camo*$qty);    
}else{
$_SESSION['axes_invitem']=array();
$_SESSION['axes_invitem'][0]['itemid']=$itemid;
$_SESSION['axes_invitem'][0]['pisid']=$id;    
$_SESSION['axes_invitem'][0]['color']=read_item($itemid,'colid'); 
$_SESSION['axes_invitem'][0]['itemname']=get_name($itemid); 
$_SESSION['axes_invitem'][0]['qty']=$qty; 
$_SESSION['axes_invitem'][0]['unit']=read_item($itemid,'unid'); 
$_SESSION['axes_invitem'][0]['price']=$price;
$_SESSION['axes_invitem'][0]['cfamo']=$camo;    
$_SESSION['axes_invitem'][0]['subtot']=($price*$qty);;
$_SESSION['axes_invitem'][0]['tcamo']=($camo*$qty);
}    
}

function invitem_exists($pid){
$pid=intval($pid);
$max=count($_SESSION['axes_invitem']);
$flag=0;
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['axes_invitem'][$i]['itemid']){
$flag=1;
$qty = $_SESSION['axes_invitem'][$i]['qty'];
$_SESSION['axes_invitem'][$i]['qty']=$qty+1;
$price = $_SESSION['axes_invitem'][$i]['price'];
$cfamo = $_SESSION['axes_invitem'][$i]['cfamo'];    
$_SESSION['axes_invitem'][$i]['subtot']=($price*($qty+1));
$_SESSION['axes_invitem'][$i]['tcamo']=($cfamo*($qty+1));    
break;    
}    
}
return $flag;    
}

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

function total_invvalue($key){
if(isset($_SESSION['axes_invitem'])){
$max=count($_SESSION['axes_invitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
if($key=='Q'){
$tot=$_SESSION['axes_invitem'][$i]['qty'];     
}elseif($key=='V'){
$tot=$_SESSION['axes_invitem'][$i]['subtot'];    
}else{
$tot=$_SESSION['axes_invitem'][$i]['tcamo'];    
}    
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Expenses
/*--------------------------------------------------------------*/

function read_expinfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_expenses WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_expinfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_expdata'])){
$_SESSION['axes_expdata']= array();
$_SESSION['axes_expdata']['invno']='';
$_SESSION['axes_expdata']['note']='';    
$_SESSION['axes_expdata']['date']='';   
}
if(isset($_SESSION['axes_expdata'])){
if($key=='invno'){
$_SESSION['axes_expdata']['invno']=strtoupper(read_expinfo('invno',$sval,$id));
}elseif($key=='note'){
$_SESSION['axes_expdata']['note']=read_expinfo('note',$sval,$id);    
}elseif($key=='date'){
$_SESSION['axes_expdata']['date']=read_expinfo('apdate',$sval,$id);   
}    
}    
}

function addtoexp($expid,$amount,$ref){
if($amount<=0) return;    
if (!isset($_SESSION['axes_expitem']) ) {
$_SESSION['axes_expitem'] = array();
}

if(is_array($_SESSION['axes_expitem'])){
$max=count($_SESSION['axes_expitem']);
$_SESSION['axes_expitem'][$max]['expid']=$expid;
$_SESSION['axes_expitem'][$max]['amount']=$amount;    
$_SESSION['axes_expitem'][$max]['ref']=$ref;    
}else{
$_SESSION['axes_expitem']=array();
$_SESSION['axes_expitem'][0]['expid']=$expid;
$_SESSION['axes_expitem'][0]['amount']=$amount;    
$_SESSION['axes_expitem'][0]['ref']=$ref; 
}    
}

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

function total_expvalue(){
if(isset($_SESSION['axes_expitem'])){
$max=count($_SESSION['axes_expitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
$tot=$_SESSION['axes_expitem'][$i]['amount'];        
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Journal entry
/*--------------------------------------------------------------*/

function read_jouinfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_journal WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_jouinfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_joudata'])){
$_SESSION['axes_joudata']= array();
$_SESSION['axes_joudata']['invno']='';
$_SESSION['axes_joudata']['note']='';    
$_SESSION['axes_joudata']['date']='';   
}
if(isset($_SESSION['axes_joudata'])){
if($key=='invno'){
$_SESSION['axes_joudata']['invno']=strtoupper(read_jouinfo('invno',$sval,$id));
}elseif($key=='note'){
$_SESSION['axes_joudata']['note']=read_jouinfo('note',$sval,$id);    
}elseif($key=='date'){
$_SESSION['axes_joudata']['date']=read_jouinfo('apdate',$sval,$id);   
}    
}    
}

function addtojou($dsgrid,$dty,$did,$amo,$cid,$cty,$csgrid,$chkno,$chkdt,$ref){
if($amo<=0) return;    
if (!isset($_SESSION['axes_jouitem']) ) {
$_SESSION['axes_jouitem'] = array();
}

if(is_array($_SESSION['axes_jouitem'])){
$max=count($_SESSION['axes_jouitem']);
$_SESSION['axes_jouitem'][$max]['dsgrid']=$dsgrid;     
$_SESSION['axes_jouitem'][$max]['dty']=$dty;
$_SESSION['axes_jouitem'][$max]['did']=$did;
$_SESSION['axes_jouitem'][$max]['amo']=$amo;
$_SESSION['axes_jouitem'][$max]['cid']=$cid;
$_SESSION['axes_jouitem'][$max]['cty']=$cty;
$_SESSION['axes_jouitem'][$max]['csgrid']=$csgrid;    
$_SESSION['axes_jouitem'][$max]['chkno']=$chkno;
$_SESSION['axes_jouitem'][$max]['chkdt']=$chkdt;
$_SESSION['axes_jouitem'][$max]['ref']=$ref; 
}else{
$_SESSION['axes_jouitem']=array();
$_SESSION['axes_jouitem'][0]['dsgrid']=$dsgrid;    
$_SESSION['axes_jouitem'][0]['dty']=$dty;
$_SESSION['axes_jouitem'][0]['did']=$did;
$_SESSION['axes_jouitem'][0]['amo']=$amo;
$_SESSION['axes_jouitem'][0]['cid']=$cid;
$_SESSION['axes_jouitem'][0]['cty']=$cty;
$_SESSION['axes_jouitem'][0]['csgrid']=$csgrid;     
$_SESSION['axes_jouitem'][0]['chkno']=$chkno;
$_SESSION['axes_jouitem'][0]['chkdt']=$chkdt;
$_SESSION['axes_jouitem'][0]['ref']=$ref;    
}    
}

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

function total_jouvalue(){
if(isset($_SESSION['axes_jouitem'])){
$max=count($_SESSION['axes_jouitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
$tot=$_SESSION['axes_jouitem'][$i]['amo'];        
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Paymeny Voucher
/*--------------------------------------------------------------*/

function read_payinfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_payvoucher WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_payinfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_paydata'])){
$_SESSION['axes_paydata']= array();
$_SESSION['axes_paydata']['invno']='';
$_SESSION['axes_paydata']['note']='';    
$_SESSION['axes_paydata']['date']='';   
}
if(isset($_SESSION['axes_paydata'])){
if($key=='invno'){
$_SESSION['axes_paydata']['invno']=strtoupper(read_payinfo('invno',$sval,$id));
}elseif($key=='note'){
$_SESSION['axes_paydata']['note']=read_payinfo('note',$sval,$id);    
}elseif($key=='date'){
$_SESSION['axes_paydata']['date']=read_payinfo('apdate',$sval,$id);   
}    
}    
}

function addtopay($dty,$did,$amo,$cid,$cty,$chkno,$chkdt,$ref){
if($amo<=0) return;    
if (!isset($_SESSION['axes_payitem']) ) {
$_SESSION['axes_payitem'] = array();
}

if(is_array($_SESSION['axes_payitem'])){
$max=count($_SESSION['axes_payitem']);     
$_SESSION['axes_payitem'][$max]['dty']=$dty;
$_SESSION['axes_payitem'][$max]['did']=$did;
$_SESSION['axes_payitem'][$max]['amo']=$amo;
$_SESSION['axes_payitem'][$max]['cid']=$cid;
$_SESSION['axes_payitem'][$max]['cty']=$cty;   
$_SESSION['axes_payitem'][$max]['chkno']=$chkno;
$_SESSION['axes_payitem'][$max]['chkdt']=$chkdt;
$_SESSION['axes_payitem'][$max]['ref']=$ref; 
}else{
$_SESSION['axes_payitem']=array();   
$_SESSION['axes_payitem'][0]['dty']=$dty;
$_SESSION['axes_payitem'][0]['did']=$did;
$_SESSION['axes_payitem'][0]['amo']=$amo;
$_SESSION['axes_payitem'][0]['cid']=$cid;
$_SESSION['axes_payitem'][0]['cty']=$cty;    
$_SESSION['axes_payitem'][0]['chkno']=$chkno;
$_SESSION['axes_payitem'][0]['chkdt']=$chkdt;
$_SESSION['axes_payitem'][0]['ref']=$ref;    
}    
}

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

function total_payvalue(){
if(isset($_SESSION['axes_payitem'])){
$max=count($_SESSION['axes_payitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
$tot=$_SESSION['axes_payitem'][$i]['amo'];        
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Received Voucher
/*--------------------------------------------------------------*/

function read_recinfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_recvoucher WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_recinfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_recdata'])){
$_SESSION['axes_recdata']= array();
$_SESSION['axes_recdata']['invno']='';
$_SESSION['axes_recdata']['note']='';    
$_SESSION['axes_recdata']['date']='';   
}
if(isset($_SESSION['axes_recdata'])){
if($key=='invno'){
$_SESSION['axes_recdata']['invno']=strtoupper(read_recinfo('invno',$sval,$id));
}elseif($key=='note'){
$_SESSION['axes_recdata']['note']=read_recinfo('note',$sval,$id);    
}elseif($key=='date'){
$_SESSION['axes_recdata']['date']=read_recinfo('apdate',$sval,$id);   
}    
}    
}

function addtorec($dty,$did,$amo,$cid,$cty,$chkno,$chkdt,$ref,$refinv='',$xrate=''){
if($amo<=0) return;    
if (!isset($_SESSION['axes_recitem']) ) {
$_SESSION['axes_recitem'] = array();
}

if(is_array($_SESSION['axes_recitem'])){
$max=count($_SESSION['axes_recitem']);
$_SESSION['axes_recitem'][$max]['refinv']=$refinv;    
$_SESSION['axes_recitem'][$max]['dty']=$dty;
$_SESSION['axes_recitem'][$max]['did']=$did;
$_SESSION['axes_recitem'][$max]['amo']=$amo;
$_SESSION['axes_recitem'][$max]['cid']=$cid;
$_SESSION['axes_recitem'][$max]['cty']=$cty;   
$_SESSION['axes_recitem'][$max]['chkno']=$chkno;
$_SESSION['axes_recitem'][$max]['chkdt']=$chkdt;
$_SESSION['axes_recitem'][$max]['ref']=$ref;
$_SESSION['axes_recitem'][$max]['xrate']=$xrate;  
}else{
$_SESSION['axes_recitem']=array();
$_SESSION['axes_recitem'][$max]['refinv']=$refinv;    
$_SESSION['axes_recitem'][0]['dty']=$dty;
$_SESSION['axes_recitem'][0]['did']=$did;
$_SESSION['axes_recitem'][0]['amo']=$amo;
$_SESSION['axes_recitem'][0]['cid']=$cid;
$_SESSION['axes_recitem'][0]['cty']=$cty;    
$_SESSION['axes_recitem'][0]['chkno']=$chkno;
$_SESSION['axes_recitem'][0]['chkdt']=$chkdt;
$_SESSION['axes_recitem'][0]['ref']=$ref;
$_SESSION['axes_recitem'][0]['xrate']=$xrate;    
}    
}

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

function total_recvalue(){
if(isset($_SESSION['axes_recitem'])){
$max=count($_SESSION['axes_recitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
$tot=$_SESSION['axes_recitem'][$i]['amo'];        
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Transaction Record
/*--------------------------------------------------------------*/

function read_trainfo($fild,$sval,$id=''){
global $con;
$sql="SELECT ".$fild." AS rdata FROM tbl_banktra WHERE id='$id' LIMIT 1";
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);
if($id!=''){
return $adm['rdata'];    
}else{
return $sval;    
}    
}

function add_trainfo($key,$sval,$id=''){
if (!isset($_SESSION['axes_tradata'])){
$_SESSION['axes_tradata']= array();
$_SESSION['axes_tradata']['invno']='';
$_SESSION['axes_tradata']['note']='';    
$_SESSION['axes_tradata']['date']='';   
}
if(isset($_SESSION['axes_tradata'])){
if($key=='invno'){
$_SESSION['axes_tradata']['invno']=strtoupper(read_trainfo('invno',$sval,$id));
}elseif($key=='note'){
$_SESSION['axes_tradata']['note']=read_trainfo('note',$sval,$id);    
}elseif($key=='date'){
$_SESSION['axes_tradata']['date']=read_trainfo('apdate',$sval,$id);   
}    
}    
}

function addtotra($dty,$did,$amo,$cid,$cty,$chkno,$chkdt,$ref){
if($amo<=0) return;    
if (!isset($_SESSION['axes_traitem']) ) {
$_SESSION['axes_traitem'] = array();
}

if(is_array($_SESSION['axes_traitem'])){
$max=count($_SESSION['axes_traitem']);     
$_SESSION['axes_traitem'][$max]['dty']=$dty;
$_SESSION['axes_traitem'][$max]['did']=$did;
$_SESSION['axes_traitem'][$max]['amo']=$amo;
$_SESSION['axes_traitem'][$max]['cid']=$cid;
$_SESSION['axes_traitem'][$max]['cty']=$cty;   
$_SESSION['axes_traitem'][$max]['chkno']=$chkno;
$_SESSION['axes_traitem'][$max]['chkdt']=$chkdt;
$_SESSION['axes_traitem'][$max]['ref']=$ref; 
}else{
$_SESSION['axes_traitem']=array();   
$_SESSION['axes_traitem'][0]['dty']=$dty;
$_SESSION['axes_traitem'][0]['did']=$did;
$_SESSION['axes_traitem'][0]['amo']=$amo;
$_SESSION['axes_traitem'][0]['cid']=$cid;
$_SESSION['axes_traitem'][0]['cty']=$cty;    
$_SESSION['axes_traitem'][0]['chkno']=$chkno;
$_SESSION['axes_traitem'][0]['chkdt']=$chkdt;
$_SESSION['axes_traitem'][0]['ref']=$ref;    
}    
}

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

function total_travalue(){
if(isset($_SESSION['axes_traitem'])){
$max=count($_SESSION['axes_traitem']);
$sum=0;
$tot=0;
for($i=0;$i<$max;$i++){
$tot=$_SESSION['axes_traitem'][$i]['amo'];        
$sum+=$tot;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}    
}

/*--------------------------------------------------------------*/
/* Function for Purchase Order
/*--------------------------------------------------------------*/
function get_ordeqty($pid){
global $con;
$sql="SELECT COALESCE ((SELECT (avqty-sloc) 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_purorder']) ) {
$_SESSION['axes_purorder'] = array();
}

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

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

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

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

function get_porder_total(){
if(isset($_SESSION['axes_purorder'])){
$max=count($_SESSION['axes_purorder']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_purorder'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_orddiscount_total($edp=''){
if(isset($_SESSION['axes_purorder'])){
$max=count($_SESSION['axes_purorder']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_purorder'][$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;   
}
}

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

/*--------------------------------------------------------------*/
/* Function for Purchase
/*--------------------------------------------------------------*/

function addtopur($pid,$qty,$col='',$siz='',$srl='',$cost=0,$disp=0,$disf=0,$disamo=0){
if($qty<=0) return;

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

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

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

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

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

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

function get_purchase_total(){
if(isset($_SESSION['axes_purchase'])){
$max=count($_SESSION['axes_purchase']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_purchase'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_purdiscount_total($edp=''){
if(isset($_SESSION['axes_purchase'])){
$max=count($_SESSION['axes_purchase']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_purchase'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_purchase_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 get_pucartitemid($pid){
$id='';    
if(isset($_SESSION['axes_purchase'])){
$max=count($_SESSION['axes_purchase']);
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_purchase'][$i]['pid'];    
if($pid==$epid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function add_purserial($pid,$data,$man=''){
		
if(!isset($_SESSION['axes_purse'])) {
$_SESSION['axes_purse'] = array();
};	
		
$excount = get_mobile_count();		
if($excount <= 0){unset($_SESSION['axes_purse']);}
		
if(is_array($_SESSION['axes_purse'])){
if($man==''){
if(imei_exists($data)) return;	
}
    
$max=count($_SESSION['axes_purse']);
$_SESSION['axes_purse'][$max]['pid']=$pid;
$_SESSION['axes_purse'][$max]['cartid']=get_pucartitemid($pid);    
$_SESSION['axes_purse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_purse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_purse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_purse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_purse']=array();
$_SESSION['axes_purse'][0]['pid']=$pid;
$_SESSION['axes_purse'][0]['cartid']=get_pucartitemid($pid);    
$_SESSION['axes_purse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_purse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_purse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_purse'][0]['imei']=strtoupper($data);
}
}

function imei_exists($data){
$data=strtoupper($data);
if (isset($_SESSION['axes_purse']) ){
$max=count($_SESSION['axes_purse']);
$flag=0;
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_purse'][$i]['imei']){
$flag=1;
break;
}
}
}else{
$flag=0;	
}
return $flag;
}

function pid_exists_wos($pid){
$spid='';    
if(isset($_SESSION['axes_purse'])){
$max=count($_SESSION['axes_purse']);
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_purse'][$i]['pid'];
$imei=$_SESSION['axes_purse'][$i]['imei'];
if($pid==$epid && $imei==''){
$spid=$i+1;
break;
}    
}   
}
return $spid; 
}
    
function get_serial_count($pid){
if(isset($_SESSION['axes_purse'])){
$max=count($_SESSION['axes_purse']);
$sum=0;
for($i=0;$i<$max;$i++){
$proid=intval($_SESSION['axes_purse'][$i]['pid']);
if($pid==$proid){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}			
}
	
function remove_imei($pid){
$pid=intval($pid);
if(isset($_SESSION['axes_purse'])){
$max=count($_SESSION['axes_purse']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_purse'][$i]);
break;
}
}
$_SESSION['axes_purse']=array_values($_SESSION['axes_purse']);
}
}

function remove_puimei($pid){ 
$max=count($_SESSION['axes_purse']);
for($i=0;$i<$max;$i++){   
if($pid==$_SESSION['axes_purse'][$i]['pid']){   
unset($_SESSION['axes_purse'][$i]);    
}    
}
$_SESSION['axes_purse']=array_values($_SESSION['axes_purse']);    
}

function check_imei($data){
$flag=0;
if(isset($_SESSION['axes_purse'])){
$max=count($_SESSION['axes_purse']);
for($i=0;$i<$max;$i++){
$imei=$_SESSION['axes_purse'][$i]['imei'];
if($data==$imei){
$flag=1;
break;	
}else{
$flag=0;	
}
}
}else{
return $flag;	
}
return $flag;
}

function check_eximei($data){
global $con;
$flag=0;
$sql="SELECT COUNT(*) AS exdata FROM (SELECT serial FROM tbl_serial 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_serial 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_empty(){
$flag=0;
if(isset($_SESSION['axes_purse'])){
$max=count($_SESSION['axes_purse']);
for($i=0;$i<$max;$i++){
$imei=$_SESSION['axes_purse'][$i]['imei'];
if($imei!=''){
$flag=0;	
}else{
$flag=1;	
}	
}	
}else{
$flag=0;	
}
return $flag;
}
	
function get_imei_count(){
if(isset($_SESSION['axes_purse'])){
$max=count($_SESSION['axes_purse']);
$sum=0;
for($i=0;$i<$max;$i++){
$imei=strtoupper($_SESSION['axes_purse'][$i]['imei']);
if($imei!=''){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}        
}

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

function purcheck(){
$cmsg='';
$flag=0;    
if(isset($_SESSION['axes_purchase'])) {
$max=count($_SESSION['axes_purchase']);
for($i=($max-1);$i>=0;$i=$i-1){
$name = $_SESSION['axes_purchase'][$i]['name'];     
$cost = $_SESSION['axes_purchase'][$i]['cost'];
if($cost=='' || $cost <= 0){
$cmsg=$name.' this item cost missing...';
$flag=1;    
break;    
}
}
  
if($flag!=1){     
for($j=($max-1);$j>=0;$j=$j-1){
$name = $_SESSION['axes_purchase'][$j]['name'];    
$price = $_SESSION['axes_purchase'][$j]['price'];    
if($price=='' || $price <= 0){
$cmsg=$name.' this item price missing...';
$flag=1;     
break;    
}    
}    
}
}

if($flag!=1){    
if(isset($_SESSION['axes_purse'])){
$n=0;    
$max=count($_SESSION['axes_purse']);
for($s=($max-1);$s>=0;$s=$s-1){
$n=$n+1;    
$imei = $_SESSION['axes_purse'][$s]['imei'];
if(strlen($imei)<1){    
$cmsg= 'Number '.($n).' Serial/IMEI missing..';    
break;
}
}    
}
}
    
return $cmsg;    
}

/*--------------------------------------------------------------*/
/* Function for Edit Purchase
/*--------------------------------------------------------------*/

function get_editpurchase($invno){
global $con;
$sql="SELECT * FROM tbl_traproduct WHERE invno='$invno' ORDER BY id ASC";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
while ($row=mysqli_fetch_array($result)){
addtoeditpurchase('E',$row['id'],$row['p_qty']);	
}
if(get_edmobile_count()>0){
$sql="SELECT * FROM tbl_serial WHERE purinv='$invno'";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
while ($row=mysqli_fetch_array($result)){
add_edpurserial('E',$row['pid'],$row['serial']);	
}	
}    
if (!isset($_SESSION['axes_edpurde'])) {

$sql="SELECT * FROM tbl_purchase WHERE invno='$invno' LIMIT 1";    
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);    
    
$_SESSION['axes_edpurde'] = array();
$_SESSION['axes_edpurde'][0]['disp']=$row['disp'];
$_SESSION['axes_edpurde'][0]['disamo']=$row['totdis'];    
$_SESSION['axes_edpurde'][0]['vatp']=$row['vatp'];
$_SESSION['axes_edpurde'][0]['vatamo']=$row['vatamo'];
$_SESSION['axes_edpurde'][0]['taxp']=$row['taxp'];
$_SESSION['axes_edpurde'][0]['taxamo']=$row['taxamo'];
$_SESSION['axes_edpurde'][0]['freight']=$row['freight'];
$_SESSION['axes_edpurde'][0]['spmony']=$row['spmoney'];
$_SESSION['axes_edpurde'][0]['name']=$row['otname'];
$_SESSION['axes_edpurde'][0]['others']=$row['otamo'];
$_SESSION['axes_edpurde'][0]['less']=$row['less'];
$_SESSION['axes_edpurde'][0]['gtotal']=$row['total'];    
}

} 

function getpuredit_info($mod,$id,$key,$cdata){
global $con;
$reval='';
if($mod=='E'){    
$sql="SELECT ".$key." AS rdata FROM tbl_traproduct WHERE id='$id' AND mods='PU' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
$reval=$row['rdata'];
}else{
$reval=$cdata;	
}
return $reval;
}

function addtoeditpurchase($mod,$pid,$qty,$col='',$siz=''){
if($qty<=0) return;
    
if ( !isset($_SESSION['axes_edpurchase']) ) {
$_SESSION['axes_edpurchase'] = array();
}

if(getpuredit_info($mod,$pid,'colid',$col)==0){
$col='';    
}    
if(getpuredit_info($mod,$pid,'sizid',$siz)==0){
$siz='';    
}
    
if(is_array($_SESSION['axes_edpurchase'])){
if($mod!='E'){    
if(purpro_edexists($pid,$col,$siz)) return;
}
$max=count($_SESSION['axes_edpurchase']);
$_SESSION['axes_edpurchase'][$max]['pid']=getpuredit_info($mod,$pid,'pid',$pid);
$_SESSION['axes_edpurchase'][$max]['code']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'code');
$_SESSION['axes_edpurchase'][$max]['name']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'name');
$_SESSION['axes_edpurchase'][$max]['unid']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'unid');
$_SESSION['axes_edpurchase'][$max]['col']=$col;
$_SESSION['axes_edpurchase'][$max]['siz']=$siz;   
$_SESSION['axes_edpurchase'][$max]['imei']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'pmod');
$_SESSION['axes_edpurchase'][$max]['check']='';    
$_SESSION['axes_edpurchase'][$max]['cost']=getpuredit_info($mod,$pid,'cost',get_proval($pid,'C')); 
$_SESSION['axes_edpurchase'][$max]['qty']=getpuredit_info($mod,$pid,'p_qty',$qty);
$_SESSION['axes_edpurchase'][$max]['price']=getpuredit_info($mod,$pid,'price',get_proval($pid,'P'));
$_SESSION['axes_edpurchase'][$max]['disp']=getpuredit_info($mod,$pid,'disp',0);
$_SESSION['axes_edpurchase'][$max]['disf']=getpuredit_info($mod,$pid,'disf',0);
$_SESSION['axes_edpurchase'][$max]['disamo']=getpuredit_info($mod,$pid,'disamo',0);
$_SESSION['axes_edpurchase'][$max]['sdisp']=0;
$_SESSION['axes_edpurchase'][$max]['sdisf']=0;
$_SESSION['axes_edpurchase'][$max]['wday']=getpuredit_info($mod,$pid,'wday',0);    
$_SESSION['axes_edpurchase'][$max]['subtot']=getpuredit_info($mod,$pid,'subtot',(get_proval($pid,'C')*$qty));    
}else{
$_SESSION['axes_edpurchase']=array();    
$_SESSION['axes_edpurchase'][0]['pid']=getpuredit_info($mod,$pid,'pid',$pid);
$_SESSION['axes_edpurchase'][0]['code']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'code');
$_SESSION['axes_edpurchase'][0]['name']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'name');
$_SESSION['axes_edpurchase'][0]['unid']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'unid');
$_SESSION['axes_edpurchase'][0]['col']=$col;
$_SESSION['axes_edpurchase'][0]['siz']=$siz;    
$_SESSION['axes_edpurchase'][0]['imei']=get_fild_data('tbl_item',getpuredit_info($mod,$pid,'pid',$pid),'pmod');
$_SESSION['axes_edpurchase'][0]['check']='';    
$_SESSION['axes_edpurchase'][0]['cost']=getpuredit_info($mod,$pid,'cost',get_proval($pid,'C')); 
$_SESSION['axes_edpurchase'][0]['qty']=getpuredit_info($mod,$pid,'p_qty',$qty);
$_SESSION['axes_edpurchase'][0]['price']=getpuredit_info($mod,$pid,'price',get_proval($pid,'P'));
$_SESSION['axes_edpurchase'][0]['disp']=getpuredit_info($mod,$pid,'disp',0);
$_SESSION['axes_edpurchase'][0]['disf']=getpuredit_info($mod,$pid,'disf',0);
$_SESSION['axes_edpurchase'][0]['disamo']=getpuredit_info($mod,$pid,'disamo',0);
$_SESSION['axes_edpurchase'][0]['sdisp']=0;
$_SESSION['axes_edpurchase'][0]['sdisf']=0;
$_SESSION['axes_edpurchase'][0]['wday']=getpuredit_info($mod,$pid,'wday',0);    
$_SESSION['axes_edpurchase'][0]['subtot']=getpuredit_info($mod,$pid,'subtot',(get_proval($pid,'C')*$qty));     
}    
}

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

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

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

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

function get_edpurchase_total(){
if(isset($_SESSION['axes_edpurchase'])){
$max=count($_SESSION['axes_edpurchase']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_edpurchase'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_edpurdiscount_total($edp=''){
if(isset($_SESSION['axes_edpurchase'])){
$max=count($_SESSION['axes_edpurchase']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_edpurchase'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_purchase_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 get_edpucartitemid($pid){
$id='';    
if(isset($_SESSION['axes_edpurchase'])){
$max=count($_SESSION['axes_edpurchase']);
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_edpurchase'][$i]['pid'];    
if($pid==$epid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function add_edpurserial($mod,$pid,$data,$man=''){
		
if(!isset($_SESSION['axes_edpurse'])) {
$_SESSION['axes_edpurse'] = array();
};	
		
$excount = get_edmobile_count();		
if($excount <= 0){unset($_SESSION['axes_edpurse']);}
		
if(is_array($_SESSION['axes_edpurse'])){
if($mod!='E'){    
if($man==''){
if(imei_edexists($data)) return;	
}
}
    
$max=count($_SESSION['axes_edpurse']);
$_SESSION['axes_edpurse'][$max]['pid']=$pid;
$_SESSION['axes_edpurse'][$max]['cartid']=get_edpucartitemid($pid);    
$_SESSION['axes_edpurse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_edpurse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_edpurse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_edpurse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_edpurse']=array();
$_SESSION['axes_edpurse'][0]['pid']=$pid;
$_SESSION['axes_edpurse'][0]['cartid']=get_edpucartitemid($pid);    
$_SESSION['axes_edpurse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_edpurse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_edpurse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_edpurse'][0]['imei']=strtoupper($data);
}
}

function imei_edexists($data){
$data=strtoupper($data);
if (isset($_SESSION['axes_edpurse']) ){
$max=count($_SESSION['axes_edpurse']);
$flag=0;
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_edpurse'][$i]['imei']){
$flag=1;
break;
}
}
}else{
$flag=0;	
}
return $flag;
}
	
function get_edserial_count($pid){
if(isset($_SESSION['axes_edpurse'])){
$max=count($_SESSION['axes_edpurse']);
$sum=0;
for($i=0;$i<$max;$i++){
$proid=intval($_SESSION['axes_edpurse'][$i]['pid']);
if($pid==$proid){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}			
}
	
function remove_edimei($pid){
$pid=intval($pid);
if(isset($_SESSION['axes_edpurse'])){
$max=count($_SESSION['axes_edpurse']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_edpurse'][$i]);
break;
}
}
$_SESSION['axes_edpurse']=array_values($_SESSION['axes_edpurse']);
}
}

function remove_edpuimei($pid){ 
$max=count($_SESSION['axes_edpurse']);
for($i=0;$i<$max;$i++){   
if($pid==$_SESSION['axes_edpurse'][$i]['pid']){   
unset($_SESSION['axes_edpurse'][$i]);    
}    
}
$_SESSION['axes_edpurse']=array_values($_SESSION['axes_edpurse']);    
}

function check_edimei($data){
$flag=0;
if(isset($_SESSION['axes_edpurse'])){
$max=count($_SESSION['axes_edpurse']);
for($i=0;$i<$max;$i++){
$imei=$_SESSION['axes_edpurse'][$i]['imei'];
if($data==$imei){
$flag=1;
break;	
}else{
$flag=0;	
}
}
}else{
return $flag;	
}
return $flag;
}

function check_edeximei($invno,$data){
global $con;
$flag=0;
$sql="SELECT COUNT(*) AS exdata FROM tbl_serial WHERE purinv!='$invno' AND 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_edempty(){
$flag=0;
if(isset($_SESSION['axes_edpurse'])){
$max=count($_SESSION['axes_edpurse']);
for($i=0;$i<$max;$i++){
$imei=$_SESSION['axes_edpurse'][$i]['imei'];
if($imei!=''){
$flag=0;	
}else{
$flag=1;	
}	
}	
}else{
$flag=0;	
}
return $flag;
}
	
function get_edimei_count(){
if(isset($_SESSION['axes_edpurse'])){
$max=count($_SESSION['axes_edpurse']);
$sum=0;
for($i=0;$i<$max;$i++){
$imei=strtoupper($_SESSION['axes_edpurse'][$i]['imei']);
if($imei!=''){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}        
}

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

/*--------------------------------------------------------------*/
/* Function for Purchase Return
/*--------------------------------------------------------------*/
function getpureturn_info($invno,$pid,$key){
global $con;
$sql="SELECT ".$key." AS rdata FROM tbl_traproduct WHERE invno='$invno' AND pid='$pid' AND mods='PU' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}

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

function get_pupreqty($refinv,$unqid){
global $con;
$sql="SELECT COALESCE((SELECT SUM(p_out) FROM tbl_traproduct WHERE refinv='$refinv' AND unqid='$unqid' AND mods='PR' GROUP BY refinv,unqid),0) AS rdata";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}

function addtopureturn($invno,$pid,$unqid,$avqty){

if ( !isset($_SESSION['axes_pureturn']) ) {
$_SESSION['axes_pureturn'] = array();
}
    
if(is_array($_SESSION['axes_pureturn'])){
$max=count($_SESSION['axes_pureturn']);
$_SESSION['axes_pureturn'][$max]['pid']=$pid;
$_SESSION['axes_pureturn'][$max]['unqid']=$unqid;    
$_SESSION['axes_pureturn'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_pureturn'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_pureturn'][$max]['unid']=get_fild_data('tbl_item',$pid,'unid');
$_SESSION['axes_pureturn'][$max]['imei']=get_fild_data('tbl_item',$pid,'pmod'); 
$_SESSION['axes_pureturn'][$max]['col']=getpureturn_info($invno,$pid,'colid');
$_SESSION['axes_pureturn'][$max]['siz']=getpureturn_info($invno,$pid,'sizid');    
$_SESSION['axes_pureturn'][$max]['cost']=getpureturn_info($invno,$pid,'cost'); 
$_SESSION['axes_pureturn'][$max]['qty']=$avqty;
$_SESSION['axes_pureturn'][$max]['prqty']=get_pupreqty($invno,$unqid);
$_SESSION['axes_pureturn'][$max]['rqty']=0;    
$_SESSION['axes_pureturn'][$max]['price']=getpureturn_info($invno,$pid,'price');
$_SESSION['axes_pureturn'][$max]['disp']=getpureturn_info($invno,$pid,'disp');
$_SESSION['axes_pureturn'][$max]['disf']=getpureturn_info($invno,$pid,'disf');    
$_SESSION['axes_pureturn'][$max]['disamo']=0;
$_SESSION['axes_pureturn'][$max]['wday']=getpureturn_info($invno,$pid,'wday');
$_SESSION['axes_pureturn'][$max]['pnote']=getpureturn_info($invno,$pid,'pnote');    
$_SESSION['axes_pureturn'][$max]['subtot']=0;     
}else{
$_SESSION['axes_pureturn']=array();    
$_SESSION['axes_pureturn'][0]['pid']=$pid;
$_SESSION['axes_pureturn'][0]['unqid']=$unqid;    
$_SESSION['axes_pureturn'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_pureturn'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_pureturn'][0]['unid']=get_fild_data('tbl_item',$pid,'unid');
$_SESSION['axes_pureturn'][0]['imei']=get_fild_data('tbl_item',$pid,'pmod'); 
$_SESSION['axes_pureturn'][0]['col']=getpureturn_info($invno,$pid,'colid');
$_SESSION['axes_pureturn'][0]['siz']=getpureturn_info($invno,$pid,'sizid');    
$_SESSION['axes_pureturn'][0]['cost']=getpureturn_info($invno,$pid,'cost'); 
$_SESSION['axes_pureturn'][0]['qty']=$avqty;
$_SESSION['axes_pureturn'][0]['prqty']=get_pupreqty($invno,$unqid);
$_SESSION['axes_pureturn'][0]['rqty']=0;    
$_SESSION['axes_pureturn'][0]['price']=getpureturn_info($invno,$pid,'price');
$_SESSION['axes_pureturn'][0]['disp']=getpureturn_info($invno,$pid,'disp');
$_SESSION['axes_pureturn'][0]['disf']=getpureturn_info($invno,$pid,'disf');    
$_SESSION['axes_pureturn'][0]['disamo']=0;
$_SESSION['axes_pureturn'][0]['wday']=getpureturn_info($invno,$pid,'wday');
$_SESSION['axes_pureturn'][0]['pnote']=getpureturn_info($invno,$pid,'pnote');    
$_SESSION['axes_pureturn'][0]['subtot']=0;   
}
}

function get_pureserial($pid,$serial){
if ( !isset($_SESSION['axes_pureturnse']) ) {
$_SESSION['axes_pureturnse'] = array();
};
    
if(is_array($_SESSION['axes_pureturnse'])){
$max=count($_SESSION['axes_pureturnse']);
$_SESSION['axes_pureturnse'][$max]['pid']=$pid;
$_SESSION['axes_pureturnse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_pureturnse'][$max]['check']='';    
$_SESSION['axes_pureturnse'][$max]['serial']=$serial;     
}else{
$_SESSION['axes_pureturnse']=array();
$_SESSION['axes_pureturnse'][0]['pid']=$pid;
$_SESSION['axes_pureturnse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_pureturnse'][0]['check']='';    
$_SESSION['axes_pureturnse'][0]['serial']=$serial;    
}    
}

function read_pureturndata($invno){
global $con;

if(isset($_SESSION['axes_pureturnde'])){
unset($_SESSION['axes_pureturnde']);    
}    

if(isset($_SESSION['axes_pureturn'])){
unset($_SESSION['axes_pureturn']);    
}    

if(isset($_SESSION['axes_pureturnse'])){
unset($_SESSION['axes_pureturnse']);    
}    
    
$sql="SELECT * FROM tbl_purchase WHERE invno='$invno' LIMIT 1";    
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
$invno=$row['invno'];    
$ibrid=$row['brid'];

$due=($row['total']-($row['adamo']+get_invcash($invno,'P','Y')));    
$rcash=get_reamo($invno,'tbl_preturn');
if($due>$rcash){
$adj=$rcash;    
}else{
$adj=$due;    
}    
    
if (!isset($_SESSION['axes_pureturnde'])) {
$_SESSION['axes_pureturnde'] = array();
$_SESSION['axes_pureturnde'][0]['invno']=$invno;
$_SESSION['axes_pureturnde'][0]['type']=$row['type'];
$_SESSION['axes_pureturnde'][0]['cusid']=$row['supid'];
$_SESSION['axes_pureturnde'][0]['itemdis']=0;    
$_SESSION['axes_pureturnde'][0]['disp']=$row['disp'];
$_SESSION['axes_pureturnde'][0]['disamo']=0;    
$_SESSION['axes_pureturnde'][0]['vatp']=$row['vatp'];
$_SESSION['axes_pureturnde'][0]['vatamo']=0;
$_SESSION['axes_pureturnde'][0]['taxp']=$row['taxp'];
$_SESSION['axes_pureturnde'][0]['taxamo']=0;
$_SESSION['axes_pureturnde'][0]['less']=0;
$_SESSION['axes_pureturnde'][0]['invdue']=0;//($row['total']-(get_invcash($row['invno'],'P')+$row['adamo']+$adj));
$_SESSION['axes_pureturnde'][0]['adjdue']=0;    
$_SESSION['axes_pureturnde'][0]['gtotal']=0;    
}    

$sql="SELECT * FROM tbl_traproduct WHERE invno='$invno'";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
while ($rowp=mysqli_fetch_array($result)){
$avqty=get_reavqty($ibrid,$rowp['unqid'],'avqty');    
addtopureturn($invno,$rowp['pid'],$rowp['unqid'],$avqty);    
};

$sql="SELECT * FROM tbl_serial WHERE purinv='$invno' AND rci='Y' AND brid='$ibrid' AND status='0'";    
$serial = mysqli_query($con,$sql) or die(mysqli_error($con));    
if($serial->num_rows > 0) {
while ($rows=mysqli_fetch_array($serial)){
get_pureserial($rows['pid'],$rows['serial']);    
}    
    
}    
}

function get_pureturn_total(){
if(isset($_SESSION['axes_pureturn'])){
$max=count($_SESSION['axes_pureturn']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_pureturn'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}

function pureturn_totqty(){
$qty=0;
if(isset($_SESSION['axes_pureturn'])){
$max=count($_SESSION['axes_pureturn']);
for($i=0;$i<$max;$i++){
$qty+=$_SESSION['axes_pureturn'][$i]['rqty'];    
}
}else{
$qty=0;    
}
return $qty;    
}

function get_puretdiscount_total($edp=''){
if(isset($_SESSION['axes_pureturn'])){
$max=count($_SESSION['axes_pureturn']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_pureturn'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_pureturn_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 pureserial_exists($pid){
$flag=0;
if(isset($_SESSION['axes_pureturnse'])){
$max=count($_SESSION['axes_pureturnse']);
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_pureturnse'][$i]['pid'];
if($pid==$epid){
$flag=1;
break;    
}    
}
}else{
$flag=0;    
}
return $flag;     
}

function get_pureturncartqty($pid){
$rqty=0;
if(isset($_SESSION['axes_pureturn'])){
$max=count($_SESSION['axes_pureturn']);
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_pureturn'][$i]['pid'];
if($pid==$epid){
$rqty=$_SESSION['axes_pureturn'][$i]['rqty'];
break;    
}    
}
}else{
$rqty=0;    
}        
return $rqty;    
}

function pureserial_count($pid){
$cou=0;
if(isset($_SESSION['axes_pureturnse'])){
$max=count($_SESSION['axes_pureturnse']);
for($i=0;$i<$max;$i++){
$epid=$_SESSION['axes_pureturnse'][$i]['pid'];
$check=$_SESSION['axes_pureturnse'][$i]['check'];    
if($pid==$epid && $check=='1'){
$cou+=1;    
}    
}
}else{
$cou=0;    
}
return $cou;    
}

function check_pureturn_err(){
$flag=0;
if(isset($_SESSION['axes_pureturn'])){
$max=count($_SESSION['axes_pureturn']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['axes_pureturn'][$i]['pid'];
$rqty=$_SESSION['axes_pureturn'][$i]['rqty'];
if($rqty>0){
if(pureserial_exists($pid)){
if($rqty!=pureserial_count($pid)){
$flag=1;
break;    
}    
}    
}    
}
}else{
$flag=0;    
}
return $flag;    
}

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

function addestimate($brid,$unqid,$qty){
if($qty<=0) return;

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

if(get_prostinfo($unqid,'disf')>0){
$disamo=getfloatval((get_prostinfo($unqid,'disf')*$qty));    
}else{
if(get_prostinfo($unqid,'disp')>0){
$disamo=getfloatval(((get_prostinfo($unqid,'disp')/100)*get_prostinfo($unqid,'price'))*$qty);    
}else{
$disamo=0;    
}    
}    
    
if(is_array($_SESSION['axes_estimate'])){
if(estpro_exists($brid,$unqid)) return;
$max=count($_SESSION['axes_estimate']);
$_SESSION['axes_estimate'][$max]['unqid']=$unqid;
$_SESSION['axes_estimate'][$max]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_estimate'][$max]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_estimate'][$max]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_estimate'][$max]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_estimate'][$max]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_estimate'][$max]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_estimate'][$max]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_estimate'][$max]['check']='';
$_SESSION['axes_estimate'][$max]['icdis']=get_prostinfo($unqid,'icdis');
$_SESSION['axes_estimate'][$max]['icinvdis']=get_prostinfo($unqid,'icinvdis');    
$_SESSION['axes_estimate'][$max]['cost']=get_prostinfo($unqid,'cost'); 
$_SESSION['axes_estimate'][$max]['qty']=$qty;
$_SESSION['axes_estimate'][$max]['price']=get_prostinfo($unqid,'price');
$_SESSION['axes_estimate'][$max]['disp']=get_prostinfo($unqid,'disp');
$_SESSION['axes_estimate'][$max]['disf']=get_prostinfo($unqid,'disf');    
$_SESSION['axes_estimate'][$max]['disamo']=$disamo;
$_SESSION['axes_estimate'][$max]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_estimate'][$max]['pnote']='';    
$_SESSION['axes_estimate'][$max]['subtot']=((get_prostinfo($unqid,'price')*$qty)-$disamo);     
}else{
$_SESSION['axes_estimate']=array();    
$_SESSION['axes_estimate'][0]['unqid']=$unqid;
$_SESSION['axes_estimate'][0]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_estimate'][0]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_estimate'][0]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_estimate'][0]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_estimate'][0]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_estimate'][0]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_estimate'][0]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_estimate'][0]['check']='';
$_SESSION['axes_estimate'][0]['icdis']=get_prostinfo($unqid,'icdis');
$_SESSION['axes_estimate'][0]['icinvdis']=get_prostinfo($unqid,'icinvdis');    
$_SESSION['axes_estimate'][0]['cost']=get_prostinfo($unqid,'cost');    
$_SESSION['axes_estimate'][0]['cost']=get_prostinfo($unqid,'cost'); 
$_SESSION['axes_estimate'][0]['qty']=$qty;
$_SESSION['axes_estimate'][0]['price']=get_prostinfo($unqid,'price');
$_SESSION['axes_estimate'][0]['disp']=get_prostinfo($unqid,'disp');
$_SESSION['axes_estimate'][0]['disf']=get_prostinfo($unqid,'disf');    
$_SESSION['axes_estimate'][0]['disamo']=$disamo;
$_SESSION['axes_estimate'][0]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_estimate'][0]['pnote']='';    
$_SESSION['axes_estimate'][0]['subtot']=((get_prostinfo($unqid,'price')*$qty)-$disamo);    
}
}

function estpro_exists($brid,$unqid){
$max=count($_SESSION['axes_estimate']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_estimate'][$i]['unqid']){
$flag=1;
$eqty=(get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));
$cqty=get_estexproduct_count($unqid);
if($cqty<$eqty){    
$qy=$_SESSION['axes_estimate'][$i]['qty'];    
$_SESSION['axes_estimate'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_estimate'][$i]['disf'];
$price=$_SESSION['axes_estimate'][$i]['price'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($price*($qy+1))-$disamo);     
$_SESSION['axes_estimate'][$i]['disamo']=$disamo;
$_SESSION['axes_estimate'][$i]['subtot']=$subtot; 
}
break;    
}        
}       
return $flag;    
}

function get_estexproduct_count($unqid){
$cqty=0;
if(isset($_SESSION['axes_estimate'])){
$max=count($_SESSION['axes_estimate']);
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_estimate'][$i]['qty'];
$itid=$_SESSION['axes_estimate'][$i]['unqid'];
if($unqid==$itid){
$cqty+=$qty;
}
}
if($cqty>0){
return $cqty;	
}else{
return 0;	
}
}else{
return 0;	
}
}

function get_estproduct_count($unqid){
if(isset($_SESSION['axes_estimate'])){
$max=count($_SESSION['axes_estimate']);
$sum=0;
$qty=0;
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_estimate'][$i]['qty'];
$exunqid=$_SESSION['axes_estimate'][$i]['unqid'];
if($unqid==$exunqid){
$sum+=$qty;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}	
}

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

function get_cartestid($unqid){
$id='';    
if(isset($_SESSION['axes_estimate'])){
$max=count($_SESSION['axes_estimate']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_estimate'][$i]['unqid'];    
if($unqid==$eunqid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function get_estimate_total(){
if(isset($_SESSION['axes_estimate'])){
$max=count($_SESSION['axes_estimate']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_estimate'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_estdiscount_total($edp=''){
if(isset($_SESSION['axes_estimate'])){
$max=count($_SESSION['axes_estimate']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_estimate'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_estimate_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 check_estvalidqty($brid){
$flag=0;
if(isset($_SESSION['axes_estimate'])){    
$max=count($_SESSION['axes_estimate']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_estimate'][$i]['unqid'];    
$eqty=(get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));
$qty=$_SESSION['axes_estimate'][$i]['qty'];
if($qty>$eqty){
$flag=1;
break;    
}    
}    
}else{
$flag=0;    
}
return $flag;    
}

function check_estprice($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_estimate'])){
$max=count($_SESSION['axes_estimate']);
for($i=0;$i<$max;$i++){
$price=floatval($_SESSION['axes_estimate'][$i]['price']);
if($price<=0){
$res+=1;    
}    
}    
}else{
$max=0;    
$res=0;    
}    
if($key=='I'){
return $max;    
}elseif($key=='P'){
return $res;    
}    
}

/*--------------------------------------------------------------*/
/* Function for Sales
/*--------------------------------------------------------------*/
function get_salesinfo($unqid,$key){
global $con;
$sql="SELECT ".$key." AS rdata FROM tbl_traproduct WHERE unqid='$unqid' AND mods='PU' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}

function get_selestockinfo($brid,$unqid,$key){
global $con;
$sql="SELECT ".$key." AS rdata FROM tbl_brstock 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 addtosel($brid,$unqid,$qty,$check='M'){
if($qty<=0) return;

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

if(get_prostinfo($unqid,'disf')>0){
$disamo=getfloatval((get_prostinfo($unqid,'disf')*$qty));    
}else{
if(get_prostinfo($unqid,'disp')>0){
$disamo=getfloatval(((get_prostinfo($unqid,'disp')/100)*get_prostinfo($unqid,'price'))*$qty);    
}else{
$disamo=0;    
}    
}    
    
if(is_array($_SESSION['axes_sales'])){
if(selpro_exists($brid,$unqid,$check)) return;
$max=count($_SESSION['axes_sales']);
$_SESSION['axes_sales'][$max]['unqid']=$unqid;
$_SESSION['axes_sales'][$max]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_sales'][$max]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_sales'][$max]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_sales'][$max]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_sales'][$max]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_sales'][$max]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_sales'][$max]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_sales'][$max]['check']=$check;
$_SESSION['axes_sales'][$max]['icdis']=get_prostinfo($unqid,'icdis');
$_SESSION['axes_sales'][$max]['icinvdis']=get_prostinfo($unqid,'icinvdis');    
$_SESSION['axes_sales'][$max]['cost']=get_prostinfo($unqid,'cost'); 
$_SESSION['axes_sales'][$max]['qty']=$qty;
$_SESSION['axes_sales'][$max]['price']=get_prostinfo($unqid,'price');
$_SESSION['axes_sales'][$max]['disp']=get_prostinfo($unqid,'disp');
$_SESSION['axes_sales'][$max]['disf']=get_prostinfo($unqid,'disf');    
$_SESSION['axes_sales'][$max]['disamo']=$disamo;
$_SESSION['axes_sales'][$max]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_sales'][$max]['pnote']='';    
$_SESSION['axes_sales'][$max]['subtot']=((get_prostinfo($unqid,'price')*$qty)-$disamo);     
}else{
$_SESSION['axes_sales']=array();    
$_SESSION['axes_sales'][0]['unqid']=$unqid;
$_SESSION['axes_sales'][0]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_sales'][0]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_sales'][0]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_sales'][0]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_sales'][0]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_sales'][0]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_sales'][0]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_sales'][0]['check']=$check;
$_SESSION['axes_sales'][0]['icdis']=get_prostinfo($unqid,'icdis');
$_SESSION['axes_sales'][0]['icinvdis']=get_prostinfo($unqid,'icinvdis');    
$_SESSION['axes_sales'][0]['cost']=get_prostinfo($unqid,'cost');    
$_SESSION['axes_sales'][0]['cost']=get_prostinfo($unqid,'cost'); 
$_SESSION['axes_sales'][0]['qty']=$qty;
$_SESSION['axes_sales'][0]['price']=get_prostinfo($unqid,'price');
$_SESSION['axes_sales'][0]['disp']=get_prostinfo($unqid,'disp');
$_SESSION['axes_sales'][0]['disf']=get_prostinfo($unqid,'disf');    
$_SESSION['axes_sales'][0]['disamo']=$disamo;
$_SESSION['axes_sales'][0]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_sales'][0]['pnote']='';    
$_SESSION['axes_sales'][0]['subtot']=((get_prostinfo($unqid,'price')*$qty)-$disamo);    
}
}

function selpro_exists($brid,$unqid,$check){
$max=count($_SESSION['axes_sales']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_sales'][$i]['unqid']){
if($check=='M'){
$_SESSION['axes_sales'][$i]['check']='M';    
}
$flag=1;
$eqty=(get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));
$cqty=get_selexproduct_count($unqid);
if($cqty<$eqty){    
$qy=$_SESSION['axes_sales'][$i]['qty'];    
$_SESSION['axes_sales'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_sales'][$i]['disf'];
$price=$_SESSION['axes_sales'][$i]['price'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($price*($qy+1))-$disamo);     
$_SESSION['axes_sales'][$i]['disamo']=$disamo;
$_SESSION['axes_sales'][$i]['subtot']=$subtot; 
}
break;    
}        
}       
return $flag;    
}

function get_selexproduct_count($unqid){
$cqty=0;
if(isset($_SESSION['axes_sales'])){
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_sales'][$i]['qty'];
$itid=$_SESSION['axes_sales'][$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_sales'])){
$max=count($_SESSION['axes_sales']);
$sum=0;
$qty=0;
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_sales'][$i]['qty'];
$exunqid=$_SESSION['axes_sales'][$i]['unqid'];
if($unqid==$exunqid){
$sum+=$qty;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}	
}

function get_selmobile_count(){
if(isset($_SESSION['axes_sales'])){
$max=count($_SESSION['axes_sales']);
$sum=0;
$qty=0;
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_sales'][$i]['qty'];
$reimei=intval($_SESSION['axes_sales'][$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_sales'])){
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_sales'][$i]);
break;
}
}
$_SESSION['axes_sales']=array_values($_SESSION['axes_sales']);
}
}

function get_cartitemid($unqid){
$id='';    
if(isset($_SESSION['axes_sales'])){
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_sales'][$i]['unqid'];    
if($unqid==$eunqid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function get_sales_total(){
if(isset($_SESSION['axes_sales'])){
$max=count($_SESSION['axes_sales']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_sales'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_seldiscount_total($edp=''){
if(isset($_SESSION['axes_sales'])){
$max=count($_SESSION['axes_sales']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_sales'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_sales_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_selserial($pid,$unqid,$data,$man=''){
		
if ( !isset($_SESSION['axes_selse']) ) {
$_SESSION['axes_selse'] = array();
};	
		
$excount = get_selmobile_count();
		
if($excount <= 0){unset($_SESSION['axes_selse']);}
		
if(is_array($_SESSION['axes_selse'])){
if($man==''){
if(selimei_exists($data)) return;	
}
    
$max=count($_SESSION['axes_selse']);
$_SESSION['axes_selse'][$max]['pid']=$pid;
$_SESSION['axes_selse'][$max]['unqid']=$unqid;
$_SESSION['axes_selse'][$max]['cartid']=get_cartitemid($unqid);    
$_SESSION['axes_selse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_selse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_selse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_selse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_selse']=array();
$_SESSION['axes_selse'][0]['pid']=$pid;
$_SESSION['axes_selse'][0]['unqid']=$unqid;
$_SESSION['axes_selse'][0]['cartid']=get_cartitemid($unqid);     
$_SESSION['axes_selse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_selse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_selse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_selse'][0]['imei']=strtoupper($data);
}
}

function selimei_exists($pid,$data){
global $con;
$flag=0;    
$data=strtoupper($data);
$sql="SELECT * FROM tbl_serial 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_selse']) ){
$max=count($_SESSION['axes_selse']);
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_selse'][$i]['imei']){
$flag=1;
break;
}
}	
}    
}else{
$flag=1;
}
return $flag;
}

function selimei_pidewos($unqid){
$flag=0;
if (isset($_SESSION['axes_selse']) ){
$max=count($_SESSION['axes_selse']);
for($i=0;$i<$max;$i++){
if($unqid==$_SESSION['axes_selse'][$i]['unqid'] && $_SESSION['axes_selse'][$i]['imei']==''){
$flag=$i+1;
break;
}
}
}
return $flag;    
}

function get_selserial_count($pid){
if(isset($_SESSION['axes_selse'])){
$max=count($_SESSION['axes_selse']);
$sum=0;
for($i=0;$i<$max;$i++){
$proid=intval($_SESSION['axes_selse'][$i]['pid']);
if($pid==$proid){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}			
}
	
function remove_selimei($pid){
$pid=intval($pid);
if(isset($_SESSION['axes_selse'])){
$max=count($_SESSION['axes_selse']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_selse'][$i]);
break;
}
}
$_SESSION['axes_selse']=array_values($_SESSION['axes_selse']);
}
}

function remove_slimei($unqid){ 
$max=count($_SESSION['axes_selse']);
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_selse'][$i]['unqid']){   
unset($_SESSION['axes_selse'][$i]);    
}    
}
$_SESSION['axes_selse']=array_values($_SESSION['axes_selse']);    
}

function check_seleximei($data){
global $con;
$flag=0;
$sql="SELECT COUNT(*) AS exdata FROM tbl_serial 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_selimei_count(){
if(isset($_SESSION['axes_selse'])){
$max=count($_SESSION['axes_selse']);
$sum=0;
for($i=0;$i<$max;$i++){
$imei=strtoupper($_SESSION['axes_selse'][$i]['imei']);
if($imei!=''){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}        
}

function check_validqty($brid){
$flag=0;
if(isset($_SESSION['axes_sales'])){    
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_sales'][$i]['unqid'];    
$eqty=(get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));
$qty=$_SESSION['axes_sales'][$i]['qty'];
if($qty>$eqty){
$flag=1;
break;    
}    
}    
}else{
$flag=0;    
}
return $flag;    
}

function check_price($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_sales'])){
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
$price=floatval($_SESSION['axes_sales'][$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_selse'])){
$max=count($_SESSION['axes_selse']);
for($i=0;$i<$max;$i++){
$serial=$_SESSION['axes_selse'][$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 for Edit Sales
/*--------------------------------------------------------------*/

function get_editsales($invno){
global $con;
$sql="SELECT * FROM tbl_traproduct WHERE invno='$invno' ORDER BY id ASC";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
while ($row=mysqli_fetch_array($result)){
addtoEdsel('E',$row['brid'],$row['unqid'],$row['s_qty']);	
}
if(get_edselmobile_count()>0){
$sql="SELECT * FROM tbl_serialsale WHERE invno='$invno'";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
while ($row=mysqli_fetch_array($result)){
add_edselserial('E',$row['pid'],$row['unqid'],$row['serial']);    
}	
}    
if (!isset($_SESSION['axes_edsalesde'])) {

$sql="SELECT * FROM tbl_sales WHERE invno='$invno' LIMIT 1";    
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);    

$_SESSION['axes_edsalesde'] = array();
$_SESSION['axes_edsalesde'][0]['disp']=$row['disp'];
$_SESSION['axes_edsalesde'][0]['disamo']=$row['totdis'];    
$_SESSION['axes_edsalesde'][0]['vatp']=$row['vatp'];
$_SESSION['axes_edsalesde'][0]['vatamo']=$row['vatamo'];
$_SESSION['axes_edsalesde'][0]['aitp']=$row['aitp'];
$_SESSION['axes_edsalesde'][0]['aitamo']=$row['aitamo'];
$_SESSION['axes_edsalesde'][0]['name']=$row['otname'];
$_SESSION['axes_edsalesde'][0]['others']=$row['otamo'];;
$_SESSION['axes_edsalesde'][0]['less']=$row['less'];;
$_SESSION['axes_edsalesde'][0]['freight']=$row['freight'];
$_SESSION['axes_edsalesde'][0]['gtotal']=$row['total'];    
}
} 

function get_soldinfo($mod,$unqid,$key,$cdata,$invno=''){
global $con;
$result='';    
if($mod!='E'){
$result=$cdata;    
}else{    
$sql="SELECT ".$key." AS rdata FROM tbl_traproduct WHERE invno='$invno' AND unqid='$unqid' AND mods='SE' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
$result=$row['rdata'];
}
return $result;     
}

function addtoEdsel($mod,$brid,$unqid,$qty){
if($qty<=0) return;

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

if(get_prostinfo($unqid,'disf')>0){
$disamo=getfloatval((get_prostinfo($unqid,'disf')*$qty));    
}else{
if(get_prostinfo($unqid,'disp')>0){
$disamo=getfloatval(((get_prostinfo($unqid,'disp')/100)*get_prostinfo($unqid,'price'))*$qty);    
}else{
$disamo=0;    
}    
}    
    
if(is_array($_SESSION['axes_edsales'])){
if($mod!='E'){      
if(selpro_edexists($brid,$unqid)) return;
}
$max=count($_SESSION['axes_edsales']);
$_SESSION['axes_edsales'][$max]['unqid']=$unqid;
$_SESSION['axes_edsales'][$max]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_edsales'][$max]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_edsales'][$max]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_edsales'][$max]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_edsales'][$max]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_edsales'][$max]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_edsales'][$max]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_edsales'][$max]['check']='';
$_SESSION['axes_edsales'][$max]['icdis']=get_prostinfo($unqid,'icdis');
$_SESSION['axes_edsales'][$max]['icinvdis']=get_prostinfo($unqid,'icinvdis');    
$_SESSION['axes_edsales'][$max]['cost']=get_prostinfo($unqid,'cost'); 
$_SESSION['axes_edsales'][$max]['qty']=$qty;
$_SESSION['axes_edsales'][$max]['price']=get_prostinfo($unqid,'price');
$_SESSION['axes_edsales'][$max]['disp']=get_prostinfo($unqid,'disp');
$_SESSION['axes_edsales'][$max]['disf']=get_prostinfo($unqid,'disf');    
$_SESSION['axes_edsales'][$max]['disamo']=$disamo;
$_SESSION['axes_edsales'][$max]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_edsales'][$max]['pnote']='';    
$_SESSION['axes_edsales'][$max]['subtot']=((get_prostinfo($unqid,'price')*$qty)-$disamo);     
}else{
$_SESSION['axes_edsales']=array();    
$_SESSION['axes_edsales'][0]['unqid']=$unqid;
$_SESSION['axes_edsales'][0]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_edsales'][0]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_edsales'][0]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_edsales'][0]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_edsales'][0]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_edsales'][0]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_edsales'][0]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_edsales'][0]['check']='';
$_SESSION['axes_edsales'][0]['icdis']=get_prostinfo($unqid,'icdis');
$_SESSION['axes_edsales'][0]['icinvdis']=get_prostinfo($unqid,'icinvdis');    
$_SESSION['axes_edsales'][0]['cost']=get_prostinfo($unqid,'cost');    
$_SESSION['axes_edsales'][0]['cost']=get_prostinfo($unqid,'cost'); 
$_SESSION['axes_edsales'][0]['qty']=$qty;
$_SESSION['axes_edsales'][0]['price']=get_prostinfo($unqid,'price');
$_SESSION['axes_edsales'][0]['disp']=get_prostinfo($unqid,'disp');
$_SESSION['axes_edsales'][0]['disf']=get_prostinfo($unqid,'disf');    
$_SESSION['axes_edsales'][0]['disamo']=$disamo;
$_SESSION['axes_edsales'][0]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_edsales'][0]['pnote']='';    
$_SESSION['axes_edsales'][0]['subtot']=((get_prostinfo($unqid,'price')*$qty)-$disamo);    
}
}

function selpro_edexists($brid,$unqid){
$max=count($_SESSION['axes_edsales']);
$flag=0;    
$invno=$_SESSION['editselinv'];    
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_edsales'][$i]['unqid']){
$flag=1;
$eqty=((get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'))+get_soproinfo($invno,$unqid,'p_out'));
$cqty=get_edselexproduct_count($unqid);
if($cqty<$eqty){    
$qy=$_SESSION['axes_edsales'][$i]['qty'];    
$_SESSION['axes_edsales'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_edsales'][$i]['sdisf'];
$price=$_SESSION['axes_edsales'][$i]['price'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($price*($qy+1))-$disamo);     
$_SESSION['axes_edsales'][$i]['disamo']=$disamo;
$_SESSION['axes_edsales'][$i]['subtot']=$subtot; 
}
break;    
}        
}       
return $flag;    
}

function get_edselexproduct_count($unqid){
$cqty=0;
if(isset($_SESSION['axes_edsales'])){
$max=count($_SESSION['axes_edsales']);
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_edsales'][$i]['qty'];
$itid=$_SESSION['axes_edsales'][$i]['unqid'];
if($unqid==$itid){
$cqty+=$qty;
}
}
if($cqty>0){
return $cqty;	
}else{
return 0;	
}
}else{
return 0;	
}
}

function get_edselproduct_count($unqid){
if(isset($_SESSION['axes_edsales'])){
$max=count($_SESSION['axes_edsales']);
$sum=0;
$qty=0;
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_edsales'][$i]['qty'];
$exunqid=$_SESSION['axes_edsales'][$i]['unqid'];
if($unqid==$exunqid){
$sum+=$qty;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}	
}

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

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

function get_edcartitemid($unqid){
$id='';    
if(isset($_SESSION['axes_edsales'])){
$max=count($_SESSION['axes_edsales']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_edsales'][$i]['unqid'];    
if($unqid==$eunqid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function get_edsales_total(){
if(isset($_SESSION['axes_edsales'])){
$max=count($_SESSION['axes_edsales']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_edsales'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_edseldiscount_total($edp=''){
if(isset($_SESSION['axes_edsales'])){
$max=count($_SESSION['axes_edsales']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_edsales'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_edsales_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_edselserial($mod,$pid,$unqid,$data,$man=''){
		
if ( !isset($_SESSION['axes_edselse']) ) {
$_SESSION['axes_edselse'] = array();
};	
		
$excount = get_edselmobile_count();
		
if($excount <= 0){unset($_SESSION['axes_edselse']);}
		
if(is_array($_SESSION['axes_edselse'])){
if($mod!='E'){    
if($man==''){
if(selimei_edexists($data)) return;	
}
}
    
$max=count($_SESSION['axes_edselse']);
$_SESSION['axes_edselse'][$max]['pid']=$pid;
$_SESSION['axes_edselse'][$max]['unqid']=$unqid;
$_SESSION['axes_edselse'][$max]['cartid']=get_edcartitemid($unqid);    
$_SESSION['axes_edselse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_edselse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_edselse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_edselse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_edselse']=array();
$_SESSION['axes_edselse'][0]['pid']=$pid;
$_SESSION['axes_edselse'][0]['unqid']=$unqid;
$_SESSION['axes_edselse'][0]['cartid']=get_edcartitemid($unqid);     
$_SESSION['axes_edselse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_edselse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_edselse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_edselse'][0]['imei']=strtoupper($data);
}
}

function selimei_edexists($pid,$data){
global $con;
$flag=0;    
$data=strtoupper($data);
$selinv=$_SESSION['editselinv'];    
$sql="SELECT * FROM (SELECT serial FROM tbl_serial WHERE pid='$pid' AND serial='$data' AND status='0' UNION ALL SELECT serial FROM tbl_serialsale WHERE invno='$selinv' AND pid='$pid' AND serial='$data') ser LIMIT 1";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));    
if($query->num_rows > 0) {
if (isset($_SESSION['axes_edselse']) ){
$max=count($_SESSION['axes_edselse']);
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_edselse'][$i]['imei']){
$flag=1;
break;
}
}	
}    
}else{
$flag=1;
}
return $flag;
}
	
function get_edselserial_count($pid){
if(isset($_SESSION['axes_edselse'])){
$max=count($_SESSION['axes_edselse']);
$sum=0;
for($i=0;$i<$max;$i++){
$proid=intval($_SESSION['axes_edselse'][$i]['pid']);
if($pid==$proid){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}			
}
	
function remove_edselimei($pid){
$pid=intval($pid);
if(isset($_SESSION['axes_edselse'])){
$max=count($_SESSION['axes_edselse']);
for($i=0;$i<$max;$i++){
if($pid==$i){
unset($_SESSION['axes_edselse'][$i]);
break;
}
}
$_SESSION['axes_edselse']=array_values($_SESSION['axes_edselse']);
}
}

function remove_edslimei($unqid){ 
$max=count($_SESSION['axes_edselse']);
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_edselse'][$i]['unqid']){   
unset($_SESSION['axes_edselse'][$i]);    
}    
}
$_SESSION['axes_edselse']=array_values($_SESSION['axes_edselse']);    
}

function check_edseleximei($data){
global $con;
$flag=0;
$sql="SELECT COUNT(*) AS exdata FROM tbl_serial 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_edselimei_count(){
if(isset($_SESSION['axes_edselse'])){
$max=count($_SESSION['axes_edselse']);
$sum=0;
for($i=0;$i<$max;$i++){
$imei=strtoupper($_SESSION['axes_edselse'][$i]['imei']);
if($imei!=''){
$sum+=1;
}
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}        
}

function get_soproinfo($invno,$unqid,$key){
global $con;
$sql="SELECT COALESCE ((SELECT ".$key." AS rdata FROM tbl_traproduct WHERE invno='$invno' AND unqid='$unqid' AND mods='SE' LIMIT 1),0) AS rdata";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];     
}

function check_edvalidqty($brid){
$flag=0;
if(isset($_SESSION['axes_edsales'])){
$invno=$_SESSION['editselinv'];    
$max=count($_SESSION['axes_edsales']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_edsales'][$i]['unqid'];    
$eqty=(get_selestockinfo($brid,$unqid,'avqty')+get_soproinfo($invno,$unqid,'p_out'));
$qty=$_SESSION['axes_edsales'][$i]['qty'];
if($qty>$eqty){
$flag=1;
break;    
}    
}    
}else{
$flag=0;    
}
return $flag;    
}

       
function check_edprice($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_edsales'])){
$max=count($_SESSION['axes_edsales']);
for($i=0;$i<$max;$i++){
$price=floatval($_SESSION['axes_edsales'][$i]['price']);
if($price<=0){
$res+=1;    
}    
}    
}else{
$max=0;    
$res=0;    
}    
if($key=='I'){
return $max;    
}elseif($key=='P'){
return $res;    
}    
}

function check_edserial($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_edselse'])){
$max=count($_SESSION['axes_edselse']);
for($i=0;$i<$max;$i++){
$serial=$_SESSION['axes_edselse'][$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 for Sales Return
/*--------------------------------------------------------------*/
function getreturn_info($invno,$unqid,$key){
global $con;
$sql="SELECT ".$key." AS rdata FROM tbl_traproduct WHERE invno='$invno' AND unqid='$unqid' AND mods='SE' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}

function getreturn_qty($invno,$unqid){
global $con;
$sql="SELECT SUM(p_out) AS rdata FROM tbl_traproduct WHERE (invno='$invno' OR refinv='$invno') AND unqid='$unqid' AND mods IN ('SE','DL') GROUP BY unqid LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}

function get_preqty($refinv,$unqid){
global $con;
$sql="SELECT COALESCE((SELECT SUM(p_in) FROM tbl_traproduct WHERE refinv='$refinv' AND unqid='$unqid' AND mods='SR' GROUP BY refinv,unqid),0) AS rdata";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}

function addtoreturn($invno,$unqid){

if ( !isset($_SESSION['axes_sereturn']) ) {
$_SESSION['axes_sereturn'] = array();
}
    
if(is_array($_SESSION['axes_sereturn'])){
$max=count($_SESSION['axes_sereturn']);
$_SESSION['axes_sereturn'][$max]['unqid']=$unqid;
$_SESSION['axes_sereturn'][$max]['pid']=getreturn_info($invno,$unqid,'pid');    
$_SESSION['axes_sereturn'][$max]['code']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'code');
$_SESSION['axes_sereturn'][$max]['name']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'name');
$_SESSION['axes_sereturn'][$max]['unid']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'unid');
$_SESSION['axes_sereturn'][$max]['imei']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'pmod');   
$_SESSION['axes_sereturn'][$max]['col']=getreturn_info($invno,$unqid,'colid');
$_SESSION['axes_sereturn'][$max]['siz']=getreturn_info($invno,$unqid,'sizid');
$_SESSION['axes_sereturn'][$max]['icdis']=getreturn_info($invno,$unqid,'icdis');
$_SESSION['axes_sereturn'][$max]['icinvdis']=getreturn_info($invno,$unqid,'icinvdis');    
$_SESSION['axes_sereturn'][$max]['cost']=getreturn_info($invno,$unqid,'cost'); 
$_SESSION['axes_sereturn'][$max]['qty']=getreturn_qty($invno,$unqid);//getreturn_info($invno,$unqid,'p_out');
$_SESSION['axes_sereturn'][$max]['prqty']=get_preqty($invno,$unqid);
$_SESSION['axes_sereturn'][$max]['rqty']=0;    
$_SESSION['axes_sereturn'][$max]['price']=getreturn_info($invno,$unqid,'price');
$_SESSION['axes_sereturn'][$max]['disp']=getreturn_info($invno,$unqid,'disp');
$_SESSION['axes_sereturn'][$max]['disf']=getreturn_info($invno,$unqid,'disf');    
$_SESSION['axes_sereturn'][$max]['disamo']=0;
$_SESSION['axes_sereturn'][$max]['wday']=getreturn_info($invno,$unqid,'wday');
$_SESSION['axes_sereturn'][$max]['pnote']=getreturn_info($invno,$unqid,'pnote');    
$_SESSION['axes_sereturn'][$max]['subtot']=0;     
}else{
$_SESSION['axes_sereturn']=array();    
$_SESSION['axes_sereturn'][0]['unqid']=$unqid;
$_SESSION['axes_sereturn'][0]['pid']=getreturn_info($invno,$unqid,'pid');    
$_SESSION['axes_sereturn'][0]['code']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'code');
$_SESSION['axes_sereturn'][0]['name']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'name');
$_SESSION['axes_sereturn'][0]['unid']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'unid');
$_SESSION['axes_sereturn'][0]['imei']=get_fild_data('tbl_item',getreturn_info($invno,$unqid,'pid'),'pmod');   
$_SESSION['axes_sereturn'][0]['col']=getreturn_info($invno,$unqid,'colid');
$_SESSION['axes_sereturn'][0]['siz']=getreturn_info($invno,$unqid,'sizid');
$_SESSION['axes_sereturn'][0]['icdis']=getreturn_info($invno,$unqid,'icdis');
$_SESSION['axes_sereturn'][0]['icinvdis']=getreturn_info($invno,$unqid,'icinvdis');    
$_SESSION['axes_sereturn'][0]['cost']=getreturn_info($invno,$unqid,'cost'); 
$_SESSION['axes_sereturn'][0]['qty']=getreturn_qty($invno,$unqid);//getreturn_info($invno,$unqid,'p_out');
$_SESSION['axes_sereturn'][0]['prqty']=get_preqty($invno,$unqid);
$_SESSION['axes_sereturn'][0]['rqty']=0;    
$_SESSION['axes_sereturn'][0]['price']=getreturn_info($invno,$unqid,'price');
$_SESSION['axes_sereturn'][0]['disp']=getreturn_info($invno,$unqid,'disp');
$_SESSION['axes_sereturn'][0]['disf']=getreturn_info($invno,$unqid,'disf');    
$_SESSION['axes_sereturn'][0]['disamo']=0;
$_SESSION['axes_sereturn'][0]['wday']=getreturn_info($invno,$unqid,'wday');
$_SESSION['axes_sereturn'][0]['pnote']=getreturn_info($invno,$unqid,'pnote');    
$_SESSION['axes_sereturn'][0]['subtot']=0;    
}
}

function get_reserial($pid,$unqid,$serial){
if ( !isset($_SESSION['axes_returnse']) ) {
$_SESSION['axes_returnse'] = array();
};
    
if(is_array($_SESSION['axes_returnse'])){
$max=count($_SESSION['axes_returnse']);
$_SESSION['axes_returnse'][$max]['pid']=$pid;
$_SESSION['axes_returnse'][$max]['unqid']=$unqid;
$_SESSION['axes_returnse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_returnse'][$max]['check']='';    
$_SESSION['axes_returnse'][$max]['serial']=$serial;     
}else{
$_SESSION['axes_returnse']=array();
$_SESSION['axes_returnse'][0]['pid']=$pid;
$_SESSION['axes_returnse'][0]['unqid']=$unqid;
$_SESSION['axes_returnse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_returnse'][0]['check']='';    
$_SESSION['axes_returnse'][0]['serial']=$serial;    
}    
}

function read_returndata($invno){
global $con;

if(isset($_SESSION['axes_returnde'])){
unset($_SESSION['axes_returnde']);    
}    

if(isset($_SESSION['axes_sereturn'])){
unset($_SESSION['axes_sereturn']);    
}    

if(isset($_SESSION['axes_returnse'])){
unset($_SESSION['axes_returnse']);    
}    
    
$sql="SELECT * FROM tbl_sales WHERE invno='$invno' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);    

$due=($row['total']-($row['adamo']+get_invcash($invno,'S','Y')));    
$rcash=get_reamo($invno,'tbl_sreturn');
if($due>$rcash){
$adj=$rcash;    
}else{
$adj=$due;    
}    
    
if (!isset($_SESSION['axes_returnde'])) {
$_SESSION['axes_returnde'] = array();
$_SESSION['axes_returnde'][0]['invno']=$invno;
$_SESSION['axes_returnde'][0]['type']=$row['type'];
$_SESSION['axes_returnde'][0]['cusid']=$row['cusid'];
$_SESSION['axes_returnde'][0]['itemdis']=0;    
$_SESSION['axes_returnde'][0]['disp']=$row['disp'];
$_SESSION['axes_returnde'][0]['disamo']=0;    
$_SESSION['axes_returnde'][0]['vatp']=$row['vatp'];
$_SESSION['axes_returnde'][0]['vatamo']=0;
$_SESSION['axes_returnde'][0]['aitp']=$row['aitp'];
$_SESSION['axes_returnde'][0]['aitamo']=0;
$_SESSION['axes_returnde'][0]['less']=0;
$_SESSION['axes_returnde'][0]['invdue']=0;//($row['total']-(get_invcash($row['invno'],'S')+$row['adamo']+$adj));
$_SESSION['axes_returnde'][0]['adjdue']=0;    
$_SESSION['axes_returnde'][0]['gtotal']=0;    
}    

$sql="SELECT * FROM tbl_traproduct WHERE invno='$invno'";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
while ($rowp=mysqli_fetch_array($result)){
addtoreturn($invno,$rowp['unqid']);    
};

$sql="SELECT * FROM tbl_serialsale WHERE invno='$invno' AND mods='S' AND dli='Y'";    
$serial = mysqli_query($con,$sql) or die(mysqli_error($con));    
if($serial->num_rows > 0) {

while ($rows=mysqli_fetch_array($serial)){
get_reserial($rows['pid'],$rows['unqid'],$rows['serial']);    
}    
    
}    
}

function get_sereturn_total(){
if(isset($_SESSION['axes_sereturn'])){
$max=count($_SESSION['axes_sereturn']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_sereturn'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}

function sereturn_totqty(){
$qty=0;
if(isset($_SESSION['axes_sereturn'])){
$max=count($_SESSION['axes_sereturn']);
for($i=0;$i<$max;$i++){
$qty+=$_SESSION['axes_sereturn'][$i]['rqty'];    
}
}else{
$qty=0;    
}
return $qty;    
}

function get_retdiscount_total($edp=''){
if(isset($_SESSION['axes_sereturn'])){
$max=count($_SESSION['axes_sereturn']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_sereturn'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_sereturn_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 reserial_exists($unqid){
$flag=0;
if(isset($_SESSION['axes_returnse'])){
$max=count($_SESSION['axes_returnse']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_returnse'][$i]['unqid'];
if($unqid==$eunqid){
$flag=1;
break;    
}    
}
}else{
$flag=0;    
}
return $flag;     
}

function get_returncartqty($unqid){
$rqty=0;
if(isset($_SESSION['axes_sereturn'])){
$max=count($_SESSION['axes_sereturn']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_sereturn'][$i]['unqid'];
if($unqid==$eunqid){
$rqty=$_SESSION['axes_sereturn'][$i]['rqty'];
break;    
}    
}
}else{
$rqty=0;    
}        
return $rqty;    
}

function reserial_count($unqid){
$cou=0;
if(isset($_SESSION['axes_returnse'])){
$max=count($_SESSION['axes_returnse']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_returnse'][$i]['unqid'];
$check=$_SESSION['axes_returnse'][$i]['check'];    
if($unqid==$eunqid && $check=='1'){
$cou+=1;    
}    
}
}else{
$cou=0;    
}
return $cou;    
}

function check_return_err(){
$flag=0;
if(isset($_SESSION['axes_sereturn'])){
$max=count($_SESSION['axes_sereturn']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_sereturn'][$i]['unqid'];
$rqty=$_SESSION['axes_sereturn'][$i]['rqty'];
if($rqty>0){
if(reserial_exists($unqid)){
if($rqty!=reserial_count($unqid)){
$flag=1;
break;    
}    
}    
}    
}
}else{
$flag=0;    
}
return $flag;    
}

/*--------------------------------------------------------------*/
/* Function for Product tranfer from brach
/*--------------------------------------------------------------*/

function addbrtransfer($brid,$unqid,$qty){
if($qty<=0) return;

if ( !isset($_SESSION['axes_transbr']) ) {
$_SESSION['axes_transbr'] = array();
}
    
if(is_array($_SESSION['axes_transbr'])){
if(trnbr_exists($brid,$unqid)) return;
$max=count($_SESSION['axes_transbr']);
$_SESSION['axes_transbr'][$max]['unqid']=$unqid;
$_SESSION['axes_transbr'][$max]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_transbr'][$max]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_transbr'][$max]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_transbr'][$max]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_transbr'][$max]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_transbr'][$max]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_transbr'][$max]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_transbr'][$max]['check']='';    
$_SESSION['axes_transbr'][$max]['cost']=0; 
$_SESSION['axes_transbr'][$max]['qty']=$qty;
$_SESSION['axes_transbr'][$max]['price']=0;
$_SESSION['axes_transbr'][$max]['disp']=0;
$_SESSION['axes_transbr'][$max]['disf']=0;    
$_SESSION['axes_transbr'][$max]['disamo']=0;
$_SESSION['axes_transbr'][$max]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_transbr'][$max]['pnote']='';    
$_SESSION['axes_transbr'][$max]['subtot']=0;     
}else{
$_SESSION['axes_transbr']=array();    
$_SESSION['axes_transbr'][0]['unqid']=$unqid;
$_SESSION['axes_transbr'][0]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_transbr'][0]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_transbr'][0]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_transbr'][0]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_transbr'][0]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_transbr'][0]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_transbr'][0]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_transbr'][0]['check']='';    
$_SESSION['axes_transbr'][0]['cost']=0; 
$_SESSION['axes_transbr'][0]['qty']=$qty;
$_SESSION['axes_transbr'][0]['price']=0;
$_SESSION['axes_transbr'][0]['disp']=0;
$_SESSION['axes_transbr'][0]['disf']=0;    
$_SESSION['axes_transbr'][0]['disamo']=0;
$_SESSION['axes_transbr'][0]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_transbr'][0]['pnote']='';    
$_SESSION['axes_transbr'][0]['subtot']=0;    
}
}

function get_trabrpro_count($unqid){
$cqty=0;
if(isset($_SESSION['axes_transbr'])){
$max=count($_SESSION['axes_transbr']);
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_transbr'][$i]['qty'];
$itid=$_SESSION['axes_transbr'][$i]['unqid'];
if($unqid==$itid){
$cqty+=$qty;
}
}
if($cqty>0){
return $cqty;	
}else{
return 0;	
}
}else{
return 0;	
}
}

function trnbr_exists($brid,$unqid){
$max=count($_SESSION['axes_transbr']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_transbr'][$i]['unqid']){
$flag=1;
$eqty=(get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));
$cqty=get_trabrpro_count($unqid);
if($cqty<$eqty){    
$qy=$_SESSION['axes_transbr'][$i]['qty'];    
$_SESSION['axes_transbr'][$i]['qty']=$qy+1;
}
break;    
}        
}       
return $flag;    
}

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

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

function get_transbritmid($unqid){
$id='';    
if(isset($_SESSION['axes_transbr'])){
$max=count($_SESSION['axes_transbr']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_transbr'][$i]['unqid'];    
if($unqid==$eunqid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function add_trbserial($pid,$unqid,$data,$man=''){
		
if ( !isset($_SESSION['axes_trbse']) ) {
$_SESSION['axes_trbse'] = array();
};	
		
$excount = get_trnbrserial_count();
		
if($excount <= 0){unset($_SESSION['axes_trbse']);}
		
if(is_array($_SESSION['axes_trbse'])){
if($man==''){
if(transbrserial_exists($data)) return;	
}
    
$max=count($_SESSION['axes_trbse']);
$_SESSION['axes_trbse'][$max]['pid']=$pid;
$_SESSION['axes_trbse'][$max]['unqid']=$unqid;
$_SESSION['axes_trbse'][$max]['cartid']=get_transbritmid($unqid);    
$_SESSION['axes_trbse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_trbse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_trbse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_trbse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_trbse']=array();
$_SESSION['axes_trbse'][0]['pid']=$pid;
$_SESSION['axes_trbse'][0]['unqid']=$unqid;
$_SESSION['axes_trbse'][0]['cartid']=get_transbritmid($unqid);     
$_SESSION['axes_trbse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_trbse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_trbse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_trbse'][0]['imei']=strtoupper($data);
}
}

function transbrserial_exists($pid,$data){
global $con;
$flag=0;    
$data=strtoupper($data);
$sql="SELECT * FROM tbl_serial 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_trbse']) ){
$max=count($_SESSION['axes_trbse']);
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_trbse'][$i]['imei']){
$flag=1;
break;
}
}	
}    
}else{
$flag=1;
}
return $flag;
}

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

function remove_trbrslimei($unqid){ 
$max=count($_SESSION['axes_trbse']);
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_trbse'][$i]['unqid']){   
unset($_SESSION['axes_trbse'][$i]);    
}    
}
$_SESSION['axes_trbse']=array_values($_SESSION['axes_trbse']);    
}

function check_trbserial($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_trbse'])){
$max=count($_SESSION['axes_trbse']);
for($i=0;$i<$max;$i++){
$serial=$_SESSION['axes_trbse'][$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 check_trbvalidqty($brid){
$flag=0;
if(isset($_SESSION['axes_transbr'])){    
$max=count($_SESSION['axes_transbr']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_transbr'][$i]['unqid'];    
$eqty=get_selestockinfo($brid,$unqid,'avqty');
$qty=$_SESSION['axes_transbr'][$i]['qty'];
if($qty>$eqty){
$flag=1;
break;    
}    
}    
}else{
$flag=0;    
}
return $flag;    
}

/*--------------------------------------------------------------*/
/* Function for Product tranfer from warehouse
/*--------------------------------------------------------------*/
function get_warestockinfo($whid,$unqid,$key){
global $con;
$sql="SELECT ".$key." AS rdata FROM tbl_whstock WHERE waid='$whid' AND unqid='$unqid' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}


function addwhtransfer($whid,$unqid,$qty){
if($qty<=0) return;

if ( !isset($_SESSION['axes_transwh']) ) {
$_SESSION['axes_transwh'] = array();
}
    
if(is_array($_SESSION['axes_transwh'])){
if(trnwh_exists($whid,$unqid)) return;
$max=count($_SESSION['axes_transwh']);
$_SESSION['axes_transwh'][$max]['whid']=$whid;
$_SESSION['axes_transwh'][$max]['unqid']=$unqid;
$_SESSION['axes_transwh'][$max]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_transwh'][$max]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_transwh'][$max]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_transwh'][$max]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_transwh'][$max]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_transwh'][$max]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_transwh'][$max]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_transwh'][$max]['check']='';    
$_SESSION['axes_transwh'][$max]['cost']=0; 
$_SESSION['axes_transwh'][$max]['qty']=$qty;
$_SESSION['axes_transwh'][$max]['price']=0;
$_SESSION['axes_transwh'][$max]['disp']=0;
$_SESSION['axes_transwh'][$max]['disf']=0;    
$_SESSION['axes_transwh'][$max]['disamo']=0;
$_SESSION['axes_transwh'][$max]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_transwh'][$max]['pnote']='';    
$_SESSION['axes_transwh'][$max]['subtot']=0;     
}else{
$_SESSION['axes_transwh']=array();
$_SESSION['axes_transwh'][0]['whid']=$whid;    
$_SESSION['axes_transwh'][0]['unqid']=$unqid;
$_SESSION['axes_transwh'][0]['pid']=get_salesinfo($unqid,'pid');    
$_SESSION['axes_transwh'][0]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_transwh'][0]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_transwh'][0]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_transwh'][0]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_transwh'][0]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_transwh'][0]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_transwh'][0]['check']='';    
$_SESSION['axes_transwh'][0]['cost']=0; 
$_SESSION['axes_transwh'][0]['qty']=$qty;
$_SESSION['axes_transwh'][0]['price']=0;
$_SESSION['axes_transwh'][0]['disp']=0;
$_SESSION['axes_transwh'][0]['disf']=0;    
$_SESSION['axes_transwh'][0]['disamo']=0;
$_SESSION['axes_transwh'][0]['wday']=get_prostinfo($unqid,'wday');
$_SESSION['axes_transwh'][0]['pnote']='';    
$_SESSION['axes_transwh'][0]['subtot']=0;    
}
}

function get_trawhpro_count($unqid){
$cqty=0;
if(isset($_SESSION['axes_transwh'])){
$max=count($_SESSION['axes_transwh']);
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_transwh'][$i]['qty'];
$itid=$_SESSION['axes_transwh'][$i]['unqid'];
if($unqid==$itid){
$cqty+=$qty;
}
}
if($cqty>0){
return $cqty;	
}else{
return 0;	
}
}else{
return 0;	
}
}

function trnwh_exists($whid,$unqid){
$max=count($_SESSION['axes_transwh']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_transwh'][$i]['unqid']){
$flag=1;
$eqty=get_warestockinfo($whid,$unqid,'avqty');
$cqty=get_trawhpro_count($unqid);
if($cqty<$eqty){    
$qy=$_SESSION['axes_transwh'][$i]['qty'];    
$_SESSION['axes_transwh'][$i]['qty']=$qy+1;
}
break;    
}        
}       
return $flag;    
}

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

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

function get_transwhitmid($unqid){
$id='';    
if(isset($_SESSION['axes_transwh'])){
$max=count($_SESSION['axes_transwh']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_transwh'][$i]['unqid'];    
if($unqid==$eunqid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function add_trwserial($pid,$unqid,$data,$man=''){
		
if ( !isset($_SESSION['axes_trwse']) ) {
$_SESSION['axes_trwse'] = array();
};	
		
$excount = get_trnwhserial_count();
		
if($excount <= 0){unset($_SESSION['axes_trwse']);}
		
if(is_array($_SESSION['axes_trwse'])){
if($man==''){
if(transwhserial_exists($data)) return;	
}
    
$max=count($_SESSION['axes_trwse']);
$_SESSION['axes_trwse'][$max]['pid']=$pid;
$_SESSION['axes_trwse'][$max]['unqid']=$unqid;
$_SESSION['axes_trwse'][$max]['cartid']=get_transwhitmid($unqid);    
$_SESSION['axes_trwse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_trwse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_trwse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_trwse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_trwse']=array();
$_SESSION['axes_trwse'][0]['pid']=$pid;
$_SESSION['axes_trwse'][0]['unqid']=$unqid;
$_SESSION['axes_trwse'][0]['cartid']=get_transwhitmid($unqid);     
$_SESSION['axes_trwse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_trwse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_trwse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_trwse'][0]['imei']=strtoupper($data);
}
}

function transwhserial_exists($pid,$data){
global $con;
$flag=0;    
$data=strtoupper($data);
$sql="SELECT * FROM tbl_serial 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_trwse']) ){
$max=count($_SESSION['axes_trwse']);
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_trwse'][$i]['imei']){
$flag=1;
break;
}
}	
}    
}else{
$flag=1;
}
return $flag;
}

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

function remove_trwhslimei($unqid){ 
$max=count($_SESSION['axes_trwse']);
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_trwse'][$i]['unqid']){   
unset($_SESSION['axes_trwse'][$i]);    
}    
}
$_SESSION['axes_trwse']=array_values($_SESSION['axes_trwse']);    
}

function check_trwserial($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_trwse'])){
$max=count($_SESSION['axes_trwse']);
for($i=0;$i<$max;$i++){
$serial=$_SESSION['axes_trwse'][$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 check_trwvalidqty($whid){
$flag=0;
if(isset($_SESSION['axes_transwh'])){    
$max=count($_SESSION['axes_transwh']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_transwh'][$i]['unqid'];    
$eqty=get_warestockinfo($whid,$unqid,'avqty');
$qty=$_SESSION['axes_transwh'][$i]['qty'];
if($qty>$eqty){
$flag=1;
break;    
}    
}    
}else{
$flag=0;    
}
return $flag;    
}

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

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

function addtoselser($code,$qty,$name='',$cost='',$price=''){
if($qty<=0) return;

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

if($name!=''){
$sname=$name;
$scost=$cost;
$sprice=$price;
$stype='O';    
}else{
$sname=get_seviceinfo($code,'name');
$scost=get_seviceinfo($code,'cost');
$sprice=get_seviceinfo($code,'price');
$stype='E';    
}    
    
if(is_array($_SESSION['axes_service'])){
if($stype=='E'){    
if(selser_exists($code,$stype)) return;
}else{
if(selser_exists($name,$stype)) return;    
}
$max=count($_SESSION['axes_service']);
$_SESSION['axes_service'][$max]['code']=$code;
$_SESSION['axes_service'][$max]['sid']=get_seviceinfo($code,'id');    
$_SESSION['axes_service'][$max]['name']=$sname;      
$_SESSION['axes_service'][$max]['cost']=$scost; 
$_SESSION['axes_service'][$max]['qty']=$qty;
$_SESSION['axes_service'][$max]['price']=$sprice;
$_SESSION['axes_service'][$max]['disp']=0;
$_SESSION['axes_service'][$max]['disf']=0;    
$_SESSION['axes_service'][$max]['disamo']=0;
$_SESSION['axes_service'][$max]['snote']=''; 
$_SESSION['axes_service'][$max]['stype']=$stype;     
$_SESSION['axes_service'][$max]['subtot']=$sprice;     
}else{
$_SESSION['axes_sales']=array();    
$_SESSION['axes_service'][0]['code']=$code;
$_SESSION['axes_service'][0]['sid']=get_seviceinfo($code,'id');    
$_SESSION['axes_service'][0]['name']=$sname;      
$_SESSION['axes_service'][0]['cost']=$scost; 
$_SESSION['axes_service'][0]['qty']=$qty;
$_SESSION['axes_service'][0]['price']=$sprice;
$_SESSION['axes_service'][0]['disp']=0;
$_SESSION['axes_service'][0]['disf']=0;    
$_SESSION['axes_service'][0]['disamo']=0;
$_SESSION['axes_service'][0]['snote']=''; 
$_SESSION['axes_service'][0]['stype']=$stype;     
$_SESSION['axes_service'][0]['subtot']=$sprice;  
}
}

function selser_exists($key,$stype){
$max=count($_SESSION['axes_service']);
$flag=0;    
    
for($i=0;$i<$max;$i++){
if($stype=='E'){    
$mval=$_SESSION['axes_service'][$i]['code'];
}else{
$mval=$_SESSION['axes_service'][$i]['name'];    
}
if($key==$mval){
$flag=1;   
$qy=$_SESSION['axes_service'][$i]['qty'];    
$_SESSION['axes_service'][$i]['qty']=$qy+1;
$dis=$_SESSION['axes_service'][$i]['disf'];
$price=$_SESSION['axes_service'][$i]['price'];
$disamo=getfloatval(($dis*($qy+1)));
$subtot=(($price*($qy+1))-$disamo);     
$_SESSION['axes_service'][$i]['disamo']=$disamo;
$_SESSION['axes_service'][$i]['subtot']=$subtot; 
break;    
}        
}       
return $flag;    
}


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

function get_service_total(){
if(isset($_SESSION['axes_service'])){
$max=count($_SESSION['axes_service']);
$sum=0;
$stotal=0;
for($i=0;$i<$max;$i++){
$stotal=$_SESSION['axes_service'][$i]['subtot'];
$sum+=$stotal;
}
if($sum > 0){
return $sum;
}else{
return 0;    
}
}else{
return 0;        
}       
}
	
function get_serdiscount_total($edp=''){
if(isset($_SESSION['axes_service'])){
$max=count($_SESSION['axes_service']);
$sum=0;
for($i=0;$i<$max;$i++){
$idis=$_SESSION['axes_service'][$i]['disamo'];
$sum+=$idis;
}	
$bota=get_sales_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 for Update Ledger
/*--------------------------------------------------------------*/

function purchase_update($id,$table,$dtnow){
global $con;
$sql="SELECT * FROM ".$table." WHERE id='$id' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);

$invno=$row['invno'];    
$refinv='NULL';//$row['refinv'];    
$amo=($row['rawcash']-$row['changes']);
$ref='';    
$xrate=0;
$apdate=$row['apdate'];
$ibrid=$row['brid'];
$aid=$row['uid'];
if(strlen($row['details']>=3)){    
$det=explode('_',$row['details']);
if($det[0]=='CHK'){
$chkno="'".$det[1]."'";
$chkdt="'".$det[2]."'";
$otnote='NULL';    
}elseif($det[0]=='CRD'){
$chkno='NULL';
$chkdt='NULL';
$otnote="'".get_fild_data('tbl_bank',$det[2],'name').' - '.$det[1]."'";     
}elseif($det[0]=='MOB'){    
$chkno='NULL';
$chkdt='NULL';
$otnote="'".$det[1]."'";     
}    
}else{
$chkno='NULL';
$chkdt='NULL';
$otnote='NULL';    
}

if($row['itemdis']>0){
$camo=$row['itemdis'];
$cty='LE';
$cid='42';
$dty=$row['type'];    
$did=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}

if($row['disamo']>0){
$camo=$row['disamo'];
$cty='LE';
$cid='41';
$dty=$row['type'];    
$did=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($row['vatamo']>0){
$camo=$row['vatamo'];
$dty='LE';
$did='25';
$cty=$row['type'];
$cid=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}     

if($row['taxamo']>0){
$camo=$row['taxamo'];
$dty='LE';
$did='26';
$cty=$row['type'];
$cid=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
 
if($row['otamo']>0){
$camo=$row['otamo'];
$dty='LE';
$did='27';
$cty=$row['type'];
$cid=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($row['spmoney']>0){
$camo=$row['spmoney'];
$dty='LE';
$did='27';
$cty=$row['type'];
$cid=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
    
if($row['freight']>0){
$camo=$row['freight'];
$dty='LE';
$did='44';
$cty=$row['type'];
$cid=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}

if($row['less']>0){
$camo=$row['less'];
$cty='LE';
$cid='43';
$dty=$row['type'];    
$did=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($amo>0){
$camo=$amo;
$ref=$row['ref'];
$refinv=$row['invno'];    
$deb=explode('_',$row['creditid']);    
$cty=$deb[0];
$cid=$deb[1];
$dty=$row['type'];
$did=$row['supid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno','$refinv','$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
    
}


function purchret_update($id,$table,$dtnow){
global $con;
$sql="SELECT * FROM ".$table." WHERE id='$id' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);

$invno=$row['invno'];    
$refinv='NULL';//$row['refinv'];    
$amo=($row['rawcash']-$row['changes']);
$ref='';    
$xrate=0;
$apdate=$row['apdate'];
$ibrid=$row['brid'];
$aid=$row['uid'];
if(strlen($row['details']>=3)){    
$det=explode('_',$row['details']);
if($det[0]=='CHK'){
$chkno="'".$det[1]."'";
$chkdt="'".$det[2]."'";
$otnote='NULL';    
}elseif($det[0]=='CRD'){
$chkno='NULL';
$chkdt='NULL';
$otnote="'".get_fild_data('tbl_bank',$det[2],'name').' - '.$det[1]."'";     
}elseif($det[0]=='MOB'){    
$chkno='NULL';
$chkdt='NULL';
$otnote="'".$det[1]."'";     
}    
}else{
$chkno='NULL';
$chkdt='NULL';
$otnote='NULL';    
}

if($row['itemdis']>0){
$camo=$row['itemdis'];
$dty='LE';
$did='42';
$cty=$row['type'];    
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}

if($row['disamo']>0){
$camo=$row['disamo'];
$dty='LE';
$did='41';
$cty=$row['type'];    
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($row['vatamo']>0){
$camo=$row['vatamo'];
$cty='LE';
$cid='25';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}     

if($row['taxamo']>0){
$camo=$row['taxamo'];
$cty='LE';
$cid='26';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
 
if($row['less']>0){
$camo=$row['less'];
$dty='LE';
$did='45';
$cty=$row['type'];    
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($amo>0){
$camo=$amo;
$ref='';
$refinv=$row['invno'];    
$deb=explode('_',$row['debitid']);    
$dty=$deb[0];
$did=$deb[1];
$cty=$row['type'];
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno','$invno','$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
    
}


function sales_update($id,$table,$dtnow){
global $con;
$sql="SELECT * FROM ".$table." WHERE id='$id' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);

$invno=$row['invno'];    
$refinv='NULL';//$row['refinv'];    
$amo=($row['rawcash']-$row['changes']);
$ref='';    
$xrate=0;
$apdate=$row['apdate'];
$ibrid=$row['brid'];
$aid=$row['uid'];
if(strlen($row['details']>=3)){    
$det=explode('_',$row['details']);
if($det[0]=='CHK'){
$chkno="'".$det[1]."'";
$chkdt="'".$det[2]."'";
$otnote='NULL';    
}elseif($det[0]=='CRD'){
$chkno='NULL';
$chkdt='NULL';
$otnote="'".get_fild_data('tbl_bank',$det[2],'name').' - '.$det[1]."'";     
}elseif($det[0]=='MOB'){    
$chkno='NULL';
$chkdt='NULL';
$otnote="'".$det[1]."'";     
}    
}else{
$chkno='NULL';
$chkdt='NULL';
$otnote='NULL';    
}

if($row['itemdis']>0){
$camo=$row['itemdis'];
$dty='LE';
$did='37';
$cty=$row['type'];    
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}

if($row['disamo']>0){
$camo=$row['disamo'];
$dty='LE';
$did='36';
$cty=$row['type'];     
$cid=$row['cusid'];   
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($row['vatamo']>0){
$camo=$row['vatamo'];
$cty='LE';
$cid='10';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}     

if($row['aitamo']>0){
$camo=$row['aitamo'];
$cty='LE';
$cid='38';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
 
if($row['otamo']>0){
$camo=$row['otamo'];
$cty='LE';
$cid='39';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
 
if($row['freight']>0){
$camo=$row['freight'];
$cty='LE';
$cid='12';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}

if($row['less']>0){
$camo=$row['less'];
$dty='LE';
$did='40';
$cty=$row['type'];
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($amo>0){
$camo=$amo;
$ref=$row['ref'];
$refinv=$row['invno'];    
$deb=explode('_',$row['debitid']);    
$dty=$deb[0];
$did=$deb[1];
$cty=$row['type'];
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno','$refinv','$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
    
}

function salesre_update($id,$table,$dtnow){
global $con;
$sql="SELECT * FROM ".$table." WHERE id='$id' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));    
$row=mysqli_fetch_array($result);

$invno=$row['invno'];    
$refinv='NULL';//$row['refinv'];    
$amo=($row['rawcash']-$row['changes']);
$ref='';    
$xrate=0;
$apdate=$row['apdate'];
$ibrid=$row['brid'];
$aid=$row['uid'];
if(strlen($row['details']>=3)){    
$det=explode('_',$row['details']);
if($det[0]=='CHK'){
$chkno="'".$det[1]."'";
$chkdt="'".$det[2]."'";
$otnote='NULL';    
}elseif($det[0]=='CRD'){
$chkno='NULL';
$chkdt='NULL';
$otnote="'".get_fild_data('tbl_bank',$det[2],'name').' - '.$det[1]."'";     
}elseif($det[0]=='MOB'){    
$chkno='NULL';
$chkdt='NULL';
$otnote="'".$det[1]."'";     
}    
}else{
$chkno='NULL';
$chkdt='NULL';
$otnote='NULL';    
}

if($row['itemdis']>0){
$camo=$row['itemdis'];
$cty='LE';
$cid='37';
$dty=$row['type'];    
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}

if($row['disamo']>0){
$camo=$row['disamo'];
$cty='LE';
$cid='36';
$dty=$row['type'];     
$did=$row['cusid'];   
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($row['vatamo']>0){
$camo=$row['vatamo'];
$dty='LE';
$did='10';
$cty=$row['type'];
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}     

if($row['aitamo']>0){
$camo=$row['aitamo'];
$dty='LE';
$did='38';
$cty=$row['type'];
$cid=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
 
if($row['less']>0){
$camo=$row['less'];
$cty='LE';
$cid='46';
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno',$refinv,'$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,'$ref','0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    

if($amo>0){
$camo=$amo;
$ref='';
$refinv=$row['invno'];    
$deb=explode('_',$row['creditid']);    
$cty=$deb[0];
$cid=$deb[1];
$dty=$row['type'];
$did=$row['cusid'];    
$sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,otnote,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$invno','$invno','$dty','$did','$camo','$cid','$cty',$chkno,$chkdt,$otnote,NULL,'0','$xrate','$apdate','$ibrid','$aid','$dtnow')";    
mysqli_query($con,$sql) or die(mysqli_error($con));
}    
    
}


/*--------------------------------------------------------------*/
/* Function for Product Disposal
/*--------------------------------------------------------------*/

function addtodispos($type,$bwid,$unqid,$qty){
if($qty<=0) return;

if ( !isset($_SESSION['axes_dispos']) ) {
$_SESSION['axes_dispos'] = array();
}
    
if(is_array($_SESSION['axes_dispos'])){
if(dispos_exists($type,$bwid,$unqid)) return;
$max=count($_SESSION['axes_dispos']);
$_SESSION['axes_dispos'][$max]['unqid']=$unqid;
$_SESSION['axes_dispos'][$max]['pid']=get_salesinfo($unqid,'pid');
$_SESSION['axes_dispos'][$max]['type']=$type;
$_SESSION['axes_dispos'][$max]['bwid']=$bwid;    
$_SESSION['axes_dispos'][$max]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_dispos'][$max]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_dispos'][$max]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_dispos'][$max]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_dispos'][$max]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_dispos'][$max]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_dispos'][$max]['check']='';    
$_SESSION['axes_dispos'][$max]['cost']=get_salesinfo($unqid,'cost'); 
$_SESSION['axes_dispos'][$max]['qty']=$qty;
$_SESSION['axes_dispos'][$max]['price']=0;
$_SESSION['axes_dispos'][$max]['disp']=0;
$_SESSION['axes_dispos'][$max]['disf']=0;    
$_SESSION['axes_dispos'][$max]['disamo']=0;
$_SESSION['axes_dispos'][$max]['wday']=0;
$_SESSION['axes_dispos'][$max]['pnote']='';    
$_SESSION['axes_dispos'][$max]['subtot']=0;     
}else{
$_SESSION['axes_dispos']=array();    
$_SESSION['axes_dispos'][0]['unqid']=$unqid;
$_SESSION['axes_dispos'][0]['pid']=get_salesinfo($unqid,'pid');
$_SESSION['axes_dispos'][0]['type']=$type;
$_SESSION['axes_dispos'][0]['bwid']=$bwid;     
$_SESSION['axes_dispos'][0]['code']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'code');
$_SESSION['axes_dispos'][0]['name']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'name');
$_SESSION['axes_dispos'][0]['unid']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'unid');
$_SESSION['axes_dispos'][0]['col']=get_salesinfo($unqid,'colid');
$_SESSION['axes_dispos'][0]['siz']=get_salesinfo($unqid,'sizid');    
$_SESSION['axes_dispos'][0]['imei']=get_fild_data('tbl_item',get_salesinfo($unqid,'pid'),'pmod');
$_SESSION['axes_dispos'][0]['check']='';    
$_SESSION['axes_dispos'][0]['cost']=get_salesinfo($unqid,'cost'); 
$_SESSION['axes_dispos'][0]['qty']=$qty;
$_SESSION['axes_dispos'][0]['price']=0;
$_SESSION['axes_dispos'][0]['disp']=0;
$_SESSION['axes_dispos'][0]['disf']=0;    
$_SESSION['axes_dispos'][0]['disamo']=0;
$_SESSION['axes_dispos'][0]['wday']=0;
$_SESSION['axes_dispos'][0]['pnote']='';    
$_SESSION['axes_dispos'][0]['subtot']=0;    
}
}

function get_dispos_count($unqid){
$cqty=0;
if(isset($_SESSION['axes_dispos'])){
$max=count($_SESSION['axes_dispos']);
for($i=0;$i<$max;$i++){
$qty=$_SESSION['axes_dispos'][$i]['qty'];
$itid=$_SESSION['axes_dispos'][$i]['unqid'];
if($unqid==$itid){
$cqty+=$qty;
}
}
if($cqty>0){
return $cqty;	
}else{
return 0;	
}
}else{
return 0;	
}
}

function get_dispos_total($key){
$total=0;
$totcost=0;    
if(isset($_SESSION['axes_dispos'])){
$max=count($_SESSION['axes_dispos']);
for($i=0;$i<$max;$i++){
$subtot=$_SESSION['axes_dispos'][$i]['subtot'];
$qty=$_SESSION['axes_dispos'][$i]['qty'];
$cost=$_SESSION['axes_dispos'][$i]['cost'];
$totcost+=($cost*$qty);    
$total+=$subtot;
}
}else{
$total=0;
$totcost=0;    
}
if($key=='P'){
return $total;    
}else{
return $totcost;    
}       
}

function dispos_exists($type,$bwid,$unqid){
$max=count($_SESSION['axes_dispos']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_dispos'][$i]['unqid']){
$flag=1;
if($type=='B'){    
$eqty=(get_selestockinfo($bwid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));
}else{
$eqty=get_warestockinfo($bwid,$unqid,'avqty');    
}
$cqty=get_dispos_count($unqid);
if($cqty<$eqty){    
$qy=$_SESSION['axes_dispos'][$i]['qty'];
$price=$_SESSION['axes_dispos'][$i]['price'];    
$_SESSION['axes_dispos'][$i]['qty']=$qy+1;
$_SESSION['axes_dispos'][$i]['subtot']=($price*($qy+1));    
}
break;    
}        
}       
return $flag;    
}

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

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

function get_dispositmid($unqid){
$id='';    
if(isset($_SESSION['axes_dispos'])){
$max=count($_SESSION['axes_dispos']);
for($i=0;$i<$max;$i++){
$eunqid=$_SESSION['axes_dispos'][$i]['unqid'];    
if($unqid==$eunqid){
$id=$i;    
break;    
}    
}
}else{
$id='';   
}
return $id;     
}

function add_disposserial($pid,$unqid,$data,$man=''){
		
if ( !isset($_SESSION['axes_dspse']) ) {
$_SESSION['axes_dspse'] = array();
};	
		
$excount = get_disposserial_count();
		
if($excount <= 0){unset($_SESSION['axes_dspse']);}
		
if(is_array($_SESSION['axes_dspse'])){
if($man==''){
if(disposserial_exists($data)) return;	
}
    
$max=count($_SESSION['axes_dspse']);
$_SESSION['axes_dspse'][$max]['pid']=$pid;
$_SESSION['axes_dspse'][$max]['unqid']=$unqid;
$_SESSION['axes_dspse'][$max]['cartid']=get_transbritmid($unqid);    
$_SESSION['axes_dspse'][$max]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_dspse'][$max]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_dspse'][$max]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_dspse'][$max]['imei']=strtoupper($data);
}else{
$_SESSION['axes_dspse']=array();
$_SESSION['axes_dspse'][0]['pid']=$pid;
$_SESSION['axes_dspse'][0]['unqid']=$unqid;
$_SESSION['axes_dspse'][0]['cartid']=get_transbritmid($unqid);     
$_SESSION['axes_dspse'][0]['code']=get_fild_data('tbl_item',$pid,'code');
$_SESSION['axes_dspse'][0]['name']=get_fild_data('tbl_item',$pid,'name');
$_SESSION['axes_dspse'][0]['reumei']=get_fild_data('tbl_item',$pid,'pmod');
$_SESSION['axes_dspse'][0]['imei']=strtoupper($data);
}
}

function disposserial_exists($pid,$data){
global $con;
$flag=0;    
$data=strtoupper($data);
$sql="SELECT * FROM tbl_serial 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_dspse']) ){
$max=count($_SESSION['axes_dspse']);
for($i=0;$i<$max;$i++){
if($data==$_SESSION['axes_dspse'][$i]['imei']){
$flag=1;
break;
}
}	
}    
}else{
$flag=1;
}
return $flag;
}

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

function remove_disposlimei($unqid){ 
$max=count($_SESSION['axes_dspse']);
for($i=0;$i<$max;$i++){   
if($unqid==$_SESSION['axes_dspse'][$i]['unqid']){   
unset($_SESSION['axes_dspse'][$i]);    
}    
}
$_SESSION['axes_dspse']=array_values($_SESSION['axes_dspse']);    
}

function check_disserial($key){
$res=0;
$max=0;    
if(isset($_SESSION['axes_dspse'])){
$max=count($_SESSION['axes_dspse']);
for($i=0;$i<$max;$i++){
$serial=$_SESSION['axes_dspse'][$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 check_dpbvalidqty($type,$bwid){
$flag=0;
if(isset($_SESSION['axes_dispos'])){    
$max=count($_SESSION['axes_dispos']);
for($i=0;$i<$max;$i++){
$unqid=$_SESSION['axes_dispos'][$i]['unqid'];    
if($type=='B'){    
$eqty=(get_selestockinfo($bwid,$unqid,'avqty')-get_selestockinfo($bwid,$unqid,'sloc'));
}else{
$eqty=get_warestockinfo($bwid,$unqid,'avqty');    
}
$qty=$_SESSION['axes_dispos'][$i]['qty'];
if($qty>$eqty){
$flag=1;
break;    
}    
}    
}else{
$flag=0;    
}
return $flag;    
}

/*--------------------------------------------------------------*/
/* Function for Warranty Bad Stock Claim
/*--------------------------------------------------------------*/
function get_warrantyinfo($id,$key){
global $con;
$sql="SELECT ".$key." AS rdata FROM (SELECT wst.id,wst.pid,itm.name,itm.image,itm.code,wst.colid,wst.sizid,wst.b_in,wst.b_out,wst.supid,wst.serial,wst.exp_date,wst.unqid,wst.binvno,wst.brid FROM (SELECT id,pid,colid,sizid,IFNULL(b_in,0) AS b_in,IFNULL(b_out,0) AS b_out,supid,serial,exp_date,unqid,binvno,brid FROM (SELECT id,supid,pid,colid,sizid,b_in,b_out,serial,exp_date,unqid,binvno,brid FROM tbl_waranty WHERE id='$id') stwar) wst LEFT JOIN (SELECT id,name,image,code FROM tbl_item) itm ON itm.id=wst.pid) AS data"; 
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$row=mysqli_fetch_array($result);
return $row['rdata'];    
}


function addbadclim($id){
if($id<=0) return;

if ( !isset($_SESSION['axes_badstclaim']) ) {
$_SESSION['axes_badstclaim'] = array();
}
    
if(is_array($_SESSION['axes_badstclaim'])){
if(badst_exists($id)) return;
$max=count($_SESSION['axes_badstclaim']);
$_SESSION['axes_badstclaim'][$max]['id']=$id;
$_SESSION['axes_badstclaim'][$max]['pid']=get_warrantyinfo($id,'pid');
$_SESSION['axes_badstclaim'][$max]['name']=get_warrantyinfo($id,'name');
$_SESSION['axes_badstclaim'][$max]['colid']=get_warrantyinfo($id,'colid');
$_SESSION['axes_badstclaim'][$max]['sizid']=get_warrantyinfo($id,'sizid');    
$_SESSION['axes_badstclaim'][$max]['serial']=get_warrantyinfo($id,'serial');    
}else{
$_SESSION['axes_badstclaim']=array();    
$_SESSION['axes_badstclaim'][0]['id']=$id;
$_SESSION['axes_badstclaim'][0]['pid']=get_warrantyinfo($id,'pid');
$_SESSION['axes_badstclaim'][0]['name']=get_warrantyinfo($id,'name');
$_SESSION['axes_badstclaim'][0]['colid']=get_warrantyinfo($id,'colid');
$_SESSION['axes_badstclaim'][0]['sizid']=get_warrantyinfo($id,'sizid');    
$_SESSION['axes_badstclaim'][0]['serial']=get_warrantyinfo($id,'serial');     
}
}

function badst_exists($id){
$max=count($_SESSION['axes_badstclaim']);
$flag=0;    
    
for($i=0;$i<$max;$i++){   
if($id==$_SESSION['axes_badstclaim'][$i]['id']){
$flag=1;
break;    
}        
}       
return $flag;    
}

function remove_badstclaim($id){
$id=intval($id);
if(isset($_SESSION['axes_badstclaim'])){
$max=count($_SESSION['axes_badstclaim']);
for($i=0;$i<$max;$i++){
if($id==$i){
unset($_SESSION['axes_badstclaim'][$i]);
break;
}
}
$_SESSION['axes_badstclaim']=array_values($_SESSION['axes_badstclaim']);
}
}
?>