From a7e794486c3eb47e97662fd953fd61b123111283 Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 7 Oct 2020 22:51:57 +0300 Subject: [PATCH] effects: payments table x season graph (OMG!) --- .../CalculationStore/Effects/reaction.ts | 361 ++++++++++++------ .../config/initialTables/tablePayments.ts | 17 + src/core/tools/array.js | 15 + 3 files changed, 281 insertions(+), 112 deletions(-) create mode 100644 src/core/tools/array.js diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index a4a3cc9..9ea33dd 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -1,7 +1,10 @@ import { openNotification } from 'client/Elements/Notification'; import CalculationService from 'client/services/CalculationService'; +import { shift, shiftRight } from 'core/tools/array'; import { IReactionEffect } from 'core/types/effect'; import { Status } from 'core/types/statuses'; +import { ITableCell, TableProps } from 'core/types/tables'; +import { toJS } from 'mobx'; import { calcPrice, calculatePerc, calculateRub } from './lib/tools'; const reactionEffects: IReactionEffect[] = [ @@ -2204,6 +2207,49 @@ const reactionEffects: IReactionEffect[] = [ calculationStore => ({ expression: () => { + const { graphType, leasingPeriod } = calculationStore.values; + return { graphType, leasingPeriod }; + }, + effect: ({ graphType, leasingPeriod }) => { + if (graphType === 100000003 && leasingPeriod < 14) { + calculationStore.setValue('leasingPeriod', 14); + openNotification({ + type: 'warning', + title: 'Внимание', + description: + 'При сезонном графике срок лизинга должен быть больше 14 месяцев', + })(); + } + }, + }), + + calculationStore => ({ + expression: () => { + const { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart: highSeasonStartId, + firstPaymentPerc, + lastPaymentPerc, + } = calculationStore.values; + + const highSeasonStart = calculationStore.options.selectHighSeasonStart?.find( + x => x.value === highSeasonStartId, + ); + + return { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart: parseInt(highSeasonStart?.name || '2'), + firstPaymentPerc, + lastPaymentPerc, + }; + }, + effect: async (nextParams, prevParams) => { const { leasingPeriod, graphType, @@ -2212,100 +2258,69 @@ const reactionEffects: IReactionEffect[] = [ highSeasonStart, firstPaymentPerc, lastPaymentPerc, - } = calculationStore.values; - return { - leasingPeriod, - graphType, - parmentsDecreasePercent, - seasonType, - highSeasonStart, - firstPaymentPerc, - lastPaymentPerc, - }; - }, - effect: ({ - leasingPeriod, - graphType, - parmentsDecreasePercent, - seasonType, - highSeasonStart, - firstPaymentPerc, - lastPaymentPerc, - }) => { - calculationStore.cleanTable('tablePayments'); - calculationStore.setTableRow( - 'tablePayments', - 0, - )({ - paymentRelation: { - value: firstPaymentPerc, - status: Status.Disabled, + } = nextParams; + + const prevValues = toJS(calculationStore.tables.tablePayments.rows).map( + x => x.paymentRelation?.value, + ); + let payments: TableProps[] = [ + { + paymentRelation: { + value: firstPaymentPerc, + status: Status.Disabled, + }, }, - }); + ]; + + calculationStore.cleanTable('tablePayments'); switch (graphType) { case 100000000: { - calculationStore.setTableRows( - 'tablePayments', - 1, - )( - Array.from({ length: leasingPeriod - 2 }, () => ({ - paymentRelation: { - value: 100, - status: Status.Disabled, - }, - })), - ); - break; - } - - case 100000001: { - calculationStore.setTableRow( - 'tablePayments', - 1, - )({ - paymentRelation: { - value: 100, - status: Status.Disabled, - }, - }); - - calculationStore.setTableRows( - 'tablePayments', - 2, - )( - Array.from({ length: leasingPeriod - 3 }, () => ({ - paymentRelation: { - value: 100, - status: Status.Default, - }, - })), - ); - - break; - } - - case 100000002: { - calculationStore.setTableRow( - 'tablePayments', - 1, - )({ - paymentRelation: { - value: 100, - status: Status.Disabled, - }, - }); - - const rows = Array.from({ length: leasingPeriod - 3 }, (v, i) => ({ + const middleRows = Array.from({ length: leasingPeriod - 2 }, () => ({ paymentRelation: { value: 100, status: Status.Disabled, }, })); + payments = [...payments, ...middleRows]; - for (let i in rows) { - const currRow = rows[parseInt(i)]; - const prevRow = rows[parseInt(i) - 1]; + break; + } + + case 100000001: { + const middleRows = Array.from({ length: leasingPeriod - 3 }, () => ({ + paymentRelation: { + value: 100, + status: Status.Default, + }, + })); + payments = [ + ...payments, + { + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + }, + ...middleRows, + ]; + + break; + } + + case 100000002: { + const middleRows = Array.from( + { length: leasingPeriod - 3 }, + (v, i) => ({ + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + }), + ); + for (let i in middleRows) { + const currRow = middleRows[parseInt(i)]; + const prevRow = middleRows[parseInt(i) - 1]; currRow.paymentRelation.value = parseFloat( ( ((prevRow ? prevRow.paymentRelation.value : 100) * @@ -2314,16 +2329,150 @@ const reactionEffects: IReactionEffect[] = [ ).toFixed(2), ); } + payments = [ + ...payments, + { + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + }, + ...middleRows, + ]; + + break; + } + + case 100000003: { + let HIGH = 100, + MIDDLE = 75, + LOW = 50; + + if ( + prevParams.graphType === nextParams.graphType + // && nextParams.graphType === 100000003 + ) { + /** + * FIND PREV HIGH, MIDDLE, LOW + */ + const { + leasingPeriod: prevLeasingPeriod, + seasonType: prevSeasonType, + highSeasonStart: prevHighSeasonStart, + } = prevParams; + + const prevPeriodsNumber = + prevLeasingPeriod <= 14 ? prevLeasingPeriod - 2 : 12; + const prevShiftNumber = prevHighSeasonStart - 2; + + let middleRows = prevValues.slice(1, prevPeriodsNumber + 1); + if (middleRows.length < 12) { + middleRows = [ + ...middleRows, + ...Array.from({ length: 12 - middleRows.length }, v => 0), + ]; + } + if (prevShiftNumber > 0) + middleRows = shiftRight(middleRows, prevShiftNumber); + + switch (prevSeasonType) { + // 6/6 + case 100000000: { + HIGH = middleRows[0]; + LOW = middleRows[6]; + break; + } + // 8/4 + case 100000001: { + HIGH = middleRows[0]; + LOW = middleRows[8]; + + break; + } + // 4/4/4 + case 100000002: { + HIGH = middleRows[0]; + MIDDLE = middleRows[4]; + LOW = middleRows[8]; + break; + } + } + /** */ + } + + /** + * GENERATE PERIODS + */ + + const { + leasingPeriod: nextLeasingPeriod, + seasonType: nextSeasonType, + highSeasonStart: nextHighSeasonStart, + } = nextParams; + + const nextPeriodsNumber = + nextLeasingPeriod <= 14 ? nextLeasingPeriod - 2 : 12; + const nextShiftNumber = nextHighSeasonStart - 2; + + let nextMiddleValues: number[] = []; + + switch (nextSeasonType) { + // 6/6 + case 100000000: { + nextMiddleValues = Array.from({ length: 12 }, (v, i) => + i < 6 ? HIGH : LOW, + ); + break; + } + // 8/4 + case 100000001: { + nextMiddleValues = Array.from({ length: 12 }, (v, i) => + i < 8 ? HIGH : LOW, + ); + break; + } + // 4/4/4 + case 100000002: { + nextMiddleValues = Array.from( + { length: 12 }, + (v, i) => (i < 4 && HIGH) || (i < 8 && MIDDLE) || LOW, + ); + break; + } + } + if (nextShiftNumber > 0) { + nextMiddleValues = shift(nextMiddleValues, nextShiftNumber); + } + nextMiddleValues.length = nextPeriodsNumber; + + const middleRows = Array.from( + { length: nextLeasingPeriod - 2 }, + (v, i) => { + return { + paymentRelation: { + value: nextMiddleValues[i] || nextMiddleValues[i - 12], + status: + i < nextPeriodsNumber ? Status.Default : Status.Disabled, + }, + }; + }, + ); + + payments = [...payments, ...middleRows]; - calculationStore.setTableRows('tablePayments', 2)(rows); break; } case 100000004: { - calculationStore.setTableRows( - 'tablePayments', - 1, - )([ + const middleRows = Array.from({ length: leasingPeriod - 5 }, () => ({ + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + })); + + payments = [ + ...payments, { paymentRelation: { value: 25, @@ -2342,20 +2491,8 @@ const reactionEffects: IReactionEffect[] = [ status: Status.Default, }, }, - ]); - - calculationStore.setTableRows( - 'tablePayments', - 4, - )( - Array.from({ length: leasingPeriod - 5 }, () => ({ - paymentRelation: { - value: 100, - status: Status.Disabled, - }, - })), - ); - + ...middleRows, + ]; break; } @@ -2364,17 +2501,17 @@ const reactionEffects: IReactionEffect[] = [ } } - calculationStore.setTableRow( - 'tablePayments', - calculationStore.tables.tablePayments.rows.length, - )({ - paymentRelation: { - value: lastPaymentPerc, - status: Status.Disabled, + payments = [ + ...payments, + { + paymentRelation: { + value: lastPaymentPerc, + status: Status.Disabled, + }, }, - }); + ]; - console.log(calculationStore.tables.tablePayments); + calculationStore.setTableRows('tablePayments', 0)(payments); }, options: { fireImmediately: true, diff --git a/src/core/config/initialTables/tablePayments.ts b/src/core/config/initialTables/tablePayments.ts index 39fc004..9e6a61b 100644 --- a/src/core/config/initialTables/tablePayments.ts +++ b/src/core/config/initialTables/tablePayments.ts @@ -85,6 +85,23 @@ const tablePayments: ITable = { } } } + + if (graphType === 100000003) { + const { leasingPeriod } = calculationStore.values; + if (rowIndex >= 1 && rowIndex <= 12) { + for (let i = rowIndex; i < leasingPeriod - 1; i += 12) { + calculationStore.setTableRow( + tableName, + i, + )({ + paymentRelation: { + value, + }, + }); + } + } + } + if (graphType === 100000004) { if (rowIndex > 0 && rowIndex < 4) { for (let i in calculationStore.tables[tableName].rows) { diff --git a/src/core/tools/array.js b/src/core/tools/array.js new file mode 100644 index 0000000..ec9907f --- /dev/null +++ b/src/core/tools/array.js @@ -0,0 +1,15 @@ +export function shift(arr, n) { + for (let i = 0; i < n; i++) { + var popped = arr.pop(); + arr.splice(0, 0, popped); + } + return arr; +} + +export function shiftRight(arr, n) { + for (let i = 0; i < n; i++) { + var shifted = arr.shift(); + arr.splice(arr.length - 1, 0, shifted); + } + return arr; +}