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
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);
28if ( ! defined('_SBG_CHARGING_MIN_VALUE'))                              define('_SBG_CHARGING_MIN_VALUE', 5000);
29
30
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');
35
36if ( ! defined('_SBG_SERVICE_COMMAND_CODE'))                    define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
37if ( ! defined('_SBG_SERVICE_SERVICEID'))                               define('_SBG_SERVICE_SERVICEID', '8062');
38
39if ( ! defined('_SBG_MO_PROCCESS_ERR'))                                 define('_SBG_MO_PROCCESS_ERR', 0);
40
41if ( ! defined('_SBG_GET_COLLABORATOR_FAIL'))                   define('_SBG_GET_COLLABORATOR_FAIL', -1);
42
43Class Services_model extends CI_Model
44{
45        private $_arySmsMO = array(
46                _SBG_MO_REGISTER                        => array('pattern' => '/^(SBG)([[:blank:]])(DK)$/'),
47                _SBG_MO_REGISTER_FOR            => array('pattern' => '/^(SBG)([[:blank:]])(DK)([[:blank:]])([0-9]{10,11})$/'),
48                //_SBG_MO_PHONENUM_CHANGE       => array('pattern' => '/^(SBG)([[:blank:]])(DDT)([[:blank:]])([0-9]{10,11})([[:blank:]])([a-zA-Z0-9]{1,})$/'),
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)$/')
55        );
56
57        public function __construct(){
58                parent::__construct();
59                $this->load->helper('language');
60                $this->lang->load('messages', 'message');
61        }
62
63        /**
64         * messageAnalys
65         *
66         * @todo Analysis content of incoming message
67         *
68         * @param String $message - coming SMS
69         * @return multitype:Ambigous <string, multitype> multitype:multitype
70         */
71        public function messageAnalys ($message)
72        {
73                $aryMatched = array();
74                $aryContent = array();
75                $result = FALSE;
76
77                foreach ($this->_arySmsMO as $case => $pattern)
78                {
79                        $result = preg_match($pattern['pattern'], strtoupper(trim($message)));
80                        if ((bool)$result) {
81                                $aryMatched = explode(' ', trim($message));
82                                break;
83                        }
84                }
85
86                $case = (bool)$result ? $case:_SBG_MO_SYNTAX_ERR;
87
88                foreach($aryMatched as $k => $val) {
89                        if ($k > 1) $aryContent[] = $val;
90                }
91
92                return array('case' => $case, 'content' => $aryContent);
93        }
94
95        /**
96         * processMORequest
97         *
98         * @todo process action analysed from messageAnalys
99         *
100         * @param unknown $sentNumber
101         * @param unknown $message
102         * @return string
103         */
104        public function processMORequest ($sentNumber, $message, &$mtBody = null)
105        {
106                $debugMsg = $message."\n";
107
108                $sentNumber = $this->formatPhoneNumber($sentNumber);
109
110                $smsReturn = '';
111                $aryMOAnalys = $this->messageAnalys($message);
112                $case = $aryMOAnalys['case'];
113                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
114                $this->load->model('frontend/user_model', 'objUserModel');
115                $debugMsg .= $case;
116
117                switch ($case) {
118                        case _SBG_MO_REGISTER:
119                        case _SBG_MO_REGISTER_FOR:
120                                if ($case == _SBG_MO_REGISTER_FOR) {
121                                        if (!preg_match('/^([0-9]{10,11}$)$/', $aryContents[0])) {
122                                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
123                                                break;
124                                        }
125                                }
126
127                                $userName = isset($aryContents[0]) ? $this->formatPhoneNumber($aryContents[0]) : $sentNumber;
128                                $collaborator = isset($aryContents[0]) ? $sentNumber:null;
129                                $collaboratorMsg = '';
130
131                                $sms = $this->objUserModel->register($userName, $collaborator, $collaboratorMsg);
132
133                                if ($case == _SBG_MO_REGISTER_FOR) {
134                                        $smsReturn = $collaboratorMsg;
135                                        $arySMS = explode ('|', $sms);
136                                        if ((int)$arySMS[0] == 1 ) {
137                                                $mtBody = $arySMS[1];
138                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $mtBody);
139                                        }
140
141                                }
142                                else {
143                                        $smsReturn = $sms;
144                                }
145
146                                break;
147                        /* case _SBG_MO_PHONENUM_CHANGE:
148                                if (!preg_match('/^(09\d{8}|01\d{9})$/', $aryContents[0])) {
149                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
150                                        break;
151                                }
152
153                                $oldPhoneNumber = $this->formatPhoneNumber($aryContents[0]);
154                                $password = $aryContents[1];
155                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
156
157                                $arySMS = explode('|', $smsReturn);
158                                $changeResult = (int)$arySMS[0];
159
160                                $aryPatterns = array('/:sentNumber:/');
161                                $sms = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_USER_OLD_PHONENUM_CHANGE_SUCCESS_MSG'));
162                                $sendMT = $changeResult == 1 ? $this->sendMT($this->formatPhoneNumber($oldPhoneNumber, -1), $sms) : '';
163                                break; */
164                        case _SBG_MO_PASSWORD_RECOVERY:
165                                $smsReturn = $this->objUserModel->changePassword($sentNumber);
166                                break;
167                        case _SBG_MO_VIP_REGISTER:
168                                $packageData = array();
169                                $p_code = $aryContents[0];
170                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code, $packageData);
171
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'))) {
176                                        $maxCharging = 50000;
177                                        $amount = $packageData ['p_price'];
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;
182                                                $amount -= $value;
183                                                $result = $this->chargeRootAccountProcess($sentNumber, $value, $sentNumber.' paid for SBG');
184                                        }
185
186                                        if ($result == _SBG_CHARGING_SUCCESS) {
187                                                $amount = $packageData ['p_price'];
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);
197                                break;
198
199                        case _SBG_MO_PAY_TO_ACCOUNT:
200                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
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');
202
203                                $userName = isset($aryContents[1]) ? $this->formatPhoneNumber($aryContents[1]) : $sentNumber;
204                                if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR && !preg_match('/^(09\d{8}|01\d{9})$/', $userName)) {
205                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
206                                        break;
207                                }
208
209                                $amount = $aryContents[0];
210                                if (!preg_match('/^[0-9]{3,}$/', $amount)) {
211                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_AMOUNT_WRONG_FORMAT_MSG');
212                                        break;
213                                }
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
220                                $result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
221
222                                if ($result == _SBG_CHARGING_SUCCESS) {
223                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
224                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
225                                                $arySmsReturn = explode ('|', $smsReturn);
226                                                $aryPatterns = array('/:amount:/', '/:sentNumber:/');
227                                                $smsMT = preg_replace($aryPatterns, array($amount, $sentNumber), lang('_SBG_MO_PAY_TO_ACCOUNT_FOR_MSG'));
228                                                $mtBody = $smsMT;
229                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $smsMT);
230                                                $smsReturn = $arySmsReturn[0].'|'.preg_replace(array('/:amount:/','/:username:/'), array($amount, $userName), lang('_SBG_MO_PAY_TO_ACCOUNT_FOR_OTHER_SUCCESS_MSG'));
231                                        }
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);
235                                }
236                                else if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
237                                        $aryPatterns = array ('/:sentNumber:/');
238                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
239                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
240                                }
241                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
242                                        /* $aryPatterns = array ('/:sendid:/');
243                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
244                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn; */
245
246
247
248
249                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
250                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
251                                }
252                                else {
253                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
254                                }
255                                break;
256
257                        case _SBG_MO_SUPPOR_SERVICE:
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                                }
263                                break;
264
265                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
266                        case _SBG_MO_SYNTAX_ERR:
267                        default:
268                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_SYNTAX_ERR_MSG');
269                                break;
270                }
271
272                $aryTmp = explode('|', $smsReturn);
273                $mtBody = $aryTmp[1] . "\n" . $mtBody;
274
275                return $smsReturn.'|'.$case;
276        }
277
278
279        /**
280         * cardPaidProcess
281         *
282         * @todo Call Topup webservice to check and lock prepaid-card
283         * @param Array $aryCardInfo
284         * @param Array $aryTopupResult
285         * @return string
286         */
287        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
288        {
289                $url = $this->config->item('topup_service_url');
290                $action = 'http://tempuri.org/ITopupService/TopUp';
291                $xml = $this->load->view('topup', $aryCardInfo, true);
292
293                $client = new nusoap_client($url);
294                $client->useHTTPPersistentConnection();
295                $client->soap_defencoding = 'UTF-8';
296                $client->operation = $action;
297
298                $result = $client->send($xml, $action);
299
300                if ($client->fault) {
301                        return _SBG_CARD_PAY_FAIL;
302                }
303
304                $keys = array_keys($result);
305
306                $aryTopupResult = $result[$keys[0]];
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
313                if ($aryTopupResult['Success'] == 'false') {
314                        return _SBG_CARD_PAY_FAIL;
315                }
316
317                return _SBG_CARD_PAY_SUCCESS;
318        }
319
320        /**
321         * chargeRootAccountProcess
322         *
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         */
329        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
330        {
331                $url = $this->config->item('charging_service_url');
332                $username = $this->config->item('charging_username');
333                $password = $this->config->item('charging_password');
334                $action = 'http://tempuri.org/processCharging';
335
336                $aryParams = array(
337                                'user' => $username,
338                                'pass' => $password,
339                                'MSISDN' => $sentID,
340                                'Charging' => $amount,
341                                'Contents' => $contents
342                );
343
344                $xml = $this->load->view('services/charging', $aryParams, true);
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                }
353
354                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
355                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
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);
362
363                if (in_array((int)$result['processChargingResult'], $aryErrs))
364                        return (int)$result['processChargingResult'];
365
366                return _SBG_CHARGING_SUCCESS;
367        }
368
369        /**
370         * sendMT
371         *
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         */
377        public function sendMT ($receiverID, $message)
378        {
379                $aryMatch = array();
380
381                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
382                        $receiverID = '84'. $aryMatch[2];
383                }
384
385                $username = $this->config->item('mt_username');
386                $password = $this->config->item('mt_password');
387                $action = 'http://tempuri.org/InsertMT';
388
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                );
401
402                $xml = $this->load->view('services/mt', $aryParams, true);
403                $url = $this->config->item('mt_service_url');
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);
409
410                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
411                        return _SBG_SEND_MT_FAIL;
412                }
413
414                return _SBG_SEND_MT_SUCCESS;
415        }
416
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                );
431
432                $xml = $this->load->view('bscc', $aryParams, true);
433
434                $client = new nusoap_client($url);
435                $client->useHTTPPersistentConnection();
436                $client->soap_defencoding = 'UTF-8';
437                $client->decode_utf8 = false;
438
439                $client->operation = $action;
440                $result = $client->send($xml, $action);
441                if ($client->fault || (int)$result['Result']['error'] != 0) {
442                        return _SBG_GET_COLLABORATOR_FAIL;
443                }
444
445                return $result['Result']['original'];
446        }
447
448        /**
449         * formatPhoneNumber
450         *
451         * @todo format phone number to prefix 0 or 84
452         * @param String $phoneNumber
453         * @param Integer $direction
454         * @return String
455         */
456        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
457                $result = $phoneNumber;
458                $aryMatch = array();
459                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
460                        $result = '0'. $aryMatch[2];
461                }
462                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
463                        $result = $phoneNumber;
464                }
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                }
471                else if (preg_match('/^[1,9]([0-9]{8,9})$/', $phoneNumber, $aryMatch) == 1 && $direction = 2) {
472                        $result = '0'. $phoneNumber;
473                }
474
475                return $result;
476        }
477
478
479        public function syncCollaborator($data){
480                if ($data){
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'].'",
485                                                                                                                                                                                                full_name = "'.$data['full_name'].'",
486                                                                                                                                                                                                passwd = "'.$data['passwd'].'",
487                                                                                                                                                                                                cellphone = "'.$data['cellphone'].'",
488                                                                                                                                                                                                precinct = "'.$data['precinct'].'",
489                                                                                                                                                                                                province = "'.$data['province'].'",
490                                                                                                                                                                                                created_time = "'.$data['created_time'].'",
491                                                                                                                                                                                                updated_time = "'.$data['updated_time'].'"';
492                        $this->db->query($sql);
493                        $id = $this->db->insert_id();
494                        if ($this->db->affected_rows()){
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);
498                        }else{
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);
502                        }
503                }else {
504                        $logMsg = date('Y-m-d H:i:s') . " Data is null \n";
505                        write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
506                }
507        }
508
509        public function deleteCollaborator($id){
510                $sql = "DELETE FROM tblcollaborator WHERE id = ". $id;
511                $this->db->query($sql);
512                if ($this->db->affected_rows()){
513                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete success!\n";
514                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
515                }else{
516                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete fail!. Sql = ".$sql."\n";
517                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
518                }
519        }
520
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        }
531
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        }
546}
Note: See TracBrowser for help on using the repository browser.