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.
EvoCalculator/src/client/services/prepareData.ts
2020-11-17 11:50:59 +03:00

727 lines
22 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.

import { calcPrice } from 'client/stores/CalculationStore/Effects/lib/tools';
import valuesConstants from 'core/constants/values';
import { PaymentRow, PreparedValues } from 'core/types/Calculation/Prepare';
import { IGetCalculationRequest } from 'core/types/Calculation/Requests';
import { ICalculationStore } from 'core/types/Calculation/Store';
import { DateTime } from 'luxon';
export const prepareCalculationData = ({
calculationStore,
}: {
calculationStore: ICalculationStore;
}): IGetCalculationRequest => {
const currentDate = DateTime.local().toUTC().toJSDate();
const { values, options, tables } = calculationStore;
//@ts-ignore
let preparedPaymentsRows: PaymentRow[] = Array.from(
{ length: values.leasingPeriod },
() => ({
numberPayment: 0,
percentPayment: 0,
sumPayment: 0,
gpsBasePayment: 0,
gpsCostPayment: 0,
tlmBasePayment: 0,
tlmCostPayment: 0,
}),
);
const tracker = options.selectTracker?.find(
x => x.evo_addproduct_typeid === values.tracker,
);
const telematic = options.selectTelematic?.find(
x => x.evo_addproduct_typeid === values.telematic,
);
for (let i = 0; i < values.leasingPeriod; i++) {
preparedPaymentsRows[i].numberPayment = i + 1;
preparedPaymentsRows[i].percentPayment =
i === 0 ? 0 : tables.tablePayments.rows[i].paymentRelation?.value;
preparedPaymentsRows[i].sumPayment = 0;
if (tracker) {
if (tracker.evo_planpayments && tracker.evo_planpayments.length > 0) {
const evo_planpayment = tracker.evo_planpayments.find(
x => x.evo_name === (i + 1).toString(),
);
if (evo_planpayment) {
preparedPaymentsRows[i].gpsBasePayment =
(evo_planpayment.evo_cost_price_telematics_withoutnds || 0) +
(evo_planpayment.evo_cost_equipment_withoutnds || 0);
preparedPaymentsRows[i].gpsCostPayment =
evo_planpayment.evo_cost_telematics_withoutnds || 0;
}
}
}
if (telematic) {
if (telematic.evo_planpayments && telematic.evo_planpayments.length > 0) {
const evo_planpayment = telematic.evo_planpayments.find(
x => x.evo_name === (i + 1).toString(),
);
if (evo_planpayment) {
preparedPaymentsRows[i].tlmBasePayment =
(evo_planpayment.evo_cost_price_telematics_withoutnds || 0) +
(evo_planpayment.evo_cost_equipment_withoutnds || 0);
preparedPaymentsRows[i].tlmCostPayment =
evo_planpayment.evo_cost_telematics_withoutnds || 0;
}
}
}
}
let preparedValues: PreparedValues = {
brandId: '00000000-0000-0000-0000-000000000000',
configurationId: '00000000-0000-0000-0000-000000000000',
firstPaymentAbs: 0,
firstPaymentNdsAbs: 0,
firstPaymentWithNdsAbs: 0,
modelId: '00000000-0000-0000-0000-000000000000',
paymentDateNew: new Date(),
plTypeId: '00000000-0000-0000-0000-000000000000',
};
preparedValues.calcDate = currentDate;
preparedValues.calcType = 100000000;
preparedValues.irrExpected = (values.IRR_Perc as number) / 100;
preparedValues.npvniExpected = 0;
preparedValues.totalExpected = 0;
preparedValues.nmper = values.leasingPeriod;
preparedValues.leasing0K =
values.product === 'LEASING0'
? ((preparedValues.nmper as number) - 2) *
(0.0234 / ((1 - 1 / 1.0234) ^ ((preparedValues.nmper as number) - 2)))
: 1;
preparedValues.loanRate = (values.creditRate as number) / 100;
preparedValues.balanceHolder = values.balanceHolder;
preparedValues.dogDate = preparedValues.calcDate;
preparedValues.paymentDateNew = undefined;
preparedValues.deliveryTime = values.deliveryTime;
preparedValues.firstPaymentAbs = undefined;
preparedValues.firstPaymentNdsAbs = undefined;
preparedValues.firstPaymentWithNdsAbs = undefined;
preparedValues.lastPayment = (values.lastPaymentPerc as number) / 100;
preparedValues.lastPaymentSum =
(values.lastPaymentRub as number) / (1 + valuesConstants.VAT);
preparedValues.scheduleOfPayments = values.graphType;
preparedValues.comissionRub =
(values.comissionRub as number) / (1 + valuesConstants.VAT);
preparedValues.plTypeId = values.leaseObjectType;
preparedValues.brandId = values.brand;
preparedValues.modelId = values.model;
preparedValues.configurationId = values.configuration;
preparedValues.plYear = values.leaseObjectYear;
preparedValues.carCapacity = values.leaseObjectMotorPower;
preparedValues.motorVolume = values.engineVolume;
preparedValues.plEngineType = values.engineType;
preparedValues.carCarrying = values.maxMass;
preparedValues.bonus = (values.saleBonus as number) / 100;
preparedValues.bonusFix = 0;
preparedValues.transTax = 0;
preparedValues.transIncludeGr = false;
preparedValues.transportTaxGrYear = 0;
preparedValues.transportTaxGr = 0;
let insuranceKaskoRow = tables.tableInsurance.rows.find(
x => x.policyType?.value === 'КАСКО',
);
let insuranceNSRow = tables.tableInsurance.rows.find(
x => x.policyType?.value === 'НС',
);
let insuranceDGORow = tables.tableInsurance.rows.find(
x => x.policyType?.value === 'ДГО',
);
let insuranceOSAGORow = tables.tableInsurance.rows.find(
x => x.policyType?.value === 'ОСАГО',
);
if (insuranceKaskoRow?.insured?.value === 100000001) {
preparedValues.insuranceKasko =
insuranceKaskoRow?.insCost?.value +
insuranceDGORow?.insCost?.value +
insuranceNSRow?.insCost?.value;
} else {
preparedValues.insuranceKasko = 0;
}
preparedValues.nmperInsurance =
insuranceKaskoRow?.insTerm?.value === 100000001 ? values.leasingPeriod : 12;
preparedValues.insuranceOsago =
insuranceOSAGORow?.insured?.value === 100000001
? insuranceOSAGORow?.insCost?.value
: 0;
preparedValues.insurance =
(preparedValues.insuranceOsago || 0) + (preparedValues.insuranceKasko || 0);
preparedValues.insuranceKaskoNmper =
preparedValues.nmperInsurance || 0 >= 16
? (preparedValues.insuranceKasko ||
0 * (preparedValues.nmperInsurance || 0)) / 12
: preparedValues.insuranceKasko;
preparedValues.insuranceContract =
(preparedValues.insuranceKaskoNmper || 0) +
(preparedValues.insuranceOsago || 0);
preparedValues.repayment = 0.25;
if (values.firstPaymentPerc < 30) {
const evo_coefficient_season = calculationStore
.getStaticData('evo_coefficient')
.filter(
x =>
x.evo_corfficient_type === 100000000 &&
x.evo_min_period &&
x.evo_min_period <= values.leasingPeriod &&
x.evo_max_period &&
x.evo_max_period >= values.leasingPeriod,
);
switch (values.graphType) {
case 100000003:
if (evo_coefficient_season && evo_coefficient_season.length > 0) {
const evo_coefficient = evo_coefficient_season.find(
x =>
x.evo_season_type === values.seasonType &&
x.evo_graph_type === 100000003,
);
if (evo_coefficient)
preparedValues.firstPayment =
(values.firstPaymentPerc +
evo_coefficient.evo_correction_coefficient) /
100;
}
break;
case 100000004:
if (evo_coefficient_season && evo_coefficient_season.length > 0) {
const evo_coefficient = evo_coefficient_season.find(
x => x.evo_graph_type === 100000004,
);
if (evo_coefficient)
preparedValues.firstPayment =
(values.firstPaymentPerc +
evo_coefficient.evo_correction_coefficient) /
100;
}
break;
//TODO: beautify
default:
preparedValues.firstPayment = values.firstPaymentPerc / 100;
break;
}
} else {
preparedValues.firstPayment = values.firstPaymentPerc / 100;
}
//plPrice && discount
const supplierCurrency = options.selectSupplierCurrency?.find(
x => x.transactioncurrencyid === values.supplierCurrency,
);
const evo_currencychanges = calculationStore.getStaticData(
'evo_currencychange',
);
const evo_currencychange = evo_currencychanges.find(
x => x.evo_ref_transactioncurrency === values.supplierCurrency,
);
preparedValues.plPrice =
calcPrice(
supplierCurrency?.isocurrencycode,
values.leaseObjectPrice,
evo_currencychange?.evo_currencychange || 1,
) /
(1 + valuesConstants.VAT);
preparedValues.discount =
calcPrice(
supplierCurrency?.isocurrencycode,
values.supplierDiscountRub,
evo_currencychange?.evo_currencychange || 1,
) /
(1 + valuesConstants.VAT);
//
preparedValues.acceptSum =
(preparedValues.plPrice || 0) - (preparedValues.discount || 0);
preparedValues.firstPaymentSum =
(preparedValues.firstPayment || 0) * (preparedValues.plPrice || 0);
preparedValues.agentsSum =
(values.indAgentRewardSumm / 100) *
preparedValues.acceptSum *
valuesConstants.ESN;
preparedValues.doubleAgentsSum =
(values.calcDoubleAgentRewardSumm / 100) *
preparedValues.acceptSum *
valuesConstants.ESN;
preparedValues.deliverySum =
(values.dealerRewardSumm / 100) * preparedValues.acceptSum;
preparedValues.brokerSum =
(values.calcBrokerRewardSum / 100) * preparedValues.acceptSum;
preparedValues.brokerOfDeliverySum =
(values.dealerBrokerRewardSumm / 100) * preparedValues.acceptSum;
preparedValues.financialDeptOfDeliverySum =
(values.finDepartmentRewardSumm / 100) * preparedValues.acceptSum;
if (values.importerRewardRub > 0) {
preparedValues.importerSum = values.importerRewardRub;
} else {
preparedValues.importerSum =
(values.importerRewardPerc / 100) * preparedValues.acceptSum;
}
const evo_coefficient_bonuses = calculationStore
.getStaticData('evo_coefficient')
.filter(
x => x.evo_corfficient_type === 100000002 && x.evo_sot_coefficient_typeid,
);
const systemuser = calculationStore.getStaticData('systemuser')[0];
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser &&
!Array.isArray(systemuser)
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'EXTRA_BONUS');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_job_titleid === systemuser.evo_job_titleid &&
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.extraBonus = evo_coefficient.evo_sot_coefficient || 0;
}
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'DIRECTOR_BONUS');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.directorBonus = evo_coefficient.evo_sot_coefficient || 0;
}
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'MARKET_RATE');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.marketRate = evo_coefficient.evo_sot_coefficient || 0;
}
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'DISTRICT_RATE');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.districtRate = evo_coefficient.evo_sot_coefficient || 0;
}
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'SALARY_RATE');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.salaryRate = evo_coefficient.evo_sot_coefficient || 0;
}
}
const technicalCard = options.selectTechnicalCard?.find(
x => x.evo_addproduct_typeid === values.technicalCard,
);
if (technicalCard) {
preparedValues.rats = technicalCard.evo_graph_price_withoutnds || 0;
preparedValues.baseRatCost =
technicalCard.evo_cost_service_provider_withoutnds || 0;
preparedValues.retroBonus = technicalCard.evo_retro_bonus_withoutnds || 0;
}
const selectedRegistration = options.selectRegistration?.find(
x => x.evo_addproduct_typeid === values.registration,
);
if (selectedRegistration) {
preparedValues.registration =
selectedRegistration.evo_graph_price_withoutnds || 0;
preparedValues.baseRegistration =
selectedRegistration.evo_cost_service_provider_withoutnds || 0;
}
if (tracker) {
preparedValues.trackerCost = tracker.evo_graph_price_withoutnds || 0;
}
if (telematic) {
preparedValues.tLMCost = telematic.evo_graph_price_withoutnds || 0;
}
const nsibBaseValue =
((preparedValues.plPrice || 0) +
(preparedValues.insuranceContract +
(preparedValues.rats || 0) +
(preparedValues.registration || 0) +
(preparedValues.trackerCost || 0) +
(preparedValues.tLMCost || 0) +
preparedValues.transportTaxGr +
preparedPaymentsRows
.map(x => x.tlmCostPayment || 0)
.reduce((a, b) => a + b, 0) +
preparedPaymentsRows
.map(x => x.gpsCostPayment || 0)
.reduce((a, b) => a + b, 0)) *
preparedValues.leasing0K -
preparedValues.firstPaymentSum -
(values.product === 'LEASING0' ? 0 : preparedValues.discount || 0)) *
(1 + valuesConstants.VAT);
if (nsibBaseValue > valuesConstants.NSIB_MAX) {
preparedValues.nsibBase = valuesConstants.NSIB_MAX;
} else {
preparedValues.nsibBase = nsibBaseValue;
}
const selectedInsNSIB = options.selectInsNSIB?.find(
x => x.evo_addproduct_typeid === values.insNSIB,
);
if (selectedInsNSIB) {
preparedValues.nsibBrutto =
((((selectedInsNSIB.evo_graph_price || 0) / 100) *
preparedValues.nsibBase) /
12) *
(preparedValues.nmper || 0);
preparedValues.nsibNetto =
((((selectedInsNSIB.evo_cost_service_provider_withoutnds || 0) / 100) *
preparedValues.nsibBase) /
12) *
(preparedValues.nmper || 0);
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser &&
!Array.isArray(systemuser)
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'BONUS_RAT_PR');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_job_titleid === systemuser.evo_job_titleid &&
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.ratBonus =
(evo_coefficient.evo_sot_coefficient || 0) * (preparedValues.rats || 0);
}
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser &&
!Array.isArray(systemuser)
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'BONUS_NSIB_PR');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_job_titleid === systemuser.evo_job_titleid &&
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient) {
preparedValues.nsibBonus =
(evo_coefficient.evo_sot_coefficient || 0) *
(preparedValues.nsibBrutto || 0);
}
}
if (
evo_coefficient_bonuses &&
evo_coefficient_bonuses.length > 0 &&
systemuser &&
!Array.isArray(systemuser)
) {
const evo_sot_coefficient_type = calculationStore
.getStaticData('evo_sot_coefficient_type')
.find(x => x.evo_id === 'BONUS_NS_PR');
const evo_coefficient = evo_coefficient_bonuses.find(
x =>
x.evo_job_titleid === systemuser.evo_job_titleid &&
x.evo_sot_coefficient_typeid ===
evo_sot_coefficient_type?.evo_sot_coefficient_typeid,
);
if (evo_coefficient && insuranceKaskoRow?.insured?.value === 100000001) {
preparedValues.nsBonus =
(evo_coefficient.evo_sot_coefficient || 0) *
(insuranceNSRow?.insCost?.value || 0);
}
}
if (values.insDecentral === true) {
if (
insuranceDGORow &&
insuranceDGORow.insCost &&
insuranceDGORow.insCost.value &&
insuranceNSRow &&
insuranceNSRow.insCost &&
insuranceNSRow.insCost.value
) {
preparedValues.insuranceBonus =
-1 *
(preparedValues.acceptSum *
valuesConstants.KASKO_PR *
valuesConstants.KASKO_BONUS_PR +
(((insuranceDGORow.insCost.value + insuranceNSRow.insCost.value) *
valuesConstants.KASKO_BONUS_PR) /
(1 + valuesConstants.VAT)) *
((preparedValues.insuranceKaskoNmper || 0) > 12 &&
(preparedValues.insuranceKaskoNmper || 0) < 16
? (preparedValues.insuranceKaskoNmper || 0) / 12
: 1));
}
} else {
preparedValues.insuranceBonus = 0;
}
preparedValues.cityc = values.townRegistration || values.regionRegistration;
const tarif = options.selectTarif?.find(x => x.evo_tarifid === values.tarif);
if (tarif) {
preparedValues.irr_MSFO_Plan = (tarif.evo_irr_plan || 0) / 100;
}
const evo_coefficient_risk = calculationStore
.getStaticData('evo_coefficient')
?.find(
x =>
x.evo_corfficient_type === 100000001 &&
x.evo_min_period &&
x.evo_min_period <= values.leasingPeriod &&
x.evo_max_period &&
x.evo_max_period >= values.leasingPeriod,
// x.evo_client_riskid === values.clientRisk,
// x.evo_leasingobject_types &&
// x.evo_leasingobject_types.length > 0 &&
// x.evo_leasingobject_types.filter(
// z => z.evo_leasingobject_typeid === values.leaseObjectType,
// ).length > 0,
);
if (evo_coefficient_risk)
preparedValues.npvniDelta = evo_coefficient_risk.evo_risk_delta || 0;
const evo_leasingobject_type = options.selectLeaseObjectType?.find(
x => x.evo_leasingobject_typeid === values.leaseObjectType,
);
if (evo_leasingobject_type && evo_leasingobject_type.evo_id) {
let deprecation_rate = 1;
if (
(evo_leasingobject_type.evo_id === '1' &&
(values.engineType === 100000000 || values.engineType === 100000004) &&
values.engineVolume <= 3.5) ||
(evo_leasingobject_type.evo_id === '2' &&
(values.engineType === 100000000 ||
values.engineType === 100000004 ||
values.engineType === 100000001) &&
values.maxMass <= 3500) ||
(evo_leasingobject_type.evo_id !== '1' &&
evo_leasingobject_type.evo_id !== '2')
) {
if (evo_leasingobject_type.evo_expluatation_period1)
preparedValues.nmperDeprecation =
evo_leasingobject_type.evo_expluatation_period1;
if (evo_leasingobject_type.evo_depreciation_rate1)
deprecation_rate = evo_leasingobject_type.evo_depreciation_rate1;
} else {
if (evo_leasingobject_type.evo_expluatation_period2)
preparedValues.nmperDeprecation =
evo_leasingobject_type.evo_expluatation_period2;
if (evo_leasingobject_type.evo_depreciation_rate2)
deprecation_rate = evo_leasingobject_type.evo_depreciation_rate2;
}
if (
preparedValues.nmperDeprecation === 85 ||
preparedValues.nmperDeprecation === 61
) {
preparedValues.deprecationTime = deprecation_rate * 3;
} else {
preparedValues.deprecationTime = deprecation_rate;
}
}
const {
plPrice = 0,
insurance,
rats = 0,
registration = 0,
trackerCost = 0,
tLMCost = 0,
transportTaxGr,
leasing0K,
nsibBrutto = 0,
insuranceContract,
comissionRub,
discount = 0,
baseRegistration = 0,
nsibNetto = 0,
nmper = 0,
firstPaymentSum,
} = preparedValues;
preparedValues.calculationCost =
plPrice +
(insurance + rats + registration + trackerCost + tLMCost + transportTaxGr) *
leasing0K;
preparedValues.priceUpTotal =
plPrice +
trackerCost +
tLMCost +
preparedPaymentsRows
.map(x => x.tlmCostPayment || 0)
.reduce((a, b) => a + b, 0) +
preparedPaymentsRows
.map(x => x.gpsCostPayment || 0)
.reduce((a, b) => a + b, 0) +
registration +
insuranceContract +
rats +
nsibBrutto +
transportTaxGr;
preparedValues.acquisitionExpenses =
plPrice +
insurance -
comissionRub -
discount +
trackerCost +
tLMCost +
baseRegistration +
rats;
preparedValues.npvBase =
plPrice +
((nsibNetto * 12) / nmper +
insurance +
baseRegistration +
trackerCost +
tLMCost +
rats -
discount -
comissionRub -
firstPaymentSum);
preparedValues.niAtInception =
preparedValues.acquisitionExpenses - firstPaymentSum;
preparedValues.dogCredit =
plPrice +
(insurance + rats + registration + trackerCost + tLMCost + transportTaxGr) *
leasing0K -
firstPaymentSum +
(nsibNetto * 12) / nmper -
(values.product === 'LEASING0' ? 0 : discount);
preparedValues.dogCreditLeasing =
plPrice +
insurance +
registration +
trackerCost +
tLMCost +
rats -
discount -
comissionRub -
firstPaymentSum;
return {
preparedPayments: { rows: preparedPaymentsRows },
preparedValues,
};
};