normativeJsonNfse method

String normativeJsonNfse(
  1. Nfse? nfse, {
  2. bool mostrarMoeda = true,
})

Gera o JSON normativo para impressão da NFSe.

nfse - Dados da NFSe que serão processados. mostrarMoeda - Define se o símbolo da moeda deve ser exibido. Valor padrão é true.

Retorna uma string JSON compatível com printer_gateway.

Implementation

String normativeJsonNfse(Nfse? nfse, {bool mostrarMoeda = true}) {
  String moeda = (mostrarMoeda == true) ? 'R\$' : '';
  final helper = JsonPrinterHelper();

  List<Map> nfseJson = [];

  // Cabeçalho - Dados do Prestador (Emitente)
  if (nfse?.emit != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 1,
        bold: true,
        fontSize: 18,
        italic: false,
        content: DanfeUtils.removeAcentos(nfse!.emit!.xNome ?? ''),
      ),
    );

    nfseJson.add(
      helper.prepareLine(
        aligment: 1,
        bold: false,
        fontSize: 12,
        italic: false,
        content: 'CNPJ - ${DanfeUtils.formatCNPJ(nfse.emit!.cnpj ?? '')}',
      ),
    );

    if (nfse.emit!.enderNac != null) {
      final endereco = nfse.emit!.enderNac!;
      final String uf = endereco.uf == null ? '' : ' - ${endereco.uf}';

      nfseJson.add(
        helper.prepareLine(
          aligment: 1,
          bold: false,
          fontSize: 12,
          italic: false,
          content: DanfeUtils.removeAcentos(
            '${endereco.xLgr ?? ''},${endereco.nro ?? ''} ${endereco.xBairro ?? ''}$uf',
          ),
        ),
      );

      if (endereco.cep != null) {
        nfseJson.add(
          helper.prepareLine(
            aligment: 1,
            bold: false,
            fontSize: 12,
            italic: false,
            content: 'CEP: ${DanfeUtils.formatCep(endereco.cep ?? '')}',
          ),
        );
      }
    }

    if (nfse.emit!.fone != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 1,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'Fone: ${nfse.emit!.fone}',
        ),
      );
    }

    if (nfse.emit!.email != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 1,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'Email: ${nfse.emit!.email}',
        ),
      );
    }
  }

  nfseJson.add(helper.divider());

  // Título do documento
  nfseJson.add(
    helper.prepareLine(
      aligment: 1,
      bold: true,
      fontSize: 14,
      italic: false,
      content: 'NFSe - Nota Fiscal de Servico Eletronica',
    ),
  );

  nfseJson.add(
    helper.prepareLine(
      aligment: 1,
      bold: false,
      fontSize: 12,
      italic: false,
      content: 'NACIONAL',
    ),
  );

  nfseJson.add(helper.divider());

  // Número da NFSe
  if (nfse?.nNFSe != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: true,
        fontSize: 12,
        italic: false,
        content: 'Numero NFSe: ${nfse!.nNFSe}',
      ),
    );
  }

  // Data e hora de processamento
  if (nfse?.dhProc != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: false,
        fontSize: 12,
        italic: false,
        content:
            'Data Processamento: ${DanfeUtils.formatDate(nfse!.dhProc!, dateOnly: false)}',
      ),
    );
  }

  // Ambiente de geração
  if (nfse?.ambGer != null) {
    final ambiente = nfse!.ambGer == '1' ? 'Producao' : 'Homologacao';
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: false,
        fontSize: 12,
        italic: false,
        content: 'Ambiente: $ambiente',
      ),
    );
  }

  // Status
  if (nfse?.cStat != null && nfse?.xLocIncid != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: false,
        fontSize: 12,
        italic: false,
        content: 'Status: ${nfse!.cStat} - ${nfse.xLocIncid}',
      ),
    );
  }

  nfseJson.add(helper.divider());

  // Dados do Prestador
  if (nfse?.infDPS?.prest != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: true,
        fontSize: 12,
        italic: false,
        content: 'DADOS DO PRESTADOR',
      ),
    );

    final prestador = nfse!.infDPS!.prest!;

    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: false,
        fontSize: 12,
        italic: false,
        content: 'CNPJ: ${DanfeUtils.formatCNPJ(prestador.cnpj ?? '')}',
      ),
    );

    nfseJson.add(helper.divider());
  }

  // Dados do Tomador
  if (nfse?.infDPS?.tomador != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: true,
        fontSize: 12,
        italic: false,
        content: 'DADOS DO TOMADOR',
      ),
    );

    final tomador = nfse!.infDPS!.tomador!;

    if (tomador.cnpj != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'CNPJ: ${DanfeUtils.formatCNPJ(tomador.cnpj!)}',
        ),
      );
    } else if (tomador.cpf != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'CPF: ${DanfeUtils.formatCPF(tomador.cpf!)}',
        ),
      );
    }

    if (tomador.xNome != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: DanfeUtils.removeAcentos('Nome: ${tomador.xNome}'),
        ),
      );
    }

    if (tomador.endereco != null) {
      final endereco = tomador.endereco!;
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: DanfeUtils.removeAcentos(
            'Endereco: ${endereco.xLgr ?? ''}, ${endereco.nro ?? ''} ${endereco.xCpl ?? ''} ${endereco.xBairro ?? ''} - ${endereco.cMun ?? ''} ${endereco.uf ?? ''}',
          ),
        ),
      );

      if (endereco.cep != null) {
        nfseJson.add(
          helper.prepareLine(
            aligment: 0,
            bold: false,
            fontSize: 12,
            italic: false,
            content: 'CEP: ${DanfeUtils.formatCep(endereco.cep!)}',
          ),
        );
      }
    }

    nfseJson.add(helper.divider());
  }

  // Dados do Serviço
  if (nfse?.infDPS?.serv?.cServ != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: true,
        fontSize: 12,
        italic: false,
        content: 'DADOS DO SERVICO',
      ),
    );

    final cServ = nfse!.infDPS!.serv!.cServ!;

    if (cServ.xDescServ != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: DanfeUtils.removeAcentos('Descricao: ${cServ.xDescServ}'),
        ),
      );
    }

    if (cServ.cTribNac != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'Codigo Trib. Nacional: ${cServ.cTribNac}',
        ),
      );
    }

    if (cServ.cTribMun != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'Codigo Trib. Municipal: ${cServ.cTribMun}',
        ),
      );
    }

    if (cServ.cNBS != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content: 'CNBS: ${cServ.cNBS}',
        ),
      );
    }

    nfseJson.add(helper.divider());
  }

  // Valores
  if (nfse?.infDPS?.valores?.vServPrest != null) {
    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: true,
        fontSize: 12,
        italic: false,
        content: 'VALORES',
      ),
    );

    final vServPrest = nfse!.infDPS!.valores!.vServPrest!;

    if (vServPrest.vServ != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content:
              'Valor Servicos: ${DanfeUtils.formatMoneyMilhar(vServPrest.vServ!, modeda: 'pt_BR', simbolo: moeda)}',
        ),
      );
    }

    if (vServPrest.vDed != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content:
              'Deducoes: ${DanfeUtils.formatMoneyMilhar(vServPrest.vDed!, modeda: 'pt_BR', simbolo: moeda)}',
        ),
      );
    }

    if (vServPrest.vBC != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: true,
          fontSize: 12,
          italic: false,
          content:
              'Base Calculo: ${DanfeUtils.formatMoneyMilhar(vServPrest.vBC!, modeda: 'pt_BR', simbolo: moeda)}',
        ),
      );
    }

    nfseJson.add(helper.divider());
  }

  // Tributos
  if (nfse?.infDPS?.valores?.trib != null) {
    final trib = nfse!.infDPS!.valores!.trib!;

    nfseJson.add(
      helper.prepareLine(
        aligment: 0,
        bold: true,
        fontSize: 12,
        italic: false,
        content: 'TRIBUTOS',
      ),
    );

    if (trib.tribMun?.pAliq != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content:
              'Percentual da alíquota: ${DanfeUtils.formatMoneyMilhar(trib.tribMun!.pAliq!, modeda: 'pt_BR', simbolo: '')}%',
        ),
      );
    }

    if (trib.totTrib?.vTotTrib != null) {
      nfseJson.add(
        helper.prepareLine(
          aligment: 0,
          bold: false,
          fontSize: 12,
          italic: false,
          content:
              'Total Tributos: ${DanfeUtils.formatMoneyMilhar(trib.totTrib!.vTotTrib!, modeda: 'pt_BR', simbolo: moeda)}',
        ),
      );
    }
    nfseJson.add(helper.divider());
  }

  return json.encode(nfseJson);
}