diff --git a/src/client/stores/CalculationStore/Effects/actions/calculate/prepareData.ts b/src/client/stores/CalculationStore/Effects/actions/calculate/prepareData.ts index 8734ee1..dfeeb44 100644 --- a/src/client/stores/CalculationStore/Effects/actions/calculate/prepareData.ts +++ b/src/client/stores/CalculationStore/Effects/actions/calculate/prepareData.ts @@ -1,4 +1,4 @@ -import { calcPrice } from '../../lib/tools'; +import { convertPrice } from '../../lib/tools'; import valuesConstants from 'core/constants/values'; import { PaymentRow, PreparedValues } from 'core/types/Calculation/Prepare'; import { IPreparedData } from 'core/types/Calculation/Requests'; @@ -238,7 +238,7 @@ export default ({ ); preparedValues.plPrice = - calcPrice( + convertPrice( supplierCurrency?.isocurrencycode, values.leaseObjectPrice, evo_currencychange?.evo_currencychange || 1, @@ -246,7 +246,7 @@ export default ({ (1 + valuesConstants.VAT); preparedValues.discount = - calcPrice( + convertPrice( supplierCurrency?.isocurrencycode, values.supplierDiscountRub, evo_currencychange?.evo_currencychange || 1, diff --git a/src/client/stores/CalculationStore/Effects/lib/tools.js b/src/client/stores/CalculationStore/Effects/lib/tools.js index 6f64417..7e334b7 100644 --- a/src/client/stores/CalculationStore/Effects/lib/tools.js +++ b/src/client/stores/CalculationStore/Effects/lib/tools.js @@ -1,4 +1,4 @@ -export function calcPrice( +export function convertPrice( isocurrencycode, leaseObjectPrice, evo_currencychange, @@ -10,45 +10,3 @@ export function calcPrice( } return price; } - -export const calculatePerc = calculationStore => ( - supplierCurrencyId, - leaseObjectPrice, - rubSum, -) => { - const supplierCurrency = calculationStore - .getOptions('selectSupplierCurrency') - .find(x => x.transactioncurrencyid === supplierCurrencyId); - const evo_currencychange = calculationStore - .getStaticData('evo_currencychange') - .find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); - const evo_currencychangeValue = - (evo_currencychange && evo_currencychange.evo_currencychange) || 0; - let price = calcPrice( - supplierCurrency.isocurrencycode, - parseFloat(leaseObjectPrice), - evo_currencychangeValue, - ); - return (rubSum / price) * 100; -}; - -export const calculateRub = calculationStore => ( - supplierCurrencyId, - leaseObjectPrice, - perc, -) => { - const supplierCurrency = calculationStore - .getOptions('selectSupplierCurrency') - .find(x => x.transactioncurrencyid === supplierCurrencyId); - const evo_currencychange = calculationStore - .getStaticData('evo_currencychange') - .find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); - const evo_currencychangeValue = - (evo_currencychange && evo_currencychange.evo_currencychange) || 0; - let price = calcPrice( - supplierCurrency.isocurrencycode, - parseFloat(leaseObjectPrice), - evo_currencychangeValue, - ); - return (perc * price) / 100; -}; diff --git a/src/client/stores/CalculationStore/Effects/reactions/index.ts b/src/client/stores/CalculationStore/Effects/reactions/index.ts index 19f0282..a6b13fe 100644 --- a/src/client/stores/CalculationStore/Effects/reactions/index.ts +++ b/src/client/stores/CalculationStore/Effects/reactions/index.ts @@ -1,11 +1,13 @@ import requestReactions from './requestReactions'; import otherReactions from './otherReactions'; +import priceReactions from './priceReactions'; import tablesReactions from './tablesReactions'; import loadKpReaction from './loadKpReaction'; import statusReactions from './statusReactions'; export default [ ...otherReactions, + ...priceReactions, ...requestReactions, ...tablesReactions, ...statusReactions, diff --git a/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts b/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts index c5a6b4a..55481ea 100644 --- a/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts +++ b/src/client/stores/CalculationStore/Effects/reactions/otherReactions.ts @@ -2,8 +2,8 @@ import { openNotification } from 'client/Elements/Notification'; import { IReactionEffect } from 'core/types/Calculation/Store/effect'; import { Process } from 'core/types/Calculation/Store/process'; import { ElementStatus } from 'core/types/statuses'; -import { calcPrice, calculatePerc, calculateRub } from '../lib/tools'; import { round } from 'lodash'; +import { convertPrice } from '../lib/tools'; const reactionEffects: IReactionEffect[] = [ // calculationStore => ({ @@ -1179,265 +1179,6 @@ const reactionEffects: IReactionEffect[] = [ }, }), - calculationStore => ({ - expression: () => { - const { leaseObjectPrice, supplierDiscountRub } = calculationStore.values; - return [leaseObjectPrice, supplierDiscountRub]; - }, - effect: ([leaseObjectPrice, supplierDiscountRub = 0]) => { - calculationStore.setValue( - 'supplierDiscountPerc', - leaseObjectPrice ? (supplierDiscountRub / leaseObjectPrice) * 100 : 0, - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - leaseObjectPrice, - supplierDiscountPerc, - } = calculationStore.values; - return [leaseObjectPrice, supplierDiscountPerc]; - }, - effect: ([leaseObjectPrice = 0, supplierDiscountPerc = 0]) => { - calculationStore.setValue( - 'supplierDiscountRub', - (supplierDiscountPerc * leaseObjectPrice) / 100, - ); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { firstPaymentRub } = calculationStore.values; - return firstPaymentRub; - }, - effect: firstPaymentRub => { - const { supplierCurrency, leaseObjectPrice } = calculationStore.values; - if (leaseObjectPrice) { - const perc = calculatePerc(calculationStore)( - supplierCurrency, - leaseObjectPrice, - firstPaymentRub, - ); - calculationStore.setValue('firstPaymentPerc', perc); - } else { - calculationStore.setValue('firstPaymentPerc', 0); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - firstPaymentPerc, - } = calculationStore.values; - return [supplierCurrency, leaseObjectPrice, firstPaymentPerc]; - }, - effect: ([ - supplierCurrencyId, - leaseObjectPrice = 0, - firstPaymentPerc = 0, - ]) => { - if (!firstPaymentPerc) { - return; - } - const rub = calculateRub(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - firstPaymentPerc, - ); - - calculationStore.setValue('firstPaymentRub', rub); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - comissionPerc, - } = calculationStore.values; - return [supplierCurrency, leaseObjectPrice, comissionPerc]; - }, - effect: ([supplierCurrencyId, leaseObjectPrice = 0, comissionPerc = 0]) => { - const rub = calculateRub(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - comissionPerc, - ); - calculationStore.setValue('comissionRub', rub); - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { comissionRub } = calculationStore.values; - return comissionRub; - }, - effect: comissionRub => { - const { supplierCurrency, leaseObjectPrice } = calculationStore.values; - if (leaseObjectPrice) { - const perc = calculatePerc(calculationStore)( - supplierCurrency, - leaseObjectPrice, - comissionRub, - ); - calculationStore.setValue('comissionPerc', perc); - } else { - calculationStore.setValue('comissionPerc', 0); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - lastPaymentPerc, - lastPaymentRule, - } = calculationStore.values; - return [ - supplierCurrency, - leaseObjectPrice, - lastPaymentPerc, - lastPaymentRule, - ]; - }, - effect: ([ - supplierCurrencyId, - leaseObjectPrice, - lastPaymentPerc = 0, - lastPaymentRule, - ]) => { - if (lastPaymentRule !== 100000001) { - return; - } - - const rub = calculateRub(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - lastPaymentPerc, - ); - calculationStore.setValue('lastPaymentRub', rub); - }, - options: { - fireImmediately: true, - }, - }), - calculationStore => ({ - expression: () => { - const { - supplierCurrency, - leaseObjectPrice, - lastPaymentRub, - lastPaymentRule, - } = calculationStore.values; - return [ - supplierCurrency, - leaseObjectPrice, - lastPaymentRub, - lastPaymentRule, - ]; - }, - effect: ([ - supplierCurrencyId, - leaseObjectPrice, - lastPaymentRub = 0, - lastPaymentRule, - ]) => { - if (lastPaymentRule !== 100000000) { - return; - } - if (leaseObjectPrice) { - const perc = calculatePerc(calculationStore)( - supplierCurrencyId, - leaseObjectPrice, - lastPaymentRub, - ); - calculationStore.setValue('lastPaymentPerc', perc); - } else { - calculationStore.setValue('lastPaymentPerc', 0); - } - }, - options: { - fireImmediately: true, - }, - }), - - calculationStore => ({ - expression: () => { - const { supplierDiscountRub } = calculationStore.values; - return supplierDiscountRub; - }, - effect: supplierDiscountRub => { - const { leaseObjectPrice } = calculationStore.values; - if ( - leaseObjectPrice && - leaseObjectPrice > 0 && - supplierDiscountRub >= leaseObjectPrice - ) { - calculationStore.setValidation('tbxSupplierDiscountRub', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Скидка от поставщика не может быть больше или равна стоимости ПЛ.', - })(); - } else { - calculationStore.setValidation('tbxSupplierDiscountRub', undefined); - } - }, - }), - - calculationStore => ({ - expression: () => { - const { firstPaymentRub } = calculationStore.values; - return firstPaymentRub; - }, - effect: firstPaymentRub => { - const { leaseObjectPrice } = calculationStore.values; - if ( - leaseObjectPrice && - leaseObjectPrice > 0 && - firstPaymentRub >= leaseObjectPrice - ) { - calculationStore.setValidation('tbxFirstPaymentRub', false); - openNotification({ - type: 'error', - title: 'Ошибка', - description: - 'Первый платеж не может быть больше или равен стоимости ПЛ.', - })(); - } else { - calculationStore.setValidation('tbxFirstPaymentRub', undefined); - } - }, - }), - calculationStore => ({ expression: () => { const { @@ -1473,7 +1214,7 @@ const reactionEffects: IReactionEffect[] = [ ?.find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); const evo_currencychangeValue = (evo_currencychange && evo_currencychange.evo_currencychange) || 0; - let price = calcPrice( + let price = convertPrice( supplierCurrency?.isocurrencycode, leaseObjectPrice, evo_currencychangeValue, @@ -1535,7 +1276,7 @@ const reactionEffects: IReactionEffect[] = [ ?.find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); const evo_currencychangeValue = (evo_currencychange && evo_currencychange.evo_currencychange) || 0; - let price = calcPrice( + let price = convertPrice( supplierCurrency?.isocurrencycode, leaseObjectPrice, evo_currencychangeValue, diff --git a/src/client/stores/CalculationStore/Effects/reactions/priceReactions/calculate.js b/src/client/stores/CalculationStore/Effects/reactions/priceReactions/calculate.js new file mode 100644 index 0000000..1a5cc45 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/priceReactions/calculate.js @@ -0,0 +1,43 @@ +import { convertPrice } from '../../lib/tools'; + +export const calculatePerc = calculationStore => ( + supplierCurrencyId, + leaseObjectPrice, + rubSum, +) => { + const supplierCurrency = calculationStore + .getOptions('selectSupplierCurrency') + .find(x => x.transactioncurrencyid === supplierCurrencyId); + const evo_currencychange = calculationStore + .getStaticData('evo_currencychange') + .find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); + const evo_currencychangeValue = + (evo_currencychange && evo_currencychange.evo_currencychange) || 0; + let price = convertPrice( + supplierCurrency.isocurrencycode, + parseFloat(leaseObjectPrice), + evo_currencychangeValue, + ); + return (rubSum / price) * 100; +}; + +export const calculateRub = calculationStore => ( + supplierCurrencyId, + leaseObjectPrice, + perc, +) => { + const supplierCurrency = calculationStore + .getOptions('selectSupplierCurrency') + .find(x => x.transactioncurrencyid === supplierCurrencyId); + const evo_currencychange = calculationStore + .getStaticData('evo_currencychange') + .find(x => x.evo_ref_transactioncurrency === supplierCurrencyId); + const evo_currencychangeValue = + (evo_currencychange && evo_currencychange.evo_currencychange) || 0; + let price = convertPrice( + supplierCurrency.isocurrencycode, + parseFloat(leaseObjectPrice), + evo_currencychangeValue, + ); + return (perc * price) / 100; +}; diff --git a/src/client/stores/CalculationStore/Effects/reactions/priceReactions/index.ts b/src/client/stores/CalculationStore/Effects/reactions/priceReactions/index.ts new file mode 100644 index 0000000..0ccc53c --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/reactions/priceReactions/index.ts @@ -0,0 +1,264 @@ +import { openNotification } from 'client/Elements/Notification'; +import { IReactionEffect } from 'core/types/Calculation/Store/effect'; +import { calculatePerc, calculateRub } from './calculate'; + +export default [ + calculationStore => ({ + expression: () => { + const { leaseObjectPrice, supplierDiscountRub } = calculationStore.values; + return [leaseObjectPrice, supplierDiscountRub]; + }, + effect: ([leaseObjectPrice, supplierDiscountRub = 0]) => { + calculationStore.setValue( + 'supplierDiscountPerc', + leaseObjectPrice ? (supplierDiscountRub / leaseObjectPrice) * 100 : 0, + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + leaseObjectPrice, + supplierDiscountPerc, + } = calculationStore.values; + return [leaseObjectPrice, supplierDiscountPerc]; + }, + effect: ([leaseObjectPrice = 0, supplierDiscountPerc = 0]) => { + calculationStore.setValue( + 'supplierDiscountRub', + (supplierDiscountPerc * leaseObjectPrice) / 100, + ); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { firstPaymentRub } = calculationStore.values; + return firstPaymentRub; + }, + effect: firstPaymentRub => { + const { supplierCurrency, leaseObjectPrice } = calculationStore.values; + if (leaseObjectPrice) { + const perc = calculatePerc(calculationStore)( + supplierCurrency, + leaseObjectPrice, + firstPaymentRub, + ); + calculationStore.setValue('firstPaymentPerc', perc); + } else { + calculationStore.setValue('firstPaymentPerc', 0); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + firstPaymentPerc, + } = calculationStore.values; + return [supplierCurrency, leaseObjectPrice, firstPaymentPerc]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice = 0, + firstPaymentPerc = 0, + ]) => { + if (!firstPaymentPerc) { + return; + } + const rub = calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + firstPaymentPerc, + ); + + calculationStore.setValue('firstPaymentRub', rub); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + comissionPerc, + } = calculationStore.values; + return [supplierCurrency, leaseObjectPrice, comissionPerc]; + }, + effect: ([supplierCurrencyId, leaseObjectPrice = 0, comissionPerc = 0]) => { + const rub = calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + comissionPerc, + ); + calculationStore.setValue('comissionRub', rub); + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { comissionRub } = calculationStore.values; + return comissionRub; + }, + effect: comissionRub => { + const { supplierCurrency, leaseObjectPrice } = calculationStore.values; + if (leaseObjectPrice) { + const perc = calculatePerc(calculationStore)( + supplierCurrency, + leaseObjectPrice, + comissionRub, + ); + calculationStore.setValue('comissionPerc', perc); + } else { + calculationStore.setValue('comissionPerc', 0); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + } = calculationStore.values; + return [ + supplierCurrency, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + ]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice, + lastPaymentPerc = 0, + lastPaymentRule, + ]) => { + if (lastPaymentRule !== 100000001) { + return; + } + + const rub = calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + lastPaymentPerc, + ); + calculationStore.setValue('lastPaymentRub', rub); + }, + options: { + fireImmediately: true, + }, + }), + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + } = calculationStore.values; + return [ + supplierCurrency, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + ]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice, + lastPaymentRub = 0, + lastPaymentRule, + ]) => { + if (lastPaymentRule !== 100000000) { + return; + } + if (leaseObjectPrice) { + const perc = calculatePerc(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + lastPaymentRub, + ); + calculationStore.setValue('lastPaymentPerc', perc); + } else { + calculationStore.setValue('lastPaymentPerc', 0); + } + }, + options: { + fireImmediately: true, + }, + }), + + calculationStore => ({ + expression: () => { + const { supplierDiscountRub } = calculationStore.values; + return supplierDiscountRub; + }, + effect: supplierDiscountRub => { + const { leaseObjectPrice } = calculationStore.values; + if ( + leaseObjectPrice && + leaseObjectPrice > 0 && + supplierDiscountRub >= leaseObjectPrice + ) { + calculationStore.setValidation('tbxSupplierDiscountRub', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Скидка от поставщика не может быть больше или равна стоимости ПЛ.', + })(); + } else { + calculationStore.setValidation('tbxSupplierDiscountRub', undefined); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { firstPaymentRub } = calculationStore.values; + return firstPaymentRub; + }, + effect: firstPaymentRub => { + const { leaseObjectPrice } = calculationStore.values; + if ( + leaseObjectPrice && + leaseObjectPrice > 0 && + firstPaymentRub >= leaseObjectPrice + ) { + calculationStore.setValidation('tbxFirstPaymentRub', false); + openNotification({ + type: 'error', + title: 'Ошибка', + description: + 'Первый платеж не может быть больше или равен стоимости ПЛ.', + })(); + } else { + calculationStore.setValidation('tbxFirstPaymentRub', undefined); + } + }, + }), +] as IReactionEffect[];