add validation for seasons payments
This commit is contained in:
parent
e0de887c45
commit
cf7189123e
@ -2,7 +2,8 @@
|
||||
/* eslint-disable no-case-declarations */
|
||||
import { counting, max, min, sort } from 'radash/dist/array';
|
||||
import type RootStore from 'stores/root';
|
||||
import { areEqual } from 'tools/array';
|
||||
import { areEqual, isSorted, shift } from 'tools/array';
|
||||
import { SEASONS_PERIODS, SEASONS_PERIOD_NUMBER } from './lib/seasons-constants';
|
||||
|
||||
export default function validatePaymentsTable({ $calculation, $tables }: RootStore) {
|
||||
switch ($calculation.getElementValue('radioGraphType')) {
|
||||
@ -92,12 +93,23 @@ export default function validatePaymentsTable({ $calculation, $tables }: RootSto
|
||||
return 'Не выбран тип сезонности';
|
||||
}
|
||||
|
||||
const highSeasonStart = $calculation.getElementValue('selectHighSeasonStart');
|
||||
if (!highSeasonStart) {
|
||||
const highSeasonStartOption = $calculation.getElementOption('selectHighSeasonStart');
|
||||
if (!highSeasonStartOption) {
|
||||
return 'Не выбрано смещение сезонности';
|
||||
}
|
||||
|
||||
// TODO: seasons validation
|
||||
{
|
||||
const seasons = $tables.payments.values.slice(1, SEASONS_PERIOD_NUMBER + 1);
|
||||
const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2;
|
||||
const unshiftedSeasons = shift(seasons, -shiftNumber);
|
||||
|
||||
const positions = SEASONS_PERIODS[seasonType];
|
||||
const seasonsValues = positions.map((position) => unshiftedSeasons[position]);
|
||||
|
||||
if (isSorted(seasonsValues)) {
|
||||
return 'Сезонные платежи должны убывать';
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@ -25,3 +25,7 @@ export function shift<T>(arr: Array<T>, n: number) {
|
||||
|
||||
return [...arr.slice(-shiftNumber, arr.length), ...arr.slice(0, -shiftNumber)];
|
||||
}
|
||||
|
||||
export function isSorted(arr: Array<number>) {
|
||||
return arr.every((value, index, array) => !index || array[index - 1] <= value);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user