-
Notifications
You must be signed in to change notification settings - Fork 7
/
temp2.js
689 lines (634 loc) · 24.4 KB
/
temp2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
var Q, _, moment;
function service() {
this.presentValueOfLumpSum = presentValueOfLumpSum;
this.numberOfPayments = numberOfPayments;
this.paymentAmount = paymentAmount;
this.firstPaymentDate = firstPaymentDate;
this.addAmorizationTable = addAmorizationTable;
this.isLoanPastDue = isLoanPastDue;
this.earnedAmount = earnedAmount;
this.cumulativeInterestPaid = cumulativeInterestPaid;
this.nextPaymentDate = nextPaymentDate;
this.dateLastPaymentShouldHaveBeenMade = dateLastPaymentShouldHaveBeenMade;
this.dateLastPaymentWasReceived = dateLastPaymentWasReceived;
this.outstandingPrincipal = outstandingPrincipal;
this.aggregateLateFees = aggregateLateFees;
this.interestAllocationForPayment = interestAllocationForPayment;
}
if (typeof module !== 'undefined' && module.exports) {
Q = require('q');
_ = require('lodash');
moment = require('moment');
module.exports = new service;
} else if (angular) {
angular.module('FinanceJs', [])
.factory('_', function () {
return window._;
})
.factory('moment', function () {
return window.moment;
})
.factory('FinanceJSService', ['$q', '_', 'moment', function ($q, _, moment) {
Q = $q;
_ = _;
moment = moment;
var temp = {};
temp.paymentAmount = function(loan){
}
return temp;
}]);
} else {
window.financejs = service;
}
/*
presentValueOfLumpSum
-----------
Calculates the present value of a lump sum received in the future. Params include:
* rate (required) - the interest rate per period
* NPER (required) - total number of periods
* FV (required) - the future value or lump sum to be received
*/
function presentValueOfLumpSum(params) {
var d = Q.defer();
if (!params || _.isEmpty(loan.rate) || _.isEmpty(loan.NPER) || _.isEmpty(loan.FV)) {
d.reject(new Error('params object not provided or params does not include rate, NPER and FV'))
} else {
var result = FV / Math.pow(1 + rate, NPER);
d.resolve(result);
}
if (callback) return d.promise.nodeify(callback);
return d.promise;
}
/*
Payments
--------
Calculates the number of payments for a loan. This is different than NPER.
NPER calculates the number of periods used in an annuity or loan from
a financial perspective. This function looks at how frequently a customer
chooses to make payments. This function has the following arguments within params:
* term (required) - the number of periods used in calculating interest for a loan
* frequency (required): the payment frequency, which can be any of the following:
- semimonthly - twice a month
- monthly - once each month
- bimonthly - every two months
- quarterly - every quarter
- semiannually - ever 6 months
- annually - ever 12 months
- none or one - only one payment at the end of the loan - typically don't mix this with balloonDate
* callback (optional) - function of CommonJs/NodeJs return, e.g., function(err, result)
*/
function numberOfPayments(loan) {
var d = Q.defer();
if (!loan || !loan.term || !loan.frequency) {
d.reject(new Error('improper parameters'));
} else {
var frequency = loan.frequency.toLowerCase();
var term = loan.term ? Number(loan.term) : 0;
if (frequency === 'semimonthly') {
(loan.payments = parseInt(term * 2));
} else if (frequency === 'bimonthly') {
loan.payments = (parseInt(term / 2));
} else if (frequency === 'quarterly') {
loan.payments = (parseInt(term / 4));
} else if (frequency === 'semiannually') {
loan.payments = (parseInt(term / 6));
} else if (frequency === 'annually') {
loan.payments = (parseInt(term / 12));
} else if (frequency === 'none' || frequency === 'one') {
loan.payments = 1;
} else {
loan.payments = term;
}
d.resolve(loan);
return d.promise;
}
}
/*
paymentAmount
---
calculates the payment for a loan with the following parameters:
* loanAmount (required) - loan amount
* NPER (required) - the number of periods
* rate (required) - the rate per period
* interestOnly (optional) - boolean indicating if the loan is an interest only loan
* type (optional) - whether the payment is made at the beginning (1) or the end (0) of a period
*/
function paymentAmount(loan) {
var d = Q.defer();
if (!loan || !loan.loanAmount || !loan.term || !loan.interestRate) {
d.reject(new Error('required parameters not provided'));
} else {
var interestRate = loan.interestRate ? Number(loan.interestRate) / 1200 : 0;
var term = loan.term ? Number(loan.term) : 0;
var loanAmount = loan.loanAmount ? Number(loan.loanAmount) : 0;
var type = loan.type != null ? Number(loan.type) : 0;
var interestOnly = loan.interestOnly ? Boolean(loan.interestOnly) : false;
if (interestRate === 0) {
if (term > 0) {
loan.paymentAmount = loanAmount / term;
} else {
loan.paymentAmount = 0;
}
} else {
if (interestOnly) {
loan.paymentAmount = (loanAmount * (interestRate * Math.pow(1 + interestRate, term)) / (Math.pow(1 + interestRate, term) - 1)) - loanAmount;
} else {
loan.paymentAmount = loanAmount * (interestRate * Math.pow(1 + interestRate, term)) / (Math.pow(1 + interestRate, term) - 1);
}
}
d.resolve(loan);
return d.promise;
}
}
/*
firstPaymentDate
----------------
This function calculates the first payment date of a loan. It defaults to
the first day of the month which is at least one full month from the date
the loan was funded (origination or funding date).
This function takes the following arguments:
* closingDate (required) - the funding or origination date of the loan
* firstPaymentDay (optional) - desired day of the month for the payment - defaults to the first day
* cb (optional) - optional CommonJS (Node style) callback
*/
function firstPaymentDate(loan) {
var d = Q.defer();
if (!loan.closingDate) {
d.reject(new Error('closing date required to determine firstPaymentDate'));
} else {
var closingDate = loan.closingDate ? moment(loan.closingDate) : moment();
var firstPaymentDay = !_.isEmpty(loan.firstPaymentDay) ? loan.firstPaymentDay : 1;
loan.firstPaymentDate = closingDate.date() > 1 ? closingDate.add('M', 2).date(firstPaymentDay)
: closingDate.add('M', 1).date(1);
loan.firstPaymentDate = loan.firstPaymentDate.toISOString();
d.resolve(loan);
}
return d.promise;
}
/*
generateAmortizationTable
-----------------------
This function generates an amortization schedule. The schedule is returned as a Javascript object.
The function accepts the following arguments:
* PV (required): the starting principal amount of the loan
* NPER (required): the number of whole months over which the loan extends
* rate (required): the annual interest rate of the loan expressed as a percentage, e.g., 10.5
* firstPaymentDate (optional): the date the first payment will be made
* frequency (optional): the payment frequency, which can be any of the following strings:
- semimonthly - twice a month
- monthly - once each month
- bimonthly - every two months
- quarterly - every quarter
- semiannually - ever 6 months
- annually - ever 12 months
- none or one - only one payment at the end of the loan - typically don't mix this with balloonDate
* balloonDate (optional/required): the date a balloon payment will be made. This date will be forced to earliest
corresponding payment date. This date will be ignored if it is greater than the term (months) of the
loan.
The function returns an array with each array element containing the following fields:
* paymentNumber - the number for a payment
* principle: the principal balance remaining at the end of the period
* accumulatedInterest: the interest accumulate from all previous periods through this period
* payment: the periodic payment the borrower is required to pay
* paymentToPrinciple: the amount of the payment allocated to paying down the principal
* paymentToInterest: the amount of the payment allocated to paying interest
* date: the date of the payment for the period
*/
function addAmorizationTable(loan) {
var d = Q.defer();
if (!loan || !loan.loanAmount || !loan.term || !loan.interestRate || !loan.firstPaymentDate || !loan.paymentAmount) {
d.reject(new Error('parameters are incorrect'));
} else {
var loanAmount = Number(loan.loanAmount);
var term = Number(loan.term);
var interestRate = Number(loan.interestRate);
var frequency = Number(loan.frequency);
var type = loan.type && !_.isEmpty(loan.type) ? Number(loan.type) : 0;
var currDate = moment(loan.firstPaymentDate);
var dateOffset = 1;
var lastPaymentDate = currDate.clone().add('M', term);
var paymentDay = currDate.date();
var payments = 0;
var semimonthly = false;
var balloonDate = loan.balloonDate ? moment(loan.balloonDate) : null;
var schedule = [];
var totalInterest = 0.0;
var currInterest = 0;
var currPrinciple = 0;
var tempDate, tempDay, balloonPeriod, balloonAmount, payment, balance;
interestRate = interestRate / 100;
if (!frequency || frequency === 'monthly') {
payments = term;
interestRate = interestRate / 12;
dateOffset = 1;
} else if (frequency.toLowerCase() === 'semimonthly') {
(payments = term * 2);
interestRate = interestRate / 12 / 2;
semimonthly = true;
dateOffset = parseInt(365.25 / 12 / 2);
} else if (frequency.toLowerCase() === 'bimonthly') {
payments = (term / 2);
interestRate = interestRate / 12 * 2;
dateOffset = 2;
} else if (frequency.toLowerCase() === 'quarterly') {
payments = (term / 4);
interestRate = interestRate / 12 * 4;
dateOffset = 4;
} else if (frequency.toLowerCase() === 'semiannually') {
payments = (term / 6);
interestRate = interestRate / 12 * 6;
dateOffset = 6;
} else if (frequency.toLowerCase() === 'annually') {
payments = (term / 12);
interestRate = interestRate / 12 * 12;
dateOffset = 12;
} else if (frequency.toLowerCase() === 'none' || frequency.toLowerCase() === 'one') {
payments = 1;
interestRate = interestRate * (term / 12);
dateOffset = payments;
}
if (balloonDate) {
if (balloonDate.isAfter(lastPaymentDate) || balloonDate.isBefore(currDate)) {
d.reject(new Error('balloon date must be after first payment date and before last payment date'))
} else {
tempDate = moment(firstPaymentDate);
tempDay = tempDate.date();
for (var a = 0; a < payments; a++) {
if (semimonthly) {
if (a.isOdd()) {
tempDate.add('d', dateOffset);
tempDate.date(tempDay);
} else {
tempDate.add('d', dateOffset);
}
} else {
tempDate.add('M', dateOffset);
}
balloonPeriod = (!balloonDate.isAfter(tempDate) && !balloonDate.isBefore(tempDate)) ? a : term;
}
}
balloonAmount = calculator.BalloonLoan(loanAmount, interestRate, payments, null, balloonPeriod, type).balloonAmount;
}
loan.payments = payments;
payment = loan.paymentAmount;
balance = loanAmount;
for (var i = 0; i < payments; i++) {
currInterest = balance * interestRate;
totalInterest += currInterest;
currPrinciple = payment - currInterest;
balance -= currPrinciple;
if (i === balloonPeriod) {
payment = payment + balloonAmount + currInterest;
currPrinciple = payment;
balance = 0;
}
schedule.push({
paymentNumber: i + 1,
principle: balance,
accumulatedInterest: totalInterest,
payment: payment,
paymentToPrinciple: currPrinciple,
paymentToInterest: currInterest,
date: currDate.toISOString()
});
if (i === balloonPeriod) break;
if (semimonthly) {
if (i.isOdd()) {
currDate.add('d', dateOffset);
currDate.date(paymentDay);
} else {
currDate.add('d', dateOffset);
}
} else {
currDate.add('M', dateOffset);
}
}
loan.amortizationTable = schedule;
d.resolve(loan);
}
return d.promise;
}
/**
* isLoanPastDue
* @param loan
* - amortizationTable - an array of transactions
* - transactions - a list of payment transactions
* - dateLastPaymentShouldHaveBeenMade - obvious
* - dateLastPaymentWasReceived - obvious
* @returns {promise|Q.promise}
* compares amount of payments that have been received against those that
* should have been received.
*/
function isLoanPastDue(loan) {
var d = Q.defer();
var paid = 0;
var earned = 0;
var date;
if (!loan || !loan.amortizationTable || !loan.transactions || !loan.dateLastPaymentShouldHaveBeenMade) {
d.reject('required isLoanPastDue parameters not provided')
} else {
var dateLastPaymentShouldHaveBeenMade = moment(loan.dateLastPaymentShouldHaveBeenMade);
loan.transactions.forEach(function (tx) {
paid += Number(tx.amount);
});
loan.amortizationTable.forEach(function (pmt) {
date = moment(pmt.date);
if (date.isBefore(dateLastPaymentShouldHaveBeenMade)) earned += Number(pmt.payment);
});
loan.pastDue = paid < earned;
d.resolve(loan);
}
return d.promise;
}
/*
EarnedAmount
------------
This function calculates the amount that should have been received for a loan at a specified
date in the future.
This function takes a Javascript object. The Javascript object should
include the following:
* loanAmount - the loan amount due when the loan was originated and closed
* closingDate - the date when the loan was originated and closed
* prepaidInterest (optional) - the amount paid on the OriginationDate to prepay interest on
the loan up to the FirstPaymentDate or before.
* firstPaymentDate - the date the first payment should have been received
* interestRate - the annual interest rate
* paymentAmount - the amount of the periodic payments
* term - the term of the loan in months
* payments - the number of payments to be paid during the Term
* determinationDate - the date when the earnedAmount is calculated
* frequency (optional) - the periodicity of the loan, e.g., 'monthly', 'quarterly', etc.
*/
function earnedAmount(loan) {
var deferred = Q.defer();
if (!loan || !loan.loanAmount || !loan.closingDate
|| !loan.firstPaymentDate || !loan.interestRate || !loan.term
|| !loan.payments || !loan.determinationDate || !loan.paymentAmount) {
deferred.reject(new Error('earnedAmount cannot be calculated without required parameters'));
} else {
}
var InitialLoanAmount = Number(loan.loanAmount);
var OriginationDate = moment(loan.closingDate);
var FirstPaymentDate = moment(loan.firstPaymentDate);
var Term = loan.term;
var Frequency = loan.frequency ? loan.frequency : 'monthly';
var Payments = Number(loan.payments);
var Rate = Number(loan.interestRate);
var DeterminationDate = moment(loan.determinationDate);
var daysBeforeFirstPayment = FirstPaymentDate.diff(OriginationDate, 'days');
var periodicRate = Rate / 100 / (12 * Payments / Term);
var dailyRate = Rate / 100 / 365;
var InterestPrepaymentAmount = loan.prepaidInterest ? Number(loan.prepaidInterest) : 0;
var interestEarnedBeforeFirstPayment = daysBeforeFirstPayment * InitialLoanAmount * dailyRate - InterestPrepaymentAmount;
var numberOfMonths = DeterminationDate.diff(FirstPaymentDate, 'months');
var numberOfExtraDays = DeterminationDate.diff(FirstPaymentDate, 'days') - numberOfMonths * 30;
var Payment = Number(loan.paymentAmount);
cumulativeInterestPaid(loan)
var cumInterestPaid = this.cumulativeInterestPaid(loan, Payments, InitialLoanAmount);
var remainingBalance = calculator.RemainingBalance(InitialLoanAmount, periodicRate, numberOfMonths, Payment);
var interestEarnedAfterLastPayment = numberOfExtraDays * remainingBalance * dailyRate;
var totalAmountEarned = interestEarnedBeforeFirstPayment + cumInterestPaid + InitialLoanAmount - remainingBalance + interestEarnedAfterLastPayment;
deferred.resolve(totalAmountEarned);
return deferred.promise;
}
/*
cumulativeInterestPaid
----------------------
Calculate the total interest paid on a loan in specified periodic payments. Arguments include:
* rate (required) - interest rate specified as a percentage, e.g., 10.5
* periods (required) - the total number of payment periods in the term
* PV (required) - the initial sum borrowed
* start (optional) - the first period to include. Periods are numbered beginning with 1
* end (optional) - the last period to include
* type (optional) - whether payments are made at the end of each period (0) or at the start of each period (1)
* callback (optional) - callback for asynchronous processing using Node's CommonJS format
*/
function cumulativeInterestPaid(loan, params, callback) {
var deferred = Q.defer();
if (!loan || !loan.interestRate || !loan.term || !loan.payments || !loan.loanAmount || !params.startPeriod || !params.endPeriod) {
deferred.reject(new Error("function arguments not provided"));
} else {
}
try {
var rate = Number(loan.interestRate);
var NPER = Number(loan.term);
var PV = Number(loan.loanAmount);
var start = params.startPeriod != null ? Number(params.startPeriod) : null;
var end = params.endPeriod != null ? Number(params.endPeriod) : null;
var type = type != null ? Number(loan.type) : null;
var ip = function (rate, NPER, PV, type) {
return type && type === 1
? (PV * (Math.pow(1 + rate, NPER) - 1)) * (1 + rate)
: PV * (Math.pow(1 + rate, NPER) - 1);
};
var presentValueBeforeStart = start != null
? ip(rate, start, PV, type) + PV
: null;
var result = presentValueBeforeStart
? ip(rate, end != null ? end - start : NPER - start, presentValueBeforeStart, type)
: ip(rate, NPER, PV, type);
deferred.resolve(result);
if (callback) return deferred.promise.nodeify(callback);
return result;
} catch (err) {
deferred.reject(err);
if (callback) return deferred.promise.nodeify(callback);
return err;
}
}
/**
* nextPaymentDate
* @param loan
* - dateLastPaymentWasReceived
* - amortizationTable
* @returns {promise|Q.promise}
*/
function nextPaymentDate(loan) {
var d = Q.defer();
var date;
var item;
var result;
if (!loan || !loan.dateLastPaymentWasReceived || !loan.amortizationTable) {
d.reject('required parameters for nextPaymentDue not provided');
} else {
var dateLastPaymentWasReceived = moment(loan.dateLastPaymentWasReceived);
for (var i = 0, len = loan.amortizationTable.length; i < len; i++) {
item = loan.amortizationTable[i];
date = moment(item.date);
if (date.isAfter(dateLastPaymentWasReceived)) {
result = item.date;
break;
}
}
}
loan.nextPaymentDate = result;
d.resolve(loan);
return d.promise;
}
/**
* Calculates the last date a payment should have been
* @param loan, which must include
* - amortizationTable - a loan amortization table with the expected payment dates
* - daysUntilLate (optional) - the grace days for the loan - defaults to 0
* - determinationDate (optional) - the date the determination is made, defaults to today
*/
function dateLastPaymentShouldHaveBeenMade(loan) {
var d = Q.defer();
var result;
if (!loan || !loan.amortizationTable) {
d.reject(new Error('required dateLastPaymentShouldHaveBeenMade not provided'));
} else {
var determinationDate = loan.determinationDate ? moment(loan.determinationDate) : moment();
var daysUntilLate = loan.daysUntilLate ? Number(loan.daysUntilLate) : 0;
determinationDate.add('days', daysUntilLate);
loan.amortizationTable.forEach(function (payment) {
var paymentDate = moment(payment.date);
if (paymentDate.isBefore(determinationDate)) result = paymentDate.toISOString();
});
loan.dateLastPaymentShouldHaveBeenMade = result;
d.resolve(loan)
}
return d.promise;
}
/**
* dateLastPaymentWasReceived
* @param loan
* - transactions
*/
function dateLastPaymentWasReceived(loan) {
var d = Q.defer();
var result = null;
if (!loan || !loan.transactions) {
d.reject('required dateLastPaymentWasReceived not provided');
} else {
loan.transactions.forEach(function (tx) {
result = tx.receivedDate;
});
if (result) {
loan.dateLastPaymentWasReceived = result;
d.resolve(loan);
} else d.reject('no transactions to determine dateLastPaymentWasReceived');
}
return d.promise;
}
/**
*
* @param loan
* - transactions
* - loanAmount
* @returns {promise|Q.promise}
*/
function outstandingPrincipal(loan) {
var d = Q.defer();
var sumOfPayments = 0;
if (!loan || !loan.loanAmount || !loan.transactions) {
d.reject(new Error('required parameters for outstandingPrincipal not provided'))
} else {
loan.transactions.forEach(function (tx) {
sumOfPayments += typeof tx.principal === "number" ? tx.principal : 0;
});
loan.loanBalance = loan.loanAmount - sumOfPayments;
d.resolve(loan);
}
return d.promise;
}
/**
*
* @param loan
* - transactions
* - lateChargeType (required) - can be 'none', 'lateChargeFixed', or 'lateChargePercent'
* - lateChargePercent (optional) - if largeChargeType == lateChargePercent this the
* percent of the monthly payment that is charged as a late fee
* - lateChargeMin$ (optional) - low er bounding for lateChargePercent
* - lateChargeMax$ (optional) - upper bounding for lateChargePercent
* - lateChargeFixed (optional) - fixed late charge
* - daysUntilLate (required) - the number of days after payment is due that user is given without late fee
* - amortizationTable (required)
* - paymentAmount (required)
* - transactions
* @returns {promise|Q.promise}
*/
function aggregateLateFees(loan) {
var d = Q.defer();
var datePaymentShouldHaveBeenReceived;
var datePaymentWasReceived;
var determinationDate = moment();
var payment;
var tx;
var lateFee = 0;
loan.aggregateLateFees = 0;
if (!loan || !loan.lateChargeType || !loan.daysUntilLate || !loan.amortizationTable || !loan.transactions) {
d.reject(new Error('required parameters for aggregateLateFees not provided'));
} else {
if (loan.lateChargeType === 'lateChargeFixed') {
if (loan.lateChargeFixed) {
lateFee = loan.lateChargeFixed;
}
} else if (loan.lateChargeType === 'lateChargePercent') {
if (loan.lateChargePercent) {
lateFee = loan.paymentAmount * loan.lateChargePercent / 100;
if (loan.lateChargeMin$ && lateFee < loan.lateChargeMin$) {
lateFee = loan.lateChargeMin$;
}
if (loan.lateChargeMax$ && lateFee > loan.lateChargeMax$) {
lateFee = loan.lateChargeMax$;
}
}
}
for (var i = 0, l1 = loan.amortizationTable.length; i < l1; i++) {
payment = loan.amortizationTable[i];
datePaymentShouldHaveBeenReceived = moment(payment.date).add('days', loan.daysUntilLate);
if (datePaymentShouldHaveBeenReceived.isBefore(determinationDate)) {
loan.aggregateLateFees += lateFee;
}
}
for (var j = 0, l2 = loan.transactions.length; j < l2; j++) {
tx = loan.transactions[j];
datePaymentWasReceived = moment(tx.receivedDate);
if (datePaymentWasReceived.isBefore(determinationDate)) {
tx.lateFees = lateFee;
loan.aggregateLateFees -= lateFee;
}
}
}
d.resolve(loan);
return d.promise;
}
/**
*
* @param loan
* - closingDate
* - loanAmount
* - interestRate
* - transactions
* - paymentAmount
* - determinationDate
* - daysInYear
* @returns {exports.pending.promise|*|promise|Q.defer.promise|Deferred.promise|Pending.promise}
*/
function interestAllocationForPayment(loan) {
var d = Q.defer();
var days = 0;
var daysInYear = loan.daysInYear || 365;
var determinationDate = loan.determinationDate ? moment(loan.determinationDate) : moment();
var closingDate;
var rate;
var interestPaid = 0;
if (!loan || !loan.closingDate || !loan.loanAmount || !loan.interestRate || !loan.paymentAmount) {
d.reject(new Error('required parameters for accruedInterestForLoanTx not provided'));
} else {
closingDate = moment(loan.closingDate);
days = determinationDate.diff(closingDate, 'days');
rate = loan.interestRate / 100 / daysInYear;
var accruedInterest = loan.loanAmount * days * rate;
loan.transactions.forEach(function (tx) {
var paymentDate = moment(tx.date);
if (paymentDate.isBefore(determinationDate)) interestPaid += tx.interest;
});
loan.allocationToInterest = (accruedInterest - interestPaid) < loan.paymentAmount
? accruedInterest - interestPaid
: loan.paymentAmount;
d.resolve(loan);
}
return d.promise;
}