Index: /pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php
===================================================================
--- /pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php	(revision 597)
+++ /pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php	(revision 598)
@@ -21,5 +21,17 @@
 
 if ( ! defined('_SBG_CHARGING_FAIL_NOT_ENOUGH')) define('_SBG_CHARGING_FAIL_NOT_ENOUGH', 401);
+if ( ! defined('_SBG_CHARGING_FAIL_NOT_VIETTEL')) define('_SBG_CHARGING_FAIL_NOT_VIETTEL', 402);
+if ( ! defined('_SBG_CHARGING_NUMBER_NOT_EXISTED')) define('_SBG_CHARGING_NUMBER_NOT_EXISTED', 403);
+
+if ( ! defined('_SBG_CHARGING_NUMBER_NOT_AVAILABLE')) define('_SBG_CHARGING_NUMBER_NOT_AVAILABLE', 404);
+if ( ! defined('_SBG_CHARGING_NUMBER_CHANGED_OWNER')) define('_SBG_CHARGING_NUMBER_CHANGED_OWNER', 405);
+
+if ( ! defined('_SBG_CHARGING_FAIL_MORETHAN_LIMIT')) define('_SBG_CHARGING_FAIL_MORETHAN_LIMIT', 406);
+if ( ! defined('_SBG_CHARGING_GENERAL_ERROR')) define('_SBG_CHARGING_GENERAL_ERROR', 440);
+if ( ! defined('_SBG_CHARGING_NUMBER_NOT_REGISTED')) define('_SBG_CHARGING_NUMBER_NOT_REGISTED', 501);
+
 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_CHARGING_FAIL_MORETHAN_LIMIT_MSG')) define('_SBG_CHARGING_FAIL_MORETHAN_LIMIT_MSG', 'So tien nap gioi han lon nhat la 50,000 vnd');
+if ( ! defined('_SBG_CHARGING_FAIL_NOT_VIETTEL_MSG')) define('_SBG_CHARGING_FAIL_NOT_VIETTEL_MSG', 'Chi danh cho thue bao Viettel');
 
 if ( ! defined('_SBG_SEND_MT_FAIL')) define('_SBG_SEND_MT_FAIL', -1);
@@ -162,22 +174,32 @@
 				$userName = isset($aryContents[3]) ? $this->formatPhoneNumber($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 {
+				$result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
+				
+				if ($result == _SBG_CHARGING_SUCCESS) {
 					$smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
-					if (_SBG_MO_PAY_TO_ACCOUNT_FOR) {
+					if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
 						$arySmsReturn = explode ('|', $smsReturn);
 						$smsMT = $arySmsReturn[1] .' tu thue bao '. $sentNumber;
 						$sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $smsMT);
 					}
-				}
-				
-				//write to paid log
-				$paidTime = date('Y-m-d H:i:s');
-				$this->objUserModel->insertPaidlog($sentNumber, $userName, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
+					//write to paid log
+					$paidTime = date('Y-m-d H:i:s');
+					$this->objUserModel->insertPaidlog($sentNumber, $userName, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
+				}
+				else 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 if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
+					$aryPatterns = array ('/:sendid:/');
+					$smsReturn = preg_replace($aryPatterns, array($sentNumber), _SBG_CHARGING_FAIL_MORETHAN_LIMIT_MSG);
+					$smsReturn = '0|'.$smsReturn;
+				}else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED) {
+					$smsReturn = '0|'._SBG_CHARGING_FAIL_NOT_VIETTEL_MSG;
+				}				
+				else {
+					$smsReturn = '0|Loi, khong thanh toan duoc tu tai khoan goc';
+				}
 				break;
 			case _SBG_MO_UNSUBSCRIBE_SERVICE:
@@ -193,24 +215,4 @@
 	public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array()) 
 	{
-		/*
-	    ["BalanceAfterTransaction"]=>
-	    string(1) "0"
-	    ["ErrorCode"]=>
-	    string(3) "199"
-	    ["ErrorMessage"]=>
-	    string(29) "L?i exception Payment service"
-	    ["LastLockedOutDate"]=>
-	    string(19) "0001-01-01T00:00:00"
-	    ["RemainTimeToUnLock"]=>
-	    string(1) "0"
-	    ["Success"]=>
-	    string(5) "false"
-	    ["TransactionAmount"]=>
-	    string(1) "0"
-	    ["TransactionID"]=>
-	    string(1) "0"
-	    ["TransactionTime"]=>
-	    string(19) "0001-01-01T00:00:00"
-    	*/
 		$url = $this->config->item('topup_service_url');
 		$action = 'http://tempuri.org/ITopupService/TopUp';
@@ -223,4 +225,9 @@
 		
 		$result = $client->send($xml, $action);
+		
+		$debugMsg = 'Logged time: '. date('Y-m-d H:i:s') ."\n";
+		$debugMsg .=  'Send:'."\n" .$xml ."\n\n";
+		$debugMsg .= 'Result:'."\n". var_export($result, TRUE);
+		write_file('topup_log', $debugMsg);
 		
 		if ($client->fault) {
@@ -260,4 +267,7 @@
 		$client->operation = $action;
 		$result = $client->send($xml, $action);
+		$debugMsg = var_export($xml, TRUE);		
+		$debugMsg .= var_export($result, TRUE);
+		write_file('mt_log', $debugMsg);
 		
 		if ($client->fault) {
@@ -265,6 +275,16 @@
 		}
 		
-		if ((int)$result['processChargingResult'] == _SBG_CHARGING_FAIL_NOT_ENOUGH) 
-			return _SBG_CHARGING_FAIL_NOT_ENOUGH;
+		$aryErrs = array (_SBG_CHARGING_FAIL_NOT_ENOUGH, 
+				_SBG_CHARGING_FAIL_NOT_VIETTEL, 
+				_SBG_CHARGING_FAIL_MORETHAN_LIMIT,
+				_SBG_CHARGING_NUMBER_NOT_EXISTED,
+				_SBG_CHARGING_NUMBER_NOT_AVAILABLE,
+				_SBG_CHARGING_NUMBER_CHANGED_OWNER,
+				_SBG_CHARGING_GENERAL_ERROR,
+				_SBG_CHARGING_NUMBER_NOT_REGISTED
+		);
+		
+		if (in_array((int)$result['processChargingResult'], $aryErrs)) 
+			return (int)$result['processChargingResult'];		
 		
 		return _SBG_CHARGING_SUCCESS;
