Skip to content

Instantly share code, notes, and snippets.

@uqmessias
Last active October 25, 2023 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uqmessias/09ec7baa16186a22a575c1c275108ef0 to your computer and use it in GitHub Desktop.
Save uqmessias/09ec7baa16186a22a575c1c275108ef0 to your computer and use it in GitHub Desktop.
const SALARIO_MINIMO_MENSAL = 1320;
/**
* @param {number} valor
* @param {number} casaDecimais (para o valor `10.2345678`, usar 0 => 10, 1 => 10.2, 2 => 10.23, 3 => 10.235)
*/
function arredondar(valor, casasDecimais) {
const multiplicador = Math.pow(10, casasDecimais);
return Math.round(valor * multiplicador) / multiplicador;
}
/**
* @param {number} salarioBruto
*/
function INSS(salarioBruto) {
/*
* https://www.gov.br/inss/pt-br/direitos-e-deveres/inscricao-e-contribuicao/tabela-de-contribuicao-mensal
* até R$ 1.320,00 (7,5%)
* de R$ 1.320,01 a R$ 2.571,29 (9%)
* de R$ 2.571,30 a R$ 3.856,94 (12%)
* de R$ 3.856,95 a R$ 7.507,49 (14%)
*/
const faixasINSS = [
[0.00, 1320.00, 7.5 / 100],
[1320.01, 2571.29, 9.0 / 100],
[2571.30, 3856.94, 12.0 / 100],
[3856.95, 7507.49, 14.0 / 100]
];
const inss = faixasINSS.reduce(
(somaINSS, [inicial, final, percentual]) => {
if (salarioBruto >= inicial) {
const faixaFinal = final >= salarioBruto ? salarioBruto : final;
return somaINSS + (faixaFinal - inicial) * percentual;
}
return somaINSS;
}, 0);
return arredondar(inss, 2);
}
/**
* @param {number} salarioBruto
* @param {number} inss
* @param {number} dependentes
* @return {number}
*/
function IRRF(salarioBruto, inss, dependentes) {
/*
* https://www.gov.br/receitafederal/pt-br/assuntos/meu-imposto-de-renda/tabelas/2023
* até R$ 2.112,00 (0%)
* de R$ 2.112,01 a R$ 2.826,65 (7,5% - R$ 158,40)
* de R$ 2.826,66 a R$ 3.751,05 (15% - R$ 370,40)
* de R$ 3.751,06 a R$ 4.664,68 (22,5% - R$ 651,73)
* a partir de R$ 4.664,69 (27,5% - R$ 884,96)
*/
const faixasIRRF = [
[0.00, 2112.00, 0 / 100, 0.00],
[2112.01, 2826.65, 7.5 / 100, 158.40],
[2826.66, 3751.05, 15.0 / 100, 370.40],
[3751.06, 4664.68, 22.5 / 100, 651.73],
[4664.69, Infinity, 27.5 / 100, 884.96]
];
const descontoPorDependente = 189.59;
if (inss === undefined) {
inss = INSS(salarioBruto);
}
const descontoPorTodosDepentendes = (typeof dependentes === 'number' ? dependentes : 0) * descontoPorDependente;
const baseDeCalculo = salarioBruto - inss - descontoPorTodosDepentendes;
const [, , percentual, deducao] = faixasIRRF.reverse().find(([inicial, final]) => inicial <= baseDeCalculo && baseDeCalculo <= final);
const irrf = baseDeCalculo * percentual - deducao;
return arredondar(irrf, 2);
}
/**
* Calcula o melhor pró-labore para economizar em impostos e aumentar a contribuição no INSS
*
* @param {number} faturamentoBrutoMensal
* @param {number} dependentesAtuais
* @param {boolean} doSegundoSocio
* @returns {boolean} retorna o melhor pró-labore para o primeiro ou segundo sócio
*/
function PROLABORE_COM_MENOR_IMPOSTO(faturamentoBrutoMensal, dependentesAtuais, doSegundoSocio) {
const salarioBrutoMensalNoFatorR = faturamentoBrutoMensal * 28 / 100;
/**
* SSBMNFR SMM Nº TESTES
* 8 2 4
* 9 2 4
* 12 1 6
*
* Nº TESTES PRO-LABORE1 PRO-LABORE2
* 1 8 2
* 2 7 3
* 3 6 4
* 4 5 5
*
*
* Nº TESTES PRO-LABORE1 PRO-LABORE2
* 1 9 2
* 2 8 3
* 3 7 4
* 4 6 5
*
*
* Nº TESTES PRO-LABORE1 PRO-LABORE2
* 1 12 1
* 2 11 2
* 3 10 3
* 4 9 4
* 5 8 5
* 6 7 6
*/
const numeroDeTestes = Math.floor(((salarioBrutoMensalNoFatorR + 1) - SALARIO_MINIMO_MENSAL) / 2);
const inssSalarioMensaNoFatorR = INSS(salarioBrutoMensalNoFatorR);
const impostoSalarioMensalNoFatorR = IRRF(salarioBrutoMensalNoFatorR, inssSalarioMensaNoFatorR, dependentesAtuais);
/**
* @type {{
* prolabore1: number,
* prolabore2: number,
* inssProlabore1: number,
* inssProlabore2: number,
* somaINSSProlabores: number,
* impostoProlabore1: number,
* impostoProlabore2: number,
* somaImpostosProlabores: number,
* economiaImposto: number,
* diferencaContribuicaoINSS: number,
* economiaImpostoMaisDiferencaContribuicaoINSS: number,
}}
*/
let melhorProlabore;
for (let i = 0; i < numeroDeTestes; i++) {
const prolabore1 = salarioBrutoMensalNoFatorR - i - SALARIO_MINIMO_MENSAL;
const prolabore2 = SALARIO_MINIMO_MENSAL + i;
const inssProlabore1 = INSS(prolabore1)
const inssProlabore2 = INSS(prolabore2)
const somaINSSProlabores = inssProlabore1 + inssProlabore2;
const impostoProlabore1 = IRRF(prolabore1, inssProlabore1, 0);
const impostoProlabore2 = IRRF(prolabore2, inssProlabore2, 0);
const somaImpostosProlabores = impostoProlabore1 + impostoProlabore2;
const economiaImposto = impostoSalarioMensalNoFatorR - somaImpostosProlabores;
const diferencaContribuicaoINSS = somaINSSProlabores - inssSalarioMensaNoFatorR;
const economiaImpostoMaisDiferencaContribuicaoINSS = economiaImposto + diferencaContribuicaoINSS;
const prolaboreAtual = {
prolabore1,
prolabore2,
inssProlabore1,
inssProlabore2,
somaINSSProlabores,
impostoProlabore1,
impostoProlabore2,
somaImpostosProlabores,
economiaImposto,
diferencaContribuicaoINSS,
economiaImpostoMaisDiferencaContribuicaoINSS,
};
if (melhorProlabore === undefined || melhorProlabore.economiaImpostoMaisDiferencaContribuicaoINSS <= economiaImpostoMaisDiferencaContribuicaoINSS) {
melhorProlabore = prolaboreAtual;
}
}
if (melhorProlabore === undefined) {
throw new Error(`Não foi possível calcular o melhor pró-labore. Verifique os valores in formados: "${JSON.stringify({
faturamentoBrutoMensal,
dependentesAtuais,
doSegundoSocio,
salarioBrutoMensalNoFatorR,
numeroDeTestes,
inssSalarioMensaNoFatorR,
impostoSalarioMensalNoFatorR,
})}"`)
}
return doSegundoSocio ? melhorProlabore.prolabore1 : melhorProlabore.prolabore2;
}
/**
* @param {number} salarioBruto
* @param {number} dependentes
*/
function salario_liquido(salarioBruto, dependentes) {
const inss = INSS(salarioBruto);
const irrf = IRRF(salarioBruto, inss, dependentes);
return salarioBruto - inss - irrf;
}
/**
* @param {number} faturamentoBruto12Meses
* @param {boolean} usarFatorR
* @param {boolean} ehExportacao
* @param {boolean} excluirDescontosDoProlabore
* @return {number}
*/
function IMPOSTO_ANUAL_SIMPLES_NACIONAL(faturamentoBruto12Meses, usarFatorR, ehExportacao, excluirDescontosDoProlabore) {
/**
* https://www.contabilizei.com.br/contabilidade-online/anexo-5-simples-nacional/
* Faixa Alíquota Valor a Deduzir (em R$) Receita Bruta em 12 Meses (em R$)
* 1a Faixa 15,50% – Até 180.000,00
* 2a Faixa 18,00% 4.500,00 De 180.000,01 a 360.000,00
* 3a Faixa 19,50% 9.900,00 De 360.000,01 a 720.000,00
* 4a Faixa 20,50% 17.100,00 De 720.000,01 a 1.800.000,00
* 5a Faixa 23,00% 62.100,00 De 1.800.000,01 a 3.600.000,00
* 6a Faixa 30,50% 540.000,00 De 3.600.000,01 a 4.800.000,00
*/
const faixasSimplesNacionalAnexoVPorAno = [
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 28,85% 14,00% 15,00% 25,00% 14,10% 1a Faixa 3,05%
*/
valorInicial: 0.00,
valorFinal: 180000,
aliquotaNominal: 15.5 / 100,
deducao: 0,
aliquotaIss: 14.00 / 100,
aliquotaCofins: 14.10 / 100,
aliquotaPis: 3.05 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 27,85% 17,00% 15,00% 23,00% 14,10% 2a Faixa 3,05%
*/
valorInicial: 180000.01,
valorFinal: 360000,
aliquotaNominal: 18.0 / 100,
deducao: 4500,
aliquotaIss: 17.00 / 100,
aliquotaCofins: 14.10 / 100,
aliquotaPis: 3.05 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 23,85% 19,00% 15,00% 24,00% 14,92% 3a Faixa 3,23%
*/
valorInicial: 360000.01,
valorFinal: 720000,
aliquotaNominal: 19.5 / 100,
deducao: 9900,
aliquotaIss: 19.00 / 100,
aliquotaCofins: 14.92 / 100,
aliquotaPis: 3.23 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 23,85% 21,00% 15,00% 21,00% 15,74% 4a Faixa 3,41%
*/
valorInicial: 720000.01,
valorFinal: 1800000,
aliquotaNominal: 20.5 / 100,
deducao: 17100,
aliquotaIss: 21.00 / 100,
aliquotaCofins: 15.74 / 100,
aliquotaPis: 3.41 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 23,85% 23,50% 12,50% 23,00% 14,10% 5a Faixa 3,05%
*/
valorInicial: 1800000.01,
valorFinal: 3600000,
aliquotaNominal: 23.0 / 100,
deducao: 62100,
aliquotaIss: 23.50 / 100,
aliquotaCofins: 14.10 / 100,
aliquotaPis: 3.05 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 29,50% – 15,50% 35,00% 16,44% 6a Faixa 3,56%
*/
valorInicial: 3600000.01,
valorFinal: 4800000,
aliquotaNominal: 30.5 / 100,
deducao: 540000,
aliquotaIss: 23.00 / 100,
aliquotaCofins: 16.44 / 100,
aliquotaPis: 3.56 / 100,
}
];
//
/**
* https://www.contabilizei.com.br/contabilidade-online/anexo-3-simples-nacional/
* Faixa Alíquota Valor a Deduzir (em R$) Receita Bruta em 12 Meses (em R$)
* 1a Faixa 6,00% – Até 180.000,00
* 2a Faixa 11,20% 9.360,00 De 180.000,01 a 360.000,00
* 3a Faixa 13,50% 17.640,00 De 360.000,01 a 720.000,00
* 4a Faixa 16,00% 35.640,00 De 720.000,01 a 1.800.000,00
* 5a Faixa 21,00% 125.640,00 De 1.800.000,01 a 3.600.000,00
* 6a Faixa 33,00% 648.000,00 De 3.600.000,01 a 4.800.000,00
*/
const faixasSimplesNacionalAnexoIIIPorAno = [
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 43,40% 33,50% 3,50% 4,00% 12,82% 1a Faixa 2,78%
*/
valorInicial: 0.00,
valorFinal: 180000,
aliquotaNominal: 6.0 / 100,
deducao: 0,
aliquotaIss: 33.50 / 100,
aliquotaCofins: 12.82 / 100,
aliquotaPis: 2.78 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 43,40% 32,00% 3,50% 4,00% 14,05% 2a Faixa 3,05%
*/
valorInicial: 180000.01,
valorFinal: 360000,
aliquotaNominal: 11.2 / 100,
deducao: 9360,
aliquotaIss: 32.00 / 100,
aliquotaCofins: 14.05 / 100,
aliquotaPis: 3.05 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 43,40% 32,50% 3,50% 4,00% 13,64% 3a Faixa 2,96%
*/
valorInicial: 360000.01,
valorFinal: 720000,
aliquotaNominal: 13.5 / 100,
deducao: 17640,
aliquotaIss: 32.50 / 100,
aliquotaCofins: 13.64 / 100,
aliquotaPis: 2.96 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 43,40% 32,50% 3,50% 4,00% 13,64% 4a Faixa 2,96%
*/
valorInicial: 720000.01,
valorFinal: 1800000,
aliquotaNominal: 16.0 / 100,
deducao: 35640,
aliquotaIss: 32.50 / 100,
aliquotaCofins: 13.64 / 100,
aliquotaPis: 2.96 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 43,40% 33,50% (*) 3,50% 4,00% 12,82% 5a Faixa 2,78%
*/
valorInicial: 1800000.01,
valorFinal: 3600000,
aliquotaNominal: 21.0 / 100,
deducao: 125640,
aliquotaIss: 33.50 / 100,
aliquotaCofins: 12.82 / 100,
aliquotaPis: 2.78 / 100,
},
{
/**
* CPP ISS CSLL IRPJ Cofins Faixas PIS/Pasep
* 30,50% - 15,00% 35,00% 16,03% 6a Faixa 3,47%
*/
valorInicial: 3600000.01,
valorFinal: 4800000,
aliquotaNominal: 33.0 / 100,
deducao: 648000,
aliquotaIss: 33.50 / 100,
aliquotaCofins: 16.03 / 100,
aliquotaPis: 3.47 / 100,
}
];
const salarioFatorR = arredondar(faturamentoBruto12Meses / 12 * (28 / 100), 2);
const forcarUsoDoFatorR = !!usarFatorR || SALARIO_MINIMO_MENSAL >= salarioFatorR;
const faixasSimplesNacional = forcarUsoDoFatorR ? faixasSimplesNacionalAnexoIIIPorAno : faixasSimplesNacionalAnexoVPorAno;
const faixaSimplesNacional = faixasSimplesNacional.find(
({ valorInicial, valorFinal }) => valorInicial <= faturamentoBruto12Meses && faturamentoBruto12Meses <= valorFinal
);
if (!faixaSimplesNacional) {
throw new Error('Este faturamento não se enquadrano Simples Nacional')
}
let { aliquotaNominal, deducao, aliquotaIss, aliquotaCofins, aliquotaPis } = faixaSimplesNacional;
let cofinsIssEPis = aliquotaIss + aliquotaCofins + aliquotaPis;
let aliquotaNominalExportacao = aliquotaNominal * (1 - cofinsIssEPis);
let aliquotaEfetivaExportacao = (faturamentoBruto12Meses * aliquotaNominalExportacao - deducao) / faturamentoBruto12Meses;
/**
* Quando a alíquota efetiva for superior a 14.92537% (0.1492537) e estiver no anexo III,
* a parte do ISS dentro da alíquota nominal é de 5% (0.05) e o que sobrar do ISS
* deve ser dividido entre os outros 5 impostos (CPP, CSLL, IRPJ, Cofins e PIS/Pasep).
*/
if (aliquotaEfetivaExportacao >= 0.1492537 && forcarUsoDoFatorR) {
const restoAliquotaIssParaDividirEntreOsOutros5Impostos = aliquotaIss - 5.0 / 100;
const aliquotaParaAdicionarNosOutrosImpostos = restoAliquotaIssParaDividirEntreOsOutros5Impostos / 5;
/**
* Para o nosso caso, como precisamos só calcular a alíquota efetiva de exportação, só vamos distribuir
* a parte proporcional para o PIS e COFINS.
*/
aliquotaCofins *= aliquotaParaAdicionarNosOutrosImpostos;
aliquotaPis *= aliquotaParaAdicionarNosOutrosImpostos;
cofinsIssEPis = aliquotaIss + aliquotaCofins + aliquotaPis;
aliquotaNominalExportacao = aliquotaNominal * (1 - cofinsIssEPis);
aliquotaEfetivaExportacao = (faturamentoBruto12Meses * aliquotaNominalExportacao - deducao) / faturamentoBruto12Meses;
}
const salarioBaseMensal = forcarUsoDoFatorR ? salarioFatorR : SALARIO_MINIMO_MENSAL;
const inssMensal = INSS(salarioBaseMensal);
const irrfMensal = IRRF(salarioBaseMensal);
const impostoFaturamento12Meses = !!ehExportacao ? faturamentoBruto12Meses * aliquotaEfetivaExportacao : faturamentoBruto12Meses * aliquotaNominal - deducao;
const impostoMensalFaturamento12Meses = impostoFaturamento12Meses / 12;
const impostoMensalSimplesNacional = (excluirDescontosDoProlabore ? 0 : inssMensal + irrfMensal) + impostoMensalFaturamento12Meses;
const impostoAnualSimplesNacional = impostoMensalSimplesNacional * 12;
return arredondar(impostoAnualSimplesNacional, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment