69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import * as CRMTypes from '@/graphql/crm.types';
|
|
import type { ProcessContext } from '@/process/types';
|
|
import dayjs from 'dayjs';
|
|
import utc from 'dayjs/plugin/utc';
|
|
import { autorun } from 'mobx';
|
|
|
|
dayjs.extend(utc);
|
|
|
|
export default function reactions({ store, apolloClient }: ProcessContext) {
|
|
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,
|
|
}
|
|
);
|
|
}
|