feat: add loan charge to existing loan accounts (#370)

- Add Loans Module.
- Add add-loan-charge component.
- Add loans-routing.module.ts
- Add loan-charge-template.resolver.ts

Fixes: #247
This commit is contained in:
Jivjyot Singh
2019-02-02 22:06:56 +05:30
committed by Abhay Chawla
parent 370fc9b481
commit 9944dcce93
12 changed files with 393 additions and 1 deletions
+3 -1
View File
@@ -26,13 +26,14 @@ import { HomeModule } from './home/home.module';
import { LoginModule } from './login/login.module';
import { SettingsModule } from './settings/settings.module';
import { NavigationModule } from './navigation/navigation.module';
import { ClientsModule} from './clients/clients.module';
import { ClientsModule } from './clients/clients.module';
import { GroupsModule } from './groups/groups.module';
import { CentersModule } from './centers/centers.module';
import { AccountingModule } from './accounting/accounting.module';
import { SelfServiceModule } from './self-service/self-service.module';
import { SystemModule } from './system/system.module';
import { ProductsModule } from './products/products.module';
import { LoansModule } from './loans/loans.module';
/** Main Routing Module */
import { AppRoutingModule } from './app-routing.module';
@@ -60,6 +61,7 @@ import { AppRoutingModule } from './app-routing.module';
CentersModule,
AccountingModule,
SelfServiceModule,
LoansModule,
SystemModule,
ProductsModule,
AppRoutingModule
@@ -0,0 +1,62 @@
<div class="container">
<mat-card>
<form [formGroup]="loanChargeForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="column">
<mat-form-field>
<mat-label>Charge</mat-label>
<mat-select required formControlName="chargeId">
<mat-option *ngFor="let loanCharge of loanChargeOptions" [value]="loanCharge.id">
{{ loanCharge.name + ' (' + loanCharge.currency.name + ')' }}
</mat-option>
</mat-select>
<mat-error *ngIf="loanChargeForm.controls.chargeId.hasError('required')">
Charge is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Amount</mat-label>
<input type=number required matInput formControlName="amount" />
<mat-error *ngIf="loanChargeForm.controls.amount.hasError('required')">
Amount is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Charge Calculation</mat-label>
<input matInput formControlName="chargeCalculation" />
</mat-form-field>
<mat-form-field>
<mat-label>Charge Time</mat-label>
<input matInput formControlName="chargeTime" />
</mat-form-field>
<mat-form-field *ngIf="loanChargeForm.controls['dueDate']">
<mat-label>Due On</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="dueDatePicker" required formControlName="dueDate">
<mat-datepicker-toggle matSuffix [for]="dueDatePicker"></mat-datepicker-toggle>
<mat-datepicker #dueDatePicker></mat-datepicker>
<mat-error *ngIf="loanChargeForm.controls.dueDate.hasError('required')">
Due Date is <strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<mat-card-actions fxLayoutGap="5px" fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center">
<button type="button" mat-raised-button [routerLink]="['../']">Cancel</button>
<button mat-raised-button color="primary" [disabled]="!loanChargeForm.valid">Submit</button>
</mat-card-actions>
</mat-card-content>
</form>
</mat-card>
</div>
@@ -0,0 +1,3 @@
.container{
width: 37rem;
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AddLoanChargeComponent } from './add-loan-charge.component';
describe('AddloanchargeComponent', () => {
let component: AddLoanChargeComponent;
let fixture: ComponentFixture<AddLoanChargeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AddLoanChargeComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AddLoanChargeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,108 @@
/** Angular Imports */
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
/** Custom Services */
import { LoansService } from '../loans.service';
/**
* Create Add Loan Charge component.
*/
@Component({
selector: 'mifosx-addloancharge',
templateUrl: './add-loan-charge.component.html',
styleUrls: ['./add-loan-charge.component.scss']
})
export class AddLoanChargeComponent implements OnInit {
/** Minimum Due Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Due Date allowed. */
maxDate = new Date();
/** Add Loan Charge form. */
loanChargeForm: FormGroup;
/** loan charge options. */
loanChargeOptions: {
id: number;
name: string;
amount: number;
chargeCalculationType: {
value: any;
};
chargeTimeType: {
id: number;
value: any;
};
}[];
/** loan Id of the loan account. */
loanId: string;
/**
* Retrieves the loan charge template data from `resolve`.
* @param {FormBuilder} formBuilder Form Builder.
* @param {AccountingService} accountingService Accounting Service.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
*/
constructor(private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private datePipe: DatePipe,
private loansService: LoansService) {
this.route.data.subscribe((data: { loanChargeTemplate: any }) => {
this.loanChargeOptions = data.loanChargeTemplate.chargeOptions;
});
this.loanId = this.route.snapshot.params['loanId'];
}
/**
* Creates the Loan Charge form.
*/
ngOnInit() {
this.createLoanChargeForm();
this.loanChargeForm.controls.chargeId.valueChanges.subscribe(chargeId => {
const chargeDetails = this.loanChargeOptions.find(option => {
return option.id === chargeId;
});
if (chargeDetails.chargeTimeType.id === 2) {
this.loanChargeForm.addControl('dueDate', new FormControl('', Validators.required));
} else {
this.loanChargeForm.removeControl('dueDate');
}
this.loanChargeForm.patchValue({
'amount': chargeDetails.amount,
'chargeCalculation': chargeDetails.chargeCalculationType.value,
'chargeTime': chargeDetails.chargeTimeType.value
});
});
}
/**
* Creates the Loan Charge form.
*/
createLoanChargeForm() {
this.loanChargeForm = this.formBuilder.group({
'chargeId': ['', Validators.required],
'amount': ['', Validators.required],
'chargeCalculation': [{ value: '', disabled: true }],
'chargeTime': [{ value: '', disabled: true }]
});
}
submit() {
const prevDueDate: Date = this.loanChargeForm.value.dueDate;
// TODO: Update once language and date settings are setup
const dateFormat = 'yyyy-MM-dd';
this.loanChargeForm.patchValue({
dueDate: this.datePipe.transform(prevDueDate, dateFormat)
});
const loanCharge = this.loanChargeForm.value;
loanCharge.locale = 'en';
loanCharge.dateFormat = dateFormat;
this.loansService.createLoanCharge(this.loanId, 'charges', loanCharge).subscribe(res => {
this.router.navigate(['../'], { relativeTo: this.route });
});
}
}
@@ -0,0 +1,31 @@
/** Angular Imports */
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
/** rxjs Imports */
import { Observable } from 'rxjs';
/** Custom Services */
import { LoansService } from '../loans.service';
/**
* Loan Charge Template data resolver.
*/
@Injectable()
export class LoanChargeTemplateResolver implements Resolve<Object> {
/**
* @param {LoansService} LoansService loans service.
*/
constructor(private loansService: LoansService) {}
/**
* Returns the Loan Charge Template.
* @returns {Observable<any>}
*/
resolve(route: ActivatedRouteSnapshot): Observable<any> {
const loanId = route.paramMap.get('loanId');
return this.loansService.getLoanChargeTemplateResource(loanId);
}
}
@@ -0,0 +1,13 @@
import { LoansRoutingModule } from './loans-routing.module';
describe('LoansRoutingModule', () => {
let loansRoutingModule: LoansRoutingModule;
beforeEach(() => {
loansRoutingModule = new LoansRoutingModule();
});
it('should create an instance', () => {
expect(loansRoutingModule).toBeTruthy();
});
});
+65
View File
@@ -0,0 +1,65 @@
/** Angular Imports */
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
/** Routing Imports */
import { Route } from '../core/route/route.service';
/** Translation Imports */
import { extract } from '../core/i18n/i18n.service';
/** Custom Components */
import { AddLoanChargeComponent } from './add-loan-charge/add-loan-charge.component';
/** Custom Resolvers */
import {LoanChargeTemplateResolver} from './common-resolvers/loan-charge-template.resolver';
const routes: Routes = [
Route.withShell([
{
path: ':clientType',
data: { title: extract('Clients'), routeParamBreadcrumb: 'clientType' },
children: [
{
path: 'view',
children: [
{
path: ':clientId',
data: { title: extract('Client View'), routeParamBreadcrumb: 'clientId' },
children: [
{
path: 'loans',
data: { title: extract('All Loans'), breadcrumb: 'Loans', routeParamBreadcrumb: false },
children: [
{
path: ':loanId',
data: { title: extract('Loan View'), routeParamBreadcrumb: 'loanId' },
// Component For Loan View Comes Here
children: [
{
path: 'add-loan-charge',
component: AddLoanChargeComponent,
data: {title: extract('Add Loan Charge'), breadcrumb: 'Add Loan Charge', routeParamBreadcrumb: false},
resolve: {
loanChargeTemplate: LoanChargeTemplateResolver
}
}
]
}
]
}
]
}
]
}
]
}
])
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
declarations: [],
providers: [LoanChargeTemplateResolver]
})
export class LoansRoutingModule {}
+13
View File
@@ -0,0 +1,13 @@
import { LoansModule } from './loans.module';
describe('LoansModule', () => {
let loansModule: LoansModule;
beforeEach(() => {
loansModule = new LoansModule();
});
it('should create an instance', () => {
expect(loansModule).toBeTruthy();
});
});
+26
View File
@@ -0,0 +1,26 @@
/** Angular Imports */
import { NgModule } from '@angular/core';
import { DatePipe} from '@angular/common';
/** Custom Modules */
import { LoansRoutingModule } from './loans-routing.module';
import { SharedModule } from 'app/shared/shared.module';
/** Custom Components */
import { AddLoanChargeComponent } from './add-loan-charge/add-loan-charge.component';
/**
* Loans Module
*
* All components related to loan functions should be declared here.
*/
@NgModule({
imports: [
SharedModule,
LoansRoutingModule
],
declarations: [AddLoanChargeComponent],
providers: [DatePipe]
})
export class LoansModule { }
+12
View File
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { LoansService } from './loans.service';
describe('LoansService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: LoansService = TestBed.get(LoansService);
expect(service).toBeTruthy();
});
});
+32
View File
@@ -0,0 +1,32 @@
/** Angular Imports */
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
/** rxjs Imports */
import { Observable } from 'rxjs';
/**
* Loans service.
*/
@Injectable({
providedIn: 'root'
})
export class LoansService {
constructor(private http: HttpClient) {}
/**
* @param {string} loanId loanId of the loan.
* @returns {Observable<any>}
*/
getLoanChargeTemplateResource(loanId: string): Observable<any> {
return this.http.get(`/loans/${loanId}/charges/template`);
}
/**
* @param {any} loanCharge to apply on a Loan Account.
* @returns {Observable<any>}
*/
createLoanCharge(loanId: string, resourceType: string, loanCharge: any): Observable<any> {
return this.http.post(`/loans/${loanId}/${resourceType}`, loanCharge);
}
}