All files / source/reports productProducts.js

100% Statements 41/41
100% Branches 46/46
100% Functions 4/4
100% Lines 39/39

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                                  8x 46x   8x                                                 8x 46x     8x 8x 8x 8x 8x     8x       8x       8x 3x 5x 1x         4x 1x 3x 1x           8x 3x 5x 1x 4x 1x         3x 1x         2x 1x             8x 8x 46x 46x 46x 6x   46x                       9x     8x      
/* eslint-disable no-param-reassign */
import moment from 'moment-timezone';
import ProductDao from '../../database/dao/product';
import { t } from '../../helpers/i18n';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
// 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);
  const headersValue = columns.map((it) => it.value);
 
  const dadosProducts = await ProductDao.findProducts(where, {
    attributes: headersValue,
    include: [
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.UnitMeasure,
        as: 'unit',
        attributes: ['name'],
      },
    ],
    ...opts,
  });
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  dadosProducts.forEach((it) => {
    it.createdUser = it.userCreated.login;
    it.updatedUser = it.userUpdated.login;
    it.unitMeasure = it.unit ? it.unit.name : '';
    it.controlExpirationDate = it.controlExpirationDate
      ? t('BEE172' /* Sim */, undefined, appLanguage)
      : t('BEE173' /* Não */, undefined, appLanguage);
    it.status =
      it.status === 1
        ? t('BEE499' /* Ativo */, undefined, appLanguage)
        : t('BEE498' /* Obsoleto */, undefined, appLanguage);
    it.origin =
      it.origin === 1
        ? t('BEE508' /* Mercado Interno */, undefined, appLanguage)
        : t('BEE509' /* Mercado Externo */, undefined, appLanguage);
    if (it.stockControlType === 1) {
      it.stockControlType = t('BEE500' /* Serial */, undefined, appLanguage);
    } else if (it.stockControlType === 2) {
      it.stockControlType = t(
        'BEE501' /* Número Série */,
        undefined,
        appLanguage
      );
    } else if (it.stockControlType === 3) {
      it.stockControlType = t('BEE428' /* Lote */, undefined, appLanguage);
    } else if (it.stockControlType === 4) {
      it.stockControlType = t(
        'BEE502' /* Referência */,
        undefined,
        appLanguage
      );
    }
    if (it.productControlType === 1) {
      it.productControlType = t('BEE503' /* Físico */, undefined, appLanguage);
    } else if (it.productControlType === 2) {
      it.productControlType = t('BEE504' /* Total */, undefined, appLanguage);
    } else if (it.productControlType === 3) {
      it.productControlType = t(
        'BEE505' /* Consignado */,
        undefined,
        appLanguage
      );
    } else if (it.productControlType === 4) {
      it.productControlType = t(
        'BEE506' /* Débito Direto */,
        undefined,
        appLanguage
      );
    } else if (it.productControlType === 5) {
      it.productControlType = t(
        'BEE507' /* Específico */,
        undefined,
        appLanguage
      );
    }
 
    const row = [];
    for (let index = 0; index < headersValue.length; index++) {
      const column = headersValue[index];
      row.push(it[column]);
      if (column === 'createdAt' || column === 'updatedAt') {
        row[index] = moment(row[index]).format('l LTS');
      }
      if (
        (column === 'volume' ||
          column === 'height' ||
          column === 'width' ||
          column === 'length' ||
          column === 'grossWeight' ||
          column === 'netWeight' ||
          column === 'minimalLeftOver' ||
          column === 'conversionFactor' ||
          column === 'multipleSale') &&
        row[index] !== null
      ) {
        row[index] = NumberFormatter.format(row[index], 4);
      }
    }
    rows.push(row);
  });
}