2023-04-21 11:13:58 +03:00

87 lines
2.4 KiB
TypeScript

import type { ProcessContext } from '@/process/types';
import { reaction } from 'mobx';
import { omit } from 'radash';
import { message } from 'ui/elements';
const key = 'KP_LOADING_INFO';
export function common({ store, trpcClient }: ProcessContext) {
const { $calculation, $process, $tables } = store;
reaction(
() => $calculation.$values.getValue('quote'),
() => {
const quote = $calculation.element('selectQuote').getOption();
if (!quote || $process.has('LoadKP')) return;
$process.add('LoadKP');
message.loading({
content: `Загружаем КП ${quote?.label}...`,
key,
});
trpcClient.getQuote
.query({
values: {
quote: quote.value,
...$calculation.$values.getValues(['lead', 'opportunity', 'recalcWithRevision']),
},
})
.then(({ values, payments, insurance, fingap }) => {
$calculation.$values.setValues(
omit(values, [
'lead',
'opportunity',
'quote',
'leadUrl',
'opportunityUrl',
'quoteUrl',
'recalcWithRevision',
'plPriceRub',
'discountRub',
'user',
])
);
$tables.payments.setValues(payments.values);
if (insurance.values.osago) {
$tables.insurance.row('osago').setValues(insurance.values.osago);
}
if (insurance.values.kasko) {
$tables.insurance.row('kasko').setValues(insurance.values.kasko);
}
if (insurance.values.fingap) {
$tables.insurance.row('fingap').setValues(insurance.values.fingap);
}
if (fingap) $tables.fingap.setSelectedKeys(fingap.keys);
message.success({
content: `КП ${quote.label} загружено`,
key,
});
})
.catch(() => {
message.error({
content: `Ошибка во время загрузки КП ${quote.label}`,
key,
});
$calculation.element('selectQuote').resetValue();
})
.finally(() => {
$process.delete('LoadKP');
});
}
);
reaction(
() => $process.has('LoadKP'),
(isLoadKP) => {
$calculation.$status.setStatus('selectQuote', isLoadKP ? 'Disabled' : 'Default');
}
);
}