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

Last change on this file since 775 was 775, checked in by dungnv, 10 years ago
File size: 19.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                                        $smsReturn = preg_replace($aryPatterns, array($amount), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
232                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
233                                                $aryPatterns = array ('/:amount:/', '/:username:/');
234                                                $smsReturn = preg_replace($aryPatterns, array($amount, $userName), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_FOR_MSG'));
235                                        }
236                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
237                                }
238                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
239                                        $aryPatterns = array ('/:sendid:/');
240                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
241                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
242                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
243                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
244                                }                               
245                                else {
246                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
247                                }
248                                break;
249                        case _SBG_MO_SUPPOR_SERVICE:
250                                $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_MSG');
251                                break;
252                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
253                        case _SBG_MO_SYNTAX_ERR:
254                        default:
255                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_SYNTAX_ERR_MSG');
256                                break;
257                }
258               
259                $aryTmp = explode('|', $smsReturn);
260                $mtBody = $aryTmp[1] . "\n" . $mtBody;
261               
262                return $smsReturn.'|'.$case;
263        }
264       
265       
266        /**
267         * cardPaidProcess
268         *
269         * @todo Call Topup webservice to check and lock prepaid-card
270         * @param Array $aryCardInfo
271         * @param Array $aryTopupResult
272         * @return string
273         */
274        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
275        {
276                $url = $this->config->item('topup_service_url');
277                $action = 'http://tempuri.org/ITopupService/TopUp';
278                $xml = $this->load->view('topup', $aryCardInfo, true);
279               
280                $client = new nusoap_client($url);
281                $client->useHTTPPersistentConnection();
282                $client->soap_defencoding = 'UTF-8';
283                $client->operation = $action;
284               
285                $result = $client->send($xml, $action);
286               
287                if ($client->fault) {
288                        return _SBG_CARD_PAY_FAIL;
289                }
290               
291                $keys = array_keys($result);
292               
293                $aryTopupResult = $result[$keys[0]];
294
295                $debugMsg = 'Logged time: '. date('Y-m-d H:i:s') ."\n";
296                $debugMsg .=  'Send:'."\n" .$xml ."\n\n";
297                $debugMsg .= 'Result:'."\n". var_export($result, TRUE);
298                write_file('./log/topup_log', $debugMsg);
299
300                if ($aryTopupResult['Success'] == 'false') {
301                        return _SBG_CARD_PAY_FAIL;
302                }
303               
304                return _SBG_CARD_PAY_SUCCESS;
305        }
306       
307        /**
308         * chargeRootAccountProcess
309         *
310         * @todo Call charging webservice to charged fee from phone root account
311         * @param String $sentID
312         * @param Integer $amount
313         * @param String $contents
314         * @return string|number
315         */
316        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
317        {
318                $url = $this->config->item('charging_service_url');
319                $username = $this->config->item('charging_username');
320                $password = $this->config->item('charging_password');
321                $action = 'http://tempuri.org/processCharging';         
322               
323                $aryParams = array(
324                                'user' => $username,
325                                'pass' => $password,
326                                'MSISDN' => $sentID,
327                                'Charging' => $amount,
328                                'Contents' => $contents
329                );
330
331                $xml = $this->load->view('charging', $aryParams, true);
332                $client = new nusoap_client($url);
333                $client->useHTTPPersistentConnection();
334                $client->soap_defencoding = 'UTF-8';
335                $client->operation = $action;
336                $result = $client->send($xml, $action);
337
338                if ($client->fault) {
339                        return _SBG_CHARGING_FAIL;
340                }
341               
342                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
343                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
344                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
345                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
346                                                        _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
347                                                        _SBG_CHARGING_NUMBER_CHANGED_OWNER,
348                                                        _SBG_CHARGING_GENERAL_ERROR,
349                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
350               
351                if (in_array((int)$result['processChargingResult'], $aryErrs))
352                        return (int)$result['processChargingResult'];           
353               
354                return _SBG_CHARGING_SUCCESS;
355        }
356       
357        /**
358         * sendMT
359         *
360         * @todo Calling MT webservice to send a SMS to a phone number
361         * @param String $receiverID
362         * @param String $message
363         * @return string
364         */
365        public function sendMT ($receiverID, $message)
366        {
367                $aryMatch = array();
368               
369                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
370                        $receiverID = '84'. $aryMatch[2];
371                }
372               
373                $username = $this->config->item('mt_username');
374                $password = $this->config->item('mt_password');
375                $action = 'http://tempuri.org/InsertMT';
376               
377                $aryParams = array(
378                                'User' => $username,
379                                'Pass' => $password,
380                                'CPCode' => _SBG_MT_CP_CODE,
381                                'UserID' => _SBG_MT_SEND_NUMBER,
382                                'RequestID' => '4',
383                                'ReceiverID' => $receiverID,
384                                'ServiceID' => _SBG_SERVICE_SERVICEID,
385                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
386                                'ContentType' => '0',
387                                'Info' => $message
388                );
389               
390                $xml = $this->load->view('mt', $aryParams, true);
391                $url = $this->config->item('mt_service_url');
392                $client = new nusoap_client($url);
393                $client->useHTTPPersistentConnection();
394                $client->soap_defencoding = 'UTF-8';
395                $client->operation = $action;
396                $result = $client->send($xml, $action);
397               
398                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
399                        return _SBG_SEND_MT_FAIL;
400                }
401               
402                return _SBG_SEND_MT_SUCCESS;
403        }
404       
405        public function getCollaborator ($type = 2, $page = 10, $displaypage = 1) {
406                $action = 'http://tempuri.org/InsertMT';
407                $url = $this->config->item('bccs_service_url');
408                $username = $this->config->item('bccs_username');
409                $password = $this->config->item('bccs_password');
410                $wscode = 'getListShopAndStaftInfo';
411                $aryParams = array(
412                                'user' => $username,
413                                'pass' => $password,
414                                'wscode' => $wscode,
415                                'type' => $type,
416                                'page' => $page,
417                                'displaypage' => $displaypage
418                );
419                $xml = $this->load->view('bscc', $aryParams, true);
420               
421                $client = new nusoap_client($url);
422                $client->useHTTPPersistentConnection();
423                $client->soap_defencoding = 'UTF-8';
424                //$client->soap_defencoding = 'ISO-8859-1';
425               
426                $client->operation = $action;
427                $result = $client->send($xml, $action);
428                if ($client->fault || (int)$result['Result']['error'] != 0) {
429                        return _SBG_GET_COLLABORATOR_FAIL;
430                }
431               
432                return $result['Result']['original'];
433        }
434       
435        /**
436         * formatPhoneNumber
437         *
438         * @todo format phone number to prefix 0 or 84
439         * @param String $phoneNumber
440         * @param Integer $direction
441         * @return String
442         */
443        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
444                $aryMatch = array();
445                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
446                        $result = '0'. $aryMatch[2];
447                }
448                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
449                        $result = $phoneNumber;
450                }
451                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
452                        $result = $phoneNumber;
453                }
454                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
455                        $result = '84'. $aryMatch[2];
456                }
457               
458                return $result;
459        }
460       
461       
462        public function syncCollaborator($data){
463                if ($data){
464                        $sql = $this->db->insert_string('tblcollaborator', $data) . ' ON DUPLICATE KEY UPDATE address = "'.$data['address'].'",
465                                                                                                                                                                                                code = "'.$data['code'].'",
466                                                                                                                                                                                                district = "'.$data['district'].'",
467                                                                                                                                                                                                email = "'.$data['email'].'",
468                                                                                                                                                                                                full_name = "'.$data['full_name'].'",
469                                                                                                                                                                                                cellphone = "'.$data['cellphone'].'",
470                                                                                                                                                                                                precinct = "'.$data['precinct'].'",
471                                                                                                                                                                                                province = "'.$data['province'].'",
472                                                                                                                                                                                                created_time = "'.$data['created_time'].'",
473                                                                                                                                                                                                updated_time = "'.$data['updated_time'].'"';
474                        $this->db->query($sql);
475                        $id = $this->db->insert_id();
476                        if ($this->db->affected_rows()){
477                                //echo "Collaborator ID: ".$id.". Sync Success !<br/>";
478                                $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Sync Success !\n";
479                                write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
480                        }else{
481                                //echo "Collaborator ID: ".$id.". Sync fail!. Sql = ".$sql."<br/>";
482                                $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Sync fail!. Sql = ".$sql."\n";
483                                write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
484                        }
485                }else {
486                        $logMsg = date('Y-m-d H:i:s') . " Data is null \n";
487                        write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
488                }
489        }
490       
491        public function deleteCollaborator($id){
492                $sql = "DELETE FROM tblcollaborator WHERE id = ". $id;
493                $this->db->query($sql);
494                if ($this->db->affected_rows()){
495                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete success!\n";
496                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
497                }else{
498                        $logMsg = date('Y-m-d H:i:s') . " Collaborator ID: ".$id.". Delete fail!. Sql = ".$sql."\n";
499                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
500                }
501        }
502       
503        public function getAllCollaboratorID(){
504                $sql = "Select id from tblcollaborator";
505                $query = $this->db->query($sql);
506                if ($this->db->affected_rows()){
507                        return $query->result();
508                }else
509                {
510                        return array();
511                }
512        }
513}
Note: See TracBrowser for help on using the repository browser.