import { Component, Input, OnInit } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; import { CurrencyService } from '@core/services/currency.service'; import { BudgetFundingSource, BudgetFundingSourceModalResponse, FundingSourceTypes, SimpleBudgetFundingSource } from '@core/typings/budget.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { ArrayHelpersService, TypeaheadSelectOption, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { MaskingService } from '@yourcause/common/masking'; import { YCModalComponent } from '@yourcause/common/modals'; import { BudgetService } from '../budget.service'; interface BudgetFundingGroup { source: SimpleBudgetFundingSource; amount: string; } @Component({ selector: 'gc-budget-funding-source-modal', templateUrl: 'budget-funding-source-modal.component.html', styleUrls: ['./budget-funding-source-modal.component.scss'] }) export class BudgetFundingSourceModalComponent extends YCModalComponent< BudgetFundingSourceModalResponse|string > implements OnInit { @Input() existingSource: BudgetFundingSource; @Input() originalExistingSources: BudgetFundingSource[]; @Input() existingSources: BudgetFundingSource[]; @Input() existingType: FundingSourceTypes; SOURCE_TYPES = FundingSourceTypes; formGroup: TypeSafeFormGroup; fundingSources = this.budgetService.get('budgetFundingSources'); fundingSourceOptions: TypeaheadSelectOption[]; mappedExistingSource: SimpleBudgetFundingSource; mappedExistingSources: SimpleBudgetFundingSource[] = []; existingAmount: number; amountLabel: string; budgetHelpText: string; maxAmount: number; minAmount: number; noSourcesAvailable = this.budgetService.noCashSourcesExist && this.budgetService.noInKindSourcesExist; noSourcesExist = this.budgetService.noCashSourcesExist && this.budgetService.noInKindSourcesExist; defaultCurrency = this.clientSettingsService.defaultCurrency; formattingData = this.currencyService.formattingData; defaultSymbol = this.formattingData[ this.defaultCurrency ].symbol; leftIconCurrency = { icon: this.defaultSymbol, isStaticText: true }; constructor ( private formBuilder: TypeSafeFormBuilder, private i18n: I18nService, private budgetService: BudgetService, private currencyService: CurrencyService, private clientSettingsService: ClientSettingsService, private maskingService: MaskingService, private analyticsService: AnalyticsService, private arrayHelper: ArrayHelpersService ) { super(); } get isDollars () { if (this.formGroup) { const source = this.formGroup.value.source; return source.fundingSourceType === FundingSourceTypes.DOLLARS; } return true; } ngOnInit () { this.mappedExistingSources = this.existingSources.map(existingSource => { const found = this.fundingSources.find(source => { return source.fundingSourceId === existingSource.fundingSourceId; }); if (found) { found.totalAmountPayments = existingSource.totalAmountPayments; } return found; }); if (this.existingType && !this.existingSource) { this.fundingSources = this.fundingSources .filter(source => source.fundingSourceType === this.existingType) .filter(source => !this.mappedExistingSources.includes(source)); } if (this.existingSource) { const found = this.fundingSources.find(source => { return source.fundingSourceId === this.existingSource.fundingSourceId; }); found.totalAmountPayments = this.existingSource.totalAmountPayments; this.mappedExistingSource = found; } else if (this.fundingSources.length) { this.mappedExistingSource = this.fundingSources[0]; this.mappedExistingSource.totalAmountPayments = 0; } else { this.noSourcesAvailable = true; } if (!this.noSourcesAvailable) { this.onFundingSourceChange(this.mappedExistingSource); let amount = '0'; if (this.existingSource) { amount = this.currencyService.getCurrencyValueForFormGroup( this.existingSource.totalAmount ); } this.formGroup = this.formBuilder.group({ source: [this.mappedExistingSource, Validators.required], amount: [ amount, [ Validators.required, this.maxAmountValidator(), this.minAmountValidator() ] ] }); this.setSelectOptions(); } } setSelectOptions () { this.fundingSourceOptions = this.arrayHelper.sort(this.fundingSources .map>(source => ({ label: source.fundingSourceName, value: source })), 'label'); } maxAmountValidator () { return (formControl: FormControl) => { const unmasked = this.maskingService.unmaskNumber( this.formattingData[this.defaultCurrency], formControl.value || '0' ); if (unmasked > this.maxAmount) { return { paymentsMax: { i18nKey: 'BUDGET:textPleaseEnterValueLessThanAmount', context: { amount: this.currencyService.formatMoney(this.maxAmount) }, defaultValue: 'Please enter a value less than or equal to __amount__' } }; } return null; }; } minAmountValidator () { return (formControl: FormControl) => { const unmasked = this.maskingService.unmaskNumber( this.formattingData[this.defaultCurrency], formControl.value || '0' ); if (unmasked < this.minAmount) { const amount = this.currencyService.formatMoney(this.minAmount); return { paymentsMin: { i18nKey: 'BUDGET:textPaymentsExistWarningDynamic', context: { amount }, defaultValue: 'Payments have been allocated to this funding source. Amount must be greater than or equal to __amount__.' } }; } else if (unmasked <= 0) { return { paymentsMin: { i18nKey: 'BUDGET:textAmountGreaterThanZero', defaultValue: 'Please enter a value greater than 0' } }; } return null; }; } onFundingSourceChange (newSource: SimpleBudgetFundingSource) { if (!this.existingSource) { this.maxAmount = newSource.totalAmountAvailable; } else { const found = this.originalExistingSources.find((source) => { return source.fundingSourceId === this.existingSource.fundingSourceId; }) || { totalAmount: 0 }; this.maxAmount = newSource.totalAmountAvailable + found.totalAmount; } const amount = this.currencyService.formatMoney(this.maxAmount); this.budgetHelpText = this.i18n.translate( 'BUDGET:txtAvailableForThisBudgetDynamic', { amount }, 'Available for this budget: __amount__' ); this.amountLabel = this.i18n.translate( 'BUDGET:lblAmountForThisBudget', {}, 'Total amount for this budget' ); this.existingType = newSource.fundingSourceType; this.minAmount = newSource.totalAmountPayments; if (this.formGroup) { this.formGroup.get('amount').updateValueAndValidity(); } } submit () { this.closeModal.emit(this.formGroup.value); this.analyticsService.emitEvent({ eventName: 'Budget funding source modal submit', eventType: EventType.Click, extras: null }); } }