219 lines
6.1 KiB
TypeScript
219 lines
6.1 KiB
TypeScript
/* eslint-disable sonarjs/cognitive-complexity */
|
|
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';
|
|
import { getCurrentDateString } from '@/utils/date';
|
|
import dayjs from 'dayjs';
|
|
import { first, sort } from 'radash';
|
|
|
|
type GetTarifInputValues = Pick<
|
|
CalculationValues,
|
|
| 'deliveryTime'
|
|
| 'firstPaymentPerc'
|
|
| 'floatingRate'
|
|
| 'lastPaymentPerc'
|
|
| 'leaseObjectType'
|
|
| 'leaseObjectUsed'
|
|
| 'leasingPeriod'
|
|
| 'partialVAT'
|
|
| 'product'
|
|
>;
|
|
|
|
type GetRatesInputValues = Pick<CalculationValues, 'tarif'>;
|
|
|
|
type GetPriceChangeInputValues = Pick<
|
|
CalculationValues,
|
|
| 'addEquipmentPrice'
|
|
| 'discountRub'
|
|
| 'importProgramSum'
|
|
| 'leaseObjectType'
|
|
| 'plPriceRub'
|
|
| 'recalcWithRevision'
|
|
>;
|
|
|
|
export default function helper({ apolloClient }: Pick<ProcessContext, 'apolloClient'>) {
|
|
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 = getCurrentDateString();
|
|
|
|
const {
|
|
data: { evo_rates },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetRatesDocument,
|
|
variables: {
|
|
currentDate,
|
|
},
|
|
});
|
|
|
|
if (!tarifId) {
|
|
return {
|
|
evo_rates,
|
|
};
|
|
}
|
|
|
|
const {
|
|
data: { evo_tarif },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetTarifDocument,
|
|
variables: { tarifId },
|
|
});
|
|
|
|
const filtered_evo_tarif_evo_rates =
|
|
evo_tarif?.evo_rates?.filter((x) => x?.evo_type === 100_000_000) || [];
|
|
|
|
const evo_tarif_evo_rate =
|
|
evo_tarif?.evo_rates &&
|
|
first(sort(filtered_evo_tarif_evo_rates, (x) => dayjs(x?.evo_datefrom).date()));
|
|
|
|
if (evo_tarif_evo_rate) {
|
|
return {
|
|
evo_rate: evo_tarif_evo_rate,
|
|
evo_rates,
|
|
};
|
|
}
|
|
|
|
const evo_rate = evo_rates?.find((x) => x?.evo_id === 'BASE');
|
|
|
|
return {
|
|
evo_rate,
|
|
evo_rates,
|
|
};
|
|
},
|
|
|
|
async getTarifs({
|
|
deliveryTime,
|
|
firstPaymentPerc,
|
|
lastPaymentPerc,
|
|
leaseObjectType: leaseObjectTypeId,
|
|
leaseObjectUsed,
|
|
leasingPeriod,
|
|
product,
|
|
floatingRate,
|
|
partialVAT,
|
|
}: GetTarifInputValues) {
|
|
const currentDate = getCurrentDateString();
|
|
|
|
const {
|
|
data: { evo_tarifs = [] },
|
|
} = await apolloClient.query({
|
|
fetchPolicy: 'network-only',
|
|
query: CRMTypes.GetTarifsDocument,
|
|
variables: {
|
|
currentDate,
|
|
},
|
|
});
|
|
|
|
const evo_tarif = evo_tarifs
|
|
?.filter(
|
|
(tarif) =>
|
|
tarif &&
|
|
product &&
|
|
tarif.evo_baseproductid === product &&
|
|
tarif.evo_min_period !== null &&
|
|
tarif.evo_min_period <= leasingPeriod &&
|
|
tarif.evo_max_period !== null &&
|
|
tarif.evo_max_period >= leasingPeriod &&
|
|
tarif.evo_delivery_time?.includes(deliveryTime) &&
|
|
tarif.evo_min_first_payment !== null &&
|
|
tarif.evo_min_first_payment <= firstPaymentPerc &&
|
|
tarif.evo_max_first_payment !== null &&
|
|
tarif.evo_max_first_payment >= firstPaymentPerc &&
|
|
tarif.evo_min_last_payment !== null &&
|
|
tarif.evo_min_last_payment <= lastPaymentPerc &&
|
|
tarif.evo_max_last_payment !== null &&
|
|
tarif.evo_max_last_payment >= lastPaymentPerc
|
|
)
|
|
.filter(
|
|
(tarif) =>
|
|
!tarif?.evo_leasingobject_types?.length ||
|
|
(leaseObjectTypeId &&
|
|
tarif.evo_leasingobject_types
|
|
.map((x) => x?.evo_leasingobject_typeid)
|
|
.includes(leaseObjectTypeId))
|
|
)
|
|
.filter((tarif) => {
|
|
if (leaseObjectUsed === true) {
|
|
if (!tarif?.evo_pl_use_type?.length || tarif.evo_pl_use_type.includes(100_000_001)) {
|
|
return true;
|
|
}
|
|
} else if (
|
|
!tarif?.evo_pl_use_type?.length ||
|
|
tarif.evo_pl_use_type.includes(100_000_000)
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
})
|
|
.filter((tarif) => {
|
|
if (partialVAT) {
|
|
return (
|
|
tarif?.evo_nds_rules && [100_000_000, 100_000_002].includes(tarif?.evo_nds_rules)
|
|
);
|
|
} else {
|
|
return (
|
|
tarif?.evo_nds_rules && [100_000_000, 100_000_001].includes(tarif?.evo_nds_rules)
|
|
);
|
|
}
|
|
})
|
|
.filter((tarif) => {
|
|
if (floatingRate) {
|
|
return tarif?.evo_floating_rate;
|
|
} else {
|
|
return !tarif?.evo_floating_rate;
|
|
}
|
|
})
|
|
.at(0);
|
|
|
|
return { evo_tarif };
|
|
},
|
|
};
|
|
}
|