127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
/* eslint-disable sonarjs/cognitive-complexity */
|
|
/* eslint-disable complexity */
|
|
import type { GetQuoteInputData, GetQuoteProcessData } from '../load-kp/types';
|
|
import helper from './lib/helper';
|
|
import defaultValues from '@/config/default-values';
|
|
import calculateHelper from '@/process/calculate/lib/helper';
|
|
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 { normalizeOptions } from '@/utils/entity';
|
|
|
|
export async function getKPData({
|
|
values,
|
|
quote,
|
|
ctx,
|
|
}: GetQuoteInputData): Promise<GetQuoteProcessData> {
|
|
const { apolloClient } = ctx;
|
|
const { recalcWithRevision } = values;
|
|
|
|
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;
|
|
let evo_tarif: unknown = null;
|
|
|
|
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 data = 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 (data?.evo_tarif?.evo_tarifid) {
|
|
tarif = data?.evo_tarif.evo_tarifid;
|
|
}
|
|
|
|
({ evo_tarif } = data);
|
|
}
|
|
|
|
let maxPriceChange = quote?.evo_max_price_change ?? defaultValues.maxPriceChange;
|
|
let minPriceChange = quote?.evo_min_change_price ?? defaultValues.minPriceChange;
|
|
|
|
if (!recalcWithRevision) {
|
|
const kpDataPrice = await getKPDataPrice({ ctx, quote, values });
|
|
const kpDataSubsidy = await getKPDataSubsidy({ ctx, quote, 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 });
|
|
|
|
const { getIrr } = calculateHelper({ apolloClient });
|
|
const irrInfo = await getIrr({
|
|
bonusCoefficient: quote?.evo_coefficien_bonus_reducttion || 0,
|
|
product: quote?.evo_baseproductid || null,
|
|
tarif,
|
|
});
|
|
|
|
return {
|
|
options: {
|
|
selectTarif: evo_tarif ? normalizeOptions([evo_tarif]) : [],
|
|
},
|
|
values: {
|
|
IRR_Perc: quote?.evo_msfo_irr ?? defaultValues.IRR_Perc,
|
|
clientType: quote?.evo_client_typeid,
|
|
floatingRate: quote?.evo_floating_rate ?? false,
|
|
irrInfo,
|
|
maxPriceChange,
|
|
minPriceChange,
|
|
partialVAT: quote?.evo_sale_without_nds ?? false,
|
|
product: quote?.evo_baseproductid,
|
|
rate: evo_rate?.evo_rateid ?? defaultValues.rate,
|
|
tarif,
|
|
},
|
|
};
|
|
}
|