Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
23 kB
1
Indexable
Never
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Browse extends WFF_CustomerController {

    function __construct() {
        parent::__construct();
        $this->load->config('worldui');
    }

    function index() {
        $pbx_customer_code = $this->session->userdata('pbx_customer_code');
        $data['title'] = ucfirst('CDR');
        $data['template'] = $this->config->item('template');
        $data['template']['header'] = 'navbar-fixed-top';
        $data['template']['footer'] = 'footer-fixed';
        $this->load->view('templates/worldui/template_start', $data);
        $this->load->view('templates/worldui/page_head', $data);
        $data['callCenter'] = $this->mongo_db->select(array('hidePhone', 'displayChars', 'displayOn'))->getOne($pbx_customer_code . '_callCenter');
        $this->load->view('customers/view', $data);
        $this->load->view('templates/worldui/page_footer');
        $this->load->view('templates/worldui/template_end');
    }

    function stopAcs() {
        header('Content-Type: application/json');
        $request = json_decode(file_get_contents('php://input'));
        $pbx_customer_code = $this->session->userdata("pbx_customer_code");

        $loadAcs = $this->mongo_db
                ->select(array('callid', 'secret'))
                ->where(array("customerNumber" => $request->phone))
                ->getOne($pbx_customer_code . '_acsLogs');

        //Start action link             
        $url = 'https://apps.worldfone.vn/apiacs/notcall.php';
        $fields = array(
            'secret' => $loadAcs["secret"],
            'id' => $loadAcs["callid"]
        );

        //url-ify the data for the POST
        $fields_string = '';
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        $fields_string = rtrim($fields_string, '&');

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 2);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));

        //execute post
        $result = curl_exec($ch);
        $err = curl_error($ch);

        //close connection
        curl_close($ch);

        //End action link

        $value = json_decode($result, true);
        $code = $value['code'];
        if ($code == 1) {
            $this->mongo_db->where(array("phone" => $request->phone))->set(array("stopacs" => "1"))->update($pbx_customer_code . "_customers");
            echo json_encode(true);
            exit;
        }

        echo json_encode(false);
        exit;
    }

    function actionAcs() {
        header('Content-Type: application/json');
        $request = json_decode(file_get_contents('php://input'));
        $pbx_customer_code = $this->session->userdata("pbx_customer_code");

        //Start action link				
        $url = 'https://apps.worldfone.vn/apiacs/import.php';
        $fields = array(
            'secret' => "4d00e877b9ec20e7a1e800775ce4d191",
            'campaignid' => "712",
            'phone' => $request->phone
        );
        //url-ify the data for the POST
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        $fields_string = rtrim($fields_string, '&');
        //open connection
        $ch = curl_init();
        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
        //execute post
        $result = curl_exec($ch);
        //close connection
        curl_close($ch);
        //End action link 

        $value = json_decode($result, true);
        $callid = $value['callid'];
        $result2 = $this->mongo_db->where(array("_id" => new MongoId($request->id)))->set(array("callid" => $callid))->update($pbx_customer_code . "_worldfonepbxmanager");
        echo json_encode(true);
    }

    function readdb() {
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            header('Content-Type: application/json');
            $request = json_decode(file_get_contents('php://input'));
            $requestArr = json_decode(file_get_contents('php://input'), TRUE);
            $userextension = $this->session->userdata('extension');
            $pbx_customer_code = $this->session->userdata("pbx_customer_code");

            foreach (["TuNgay", "DenNgay"] as $field) {
                if (!empty($requestArr[$field])) {
                    $d = new DateTime($requestArr[$field]);
                    if ($field == "TuNgay")
                        $d->setTime(0, 0, 0);
                    else
                        $d->setTime(23, 59, 59);
                    $time[$field] = $d->getTimestamp();
                } else {
                    if ($field == "TuNgay")
                        $time[$field] = strtotime("today");
                    else
                        $time[$field] = strtotime("tomorrow");
                }
            }


            $this->load->library('mongodatasourceresult');

            if ($this->session->userdata('issupervisor') != 1 && $this->session->userdata('isadmin') != 1) {
                $loadCdrType = $this->mongo_db->select(array('loadCdrType'))->getOne($pbx_customer_code . '_callCenter');
                if (isset($loadCdrType['loadCdrType'])) {
                    if ($loadCdrType['loadCdrType'] == 'byextension') {
                        $filterUser = new stdClass();
                        $filterUser->logic = "and";
                        $filterUser->field = "userextension";
                        $filterUser->value = $userextension;
                        $filterUser->operator = "eq";
                        $request->filter->filters[] = $filterUser;

                        if (isset($request->filter->filters[0]->field)) {
                            if ($request->filter->filters[0]->field != 'starttime') {
                                $request->filter->logic = "or";
                            }
                        }
                    }
                }
            }

            $filterFromDate = new stdClass();
            $filterFromDate->field = "starttime";
            $filterFromDate->operator = "gte";
            $filterFromDate->value = $time["TuNgay"];

            $filterToDate = new stdClass();
            $filterToDate->field = "starttime";
            $filterToDate->operator = "lte";
            $filterToDate->value = $time["DenNgay"];

            $request->filter->filters[] = $filterFromDate;
            $request->filter->filters[] = $filterToDate;
            $request->filter->logic = "and";
            // print_r($request);exit();
            if ($request->filter->filters[0]->field != 'starttime' && $request->filter->filters[0]->field != 'userextension') {
                if ($request->filter->filters[0]->field == 'customernumber' || $request->filter->filters[0]->field == 'customername') {
                    $request->filter->filters[0] = (array) $request->filter->filters[0];
                    $request->filter->filters[0]['logic'] = 'or';
                    $request->filter->filters[0] = (object) $request->filter->filters[0];
                    $request->filter->filters[1] = (array) $request->filter->filters[1];
                    $request->filter->filters[1]['logic'] = 'or';
                    $request->filter->filters[1] = (object) $request->filter->filters[1];
                    if ($this->session->userdata('issupervisor') != 1 && $this->session->userdata('isadmin') != 1) {
                        unset($request->filter->filters[3]);
                        unset($request->filter->filters[4]);
                    } else {
                        unset($request->filter->filters[2]);
                        unset($request->filter->filters[3]);
                    }
                    //TuanTruong 26052020
                    $filterFromDate->field = "starttime";
                    $filterFromDate->operator = "gte";
                    $filterFromDate->logic = "and";
                    $filterFromDate->value = $time["TuNgay"];

                    $filterToDate = new stdClass();
                    $filterToDate->field = "starttime";
                    $filterToDate->operator = "lte";
                    $filterToDate->logic = "and";
                    $filterToDate->value = $time["DenNgay"];

                    $request->filter->filters[] = $filterFromDate;
                    $request->filter->filters[] = $filterToDate;
                    //end TuanTruong 26052020
                } else {
                    if (!isset($request->filter->filters[0]->field)) {
                        unset($request->filter->filters[0]);
                    }
                }
            }

            $data = $this->mongodatasourceresult->read($pbx_customer_code . '_worldfonepbxmanager', array('_id', 'direction', 'callstatus', 'workstatus', 'calluuid', 'userextension', 'diallistid', 'customernumber', 'starttime', 'answertime', 'endtime', 'billduration', 'totalduration', 'causetxt', 'calltype', 'customercode', 'customername', 'disposition', 'callnote', 'callresultvalue', 'acsresult', 'company_name', 'agentname', 'customer', 'customer.address', 'customer.email', 'customer.describe', 'customer.tinhtranggiaiquyet', 'customer.donvixuly', 'customer.loaicuocgoi', 'customer.type', 'customer.ghiChuCuocGoi', 'customer.thoigiangoi', 'customer.source', 'customer.sanpham', 'customer.kenhbansanpham', 'csdescription', 'csdescription.caseID', 'csdescription.statusVta', 'csdescription.inboundOutbound', 'csdescription.sourceCustomer', 'csdescription.subjectVta', 'csdescription.teamCRM', 'officials_reception', 'lastresult', 'customer.description', 'dnis'), $request);

            foreach ($data['data'] as &$value) {
                $value["starttime"] = !empty($value["starttime"]) ? date('d/m/Y H:i:s', $value["starttime"]) : null;
                if($value['direction'] == 'inbound'){
                    $value['sobigoi'] = $value['dnis'];
                    $value['sogoi'] = $value['customernumber'];
                    
                }else {
                    $value['sobigoi'] = $value['customernumber'];
                    $value['sogoi'] = $value['dnis'];
                }
                if (!isset($value['customer'])){
                    $value['customer'] = [];
                    $value['customer']['sanpham'] = '';
                    $value['customer']['kenhbansanpham'] = '';
                }
                else {
                    if (!isset($value['customer']['sanpham'])){
                        $value['customer']['sanpham'] = '';
                    }
                    if (!isset($value['customer']['kenhbansanpham'])){
                        $value['customer']['kenhbansanpham'] = '';
                    }
                }
                       
             }
            if ($this->session->userdata('issupervisor') != 1 && $this->session->userdata('isadmin') != 1) {
                $loadCdrType = $this->mongo_db->select(array('loadCdrType'))->getOne($pbx_customer_code . '_callCenter');
                if (isset($loadCdrType['loadCdrType'])) {
                    $countInOutAns = $this->mongo_db->aggregate_pipeline('C0121_worldfonepbxmanager', array(
                        array(
                            '$match' => array(
                                '$and' => array(
                                    array('starttime' => array('$gte' => $time["TuNgay"])),
                                    array('starttime' => array('$lte' => $time["DenNgay"])),
                                    array('userextension' => $userextension)
                                )
                            )
                        ),
                        array(
                            '$group' => array(
                                '_id' => array(
                                ),
                                'count_inbound' => array(
                                    '$sum' => array(
                                        '$cond' => array(
                                            array('$eq' => array('$direction', 'inbound'))
                                            , 1, 0
                                        )
                                    )
                                ),
                                'count_outbound' => array(
                                    '$sum' => array(
                                        '$cond' => array(
                                            array('$eq' => array('$direction', 'outbound'))
                                            , 1, 0
                                        )
                                    )
                                ),
                                'count_answered' => array(
                                    '$sum' => array(
                                        '$cond' => array(
                                            array('$eq' => array('$disposition', 'ANSWERED'))
                                            , 1, 0
                                        )
                                    )
                                )
                            )
                        )
                    ));
                }
            } else {
                $countInOutAns = $this->mongo_db->aggregate_pipeline('C0121_worldfonepbxmanager', array(
                    array(
                        '$match' => array(
                            '$and' => array(
                                array('starttime' => array('$gte' => $time["TuNgay"])),
                                array('starttime' => array('$lte' => $time["DenNgay"]))
                            )
                        )
                    ),
                    array(
                        '$group' => array(
                            '_id' => array(
                            ),
                            'count_inbound' => array(
                                '$sum' => array(
                                    '$cond' => array(
                                        array('$eq' => array('$direction', 'inbound'))
                                        , 1, 0
                                    )
                                )
                            ),
                            'count_outbound' => array(
                                '$sum' => array(
                                    '$cond' => array(
                                        array('$eq' => array('$direction', 'outbound'))
                                        , 1, 0
                                    )
                                )
                            ),
                            'count_answered' => array(
                                '$sum' => array(
                                    '$cond' => array(
                                        array('$eq' => array('$disposition', 'ANSWERED'))
                                        , 1, 0
                                    )
                                )
                            )
                        )
                    )
                ));
            }
            // print_r($countInOutAns);exit();
            if (isset($countInOutAns[0])) {
                $data['count_inbound'] = $countInOutAns[0]['count_inbound'];
                $data['count_outbound'] = $countInOutAns[0]['count_outbound'];
                $data['count_answered'] = $countInOutAns[0]['count_answered'];
            }
            $callCenter = $this->mongo_db->select(array('hidePhone'))->getOne($pbx_customer_code . '_callCenter');

            if (is_array($callCenter) && array_key_exists("hidePhone", $callCenter)) {
                $data['hidePhone'] = $callCenter['hidePhone'];
            } else {
                $data['hidePhone'] = 'show';
            }

            foreach ($data['data'] as $keys => $values) {
                    if(!isset($values["sobigoi"])){
                        $data['data'][$keys]["sobigoi"] = "500" ;
                    }

            }
            echo json_encode($data);
        }
    }

    function readdbToExcel() {
        header('Content-Type: application/json');
        $request = json_decode(file_get_contents('php://input'));
        $userextension = $this->session->userdata('extension');
        $pbx_customer_code = $this->session->userdata("pbx_customer_code");

        $this->load->library('mongodatasourceresult');

        if ($this->session->userdata('issupervisor') != 1 && $this->session->userdata('isadmin') != 1) {
            $loadCdrType = $this->mongo_db->select(array('loadCdrType'))->getOne($pbx_customer_code . '_callCenter');
            if (isset($loadCdrType['loadCdrType'])) {
                if ($loadCdrType['loadCdrType'] == 'byextension') {
                    $filterUser = new stdClass();
                    $filterUser->field = "userextension";
                    $filterUser->value = $userextension;
                    $filterUser->operator = "eq";

                    $request->filter->filters[] = $filterUser;
                    $request->filter->logic = "and";
                }
            }
        }


        $data = $this->mongodatasourceresult->read($pbx_customer_code . '_worldfonepbxmanager', array('_id', 'direction', 'callstatus', 'workstatus', 'calluuid', 'userextension', 'customernumber', 'starttime', 'answertime', 'endtime', 'billduration', 'totalduration', 'causetxt', 'calltype', 'customercode', 'customername', 'disposition', 'callnote', 'callresultvalue', 'acsresult', 'company_name', 'agentname', 'customer', 'customer.address', 'customer.email', 'customer.describe', 'customer.tinhtranggiaiquyet', 'customer.donvixuly', 'customer.loaicuocgoi', 'customer.type', 'customer.ghiChuCuocGoi', 'customer.thoigiangoi', 'customer.source', 'csdescription', 'csdescription.caseID', 'csdescription.statusVta', 'csdescription.inboundOutbound', 'csdescription.sourceCustomer', 'csdescription.subjectVta', 'csdescription.teamCRM', 'officials_reception', 'lastresult', 'customer.description'), $request);
        echo json_encode($data);
    }

    function dongbo_cus_cdr() {
        $this->load->library('mongo_db');

        $data = $this->mongo_db->aggregate_pipeline('C0211_worldfonepbxmanager', array(
            array(
                '$match' => array(
                    'customer' => array('$exists' => false)
                )
            ),
            array(
                '$group' => array(
                    '_id' => array('customernumber' => '$customernumber')
                )
            )
            ,
            array(
                '$lookup' => array(
                    'from' => 'C0211_customers',
                    'localField' => '_id.customernumber',
                    'foreignField' => 'phone',
                    'as' => 'customerinfo'
                )
            ),
            array(
                '$project' => array(
                    'customernumber' => '$_id.customernumber',
                    'customerinfo' => 1,
                    'cus_size' => array('$size' => '$customerinfo')
                )
            ),
            array(
                '$match' => array(
                    'cus_size' => array('$gt' => 0)
                )
            )
        ));

        for ($i = 0; $i < count($data); $i++) {
            if (count($data[$i]["customerinfo"]) != 0) {
                $arr_customer = array(
                    "customer" => $data[$i]["customerinfo"][0]
                );
                $result = $this->mongo_db->where(array('customernumber' => $data[$i]["_id"]["customernumber"]))->set($arr_customer)->update_all('C0211_worldfonepbxmanager');
            }
        }
    }

    function readfilter() {
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            header('Content-Type: application/json');
            $request = json_decode(file_get_contents('php://input'));

            $this->load->library('mongodatasourceresult');
            $pbx_customer_code = $this->session->userdata("pbx_customer_code");
            $data = $this->mongodatasourceresult->read($pbx_customer_code . '_worldfonepbxmanager', array('DISTINCT' => $this->input->get('details')), $request);
            echo json_encode($data);
        }
    }

    function readCustDDLFilter() {
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            header('Content-Type: application/json');
            $request = json_decode(file_get_contents('php://input'));

            $this->load->library('mongodatasourceresult');
            $pbx_customer_code = $this->session->userdata("pbx_customer_code");
            $_data = $this->mongo_db->select(array('dropDownListContent'))->where(array('dropDownListName' => 'type'))->getOne($pbx_customer_code . '_dropDownList');
            $data = [];
            $data['data'] = $_data['dropDownListContent'];
            echo json_encode($data);
        }
    }

    function export() {
        $fileName = $_POST['fileName'];
        $contentType = $_POST['contentType'];
        $base64 = $_POST['base64'];
        $data = base64_decode($base64);
        header('Content-Type:' . $contentType);
        header('Content-Length:' . strlen($data));
        header('Content-Disposition: attachment; filename=' . $fileName);
        echo $data;
    }

    function updateLastResult() {
        header('Content-Type: application/json');
        $request = json_decode(file_get_contents('php://input'));
        $pbx_customer_code = $this->session->userdata("pbx_customer_code");

        $loadAcs = $this->mongo_db->select(array('agentname'))->where(array("_id" => new MongoId($request->id)))->getOne($pbx_customer_code . '_worldfonepbxmanager');
        if (!empty($loadAcs)) {
            $this->mongo_db->where(array("_id" => new MongoId($request->id)))->set(array("lastresult" => $request->value))->update($pbx_customer_code . "_worldfonepbxmanager");
            echo json_encode(true);
            exit;
        }

        echo json_encode(false);
        exit;
    }

}