source: pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php @ 887

Last change on this file since 887 was 884, checked in by dungnv, 10 years ago
File size: 20.2 KB
RevLine 
[499]1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
[647]3if ( ! defined('_SBG_MO_REGISTER'))                                     define('_SBG_MO_REGISTER',                              'SUBSCR');
4if ( ! defined('_SBG_MO_REGISTER_FOR'))                                 define('_SBG_MO_REGISTER_FOR',                  'SUBSCR_FOR');
5if ( ! defined('_SBG_MO_PHONENUM_CHANGE'))                              define('_SBG_MO_PHONENUM_CHANGE',               'PHONE_CHANGE');
6if ( ! defined('_SBG_MO_PASSWORD_RECOVERY'))                    define('_SBG_MO_PASSWORD_RECOVERY',     'PWD_RECOVER');
7if ( ! defined('_SBG_MO_VIP_REGISTER'))                                 define('_SBG_MO_VIP_REGISTER',                  'REG_VIP');
8if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT'))                               define('_SBG_MO_PAY_TO_ACCOUNT',                'PAY');
9if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT_FOR'))                   define('_SBG_MO_PAY_TO_ACCOUNT_FOR',    'PAY_FOR');
10if ( ! defined('_SBG_MO_UNSUBSCRIBE_SERVICE'))                  define('_SBG_MO_UNSUBSCRIBE_SERVICE',   'UNSUBSCR');
11if ( ! defined('_SBG_MO_SUPPOR_SERVICE'))                               define('_SBG_MO_SUPPOR_SERVICE',                'SUPPORT');
12if ( ! defined('_SBG_MO_SYNTAX_ERR'))                                   define('_SBG_MO_SYNTAX_ERR',                    'SYNTAX_ERR');
[499]13
[647]14if ( ! defined('_SBG_CARD_PAY_FAIL'))                                   define('_SBG_CARD_PAY_FAIL',                    -1);
15if ( ! defined('_SBG_CARD_PAY_SUCCESS'))                                define('_SBG_CARD_PAY_SUCCESS',                 1);
[499]16
[647]17if ( ! defined('_SBG_CHARGING_FAIL'))                                   define('_SBG_CHARGING_FAIL', -1);
18if ( ! defined('_SBG_CHARGING_SUCCESS'))                                define('_SBG_CHARGING_SUCCESS', 1);
19if ( ! defined('_SBG_CHARGING_FAIL_NOT_ENOUGH'))                define('_SBG_CHARGING_FAIL_NOT_ENOUGH', 401);
20if ( ! defined('_SBG_CHARGING_FAIL_NOT_VIETTEL'))               define('_SBG_CHARGING_FAIL_NOT_VIETTEL', 402);
21if ( ! defined('_SBG_CHARGING_NUMBER_NOT_EXISTED'))     define('_SBG_CHARGING_NUMBER_NOT_EXISTED', 403);
22if ( ! defined('_SBG_CHARGING_NUMBER_NOT_AVAILABLE'))   define('_SBG_CHARGING_NUMBER_NOT_AVAILABLE', 404);
23if ( ! defined('_SBG_CHARGING_NUMBER_CHANGED_OWNER'))   define('_SBG_CHARGING_NUMBER_CHANGED_OWNER', 405);
24if ( ! defined('_SBG_CHARGING_FAIL_MORETHAN_LIMIT'))    define('_SBG_CHARGING_FAIL_MORETHAN_LIMIT', 406);
25if ( ! defined('_SBG_CHARGING_GENERAL_ERROR'))                  define('_SBG_CHARGING_GENERAL_ERROR', 440);
26if ( ! defined('_SBG_CHARGING_NUMBER_NOT_REGISTED'))    define('_SBG_CHARGING_NUMBER_NOT_REGISTED', 501);
[716]27if ( ! defined('_SBG_CHARGING_MAX_VALUE'))                              define('_SBG_CHARGING_MAX_VALUE', 50000);
[884]28if ( ! defined('_SBG_CHARGING_MIN_VALUE'))                              define('_SBG_CHARGING_MIN_VALUE', 5000);
[499]29
[716]30
[647]31if ( ! defined('_SBG_SEND_MT_FAIL'))                                    define('_SBG_SEND_MT_FAIL', -1);
32if ( ! defined('_SBG_SEND_MT_SUCCESS'))                                 define('_SBG_SEND_MT_SUCCESS', 1);
33if ( ! defined('_SBG_MT_SEND_NUMBER'))                                  define('_SBG_MT_SEND_NUMBER', '841662860000');
34if ( ! defined('_SBG_MT_CP_CODE'))                                              define('_SBG_MT_CP_CODE', '601');
[499]35
[647]36if ( ! defined('_SBG_SERVICE_COMMAND_CODE'))                    define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
37if ( ! defined('_SBG_SERVICE_SERVICEID'))                               define('_SBG_SERVICE_SERVICEID', '8062');
[598]38
[647]39if ( ! defined('_SBG_MO_PROCCESS_ERR'))                                 define('_SBG_MO_PROCCESS_ERR', 0);
[598]40
[716]41if ( ! defined('_SBG_GET_COLLABORATOR_FAIL'))                   define('_SBG_GET_COLLABORATOR_FAIL', -1);
[685]42
[884]43Class Services_model extends CI_Model
[499]44{
45        private $_arySmsMO = array(
[647]46                _SBG_MO_REGISTER                        => array('pattern' => '/^(SBG)([[:blank:]])(DK)$/'),
47                _SBG_MO_REGISTER_FOR            => array('pattern' => '/^(SBG)([[:blank:]])(DK)([[:blank:]])([0-9]{10,11})$/'),
[707]48                //_SBG_MO_PHONENUM_CHANGE       => array('pattern' => '/^(SBG)([[:blank:]])(DDT)([[:blank:]])([0-9]{10,11})([[:blank:]])([a-zA-Z0-9]{1,})$/'),
[647]49                _SBG_MO_PASSWORD_RECOVERY       => array('pattern' => '/^(SBG)([[:blank:]])(MK)$/'),
50                _SBG_MO_VIP_REGISTER            => array('pattern' => '/^(SBG)([[:blank:]])(VIP)([[:blank:]])([a-zA-Z0-9]{1,})$/'),
51                _SBG_MO_PAY_TO_ACCOUNT          => array('pattern' => '/^(SBG)([[:blank:]])(NAP)([[:blank:]])([0-9]{3,})$/'),
52                _SBG_MO_PAY_TO_ACCOUNT_FOR      => array('pattern' => '/^(SBG)([[:blank:]])(NAP)([[:blank:]])([0-9]{3,})([[:blank:]])([0-9]{10,11})$/'),
53                _SBG_MO_SUPPOR_SERVICE          => array('pattern' => '/^(SBG)([[:blank:]])(HD)$/'),
54                _SBG_MO_UNSUBSCRIBE_SERVICE => array('pattern' => '/^(SBG)([[:blank:]])(HUY)$/')
[884]55        );
56
[499]57        public function __construct(){
58                parent::__construct();
[636]59                $this->load->helper('language');
60                $this->lang->load('messages', 'message');
[499]61        }
[884]62
[647]63        /**
64         * messageAnalys
[884]65         *
[647]66         * @todo Analysis content of incoming message
[884]67         *
[647]68         * @param String $message - coming SMS
69         * @return multitype:Ambigous <string, multitype> multitype:multitype
70         */
[579]71        public function messageAnalys ($message)
[499]72        {
[884]73                $aryMatched = array();
[579]74                $aryContent = array();
[647]75                $result = FALSE;
[884]76
[647]77                foreach ($this->_arySmsMO as $case => $pattern)
[499]78                {
[647]79                        $result = preg_match($pattern['pattern'], strtoupper(trim($message)));
80                        if ((bool)$result) {
81                                $aryMatched = explode(' ', trim($message));
82                                break;
83                        }
[499]84                }
[884]85
[647]86                $case = (bool)$result ? $case:_SBG_MO_SYNTAX_ERR;
[884]87
[647]88                foreach($aryMatched as $k => $val) {
[884]89                        if ($k > 1) $aryContent[] = $val;
[579]90                }
[884]91
[707]92                return array('case' => $case, 'content' => $aryContent);
[499]93        }
[884]94
[647]95        /**
96         * processMORequest
[884]97         *
[647]98         * @todo process action analysed from messageAnalys
[884]99         *
[647]100         * @param unknown $sentNumber
101         * @param unknown $message
102         * @return string
103         */
[884]104        public function processMORequest ($sentNumber, $message, &$mtBody = null)
[499]105        {
[513]106                $debugMsg = $message."\n";
[884]107
[575]108                $sentNumber = $this->formatPhoneNumber($sentNumber);
[884]109
[499]110                $smsReturn = '';
[884]111                $aryMOAnalys = $this->messageAnalys($message);
[499]112                $case = $aryMOAnalys['case'];
113                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
114                $this->load->model('frontend/user_model', 'objUserModel');
[513]115                $debugMsg .= $case;
[884]116
[499]117                switch ($case) {
118                        case _SBG_MO_REGISTER:
119                        case _SBG_MO_REGISTER_FOR:
[583]120                                if ($case == _SBG_MO_REGISTER_FOR) {
[647]121                                        if (!preg_match('/^([0-9]{10,11}$)$/', $aryContents[0])) {
[636]122                                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
[583]123                                                break;
124                                        }
125                                }
[884]126
127                                $userName = isset($aryContents[0]) ? $this->formatPhoneNumber($aryContents[0]) : $sentNumber;
[647]128                                $collaborator = isset($aryContents[0]) ? $sentNumber:null;
[579]129                                $collaboratorMsg = '';
[884]130
[579]131                                $sms = $this->objUserModel->register($userName, $collaborator, $collaboratorMsg);
[884]132
[528]133                                if ($case == _SBG_MO_REGISTER_FOR) {
[579]134                                        $smsReturn = $collaboratorMsg;
[528]135                                        $arySMS = explode ('|', $sms);
[768]136                                        if ((int)$arySMS[0] == 1 ) {
137                                                $mtBody = $arySMS[1];
138                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $mtBody);
139                                        }
[884]140
[528]141                                }
142                                else {
143                                        $smsReturn = $sms;
144                                }
[707]145
[499]146                                break;
[768]147                        /* case _SBG_MO_PHONENUM_CHANGE:
[647]148                                if (!preg_match('/^(09\d{8}|01\d{9})$/', $aryContents[0])) {
[636]149                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
[583]150                                        break;
151                                }
[884]152
[647]153                                $oldPhoneNumber = $this->formatPhoneNumber($aryContents[0]);
154                                $password = $aryContents[1];
[499]155                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
[884]156
[615]157                                $arySMS = explode('|', $smsReturn);
158                                $changeResult = (int)$arySMS[0];
[884]159
[636]160                                $aryPatterns = array('/:sentNumber:/');
161                                $sms = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_USER_OLD_PHONENUM_CHANGE_SUCCESS_MSG'));
[615]162                                $sendMT = $changeResult == 1 ? $this->sendMT($this->formatPhoneNumber($oldPhoneNumber, -1), $sms) : '';
[768]163                                break; */
[499]164                        case _SBG_MO_PASSWORD_RECOVERY:
[647]165                                $smsReturn = $this->objUserModel->changePassword($sentNumber);
166                                break;
[499]167                        case _SBG_MO_VIP_REGISTER:
[707]168                                $packageData = array();
[647]169                                $p_code = $aryContents[0];
[707]170                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code, $packageData);
[884]171
[707]172                                $arySmsReturn = explode('|', $smsReturn);
173                                $result = $arySmsReturn[0];
174
175                                if (trim($arySmsReturn[1]) == trim(lang('_SBG_MO_USER_REGISTER_VIP_FAIL_NOT_ENOUGH_MSG'))) {
[768]176                                        $maxCharging = 50000;
[707]177                                        $amount = $packageData ['p_price'];
[716]178                                        $count = $amount > $maxCharging ? ceil($amount/ _SBG_CHARGING_MAX_VALUE) : 1;
179                                        $value = 0;
180                                        for ($i = 1; $i <= $count; $i++) {
181                                                $value = $amount > _SBG_CHARGING_MAX_VALUE ? _SBG_CHARGING_MAX_VALUE : $amount;
[884]182                                                $amount -= $value;
[716]183                                                $result = $this->chargeRootAccountProcess($sentNumber, $value, $sentNumber.' paid for SBG');
[884]184                                        }
[707]185
186                                        if ($result == _SBG_CHARGING_SUCCESS) {
[716]187                                                $amount = $packageData ['p_price'];
[707]188                                                $this->objUserModel->rechargeAccount($sentNumber, $amount);
189                                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code, $packageData);
190                                                $paidTime = date('Y-m-d H:i:s');
191                                                $this->objUserModel->insertPaidlog($sentNumber, $sentNumber, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
192                                                $result = 1;
193                                        }
194                                }
195
196                                if ((int)$result == 1) $this->objUserModel->insertRegisterVipLog($sentNumber, $p_code, true);
[499]197                                break;
[707]198
[499]199                        case _SBG_MO_PAY_TO_ACCOUNT:
200                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
[716]201                                error_log(date('Y-m-d H:i:s').' -- DATA: '. var_export($aryContents, TRUE).' -- CASE: '. $case . "\n", 3, '/srv/www/sbg/log/pay.log');
[884]202
[647]203                                $userName = isset($aryContents[1]) ? $this->formatPhoneNumber($aryContents[1]) : $sentNumber;
[612]204                                if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR && !preg_match('/^(09\d{8}|01\d{9})$/', $userName)) {
[636]205                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
[603]206                                        break;
207                                }
[884]208
[647]209                                $amount = $aryContents[0];
[615]210                                if (!preg_match('/^[0-9]{3,}$/', $amount)) {
[636]211                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_AMOUNT_WRONG_FORMAT_MSG');
[615]212                                        break;
213                                }
[884]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
[598]220                                $result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
[884]221
[598]222                                if ($result == _SBG_CHARGING_SUCCESS) {
[499]223                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
[598]224                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
[579]225                                                $arySmsReturn = explode ('|', $smsReturn);
[653]226                                                $aryPatterns = array('/:amount:/', '/:sentNumber:/');
227                                                $smsMT = preg_replace($aryPatterns, array($amount, $sentNumber), lang('_SBG_MO_PAY_TO_ACCOUNT_FOR_MSG'));
[768]228                                                $mtBody = $smsMT;
[579]229                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $smsMT);
[707]230                                                $smsReturn = $arySmsReturn[0].'|'.preg_replace(array('/:amount:/','/:username:/'), array($amount, $userName), lang('_SBG_MO_PAY_TO_ACCOUNT_FOR_OTHER_SUCCESS_MSG'));
[556]231                                        }
[598]232                                        //write to paid log
233                                        $paidTime = date('Y-m-d H:i:s');
234                                        $this->objUserModel->insertPaidlog($sentNumber, $userName, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
[499]235                                }
[598]236                                else if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
[818]237                                        $aryPatterns = array ('/:sentNumber:/');
238                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
[636]239                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
[598]240                                }
241                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
[884]242                                        /* $aryPatterns = array ('/:sendid:/');
[636]243                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
[884]244                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn; */
245
246
247
248
[612]249                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
[636]250                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
[884]251                                }
[598]252                                else {
[636]253                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
[598]254                                }
[499]255                                break;
[884]256
[647]257                        case _SBG_MO_SUPPOR_SERVICE:
[818]258                                if ($this->isViettel($sentNumber)){
259                                        $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_VIETTEL_MSG');
260                                }else{
261                                        $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_NOT_VIETTEL_MSG');
262                                }
[647]263                                break;
[884]264
[529]265                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
[499]266                        case _SBG_MO_SYNTAX_ERR:
267                        default:
[636]268                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_SYNTAX_ERR_MSG');
[499]269                                break;
270                }
[884]271
[775]272                $aryTmp = explode('|', $smsReturn);
273                $mtBody = $aryTmp[1] . "\n" . $mtBody;
[884]274
[579]275                return $smsReturn.'|'.$case;
[499]276        }
[884]277
278
[647]279        /**
280         * cardPaidProcess
[884]281         *
[647]282         * @todo Call Topup webservice to check and lock prepaid-card
283         * @param Array $aryCardInfo
284         * @param Array $aryTopupResult
285         * @return string
286         */
[884]287        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
[499]288        {
289                $url = $this->config->item('topup_service_url');
290                $action = 'http://tempuri.org/ITopupService/TopUp';
291                $xml = $this->load->view('topup', $aryCardInfo, true);
[884]292
[499]293                $client = new nusoap_client($url);
294                $client->useHTTPPersistentConnection();
295                $client->soap_defencoding = 'UTF-8';
296                $client->operation = $action;
[884]297
[519]298                $result = $client->send($xml, $action);
[884]299
[499]300                if ($client->fault) {
301                        return _SBG_CARD_PAY_FAIL;
302                }
[884]303
[528]304                $keys = array_keys($result);
[884]305
[528]306                $aryTopupResult = $result[$keys[0]];
[632]307
308                $debugMsg = 'Logged time: '. date('Y-m-d H:i:s') ."\n";
309                $debugMsg .=  'Send:'."\n" .$xml ."\n\n";
310                $debugMsg .= 'Result:'."\n". var_export($result, TRUE);
311                write_file('./log/topup_log', $debugMsg);
312
[499]313                if ($aryTopupResult['Success'] == 'false') {
314                        return _SBG_CARD_PAY_FAIL;
315                }
[884]316
[499]317                return _SBG_CARD_PAY_SUCCESS;
318        }
[884]319
[647]320        /**
321         * chargeRootAccountProcess
[884]322         *
[647]323         * @todo Call charging webservice to charged fee from phone root account
324         * @param String $sentID
325         * @param Integer $amount
326         * @param String $contents
327         * @return string|number
328         */
[884]329        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
[499]330        {
331                $url = $this->config->item('charging_service_url');
332                $username = $this->config->item('charging_username');
333                $password = $this->config->item('charging_password');
[884]334                $action = 'http://tempuri.org/processCharging';
335
[499]336                $aryParams = array(
337                                'user' => $username,
338                                'pass' => $password,
339                                'MSISDN' => $sentID,
340                                'Charging' => $amount,
341                                'Contents' => $contents
342                );
[707]343
[883]344                $xml = $this->load->view('services/charging', $aryParams, true);
[499]345                $client = new nusoap_client($url);
346                $client->useHTTPPersistentConnection();
347                $client->soap_defencoding = 'UTF-8';
348                $client->operation = $action;
349                $result = $client->send($xml, $action);
350                if ($client->fault) {
351                        return _SBG_CHARGING_FAIL;
352                }
[884]353
354                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
355                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
[647]356                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
357                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
358                                                        _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
359                                                        _SBG_CHARGING_NUMBER_CHANGED_OWNER,
360                                                        _SBG_CHARGING_GENERAL_ERROR,
361                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
[884]362
363                if (in_array((int)$result['processChargingResult'], $aryErrs))
364                        return (int)$result['processChargingResult'];
365
[499]366                return _SBG_CHARGING_SUCCESS;
367        }
[884]368
[647]369        /**
370         * sendMT
[884]371         *
[647]372         * @todo Calling MT webservice to send a SMS to a phone number
373         * @param String $receiverID
374         * @param String $message
375         * @return string
376         */
[499]377        public function sendMT ($receiverID, $message)
378        {
379                $aryMatch = array();
[884]380
[499]381                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
382                        $receiverID = '84'. $aryMatch[2];
383                }
[884]384
[499]385                $username = $this->config->item('mt_username');
386                $password = $this->config->item('mt_password');
387                $action = 'http://tempuri.org/InsertMT';
[884]388
[499]389                $aryParams = array(
390                                'User' => $username,
391                                'Pass' => $password,
392                                'CPCode' => _SBG_MT_CP_CODE,
393                                'UserID' => _SBG_MT_SEND_NUMBER,
394                                'RequestID' => '4',
395                                'ReceiverID' => $receiverID,
396                                'ServiceID' => _SBG_SERVICE_SERVICEID,
397                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
398                                'ContentType' => '0',
399                                'Info' => $message
400                );
[884]401
[883]402                $xml = $this->load->view('services/mt', $aryParams, true);
[513]403                $url = $this->config->item('mt_service_url');
[499]404                $client = new nusoap_client($url);
405                $client->useHTTPPersistentConnection();
406                $client->soap_defencoding = 'UTF-8';
407                $client->operation = $action;
408                $result = $client->send($xml, $action);
[884]409
[499]410                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
411                        return _SBG_SEND_MT_FAIL;
412                }
[884]413
[499]414                return _SBG_SEND_MT_SUCCESS;
415        }
[884]416
[677]417        public function getCollaborator ($type = 2, $page = 10, $displaypage = 1) {
418                $action = 'http://tempuri.org/InsertMT';
419                $url = $this->config->item('bccs_service_url');
420                $username = $this->config->item('bccs_username');
421                $password = $this->config->item('bccs_password');
422                $wscode = 'getListShopAndStaftInfo';
423                $aryParams = array(
424                                'user' => $username,
425                                'pass' => $password,
426                                'wscode' => $wscode,
427                                'type' => $type,
428                                'page' => $page,
429                                'displaypage' => $displaypage
430                );
[884]431
[677]432                $xml = $this->load->view('bscc', $aryParams, true);
[884]433
[677]434                $client = new nusoap_client($url);
435                $client->useHTTPPersistentConnection();
436                $client->soap_defencoding = 'UTF-8';
[822]437                $client->decode_utf8 = false;
[884]438
[677]439                $client->operation = $action;
440                $result = $client->send($xml, $action);
[685]441                if ($client->fault || (int)$result['Result']['error'] != 0) {
442                        return _SBG_GET_COLLABORATOR_FAIL;
443                }
[884]444
[685]445                return $result['Result']['original'];
[677]446        }
[884]447
[647]448        /**
449         * formatPhoneNumber
[884]450         *
[647]451         * @todo format phone number to prefix 0 or 84
452         * @param String $phoneNumber
453         * @param Integer $direction
454         * @return String
455         */
[579]456        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
[838]457                $result = $phoneNumber;
[575]458                $aryMatch = array();
[579]459                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
[575]460                        $result = '0'. $aryMatch[2];
461                }
[579]462                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
[575]463                        $result = $phoneNumber;
464                }
[579]465                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
466                        $result = $phoneNumber;
467                }
468                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
469                        $result = '84'. $aryMatch[2];
470                }
[838]471                else if (preg_match('/^[1,9]([0-9]{8,9})$/', $phoneNumber, $aryMatch) == 1 && $direction = 2) {
472                        $result = '0'. $phoneNumber;
473                }
[884]474
[575]475                return $result;
476        }
[884]477
478
[688]479        public function syncCollaborator($data){
480                if ($data){
[884]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'].'",
[838]485                                                                                                                                                                                                full_name = "'.$data['full_name'].'",
486                                                                                                                                                                                                passwd = "'.$data['passwd'].'",
[884]487                                                                                                                                                                                                cellphone = "'.$data['cellphone'].'",
488                                                                                                                                                                                                precinct = "'.$data['precinct'].'",
[688]489                                                                                                                                                                                                province = "'.$data['province'].'",
[711]490                                                                                                                                                                                                created_time = "'.$data['created_time'].'",
[688]491                                                                                                                                                                                                updated_time = "'.$data['updated_time'].'"';
492                        $this->db->query($sql);
493                        $id = $this->db->insert_id();
494                        if ($this->db->affected_rows()){
[689]495                                //echo "Collaborator ID: ".$id.". Sync Success !<br/>";
496                                $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Sync Success !\n";
497                                write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
[688]498                        }else{
[689]499                                //echo "Collaborator ID: ".$id.". Sync fail!. Sql = ".$sql."<br/>";
500                                $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Sync fail!. Sql = ".$sql."\n";
501                                write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
[688]502                        }
503                }else {
[689]504                        $logMsg = date('Y-m-d H:i:s') . " Data is null \n";
505                        write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
[688]506                }
507        }
[884]508
[717]509        public function deleteCollaborator($id){
510                $sql = "DELETE FROM tblcollaborator WHERE id = ". $id;
511                $this->db->query($sql);
512                if ($this->db->affected_rows()){
[721]513                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete success!\n";
[717]514                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
515                }else{
[721]516                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete fail!. Sql = ".$sql."\n";
[717]517                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
518                }
519        }
[884]520
[717]521        public function getAllCollaboratorID(){
522                $sql = "Select id from tblcollaborator";
523                $query = $this->db->query($sql);
524                if ($this->db->affected_rows()){
525                        return $query->result();
526                }else
527                {
528                        return array();
529                }
530        }
[884]531
[818]532        public function isViettel($number) {
533                $prefixViettel = array (
534                                        '96', '97', '98',
535                                        '096', '097', '098', '162', '163', '164', '165', '166', '167', '168', '169',
536                                        '8496', '8497', '8498', '0162', '0163', '0164', '0165', '0166', '0167', '0168', '0169',
537                                        '84162', '84163', '84164', '84165', '84166', '84167', '84168', '84169'
538                );
539                foreach ( $prefixViettel as $value ) {
540                        if (strpos ( $number, $value ) === 0) {
541                                return 1;
542                        }
543                }
544                return 0;
545        }
[499]546}
Note: See TracBrowser for help on using the repository browser.