Created
September 19, 2017 11:53
-
-
Save SuperOleg39/5fef938c7e09ac030d676baf130a9a52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ITerms } from './index'; | |
| export interface ITerms { | |
| totalPrice: number; | |
| initialPayment: number; | |
| periodMonth: number; | |
| monthlyPayment: number; | |
| initialPaymentRangePercent: { | |
| min: number; | |
| max: number; | |
| }; | |
| periodMonthRange: { | |
| min: number; | |
| max: number; | |
| }; | |
| } | |
| const MULTIPLIER: number = 100; | |
| /** | |
| * Изменение первоначального взноса | |
| * - влечет перерасчет ежемесячного платежа | |
| */ | |
| function setInitialPayment(terms: ITerms, payment: number): ITerms { | |
| const newTerms = Object.assign({}, terms); | |
| newTerms.initialPayment = Math.floor(payment); | |
| newTerms.monthlyPayment = Math.floor( | |
| safeDivide((newTerms.totalPrice - newTerms.initialPayment), newTerms.periodMonth), | |
| ); | |
| return newTerms; | |
| } | |
| /** | |
| * Изменение срока кредита | |
| * - влечет перерасчет ежемесячного платежа | |
| */ | |
| function setMonthPeriod(terms: ITerms, period: number): ITerms { | |
| const newTerms = Object.assign({}, terms); | |
| newTerms.periodMonth = Math.floor(period); | |
| newTerms.monthlyPayment = Math.floor( | |
| safeDivide((newTerms.totalPrice - newTerms.initialPayment), newTerms.periodMonth), | |
| ); | |
| return newTerms; | |
| } | |
| /** | |
| * Изменение ежемесячного платежа | |
| * - влечет перерасчет срока кредита | |
| */ | |
| function setMonthlyPayment(terms: ITerms, payment: number): ITerms { | |
| const newTerms = Object.assign({}, terms); | |
| newTerms.periodMonth = Math.floor( | |
| safeDivide((newTerms.totalPrice - newTerms.initialPayment), payment), | |
| ); | |
| newTerms.monthlyPayment = Math.floor( | |
| safeDivide((newTerms.totalPrice - newTerms.initialPayment), newTerms.periodMonth), | |
| ); | |
| return newTerms; | |
| } | |
| /** | |
| * Ограничиваем условия кредита доступными периодами | |
| */ | |
| function applyRanges(terms: ITerms): ITerms { | |
| let newTerms = Object.assign({}, terms); | |
| const minInitialPayment = newTerms.totalPrice * newTerms.initialPaymentRangePercent.min / MULTIPLIER; | |
| const maxInitialPayment = newTerms.totalPrice * newTerms.initialPaymentRangePercent.max / MULTIPLIER; | |
| if (newTerms.initialPayment <= minInitialPayment) { | |
| newTerms = setInitialPayment(newTerms, minInitialPayment); | |
| } else if (newTerms.initialPayment > maxInitialPayment) { | |
| newTerms = setInitialPayment(newTerms, maxInitialPayment); | |
| } | |
| if (newTerms.periodMonth <= newTerms.periodMonthRange.min) { | |
| newTerms = setMonthPeriod(newTerms, newTerms.periodMonthRange.min); | |
| } else if (newTerms.periodMonth > newTerms.periodMonthRange.max) { | |
| newTerms = setMonthPeriod(newTerms, newTerms.periodMonthRange.max); | |
| } | |
| return newTerms; | |
| } | |
| /** | |
| * Возвращает 0 когда делитель равен 0, в ином случае - результат вычисления | |
| */ | |
| function safeDivide(dividend: number, divider: number): number { | |
| return divider === 0 ? 0 : dividend / divider; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment