From 4222d0565c8fbc9235036d5641ab8c448e4e60c9 Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 29 Sep 2020 17:08:30 +0300 Subject: [PATCH] effects: currency --- .../CalculationStore/Effects/lib/tools.js | 48 +++++ .../CalculationStore/Effects/reaction.ts | 188 +++++++++++++++--- 2 files changed, 204 insertions(+), 32 deletions(-) diff --git a/src/client/stores/CalculationStore/Effects/lib/tools.js b/src/client/stores/CalculationStore/Effects/lib/tools.js index a5c7415..0e9ec4a 100644 --- a/src/client/stores/CalculationStore/Effects/lib/tools.js +++ b/src/client/stores/CalculationStore/Effects/lib/tools.js @@ -10,3 +10,51 @@ export function calcSupplierPrice( } return price; } + +export const calculatePerc = calculationStore => ( + supplierCurrencyId, + leaseObjectPrice, + discountRub, + discountPercFieldName, +) => { + // TODO: check evo_currecychange + 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 = calcSupplierPrice( + supplierCurrency?.isocurrencycode, + parseFloat(leaseObjectPrice), + evo_currencychangeValue, + ); + let perc = (discountRub / price) * 100; + calculationStore.setValue(discountPercFieldName, perc); +}; + +export const calculateRub = calculationStore => ( + supplierCurrencyId, + leaseObjectPrice, + discountPerc, + discountRubFieldName, +) => { + // TODO: check evo_currecychange + 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 = calcSupplierPrice( + supplierCurrency?.isocurrencycode, + parseFloat(leaseObjectPrice), + evo_currencychangeValue, + ); + let rub = (discountPerc * price) / 100; + calculationStore.setValue(discountRubFieldName, rub); +}; diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index 8e73cc5..46e2275 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -2,7 +2,7 @@ import { openNotification } from 'client/Elements/Notification'; import CalculationService from 'client/services/CalculationService'; import { IReactionEffect } from 'core/types/effect'; import { Status } from 'core/types/statuses'; -import { calcSupplierPrice } from './lib/tools'; +import { calculatePerc, calculateRub } from './lib/tools'; const reactionEffects: IReactionEffect[] = [ calculationStore => ({ @@ -1551,25 +1551,14 @@ const reactionEffects: IReactionEffect[] = [ if (supplierDiscountRub === undefined) { return; } - // TODO: check evo_currecychange - 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 = calcSupplierPrice( - supplierCurrency?.isocurrencycode, - parseFloat(leaseObjectPrice), - evo_currencychangeValue, + calculatePerc(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + supplierDiscountRub, + 'supplierDiscountPerc', ); - let perc = (supplierDiscountRub / price) * 100; - calculationStore.setValue('supplierDiscountPerc', perc); }, }), - calculationStore => ({ expression: () => { const { @@ -1583,22 +1572,157 @@ const reactionEffects: IReactionEffect[] = [ if (supplierDiscountPerc === undefined) { return; } - // TODO: check evo_currecychange - 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 = calcSupplierPrice( - supplierCurrency?.isocurrencycode, - parseFloat(leaseObjectPrice), - evo_currencychangeValue, + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + supplierDiscountPerc, + 'supplierDiscountRub', + ); + }, + }), + + calculationStore => ({ + expression: () => { + const { firstPaymentRub } = calculationStore.values; + return firstPaymentRub; + }, + effect: firstPaymentRub => { + if (firstPaymentRub === undefined) { + return; + } + const { supplierCurrency, leaseObjectPrice } = calculationStore.values; + calculatePerc(calculationStore)( + supplierCurrency, + leaseObjectPrice, + firstPaymentRub, + 'firstPaymentPerc', + ); + }, + }), + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + firstPaymentPerc, + } = calculationStore.values; + return [supplierCurrency, leaseObjectPrice, firstPaymentPerc]; + }, + effect: ([supplierCurrencyId, leaseObjectPrice, firstPaymentPerc]) => { + if (firstPaymentPerc === undefined) { + return; + } + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + firstPaymentPerc, + 'firstPaymentRub', + ); + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + comissionPerc, + } = calculationStore.values; + return [supplierCurrency, leaseObjectPrice, comissionPerc]; + }, + effect: ([supplierCurrencyId, leaseObjectPrice, comissionPerc]) => { + if (comissionPerc === undefined) { + return; + } + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + comissionPerc, + 'comissionRub', + ); + }, + }), + calculationStore => ({ + expression: () => { + const { comissionRub } = calculationStore.values; + return comissionRub; + }, + effect: comissionRub => { + if (comissionRub === undefined) { + return; + } + const { supplierCurrency, leaseObjectPrice } = calculationStore.values; + calculatePerc(calculationStore)( + supplierCurrency, + leaseObjectPrice, + comissionRub, + 'comissionPerc', + ); + }, + }), + + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + } = calculationStore.values; + return [ + supplierCurrency, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + ]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice, + lastPaymentPerc, + lastPaymentRule, + ]) => { + if (lastPaymentPerc === undefined || lastPaymentRule !== 100000001) { + return; + } + calculateRub(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + lastPaymentPerc, + 'lastPaymentRub', + ); + }, + }), + calculationStore => ({ + expression: () => { + const { + supplierCurrency, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + } = calculationStore.values; + return [ + supplierCurrency, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + ]; + }, + effect: ([ + supplierCurrencyId, + leaseObjectPrice, + lastPaymentRub, + lastPaymentRule, + ]) => { + if (lastPaymentRub === undefined || lastPaymentRule !== 100000000) { + return; + } + calculatePerc(calculationStore)( + supplierCurrencyId, + leaseObjectPrice, + lastPaymentRub, + 'lastPaymentPerc', ); - let rub = (supplierDiscountPerc * price) / 100; - calculationStore.setValue('supplierDiscountRub', rub); }, }), ];