useNewReport = \Bitrix\Main\Config\Option::get('itach.report.setting', 'use_new_invoice', 'N') === 'Y'; $this->pay_date = \Bitrix\Main\Config\Option::get('itach.report.setting', 'pay_date'); $this->type_invoice = \Bitrix\Main\Config\Option::get('itach.report.setting', 'type_invoice') ; $this->city = \Bitrix\Main\Config\Option::get('itach.report.setting', 'city'); $this->comments = \Bitrix\Main\Config\Option::get('itach.report.setting', 'comments'); if ($this->useNewReport) { $this->getResultByNeInvoice(); } else { $this->getResult(); } $this->includeComponentTemplate(); } catch (SystemException $e) { ShowError($e->getMessage()); } } private function getResultByNeInvoice() { $this->arResult['RESULT'] = []; $this->getParamsUrl(); $this->getCityes(); $this->getMacroRegion(); if ($this->arResult['PARAMS']['status'] != 2) { $this->getNewInvoice(); } if ($this->arResult['PARAMS']['status'] != 1) { $this->getСonsumption(); } $this->getContacts(); $this->getUsers(); usort($this->arResult['RESULT'], $this->sort_array('SORT')); } private function getResult() { $this->arResult['RESULT'] = []; $this->getParamsUrl(); $this->getCityes(); $this->getMacroRegion(); $this->getTypeInvoice(); if ($this->arResult['PARAMS']['status'] != 2) { $this->getInvoice(); } if ($this->arResult['PARAMS']['status'] != 1) { $this->getСonsumption(); } $this->getContacts(); $this->getUsers(); usort($this->arResult['RESULT'], $this->sort_array('SORT')); } private function getParamsUrl() { $this->arResult['PARAMS'] = json_decode($_REQUEST['PLACEMENT_OPTIONS'], 1); } private function getMacroRegion() { $types = []; if (!empty($this->arResult['CITYES'])) { foreach ($this->arResult['CITYES'] as $city) { if (!empty($city['PROPERTY_MAKROREGION_VALUE'])) $types[$city['PROPERTY_MAKROREGION_ENUM_ID']] = $city['PROPERTY_MAKROREGION_VALUE']; } } $this->arResult['MACROREGION'] = $types; } public function getTypeNewInvoices(): array { $typesInvoice[] = [ 'code' => 0, 'name' => 'Все' ]; // TODO: Переделать под настройки в модуле $FIELD_NAME = $this->type_invoice; $ENTITY_ID = 'CRM_SMART_INVOICE'; $rsData = \CUserTypeEntity::GetList([], [ 'FIELD_NAME' => $FIELD_NAME, 'ENTITY_ID' => $ENTITY_ID ]); $typeInvoiceEntity = $rsData->Fetch(); if (!empty($typeInvoiceEntity)) { $obEnum = new \CUserFieldEnum(); $obList = $obEnum->GetList([], [ 'USER_FIELD_ID' => $typeInvoiceEntity['ID'], ]); while ($typeInvoiceEntityEnum = $obList->Fetch()) { $typesInvoice[$typeInvoiceEntityEnum['ID']] = [ 'code' => $typeInvoiceEntityEnum['ID'], 'name' => $typeInvoiceEntityEnum['VALUE'] ]; } } $this->arResult['TYPE_INVOICE'] = array_column($typesInvoice, 'name', 'code'); return $typesInvoice; } private function getTypeInvoice() { $typesInvoice = []; $rsData = \CUserTypeEntity::GetList([], [ 'FIELD_NAME' => 'UF_CRM_5F34F4F0AC84B', 'ENTITY_ID' => 'CRM_INVOICE' ]); $typeInvoiceEntity = $rsData->Fetch(); if (!empty($typeInvoiceEntity)) { $obEnum = new \CUserFieldEnum(); $obList = $obEnum->GetList([], [ 'USER_FIELD_ID' => $typeInvoiceEntity['ID'], ]); while ($typeInvoiceEntityEnum = $obList->Fetch()) { $typesInvoice[$typeInvoiceEntityEnum['ID']] = $typeInvoiceEntityEnum['VALUE']; } } $this->arResult['TYPE_INVOICE'] = $typesInvoice; } /**Список городов * @return [array] */ private function getCityes() { global $USER; $arResult = []; $arSelect = ["ID", "IBLOCK_ID", "NAME", "PROPERTY_MAKROREGION"]; $arFilter = ["IBLOCK_ID" => 54, 'PROPERTY_SOTRUDNIKI' => $USER->GetID()]; $res = \CIBlockElement::GetList(['NAME' => 'ASC'], $arFilter, false, false, $arSelect); while ($ob = $res->GetNextElement()) { $arFields = $ob->GetFields(); $arResult[$arFields['ID']] = $arFields; } $this->arResult['CITYES'] = $arResult; } private function getContacts() { if ($this->arResult['CONTACTS']) { $ids = array_keys($this->arResult['CONTACTS']); $arFilter = [ 'CHECK_PERMISSIONS' => 'N', 'ID' => $ids ]; $resContacts = \CCrmContact::GetListEx([], $arFilter, false, false, ['ID', 'FULL_NAME']); while ($arContacts = $resContacts->GetNext()) { $this->arResult['CONTACTS'][$arContacts['ID']] = $arContacts; } } if ( $this->arResult['COMPANY'] ) { $idsComp = array_keys($this->arResult['COMPANY']); $arFilter = [ 'CHECK_PERMISSIONS' => 'N', 'ID' => $idsComp ]; $resCompany = \CCrmCompany::GetListEx([], $arFilter, false, false, ['ID', 'TITLE']); while ($arCompany = $resCompany->GetNext()) { $this->arResult['COMPANY'][$arCompany['ID']] = $arCompany; } } } private function getUsers() { if (!empty($this->arResult['USERS'])) { $this->arResult['USERS'] = is_array($this->arResult['USERS']) ? $this->arResult['USERS'] : [$this->arResult['USERS']]; } else $this->arResult['USERS'] = []; $filter = ['ID' => implode('|', $this->arResult['USERS'])]; $rsUsers = \CUser::GetList(($by = "NAME"), ($order = "desc"), $filter); while ($arUser = $rsUsers->Fetch()) { $this->arResult['USERS'][$arUser['ID']] = $arUser['NAME'] . ' ' . $arUser['LAST_NAME']; } } /** * Расходы из списка * @return [type] */ private function getСonsumption() { $arSelect = ["ID", "NAME", 'PROPERTY_1986', 'PROPERTY_1984', 'PROPERTY_1985', 'PROPERTY_1987', 'CREATED_BY', 'PROPERTY_1992', 'PROPERTY_1991']; $arFilter["IBLOCK_ID"] = 230; $arFilter['STATUS_ID'] = 'P'; $arFilter['PROPERTY_1983'] = 356; if ($this->arResult['PARAMS']['date_from']) { $arFilter['>=PROPERTY_1985'] = date('Y-m-d', strtotime($this->arResult['PARAMS']['date_from'])); } if ($this->arResult['PARAMS']['date_to']) { $arFilter['<=PROPERTY_1985'] = date('Y-m-d', strtotime($this->arResult['PARAMS']['date_to'])); } if ($this->arResult['PARAMS']['city']) { $arFilter['PROPERTY_1984'] = explode(',', $this->arResult['PARAMS']['city']); } else { if ($this->arResult['PARAMS']['macroregion']) { $cities = array_filter($this->arResult['CITYES'], function ($city) { return (int)$city['PROPERTY_MAKROREGION_ENUM_ID'] === (int)$this->arResult['PARAMS']['macroregion']; }); $arFilter['PROPERTY_1984'] = array_keys($cities); } else { $arFilter['PROPERTY_1984'] = array_keys($this->arResult['CITYES']); } } if ($this->arResult['PARAMS']['assigned']) { $arFilter['CREATED_BY'] = $this->arResult['PARAMS']['assigned']; } $res = \CIBlockElement::GetList([], $arFilter, false, false, $arSelect); while ($ob = $res->GetNextElement()) { $arFields = $ob->GetFields(); // Исключаем элементы, у которых PROPERTY_1991 = 242 if ($arFields['PROPERTY_1991_VALUE'] == "Снятие кассы") { continue; } $this->arResult['USERS'][$arFields['PROPERTY_1986_VALUE']] = $arFields['PROPERTY_1986_VALUE']; $this->arResult['USERS'][$arFields['CREATED_BY']] = $arFields['CREATED_BY']; $arFields['CITY'] = $arFields['PROPERTY_1984_VALUE']; $arFields['PRICE'] = $arFields['PROPERTY_1987_VALUE']; $arFields['SORT'] = strtotime($arFields['PROPERTY_1985_VALUE']); $arFields['RESPONSIBLE_ID'] = $arFields['CREATED_BY']; $arFields['COMMENT_RASHOD'] = $arFields['PROPERTY_1992']; $arFields['METHOD_PAY'] = $this->arResult['PARAMS']['method_pay']; $this->arResult['RESULT'][] = $arFields; } } private function getNewInvoice($arFilterInvoices = []) { $typeInvoices = $this->getTypeNewInvoices(); $arSelect = ['*']; $arFilterInvoices['STAGE_ID'] = 'DT31_1:P'; if ($this->arResult['PARAMS']['date_from']) { $arFilterInvoices['>=' . $this->pay_date] = date('d.m.Y 00:00:00', strtotime($this->arResult['PARAMS']['date_from'])); } if ($this->arResult['PARAMS']['date_to']) { $arFilterInvoices['<=' . $this->pay_date] = date('d.m.Y 23:59:59', strtotime($this->arResult['PARAMS']['date_to'])); } if ($this->arResult['PARAMS']['type_invoice']) { $arFilterInvoices[$this->type_invoice] = $this->arResult['PARAMS']['type_invoice']; } if ($this->arResult['PARAMS']['city']) { $arFilterInvoices[$this->city] = explode(',', $this->arResult['PARAMS']['city']); } else { if ($this->arResult['PARAMS']['macroregion']) { $cities = array_filter($this->arResult['CITYES'], function ($city) { return (int)$city['PROPERTY_MAKROREGION_ENUM_ID'] === (int)$this->arResult['PARAMS']['macroregion']; }); $arFilterInvoices[$this->city] = array_keys($cities); } else $arFilterInvoices[$this->city] = array_keys($this->arResult['CITYES']); } if ($this->arResult['PARAMS']['assigned']) { $arFilterInvoices['ASSIGNED_BY_ID'] = $this->arResult['PARAMS']['assigned']; } if ($this->arResult['PARAMS']['payment_method']) { if ($this->arResult['PARAMS']['payment_method'] == 1) { $arFilterInvoices['UF_CRM_SMART_INVOICE_1725356584'] = '2724'; } if ($this->arResult['PARAMS']['payment_method'] == 2) { $arFilterInvoices['UF_CRM_SMART_INVOICE_1725356584'] = '2725'; } } $arFilterInvoices['UF_CRM_SMART_INVOICE_1723538355'] = '4058'; $factory = Container::getInstance()->getFactory(31); $employees = $factory->getItems([ 'filter' => $arFilterInvoices, 'select' => $arSelect, 'order' => [] ]); foreach ($employees as $employee) { $result = [ 'PAY_VOUCHER_DATE' => (new DateTime($employee[$this->pay_date]))->format('d.m.Y'), 'SORT' => (new DateTime($employee[$this->pay_date]))->getTimestamp(), 'UF_CRM_5F34F4F0AC84B' => $typeInvoices[$employee[$this->type_invoice]]['name'], 'PRICE' => $employee['OPPORTUNITY'], 'ID' => $employee['ID'], 'ACCOUNT_NUMBER' => $employee['ACCOUNT_NUMBER'], 'UF_CRM_5F4E400A48183' => $employee[$this->city], 'CITY' => $employee[$this->city], 'STATUS_ID' => null, 'NEW_INVOICE' => true, 'RESPONSIBLE_ID' => $employee['ASSIGNED_BY_ID'], 'UF_CONTACT_ID' => $employee['CONTACT_ID'], 'COMPANY' => $employee['COMPANY_ID'], 'PROPERTY_1992_VALUE' => $employee[$this->comments], 'PAYMAN_METHOD' => $employee['UF_CRM_SMART_INVOICE_1725356584'], 'PARENT_DEAL' => $employee['PARENT_ID_2'] ]; $this->arResult['USERS'][$employee['ASSIGNED_BY_ID']] = $employee['ASSIGNED_BY_ID']; $this->arResult['CONTACTS'][$employee['CONTACT_ID']] = $employee['CONTACT_ID']; $this->arResult['COMPANY'][$employee['COMPANY_ID']] = $employee['COMPANY_ID']; $this->arResult['RESULT'][] = $result; } } private function getInvoice($arFilterInvoices = []) { $arSelect = ["PAY_VOUCHER_DATE", 'UF_CRM_5F34F4F0AC84B', "PRICE", "ID", "ACCOUNT_NUMBER", 'UF_DEAL_ID', 'COMMENTS', 'UF_CRM_5F4E400A48183', 'STATUS_ID', 'RESPONSIBLE_ID', 'UF_CONTACT_ID']; $arFilterInvoices['STATUS_ID'] = 'P'; if ($this->arResult['PARAMS']['date_from']) { $arFilterInvoices['>=PAY_VOUCHER_DATE'] = date('d.m.Y 00:00:00', strtotime($this->arResult['PARAMS']['date_from'])); } if ($this->arResult['PARAMS']['date_to']) { $arFilterInvoices['<=PAY_VOUCHER_DATE'] = date('d.m.Y 23:59:59', strtotime($this->arResult['PARAMS']['date_to'])); } if ($this->arResult['PARAMS']['type_invoice']) { $arFilterInvoices['UF_CRM_5F34F4F0AC84B'] = $this->arResult['PARAMS']['type_invoice']; } if ($this->arResult['PARAMS']['city']) { $arFilterInvoices['UF_CRM_5F4E400A48183'] = explode(',', $this->arResult['PARAMS']['city']); } else { if ($this->arResult['PARAMS']['macroregion']) { $cities = array_filter($this->arResult['CITYES'], function ($city) { return (int)$city['PROPERTY_MAKROREGION_ENUM_ID'] === (int)$this->arResult['PARAMS']['macroregion']; }); $arFilterInvoices['UF_CRM_5F4E400A48183'] = array_keys($cities); } else $arFilterInvoices['UF_CRM_5F4E400A48183'] = array_keys($this->arResult['CITYES']); } if ($this->arResult['PARAMS']['assigned']) { $arFilterInvoices['RESPONSIBLE_ID'] = $this->arResult['PARAMS']['assigned']; } // $arFilterInvoices['ID'] = 211241; $res = \CCrmInvoice::GetList([], $arFilterInvoices, false, false, $arSelect); while ($ob = $res->fetch()) { $ob['SORT'] = strtotime($ob["PAY_VOUCHER_DATE"]); $ob['CITY'] = $ob['UF_CRM_5F4E400A48183']; $ob['NEW_INVOICE'] = false; $ob['TYPE_INVOICE'] = $this->arResult['TYPE_INVOICE'][$ob['UF_CRM_5F34F4F0AC84B']]; $ob['METHOD_PAY'] = $methodPay; $this->arResult['USERS'][$ob['RESPONSIBLE_ID']] = $ob['RESPONSIBLE_ID']; $this->arResult['CONTACTS'][$ob['UF_CONTACT_ID']] = $ob['UF_CONTACT_ID']; $this->arResult['RESULT'][] = $ob; } } protected function sort_array($key) { return function ($a, $b) use ($key) { return $a[$key] <=> $b[$key]; }; } }