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

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