<?php
//*######################MOHAMMAD###################################
//=====================================================
function get_rquestSpecialIssue(){
		$db = con();
		$sql = $db->query("SELECT * FROM sms_specialIssue WHERE status = '0'");
		$num = $db->numRows($sql);
	return $num;
}
//================================================
//get number of posible sending message (english)-
function getamountsmsenglish ($user_id){
	$db = con();
		$goroohe_alan=user_group($user_id);
		$res = $db->fetchNextArray($db->query("SELECT * FROM sms_nerkhe_sms where id = 1"));
		$nerkhe_tamas=nerkhe_tamas($goroohe_alan,2);
		$hala = (int)get_credit_amount($res_user['ID']);
		$hala = (int)get_credit_amount($user_id);
		$result = (int)($hala/$nerkhe_tamas);
return $result;
}
//==============================================
//get number of posible sending message (farsi)

function getamountsmsenglish_all_users (){
	$db = con();
	$result = 0;
	$sql_user=$db->query("select ID from u_users ");
	while($res_user=$db->fetchNextArray($sql_user)){
		$goroohe_alan=user_group($res_user['ID']);
		$res = $db->fetchNextArray($db->query("SELECT * FROM sms_nerkhe_sms where id = 1"));
		$nerkhe_tamas=nerkhe_tamas($goroohe_alan,2);
		$hala = (int)get_credit_amount($res_user['ID']);
		$result = (int)($hala/$nerkhe_tamas) + $result;
	}
return $result;
}

//==============================================
//get number of posible sending message (farsi)

function getamountsmsfarsi_all_users (){
	$db = con();
	$result = 0;
	$sql_user=$db->query("select ID from u_users ");
	while($res_user=$db->fetchNextArray($sql_user)){
		$goroohe_alan=user_group($res_user['ID']);
		$res = $db->fetchNextArray($db->query("SELECT * FROM sms_nerkhe_sms where id = 1"));
		$nerkhe_tamas=nerkhe_tamas($goroohe_alan,1);
		$hala = (int)get_credit_amount($res_user['ID']);
		$result = (int)($hala/$nerkhe_tamas) + $result;
	}
return $result;
}

//==============================================
//get number of posible sending message (farsi)

function getamountsmsfarsi ($user_id){
	$db = con();
		$goroohe_alan=user_group($user_id);
		$res = $db->fetchNextArray($db->query("SELECT * FROM sms_nerkhe_sms where id = 1"));
		$nerkhe_tamas=nerkhe_tamas($goroohe_alan,1);
		$hala = (int)get_credit_amount($res_user['ID']);
		$hala = (int)get_credit_amount($user_id);
		$result = (int)($hala/$nerkhe_tamas);
return $result;
}

//-----------------------------------------------------
function user_group($userID){
	$db =con();
	$res_gorooh = $db->fetchNextArray($db->query("SELECT * FROM sms_goroohe_karbaran WHERE user_id = '".$userID."'"));
	$goroohe_alan = $res_gorooh['groupid'];
	return $goroohe_alan;
}	
		
//====================================================
function nerkhe_tamas($groupid,$utf){
	$db = con();
	$sql=$db->query("SELECT * from sms_nerkhe_sms where id=1");
	$res=$db->fetchNextArray($sql);
	if($utf==1){
			switch($groupid) {
				case 1:
					$nerkhe_tamas = $res['farsi_1'];
				break;
				case 2:
					$nerkhe_tamas = $res['farsi_2'];
				break;
				case 3:
					$nerkhe_tamas = $res['farsi_3'];
				break;
				case 4:
					$nerkhe_tamas = $res['farsi_4'];
				break;
				case 5:
					$nerkhe_tamas = $res['farsi_5'];
				break;
				case 6:
					$nerkhe_tamas = $res['farsi_6'];
				break;
				case 7:
					$nerkhe_tamas = $res['farsi_7'];
				break;
				case 8:
					$nerkhe_tamas = $res['farsi_8'];
				break;
			}
	}elseif($utf==2){
			switch($groupid) {
				case 1:
					$nerkhe_tamas = $res['english_1'];
				break;
				case 2:
					$nerkhe_tamas = $res['english_2'];
				break;
				case 3:
					$nerkhe_tamas = $res['english_3'];
				break;
				case 4:
					$nerkhe_tamas = $res['english_4'];
				break;
				case 5:
					$nerkhe_tamas = $res['english_5'];
				break;
				case 6:
					$nerkhe_tamas = $res['english_6'];
				break;
				case 7:
					$nerkhe_tamas = $res['english_7'];
				break;
				case 8:
					$nerkhe_tamas = $res['english_8'];
				break;
			}		
	}
	if($nerkhe_tamas){
		return $nerkhe_tamas;
	}else{
		return 240;
	}
}
//====================================================
	function get_credit_amount_majazi($user_id){
		$db = con();
		$credit=0;
		$sql = $db->query("select credit from sms_records where user_id='".(int)$user_id."' ");
		while($res = $db->fetchNextArray($sql)){
			$credit = $credit+$res['credit'];
		}
		
			return $credit;
	}
//-----------------------------------------------------
function return_all_no_send($id,$type){
	$db =con();
	$where_claus .= " WHERE smsStatus = 0 ";
	if($type){
		$where_claus .= " AND smsType = ".(int)$type;
	}else{
		$where_claus .= " ";
	}
	if($id < 1){
		
	}else{
		$where_claus .= " AND user_id = ".(int)$id;
	}	
	
	$all_amount=0;
	$sql =$db->query("SELECT * FROM sms_records $where_claus ");
	while($res = $db->fetchNextArray($sql)){
		$detect_encoding =  mb_detect_encoding($res['text_message']);
		$replace_text_message=str_replace("\r","",$res['text_message']);
		$str_len = mb_strlen ($replace_text_message,'UTF-8');
		if ($detect_encoding == 'UTF-8'){
			$maxLength=70;
			$udhLength=3;
			if($str_len<=70){
				$sms_amount=1;
			}else{
				$sms_amount = ((int)($str_len - 1) / (int)($maxLength - $udhLength)) + 1;
			}
		} else {
			$maxLength=160;
			$udhLength=6;
			if($str_len<=160){
				$sms_amount=1;
			}else{
				$sms_amount = ((int)($str_len - 1) / (int)($maxLength - $udhLength)) + 1;
			}
		}	
	$all_amount = ((int)$sms_amount * (int)$res['amount'])  + (int)$all_amount ;
	}
	return $all_amount;
}
	
//-----------------------------------------------------
function return_city($id){
	$db =con();
	$sql = $db->query("SELECT * FROM shahr where id = '".$id."' ");
	$res = $db->fetchNextArray($sql);
	return $res['name'];
}
//-----------------------------------------------------
function return_state($id){
	$db =con();
	$sql = $db->query("SELECT * FROM ostan where id = '".$id."' ");
	$res = $db->fetchNextArray($sql);
	return $res['name'];
}	
//-----------------------------------------------------
//-----------------------------------------------------
function get_sms_type ($status) {
		switch($status) {
			case 0:
				$output ='<img src="../images/icon/1.png" alt="ارسال تکی" title="ارسال تکی"> ';
			break;
			case 1:
				$output = '<img src="../images/icon/2.png" alt="ارسال منطقه ای" title="ارسال منطقه ای">';
			break;
			case 2:
				$output = '<img src="../images/icon/3.png" alt="ارسال به اصناف" title="ارسال به اصناف">';
			break;
			case 3:
				$output = '<img src="../images/icon/4.png" alt="ارسال شهر و استان " title="ارسال شهر و استان">';
			break;
			case 4:
				$output = '<img src="../images/icon/5.png" alt="ارسال زمانبندی" title="ارسال زمانبندی">';
			break;
			case 5:
				$output = '<img src="../images/icon/" alt="ارسال ';
			break;
			case 6:
				$output = '<img src="../images/icon/" alt="ارسال پیوسته" title="ارسال پیوسته">';
			break;
			case 7:
				$output = '<img src="../images/icon/6.png" alt="ارسال تصادفی" title="ارسال تصادفی">';
			break;
			case 8:
				$output = '<img src="../images/icon/7.png" alt="ارسال گروهی" title="ارسال گروهی">';
			break;	
			case 10:
				$output = '<img src="../images/icon/8.png" alt="sms srvice " title="sms srvice">';
			break;			
							
	}
return $output;
}
//-----------------------------------------------------
function return_all_send($id,$type){
	$db =con();
	$where_claus .= " WHERE smsStatus = 1 ";
	if($type){
		$where_claus .= " AND smsType = ".(int)$type;
	}else{
		$where_claus .= " ";
	}
	if($id < 1){
		
	}else{
		$where_claus .= " AND user_id = ".(int)$id;
	}	
	
	$all_amount=0;
	$sql =$db->query("SELECT * FROM sms_records $where_claus ");
	while($res = $db->fetchNextArray($sql)){
		$detect_encoding =  mb_detect_encoding($res['text_message']);
		$replace_text_message=str_replace("\r","",$res['text_message']);
		$str_len = mb_strlen ($replace_text_message,'UTF-8');
		if ($detect_encoding == 'UTF-8'){
			$maxLength=70;
			$udhLength=3;
			if($str_len<=70){
				$sms_amount=1;
			}else{
				$sms_amount = ((int)($str_len - 1) / (int)($maxLength - $udhLength)) + 1;
			}
		} else {
			$maxLength=160;
			$udhLength=6;
			if($str_len<=160){
				$sms_amount=1;
			}else{
				$sms_amount = ((int)($str_len - 1) / (int)($maxLength - $udhLength)) + 1;
			}
		}	
	$all_amount = ((int)$sms_amount * (int)$res['amount'])  + (int)$all_amount ;
	}
	return $all_amount;
}	
?>