2023-02-03 08:30:24 +03:00

70 lines
2.0 KiB
TypeScript

/* eslint-disable @typescript-eslint/naming-convention */
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import * as CRMTypes from 'graphql/crm.types';
import { autorun } from 'mobx';
import type { ReactionsContext } from 'process/types';
dayjs.extend(utc);
export default function computedReactions({ store, apolloClient }: ReactionsContext) {
const { $calculation } = store;
autorun(
async () => {
const supplierCurrencyId = $calculation.element('selectSupplierCurrency').getValue();
const leaseObjectPrice = $calculation.element('tbxLeaseObjectPrice').getValue();
const supplierDiscountRub = $calculation.element('tbxSupplierDiscountRub').getValue();
if (!supplierCurrencyId) {
$calculation.$values.setValue('plPriceRub', leaseObjectPrice);
$calculation.$values.setValue('discountRub', supplierDiscountRub);
return;
}
const {
data: { transactioncurrency },
} = await apolloClient.query({
query: CRMTypes.GetTransactionCurrencyDocument,
variables: {
currencyid: supplierCurrencyId,
},
});
if (transactioncurrency?.isocurrencycode === 'RUB') {
$calculation.$values.setValue('plPriceRub', leaseObjectPrice);
$calculation.$values.setValue('discountRub', supplierDiscountRub);
return;
}
const {
data: { evo_currencychanges },
} = await apolloClient.query({
query: CRMTypes.GetCurrencyChangesDocument,
variables: {
currentDate: dayjs().utc(false).format('YYYY-MM-DD'),
},
});
const evo_currencychange = evo_currencychanges?.find(
(x) => x?.evo_ref_transactioncurrency === supplierCurrencyId
);
$calculation.$values.setValue(
'plPriceRub',
leaseObjectPrice * (evo_currencychange?.evo_currencychange || 0)
);
$calculation.$values.setValue(
'discountRub',
supplierDiscountRub * (evo_currencychange?.evo_currencychange || 0)
);
},
{
delay: 100,
}
);
}