Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
- global $USER_FIELD_MANAGER;
- $requiredModules = array('report', 'studiobit.alliance');
- foreach ($requiredModules as $requiredModule)
- {
- if (!CModule::IncludeModule($requiredModule))
- {
- ShowError(GetMessage("F_NO_MODULE"));
- return 0;
- }
- }
- // Suppress the timezone, while report works in server time
- CTimeZone::Disable();
- use Bitrix\Main\Entity;
- // <editor-fold defaultstate="collapsed" desc="period types">
- $periodTypes =
- $arResult['periodTypes'] = array(
- 'month',
- 'month_ago',
- 'week',
- 'week_ago',
- 'days',
- 'after',
- 'before',
- 'interval',
- 'all'
- );
- // </editor-fold>
- if(!isset($arParams["SAVE_FILE_NAME"])) $arParams["SAVE_FILE_NAME"] = "report.xlsx";
- // <editor-fold defaultstate="collapsed" desc="chart types">
- if ($arParams['USE_CHART'])
- {
- $arResult['chartTypes'] = array(
- array('id' => 'line', 'name' => GetMessage('REPORT_CHART_TYPE_LINE'), 'value_types' => array(
- /*'boolean', 'date', 'datetime', */
- 'float', 'integer'/*, 'string', 'text', 'enum', 'file', 'disk_file', 'employee', 'crm', 'crm_status',
- 'iblock_element', 'iblock_section'*/
- )),
- array('id' => 'bar', 'name' => GetMessage('REPORT_CHART_TYPE_BAR'), 'value_types' => array(
- /*'boolean', 'date', 'datetime', */
- 'float', 'integer'/*, 'string', 'text', 'enum', 'file', 'disk_file', 'employee', 'crm', 'crm_status',
- 'iblock_element', 'iblock_section'*/
- )),
- array('id' => 'pie', 'name' => GetMessage('REPORT_CHART_TYPE_PIE'), 'value_types' => array(
- /*'boolean', 'date', 'datetime', */
- 'float', 'integer'/*, 'string', 'text', 'enum', 'file', 'disk_file', 'employee', 'crm', 'crm_status',
- 'iblock_element', 'iblock_section'*/
- ))
- );
- }
- // </editor-fold>
- // get view params
- $strReportViewParams = CReport::getViewParams($arParams['REPORT_ID'], $this->GetTemplateName());
- if (isset($_GET['set_filter']))
- {
- if (substr($_SERVER['QUERY_STRING'], 0, 6) !== 'EXCEL=')
- {
- if ($_SERVER['QUERY_STRING'] !== $strReportViewParams)
- {
- CReport::setViewParams($arParams['REPORT_ID'], $this->GetTemplateName(), $_SERVER['QUERY_STRING']);
- }
- }
- }
- else
- {
- if (!empty($strReportViewParams))
- {
- if (!is_set($_GET['sort_id']))
- {
- $len = strpos($arParams['PATH_TO_REPORT_VIEW'], '?');
- if ($len === false) $redirectUrl = $arParams['PATH_TO_REPORT_VIEW'];
- else $redirectUrl = substr($arParams['PATH_TO_REPORT_VIEW'], 0, $len);
- $redirectUrl = CComponentEngine::makePathFromTemplate($redirectUrl, array('report_id' => $arParams['REPORT_ID']));
- $redirectUrl .= '?'.$strReportViewParams;
- LocalRedirect($redirectUrl);
- }
- else
- {
- CReport::clearViewParams($arParams['REPORT_ID']);
- }
- }
- }
- try
- {
- // select report info/settings
- $report = array();
- $result = false;
- if (intval($arParams['REPORT_ID']) > 0)
- {
- $result = Bitrix\Report\ReportTable::getById($arParams['REPORT_ID']);
- }
- if (is_object($result))
- {
- $report = $result->fetch();
- }
- if (empty($report))
- {
- throw new BXUserException(sprintf(GetMessage('REPORT_NOT_FOUND'), $arParams['REPORT_ID']));
- }
- if ($report['CREATED_BY'] != $USER->GetID())
- {
- throw new BXUserException(GetMessage('REPORT_VIEW_PERMISSION_DENIED'));
- }
- $arResult['MARK_DEFAULT'] = 0;
- if (isset($report['MARK_DEFAULT']))
- {
- $arResult['MARK_DEFAULT'] = intval($report['MARK_DEFAULT']);
- }
- // action
- $settings = unserialize($report['SETTINGS']);
- // <editor-fold defaultstate="collapsed" desc="parse period">
- $date_from = $date_to = null;
- $form_date = array('from' => null, 'to' => null, 'days' => null);
- // <editor-fold defaultstate="collapsed" desc="get value from POST or DB">
- if (!empty($_GET['F_DATE_TYPE']) && in_array($_GET['F_DATE_TYPE'], $periodTypes, true))
- {
- $period = array('type' => $_GET['F_DATE_TYPE']);
- switch ($_GET['F_DATE_TYPE'])
- {
- case 'days':
- $days = !empty($_GET['F_DATE_DAYS']) ? (int) $_GET['F_DATE_DAYS'] : 1;
- $period['value'] = $days ? $days : 1;
- break;
- case 'after':
- $date = !empty($_GET['F_DATE_TO']) ? (string) $_GET['F_DATE_TO'] : ConvertTimeStamp(false, 'SHORT');
- $date = MakeTimeStamp($date);
- $period['value'] = $date ? $date : time();
- break;
- case 'before':
- $date = !empty($_GET['F_DATE_FROM']) ? (string) $_GET['F_DATE_FROM'] : ConvertTimeStamp(false, 'SHORT');
- $date = MakeTimeStamp($date);
- $period['value'] = $date ? $date + (3600*24-1) : time() + (3600*24-1);
- break;
- case 'interval':
- $date_f = !empty($_GET['F_DATE_FROM']) ? (string) $_GET['F_DATE_FROM'] : ConvertTimeStamp(false, 'SHORT');
- $date_f = MakeTimeStamp($date_f);
- $date_t = !empty($_GET['F_DATE_TO']) ? (string) $_GET['F_DATE_TO'] : ConvertTimeStamp(false, 'SHORT');
- $date_t = MakeTimeStamp($date_t);
- if ($date_f || $date_t)
- {
- $period['value'][0] = $date_f ? $date_f : time();
- $period['value'][1] = $date_t ? $date_t + (3600*24-1) : time() + (3600*24-1);
- }
- break;
- default:
- $period['value'] = null;
- }
- }
- else
- {
- $period = $settings['period'];
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="parse period">
- switch ($period['type'])
- {
- case 'month':
- $date_from = strtotime(date("Y-m-01"));
- break;
- case 'month_ago':
- $date_from = strtotime(date("Y-m-01", strtotime("-1 month")));
- $date_to = strtotime(date("Y-m-t", strtotime("-1 month"))) + (3600*24-1);
- break;
- case 'week':
- $date_from = strtotime("-".((date("w") == 0 ? 7 : date("w")) - 1)." day 00:00");
- break;
- case 'week_ago':
- $date_from = strtotime("-".((date("w") == 0 ? 7 : date("w")) + 6)." day 00:00");
- $date_to = strtotime("-".(date("w") == 0 ? 7 : date("w"))." day 23:59:59");
- break;
- case 'days':
- $date_from = strtotime(date("Y-m-d")." -".intval($period['value'])." day");
- $form_date['days'] = intval($period['value']);
- break;
- case 'after':
- $date_from = $period['value'];
- $form_date['to'] = ConvertTimeStamp($period['value'], 'SHORT');
- break;
- case 'before':
- $date_to = $period['value'];
- $form_date['from'] = ConvertTimeStamp($period['value'], 'SHORT');
- break;
- case 'interval':
- list($date_from, $date_to) = $period['value'];
- $form_date['from'] = ConvertTimeStamp($period['value'][0], 'SHORT');
- $form_date['to'] = ConvertTimeStamp($period['value'][1], 'SHORT');
- break;
- }
- $site_date_from = !is_null($date_from) ? ConvertTimeStamp($date_from, 'FULL') : null;
- $site_date_to = !is_null($date_to) ? ConvertTimeStamp($date_to, 'FULL') : null;
- // to_date for oracle
- // rewrite to CDatabase::CharToDateFunction
- global $DB;
- $db_date_from = !is_null($site_date_from) ? $DB->CharToDateFunction($site_date_from) : null;
- $db_date_to = !is_null($site_date_to) ? $DB->CharToDateFunction($site_date_to) : null;
- // user name format
- if (isset($arParams['USER_NAME_FORMAT'])
- && is_string($arParams['USER_NAME_FORMAT'])
- && !empty($arParams['USER_NAME_FORMAT']))
- {
- call_user_func(
- array($arParams['REPORT_HELPER_CLASS'], 'setUserNameFormat'),
- $arParams['USER_NAME_FORMAT']
- );
- }
- // period filter
- $filter = array('LOGIC' => 'AND');
- $period_filter = call_user_func(
- array($arParams['REPORT_HELPER_CLASS'], 'getPeriodFilter'),
- $site_date_from, $site_date_to
- );
- if (!empty($period_filter))
- {
- $filter[] = $period_filter;
- }
- // preiod option
- if (!is_null($date_from) && !is_null($date_to))
- {
- $sqlTimeInterval = "BETWEEN ".$db_date_from." AND ".$db_date_to;
- }
- else if (!is_null($date_from))
- {
- $sqlTimeInterval = "> ".$db_date_from;
- }
- else if (!is_null($date_to))
- {
- $sqlTimeInterval = "< ".$db_date_to;
- }
- else
- {
- $sqlTimeInterval = " IS NOT NULL";
- }
- // </editor-fold>
- // </editor-fold>
- $runtime = array();
- $select = array();
- $group = array();
- $order = array();
- $limit = array();
- $options = array(
- 'SQL_TIME_INTERVAL' => $sqlTimeInterval
- );
- $excelView = isset($_GET["EXCEL"]) && $_GET["EXCEL"] == "Y";
- // <editor-fold defaultstate="collapsed" desc="parse entity">
- $entityName = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getEntityName'));
- $entityFields = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getColumnList'));
- $grcFields = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getGrcColumns'));
- //$arUFInfo = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getUFInfo'));
- $arUFEnumerations = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getUFEnumerations'));
- // customize entity
- $entity = clone Entity\Base::getInstance($entityName);
- call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'setRuntimeFields'), $entity, $sqlTimeInterval);
- $chains = CReport::generateChains($entityFields, $entity, '');
- $fieldsTree = CReport::generateColumnTree($chains, $entity, $arParams['REPORT_HELPER_CLASS']);
- unset($chains);
- // custom columns types
- $customColumnTypes = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getCustomColumnTypes'));
- if (!is_array($customColumnTypes))
- $customColumnTypes = array();
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="parse select columns">
- // <editor-fold defaultstate="collapsed" desc="collect fields">
- $fList = array();
- $fChainList = array();
- $bGroupingMode = false;
- foreach ($settings['select'] as $elem)
- {
- if (!$bGroupingMode) if ($elem['grouping'] === true) $bGroupingMode = true;
- $fName = $elem['name'];
- if (array_key_exists($fName, $fList))
- {
- continue;
- }
- try
- {
- $chain = Entity\QueryChain::getChainByDefinition($entity, $fName);
- }
- catch (Exception $e)
- {
- if ($e->getCode() == 100)
- throw new BXUserException('<p style="color: red;">'.GetMessage('REPORT_UNKNOWN_FIELD_DEFINITION').'</p>');
- else
- throw $e;
- }
- $fList[$fName] = $chain->getLastElement()->getValue();
- if (is_array($fList[$fName])) $fList[$fName] = end($fList[$fName]);
- $fChainList[$fName] = $chain;
- }
- // customize select fields
- $customSelectFields = call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'getCustomSelectFields'),
- array($settings['select'], $fList)
- );
- if (is_array($customSelectFields) && !empty($customSelectFields))
- {
- $customSelectKeys = array_keys($customSelectFields);
- $newSelect = array();
- foreach ($settings['select'] as $k => $elem)
- {
- if (in_array($k, $customSelectKeys, true))
- {
- $fName = $customSelectFields[$k]['name'];
- if (array_key_exists($fName, $fList))
- continue;
- try
- {
- $chain = Entity\QueryChain::getChainByDefinition($entity, $fName);
- }
- catch (Exception $e)
- {
- if ($e->getCode() == 100)
- throw new BXUserException('<p style="color: red;">'.GetMessage('REPORT_UNKNOWN_FIELD_DEFINITION').'</p>');
- else
- throw $e;
- }
- $fList[$fName] = $chain->getLastElement()->getValue();
- if (is_array($fList[$fName])) $fList[$fName] = end($fList[$fName]);
- $fChainList[$fName] = $chain;
- $newSelect[$k] = $customSelectFields[$k];
- }
- else
- {
- $newSelect[$k] = $elem;
- }
- }
- $settings['select'] = $newSelect;
- unset($customSelectKeys, $newSelect);
- }
- unset($customSelectFields);
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="collect hrefs' fields">
- //$settings['select'][0]['href'] = array(
- // 'pattern' => '/company/personal/user/#RESPONSIBLE_ID#/tasks/task/view/#ID#/', //'/tasks/#ID#/',
- // /*'elements' => array( // not required
- // 'ID' => array(
- // 'name' => 'ID',
- // 'aggr' => null
- // )
- // )*/
- //);
- foreach ($settings['select'] as &$elem)
- {
- //if (in_array($elem['name'], $grcFields, true) && empty($elem['aggr']))
- if ($elem['aggr'] == 'GROUP_CONCAT')
- {
- continue;
- }
- CReport::appendHrefSelectElements($elem, $fList, $entity, $arParams['REPORT_HELPER_CLASS'], $select, $runtime);
- }
- unset($elem);
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="collect columns with aliases, build runtime fields">
- // if there is aggr of init entity or there is no init entity at all, then we think that 1:N need double aggregation
- $is_init_entity_aggregated = false;
- $is_init_entity_in_select = false;
- foreach ($settings['select'] as $num => $elem)
- {
- $chain = $fChainList[$elem['name']];
- if ($chain->getSize() == 2)
- {
- $is_init_entity_in_select = true;
- if (!empty($elem['aggr']))
- {
- $is_init_entity_aggregated = true;
- break;
- }
- }
- }
- if (!$is_init_entity_aggregated && !$is_init_entity_in_select)
- {
- $is_init_entity_aggregated = true;
- }
- // init variables
- $viewColumns = array();
- $viewColumnsByResultName = array();
- // blacklist of entity with aggr
- $aggr_bl = array();
- // grc stuff
- $grcSelectPrimaries = array();
- $grcInitPrimary = false;
- $need_concat_rows = false;
- $grcSettingsNum = array();
- foreach ($settings['select'] as $num => $elem)
- {
- /** @var Entity\Field $field */
- $chain = $fChainList[$elem['name']];
- $field = $fList[$elem['name']];
- $fType = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getFieldDataType'), $field);
- $is_grc = false;
- //if (in_array($elem['name'], $grcFields, true) && empty($elem['aggr']) && !strlen($elem['prcnt']))
- if ($elem['aggr'] == 'GROUP_CONCAT')
- {
- $is_grc = true;
- // collect grc_fields pointers
- $need_concat_rows = true;
- $grcSettingsNum[] = $num;
- }
- list($alias, $selElem) = CReport::prepareSelectViewElement($elem, $settings['select'], $is_init_entity_aggregated, $fList, $fChainList, $arParams['REPORT_HELPER_CLASS'], $entity);
- if (is_array($selElem) && !empty($selElem['expression']))
- {
- // runtime expr
- $fType = $selElem['data_type'];
- }
- else
- {
- // normal field
- $alias = Entity\QueryChain::getAliasByDefinition($entity, $elem['name']);
- }
- if (!$is_grc)
- {
- // grc will be selected later
- if (is_array($selElem))
- {
- // runtime field
- $select[$alias] = $alias;
- $runtime[$alias] = $selElem;
- }
- else
- {
- $select[$alias] = $selElem;
- }
- }
- // default sort
- if ($is_grc
- || ((in_array($fType, array('file', 'disk_file', 'employee', 'crm', 'crm_status', 'iblock_element',
- 'iblock_section'), true)
- || ($arUF['isUF'] && $arUF['ufInfo']['MULTIPLE'] === 'Y'))
- && empty($elem['aggr'])))
- {
- $defaultSort = '';
- }
- else if ($num == $settings['sort'] && array_key_exists('sort_type', $settings))
- {
- $defaultSort = $settings['sort_type'];
- }
- else if (($fType === 'string' || $fType === 'enum') && empty($elem['aggr']))
- {
- $defaultSort = 'ASC';
- }
- else
- {
- $defaultSort = 'DESC';
- }
- $arUF = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'detectUserField'), $field);
- $viewColumns[$num] = array(
- 'field' => $field,
- 'fieldName' => $elem['name'],
- 'resultName' => $alias,
- 'humanTitle' => empty($elem['alias']) ? $alias : $elem['alias'],
- 'defaultSort' => $defaultSort,
- 'aggr' => empty($elem['aggr']) ? '' : $elem['aggr'],
- 'prcnt' => strlen($elem['prcnt']) ? $elem['prcnt'] : '',
- 'href' => empty($elem['href']) ? '' : $elem['href'],
- 'grouping' => ($elem['grouping'] === true) ? true : false,
- 'grouping_subtotal' => ($elem['grouping_subtotal'] === true) ? true : false,
- 'isUF' => $arUF['isUF'],
- 'ufInfo' => $arUF['ufInfo']
- );
- unset($arUF);
- $viewColumnsByResultName[$alias] = &$viewColumns[$num];
- // blacklist of entity with aggr
- //if (!in_array($elem['name'], $grcFields, true) && !empty($elem['aggr']))
- if ($elem['aggr'] != 'GROUP_CONCAT' && !empty($elem['aggr']))
- {
- $preDef = substr($elem['name'], 0, strrpos($elem['name'], '.'));
- $preDef = strlen($preDef) ? $preDef.'.' : '';
- $aggr_bl[$preDef] = true;
- }
- }
- // collect entity primaries of fields without aggregation
- foreach ($settings['select'] as $num => $elem)
- {
- //if (!in_array($elem['name'], $grcFields, true) && empty($elem['aggr']))
- if (empty($elem['aggr']))
- {
- $primary = $viewColumns[$num]['field']->getEntity()->getPrimaryArray();
- $preDef = substr($elem['name'], 0, strrpos($elem['name'], '.'));
- $preDef = strlen($preDef) ? $preDef.'.' : '';
- if (array_key_exists($preDef, $aggr_bl))
- {
- continue;
- }
- foreach ($primary as $pField)
- {
- $palias = Entity\QueryChain::getAliasByDefinition($entity, $preDef.$pField);
- $grcSelectPrimaries[$palias] = $preDef.$pField;
- }
- // remember if there is initEntity primary in data
- if ($viewColumns[$num]['field']->getEntity() === $entity)
- {
- $grcInitPrimary = true;
- }
- }
- }
- // normalize $grcSelectPrimaries
- if ($grcInitPrimary)
- {
- // it's enough only init primary
- $initPrimary = $entity->getPrimaryArray();
- foreach ($grcSelectPrimaries as $k => $v)
- {
- if (!in_array($v, $initPrimary, true))
- {
- unset($grcSelectPrimaries[$k]);
- }
- }
- }
- $select = array_merge($select, $grcSelectPrimaries);
- // </editor-fold>
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="parse filter">
- // <editor-fold defaultstate="collapsed" desc="rewrite values by filter">
- $isFieldResponsibleUser = false;
- $countNumberElementFilter = 1;
- if(substr_count($settings['entity'], "Lead") > 0){
- $nameFieldsResponsibly = LEAD_PROP_RESPONSIBLE_USER;
- }
- else if(substr_count($settings['entity'], "Company") > 0){
- $nameFieldsResponsibly = COMPANY_PROP_RESPONSIBLE_USER;
- }
- else if(substr_count($settings['entity'], "Contact") > 0){
- $nameFieldsResponsibly = CONTACT_PROP_RESPONSIBLE_USER;
- }
- else if(substr_count($settings['entity'], "Deal") > 0){
- $nameFieldsResponsibly = DEAL_PROP_RESPONSIBLE_USER;
- }
- else if(substr_count($settings['entity'], "Activity") > 0){
- $nameFieldsResponsibly = "RESPONSIBLE_ID";
- }
- else if(substr_count($settings['entity'], "Tasks") > 0){
- $nameFieldsResponsibly = "TASK";
- $isFieldResponsibleUser = true;
- }
- else{
- $nameFieldsResponsibly = "TASK";
- $isFieldResponsibleUser = true;
- }
- foreach ($settings['filter'] as $fId => &$fInfo)
- {
- foreach ($fInfo as $k => &$fElem)
- {
- if (!empty($_GET['filter'][$fId]) && array_key_exists($k, $_GET['filter'][$fId]))
- {
- $fElem['value'] = $_GET['filter'][$fId][$k];
- }
- if(($fElem['name'] == $nameFieldsResponsibly) && ($arParams["OWNER_ID"] == "crm")){
- $isFieldResponsibleUser = true;
- }
- }
- }
- if(!$isFieldResponsibleUser){
- // текущий департамент
- $arDepartament = CAllianceCommon::DepartmentOfCurrentUser($USER->GetID());
- if(CAllianceCommon::isUserOfGroup(GROUP_USER_ADMIN) || CAllianceCommon::isUserOfGroup(GROUP_USER_BUSINESS_ADMIN) || CAllianceCommon::isUserOfGroup(GROUP_USER_DIRECTION) || (CAllianceCommon::isUserOfGroup(GROUP_USER_ANALITIC_MASTER))){ // если админ или руководство
- $departmentID = 0;
- }
- else{
- // текущий департамент
- $arDepartament = CAllianceCommon::DepartmentOfCurrentUser($USER->GetID());
- if(in_array(GROUP_USER_SUPPORT, $USER->GetUserGroupArray()) || in_array(GROUP_USER_ANALITIC, $USER->GetUserGroupArray())){
- $departmentID = $arDepartament[0];
- }
- else{
- $departmentID = 1111111;
- }
- }
- // все подразделы текущего департамента
- if($departmentID == 0){
- $arDepartamentOpend = array();
- $rsSect = CIBlockSection::GetList(array('left_margin' => 'asc'), array('IBLOCK_ID' => IBLOCK_DEPARTMENT, 'DEPTH_LEVEL' => 1));
- while ($arSect = $rsSect->GetNext()){
- $arDepartamentOpend = array_merge($arDepartamentOpend, CAllianceCommon::GetListSectionChild($arSect["ID"]));
- }
- }
- else{
- if(count($arDepartament) > 1){
- $arDepartamentOpend = array();
- foreach($arDepartament as $id){
- $arDepartamentOpend = array_merge($arDepartamentOpend, CAllianceCommon::GetListSectionChild($id));
- }
- }
- else
- $arDepartamentOpend = CAllianceCommon::GetListSectionChild($departmentID);
- }
- // список пользователей котрые будут задействованы в фильтре
- $arUserListIDOpenedUser = array();
- // если есть фильтр по подразделениям тогда нужно опрделить подразделы выбранного раздела
- if(IntVal($arResult["other_filter"]["department"]) > 0){
- $arListSelectedSection = CAllianceCommon::GetListSectionChild($arResult["other_filter"]["department"]);
- // удаляем лишние разделы
- foreach($arListSelectedSection as $key=>$sectionId){
- if(!in_array($sectionId, $arDepartamentOpend)) unset($arListSelectedSection[$key]);
- }
- }
- else{
- $arUserListIDOpenedUser[] = $USER->GetID();
- $arListSelectedSection = $arDepartamentOpend;
- }
- // список пользователей этих департаментов
- foreach($arListSelectedSection as $id_department){
- $rsUserListOfDepartament = CUser::GetList($by="ID", $order="desc", array("UF_DEPARTMENT"=>$id_department), array("SELECT"=>array("UF_DEPARTMENT"), "FIELDS"=>array("ID", "NAME", "LAST_NAME", "LOGIN")));
- while($arUserList = $rsUserListOfDepartament->GetNext()):
- if((IntVal($arResult["other_filter"]["staff"]) > 0) && ($arResult["other_filter"]["staff"] != $arUserList["ID"])){
- continue;
- }
- $arUserListIDOpenedUser[] = $arUserList["ID"];
- endwhile;
- }
- if(!CAllianceCommon::isUserOfGroup(GROUP_USER_ADMIN) &&
- !CAllianceCommon::isUserOfGroup(GROUP_USER_BUSINESS_ADMIN) &&
- !CAllianceCommon::isUserOfGroup(GROUP_USER_DIRECTION) &&
- !CAllianceCommon::isUserOfGroup(GROUP_USER_ANALITIC_MASTER) &&
- !CAllianceCommon::isUserOfGroup(GROUP_USER_ANALITIC) &&
- !CAllianceCommon::isUserOfGroup(GROUP_USER_SUPPORT)
- ){
- // проверка, руководитель это или нет
- $rsSect = CIBlockSection::GetList(array('left_margin' => 'asc'), array('IBLOCK_ID' => IBLOCK_DEPARTMENT, 'UF_HEAD' => $USER->GetID()));
- if($arSect = $rsSect->GetNext()){
- // все ок, идет дальше
- $arUserListIDOpenedUser = CAllianceAccountTeam::getUserIfHead($USER->GetID());
- }
- else if(CAllianceCommon::isUserOfGroup(GROUP_USER_ANALITIC)){
- $arUserListIDOpenedUser = CAllianceAccountTeam::getUserIfHead($USER->GetID());
- }
- else{
- // простой клерк, - значит от видит только свои записи
- $arUserListIDOpenedUser = array($USER->GetID());
- }
- }
- if(CAllianceCommon::isUserOfGroup(GROUP_USER_ADMIN) || CAllianceCommon::isUserOfGroup(GROUP_USER_BUSINESS_ADMIN) || CAllianceCommon::isUserOfGroup(GROUP_USER_DIRECTION) || (CAllianceCommon::isUserOfGroup(GROUP_USER_ANALITIC_MASTER))){ // если админ или руководство
- $arUserListIDOpenedUser = array();
- }
- $arUserListIDOpenedUser = array_unique($arUserListIDOpenedUser);
- trace($arUserListIDOpenedUser);
- $arListFilterResponsibly = array();
- foreach($arUserListIDOpenedUser as $userResp){
- if($arParams["OWNER_ID"] == "crm_activity"){
- $arListFilterResponsibly[] = array(
- '=RESPONSIBLE_ID' => $userResp
- );
- }
- else{
- $arListFilterResponsibly[] = array(
- '='.DEAL_PROP_OWNER => $userResp
- );
- $arListFilterResponsibly[] = array(
- '='.DEAL_PROP_SELLER => $userResp
- );
- }
- }
- $arListFilterResponsibly["LOGIC"] = "OR";
- $countNumberElementFilter = count($settings['filter']);
- if(count($arUserListIDOpenedUser) > 0){
- $settings['filter'][$countNumberElementFilter] = $arListFilterResponsibly;
- }
- }
- #trace($settings['filter']);
- unset($fInfo);
- unset($fElem);
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="add filter to fList and fChainList">
- foreach ($settings['filter'] as $fId => $fInfo)
- {
- foreach ($fInfo as $k => $fElem)
- {
- if (is_array($fElem) && $fElem['type'] == 'field')
- {
- if (preg_match('/__COLUMN__\d+/', $fElem['name']))
- {
- continue;
- }
- try
- {
- $chain = Entity\QueryChain::getChainByDefinition($entity, $fElem['name']);
- }
- catch (Exception $e)
- {
- if ($e->getCode() == 100)
- throw new BXUserException('<p style="color: red;">'.GetMessage('REPORT_UNKNOWN_FIELD_DEFINITION').'</p>');
- else
- throw $e;
- }
- $field = $chain->getLastElement()->getValue();
- if (is_array($field)) $field = end($field);
- $fList[$fElem['name']] = $field;
- $fChainList[$fElem['name']] = $chain;
- }
- }
- }
- // </editor-fold>
- #p($settings['filter']);
- // <editor-fold defaultstate="collapsed" desc="collect changeables">
- $changeableFilters = array();
- $changeableFiltersEntities = array();
- foreach ($settings['filter'] as $fId => &$fInfo)
- {
- foreach ($fInfo as $k => &$fElem)
- {
- if (is_array($fElem) && $fElem['type'] == 'field' && (int) $fElem['changeable'] > 0)
- {
- $match = array();
- if (preg_match('/__COLUMN__(\d+)/', $fElem['name'], $match))
- {
- /** @var Entity\Field[] $view */
- $num = $match[1];
- $view = $viewColumns[$num];
- $data_type = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getFieldDataType'), $view['field']);
- if ($view['prcnt'])
- {
- $data_type = 'float';
- }
- else if ($view['aggr'] == 'COUNT_DISTINCT')
- {
- $data_type = 'integer';
- }
- $field = null;
- }
- else
- {
- $field = $fList[$fElem['name']];
- $data_type = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getFieldDataType'), $field);
- }
- if ($field instanceof Entity\ReferenceField)
- {
- $tmpElem = $fElem;
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'fillFilterReferenceColumn'),
- array(&$tmpElem, &$field)
- );
- $value = $tmpElem['value'];
- $changeableFiltersEntities[$field->getRefEntityName()] = true;
- }
- else
- {
- $arUF = call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'detectUserField'),
- array($field)
- );
- if ($arUF['isUF'] && is_array($arUF['ufInfo']) && isset($arUF['ufInfo']['USER_TYPE_ID']))
- {
- $tmpElem = $fElem;
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'fillFilterUFColumn'),
- array(&$tmpElem, $field, $arUF['ufInfo'])
- );
- $value = $tmpElem['value'];
- }
- else
- {
- $value = $fElem['value'];
- }
- }
- // detect UF
- $arUF = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'detectUserField'), $field);
- $changeableFilters[] = array(
- 'name' => $fElem['name'],
- 'title' => '', // will be added later
- 'value' => $value,
- 'compare' => $fElem['compare'],
- 'filter' => $fId,
- 'num' => $k,
- 'formName' => 'filter['.$fId.']['.$k.']',
- 'formId' => 'filter_'.$fId.'_'.$k,
- 'field' => $field,
- 'data_type' => $data_type,
- 'isUF' => $arUF['isUF'],
- 'ufId' => $arUF['isUF'] ? $arUF['ufInfo']['ENTITY_ID'] : '',
- 'ufName' => $arUF['isUF'] ? $arUF['ufInfo']['FIELD_NAME'] : ''
- );
- unset($arUF);
- }
- }
- }
- unset($fInfo);
- unset($fElem);
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="rewrite references to primary">
- foreach ($settings['filter'] as $fId => &$fInfo)
- {
- foreach ($fInfo as $k => &$fElem)
- {
- if (is_array($fElem) && $fElem['type'] == 'field')
- {
- // delete empty filters
- if (is_array($fElem['value']))
- {
- foreach ($fElem['value'] as $l => $value)
- {
- if ($value === '' || !is_numeric($l)) unset($fElem['value'][$l]);
- }
- $l = count($fElem['value']);
- if ($l === 0) $fElem['value'] = '';
- else if ($l === 1) $fElem['value'] = $fElem['value'][0];
- }
- if (preg_match('/__COLUMN__(\d+)/', $fElem['name'], $match))
- {
- $num = $match[1];
- $field = $viewColumns[$num]['field'];
- }
- else
- {
- $field = $fList[$fElem['name']];
- }
- // rewrite
- if ($field instanceof Entity\ReferenceField)
- {
- // get primary
- $field = $field->GetRefEntity()->getField('ID');
- // get primary filter field name
- $primaryFilterField = call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'getEntityFilterPrimaryFieldName'),
- array($fElem)
- );
- $fElem['name'] .= '.'.$primaryFilterField;
- unset($primaryFilterField);
- $fList[$fElem['name']] = $field;
- $fChainList[$fElem['name']] = Entity\QueryChain::getChainByDefinition($entity, $fElem['name']);
- }
- $dataType = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getFieldDataType'), $field);
- if (($dataType != 'crm') && ($fElem['compare'] != "заполнено" && $fElem['compare'] != "пусто"))
- {
- if ($fElem['value'] === '')
- {
- unset($fInfo[$k]);
- continue;
- }
- }
- // rewrite date <=> {today, yesterday, tomorrow, etc}
- if ($dataType === 'datetime'
- && !CheckDateTime($fElem['value'], CSite::GetDateFormat('SHORT'))
- )
- {
- $fElem['value'] = ConvertTimeStamp(strtotime($fElem['value']), 'SHORT');
- }
- // rewrite date=DAY to date BETWEEN DAY_START AND DAY_END
- if ($dataType === 'datetime')
- {
- if ($fElem['compare'] == 'EQUAL')
- {
- // clone filter
- $fElem_start = $fElem;
- $fElem_end = $fElem;
- $fElem_start['compare'] = 'GREATER_OR_EQUAL';
- $fElem_start['value'] .= ' 00:00:00';
- $fElem_end['compare'] = 'LESS_OR_EQUAL';
- $fElem_end['value'] .= ' 23:59:59';
- // replace filter by subfilter
- $settings['filter'][] = array('LOGIC' => 'AND', $fElem_start, $fElem_end);
- end($settings['filter']);
- $lastFilterNum = key($settings['filter']);
- $fElem = array('type' => 'filter', 'name' => $lastFilterNum);
- }
- else if ($fElem['compare'] == 'NOT_EQUAL')
- {
- // clone filter
- $fElem_start = $fElem;
- $fElem_end = $fElem;
- $fElem_start['compare'] = 'LESS';
- $fElem_start['value'] .= ' 00:00:00';
- $fElem_end['compare'] = 'GREATER';
- $fElem_end['value'] .= ' 23:59:59';
- // replace filter by subfilter
- $settings['filter'][] = array('LOGIC' => 'AND', $fElem_start, $fElem_end);
- end($settings['filter']);
- $lastFilterNum = key($settings['filter']);
- $fElem = array('type' => 'filter', 'name' => $lastFilterNum);
- }
- else if ($fElem['compare'] == 'LESS_OR_EQUAL')
- {
- $fElem['value'] .= ' 23:59:59';
- }
- else if ($fElem['compare'] == 'GREATER_OR_EQUAL')
- {
- $fElem['value'] .= ' 00:00:00';
- }
- else if ($fElem['compare'] == 'LESS')
- {
- $fElem['value'] .= ' 00:00:00';
- }
- else if ($fElem['compare'] == 'GREATER')
- {
- $fElem['value'] .= ' 23:59:59';
- }
- }
- }
- }
- }
- unset($fInfo);
- unset($fElem);
- // </editor-fold>
- #p($settings['filter']);
- // <editor-fold defaultstate="collapsed" desc="rewrite 1:N relations to EXISTS expression">
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'beforeFilterBackReferenceRewrite'),
- array(&$settings['filter'], $viewColumns)
- );
- $f_filter_alias_count = 0;
- foreach ($settings['filter'] as $fId => &$fInfo)
- {
- foreach ($fInfo as $k => &$fElem)
- {
- if (is_array($fElem) && $fElem['type'] == 'field')
- {
- if (preg_match('/__COLUMN__\d+/', $fElem['name']))
- {
- continue;
- }
- $fField = $fList[$fElem['name']];
- $arUF = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'detectUserField'), $fField);
- if ($arUF['isUF'])
- {
- $fFieldDataType = call_user_func(
- array($arParams['REPORT_HELPER_CLASS'], 'getUserFieldDataType'), $arUF
- );
- if ($fFieldDataType === 'boolean')
- {
- if ($fElem['value'] === 'true')
- $fElem['value'] = 1;
- else
- $fElem['value'] = 0;
- }
- }
- $chain = $fChainList[$fElem['name']];
- if ($chain->hasBackReference())
- {
- $confirm = call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'confirmFilterBackReferenceRewrite'),
- array(&$fElem, $chain)
- );
- if (!$confirm)
- {
- continue;
- }
- $_sub_init_table_alias = ToLower($entity->getCode());
- $_sub_filter = array();
- // add primary linking with main query
- foreach ($entity->GetPrimaryArray() as $_primary)
- {
- $_sub_filter['='.$_primary] = new CSQLWhereExpression('?#', $_sub_init_table_alias.'.'.$_primary);
- }
- // add value filter
- $arCompareVariations = CAllianceReportDefault::$iBlockCompareVariations;
- $filterCompare = $arCompareVariations[$fElem['compare']];
- $filterName = $fElem['name'];
- $filterValue = $fElem['value'];
- if ($filterCompare === '>%')
- {
- $filterCompare = '';
- $filterValue = $filterValue.'%';
- }
- $_sub_filter[$filterCompare.$filterName] = $filterValue;
- // build subquery
- $_sub_query = new Entity\Query($entity);
- $_sub_query->setFilter($_sub_filter);
- $_sub_query->setTableAliasPostfix('_sub');
- $_sub_sql = 'EXISTS('.$_sub_query->getQuery().')';
- $_sub_sql = '(CASE WHEN '.$_sub_sql.' THEN 1 ELSE 0 END)';
- // expression escaping as sprintf requires
- $_sub_sql = str_replace('%', '%%', $_sub_sql);
- $_runtime_field = array(
- 'data_type' => 'integer',
- 'expression' => array($_sub_sql)
- );
- $f_filter_alias = 'F_FILTER_ALIAS_'.(++$f_filter_alias_count);
- $runtime[$f_filter_alias] = $_runtime_field;
- $fElem['name'] = $f_filter_alias;
- $fElem['compare'] = 'EQUAL';
- $fElem['value'] = 1;
- }
- }
- }
- }
- unset($fInfo);
- unset($fElem);
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="rewrite __COLUMN__\d filters">
- foreach ($settings['filter'] as $fId => &$fInfo)
- {
- foreach ($fInfo as $k => &$fElem)
- {
- if (is_array($fElem) && $fElem['type'] == 'field')
- {
- if (preg_match('/__COLUMN__(\d+)/', $fElem['name'], $match))
- {
- $num = $match[1];
- $view = $viewColumns[$num];
- if (!empty($view['prcnt']) || !empty($view['aggr']))
- {
- $fElem['name'] = $view['resultName'];
- }
- else
- {
- $fElem['name'] = $view['fieldName'];
- }
- }
- }
- }
- }
- // </editor-fold>
- #p($settings['filter']);
- $iFilter = CAllianceReportDefault::makeSingleFilter($settings['filter']);
- if(!$isFieldResponsibleUser){
- $iFilter[] = $settings['filter'][$countNumberElementFilter];
- }
- #trace($iFilter);
- $filter[] = $iFilter;
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="parse sort">
- $sort_id = $settings['sort'];
- $sort_name = $viewColumns[$sort_id]['resultName'];
- $sort_type = $viewColumns[$sort_id]['defaultSort'];
- // rewrite sort by POST
- if (array_key_exists('sort_id', $_GET) && array_key_exists($_GET['sort_id'], $viewColumns))
- {
- $sort_id = $_GET['sort_id'];
- $sort_name = $viewColumns[$sort_id]['resultName'];
- if ($_GET['sort_type'] === 'ASC' || $_GET['sort_type'] === 'DESC')
- {
- $sort_type = $_GET['sort_type'];
- }
- else
- {
- $sort_type = $viewColumns[$sort_id]['defaultSort'];
- }
- }
- if ($sort_name != '' && ($sort_type === 'ASC' || $sort_type === 'DESC'))
- $order = array($sort_name => $sort_type);
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="parse limit">
- if (!$bGroupingMode) // no limit in grouping mode
- {
- $limit['nPageSize'] = $arParams['ROWS_PER_PAGE'];
- if (!empty($settings['limit']))
- {
- $limit['nPageTop'] = $settings['limit'];
- }
- else if (!$excelView)
- {
- $limit['iNumPage'] = is_set($_GET['PAGEN_1']) ? $_GET['PAGEN_1'] : 1;
- $limit['bShowAll'] = true;
- }
- }
- if($excelView){
- $limit['nPageTop'] = 1000000;
- $limit['bShowAll'] = true;
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="connect Lang">
- $fullHumanTitles = CReport::collectFullHumanTitles($fieldsTree);
- foreach ($viewColumns as $num => &$view)
- {
- if ($view['resultName'] == $view['humanTitle'])
- {
- $view['humanTitle'] = CReport::getFullColumnTitle($view, $viewColumns, $fullHumanTitles);
- }
- }
- unset($view);
- foreach ($changeableFilters as &$chFilter)
- {
- if (preg_match('/__COLUMN__(\d+)/', $chFilter['name'], $match))
- {
- $num = $match[1];
- $chFilter['title'] = $viewColumns[$num]['humanTitle'];
- }
- else
- {
- $chFilter['title'] = $fullHumanTitles[$chFilter['name']];
- }
- }
- unset($chFilter);
- // </editor-fold>
- // rewrite User SHORT_NAME
- CReport::rewriteUserShortName($select, $runtime, $arParams['USER_NAME_FORMAT'], $entity);
- // <editor-fold defaultstate="collapsed" desc="retrieve report rows">
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'beforeViewDataQuery'),
- array(&$select, &$filter, &$group, &$order, &$limit, &$options, &$runtime)
- );
- // отчет по задачам
- if($arParams['REPORT_HELPER_CLASS'] == "CTasksReportHelper"){
- $select["PARENT_ID"] = "PARENT_ID";
- $select["ADD_IN_REPORT"] = "ADD_IN_REPORT";
- #echo '<pre>'.print_r($select, true).'</pre>';
- }
- $main_query = new Entity\Query($entity);
- $main_query->setSelect($select)
- ->setFilter($filter)
- ->setGroup($group)
- ->setOrder($order)
- ;
- foreach ($runtime as $k => $v)
- {
- $main_query->registerRuntimeField($k, $v);
- // add view column if needed
- if (isset($v['view_column']) && is_array($v['view_column']))
- {
- $runtimeColumnInfo = $v['view_column'];
- $newNum = max(array_keys($viewColumns)) + 1;
- $queryChains = $main_query->getChains();
- if (isset($queryChains[$k]))
- {
- $runtimeField = $queryChains[$k]->getLastElement()->getValue();
- if (is_array($runtimeField)) $runtimeField = end($runtimeField);
- /*$arUF = CReport::detectUserField($runtimeField, $arUFInfo);*/
- $viewColumns[$newNum] = array(
- 'field' => $runtimeField,
- 'fieldName' => $k,
- 'resultName' => $k,
- 'humanTitle' => empty($runtimeColumnInfo['humanTitle']) ? '' : $runtimeColumnInfo['humanTitle'],
- 'defaultSort' => '',
- 'aggr' => '',
- 'prcnt' => '',
- 'href' => empty($runtimeColumnInfo['href']) ? '' : $runtimeColumnInfo['href'],
- 'grouping' => false,
- 'grouping_subtotal' => ($runtimeColumnInfo['grouping_subtotal'] === true) ? true : false,
- 'runtime' => true/*,
- 'isUF' => $arUF['isUF'],
- 'isUF' => $arUF['isUF'],
- 'ufInfo' => $arUF['ufInfo']*/
- );
- /*unset($arUF);*/
- $viewColumnsByResultName[$k] = &$viewColumns[$newNum];
- }
- }
- }
- if (isset($limit['nPageTop']))
- $main_query->setLimit($limit['nPageTop']);
- $result = $main_query->exec();
- $result = new CDBResult($result);
- if (!$bGroupingMode)
- {
- if (isset($limit['nPageTop']))
- $result->NavStart($limit['nPageTop']);
- else
- $result->NavStart($limit['nPageSize']/*, true, $limit['iNumPage']*/);
- }
- $data = array();
- $grcDataPrimaryValues = array();
- $grcDataPrimaryPointers = array();
- while ($row = $result->Fetch())
- {
- // rewrite UF values
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'rewriteResultRowValues'),
- array(&$row, &$viewColumnsByResultName)
- );
- // attach URLs
- foreach ($row as $k => $v)
- {
- if (!array_key_exists($k, $viewColumnsByResultName))
- {
- continue;
- }
- $elem = $viewColumnsByResultName[$k];
- if (!empty($elem['href']))
- {
- $url = CReport::generateValueUrl($elem, $row, $entity);
- $row['__HREF_'.$k] = $url;
- }
- }
- $row['TITLE'] = htmlspecialchars_decode($row['TITLE']);
- #echo '<pre>'.print_r($row, true).'</pre>';
- // collect
- $data[] = $row;
- // grc stuff
- $grc_primary_string = '';
- foreach ($grcSelectPrimaries as $alias => $def)
- {
- // for grc filter
- $grcDataPrimaryValues['='.$def][] = $row[$alias];
- // for concat
- $grc_primary_string .= (string) $row[$alias] . '/';
- }
- // save original data indexes for grc values
- if (!isset($grcDataPrimaryPointers[$grc_primary_string]))
- {
- $grcDataPrimaryPointers[$grc_primary_string] = array();
- }
- $grcDataPrimaryPointers[$grc_primary_string][] = count($data)-1;
- }
- $grcDataPrimaryValues = array_map('array_unique', $grcDataPrimaryValues);
- // </editor-fold>
- if (empty($settings['limit']))
- {
- $arResult["NAV_STRING"] = $result->GetPageNavString('', (is_set($arParams['NAV_TEMPLATE'])) ? $arParams['NAV_TEMPLATE'] : 'arrows');
- $arResult["NAV_PARAMS"] = $result->GetNavParams();
- $arResult["NAV_NUM"] = $result->NavNum;
- }
- // если это последняя страница
- if(substr_count($arResult["NAV_STRING"], 'id="navigation_1_next_page') == 0){
- // сделать выборку параметров фильтрации по компаниям
- $arFilterCompany = array();
- if(substr_count($settings['entity'], "Deal") > 0){
- $arFilterCompany = CAllianceReport::GetFilterCompany($filter);
- $arSelectCompany = CAllianceReport::GetSelectCompany($select);
- if(count($arFilterCompany) > 0){
- $dataCompany = CAllianceReport::GetDataCompanyNotQuotation($arFilterCompany, $arSelectCompany, $select);
- if(count($data) < 10){
- $data = array_merge($data, $dataCompany);
- }
- }
- }
- }
- // <editor-fold defaultstate="collapsed" desc="retrieve total counts">
- $totalSelect = $select;
- $totalColumns = array();
- foreach ($viewColumns as $num => $view)
- {
- // total's fields are the same as percentable fields
- // they are also all numerics
- if (CReport::isColumnTotalCountable($view, $arParams['REPORT_HELPER_CLASS']))
- {
- // exclude from select all except those
- $totalColumns[$view['resultName']] = true;
- }
- }
- // save only totalCountable visible fields
- foreach ($totalSelect as $k => $v)
- {
- if (!array_key_exists($k, $totalColumns))
- {
- unset($totalSelect[$k]);
- }
- }
- // add SUM aggr
- $_totalSelect = $totalSelect;
- $totalSelect = array();
- foreach ($_totalSelect as $k => $v)
- {
- $totalSelect[] = new Entity\ExpressionField('TOTAL_'.$k, 'SUM(%s)', $k);
- }
- if (!empty($totalSelect))
- {
- // source query
- $query_from = new Entity\Query($entity);
- $query_from->setSelect($select);
- $query_from->setFilter($filter);
- $query_from->setGroup($group);
- foreach ($runtime as $k => $v)
- {
- $query_from->registerRuntimeField($k, $v);
- }
- // total query
- $total_query = new Entity\Query($query_from);
- $total_query->setSelect($totalSelect);
- $result = $total_query->exec();
- $total = $result->fetch();
- $total = ($total === false) ? array() : $total;
- }
- else
- {
- $total = array();
- }
- // </editor-fold>
- // <editor-fold defaultstate="collapsed" desc="group_concat fields">
- $grcData = array();
- // check necessity of concat rows
- if ($need_concat_rows && $grcDataPrimaryValues)
- {
- // filter - add primaries from data
- if ($grcInitPrimary)
- {
- // init primary enough
- $grcFilter = $grcDataPrimaryValues;
- }
- else
- {
- // merge with primaries
- $grcFilter = array_merge($filter, $grcDataPrimaryValues);
- }
- // select data for each grc field
- foreach ($grcSettingsNum as $num)
- {
- $elem = $settings['select'][$num];
- // prepare
- $grcSelect = $grcSelectPrimaries;
- CReport::appendHrefSelectElements($elem, $fList, $entity, $arParams['REPORT_HELPER_CLASS'], $grcSelect, $runtime);
- if (!empty($elem['href']))
- {
- $viewColumns[$num]['href'] = $elem['href'];
- }
- list($alias, $selElem) = CReport::prepareSelectViewElement($elem, $settings['select'], $is_init_entity_aggregated, $fList, $fChainList, $arParams['REPORT_HELPER_CLASS'], $entity);
- if (is_array($selElem) && !empty($selElem['expression']))
- {
- $runtime[$alias] = $selElem;
- $grcSelect[] = $alias;
- }
- else
- {
- // normal field
- $alias = Entity\QueryChain::getAliasByDefinition($entity, $elem['name']);
- $grcSelect[$alias] = $selElem;
- }
- CReport::rewriteUserShortName($grcSelect, $runtime, $arParams['USER_NAME_FORMAT'], $entity, true);
- // add primary of grc entity field
- $grcChain = Entity\QueryChain::getChainByDefinition($entity, $elem['name']);
- $grc_field = $grcChain->getLastElement()->getValue();
- if (is_array($grc_field)) $grc_field = end($grc_field);
- $grc_primary = end($grc_field->getEntity()->getPrimaryArray());
- $grc_marker = substr($elem['name'], 0, strrpos($elem['name'], '.')) . '.' . $grc_primary;
- $grc_marker_alias = Entity\QueryChain::getAliasByDefinition($entity, $grc_marker);
- $grcSelect[$grc_marker_alias] = $grc_marker;
- // select
- $resultName = $viewColumns[$num]['resultName'];
- $grcData[$resultName] = array();
- $grc_query = new Entity\Query($entity);
- $grc_query->setSelect($grcSelect);
- $grc_query->setFilter($grcFilter);
- foreach ($runtime as $k => $v)
- {
- $grc_query->registerRuntimeField($k, $v);
- }
- $result = $grc_query->exec();
- while ($row = $result->fetch())
- {
- if (empty($row[$grc_marker_alias]))
- {
- continue;
- }
- $grcData[$resultName][] = $row;
- }
- // add empty values to data
- foreach ($data as $k => $v)
- {
- $data[$k][$alias] = null;
- }
- // add values to data
- foreach ($grcData[$resultName] as $grcIndex => &$row)
- {
- $grc_primary_string = '';
- foreach ($grcSelectPrimaries as $pResultName => $def)
- {
- $grc_primary_string .= (string) $row[$pResultName] . '/';
- }
- $dataIndexes = $grcDataPrimaryPointers[$grc_primary_string];
- foreach ($dataIndexes as $dataIndex)
- {
- if (!isset($data[$dataIndex][$alias]))
- {
- $data[$dataIndex][$alias] = array();
- }
- if (!empty($elem['href']) && strlen($row[$alias]))
- {
- $url = CReport::generateValueUrl($elem, $row, $entity);
- $row['__HREF_'.$alias] = $url;
- }
- $data[$dataIndex][$alias][$grcIndex] = $row[$alias];
- }
- }
- unset($row);
- }
- } // end concat grc
- // </editor-fold>
- // collect UF values
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'collectUFValues'),
- array(&$data, &$viewColumnsByResultName, $total)
- );
- $customChartTotal = $customChartData = array();
- // format results
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'formatResults'),
- array(&$data, &$viewColumnsByResultName, $total, &$customChartData)
- );
- call_user_func_array(
- array($arParams['REPORT_HELPER_CLASS'], 'formatResultsTotal'),
- array(&$total, &$viewColumnsByResultName, &$customChartTotal)
- );
- // отчет по задачам
- if($arParams['REPORT_HELPER_CLASS'] == "CTasksReportHelper"){
- foreach($data as $keyData=>$valueData){
- if(IntVal($valueData["PARENT_ID"]) > 0){
- $data[$keyData]["ID"] = IntVal($valueData["PARENT_ID"]);
- $data[$keyData]["ADD_IN_REPORT"] = "Подзадача";
- }
- else{
- $data[$keyData]["ADD_IN_REPORT"] = "Задача";
- }
- }
- }
- if(substr_count($entityName, 'Activity') > 0){
- $arClient = $arDescription = $arActivityID = array();
- // собираем данные о клиентах и описаниям всех событий
- foreach($data as $keyData=>$valueData){
- $arActivityID[] = $valueData["ID"];
- }
- // выборка данных
- if(count($arActivityID) > 0){
- $rsActivity = CCrmActivity::GetList(array(), array("ID"=>$arActivityID), false, false, array("ID", "SUBJECT", "DESCRIPTION", "OWNER_ID", "OWNER_TYPE_ID"));
- while($arActivity = $rsActivity->Fetch()){
- if($arActivity["OWNER_TYPE_ID"] == CCrmOwnerType::Contact)
- {
- $arOwner = CCrmContact::GetByID($arActivity["OWNER_ID"]);
- $arClient[$arActivity["ID"]] = $arOwner["LAST_NAME"]." ".$arOwner["NAME"];
- }
- elseif($arActivity["OWNER_TYPE_ID"] == CCrmOwnerType::Company)
- {
- $arOwner = CCrmCompany::GetByID($arActivity["OWNER_ID"]);
- $arClient[$arActivity["ID"]] = $arOwner["TITLE"];
- }
- elseif($arActivity["OWNER_TYPE_ID"] == CCrmOwnerType::Lead)
- {
- $arOwner = CCrmLead::GetByID($arActivity["OWNER_ID"]);
- $arClient[$arActivity["ID"]] = $arOwner["LAST_NAME"]." ".$arOwner["NAME"];
- }
- elseif($arActivity["OWNER_TYPE_ID"] == CCrmOwnerType::Deal)
- {
- $rsListDeal = CCrmDeal::GetListEx(array("ID"=>"ASC"), array("ID"=>$arActivity["OWNER_ID"]), false, false, array("ID", "TITLE", "COMPANY_ID", DEAL_PROP_LEAD_ID, DEAL_PROP_NAME_INSURED));
- if($arDeal = $rsListDeal->GetNext()){
- if(IntVal($arDeal[DEAL_PROP_LEAD_ID]) == 0){
- $arOwner = CCrmCompany::GetByID(str_replace("CO_", "", $arDeal[DEAL_PROP_NAME_INSURED]));
- $arClient[$arActivity["ID"]] = $arOwner["TITLE"];
- }
- else{
- $arClient[$arActivity["ID"]] = 'Лид';
- }
- }
- else{
- $arClient[$arActivity["ID"]] = "";
- }
- }
- $arDescription[$arActivity["ID"]] = preg_replace('#\[.+\]#Uis', '', HTMLToTxt(htmlspecialchars_decode($arActivity["DESCRIPTION"])));
- }
- }
- foreach($data as $keyData=>&$valueData){
- $valueData["LOCATION"] = $arClient[$valueData["ID"]]?$arClient[$valueData["ID"]]:"";
- $valueData["DIRECTION"] = $arDescription[$valueData["ID"]]?$arDescription[$valueData["ID"]]:"";
- }
- }
- foreach($data as $keyData=>$valueData){
- if(strlen($valueData["CRM_DEAL_COMPANY_BY_TITLE"]) > 0){
- $arDealInfo = CAllianceCommon::GetUFPropertyDeal($valueData["ID"], array(DEAL_PROP_LEAD_ID));
- $LeadID = $arDealInfo[DEAL_PROP_LEAD_ID];
- if(IntVal($LeadID) > 0){
- $arLeadInfo = CCrmLead::GetByID($LeadID, false);
- $data[$keyData]["CRM_DEAL_COMPANY_BY_TITLE"] = "<a href='/crm/lead/show/".$arLeadInfo["ID"]."/' target='_blank'>".$arLeadInfo["TITLE"]."</a>";
- }
- }
- // вывод данных по лиду
- $arListPropLead = array();
- foreach($valueData as $key=>$value){
- if(substr_count($key, "CRM_DEAL_LEAD_BY_") > 0){
- $arListPropLead[] = str_replace("CRM_DEAL_LEAD_BY_", "", $key);
- }
- }
- if(count($arListPropLead) > 0){
- if(IntVal($LeadID) == 0){
- $arDealInfo = CAllianceCommon::GetUFPropertyDeal($valueData["ID"], array(DEAL_PROP_LEAD_ID));
- $LeadID = $arDealInfo[DEAL_PROP_LEAD_ID];
- }
- $arLeadPropList = CAllianceCommon::GetUFPropertyLead($LeadID, $arListPropLead);
- foreach($arListPropLead as $propName){
- if($propName == "UF_COMPANY_TYPE"){
- $rsEnum = CUserFieldEnum::GetList(array(), array("USER_FIELD_NAME"=>"UF_COMPANY_TYPE", "ID" =>$arLeadPropList[$propName]));
- if($arEnum = $rsEnum->GetNext()){
- $arLeadPropList[$propName] = $arEnum['VALUE'];
- }
- }
- else if($propName == "UF_TYPE"){
- $rsEnum = CUserFieldEnum::GetList(array(), array("USER_FIELD_NAME"=>"UF_TYPE", "ID" =>$arLeadPropList[$propName]));
- if($arEnum = $rsEnum->GetNext()){
- $arLeadPropList[$propName] = $arEnum['VALUE'];
- }
- }
- else if($propName == "UF_NORESIDENT"){
- $rsEnum = CUserFieldEnum::GetList(array(), array("USER_FIELD_NAME"=>"UF_NORESIDENT", "ID" =>$arLeadPropList[$propName]));
- if($arEnum = $rsEnum->GetNext()){
- $arLeadPropList[$propName] = $arEnum['VALUE'];
- }
- }
- else if($propName == LEAD_PROP_INDUSTRY){
- $rsEnum = CIBlockSection::GetByID($arLeadPropList[$propName])->GetNext();
- $arLeadPropList[$propName] = $rsEnum['NAME'];
- }
- else if($propName == LEAD_PROP_POD_INDUSTRY){
- $rsEnum = CIBlockElement::GetByID($arLeadPropList[$propName])->GetNext();
- $arLeadPropList[$propName] = $rsEnum['NAME'];
- }
- $data[$keyData]["CRM_DEAL_LEAD_BY_".$propName] = $arLeadPropList[$propName];
- }
- }
- }
- }
- catch (Exception $e)
- {
- if ($e instanceof BXUserException)
- {
- $arResult['ERROR'] = $e->getMessage();
- }
- else
- {
- CTimeZone::Enable();
- throw $e;
- }
- }
- CTimeZone::Enable();
- if(substr_count($entityName, 'Activity') > 0){
- foreach($viewColumns as &$arItemTree){
- if($arItemTree["fieldName"] == "DIRECTION"){
- $arItemTree["humanTitle"] = $arItemTree["fullHumanTitle"] = "Описание";
- }
- if($arItemTree["fieldName"] == "LOCATION"){
- $arItemTree["humanTitle"] = $arItemTree["fullHumanTitle"] = "Клиент";
- }
- }
- }
- // template vars
- $arResult['entityName'] = $entityName;
- $arResult['helperClassName'] = $arParams['REPORT_HELPER_CLASS'];
- $arResult['fList'] = $fList;
- $arResult['settings'] = $settings;
- $arResult['sort_id'] = $sort_id;
- $arResult['sort_type'] = $sort_type;
- $arResult['report'] = $report;
- $arResult['viewColumns'] = $viewColumns;
- $arResult['data'] = $data;
- $arResult['grcData'] = $grcData;
- $arResult['changeableFilters'] = $changeableFilters;
- $arResult['ufEnumerations'] = $arUFEnumerations;
- $arResult['changeableFiltersEntities'] = $changeableFiltersEntities;
- $arResult['chfilter_examples'] = array();
- $arResult['total'] = $total;
- $arResult['form_date'] = $form_date;
- $arResult['period'] = $period;
- $arResult['groupingMode'] = $bGroupingMode;
- $arResult['customColumnTypes'] = $customColumnTypes;
- $arResult['customChartData'] = $customChartData;
- $arResult['customChartTotal'] = $customChartTotal;
- $arResult['ufInfo'] = call_user_func(array($arParams['REPORT_HELPER_CLASS'], 'getUFInfo'));
- if ($excelView)
- {
- $APPLICATION->RestartBuffer();
- while(ob_get_level()) {
- ob_end_clean();
- }
- ob_start();
- $this->IncludeComponentTemplate('excel_x'); //build HTML
- $report_excel_data = ob_get_contents();
- ob_end_clean();
- if($arParams['CRON'] != "Y"){
- if($report_excel_data) {
- require_once($_SERVER['DOCUMENT_ROOT'].'/local/php_interface/excel_parser/PHPExcel.php');
- require_once($_SERVER['DOCUMENT_ROOT'].'/local/php_interface/excel_parser/PHPExcel/Writer/Excel2007.php');
- $current_micro = mktime();
- $inputFileType = 'HTML';
- $inputFileName = $_SERVER['DOCUMENT_ROOT'].'/upload/tmp/report/report_'.$current_micro.'.html';
- $outputFileType = 'Excel2007';
- //$outputFileName = $_SERVER['DOCUMENT_ROOT']'./upload/tmp/reports/report_'.$current_micro.'.xlsx';
- $outputFileName = 'report_'.$current_micro.'.xlsx';
- if(file_put_contents($inputFileName, $report_excel_data) === false) {
- throw new \Exception('Cannot create file '.$inputFileName.' to save report in XLSX format! Exit.');
- }
- else {
- try {
- $objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
- $objPHPExcel = $objPHPExcelReader->load($inputFileName);
- $first_row_index = 1;
- $objPHPExcel->getActiveSheet()->removeRow($first_row_index); //hack - because first row always appeared empty!
- $objPHPExcel->getActiveSheet()->getStyle($first_row_index)->getFont()->setBold(true);
- $lastColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
- $lastColumn++;
- //cell styles begin
- $cell_styleArray = array(
- 'font' => array(
- 'bold' => true,
- 'color' => array('rgb' => '000000'),
- 'size' => 8,
- ),
- 'alignment' => array(
- 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
- 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
- ),
- );
- $cell_width = 20;
- $row_height = 30;
- $objPHPExcel->getActiveSheet()->getRowDimension($first_row_index)->setRowHeight($row_height); //apply first row styles
- for($column = 'A'; $column != $lastColumn; $column++) {
- $objPHPExcel->getActiveSheet()->getStyle($column.$first_row_index)->applyFromArray($cell_styleArray); //apply first row cell styles
- //$objPHPExcel->getActiveSheet()->getStyle($column.$first_row_index)->getAlignment()->setWrapText(true);
- $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth($cell_width);
- }
- $lastRow = $objPHPExcel->getActiveSheet()->getHighestRow();
- for($column = 'A'; $column != $lastColumn; $column++) {
- for($row = $first_row_index + 1; $row <= $lastRow; $row++) {
- $cell_value = $objPHPExcel->getActiveSheet()->getCell($column.$row)->getValue();
- $cell_value_blanked = trim(preg_replace("/[[:blank:]]+/", "", $cell_value));
- $arValue = explode(".", $cell_value_blanked);
- if(is_numeric($cell_value_blanked)) {
- $objPHPExcel->getActiveSheet()->getCell($column.$row)->setValue($cell_value_blanked);
- $objPHPExcel->getActiveSheet()->getStyle($column.$row)->getNumberFormat()->setFormatCode('_-* # ##0_р_._-;-* # ##0_р_._-;_-* "-"??_р_._-;_-@_-'); //correct formatting of numeric values
- }
- else if(substr_count($cell_value_blanked, "href") > 0){
- $objPHPExcel->getActiveSheet()->getStyle($column.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
- }
- else if(count($arValue) == 3){ // date
- $objPHPExcel->getActiveSheet()->getStyle($column.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14);
- }
- }
- }
- $objPHPExcel->getActiveSheet()->getStyle('A1:'.$lastColumn.$lastRow)->applyFromArray(array(
- 'borders' => array(
- 'allborders' => array(
- 'style' => PHPExcel_Style_Border::BORDER_THIN
- )
- )
- )); //set borders to all cells
- unlink($inputFileName); //remove source file
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header("Content-Disposition: attachment;filename=$outputFileName");
- header("Cache-Control: max-age=0");
- $objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
- $objPHPExcel = $objPHPExcelWriter->save("php://output");
- } catch (Exception $e) {
- Header("Content-Type: application/force-download");
- Header("Content-Type: application/octet-stream");
- Header("Content-Type: application/download");
- Header("Content-Disposition: attachment;filename=report.xls");
- Header("Content-Transfer-Encoding: binary");
- echo $report_excel_data;
- }
- }
- }
- }
- else{
- echo 'OK';
- }
- exit;
- }
- else
- {
- $this->IncludeComponentTemplate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement