diff --git a/apps/web/trpc/routers/calculate/index.ts b/apps/web/trpc/routers/calculate/index.ts index 981155b..49c0c51 100644 --- a/apps/web/trpc/routers/calculate/index.ts +++ b/apps/web/trpc/routers/calculate/index.ts @@ -1,6 +1,6 @@ import { user } from '../../middleware'; import { t } from '../../server'; -import { prepareData } from './prepare-data'; +import { collectPrepareData } from './prepare-data'; import { CalculateInputSchema, CalculateOutputSchema } from './types'; import { validate } from './validation'; import initializeApollo from '@/apollo/client'; @@ -30,7 +30,7 @@ const calculateRouter = t.router({ }; } - const preparedData = await prepareData({ + const preparedData = await collectPrepareData({ context: { apolloClient, queryClient, diff --git a/apps/web/trpc/routers/calculate/prepare-data.ts b/apps/web/trpc/routers/calculate/prepare-data.ts index 8d14dad..1b93f63 100644 --- a/apps/web/trpc/routers/calculate/prepare-data.ts +++ b/apps/web/trpc/routers/calculate/prepare-data.ts @@ -1,8 +1,12 @@ -import { getPreparedValues } from './prepared-values'; -import type { CalculateInput, Context, PreparedData } from './types'; +/* eslint-disable sonarjs/cognitive-complexity */ +import type { CalculateInput, Context, PreparedData, PreparedValues } from './types'; import type { User } from '@/api/user/types'; +import { ESN, NSIB_MAX, VAT } from '@/constants/values'; +import * as CRMTypes from '@/graphql/crm.types'; +import { createCurrencyUtility } from '@/utils/currency'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; +import { max, sum } from 'radash'; dayjs.extend(utc); @@ -12,8 +16,15 @@ type Input = { user: User; }; -export async function prepareData({ context, input, user }: Input): Promise { - const { payments } = input; +type PreparedValuesGetters = { + [Key in keyof PreparedValues]: () => Promise; +}; + +export async function collectPrepareData({ context, input, user }: Input): Promise { + const { apolloClient } = context; + const { values, insurance, payments } = input; + + const { RUB } = createCurrencyUtility({ apolloClient }); const preparedPayments: PreparedData['preparedPayments'] = { rows: payments.values.map((payment, index) => ({ @@ -27,7 +38,1145 @@ export async function prepareData({ context, input, user }: Input): Promise x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_FINGAP_PR' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async bonusFix() { + const evo_coefficient = evo_coefficients?.find( + (x) => + x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_FIX' && + Boolean(values.product) && + x.evo_baseproducts?.some((evo_baseproduct) => evo_baseproduct?.evo_id === values.product) + ); + + return evo_coefficient?.evo_sot_coefficient || 0; + }, + + async bonusNsPr() { + return 0; + }, + + async bonusNsibPr() { + return 0; + }, + + async bonusRatPr() { + return 0; + }, + + async brandId() { + return values.brand; + }, + + async brokerOfDeliverySum() { + if (values.dealerBrokerRewardCondition) { + const { + data: { evo_reward_condition }, + } = await apolloClient.query({ + query: CRMTypes.GetRewardConditionDocument, + variables: { conditionId: values.dealerBrokerRewardCondition }, + }); + + if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { + return values.dealerBrokerRewardSumm / (1 + VAT); + } else if ( + evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 + ) { + const plPriceWithVAT = await this.plPriceWithVAT(); + const discount = await this.discount(); + + return (values.dealerBrokerRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)); + } else { + const acceptSum = await this.acceptSum(); + + return (values.dealerBrokerRewardSumm / 100) * acceptSum; + } + } + + return 0; + }, + + async brokerSum() { + if (values.calcBrokerRewardCondition) { + const { + data: { evo_reward_condition }, + } = await apolloClient.query({ + query: CRMTypes.GetRewardConditionDocument, + variables: { conditionId: values.calcBrokerRewardCondition }, + }); + + if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { + return values.calcBrokerRewardSum / (1 + VAT); + } else if ( + evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 + ) { + const discount = await this.discount(); + const plPriceWithVAT = await this.plPriceWithVAT(); + + return (values.calcBrokerRewardSum / 100) * (plPriceWithVAT - discount * (1 + VAT)); + } else { + const acceptSum = await this.acceptSum(); + + return (values.calcBrokerRewardSum / 100) * acceptSum; + } + } + + return 0; + }, + + async calcDate() { + return currentUTCDate.toDate(); + }, + + async calcType() { + return values.calcType; + }, + + async carCapacity() { + return values.leaseObjectMotorPower; + }, + + async carCarrying() { + return values.maxMass; + }, + + async carSeats() { + return 0; + }, + + async cityc() { + return values.townRegistration || values.regionRegistration || ''; + }, + + async comissionRub() { + return values.comissionRub / (1 + VAT); + }, + + async configurationId() { + return values.configuration; + }, + + async deliverySum() { + if (values.calcDoubleAgentRewardCondition) { + const { + data: { evo_reward_condition }, + } = await apolloClient.query({ + query: CRMTypes.GetRewardConditionDocument, + variables: { conditionId: values.calcDoubleAgentRewardCondition }, + }); + + if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { + return values.dealerRewardSumm / (1 + VAT); + } else if ( + evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 + ) { + const discount = await this.discount(); + const plPriceWithVAT = await this.plPriceWithVAT(); + + return (values.dealerRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)); + } else { + const acceptSum = await this.acceptSum(); + + return (values.dealerRewardSumm / 100) * acceptSum; + } + } + + return 0; + }, + + async deliveryTime() { + return values.deliveryTime; + }, + + async deprecationTime() { + const deprecation = await getDeprecation(); + + if (deprecation?.nmperDeprecation && [85, 61].includes(deprecation?.nmperDeprecation)) { + return (deprecation.deprecationRate || 0) * 3; + } + + return deprecation?.deprecationRate || 0; + }, + + async directorBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async directorBonusFinGAP() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS_FINGAP' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async directorBonusFix() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS_FIX' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async directorBonusNsib() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS_NSIB' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async directorExtraBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_EXTRA_BONUS' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async discount() { + if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { + return ( + ((await RUB({ + currencyid: values.supplierCurrency, + value: values.supplierDiscountRub, + })) + + values.importProgramSum) / + (1 + VAT) + ); + } + + return (values.supplierDiscountRub + values.importProgramSum) / (1 + VAT); + }, + + async districtRate() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DISTRICT_RATE' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async doubleAgentsSum() { + if (values.calcDoubleAgentRewardCondition) { + const { + data: { evo_reward_condition }, + } = await apolloClient.query({ + query: CRMTypes.GetRewardConditionDocument, + variables: { conditionId: values.calcDoubleAgentRewardCondition }, + }); + + if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { + return values.calcDoubleAgentRewardSumm * ESN; + } else if ( + evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 + ) { + const plPriceWithVAT = await this.plPriceWithVAT(); + const discount = await this.discount(); + + return ( + (values.calcDoubleAgentRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)) * ESN + ); + } else { + const acceptSum = await this.acceptSum(); + + return (values.calcDoubleAgentRewardSumm / 100) * acceptSum * ESN; + } + } + + return 0; + }, + + async extraBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'EXTRA_BONUS' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async financialDeptOfDeliverySum() { + if (values.finDepartmentRewardCondtion) { + const { + data: { evo_reward_condition }, + } = await apolloClient.query({ + query: CRMTypes.GetRewardConditionDocument, + variables: { conditionId: values.finDepartmentRewardCondtion }, + }); + + if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { + return values.dealerRewardSumm / (1 + VAT); + } else if ( + evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 + ) { + const discount = await this.discount(); + const plPriceWithVAT = await this.plPriceWithVAT(); + + return (values.dealerRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)); + } else { + const acceptSum = await this.acceptSum(); + + return (values.dealerRewardSumm / 100) * acceptSum; + } + } + + return 0; + }, + + async firstPayment() { + const plPrice = await this.plPrice(); + const importProgramSum = await this.importProgramSum(); + const firstPaymentPercWthtVAT = + values.firstPaymentRub / (1 + VAT) / (plPrice - importProgramSum); + + if (firstPaymentPercWthtVAT * 100 < 30) { + if (values.graphType === 100_000_003 && values.highSeasonStart !== 100_000_000) { + const evo_coefficient = evo_coefficients?.find( + (x) => + x?.evo_corfficient_type === 100_000_000 && + x.evo_min_period && + x.evo_min_period <= values.leasingPeriod && + x.evo_max_period && + x.evo_max_period >= values.leasingPeriod && + x.evo_season_type === values.seasonType && + x.evo_graph_type === values.graphType + ); + + return ( + (firstPaymentPercWthtVAT * 100 + (evo_coefficient?.evo_correction_coefficient || 0)) / + 100 + ); + } + + if (values.graphType === 100_000_004) { + const evo_coefficient = evo_coefficients?.find( + (x) => + x?.evo_corfficient_type === 100_000_000 && + x.evo_min_period && + x.evo_min_period <= values.leasingPeriod && + x.evo_max_period && + x.evo_max_period >= values.leasingPeriod && + x.evo_graph_type === values.graphType + ); + + return ( + (firstPaymentPercWthtVAT * 100 + (evo_coefficient?.evo_correction_coefficient || 0)) / + 100 + ); + } + } + + return firstPaymentPercWthtVAT; + }, + + async firstPaymentAbs() { + return 0; + }, + + async firstPaymentNdsAbs() { + return 0; + }, + async firstPaymentSum() { + const firstPayment = await this.firstPayment(); + const plPrice = await this.plPrice(); + const importProgramSum = await this.importProgramSum(); + + return firstPayment * (plPrice - importProgramSum); + }, + + async firstPaymentWithNdsAbs() { + return 0; + }, + + async fuelCardSum() { + if (values.fuelCard) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.fuelCard }, + }); + + return evo_addproduct_type?.evo_graph_price_withoutnds || 0; + } + + return 0; + }, + + async gpsCostPaymentSum() { + return sum(preparedPayments.rows, (p) => p.gpsCostPayment); + }, + + async iRR_MSFO_Plan() { + if (values.tarif) { + const { + data: { evo_tarif }, + } = await apolloClient.query({ + query: CRMTypes.GetTarifDocument, + variables: { tarifId: values.tarif }, + }); + + return (evo_tarif?.evo_irr_plan || 0) / 100; + } + + return 0; + }, + + async importProgramSum() { + return values.importProgramSum / (1 + VAT); + }, + + async importerSum() { + const acceptSum = await this.acceptSum(); + + return values.importerRewardRub || (values.importerRewardPerc / 100) * acceptSum; + }, + + async insuranceBonus() { + return 0; + }, + + async insuranceBonusLoss() { + if (values.leasingWithoutKasko) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.leasingWithoutKasko }, + }); + + if (evo_addproduct_type?.evo_evokasko_calc_type === 100_000_001) { + return ( + ((evo_addproduct_type?.evo_loss_kv || 0) * + (values.plPriceRub - + values.discountRub - + values.importProgramSum + + values.addEquipmentPrice - + values.firstPaymentRub)) / + 100 + ); + } else { + return ( + ((evo_addproduct_type?.evo_loss_kv || 0) * + (values.plPriceRub - + values.discountRub - + values.importProgramSum + + values.addEquipmentPrice)) / + 100 + ); + } + } + + return 0; + }, + + async insuranceContract() { + const insuranceKaskoNmper = await this.insuranceKaskoNmper(); + const insuranceOsago = await this.insuranceOsago(); + + return insuranceKaskoNmper + insuranceOsago; + }, + + async insuranceEvoKasko() { + if (values.leasingWithoutKasko) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.leasingWithoutKasko }, + }); + + if (evo_addproduct_type?.evo_evokasko_calc_type === 100_000_001) { + return ( + (evo_addproduct_type?.evo_cost_service_provider_withoutnds || 0) * + (values.plPriceRub - + values.discountRub - + values.importProgramSum + + values.addEquipmentPrice - + values.firstPaymentRub) + + (evo_addproduct_type?.evo_price_service_provider_withoutnds || 0) + ); + } else { + return ( + (evo_addproduct_type?.evo_cost_service_provider_withoutnds || 0) * + (values.plPriceRub - + values.discountRub - + values.importProgramSum + + values.addEquipmentPrice) + + (evo_addproduct_type?.evo_price_service_provider_withoutnds || 0) + ); + } + } + + return 0; + }, + + async insuranceFinGAP() { + const { insured, insCost } = insurance.values.fingap; + + return insured === 100_000_001 ? insCost : 0; + }, + + async insuranceKasko() { + const { insured, insCost } = insurance.values.kasko; + + return insured === 100_000_001 ? insCost : 0; + }, + + async insuranceKaskoNmper() { + const nmperInsurance = await this.nmperInsurance(); + const insuranceKasko = await this.insuranceKasko(); + + return nmperInsurance >= 16 ? (insuranceKasko * nmperInsurance) / 12 : insuranceKasko; + }, + + async insuranceOsago() { + const { insured, insCost } = insurance.values.osago; + + return insured === 100_000_001 ? insCost : 0; + }, + + async irrExpected() { + return values.IRR_Perc / 100; + }, + + async lastPayment() { + return values.lastPaymentPerc / 100; + }, + + async lastPaymentFix() { + return values.lastPaymentRule === 100_000_002; + }, + + async lastPaymentSum() { + return values.lastPaymentRub / (1 + VAT); + }, + + async leasing0K() { + if (values.product) { + const nmper = await this.nmper(); + const { + data: { evo_baseproduct }, + } = await apolloClient.query({ + query: CRMTypes.GetProductDocument, + variables: { productId: values.product }, + }); + + return evo_baseproduct?.evo_id === 'LEASING0' + ? (nmper - 2) * (0.0234 / (1 - 1 / 1.0234) ** (nmper - 2)) + : 1; + } + + return 1; + }, + + async loanRate() { + return values.creditRate; + }, + + async loanRatePeriod() { + if (values.rate) { + const { + data: { evo_rate }, + } = await apolloClient.query({ + query: CRMTypes.GetRateDocument, + variables: { rateId: values.rate }, + }); + + return evo_rate?.evo_credit_period || 0; + } + + return 0; + }, + + async marketRate() { + const evo_coefficient = evo_coefficients?.find( + (x) => + x?.evo_sot_coefficient_typeidData?.evo_id === 'MARKET_RATE' && + systemuser?.businessunitid && + x.evo_businessunits?.find( + (evo_businessunit) => + evo_businessunit?.evo_sale_businessunitid === systemuser?.businessunitid + ) + ); + + return evo_coefficient?.evo_sot_coefficient || 0; + }, + + async modelId() { + return values.model; + }, + + async motorVolume() { + return values.engineVolume; + }, + + async nmper() { + return values.leasingPeriod; + }, + + async nmperDeprecation() { + return (await getDeprecation())?.nmperDeprecation || 0; + }, + + async nmperFinGAP() { + const { insTerm } = insurance.values.fingap; + + return insTerm === 100_000_001 ? values.leasingPeriod : 12; + }, + + async nmperInsurance() { + const { insured } = insurance.values.kasko; + + return insured === 100_000_001 ? values.leasingPeriod : 12; + }, + + async npvniDelta() { + const evo_coefficient = evo_coefficients?.find( + (x) => + x?.evo_corfficient_type === 100_000_001 && + x.evo_min_period && + x.evo_min_period <= values.leasingPeriod && + x.evo_max_period && + x.evo_max_period >= values.leasingPeriod + ); + + return evo_coefficient?.evo_risk_delta || 0; + }, + + async npvniExpected() { + return 0; + }, + + async nsBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_NS_PR' + ); + + return evo_coefficient?.evo_sot_coefficient || 0; + }, + + async nsibBase() { + const discount = await this.discount(); + const plPrice = await this.plPrice(); + const importProgramSum = await this.importProgramSum(); + const insuranceContract = await this.insuranceContract(); + const rats = await this.rats(); + const registration = await this.registration(); + const trackerCost = await this.trackerCost(); + const tLMCost = await this.tLMCost(); + const transportTaxGr = await this.transportTaxGr(); + const tlmCostPaymentSum = await this.tlmCostPaymentSum(); + const gpsCostPaymentSum = await this.gpsCostPaymentSum(); + const leasing0K = await this.leasing0K(); + const firstPaymentSum = await this.firstPaymentSum(); + + const value = + (plPrice - + importProgramSum + + (insuranceContract + + rats + + registration + + trackerCost + + tLMCost + + transportTaxGr + + tlmCostPaymentSum + + gpsCostPaymentSum) * + leasing0K - + firstPaymentSum - + (values.product === 'LEASING0' ? 0 : discount)) * + (1 + VAT); + + return max([NSIB_MAX, value], (v) => v) ?? NSIB_MAX; + }, + + async nsibBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_NSIB_PR' + ); + const nsibBrutto = await this.nsibBrutto(); + + return (evo_coefficient?.evo_sot_coefficient || 0) * nsibBrutto; + }, + + async nsibBrutto() { + if (values.insNSIB) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.insNSIB }, + }); + + const nsibBase = await this.nsibBase(); + const nmper = await this.nmper(); + + return ((((evo_addproduct_type?.evo_graph_price || 0) / 100) * nsibBase) / 12) * nmper; + } + + return 0; + }, + + async nsibBruttoPr() { + return 0; + }, + + async nsibNetto() { + if (values.insNSIB) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.insNSIB }, + }); + + const nsibBase = await this.nsibBase(); + const nmper = await this.nmper(); + + return ( + ((((evo_addproduct_type?.evo_cost_service_provider_withoutnds || 0) / 100) * nsibBase) / + 12) * + nmper + ); + } + + return 0; + }, + + async nsibNettoPr() { + return 0; + }, + + async paymentDateNew() { + return null; + }, + + async plEngineType() { + return values.engineType; + }, + + async plPrice() { + if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { + return ( + (await RUB({ + currencyid: values.supplierCurrency, + value: values.leaseObjectPriceWthtVAT, + })) + + values.addEquipmentPrice / (1 + VAT) + ); + } + + return values.leaseObjectPriceWthtVAT + values.addEquipmentPrice / (1 + VAT); + }, + + async plPriceVAT() { + if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { + return ( + (await RUB({ + currencyid: values.supplierCurrency, + value: values.VATInLeaseObjectPrice, + })) + + (values.addEquipmentPrice - values.addEquipmentPrice / (1 + VAT)) + ); + } + + return ( + values.VATInLeaseObjectPrice + + (values.addEquipmentPrice - values.addEquipmentPrice / (1 + VAT)) + ); + }, + + async plPriceWithVAT() { + if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { + return ( + (await RUB({ + currencyid: values.supplierCurrency, + value: values.leaseObjectPrice, + })) + values.addEquipmentPrice + ); + } + + return values.leaseObjectPrice + values.addEquipmentPrice; + }, + + async plTypeId() { + return values.leaseObjectType; + }, + + async plYear() { + return values.leaseObjectYear; + }, + + async profitExpected() { + if (values.tarif) { + const { + data: { evo_tarif }, + } = await apolloClient.query({ + query: CRMTypes.GetTarifDocument, + variables: { tarifId: values.tarif }, + }); + + return evo_tarif?.evo_margin_min || 0; + } + + return 0; + }, + + async ratBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_RAT_PR' + ); + const rats = await this.rats(); + + return (evo_coefficient?.evo_sot_coefficient || 0) * rats; + }, + + async rats() { + if (values.technicalCard) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.technicalCard }, + }); + + return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; + } + + return 0; + }, + + async regionalDirectorBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS' + ); + + return evo_coefficient?.evo_sot_coefficient || 0; + }, + + async regionalDirectorBonusFinGAP() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS_FINGAP' + ); + const { insCost } = insurance.values.fingap; + + return (evo_coefficient?.evo_sot_coefficient || 0) * insCost; + }, + + async regionalDirectorBonusFix() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS_FIX' + ); + + return evo_coefficient?.evo_sot_coefficient || 0; + }, + + async regionalDirectorBonusNsib() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS_NSIB' + ); + const nsibBrutto = await this.nsibBrutto(); + + return (evo_coefficient?.evo_sot_coefficient || 0) * nsibBrutto; + }, + + async regionalDirectorExtraBonus() { + const evo_coefficient = evo_coefficients?.find( + (x) => + x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_EXTRA_BONUS' && + Boolean(values.product) && + x.evo_baseproducts?.some((evo_baseproduct) => evo_baseproduct?.evo_id === values.product) + ); + + return evo_coefficient?.evo_sot_coefficient || 0; + }, + + async registration() { + if (values.registration) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.registration }, + }); + + return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; + } + + return 0; + }, + + async repayment() { + return 0.25; + }, + + async retroBonus() { + if (values.technicalCard) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.technicalCard }, + }); + + return evo_addproduct_type?.evo_retro_bonus_withoutnds ?? 0; + } + + return 0; + }, + + async salaryRate() { + const evo_coefficient = evo_coefficients?.find( + (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'SALARY_RATE' + ); + + return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; + }, + + async scheduleOfPayments() { + return values.graphType; + }, + + async subsidyPaymentNumber() { + if (values.subsidy) { + const { + data: { evo_subsidy }, + } = await apolloClient.query({ + query: CRMTypes.GetSubsidyDocument, + variables: { subsidyId: values.subsidy }, + }); + + return evo_subsidy?.evo_get_subsidy_payment || 0; + } + + return 0; + }, + + async subsidySum() { + return values.subsidySum / (1 + VAT); + }, + + async supplierFinancing() { + return false; + }, + + async tLMCost() { + if (values.telematic) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.telematic }, + }); + + return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; + } + + return 0; + }, + + async tlmCostPaymentSum() { + return sum(preparedPayments.rows, (p) => p.tlmCostPayment); + }, + + async totalExpected() { + return values.totalPayments + values.subsidySum; + }, + + async trackerCost() { + if (values.tracker) { + const { + data: { evo_addproduct_type }, + } = await apolloClient.query({ + query: CRMTypes.GetAddProductTypeDocument, + variables: { addproductTypeId: values.tracker }, + }); + + return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; + } + + return 0; + }, + + async transIncludeGr() { + return values.vehicleTaxInYear > 0; + }, + + async transTax() { + return values.vehicleTaxInYear; + }, + + async transportTaxGr() { + return values.vehicleTaxInLeasingPeriod; + }, + + async transportTaxGrYear() { + return values.vehicleTaxInYear; + }, + }; + + const preparedValuesResult = await Promise.all( + (Object.keys(getters) as Array).map( + async (key: T) => { + const value = await getters[key](); + + return { [key]: value }; + } + ) + ); + + const preparedValues = Object.assign({}, ...preparedValuesResult) as PreparedValues; return { preparedPayments, diff --git a/apps/web/trpc/routers/calculate/prepared-values.ts b/apps/web/trpc/routers/calculate/prepared-values.ts deleted file mode 100644 index 2cc1975..0000000 --- a/apps/web/trpc/routers/calculate/prepared-values.ts +++ /dev/null @@ -1,1174 +0,0 @@ -/* eslint-disable sonarjs/cognitive-complexity */ -import type { CalculateInput, Context, PreparedPayments, PreparedValues } from './types'; -import type { User } from '@/api/user/types'; -import { ESN, NSIB_MAX, VAT } from '@/constants/values'; -import * as CRMTypes from '@/graphql/crm.types'; -import { createCurrencyUtility } from '@/utils/currency'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; -import { max, sum } from 'radash'; - -dayjs.extend(utc); - -type Input = { - context: Context; - input: CalculateInput; - preparedPayments: PreparedPayments; - user: User; -}; - -type Getters = { - [Key in keyof PreparedValues]: () => Promise; -}; - -export async function getPreparedValues({ - input, - context, - user, - preparedPayments, -}: Input): Promise { - const { apolloClient } = context; - const { values, insurance } = input; - - const { RUB } = createCurrencyUtility({ apolloClient }); - - const currentUTCDate = dayjs().utc(false); - - const transactioncurrency = values.supplierCurrency - ? ( - await apolloClient.query({ - query: CRMTypes.GetTransactionCurrencyDocument, - variables: { currencyid: values.supplierCurrency }, - }) - ).data.transactioncurrency - : null; - - const { - data: { systemuser }, - } = await apolloClient.query({ - query: CRMTypes.GetSystemUserDocument, - variables: { - domainname: user.domainName, - }, - }); - - async function getDeprecation() { - if (values.leaseObjectType) { - const { - data: { evo_leasingobject_type }, - } = await apolloClient.query({ - query: CRMTypes.GetLeaseObjectTypeDocument, - variables: { leaseObjectTypeId: values.leaseObjectType }, - }); - - if ( - (evo_leasingobject_type?.evo_id === '1' && - (values.engineType === 100_000_000 || values.engineType === 100_000_004) && - values.engineVolume <= 3.5) || - (evo_leasingobject_type?.evo_id === '2' && - (values.engineType === 100_000_000 || - values.engineType === 100_000_004 || - values.engineType === 100_000_001) && - values.maxMass <= 3500) || - (evo_leasingobject_type?.evo_id !== '1' && evo_leasingobject_type?.evo_id !== '2') - ) { - return { - deprecationRate: evo_leasingobject_type?.evo_depreciation_rate1, - nmperDeprecation: evo_leasingobject_type?.evo_expluatation_period1, - }; - } else - return { - deprecationRate: evo_leasingobject_type?.evo_depreciation_rate2, - nmperDeprecation: evo_leasingobject_type.evo_expluatation_period2, - }; - } - - return null; - } - - const evo_coefficients = systemuser?.evo_job_titleid - ? ( - await apolloClient.query({ - query: CRMTypes.GetCoefficientsDocument, - variables: { - currentDate: currentUTCDate.format('YYYY-MM-DD'), - jobTitleId: systemuser?.evo_job_titleid, - }, - }) - ).data.evo_coefficients - : null; - - const getters: Getters = { - async acceptSum() { - const plPrice = await this.plPrice(); - const discount = await this.discount(); - - return plPrice - discount; - }, - - async agentsSum() { - if (values.indAgentRewardCondition) { - const { - data: { evo_reward_condition }, - } = await apolloClient.query({ - query: CRMTypes.GetRewardConditionDocument, - variables: { conditionId: values.indAgentRewardCondition }, - }); - - if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { - return values.indAgentRewardSumm * ESN; - } else if ( - evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 - ) { - const plPriceWithVAT = await this.plPriceWithVAT(); - const discount = await this.discount(); - - return (values.indAgentRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)) * ESN; - } - } - - const acceptSum = await this.acceptSum(); - - return (values.indAgentRewardSumm / 100) * acceptSum * ESN; - }, - - async balanceHolder() { - return values.balanceHolder; - }, - - async baseRatCost() { - if (values.technicalCard) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.technicalCard }, - }); - - return evo_addproduct_type?.evo_cost_service_provider_withoutnds ?? 0; - } - - return 0; - }, - - async baseRegistration() { - if (values.registration) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.registration }, - }); - - return evo_addproduct_type?.evo_cost_service_provider_withoutnds ?? 0; - } - - return 0; - }, - - async bonus() { - return values.saleBonus / 100; - }, - - async bonusCoefficient() { - return values.bonusCoefficient; - }, - - async bonusFinGAP() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_FINGAP_PR' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async bonusFix() { - const evo_coefficient = evo_coefficients?.find( - (x) => - x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_FIX' && - Boolean(values.product) && - x.evo_baseproducts?.some((evo_baseproduct) => evo_baseproduct?.evo_id === values.product) - ); - - return evo_coefficient?.evo_sot_coefficient || 0; - }, - - async bonusNsPr() { - return 0; - }, - - async bonusNsibPr() { - return 0; - }, - - async bonusRatPr() { - return 0; - }, - - async brandId() { - return values.brand; - }, - - async brokerOfDeliverySum() { - if (values.dealerBrokerRewardCondition) { - const { - data: { evo_reward_condition }, - } = await apolloClient.query({ - query: CRMTypes.GetRewardConditionDocument, - variables: { conditionId: values.dealerBrokerRewardCondition }, - }); - - if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { - return values.dealerBrokerRewardSumm / (1 + VAT); - } else if ( - evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 - ) { - const plPriceWithVAT = await this.plPriceWithVAT(); - const discount = await this.discount(); - - return (values.dealerBrokerRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)); - } else { - const acceptSum = await this.acceptSum(); - - return (values.dealerBrokerRewardSumm / 100) * acceptSum; - } - } - - return 0; - }, - - async brokerSum() { - if (values.calcBrokerRewardCondition) { - const { - data: { evo_reward_condition }, - } = await apolloClient.query({ - query: CRMTypes.GetRewardConditionDocument, - variables: { conditionId: values.calcBrokerRewardCondition }, - }); - - if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { - return values.calcBrokerRewardSum / (1 + VAT); - } else if ( - evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 - ) { - const discount = await this.discount(); - const plPriceWithVAT = await this.plPriceWithVAT(); - - return (values.calcBrokerRewardSum / 100) * (plPriceWithVAT - discount * (1 + VAT)); - } else { - const acceptSum = await this.acceptSum(); - - return (values.calcBrokerRewardSum / 100) * acceptSum; - } - } - - return 0; - }, - - async calcDate() { - return currentUTCDate.toDate(); - }, - - async calcType() { - return values.calcType; - }, - - async carCapacity() { - return values.leaseObjectMotorPower; - }, - - async carCarrying() { - return values.maxMass; - }, - - async carSeats() { - return 0; - }, - - async cityc() { - return values.townRegistration || values.regionRegistration || ''; - }, - - async comissionRub() { - return values.comissionRub / (1 + VAT); - }, - - async configurationId() { - return values.configuration; - }, - - async deliverySum() { - if (values.calcDoubleAgentRewardCondition) { - const { - data: { evo_reward_condition }, - } = await apolloClient.query({ - query: CRMTypes.GetRewardConditionDocument, - variables: { conditionId: values.calcDoubleAgentRewardCondition }, - }); - - if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { - return values.dealerRewardSumm / (1 + VAT); - } else if ( - evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 - ) { - const discount = await this.discount(); - const plPriceWithVAT = await this.plPriceWithVAT(); - - return (values.dealerRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)); - } else { - const acceptSum = await this.acceptSum(); - - return (values.dealerRewardSumm / 100) * acceptSum; - } - } - - return 0; - }, - - async deliveryTime() { - return values.deliveryTime; - }, - - async deprecationTime() { - const deprecation = await getDeprecation(); - - if (deprecation?.nmperDeprecation && [85, 61].includes(deprecation?.nmperDeprecation)) { - return (deprecation.deprecationRate || 0) * 3; - } - - return deprecation?.deprecationRate || 0; - }, - - async directorBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async directorBonusFinGAP() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS_FINGAP' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async directorBonusFix() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS_FIX' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async directorBonusNsib() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_BONUS_NSIB' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async directorExtraBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DIRECTOR_EXTRA_BONUS' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async discount() { - if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { - return ( - ((await RUB({ - currencyid: values.supplierCurrency, - value: values.supplierDiscountRub, - })) + - values.importProgramSum) / - (1 + VAT) - ); - } - - return (values.supplierDiscountRub + values.importProgramSum) / (1 + VAT); - }, - - async districtRate() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'DISTRICT_RATE' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async doubleAgentsSum() { - if (values.calcDoubleAgentRewardCondition) { - const { - data: { evo_reward_condition }, - } = await apolloClient.query({ - query: CRMTypes.GetRewardConditionDocument, - variables: { conditionId: values.calcDoubleAgentRewardCondition }, - }); - - if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { - return values.calcDoubleAgentRewardSumm * ESN; - } else if ( - evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 - ) { - const plPriceWithVAT = await this.plPriceWithVAT(); - const discount = await this.discount(); - - return ( - (values.calcDoubleAgentRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)) * ESN - ); - } else { - const acceptSum = await this.acceptSum(); - - return (values.calcDoubleAgentRewardSumm / 100) * acceptSum * ESN; - } - } - - return 0; - }, - - async extraBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'EXTRA_BONUS' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async financialDeptOfDeliverySum() { - if (values.finDepartmentRewardCondtion) { - const { - data: { evo_reward_condition }, - } = await apolloClient.query({ - query: CRMTypes.GetRewardConditionDocument, - variables: { conditionId: values.finDepartmentRewardCondtion }, - }); - - if (evo_reward_condition?.evo_calc_reward_rules === 100_000_001) { - return values.dealerRewardSumm / (1 + VAT); - } else if ( - evo_reward_condition?.evo_agency_agreementidData?.evo_leasingobject_price === 100_000_001 - ) { - const discount = await this.discount(); - const plPriceWithVAT = await this.plPriceWithVAT(); - - return (values.dealerRewardSumm / 100) * (plPriceWithVAT - discount * (1 + VAT)); - } else { - const acceptSum = await this.acceptSum(); - - return (values.dealerRewardSumm / 100) * acceptSum; - } - } - - return 0; - }, - - async firstPayment() { - const plPrice = await this.plPrice(); - const importProgramSum = await this.importProgramSum(); - const firstPaymentPercWthtVAT = - values.firstPaymentRub / (1 + VAT) / (plPrice - importProgramSum); - - if (firstPaymentPercWthtVAT * 100 < 30) { - if (values.graphType === 100_000_003 && values.highSeasonStart !== 100_000_000) { - const evo_coefficient = evo_coefficients?.find( - (x) => - x?.evo_corfficient_type === 100_000_000 && - x.evo_min_period && - x.evo_min_period <= values.leasingPeriod && - x.evo_max_period && - x.evo_max_period >= values.leasingPeriod && - x.evo_season_type === values.seasonType && - x.evo_graph_type === values.graphType - ); - - return ( - (firstPaymentPercWthtVAT * 100 + (evo_coefficient?.evo_correction_coefficient || 0)) / - 100 - ); - } - - if (values.graphType === 100_000_004) { - const evo_coefficient = evo_coefficients?.find( - (x) => - x?.evo_corfficient_type === 100_000_000 && - x.evo_min_period && - x.evo_min_period <= values.leasingPeriod && - x.evo_max_period && - x.evo_max_period >= values.leasingPeriod && - x.evo_graph_type === values.graphType - ); - - return ( - (firstPaymentPercWthtVAT * 100 + (evo_coefficient?.evo_correction_coefficient || 0)) / - 100 - ); - } - } - - return firstPaymentPercWthtVAT; - }, - - async firstPaymentAbs() { - return 0; - }, - - async firstPaymentNdsAbs() { - return 0; - }, - async firstPaymentSum() { - const firstPayment = await this.firstPayment(); - const plPrice = await this.plPrice(); - const importProgramSum = await this.importProgramSum(); - - return firstPayment * (plPrice - importProgramSum); - }, - - async firstPaymentWithNdsAbs() { - return 0; - }, - - async fuelCardSum() { - if (values.fuelCard) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.fuelCard }, - }); - - return evo_addproduct_type?.evo_graph_price_withoutnds || 0; - } - - return 0; - }, - - async gpsCostPaymentSum() { - return sum(preparedPayments.rows, (p) => p.gpsCostPayment); - }, - - async iRR_MSFO_Plan() { - if (values.tarif) { - const { - data: { evo_tarif }, - } = await apolloClient.query({ - query: CRMTypes.GetTarifDocument, - variables: { tarifId: values.tarif }, - }); - - return (evo_tarif?.evo_irr_plan || 0) / 100; - } - - return 0; - }, - - async importProgramSum() { - return values.importProgramSum / (1 + VAT); - }, - - async importerSum() { - const acceptSum = await this.acceptSum(); - - return values.importerRewardRub || (values.importerRewardPerc / 100) * acceptSum; - }, - - async insuranceBonus() { - return 0; - }, - - async insuranceBonusLoss() { - if (values.leasingWithoutKasko) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.leasingWithoutKasko }, - }); - - if (evo_addproduct_type?.evo_evokasko_calc_type === 100_000_001) { - return ( - ((evo_addproduct_type?.evo_loss_kv || 0) * - (values.plPriceRub - - values.discountRub - - values.importProgramSum + - values.addEquipmentPrice - - values.firstPaymentRub)) / - 100 - ); - } else { - return ( - ((evo_addproduct_type?.evo_loss_kv || 0) * - (values.plPriceRub - - values.discountRub - - values.importProgramSum + - values.addEquipmentPrice)) / - 100 - ); - } - } - - return 0; - }, - - async insuranceContract() { - const insuranceKaskoNmper = await this.insuranceKaskoNmper(); - const insuranceOsago = await this.insuranceOsago(); - - return insuranceKaskoNmper + insuranceOsago; - }, - - async insuranceEvoKasko() { - if (values.leasingWithoutKasko) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.leasingWithoutKasko }, - }); - - if (evo_addproduct_type?.evo_evokasko_calc_type === 100_000_001) { - return ( - (evo_addproduct_type?.evo_cost_service_provider_withoutnds || 0) * - (values.plPriceRub - - values.discountRub - - values.importProgramSum + - values.addEquipmentPrice - - values.firstPaymentRub) + - (evo_addproduct_type?.evo_price_service_provider_withoutnds || 0) - ); - } else { - return ( - (evo_addproduct_type?.evo_cost_service_provider_withoutnds || 0) * - (values.plPriceRub - - values.discountRub - - values.importProgramSum + - values.addEquipmentPrice) + - (evo_addproduct_type?.evo_price_service_provider_withoutnds || 0) - ); - } - } - - return 0; - }, - - async insuranceFinGAP() { - const { insured, insCost } = insurance.values.fingap; - - return insured === 100_000_001 ? insCost : 0; - }, - - async insuranceKasko() { - const { insured, insCost } = insurance.values.kasko; - - return insured === 100_000_001 ? insCost : 0; - }, - - async insuranceKaskoNmper() { - const nmperInsurance = await this.nmperInsurance(); - const insuranceKasko = await this.insuranceKasko(); - - return nmperInsurance >= 16 ? (insuranceKasko * nmperInsurance) / 12 : insuranceKasko; - }, - - async insuranceOsago() { - const { insured, insCost } = insurance.values.osago; - - return insured === 100_000_001 ? insCost : 0; - }, - - async irrExpected() { - return values.IRR_Perc / 100; - }, - - async lastPayment() { - return values.lastPaymentPerc / 100; - }, - - async lastPaymentFix() { - return values.lastPaymentRule === 100_000_002; - }, - - async lastPaymentSum() { - return values.lastPaymentRub / (1 + VAT); - }, - - async leasing0K() { - if (values.product) { - const nmper = await this.nmper(); - const { - data: { evo_baseproduct }, - } = await apolloClient.query({ - query: CRMTypes.GetProductDocument, - variables: { productId: values.product }, - }); - - return evo_baseproduct?.evo_id === 'LEASING0' - ? (nmper - 2) * (0.0234 / (1 - 1 / 1.0234) ** (nmper - 2)) - : 1; - } - - return 1; - }, - - async loanRate() { - return values.creditRate; - }, - - async loanRatePeriod() { - if (values.rate) { - const { - data: { evo_rate }, - } = await apolloClient.query({ - query: CRMTypes.GetRateDocument, - variables: { rateId: values.rate }, - }); - - return evo_rate?.evo_credit_period || 0; - } - - return 0; - }, - - async marketRate() { - const evo_coefficient = evo_coefficients?.find( - (x) => - x?.evo_sot_coefficient_typeidData?.evo_id === 'MARKET_RATE' && - systemuser?.businessunitid && - x.evo_businessunits?.find( - (evo_businessunit) => - evo_businessunit?.evo_sale_businessunitid === systemuser?.businessunitid - ) - ); - - return evo_coefficient?.evo_sot_coefficient || 0; - }, - - async modelId() { - return values.model; - }, - - async motorVolume() { - return values.engineVolume; - }, - - async nmper() { - return values.leasingPeriod; - }, - - async nmperDeprecation() { - return (await getDeprecation())?.nmperDeprecation || 0; - }, - - async nmperFinGAP() { - const { insTerm } = insurance.values.fingap; - - return insTerm === 100_000_001 ? values.leasingPeriod : 12; - }, - - async nmperInsurance() { - const { insured } = insurance.values.kasko; - - return insured === 100_000_001 ? values.leasingPeriod : 12; - }, - - async npvniDelta() { - const evo_coefficient = evo_coefficients?.find( - (x) => - x?.evo_corfficient_type === 100_000_001 && - x.evo_min_period && - x.evo_min_period <= values.leasingPeriod && - x.evo_max_period && - x.evo_max_period >= values.leasingPeriod - ); - - return evo_coefficient?.evo_risk_delta || 0; - }, - - async npvniExpected() { - return 0; - }, - - async nsBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_NS_PR' - ); - - return evo_coefficient?.evo_sot_coefficient || 0; - }, - - async nsibBase() { - const discount = await this.discount(); - const plPrice = await this.plPrice(); - const importProgramSum = await this.importProgramSum(); - const insuranceContract = await this.insuranceContract(); - const rats = await this.rats(); - const registration = await this.registration(); - const trackerCost = await this.trackerCost(); - const tLMCost = await this.tLMCost(); - const transportTaxGr = await this.transportTaxGr(); - const tlmCostPaymentSum = await this.tlmCostPaymentSum(); - const gpsCostPaymentSum = await this.gpsCostPaymentSum(); - const leasing0K = await this.leasing0K(); - const firstPaymentSum = await this.firstPaymentSum(); - - const value = - (plPrice - - importProgramSum + - (insuranceContract + - rats + - registration + - trackerCost + - tLMCost + - transportTaxGr + - tlmCostPaymentSum + - gpsCostPaymentSum) * - leasing0K - - firstPaymentSum - - (values.product === 'LEASING0' ? 0 : discount)) * - (1 + VAT); - - return max([NSIB_MAX, value], (v) => v) ?? NSIB_MAX; - }, - - async nsibBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_NSIB_PR' - ); - const nsibBrutto = await this.nsibBrutto(); - - return (evo_coefficient?.evo_sot_coefficient || 0) * nsibBrutto; - }, - - async nsibBrutto() { - if (values.insNSIB) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.insNSIB }, - }); - - const nsibBase = await this.nsibBase(); - const nmper = await this.nmper(); - - return ((((evo_addproduct_type?.evo_graph_price || 0) / 100) * nsibBase) / 12) * nmper; - } - - return 0; - }, - - async nsibBruttoPr() { - return 0; - }, - - async nsibNetto() { - if (values.insNSIB) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.insNSIB }, - }); - - const nsibBase = await this.nsibBase(); - const nmper = await this.nmper(); - - return ( - ((((evo_addproduct_type?.evo_cost_service_provider_withoutnds || 0) / 100) * nsibBase) / - 12) * - nmper - ); - } - - return 0; - }, - - async nsibNettoPr() { - return 0; - }, - - async paymentDateNew() { - return null; - }, - - async plEngineType() { - return values.engineType; - }, - - async plPrice() { - if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { - return ( - (await RUB({ - currencyid: values.supplierCurrency, - value: values.leaseObjectPriceWthtVAT, - })) + - values.addEquipmentPrice / (1 + VAT) - ); - } - - return values.leaseObjectPriceWthtVAT + values.addEquipmentPrice / (1 + VAT); - }, - - async plPriceVAT() { - if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { - return ( - (await RUB({ - currencyid: values.supplierCurrency, - value: values.VATInLeaseObjectPrice, - })) + - (values.addEquipmentPrice - values.addEquipmentPrice / (1 + VAT)) - ); - } - - return ( - values.VATInLeaseObjectPrice + - (values.addEquipmentPrice - values.addEquipmentPrice / (1 + VAT)) - ); - }, - - async plPriceWithVAT() { - if (values.supplierCurrency && transactioncurrency?.isocurrencycode !== 'RUB') { - return ( - (await RUB({ - currencyid: values.supplierCurrency, - value: values.leaseObjectPrice, - })) + values.addEquipmentPrice - ); - } - - return values.leaseObjectPrice + values.addEquipmentPrice; - }, - - async plTypeId() { - return values.leaseObjectType; - }, - - async plYear() { - return values.leaseObjectYear; - }, - - async profitExpected() { - if (values.tarif) { - const { - data: { evo_tarif }, - } = await apolloClient.query({ - query: CRMTypes.GetTarifDocument, - variables: { tarifId: values.tarif }, - }); - - return evo_tarif?.evo_margin_min || 0; - } - - return 0; - }, - - async ratBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'BONUS_RAT_PR' - ); - const rats = await this.rats(); - - return (evo_coefficient?.evo_sot_coefficient || 0) * rats; - }, - - async rats() { - if (values.technicalCard) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.technicalCard }, - }); - - return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; - } - - return 0; - }, - - async regionalDirectorBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS' - ); - - return evo_coefficient?.evo_sot_coefficient || 0; - }, - - async regionalDirectorBonusFinGAP() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS_FINGAP' - ); - const { insCost } = insurance.values.fingap; - - return (evo_coefficient?.evo_sot_coefficient || 0) * insCost; - }, - - async regionalDirectorBonusFix() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS_FIX' - ); - - return evo_coefficient?.evo_sot_coefficient || 0; - }, - - async regionalDirectorBonusNsib() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_BONUS_NSIB' - ); - const nsibBrutto = await this.nsibBrutto(); - - return (evo_coefficient?.evo_sot_coefficient || 0) * nsibBrutto; - }, - - async regionalDirectorExtraBonus() { - const evo_coefficient = evo_coefficients?.find( - (x) => - x?.evo_sot_coefficient_typeidData?.evo_id === 'REGIONAL_DIRECTOR_EXTRA_BONUS' && - Boolean(values.product) && - x.evo_baseproducts?.some((evo_baseproduct) => evo_baseproduct?.evo_id === values.product) - ); - - return evo_coefficient?.evo_sot_coefficient || 0; - }, - - async registration() { - if (values.registration) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.registration }, - }); - - return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; - } - - return 0; - }, - - async repayment() { - return 0.25; - }, - - async retroBonus() { - if (values.technicalCard) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.technicalCard }, - }); - - return evo_addproduct_type?.evo_retro_bonus_withoutnds ?? 0; - } - - return 0; - }, - - async salaryRate() { - const evo_coefficient = evo_coefficients?.find( - (x) => x?.evo_sot_coefficient_typeidData?.evo_id === 'SALARY_RATE' - ); - - return (evo_coefficient?.evo_sot_coefficient || 0) + insurance.values.fingap.insCost; - }, - - async scheduleOfPayments() { - return values.graphType; - }, - - async subsidyPaymentNumber() { - if (values.subsidy) { - const { - data: { evo_subsidy }, - } = await apolloClient.query({ - query: CRMTypes.GetSubsidyDocument, - variables: { subsidyId: values.subsidy }, - }); - - return evo_subsidy?.evo_get_subsidy_payment || 0; - } - - return 0; - }, - - async subsidySum() { - return values.subsidySum / (1 + VAT); - }, - - async supplierFinancing() { - return false; - }, - - async tLMCost() { - if (values.telematic) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.telematic }, - }); - - return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; - } - - return 0; - }, - - async tlmCostPaymentSum() { - return sum(preparedPayments.rows, (p) => p.tlmCostPayment); - }, - - async totalExpected() { - return values.totalPayments + values.subsidySum; - }, - - async trackerCost() { - if (values.tracker) { - const { - data: { evo_addproduct_type }, - } = await apolloClient.query({ - query: CRMTypes.GetAddProductTypeDocument, - variables: { addproductTypeId: values.tracker }, - }); - - return evo_addproduct_type?.evo_graph_price_withoutnds ?? 0; - } - - return 0; - }, - - async transIncludeGr() { - return values.vehicleTaxInYear > 0; - }, - - async transTax() { - return values.vehicleTaxInYear; - }, - - async transportTaxGr() { - return values.vehicleTaxInLeasingPeriod; - }, - - async transportTaxGrYear() { - return values.vehicleTaxInYear; - }, - }; - - const res = await Promise.all( - (Object.keys(getters) as Array).map( - async (key: T) => { - const value = await getters[key](); - - return { [key]: value }; - } - ) - ); - - return Object.assign({}, ...res); -}