Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
17 kB
1
Indexable
Never
public function actionHistoryCallNew()
    {
        $siteId = Yii::$app->params["siteId"];
        $searchModel = new CdrSearch();
        $searchModel::$isHistoryCall = true;
        $searchModel->site_id = Yii::$app->params["siteId"];
        $searchParams = Yii::$app->request->queryParams;
        $defaultDaterange =  date('d/m/Y') . ' 00:00' . ' - ' . date('d/m/Y') . ' 23:59';
        $canExport = 0;
        if (Yii::$app->user->canSite('export_history_call_data', Yii::$app->params["siteId"])) {
            $canExport = 1;
        }
        $canSearchAllExtHotline = 0;
        if (Yii::$app->user->canSite('search_all_ext_hotline', Yii::$app->params["siteId"])) {
            // Hiện tại quyền này không được dùng, sẽ bỏ sau.
            $canSearchAllExtHotline = 1;
        }

        $rangeTimeCall = Yii::$app->request->get('rangeTimeCall');
        $rangeTimeHold = Yii::$app->request->get('rangeTimeHold');
        $arrTimeCall = []; $arrTimeHold = [];
        if (!empty($rangeTimeCall)) {
            $tmp = explode('-', $rangeTimeCall);
            $arrTimeCall = array_map('intval', $tmp);
            if (count($arrTimeCall) !== 2) {
                $arrTimeCall = [];
            }
        }
        if(!empty($rangeTimeHold)) {
            $tmp = explode('-', $rangeTimeHold);
            $arrTimeHold = array_map('intval', $tmp);
            if (count($arrTimeHold) !== 2) {
                $arrTimeHold = [];
            }
        }

        $dpmIds = Yii::$app->request->get('departmentIds');
        $dpmIds =  $this->remove_sql_injection($dpmIds);
        $selectDepartments = $dpmIds;
        $campaignId = Yii::$app->request->get('campaignId');
        $extension = Yii::$app->request->get('extension');
        $cdrId = Yii::$app->request->get('cdrId');
        $cdrId = addslashes($cdrId);
        $cdrUUID = Yii::$app->request->get('cdrUUID');
        $cdrUUID = isset($cdrUUID) ? trim($cdrUUID) : $cdrUUID;
        
        $customerPhone = Yii::$app->request->get('customerPhone');
        $customerState = Yii::$app->request->get('customerState');
        $username = trim(Yii::$app->request->get('username'));
        $call_direction = Yii::$app->request->get('call_direction');
        // get ids of page and remove xss
        $ids_page = Yii::$app->request->get('ids_page');
        $ids_page = $this->remove_sql_injection($ids_page);

        $hotlineIdList = Yii::$app->request->get('hotlineIdList');
        $hotlineIdList = $this->remove_sql_injection($hotlineIdList);
        $extension_hotline_ids = Yii::$app->request->get('extension_hotline_ids');
        $extension_hotline_ids = $this->remove_sql_injection($extension_hotline_ids);
        $branchs = Yii::$app->request->get('branch_ids');
        $branchs = $this->remove_sql_injection($branchs);
        $callType = Yii::$app->request->get('callType');
        $status = Yii::$app->request->get('status');
        $type = Yii::$app->request->get('type');
        $last_id = Yii::$app->request->get('last_id'); // id cuối cùng hoặc đầu tiên
        $last_time = (int) Yii::$app->request->get('last_time'); // thời gian cuối cùng hoặc đầu tiên
        $pageSize = Yii::$app->request->get('per-page');
        $pageSize = $pageSize ? $pageSize:20;
        $total = Yii::$app->request->get('total');
        $daterange = Yii::$app->request->get('daterange');
        $is_new = Yii::$app->request->get('is_new');

        if (!empty($daterange)) {
            $arrTimeRange = explode(" - ", $daterange);
            $startTime = strtotime(str_replace("/", "-", $arrTimeRange[0]));
            $endTime = strtotime(str_replace("/", "-", $arrTimeRange[1]));
            // check không quá 31 ngày
            if ($endTime - $startTime > (86400 * 31)) {
                Yii::$app->session->setFlash('msgError', 'Thời gian tìm kiếm không quá 31 ngày');
                return $this->redirect(Yii::$app->request->referrer);
            }
        } 

        $userExtensions = 0;

        if (Yii::$app->user->canSite('managerCdrFull', $siteId)) { // Quyen admindepartment tro len
            if (!empty($username)) {
                $userList = User::findOne(['username' => $username]);
                if (!empty($userList)) {
                    $userExtensions = $userList->id;
                }  else {
                    $userExtensions = -1;
                }
            }

            if (!empty($selectDepartments)) $searchModel->searchDepartments = $selectDepartments;

        } else if (Yii::$app->user->canSite('managerCdr', $siteId)) { // Quyen admindepartment tro len

            if(empty($selectDepartments)){
                if (!Yii::$app->user->canSite('Sysadmin', $siteId)) {
                    // xử lý lấy toàn bộ quyền con trong quyền cha
                    $allPermissionsParent = (new \yii\db\Query())->select("parent")->from('auth_item_child')->where(['child' => ['managerCdr']])->all();
                    $departmentIds = '';
                    if (count($allPermissionsParent) > 0) {
                        foreach ($allPermissionsParent as $item) {
                            if (Yii::$app->user->canSite($item['parent'], $siteId) ) {
                                $hasParentPermission = true;
                                $departmentIds .= Yii::$app->user->getDepartments($item['parent'], $siteId).',';
                            }
                        }
                    }

                    if (!empty($departmentIds)) {
                        $departmentIds =  $this->remove_sql_injection($departmentIds);
                    }

                    if (!$hasParentPermission) {
                        if (Yii::$app->user->canSite('managerCdr', $siteId)) {
                            $departmentIds = Yii::$app->user->getDepartments('managerCdr', $siteId);
                        }
                    }

                    if(empty($departmentIds)){
                        $selectDepartments = [-1];
                    } else {
                        $selectDepartments = $this->remove_sql_injection($departmentIds,'int');
                    }
                }
            }

            if (!empty($username)) {
                $userList = User::findOne(['username' => $username, 'department_id' => $selectDepartments ]);
                if (!empty($userList)) {
                    $userExtensions = $userList->id;
                }  else {
                    $userExtensions = -1;
                }
            }

            if (!empty($selectDepartments)) $searchModel->searchDepartments = $selectDepartments;

        } else if (Yii::$app->user->canSite('managerCdrPrivate', $siteId)) { // Quyen Agent

            $user = User::findOne(['username' => $username]);
            $userId = -1;

            if (isset($user)) $userId = $user->id;

            $userExtensions = Yii::$app->user->getId();

            if (!empty($username)) $searchModel->user_id = $userId;
            /*
            if(empty($selectDepartments)){
                if (!Yii::$app->user->canSite('Sysadmin', $siteId)) {
                    $selectDepartments = Yii::$app->user->getDepartments('managerCdrPrivate', $siteId);
                    if(empty($selectDepartments)) {
                        $selectDepartments = [-1];
                    } else {
                        $selectDepartments = $this->remove_sql_injection($selectDepartments,'int');
                    }
                }
            }
            */
            if (!empty($selectDepartments)) $searchModel->searchDepartments = $selectDepartments;

        } else {
            throw new NotFoundHttpException('Không có quyền truy cập trang!');
        }

        if (!empty($dpmIds)) $searchModel->searchDepartments = explode(',',$dpmIds);

        $searchModel->limitHistoryCall = $pageSize;

        if (isset($campaignId) && $campaignId != '' && $campaignId != '-999999') {
            $searchModel->campaign_id = $campaignId;
        }

        if (!empty($userExtensions)) $searchModel->searchUsers = $userExtensions;

        if (!empty($daterange)) {
            $searchModel->createTimeRange = $daterange;
        } else {
            $searchModel->createTimeRange = $defaultDaterange;
        }

        if (!empty($extension)) $searchModel->extension = $extension;
        if (!empty($cdrId)) $searchModel->id = is_numeric($cdrId) ? $cdrId : -1;
        if (!empty($cdrUUID)) $searchModel->uuid = $cdrUUID;
        if (!empty($customerPhone)) $searchModel->customer_phone = $customerPhone;
        if ($callType != '')  $searchModel->call_type = $callType;
        if ($status != '') $searchModel->status = $status;

        if (!empty($customerState)) {
            switch ($customerState) {
                case 'OUT_OF_CALL_BOT':
                case 'OUT_OF_SKILL_GROUP':
                case 'ORIGINATOR_CANCEL':
                case 'SUCCESS':
                case 'OUT_OF_QUEUE':
                    $searchModel->customer_state = $customerState;
                    break;
                case 'KHONG_TRA_LOI':
                    $searchModel->customer_state = [
                        'NO_ANSWER',
                        'USER_BUSY'
                    ];
                    break;
                case 'TU_CHOI_CUOC_GOI':
                    $searchModel->customer_state = [
                        'NO_USER_RESPONSE',
                        'CALL_REJECTED'
                    ];
                    break;
                case 'DTV_CHUA_ONLINE':
                    $searchModel->customer_state = [
                        'USER_NOT_REGISTERED',
                        'NO_ACTIVE',
                    ];
                    break;
                case 'KHONG_CO_QUYEN_GOI':
                    $searchModel->customer_state = [
                        'NO_PERMISSION',
                        'NO_PERMISSION_SOFTPHONE',
                        'NO_MINUTE',
                    ];
                    break;
                case 'LOI_HE_THONG':
                    $searchModel->customer_state = [
                        'NORMAL_TEMPORARY_FAILURE',
                        'NETWORK_OUT_OF_ORDER',
                        'INCOMPATIBLE_DESTINATION',
                        'DESTINATION_OUT_OF_ORDER',
                        'INVALID_NUMBER_FORMAT',
                        'UNLOCATED_NUMBER',
                        ''
                    ];
                    $searchModel->isLoiHeThong = true;
                    break;
                default:
                    break;

            }
        }

        if (!empty($extension_hotline_ids)) $searchModel->listExtensionHotline = $extension_hotline_ids;
        if (!empty($hotlineIdList)) $searchModel->listHotline = $hotlineIdList;
        if (!empty($branchs)) $searchModel->branch_ids = $branchs;
        if (isset($call_direction) && $call_direction != '') $searchModel->call_direction = $call_direction;
        if (!empty($rangeTimeCall)) $searchModel->arrTimeCall = $arrTimeCall;
        if (!empty($rangeTimeHold)) $searchModel->arrTimeHold = $arrTimeHold;

        if (!empty($type)) {
            $searchModel->type = $type;

            /// mục đích xử lý where theo id của trang trước đó
            if ($type == 'back' && !empty($ids_page) ) $searchModel->ids_before = $ids_page;
        }

        if (!empty($last_id)) $searchModel->last_id = $last_id;
        if (!empty($last_time) && $last_time > 0) $searchModel->last_time = $last_time;

        // dùng để lưu số item của trang trước đó, xử lý cho TH trang cuối cùng khi không có dữ liệu
        $showNext =  true; // dùng để xác định ẩn hiện nút Trang Tiếp Theo
        $showPrevious = true; // dùng để xác định ẩn/hiện nút Trang trước đó

        // nếu là trang đầu thì ẩn nút trang trước đó
        if (!$type) $showPrevious = false;

        $data = [];

        if (!empty($daterange) && $userExtensions != -1) {
            $result = $searchModel->searchNewV2($searchParams);

            if (!empty($result)) { // > 0

                $ids = json_encode($result['ids']); // danh sách id tìm kiếm
                $data = $result['data']; // 50 bản ghi tìm kiếm
                
                if($is_new){
                    $total = $result['total']; // tổng số bản ghi toàn bộ
                }
                
                //$total = $result['total']; // tổng số bản ghi toàn bộ

                $totalItem = count($data);
                if ($type == 'back') {
                    //$data = array_reverse($data);
                }

                $last_item_start = current($data);
                //$last_item_start_id = $last_item_start['id'];
                //$last_item_start_time = $last_item_start['start_cdr_timestamp'];
                $last_item_start_time = $last_item_start['created_at'];
                $last_item_end = end($data);
                //$last_item_end_id = $last_item_end['id'];
                //$last_item_end_time = $last_item_end['start_cdr_timestamp'];
                $last_item_end_time = $last_item_end['created_at'];

                $all_last_id = '';
                if (!empty($data[1])) $all_last_id .= $data[$totalItem - 1]['id'];
                if (!empty($data[2])) $all_last_id .= ',' . $data[$totalItem - 2]['id'];
                if (!empty($data[3])) $all_last_id .= ',' . $data[$totalItem - 3]['id'];
                if (!empty($data[4])) $all_last_id .= ',' . $data[$totalItem - 4]['id'];
                if (!empty($data[5])) $all_last_id .= ',' . $data[$totalItem - 5]['id'];
                if (!empty($data[6])) $all_last_id .= ',' . $data[$totalItem - 6]['id'];
                if (!empty($data[7])) $all_last_id .= ',' . $data[$totalItem - 7]['id'];

                // dùng để lưu số item của trang trước đó, xử lý cho TH trang cuối cùng khi không có dữ liệu
                if ($totalItem < $pageSize) {
                    $showNext = false;
                }

            } else {
                // không có dữ liệu trang tiếp theo
                $showNext = false;
            }
        }

        $dataProvider = new ArrayDataProvider(['allModels' => $data, 'pagination' =>[
            'pageSize'=>$pageSize
        ]]);

        return $this->render('history_call_new_v2', [
            'dpmIds' => isset($dpmIds) ? $dpmIds : '',
            'extension_hotline_id' => isset($extensionHotlineId) ? $extensionHotlineId : '',
            'branchs' => $branchs,
            'campaignId' => isset($campaignId) ? $campaignId : '',
            'extension' => isset($extension) ? $extension : '',
            'cdr_id' => isset($cdrId) ? $cdrId : '',
            'cdr_uuid' => isset($cdrUUID) ? $cdrUUID : '',
            'msisdn' => isset($msisdn) ? $msisdn : '',
            'customerPhone' => isset($customerPhone) ? $customerPhone : '',
            'daterange' => isset($daterange) ? $daterange : $defaultDaterange,
            'customer_state' => isset($customerState) ? $customerState : '',
            'username' => isset($username) ? $username : '',
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'dataProviderExport' => isset($dataProviderExport) ? $dataProviderExport : '',
            'call_direction' => isset($call_direction) ? $call_direction : '',
            'extensionHotlineIdList' => $extension_hotline_ids,
            'hotlineIdList' => $hotlineIdList,
            'canExport' => $canExport,
            'canSearchAllExtHotline' => $canSearchAllExtHotline,
            'startTimeCall' => isset($arrTimeCall[0]) ? $arrTimeCall[0] : '',
            'endTimeCall' => isset($arrTimeCall[1]) ? $arrTimeCall[1] : '',
            'startTimeHold' =>  isset($arrTimeHold[0]) ? $arrTimeHold[0] : '',
            'endTimeHold' => isset($arrTimeHold[1]) ? $arrTimeHold[1] : '',
            'callType' => $callType,
            'status' => $status,
            'pageSize' => $pageSize,
            'selectedDateRange' => isset($daterange) ? $daterange : '',
            'last_item_start_id' => isset($last_item_start_id) ? $last_item_start_id : '',
            'last_item_end_id' => isset($last_item_end_id) ? $last_item_end_id : '',
            'last_item_start_time' => isset($last_item_start_time) ? $last_item_start_time : '',
            'last_item_end_time' => isset($last_item_end_time) ? $last_item_end_time : '',
            'totalItem' => isset($totalItem) ? $totalItem : 0,
            'showNext' => isset($showNext) ? $showNext : false,
            'showPrevious' => isset($showPrevious) ? $showPrevious : false,
            'page_history' => isset($page_history) ? $page_history : false,
            'ids' => isset($ids) ? $ids : 0,
            'type' => !empty($type) ? $type : '',
            'total' => !empty($total) ? $total : 0,
            'all_last_id' => $all_last_id ?? ''
        ]);
    }