<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
include ('../inc/repfunctions.php');
include ('../inc/invfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
$today = date("Y-m-d");    
$dtnow = date("Y-m-d h:i:s", time());    
}else{
header('Location:../index.php');
exit;    
}

if(isset($_POST['additem'])){
$unqid=$_POST['additem'];   
addtosel($brid,$unqid,1);    
}

if(isset($_POST['tyserial'])){
$serial = $_POST['search'];
$sql="SELECT tbl_stock.unqid,tbl_stock.price,tbl_item.name,tbl_serial.serial FROM tbl_serial LEFT JOIN tbl_stock ON tbl_stock.invno=tbl_serial.purinv AND tbl_stock.pid=tbl_serial.pid LEFT JOIN tbl_item ON tbl_item.id=tbl_serial.pid WHERE tbl_serial.serial LIKE '%$serial%' AND tbl_serial.rci='Y' AND tbl_serial.brid='$brid' AND tbl_serial.status='0' LIMIT 20";    
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    
while($row = mysqli_fetch_array($result) ){
$response[] = array("value"=>$row['unqid'],"label"=>$row['serial']);
}

echo json_encode($response);
exit;    
}

if(isset($_POST['addslitem'])){
$unqid=$_POST['addslitem'];
$serial=$_POST['serial'];
$pid=get_fild_data('tbl_stock','','pid',"unqid='$unqid'");     
if(selimei_exists($pid,$serial)){
return;
exit;    
}    
addtosel($brid,$unqid,1);   
add_selserial($pid,$unqid,$serial,'B');    
}

if(isset($_POST['itmsear'])){
$search = $_POST['search'];
$sql = "SELECT tbl_brstock.unqid,tbl_brstock.code,tbl_brstock.name,tbl_stock.cost,tbl_stock.price,tbl_brstock.avqty FROM tbl_brstock LEFT JOIN tbl_stock ON tbl_stock.unqid=tbl_brstock.unqid WHERE tbl_brstock.brid='$brid' AND tbl_brstock.status='1' AND (tbl_brstock.name LIKE '%$search%' OR tbl_brstock.code LIKE '%$search%') AND tbl_brstock.avqty>0 LIMIT 20";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    
while($row = mysqli_fetch_array($result) ){
$response[] = array("value"=>$row['unqid'],"label"=>$row['code'].' | '.$row['name'].' | Qty:'.$row['avqty'].' | '.$row['price']);
}

echo json_encode($response);
exit;    
}

if(isset($_POST['scanbarcode'])){
$out='';	
$bcode=strtoupper(remove_junk(escape($_POST['scanbarcode'])));
$qty=$_POST['qty'];    
$type=substr($bcode,0,3);
if(substr($type,0,2)==='OU'){$type='JOU';$bcode='J'.$bcode;}    
$parray = array('EMP','JOU','REV','PAV','EXP','PRE','PUR','POR','SRE','SEL','EST','SER','RCI','DLI','PDR','CLA','TRB','TRW');     
if(strlen(array_search($type,$parray))>0){
$pmsg=array(
'status' => 'invno',
'invoice'=> $bcode    
);
echo json_encode($pmsg);
return;    
}    
    
$sql="SELECT tbl_stock.unqid FROM tbl_serial LEFT JOIN tbl_stock ON tbl_stock.invno=tbl_serial.purinv AND tbl_stock.pid=tbl_serial.pid WHERE tbl_serial.serial='$bcode' AND tbl_serial.rci='Y' AND tbl_serial.status='0' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
if($result->num_rows > 0){
$rowsl = mysqli_fetch_array($result);
$sql="SELECT pid,unqid FROM tbl_brstock WHERE brid='$brid' AND unqid='".$rowsl['unqid']."' AND avqty>0 AND status='1' LIMIT 1";		
$resl=mysqli_query($con,$sql) or die(mysqli_error($con));
$rowp = mysqli_fetch_array($resl);
if(selimei_exists($rowp['pid'],$bcode)){
$pmsg=array(
'status' => 'exits',
'message'=> 'Serial / IMEI already Added!!'    
);
echo json_encode($pmsg);
return;    
}
if(selimei_pidewos($rowp['unqid'])>0){
$sid=selimei_pidewos($rowp['unqid'])-1;    
$_SESSION['axes_selse'][$sid]['imei']=$bcode;
$pmsg=array(
'status' => 'success',
'message'=> 'Serial Add to cart Successfully!!'    
);
echo json_encode($pmsg);
return;    
}else{    
addtosel($brid,$rowp['unqid'],1,'A');
add_selserial($rowp['pid'],$rowp['unqid'],$bcode,'B');    
$pmsg=array(
'status' => 'success',
'message'=> 'Product Add to cart Successfully!!'    
);
echo json_encode($pmsg);
return;
}
}else{
$bt=substr($bcode,0,2);    
if($bt=='GP'){
$sql="SELECT pid,unqid FROM tbl_brstock WHERE brid='$brid' AND unqid='$bcode' AND avqty>0 AND status='1' LIMIT 1";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
if($result->num_rows > 0){
$rowgp = mysqli_fetch_array($result);    
addtosel($brid,$rowgp['unqid'],1);
$pmsg=array(
'status' => 'success',
'message'=> 'Product Add to cart Successfully!!'    
);
echo json_encode($pmsg);
return;    
}else{
$pmsg=array(
'status' => 'fail',
'message'=> 'No item found!!'    
);
echo json_encode($pmsg);
return;    
}
}else{
$sql="SELECT tbl_brstock.pid,tbl_brstock.name,tbl_brstock.code,tbl_brstock.unqid,tbl_stock.price FROM tbl_brstock LEFT JOIN tbl_item ON tbl_item.id=tbl_brstock.pid LEFT JOIN tbl_stock ON tbl_stock.unqid=tbl_brstock.unqid WHERE tbl_brstock.brid='$brid' AND tbl_brstock.avqty>0 AND tbl_brstock.status='1' AND  tbl_item.barcode='$bcode'";
$result=mysqli_query($con,$sql) or die(mysqli_error($con));
if($result->num_rows > 0){
$fitem=$result->num_rows;    
if($fitem > 1){
$out.='<br>';
while($row = mysqli_fetch_array($result)){
$out.='<input type="radio" name="prorbuton" class="prorbuton" value="'.$row['unqid'].'" onclick="myFunctions(this.value)">'.' '.$row['code'].' - '.$row['name'].' - '.$row['price'].'<br>';	
}
$pmsg=array(
'status' => 'multi',
'html'=> $out    
);
echo json_encode($pmsg);
return;        
}else{
$row = mysqli_fetch_array($result);
addtosel($brid,$row['unqid'],1,'B');
$pmsg=array(
'status' => 'success',
'message'=> 'Product Add to cart Successfully!!'    
);
echo json_encode($pmsg);
return;    
}    
}else{
$pmsg=array(
'status' => 'fail',
'message'=> 'No item found!!'    
);
echo json_encode($pmsg);    
}
}	
}
}

if(isset($_POST['itmsear'])){
$search = $_POST['search'];
$sql = "SELECT tbl_brstock.unqid,tbl_brstock.code,tbl_brstock.name,tbl_stock.cost,tbl_stock.price FROM tbl_brstock LEFT JOIN tbl_stock ON tbl_stock.unqid=tbl_brstock.unqid WHERE tbl_brstock.brid='$brid' AND tbl_brstock.status='1' AND (tbl_brstock.name LIKE '%$search%' OR tbl_brstock.code LIKE '%$search%') LIMIT 20";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    
while($row = mysqli_fetch_array($result) ){
$response[] = array("value"=>$row['unqid'],"label"=>$row['code'].' | '.$row['name'].' | '.$row['price']);
}

echo json_encode($response);
exit;    
}

if(isset($_POST['remove'])){
$ids = floatval($_POST['remove']);
$unqid=$_SESSION['axes_sales'][$ids]['unqid'];    
remove_sales($ids);
if(isset($_SESSION['axes_selse'])){    
remove_slimei($unqid);
}
$max=count($_SESSION['axes_sales']);
if($max <= 0){
unset($_SESSION['axes_sales']);
unset($_SESSION['axes_salesde']);
if(isset($_SESSION['axes_selse'])){
unset($_SESSION['axes_selse']);	
}	
}
if(isset($_SESSION['axes_selse'])){
$smax=count($_SESSION['axes_selse']);
if($smax <= 0){    
unset($_SESSION['axes_selse']);
}
}    
}

if(isset($_POST['upqty'])){
$ids=intval($_POST['upqty']);
$qty=floatval($_POST['qty']);
if($qty<0 || $qty==''){
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata);    
exit;
}
$unqid = $_SESSION['axes_sales'][$ids]['unqid'];
$eqty=(get_selestockinfo($brid,$unqid,'avqty')-get_selestockinfo($brid,$unqid,'sloc'));    
$dis = $_SESSION['axes_sales'][$ids]['disf'];
if($qty<=$eqty){
$_SESSION['axes_sales'][$ids]['qty']=$qty;
$_SESSION['axes_sales'][$ids]['check']='M';    
}elseif($qty>$eqty){
$_SESSION['axes_sales'][$ids]['qty']=$eqty;
$_SESSION['axes_sales'][$ids]['check']='M';    
}    
$price = $_SESSION['axes_sales'][$ids]['price'];
$sqty = $_SESSION['axes_sales'][$ids]['qty'];
$disamo = getfloatval(($dis*$sqty));
$stot = ($price*$sqty);
if($price>0){
$disp=(($dis/$price)*100);
}else{
$disp=0;	
}
$_SESSION['axes_sales'][$ids]['disp']=getfloatval($disp);
$_SESSION['axes_sales'][$ids]['disamo']=$disamo;
$_SESSION['axes_sales'][$ids]['subtot'] = getfloatval(($stot-$disamo));
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata);
}

if(isset($_POST['upprice'])){
$ids=intval($_POST['upprice']);
$amo=floatval($_POST['amo']);
if($amo<0 || $amo==''){
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata);    
exit;}
$qty=$_SESSION['axes_sales'][$ids]['qty'];
$dis=$_SESSION['axes_sales'][$ids]['disf'];
$_SESSION['axes_sales'][$ids]['price']=$amo;
$disamo= getfloatval(($dis*$qty));
$disp=(($dis/$amo)*100);
$_SESSION['axes_sales'][$ids]['disp']=getfloatval($disp);
$_SESSION['axes_sales'][$ids]['disamo']=$disamo;
$_SESSION['axes_sales'][$ids]['subtot']=(($amo*$qty)-$disamo);
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata);	
}

if(isset($_POST['check'])){
$ids=$_POST['pid'];
$val=$_POST['check'];
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
if($ids==$i){
$_SESSION['axes_sales'][$i]['check']=$val;	
}else{
$_SESSION['axes_sales'][$i]['check']=0;	
}	
}	
}

if(isset($_POST['upnote'])){
$ids=intval($_POST['upnote']);
$note=ucwords(remove_junk(escape($_POST['note'])));
$_SESSION['axes_sales'][$ids]['pnote']=$note;    
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata);    
}

if(isset($_POST['upwday'])){
  $ids=intval($_POST['upwday']);
  $wdays=floatval($_POST['wdays']);
  $_SESSION['axes_sales'][$ids]['wday']=$wdays;    
  $redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo'],$_SESSION['axes_sales'][$ids]['wday']);
  echo json_encode($redata);    
  }

if(isset($_POST['itemdisp'])){
$ids=intval($_POST['itemdisp']);
$disc=floatval($_POST['disp']);
//if( $disc==''){exit;}
$_SESSION['axes_sales'][$ids]['disp']=$disc;
$price=$_SESSION['axes_sales'][$ids]['price'];
$uqty=$_SESSION['axes_sales'][$ids]['qty'];
$dis=(($price*$disc)*0.01);
$disamo = ($dis*$uqty); 
$_SESSION['axes_sales'][$ids]['disf']=getfloatval($dis);
$_SESSION['axes_sales'][$ids]['disamo']=getfloatval($disamo);
$_SESSION['axes_sales'][$ids]['subtot']=(($price*$uqty)-$disamo);
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata); 
}

if(isset($_POST['itemdisf'])){
$ids=intval($_POST['itemdisf']);
$dica=floatval($_POST['disf']);
$price=$_SESSION['axes_sales'][$ids]['price'];
$uqty=$_SESSION['axes_sales'][$ids]['qty'];
$disp=(($dica/$price)*100);
$_SESSION['axes_sales'][$ids]['disf']=getfloatval($dica);
$disamo = ($dica*$uqty);
$_SESSION['axes_sales'][$ids]['disamo']=getfloatval($disamo);
if($dica<=0){
$_SESSION['axes_sales'][$ids]['disp']=getfloatval(0);	
}else{
$_SESSION['axes_sales'][$ids]['disp']=getfloatval($disp);	
}
$_SESSION['axes_sales'][$ids]['subtot']=(($price*$uqty)-$disamo);
$redata=array($_SESSION['axes_sales'][$ids]['qty'],$_SESSION['axes_sales'][$ids]['price'],$_SESSION['axes_sales'][$ids]['subtot'],$_SESSION['axes_sales'][$ids]['pnote'],$_SESSION['axes_sales'][$ids]['disp'],$_SESSION['axes_sales'][$ids]['disf'],$_SESSION['axes_sales'][$ids]['disamo']);
echo json_encode($redata); 
}

if(isset($_POST['foot'])){
$disp=$_SESSION['axes_salesde'][0]['disp'];
$redata=array($_SESSION['axes_salesde'][0]['disp'],(get_seldiscount_total(floatval($disp))-get_seldiscount_total()),get_seldiscount_total($disp),$_SESSION['axes_salesde'][0]['vatp'],$_SESSION['axes_salesde'][0]['vatamo'],$_SESSION['axes_salesde'][0]['aitp'],$_SESSION['axes_salesde'][0]['aitamo'],$_SESSION['axes_salesde'][0]['name'],$_SESSION['axes_salesde'][0]['others'],$_SESSION['axes_salesde'][0]['freight'],$_SESSION['axes_salesde'][0]['less'],$_SESSION['axes_salesde'][0]['gtotal']);
echo json_encode($redata);	
exit;
}

if(isset($_POST['seldis'])){
$disp=floatval($_POST['seldis']);
if(isset($_SESSION['axes_salesde'])){
if($disp>0){
$_SESSION['axes_salesde'][0]['disp']= getfloatval($disp);
$_SESSION['axes_salesde'][0]['disamo']=getfloatval(get_seldiscount_total(floatval($disp)));    
}else{    
$_SESSION['axes_salesde'][0]['disp']= getfloatval(0);
$_SESSION['axes_salesde'][0]['disamo']=getfloatval(0);    
}
}
}

if(isset($_POST['selvat'])){
$rvat=floatval($_POST['selvat']);
$disp=$_SESSION['axes_salesde'][0]['disp'];    
if(isset($_SESSION['axes_salesde'])){
if($rvat>0){	
$_SESSION['axes_salesde'][0]['vatp']= getfloatval($rvat);
$_SESSION['axes_salesde'][0]['vatamo']= getfloatval((((get_sales_total()-(get_seldiscount_total($disp)-get_seldiscount_total()))*$rvat)*0.01));    
}else{    
$_SESSION['axes_salesde'][0]['vatp']= 0;
$_SESSION['axes_salesde'][0]['vatamo']= 0;    
}
} 
}

if(isset($_POST['seltax'])){
$rtax=floatval($_POST['seltax']);
$disp=$_SESSION['axes_salesde'][0]['disp'];    
if(isset($_SESSION['axes_salesde'])){
if($rtax>0){	
$_SESSION['axes_salesde'][0]['aitp']= getfloatval($rtax);
$_SESSION['axes_salesde'][0]['aitamo']= getfloatval((((get_sales_total()-(get_seldiscount_total($disp)-get_seldiscount_total()))*$rtax)*0.01));    
}else{    
$_SESSION['axes_salesde'][0]['aitp']= 0;
$_SESSION['axes_salesde'][0]['aitamo']= 0;    
}
}
}

if(isset($_POST['others'])){
$ota=floatval($_POST['others']);
if(isset($_SESSION['axes_salesde'])){
$_SESSION['axes_salesde'][0]['others']=getfloatval($ota);	
}
}

if(isset($_POST['otname'])){
$otn=$_POST['otname'];
if($otn==''){return;}
if(isset($_SESSION['axes_salesde'])){
$_SESSION['axes_salesde'][0]['name']=ucwords($otn);	
}	
}

if(isset($_POST['freight'])){
$fre=floatval($_POST['freight']);
if(isset($_SESSION['axes_salesde'])){
$_SESSION['axes_salesde'][0]['freight']=getfloatval($fre);	
}
}

if(isset($_POST['less'])){
$less=floatval($_POST['less']);
if(isset($_SESSION['axes_salesde'])){
if($less>0){
$_SESSION['axes_salesde'][0]['less']= getfloatval($less);    
}else{    
$_SESSION['axes_salesde'][0]['less']= 0;   
}
}
}

if(isset($_POST['removeitmsl'])){
$id=intval($_POST['removeitmsl']);
$unqid=$_SESSION['axes_selse'][$id]['unqid'];
$itid=get_cartitemid($unqid);
$itmqty=$_SESSION['axes_sales'][$itid]['qty'];

remove_selimei($id);    
if($itmqty<=1){
remove_sales($itid);    
}else{
$_SESSION['axes_sales'][$itid]['qty']=($itmqty-1);
$dis = $_SESSION['axes_sales'][$itid]['disf'];    
$price = $_SESSION['axes_sales'][$itid]['price'];
$sqty = $_SESSION['axes_sales'][$itid]['qty'];
$disamo = getfloatval(($dis*$sqty));
$stot = ($price*$sqty);
if($price>0){
$disp=(($dis/$price)*100);
}else{
$disp=0;	
}
$_SESSION['axes_sales'][$itid]['disp']=getfloatval($disp);
$_SESSION['axes_sales'][$itid]['disamo']=$disamo;
$_SESSION['axes_sales'][$itid]['subtot'] = getfloatval(($stot-$disamo));    
}
    
$max=count($_SESSION['axes_sales']);
if($max <= 0){
unset($_SESSION['axes_sales']);
unset($_SESSION['axes_salesde']);
if(isset($_SESSION['axes_selse'])){
unset($_SESSION['axes_selse']);	
}	
}    
}

if(isset($_POST['upimei'])){
$ids=intval($_POST['upimei']);
$serialno=strtoupper(remove_junk(escape($_POST['imeidata'])));
$pid=intval($_SESSION['axes_selse'][$ids]['pid']);
if($serialno!=''){
if(selimei_exists($pid,$serialno)){
$redata=array($_SESSION['axes_selse'][$ids]['imei']);
echo json_encode($redata);    
return;
exit;    
}
$_SESSION['axes_selse'][$ids]['imei']= $serialno;    
}else{
$_SESSION['axes_selse'][$ids]['imei']='';    
}    
$redata=array($_SESSION['axes_selse'][$ids]['imei']);
echo json_encode($redata); 
}

if(isset($_POST['audimei'])){
$id=$_POST['audimei'];
$pid=intval($_SESSION['axes_selse'][$id]['pid']);
$unqid=$_SESSION['axes_selse'][$id]['unqid'];    
$search = $_POST['search'];
$purinv=get_salesinfo($unqid,'invno');
    
$sql="SELECT pid,serial FROM tbl_serial WHERE purinv='$purinv' AND pid='$pid' AND serial LIKE '%$search%' AND brid='$brid' AND rci='Y' AND status='0' ORDER BY serial ASC LIMIT 30";		
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    
while($row = mysqli_fetch_array($result) ){
$response[] = array("value"=>$row['pid'],"label"=>$row['serial']);
}

// encoding array to json format
echo json_encode($response);
exit;    
}

if(isset($_SESSION['axes_salesde'])){
if(is_array($_SESSION['axes_salesde'])){    
$disps=$_SESSION['axes_salesde'][0]['disp'];
if($disps>0){
$_SESSION['axes_salesde'][0]['disamo']=getfloatval(get_seldiscount_total(floatval($disps)));	
}else{
$_SESSION['axes_salesde'][0]['disamo']=getfloatval(get_seldiscount_total());	
}
$vatps=$_SESSION['axes_salesde'][0]['vatp'];
if($vatps>0){
$_SESSION['axes_salesde'][0]['vatamo']= getfloatval(((get_sales_total()-(get_seldiscount_total($disps)- get_seldiscount_total()))*$vatps)*0.01);	
}
$taxps=$_SESSION['axes_salesde'][0]['aitp'];
if($taxps>0){
$_SESSION['axes_salesde'][0]['aitamo']= getfloatval(((get_sales_total()-(get_seldiscount_total($disps)- get_seldiscount_total()))*$taxps)*0.01);	
}
$freight=$_SESSION['axes_salesde'][0]['freight'];
$vatamos=$_SESSION['axes_salesde'][0]['vatamo'];
$aitamos=$_SESSION['axes_salesde'][0]['aitamo'];
$other=$_SESSION['axes_salesde'][0]['others'];
$lesss=$_SESSION['axes_salesde'][0]['less'];    
}else{
$disps=0;
$vatps=0;
$vatamos=0;
$aitamos=0;
$freight=0;
$other=0;
$lesss=0;    
}
$_SESSION['axes_salesde'][0]['gtotal']=(get_sales_total()+$vatamos+$other+$aitamos+$freight)-((get_seldiscount_total(floatval($disps))- get_seldiscount_total())+$lesss);
}

if(isset($_POST['clear'])){
if(isset($_SESSION['axes_sales'])){
unset($_SESSION['axes_sales']);
unset($_SESSION['axes_salesde']);    
}
if(isset($_SESSION['axes_selse'])){
unset($_SESSION['axes_selse']);	
}
}

if(isset($_POST['getcus'])){
$search = $_POST['search'];
$sql="SELECT * FROM (SELECT CONCAT('CU_',id) AS id,code,name,cnumber FROM tbl_customer UNION ALL SELECT CONCAT('SU_',id) AS id,code,name,cnumber FROM tbl_supplier) AS customer WHERE (name LIKE '%$search%' OR code LIKE '%$search%' OR cnumber LIKE '%$search%') ORDER BY name ASC,code ASC LIMIT 30";		
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    
while($row = mysqli_fetch_array($result) ){
$response[] = array("value"=>$row['id'],"label"=>$row['code'].' | '.$row['name'].' | '.$row['cnumber']);
}

// encoding array to json format
echo json_encode($response);
exit;
}

if(isset($_POST['checkbal'])){
if(strlen($_POST['checkbal'])>0){    
$id=str_replace('_','',$_POST['checkbal']);
$ldebit=get_ledgerval($id,'D','N');
$lcredit=get_ledgerval($id,'C','N');
$lnet=($ldebit-$lcredit);
echo number_format($lnet,2);
}else{
echo '0.00';    
}    
}

// if(isset($_POST['checkbal'])){
//   if(strlen($_POST['checkbal'])>0){    
//   $sdata=explode('_',remove_junk(escape($_POST['checkbal'])));
//   $type=$sdata['0'];    
//   $id = $sdata['1'];
  
//   if($type=='SU'){
//   $sql="SELECT (debit-credit) as balance From tbl_supplier LEFT JOIN tbl_supdebit ON tbl_supdebit.id=tbl_supplier.id LEFT JOIN tbl_supcredit ON tbl_supcredit.id=tbl_supplier.id WHERE tbl_supplier.id='$id' ORDER BY name ASC";    
//   }else{
//   $sql="SELECT (debit-credit) as balance FROM tbl_cusbalance WHERE id='$id'";  
//   }  
//   $over=mysqli_query($con,$sql) or die(mysqli_error($con));    
//   $ost=mysqli_fetch_array($over);    
//   $lnet=$ost['balance'];
//   echo number_format($lnet,2);
//   }else{
//   echo '0.00';    
//   }    
//   }

if(isset($_POST['expcsv'])){
$fdate=$_POST['fdate'];
$tdate=$_POST['tdate'];
$output='';
$sql="SELECT Package_Description,Receiver_Name,IF(endata.type='CU' AND endata.cusid=0,(SELECT waddress FROM tbl_sales WHERE invno=endata.invno),(SELECT address FROM tbl_customer WHERE id=endata.cusid)) AS Receiver_Address,(SELECT mobile FROM tbl_sales WHERE invno=endata.invno) AS Receiver_Number,IF(endata.city!='',(SELECT name FROM tbl_district WHERE id=endata.city),'') AS Receiver_City,IF(endata.zone!='',(SELECT name FROM tbl_zone WHERE id=endata.zone),'') AS Receiver_Zone,IF(endata.type='CU' AND endata.cusid=0,(SELECT wemail FROM tbl_sales WHERE invno=endata.invno),(SELECT cemail FROM tbl_customer WHERE id=endata.cusid)) AS Recipient_Email,Price,'' AS Instructions,qty,date FROM (SELECT GROUP_CONCAT(tbl_item.name) AS Package_Description,SUM(tbl_traproduct.s_qty) AS qty,type,tid AS cusid,(SELECT did FROM tbl_sales WHERE invno=tbl_traproduct.invno) AS city,(SELECT zid FROM tbl_sales WHERE invno=tbl_traproduct.invno) AS zone,(SELECT name FROM tbl_sales WHERE invno=tbl_traproduct.invno) AS Receiver_Name,(SELECT total FROM tbl_sales WHERE invno=tbl_traproduct.invno) AS Price,(SELECT apdate FROM tbl_sales WHERE invno=tbl_traproduct.invno) AS date,invno
FROM tbl_traproduct LEFT JOIN tbl_item ON tbl_item.id=tbl_traproduct.pid WHERE mods='SE' GROUP BY invno) AS endata WHERE date BETWEEN '$fdate' AND '$tdate'";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$output.='<tr><th>Package_Description</th><th>Receiver_Name</th><th>Receiver_Address</th><th>Receiver_Number</th><th>Receiver_City</th><th>Receiver_Zone</th><th>Recipient_Email</th><th>Price</th><th>Instructions</th><th>Qty</th><th>Date</th></tr>';
while($row = mysqli_fetch_array($result) ){
$output.='<tr><td>'.$row['Package_Description'].'</td><td>'.$row['Receiver_Name'].'</td><td>'.$row['Receiver_Address'].'</td><td>'.$row['Receiver_Number'].'</td><td>'.$row['Receiver_City'].'</td><td>'.$row['Receiver_Zone'].'</td><td>'.$row['Recipient_Email'].'</td><td>'.$row['Price'].'</td><td>'.$row['Instructions'].'</td><td>'.$row['qty'].'</td><td>'.$row['date'].'</td></tr>';	
}
// encoding array to json format
echo $output;
}

if(isset($_POST['seldata'])){
$edata=array(check_price('P'),check_serial('S'));
echo json_encode($edata);
}

if(isset($_POST['addsales'])){
$invno = gen_newinvno('tbl_sales','SEL');
$apdate = date('Y-m-d');
$nxtdt = date('Y-m-d');
  

if($_POST['cusid']==""){
  echo json_encode(array(
    'status' => 'error',
    'message'=> 'Please Select A Customer'
  )); 
  return;
  exit;  
}
$cdata=explode('_',remove_junk(escape($_POST['cusid'])));

$cty=$cdata['0'];    
$cid = $cdata['1'];    
 

if($cid!=0){
if($cty=='CU'){    
$cname=get_fild_data('tbl_customer',$cid,'name');
$cmobile=get_fild_data('tbl_customer',$cid,'cnumber'); 
$climit=get_fild_data('tbl_customer',$cid,'creditlimit');  
$cusdebit=get_fild_data('tbl_cusdebit',$cid,'debit');
$cuscredit=get_fild_data('tbl_cuscredit',$cid,'credit');
$cusbal=$cusdebit-$cuscredit;

}else{
$cname=get_fild_data('tbl_supplier',$cid,'name');
$cmobile=get_fild_data('tbl_supplier',$cid,'cnumber');     
}
$wdid='NULL';
$wzid='NULL';
$wadd='NULL';
$wemail='NULL';    
}else{
$cname=remove_junk(escape($_POST['walkname']));
$cmobile=remove_junk(escape($_POST['walkmobile']));
$wdid="'".remove_junk(escape($_POST['wdid']))."'";
$wzid="'".remove_junk(escape($_POST['wzid']))."'";
$wadd="'".remove_junk(escape($_POST['wadd']))."'"; 
$wemail="'".remove_junk(escape($_POST['wemail']))."'";     
}    
    
$baid = remove_junk(escape($_POST['baid']));    
$chkno = remove_junk(escape($_POST['chkno']));
$chkdt = remove_junk(escape($_POST['chkdt']));
if($chkno=='' || strlen($chkno)<2){$chkdt='NULL';$chkno='NULL';}//else{$chkdt="'".$chkdt."'";$chkno="'".$chkno."'";} 

$caname = remove_junk(escape($_POST['caname']));    
$chbid = remove_junk(escape($_POST['chbid']));
$caid = remove_junk(escape($_POST['caid']));
if(strlen($caname)<2){$chbid='NULL';$caid='NULL';}//else{$chbid="'".$chbid."'";$caid="'".$caid."'";}    

$mobid = remove_junk(escape($_POST['mobid']));    
$trxid = remove_junk(escape($_POST['trxid']));

if($baid!=''){
$debitid=$baid;
$details="'".'CHK_'.$chkno.'_'.$chkdt."'";    
}elseif(strlen($caname)>0){
$debitid=$caid;
$details="'".'CRD_'.$caname.'_'.$chbid."'";    
}elseif($mobid!=''){
$debitid=$mobid;
$details="'".'MOB_'.$trxid."'";    
}else{
$debitid='LE_2';
$details='NULL';    
}    
    
$pname = remove_junk(escape($_POST['pname']));    
$dipname = remove_junk(escape($_POST['dipname']));
$paddress = remove_junk(escape($_POST['paddress']));
    
$ref = remove_junk(escape($_POST['ref']));
$note = remove_junk(escape($_POST['note']));
$selp = remove_junk(escape($_POST['selp']));
if($selp=='' || $selp=='-Select-'){$selp='NULL';}else{$selp="'".$selp."'";} 

$otclient = remove_junk(escape($_POST['otclient']));
$otmobile = remove_junk(escape($_POST['otmobile']));

$adjust = remove_junk(escape($_POST['adjust']));
if($adjust==''){$adjust=0;}     
$cash = remove_junk(escape($_POST['cash']));
if($cash==''){$cash=0;}     

$itmdis=remove_junk(escape(get_seldiscount_total()));    
$disp=remove_junk(escape($_SESSION['axes_salesde'][0]['disp']));
$disamo=remove_junk(escape($_SESSION['axes_salesde'][0]['disamo']));   
$vatp=remove_junk(escape($_SESSION['axes_salesde'][0]['vatp']));
$vatamo=remove_junk(escape($_SESSION['axes_salesde'][0]['vatamo']));
$aitp=remove_junk(escape($_SESSION['axes_salesde'][0]['aitp']));
$aitamo=remove_junk(escape($_SESSION['axes_salesde'][0]['aitamo']));
$freight=remove_junk(escape($_SESSION['axes_salesde'][0]['freight']));
$otname=remove_junk(escape($_SESSION['axes_salesde'][0]['name']));
$otamo=remove_junk(escape($_SESSION['axes_salesde'][0]['others']));
$less=remove_junk(escape($_SESSION['axes_salesde'][0]['less']));
$subtot=get_sales_total();    
$gtotal=remove_junk(escape($_SESSION['axes_salesde'][0]['gtotal']));
if($disp>0){
$invdis=($disamo-$itmdis);    
}else{
$invdis=0;    
}    

if(($adjust+$cash)>=$gtotal){
$rcash=($gtotal-$adjust);
$change=(($adjust+$cash)-$gtotal);
$due=0;
$duedt='NULL';    
}else{
$rcash=$cash;
$change=0;
$due=($gtotal-($adjust+$cash));
$duedt="'".$nxtdt."'";    
}   


  
// Customer Limit Code
if(get_fild_data('tbl_setting','13','sval')==0){
  if($cty=='CU'){    
 
    $climit=get_fild_data('tbl_customer',$cid,'creditlimit');  
    $cusdebit=get_fild_data('tbl_cusdebit',$cid,'debit');
    $cuscredit=get_fild_data('tbl_cuscredit',$cid,'credit');
    $cusbal=$cusdebit-$cuscredit;
    $totallimit=$cusbal+$due;
  
  if($totallimit > $climit){
    echo json_encode(array(
    'status' => 'error',
    'message'=> 'Sorry! Customer Limit Over!!'
    ));
    return;
    exit;  
    }
  
  }


}



    
if(!isset($_SESSION['axes_sales'])){
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! No item found!!'
));
return;
exit;  
}   

if(check_validqty($brid)){
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Quantity not available!!'
));
return;
exit;     
} 

//SMS Code Start

if($cmobile>0){
$sql="SELECT * FROM tbl_sms WHERE id=1 LIMIT 1";
$smsde = mysqli_query($con,$sql);
$sms=mysqli_fetch_array($smsde);

if($sms['status']==1){
$lang=$sms['language'];
$appkey=$sms['appkey'];
$senderid=$sms['senderid'];
$type=$sms['type'];
$url=$sms['url'];
$htext=$sms['htext'];
$ftext=$sms['ftext'];

$hbtext=$sms['hbtext'];
$fbtext=$sms['fbtext'];


if($lang==1){
    $url = "$url";
    $data = [
      "api_key" => "$appkey",
      "type" => "$type",
      "contacts" => "$cmobile",
      "senderid" => "$senderid",
      "msg" => "$htext SN:$invno. Total Amount: $gtotal. Paid Amount: $cash. Due Amount: $due. $ftext.",
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);

    echo json_encode(array(
      'status' => 'error',
      'message'=> $response
      ));
    exit;
    curl_close($ch);
}else{
  $url = "$url";
  $data = [
    "api_key" => "$appkey",
    "type" => "$type",
    "contacts" => "$cmobile",
    "senderid" => "$senderid",
    "msg" => "$hbtext রসিদ নং:$invno. সর্বমোট: $gtotal. পরিশোধ: $cash. বাকি: $due. $fbtext.",
  ];
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $response = curl_exec($ch);
  curl_close($ch);

}

}
}

//SMS Code End




if(get_fild_data('tbl_setting','12','sval')==0){
if($chkno != 'NULL'){
$first = filter_var($baid, FILTER_SANITIZE_NUMBER_INT);
$acid = preg_replace("/[^[:alnum:][:space:]]/u", '', $first);
$sql="INSERT INTO tbl_cheqman(clientid,type,chkno,chkdt,amount,acid,refinv,ref,paymod,status,apdate,brid,uid,date) VALUES ('$cid','$cty','$chkno','$chkdt','$cash','$acid','$invno','$ref','REC','1','$apdate','$brid','$aid','$dtnow')";
mysqli_query($con,$sql) or die(mysqli_error($con)); 
$cash=0;
}  
} 







$sql="INSERT INTO tbl_sales (invno,refinv,type,cusid,name,	mobile,did,zid,itemdis,subtot,disp,disamo,totdis,vatp,vatamo,aitp,aitamo,otname,otamo,freight,less,adamo,total,curid,ref,note,apdate,nxtduedate,ocusname,ocusmobile,pname,pdep,paddress,waddress,wemail,rawcash,changes,selp,debitid,details,brid,uid,date) VALUES ('$invno',NULL,'$cty','$cid','$cname','$cmobile',$wdid,$wzid,'$itmdis','$subtot','$disp','$invdis','$disamo','$vatp','$vatamo','$aitp','$aitamo','$otname','$otamo','$freight','$less','$adjust','$gtotal','0','$ref','$note','$apdate',$duedt,'$otclient','$otmobile','$pname','$dipname','$paddress',$wadd,$wemail,'$cash','$change',$selp,'$debitid',$details,'$brid','$aid','$dtnow')";  
$result = mysqli_query($con,$sql) or die(mysqli_error($con));

$sid=$con->insert_id;    
$efid=mysqli_affected_rows($con);     

if($efid>0){
$max=count($_SESSION['axes_sales']);
for($i=0;$i<$max;$i++){
$pid = $_SESSION['axes_sales'][$i]['pid'];
$unqid= $_SESSION['axes_sales'][$i]['unqid'];   
$col=$_SESSION['axes_sales'][$i]['col'];
$siz=$_SESSION['axes_sales'][$i]['siz'];
$icdis=$_SESSION['axes_sales'][$i]['icdis'];
$icinvdis=$_SESSION['axes_sales'][$i]['icinvdis'];    
$cost=$_SESSION['axes_sales'][$i]['cost'];
$puqty=0;    
$qtyin=0;   
$soqty=$_SESSION['axes_sales'][$i]['qty'];
    
if(get_fild_data('tbl_setting','3','sval')==0){    
$qtyout=$_SESSION['axes_sales'][$i]['qty'];
$ploc=0;     
}else{
if(isset($_POST['sercv']) && $_POST['sercv']==0){
$qtyout=$_SESSION['axes_sales'][$i]['qty'];
$ploc=0;     
//}elseif(isset($_POST['sercv']) && $_POST['sercv']==1){
//$qtyout=0;
//$ploc=0;    
}else{
$qtyout=0;
$ploc=$_SESSION['axes_sales'][$i]['qty'];    
}       
}
    
$taxp=0;
$taxamo=0;
$idisp=$_SESSION['axes_sales'][$i]['disp'];
$idisf=$_SESSION['axes_sales'][$i]['disf'];
$disamo=$_SESSION['axes_sales'][$i]['disamo'];
$sdisp=0;
$sdisf=0;
$price=$_SESSION['axes_sales'][$i]['price'];
$isubtot=$_SESSION['axes_sales'][$i]['subtot'];
$wday=$_SESSION['axes_sales'][$i]['wday'];
$pnote=$_SESSION['axes_sales'][$i]['pnote'];    
    
$sql="INSERT INTO tbl_traproduct (type,tid,invno,refinv,pid,unqid,colid,sizid,icdis,icinvdis,cost,price,p_qty,p_in,s_qty,p_out,p_loc,taxp,taxamo,disp,disf,disamo,subtot,wday,mods,brid,waid,tramod,isinv,pnote,uid,apdate,date) VALUES ('$cty','$cid','$invno',NULL,'$pid','$unqid','$col','$siz','$icdis','$icinvdis','$cost','$price','$puqty','$qtyin','$soqty','$qtyout','$ploc','$taxp','$taxamo','$idisp','$idisf','$disamo','$isubtot','$wday','SE','$brid',NULL,NULL,'Y','$pnote','$aid','$apdate','$dtnow')";
    
$result = mysqli_query($con,$sql) or die(mysqli_error($con));    
}

if(isset($_SESSION['axes_selse'])){
$max=count($_SESSION['axes_selse']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['axes_selse'][$i]['pid'];
$unqid=$_SESSION['axes_selse'][$i]['unqid'];    
$serial=$_SESSION['axes_selse'][$i]['imei'];
if(get_fild_data('tbl_setting','3','sval')==0){$dli='Y';}else{if(isset($_POST['sercv']) && $_POST['sercv']==0){$dli='Y';}else{$dli='N';}}    
$sql="UPDATE tbl_serial SET unqid='$unqid',status='1' WHERE serial='$serial'";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$sql="INSERT INTO tbl_serialsale (invno,pid,unqid,serial,mods,dli) VALUES ('$invno','$pid','$unqid','$serial','S','$dli')";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));    
}    
}

//sales_update($sid,'tbl_sales',$dtnow);   
unset($_SESSION['axes_sales']);
unset($_SESSION['axes_salesde']);
if(isset($_SESSION['axes_selse'])){
unset($_SESSION['axes_selse']);
}    
    
$act =remove_junk(escape('Sales Invoice: '.$invno));    
write_activity($aid,'SEL','New sales invoice has been Created',$act);
echo json_encode(array(
'status' => 'success',
'message'=> 'Sales Save Successfully!!',
'invid'=> $sid    
));
exit;    
}else{
  echo json_encode(array(
  'status' => 'error',
  'message'=> 'Sorry! Data Fail to Saved!!'
  ));    
}    
    
}


if(isset($_POST['savecus'])){
$name = ucwords(remove_junk(escape($_POST['name'])));
$code = get_genid('CU');
$cperson = remove_junk(escape($_POST['cperson']));
$cnumber = remove_junk(escape($_POST['cnumber']));
$cemail = remove_junk(escape($_POST['cemail']));    
$address = remove_junk(escape($_POST['address']));
$did = remove_junk(escape($_POST['did']));
if($did!=''){$did="'".$did."'";}else{$did='NULL';}
$zid = remove_junk(escape($_POST['zid']));
if($zid!=''){$zid="'".$zid."'";}else{$zid='NULL';}    
if(isset($_POST['cnumber'])){
$ducode = mysqli_query($con,"SELECT * FROM tbl_customer WHERE cnumber = '$cnumber'");
}
	
if($ducode->num_rows > 0) {
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! This contact number already exists!!'
));
return;
exit;
}else{
$sql="INSERT INTO tbl_customer(code,name,cperson,cnumber,cemail,address,did,zid,uid,date) VALUES ('$code','$name','$cperson','$cnumber','$cemail','$address',$did,$zid,'$aid','$dtnow')";    
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$sid=$con->insert_id;    
$efid=mysqli_affected_rows($con);    
if($efid>0){
$act =remove_junk(escape('Customer name: '.$name));    
write_activity($aid,'CUS','New customer has been Added',$act);
echo json_encode(array(
'status' => 'success',
'cbal'=> '0.00',
'cudata'=> $code.'|'.$name.'|'.$cnumber,
'cuid'=> 'CU_'.$sid    
));
exit;   
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Customer Fail to Saved!!'
));    
} 
}    
}
?>

<?php if(isset($_POST['addcus'])){ ?>
<div class="col-md-12 popup_details_div addcustomer">
<div class="row">
<div id="server-results"><!-- For server results --></div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" maxlength="45" value="" id="name" class="form-control" placeholder="e.g. Md.Sumon Rahman"/>
<input type="hidden" name="savecus" readonly />    
</div>    
<div class="form-group">
<label>Contact Name</label>    
<input type="text" name="cperson" maxlength="45" value="" id="cperson" class="form-control" placeholder="e.g. Md.Rahman Sumon(CEO)"/>
</div>
<div class="form-group">
<label>Contact Number</label>    
<input type="text" name="cnumber" maxlength="18" value="" id="cnumber" class="form-control" placeholder="e.g. +88016161xxxxx70"/>
</div>
<div class="form-group">
<label>Contact Email</label>    
<input type="text" name="cemail" maxlength="45" value="" id="cemail" class="form-control" placeholder="e.g. info@axesgl.com"/>
</div>
<div class="form-group">    
<label>Address</label>
<textarea class="form-control" name="address" id="address" maxlength="250" rows="5" placeholder="Address"></textarea>    
</div>
<div class="row">
<div class="col-md-6">    
<div class="form-group">
<label>Select District</label>
<select class="form-control select2" name="did" id="did" onchange="getAllSubgroup(this.value)">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_district ORDER BY name ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
</select>    
</div>
</div>
<div class="col-md-6">    
<div class="form-group">
<label>Select Zone</label>
<select class="form-control select2" name="zid" id="zid">
<option value="">-Select-</option>    
</select>     
</div>
</div>    
</div>    
</div>
<div class="col-md-1"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-2 text-right" >
<input type="button" id="adcus" class="btn btn-flat bg-purple btn-sm " value="Save"/>
</div>
<div class="col-md-2"></div>    
</div>

<script type="text/javascript">
function chek_error(){
var name = $('#name').val();
var cnumber = $('#cnumber').val();
var address = $('#address').val();    
var cemail = $('#cemail').val();    
var result = true;
    
var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,8}|[0-9]{1,3})(\]?)$/;
    
$('.LV_validation_message').remove();
    
if(name.length<=0){
$('#name').addClass('LV_invalid_field');   
$('#name').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); } else {
$('#name').removeClass('LV_invalid_field'); 
}    
    
if(cnumber.length<=0){
$('#cnumber').addClass('LV_invalid_field');   
$('#cnumber').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); } else {
$('#cnumber').removeClass('LV_invalid_field'); 
}    

if(address.length<=0){
$('#address').addClass('LV_invalid_field');   
$('#address').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); } else {
$('#address').removeClass('LV_invalid_field'); 
}    
    
if(cemail.length >0){
result = expr.test(cemail);
if(result){
$('#cemail').removeClass('LV_invalid_field');        
}else{
$('#cemail').addClass('LV_invalid_field');
$('#cemail').after("<span class='LV_validation_message LV_invalid'>Not a valid email address!</span>").addClass('has-error');    
}    
}    
    
if(name.length<=0 || address.length<=0 || cnumber.length<=0 || !result){
return false;    
}else{
return true;     
}    
}

function getAllSubgroup(id){
$.ajax({
url: 'axe_getsub.php',
type: 'post',
data: {zone : id},
success:function(data) {
$('#zid').html(data);
}
});
};     
    
$(document).on('blur', '#name, #cnumber, #cemail', function() {
chek_error();    
});        
</script>    
<?php } ?>
