This repository has been archived on 2025-05-09. You can view files and clone it, but cannot push or open issues or pull requests.
2020-11-16 17:16:02 +03:00

338 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @ts-nocheck
import { toJS } from 'mobx';
import { gql } from '@apollo/client';
import { message } from 'antd';
import { openNotification } from 'client/Elements/Notification';
import CalculationService from 'client/services/CalculationService';
import { prepareCalculationData } from 'client/services/prepareData';
import CalculationStore from 'client/stores/CalculationStore';
import { getUser } from 'client/tools/user';
import { TAction } from 'core/types/Calculation/Store/effect';
import { ITableCell } from 'core/types/Calculation/Store/tables';
import { TableProps } from './../../../../core/types/Calculation/Store/tables';
import { Status } from 'core/types/statuses';
const actions: TAction = {
createLead: () => {
// TODO: block button for time
// TODO: collect errors
const {
contactClient,
contact,
newClient,
account,
commentLead,
phoneNumber,
} = CalculationStore.values;
if (!contactClient && !contact) {
CalculationStore.setValidation('selectContactClient', false);
CalculationStore.setValidation('tbxContact', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан контакт клиента',
})();
}
if (!newClient && !account) {
CalculationStore.setValidation('tbxNewClient', false);
CalculationStore.setValidation('selectAccount', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан клиент',
})();
}
if (!commentLead) {
CalculationStore.setValidation('tbxCommentLead', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан комментарий к интересу',
})();
}
if (contact && !phoneNumber) {
CalculationStore.setValidation('tbxPhoneNumber', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан телефон контактного лица',
})();
}
const { channel } = CalculationStore.values;
if (!channel) {
CalculationStore.setValidation('selectChannel', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан канал привлечения',
})();
} else {
const {
supplier,
agent,
finDepartment,
broker,
} = CalculationStore.values;
switch (channel) {
case 100000000: {
CalculationStore.setValidation('selectFinDepartment', true);
CalculationStore.setValidation('selectBroker', true);
if (!supplier) {
CalculationStore.setValidation('selectSupplier', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан поставщик',
})();
}
if (!agent) {
CalculationStore.setValidation('selectAgent', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан агент',
})();
}
break;
}
case 100000001: {
CalculationStore.setValidation('selectAgent', true);
CalculationStore.setValidation('selectBroker', true);
if (!supplier) {
CalculationStore.setValidation('selectSupplier', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан поставщик',
})();
}
if (!finDepartment) {
CalculationStore.setValidation('selectFinDepartment', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан финотдел',
})();
}
break;
}
case 100000002: {
CalculationStore.setValidation('selectFinDepartment', true);
CalculationStore.setValidation('selectSupplier', true);
CalculationStore.setValidation('selectBroker', true);
if (!agent) {
CalculationStore.setValidation('selectAgent', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан агент',
})();
}
break;
}
case 100000003: {
CalculationStore.setValidation('selectFinDepartment', true);
CalculationStore.setValidation('selectSupplier', true);
CalculationStore.setValidation('selectAgent', true);
if (!broker) {
CalculationStore.setValidation('selectBroker', false);
openNotification({
type: 'error',
title: 'Ошибка во время создания интереса',
description: 'Не указан брокер',
})();
}
break;
}
case 100000004:
default: {
CalculationStore.setValidation('selectFinDepartment', true);
CalculationStore.setValidation('selectSupplier', true);
CalculationStore.setValidation('selectAgent', true);
CalculationStore.setValidation('selectBroker', true);
break;
}
}
}
},
createKP: async () => {
const { values } = CalculationStore;
const calculationRes = await actions.calculate();
const {
columns,
postValues,
preparedValues,
preparedPayments,
} = calculationRes;
const domainName = (await getUser()).FullName;
// if (process.env.NODE_ENV === 'development') {
console.log('values', values);
console.log('domainName', domainName);
// }
CalculationService.createKp({
domainName,
calculation: {
preparedValues,
preparedPayments,
calculationColumns: columns,
postValues,
calculationValues: values,
},
})
.then(({ quoteid }) => {
CalculationService.crmgqlquery({
query: gql`
query($quoteid: Uuid!) {
quote(quoteId: $quoteid) {
offerprintform
evo_quotename
}
}
`,
variables: {
quoteid,
},
}).then(({ entities: { quote } }) => {
message.success({
content: `КП ${quote.evo_quotename} создано!`,
duration: 5,
style: {
marginTop: '7vh',
},
});
if (quote.offerprintform) window.open(quote.offerprintform, '_blank');
});
})
.catch(err => {
throw err;
});
},
calculate: async () => {
// TODO: collect errors
CalculationStore.setStatus('btnCreateKP', Status.Loading);
setTimeout(() => {
CalculationStore.setStatus('btnCreateKP', Status.Default);
}, 3500);
CalculationStore.setStatus('btnCalculate', Status.Loading);
setTimeout(() => {
CalculationStore.setStatus('btnCalculate', Status.Default);
}, 3500);
const { rows } = CalculationStore.tables.tableInsurance;
const kaskoRow = rows[1];
const DGORow = rows[2];
const NSRow = rows[3];
if (
kaskoRow &&
kaskoRow.insCost?.value === 0 &&
((DGORow && DGORow.insCost?.value > 0) ||
(NSRow && NSRow.insCost?.value > 0))
) {
CalculationStore.setTableRow(
'tableInsurance',
1,
)({
insCost: {
validation: false,
},
});
openNotification({
type: 'error',
title: 'Ошибка во время расчета графика',
description: 'Укажите стоимость КАСКО',
})();
// return;
}
if (
kaskoRow &&
kaskoRow.insured?.value === 100000001 &&
kaskoRow.insCost?.value === 0
) {
CalculationStore.setTableRow(
'tableInsurance',
1,
)({
insCost: {
validation: false,
},
});
openNotification({
type: 'error',
title: 'Ошибка во время расчета графика',
description: 'Укажите стоимость КАСКО, включаемую в график',
})();
// return;
}
CalculationStore.setTableRow(
'tableInsurance',
1,
)({
insCost: {
validation: true,
},
});
var preparedData = prepareCalculationData({
calculationStore: CalculationStore,
});
CalculationStore.setStatus('btnCalculate', Status.Loading);
setTimeout(() => {
CalculationStore.setStatus('btnCalculate', Status.Default);
}, 3500);
CalculationStore.cleanTable('tableResults');
console.log('preparedData', preparedData);
return CalculationService.calculate(preparedData)
.then(res => {
message.success({
content: 'Расчет завершен успешно!',
duration: 5,
style: {
marginTop: '7vh',
},
});
const { sumWithVatColumn, vatColumn, sumRepaymentColumn } = res.columns;
let results: TableProps<ITableCell>[] = [];
//@ts-ignore
for (let i = 0; i < preparedData.preparedValues.nmper; i++) {
results.push({
paymentSum: { value: sumWithVatColumn?.values[i + 1] },
ndsCompensation: { value: vatColumn?.values[i + 1] },
redemptionAmount: {
value: sumRepaymentColumn?.values[i + 1],
},
});
}
//TODO: speed up table render
CalculationStore.setTableRows('tableResults', 0)(results);
console.log('calculation res', res);
return { ...res, ...preparedData };
})
.catch(err => {
openNotification({
type: 'error',
title: 'Ошибка во время расчета!',
})();
throw err;
});
},
};
export default { actions };