diff --git a/actions/fileActions.js b/actions/fileActions.js index 5f04a8d..15ef1ab 100644 --- a/actions/fileActions.js +++ b/actions/fileActions.js @@ -188,23 +188,6 @@ export const getBitrixFile = ({ url, filename }) => reject(); }); - - /* - axios.get(`${ url }`, { - responseType: 'blob', - }) - .then((response) => - { - fileDownload(response.data, filename); - resolve(); - }) - .catch((error) => - { - console.log("error"); - console.error(error); - reject(); - }); - */ }); } @@ -253,6 +236,31 @@ export const getContractPenaltyInvoiceFile = ({ contract }) => console.error("getContractPenaltyInvoiceFile", "error"); console.error(error); + reject(); + }); + }); +} + +export const getContractCalculationPDFFile = ({ calculation }) => +{ + console.log("ACTION", "getContractCalculationPDFFile", { calculation }); + + return new Promise((resolve, reject) => + { + axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/contract/graphic`, { + params: { calculation }, + responseType: 'blob', + }) + .then((response) => + { + fileDownload(response.data, Buffer.from(response.headers.filename, 'base64')); + resolve(); + }) + .catch((error) => + { + console.error("getContractCalculationPDFFile", "error"); + console.error(error); + reject(); }); }); diff --git a/pages/api/file/bitrix.js b/pages/api/file/bitrix.js index 16de1f3..8b266fd 100644 --- a/pages/api/file/bitrix.js +++ b/pages/api/file/bitrix.js @@ -18,7 +18,6 @@ export default async function handler(req, res) { if(jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT)) { - console.log("req.query.urlreq.query.urlreq.query.urlreq.query.urlreq.query.url", req.query.url); try { axios.get(req.query.url, { diff --git a/pages/api/file/contract/graphic.js b/pages/api/file/contract/graphic.js new file mode 100644 index 0000000..5e355cb --- /dev/null +++ b/pages/api/file/contract/graphic.js @@ -0,0 +1,58 @@ +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import axios from 'axios'; +import { Cookies } from 'react-cookie'; +import cookie from 'cookie'; +import moment from 'moment'; +import jwt from 'jsonwebtoken'; +import { cors } from '../../../../lib/cors'; + +export default async function handler(req, res) +{ + await cors(req, res); + + console.log("API", "file", "contract", "graphic"); + console.log(req.query); + console.log("-".repeat(50)); + + if(req.headers.cookie !== undefined) + { + const cookies = cookie.parse(req.headers?.cookie ? req.headers?.cookie : ""); + + if(cookies.jwt !== undefined && cookies.jwt !== null) + { + var client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT); + var crm_jwt = jwt.sign(client_jwt_decoded, process.env.JWT_SECRET_CRM, { noTimestamp: true }); + + try + { + axios.get(`${ process.env.CRM_API_HOST }/lk/add-contract/GetPreCalculationGraphPrintForm`, { + params: { addcontract_number: req.query.calculation }, + headers: { + "Authorization": `Bearer ${ crm_jwt }`, + } + }) + .then((crm_response) => + { + console.log("API", "/file/contract/graphic", "crm_response.data"); + + res.setHeader("filename", Buffer.from(crm_response.data.key).toString('base64')); + res.status(200).send(Buffer.from(crm_response.data.value, 'base64')); + }) + .catch((error) => + { + console.error(error); + res.status(500); + }); + } + catch(e) + { + console.error(e); + res.status(500); + } + } + else + { + res.status(403); + } + } +} \ No newline at end of file diff --git a/pages/contract/change/components/Comparison/index.js b/pages/contract/change/components/Comparison/index.js index 4962a9c..619dad7 100644 --- a/pages/contract/change/components/Comparison/index.js +++ b/pages/contract/change/components/Comparison/index.js @@ -125,6 +125,7 @@ export default class Comparison extends React.Component console.log(this.props); console.log("this.state"); console.log(this.state); + this.props.onDownloadPDF(0); } _renderMixedPayments = () => @@ -226,16 +227,16 @@ export default class Comparison extends React.Component render() { const { calculation, mixed_index, today, show_previous, signatories_show_all } = this.state; - const { number, signer, signatories, current, calculated } = this.props; + const { number, signer, signatories, current, calculated, loading_pdf } = this.props; return (

Выбранный(ые) варианты изменения графика

Вернуться к параметрам изменения графика - {/*} - - {*/} +
{ calculation !== undefined && calculation !== null && calculation.fix_last_payment_available_comment !== null && ( diff --git a/pages/contract/change/index.js b/pages/contract/change/index.js index 91dfb20..34e687a 100644 --- a/pages/contract/change/index.js +++ b/pages/contract/change/index.js @@ -29,7 +29,8 @@ import { getContractGraphicChangeGetCalculated, getContractGraphicChangeSignatories, getContractGraphicChangeCalculationsList, - signContractGraphicChange, + signContractGraphicChange, + getContractCalculationPDFFile, } from "../../../actions"; import Options from "./components/Options"; import Comparison from "./components/Comparison"; @@ -43,6 +44,7 @@ class ChangeGraphicPage extends React.Component super(props); this.state = { loading: false, + loading_pdf: false, contracts_info: {}, mode_options: false, mode_comparison: false, @@ -233,6 +235,27 @@ class ChangeGraphicPage extends React.Component }); } + _handle_onDownloadPDF = () => + { + const { calculation_id, } = this.state; + + this.setState({ loading_pdf: true, }, () => + { + getContractCalculationPDFFile({ calculation: calculation_id, }) + .then(() => + { + this.setState({ loading_pdf: false, }); + }) + .catch(() => + { + this.setState({ loading_pdf: false, }, () => + { + alert("К сожалению, при создании файла PDF расчёта возникла ошибка.\n\nПожалуйста, попробуйте позднее или воспользуйтесь графиком ниже."); + }); + }); + }); + } + _handle_onCalculate = (calculation_id) => { const { dispatch, number } = this.props; @@ -270,7 +293,7 @@ class ChangeGraphicPage extends React.Component render() { const { number } = this.props; - const { loading, contracts_info, mode_options, mode_comparison, mode_calculation, mode_final, signer, signatories, calculations, calculation_id, editable, variants, variants_loading, variants_selected, variants_types, variants_unavailable, options, current, calculated, comment } = this.state; + const { loading, loading_pdf, contracts_info, mode_options, mode_comparison, mode_calculation, mode_final, signer, signatories, calculations, calculation_id, editable, variants, variants_loading, variants_selected, variants_types, variants_unavailable, options, current, calculated, comment } = this.state; let { date, car, status } = contracts_info[ number ] !== undefined ? contracts_info[ number ] : {}; @@ -310,6 +333,7 @@ class ChangeGraphicPage extends React.Component ) : (
) }