import {Component, OnInit} from '@angular/core'; import {FormArray, FormBuilder, FormGroup} from '@angular/forms'; import {MatDialog} from '@angular/material'; import {ActivatedRoute, Router} from '@angular/router'; import {BehaviorSubject, Observable} from 'rxjs'; import {IncentiveDataSource} from './incentive-datasource'; import {Account} from '../account/account'; import {VoucherService} from '../journal/voucher.service'; import {Incentive, Voucher} from '../journal/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([]); dataSource: IncentiveDataSource; form: FormGroup; voucher: Voucher; account: Account; accBal: any; displayedColumns = ['name', 'designation', 'department', 'daysWorked', 'points', 'amount']; accounts: Observable; 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.voucher = data.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.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( i => this.fb.group({ points: i.points }) ) )); this.dataSource = new IncentiveDataSource(this.incentiveObservable); this.incentiveObservable.next(this.voucher.incentives); }); } }); } 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}); } } 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.toaster.show('Success', 'Voucher Posted'); }, (error) => { this.toaster.show('Danger', error.error); } ); } save() { this.ser.save(this.getVoucher()) .subscribe( (result) => { this.toaster.show('Success', ''); this.router.navigate(['/Incentive', result.id]); }, (error) => { this.toaster.show('Danger', error.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; }); this.voucher.narration = formModel.narration; 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.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(); } }); } }