<?php
function innerMenu(){
	$str = '
			<li class="active"><a href="'.SITEURL.'/bill/dashboard"><i class="nc-icon nc-bank"></i><p>Dashboard</p></a></li>
			<!--<li><a href="'.SITEURL.'/bill/add-category"><i class="nc-icon nc-tile-56"></i><p>Add Unit</p></a></li>-->
			<li><a href="'.SITEURL.'/bill/add-product"><i class="nc-icon nc-globe"></i><p>Add Product</p></a></li>
			<!--<li><a href="'.SITEURL.'/bill/add-customer"><i class="nc-icon nc-single-02"></i><p>Add Customer</p></a></li>-->
			<li><a href="'.SITEURL.'/bill/add-bill"><i class="nc-icon nc-paper"></i><p>Add Bill</p></a></li>
			<!--<li><a href="'.SITEURL.'/bill/add-bank"><i class="nc-icon nc-paper"></i><p>Add Bank</p></a></li>-->
			<li><a href="'.SITEURL.'/bill/change-password"><i class="nc-icon nc-paper"></i><p>Change Password</p></a></li>
			<li class="navbar dropdown"><a href="#" class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="nc-icon nc-single-copy-04"></i><p>Get Report</p></a>
			  <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
			  	<li><a href="'.SITEURL.'/report/stock-report">Stock Report</a></li>
				<li><a href="'.SITEURL.'/report/sale-report">Sales Report</a></li>
				<li><a href="'.SITEURL.'/report/product-wise-sales">Product Wise Sales</a></li>
				<li><a href="'.SITEURL.'/report/commission-report">Commission Report</a></li>
			  </ul>
			</li>
			<!--<li><a href="'.SITEURL.'/bill/purchase-order"><i class="nc-icon nc-paper"></i><p>Purchase Order</p></a></li>-->';
	return $str;
}
function getGallery(){
	global $DB;
	$sql = "SELECT * FROM ".$DB->pre."home_gallery WHERE status=1";
	$data = $DB->dbRows($sql);
	$str = "";
	foreach($data as $v){
		$str .='<a class="fancybox" rel="gallery1" href="'.SITEURL.'/uploads/home_gallery/'.$v['homeImg'].'">
			<img src="'.SITEURL.'/core/image.inc.php?path=home_gallery/'.$v['homeImg'].'&w=720&h=478&type=crop'.'" alt="" />
			</a>';
	}
	return $str;
}

function addToCart1111(){
	@session_start();
	global $DB;
	$data = json_decode($_REQUEST['productID']);
	$productID = $data->productID;
	$sql = 'SELECT P.*,C.categoryTitle FROM '.$DB->pre.'product as P 
			LEFT JOIN '.$DB->pre.'category as C on P.categoryID = C.categoryID
			WHERE P.productID='.$data->productID;
	$d = $DB->dbRow($sql);
	
	if(!isset($_SESSION["PRODUCTCART"]))
			$_SESSION['PRODUCTCART'] = array();
			
	$productQty = intval($_REQUEST['qty']);
	$discountPrice = $d["prodDistPrice"];
	$productPrice = $d["prodPrice"];
	$productName = $d['prodTitle'];
	$categoryTitle = $d['categoryTitle'];
	
	if(count($_SESSION["PRODUCTCART"]) > 0){
			$qty = $_SESSION["PRODUCTCART"][$productID]['productQty'];
			$_SESSION["PRODUCTCART"][$productID] = array("productQty"=>($productQty), "productID"=>$productID, "categoryTitle"=>$categoryTitle, "productName"=>$productName, "productPrice"=>$productPrice, "discountPrice"=>$discountPrice, "productImage"=>$d['prodImg1']);
		}else{
			$_SESSION["PRODUCTCART"][$productID] = array("productQty"=>($productQty), "productID"=>$productID, "categoryTitle"=>$categoryTitle, "productName"=>$productName, "productPrice"=>$productPrice, "discountPrice"=>$discountPrice,   "productImage"=>$d['prodImg1']);
		}
	
}
function addBill(){
	putenv("TZ=Asia/Calcutta");
	global $DB;
	@session_start();
	$tot = 0;
	foreach($_REQUEST['bproduct'] as $k=>$P){
			if($P){
				$tot += $_REQUEST['bTot'][$k];
			}
	}
	
	$sqlR = 'SELECT regID FROM '.$DB->pre.'user_reg WHERE userCode="'.$_REQUEST['custUserCode'].'"';
	$R = $DB->dbRow($sqlR);
	
	
	$DB->table = $DB->pre."transaction";		
	$DB->data  = array('tTotal'=>$tot,'invoiceNo'=>$_REQUEST['invoiceNo'],'custID'=>$R['regID'],'paid'=>$_REQUEST['paid'],'balance'=>$_REQUEST['balance'],'cgst'=>$_REQUEST['cgst'],'sgst'=>$_REQUEST['sgst'],'gTotal'=>$_REQUEST['gTotal'],'dateAdded'=>date('Y-m-d H:i:s'));
	$DB->dbInsert();
	$billID = $DB->insertID; 								
	
	$bv = 0;
	//addLevelPayout(4.3,25,'RVS112');die;
	foreach($_REQUEST['bproduct'] as $k=>$P){
		if($P){
			$DB->table = $DB->pre."bill_table";		
			$DB->data  = array('tranBillID'=>$billID,'userID'=>$_SESSION['userID'],'userCode'=>$_REQUEST['custUserCode'],'productID'=>$P,'quantity'=>$_REQUEST['bProdQty'][$k],'price'=>$_REQUEST['bProdPrice'][$k],'total'=>$_REQUEST['bTot'][$k],'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
			
			addSponcerPayout($_REQUEST['custUserCode'],$R['regID'],$P,$_REQUEST['bProdQty'][$k],$_REQUEST['invoiceNo']);
			
			$bv += getBV($P,$_REQUEST['bProdQty']);
			
			$sqlS = 'SELECT fProdQty FROM '.$DB->pre.'franchise_product WHERE franchiseProdID="'.$P.'"';
			$S = $DB->dbRow($sqlS);

			$sqlU = 'UPDATE '.$DB->pre.'franchise_product SET fProdQty="'.($S['fProdQty']-$_REQUEST['bProdQty'][$k]).'" WHERE franchiseProdID="'.$P.'"';
			$DB->dbQuery($sqlU);
		}
	
	$str = array('str'=>'OK','billID'=>$billID);
	}
	 $distBV = (($bv/100)*3);//addLevelPayout($distrAmt,$orderID,$userCode)
	
	$sqlB = 'INSERT INTO '.$DB->pre.'user_bv SET regID="'.$R['regID'].'", userCode="'.$_REQUEST['custUserCode'].'", bv="'.$bv.'",dateAdded="'.date('Y-m-d H:i:s').'"';
	$DB->dbQuery($sqlB);
	
	activateUser($bv,$_REQUEST['custUserCode']);
	addLevelPayout($distBV,$_REQUEST['invoiceNo'],$_REQUEST['custUserCode']);
	
	addShopPayout($tot,$_REQUEST['invoiceNo']);
	return json_encode($str);
}
function addSponcerPayout($fromUsercode,$fromRegID,$productID,$qty,$orderID){
	global $DB;
	$sql = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$fromRegID.'"';
	$D = $DB->dbRow($sql);
	
	$sqlID2 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D['referenceNo'].'" AND isActivate="1"';
	$ID2 = $DB->dbRow($sqlID2);
	
	
	
	if($ID2['regID'] != ''){
		
		$sqlC = 'SELECT walletID FROM '.$DB->pre.'sponcer_wallet WHERE regID="'.$ID2['userID'].'" AND txnID="'.$orderID.'" AND productID="'.$productID.'"';
		$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				
				
				$sqlP = 'SELECT directBonus FROM '.$DB->pre.'product WHERE productID="'.$productID.'"';
				$P1 = $DB->dbRow($sqlP);
				
				$amt = ($P1['directBonus']*$qty);
				 $tds = (($amt/100)*5);
				 $admin = (($amt/100)*10);
				 $actAmt = ($amt-($admin+$tds));
				
				
			    $sqlI = 'INSERT INTO '.$DB->pre.'sponcer_wallet SET userCode="'.$ID2['userCode'].'", regID="'.$ID2['regID'].'",fromUserCode="'.$fromUsercode.'",fromRegID="'.$fromRegID.'",productID="'.$productID.'",txnID="'.$orderID.'",payout="'.$actAmt.'",adminChrgs="'.$admin.'",tdsChrgs="'.$tds.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateSponcerBalance($ID2['regID'],$actAmt);
			}
		
	}
}
function addShopPayout($tot,$orderID){
	global $DB;
	
	
	 $actAmt = $tot;
	 
	 if($C['walletID'] == ''){
		$sqlI = 'INSERT INTO '.$DB->pre.'shop_wallet SET  regID="'.$_SESSION['userID'].'",txnID="'.$orderID.'",payout="'.$actAmt.'",adminChrgs="'.$admin.'",tdsChrgs="'.$tds.'",dateAdded="'.date('Y-m-d H:i:s').'"';
		$DB->dbQuery($sqlI);					
		updateShopBalance($_SESSION['userID'],$actAmt);
	}
}
function activateUser($bv,$userCode){
	global $DB;
	$sql = 'SELECT regID FROM '.$DB->pre.'user_reg WHERE status="1" AND userCode="'.$userCode.'" AND isActivate="0"';
	$D = $DB->dbRow($sql);
	
	if($D['regID'] == ''){
			$sqlW = 'SELECT sum(bv) as S FROM '.$DB->pre.'user_bv WHERE regID="'.$D['regID'].'"';
			$W = $DB->dbRow($sqlW);
			
			if($W['S'] >= 500){
				$sqlU = 'UPDATE '.$DB->pre.'user_reg SET isActivate="1" WHERE regID="'.$D['regID'].'"';
				$DB->dbQuery($sqlU);
			}
	}
	
	
}
function getBV($prodID,$qty){
	global $DB;
	$sql = 'SELECT bv FROM '.$DB->pre.'product WHERE productID="'.intval($prodID).'"';
	$D = $DB->dbRow($sql);
	$bv = (intval($D['bv'])*intval($qty));
	return ($bv);
}
function addLevelPayout($distrAmt,$orderID,$userCode){
	global $DB;
	
	
	$sqlR = 'SELECT regID FROM '.$DB->pre.'user_reg WHERE userCode="'.$userCode.'"';
	$R = $DB->dbRow($sqlR);
	

	
	//Level-1
	$sql2 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$R['regID'].'"';
	$D2 = $DB->dbRow($sql2);
	
	//addRank($D['referenceNo']);
	
	$sqlID2 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D2['referenceNo'].'" AND isActivate="1"';
	$ID2 = $DB->dbRow($sqlID2);
	
	if($D2['referenceNo'] != ''){
		
			 $amt2 = $distrAmt;
			 $tds2 = (($amt2/100)*5);
			 $admin2 = (($amt2/100)*10);
			 $actAmt2 = ($amt2-($admin2+$tds2));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID2['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			
			if($C['walletID'] == ''){
			    $sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID2['userCode'].'", regID="'.$ID2['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt2.'",adminChrgs="'.$admin2.'",tdsChrgs="'.$tds2.'",atLevel="1",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID2['regID'],$actAmt2);
			}
	
	}
	
	
	//Level-2
	$sql3 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID2['regID'].'"';
	$D3 = $DB->dbRow($sql3);
	
	$sqlID3 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D3['referenceNo'].'"';
	$ID3 = $DB->dbRow($sqlID3);
	
	if($D3['referenceNo'] != ''){
		
			 $amt3 = $distrAmt;
			$tds3 = (($amt3/100)*5);
			$admin3 = (($amt3/100)*10);
			$actAmt3 = ($amt3-($admin3+$tds3));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID3['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID3['userCode'].'",atLevel="2", regID="'.$ID3['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt3.'",adminChrgs="'.$admin3.'",tdsChrgs="'.$tds3.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID3['regID'],$actAmt3);
			}
	
	}
	
	
	//Level-3
	$sql4 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID3['regID'].'"';
	$D4 = $DB->dbRow($sql4);
	
	$sqlID4 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D4['referenceNo'].'"';
	$ID4 = $DB->dbRow($sqlID4);
	
	if($D4['referenceNo'] != ''){
		
			 $amt4 = $distrAmt;
			$tds4 = (($amt4/100)*5);
			$admin4 = (($amt4/100)*10);
			$actAmt4 = ($amt4-($admin4+$tds4));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID4['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID4['userCode'].'",atLevel="3", regID="'.$ID4['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt4.'",adminChrgs="'.$admin4.'",tdsChrgs="'.$tds4.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID4['regID'],$actAmt4);
			}
	
	}
	
	//Level-4
	$sql5 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID4['regID'].'"';
	$D5 = $DB->dbRow($sql5);
	
	$sqlID5 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D5['referenceNo'].'"';
	$ID5 = $DB->dbRow($sqlID5);
	
	if($D5['referenceNo'] != ''){
		
			 $amt5 = $distrAmt;
			$tds5 = (($amt5/100)*5);
			$admin5 = (($amt5/100)*10);
			$actAmt5 = ($amt5-($admin5+$tds5));
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID5['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID5['userCode'].'",atLevel="4", regID="'.$ID5['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt5.'",adminChrgs="'.$admin5.'",tdsChrgs="'.$tds5.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID5['regID'],$actAmt5);
			}
	
	}
	
	//Level-5
	$sql6 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID5['regID'].'"';
	$D6 = $DB->dbRow($sql6);
	
	$sqlID6 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D6['referenceNo'].'"';
	$ID6 = $DB->dbRow($sqlID6);
	
	if($D6['referenceNo'] != ''){
		
			 $amt6 = $distrAmt;
			$tds6 = (($amt6/100)*5);
			$admin6 = (($amt6/100)*10);
			$actAmt6 = ($amt6-($admin6+$tds6));
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID6['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID6['userCode'].'",atLevel="5", regID="'.$ID6['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt6.'",adminChrgs="'.$admin6.'",tdsChrgs="'.$tds6.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID6['regID'],$actAmt6);
			}
	
	}
	
	//Level-6
	$sql7 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID6['regID'].'"';
	$D7 = $DB->dbRow($sql7);
	
	$sqlID7 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D7['referenceNo'].'"';
	$ID7 = $DB->dbRow($sqlID7);
	
	if($D7['referenceNo'] != ''){
		
			 $amt7 = $distrAmt;
			$tds7 = (($amt7/100)*5);
			$admin7 = (($amt7/100)*10);
			$actAmt7 = ($amt7-($admin7+$tds7));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID7['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID7['userCode'].'",atLevel="6", regID="'.$ID7['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt7.'",adminChrgs="'.$admin7.'",tdsChrgs="'.$tds7.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID7['regID'],$actAmt7);
			}
	
	}
	
	//Level-7
	$sql8 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID7['regID'].'"';
	$D8 = $DB->dbRow($sql8);
	
	$sqlID8 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D8['referenceNo'].'"';
	$ID8 = $DB->dbRow($sqlID8);
	
	if($D8['referenceNo'] != ''){
		
			 $amt8 = $distrAmt;
			$tds8 = (($amt8/100)*5);
			$admin8 = (($amt8/100)*10);
			$actAmt8 = ($amt8-($admin8+$tds8));
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID8['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID8['userCode'].'",atLevel="7",regID="'.$ID8['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt8.'",adminChrgs="'.$admin8.'",tdsChrgs="'.$tds8.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID8['regID'],$actAmt8);
			}
	
	}
	
	
	//Level-8
	$sql9 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID8['regID'].'"';
	$D9 = $DB->dbRow($sql9);
	
	$sqlID9 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D9['referenceNo'].'"';
	$ID9 = $DB->dbRow($sqlID9);
	
	if($D9['referenceNo'] != ''){
		
			 $amt9 = $distrAmt;
			$tds9 = (($amt9/100)*5);
			$admin9 = (($amt9/100)*10);
			$actAmt9 = ($amt9-($admin9+$tds9));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID9['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID9['userCode'].'",atLevel="8", regID="'.$ID9['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt9.'",adminChrgs="'.$admin9.'",tdsChrgs="'.$tds9.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID9['regID'],$actAmt9);
			}
	
	}
	
	//Level-9
	$sql10 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID9['regID'].'"';
	$D10 = $DB->dbRow($sql10);
	
	$sqlID10 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D10['referenceNo'].'"';
	$ID10 = $DB->dbRow($sqlID10);
	
	if($D10['referenceNo'] != ''){
		
			 $amt10 = $distrAmt;
			$tds10 = (($amt10/100)*5);
			$admin10 = (($amt10/100)*10);
			$actAmt10 = ($amt10-($admin10+$tds10));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID10['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID10['userCode'].'",atLevel="9",regID="'.$ID10['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt10.'",adminChrgs="'.$admin10.'",tdsChrgs="'.$tds10.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID10['regID'],$actAmt10);
			}
	
	}
	
	//Level-10
	/*$sql11 = 'SELECT referenceNo FROM '.$DB->pre.'user_reg WHERE regID="'.$ID10['regID'].'"';
	$D11 = $DB->dbRow($sql11);
	
	$sqlID11 = 'SELECT regID,userCode FROM '.$DB->pre.'user_reg WHERE userCode="'.$D11['referenceNo'].'"';
	$ID11 = $DB->dbRow($sqlID11);
	
	if($D11['referenceNo'] != ''){
		
			 $amt11 = $distrAmt;
			$tds11 = (($amt11/100)*5);
			$admin11 = (($amt11/100)*10);
			$actAmt11 = ($amt11-($admin11+$tds11));
			
			
			$sqlC = 'SELECT walletID FROM '.$DB->pre.'wallet WHERE regID="'.$ID11['regID'].'" AND txnID="'.$orderID.'"';
			$C = $DB->dbRow($sqlC);
			
			if($C['walletID'] == ''){
				$sqlI = 'INSERT INTO '.$DB->pre.'wallet SET userCode="'.$ID11['userCode'].'",atLevel="10",regID="'.$ID11['regID'].'",txnID="'.$orderID.'",payout="'.$actAmt11.'",adminChrgs="'.$admin11.'",tdsChrgs="'.$tds11.'",dateAdded="'.date('Y-m-d H:i:s').'"';
				$DB->dbQuery($sqlI);
								
				updateBuyerBalance($ID11['regID'],$actAmt11);
			}
	
	}*/
	
	
	
	
}
function updateBuyerBalance($regID,$amt){
			global $DB;
			$sqlB = 'SELECT balAmt FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE   regID="'.$regID.'")';
			$B = $DB->dbRow($sqlB);
			$bal = ($B['balAmt']+$amt);
			
			$DB->table = $DB->pre."balance";
			$DB->data  = array('regID'=>$regID,'balAmt'=>$bal,'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
}
function updateSponcerBalance($regID,$amt){
			global $DB;
			$sqlB = 'SELECT balAmt FROM '.$DB->pre.'sponcer_balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'sponcer_balance WHERE   regID="'.$regID.'")';
			$B = $DB->dbRow($sqlB);
			$bal = ($B['balAmt']+$amt);
			
			$DB->table = $DB->pre."sponcer_balance";
			$DB->data  = array('regID'=>$regID,'balAmt'=>$bal,'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
}
function updateShopBalance($regID,$amt){
			global $DB;
			$sqlB = 'SELECT balAmt FROM '.$DB->pre.'shop_balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'shop_balance WHERE   regID="'.$regID.'")';
			$B = $DB->dbRow($sqlB);
			$bal = ($B['balAmt']+$amt);
			
			$DB->table = $DB->pre."shop_balance";
			$DB->data  = array('regID'=>$regID,'balAmt'=>$bal,'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
}

/*function getInfo(){
	global $DB;
	@session_start();
	//$sql = 'SELECT * FROM '.$DB->pre.'franchise_product WHERE productID = "'.$_REQUEST['productID'].'" AND userID="'.$_SESSION['userID'].'"';
	//$D = $DB->dbRow($sql);
	
	$sql1 = 'SELECT * FROM '.$DB->pre.'product WHERE productID = "'.$_REQUEST['productID'].'"';
	$D1 = $DB->dbRow($sql1);
	$str = array('str'=>'ERR');
	if($D1 > 0){
		$str = array('str'=>'OK','price'=>$D1['productPrice'],"stock"=>$D1['stock']);
	}
	return json_encode($str);
}*/

function getProdInfo(){
	global $DB;
	@session_start();
	$sql = 'SELECT * FROM '.$DB->pre.'franchise_product WHERE productID = "'.$_REQUEST['productID'].'" AND userID="'.$_SESSION['userID'].'"';
	$D = $DB->dbRow($sql);
	
	$str = array('str'=>'ERR');
	if($D > 0){
		$str = array('str'=>'OK','price'=>$D['fProdPrice'],"stock"=>$D['fProdQty']);
	}
	return json_encode($str);
}
function getInvoiceDetail(){
	global $DB;
	 $sql = 'SELECT * FROM '.$DB->pre.'transaction WHERE transID = "'.$_REQUEST['tID'].'"';
	$T = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if($T > 0){
		$str = array('str'=>'OK','total'=>$T['total'],'balance'=>$T['balance']);
	}
	return json_encode($str);
}
function chkQty(){
	global $DB;
	$sql = 'SELECT availQty FROM '.$DB->pre.'product WHERE prodID = "'.$_REQUEST['productID'].'"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if($D['availQty'] > $_REQUEST['qty']){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function addProd(){
	global $DB;
	$str = 'ERR';
	foreach($_REQUEST['bproduct'] as $k=>$P){
		if($P){
			$DB->table = $DB->pre.'order_details';
			$DB->data = array('productID'=>$P,'userID'=>$_REQUEST['userID'],'prodQty'=>$_REQUEST['bProdQty'][$k],'prodPrice'=>$_REQUEST['bProdPrice'][$k],'prodTotal'=>$_REQUEST['bTot'][$k],'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
		}
		$str = 'OK';
	}
	return $str;
	
}
function addCat(){
	global $DB;
	$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
	$DB->table = $DB->pre."category";		
	$DB->data  = $_REQUEST;
	$str = 'ERR';
	if($DB->dbInsert()){
		$str = 'OK';
	}
	return $str;
	
}

function updateCust(){
	global $DB;
	$_REQUEST['dateModified'] = date('Y-m-d H:i:s');
	$custID = sprintf("%d",$_REQUEST["custID"]);
	$DB->table = $DB->pre."customer";
	$DB->data  = $_REQUEST;
	$str = array('str'=>'ERR');
	if($DB->dbUpdate("custID='$custID'")){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
	
}

function updateProd(){
	global $DB;
	$_REQUEST['dateModified'] = date('Y-m-d H:i:s');
	$_REQUEST['mfgDate'] = date('Y-m-d',strtotime($_REQUEST['mfgDate']));
	$_REQUEST['expDate'] = date('Y-m-d',strtotime($_REQUEST['expDate']));
	$prodID = sprintf("%d",$_REQUEST["prodID"]);
	$DB->table = $DB->pre."product";
	$DB->data  = $_REQUEST;
	$str = array('str'=>'ERR');
	if($DB->dbUpdate("prodID='$prodID'")){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
	
}

function updateCat(){
	global $DB;
	$_REQUEST['dateModified'] = date('Y-m-d H:i:s');
	$categoryID = sprintf("%d",$_REQUEST["categoryID"]);
	$DB->table = $DB->pre."category";
	$DB->data  = $_REQUEST;
	$str = array('str'=>'ERR');
	if($DB->dbUpdate("categoryID='$categoryID'")){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
	
}

function addCust(){
	global $DB;
	$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
	$DB->table = $DB->pre."customer";		
	$DB->data  = $_REQUEST;
	$str = 'ERR';
	if($DB->dbInsert()){
		$str = 'OK';
	}
	return $str;
	
}
function addExpence(){
	global $DB;
	$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
	$DB->table = $DB->pre."expence_for";		
	$DB->data  = $_REQUEST;
	$str = 'ERR';
	if($DB->dbInsert()){
		$str = 'OK';
	}
	return $str;
	
}
function payUpdate(){
	global $DB;
	@session_start();
	$sqlG = 'SELECT * FROM '.$DB->pre.'transaction WHERE invoiceNo = "'.$_REQUEST['invoiceNo'].'"';
	$G = $DB->dbRow($sqlG);
	$sqlU = 'UPDATE '.$DB->pre.'transaction SET paid = "'.($G['paid']+$_REQUEST['amount']).'" , balance = "'.($G['balance']-$_REQUEST['amount']).'" WHERE invoiceNo = "'.$_REQUEST['invoiceNo'].'"';
	$DB->dbQuery($sqlU);
	
	$DB->table = $DB->pre."bill_track";	
	$DB->data  = array('invoiceNo'=>$_REQUEST['invoiceNo'],'amount'=>$_REQUEST['amount'],'unitID'=>$_SESSION['unitID'],'userID'=>$_SESSION['userID'],'dateAdded'=>date('Y-m-d H:i:s'));	
	//$DB->dbInsert();
	$str = array('str'=>'ERR');
	if($DB->dbInsert()){
		$str = array('str'=>'OK','invoiceNo'=>$_REQUEST['invoiceNo'],'amount'=>$_REQUEST['amount'],'dateAdded'=>date('Y-m-d H:i:s'));
	}
	return json_encode($str);
}
function delProd(){
	global $DB;
	$sql = "UPDATE ".$DB->pre."product SET dateModified = '".date('Y-m-d H:i:s')."',status = '0' WHERE prodID = '".$_REQUEST['prodID']."'" ;
	$str = "ERR";
	if($DB->dbQuery($sql)){
		$str = 'OK';
	}
	return $str;
}
function trashProd(){
	global $DB;
	$sql = "UPDATE ".$DB->pre."product SET dateModified = '".date('Y-m-d H:i:s')."',status = '1' WHERE prodID = '".$_REQUEST['prodID']."'" ;
	$str = "ERR";
	if($DB->dbQuery($sql)){
		$str = 'OK';
	}
	return $str;
	
}
function delCust(){
	global $DB;
	$sql = "UPDATE ".$DB->pre."customer SET dateModified = '".date('Y-m-d H:i:s')."',status = '0' WHERE custID = '".$_REQUEST['custID']."'" ;
	$str = "ERR";
	if($DB->dbQuery($sql)){
		$str = 'OK';
	}
	return $str;
}
function trashCust(){
	global $DB;
	$sql = "UPDATE ".$DB->pre."customer SET dateModified = '".date('Y-m-d H:i:s')."',status = '1' WHERE custID = '".$_REQUEST['custID']."'" ;
	$str = "ERR";
	if($DB->dbQuery($sql)){
		$str = 'OK';
	}
	return $str;
}

function delCat(){
	global $DB;
	$sql = "UPDATE ".$DB->pre."category SET dateModified = '".date('Y-m-d H:i:s')."',status = '0' WHERE categoryID = '".$_REQUEST['catID']."'" ;
	$str = "ERR";
	if($DB->dbQuery($sql)){
		$str = 'OK';
	}
	return $str;
}
function trashCat(){
	global $DB;
	$sql = "UPDATE ".$DB->pre."category SET dateModified = '".date('Y-m-d H:i:s')."',status = '1' WHERE categoryID = '".$_REQUEST['catID']."'" ;
	$str = "ERR";
	if($DB->dbQuery($sql)){
		$str = 'OK';
	}
	return $str;
}
function chkUsercode(){
	global $DB;
	$sql = 'SELECT name FROM '.$DB->pre.'user_reg WHERE userCode = "'.strtoupper($_REQUEST['userCode']).'"';
	$U = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if(count($U) > 0){
		$str = array('str'=>'OK','name'=>$U['name']);
	}
	return json_encode($str);
}
function chkPass(){
	global $DB;
	@session_start();
	$sql = 'SELECT * FROM '.$DB->pre.'admin_user WHERE userPass="'.md5($_REQUEST['password']).'" AND userID="'.$_REQUEST['userID'].'" AND status="1"';
	$DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if($DB->numRows > 0){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function changePass(){
	global $DB;
	@session_start();
	//$_REQUEST["dateModified"] = date("Y-m-d H:i:s");
	$userID = sprintf("%d",$_REQUEST["userID"]);
	$DB->table = $DB->pre."admin_user";
	$DB->data  = array('userPass'=>md5($_REQUEST['nPass']));
	$str = array('str'=>'ERR');
	if($DB->dbUpdate("userID='$userID'")){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function getDist(){
	global $DB;
	$where = ' AND stateID="'.$_REQUEST['stateID'].'"';
	return getTableDD($DB->pre.'district','districtID','district','','  status="1" '.$where);
}
function getTaluka(){
	global $DB;
	
	$where = ' AND districtID="'.$_REQUEST['districtID'].'"';
	return getTableDD($DB->pre.'taluka','talukaID','taluka','',' status="1"'.$where);
}
function addBank(){
	global $DB;
	$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');
	$DB->table = $DB->pre.'bank';
	$DB->data = $_REQUEST;
	$str = 'ERR';
	if($DB->dbInsert()){
		$str = 'OK';
	}
	return $str;
}
switch($_REQUEST['xAction']){
	case 'getPageData':
					include("../../connectdb.inc.php");
					echo getPageData();
					break;
	case 'addToCart1111':
					include("../../connectdb.inc.php");
					echo addToCart();
					break;
	case 'bill':
					include("../../connectdb.inc.php");
					echo addBill();
					break;
	case 'expence':
					include("../../connectdb.inc.php");
					echo addExpence();
					break;
	case 'info':
					include("../../connectdb.inc.php");
					echo getProdInfo();
					break;
	case 'prodInfo':
					include("../../connectdb.inc.php");
					echo getInfo();
					break;
	case 'chkQty':
					include("../../connectdb.inc.php");
					echo chkQty();
					break;
	case 'addProd':
					include("../../connectdb.inc.php");
					echo addProd();
					break;
	case 'addCat':
					include("../../connectdb.inc.php");
					echo addCat();
					break;
	case 'addCust':
					include("../../connectdb.inc.php");
					echo addCust();
					break;
	case 'getDet':
					include("../../connectdb.inc.php");
					echo getInvoiceDetail();
					break;
	case 'payup':
					include("../../connectdb.inc.php");
					echo payUpdate();
					break;
	case 'delProd':
		include("../../connectdb.inc.php");
		echo delProd();
		break;
	case 'trashProd':
		include("../../connectdb.inc.php");
		echo trashProd();
		break;
	case 'delCust':
		include("../../connectdb.inc.php");
		echo delCust();
		break;
	case 'trashCust':
		include("../../connectdb.inc.php");
		echo trashCust();
		break;
	case 'updateProd':
		include("../../connectdb.inc.php");
		echo updateProd();
		break;
	case 'chkUsercode':
		include("../../connectdb.inc.php");
		echo chkUsercode();
		break;
	case 'updateCust':
		include("../../connectdb.inc.php");
		echo updateCust();
		break;
	case 'delCat':
		include("../../connectdb.inc.php");
		echo delCat();
		break;
	case 'trashCat':
		include("../../connectdb.inc.php");
		echo trashCat();
		break;
	case 'updateCat':
		include('../../connectdb.inc.php');
		echo updateCat();
		break;
	case 'chkPass':
		include('../../connectdb.inc.php');
		echo chkPass();
		break;
	case 'changePass':
		include('../../connectdb.inc.php');
		echo changePass();
		break;
	case 'getDist':
		include('../../connectdb.inc.php');
		echo getDist();
		break;
	case 'getTaluka':
		include('../../connectdb.inc.php');
		echo getTaluka();
		break;
	case 'addBank':
		include('../../connectdb.inc.php');
		echo addBank();
		break;
	case 'purOrder':
		include('../../connectdb.inc.php');
		echo addOrder();
		break;
}
?>