183 lines
5.6 KiB
TypeScript

import { VAT } from '@/constants/values';
import type { ProcessContext } from '@/process/types';
import { disposableReaction } from '@/utils/mobx';
import { reaction } from 'mobx';
import { round } from 'tools';
export default function reactions({ store }: ProcessContext) {
const { $calculation, $process } = store;
reaction(
() => $calculation.element('radioLastPaymentRule').getValue(),
(lastPaymentRule) => {
switch (lastPaymentRule) {
case 100_000_000: {
$calculation.element('tbxLastPaymentPerc').block();
$calculation.element('tbxLastPaymentRub').unblock();
break;
}
case 100_000_001: {
$calculation.element('tbxLastPaymentPerc').unblock();
$calculation.element('tbxLastPaymentRub').block();
break;
}
default: {
$calculation.element('tbxLastPaymentPerc').block();
$calculation.element('tbxLastPaymentRub').block();
}
}
},
{
fireImmediately: true,
}
);
/**
* Расчет размера скидки поставщика в валюте
*/
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.$values.getValues(['leaseObjectPrice', 'supplierDiscountRub']),
({ leaseObjectPrice, supplierDiscountRub }) => {
// NaN fix
if (leaseObjectPrice === 0) {
$calculation.element('tbxSupplierDiscountPerc').resetValue();
} else {
$calculation
.element('tbxSupplierDiscountPerc')
.setValue((supplierDiscountRub / leaseObjectPrice) * 100);
}
},
{
fireImmediately: true,
}
);
/**
* Расчет суммы скидки поставщика в валюте
*/
reaction(
() => $calculation.$values.getValues(['leaseObjectPrice', 'supplierDiscountPerc']),
({ leaseObjectPrice, supplierDiscountPerc }) => {
$calculation
.element('tbxSupplierDiscountRub')
.setValue((supplierDiscountPerc * leaseObjectPrice) / 100);
},
{
fireImmediately: true,
}
);
reaction(
() =>
$calculation.$values.getValues([
'product',
'leaseObjectPrice',
'VATInLeaseObjectPrice',
'partialVAT',
]),
async ({ product: productId, leaseObjectPrice, VATInLeaseObjectPrice, partialVAT }) => {
if (productId && partialVAT) {
$calculation
.element('tbxLeaseObjectPriceWthtVAT')
.setValue(leaseObjectPrice - VATInLeaseObjectPrice);
} else {
const priceWithVAT = round(leaseObjectPrice / (1 + VAT), 2);
const priceVAT = leaseObjectPrice - priceWithVAT;
$calculation.element('tbxLeaseObjectPriceWthtVAT').setValue(priceWithVAT);
$calculation.element('tbxVATInLeaseObjectPrice').setValue(priceVAT);
}
}
);
disposableReaction(
() => $process.has('LoadKP'),
() =>
$calculation.$values.getValues([
'firstPaymentPerc',
'addEquipmentPrice',
'importProgramSum',
'plPriceRub',
]),
({ firstPaymentPerc, addEquipmentPrice, importProgramSum, plPriceRub }) => {
const rub = (firstPaymentPerc * (plPriceRub + addEquipmentPrice - importProgramSum)) / 100;
$calculation.element('tbxFirstPaymentRub').setValue(rub);
}
);
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.element('tbxFirstPaymentRub').getValue(),
(firstPaymentRub) => {
const { plPriceRub, addEquipmentPrice, importProgramSum } = $calculation.$values.getValues();
const perc = (firstPaymentRub / (plPriceRub + addEquipmentPrice - importProgramSum)) * 100;
$calculation.element('tbxFirstPaymentPerc').setValue(perc || 0);
}
);
reaction(
() =>
$calculation.$values.getValues([
'comissionPerc',
'plPriceRub',
'addEquipmentPrice',
'importProgramSum',
]),
({ plPriceRub, comissionPerc, addEquipmentPrice, importProgramSum }) => {
const rub = (comissionPerc * (plPriceRub + addEquipmentPrice - importProgramSum)) / 100;
$calculation.element('tbxComissionRub').setValue(rub);
}
);
disposableReaction(
() => $process.has('LoadKP'),
() => $calculation.element('tbxComissionRub').getValue(),
(comissionRub) => {
const { plPriceRub, addEquipmentPrice, importProgramSum } = $calculation.$values.getValues();
const perc = (comissionRub / (plPriceRub + addEquipmentPrice - importProgramSum)) * 100;
$calculation.element('tbxComissionPerc').setValue(perc || 0);
}
);
disposableReaction(
() => $process.has('LoadKP'),
() =>
$calculation.$values.getValues([
'plPriceRub',
'lastPaymentPerc',
'addEquipmentPrice',
'importProgramSum',
'lastPaymentRule',
]),
({ addEquipmentPrice, lastPaymentPerc, plPriceRub, importProgramSum, lastPaymentRule }) => {
if (lastPaymentRule === 100_000_000) {
return;
}
const rub = (lastPaymentPerc * (plPriceRub + addEquipmentPrice - importProgramSum)) / 100;
$calculation.element('tbxLastPaymentRub').setValue(rub);
}
);
disposableReaction(
() => $process.has('LoadKP'),
() =>
$calculation.$values.getValues([
'plPriceRub',
'lastPaymentRub',
'addEquipmentPrice',
'importProgramSum',
'lastPaymentRule',
]),
({ lastPaymentRub, plPriceRub, addEquipmentPrice, importProgramSum, lastPaymentRule }) => {
if (lastPaymentRule === 100_000_001) {
return;
}
const perc = (lastPaymentRub / (plPriceRub + addEquipmentPrice - importProgramSum)) * 100;
$calculation.element('tbxLastPaymentPerc').setValue(perc || 0);
}
);
}