import { Component, ViewEncapsulation, Inject } from '@angular/core'; import { Observable } from 'rxjs'; import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; import { process, State } from '@progress/kendo-data-query'; import { map } from 'rxjs/operators'; import { SelectItem } from 'primeng/api'; import { FormGroup, FormControl } from '@angular/forms'; import { AttnRawData } from '../../_models/Attendance/attnRawData' import { EnumEntryMode, EnumExtension } from '../../_models/enums'; import { HRMNotificationService } from '../../app.notification.service'; import { EmployeeServices } from '../../_services/employee/employee.service'; import { Employee } from '../../_models/Employee/employee'; import { AttendanceServices } from '../../_services/attendance/attendance.service'; import { SearchEmployee } from '../../_models/Employee/searchEmployee'; import { loadingPanelService } from '../../hrm-loding panel/loding.panel.service'; import { ApiService } from '../../app.api.service'; @Component({ selector: 'app-read-attendance-rawdata', templateUrl: './read-attendance-rawdata.component.html', //styleUrls: ['../../../assets/kendo-grid/grid.css'], encapsulation: ViewEncapsulation.None }) export class ReadAttendanceRawdataComponent { selectedEmployees: SearchEmployee[] = []; attnRawDataList: AttnRawData[] = []; employeeIdList: number[] = []; employeeIds: string; rawDataForm: FormGroup; cardStatuses: SelectItem[]; public attnRawDatas: AttnRawData[] = []; private uploadedLines: string[][]; public uploadeddata: csvFileData; public uploadeddatas: csvFileData[] = []; public faultyItems: csvFileData[] = []; public employees: Employee[] = []; selectedCard: any; entryModes: any = EnumExtension.getNamesAndValues(EnumEntryMode); format: string = ".csv (Column Names: EmpNo,PunchTime)"; // (Date Time Format: MM/dd/yyyy h:mm) public gridView: GridDataResult; public gridView2: GridDataResult; public mandatoryItems: mandatoryData[] = []; public view: Observable; public gridState: State = { sort: [], skip: 0, take: 10 }; public file: File; fromDate: Date = new Date(); toDate: Date = new Date(); public mySelection: string[] = []; public isNew: boolean; public pageSize = 20; public skip = 0; public pageSize2 = 20; public skip2 = 0; /** user-role-list ctor */ //constructor(@Inject(ReadAttendanceRawdataService) readAttendanceRawdataServiceFactory: any) { // this.readAttendanceRawdataService = readAttendanceRawdataServiceFactory(); //} constructor(public _apiService: ApiService, public _notificationService: HRMNotificationService, public _empService: EmployeeServices, public _attendanceServices: AttendanceServices, public loadingService: loadingPanelService ) { this._apiService.selectedMenuName = "Read Raw Data"; this.loadingService.ShowLoadingPanel = true; this._empService.getAllEmployees().subscribe( (resp: any) => { this.employees = resp; }, (err: any) => { this._notificationService.showError(err); this.loadingService.ShowLoadingPanel = false; }, () => { this.loadingService.ShowLoadingPanel = false; } ); //this.readAttendanceRawdataService = readAttendanceRawdataServiceFactory(); } public ngOnInit(): void { this.rawDataForm = new FormGroup({ type: new FormControl(), attachment: new FormControl(), fromDate: new FormControl(), toDate: new FormControl() }); //this.cardFoundForm = new FormGroup({ // card: new FormControl(), // FoundDate: new FormControl(), // comment: new FormControl() //}); this.cardStatuses = []; this.cardStatuses.push({ label: 'Select Card', value: '' }); this.cardStatuses.push({ label: 'Attached', value: '1' }); this.cardStatuses.push({ label: 'Unattached', value: '2' }); // this.view = this.readAttendanceRawdataService.pipe(map(data => process(data, this.gridState))); // this.readAttendanceRawdataService.read(); // this.gridView = this.gridData; } fileUpload(event) { this.loadingService.ShowLoadingPanel = true; this.file = undefined; this.attnRawDatas = []; this.mandatoryItems = []; this.faultyItems = []; this.uploadeddatas = []; this.file = event.target.files[0]; var reader = new FileReader(); reader.onload = () => { this.performReading(reader); }; reader.readAsText(this.file); this.loadingService.ShowLoadingPanel = false; } performReading(reader: any) { var text = undefined; //this.attnRawDatas = []; //this.mandatoryItems = []; //this.faultyItems = []; text = reader.result; reader = undefined; var regex = /"[^"]*"/g; text = text.replace(regex, function (match, capture) { // new line is replaced with space in double quoted string; return match.replace(/[\n\r]\s*/g, " "); }); //console.log(text); var lines = text.split("\n"); //console.log(lines); this.uploadedLines = []; try { for (var i = 1; i < lines.length; i++) { this.uploadeddata = new csvFileData(); if (lines[i] !== "") { //console.log(lines[i]); var temp = lines[i].split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/); //spliting comma separated values but ignoring commas in double quoted string this.uploadeddata.rowNo = i.toString(); this.uploadeddata.employeeNo = temp[0].replace(/^"(.*)"$/, '$1'); this.uploadeddata.punchTime = temp[1].replace(/^"(.*)"$/, '$1'); //this.uploadeddata.punchTime = temp[2].replace(/^"(.*)"$/, '$1'); //this.uploadeddata.deviceNo = temp[3].replace(/^"(.*)"$/, '$1'); //this.uploadeddata.deviceIPAddress = temp[4].replace(/^"(.*)"$/, '$1'); //this.uploadeddata.entryMode = temp[5].replace(/^"(.*)"$/, '$1'); this.uploadeddata.employeeNo = this.uploadeddata.employeeNo.replace(/\s/g, ''); //this.uploadeddata.cardNo = this.uploadeddata.cardNo.replace(/\s/g, ''); //this.uploadeddata.entryMode = this.uploadeddata.entryMode.replace(/\s/g, ''); ////console.log(this.uploadeddata.EmpID); this.uploadeddatas.push(this.uploadeddata); } } this.mandatoryDataChecker(this.uploadeddatas); this.errorChecker(this.uploadeddatas); this.loadGrid2(); //console.log(this.uploadeddatas); } catch (e) { //this.no.showError(e); } // //console.log(this.iJobs); } fileUploadChange(event) { this.loadingService.ShowLoadingPanel = true; this.file = undefined; this.attnRawDatas = []; this.file = event.target.files[0]; var reader = new FileReader(); reader.onload = () => { this.performReading(reader); }; reader.readAsText(this.file); this.loadingService.ShowLoadingPanel = false; } public upload() { if (this.uploadeddata == undefined || this.uploadeddatas.length <= 0) { this._notificationService.showError("Data not defined"); return; } if (this.mandatoryItems !== undefined && this.mandatoryItems.length > 0) { this._notificationService.showError("Employee No and PunchTime cannot be empty"); return; } if (this.faultyItems !== undefined && this.faultyItems.length > 0) { this._notificationService.showError("Employee No not Found"); return; } if (this.mandatoryItems.length <= 0) { this.attnRawDatas = []; this.uploadeddatas.forEach(x => { var item = new AttnRawData(); //const localDate: Date = this.convertToLocalDate(x.punchTime);//now working //item.punchTime = localDate; //var d = x.punchTime; //var d1 = d.split(" "); //var date = d1[0].split("/"); //var time = d1[1].split(":"); //var dd = Number(date[0]); //var mm = Number(date[1]) - 1; //var yy = Number(date[2]); //var hh = Number(time[0]); //var min = Number(time[1]); //var ss = Number(time[2]); //var fromdt = new Date(yy, mm, dd, hh, min, ss); ////console.log(fromdt); //item.punchTime = fromdt; ////// working if the format is dd/mm/yyyy hh:mm:ss item.punchTime = new Date; // otherwise error item.punchTimeDateString = x.punchTime; //console.log(item.punchTime); item.employeeID = this.employees.find(e => e.employeeNo.trim().toLowerCase() == x.employeeNo.trim().toLowerCase()) == undefined ? 0 : this.employees.find(e => e.employeeNo.trim().toLowerCase() == x.employeeNo.trim().toLowerCase()).id; item.employeeNo = x.employeeNo, item.entryMode = (x.entryMode == undefined || x.entryMode.length <= 0) ? 0 : this.entryModes.find(e => e.name.trim().toLowerCase() == x.entryMode.trim().toLowerCase()).value, item.cardNo = (x.cardNo !== undefined || x.cardNo !== "" || x.cardNo !== null) ? item.employeeNo : item.cardNo, item.deviceNo = (x.deviceNo !== undefined || x.deviceNo !== "" || x.deviceNo !== null) ? null : item.deviceNo, item.deviceIPAddress = (x.deviceIPAddress !== undefined || x.deviceIPAddress !== "" || x.deviceIPAddress !== null) ? null : item.deviceIPAddress this.attnRawDatas.push(item); }); //console.log(this.attnRawDatas); let data = { dataList: this.attnRawDatas } this.loadingService.ShowLoadingPanel = true; this._attendanceServices.saveAttnRawData(this.attnRawDatas).subscribe( (resp: any) => { let x = resp; }, (err: any) => { this._notificationService.showError(err.error); this.loadingService.ShowLoadingPanel = false; }, () => { this.loadingService.ShowLoadingPanel = false; this._notificationService.showSuccess("Save Successfull"); } ); } else { this._notificationService.showError("Please solve the errors"); return; } } convertToLocalDate(responseDate: any) { let minDate = new Date(-8640000000000000); try { if (responseDate != null) { if (typeof (responseDate) === 'string') { if (String(responseDate.indexOf('T') >= 0)) { responseDate = responseDate.split('T')[0]; } if (String(responseDate.indexOf('+') >= 0)) { responseDate = responseDate.split('+')[0]; } } responseDate = new Date(responseDate); const newDate = new Date(responseDate.getFullYear(), responseDate.getMonth(), responseDate.getDate(), responseDate.getHours(), responseDate.getMinutes(), responseDate.getSeconds()); const userTimezoneOffset = newDate.getTimezoneOffset() * 60000; const finalDate: Date = new Date(newDate.getTime() - userTimezoneOffset); // return finalDate > Util.minDateValue ? finalDate : null; return finalDate > minDate ? finalDate : null; } else { return null; } } catch (error) { return responseDate; } } mandatoryDataChecker(uploadeddatas: csvFileData[]) { this.mandatoryItems = []; if (uploadeddatas != undefined && uploadeddatas.length > 0) { for (var i = 0; i < uploadeddatas.length; i++) { var item = new csvFileData(); if (uploadeddatas[i].employeeNo === undefined || uploadeddatas[i].employeeNo === "") { var mandatoryItem = new mandatoryData(); mandatoryItem.EmpNo = uploadeddatas[i].employeeNo; mandatoryItem.ColumnName = "EmployeeNo"; mandatoryItem.RowNo = (i + 1).toString(); mandatoryItem.Error = "EmployeeNo cannot be empty"; this.mandatoryItems.push(mandatoryItem); } if (uploadeddatas[i].punchTime === undefined || uploadeddatas[i].punchTime === "") { var mandatoryItem = new mandatoryData(); mandatoryItem.ColumnName = "PunchTime"; mandatoryItem.EmpNo = uploadeddatas[i].employeeNo; mandatoryItem.RowNo = (i + 1).toString(); mandatoryItem.Error = "Punch Time cannanot be empty"; this.mandatoryItems.push(mandatoryItem); } } } } searchForm() { const fromDate = (this.fromDate === undefined || this.fromDate === null) ? null : this.fromDate.toDateString(); const toDate = (this.toDate === undefined || this.toDate === null) ? null : this.toDate.toDateString(); if (this.employeeIdList != undefined && this.employeeIdList.length > 0) { this.employeeIds = this.employeeIdList.join(); } else { this.employeeIds = null; } this.loadingService.ShowLoadingPanel = true; this._attendanceServices.getAttnRawData(fromDate, toDate, this.employeeIds).subscribe( (resp: any) => { this.attnRawDataList = resp as any; console.log(resp); debugger; }, (err: any) => { this._notificationService.showError(err); this.loadingService.ShowLoadingPanel = false; }, () => { this.loadGrid(); this.loadingService.ShowLoadingPanel = false; // this._notificationService.showSuccess("Save Successfull"); } ); } public loadGrid(): void { this.gridView = { data: this.attnRawDataList.slice(this.skip, this.skip + this.pageSize), total: this.attnRawDataList.length }; } public loadGrid2(): void { this.gridView2 = { data: this.mandatoryItems.slice(this.skip2, this.skip2 + this.pageSize2), total: this.mandatoryItems.length }; } public GetSelectedEmployee(childData) { this.selectedEmployees = childData; if (this.selectedEmployees != undefined && this.selectedEmployees.length > 0) { this.selectedEmployees.forEach(x => { var employee = new Employee(); employee.id = x.employeeID; employee.employeeNo = x.employeeNo; employee.name = x.name; employee.departmentID = x.departmentID; employee.gradeID = x.gradeID; employee.designationID = x.designationID; employee.locationID = x.locationID; employee.cardID = x.cardID; employee.gradeName = x.gradeName; employee.designationName = x.designationName; this.employeeIdList.push(x.employeeID); //irEmployee.employee = employee; //this.internalRecruitment.irEmployees.push(irEmployee); }) } //console.log(this.selectedEmployees); } public errorChecker(uploadeddatas: csvFileData[]): void { this.faultyItems = []; let count = 0; // this.iJobs = []; if (uploadeddatas != undefined && uploadeddatas.length > 0) { for (let item of uploadeddatas) { count++; // var job = new Job(); //if (item.punchTime == undefined) { // var mandatoryItem = new mandatoryData(); // mandatoryItem.ColumnName = "Campaign"; // mandatoryItem.RowNo = count.toString(); // mandatoryItem.Error = "Campaign Not Found"; // this.mandatoryItems.push(mandatoryItem); // this.faultyItems.push(item); //} var employeeId = 0; if (this.employees != undefined) { var employee = this.employees.find(x => x.employeeNo.trim() === item.employeeNo.trim()); if (employee == undefined) { var mandatoryItem = new mandatoryData(); mandatoryItem.ColumnName = "EmployeeNo"; mandatoryItem.EmpNo = item.employeeNo; mandatoryItem.RowNo = count.toString(); mandatoryItem.Error = "Employee Not Found"; this.mandatoryItems.push(mandatoryItem); this.faultyItems.push(item); } } } } } public pageChange(event: PageChangeEvent): void { this.skip = event.skip; this.gridView = { data: this.attnRawDataList.slice(this.skip, this.skip + this.pageSize), total: this.attnRawDataList.length }; } public pageChange2(event: PageChangeEvent): void { this.skip2 = event.skip; this.gridView2 = { data: this.mandatoryItems.slice(this.skip2, this.skip2 + this.pageSize2), total: this.mandatoryItems.length }; } //public onFilter(inputValue: string): void { // this.gridView = process(this.gridData, { // filter: { // logic: "or", // filters: [ // ], // } // }).data; // //this.dataBinding.skip = 0; //} //public onStateChange(state: State) { // this.gridState = state; // this.readAttendanceRawdataService.read(); //} //public addHandler() { // this.editDataItem = new readAttendanceRawData(); // this.isNew = true; //} //public editHandler({ dataItem }) { // this.editDataItem = dataItem; // this.isNew = false; //} //public cancelHandler() { // this.editDataItem = undefined; //} //public saveHandler(readAttendanceRawdata: readAttendanceRawData) { // this.readAttendanceRawdataService.save(readAttendanceRawdata, this.isNew); // this.editDataItem = undefined; //} //public removeHandler({ dataItem }) { // this.readAttendanceRawdataService.remove(dataItem); //} } export class csvFileData { rowNo: string; employeeID: number; cardNo: string; employeeNo: string; punchTime: string; fromTime: Date; entryMode: string; deviceIPAddress: string; deviceNo: string; } export class mandatoryData { ColumnName: string; RowNo: string; Error: string; EmpNo: string; }