258 lines
8.1 KiB
TypeScript
258 lines
8.1 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormControl, FormGroup } from '@angular/forms';
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import * as moment from 'moment';
|
|
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
|
|
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
|
|
|
|
import { AuthService } from '../auth/auth.service';
|
|
import { AccountBalance } from '../core/account-balance';
|
|
import { EmployeeBenefit } from '../core/employee-benefit';
|
|
import { ToasterService } from '../core/toaster.service';
|
|
import { User } from '../core/user';
|
|
import { Voucher } from '../core/voucher';
|
|
import { VoucherService } from '../core/voucher.service';
|
|
import { Employee } from '../employee/employee';
|
|
import { EmployeeService } from '../employee/employee.service';
|
|
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
|
|
|
import { EmployeeBenefitsDataSource } from './employee-benefits-datasource';
|
|
|
|
@Component({
|
|
selector: 'app-employee-benefits',
|
|
templateUrl: './employee-benefits.component.html',
|
|
styleUrls: ['./employee-benefits.component.css'],
|
|
})
|
|
export class EmployeeBenefitsComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('employeeElement', { static: true }) employeeElement?: ElementRef;
|
|
public benefitsObservable = new BehaviorSubject<EmployeeBenefit[]>([]);
|
|
dataSource: EmployeeBenefitsDataSource = new EmployeeBenefitsDataSource(this.benefitsObservable);
|
|
form: FormGroup<{
|
|
date: FormControl<Date>;
|
|
addRow: FormGroup<{
|
|
employee: FormControl<string | null>;
|
|
grossSalary: FormControl<string | null>;
|
|
daysWorked: FormControl<string | null>;
|
|
}>;
|
|
}>;
|
|
|
|
voucher: Voucher = new Voucher();
|
|
employee: Employee | null = null;
|
|
accBal: AccountBalance | null = null;
|
|
|
|
displayedColumns = [
|
|
'name',
|
|
'designation',
|
|
'department',
|
|
'grossSalary',
|
|
'daysWorked',
|
|
'esiEmployee',
|
|
'pfEmployee',
|
|
'esiEmployer',
|
|
'pfEmployer',
|
|
];
|
|
|
|
employees: Observable<Employee[]>;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private dialog: MatDialog,
|
|
private toaster: ToasterService,
|
|
public auth: AuthService,
|
|
private ser: VoucherService,
|
|
private employeeSer: EmployeeService,
|
|
) {
|
|
this.form = new FormGroup({
|
|
date: new FormControl(new Date(), { nonNullable: true }),
|
|
addRow: new FormGroup({
|
|
employee: new FormControl(''),
|
|
grossSalary: new FormControl(''),
|
|
daysWorked: new FormControl(''),
|
|
}),
|
|
});
|
|
// Setup Employee Autocomplete
|
|
this.employees = this.form.controls.addRow.controls.employee.valueChanges.pipe(
|
|
debounceTime(150),
|
|
distinctUntilChanged(),
|
|
switchMap((x) => (x === null ? observableOf([]) : this.employeeSer.autocomplete(x))),
|
|
);
|
|
}
|
|
|
|
static getPf(grossSalary: number, daysWorked: number, daysInMonth: number) {
|
|
const limit = 15000;
|
|
const employeeRate = 0.12;
|
|
const employerRate = 0.12 + 0.011 + 0.005 + 0.0001;
|
|
const employee = grossSalary > limit ? 0 : Math.ceil((employeeRate * grossSalary * daysWorked) / daysInMonth);
|
|
const employer = grossSalary > limit ? 0 : Math.ceil((employerRate * grossSalary * daysWorked) / daysInMonth);
|
|
return { ee: employee, er: employer, both: employee + employer };
|
|
}
|
|
|
|
static getEsi(grossSalary: number, daysWorked: number, daysInMonth: number) {
|
|
const limit = 21000;
|
|
const employeeRate = 0.0175;
|
|
const employerRate = 0.0475;
|
|
const employee = grossSalary > limit ? 0 : Math.ceil((employeeRate * grossSalary * daysWorked) / daysInMonth);
|
|
const employer = grossSalary > limit ? 0 : Math.ceil((employerRate * grossSalary * daysWorked) / daysInMonth);
|
|
return { ee: employee, er: employer, both: employee + employer };
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { voucher: Voucher };
|
|
|
|
this.loadVoucher(data.voucher);
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.employeeElement) {
|
|
this.employeeElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
loadVoucher(voucher: Voucher) {
|
|
this.voucher = voucher;
|
|
this.form.setValue({
|
|
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
|
addRow: {
|
|
employee: '1',
|
|
grossSalary: '',
|
|
daysWorked: '',
|
|
},
|
|
});
|
|
this.dataSource = new EmployeeBenefitsDataSource(this.benefitsObservable);
|
|
this.benefitsObservable.next(this.voucher.employeeBenefits);
|
|
}
|
|
|
|
displayFn(employee?: Employee | string): string {
|
|
return !employee ? '' : typeof employee === 'string' ? employee : employee.name;
|
|
}
|
|
|
|
employeeSelected(event: MatAutocompleteSelectedEvent): void {
|
|
this.employee = event.option.value as Employee;
|
|
}
|
|
|
|
addRow() {
|
|
const oldFiltered = this.voucher.employeeBenefits.filter((x) => x.employee.id === (this.employee as Employee).id);
|
|
if (oldFiltered.length) {
|
|
this.toaster.show('Danger', 'Employee has already been added');
|
|
return;
|
|
}
|
|
|
|
const formValue = this.form.value.addRow;
|
|
if (formValue === undefined) {
|
|
return;
|
|
}
|
|
const grossSalary = Number(formValue.grossSalary);
|
|
const daysWorked = Number(formValue.daysWorked);
|
|
const date = this.form.value.date ?? new Date();
|
|
const daysInMonth = moment(date).daysInMonth();
|
|
const esi = EmployeeBenefitsComponent.getEsi(grossSalary, daysWorked, daysInMonth);
|
|
const pf = EmployeeBenefitsComponent.getPf(grossSalary, daysWorked, daysInMonth);
|
|
|
|
this.voucher.employeeBenefits.push(
|
|
new EmployeeBenefit({
|
|
employee: this.employee as Employee,
|
|
grossSalary,
|
|
daysWorked,
|
|
esiEmployee: esi.ee,
|
|
pfEmployee: pf.ee,
|
|
esiEmployer: esi.er,
|
|
pfEmployer: pf.er,
|
|
}),
|
|
);
|
|
this.benefitsObservable.next(this.voucher.employeeBenefits);
|
|
this.resetAddRow();
|
|
}
|
|
|
|
resetAddRow() {
|
|
this.form.controls.addRow.reset();
|
|
this.employee = null;
|
|
setTimeout(() => {
|
|
if (this.employeeElement) {
|
|
this.employeeElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
deleteRow(row: EmployeeBenefit) {
|
|
this.voucher.employeeBenefits.splice(this.voucher.employeeBenefits.indexOf(row), 1);
|
|
this.benefitsObservable.next(this.voucher.employeeBenefits);
|
|
}
|
|
|
|
canSave() {
|
|
if (!this.voucher.id) {
|
|
return true;
|
|
}
|
|
if (this.voucher.posted && this.auth.allowed('edit-posted-vouchers')) {
|
|
return true;
|
|
}
|
|
return this.voucher.user.id === (this.auth.user as User).id || this.auth.allowed("edit-other-user's-vouchers");
|
|
}
|
|
|
|
post() {
|
|
this.ser.post(this.voucher.id as string).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) => {
|
|
this.toaster.show('Success', '');
|
|
if (voucher.id === result.id) {
|
|
this.loadVoucher(result);
|
|
} else {
|
|
this.router.navigate(['/employee-benefits', 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');
|
|
return this.voucher;
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.voucher.id as string).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigate(['/employee-benefits'], { 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();
|
|
}
|
|
});
|
|
}
|
|
}
|