Ignore:
Timestamp:
Apr 7, 2015 2:17:28 PM (10 years ago)
Author:
dungnv
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php

    r883 r884  
    2626if ( ! defined('_SBG_CHARGING_NUMBER_NOT_REGISTED'))    define('_SBG_CHARGING_NUMBER_NOT_REGISTED', 501);
    2727if ( ! defined('_SBG_CHARGING_MAX_VALUE'))                              define('_SBG_CHARGING_MAX_VALUE', 50000);
     28if ( ! defined('_SBG_CHARGING_MIN_VALUE'))                              define('_SBG_CHARGING_MIN_VALUE', 5000);
    2829
    2930
     
    4041if ( ! defined('_SBG_GET_COLLABORATOR_FAIL'))                   define('_SBG_GET_COLLABORATOR_FAIL', -1);
    4142
    42 Class Services_model extends CI_Model 
     43Class Services_model extends CI_Model
    4344{
    4445        private $_arySmsMO = array(
     
    5253                _SBG_MO_SUPPOR_SERVICE          => array('pattern' => '/^(SBG)([[:blank:]])(HD)$/'),
    5354                _SBG_MO_UNSUBSCRIBE_SERVICE => array('pattern' => '/^(SBG)([[:blank:]])(HUY)$/')
    54         );     
    55        
     55        );
     56
    5657        public function __construct(){
    5758                parent::__construct();
     
    5960                $this->lang->load('messages', 'message');
    6061        }
    61        
     62
    6263        /**
    6364         * messageAnalys
    64          * 
     65         *
    6566         * @todo Analysis content of incoming message
    66          * 
     67         *
    6768         * @param String $message - coming SMS
    6869         * @return multitype:Ambigous <string, multitype> multitype:multitype
     
    7071        public function messageAnalys ($message)
    7172        {
    72                 $aryMatched = array(); 
     73                $aryMatched = array();
    7374                $aryContent = array();
    7475                $result = FALSE;
    75                
     76
    7677                foreach ($this->_arySmsMO as $case => $pattern)
    7778                {
     
    8283                        }
    8384                }
    84                
     85
    8586                $case = (bool)$result ? $case:_SBG_MO_SYNTAX_ERR;
    86                
     87
    8788                foreach($aryMatched as $k => $val) {
    88                         if ($k > 1) $aryContent[] = $val;                       
    89                 }
    90                
     89                        if ($k > 1) $aryContent[] = $val;
     90                }
     91
    9192                return array('case' => $case, 'content' => $aryContent);
    9293        }
    93        
     94
    9495        /**
    9596         * processMORequest
    96          * 
     97         *
    9798         * @todo process action analysed from messageAnalys
    98          * 
     99         *
    99100         * @param unknown $sentNumber
    100101         * @param unknown $message
    101102         * @return string
    102103         */
    103         public function processMORequest ($sentNumber, $message, &$mtBody = null) 
     104        public function processMORequest ($sentNumber, $message, &$mtBody = null)
    104105        {
    105106                $debugMsg = $message."\n";
    106                
     107
    107108                $sentNumber = $this->formatPhoneNumber($sentNumber);
    108                
     109
    109110                $smsReturn = '';
    110                 $aryMOAnalys = $this->messageAnalys($message);         
     111                $aryMOAnalys = $this->messageAnalys($message);
    111112                $case = $aryMOAnalys['case'];
    112113                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
    113114                $this->load->model('frontend/user_model', 'objUserModel');
    114115                $debugMsg .= $case;
    115                
     116
    116117                switch ($case) {
    117118                        case _SBG_MO_REGISTER:
     
    123124                                        }
    124125                                }
    125                                
    126                                 $userName = isset($aryContents[0]) ? $this->formatPhoneNumber($aryContents[0]) : $sentNumber; 
     126
     127                                $userName = isset($aryContents[0]) ? $this->formatPhoneNumber($aryContents[0]) : $sentNumber;
    127128                                $collaborator = isset($aryContents[0]) ? $sentNumber:null;
    128129                                $collaboratorMsg = '';
    129                                
     130
    130131                                $sms = $this->objUserModel->register($userName, $collaborator, $collaboratorMsg);
    131                                
     132
    132133                                if ($case == _SBG_MO_REGISTER_FOR) {
    133134                                        $smsReturn = $collaboratorMsg;
     
    137138                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $mtBody);
    138139                                        }
    139                                                                                
     140
    140141                                }
    141142                                else {
     
    149150                                        break;
    150151                                }
    151                                
     152
    152153                                $oldPhoneNumber = $this->formatPhoneNumber($aryContents[0]);
    153154                                $password = $aryContents[1];
    154155                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
    155                                
     156
    156157                                $arySMS = explode('|', $smsReturn);
    157158                                $changeResult = (int)$arySMS[0];
    158                                
     159
    159160                                $aryPatterns = array('/:sentNumber:/');
    160161                                $sms = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_USER_OLD_PHONENUM_CHANGE_SUCCESS_MSG'));
     
    168169                                $p_code = $aryContents[0];
    169170                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code, $packageData);
    170                                
     171
    171172                                $arySmsReturn = explode('|', $smsReturn);
    172173                                $result = $arySmsReturn[0];
     
    179180                                        for ($i = 1; $i <= $count; $i++) {
    180181                                                $value = $amount > _SBG_CHARGING_MAX_VALUE ? _SBG_CHARGING_MAX_VALUE : $amount;
    181                                                 $amount -= $value;                                                     
     182                                                $amount -= $value;
    182183                                                $result = $this->chargeRootAccountProcess($sentNumber, $value, $sentNumber.' paid for SBG');
    183                                         }       
     184                                        }
    184185
    185186                                        if ($result == _SBG_CHARGING_SUCCESS) {
     
    199200                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
    200201                                error_log(date('Y-m-d H:i:s').' -- DATA: '. var_export($aryContents, TRUE).' -- CASE: '. $case . "\n", 3, '/srv/www/sbg/log/pay.log');
     202
    201203                                $userName = isset($aryContents[1]) ? $this->formatPhoneNumber($aryContents[1]) : $sentNumber;
    202204                                if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR && !preg_match('/^(09\d{8}|01\d{9})$/', $userName)) {
     
    204206                                        break;
    205207                                }
    206                                
     208
    207209                                $amount = $aryContents[0];
    208210                                if (!preg_match('/^[0-9]{3,}$/', $amount)) {
     
    210212                                        break;
    211213                                }
    212                                
     214
     215                                if ((int)$amount < _SBG_CHARGING_MIN_VALUE) {
     216                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_AMOUNT_LESS_THAN_MIN_VALUE_MSG');
     217                                        break;
     218                                }
     219
    213220                                $result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
    214                                
     221
    215222                                if ($result == _SBG_CHARGING_SUCCESS) {
    216223                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
     
    228235                                }
    229236                                else if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
    230                                         //$aryPatterns = array ('/:amount:/');
    231237                                        $aryPatterns = array ('/:sentNumber:/');
    232238                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
    233                                         /*
    234                                         if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
    235                                                 $aryPatterns = array ('/:amount:/', '/:username:/');
    236                                                 $smsReturn = preg_replace($aryPatterns, array($amount, $userName), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_FOR_MSG'));
    237                                         }*/
    238239                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
    239240                                }
    240241                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
    241                                         $aryPatterns = array ('/:sendid:/');
     242                                        /* $aryPatterns = array ('/:sendid:/');
    242243                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
    243                                         $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
     244                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn; */
     245
     246
     247
     248
    244249                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
    245250                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
    246                                 }                               
     251                                }
    247252                                else {
    248253                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
    249254                                }
    250255                                break;
     256
    251257                        case _SBG_MO_SUPPOR_SERVICE:
    252                                 //$smsReturn = '1|'.lang('_SBG_MO_SUPPORT_MSG');
    253258                                if ($this->isViettel($sentNumber)){
    254259                                        $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_VIETTEL_MSG');
     
    257262                                }
    258263                                break;
     264
    259265                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
    260266                        case _SBG_MO_SYNTAX_ERR:
     
    263269                                break;
    264270                }
    265                
     271
    266272                $aryTmp = explode('|', $smsReturn);
    267273                $mtBody = $aryTmp[1] . "\n" . $mtBody;
    268                
     274
    269275                return $smsReturn.'|'.$case;
    270276        }
    271        
    272        
     277
     278
    273279        /**
    274280         * cardPaidProcess
    275          * 
     281         *
    276282         * @todo Call Topup webservice to check and lock prepaid-card
    277283         * @param Array $aryCardInfo
     
    279285         * @return string
    280286         */
    281         public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array()) 
     287        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
    282288        {
    283289                $url = $this->config->item('topup_service_url');
    284290                $action = 'http://tempuri.org/ITopupService/TopUp';
    285291                $xml = $this->load->view('topup', $aryCardInfo, true);
    286                
     292
    287293                $client = new nusoap_client($url);
    288294                $client->useHTTPPersistentConnection();
    289295                $client->soap_defencoding = 'UTF-8';
    290296                $client->operation = $action;
    291                
     297
    292298                $result = $client->send($xml, $action);
    293                
     299
    294300                if ($client->fault) {
    295301                        return _SBG_CARD_PAY_FAIL;
    296302                }
    297                
     303
    298304                $keys = array_keys($result);
    299                
     305
    300306                $aryTopupResult = $result[$keys[0]];
    301307
     
    308314                        return _SBG_CARD_PAY_FAIL;
    309315                }
    310                
     316
    311317                return _SBG_CARD_PAY_SUCCESS;
    312318        }
    313        
     319
    314320        /**
    315321         * chargeRootAccountProcess
    316          * 
     322         *
    317323         * @todo Call charging webservice to charged fee from phone root account
    318324         * @param String $sentID
     
    321327         * @return string|number
    322328         */
    323         public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '') 
     329        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
    324330        {
    325331                $url = $this->config->item('charging_service_url');
    326332                $username = $this->config->item('charging_username');
    327333                $password = $this->config->item('charging_password');
    328                 $action = 'http://tempuri.org/processCharging';         
    329                
     334                $action = 'http://tempuri.org/processCharging';
     335
    330336                $aryParams = array(
    331337                                'user' => $username,
     
    345351                        return _SBG_CHARGING_FAIL;
    346352                }
    347                
    348                 $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH, 
    349                                                         _SBG_CHARGING_FAIL_NOT_VIETTEL, 
     353
     354                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
     355                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
    350356                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
    351357                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
     
    354360                                                        _SBG_CHARGING_GENERAL_ERROR,
    355361                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
    356                
    357                 if (in_array((int)$result['processChargingResult'], $aryErrs)) 
    358                         return (int)$result['processChargingResult'];           
    359                
     362
     363                if (in_array((int)$result['processChargingResult'], $aryErrs))
     364                        return (int)$result['processChargingResult'];
     365
    360366                return _SBG_CHARGING_SUCCESS;
    361367        }
    362        
     368
    363369        /**
    364370         * sendMT
    365          * 
     371         *
    366372         * @todo Calling MT webservice to send a SMS to a phone number
    367373         * @param String $receiverID
     
    372378        {
    373379                $aryMatch = array();
    374                
     380
    375381                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
    376382                        $receiverID = '84'. $aryMatch[2];
    377383                }
    378                
     384
    379385                $username = $this->config->item('mt_username');
    380386                $password = $this->config->item('mt_password');
    381387                $action = 'http://tempuri.org/InsertMT';
    382                
     388
    383389                $aryParams = array(
    384390                                'User' => $username,
     
    393399                                'Info' => $message
    394400                );
    395                
     401
    396402                $xml = $this->load->view('services/mt', $aryParams, true);
    397403                $url = $this->config->item('mt_service_url');
     
    401407                $client->operation = $action;
    402408                $result = $client->send($xml, $action);
    403                
     409
    404410                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
    405411                        return _SBG_SEND_MT_FAIL;
    406412                }
    407                
     413
    408414                return _SBG_SEND_MT_SUCCESS;
    409415        }
    410        
     416
    411417        public function getCollaborator ($type = 2, $page = 10, $displaypage = 1) {
    412418                $action = 'http://tempuri.org/InsertMT';
     
    423429                                'displaypage' => $displaypage
    424430                );
     431
    425432                $xml = $this->load->view('bscc', $aryParams, true);
    426                
     433
    427434                $client = new nusoap_client($url);
    428435                $client->useHTTPPersistentConnection();
    429436                $client->soap_defencoding = 'UTF-8';
    430437                $client->decode_utf8 = false;
    431                
     438
    432439                $client->operation = $action;
    433440                $result = $client->send($xml, $action);
     
    435442                        return _SBG_GET_COLLABORATOR_FAIL;
    436443                }
    437                
     444
    438445                return $result['Result']['original'];
    439446        }
    440        
     447
    441448        /**
    442449         * formatPhoneNumber
    443          * 
     450         *
    444451         * @todo format phone number to prefix 0 or 84
    445452         * @param String $phoneNumber
     
    465472                        $result = '0'. $phoneNumber;
    466473                }
    467                
     474
    468475                return $result;
    469476        }
    470        
    471        
     477
     478
    472479        public function syncCollaborator($data){
    473480                if ($data){
    474                         $sql = $this->db->insert_string('tblcollaborator', $data) . ' ON DUPLICATE KEY UPDATE address = "'.$data['address'].'", 
    475                                                                                                                                                                                                 code = "'.$data['code'].'", 
    476                                                                                                                                                                                                 district = "'.$data['district'].'", 
    477                                                                                                                                                                                                 email = "'.$data['email'].'", 
     481                        $sql = $this->db->insert_string('tblcollaborator', $data) . ' ON DUPLICATE KEY UPDATE address = "'.$data['address'].'",
     482                                                                                                                                                                                                code = "'.$data['code'].'",
     483                                                                                                                                                                                                district = "'.$data['district'].'",
     484                                                                                                                                                                                                email = "'.$data['email'].'",
    478485                                                                                                                                                                                                full_name = "'.$data['full_name'].'",
    479486                                                                                                                                                                                                passwd = "'.$data['passwd'].'",
    480                                                                                                                                                                                                 cellphone = "'.$data['cellphone'].'", 
    481                                                                                                                                                                                                 precinct = "'.$data['precinct'].'", 
     487                                                                                                                                                                                                cellphone = "'.$data['cellphone'].'",
     488                                                                                                                                                                                                precinct = "'.$data['precinct'].'",
    482489                                                                                                                                                                                                province = "'.$data['province'].'",
    483490                                                                                                                                                                                                created_time = "'.$data['created_time'].'",
     
    499506                }
    500507        }
    501        
     508
    502509        public function deleteCollaborator($id){
    503510                $sql = "DELETE FROM tblcollaborator WHERE id = ". $id;
     
    511518                }
    512519        }
    513        
     520
    514521        public function getAllCollaboratorID(){
    515522                $sql = "Select id from tblcollaborator";
     
    522529                }
    523530        }
    524        
     531
    525532        public function isViettel($number) {
    526533                $prefixViettel = array (
Note: See TracChangeset for help on using the changeset viewer.