import { Component, OnInit } from '@angular/core'; import { BsModalRef } from 'ngx-bootstrap/modal'; import { AllocationMethod } from '../../../../utils/enums'; const cloneDeep = require('rfdc')(); @Component({ selector: 'esp-allocation-method-modal', templateUrl: './allocation-method.modal.component.html', styleUrls: ['./allocation-method.modal.component.scss'], }) export class AllocationMethodModalComponent implements OnInit { config; aggregatedMeasureOptions = []; weightedByMeasureOptions = []; periods = []; allocationMethodValues = AllocationMethod; allocationMethod = AllocationMethod[0]; selectedMeasure: any; selectedWeightedMeasure: any; selectedPeriod: any; existingDisAggregation: any; defaultDisaggregation; dimensionLevelColumnName; constructor(private modelRef: BsModalRef) {} ngOnInit(): void { this.aggregatedMeasureOptions = this.config?.['select-measure']?.['dropdown-options']; this.weightedByMeasureOptions = this.config?.['select-allocation-method']?.['allocation-method-options']?.[0]?.[ 'select-weighted-by-options' ]['dropdown-options']; this.periods = this.config?.['select-allocation-method']?.['allocation-method-options']?.[0]?.[ 'select-weighted-period' ]['dropdown-options']; this.dimensionLevelColumnName = this.config?.['select-allocation-method']?.['allocation-method-options']?.[0]?.['select-weighted-period']['period-config']['dimensionLevel']; this.clearAll(this.periods); this.existingDisAggregation = JSON.parse(sessionStorage.getItem('disAggregation')); this.defaultDisaggregation = cloneDeep(this.config?.defaultDisaggregation); let disaggregation; let timeId; if (this.existingDisAggregation) { disaggregation = this.existingDisAggregation[0]; timeId = disaggregation?.fromTimeScope?.dimensionFilters?.[0]?.and?.[0]?.values?.[0]; if (timeId) { this.selectedPeriod = this.periods.find(period => period.id === timeId); } } else { disaggregation = this.defaultDisaggregation[0]; timeId = disaggregation?.weightingPeriod?.value; if (timeId) { this.selectedPeriod = this.periods.find(period => period.externalID === timeId); } } this.allocationMethod = disaggregation?.type; this.selectedMeasure = this.aggregatedMeasureOptions.find( measure => measure['measure-name'] === disaggregation?.aggregatedMeasure ); this.selectedMeasure.selected = true; if (this.allocationMethod === AllocationMethod[0]) { this.selectedWeightedMeasure = this.weightedByMeasureOptions.find( measure => measure['measure-name'] === disaggregation?.weightingMeasure ); this.selectedWeightedMeasure.selected = true; if (this.selectedPeriod) { this.selectedPeriod.selected = true; } } this.defaultDisaggregation.map(disAggn => { let fromTimeScope = null; if (disAggn.type === AllocationMethod[0]) { const period = this.periods.find(period => period.externalID === disAggn?.weightingPeriod?.value); if(period){ fromTimeScope = { dimensionFilters: [ { dimensionColumnName: 'time', and: [ { dimensionLevelColumnName: disAggn?.weightingPeriod?.dimensionLevelColumnName ? disAggn?.weightingPeriod?.dimensionLevelColumnName : this.dimensionLevelColumnName, cmpOperator: 'IN', values: [period?.id], }, ], }, ], }; } else { fromTimeScope = null; } } disAggn.fromTimeScope = fromTimeScope; delete disAggn.weightingPeriod; }); if (!this.existingDisAggregation) { sessionStorage.setItem('disAggregation', JSON.stringify(this.defaultDisaggregation)); } } clearAll(array) { array.map(item => (item.selected = false)); } selectAllocationMethod(method) { this.allocationMethod = method; if (this.selectedWeightedMeasure.hasOwnProperty('selected')) { this.selectedWeightedMeasure.selected = false; } if (this.selectedPeriod.hasOwnProperty('selected')) { this.selectedPeriod.selected = false; } this.selectedWeightedMeasure = null; this.selectedPeriod = null; } onClose(event) { this.modelRef.hide(); } saveAllocationMethod() { const disAggregation = { type: this.allocationMethod, aggregatedMeasure: this.selectedMeasure.measure ? this.selectedMeasure.measure : this.selectedMeasure['measure-name'] ? this.selectedMeasure['measure-name'] : '', weightingMeasure: this.allocationMethod === AllocationMethod[0] ? this.selectedWeightedMeasure?.['measure-name'] : '', fromTimeScope: null, }; if (this.allocationMethod === AllocationMethod[0]) { if(this.selectedPeriod){ const existingMeasure = this.existingDisAggregation.find( disAgrgn => disAggregation?.aggregatedMeasure === disAgrgn?.aggregatedMeasure ); const fromTimeScope = { dimensionFilters: [ { dimensionColumnName: 'time', and: [ { dimensionLevelColumnName: existingMeasure?.fromTimeScope?.dimensionFilters?.[0]?.and?.[0]?.dimensionLevelColumnName ? existingMeasure?.fromTimeScope?.dimensionFilters?.[0]?.and?.[0]?.dimensionLevelColumnName : this.dimensionLevelColumnName, cmpOperator: 'IN', values: [this.selectedPeriod?.id], }, ], }, ], }; disAggregation.fromTimeScope = fromTimeScope; } } if (this.existingDisAggregation) { const existingMeasureIndex = this.existingDisAggregation.findIndex( disAgrgn => disAggregation?.aggregatedMeasure === disAgrgn?.aggregatedMeasure ); if (existingMeasureIndex !== -1) { this.existingDisAggregation.splice(existingMeasureIndex, 1, disAggregation); } else { this.existingDisAggregation.push(disAggregation); } localStorage.removeItem('disAggregation'); sessionStorage.setItem('disAggregation', JSON.stringify(this.existingDisAggregation)); } else { sessionStorage.setItem('disAggregation', JSON.stringify([disAggregation])); } this.modelRef.hide(); } onMeasureSelected(event) { if (this.selectedWeightedMeasure.hasOwnProperty('selected')) { this.selectedWeightedMeasure.selected = false; } if (this.selectedPeriod.hasOwnProperty('selected')) { this.selectedPeriod.selected = false; } this.selectedWeightedMeasure = null; this.selectedPeriod = null; this.selectedMeasure = event?.singleSelectOptions; let disAggregation; let timeId; if (this.existingDisAggregation) { disAggregation = this.existingDisAggregation.find( disAggn => disAggn.aggregatedMeasure === this.selectedMeasure['measure-name'] ); timeId = disAggregation?.fromTimeScope?.dimensionFilters?.[0]?.and?.[0]?.values?.[0]; if (timeId) { this.selectedPeriod = this.periods.find(period => period.id === timeId); } } else { disAggregation = this.defaultDisaggregation.find( disAggn => disAggn.aggregatedMeasure === this.selectedMeasure['measure-name'] ); timeId = disAggregation?.weightingPeriod?.value; if (timeId) { this.selectedPeriod = this.periods.find(period => period.externalID === timeId); } } this.allocationMethod = disAggregation?.type; if (this.allocationMethod === AllocationMethod[0]) { this.selectedWeightedMeasure = this.weightedByMeasureOptions.find( measure => measure['measure-name'] === disAggregation?.weightingMeasure ); this.selectedWeightedMeasure.selected = true; if (this.selectedPeriod) { this.selectedPeriod.selected = true; } } } onWeightedMeasureSelected(event) { this.selectedWeightedMeasure = event?.singleSelectOptions; } onWeightedPeriodSelected(event) { this.selectedPeriod = event?.singleSelectOptions; } }