import { Component, Input, OnInit } from '@angular/core'; import { CurrencyService } from '@core/services/currency.service'; import { SpinnerService } from '@core/services/spinner.service'; import { TranslationService } from '@core/services/translation.service'; import { FundingSourceTypes } from '@core/typings/budget.typing'; import { NotesDetail, PaymentForProcess, PaymentNotes, ProcessingTypes } from '@core/typings/payment.typing'; import { BatchStatuses, PaymentStatus } from '@core/typings/status.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { InKindService } from '@features/in-kind/in-kind.service'; import { I18nService } from '@yourcause/common/i18n'; import { ModalFactory, YCModalComponent } from '@yourcause/common/modals'; import { PaymentProcessingResources } from '../payment-processing.resources'; import { PaymentProcessingService } from '../payment-processing.service'; import { PaymentRecallModalComponent } from '../payment-recall-modal/payment-recall-modal.component'; @Component({ selector: 'gc-payment-details-modal', templateUrl: './payment-details-modal.component.html', styleUrls: ['./payment-details-modal.component.scss'] }) export class PaymentDetailsModalComponent extends YCModalComponent implements OnInit { @Input() payment: PaymentForProcess; @Input() batchStatus: BatchStatuses; ProcessingTypes = ProcessingTypes; allItemsMap = this.inKindService.allItemsMap; clientSettings = this.clientSettingsService.get('clientSettings'); viewTranslations = this.translationService.viewTranslations; programTranslationMap = this.viewTranslations.Grant_Program; cycleTranslationMap = this.viewTranslations.Grant_Program_Cycle; notes: PaymentNotes; notesArray: NotesDetail[] = []; isInKind = false; inKindAmountString = ''; recallModalOpen: boolean; BatchStatuses = BatchStatuses; PaymentStatuses = PaymentStatus; constructor ( private i18n: I18nService, private clientSettingsService: ClientSettingsService, private currencyService: CurrencyService, private inKindService: InKindService, private translationService: TranslationService, private modalFactory: ModalFactory, private paymentProcessingService: PaymentProcessingService, private paymentProcessingResources: PaymentProcessingResources, private spinnerService: SpinnerService ) { super(); } async ngOnInit () { this.isInKind = this.payment.fundingSourceType === FundingSourceTypes.UNITS; if (this.isInKind) { const number = (this.payment.inKindItems || []).reduce((acc, item) => { return acc + item.count; }, 0); this.inKindAmountString = this.i18n.translate( 'GLOBAL:textNumberUnits', { number }, '__number__ units' ) + ` (${this.currencyService.formatMoney(this.payment.totalAmount)})`; } this.notes = await this.paymentProcessingResources.getPaymentNotes( this.payment.paymentId ); this.notesArray = [{ type: 'payment', notes: this.notes.paymentNotes, setBy: this.notes.paymentNotesSetByInfo, date: this.notes.paymentNotesSetDate, title: this.i18n.translate( 'MANAGE:textCreateOrEditPayment', {}, 'Create or edit payment' ) }, { type: 'hold', notes: this.notes.onHoldNotes, setBy: this.notes.onHoldSetByInfo, date: this.notes.onHoldSetDate, title: this.i18n.translate( 'GLOBAL:textOnHold', {}, 'On hold' ) }, { type: 'batch', notes: this.notes.batchNotes, setBy: this.notes.batchNotesSetByInfo, date: this.notes.batchNotesSetDate, title: this.i18n.translate( 'MANAGE:textCreateOrEditBatch', {}, 'Create or edit batch' ) }, { type: 'batchStatus', notes: this.notes.batchStatusUpdateNotes, setBy: this.notes.batchStatusUpdateSetByInfo, date: this.notes.batchStatusUpdateSetDate, title: this.i18n.translate( 'MANAGE:textUpdateBatchStatus', {}, 'Update batch status' ) }]; if (this.clientSettings.allowExpeditedPayments) { this.notesArray.push({ type: 'expedite', notes: this.notes.expeditedNotes, setBy: this.notes.expeditedSetByInfo, date: this.notes.expeditedSetDate, title: this.i18n.translate( 'MANAGE:btnExpedite' ) }); } this.notesArray = this.notesArray.filter((item) => { return !!item.notes; }); } async removePaymentFromBatch (payment: PaymentForProcess) { this.recallModalOpen = true; this.spinnerService.startSpinner(); const isEligible = await this.paymentProcessingService.handlePaymentIsEligibleForRecall(this.payment.paymentId); this.spinnerService.stopSpinner(); const response = await this.modalFactory.open(PaymentRecallModalComponent, { payment, isEligible }); if (response) { this.spinnerService.startSpinner(); await this.paymentProcessingService.handleRecallPayment({ paymentId: this.payment.paymentId, reasonForRecall: response.comment }); this.spinnerService.stopSpinner(); this.closeModal.emit(true); } else { this.recallModalOpen = false; } } }