From 0f61d4a399041da6f420bbec693d1a6af5b2d554 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Mon, 17 Jul 2023 14:04:58 +0300 Subject: [PATCH] merge branch fix/kp-data_price-change --- apps/web/graphql/crm.types.ts | 4 +- apps/web/process/configurator/get-kp-data.ts | 56 +++++++++++++++++-- apps/web/process/configurator/lib/helper.ts | 56 +++++++++++++++++++ .../process/configurator/reactions/values.ts | 25 ++++++++- apps/web/process/recalc/reactions.ts | 55 +----------------- 5 files changed, 135 insertions(+), 61 deletions(-) diff --git a/apps/web/graphql/crm.types.ts b/apps/web/graphql/crm.types.ts index da2446d..517a5f2 100644 --- a/apps/web/graphql/crm.types.ts +++ b/apps/web/graphql/crm.types.ts @@ -1033,7 +1033,7 @@ export type GetQuoteConfiguratorDataQueryVariables = Exact<{ }>; -export type GetQuoteConfiguratorDataQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_baseproductid: string | null, evo_client_typeid: string | null, evo_msfo_irr: number | null, evo_delivery_time: number | null, evo_first_payment_perc: number | null, evo_last_payment_perc: number | null, evo_leasingobject_typeid: string | null, evo_leasingobject_used: boolean | null, evo_period: number | null, evo_accept_period: number | null, evo_rateid: string | null } | null }; +export type GetQuoteConfiguratorDataQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_baseproductid: string | null, evo_client_typeid: string | null, evo_msfo_irr: number | null, evo_delivery_time: number | null, evo_first_payment_perc: number | null, evo_last_payment_perc: number | null, evo_leasingobject_typeid: string | null, evo_leasingobject_used: boolean | null, evo_period: number | null, evo_accept_period: number | null, evo_rateid: string | null, evo_min_change_price: number | null, evo_max_price_change: number | null } | null }; export type GetQuoteCreateKpDataQueryVariables = Exact<{ quoteId: Scalars['Uuid']; @@ -1197,7 +1197,7 @@ export const GetInsuranceCompaniesDocument = {"kind":"Document","definitions":[{ export const GetRolesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetRoles"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"roleName"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"roles"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"name"},"value":{"kind":"Variable","name":{"kind":"Name","value":"roleName"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"systemusers"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"label"},"name":{"kind":"Name","value":"fullname"}},{"kind":"Field","alias":{"kind":"Name","value":"value"},"name":{"kind":"Name","value":"domainname"}}]}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteAddProductDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteAddProductData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_types"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_product_type"}},{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_typeid"}}]}},{"kind":"Field","name":{"kind":"Name","value":"evo_accept_control_addproduct_typeid"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteBonusDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteBonusData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_sale_bonus"}}]}}]}}]} as unknown as DocumentNode; -export const GetQuoteConfiguratorDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteConfiguratorData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_client_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_msfo_irr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_delivery_time"}},{"kind":"Field","name":{"kind":"Name","value":"evo_first_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_used"}},{"kind":"Field","name":{"kind":"Name","value":"evo_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_accept_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_rateid"}}]}}]}}]} as unknown as DocumentNode; +export const GetQuoteConfiguratorDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteConfiguratorData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_client_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_msfo_irr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_delivery_time"}},{"kind":"Field","name":{"kind":"Name","value":"evo_first_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_typeid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_used"}},{"kind":"Field","name":{"kind":"Name","value":"evo_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_accept_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_rateid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_change_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_price_change"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteCreateKpDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteCreateKPData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_price_with_discount"}},{"kind":"Field","name":{"kind":"Name","value":"evo_price_without_discount_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_cost_increace"}},{"kind":"Field","name":{"kind":"Name","value":"evo_insurance"}},{"kind":"Field","name":{"kind":"Name","value":"evo_registration_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_card_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_nsib_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_redemption_graph"}},{"kind":"Field","name":{"kind":"Name","value":"evo_fingap_quote"}},{"kind":"Field","name":{"kind":"Name","value":"evo_contact_name"}},{"kind":"Field","name":{"kind":"Name","value":"evo_gender"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_redemption"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteEltDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteEltData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_kasko_accountid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_kasko_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id_elt_kasko"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id_kasko_calc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_franchise"}},{"kind":"Field","name":{"kind":"Name","value":"evo_osago_accountid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id_elt_osago"}},{"kind":"Field","name":{"kind":"Name","value":"evo_osago_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_leasingobject_typeid"}}]}}]}}]} as unknown as DocumentNode; export const GetQuoteFingapDataDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuoteFingapData"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_product_risks"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_addproduct_typeid"}}]}}]}}]}}]} as unknown as DocumentNode; diff --git a/apps/web/process/configurator/get-kp-data.ts b/apps/web/process/configurator/get-kp-data.ts index 2e5d736..d8f0ab4 100644 --- a/apps/web/process/configurator/get-kp-data.ts +++ b/apps/web/process/configurator/get-kp-data.ts @@ -1,8 +1,13 @@ +/* eslint-disable sonarjs/cognitive-complexity */ +/* eslint-disable complexity */ import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types'; import helper from './lib/helper'; import initializeApollo from '@/apollo/client'; import defaultValues from '@/config/default-values'; import * as CRMTypes from '@/graphql/crm.types'; +import { getKPData as getKPDataPrice } from '@/process/price/get-kp-data'; +import { getKPData as getKPDataSubsidy } from '@/process/subsidy/get-kp-data'; +import { createCurrencyUtility } from '@/utils/currency'; import { gql } from '@apollo/client'; // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -20,13 +25,14 @@ const QUERY_GET_QUOTE_CONFIGURATOR_DATA = gql` evo_period evo_accept_period evo_rateid + evo_min_change_price + evo_max_price_change } } `; -export async function getKPData({ - values: { quote: quoteId, recalcWithRevision }, -}: GetQuoteInputData): Promise { +export async function getKPData({ values }: GetQuoteInputData): Promise { + const { quote: quoteId, recalcWithRevision } = values; const apolloClient = initializeApollo(); const { @@ -42,7 +48,7 @@ export async function getKPData({ ? Math.min(quote?.evo_period ?? 0, quote?.evo_accept_period ?? 0) : quote?.evo_period ?? 0; - const { getTarifs, getRates } = helper({ apolloClient }); + const { getTarifs, getRates, getPriceChange } = helper({ apolloClient }); let tarif = defaultValues.tarif; if ( @@ -72,12 +78,54 @@ export async function getKPData({ } } + let maxPriceChange = quote?.evo_max_price_change ?? defaultValues.maxPriceChange; + let minPriceChange = quote?.evo_min_change_price ?? defaultValues.minPriceChange; + + if (!recalcWithRevision) { + const kpDataPrice = await getKPDataPrice({ values }); + const kpDataSubsidy = await getKPDataSubsidy({ values }); + + const supplierCurrency = kpDataPrice.values?.supplierCurrency ?? defaultValues.supplierCurrency; + const leaseObjectPrice = kpDataPrice.values?.leaseObjectPrice ?? defaultValues.leaseObjectPrice; + const supplierDiscountRub = + kpDataPrice.values?.supplierDiscountRub ?? defaultValues.supplierDiscountRub; + + const { RUB } = createCurrencyUtility({ apolloClient }); + + let plPriceRub = 0; + if (supplierCurrency && leaseObjectPrice) { + plPriceRub = await RUB({ + currencyid: supplierCurrency, + value: leaseObjectPrice, + }); + } + + let discountRub = 0; + if (supplierCurrency && supplierDiscountRub) { + discountRub = await RUB({ + currencyid: supplierCurrency, + value: supplierDiscountRub, + }); + } + + ({ minPriceChange, maxPriceChange } = await getPriceChange({ + addEquipmentPrice: kpDataPrice.values?.addEquipmentPrice ?? defaultValues.addEquipmentPrice, + discountRub, + importProgramSum: kpDataSubsidy.values?.importProgramSum ?? defaultValues.importProgramSum, + leaseObjectType: quote?.evo_leasingobject_typeid ?? defaultValues.leaseObjectType, + plPriceRub, + recalcWithRevision, + })); + } + const { evo_rate } = await getRates({ tarif }); return { values: { IRR_Perc: quote?.evo_msfo_irr ?? defaultValues.IRR_Perc, clientType: quote?.evo_client_typeid, + maxPriceChange, + minPriceChange, product: quote?.evo_baseproductid, rate: evo_rate?.evo_rateid ?? defaultValues.rate, tarif, diff --git a/apps/web/process/configurator/lib/helper.ts b/apps/web/process/configurator/lib/helper.ts index 324eb8b..a93de4d 100644 --- a/apps/web/process/configurator/lib/helper.ts +++ b/apps/web/process/configurator/lib/helper.ts @@ -1,3 +1,4 @@ +import defaultValues from '@/config/default-values'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import type { CalculationValues } from '@/stores/calculation/values/types'; @@ -17,8 +18,63 @@ type GetTarifInputValues = Pick< type GetRatesInputValues = Pick; +type GetPriceChangeInputValues = Pick< + CalculationValues, + | 'addEquipmentPrice' + | 'discountRub' + | 'importProgramSum' + | 'leaseObjectType' + | 'plPriceRub' + | 'recalcWithRevision' +>; + export default function helper({ apolloClient }: Pick) { return { + async getPriceChange({ + discountRub, + importProgramSum, + addEquipmentPrice, + plPriceRub, + recalcWithRevision, + leaseObjectType: leaseObjectTypeId, + }: GetPriceChangeInputValues) { + if (recalcWithRevision) { + return { + maxPriceChange: defaultValues.maxPriceChange, + minPriceChange: defaultValues.minPriceChange, + }; + } + + let evo_leasingobject_type: CRMTypes.GetLeaseObjectTypeQuery['evo_leasingobject_type'] = null; + + if (leaseObjectTypeId) { + const { data } = await apolloClient.query({ + query: CRMTypes.GetLeaseObjectTypeDocument, + variables: { leaseObjectTypeId }, + }); + + ({ evo_leasingobject_type } = data); + } + + const price = plPriceRub + addEquipmentPrice - importProgramSum; + const maxPriceChange = + price - discountRub < 800_000 + ? price - discountRub + 50_000 + : (price - discountRub) * + (evo_leasingobject_type?.evo_vehicle_type?.includes(100_000_001) || + evo_leasingobject_type?.evo_vehicle_type?.includes(100_000_005) + ? 1.01 + : 1.05); + + const minPriceChange = + price - discountRub < 800_000 ? price - discountRub - 50_000 : (price - discountRub) * 0.95; + + return { + maxPriceChange, + minPriceChange, + }; + }, + async getRates({ tarif: tarifId }: GetRatesInputValues) { const currentDate = dayjs().utc(false).toISOString(); diff --git a/apps/web/process/configurator/reactions/values.ts b/apps/web/process/configurator/reactions/values.ts index 0b42a8c..405d7c2 100644 --- a/apps/web/process/configurator/reactions/values.ts +++ b/apps/web/process/configurator/reactions/values.ts @@ -13,7 +13,7 @@ dayjs.extend(utc); export default function valuesReactions({ store, apolloClient }: ProcessContext) { const { $calculation, $process } = store; - const { getTarifs, getRates } = helper({ apolloClient }); + const { getTarifs, getRates, getPriceChange } = helper({ apolloClient }); disposableReaction( () => $process.has('LoadKP'), @@ -131,6 +131,29 @@ export default function valuesReactions({ store, apolloClient }: ProcessContext) } ); + disposableReaction( + () => $process.has('LoadKP'), + () => + $calculation.$values.getValues([ + 'discountRub', + 'addEquipmentPrice', + 'importProgramSum', + 'plPriceRub', + 'leaseObjectType', + 'recalcWithRevision', + ]), + async (values) => { + const { maxPriceChange, minPriceChange } = await getPriceChange(values); + + $calculation.element('tbxMaxPriceChange').setValue(maxPriceChange); + $calculation.element('tbxMinPriceChange').setValue(minPriceChange); + }, + { + delay: 5, + equals: comparer.shallow, + } + ); + ( [ 'selectProduct', diff --git a/apps/web/process/recalc/reactions.ts b/apps/web/process/recalc/reactions.ts index 61a866f..2eeb8cd 100644 --- a/apps/web/process/recalc/reactions.ts +++ b/apps/web/process/recalc/reactions.ts @@ -1,16 +1,10 @@ import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; import type { Elements } from '@/Components/Calculation/config/map/values'; -import * as CRMTypes from '@/graphql/crm.types'; import { createValidationReaction } from '@/process/tools'; -import { disposableReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; -dayjs.extend(utc); - -export function common({ store, apolloClient }: ProcessContext) { +export function common({ store }: ProcessContext) { const { $calculation, $tables } = store; reaction( @@ -18,53 +12,6 @@ export function common({ store, apolloClient }: ProcessContext) { () => $calculation.element('selectQuote').resetValue() ); - disposableReaction( - () => $calculation.element('cbxRecalcWithRevision').getValue() === true, - () => - $calculation.$values.getValues([ - 'discountRub', - 'addEquipmentPrice', - 'importProgramSum', - 'plPriceRub', - 'leaseObjectType', - ]), - async ({ - discountRub, - importProgramSum, - addEquipmentPrice, - plPriceRub, - leaseObjectType: leaseObjectTypeId, - }) => { - let evo_leasingobject_type: CRMTypes.GetLeaseObjectTypeQuery['evo_leasingobject_type'] = null; - - if (leaseObjectTypeId) { - const { data } = await apolloClient.query({ - query: CRMTypes.GetLeaseObjectTypeDocument, - variables: { leaseObjectTypeId }, - }); - - ({ evo_leasingobject_type } = data); - } - - const price = plPriceRub + addEquipmentPrice - importProgramSum; - const maxPriceChange = - price - discountRub < 800_000 - ? price - discountRub + 50_000 - : (price - discountRub) * - (evo_leasingobject_type?.evo_vehicle_type?.includes(100_000_001) || - evo_leasingobject_type?.evo_vehicle_type?.includes(100_000_005) - ? 1.01 - : 1.05); - - $calculation.element('tbxMaxPriceChange').setValue(maxPriceChange); - - const minPriceChange = - price - discountRub < 800_000 ? price - discountRub - 50_000 : (price - discountRub) * 0.95; - - $calculation.element('tbxMinPriceChange').setValue(minPriceChange); - } - ); - reaction( () => $calculation.$values.getValues(['leaseObjectUsed', 'recalcWithRevision']), ({ leaseObjectUsed, recalcWithRevision }) => {