139 lines
4.6 KiB
TypeScript
139 lines
4.6 KiB
TypeScript
/* 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
|
|
const QUERY_GET_QUOTE_CONFIGURATOR_DATA = gql`
|
|
query GetQuoteConfiguratorData($quoteId: Uuid!) {
|
|
quote(quoteId: $quoteId) {
|
|
evo_baseproductid
|
|
evo_client_typeid
|
|
evo_msfo_irr
|
|
evo_delivery_time
|
|
evo_first_payment_perc
|
|
evo_last_payment_perc
|
|
evo_leasingobject_typeid
|
|
evo_leasingobject_used
|
|
evo_period
|
|
evo_accept_period
|
|
evo_rateid
|
|
evo_min_change_price
|
|
evo_max_price_change
|
|
evo_floating_rate
|
|
evo_sale_without_nds
|
|
}
|
|
}
|
|
`;
|
|
|
|
export async function getKPData({ values }: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
|
const { quote: quoteId, recalcWithRevision } = values;
|
|
const apolloClient = initializeApollo();
|
|
|
|
const {
|
|
data: { quote },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetQuoteConfiguratorDataDocument,
|
|
variables: {
|
|
quoteId,
|
|
},
|
|
});
|
|
|
|
const leasingPeriod = recalcWithRevision
|
|
? Math.min(quote?.evo_period ?? 0, quote?.evo_accept_period ?? 0)
|
|
: quote?.evo_period ?? 0;
|
|
|
|
const { getTarifs, getRates, getPriceChange } = helper({ apolloClient });
|
|
|
|
let tarif = defaultValues.tarif;
|
|
if (
|
|
quote?.evo_delivery_time !== null &&
|
|
quote?.evo_delivery_time !== undefined &&
|
|
quote?.evo_first_payment_perc !== undefined &&
|
|
quote?.evo_first_payment_perc !== null &&
|
|
quote?.evo_last_payment_perc !== undefined &&
|
|
quote?.evo_last_payment_perc !== null &&
|
|
quote?.evo_leasingobject_typeid !== undefined &&
|
|
quote?.evo_leasingobject_typeid !== null &&
|
|
quote?.evo_leasingobject_used !== undefined &&
|
|
quote?.evo_leasingobject_used !== null
|
|
) {
|
|
const { evo_tarif } = await getTarifs({
|
|
deliveryTime: quote?.evo_delivery_time,
|
|
firstPaymentPerc: quote?.evo_first_payment_perc,
|
|
floatingRate: quote?.evo_floating_rate ?? false,
|
|
lastPaymentPerc: quote?.evo_last_payment_perc,
|
|
leaseObjectType: quote?.evo_leasingobject_typeid,
|
|
leaseObjectUsed: quote?.evo_leasingobject_used,
|
|
leasingPeriod,
|
|
partialVAT: quote?.evo_sale_without_nds ?? false,
|
|
product: quote?.evo_baseproductid,
|
|
});
|
|
|
|
if (evo_tarif?.evo_tarifid) {
|
|
tarif = evo_tarif.evo_tarifid;
|
|
}
|
|
}
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|