AURORA-1916: Справочники: импорт ГК и ориганизаций из выгрузки ЕЦП #217
@ -1,3 +1,4 @@
|
||||
// Коды прав, от которых зависит интерфейс (как в `GET /me` → `permissions`). Доступ на самом деле решает сервер:
|
||||
// проверки на клиенте только прячут то, что пользователю всё равно не разрешат.
|
||||
export const USERS_ADMIN = 'USERS_ADMIN';
|
||||
export const DIRECTORY_ADMIN = 'DIRECTORY_ADMIN';
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
import { PlusOutlined } from '@ant-design/icons';
|
||||
import { PlusOutlined, UploadOutlined } from '@ant-design/icons';
|
||||
import { Button, Empty, Space } from 'antd';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { ApiError } from '../api/client';
|
||||
import { useCurrentUser } from '../auth/MeContext';
|
||||
import { DIRECTORY_ADMIN } from '../auth/permissions';
|
||||
import PageTitle from '../components/PageTitle';
|
||||
import { createCatalogueJur } from '../services/api/directories/catalogue_jur';
|
||||
import {
|
||||
@ -30,6 +32,7 @@ import {
|
||||
} from '../widgets/CatalogueCompanyGroup/CatalogueCompanyGroupTabs/CatalogueCompanyGroupTabs';
|
||||
import { CompanyGroupForm } from '../widgets/CatalogueCompanyGroup/CompanyGroupForm/CompanyGroupForm';
|
||||
import { CompanyGroupsList } from '../widgets/CatalogueCompanyGroup/CompanyGroupsList/CompanyGroupsList';
|
||||
import { ImportCompanyGroupsModal } from '../widgets/CatalogueCompanyGroup/ImportCompanyGroupsModal/ImportCompanyGroupsModal';
|
||||
import { CatalogueJurForm } from '../widgets/CatalogueJur/CatalogueJurForm/CatalogueJurForm';
|
||||
import { CatalogueJurTable } from '../widgets/CatalogueJur/CatalogueJurTable/CatalogueJurTable';
|
||||
import { CreateEntityPickerModal } from '../widgets/CreateEntityPickerModal/CreateEntityPickerModal';
|
||||
@ -39,10 +42,12 @@ import { RelatedGroupTable } from '../widgets/RelatedGroup/RelatedGroupTable/Rel
|
||||
export function CatalogueGroup() {
|
||||
const dispatch = useAppDispatch();
|
||||
const notification = useNotificationApi();
|
||||
const me = useCurrentUser();
|
||||
|
||||
const isGettingArchive = useAppSelector((state) => state.catalogue_jur.isGettingArchive);
|
||||
|
||||
const [entityCreateModalOpen, setEntityCreateModalOpen] = useState(false);
|
||||
const [importModalOpen, setImportModalOpen] = useState(false);
|
||||
const [kindForm, setKindForm] = useState<CreateEntityKind | null>(null);
|
||||
const [relatedGroupLoading, setRelatedGroupLoading] = useState(false);
|
||||
const [companyGroupEditFormOpen, setCompanyGroupEditFormOpen] = useState(false);
|
||||
@ -93,6 +98,18 @@ export function CatalogueGroup() {
|
||||
}
|
||||
}
|
||||
|
||||
function importedHandler() {
|
||||
dispatch(getCompanyGroupsThunk());
|
||||
dispatch(
|
||||
getCatalogueJurThunk({
|
||||
closed: isGettingArchive,
|
||||
outside_group: false,
|
||||
company_group_id: activeCompanyGroup?.id,
|
||||
}),
|
||||
);
|
||||
getRelatedGroupsHandler();
|
||||
}
|
||||
|
||||
async function archiveCompanyGroupHandler() {
|
||||
if (!activeCompanyGroup) {
|
||||
return;
|
||||
@ -168,9 +185,16 @@ export function CatalogueGroup() {
|
||||
}}>
|
||||
<Space style={{ justifyContent: 'space-between' }}>
|
||||
<PageTitle header='Группы компаний и организаций' location='Справочники' />
|
||||
<Button type='primary' icon={<PlusOutlined />} onClick={() => setEntityCreateModalOpen(true)}>
|
||||
Создать ГК/ЮЛ
|
||||
</Button>
|
||||
<Space>
|
||||
{me.permissions.includes(DIRECTORY_ADMIN) && (
|
||||
<Button icon={<UploadOutlined />} onClick={() => setImportModalOpen(true)}>
|
||||
Импорт из файла
|
||||
</Button>
|
||||
)}
|
||||
<Button type='primary' icon={<PlusOutlined />} onClick={() => setEntityCreateModalOpen(true)}>
|
||||
Создать ГК/ЮЛ
|
||||
</Button>
|
||||
</Space>
|
||||
</Space>
|
||||
<div
|
||||
style={{
|
||||
@ -231,6 +255,9 @@ export function CatalogueGroup() {
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{importModalOpen && (
|
||||
<ImportCompanyGroupsModal open={importModalOpen} onCancel={() => setImportModalOpen(false)} onImported={importedHandler} />
|
||||
)}
|
||||
<CreateEntityPickerModal
|
||||
open={entityCreateModalOpen}
|
||||
onCancel={() => {
|
||||
|
||||
10
web/src/services/api/directories/directory_import.ts
Normal file
10
web/src/services/api/directories/directory_import.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { apiFetch } from '../../../api/client';
|
||||
import type { ImportReportOut, OnExisting } from '../../../types/api';
|
||||
|
||||
export function importDirectory(file: File, dryRun: boolean, onExisting: OnExisting): Promise<ImportReportOut> {
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
form.append('dry_run', String(dryRun));
|
||||
form.append('on_existing', onExisting);
|
||||
return apiFetch<ImportReportOut>('/directories/import', { method: 'POST', body: form });
|
||||
}
|
||||
@ -30,6 +30,27 @@ export interface UploadResult {
|
||||
rejected: RejectedOut[];
|
||||
}
|
||||
|
||||
export type OnExisting = 'skip' | 'update';
|
||||
export interface ImportReportLine {
|
||||
row: number;
|
||||
inn: string;
|
||||
code: string;
|
||||
message: string;
|
||||
}
|
||||
export interface ImportReportOut {
|
||||
rows: number;
|
||||
groups_created: number;
|
||||
groups_updated: number;
|
||||
groups_skipped: number;
|
||||
entities_created: number;
|
||||
entities_updated: number;
|
||||
entities_skipped: number;
|
||||
memberships_created: number;
|
||||
offices_created: number;
|
||||
errors: number;
|
||||
lines: ImportReportLine[];
|
||||
}
|
||||
|
||||
/** Статусы строки формы (v5.19): ok — строка бланка; unreadable — код не распознан; missing — кода нет; detail — расшифровка. */
|
||||
export type LineStatus = 'ok' | 'unreadable' | 'missing' | 'detail';
|
||||
export type Severity = 'error' | 'info';
|
||||
|
||||
@ -0,0 +1,114 @@
|
||||
import { FileAddOutlined } from '@ant-design/icons';
|
||||
import { Button, Checkbox, Form, Select, Upload } from 'antd';
|
||||
import type { UploadFile } from 'antd/es/upload/interface';
|
||||
import type React from 'react';
|
||||
import { useState } from 'react';
|
||||
import { ApiError } from '../../../api/client';
|
||||
import { AppModal } from '../../../components/AppModal/AppModal';
|
||||
import { importDirectory } from '../../../services/api/directories/directory_import';
|
||||
import type { OnExisting } from '../../../types/api';
|
||||
import { useNotificationApi } from '../../../utils/useNotification';
|
||||
|
||||
type Props = {
|
||||
open: boolean;
|
||||
onCancel: () => void;
|
||||
onImported: () => void;
|
||||
};
|
||||
|
||||
type FormValues = {
|
||||
on_existing: OnExisting;
|
||||
dry_run: boolean;
|
||||
};
|
||||
|
||||
const ON_EXISTING_OPTIONS: { value: OnExisting; label: string }[] = [
|
||||
{ value: 'skip', label: 'Пропустить существующие' },
|
||||
{ value: 'update', label: 'Обновить существующие' },
|
||||
];
|
||||
|
||||
/**
|
||||
* Импорт выгрузки ЕЦП CRM. Сценарий двухшаговый: после пробного прогона форма и файл остаются, чтобы снять галочку
|
||||
* и записать тем же файлом; после записи — уведомление и закрытие окна.
|
||||
*/
|
||||
export const ImportCompanyGroupsModal: React.FC<Props> = ({ open, onCancel, onImported }) => {
|
||||
const [form] = Form.useForm<FormValues>();
|
||||
const notification = useNotificationApi();
|
||||
|
||||
const [fileList, setFileList] = useState<UploadFile[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleOk = async () => {
|
||||
const file = fileList[0]?.originFileObj;
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
const values = form.getFieldsValue();
|
||||
setLoading(true);
|
||||
try {
|
||||
await importDirectory(file, values.dry_run, values.on_existing);
|
||||
if (values.dry_run) {
|
||||
notification.info({ description: 'Пробный прогон завершён: в справочник ничего не записано' });
|
||||
return;
|
||||
}
|
||||
notification.success({ description: 'Импорт завершён' });
|
||||
onImported();
|
||||
onCancel();
|
||||
} catch (e) {
|
||||
notification.error({ description: e instanceof ApiError ? e.message : 'Не удалось выполнить импорт' });
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
if (!loading) {
|
||||
onCancel();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<AppModal
|
||||
title='Импорт групп компаний из файла'
|
||||
open={open}
|
||||
onCancel={handleCancel}
|
||||
onOk={handleOk}
|
||||
okText='Загрузить'
|
||||
okButtonProps={{ disabled: fileList.length === 0 }}
|
||||
cancelText='Отмена'
|
||||
cancelButtonProps={{ disabled: loading }}
|
||||
confirmLoading={loading}
|
||||
closable={!loading}
|
||||
destroyOnHidden
|
||||
width={720}>
|
||||
<Form form={form} layout='vertical' disabled={loading} initialValues={{ on_existing: 'skip', dry_run: true }}>
|
||||
<Form.Item name='on_existing' label='При дублировании' style={{ marginBottom: 12 }}>
|
||||
<Select options={ON_EXISTING_OPTIONS} />
|
||||
</Form.Item>
|
||||
<Form.Item name='dry_run' valuePropName='checked' style={{ marginBottom: 12 }}>
|
||||
<Checkbox>Пробный прогон</Checkbox>
|
||||
</Form.Item>
|
||||
<Form.Item label='Файл' required style={{ marginBottom: 0 }}>
|
||||
<Upload.Dragger
|
||||
accept='.xlsx,.csv'
|
||||
maxCount={1}
|
||||
fileList={fileList}
|
||||
beforeUpload={(file) => {
|
||||
setFileList([{ uid: file.uid, name: file.name, originFileObj: file }]);
|
||||
return false;
|
||||
}}
|
||||
onRemove={() => {
|
||||
setFileList([]);
|
||||
}}>
|
||||
<p className='ant-upload-drag-icon'>
|
||||
<FileAddOutlined />
|
||||
</p>
|
||||
<p className='ant-upload-text'>Перетащите файл сюда</p>
|
||||
<Button>Выбрать файл</Button>
|
||||
<p className='ant-upload-hint' style={{ marginTop: 12 }}>
|
||||
Формат: .xlsx, .csv
|
||||
</p>
|
||||
</Upload.Dragger>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</AppModal>
|
||||
);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user