93 lines
3.5 KiB
TypeScript
93 lines
3.5 KiB
TypeScript
import { askUser } from '../../core/ui';
|
||
import { getEmailConfig } from '../../core/mail/emailConfig';
|
||
import { pickRoleEmail, sendMail } from '../../core/mail/notify';
|
||
import { exportSheetToPdf } from '../../core/sheets/export';
|
||
import { getColumnIndexesByName } from '../../core/sheets/reportLifecycle';
|
||
import { MetricsStorage } from '../../core/metrics/storage';
|
||
|
||
function validateBlocks() {
|
||
const sheet = SpreadsheetApp.getActiveSheet();
|
||
const MARKER = 'Оценка руководителя филиала (1-5):';
|
||
const [executorCol, filialHeadCol] = getColumnIndexesByName(1, ['Исполнитель', 'Руководитель филиала']);
|
||
|
||
if (executorCol === -1 || filialHeadCol === -1) {
|
||
return {
|
||
valid: false,
|
||
errors: 'Не найдены столбцы "Исполнитель" или "Руководитель филиала"'
|
||
};
|
||
}
|
||
|
||
const lastRow = sheet.getLastRow();
|
||
const columnB = sheet.getRange(1, 2, lastRow, 1).getValues();
|
||
const errors: string[] = [];
|
||
const metricsStorage = new MetricsStorage();
|
||
const now = new Date();
|
||
|
||
for (let row = 1; row <= lastRow; row++) {
|
||
if (columnB[row - 1][0] !== MARKER) continue;
|
||
|
||
const percentRow = row - 4;
|
||
const scoreRow = row;
|
||
const commentRow = row + 1;
|
||
const taskRow = row + 2;
|
||
const infoRow = percentRow - 1;
|
||
|
||
const executor = sheet.getRange(infoRow, executorCol).getDisplayValue().trim();
|
||
const filialHead = sheet.getRange(infoRow, filialHeadCol).getDisplayValue().trim();
|
||
const personInfo = `"${filialHead}","${executor}"`;
|
||
const percentValue = sheet.getRange(percentRow, 3).getDisplayValue().trim();
|
||
const scoreValue = sheet.getRange(scoreRow, 3).getDisplayValue().trim();
|
||
const commentValue = sheet.getRange(commentRow, 3).getDisplayValue().trim();
|
||
const taskValue = sheet.getRange(taskRow, 3).getDisplayValue().trim();
|
||
|
||
if (!scoreValue) {
|
||
errors.push(`${personInfo}: не заполнена оценка `);
|
||
continue;
|
||
}
|
||
|
||
const match = percentValue.match(/^(\d+)/);
|
||
const percent = match ? Number(match[1]) : 0;
|
||
const score = Number(scoreValue);
|
||
metricsStorage.updateMetric(now, executor, 'efficiency', score);
|
||
|
||
if (percent < 50 || score < 4) {
|
||
if (!commentValue) errors.push(`${personInfo}: не заполнен комментарий `);
|
||
if (!taskValue) errors.push(`${personInfo}: не заполнен ID задачи руководителя `);
|
||
}
|
||
}
|
||
|
||
return {
|
||
valid: errors.length === 0,
|
||
errors: errors.join('\n')
|
||
};
|
||
}
|
||
|
||
export function notifySalesDepartmentHead(): void {
|
||
const confirmed = askUser('Подтвеждаете отправку?');
|
||
if (!confirmed) return;
|
||
|
||
const validationResult = validateBlocks();
|
||
if (!validationResult.valid) {
|
||
SpreadsheetApp.getUi().alert(validationResult.errors);
|
||
return;
|
||
}
|
||
|
||
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
|
||
const sheet = spreadsheet.getActiveSheet();
|
||
const emailConfig = getEmailConfig(spreadsheet);
|
||
const mainEmail = pickRoleEmail(
|
||
emailConfig,
|
||
'salesDepartmentHeadEmail',
|
||
'salesDepartmentDeputyEmail'
|
||
);
|
||
|
||
if (!mainEmail) {
|
||
SpreadsheetApp.getUi().alert('Не заполнены email РОПа или его заместителя');
|
||
return;
|
||
}
|
||
|
||
const pdfBlob = exportSheetToPdf(spreadsheet.getId(), sheet.getSheetId(), sheet.getName());
|
||
const spreadsheetUrl = spreadsheet.getUrl() + '#gid=' + sheet.getSheetId();
|
||
sendMail(mainEmail, 'Отчет по завершенным задачам', spreadsheetUrl, pdfBlob);
|
||
}
|