import { ChangeDetectorRef, Component, Input, OnInit} from '@angular/core'; import { IContactPoint, ILimit, IPolicy, IRating } from './interface'; import { DatePipe, DecimalPipe } from '@angular/common'; import { IEnumList, IExternalCode } from './interface/EnumExternal.interface'; import { CheckCircle } from 'lucide-angular'; @Component({ selector: 'kit-policy-summary', templateUrl: './policy-summary.component.html', styleUrls: ['../styles/index.scss'] }) export class PolicySummaryComponent implements OnInit { @Input() policy!:IPolicy; @Input() enumList!: IEnumList[]; @Input() excList!: {externalCode:IExternalCode[]}[]; public readonly circleCheck = CheckCircle; public readonly HIDDE_DETAILS: string = 'Ocultar detalles'; public readonly SHOW_MORE_DETAILS: string = 'Ver más detalles'; public readonly SHOW_LESS: string = 'Ver menos'; public readonly SHOW_MORE_COVERAGE_LIST: string = 'Ver más coberturas'; public readonly SHOW_MORE_INSURED_LIST: string = 'Ver más asegurados'; public readonly SHOW_MORE_BENEFICIARY_LIST: string = 'Ver más beneficiarios'; private readonly YES: string = 'Si'; private readonly NO: string = 'No'; private readonly datePipe = new DatePipe('en-US'); public isShowMorePolicyDetails: boolean = false; public isShowMoreCoverageList: boolean = false; public isShowMoreInsuredList: boolean = false; public isShowMoreObjectInsuredList: boolean = false; public isShowMoreBeneficiaryList: boolean = false; public quantityCoverageListToShow!: number; public quantityInsuredListToShow!: number; public quantityObjectInsuredListToShow!: number; public quantityBeneficiaryListToShow!: number; private readonly defaultQuatityToShow: number = 4; public hasValidBeneficiaryData = false; private readonly COMPONENT_TYPE_CODE_PRODUCT: string = 'PRODUCT'; public decimalPipe = new DecimalPipe('es-ES') //Tabla Coberturas public tableColumns: { columnDef: string, header: string }[] = [{ columnDef: "name", header: "Nombre" }, { columnDef: "insuredAmmout", header: "Monto asegurado " }, { columnDef: "deductible", header: "Deducible" }, { columnDef: 'prima', header: 'Prima' }]; public tableDisplayedColumns: string[] = ['name', 'insuredAmmout', 'deductible', 'prima']; public dataSource: Array<{ name: string, insuredAmmout: string, deductible: string, prima: string }> = []; //Tabla producers public tableColumnsProducer: { columnDef: string, header: string }[] = [{ columnDef: "name", header: "Nombre" }, { columnDef: "identifier", header: "Identificador (RUT)" }, { columnDef: "lider", header: "Productor líder" }]; public tableDisplayedColumnsProducer: string[] = ['name', 'identifier', 'lider']; public dataSourceProducer: Array<{ name: string, identifier: string, lider: string }> = []; //Tabla materia asegurada public tableColumnsInsuredObject: { columnDef: string, header: string }[] = [{ columnDef: "class", header: "Clase" }, { columnDef: "identifier", header: "Identificador" }, { columnDef: "description", header: "Descripción" }]; public tableDisplayedColumnsInsuredObject: string[] = ['class', 'identifier', 'description']; public dataSourceInsuredObject: Array<{ class: string, identifier: string, description: string }> = []; //Tabla asegurados public tableColumnsInsured: { columnDef: string, header: string }[] = [{ columnDef: "name", header: "Nombre" }, { columnDef: "identifier", header: "Identificador (RUT)" }, { columnDef: "type", header: "Tipo de asegurado" }, { columnDef: "insurableInterest", header: "Interés asegurable" }]; public tableDisplayedColumnsInsured: string[] = ['name', 'identifier', 'type', 'insurableInterest']; public dataSourceInsured: Array<{ name: string, identifier: string, type: string, insurableInterest: string }> = []; //Tabla beneficiarios public tableColumnsBeneficiary: { columnDef: string, header: string }[] = [{ columnDef: "name", header: "Nombre" }, { columnDef: "identifier", header: "Identificador (RUT)" }, { columnDef: "percentage", header: "Porcentaje asignación" }]; public tableDisplayedColumnsBeneficiary: string[] = ['name', 'identifier', 'percentage']; public dataSourceBeneficiary: Array<{ name: string, identifier: string, percentage: string }> = []; constructor( private cdr: ChangeDetectorRef ) { } public ngOnInit(): void { this.quantityCoverageListToShow = this.defaultQuatityToShow; this.quantityInsuredListToShow = this.defaultQuatityToShow; this.quantityObjectInsuredListToShow = this.defaultQuatityToShow; this.quantityBeneficiaryListToShow = this.defaultQuatityToShow; this.setInitTables() } private setInitTables() { if (this.policy.coverageList) { this.setTableCoverage(); } if (this.policy.agreementProducerList) { this.setTableProducer(); } if (this.policy.insuredObjectList) { this.setTableInsuredObject() } if (this.policy.insuredList) { this.setTableInsured() } if (this.policy.beneficiaryList) { this.setTableBeneficiary() } } private setTableCoverage() { if (this.policy.coverageList.length <= 4) { for (let index = 0; index < this.policy.coverageList.length; index++) { const element = this.policy.coverageList[index]; this.dataSource.push({ name: element.productComponentMarketingName, insuredAmmout: this.getLimite(element.ratingList)??'-', deductible: this.getDeducible(element.ratingList)??'-', prima: this.getPrima(element.ratingList)??'-' }) } } else { for (let index = 0; index < this.policy.coverageList.length; index++) { const element = this.policy.coverageList[index]; if (this.dataSource.length === 4) { break; } this.dataSource.push({ name: element.productComponentMarketingName, insuredAmmout: this.getLimite(element.ratingList)??'-', deductible: this.getDeducible(element.ratingList)??'-', prima: this.getPrima(element.ratingList)??'-' }) } } this.dataSource = [...this.dataSource] } public showMoreCoverages() { this.isShowMoreCoverageList = !this.isShowMoreCoverageList this.quantityCoverageListToShow = this.isShowMoreCoverageList ? this.policy.coverageList.length : this.defaultQuatityToShow if (this.isShowMoreCoverageList) { for (let index = 4; index < this.policy.coverageList.length; index++) { const element = this.policy.coverageList[index]; this.dataSource.push({ name: element.productComponentMarketingName, insuredAmmout: this.getLimite(element.ratingList)??'-', deductible: this.getDeducible(element.ratingList)??'-', prima: this.getPrima(element.ratingList)??'-' }) } } else { this.dataSource.splice(4) } this.dataSource = [...this.dataSource] } private getLimite(ratingList: IRating[]) { let truncatedNumber: number = 0; const limitList: ILimit[] = ratingList.reduce((acc: any, item: IRating) => [...acc, ...item.limitList], []) const findComponentTypeCodeProduct = limitList.filter(e => e.componentTypeCode === this.COMPONENT_TYPE_CODE_PRODUCT); if (findComponentTypeCodeProduct.length > 0) { const limit = findComponentTypeCodeProduct[0]?.maximumAmount ? findComponentTypeCodeProduct[0].maximumAmount: findComponentTypeCodeProduct[0]?.fixedAmount ? findComponentTypeCodeProduct[0].fixedAmount : 0 truncatedNumber = Math.floor((limit) * Math.pow(10, 4)) / Math.pow(10, 4); } return this.getFloatFixed(truncatedNumber) } public getDeducible(ratingList: IRating[]) { let limit: number = 0 const limitList: ILimit[] = ratingList.reduce((acc: any, item: IRating) => [...acc, ...item.limitList], []) const findComponentTypeCodeProduct = limitList.filter(e => e.componentTypeCode === this.COMPONENT_TYPE_CODE_PRODUCT); if (findComponentTypeCodeProduct.length > 0) { limit = Math.floor((findComponentTypeCodeProduct[0]?.amountLic??0) * Math.pow(10, 4)) / Math.pow(10, 4); } return this.getFloatFixed(limit); } private findExcNameByCode(searchCode: string): string { if (!this.excList || !searchCode) { return ''; } const foundExc = this.excList.find(item => item.externalCode.find((ext) => ext.code === searchCode)); const fountItem = foundExc?.externalCode.find((ext) => ext.code === searchCode) return fountItem ? fountItem.name : searchCode; } public getPrima(ratingList: IRating[]) { let prima : number = 0 for (let index = 0; index < ratingList.length; index++) { const element = ratingList[index]; prima += Math.floor(((element.premiumBalance.premiumAmount??0)) * Math.pow(10, 4)) / Math.pow(10, 4); } return prima ? this.getFloatFixed(prima) : '-' } public findEnumNameByCode(listCode: string, searchCode: string): string { if (!this.enumList || !searchCode || !listCode) { return ''; } const enumList = this.enumList.find(item => item.code === listCode); if (!enumList) { return searchCode; } const foundEnum = enumList.enumeration.find(item => item.code === searchCode); return foundEnum?.name || searchCode; } public getDate(date: string | undefined | null) { return date ? this.datePipe.transform(date, 'dd/MM/yyyy') : '---' } private setTableProducer() { if (this.policy.agreementProducerList.length > 0) { for (let index = 0; index < this.policy.agreementProducerList.length; index++) { const element = this.policy.agreementProducerList[index]; const partyKey = element.agreementProducerPartyKey ? element.agreementProducerPartyKey.replace('RUT::', '') : '-'; this.dataSourceProducer.push({ name: element.agreementProducerFullName, identifier: `${this.formatRut(partyKey, true)}`, lider: (element.producerLeaderIndicator ? this.YES : this.NO) }) } } } private formatRut(rut: string, useDotDelimiter: boolean) { rut = this.cleanRut(rut) let result = rut.slice(-4, -1) + '-' + rut.substring(rut.length - 1) for (let i = 4; i < rut.length; i += 3) { if (useDotDelimiter) { result = rut.slice(-3 - i, -i) + '.' + result } else { result = rut.slice(-3 - i, -i) + result } } if (result == '-') { result = ''; } return result } private cleanRut(rut: string) { return typeof rut === 'string' ? rut.replace(/^0+|[^0-9kK]+/g, '').toUpperCase() : '' } public getFormatIdentifier(identifier: string) { return this.formatRut(identifier, true) } public getContactData(contact: IContactPoint[], data: string): string { switch (data) { case 'email': return contact[0].personalContactPoint && contact[0].personalContactPoint.personalEmailAddress ? contact[0].personalContactPoint.personalEmailAddress??'-' : contact[0].businessContactPoint ? contact[0].businessContactPoint.businessEmailAddress : '-' case 'cellphone': return contact[0].personalContactPoint ? contact[0].personalContactPoint.personalTelephoneCellNumber??'-' : contact[0].businessContactPoint ? contact[0].businessContactPoint.businessTelephoneCellNumber : '-' case 'landlinephone': return contact[0].personalContactPoint ? contact[0].personalContactPoint.personalTelephoneLandlineNumber??'-' : contact[0].businessContactPoint ? contact[0].businessContactPoint.businessTelephoneLandlineNumber : '-' case 'address': return contact[0].personalContactPoint && contact[0].personalContactPoint.personalMunicipalityCode ? contact[0].personalContactPoint.personalMunicipalityCode : contact[0].businessContactPoint ? contact[0].businessContactPoint.businessMunicipalityCode : '-' default: return '-'; } } public setTableInsuredObject() { if (this.policy.insuredObjectList.length > 0) { for (let index = 0; index < this.policy.insuredObjectList.length; index++) { const element = this.policy.insuredObjectList[index]; this.dataSourceInsuredObject.push({ class: this.findExcNameByCode(element.physicalObjectClassCode), identifier: element.physicalObject.identifier, description: element.physicalObject.description }) } this.dataSourceInsuredObject = [...this.dataSourceInsuredObject] } } public setTableInsured() { if (this.policy.insuredList.length <= 4) { for (let index = 0; index < this.policy.insuredList.length; index++) { const element = this.policy.insuredList[index]; this.dataSourceInsured.push({ name: element.insuredFullName, identifier: this.formatRut(element.party.identifier, true), insurableInterest: this.findEnumNameByCode("InsuredTypeCodeList", element.insuredTypeCode), type: this.findEnumNameByCode("InsurableInterestCodeList", element.insurableInterestTypeCode) }) } } else { for (let index = 0; index < this.policy.insuredList.length; index++) { const element = this.policy.insuredList[index]; if (this.dataSourceInsured.length === 4) { break; } this.dataSourceInsured.push({ name: element.insuredFullName, identifier: this.formatRut(element.party.identifier, true), insurableInterest: this.findEnumNameByCode("InsuredTypeCodeList", element.insuredTypeCode), type: this.findEnumNameByCode("InsurableInterestCodeList", element.insurableInterestTypeCode) }) } } this.dataSourceInsured = [...this.dataSourceInsured] } public showMoreInsured() { this.isShowMoreInsuredList = !this.isShowMoreInsuredList this.quantityInsuredListToShow = this.isShowMoreInsuredList ? this.policy.insuredList.length : this.defaultQuatityToShow if (this.isShowMoreInsuredList) { for (let index = 4; index < this.policy.insuredList.length; index++) { const element = this.policy.insuredList[index]; this.dataSourceInsured.push({ name: element.insuredFullName, identifier: this.formatRut(element.party.identifier, true), insurableInterest: this.findEnumNameByCode("InsuredTypeCodeList", element.insuredTypeCode), type: this.findEnumNameByCode("InsurableInterestCodeList", element.insurableInterestTypeCode) }) } } else { this.dataSourceInsured.splice(4) } this.dataSourceInsured = [...this.dataSourceInsured] } private setTableBeneficiary() { if (this.policy.beneficiaryList.length <= 4) { for (let index = 0; index < this.policy.beneficiaryList.length; index++) { const element = this.policy.beneficiaryList[index]; this.dataSourceBeneficiary.push({ name: element.beneficiaryFullName, identifier: this.formatRut(element.party.identifier, true), percentage: element.percentage + '%' }) } } else { for (let index = 0; index < this.policy.beneficiaryList.length; index++) { const element = this.policy.beneficiaryList[index]; if (this.dataSourceBeneficiary.length === 4) { break; } this.dataSourceBeneficiary.push({ name: element.beneficiaryFullName, identifier: this.formatRut(element.party.identifier, true), percentage: element.percentage + '%' }) } } this.dataSourceBeneficiary = [...this.dataSourceBeneficiary] } public showMoreBeneficiary() { this.isShowMoreBeneficiaryList = !this.isShowMoreBeneficiaryList this.quantityBeneficiaryListToShow = this.isShowMoreBeneficiaryList ? this.policy.beneficiaryList.length : this.defaultQuatityToShow if (this.isShowMoreBeneficiaryList) { for (let index = 4; index < this.policy.beneficiaryList.length; index++) { const element = this.policy.beneficiaryList[index]; this.dataSourceBeneficiary.push({ name: element.beneficiaryFullName, identifier: this.formatRut(element.party.identifier, true), percentage: element.percentage + '%' }) } } else { this.dataSourceBeneficiary.splice(4) } this.dataSourceBeneficiary = [...this.dataSourceBeneficiary] } public getFloatFixed(number:number){ const truncatedNumber = Math.floor((number) * Math.pow(10, 4)) / Math.pow(10, 4); return truncatedNumber ? this.decimalPipe.transform(truncatedNumber, '1.3-3') : '-' } }