2023-03-16 15:35:44 +03:00

183 lines
5.7 KiB
TypeScript

import { VAT } from '@/constants/values';
import * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import { reaction } from 'mobx';
import { makeDisposable } from 'tools';
export default function reactions({ store, apolloClient }: 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,
}
);
/**
* Расчет размера скидки поставщика в валюте
*/
makeDisposable(
() =>
reaction(
() => $calculation.$values.getValues(['leaseObjectPrice', 'supplierDiscountRub']),
({ leaseObjectPrice, supplierDiscountRub }) => {
$calculation
.element('tbxSupplierDiscountPerc')
.setValue((supplierDiscountRub / leaseObjectPrice) * 100);
},
{
fireImmediately: true,
}
),
() => $process.has('LoadKP')
);
/**
* Расчет суммы скидки поставщика в валюте
*/
reaction(
() => $calculation.$values.getValues(['leaseObjectPrice', 'supplierDiscountPerc']),
({ leaseObjectPrice, supplierDiscountPerc }) => {
$calculation
.element('tbxSupplierDiscountRub')
.setValue((supplierDiscountPerc * leaseObjectPrice) / 100);
},
{
fireImmediately: true,
}
);
reaction(
() => $calculation.$values.getValues(['product', 'leaseObjectPrice', 'VATInLeaseObjectPrice']),
async ({ product: productId, leaseObjectPrice, VATInLeaseObjectPrice }) => {
let evo_sale_without_nds = false;
if (productId) {
const {
data: { evo_baseproduct },
} = await apolloClient.query({
query: CRMTypes.GetProductDocument,
variables: {
productId,
},
});
if (evo_baseproduct?.evo_sale_without_nds) {
evo_sale_without_nds = evo_baseproduct.evo_sale_without_nds;
}
}
if (evo_sale_without_nds) {
$calculation
.element('tbxLeaseObjectPriceWthtVAT')
.setValue(leaseObjectPrice - VATInLeaseObjectPrice);
} else {
const priceWithVAT = leaseObjectPrice / (1 + VAT);
const priceVAT = leaseObjectPrice - priceWithVAT;
$calculation.element('tbxLeaseObjectPriceWthtVAT').setValue(priceWithVAT);
$calculation.element('tbxVATInLeaseObjectPrice').setValue(priceVAT);
}
}
);
reaction(
() =>
$calculation.$values.getValues([
'firstPaymentPerc',
'addEquipmentPrice',
'importProgramSum',
'plPriceRub',
]),
({ firstPaymentPerc, addEquipmentPrice, importProgramSum, plPriceRub }) => {
const rub = (firstPaymentPerc * (plPriceRub + addEquipmentPrice - importProgramSum)) / 100;
$calculation.element('tbxFirstPaymentRub').setValue(rub);
}
);
makeDisposable(
() =>
reaction(
() => $calculation.element('tbxFirstPaymentRub').getValue(),
(firstPaymentRub) => {
const { plPriceRub, addEquipmentPrice, importProgramSum } = $calculation.$values.values;
const perc =
(firstPaymentRub / (plPriceRub + addEquipmentPrice - importProgramSum)) * 100;
$calculation.element('tbxFirstPaymentPerc').setValue(perc);
}
),
() => $process.has('LoadKP')
);
reaction(
() => $calculation.$values.getValues(['comissionPerc', 'plPriceRub']),
({ plPriceRub, comissionPerc }) => {
const rub = (comissionPerc * plPriceRub) / 100;
$calculation.element('tbxComissionRub').setValue(rub);
}
);
makeDisposable(
() =>
reaction(
() => $calculation.element('tbxComissionRub').getValue(),
(comissionRub) => {
const { plPriceRub } = $calculation.$values.values;
const perc = (comissionRub / plPriceRub) * 100;
$calculation.element('tbxComissionPerc').setValue(perc);
}
),
() => $process.has('LoadKP')
);
makeDisposable(
() =>
reaction(
() =>
$calculation.$values.getValues([
'plPriceRub',
'lastPaymentPerc',
'addEquipmentPrice',
'importProgramSum',
]),
({ addEquipmentPrice, lastPaymentPerc, plPriceRub, importProgramSum }) => {
const rub = (lastPaymentPerc * (plPriceRub + addEquipmentPrice - importProgramSum)) / 100;
$calculation.element('tbxLastPaymentRub').setValue(rub);
}
),
() => $calculation.element('radioLastPaymentRule').getValue() === 100_000_000
);
makeDisposable(
() =>
reaction(
() => $calculation.element('tbxLastPaymentRub').getValue(),
(lastPaymentRub) => {
const { plPriceRub, addEquipmentPrice, importProgramSum } = $calculation.$values.values;
const perc = (lastPaymentRub / (plPriceRub + addEquipmentPrice - importProgramSum)) * 100;
$calculation.element('tbxLastPaymentPerc').setValue(perc);
}
),
() =>
$process.has('LoadKP') ||
$calculation.element('radioLastPaymentRule').getValue() === 100_000_001
);
}