2020-10-01 15:21:22 +00:00
|
|
|
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
|
|
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
2019-06-12 11:55:10 +00:00
|
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
2020-10-01 15:21:22 +00:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
|
|
|
|
import { EmployeeAttendanceDataSource } from './employee-attendance-datasource';
|
2018-05-25 13:49:00 +00:00
|
|
|
import * as moment from 'moment';
|
2020-10-01 15:21:22 +00:00
|
|
|
import { AuthService } from '../auth/auth.service';
|
|
|
|
import { ToasterService } from '../core/toaster.service';
|
|
|
|
import { EmployeeAttendanceService } from './employee-attendance.service';
|
|
|
|
import { EmployeeAttendance, EmployeeAttendanceItem } from './employee-attendance';
|
|
|
|
import { EmployeeService } from '../employee/employee.service';
|
|
|
|
import { Employee } from '../employee/employee';
|
|
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
import { AttendanceType } from '../attendance/attendance-type';
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-employee-attendance',
|
|
|
|
templateUrl: './employee-attendance.component.html',
|
2020-10-01 15:21:22 +00:00
|
|
|
styleUrls: ['./employee-attendance.component.css'],
|
2018-05-25 13:49:00 +00:00
|
|
|
})
|
|
|
|
export class EmployeeAttendanceComponent implements OnInit, AfterViewInit {
|
2019-06-12 11:55:10 +00:00
|
|
|
@ViewChild('employeeElement', { static: true }) employeeElement: ElementRef;
|
2018-05-25 13:49:00 +00:00
|
|
|
public employeeAttendanceObservable = new BehaviorSubject<EmployeeAttendanceItem[]>([]);
|
|
|
|
dataSource: EmployeeAttendanceDataSource;
|
|
|
|
form: FormGroup;
|
|
|
|
|
|
|
|
info: EmployeeAttendance;
|
|
|
|
attendanceTypes: AttendanceType[];
|
|
|
|
|
|
|
|
displayedColumns = ['date', 'status', 'prints'];
|
|
|
|
|
|
|
|
employees: Observable<Employee[]>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private fb: FormBuilder,
|
|
|
|
private dialog: MatDialog,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
private auth: AuthService,
|
|
|
|
private ser: EmployeeAttendanceService,
|
2020-10-01 15:21:22 +00:00
|
|
|
private employeeSer: EmployeeService,
|
2018-05-25 13:49:00 +00:00
|
|
|
) {
|
|
|
|
this.createForm();
|
|
|
|
this.listenToEmployeeValueChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.route.data.subscribe(
|
|
|
|
(data: { info: EmployeeAttendance; attendanceTypes: AttendanceType[] }) => {
|
2018-05-25 13:49:00 +00:00
|
|
|
this.info = data.info;
|
|
|
|
this.attendanceTypes = data.attendanceTypes;
|
|
|
|
this.form.get('startDate').setValue(moment(this.info.startDate, 'DD-MMM-YYYY').toDate());
|
|
|
|
this.form.get('finishDate').setValue(moment(this.info.finishDate, 'DD-MMM-YYYY').toDate());
|
|
|
|
this.form.get('employee').setValue(this.info.employee);
|
2020-10-01 15:21:22 +00:00
|
|
|
this.form.setControl(
|
|
|
|
'attendances',
|
|
|
|
this.fb.array(
|
|
|
|
this.info.body.map((x) =>
|
|
|
|
this.fb.group({
|
|
|
|
attendanceType: x.attendanceType.id,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
this.dataSource = new EmployeeAttendanceDataSource(this.employeeAttendanceObservable);
|
|
|
|
this.employeeAttendanceObservable.next(this.info.body);
|
2020-10-01 15:21:22 +00:00
|
|
|
},
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.employeeElement.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
createForm() {
|
|
|
|
this.form = this.fb.group({
|
|
|
|
startDate: '',
|
|
|
|
finishDate: '',
|
|
|
|
employee: '',
|
2020-10-01 15:21:22 +00:00
|
|
|
attendances: this.fb.array([]),
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listenToEmployeeValueChanges() {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.employees = this.form.get('employee').valueChanges.pipe(
|
|
|
|
startWith(null),
|
|
|
|
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
|
|
|
debounceTime(150),
|
|
|
|
distinctUntilChanged(),
|
|
|
|
switchMap((x) => (x === null ? observableOf([]) : this.employeeSer.autocomplete(x))),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayFn(employee?: Employee): string | undefined {
|
|
|
|
return employee ? employee.name : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
selected(event: MatAutocompleteSelectedEvent): void {
|
|
|
|
this.info.employee = event.option.value;
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
getClass(index: number) {
|
2018-05-25 13:49:00 +00:00
|
|
|
const array = this.form.get('attendances') as FormArray;
|
|
|
|
const id = array.controls[index].value.attendanceType;
|
2020-10-01 15:21:22 +00:00
|
|
|
const name: string = this.attendanceTypes.filter((x) => x.id === id)[0].name;
|
2018-05-25 13:49:00 +00:00
|
|
|
return name.toLowerCase().replace(/(\s+\+\s+)|(\s+)/g, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
const formValue = this.form.value;
|
|
|
|
this.info.startDate = moment(formValue.startDate).format('DD-MMM-YYYY');
|
|
|
|
this.info.finishDate = moment(formValue.finishDate).format('DD-MMM-YYYY');
|
2019-06-12 14:04:50 +00:00
|
|
|
this.router.navigate(['/employee-attendance', this.info.employee.id], {
|
2018-05-25 13:49:00 +00:00
|
|
|
queryParams: {
|
|
|
|
startDate: this.info.startDate,
|
2020-10-01 15:21:22 +00:00
|
|
|
finishDate: this.info.finishDate,
|
|
|
|
},
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.ser.save(this.getEmployeeAttendance()).subscribe(
|
|
|
|
(result) => {
|
|
|
|
this.toaster.show('Success', '');
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.toaster.show('Danger', error);
|
|
|
|
},
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getEmployeeAttendance(): EmployeeAttendance {
|
|
|
|
const formValue = this.form.value;
|
|
|
|
this.info.startDate = moment(formValue.startDate).format('DD-MMM-YYYY');
|
|
|
|
this.info.finishDate = moment(formValue.finishDate).format('DD-MMM-YYYY');
|
|
|
|
const array = this.form.get('attendances') as FormArray;
|
|
|
|
this.info.body.forEach((item, index) => {
|
|
|
|
item.attendanceType.id = array.controls[index].value.attendanceType;
|
|
|
|
});
|
|
|
|
return this.info;
|
|
|
|
}
|
|
|
|
}
|