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

Last change on this file since 857 was 838, checked in by namnd, 10 years ago
File size: 20.4 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('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
345                if ($client->fault) {
346                        return _SBG_CHARGING_FAIL;
347                }
348               
349                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
350                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
351                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
352                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
353                                                        _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
354                                                        _SBG_CHARGING_NUMBER_CHANGED_OWNER,
355                                                        _SBG_CHARGING_GENERAL_ERROR,
356                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
357               
358                if (in_array((int)$result['processChargingResult'], $aryErrs))
359                        return (int)$result['processChargingResult'];           
360               
361                return _SBG_CHARGING_SUCCESS;
362        }
363       
364        /**
365         * sendMT
366         *
367         * @todo Calling MT webservice to send a SMS to a phone number
368         * @param String $receiverID
369         * @param String $message
370         * @return string
371         */
372        public function sendMT ($receiverID, $message)
373        {
374                $aryMatch = array();
375               
376                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
377                        $receiverID = '84'. $aryMatch[2];
378                }
379               
380                $username = $this->config->item('mt_username');
381                $password = $this->config->item('mt_password');
382                $action = 'http://tempuri.org/InsertMT';
383               
384                $aryParams = array(
385                                'User' => $username,
386                                'Pass' => $password,
387                                'CPCode' => _SBG_MT_CP_CODE,
388                                'UserID' => _SBG_MT_SEND_NUMBER,
389                                'RequestID' => '4',
390                                'ReceiverID' => $receiverID,
391                                'ServiceID' => _SBG_SERVICE_SERVICEID,
392                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
393                                'ContentType' => '0',
394                                'Info' => $message
395                );
396               
397                $xml = $this->load->view('mt', $aryParams, true);
398                $url = $this->config->item('mt_service_url');
399                $client = new nusoap_client($url);
400                $client->useHTTPPersistentConnection();
401                $client->soap_defencoding = 'UTF-8';
402                $client->operation = $action;
403                $result = $client->send($xml, $action);
404               
405                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
406                        return _SBG_SEND_MT_FAIL;
407                }
408               
409                return _SBG_SEND_MT_SUCCESS;
410        }
411       
412        public function getCollaborator ($type = 2, $page = 10, $displaypage = 1) {
413                $action = 'http://tempuri.org/InsertMT';
414                $url = $this->config->item('bccs_service_url');
415                $username = $this->config->item('bccs_username');
416                $password = $this->config->item('bccs_password');
417                $wscode = 'getListShopAndStaftInfo';
418                $aryParams = array(
419                                'user' => $username,
420                                'pass' => $password,
421                                'wscode' => $wscode,
422                                'type' => $type,
423                                'page' => $page,
424                                'displaypage' => $displaypage
425                );
426                $xml = $this->load->view('bscc', $aryParams, true);
427               
428                $client = new nusoap_client($url);
429                $client->useHTTPPersistentConnection();
430                $client->soap_defencoding = 'UTF-8';
431                $client->decode_utf8 = false;
432               
433                $client->operation = $action;
434                $result = $client->send($xml, $action);
435                if ($client->fault || (int)$result['Result']['error'] != 0) {
436                        return _SBG_GET_COLLABORATOR_FAIL;
437                }
438               
439                return $result['Result']['original'];
440        }
441       
442        /**
443         * formatPhoneNumber
444         *
445         * @todo format phone number to prefix 0 or 84
446         * @param String $phoneNumber
447         * @param Integer $direction
448         * @return String
449         */
450        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
451                $result = $phoneNumber;
452                $aryMatch = array();
453                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
454                        $result = '0'. $aryMatch[2];
455                }
456                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
457                        $result = $phoneNumber;
458                }
459                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
460                        $result = $phoneNumber;
461                }
462                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
463                        $result = '84'. $aryMatch[2];
464                }
465                else if (preg_match('/^[1,9]([0-9]{8,9})$/', $phoneNumber, $aryMatch) == 1 && $direction = 2) {
466                        $result = '0'. $phoneNumber;
467                }
468               
469                return $result;
470        }
471       
472       
473        public function syncCollaborator($data){
474                if ($data){
475                        $sql = $this->db->insert_string('tblcollaborator', $data) . ' ON DUPLICATE KEY UPDATE address = "'.$data['address'].'",
476                                                                                                                                                                                                code = "'.$data['code'].'",
477                                                                                                                                                                                                district = "'.$data['district'].'",
478                                                                                                                                                                                                email = "'.$data['email'].'",
479                                                                                                                                                                                                full_name = "'.$data['full_name'].'",
480                                                                                                                                                                                                passwd = "'.$data['passwd'].'",
481                                                                                                                                                                                                cellphone = "'.$data['cellphone'].'",
482                                                                                                                                                                                                precinct = "'.$data['precinct'].'",
483                                                                                                                                                                                                province = "'.$data['province'].'",
484                                                                                                                                                                                                created_time = "'.$data['created_time'].'",
485                                                                                                                                                                                                updated_time = "'.$data['updated_time'].'"';
486                        $this->db->query($sql);
487                        $id = $this->db->insert_id();
488                        if ($this->db->affected_rows()){
489                                //echo "Collaborator ID: ".$id.". Sync Success !<br/>";
490                                $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Sync Success !\n";
491                                write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
492                        }else{
493                                //echo "Collaborator ID: ".$id.". Sync fail!. Sql = ".$sql."<br/>";
494                                $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Sync fail!. Sql = ".$sql."\n";
495                                write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
496                        }
497                }else {
498                        $logMsg = date('Y-m-d H:i:s') . " Data is null \n";
499                        write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
500                }
501        }
502       
503        public function deleteCollaborator($id){
504                $sql = "DELETE FROM tblcollaborator WHERE id = ". $id;
505                $this->db->query($sql);
506                if ($this->db->affected_rows()){
507                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete success!\n";
508                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
509                }else{
510                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete fail!. Sql = ".$sql."\n";
511                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
512                }
513        }
514       
515        public function getAllCollaboratorID(){
516                $sql = "Select id from tblcollaborator";
517                $query = $this->db->query($sql);
518                if ($this->db->affected_rows()){
519                        return $query->result();
520                }else
521                {
522                        return array();
523                }
524        }
525       
526        public function isViettel($number) {
527                $prefixViettel = array (
528                                        '96', '97', '98',
529                                        '096', '097', '098', '162', '163', '164', '165', '166', '167', '168', '169',
530                                        '8496', '8497', '8498', '0162', '0163', '0164', '0165', '0166', '0167', '0168', '0169',
531                                        '84162', '84163', '84164', '84165', '84166', '84167', '84168', '84169'
532                );
533                foreach ( $prefixViettel as $value ) {
534                        if (strpos ( $number, $value ) === 0) {
535                                return 1;
536                        }
537                }
538                return 0;
539        }
540}
Note: See TracBrowser for help on using the repository browser.