import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {MatTableDataSource} from "@angular/material/table"; import {DatePipe} from "@angular/common"; import {ReportingService} from "../../../services/reporting.service"; import {ActivatedRoute, Router} from "@angular/router"; import {Response} from "../../../models/response"; import {SearchComponent} from "../../../components/search/search.component"; import {MatDialog} from "@angular/material/dialog"; import {Activity} from "../../../models/activity"; import {ActivitiesComponent} from "../../../components/activities/activities.component"; import * as FileSaver from "file-saver"; import {ToastrService} from "ngx-toastr"; import {ConfirmComponent} from "../../../components/confirm/confirm.component"; import {MatPaginator, PageEvent} from "@angular/material/paginator"; import {MatSort} from "@angular/material/sort"; import {CaseService} from "../../../services/case.service"; import {Case} from "../../../models/case"; import {ActivityService} from "../../../services/activity.service"; import {TableStateService} from "../../../services/table-state.service"; @Component({ selector: 'app-bills', templateUrl: './bills.component.html', styleUrls: ['./bills.component.scss'] }) export class BillsComponent implements OnInit { export: boolean = false; spinner = false; today; displayedColumns: string[] = ['id', 'description', 'created', 'displayName', 'debe', 'haber', 'edit', 'delete']; caseFee: any; dataSource!: MatTableDataSource; infoCase: Case | undefined; private tableStateKey = 'case:bills'; private paginatorRef: MatPaginator | null = null; @ViewChild('input') filterInput?: ElementRef; @ViewChild(MatPaginator, {static: false}) set paginator(value: MatPaginator) { this.paginatorRef = value; if (this.dataSource) { this.dataSource.paginator = value; this.restoreTableState(); } }; @ViewChild(MatSort, {static: false}) set sort(value: MatSort) { if (this.dataSource) { this.dataSource.sort = value; } } constructor(private datePipe: DatePipe, private reportingService: ReportingService, private activateRoute: ActivatedRoute, private router: Router, private dialog: MatDialog, private toastr: ToastrService, private caseService: CaseService, private activityService: ActivityService, private tableStateService: TableStateService, ) { this.today = this.datePipe.transform(new Date(), 'YYYY-MM-ddThh:mm'); } ngOnInit(): void { if (!this.activateRoute.snapshot.params['id']) { this.router.navigate(['/case']) } else { this.tableStateKey = `case:bills:${this.activateRoute.snapshot.params['id']}`; this.getActivityRelations(); } } getActivityRelations(fecha_inicio?: string | null, fecha_fin?: string | null) { this.spinner = true; this.caseFee = undefined; this.infoCase = undefined; this.reportingService.postActivityRelations(this.activateRoute.snapshot.params['id'], fecha_inicio ? fecha_inicio : null, fecha_fin ? fecha_fin : null) // @ts-ignore .subscribe((response: Response) => { if (response.data.data.length === 0) { // @ts-ignore this.caseService.getCaseByID(this.activateRoute.snapshot.params['id']).subscribe((response: Response) => { this.infoCase = response.data.case; // @ts-ignore this.dataSource = new MatTableDataSource([]); this.dataSource.paginator = this.paginatorRef this.dataSource.sort = this.sort; this.restoreTableState(); this.spinner = false; }, error => { const title = '¡Error ' + error.status + '!'; this.toastr.error(error.message, title); }) } else { this.caseFee = response.data.data; // @ts-ignore this.caseFee.map(activity => { activity.prevdescription = activity.description.length > 50 ? activity.description.slice(0, 40) + '...' : activity.description; activity.created = this.datePipe.transform(activity.created, 'YYYY-MM-dd') return activity }); this.dataSource = new MatTableDataSource(this.caseFee); this.dataSource.paginator = this.paginatorRef this.dataSource.sort = this.sort; this.restoreTableState(); this.spinner = false; } }, error => { const title = '¡Error ' + error.status + '!' this.toastr.error(error.message, title); }) } applyFilter(event: Event) { const filterValue = (event.target as HTMLInputElement).value; this.dataSource.filter = filterValue.trim().toLowerCase(); this.tableStateService.updateState(this.tableStateKey, { filter: filterValue, pageIndex: 0 }); if (this.dataSource.paginator) { this.dataSource.paginator.firstPage(); } } filterActivities() { this.dialog.open(SearchComponent, { maxWidth: '60%', minWidth: '330px', panelClass: 'dialog-container', disableClose: true, }).afterClosed().subscribe(result => { if (result != false) { this.getActivityRelations(result.fecha_inicio, result.fecha_fin) } }) } addActivities(activity?: any) { if (activity) { activity = { ...activity, id_activity: activity.id } } this.dialog.open(ActivitiesComponent, { maxWidth: '60%', minWidth: '330px', panelClass: 'dialog-container', disableClose: true, data: { info: this.today, bills: true, id_case: Number(this.activateRoute.snapshot.params['id']), id_client: this.infoCase ? this.infoCase.id_client : this.caseFee[0].id_client, activity: activity } }).afterClosed().subscribe(result => { if (result == true) { this.getActivityRelations(); } }) } exportFile() { this.export = true; this.dialog.open(SearchComponent, { maxWidth: '60%', minWidth: '330px', panelClass: 'dialog-container', disableClose: true, }).afterClosed().subscribe(result => { if (result != false) { this.reportingService.postExporOthertBills(this.activateRoute.snapshot.params['id'], result.fecha_inicio, result.fecha_fin).subscribe((res: Blob) => { this.toastr.success('Archivo descargado con exito!', 'Exito!'); var blob = new Blob([res], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); FileSaver.saveAs(blob, 'gastos.xlsx'); this.export = false; }) } else { this.export = false; } }) } deleteActivity(id: number) { this.dialog.open(ConfirmComponent, { maxWidth: '60%', minWidth: '330px', panelClass: 'dialog-container', disableClose: true, data: {type: 'warning', message: 'Seguro que deseas eliminar la actividad?'} }).afterClosed().subscribe(result => { if (result === true) { // @ts-ignore this.activityService.getDisableActivity(id).subscribe((response: Response) => { this.toastr.success(response.data.message, 'Exito!'); this.getActivityRelations(); }, error => { const title = '¡Error ' + error.status + '!' this.toastr.error(error.message, title); }); } }) } onPageChange(event: PageEvent) { this.tableStateService.updateState(this.tableStateKey, { pageIndex: event.pageIndex, pageSize: event.pageSize }); } private restoreTableState() { if (!this.dataSource || !this.paginatorRef) { return; } this.tableStateService.applyState( this.tableStateKey, this.dataSource, this.paginatorRef, this.filterInput?.nativeElement ); } }