All files / source/reports deposit.js

100% Statements 32/32
91.3% Branches 21/23
100% Functions 4/4
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                1x     1x       1x       1x       1x       1x       1x       1x       1x       1x           1x                                     16x   1x                                                           1x 16x   1x 2x 2x   2x   2x 32x   32x                     20x 9x 11x 9x       32x 32x 4x     2x      
import moment from 'moment-timezone';
import { head } from 'lodash';
import I18n from '../../helpers/i18n';
import DepositDao from '../../database/dao/deposit';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
// Funcao criada para popular o array headers e rows enviados por parametro
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const where = mountWhereBasedFilter(filters);
 
  // Introduzir colunas booleanas
  columns.push({
    label: I18n.t('BEE571' /* Revenda */, undefined, appLanguage),
    value: 'resale',
  });
  columns.push({
    label: I18n.t('BEE36' /* Recebimento */, undefined, appLanguage),
    value: 'inbound',
  });
  columns.push({
    label: I18n.t('BEE572' /* Inventário */, undefined, appLanguage),
    value: 'inventory',
  });
  columns.push({
    label: I18n.t('BEE176' /* Divergência */, undefined, appLanguage),
    value: 'divergence',
  });
  columns.push({
    label: I18n.t('BEE3538' /* Rejeitados */, undefined, appLanguage),
    value: 'rejected',
  });
  columns.push({
    label: I18n.t('BEE573' /* Avaria */, undefined, appLanguage),
    value: 'damage',
  });
  columns.push({
    label: I18n.t('BEE574' /* Qualidade */, undefined, appLanguage),
    value: 'quality',
  });
  columns.push({
    label: I18n.t('BEE789' /* Sobra */, undefined, appLanguage),
    value: 'spare',
  });
  columns.push({
    label: I18n.t('BEE171' /* Aloca Saldo */, undefined, appLanguage),
    value: 'allocateBalance',
  });
 
  // Reorganização da ordem de exibição das colunas na planilha
  columns = [
    columns[0],
    columns[1],
    columns[2],
    columns[7],
    columns[8],
    columns[9],
    columns[10],
    columns[11],
    columns[12],
    columns[13],
    columns[14],
    columns[15],
    columns[3],
    columns[4],
    columns[5],
    columns[6],
  ];
 
  const headersValue = columns.map((it) => it.value);
 
  const dados = await DepositDao.findDeposits(where, {
    attributes: [
      'resale',
      'inbound',
      'inventory',
      'divergence',
      'rejected',
      'damage',
      'quality',
      'spare',
      'allocateBalance',
      ...headersValue,
    ],
    include: [
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
    ],
    ...opts,
  });
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
  dados.forEach((it) => {
    it.createdUser = it.userCreated.dataValues.login;
    it.updatedUser = it.userUpdated.dataValues.login;
 
    const row = [];
 
    for (let index = 0; index < headersValue.length; index++) {
      const column = headersValue[index];
 
      if (
        column.indexOf('resale') > -1 ||
        column.indexOf('inbound') > -1 ||
        column.indexOf('inventory') > -1 ||
        column.indexOf('divergence') > -1 ||
        column.indexOf('rejected') > -1 ||
        column.indexOf('damage') > -1 ||
        column.indexOf('quality') > -1 ||
        column.indexOf('spare') > -1 ||
        column.indexOf('allocateBalance') > -1
      ) {
        if (it[column] == true) {
          it[column] = I18n.t('BEE172' /* Sim */, undefined, appLanguage);
        } else if (it[column] == false) {
          it[column] = I18n.t('BEE173' /* Não */, undefined, appLanguage);
        }
      }
 
      row.push(it[column]);
      if (column === 'createdAt' || column === 'updatedAt') {
        row[index] = moment(row[index]).format('l LTS');
      }
    }
    rows.push(row);
  });
}