Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions src/controllers/fichas-tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ function formataDataSaida(data) {
.format('D/M/YYYY');
}

function formataDataIdentificacao(dia, mes, ano, arrayRomanos) {
if (!dia || !mes || !ano) {
return '';
}
return `${dia}/${arrayRomanos[mes - 1]}/${ano}`;
}

function parseJsonSafe(str) {
try {
const parsed = JSON.parse(str);
if (parsed && typeof parsed === 'object') {
return parsed; // existe e é um objeto/array
}
return null; // pode ser null, number, string etc.
} catch {
return null; // inválido
}
}

export default function fichaTomboController(request, response, next) {
const { tombo_id: tomboId } = request.params;
const { qtd, code } = request.query;
Expand All @@ -53,7 +72,6 @@ export default function fichaTomboController(request, response, next) {
.then(() => {
const include = [
{
required: true,
model: Coletor,
},
{
Expand Down Expand Up @@ -99,19 +117,16 @@ export default function fichaTomboController(request, response, next) {
},
{
as: 'local_coleta',
required: true,
model: LocalColeta,
include: [
{
required: false,
model: Cidade,
include: {
required: true,
model: Estado,
attributes: ['id', 'nome', 'sigla', 'codigo_telefone', 'pais_id'],
include: {
as: 'pais',
required: true,
model: Pais,
},
},
Expand Down Expand Up @@ -201,7 +216,8 @@ export default function fichaTomboController(request, response, next) {
.then(resultado => {
const { tombo, identificacao, fotos } = resultado;

const coletores = `${tombo.coletore.nome}${tombo.coletor_complementar ? tombo.coletor_complementar.complementares : ''}`;
// eslint-disable-next-line max-len
const coletores = `${!!tombo?.coletore?.nome !== false ? tombo?.coletore?.nome : ''}${tombo?.coletor_complementar ? tombo.coletor_complementar?.complementares : ''}`;

const localColeta = tombo.local_coleta;
const cidade = localColeta.cidade || '';
Expand All @@ -211,8 +227,18 @@ export default function fichaTomboController(request, response, next) {
const romanos = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'];
const dataTombo = new Date(tombo.data_tombo);
const romanoDataTombo = (`${dataTombo.getDate()}/${romanos[dataTombo.getMonth()]}/${dataTombo.getFullYear()}`);

const jsonParaIdentificacao = parseJsonSafe(identificacao?.tombo_json);
let temDataIdentificacao = !jsonParaIdentificacao && false;
const romanoDataIdentificacao = formataDataIdentificacao(
jsonParaIdentificacao?.data_identificacao_dia,
jsonParaIdentificacao?.data_identificacao_mes,
jsonParaIdentificacao?.data_identificacao_ano,
romanos
);
temDataIdentificacao = romanoDataIdentificacao !== '';

// eslint-disable-next-line max-len
const romanoDataIdentificacao = (`${identificacao.data_identificacao_dia}/${romanos[identificacao.data_identificacao_mes - 1]}/${identificacao.data_identificacao_ano}`);
const romanoDataColeta = (`${tombo.data_coleta_dia}/${romanos[tombo.data_coleta_mes - 1]}/${tombo.data_coleta_ano}`);

const parametros = {
Expand All @@ -234,8 +260,8 @@ export default function fichaTomboController(request, response, next) {
variedade: tombo.variedade,
subespecie: tombo.sub_especy,

relevo: tombo.relevo?.nome || '',
vegetacao: tombo.vegetaco?.nome || '',
relevo: tombo?.relevo?.nome || '',
vegetacao: tombo?.vegetaco?.nome || '',

familia: tombo.familia,
imprimir: request.params.imprimir_cod,
Expand All @@ -251,13 +277,18 @@ export default function fichaTomboController(request, response, next) {
},

localColeta,
romano_data_coleta: romanoDataColeta,

cidade,
estado,
estado_sigla: estado.sigla,
pais,

romano_data_tombo: romanoDataTombo,
romano_data_identificacao: romanoDataIdentificacao,
romano_data_coleta: romanoDataColeta,

romano_data_identificacao: romanoDataIdentificacao, // Data de identificação. Se não existir, será uma string vazia
tem_data_identificacao: temDataIdentificacao, // Chave que diz se a data de identificação existe e deve ser exibida

numero_copias: qtd || 1,
codigo_barras_selecionado: code,
};
Expand Down
4 changes: 3 additions & 1 deletion src/views/ficha-tombo.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@
<b style="font-size: 14px;">Identificador:</b> <%- identificacao.usuario.nome %>
</td>
<td>
<b style="font-size: 14px;">Data:</b> <%- romano_data_identificacao %>
<% if (tem_data_identificacao) { %>
<b style="font-size: 14px;">Data:</b> <%- romano_data_identificacao %>
<% } %>
</td>
</tr>
<% } %>
Expand Down
Loading