Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { ComboConstant } from '../../../../../../gaji-id-resources/combo-constants/combo-constant.interface';
- import { Component, OnInit, OnDestroy, Output, EventEmitter, ViewChild } from '@angular/core';
- import { FormGroup, FormBuilder, Validators } from '@angular/forms';
- import { Subject } from 'rxjs';
- import { takeUntil, map } from 'rxjs/operators';
- import { MatAccordion, MatDialog, MatDialogRef, MatDialogConfig } from '@angular/material';
- import { Employee } from 'src/app/gaji-id-resources/resources/employee/data/employee/employee.model';
- import { PagingHelper } from 'src/app/components/helper/paging-helper';
- import { SuratKetidakhadiran, SuratKetidakhadiranKaryawan } from '../../../../../../gaji-id-resources/resources/attendance/transaction/surat-ketidakhadiran/surat-ketidakhadiran.model';
- import { SuratKetidakhadiranKaryawanSearchParams, SuratKetidakhadiranKaryawanSorts } from '../../../../../../gaji-id-resources/resources/attendance/transaction/surat-ketidakhadiran/surat-ketidakhadiran.service';
- import { Workplace } from '../../../../../../gaji-id-resources/resources/master/workplace/workplace.model';
- import { YesNoStates } from '../../../../../../gaji-id-resources/combo-constants/yes-no-states.constant';
- import { ContentAlertService } from '../../../../../../components/messages/alerts/content-alert/content-alert.service';
- import { EmployeeService } from '../../../../../../gaji-id-resources/resources/employee/data/employee/employee.service';
- import { SuratKetidakhadiranService } from '../../../../../../gaji-id-resources/resources/attendance/transaction/surat-ketidakhadiran/surat-ketidakhadiran.service';
- import { UiBlockService } from '../../../../../../components/ui-block/ui-block.service';
- import { ServerDateTimeService } from 'src/app/gaji-id-resources/resources/system/server/server-date-time.service';
- import { WorkplaceService } from '../../../../../../gaji-id-resources/resources/master/workplace/workplace.service';
- import * as _ from 'lodash';
- const ALL_MONTH_NAME = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
- const izin = 'Izin';
- const cuti = ['Annual leave', 'Long leave', 'Replacement leave', 'Sick leave', 'Unpaid leave', 'Special leave', 'Day off payment', 'Replacement public holiday'];
- @Component({
- selector: 'app-attendance-cuti-izin-browse',
- templateUrl: './cuti-izin-browse.component.html'
- })
- export class CutiIzinBrowseComponent implements OnInit, OnDestroy {
- @Output() public onKetidakhadiranBelumKonfirmasi: EventEmitter<boolean> = new EventEmitter();
- @Output() public onKetidakhadiranBelumAdaKeterangan: EventEmitter<boolean> = new EventEmitter();
- @Output() public onInputIzin: EventEmitter<SuratKetidakhadiran> = new EventEmitter();
- @Output() public onInputCuti: EventEmitter<SuratKetidakhadiran> = new EventEmitter();
- public currentDate: Date = new Date();
- public currentMonthName: string = ALL_MONTH_NAME[this.currentDate.getMonth()];
- public currentYear: number = this.currentDate.getFullYear();
- public employeeList: Employee[] = [];
- public loading: boolean;
- public isLoadingResults: boolean;
- public suratKetidakhadiranKaryawanList: SuratKetidakhadiranKaryawan[] = [];
- public ketidakhadiranBelumKonfirmasiCount: number;
- public searchParams: SuratKetidakhadiranKaryawanSearchParams = {};
- public sorts: SuratKetidakhadiranKaryawanSorts = { trxDate: 'desc' };
- public rows: number = 5;
- public totalRecords: number = 0;
- public workplaceList: Workplace[] = [];
- public filterForm: FormGroup;
- public yesNoStates: ComboConstant[] = YesNoStates.getValues();
- public frozenCols: any[] = [];
- public scrollableCols: any[] = [];
- private ngUnsubscribe: Subject<boolean> = new Subject();
- constructor(
- private contentAlertService: ContentAlertService,
- private fb: FormBuilder,
- private employeeService: EmployeeService,
- private serverDateTimeService: ServerDateTimeService,
- private suratKetidakhadiranKaryawanService: SuratKetidakhadiranService,
- private uiBlockService: UiBlockService,
- private workplaceService: WorkplaceService
- ) {
- this.initFilterForm();
- }
- public ngOnInit() {
- this.initServerTime();
- }
- public ngOnDestroy() {
- this.ngUnsubscribe.next(true);
- this.ngUnsubscribe.complete();
- }
- private initServerTime() {
- this.serverDateTimeService.get()
- .pipe(takeUntil(this.ngUnsubscribe))
- .subscribe(
- (result) => {
- this.currentDate = result.data.currentDateTime;
- this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
- this.currentYear = this.currentDate.getFullYear();
- this.patchFilterFormValue();
- this.initTable({
- filters: {},
- first: 0,
- globalFilter: null,
- multiSortMeta: undefined,
- rows: this.rows,
- sortField: undefined,
- sortOrder: 1,
- });
- },
- (error) => {
- this.contentAlertService.error(error.errors);
- }
- );
- }
- private initFilterForm() {
- this.filterForm = this.fb.group({
- employee: null,
- workplace: null,
- tahunBulan: null
- });
- }
- private patchFilterFormValue() {
- const tahunBulan = this.setTahunBulan(this.currentDate.getFullYear(), this.currentDate.getMonth());
- this.filterForm.patchValue({ tahunBulan: tahunBulan });
- this.searchParams.tahunBulan = tahunBulan;
- }
- private setTahunBulan(year, month): string {
- if (month + 1 < 10) {
- month = '0' + (this.currentDate.getMonth() + 1);
- } else {
- month = this.currentDate.getMonth() + 1;
- }
- return `${year}${month}`;
- }
- public initTable(event?: any) {
- const yearNum = this.currentDate.getFullYear();
- const monthNum = this.currentDate.getMonth() + 1;
- const dateCount = new Date(yearNum, monthNum, 0).getDate();
- this.scrollableCols = [];
- for (let i = 1; i <= dateCount; i++) {
- this.scrollableCols.push({ field: new Date(`${yearNum}-${monthNum}-${i}`), header: i });
- }
- this.frozenCols = [
- { field: 'employeeId', header: 'IDKaryawan', rowspan: 2 },
- { field: 'name', header: 'NamaKaryawan', rowspan: 2 }
- ];
- let month;
- if (monthNum < 10) {
- month = '0' + monthNum;
- }
- this.doInstantFilter(event);
- }
- public navigatePrevMonth() {
- const currentMonth = this.currentDate.getMonth();
- const currentYear = this.currentDate.getFullYear();
- const month = (currentMonth === 0) ? 11 : currentMonth - 1;
- const year = (currentMonth === 0) ? currentYear - 1 : currentYear;
- this.currentDate = new Date(year, month);
- this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
- this.currentYear = this.currentDate.getFullYear();
- this.searchParams.tahunBulan = this.setTahunBulan(year, month);
- this.initTable({
- filters: {},
- first: 0,
- globalFilter: null,
- multiSortMeta: undefined,
- rows: this.rows,
- sortField: undefined,
- sortOrder: 1,
- });
- }
- public navigateNextMonth() {
- const currentMonth = this.currentDate.getMonth();
- const currentYear = this.currentDate.getFullYear();
- const month = (currentMonth === 11) ? 0 : currentMonth + 1;
- const year = (currentMonth === 11) ? currentYear + 1 : currentYear;
- this.currentDate = new Date(year, month);
- this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
- this.currentYear = this.currentDate.getFullYear();
- this.searchParams.tahunBulan = this.setTahunBulan(year, month);
- this.initTable({
- filters: {},
- first: 0,
- globalFilter: null,
- multiSortMeta: undefined,
- rows: this.rows,
- sortField: undefined,
- sortOrder: 1,
- });
- }
- public onEmployeeLazyLoad(event) {
- this.loading = true;
- this.employeeService.search({ name: event })
- .pipe(takeUntil(this.ngUnsubscribe))
- .subscribe(
- (result) => {
- this.loading = false;
- this.employeeList = result.data;
- },
- (error) => {
- this.loading = false;
- this.contentAlertService.error(error.errors);
- },
- () => { this.loading = false; }
- );
- }
- public onWorkplaceLazyLoad(event) {
- this.loading = true;
- this.workplaceService.search({ name: event })
- .pipe(takeUntil(this.ngUnsubscribe))
- .subscribe(
- (result) => {
- this.loading = false;
- this.workplaceList = result.data;
- },
- (error) => {
- this.loading = false;
- this.contentAlertService.error(error.errors);
- },
- () => { this.loading = false; }
- );
- }
- public doInstantFilter(event?: any) {
- this.contentAlertService.clear();
- if (event) {
- const result = PagingHelper.getPaging(event);
- result.searchParams = this.searchParams;
- if (result.sorts === null) {
- result.sorts = this.sorts;
- }
- if (this.searchParams.tahunBulan != null) {
- this.isLoadingResults = true;
- this.suratKetidakhadiranKaryawanService.searchSuratKetidakhadiranKaryawan(result.searchParams, result.sorts, result.paging)
- .pipe(takeUntil(this.ngUnsubscribe))
- .subscribe(
- (result) => {
- this.isLoadingResults = false;
- this.totalRecords = result.meta.pagination.dataCount;
- let totalClonedSuratKetidakhadiran: SuratKetidakhadiran[] = [];
- result.data.forEach((item) => {
- totalClonedSuratKetidakhadiran = totalClonedSuratKetidakhadiran.concat(this.cloneSuratKetidakhadiran(item));
- });
- this.suratKetidakhadiranKaryawanList = this.convertToSuratKetidakhadiranKaryawan(totalClonedSuratKetidakhadiran);
- },
- (error) => {
- this.isLoadingResults = false;
- this.contentAlertService.error(error.errors);
- },
- () => { this.isLoadingResults = false; }
- );
- }
- }
- }
- // Returns an array of Surat Ketidakhadiran between the two dates (tanggalAwal - tanggalAkhir)
- private cloneSuratKetidakhadiran(suratKetidakhadiran: SuratKetidakhadiran): SuratKetidakhadiran[] {
- if (suratKetidakhadiran.hasOwnProperty('tanggalAwal') && suratKetidakhadiran.hasOwnProperty('tanggalAkhir')) {
- const clonedSuratKetidakhadiran = [];
- let currentDate = new Date(suratKetidakhadiran.tanggalAwal);
- const tanggalAkhir = new Date(suratKetidakhadiran.tanggalAkhir);
- suratKetidakhadiran.tanggal = null;
- const addDays = function (days) {
- const date = new Date(this.valueOf());
- date.setDate(date.getDate() + days);
- return date;
- };
- while (currentDate <= tanggalAkhir) {
- const newSuratKetidakhadiran = Object.assign({}, suratKetidakhadiran);
- newSuratKetidakhadiran.tanggal = currentDate;
- clonedSuratKetidakhadiran.push(newSuratKetidakhadiran);
- currentDate = addDays.call(currentDate, 1);
- }
- return clonedSuratKetidakhadiran;
- } else {
- console.log('Object format is invalid');
- return [];
- }
- }
- private convertToSuratKetidakhadiranKaryawan(
- suratKetidakhadiranList: SuratKetidakhadiran[]): SuratKetidakhadiranKaryawan[] {
- const suratKetidakhadiranKaryawanList = [];
- const getKaryawanIdList = suratKetidakhadiranList.map(
- function (item, index) {
- return item.karyawan.id;
- });
- const groupedKaryawan = getKaryawanIdList.filter(
- function (filterItem, filterIndex) {
- return getKaryawanIdList.indexOf(filterItem) === filterIndex;
- });
- for (let i = 0; i < groupedKaryawan.length; i++) {
- const suratKetidakhadiranKaryawan = new SuratKetidakhadiranKaryawan();
- suratKetidakhadiranList.map(
- function (item, index) {
- if (groupedKaryawan[i] === item.karyawan.id) {
- suratKetidakhadiranKaryawan.karyawan = item.karyawan;
- suratKetidakhadiranKaryawan.suratKetidakhadiranList.push(item);
- }
- }
- );
- suratKetidakhadiranKaryawanList.push(suratKetidakhadiranKaryawan);
- }
- return suratKetidakhadiranKaryawanList;
- }
- public filter() {
- this.searchParams.karyawanId = this.filterForm.value.employee ? this.filterForm.value.employee.id : null;
- this.searchParams.lokasiKerjaId = this.filterForm.value.workplace ? this.filterForm.value.workplace.id : null;
- this.searchParams.tahunBulan = this.filterForm.value.tahunBulan;
- const year = this.searchParams.tahunBulan.substring(0, 4);
- const month = this.searchParams.tahunBulan.substring(4, 6);
- this.currentDate = new Date(year + '-' + month);
- this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
- this.currentYear = this.currentDate.getFullYear();
- this.initTable({
- filters: {},
- first: 0,
- globalFilter: null,
- multiSortMeta: undefined,
- rows: this.rows,
- sortField: undefined,
- sortOrder: 1,
- });
- }
- public addCuti() {
- this.onInputCuti.emit();
- }
- public addIzin() {
- this.onInputIzin.emit();
- }
- public edit(suratKetidakhadiran: SuratKetidakhadiran) {
- const ketidakhadiran = suratKetidakhadiran.jenisAbsensi.sdKelompokJenisAbsensi.nama;
- if (cuti.indexOf(ketidakhadiran) > -1) {
- this.onInputCuti.emit(suratKetidakhadiran);
- } else if (izin === ketidakhadiran) {
- this.onInputIzin.emit(suratKetidakhadiran);
- }
- }
- public ketidakhadiranBelumKonfirmasi() {
- this.onKetidakhadiranBelumKonfirmasi.next(true);
- }
- public ketidakhadiranBelumAdaKeterangan() {
- this.onKetidakhadiranBelumAdaKeterangan.next(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment