process/payments: fix seasons payments

This commit is contained in:
Chika 2022-10-14 12:59:16 +03:00
parent ab89ca3475
commit 5a25b342f6
3 changed files with 31 additions and 33 deletions

View File

@ -1,20 +1,25 @@
import { getSeasonsIndex } from '../seasons';
import { getPositionIndex } from '../seasons';
describe('process/payments/lib/seasons', () => {
describe('[function] getSeasonsIndex', () => {
describe('[function] getPositionIndex', () => {
test('should return 0', () => {
const result = getSeasonsIndex(0);
const result = getPositionIndex(100_000_001, 0);
expect(result).toEqual(0);
});
test('should return 7', () => {
const result = getSeasonsIndex(7);
expect(result).toEqual(7);
test('should return 0', () => {
const result = getPositionIndex(100_000_001, 7);
expect(result).toEqual(0);
});
test('should return 7', () => {
const result = getSeasonsIndex(55);
expect(result).toEqual(7);
test('should return 1', () => {
const result = getPositionIndex(100_000_001, 8);
expect(result).toEqual(1);
});
test('should return 1', () => {
const result = getPositionIndex(100_000_001, 11);
expect(result).toEqual(1);
});
});
});

View File

@ -11,10 +11,6 @@ const SEASONS_PERIODS: Record<number, Array<number>> = {
};
export const SEASONS_PERIOD_NUMBER = 12;
export function getSeasonsIndex(paymentsIndex: number) {
return paymentsIndex - Math.floor(paymentsIndex / SEASONS_PERIOD_NUMBER) * SEASONS_PERIOD_NUMBER;
}
export function getPositionIndex(seasonType: SeasonType, seasonsIndex: number) {
let positionIndex = 0;

View File

@ -9,6 +9,7 @@ import type RootStore from 'stores/root';
import type { Row } from 'stores/tables/payments/types';
import { difference, shift } from 'tools/array';
import * as seasonsTools from './lib/seasons';
import { SEASONS_PERIOD_NUMBER } from './lib/seasons';
import validatePaymentsTable from './validation';
export default function paymentsReactions(
@ -409,11 +410,10 @@ export default function paymentsReactions(
leasingPeriod,
shift(seasons, shiftNumber)
);
const rows: Row[] = payments.map((value) => ({
const rows: Row[] = payments.map((value, i) => ({
value,
status: 'Default',
status: i < SEASONS_PERIOD_NUMBER ? 'Default' : 'Disabled',
}));
const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc');
const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc');
@ -434,10 +434,11 @@ export default function paymentsReactions(
reaction(
() => {
const payments = toJS($tables.payments.values);
const seasons = payments.slice(1, SEASONS_PERIOD_NUMBER + 1);
return payments;
return seasons;
},
(nextPayments, prevPayments) => {
(nextSeasons, prevSeasons) => {
const graphType = $calculation.getElementValue('radioGraphType');
if (graphType !== 100_000_003) return;
@ -446,21 +447,17 @@ export default function paymentsReactions(
if (!seasonType || !highSeasonStartOption) return;
const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2;
const unshiftedNextPayments = shift(nextPayments.slice(1, -1), -shiftNumber);
const unshiftedPrevPayments = shift(prevPayments.slice(1, -1), -shiftNumber);
const unshiftedNextSeasons = shift(nextSeasons, -shiftNumber);
const unshiftedPrevSeasons = shift(prevSeasons, -shiftNumber);
const changes = difference(unshiftedNextPayments, unshiftedPrevPayments);
const changes = difference(unshiftedNextSeasons, unshiftedPrevSeasons);
if (changes === null || changes.length > 1) return;
const [changeIndex] = changes;
const positionIndex = seasonsTools.getPositionIndex(seasonType, changeIndex);
const nextSeasons = unshiftedNextPayments.slice(0, seasonsTools.SEASONS_PERIOD_NUMBER + 1);
const values = seasonsTools.getSeasonsValues(seasonType, nextSeasons);
const positionIndex = seasonsTools.getPositionIndex(
seasonType,
seasonsTools.getSeasonsIndex(changeIndex)
);
values[positionIndex] = unshiftedNextPayments[changeIndex];
const values = seasonsTools.getSeasonsValues(seasonType, unshiftedNextSeasons);
values[positionIndex] = unshiftedNextSeasons[changeIndex];
const seasons = seasonsTools.generateSeasons(seasonType, values);
const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod');
@ -468,11 +465,11 @@ export default function paymentsReactions(
leasingPeriod,
shift(seasons, shiftNumber)
);
const rows: Row[] = payments.map((value) => ({
value,
status: 'Default',
}));
const rows: Row[] = payments.map((value, i) => ({
value,
status: i < SEASONS_PERIOD_NUMBER ? 'Default' : 'Disabled',
}));
const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc');
const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc');