402 lines
12 KiB
JavaScript
402 lines
12 KiB
JavaScript
import React from "react";
|
||
import { connect } from "react-redux";
|
||
import moment from "moment";
|
||
import { SpinnerCircular } from "spinners-react";
|
||
import { concatSeries } from "async";
|
||
import { eachSeries } from 'async';
|
||
import Select from 'react-select';
|
||
import fileDownload from 'js-file-download';
|
||
|
||
|
||
import { createEDOProject, signCheckEDOCreatePrintForm, signCheckPowerAttorneyClient, signDownloadFile, signGetGUIDEntity, signGetPowerAttorneyClient, signGetWMDoc } from "../../actions";
|
||
//import { getDeals, getDealOffers, getDealDocuments, getDealContracts } from "../../actions";
|
||
|
||
class EDOSign extends React.Component
|
||
{
|
||
constructor(props)
|
||
{
|
||
super(props)
|
||
this.state = {
|
||
operators: null,
|
||
loading: false,
|
||
documents: [],
|
||
operator_selected: {},
|
||
statuses: {},
|
||
step: 0,
|
||
}
|
||
}
|
||
|
||
static getDerivedStateFromProps(nextProps, prevState)
|
||
{
|
||
console.log("EDOSign", "getDerivedStateFromProps", { nextProps });
|
||
return {
|
||
operators: nextProps.operators,
|
||
}
|
||
}
|
||
|
||
componentDidMount()
|
||
{
|
||
const docs = [];
|
||
for(let i in this.props.documents)
|
||
{
|
||
docs.push({ ...this.props.documents[i], ...{ index: i, loading: true, completed: false, attorney: false } });
|
||
}
|
||
this.setState({ documents: docs });
|
||
|
||
if(this.state.operator_selected.box_id === undefined)
|
||
{
|
||
this.setState({ operator_selected: this.state.operators !== null && this.state.operators[0] !== undefined ? this.state.operators[0] : {} });
|
||
}
|
||
}
|
||
|
||
_updateDocs = (id, update) =>
|
||
{
|
||
return new Promise((resolve) =>
|
||
{
|
||
const docs = [ ...this.state.documents ];
|
||
docs[ id ] = { ...docs[id], ...update };
|
||
|
||
this.setState({ documents: docs }, () =>
|
||
{
|
||
resolve();
|
||
});
|
||
});
|
||
}
|
||
|
||
_sign = () =>
|
||
{
|
||
//const { documents } = this.props;
|
||
const { operator_selected, step } = this.state;
|
||
const documents = [ ...this.state.documents ];
|
||
|
||
console.log("_handle_onFormSubmit");
|
||
console.log({ documents, operator_selected });
|
||
|
||
this.setState({ loading: true }, () =>
|
||
{
|
||
eachSeries(documents, async (document, callback) =>
|
||
{
|
||
if(document.completed)
|
||
{
|
||
callback();
|
||
}
|
||
else
|
||
{
|
||
console.log({ document })
|
||
await this._updateDocs(document.index, { loading: true });
|
||
|
||
if(document.attorney && document.attorney_selected !== undefined)
|
||
{
|
||
const create_edo_project_result = await createEDOProject({
|
||
contract_number: document.id,
|
||
power_attorney: document.attorney_selected.value,
|
||
power_attorney_number: document.attorney_selected.label,
|
||
edo_box: operator_selected.edo_box,
|
||
});
|
||
|
||
console.log("\n\nIMPORTANT !!!!!!!!!!\n\n");
|
||
console.log({ create_edo_project_result });
|
||
|
||
const get_guid_entity_result = await signGetGUIDEntity({
|
||
contract_number: document.id,
|
||
});
|
||
|
||
console.log("\n\nENTITY !!!!!!!!!!\n\n");
|
||
console.log({ get_guid_entity_result });
|
||
|
||
await this._updateDocs(document.index, {
|
||
entity_id: get_guid_entity_result.entityid,
|
||
completed: true,
|
||
loading: false,
|
||
attorney: false,
|
||
});
|
||
|
||
callback();
|
||
}
|
||
else
|
||
{
|
||
const create_print_form_result = await signCheckEDOCreatePrintForm({ contract_number: document.id === "2023_5885" ? "2023_5894" : document.id, sign_type: "EDO" });
|
||
console.log({ create_print_form_result });
|
||
|
||
const docs = { status: create_print_form_result.status };
|
||
|
||
if(create_print_form_result.status !== "success")
|
||
{
|
||
docs.loading = false;
|
||
docs.error = true;
|
||
docs.completed = true;
|
||
docs.message = create_print_form_result.message;
|
||
}
|
||
|
||
await this._updateDocs(document.index, docs);
|
||
|
||
if(create_print_form_result.status === "success")
|
||
{
|
||
const check_attorney_client_result = await signCheckPowerAttorneyClient({ contract_number: document.id })
|
||
console.log({ check_attorney_client_result });
|
||
|
||
await this._updateDocs(document.index, { attorney: check_attorney_client_result.power_attorney_required, });
|
||
|
||
if(check_attorney_client_result.power_attorney_required)
|
||
{
|
||
const get_attorney_client_result = await signGetPowerAttorneyClient({ contract_number: document.id });
|
||
console.log({ get_attorney_client_result });
|
||
|
||
if(get_attorney_client_result === null)
|
||
{
|
||
await this._updateDocs(document.index, { loading: false, error: true, completed: true, message: "Возникла ошибка при создании документа для подписания." });
|
||
callback();
|
||
}
|
||
else
|
||
{
|
||
if(get_attorney_client_result.length === null)
|
||
{
|
||
await this._updateDocs(document.index, { loading: false, error: true, completed: true, message: "Не обнаружена доверенность на подписанта. Для продолжения подписания обратитесь к своему персональному менеджеру" });
|
||
callback();
|
||
}
|
||
else
|
||
{
|
||
const attorneys = [];
|
||
for(let i in get_attorney_client_result)
|
||
{
|
||
attorneys.push({
|
||
value: get_attorney_client_result[i].power_attorney,
|
||
label: get_attorney_client_result[i].power_attorney_number,
|
||
});
|
||
}
|
||
|
||
await this._updateDocs(document.index, { loading: false, error: false, attorneys, attorney_selected: undefined });
|
||
|
||
callback();
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
const create_edo_project_result = await createEDOProject({
|
||
contract_number: document.id,
|
||
edo_box: operator_selected.edo_box,
|
||
});
|
||
|
||
const get_guid_entity_result = await signGetGUIDEntity({
|
||
contract_number: document.id,
|
||
});
|
||
|
||
const wmdoc_result = await signGetWMDoc({
|
||
entity_name: "evo_contract",
|
||
entity_id: get_guid_entity_result.entityid,
|
||
sign_type: "EDO",
|
||
evo_id: "144",
|
||
});
|
||
console.log({ wmdoc_result });
|
||
|
||
await this._updateDocs(document.index, {
|
||
entity_id: get_guid_entity_result.entityid,
|
||
completed: true,
|
||
loading: false,
|
||
attorney: false,
|
||
});
|
||
|
||
callback();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
callback();
|
||
}
|
||
}
|
||
}
|
||
}, () =>
|
||
{
|
||
this.setState({ loading: false });
|
||
});
|
||
});
|
||
}
|
||
|
||
_handle_onFormSubmit = (event) =>
|
||
{
|
||
event.preventDefault();
|
||
|
||
//const { documents } = this.props;
|
||
const { operator_selected, step } = this.state;
|
||
const documents = [ ...this.state.documents ];
|
||
|
||
console.log("_handle_onFormSubmit");
|
||
console.log({ documents, operator_selected });
|
||
|
||
switch(step)
|
||
{
|
||
case 0:
|
||
{
|
||
this.setState({ step: 1 }, () =>
|
||
{
|
||
this._sign();
|
||
});
|
||
}
|
||
break;
|
||
|
||
case 1:
|
||
{
|
||
this._sign();
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
_handle_onSelectOperator = (operator) =>
|
||
{
|
||
this.setState({ operator_selected: operator });
|
||
}
|
||
|
||
_handle_onAttorneyChange = (index, attorney) =>
|
||
{
|
||
console.log("_handle_onAttorneyChange", { index, attorney });
|
||
this._updateDocs(index, { attorney_selected: attorney });
|
||
}
|
||
|
||
_handle_onDownloadFile = (index, entity_id, name) =>
|
||
{
|
||
console.log("_handle_onDownloadFile", { index, entity_id });
|
||
this.setState({ loading: true }, async () =>
|
||
{
|
||
await this._updateDocs(index, { loading: true });
|
||
|
||
const wmdoc_result = await signDownloadFile({ entity_name: "evo_contract", entity_id, sign_type: "EDO", evo_id: "144", });
|
||
console.log({ wmdoc_result });
|
||
fileDownload(wmdoc_result, `ЛК ЭВОЛЮЦИЯ ${ name }.pdf`);
|
||
|
||
await this._updateDocs(index, { loading: false });
|
||
});
|
||
}
|
||
|
||
_handle_onCancelFile = async (index, entity_id) =>
|
||
{
|
||
await this._updateDocs(index, { loading: true });
|
||
this.setState({ loading: true }, async () =>
|
||
{
|
||
await this._updateDocs(index, { loading: true });
|
||
});
|
||
}
|
||
|
||
render()
|
||
{
|
||
const { step, operators, documents, loading, operator_selected } = this.state;
|
||
const { onCancel, } = this.props;
|
||
console.log({ operators, documents });
|
||
|
||
return (
|
||
<div className="edo_detail">
|
||
<div className="docs_list medium-icon">
|
||
<p className="list_title">Подписание через ЭДО</p>
|
||
</div>
|
||
{ step === 0 && (
|
||
<div className="form_field edo_list_field">
|
||
<label>Выберите оператора для отправки пакета документов</label>
|
||
<div className="edo_list_selection">
|
||
{ operators !== undefined && operators !== null && operators.map((operator, index) => (
|
||
<div className="form_field checkbox item" key={ index }>
|
||
<input type="radio"
|
||
checked={ operator.box_id === operator_selected.box_id }
|
||
hidden=""
|
||
id={ `operator_${ index }` }
|
||
name={ `operator_${ index }` }
|
||
onChange={ (event) => this._handle_onSelectOperator(operator) }
|
||
disabled={ false }
|
||
/>
|
||
<label htmlFor={ `operator_${ index }` } className="unselectable">{ operator.provider_edo }</label>
|
||
</div>
|
||
)) }
|
||
</div>
|
||
</div>
|
||
) }
|
||
{ step === 1 && (
|
||
<div className="docs_list medium-icon edo_sign_documents_list">
|
||
{ documents.map((document, index) =>
|
||
(
|
||
<div className="edo_sign_documents_document" key={ index }>
|
||
<div className="edo_sign_documents_document_left">
|
||
<p className="doc_name i-pdf extension edo_sign_document" data-format={ document.extension }>
|
||
{ document.name } от { moment(document.date).format("DD.MM.YYYY") }
|
||
{ document.type !== undefined && (<span>{ document.type }</span>) }
|
||
</p>
|
||
</div>
|
||
<div className="edo_sign_documents_document_right">
|
||
{ document.loading ? (
|
||
<>
|
||
{ loading && (
|
||
<SpinnerCircular size={ 22 } thickness={ 100 } speed={ 100 } color="rgba(236, 239, 244, 1)" secondaryColor="rgba(28, 1, 169, 1)" />
|
||
) }
|
||
</>
|
||
) : (
|
||
<>
|
||
{ document.error ? (
|
||
<>
|
||
<div className="status_icon error"></div>
|
||
<i>{ document.message }</i>
|
||
</>
|
||
) : (
|
||
<>
|
||
{ document.attorney && (
|
||
<>
|
||
<div className="status_icon await"></div>
|
||
<div className="edo_sign_status_attorney_select">
|
||
<Select
|
||
id="edo_attorneys_list"
|
||
name="edo_attorneys_list"
|
||
options={ document.attorneys }
|
||
placeholder="Выберите доверенность"
|
||
noOptionsMessage={ ({ inputValue }) => !inputValue ? "" :"Ничего не найдено" }
|
||
isSearchable={ true }
|
||
className="autocomlete autocomlete_with_indicators"
|
||
classNamePrefix="react-select"
|
||
value={ document.attorney_selected }
|
||
onChange={ (element) => { this._handle_onAttorneyChange(document.index, element) } }
|
||
required={ true }
|
||
/>
|
||
</div>
|
||
</>
|
||
) }
|
||
{ document.completed && (
|
||
<>
|
||
<div className="status_icon success"></div>
|
||
{ document.entity_id !== undefined && (
|
||
<>
|
||
<button className="button" onClick={ () => this._handle_onDownloadFile(index, document.entity_id, document.name) }>Посмотреть проект договора</button>
|
||
<button className="button" onClick={ () => this._handle_onCancelFile(index, document.entity_id) }>Отменить подписание</button>
|
||
</>
|
||
) }
|
||
</>
|
||
) }
|
||
</>
|
||
) }
|
||
</>
|
||
) }
|
||
</div>
|
||
</div>
|
||
)) }
|
||
</div>
|
||
) }
|
||
<form ref={ this.ref_form } onSubmit={ this._handle_onFormSubmit } onKeyDown={(e) => {if (e.key === 'Enter') e.preventDefault() }}>
|
||
<div className="form_field" style={{ display: "flex", justifyContent: "space-between" }}>
|
||
<button className="button button-blue" onClick={ onCancel }>Отменить</button>
|
||
<button type="submit" className="button button-blue">
|
||
{ loading ? (
|
||
<SpinnerCircular size={24} thickness={100} speed={100} color="rgba(255, 255, 255, 1)" secondaryColor="rgba(255, 255, 255, 0.5)" style={{ marginTop: "4px" }}/>
|
||
) : "Продолжить" }
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
)
|
||
}
|
||
}
|
||
|
||
function mapStateToProps(state, ownProps)
|
||
{
|
||
console.log("EDOSign", "mapStateToProps", { state: state });
|
||
return {
|
||
operators: state.edo.operators,
|
||
}
|
||
}
|
||
|
||
export default connect(mapStateToProps)(EDOSign) |