Files
cbsnexora/src/app/loans/loans-view/loan-account-actions/make-repayment/make-repayment.component.ts
T

127 lines
4.1 KiB
TypeScript

/** Angular Imports */
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
/** Custom Services */
import { LoansService } from 'app/loans/loans.service';
import { SettingsService } from 'app/settings/settings.service';
import { Dates } from 'app/core/utils/dates';
/**
* Loan Make Repayment Component
*/
@Component({
selector: 'mifosx-make-repayment',
templateUrl: './make-repayment.component.html',
styleUrls: ['./make-repayment.component.scss']
})
export class MakeRepaymentComponent implements OnInit, OnDestroy {
@Input() dataObject: any;
/** Loan Id */
loanId: string;
/** Payment Type Options */
paymentTypes: any;
/** Show payment details */
showPaymentDetails = false;
/** Minimum Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Date allowed. */
maxDate = new Date();
/** Repayment Loan Form */
repaymentLoanForm: FormGroup;
/**
* @param {FormBuilder} formBuilder Form Builder.
* @param {LoansService} loanService Loan Service.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {SettingsService} settingsService Settings Service
*/
constructor(private formBuilder: FormBuilder,
private loanService: LoansService,
private route: ActivatedRoute,
private router: Router,
private dateUtils: Dates,
private settingsService: SettingsService) {
this.loanId = this.route.snapshot.params['loanId'];
}
/**
* Creates the repayment loan form
* and initialize with the required values
*/
ngOnInit() {
this.maxDate = this.settingsService.businessDate;
this.createRepaymentLoanForm();
this.setRepaymentLoanDetails();
}
ngOnDestroy(): void {
}
/**
* Creates the create close form.
*/
createRepaymentLoanForm() {
this.repaymentLoanForm = this.formBuilder.group({
'transactionDate': [this.settingsService.businessDate, Validators.required],
'transactionAmount': ['', Validators.required],
'externalId': '',
'paymentTypeId': '',
'note': ''
});
}
setRepaymentLoanDetails() {
this.paymentTypes = this.dataObject.paymentTypeOptions;
this.repaymentLoanForm.patchValue({
transactionAmount: this.dataObject.amount
});
}
/**
* Add payment detail fields to the UI.
*/
addPaymentDetails() {
this.showPaymentDetails = !this.showPaymentDetails;
if (this.showPaymentDetails) {
this.repaymentLoanForm.addControl('accountNumber', new FormControl(''));
this.repaymentLoanForm.addControl('checkNumber', new FormControl(''));
this.repaymentLoanForm.addControl('routingCode', new FormControl(''));
this.repaymentLoanForm.addControl('receiptNumber', new FormControl(''));
this.repaymentLoanForm.addControl('bankNumber', new FormControl(''));
} else {
this.repaymentLoanForm.removeControl('accountNumber');
this.repaymentLoanForm.removeControl('checkNumber');
this.repaymentLoanForm.removeControl('routingCode');
this.repaymentLoanForm.removeControl('receiptNumber');
this.repaymentLoanForm.removeControl('bankNumber');
}
}
/** Submits the repayment form */
submit() {
const repaymentLoanFormData = this.repaymentLoanForm.value;
const locale = this.settingsService.language.code;
const dateFormat = this.settingsService.dateFormat;
const prevTransactionDate: Date = this.repaymentLoanForm.value.transactionDate;
if (repaymentLoanFormData.transactionDate instanceof Date) {
repaymentLoanFormData.transactionDate = this.dateUtils.formatDate(prevTransactionDate, dateFormat);
}
const data = {
...repaymentLoanFormData,
dateFormat,
locale
};
const command = this.dataObject.type.code.split('.')[1];
this.loanService.submitLoanActionButton(this.loanId, data, command)
.subscribe((response: any) => {
this.router.navigate(['../../transactions'], { relativeTo: this.route });
});
}
}