import React from "react"; import Head from 'next/head'; import Image from 'next/image'; import { connect } from "react-redux"; import { withRouter } from 'next/router'; import { SpinnerCircular } from 'spinners-react'; import moment from "moment"; import { reduxWrapper } from '../../store'; import Header from '../components/Header'; import Footer from '../components/Footer'; import InnerMenu from "./components/InnerMenu"; import Company from "../components/Company"; import DateInput from '../components/DatePicker'; import { getContractsList, getReconciliationFile } from '../../actions'; import { eachSeries } from "async"; class ReconciliationsPage extends React.Component { constructor(props) { super(props); this.state = { loading: false, contracts: null, checked: [], checked_all: true, valid_date_start: null, valid_date_end: null, period_date_start: null, period_date_end: null, reconciliation_requested: false, } } static getDerivedStateFromProps(nextProps, prevState) { return { contracts: nextProps.contracts, }; } componentDidMount() { this.setState({ loading: true }, () => { getContractsList({ dispatch: this.props.dispatch, all: true }).then(() => { this.setState({ loading: false }); }).catch(() => {}); }); } componentDidUpdate(prevProps, prevState) { if(prevState.contracts === null && this.state.contracts !== null) { const ds = this.state.contracts.length > 0 ? moment(this.state.contracts[ this.state.contracts.length-1 ].date).toDate() : moment().toDate(); const de = moment().toDate(); this.setState({ period_date_start: ds, period_date_end: de, valid_date_start: ds, valid_date_end: de, }); } } _handle_onAllContracts = () => { this.setState({ checked_all: this.state.checked_all ? false : true, checked: this.state.checked ? [] : this.state.checked }); } _handle_onContract = (number) => { const { checked_all, contracts } = this.state; const checked = [ ...this.state.checked ]; if(checked_all) { for(let i in contracts) { if(contracts[i].number !== number) { checked.push(contracts[i].number); } } this.setState({ checked_all: false, checked: checked }); } else { if(checked.indexOf(number) < 0) { checked.push(number); } else { checked.splice(checked.indexOf(number), 1); } this.setState({ checked: checked }); } } _handle_onPeriodDate_start = (date) => { const { valid_date_start, valid_date_end, period_date_start, period_date_end } = this.state; const md = moment(date, "DD.MM.YYYY"); if(md.isValid()) { console.log(md); console.log("vs", valid_date_start); console.log("ve", valid_date_end); if(date >= valid_date_start && date < valid_date_end) { console.log("ps", period_date_start); if(date < period_date_end) { console.log(">>>>>"); this.setState({ period_date_start: date, reconciliation_disabled: false }); } else { this.setState({ reconciliation_disabled: true }); } } else { this.setState({ reconciliation_disabled: true }); } } else { console.log("invalid date", md); this.setState({ reconciliation_disabled: true }); } } _handle_onPeriodDate_end = (date) => { const { valid_date_start, valid_date_end, period_date_start, period_date_end } = this.state; if(moment(date).isValid()) { if(date >= valid_date_start && date < valid_date_end) { if(date > period_date_start) { this.setState({ period_date_end: date, reconciliation_disabled: false }); } else { this.setState({ reconciliation_disabled: true }); } } else { this.setState({ reconciliation_disabled: true }); } } else { this.setState({ reconciliation_disabled: true }); } } _handle_onReconciliationFiles = () => { const { checked, checked_all, contracts, reconciliation_requested, period_date_start, period_date_end, } = this.state; let list = []; if(!reconciliation_requested) { if(checked_all) { for(let i in contracts) { list.push(contracts[i].number); } } else { list = checked; } console.log(list); const date_from = moment(period_date_start).format("YYYY-MM-DD"); const date_to = moment(period_date_end).format("YYYY-MM-DD"); this.setState({ reconciliation_requested: true }, () => { eachSeries(list, (number, callback) => { getReconciliationFile({ contract: number, date_from: date_from, date_to: date_to, filename: `${ number }_reconciliation_${ date_from }_${ date_to }.pdf`, }) .then(() => { callback(); }) .catch(() => { callback(); }); }, () => { this.setState({ reconciliation_requested: false }); }); }); } } render() { const { loading, contracts, checked, checked_all, valid_date_start, valid_date_end, period_date_start, period_date_end, reconciliation_disabled, reconciliation_requested } = this.state; return ( ЛК Эволюция автолизинга

Акты сверок

{ loading ? (
) : ( <> { contracts !== undefined && contracts !== null && contracts.length > 0 ? ( <>

{ contracts !== undefined && contracts !== null && contracts.map((contract, index) => (

-1 } name={ `contract_${ contract.number }` } id={ `contract_${ contract.number }` } onChange={ () => this._handle_onContract(contract.number) }/>

)) }
{/**/}
) : (

Нет договоров для запроса актов сверок

) } ) }