Files
cbsnexora/src/app/deposits/recurring-deposits/recurring-deposits-account-view/charges-tab/charges-tab.component.ts
T

213 lines
6.6 KiB
TypeScript

/** Angular Imports */
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { MatTableDataSource, MatTable } from '@angular/material/table';
import { DatePipe } from '@angular/common';
/** Custom Services */
import { SavingsService } from 'app/savings/savings.service';
/** Custom Dialogs */
import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component';
import { RecurringDepositConfirmationDialogComponent } from '../custom-dialogs/recurring-deposit-confirmation-dialog/recurring-deposit-confirmation-dialog.component';
import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component';
/** Custom Models */
import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base';
import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base';
import { DatepickerBase } from 'app/shared/form-dialog/formfield/model/datepicker-base';
/**
* Charges Tab Component
*/
@Component({
selector: 'mifosx-charges-tab',
templateUrl: './charges-tab.component.html',
styleUrls: ['./charges-tab.component.scss']
})
export class ChargesTabComponent implements OnInit {
/** Recurring Deposits Account Data */
recurringDepositsAccountData: any;
/** Charges Data */
chargesData: any[];
/** Data source for charges table. */
dataSource: MatTableDataSource<any>;
/** Toggles Charges Table */
showInactiveCharges = false;
/** Columns to be displayed in charges table. */
displayedColumns: string[] = [
'name',
'feeOrPenalty',
'paymentDueAt',
'dueAsOf',
'repeatsOn',
'calculationType',
'due',
'paid',
'waived',
'outstanding',
'actions'
];
/** Charges Table Reference */
@ViewChild('chargesTable', { static: true }) chargesTableRef: MatTable<Element>;
/**
* Retrieves Recurring Deposits Account Data from `resolve`.
* @param {ActivatedRoute} route Activated Route.
*/
constructor(private route: ActivatedRoute,
private savingsService: SavingsService,
private datePipe: DatePipe,
private router: Router,
public dialog: MatDialog) {
this.route.parent.data.subscribe((data: { recurringDepositsAccountData: any }) => {
this.recurringDepositsAccountData = data.recurringDepositsAccountData;
this.chargesData = this.recurringDepositsAccountData.charges;
});
}
ngOnInit() {
const activeCharges = this.chargesData ? this.chargesData.filter(charge => charge.isActive) : [];
this.dataSource = new MatTableDataSource(activeCharges);
}
/**
* Pays the charge.
* @param {any} chargeId Charge Id
*/
payCharge(chargeId: any) {
const formfields: FormfieldBase[] = [
new InputBase({
controlName: 'amount',
label: 'Amount',
value: '',
type: 'number',
required: true
}),
new DatepickerBase({
controlName: 'dueDate',
label: 'Payment Date',
value: '',
type: 'date',
required: true
})
];
const data = {
title: `Pay Charge ${chargeId}`,
layout: { addButtonText: 'Confirm' },
formfields: formfields
};
const payChargeDialogRef = this.dialog.open(FormDialogComponent, { data });
payChargeDialogRef.afterClosed().subscribe((response: any) => {
if (response.data) {
const locale = 'en';
const dateFormat = 'dd MMMM yyyy';
const dataObject = {
...response.data.value,
dueDate: this.datePipe.transform(response.data.value.dueDate, dateFormat),
dateFormat,
locale
};
this.savingsService.executeSavingsAccountChargesCommand(this.recurringDepositsAccountData.id, 'paycharge', dataObject, chargeId)
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Waive's the charge
* @param {any} chargeId Charge Id
*/
waiveCharge(chargeId: any) {
const waiveChargeDialogRef = this.dialog.open(RecurringDepositConfirmationDialogComponent, { data: { heading: 'Waive Charge', dialogContext: `Are you sure you want to waive charge with id: ${chargeId}?` } });
waiveChargeDialogRef.afterClosed().subscribe((response: any) => {
if (response.confirm) {
this.savingsService.executeSavingsAccountChargesCommand(this.recurringDepositsAccountData.id, 'waive', {}, chargeId)
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Edits the charge
* @param {any} charge Charge
*/
editCharge(charge: any) {
const formfields: FormfieldBase[] = [
new InputBase({
controlName: 'amount',
label: 'Amount',
value: charge.amount || charge.amountOrPercentage,
type: 'number',
required: true
})
];
const data = {
title: `Edit Charge ${charge.id}`,
layout: { addButtonText: 'Confirm' },
formfields: formfields
};
const editChargeDialogRef = this.dialog.open(FormDialogComponent, { data });
editChargeDialogRef.afterClosed().subscribe((response: any) => {
if (response.data) {
const locale = 'en';
const dateFormat = 'dd MMMM yyyy';
const dataObject = {
...response.data.value,
dateFormat,
locale
};
this.savingsService.editSavingsAccountCharge(this.recurringDepositsAccountData.id, dataObject, charge.id)
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Deletes the charge
* @param {any} chargeId Charge Id
*/
deleteCharge(chargeId: any) {
const deleteChargeDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `charge id:${chargeId}` }
});
deleteChargeDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.savingsService.deleteSavingsAccountCharge(this.recurringDepositsAccountData.id, chargeId)
.subscribe(() => {
this.reload();
});
}
});
}
/**
* Stops the propagation to view charge page.
* @param $event Mouse Event
*/
routeEdit($event: MouseEvent) {
$event.stopPropagation();
}
/**
* Refetches data for the component
* TODO: Replace by a custom reload component instead of hard-coded back-routing.
*/
private reload() {
const clientId = this.recurringDepositsAccountData.clientId;
const url: string = this.router.url;
this.router.navigateByUrl(`/clients/${clientId}/recurringdeposits`, { skipLocationChange: true })
.then(() => this.router.navigate([url]));
}
}