import { Component , OnInit , OnDestroy } from '@angular/core' ; import { TranslationBaseComponent } from '../../@shared/language-base/translation-base.component' ; import { IOrganization , RecurringExpenseDefaultCategoriesEnum , RecurringExpenseDeletionEnum , IEmployeeRecurringExpense , IEmployee , ISelectedEmployee } from '@gauzy/contracts' ; import { NbDialogService } from '@nebular/theme' ; import { TranslateService } from '@ngx-translate/core' ; import { debounceTime , filter , withLatestFrom } from 'rxjs/operators' ; import { firstValueFrom } from 'rxjs' ; import { monthNames } from '../../@core/utils/date' ; import { RecurringExpenseDeleteConfirmationComponent } from '../../@shared/expenses/recurring-expense-delete-confirmation/recurring-expense-delete-confirmation.component' ; import { RecurringExpenseMutationComponent , COMPONENT_TYPE } from '../../@shared/expenses/recurring-expense-mutation/recurring-expense-mutation.component' ; import { Store } from '../../@core/services/store.service' ; import { ActivatedRoute } from '@angular/router' ; import { EmployeesService } from '../../@core/services' ; import { EmployeeRecurringExpenseService } from '../../@core/services/employee-recurring-expense.service' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { ToastrService } from '../../@core/services/toastr.service' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-recurring-expenses-employee' , templateUrl : './recurring-expense-employee.component.html' , styleUrls : [ './recurring-expense-employee.component.scss' ] } ) export class RecurringExpensesEmployeeComponent extends TranslationBaseComponent implements OnInit , OnDestroy { selectedEmployee : IEmployee ; selectedDate : Date ; employeeList : IEmployee [ ] = [ ] ; selectedEmployeeFromHeader : ISelectedEmployee ; selectedEmployeeRecurringExpense : IEmployeeRecurringExpense [ ] = [ ] ; selectedRowIndexToShow : number ; employeeName = this . getTranslation ( 'EMPLOYEES_PAGE.EMPLOYEE_NAME' ) ; fetchedHistories : Object = { } ; selectedOrganization : IOrganization ; selectedEmployeeId : string ; constructor ( private route , private employeeService , private store , private dialogService , private employeeRecurringExpenseService , private toastrService , readonly translateService ) { super ( translateService ) ; } async ngOnInit ( ) { this . store . selectedDate$ . pipe ( untilDestroyed ( this ) ) . subscribe ( ( date ) => { this . selectedDate = date ; if ( this . selectedOrganization ) { this . _loadEmployeeRecurringExpense ( ) ; } } ) ; const storeEmployee$ = this . store . selectedEmployee$ ; const storeOrganization$ = this . store . selectedOrganization$ ; storeEmployee$ . pipe ( filter ( ( employee ) => ! ! employee ) , debounceTime ( 200 ) , withLatestFrom ( storeOrganization$ ) , untilDestroyed ( this ) ) . subscribe ( ( [ employee ] ) => { if ( employee && this . selectedOrganization ) { this . selectedEmployeeId = employee . id ; } } ) ; storeOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , debounceTime ( 200 ) , withLatestFrom ( storeEmployee$ ) , untilDestroyed ( this ) ) . subscribe ( ( [ organization , employee ] ) => { this . selectedEmployeeId = employee ? employee . id : null ; if ( organization ) { this . selectedOrganization = organization ; this . _loadEmployeeRecurringExpense ( ) ; } } ) ; this . route . params . pipe ( untilDestroyed ( this ) ) . subscribe ( async ( params ) => { if ( params . hasOwnProperty ( 'id' ) ) { const id = params . id ; const { items } = await firstValueFrom ( this . employeeService . getAll ( [ 'user' , 'organizationPosition' , 'tags' , 'skills' ] , { id } ) ) ; this . selectedEmployee = items [ 0 ] ; this . store . selectedEmployee = { id : items [ 0 ] . id , firstName : items [ 0 ] . user . firstName , lastName : items [ 0 ] . user . lastName , imageUrl : items [ 0 ] . user . imageUrl , tags : items [ 0 ] . user . tags , skills : items [ 0 ] . skills } ; const checkUsername = this . selectedEmployee . user . username ; this . employeeName = checkUsername ? checkUsername : this . getTranslation ( 'EMPLOYEES_PAGE.EMPLOYEE_NAME' ) ; } } ) ; } getMonthString ( month ) { return monthNames [ month ] ; } getCategoryName ( categoryName ) { return categoryName in RecurringExpenseDefaultCategoriesEnum ? this . getTranslation ( `EXPENSES_PAGE.DEFAULT_CATEGORY.${ categoryName }` ) : categoryName ; } showMenu ( index ) { this . selectedRowIndexToShow = index ; } async addEmployeeRecurringExpense ( ) { const result = await firstValueFrom ( this . dialogService . open ( RecurringExpenseMutationComponent , { context : { componentType : COMPONENT_TYPE . EMPLOYEE , selectedDate : this . selectedDate , isAdd : true } } ) . onClose ) ; if ( result ) { try { const employeeRecurringExpense = this . _recurringExpenseMutationResultTransform ( result ) ; await this . employeeRecurringExpenseService . create ( employeeRecurringExpense ) . then ( ( ) => { this . toastrService . success ( 'TOASTR.MESSAGE.RECURRING_EXPENSE_SET' , { name : this . employeeName } ) ; this . _loadEmployeeRecurringExpense ( ) ; } ) . catch ( ( error ) => { this . toastrService . danger ( error ) ; } ) ; } catch ( error ) { this . toastrService . danger ( error ) ; } } } async editEmployeeRecurringExpense ( index ) { const result = await firstValueFrom ( this . dialogService . open ( RecurringExpenseMutationComponent , { context : { recurringExpense : this . selectedEmployeeRecurringExpense [ index ] , componentType : COMPONENT_TYPE . EMPLOYEE } } ) . onClose ) ; if ( result ) { try { const id = this . selectedEmployeeRecurringExpense [ index ] . id ; const employeeRecurringExpense = this . _recurringExpenseMutationResultTransform ( result ) ; this . employeeRecurringExpenseService . update ( id , employeeRecurringExpense ) . then ( ( ) => { this . selectedRowIndexToShow = null ; this . toastrService . success ( 'TOASTR.MESSAGE.RECURRING_EXPENSE_UPDATED' , { name : this . employeeName } ) ; this . _loadEmployeeRecurringExpense ( ) ; } ) . catch ( ( error ) => { this . toastrService . danger ( error ) ; } ) ; } catch ( error ) { this . toastrService . danger ( error ) ; } } } async deleteEmployeeRecurringExpense ( index ) { const selectedExpense = this . selectedEmployeeRecurringExpense [ index ] ; const result = await firstValueFrom ( this . dialogService . open ( RecurringExpenseDeleteConfirmationComponent , { context : { recordType : this . getTranslation ( 'EMPLOYEES_PAGE.RECURRING_EXPENSE' ) , start : `${ this . getMonthString ( selectedExpense . startMonth ) }, ${ selectedExpense . startYear }` , current : `${ this . getMonthString ( this . selectedDate . getMonth ( ) ) }, ${ this . selectedDate . getFullYear ( ) }` , end : selectedExpense . endMonth ? `${ this . getMonthString ( selectedExpense . endMonth ) }, ${ selectedExpense . endYear }` : 'end' } } ) . onClose ) ; if ( result ) { try { const id = selectedExpense . id ; this . employeeRecurringExpenseService . delete ( id , { deletionType : result , month : this . selectedDate . getMonth ( ) , year : this . selectedDate . getFullYear ( ) } ) . then ( ( ) => { this . selectedRowIndexToShow = null ; this . toastrService . success ( 'TOASTR.MESSAGE.RECURRING_EXPENSE_DELETED' , { name : this . employeeName } ) ; this . _loadEmployeeRecurringExpense ( ) ; } ) . catch ( ( error ) => { this . toastrService . danger ( error ) ; } ) ; } catch ( error ) { this . toastrService . danger ( error ) ; } } } private _recurringExpenseMutationResultTransform ( result ) { const { id : organizationId , tenantId } = this . selectedOrganization ; return { organizationId , tenantId , employeeId : result . employee . id , categoryName : result . categoryName , value : result . value , currency : result . currency , startDay : result . startDay || 1 , startMonth : result . startMonth || this . selectedDate . getMonth ( ) , startYear : result . startYear || this . selectedDate . getFullYear ( ) , startDate : result . startDate || new Date ( this . selectedDate . getFullYear ( ) , this . selectedDate . getMonth ( ) , 1 ) } ; } private async _loadEmployeeRecurringExpense ( ) { if ( ! this . selectedOrganization ) return ; const { id : organizationId , tenantId } = this . selectedOrganization ; this . fetchedHistories = { } ; if ( this . selectedEmployeeId ) { this . selectedEmployeeRecurringExpense = ( await this . employeeRecurringExpenseService . getAllByMonth ( [ 'employee' , 'employee.user' ] , { employeeId : this . selectedEmployeeId , year : this . selectedDate . getFullYear ( ) , month : this . selectedDate . getMonth ( ) , organizationId , tenantId } ) ) . items ; } else { this . selectedEmployeeRecurringExpense = ( await this . employeeRecurringExpenseService . getAll ( [ 'employee' , 'employee.user' ] , { organizationId , tenantId } ) ) . items ; } } public async fetchHistory ( i ) { this . fetchedHistories [ i ] = ( await this . employeeRecurringExpenseService . getAll ( [ ] , { parentRecurringExpenseId : this . selectedEmployeeRecurringExpense [ i ] . parentRecurringExpenseId } , { startDate : 'ASC' } ) ) . items ; } ngOnDestroy ( ) { clearTimeout ( ) ; } }