const customSliderSettings={initFill:"#91067C",endFill:"#e6258b",background:"#d7dcdf"};const customSliders=document.querySelectorAll(".range-slider");Array.prototype.forEach.call(customSliders,slider=>{slider.querySelector("input").addEventListener("input",event=>{applyFill(event.target)});applyFill(slider.querySelector("input"))});function applyFill(slider){const percentage=(100*(slider.value-slider.min))/(slider.max-slider.min);const bg=`linear-gradient(111.28deg, ${customSliderSettings.initFill} 0%, ${ customSliderSettings.endFill } ${percentage}%, ${customSliderSettings.background} ${percentage + 0.001}%)`;slider.style.background=bg} const INPUT_INVESTIMENT_TIME_EL=document.getElementById("inp-investiment-time");const INPUT_INVESTIMENT_TIME_MOB_EL=document.getElementById("inp-investiment-time-mob");const INPUT_INITIAL_EL=document.getElementById("inp-initial");const INPUT_MONTHLY_EL=document.getElementById("inp-monthly");const SPAN_INVESTIMENT_TIME_EL=document.getElementById("span-investiment-time");const SPAN_INVESTIMENT_TIME_MOB_EL=document.getElementById("span-investiment-time-mob");const SPAN_POUPANCA_RENTABILITY_EL=document.getElementById("poupanca-rentability");const SPAN_TYPE_RENTABILITY_DETAILS_EL=document.getElementById("type-rentability-detail");const SPAN_RENTABILITY_DIFF_EL=document.getElementById("rentability-diff");const SPAN_RENTABILITY_DIFF_PERCENT_EL=document.getElementById("rentability-diff-percent");const TD_TOTAL_INVESTIMENT_EL=document.getElementById("total-investment");const TD_TOTAL_INVESTIMENT_MOB_EL=document.getElementById("total-investment-mob");const TD_TOTAL_INVESTIMENT_TEXT_EL=document.getElementById("total-investiment-text");const TD_TOTAL_INVESTIMENT_FUTURE_EL=document.getElementById("total-investment-future");const TD_MONTHLY_INVESTIMENT_TEXT_EL=document.getElementById("monthly-investment-text");const TD_MONTHLY_INVESTIMENT_EL=document.getElementById("monthly-investment");const TD_MONTHLY_POUPANCA_EL=document.getElementById("monthly-poupanca");const SECTION_INPUTS_EL=document.getElementById("inp-section");const INP_TESOURO_PREFIXADO=document.getElementById("inp-tesouro-prefixado");const INP_TESOURO_SELIC=document.getElementById("inp-tesouro-selic");const INP_TESOURO_IPCA=document.getElementById("inp-tesouro-ipca");const INP_CDB_LC=document.getElementById("inp-cdb-lc");const INP_LCI_LCA=document.getElementById("inp-lci-lca");const INP_POUPANCA=document.getElementById("inp-poupanca");const INP_TESOURO_PREFIXADO_MOB=document.getElementById("inp-tesouro-prefixado-mob");const INP_TESOURO_SELIC_MOB=document.getElementById("inp-tesouro-selic-mob");const INP_TESOURO_IPCA_MOB=document.getElementById("inp-tesouro-ipca-mob");const INP_CDB_LC_MOB=document.getElementById("inp-cdb-lc-mob");const INP_LCI_LCA_MOB=document.getElementById("inp-lci-lca-mob");const INP_POUPANCA_MOB=document.getElementById("inp-poupanca-mob");const GRAPHIC_TESOURO_PRE_TOTAL_VALUE=document.getElementById("rentability-diff-tesouro-pre-total-value");const GRAPHIC_TESOURO_PRE_RENTABILITY_VALUE=document.getElementById("rentability-diff-tesouro-pre-rentability-value");const GRAPHIC_TESOURO_PRE_PERCENT_VALUE=document.getElementById("rentability-diff-tesouro-pre-percent-value");const GRAPHIC_TESOURO_PRE_PERCENT_GRAPHIC=document.getElementById("rentability-diff-tesouro-pre-percent-graphic");const GRAPHIC_TESOURO_SELIC_TOTAL_VALUE=document.getElementById("rentability-diff-tesouro-selic-total-value");const GRAPHIC_TESOURO_SELIC_RENTABILITY_VALUE=document.getElementById("rentability-diff-tesouro-selic-rentability-value");const GRAPHIC_TESOURO_SELIC_PERCENT_VALUE=document.getElementById("rentability-diff-tesouro-selic-percent-value");const GRAPHIC_TESOURO_SELIC_PERCENT_GRAPHIC=document.getElementById("rentability-diff-tesouro-selic-percent-graphic");const GRAPHIC_TESOURO_IPCA_TOTAL_VALUE=document.getElementById("rentability-diff-tesouro-ipca-total-value");const GRAPHIC_TESOURO_IPCA_RENTABILITY_VALUE=document.getElementById("rentability-diff-tesouro-ipca-rentability-value");const GRAPHIC_TESOURO_IPCA_PERCENT_VALUE=document.getElementById("rentability-diff-tesouro-ipca-percent-value");const GRAPHIC_TESOURO_IPCA_PERCENT_GRAPHIC=document.getElementById("rentability-diff-tesouro-ipca-percent-graphic");const GRAPHIC_CDB_TOTAL_VALUE=document.getElementById("rentability-diff-cdb-total-value");const GRAPHIC_CDB_RENTABILITY_VALUE=document.getElementById("rentability-diff-cdb-rentability-value");const GRAPHIC_CDB_PERCENT_VALUE=document.getElementById("rentability-diff-cdb-percent-value");const GRAPHIC_CDB_PERCENT_GRAPHIC=document.getElementById("rentability-diff-cdb-percent-graphic");const GRAPHIC_LCI_TOTAL_VALUE=document.getElementById("rentability-diff-lci-total-value");const GRAPHIC_LCI_RENTABILITY_VALUE=document.getElementById("rentability-diff-lci-rentability-value");const GRAPHIC_LCI_PERCENT_VALUE=document.getElementById("rentability-diff-lci-percent-value");const GRAPHIC_LCI_PERCENT_GRAPHIC=document.getElementById("rentability-diff-lci-percent-graphic");const TYPE_TESOURO_PREFIXADO=1;const TYPE_TESOURO_SELIC=2;const TYPE_TESOURO_IPCA=3;const TYPE_CDB_LC=4;const TYPE_LCA_LCI=5;const BOLSA_FEE=0.003/100;const MONTHS_VALUES=[1,2,3,6,12,24,60,360];const CURRENT_YEAR=new Date().getFullYear();const CDI_FEE=RATE_CDI/100;let state={type:TYPE_TESOURO_SELIC,initiaValue:5000,monthlyValue:000,investimentTime:4};const init=()=>{addEventListeners();fillInputsWithDefaultValues();updateDetails()};const addEventListeners=()=>{INPUT_INVESTIMENT_TIME_EL.addEventListener("input",changeInputInvestimentTime);INPUT_INVESTIMENT_TIME_MOB_EL.addEventListener("input",changeInputInvestimentTime);INPUT_INITIAL_EL.addEventListener("input",changeInputInitial);INPUT_MONTHLY_EL.addEventListener("input",changeInputMonthly);INP_TESOURO_PREFIXADO.addEventListener("input",changeRatePercentInput);INP_TESOURO_SELIC.addEventListener("input",changeRatePercentInput);INP_TESOURO_IPCA.addEventListener("input",changeRatePercentInput);INP_CDB_LC.addEventListener("input",changeRateIntegerInput);INP_LCI_LCA.addEventListener("input",changeRateIntegerInput);INP_POUPANCA.addEventListener("input",changeRatePercentInput);INP_TESOURO_PREFIXADO_MOB.addEventListener("input",changeRatePercentInput);INP_TESOURO_SELIC_MOB.addEventListener("input",changeRatePercentInput);INP_TESOURO_IPCA_MOB.addEventListener("input",changeRatePercentInput);INP_CDB_LC_MOB.addEventListener("input",changeRateIntegerInput);INP_LCI_LCA_MOB.addEventListener("input",changeRateIntegerInput);INP_POUPANCA_MOB.addEventListener("input",changeRatePercentInput)};const changeRatePercentInput=event=>{setTimeout(()=>{const targetValue=event.target.value===''?'0':event.target.value event.target.value=maskPercent(targetValue) updateDetails()})} const changeRateIntegerInput=event=>{setTimeout(()=>{const targetValue=event.target.value event.target.value=targetValue updateDetails()})} const changeInputType=event=>{const value=event.target.value;const investmentType=parseInt(value,10) state={...state,type:investmentType};SECTION_INPUTS_EL.dataset.type=investmentType;updateDetails()};const changeInputInvestimentTime=event=>{const value=event.target.value;const suffix=MONTHS_VALUES[value]===1?" Mês":" Meses";const el=isMobile()?SPAN_INVESTIMENT_TIME_MOB_EL:SPAN_INVESTIMENT_TIME_EL;el.innerHTML=`${MONTHS_VALUES[value]} ${suffix}`;state={...state,investimentTime:parseInt(value,10)};updateDetails()};const changeInputInitial=event=>{if(event.target.tagName!=="INPUT")return;setTimeout(()=>{const targetValue=event.target.value===''?'0':event.target.value const value=parseInt(FormaterUtils.unmask(targetValue));event.target.value=FormaterUtils.maskCurrency(targetValue);state={...state,initiaValue:value};updateDetails()})};const changeInputMonthly=event=>{if(event.target.tagName!=="INPUT")return;setTimeout(()=>{const targetValue=event.target.value===''?'0':event.target.value const value=parseInt(FormaterUtils.unmask(targetValue));event.target.value=FormaterUtils.maskCurrency(targetValue);state={...state,monthlyValue:value};updateDetails()})};const fillInputsWithDefaultValues=()=>{INP_TESOURO_SELIC.value=RATE_SELIC INP_TESOURO_PREFIXADO.value=RATE_TESOURO_PREFIXADO INP_TESOURO_IPCA.value=RATE_TESOURO_IPCA INP_CDB_LC.value=RATE_CDB_LC INP_LCI_LCA.value=RATE_LCA_LCI INP_POUPANCA.value=POUPANCA_RENTABILITY INP_TESOURO_SELIC_MOB.value=RATE_SELIC INP_TESOURO_PREFIXADO_MOB.value=RATE_TESOURO_PREFIXADO INP_TESOURO_IPCA_MOB.value=RATE_TESOURO_IPCA INP_CDB_LC_MOB.value=RATE_CDB_LC INP_LCI_LCA_MOB.value=RATE_LCA_LCI INP_POUPANCA_MOB.value=POUPANCA_RENTABILITY} const getTaxFee=time=>{if(time>=24){return 0.15} if(time>=12&&time<24){return 0.175} if(time>=6&&time<12){return 0.2} if(time<=5){return 0.225}};const getEasynvestTax=(incomeTax,value,allValue,bolsaValue)=>{const finalCapital=parseFloat(allValue)-parseFloat(value)-parseFloat(bolsaValue*100);const DeductedIR=incomeTax*finalCapital;return DeductedIR};const getFinalValue=(allValue,bolsaValue,easynvestTax)=>{const finalValue=parseFloat(allValue)-parseFloat(bolsaValue)-parseFloat(easynvestTax);return finalValue};const getInvesmtentRateByType=type=>{switch(type){case TYPE_TESOURO_IPCA:return parseFloat(isMobile()?INP_TESOURO_IPCA_MOB.value:INP_TESOURO_IPCA.value) case TYPE_TESOURO_PREFIXADO:return parseFloat(isMobile()?INP_TESOURO_PREFIXADO_MOB.value:INP_TESOURO_PREFIXADO.value) case TYPE_TESOURO_SELIC:return parseFloat(isMobile()?INP_TESOURO_SELIC_MOB.value:INP_TESOURO_SELIC.value) case TYPE_CDB_LC:const cdbValue=isMobile()?INP_CDB_LC_MOB.value:INP_CDB_LC.value return parseFloat(parseFloat(cdbValue/100).toFixed(2)) case TYPE_LCA_LCI:const lcaValue=isMobile()?INP_LCI_LCA_MOB.value:INP_LCI_LCA.value return parseFloat(parseFloat(lcaValue/100).toFixed(2))}};const getPoupancaTax=()=>{const value=isMobile()?INP_POUPANCA_MOB.value:INP_POUPANCA.value return value?value/100:0} const getFixSavings=(initialValue,investimentTime)=>{return initialValue*Math.pow(1+getPoupancaTax(),investimentTime)};const getMonthlySavings=(monthlyValue,investimentTime)=>{let finalValue=0;let time=investimentTime;for(let i=time;i>0;i--){i==investimentTime?(finalValue=monthlyValue):(finalValue=finalValue+monthlyValue*(1+getPoupancaTax())**time);time-=1} return finalValue};const calculateCDBFixed=(investimentTime,initiaValue)=>{const incomeTax=getTaxFee(investimentTime);const titleRate=getInvesmtentRateByType(TYPE_CDB_LC);const allValue=initiaValue*(((1+CDI_FEE)**(1/252)-1)*titleRate+1)**(investimentTime*21);const easynvestTax=getEasynvestTax(incomeTax,initiaValue,allValue,0);return getFinalValue(allValue,0,easynvestTax)};const getCDBMonthly=(month,investimentTime)=>{let finalValue=0;for(let i=investimentTime;i>0;i--){finalValue+=calculateCDBFixed(i,month);investimentTime-=1} return finalValue};const calculateCdbAndLC=()=>{const{investimentTime,type,initiaValue,monthlyValue}=state;const time=MONTHS_VALUES[investimentTime];const calculatedFixedValue=calculateCDBFixed(time,initiaValue);const calculatedMonthlyValue=getCDBMonthly(monthlyValue,time);calculateGeneralValuesAndPrint({totalValueEl:GRAPHIC_CDB_TOTAL_VALUE,rentabilityValueEl:GRAPHIC_CDB_RENTABILITY_VALUE,rentabilityPercentValueEl:GRAPHIC_CDB_PERCENT_VALUE,rentabilityPercentGraphicEl:GRAPHIC_CDB_PERCENT_GRAPHIC,investimentTime,type,initiaValue,monthlyValue,calculatedFixedValue,calculatedMonthlyValue,time})};const calculateLciAndLcaFixed=(investimentTime,initiaValue)=>{const time=investimentTime;const titleRate=getInvesmtentRateByType(TYPE_LCA_LCI);return(initiaValue*(((1+CDI_FEE)**(1/252)-1)*titleRate+1)**(time*21))};const getLciAndLcaMonthly=(month,investimentTime)=>{let finalValue=0;for(let i=investimentTime;i>0;i--){finalValue+=calculateLciAndLcaFixed(i,month);investimentTime-=1} return finalValue};const calculateLciAndLca=()=>{const{investimentTime,type,initiaValue,monthlyValue}=state;const time=MONTHS_VALUES[investimentTime];const calculatedFixedValue=calculateLciAndLcaFixed(time,initiaValue);const calculatedMonthlyValue=getLciAndLcaMonthly(monthlyValue,time);calculateGeneralValuesAndPrint({totalValueEl:GRAPHIC_LCI_TOTAL_VALUE,rentabilityValueEl:GRAPHIC_LCI_RENTABILITY_VALUE,rentabilityPercentValueEl:GRAPHIC_LCI_PERCENT_VALUE,rentabilityPercentGraphicEl:GRAPHIC_LCI_PERCENT_GRAPHIC,investimentTime,type,initiaValue,monthlyValue,calculatedFixedValue,calculatedMonthlyValue,time})};const getGeneralValue=({utilDays,value,titleRate,type,time})=>{let allValue;if(type==TYPE_TESOURO_IPCA){allValue=(value*Math.pow((1+titleRate)*(1+INFLATION_IPCA/100),utilDays/252)) return allValue}else if(type==TYPE_TESOURO_PREFIXADO){return value*(1+titleRate)**(time/12)}else{allValue=value*Math.pow(1+titleRate,utilDays/252);return allValue}};const getBolsaValue=({value,allValue,time})=>{const first=(parseInt(time)-1)/365;const second=(parseInt(value)+parseInt(allValue))/2;return BOLSA_FEE*(first*second)};const calculateGeneralFixed=(investimentTime,initiaValue,type)=>{const time=investimentTime;const value=initiaValue;const utilDays=time*21;const totalDays=time*30.42;const titleRate=getInvesmtentRateByType(type)/100;const incomeTax=getTaxFee(time);const allValue=getGeneralValue({utilDays,value,titleRate,type,time});const bolsaValue=getBolsaValue({value,allValue,time:totalDays});const easynvestTax=getEasynvestTax(incomeTax,value,allValue,bolsaValue);return getFinalValue(allValue,bolsaValue*100,easynvestTax)};const getGeneralMonthly=(month,value,type)=>{const titleRate=getInvesmtentRateByType(type)/100;let time=month;let utilDays=time*21;let totalDays=time*30.42;let incomeTax=0;let allValue=0;let bolsaValue=0;let easynvestTax=0;let finalValue=0;for(let i=time;i>0;i--){utilDays=time*21;totalDays=time*30.42;incomeTax=getTaxFee(time);allValue=getGeneralValue({utilDays,value,titleRate,type,time});bolsaValue=getBolsaValue({value,allValue,time:totalDays});easynvestTax=getEasynvestTax(incomeTax,value,allValue,bolsaValue);finalValue=finalValue+getFinalValue(allValue,bolsaValue,easynvestTax);time-=1} return finalValue};const calculateGeneral=type=>{const{investimentTime,initiaValue,monthlyValue}=state;const time=MONTHS_VALUES[investimentTime];const calculatedFixedValue=calculateGeneralFixed(time,initiaValue,type);const calculatedMonthlyValue=getGeneralMonthly(time,monthlyValue,type);let totalValueEl;let rentabilityValueEl;let rentabilityPercentValueEl;let rentabilityPercentGraphicEl;if(type===TYPE_TESOURO_PREFIXADO){totalValueEl=GRAPHIC_TESOURO_PRE_TOTAL_VALUE;rentabilityValueEl=GRAPHIC_TESOURO_PRE_RENTABILITY_VALUE rentabilityPercentValueEl=GRAPHIC_TESOURO_PRE_PERCENT_VALUE rentabilityPercentGraphicEl=GRAPHIC_TESOURO_PRE_PERCENT_GRAPHIC}else if(type===TYPE_TESOURO_SELIC){totalValueEl=GRAPHIC_TESOURO_SELIC_TOTAL_VALUE;rentabilityValueEl=GRAPHIC_TESOURO_SELIC_RENTABILITY_VALUE rentabilityPercentValueEl=GRAPHIC_TESOURO_SELIC_PERCENT_VALUE rentabilityPercentGraphicEl=GRAPHIC_TESOURO_SELIC_PERCENT_GRAPHIC}else if(type===TYPE_TESOURO_IPCA){totalValueEl=GRAPHIC_TESOURO_IPCA_TOTAL_VALUE;rentabilityValueEl=GRAPHIC_TESOURO_IPCA_RENTABILITY_VALUE rentabilityPercentValueEl=GRAPHIC_TESOURO_IPCA_PERCENT_VALUE rentabilityPercentGraphicEl=GRAPHIC_TESOURO_IPCA_PERCENT_GRAPHIC} calculateGeneralValuesAndPrint({totalValueEl,rentabilityValueEl,rentabilityPercentValueEl,rentabilityPercentGraphicEl,investimentTime,type,initiaValue,monthlyValue,calculatedFixedValue,calculatedMonthlyValue,time})};const getInvestmentFinalYear=time=>{const timeParsed=parseInt(time);const actualYearParsed=parseInt(CURRENT_YEAR);if(timeParsed>=6){if(timeParsed/12===0.5){return actualYearParsed+1}else{return timeParsed/12+actualYearParsed}}else{return CURRENT_YEAR}};const calculateGeneralValuesAndPrint=({calculatedFixedValue,calculatedMonthlyValue,time,initiaValue,monthlyValue,totalValueEl,rentabilityValueEl,rentabilityPercentValueEl,rentabilityPercentGraphicEl,})=>{const totalValue=calculatedFixedValue+calculatedMonthlyValue;const savingFixed=getFixSavings(initiaValue,time);const savingMonthly=getMonthlySavings(monthlyValue,time);const totalInvestment=monthlyValue*time+initiaValue;const absoluteValue=totalValue-totalInvestment;const absoluteSaving=savingFixed+savingMonthly-totalInvestment;let percent=((absoluteValue*100)/absoluteSaving)-100 if(percent===Infinity||percent<0)percent=0 printPercent({percent,rentabilityPercentValueEl,rentabilityPercentGraphicEl,}) printValues({totalValueEl,rentabilityValueEl,totalValue,totalInvestment,absoluteValue,})};const printPercent=({percent,rentabilityPercentValueEl,rentabilityPercentGraphicEl})=>{if(!isNaN(percent)){const value=percent.toFixed(2);if(rentabilityPercentGraphicEl)rentabilityPercentGraphicEl.style.height=`${value > 190 ? 190 : value}px` if(rentabilityPercentValueEl)rentabilityPercentValueEl.innerHTML="+ "+parseDotToComma(percent.toFixed(2))+"%"}} const printValues=({totalValueEl,rentabilityValueEl,totalValue,totalInvestment,absoluteValue,})=>{const totalTextEl=isMobile()?TD_TOTAL_INVESTIMENT_MOB_EL:TD_TOTAL_INVESTIMENT_EL;totalTextEl.innerHTML=`R$ ${parseToString(totalInvestment)}`;if(totalValueEl)totalValueEl.innerHTML=parseToString(totalValue);if(rentabilityValueEl)rentabilityValueEl.innerHTML=parseToString(absoluteValue)};const updateDetails=()=>{calculateCdbAndLC();calculateLciAndLca();calculateGeneral(TYPE_TESOURO_IPCA);calculateGeneral(TYPE_TESOURO_PREFIXADO);calculateGeneral(TYPE_TESOURO_SELIC)};const parseToString=value=>`${value .toFixed(2) .replace(".", ",") .replace(/(\d)(?=(\d{3})+\,)/g, "$1.")}`;const parseDotToComma=value=>value.toString().replace(/\./g,",");const clearActive=event=>{const optionsMenu=document.querySelectorAll(".form-group-radio");optionsMenu.forEach(opt=>{opt.classList.remove("active")});event.classList.add("active")};const maskPercent=value=>{const PERCENT_FORMATER=new Intl.NumberFormat('en-US',{style:'decimal',minimumFractionDigits:2,maximumFractionDigits:2});if(!value)return PERCENT_FORMATER.format(0) const unmaskedNumber=FormaterUtils.unmask(value) const formatedValue=unmaskedNumber==='NaN'?PERCENT_FORMATER.format(0):PERCENT_FORMATER.format(unmaskedNumber) return formatedValue.replace(/,/g,'')} const isMobile=()=>window.screen.width<=1010 init()