2023-04-12 12:50:54 +03:00

194 lines
6.0 KiB
TypeScript

import { VAT } from '@/constants/values';
import * as CRMTypes from '@/graphql/crm.types';
import type { ProcessContext } from '@/process/types';
import { disposableReaction } from '@/utils/mobx';
import { reaction } from 'mobx';
import { round } 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,
}
);
/**
* Расчет размера скидки поставщика в валюте
*/
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']),
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 = 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);
}
);
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);
}
);
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);
}
);
}