Index: pro-violet-viettel/sourcecode/application/modules/frontend/models/servicepackage_model.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/frontend/models/servicepackage_model.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/modules/frontend/models/servicepackage_model.php	(revision 499)
@@ -8,5 +8,6 @@
 	const TOKENPW = 'violet';
 	
-	function __construct(){
+	function __construct()
+	{
         parent::__construct();
 		$this->vservices->setApiUrl($this->config->item('api_url'));
@@ -19,4 +20,5 @@
         return $this->db->query($sql)->result_array();
     }
+    
     function getPackage($p_id)
     {
@@ -24,4 +26,20 @@
         return $this->db->query($sql)->row_array();
     }
+    
+    function getPackagePriceByCode ($p_code) 
+    {
+    	$sql='SELECT p_price FROM '. $this->table_name .' WHERE p_code = ?';
+    	$query =  $this->db->query($sql , array($p_code));
+    	$row = $query->row_array();
+    	return $row['p_price'];
+    }
+    
+    function getPackageByCode ($p_code)
+    {
+    	$sql='SELECT * FROM '. $this->table_name .' WHERE p_code = ?';
+    	$query =  $this->db->query($sql , array($p_code));
+    	$row = $query->row_array();
+    	return $row;
+    }
 	
 }
Index: pro-violet-viettel/sourcecode/application/modules/frontend/models/user_model.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/frontend/models/user_model.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/modules/frontend/models/user_model.php	(revision 499)
@@ -6,5 +6,5 @@
 	const TOKENPW = 'violet';
 	
-	function __construct(){
+	public function __construct(){
         parent::__construct();
 		$this->vservices->setApiUrl($this->config->item('api_url'));
@@ -12,8 +12,8 @@
     } 
 	
-	function checkLogin ($src, $token, $username, $password) 
+	public function checkLogin ($src, $token, $username, $password) 
 	{
 		$data = $this->vservices->actionExecute('login', array('src' => $src, 'token' => $token, 'username' => $username, 'password' => $password), 'user');
-				
+		
 		$arr_users = explode("&", $data);
 		$str_status = '';
@@ -22,4 +22,5 @@
 		$usid = '';
 		$fullname = '';
+		
 		for ($i=0; $i < count($arr_users); $i++)
 		{
@@ -68,5 +69,5 @@
 	}
 
-	function register ($username, $collaborator, &$status)
+	public function register ($username, $collaborator, &$status='')
 	{
 		$user = array();
@@ -76,64 +77,41 @@
 		$id = 0;
 		$errMsg = '';
-		$username = '0'.substr($username, 2, strlen($username));
 		$token = md5($username.self::TOKENPW);		
 		$phone = $username;
+		
 		if (!is_null($collaborator) || $collaborator != ''){
-			if($this->isExistcollaborator($collaborator)){
-				$data = $this->vservices->actionExecute('update', array('src'=>$src, 'username'=>$username, 'password'=>$password, 'fullname'=>null, 'phone'=>$username, 'token'=>$token), 'user');
-				parse_str($data);
-				switch((int)$status)
-				{
-					case 0:
-						$us_id = $id;
-						$user['created_time'] = date('Y-m-d H:i:s');
-						$user['us_id'] = $us_id;
-						$user['cellphone'] = $username;
-						$user['collaborator'] = $collaborator;
-						$this->db->insert('tbluser', $user);
-						$sms = "Chuc mung Qui vi da dang ky thanh cong tai khoan Soan Bai giang Online. Hay dang nhap vao soanbaigiang.smas.vn voi ten truy nhap ".$username.", mat khau ".$password." de su dung.";
-						$status = 1;
-						break;
-					case 2:				
-					case 4:
-						$sms = "So dien thoai cua Qui vi da duoc dang ky !";
-						$status = 0;
-						break;
-					default:
-						$status = 0;
-						$sms = $errMsg;
-						break;
-				}
-			}
-			else
-			{
+			if(!$this->isExistcollaborator($collaborator)){
 				$sms = "Khong tim thay cong tac vien nao co so dien thoai [".$collaborator."] tren soanbaigiang.smas.vn!";
 				$status = 0;
-			}
-		}else
-		{
-			$data = $this->vservices->actionExecute('update', array('src'=>$src, 'username'=>$username, 'password'=>$password, 'fullname'=>null, 'phone'=>$username, 'token'=>$token), 'user');
-			parse_str($data);		
-			switch((int)$status)
-			{
-				case 0:
-					$us_id = $id;
-					$user['created_time'] = date('Y-m-d H:i:s');
-					$user['us_id'] = $us_id;
-					$user['cellphone'] = $username;
-					$this->db->insert('tbluser', $user);
-					$sms = "Chuc mung Qui vi da dang ky thanh cong tai khoan Soan Bai giang Online. Hay dang nhap vao soanbaigiang.smas.vn voi ten truy nhap ".$username.", mat khau ".$password." de su dung.";
-					$status = 1;
-					break;
-				case 2:				
-				case 4:
-					$sms = "So dien thoai cua Qui vi da duoc dang ky !";
-					$status = 0;
-					break;
-				default:
-					$status = 0;
-					$sms = $errMsg;
-					break;
-			}
+				return $status.'|'.$sms;
+			}
+		}
+		
+		$data = $this->vservices->actionExecute('update', array('src'=>$src, 'username'=>$username, 'password'=>$password, 'fullname'=>null, 'phone'=>$username, 'token'=>$token), 'user');
+		parse_str($data);
+		switch((int)$status)
+		{
+			case 0:
+				$us_id = $id;
+				$user['created_time'] = date('Y-m-d H:i:s');
+				$user['us_id'] = $us_id;
+				$user['cellphone'] = $username;
+				if (!is_null($collaborator) || $collaborator != ''){
+					$user['collaborator'] = $collaborator;
+				}
+				$this->db->query('UNLOCK TABLES');
+				$this->db->insert('tbluser', $user);
+				$sms = "Chuc mung Qui vi da dang ky thanh cong tai khoan Soan Bai giang Online. Hay dang nhap vao soanbaigiang.smas.vn voi ten truy nhap ".$username.", mat khau ".$password." de su dung.";
+				$status = 1;
+				break;
+			case 2:
+			case 4:
+				$sms = "So dien thoai cua Qui vi da duoc dang ky!";
+				$status = 0;
+				break;
+			default:
+				$status = 0;
+				$sms = $errMsg;
+				break;
 		}
 		
@@ -141,7 +119,8 @@
 	}
 	
-	function create_random_password()
-	{
-		$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
+	public function create_random_password()
+	{
+		//$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
+		$alphabet = "0123456789";
 		$pass = array();
 		$alphaLength = strlen($alphabet) - 1;
@@ -153,6 +132,7 @@
 	}
 	
-	function get_user_by_id($us_id)
-	{
+	public function get_user_by_id($us_id)
+	{
+		$this->db->query('UNLOCK TABLES');
 		$this->db->select('*')->from('tbluser')->where('us_id', $us_id);
 		$query = $this->db->get();
@@ -164,5 +144,5 @@
 	}
 	
-	function get_user_info($src, $token, $us_id)
+	public function get_user_info($src, $token, $us_id)
 	{
 		$data = $this->vservices->actionExecute('getinfo', array('src' => $src, 'us_id' => $us_id, 'token'=>$token), 'user');
@@ -170,5 +150,5 @@
 	}
 	
-	function update_user($src, $token, $user, $us_id)
+	public function update_user($src, $token, $user, $us_id)
 	{
 		$data = $this->vservices->actionExecute('update', array('src'=>$src, 'token'=>$token, 'us_id'=>$us_id, 
@@ -180,5 +160,5 @@
 	}
 	
-	function changeProfile($sentNumber, $username, $password, &$status)
+	public function changeProfile($sentNumber, $username, $password, &$status='')
 	{
 		$user = array();
@@ -205,5 +185,5 @@
 					break;
 				case 5:
-					$sms = "So dien thoai cá»§a Qui vi chua duoc dang ky !";
+					$sms = "So dien thoai cá»§a Qui vi chua duoc dang ky!";
 					$status = 0;
 					break;
@@ -219,5 +199,5 @@
 		}else
 		{
-			$sms = "So dien thoai cá»§a Qui vi chua duoc dang ky !";
+			$sms = "So dien thoai cá»§a Qui vi chua duoc dang ky!";
 			$status = 0;
 		}
@@ -225,5 +205,5 @@
 	}
 	
-	function changePassword($username, $password, &$status)
+	public function changePassword($username, $password, &$status = '')
 	{
 		$user = array();
@@ -255,10 +235,10 @@
 						$sms = "Mat khau moi cua quy vi tren soanbaigiang.smas.vn la [".$pass."]";
 					}else{
-						$sms = "Qui vi da doi thanh cong mat khau tren soanbaigiang.smas.vn. ";
+						$sms = "Qui vi da doi thanh cong mat khau tren soanbaigiang.smas.vn.";
 					}
 					$status = 1;
 					break;
 				case 5:
-					$sms = "So dien thoai cá»§a Qui vi chua duoc dang ky !";
+					$sms = "So dien thoai cá»§a Qui vi chua duoc dang ky!";
 					$status = 0;
 					break;
@@ -271,5 +251,5 @@
 		else
 		{
-			$sms = "So dien thoai cá»§a Qui vi da duoc dang ky !";
+			$sms = "So dien thoai cá»§a Qui vi da duoc dang ky!";
 			$status = 0;
 		}
@@ -278,8 +258,10 @@
 	
 	
-	function checkLicense ($userId) 
+	public function checkLicense ($userId) 
 	{
 		$isNotExpired = false;
-		$userInfo = $this->get_user_by_id($userId);		
+		$userInfo = array();
+		$userInfo = $this->get_user_by_id($userId);	
+
 		$expireDate = $userInfo['expire_date'];
 		
@@ -292,15 +274,17 @@
 	}
 	
-	function registerVip($username, $p_name, &$status)
-	{
-		$user = array();
-		$sms = '';
+	public function registerVip($username, $p_code)
+	{
+		$user = array();
+		$sms = '';
+		
 		$result = $this->get_user_by_cellphone($username);
+		$package = $this->get_package((int)$p_code);
 		if($result){
-			 $package = $this->get_package($p_name);
-			if ($result['acc_balanced']>=$package['p_price'])
+			
+			if ($result['acc_balanced'] >= $package['p_price'])
 			{
 				$us_id = $result['us_id'];
-				$user['acc_balanced'] = $result['acc_balanced'] - $p_price;
+				$user['acc_balanced'] = $result['acc_balanced'] - $package['p_price'];
 				if ($this->checkLicense($us_id)){
 					$user['expire_date'] = date('Y-m-d', strtotime($result['expire_date'].' + '.$package['p_period'].' month'));
@@ -309,6 +293,8 @@
 					$user['expire_date'] = date('Y-m-d', strtotime('now + '.$package['p_period'].' month'));
 				}
-				$user['user_package'] = $p_id;
+				
+				$user['p_id'] = $package['p_id'];
 				$user['updated_time'] = date('Y-m-d H:i:s');
+				
 				$this->updateUser($us_id, $user);
 				$sms = "Chuc mung Qui vi da dang ky su dung Soan Bai giang truc tuyen tren soanbaigiang.smas.vn. Qui vi co the su dung dich vu den 24h00 ngay ".date('d/m/Y', strtotime($user['expire_date']));
@@ -327,5 +313,5 @@
 	}
 	
-	function rechargeAccount($username, $amount, &$status)
+	public function rechargeAccount($username, $amount)
 	{
 		$user = array();
@@ -338,9 +324,9 @@
 			$user['updated_time'] = date('Y-m-d H:i:s');
 			$this->updateUser($us_id, $user);
-			$sms = "Qui vi da nap thanh cong [".$amount."] vao tai khoan ".$username." tren soanbaigiang.smas.vn";
+			$sms = "Qui vi da nap thanh cong ".$amount."VND vao tai khoan ".$username." tren soanbaigiang.smas.vn";
 			$status = 1;
 		}else
 		{
-			$sms = "Tai khoan [".$username."] chua dang ki tai khoan tren he thong SBG Online soanbaigiang.smas.vn.";
+			$sms = "Tai khoan ".$username." chua dang ki tai khoan tren he thong SBG Online soanbaigiang.smas.vn.";
 			$status = 0;
 		}
@@ -348,13 +334,16 @@
 	}
 	
-	function get_user_by_cellphone($cellphone)
-	{
-		$this->db->select('*')->from('tbluser')->where('cellphone', $cellphone)->limit(1);
-		$result = $this->db->get()->row_array();
-		return $result;
-	}
-	
-	function isExistcollaborator($collaborator)
-	{
+	public function get_user_by_cellphone($cellphone)
+	{
+		$this->db->query('UNLOCK TABLES');
+		$sql = 'SELECT * FROM tbluser where cellphone = ? LIMIT 0,1';
+		$query = $this->db->query($sql, array($cellphone));
+		$row = $query->row_array();
+		return $row;
+	}
+	
+	public function isExistcollaborator($collaborator)
+	{
+		$this->db->query('UNLOCK TABLES');
 		$this->db->select('*')->from('tblcollaborator')->where('login_name', $collaborator)->limit(1);
 		$result = $this->db->get()->row_array();
@@ -362,31 +351,44 @@
 	}
 	
-	function updateUser($us_id, $data)
-	{
+	public function updateUser($us_id, $data)
+	{
+		$this->db->query('UNLOCK TABLES');
 		$this->db->where('us_id', $us_id);
 		$this->db->update('tbluser', $data);
 	}
 	
-	function get_package($p_name)
-	{
-		$this->db->select('*')->from('tblservicepackage')->where('p_name', $p_name);
-		$query = $this->db->get();
-		if($query->num_rows() > 0)
-		{
-			$row = $query->row_array();
-			return $row;
-		}
-	}
-	
-	function insertSmslog($sentNumber, $userID, $serviceID, $commandcode, $info, $receiveTime)
-	{
+	public function get_package($p_code)
+	{
+		$this->db->query('UNLOCK TABLES');
+		$sql = 'SELECT * FROM tblservicepackage where p_code = ? LIMIT 0,1';
+		$query = $this->db->query($sql, array($p_code));
+		$row = $query->row_array();
+		return $row;
+	}
+	
+	public function insertSmslog($sentNumber, $serviceID, $commandcode, $info, $receiveTime)
+	{
+		if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) {
+			$sentNumber = '0'. $aryMatch[2];
+		}
+		
+		try {
+			$user = $this->get_user_by_cellphone($sentNumber);
+		}
+		catch(Exception $e) {
+			
+		}
+		
 		$smslog = array();
 		$smslog['sender'] = $sentNumber;
-		$smslog['us_id'] = $userID;
+		$smslog['us_id'] = isset($user['us_id']) ? $user['us_id']:NULL;
 		$smslog['service_id'] = $serviceID;
 		$smslog['commandcode'] = $commandcode;
 		$smslog['info'] = $info;
 		$smslog['created_time'] = $receiveTime;
-		$this->db->insert('tblsmslog', $smslog);
+		$this->db->query('UNLOCK TABLES');
+		$sql = 'INSERT INTO tblsmslog(sender, us_id, service_id, commandcode, info, created_time) VALUES(?,?,?,?,?,?)';
+		$query = $this->db->query($sql, $smslog);
+		//$this->db->insert('tblsmslog', $smslog);
 	}
 }
