brewman/overlord/src/app/incentive/incentive.component.ts

196 lines
5.4 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {FormArray, FormBuilder, FormGroup} from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import {ActivatedRoute, Router} from '@angular/router';
import {BehaviorSubject, Observable} from 'rxjs';
import {IncentiveDataSource} from './incentive-datasource';
import {Account} from '../core/account';
import {VoucherService} from '../core/voucher.service';
import {Incentive, Voucher} from '../core/voucher';
import * as moment from 'moment';
import {AuthService} from '../auth/auth.service';
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
import {ToasterService} from '../core/toaster.service';
import {map} from 'rxjs/operators';
@Component({
selector: 'app-incentive',
templateUrl: './incentive.component.html',
styleUrls: ['./incentive.component.css']
})
export class IncentiveComponent implements OnInit {
public incentiveObservable = new BehaviorSubject<Incentive[]>([]);
dataSource: IncentiveDataSource;
form: FormGroup;
voucher: Voucher;
account: Account;
accBal: any;
displayedColumns = ['name', 'designation', 'department', 'daysWorked', 'points', 'amount'];
accounts: Observable<Account[]>;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private dialog: MatDialog,
private toaster: ToasterService,
private auth: AuthService,
private ser: VoucherService
) {
this.createForm();
this.listenToDateChange();
}
ngOnInit() {
this.route.data
.subscribe((data: { voucher: Voucher }) => {
this.loadVoucher(data.voucher);
});
}
loadVoucher(voucher) {
this.voucher = voucher;
this.form.get('date').setValue(moment(this.voucher.date, 'DD-MMM-YYYY').toDate());
this.form.setControl('incentives', this.fb.array(
this.voucher.incentives.map(
x => this.fb.group({
points: x.points
})
)
));
this.dataSource = new IncentiveDataSource(this.incentiveObservable);
this.incentiveObservable.next(this.voucher.incentives);
}
createForm() {
this.form = this.fb.group({
date: '',
incentives: this.fb.array([])
});
}
listenToDateChange(): void {
this.form.get('date').valueChanges.pipe(
map(x => moment(x).format('DD-MMM-YYYY'))
).subscribe(x => {
if (x !== this.voucher.date && !this.voucher.id) {
return this.ser.getIncentive(x)
.subscribe((voucher: Voucher) => {
this.loadVoucher(voucher);
});
}
});
}
totalPoints() {
return this.voucher.incentives.map((item) => {
return item.daysWorked * item.points;
}).reduce((sum, item) => {
return sum + item;
});
}
pointValue() {
return Math.round(this.voucher.incentive * 100 / this.totalPoints()) / 100;
}
less(row: Incentive, i: number) {
if (row.points >= 1) {
row.points -= 1;
this.form.get('incentives').get('' + i).setValue({'points': '' + row.points});
}
}
change(row: Incentive, i: number) {
row.points = +(this.form.get('incentives').get('' + i).get('points').value);
this.form.get('incentives').get('' + i).setValue({'points': '' + row.points});
}
more(row: Incentive, i: number) {
row.points += 1;
this.form.get('incentives').get('' + i).setValue({'points': '' + row.points});
}
amount(row) {
return row.points * row.daysWorked * this.pointValue();
}
canSave() {
if (!this.voucher.id) {
return true;
} else if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
return true;
} else {
return this.voucher.user.id === this.auth.user.id || this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1;
}
}
post() {
this.ser.post(this.voucher.id)
.subscribe(
(result) => {
this.loadVoucher(result);
this.toaster.show('Success', 'Voucher Posted');
},
(error) => {
this.toaster.show('Danger', error);
}
);
}
save() {
const voucher: Voucher = this.getVoucher();
this.ser.saveOrUpdate(voucher)
.subscribe(
(result) => {
if (voucher.id === result.id) {
this.loadVoucher(result);
} else {
this.router.navigate(['/incentive', result.id]);
}
},
(error) => {
this.toaster.show('Danger', error);
}
);
}
getVoucher(): Voucher {
const formModel = this.form.value;
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
const array = this.form.get('incentives') as FormArray;
this.voucher.incentives.forEach((item, index) => {
item.points = array.controls[index].value.points;
});
return this.voucher;
}
delete() {
this.ser.delete(this.voucher.id)
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigate(['/incentive'], {replaceUrl: true});
},
(error) => {
this.toaster.show('Danger', error);
}
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.'}
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
}