Index: pro-violet-viettel/sourcecode/application/config/config.php
===================================================================
--- pro-violet-viettel/sourcecode/application/config/config.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/config/config.php	(revision 499)
@@ -388,6 +388,10 @@
 $config['mo_password'] = 'sbg571285';
 
+$config['mt_username'] = 'sbg';
+$config['mt_password'] = 'sbg56ab77cdsgb';
+
 $config['charging_username'] = 'sbg';
 $config['charging_password'] = 'sbg123#@!';
+
 /*
 |--------------------------------------------------------------------------
Index: pro-violet-viettel/sourcecode/application/config/database.php
===================================================================
--- pro-violet-viettel/sourcecode/application/config/database.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/config/database.php	(revision 499)
@@ -51,5 +51,5 @@
 $db['default']['hostname'] = 'mysql:host=127.0.0.1';
 $db['default']['username'] = 'root';//sbg
-$db['default']['password'] = 'Quyenhue@123';//sbg~~!!@@123
+$db['default']['password'] = '123456';//sbg~~!!@@123
 $db['default']['database'] = 'violet-viettel';
 $db['default']['dbdriver'] = 'pdo';
Index: pro-violet-viettel/sourcecode/application/config/routes.php
===================================================================
--- pro-violet-viettel/sourcecode/application/config/routes.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/config/routes.php	(revision 499)
@@ -43,5 +43,9 @@
 
 $route['violetservice/soanbai/mo'] = 'services/viettel/mo';
+$route['violetservice/soanbai/mo/'] = 'services/viettel/mo';
 $route['violetservice/soanbai/mo/wsdl'] = 'services/viettel/index/wsdl';
+$route['violetservice/soanbai/index'] = 'services/viettel/index/wsdl';
+$route['violetservice/soanbai'] = 'services/viettel/index/wsdl';
+$route['violetservice/soanbai/'] = 'services/viettel/index/wsdl';
 
 $route['admin'] = 'admin/home';
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);
 	}
 }
Index: pro-violet-viettel/sourcecode/application/modules/services/controllers/sendmessage.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/services/controllers/sendmessage.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/modules/services/controllers/sendmessage.php	(revision 499)
@@ -16,4 +16,13 @@
 	}
 	
+	public function index () 
+	{
+		error_reporting(E_ALL);
+		$this->load->model('Services_model');
+		//$this->Services_model->cardPaidProcess($aryParams);
+		echo $this->Services_model->processMORequest('01662860000', strtoupper('Sbg vip 1'));
+		
+	}
+	
 	public function testmo() {
 		$url = 'http://10.58.38.162/violetservice/soanbai/mo?wsdl';
@@ -59,5 +68,5 @@
 	}
 	
-	public function send($msg)
+	public function send($msg, $receiverID)
 	{
 		$url = $this->config->item('mt_service_url');
@@ -66,5 +75,10 @@
 		$operator = 'InsertMT';
 		$operation = 'http://tempuri.org/InsertMT';
-			
+		
+		$aryMatch = array();
+		if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
+			$receiverID = '84'. $aryMatch[2];
+		}
+		
 		$aryParams = array(
 				'User' => $username,
@@ -73,5 +87,5 @@
 				'UserID' => '841662860000',
 				'RequestID' => '4',
-				'ReceiverID' => '84988568786',
+				'ReceiverID' => $receiverID,
 				'ServiceID' => '8062',
 				'CommandCode' => 'SBG',
@@ -93,4 +107,6 @@
 		}
 	
+		var_dump($result);
+		
 		echo "<br/>";
 		echo "<h2>Request</h2>";
@@ -104,6 +120,5 @@
 	{
 		$url = $this->config->item('topup_service_url');
-		$operator = 'TopupService';
-		$operation = 'http://soanbaigiang.smas.vn/ITopupService/TopUp';
+		$operation = 'http://tempuri.org/ITopupService/TopUp';;
 		
 		$username = 'sbg';
@@ -162,5 +177,5 @@
 				'user' => $username,
 				'pass' => $password,
-				'MSISDN' => '1689925260',
+				'MSISDN' => '01662860000',
 				'Charging' => '100',
 				'Contents' => 'Namta-Active'
@@ -180,4 +195,6 @@
 		}
 		
+		var_dump($result);
+		
 		echo "<br/>";
 		echo "<h2>Request</h2>";
Index: pro-violet-viettel/sourcecode/application/modules/services/controllers/viettel.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/services/controllers/viettel.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/modules/services/controllers/viettel.php	(revision 499)
@@ -35,5 +35,5 @@
 		function messageReceiver(){
 			$aryArgs = func_get_args();
-			list($username, $password, $requestID, $userID, $receiverID, $serviceID, $commandCode, $contentType ,$info, $receiveTime) = $aryArgs;
+			list($username, $password, $requestID, $userID, $receiverID, $serviceID, $commandCode, $info, $receiveTime) = $aryArgs;
 			
 			$msgType = '0'; //text message
@@ -48,25 +48,20 @@
 			if ($username != $mo_username) return '0|0|Username is not corrected!';
 			if ($password != $mo_password) return '0|0|Password is not corrected!';
-			if (!$requestID) return '0|0|requestID is required!';
-			if ((int)$requestID != 4) return '0|0|requestID is not corrected!';
+			if (!$requestID) return '0|0|requestID is required!';			
 			if (!$userID) return '0|0|UserID is required!';
 			if (!$serviceID) return '0|0|serviceID is required!';
 			if ($serviceID != '8062') return '0|0|serviceID is not corrected!';
-			
-			$status = 0;
-			$sentNumber = $userID;									
-			$username = !$receiverID ? $sentNumber : $receiverID;
-			
-			$CI->load->model('frontend/user_model', 'userModel');
-			$password = $CI->userModel->create_random_password();		
-			
-			$result = $CI->userModel->register ($sentNumber, $receiverID, $serviceID, $commandCode, $info, $receiveTime, $status);
+
+			$CI->load->model('Services_model');
+			$CI->load->model('frontend/User_model', 'objUserModel');
+			$result = $CI->Services_model->processMORequest($userID, strtoupper($info));
+			$CI->objUserModel->insertSmslog($receiverID, $serviceID, $commandCode, $info, $receiveTime);
 					
 			$aryResult = explode ('|', $result);
-			
 			return $aryResult[0].'|'.$msgType.'|'.$aryResult[1];
 		}
 		
 		$this->_server->addFunction('messageReceiver');
+		
 		try {
 			ob_start();
Index: pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php	(revision 499)
+++ pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php	(revision 499)
@@ -0,0 +1,230 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+
+if ( ! defined('_SBG_MO_REGISTER')) define('_SBG_MO_REGISTER', 0);
+if ( ! defined('_SBG_MO_REGISTER_FOR')) define('_SBG_MO_REGISTER_FOR', 1);
+if ( ! defined('_SBG_MO_PHONENUM_CHANGE')) define('_SBG_MO_PHONENUM_CHANGE', 2);
+if ( ! defined('_SBG_MO_PASSWORD_RECOVERY')) define('_SBG_MO_PASSWORD_RECOVERY', 3);
+if ( ! defined('_SBG_MO_PASSWORD_CHANGE')) define('_SBG_MO_PASSWORD_CHANGE', 4);
+if ( ! defined('_SBG_MO_VIP_REGISTER')) define('_SBG_MO_VIP_REGISTER', 5);
+if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT')) define('_SBG_MO_PAY_TO_ACCOUNT', 6);
+if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT_FOR')) define('_SBG_MO_PAY_TO_ACCOUNT_FOR', 7);
+
+if ( ! defined('_SBG_MO_SYNTAX_ERR')) define('_SBG_MO_SYNTAX_ERR', -1);
+if ( ! defined('_SBG_MO_SYNTAX_ERR_MSG')) define('_SBG_MO_SYNTAX_ERR_MSG', 'soanbaigiang.smas.vn khong co dich vu nay!');
+
+if ( ! defined('_SBG_CARD_PAY_FAIL')) define('_SBG_CARD_PAY_FAIL', -1);
+if ( ! defined('_SBG_CARD_PAY_SUCCESS')) define('_SBG_CARD_PAY_SUCCESS', 1);
+
+if ( ! defined('_SBG_CHARGING_FAIL')) define('_SBG_CHARGING_FAIL', -1);
+if ( ! defined('_SBG_CHARGING_SUCCESS')) define('_SBG_CHARGING_SUCCESS', 1);
+
+if ( ! defined('_SBG_CHARGING_FAIL_NOT_ENOUGH')) define('_SBG_CHARGING_FAIL_NOT_ENOUGH', 401);
+if ( ! defined('_SBG_CHARGING_FAIL_NOT_ENOUGH_MSG')) define('_SBG_CHARGING_FAIL_NOT_ENOUGH_MSG', 'Tai khoan chinh cua so dien thoai :sendid: khong du de thuc hien lenh nap :amount: vao tai khoan :username: tren soanbaigiang.smas.vn');
+
+if ( ! defined('_SBG_SEND_MT_FAIL')) define('_SBG_SEND_MT_FAIL', -1);
+if ( ! defined('_SBG_SEND_MT_SUCCESS')) define('_SBG_SEND_MT_SUCCESS', 1);
+
+if ( ! defined('_SBG_MT_SEND_NUMBER')) define('_SBG_MT_SEND_NUMBER', '841662860000');
+if ( ! defined('_SBG_MT_CP_CODE')) define('_SBG_MT_CP_CODE', '601');
+
+if ( ! defined('_SBG_SERVICE_COMMAND_CODE')) define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
+if ( ! defined('_SBG_SERVICE_SERVICEID')) define('_SBG_SERVICE_SERVICEID', '8062');
+
+Class Services_model extends CI_Model 
+{
+	private $_arySmsMO = array(
+		_SBG_MO_REGISTER 			=> array('pattern' => '/^(SBG[[:blank:]])(DK)$/'),
+		_SBG_MO_REGISTER_FOR 		=> array('pattern' => '/^(SBG[[:blank:]])(DK)([[:blank:]][0-9]{10,11})$/'),
+		_SBG_MO_PHONENUM_CHANGE 	=> array('pattern' => '/^(SBG[[:blank:]])(DDT)([[:blank:]][0-9]{10,11})([[:blank:]][a-zA-Z0-9]{4,16})$/'),
+		_SBG_MO_PASSWORD_RECOVERY 	=> array('pattern' => '/^(SBG[[:blank:]])(MK)$/'),
+		_SBG_MO_PASSWORD_CHANGE 	=> array('pattern' => '/^(SBG[[:blank:]])(MK)([[:blank:]][a-zA-Z0-9]{4,16})$/'),
+		_SBG_MO_VIP_REGISTER 		=> array('pattern' => '/^(SBG[[:blank:]])(VIP)([[:blank:]][0-9]{1,2})$/'),
+		_SBG_MO_PAY_TO_ACCOUNT 		=> array('pattern' => '/^(SBG[[:blank:]])(NAP)([[:blank:]][0-9]{3,})$/'),
+		_SBG_MO_PAY_TO_ACCOUNT_FOR 	=> array('pattern' => '/^(SBG[[:blank:]])(NAP)([[:blank:]][0-9]{3,})([[:blank:]][0-9]{10,11})$/'),
+	);
+	
+	public function __construct(){
+		parent::__construct();
+	}
+	
+	private function messageAnalys ($message) 
+	{
+		$aryMatched = array();
+		$result = false;
+		foreach ($this->_arySmsMO as $key => $pattern) 
+		{
+			$result = preg_match($pattern['pattern'], $message, $aryMatched);
+			if ((bool)$result) break;
+		}
+		
+		$key = (bool)$result ? $key:_SBG_MO_SYNTAX_ERR;
+		$aryResult = (bool)$result ? array('case' => $key, 'content' => $aryMatched):array('case' => $key);
+		return $aryResult;
+	}
+	
+	public function processMORequest ($sentNumber,$message) 
+	{
+		$aryMatch = array();
+		if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) {
+			$sentNumber = '0'. $aryMatch[2];
+		}
+		
+		$smsReturn = '';
+		$aryMOAnalys = $this->messageAnalys($message);		
+		$case = $aryMOAnalys['case'];
+		$aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
+		if ($aryContents != null){
+			unset($aryContents[0]);
+			$aryTmp = array();			
+			foreach ($aryContents as $key => $val)
+			{
+				$aryTmp[] = trim($val);
+			}
+			$aryContents = $aryTmp;
+		}
+		
+		$this->load->model('frontend/user_model', 'objUserModel');
+		
+		switch ($case) {
+			case _SBG_MO_REGISTER:
+			case _SBG_MO_REGISTER_FOR:
+				$userName = isset($aryContents[2]) ? $aryContents[2]:$sentNumber;  
+				$collaborator = isset($aryContents[2]) ? $aryContents[0]:null;
+				$smsReturn = $this->objUserModel->register($userName, $collaborator);
+				break;
+			case _SBG_MO_PHONENUM_CHANGE:
+				$oldPhoneNumber = $aryContents[2];
+				$password = $aryContents[3];
+				$smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
+				$this->sendMT($oldPhoneNumber, $smsReturn);
+				break;
+			case _SBG_MO_PASSWORD_RECOVERY:
+			case _SBG_MO_PASSWORD_CHANGE:
+				$password = isset($aryContents[2]) ? $aryContents[2] : null;
+				$smsReturn = $this->objUserModel->changePassword($sentNumber, $password);
+				break;						
+			case _SBG_MO_VIP_REGISTER:
+				$p_code = $aryContents[2];
+				$smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
+				//$smsReturn = $this->objUserModel->getVIP($sentNumber, $p_code);
+				break;
+			case _SBG_MO_PAY_TO_ACCOUNT:
+			case _SBG_MO_PAY_TO_ACCOUNT_FOR:
+				$userName = isset($aryContents[3]) ? $aryContents[3] : $sentNumber;
+				$amount = $aryContents[2];
+				$result =$this->chargeRootAccountProcess($sentNumber, $amount);
+				if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
+					$aryPatterns = array ('/:sendid:/', '/:amount:/', '/:username:/');
+					$smsReturn = preg_replace($aryPatterns, array($sentNumber, $amount, $userName), _SBG_CHARGING_FAIL_NOT_ENOUGH_MSG);
+					$smsReturn = '0|'.$smsReturn;
+				}
+				else {
+					$smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
+				}
+				//write to paid log
+				break;
+			case _SBG_MO_SYNTAX_ERR:
+			default:
+				$smsReturn = '0|'._SBG_MO_SYNTAX_ERR_MSG;
+				break;
+		}
+		
+		return $smsReturn;
+	}
+	
+	public function cardPaidProcess ($aryCardInfo) 
+	{
+		$url = $this->config->item('topup_service_url');
+		$action = 'http://tempuri.org/ITopupService/TopUp';
+		$xml = $this->load->view('topup', $aryCardInfo, true);
+		
+		$client = new nusoap_client($url);
+		$client->useHTTPPersistentConnection();
+		$client->soap_defencoding = 'UTF-8';
+		$client->operation = $action;
+		
+		$result = $client->send($this->_xmlHeader.$xml, $operation);
+		
+		if ($client->fault) {
+			return _SBG_CARD_PAY_FAIL;
+		}
+		
+		$aryTopupResult = $result['TopUpResult'];
+		
+		if ($aryTopupResult['Success'] == 'false') {
+			return _SBG_CARD_PAY_FAIL;
+		}
+		
+		return _SBG_CARD_PAY_SUCCESS;
+	}
+	
+	public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '') 
+	{
+		$url = $this->config->item('charging_service_url');
+		$username = $this->config->item('charging_username');
+		$password = $this->config->item('charging_password');
+		$action = 'http://tempuri.org/processCharging';		
+		
+		$aryParams = array(
+				'user' => $username,
+				'pass' => $password,
+				'MSISDN' => $sentID,
+				'Charging' => $amount,
+				'Contents' => $contents
+		);
+		
+		$xml = $this->load->view('charging', $aryParams, true);
+		$client = new nusoap_client($url);
+		$client->useHTTPPersistentConnection();
+		$client->soap_defencoding = 'UTF-8';
+		$client->operation = $action;
+		$result = $client->send($xml, $action);
+		
+		if ($client->fault) {
+			return _SBG_CHARGING_FAIL;
+		}
+		
+		if ((int)$result['processChargingResult'] == _SBG_CHARGING_FAIL_NOT_ENOUGH) 
+			return _SBG_CHARGING_FAIL_NOT_ENOUGH;
+		
+		return _SBG_CHARGING_SUCCESS;
+	}
+	
+	public function sendMT ($receiverID, $message)
+	{
+		$aryMatch = array();
+		if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
+			$receiverID = '84'. $aryMatch[2];
+		}
+		
+		$username = $this->config->item('mt_username');
+		$password = $this->config->item('mt_password');
+		$action = 'http://tempuri.org/InsertMT';
+		
+		$aryParams = array(
+				'User' => $username,
+				'Pass' => $password,
+				'CPCode' => _SBG_MT_CP_CODE,
+				'UserID' => _SBG_MT_SEND_NUMBER,
+				'RequestID' => '4',
+				'ReceiverID' => $receiverID,
+				'ServiceID' => _SBG_SERVICE_SERVICEID,
+				'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
+				'ContentType' => '0',
+				'Info' => $message
+		);
+		
+		$xml = $this->load->view('mt', $aryParams, true);
+		$client = new nusoap_client($url);
+		$client->useHTTPPersistentConnection();
+		$client->soap_defencoding = 'UTF-8';
+		$client->operation = $action;
+		$result = $client->send($xml, $action);
+		
+		if ($client->fault || (int)$result['InsertMTResult'] != 1) {
+			return _SBG_SEND_MT_FAIL;
+		}
+		
+		return _SBG_SEND_MT_SUCCESS;
+	}
+}
Index: pro-violet-viettel/sourcecode/application/modules/services/views/mowsdl.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/services/views/mowsdl.php	(revision 498)
+++ pro-violet-viettel/sourcecode/application/modules/services/views/mowsdl.php	(revision 499)
@@ -6,14 +6,13 @@
         <s:complexType>
           <s:sequence>
-            <s:element minOccurs="0" maxOccurs="1" name="username" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="requestID" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="userID" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="receiverID" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="serviceID" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="commandCode" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="contentType" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="info" type="s:string"/>
-            <s:element minOccurs="0" maxOccurs="1" name="receiveTime" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="RequestID" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="UserID" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="ReceiverID" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="ServiceID" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="CommandCode" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="Info" type="s:string"/>
+            <s:element minOccurs="0" maxOccurs="1" name="ReceiveTime" type="s:string"/>
           </s:sequence>
         </s:complexType>
