ronaldkwandy

Untitled

May 26th, 2020
1,509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ComboConstant } from '../../../../../../gaji-id-resources/combo-constants/combo-constant.interface';
  2. import { Component, OnInit, OnDestroy, Output, EventEmitter, ViewChild } from '@angular/core';
  3. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  4.  
  5. import { Subject } from 'rxjs';
  6. import { takeUntil, map } from 'rxjs/operators';
  7.  
  8. import { MatAccordion, MatDialog, MatDialogRef, MatDialogConfig } from '@angular/material';
  9.  
  10. import { Employee } from 'src/app/gaji-id-resources/resources/employee/data/employee/employee.model';
  11. import { PagingHelper } from 'src/app/components/helper/paging-helper';
  12. import { SuratKetidakhadiran, SuratKetidakhadiranKaryawan } from '../../../../../../gaji-id-resources/resources/attendance/transaction/surat-ketidakhadiran/surat-ketidakhadiran.model';
  13. import { SuratKetidakhadiranKaryawanSearchParams, SuratKetidakhadiranKaryawanSorts } from '../../../../../../gaji-id-resources/resources/attendance/transaction/surat-ketidakhadiran/surat-ketidakhadiran.service';
  14. import { Workplace } from '../../../../../../gaji-id-resources/resources/master/workplace/workplace.model';
  15. import { YesNoStates } from '../../../../../../gaji-id-resources/combo-constants/yes-no-states.constant';
  16.  
  17. import { ContentAlertService } from '../../../../../../components/messages/alerts/content-alert/content-alert.service';
  18. import { EmployeeService } from '../../../../../../gaji-id-resources/resources/employee/data/employee/employee.service';
  19. import { SuratKetidakhadiranService } from '../../../../../../gaji-id-resources/resources/attendance/transaction/surat-ketidakhadiran/surat-ketidakhadiran.service';
  20. import { UiBlockService } from '../../../../../../components/ui-block/ui-block.service';
  21. import { ServerDateTimeService } from 'src/app/gaji-id-resources/resources/system/server/server-date-time.service';
  22. import { WorkplaceService } from '../../../../../../gaji-id-resources/resources/master/workplace/workplace.service';
  23.  
  24. import * as _ from 'lodash';
  25.  
  26.  
  27. const ALL_MONTH_NAME = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
  28. const izin = 'Izin';
  29. const cuti = ['Annual leave', 'Long leave', 'Replacement leave', 'Sick leave', 'Unpaid leave', 'Special leave', 'Day off payment', 'Replacement public holiday'];
  30.  
  31.  
  32. @Component({
  33.   selector: 'app-attendance-cuti-izin-browse',
  34.   templateUrl: './cuti-izin-browse.component.html'
  35. })
  36. export class CutiIzinBrowseComponent implements OnInit, OnDestroy {
  37.  
  38.   @Output() public onKetidakhadiranBelumKonfirmasi: EventEmitter<boolean> = new EventEmitter();
  39.   @Output() public onKetidakhadiranBelumAdaKeterangan: EventEmitter<boolean> = new EventEmitter();
  40.   @Output() public onInputIzin: EventEmitter<SuratKetidakhadiran> = new EventEmitter();
  41.   @Output() public onInputCuti: EventEmitter<SuratKetidakhadiran> = new EventEmitter();
  42.  
  43.   public currentDate: Date = new Date();
  44.   public currentMonthName: string = ALL_MONTH_NAME[this.currentDate.getMonth()];
  45.   public currentYear: number = this.currentDate.getFullYear();
  46.   public employeeList: Employee[] = [];
  47.   public loading: boolean;
  48.   public isLoadingResults: boolean;
  49.   public suratKetidakhadiranKaryawanList: SuratKetidakhadiranKaryawan[] = [];
  50.   public ketidakhadiranBelumKonfirmasiCount: number;
  51.   public searchParams: SuratKetidakhadiranKaryawanSearchParams = {};
  52.   public sorts: SuratKetidakhadiranKaryawanSorts = { trxDate: 'desc' };
  53.   public rows: number = 5;
  54.   public totalRecords: number = 0;
  55.   public workplaceList: Workplace[] = [];
  56.  
  57.   public filterForm: FormGroup;
  58.   public yesNoStates: ComboConstant[] = YesNoStates.getValues();
  59.  
  60.   public frozenCols: any[] = [];
  61.   public scrollableCols: any[] = [];
  62.  
  63.   private ngUnsubscribe: Subject<boolean> = new Subject();
  64.  
  65.   constructor(
  66.     private contentAlertService: ContentAlertService,
  67.     private fb: FormBuilder,
  68.     private employeeService: EmployeeService,
  69.     private serverDateTimeService: ServerDateTimeService,
  70.     private suratKetidakhadiranKaryawanService: SuratKetidakhadiranService,
  71.     private uiBlockService: UiBlockService,
  72.     private workplaceService: WorkplaceService
  73.   ) {
  74.     this.initFilterForm();
  75.   }
  76.  
  77.   public ngOnInit() {
  78.     this.initServerTime();
  79.   }
  80.  
  81.   public ngOnDestroy() {
  82.     this.ngUnsubscribe.next(true);
  83.     this.ngUnsubscribe.complete();
  84.   }
  85.  
  86.   private initServerTime() {
  87.     this.serverDateTimeService.get()
  88.       .pipe(takeUntil(this.ngUnsubscribe))
  89.       .subscribe(
  90.         (result) => {
  91.           this.currentDate = result.data.currentDateTime;
  92.           this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
  93.           this.currentYear = this.currentDate.getFullYear();
  94.           this.patchFilterFormValue();
  95.           this.initTable({
  96.             filters: {},
  97.             first: 0,
  98.             globalFilter: null,
  99.             multiSortMeta: undefined,
  100.             rows: this.rows,
  101.             sortField: undefined,
  102.             sortOrder: 1,
  103.           });
  104.         },
  105.         (error) => {
  106.           this.contentAlertService.error(error.errors);
  107.         }
  108.       );
  109.   }
  110.  
  111.   private initFilterForm() {
  112.     this.filterForm = this.fb.group({
  113.       employee: null,
  114.       workplace: null,
  115.       tahunBulan: null
  116.     });
  117.   }
  118.  
  119.   private patchFilterFormValue() {
  120.     const tahunBulan = this.setTahunBulan(this.currentDate.getFullYear(), this.currentDate.getMonth());
  121.     this.filterForm.patchValue({ tahunBulan: tahunBulan });
  122.     this.searchParams.tahunBulan = tahunBulan;
  123.   }
  124.  
  125.   private setTahunBulan(year, month): string {
  126.     if (month + 1 < 10) {
  127.       month = '0' + (this.currentDate.getMonth() + 1);
  128.     } else {
  129.       month = this.currentDate.getMonth() + 1;
  130.     }
  131.     return `${year}${month}`;
  132.   }
  133.  
  134.   public initTable(event?: any) {
  135.     const yearNum = this.currentDate.getFullYear();
  136.     const monthNum = this.currentDate.getMonth() + 1;
  137.  
  138.     const dateCount = new Date(yearNum, monthNum, 0).getDate();
  139.  
  140.     this.scrollableCols = [];
  141.  
  142.     for (let i = 1; i <= dateCount; i++) {
  143.       this.scrollableCols.push({ field: new Date(`${yearNum}-${monthNum}-${i}`), header: i });
  144.     }
  145.  
  146.     this.frozenCols = [
  147.       { field: 'employeeId', header: 'IDKaryawan', rowspan: 2 },
  148.       { field: 'name', header: 'NamaKaryawan', rowspan: 2 }
  149.     ];
  150.  
  151.     let month;
  152.     if (monthNum < 10) {
  153.       month = '0' + monthNum;
  154.     }
  155.     this.doInstantFilter(event);
  156.   }
  157.  
  158.   public navigatePrevMonth() {
  159.     const currentMonth = this.currentDate.getMonth();
  160.     const currentYear = this.currentDate.getFullYear();
  161.     const month = (currentMonth === 0) ? 11 : currentMonth - 1;
  162.     const year = (currentMonth === 0) ? currentYear - 1 : currentYear;
  163.  
  164.     this.currentDate = new Date(year, month);
  165.     this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
  166.     this.currentYear = this.currentDate.getFullYear();
  167.  
  168.     this.searchParams.tahunBulan = this.setTahunBulan(year, month);
  169.     this.initTable({
  170.       filters: {},
  171.       first: 0,
  172.       globalFilter: null,
  173.       multiSortMeta: undefined,
  174.       rows: this.rows,
  175.       sortField: undefined,
  176.       sortOrder: 1,
  177.     });
  178.   }
  179.  
  180.   public navigateNextMonth() {
  181.     const currentMonth = this.currentDate.getMonth();
  182.     const currentYear = this.currentDate.getFullYear();
  183.     const month = (currentMonth === 11) ? 0 : currentMonth + 1;
  184.     const year = (currentMonth === 11) ? currentYear + 1 : currentYear;
  185.  
  186.     this.currentDate = new Date(year, month);
  187.     this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
  188.     this.currentYear = this.currentDate.getFullYear();
  189.  
  190.     this.searchParams.tahunBulan = this.setTahunBulan(year, month);
  191.     this.initTable({
  192.       filters: {},
  193.       first: 0,
  194.       globalFilter: null,
  195.       multiSortMeta: undefined,
  196.       rows: this.rows,
  197.       sortField: undefined,
  198.       sortOrder: 1,
  199.     });
  200.   }
  201.  
  202.   public onEmployeeLazyLoad(event) {
  203.     this.loading = true;
  204.     this.employeeService.search({ name: event })
  205.       .pipe(takeUntil(this.ngUnsubscribe))
  206.       .subscribe(
  207.         (result) => {
  208.           this.loading = false;
  209.           this.employeeList = result.data;
  210.         },
  211.         (error) => {
  212.           this.loading = false;
  213.           this.contentAlertService.error(error.errors);
  214.         },
  215.         () => { this.loading = false; }
  216.       );
  217.   }
  218.  
  219.   public onWorkplaceLazyLoad(event) {
  220.     this.loading = true;
  221.     this.workplaceService.search({ name: event })
  222.       .pipe(takeUntil(this.ngUnsubscribe))
  223.       .subscribe(
  224.         (result) => {
  225.           this.loading = false;
  226.           this.workplaceList = result.data;
  227.         },
  228.         (error) => {
  229.           this.loading = false;
  230.           this.contentAlertService.error(error.errors);
  231.         },
  232.         () => { this.loading = false; }
  233.       );
  234.   }
  235.  
  236.   public doInstantFilter(event?: any) {
  237.     this.contentAlertService.clear();
  238.     if (event) {
  239.       const result = PagingHelper.getPaging(event);
  240.       result.searchParams = this.searchParams;
  241.       if (result.sorts === null) {
  242.         result.sorts = this.sorts;
  243.       }
  244.  
  245.       if (this.searchParams.tahunBulan != null) {
  246.         this.isLoadingResults = true;
  247.         this.suratKetidakhadiranKaryawanService.searchSuratKetidakhadiranKaryawan(result.searchParams, result.sorts, result.paging)
  248.           .pipe(takeUntil(this.ngUnsubscribe))
  249.           .subscribe(
  250.             (result) => {
  251.               this.isLoadingResults = false;
  252.  
  253.               this.totalRecords = result.meta.pagination.dataCount;
  254.               let totalClonedSuratKetidakhadiran: SuratKetidakhadiran[] = [];
  255.               result.data.forEach((item) => {
  256.                 totalClonedSuratKetidakhadiran = totalClonedSuratKetidakhadiran.concat(this.cloneSuratKetidakhadiran(item));
  257.               });
  258.               this.suratKetidakhadiranKaryawanList = this.convertToSuratKetidakhadiranKaryawan(totalClonedSuratKetidakhadiran);
  259.             },
  260.             (error) => {
  261.               this.isLoadingResults = false;
  262.               this.contentAlertService.error(error.errors);
  263.             },
  264.             () => { this.isLoadingResults = false; }
  265.           );
  266.       }
  267.     }
  268.   }
  269.  
  270.   // Returns an array of Surat Ketidakhadiran between the two dates (tanggalAwal - tanggalAkhir)
  271.   private cloneSuratKetidakhadiran(suratKetidakhadiran: SuratKetidakhadiran): SuratKetidakhadiran[] {
  272.  
  273.     if (suratKetidakhadiran.hasOwnProperty('tanggalAwal') && suratKetidakhadiran.hasOwnProperty('tanggalAkhir')) {
  274.       const clonedSuratKetidakhadiran = [];
  275.       let currentDate = new Date(suratKetidakhadiran.tanggalAwal);
  276.       const tanggalAkhir = new Date(suratKetidakhadiran.tanggalAkhir);
  277.       suratKetidakhadiran.tanggal = null;
  278.  
  279.       const addDays = function (days) {
  280.         const date = new Date(this.valueOf());
  281.         date.setDate(date.getDate() + days);
  282.         return date;
  283.       };
  284.       while (currentDate <= tanggalAkhir) {
  285.         const newSuratKetidakhadiran = Object.assign({}, suratKetidakhadiran);
  286.         newSuratKetidakhadiran.tanggal = currentDate;
  287.         clonedSuratKetidakhadiran.push(newSuratKetidakhadiran);
  288.         currentDate = addDays.call(currentDate, 1);
  289.       }
  290.       return clonedSuratKetidakhadiran;
  291.     } else {
  292.       console.log('Object format is invalid');
  293.       return [];
  294.     }
  295.   }
  296.  
  297.   private convertToSuratKetidakhadiranKaryawan(
  298.     suratKetidakhadiranList: SuratKetidakhadiran[]): SuratKetidakhadiranKaryawan[] {
  299.  
  300.     const suratKetidakhadiranKaryawanList = [];
  301.  
  302.     const getKaryawanIdList = suratKetidakhadiranList.map(
  303.       function (item, index) {
  304.         return item.karyawan.id;
  305.       });
  306.  
  307.     const groupedKaryawan = getKaryawanIdList.filter(
  308.       function (filterItem, filterIndex) {
  309.         return getKaryawanIdList.indexOf(filterItem) === filterIndex;
  310.       });
  311.  
  312.     for (let i = 0; i < groupedKaryawan.length; i++) {
  313.       const suratKetidakhadiranKaryawan = new SuratKetidakhadiranKaryawan();
  314.  
  315.       suratKetidakhadiranList.map(
  316.         function (item, index) {
  317.           if (groupedKaryawan[i] === item.karyawan.id) {
  318.             suratKetidakhadiranKaryawan.karyawan = item.karyawan;
  319.             suratKetidakhadiranKaryawan.suratKetidakhadiranList.push(item);
  320.           }
  321.         }
  322.       );
  323.       suratKetidakhadiranKaryawanList.push(suratKetidakhadiranKaryawan);
  324.     }
  325.     return suratKetidakhadiranKaryawanList;
  326.  
  327.   }
  328.  
  329.   public filter() {
  330.     this.searchParams.karyawanId = this.filterForm.value.employee ? this.filterForm.value.employee.id : null;
  331.     this.searchParams.lokasiKerjaId = this.filterForm.value.workplace ? this.filterForm.value.workplace.id : null;
  332.     this.searchParams.tahunBulan = this.filterForm.value.tahunBulan;
  333.  
  334.     const year = this.searchParams.tahunBulan.substring(0, 4);
  335.     const month = this.searchParams.tahunBulan.substring(4, 6);
  336.     this.currentDate = new Date(year + '-' + month);
  337.     this.currentMonthName = ALL_MONTH_NAME[this.currentDate.getMonth()];
  338.     this.currentYear = this.currentDate.getFullYear();
  339.  
  340.     this.initTable({
  341.       filters: {},
  342.       first: 0,
  343.       globalFilter: null,
  344.       multiSortMeta: undefined,
  345.       rows: this.rows,
  346.       sortField: undefined,
  347.       sortOrder: 1,
  348.     });
  349.   }
  350.  
  351.   public addCuti() {
  352.     this.onInputCuti.emit();
  353.   }
  354.  
  355.   public addIzin() {
  356.     this.onInputIzin.emit();
  357.   }
  358.  
  359.   public edit(suratKetidakhadiran: SuratKetidakhadiran) {
  360.     const ketidakhadiran = suratKetidakhadiran.jenisAbsensi.sdKelompokJenisAbsensi.nama;
  361.     if (cuti.indexOf(ketidakhadiran) > -1) {
  362.       this.onInputCuti.emit(suratKetidakhadiran);
  363.     } else if (izin === ketidakhadiran) {
  364.       this.onInputIzin.emit(suratKetidakhadiran);
  365.     }
  366.   }
  367.  
  368.   public ketidakhadiranBelumKonfirmasi() {
  369.     this.onKetidakhadiranBelumKonfirmasi.next(true);
  370.   }
  371.  
  372.   public ketidakhadiranBelumAdaKeterangan() {
  373.     this.onKetidakhadiranBelumAdaKeterangan.next(true);
  374.   }
  375.  
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment