bitrix_task_control/src/departments/salesOriginal/overdue/getExecutorComments.ts

122 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { EXECUTOR_SHEET_NAMES } from '../../../core/constants';
import { loadConfig } from '../../../core/mail/executorTables';
import { loadTaskResultsById } from '../../../core/bitrix/client';
import { stripBitrixMarkup } from '../../../reports/waitingControl/main';
import { formatDateWithTime } from '../../../core/dates';
function getTaskResult(taskId: string): any {
const results = loadTaskResultsById(taskId);
return results.slice()
.sort((a, b) => {
const dateA = a.createdAt ? new Date(a.createdAt).getTime() : 0;
const dateB = b.createdAt ? new Date(b.createdAt).getTime() : 0;
return dateA - dateB;
})
.map(r => formatDateWithTime(new Date(r.createdAt)) + ' ' + stripBitrixMarkup(r.text).trim())
.filter(Boolean)
.join('\n\n');
}
export function pullExecutorAnswers(): void {
const config = loadConfig();
const reportSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const reportData = reportSheet.getDataRange().getValues();
if (reportData.length < 2) {
return;
}
const reportHeaders = reportData[0];
const reportCol: Record<string, number> = {};
reportHeaders.forEach((header, index) => {
reportCol[String(header).trim()] = index;
});
const executorTasks: Record<string, Array<{ taskId: string; reportRow: number }>> = {};
for (let i = 1; i < reportData.length; i++) {
const row = reportData[i];
const executor = String(row[reportCol['Исполнитель']] || '').trim();
const taskId = String(row[reportCol['ID задачи']] || '').trim();
if (!executor || !taskId) continue;
if (!executorTasks[executor]) {
executorTasks[executor] = [];
}
executorTasks[executor].push({ taskId, reportRow: i + 1 });
}
const taskAnswers: Record<string, any> = {};
Object.keys(executorTasks).forEach(executorName => {
const spreadsheetId = config[executorName];
if (!spreadsheetId) return;
try {
const executorSpreadsheet = SpreadsheetApp.openById(spreadsheetId);
const sheet = executorSpreadsheet.getSheetByName(EXECUTOR_SHEET_NAMES.overdueQuestions);
if (!sheet) return;
const values = sheet.getDataRange().getValues();
if (values.length < 2) return;
const headers = values[0];
const col: Record<string, number> = {};
headers.forEach((header, index) => {
col[String(header).trim()] = index;
});
for (let r = 1; r < values.length; r++) {
const row = values[r];
const taskId = String(row[col['ID задачи']] || '').trim();
if (!taskId) continue;
taskAnswers[taskId] = {
comment: getTaskResult(taskId),
deadline: row[col['Новый крайний срок от менеджера']],
executorName,
executorSheetRow: r + 1
};
}
} catch (e: any) {
Logger.log(executorName + ': ' + e.message);
}
});
const missing: string[] = [];
for (let i = 1; i < reportData.length; i++) {
const row = reportData[i];
const taskId = String(row[reportCol['ID задачи']] || '').trim();
if (!taskId) continue;
const answer = taskAnswers[taskId];
if (!answer) {
missing.push('Строка ' + (i + 1) + ' задачи нет в листах менеджеров. Убедись что менеджеры получили список задач перед сбором комментариев');
continue;
}
const needReason = String(row[reportCol['Задача, требующая описания причины при переносе сроков']] || '')
.trim()
.toLowerCase() === 'да';
if (!answer.deadline) {
missing.push('Строка ' + answer.executorSheetRow + ' в таблице ' + answer.executorName + ' (нет даты)');
}
if (needReason) {
reportSheet.getRange(i + 1, reportCol['Текущая причина переноса'] + 1).setValue(answer.comment);
}
reportSheet.getRange(i + 1, reportCol['Новый крайний срок от менеджера'] + 1).setValue(answer.deadline);
}
SpreadsheetApp.getUi().alert(
missing.length
? 'Не заполнены данные по задачам:\n\n' + missing.join('\n')
: 'Все данные исполнителей успешно загружены.'
);
}